QDP++
qdp_io.cc
Go to the documentation of this file.
1// -*- C++ -*-
5
6#include "qdp.h"
7#include "qdp_byteorder.h"
8#include <complex>
9
10namespace QDP
11{
12
13 //--------------------------------------------------------------------------------
14 // Text reader object support
16
17 // Propagate status to all nodes
19 {
20 bool s;
21
23 s = getIstream().fail();
24
26 return s;
27 }
28
29 // String reader
30 void TextReader::read(std::string& input)
31 {
32 char *dd_tmp;
33 int lleng;
34
35 // Only primary node can grab string
37 {
38 getIstream() >> input;
39 lleng = input.length() + 1;
40 }
41
42 // First must broadcast size of string
44
45 // Now every node can alloc space for string
46 dd_tmp = new(std::nothrow) char[lleng];
47 if( dd_tmp == 0x0 ) {
48 QDP_error_exit("Unable to allocate dd_tmp in qdp_io.cc\n");
49 }
50
52 memcpy(dd_tmp, input.c_str(), lleng);
53
54 // Now broadcast char array out to all nodes
55 QDPInternal::broadcast((void *)dd_tmp, lleng);
56
57 // All nodes can now grab char array and make a string
58 input = dd_tmp;
59
60 // Clean-up and boogie
61 delete[] dd_tmp;
62 }
63
64 // Readers
65 void TextReader::read(char& input)
66 {
68 }
69 void TextReader::read(int& input)
70 {
71 readPrimitive<int>(input);
72 }
73 void TextReader::read(unsigned int& input)
74 {
76 }
77 void TextReader::read(short int& input)
78 {
80 }
81 void TextReader::read(unsigned short int& input)
82 {
84 }
85 void TextReader::read(long int& input)
86 {
88 }
89 void TextReader::read(unsigned long int& input)
90 {
92 }
93 void TextReader::read(long long int& input)
94 {
96 }
97 void TextReader::read(float& input)
98 {
100 }
101 void TextReader::read(double& input)
102 {
104 }
105 void TextReader::read(bool& input)
106 {
107 readPrimitive<bool>(input);
108 }
109
110 template< typename T>
112 {
114 getIstream() >> input;
115
116 // Now broadcast back out to all nodes
118 }
119
120 // Different bindings for read functions
121 TextReader& operator>>(TextReader& txt, std::string& input)
122 {
123 txt.read(input);
124 return txt;
125 }
126 TextReader& operator>>(TextReader& txt, char& input)
127 {
128 txt.read(input);
129 return txt;
130 }
132 {
133 txt.read(input);
134 return txt;
135 }
136 TextReader& operator>>(TextReader& txt, unsigned int& input)
137 {
138 txt.read(input);
139 return txt;
140 }
141 TextReader& operator>>(TextReader& txt, short int& input)
142 {
143 txt.read(input);
144 return txt;
145 }
146 TextReader& operator>>(TextReader& txt, unsigned short int& input)
147 {
148 txt.read(input);
149 return txt;
150 }
151 TextReader& operator>>(TextReader& txt, long int& input)
152 {
153 txt.read(input);
154 return txt;
155 }
156 TextReader& operator>>(TextReader& txt, unsigned long int& input)
157 {
158 txt.read(input);
159 return txt;
160 }
161 TextReader& operator>>(TextReader& txt, long long int& input)
162 {
163 txt.read(input);
164 return txt;
165 }
166 TextReader& operator>>(TextReader& txt, float& input)
167 {
168 txt.read(input);
169 return txt;
170 }
171 TextReader& operator>>(TextReader& txt, double& input)
172 {
173 txt.read(input);
174 return txt;
175 }
176 TextReader& operator>>(TextReader& txt, bool& input)
177 {
178 txt.read(input);
179 return txt;
180 }
181
182
183 //--------------------------------------------------------------------------------
184 // Text buffer reader support
186
187 TextBufferReader::TextBufferReader(const std::string& s) {open(s);}
188
189 void TextBufferReader::open(const std::string& s)
190 {
192 f.str(s);
193 }
194
195 // Output the stream
197 {
198 std::string s;
199
200 if (Layout::primaryNode())
201 s = f.str();
202
203 return s;
204 }
205
206 // Output the stream
207 std::string TextBufferReader::str() const
208 {
209 std::string s;
210
211 if (Layout::primaryNode())
212 s = f.str();
213
215 return s;
216 }
217
218 // Close
220
221
222 //--------------------------------------------------------------------------------
223 // Text file reader support
225
226 TextFileReader::TextFileReader(const std::string& p) {open(p);}
227
228 void TextFileReader::open(const std::string& p)
229 {
231 f.open(p.c_str(),std::ifstream::in);
232
233 if (! is_open())
234 QDP_error_exit("failed to open file %s",p.c_str());
235 }
236
238 {
239 if (is_open())
240 {
241 if (Layout::primaryNode())
242 f.close();
243 }
244 }
245
246 // Propagate status to all nodes
248 {
249 bool s = QDP_isInitialized();
250
251 if (s)
252 {
254 s = f.is_open();
255
257 }
258
259 return s;
260 }
261
262 // Close
264
265
266 //--------------------------------------------------------------------------------
267 // Text object writer support
269
270 // Propagate status to all nodes
272 {
273 bool s;
274
275 if (Layout::primaryNode())
276 s = getOstream().fail();
277
279 return s;
280 }
281
282 // Different bindings for write functions
283 TextWriter& operator<<(TextWriter& txt, const std::string& output)
284 {
285 txt.write(output);
286 return txt;
287 }
288
289 TextWriter& operator<<(TextWriter& txt, const char* output)
290 {
291 txt.write(output);
292 return txt;
293 }
294
296 {
297 txt.write(output);
298 return txt;
299 }
300
301 TextWriter& operator<<(TextWriter& txt, unsigned int output)
302 {
303 txt.write(output);
304 return txt;
305 }
306
307 TextWriter& operator<<(TextWriter& txt, short int output)
308 {
309 txt.write(output);
310 return txt;
311 }
312
313 TextWriter& operator<<(TextWriter& txt, unsigned short int output)
314 {
315 txt.write(output);
316 return txt;
317 }
318
319 TextWriter& operator<<(TextWriter& txt, long int output)
320 {
321 txt.write(output);
322 return txt;
323 }
324
325 TextWriter& operator<<(TextWriter& txt, unsigned long int output)
326 {
327 txt.write(output);
328 return txt;
329 }
330
331 TextWriter& operator<<(TextWriter& txt, long long int output)
332 {
333 txt.write(output);
334 return txt;
335 }
336
337 TextWriter& operator<<(TextWriter& txt, float output)
338 {
339 txt.write(output);
340 return txt;
341 }
342
343 TextWriter& operator<<(TextWriter& txt, double output)
344 {
345 txt.write(output);
346 return txt;
347 }
348
349 TextWriter& operator<<(TextWriter& txt, bool output)
350 {
351 txt.write(output);
352 return txt;
353 }
354
355
356 void TextWriter::write(const std::string& output)
357 {
359 }
360
361 void TextWriter::write(const char* output)
362 {
363 write(std::string(output));
364 }
365
366 void TextWriter::write(const char& output)
367 {
368 writePrimitive<char>(output);
369 }
370
371 void TextWriter::write(const int& output)
372 {
373 writePrimitive<int>(output);
374 }
375
376 void TextWriter::write(const unsigned int& output)
377 {
379 }
380
381 void TextWriter::write(const short int& output)
382 {
384 }
385
386 void TextWriter::write(const unsigned short int& output)
387 {
389 }
390
391 void TextWriter::write(const long int& output)
392 {
394 }
395
396 void TextWriter::write(const unsigned long int& output)
397 {
399 }
400
401 void TextWriter::write(const long long int& output)
402 {
404 }
405
406 void TextWriter::write(const float& output)
407 {
408 writePrimitive<float>(output);
409 }
410
411 void TextWriter::write(const double& output)
412 {
414 }
415
416 void TextWriter::write(const bool& output)
417 {
418 writePrimitive<bool>(output);
419 }
420
421 template< typename T>
422 void TextWriter::writePrimitive(const T& output)
423 {
425 getOstream() << output;
426 }
427
428
429 //--------------------------------------------------------------------------------
430 // Text buffer writer support
432
433 TextBufferWriter::TextBufferWriter(const std::string& p) {open(p);}
434
435 void TextBufferWriter::open(const std::string& s)
436 {
438 f.str(s);
439 }
440
441 // Output the stream
443 {
444 std::string s;
445
446 if (Layout::primaryNode())
447 s = f.str();
448
449 return s;
450 }
451
452 // Output the stream
453 std::string TextBufferWriter::str() const
454 {
455 std::string s;
456
457 if (Layout::primaryNode())
458 s = f.str();
459
461 return s;
462 }
463
464 // Close
466
467
468 //--------------------------------------------------------------------------------
469 // Text file writer support
471
472 TextFileWriter::TextFileWriter(const std::string& p) {open(p);}
473
474 void TextFileWriter::open(const std::string& p)
475 {
477 f.open(p.c_str(),std::ofstream::out | std::ofstream::trunc);
478
479 if (! is_open())
480 QDP_error_exit("failed to open file %s",p.c_str());
481 }
482
484 {
485 if (is_open())
486 {
487 if (Layout::primaryNode())
488 f.close();
489 }
490 }
491
492 // Propagate status to all nodes
494 {
495 bool s = QDP_isInitialized();
496
497 if (s)
498 {
500 s = f.is_open();
501
503 }
504
505 return s;
506 }
507
509 {
510 if (is_open())
511 {
512 if (Layout::primaryNode())
513 f.flush();
514 }
515 }
516
517 // Close
519
520
521 //--------------------------------------------------------------------------------
522 // Binary reader support
523 // Propagate status to all nodes
525 {
526 bool s;
527
528 if (Layout::primaryNode())
529 s = getIstream().fail();
530
532 return s;
533 }
534
535 // Get the checksum from the binary node to all nodes
537 {
538 // Keep the checksum in sync on all nodes. This only really
539 // is needed if nodes do detailed checks on the checksums
542 internalChecksum() = chk;
543 return chk;
544 }
545
548 {
549 internalChecksum() = 0;
550 }
551
553 std::istream::pos_type BinaryReader::currentPosition()
554 {
555 pos_type pos;
557 pos = getIstream().tellg();
558
560 return pos;
561 }
562
565 {
567 getIstream().seekg(pos);
568
569 internalChecksum() = 0;
570 }
571
574 {
576 getIstream().seekg(off, std::ios_base::beg);
577
578 internalChecksum() = 0;
579 }
580
583 {
585 getIstream().seekg(off, std::ios_base::cur);
586
587 internalChecksum() = 0;
588 }
589
592 {
594 getIstream().seekg(-off, std::ios_base::end);
595
596 internalChecksum() = 0;
597 }
598
601 {
603 getIstream().seekg(0, std::ios_base::beg);
604
605 internalChecksum() = 0;
606 }
607
608
609 // Readers
610 void BinaryReader::readDesc(std::string& input)
611 {
613 {
614 // Get the length
615 int n;
616 readArrayPrimaryNode((char*)&n, sizeof(int), 1);
617
618 // Read
619 char* str = new char[n];
620 readArrayPrimaryNode(str, sizeof(char), n);
621
622 input.assign(str, n);
623
624 delete[] str;
625 }
626
628 }
629
630 void BinaryReader::read(std::string& input, size_t maxBytes)
631 {
632 char *str = new(std::nothrow) char[maxBytes];
633 if( str == 0x0 ) {
634 QDP_error_exit("Couldnt new str in qdp_io.cc\n");
635 }
636
637 size_t n;
638
640 {
641 getIstream().getline(str, maxBytes);
642 n = strlen(str);
643 internalChecksum() = QDPUtil::crc32(internalChecksum(), str, n); // no string terminator
644 ++n;
645 internalChecksum() = QDPUtil::crc32(internalChecksum(), "\n", 1); // account for newline written
646 }
647
649 QDPInternal::broadcast((void *)str, n);
650
651 input = str;
652 delete[] str;
653 }
654
655 void BinaryReader::read(char& input)
656 {
657 readPrimitive<char>(input);
658 }
659 void BinaryReader::read(int& input)
660 {
661 readPrimitive<int>(input);
662 }
663 void BinaryReader::read(unsigned int& input)
664 {
666 }
667 void BinaryReader::read(short int& input)
668 {
670 }
671 void BinaryReader::read(unsigned short int& input)
672 {
674 }
675 void BinaryReader::read(long int& input)
676 {
678 }
679 void BinaryReader::read(unsigned long int& input)
680 {
682 }
683 void BinaryReader::read(long long int& input)
684 {
686 }
687 void BinaryReader::read(float& input)
688 {
690 }
691 void BinaryReader::read(double& input)
692 {
694 }
695 void BinaryReader::read(bool& input)
696 {
697 readPrimitive<bool>(input);
698 }
699
700 template< typename T>
702 {
703 readArray((char*)&input, sizeof(T), 1);
704 }
705
706 void BinaryReader::readArray(char* input, size_t size, size_t nmemb)
707 {
708 readArrayPrimaryNode(input, size, nmemb);
709
710 // Now broadcast back out to all nodes
711 QDPInternal::broadcast((void*)input, size*nmemb);
712 }
713
714 // Read array from the primary node
715 void BinaryReader::readArrayPrimaryNode(char* input, size_t size, size_t nmemb)
716 {
718 {
719 // Read
720 // By default, we expect all data to be in big-endian
721 getIstream().read(input, size*nmemb);
722 internalChecksum() = QDPUtil::crc32(internalChecksum(), input, size*nmemb);
723
724 if (! QDPUtil::big_endian())
725 {
726 // little-endian
727 // Swap
728 QDPUtil::byte_swap(input, size, nmemb);
729 }
730 }
731 }
732
733
734 void BinaryReader::readArrayLittleEndian(char* input, size_t size, size_t nmemb)
735 {
737 {
738 // Read
739 // By default, we expect all data to be in big-endian
740 getIstream().read(input, size*nmemb);
741 // internalChecksum() = QDPUtil::crc32(internalChecksum(), input, size*nmemb);
742
744 {
745 // big-endian
746 // Swap
747 QDPUtil::byte_swap(input, size, nmemb);
748 }
749 }
750 // Now broadcast back out to all nodes
751 QDPInternal::broadcast((void*)input, size*nmemb);
752 }
753
754 void BinaryReader::readArrayPrimaryNodeLittleEndian(char* input, size_t size, size_t nmemb)
755 {
757 {
758 // Read
759 // By default, we expect all data to be in big-endian
760 getIstream().read(input, size*nmemb);
761 // internalChecksum() = QDPUtil::crc32(internalChecksum(), input, size*nmemb);
762
764 {
765 // big-endian
766 // Swap
767 QDPUtil::byte_swap(input, size, nmemb);
768 }
769 }
770 }
771
772
773 // Wrappers for read functions
774 void readDesc(BinaryReader& bin, std::string& input)
775 {
776 bin.readDesc(input);
777 }
778 void read(BinaryReader& bin, std::string& input, size_t maxBytes)
779 {
780 bin.read(input, maxBytes);
781 }
782 void read(BinaryReader& bin, char& input)
783 {
784 bin.read(input);
785 }
786 void read(BinaryReader& bin, int& input)
787 {
788 bin.read(input);
789 }
790 void read(BinaryReader& bin, unsigned int& input)
791 {
792 bin.read(input);
793 }
794 void read(BinaryReader& bin, short int& input)
795 {
796 bin.read(input);
797 }
798 void read(BinaryReader& bin, unsigned short int& input)
799 {
800 bin.read(input);
801 }
802 void read(BinaryReader& bin, long int& input)
803 {
804 bin.read(input);
805 }
806 void read(BinaryReader& bin, unsigned long int& input)
807 {
808 bin.read(input);
809 }
810 void read(BinaryReader& bin, long long int& input)
811 {
812 bin.read(input);
813 }
814 void read(BinaryReader& bin, float& input)
815 {
816 bin.read(input);
817 }
818 void read(BinaryReader& bin, double& input)
819 {
820 bin.read(input);
821 }
822 void read(BinaryReader& bin, bool& input)
823 {
824 bin.read(input);
825 }
826
828 void read(BinaryReader& bin, std::complex<float>& param)
829 {
830 // NOTE: the C++-11 standard says a "real()" function returns a r-value - not an "l-value", so
831 // cannot simply read into a param.real()
832 float re, im;
833 read(bin, re);
834 read(bin, im);
835 param = std::complex<float>(re,im);
836 }
837
839 void read(BinaryReader& bin, std::complex<double>& param)
840 {
841 double re, im;
842 read(bin, re);
843 read(bin, im);
844 param = std::complex<double>(re,im);
845 }
846
847
848 // Different bindings for read functions
850 {
851 read(bin, input);
852 return bin;
853 }
854
856 {
857 read(bin, input);
858 return bin;
859 }
860
861 BinaryReader& operator>>(BinaryReader& bin, unsigned int& input)
862 {
863 read(bin, input);
864 return bin;
865 }
866
867 BinaryReader& operator>>(BinaryReader& bin, short int& input)
868 {
869 read(bin, input);
870 return bin;
871 }
872
873 BinaryReader& operator>>(BinaryReader& bin, unsigned short int& input)
874 {
875 read(bin, input);
876 return bin;
877 }
878
879 BinaryReader& operator>>(BinaryReader& bin, long int& input)
880 {
881 read(bin, input);
882 return bin;
883 }
884
885 BinaryReader& operator>>(BinaryReader& bin, unsigned long int& input)
886 {
887 read(bin, input);
888 return bin;
889 }
890
891 BinaryReader& operator>>(BinaryReader& bin, long long int& input)
892 {
893 read(bin, input);
894 return bin;
895 }
896
898 {
899 read(bin, input);
900 return bin;
901 }
902
904 {
905 read(bin, input);
906 return bin;
907 }
908
910 {
911 read(bin, input);
912 return bin;
913 }
914
915 //--------------------------------------------------------------------------------
916 // Binary buffer reader support
918
919 // Construct from a string
921
923
924 void BinaryBufferReader::open(const std::string& s)
925 {
927 f.str(s);
928 }
929
930 // Output the stream
932 {
933 std::string s;
934
935 if (Layout::primaryNode())
936 s = f.str();
937
938 return s;
939 }
940
941 // Output the stream
942 std::string BinaryBufferReader::str() const
943 {
944 std::string s;
945
946 if (Layout::primaryNode())
947 s = f.str();
948
950 return s;
951 }
952
953 // Clear the stream
955 {
956 if (Layout::primaryNode())
957 f.clear();
958
959 checksum = 0;
960 }
961
962
963
964 //--------------------------------------------------------------------------------
965 // Binary reader support
967
968 BinaryFileReader::BinaryFileReader(const std::string& p) {checksum=0;open(p);}
969
970 void BinaryFileReader::open(const std::string& p)
971 {
972 checksum = 0;
973 if (Layout::primaryNode())
974 f.open(p.c_str(),std::ifstream::in | std::ifstream::binary);
975
976 if (! is_open())
977 QDP_error_exit("BinaryFileReader: error opening file %s",p.c_str());
978 }
979
980 // Close
982 {
983 if (is_open())
984 {
985 if (Layout::primaryNode())
986 f.close();
987 }
988 }
989
990 // Propagate status to all nodes
992 {
993 bool s = QDP_isInitialized();
994
995 if (s)
996 {
998 s = f.is_open();
999
1001 }
1002
1003 return s;
1004 }
1005
1006 // Shutdown
1008
1009
1010 //--------------------------------------------------------------------------------
1011 // Binary writer support
1013
1014 // Propagate status to all nodes
1016 {
1017 bool s;
1018
1019 if (Layout::primaryNode())
1020 s = getOstream().fail();
1021
1023 return s;
1024 }
1025
1028 {
1029 internalChecksum() = 0;
1030 }
1031
1033 std::ostream::pos_type BinaryWriter::currentPosition()
1034 {
1035 pos_type pos;
1036 if (Layout::primaryNode())
1037 pos = getOstream().tellp();
1038
1040 return pos;
1041 }
1042
1045 {
1046 if (Layout::primaryNode())
1047 getOstream().seekp(pos);
1048
1049 internalChecksum() = 0;
1050 }
1051
1054 {
1055 if (Layout::primaryNode())
1056 getOstream().seekp(off, std::ios_base::beg);
1057
1058 internalChecksum() = 0;
1059 }
1060
1063 {
1064 if (Layout::primaryNode())
1065 getOstream().seekp(off, std::ios_base::cur);
1066
1067 internalChecksum() = 0;
1068 }
1069
1072 {
1073 if (Layout::primaryNode())
1074 getOstream().seekp(-off, std::ios_base::end);
1075
1076 internalChecksum() = 0;
1077 }
1078
1081 {
1082 if (Layout::primaryNode())
1083 getOstream().seekp(0, std::ios_base::beg);
1084
1085 internalChecksum() = 0;
1086 }
1087
1088
1089 void BinaryWriter::writeDesc(const std::string& output)
1090 {
1091 size_t n = output.length();
1093 writeArray(output.c_str(), sizeof(char), n);
1094 }
1095
1096 void BinaryWriter::write(const std::string& output)
1097 {
1098 // WARNING: CHECK ON NEWLINE IN CHECKSUM
1099 size_t n = output.length();
1100 writeArray(output.c_str(), sizeof(char), n);
1101 write('\n'); // Convention is to write a line terminator
1102 }
1103
1104 void BinaryWriter::write(const char* output)
1105 {
1106 write(std::string(output));
1107 }
1108 void BinaryWriter::write(const char& output)
1109 {
1110 writePrimitive<char>(output);
1111 }
1112 void BinaryWriter::write(const int& output)
1113 {
1114 writePrimitive<int>(output);
1115 }
1116 void BinaryWriter::write(const unsigned int& output)
1117 {
1119 }
1120 void BinaryWriter::write(const short int& output)
1121 {
1123 }
1124 void BinaryWriter::write(const unsigned short int& output)
1125 {
1127 }
1128 void BinaryWriter::write(const long int& output)
1129 {
1131 }
1132 void BinaryWriter::write(const unsigned long int& output)
1133 {
1135 }
1136 void BinaryWriter::write(const long long int& output)
1137 {
1139 }
1140 void BinaryWriter::write(const float& output)
1141 {
1142 writePrimitive<float>(output);
1143 }
1144 void BinaryWriter::write(const double& output)
1145 {
1146 writePrimitive<double>(output);
1147 }
1148 void BinaryWriter::write(const bool& output)
1149 {
1150 writePrimitive<bool>(output);
1151 }
1152
1153 template< typename T>
1154 void BinaryWriter::writePrimitive(const T& output)
1155 {
1156 writeArray((const char*)&output, sizeof(T), 1);
1157 }
1158
1159 void BinaryWriter::writeArrayPrimaryNode(const char* output, size_t size, size_t nmemb)
1160 {
1161 if (Layout::primaryNode())
1162 {
1163 if (QDPUtil::big_endian())
1164 {
1165 /* big-endian */
1166 /* Write */
1167 internalChecksum() = QDPUtil::crc32(internalChecksum(), output, size*nmemb);
1168 getOstream().write(output, size*nmemb);
1169 }
1170 else
1171 {
1172 /* little-endian */
1173 /* Swap and write and swap */
1174 QDPUtil::byte_swap(const_cast<char *>(output), size, nmemb);
1175 internalChecksum() = QDPUtil::crc32(internalChecksum(), output, size*nmemb);
1176 getOstream().write(output, size*nmemb);
1177 QDPUtil::byte_swap(const_cast<char *>(output), size, nmemb);
1178 }
1179 }
1180 }
1181
1182 void BinaryWriter::writeArray(const char* output, size_t size, size_t nmemb)
1183 {
1184 writeArrayPrimaryNode(output, size, nmemb);
1185 }
1186
1187 // Get the checksum from the binary node to all nodes
1189 {
1190 // Keep the checksum in sync on all nodes. This only really
1191 // is needed if nodes do detailed checks on the checksums
1194 internalChecksum() = chk;
1195 return chk;
1196 }
1197
1198
1199 // Wrappers for write functions
1200 void writeDesc(BinaryWriter& bin, const std::string& output)
1201 {
1202 bin.writeDesc(output);
1203 }
1204 void write(BinaryWriter& bin, const std::string& output)
1205 {
1206 bin.write(output);
1207 }
1208 void write(BinaryWriter& bin, const char* output)
1209 {
1210 bin.write(std::string(output));
1211 }
1212 void write(BinaryWriter& bin, char output)
1213 {
1214 bin.write(output);
1215 }
1216 void write(BinaryWriter& bin, int output)
1217 {
1218 bin.write(output);
1219 }
1220 void write(BinaryWriter& bin, unsigned int output)
1221 {
1222 bin.write(output);
1223 }
1224 void write(BinaryWriter& bin, short int output)
1225 {
1226 bin.write(output);
1227 }
1228 void write(BinaryWriter& bin, unsigned short int output)
1229 {
1230 bin.write(output);
1231 }
1232 void write(BinaryWriter& bin, long int output)
1233 {
1234 bin.write(output);
1235 }
1236 void write(BinaryWriter& bin, unsigned long int output)
1237 {
1238 bin.write(output);
1239 }
1240 void write(BinaryWriter& bin, long long int output)
1241 {
1242 bin.write(output);
1243 }
1244 void write(BinaryWriter& bin, float output)
1245 {
1246 bin.write(output);
1247 }
1248 void write(BinaryWriter& bin, double output)
1249 {
1250 bin.write(output);
1251 }
1252 void write(BinaryWriter& bin, bool output)
1253 {
1254 bin.write(output);
1255 }
1256
1258 void write(BinaryWriter& bin, const std::complex<float>& param)
1259 {
1260 write(bin, param.real());
1261 write(bin, param.imag());
1262 }
1263
1265 void write(BinaryWriter& bin, const std::complex<double>& param)
1266 {
1267 write(bin, param.real());
1268 write(bin, param.imag());
1269 }
1270
1271
1272 // Different bindings for write functions
1273 BinaryWriter& operator<<(BinaryWriter& bin, const std::string& output)
1274 {
1275 write(bin, output);
1276 return bin;
1277 }
1278
1279 BinaryWriter& operator<<(BinaryWriter& bin, const char* output)
1280 {
1281 write(bin, output);
1282 return bin;
1283 }
1284
1286 {
1287 write(bin, output);
1288 return bin;
1289 }
1290
1291 BinaryWriter& operator<<(BinaryWriter& bin, unsigned int output)
1292 {
1293 write(bin, output);
1294 return bin;
1295 }
1296
1297 BinaryWriter& operator<<(BinaryWriter& bin, short int output)
1298 {
1299 write(bin, output);
1300 return bin;
1301 }
1302
1303 BinaryWriter& operator<<(BinaryWriter& bin, unsigned short int output)
1304 {
1305 write(bin, output);
1306 return bin;
1307 }
1308
1309 BinaryWriter& operator<<(BinaryWriter& bin, long int output)
1310 {
1311 write(bin, output);
1312 return bin;
1313 }
1314
1315 BinaryWriter& operator<<(BinaryWriter& bin, unsigned long int output)
1316 {
1317 write(bin, output);
1318 return bin;
1319 }
1320
1321 BinaryWriter& operator<<(BinaryWriter& bin, long long int output)
1322 {
1323 write(bin, output);
1324 return bin;
1325 }
1326
1328 {
1329 write(bin, output);
1330 return bin;
1331 }
1332
1334 {
1335 write(bin, output);
1336 return bin;
1337 }
1338
1340 {
1341 write(bin, output);
1342 return bin;
1343 }
1344
1345 //--------------------------------------------------------------------------------
1346 // Binary writer support
1348
1349 // Construct from a string
1351
1353
1354 void BinaryBufferWriter::open(const std::string& s)
1355 {
1356 if (Layout::primaryNode())
1357 f.str(s);
1358 }
1359
1360 // Output the stream
1362 {
1363 std::string s;
1364
1365 if (Layout::primaryNode())
1366 s = f.str();
1367
1368 return s;
1369 }
1370
1371 // Output the stream
1372 std::string BinaryBufferWriter::str() const
1373 {
1374 std::string s;
1375
1376 if (Layout::primaryNode())
1377 s = f.str();
1378
1380 return s;
1381 }
1382
1383 // Clear the stream
1385 {
1386 if (Layout::primaryNode())
1387 f.clear();
1388
1389 checksum = 0;
1390 }
1391
1392
1393 //--------------------------------------------------------------------------------
1394 // Binary writer support
1396
1397 BinaryFileWriter::BinaryFileWriter(const std::string& p) {checksum = 0; open(p);}
1398
1399 void BinaryFileWriter::open(const std::string& p)
1400 {
1401 checksum = 0;
1402 if (Layout::primaryNode())
1403 f.open(p.c_str(),std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
1404
1405 if (! is_open())
1406 QDP_error_exit("BinaryFileWriter: error opening file %s",p.c_str());
1407 }
1408
1410 {
1411 if (is_open())
1412 {
1413 if (Layout::primaryNode())
1414 {
1415 f.close();
1416 }
1417 }
1418 }
1419
1420
1421 // Propagate status to all nodes
1423 {
1424 bool s = QDP_isInitialized();
1425
1426 if (s)
1427 {
1428 if (Layout::primaryNode())
1429 s = f.is_open();
1430
1432 }
1433
1434 return s;
1435 }
1436
1438 {
1439 if (is_open())
1440 {
1441 if (Layout::primaryNode())
1442 f.flush();
1443 }
1444 }
1445
1446 // Close the file
1448
1449
1450 //--------------------------------------------------------------------------------
1451 // Binary reader/writer support
1452 // Propagate status to all nodes
1454 {
1455 bool s;
1456
1457 if (Layout::primaryNode())
1458 s = getIOstream().fail();
1459
1461 return s;
1462 }
1463
1464 // Get the checksum from the binary node to all nodes
1466 {
1467 // Keep the checksum in sync on all nodes. This only really
1468 // is needed if nodes do detailed checks on the checksums
1471 internalChecksum() = chk;
1472 return chk;
1473 }
1474
1477 {
1478 internalChecksum() = 0;
1479 }
1480
1483 {
1484 pos_type pos;
1485 if (Layout::primaryNode())
1486 pos = getIOstream().tellg();
1487
1489 return pos;
1490 }
1491
1494 {
1495 if (Layout::primaryNode())
1496 getIOstream().seekg(pos);
1497
1498 internalChecksum() = 0;
1499 }
1500
1503 {
1504 if (Layout::primaryNode())
1505 getIOstream().seekg(off, std::ios_base::beg);
1506
1507 internalChecksum() = 0;
1508 }
1509
1512 {
1513 if (Layout::primaryNode())
1514 getIOstream().seekg(off, std::ios_base::cur);
1515
1516 internalChecksum() = 0;
1517 }
1518
1521 {
1522 if (Layout::primaryNode())
1523 getIOstream().seekg(-off, std::ios_base::end);
1524
1525 internalChecksum() = 0;
1526 }
1527
1530 {
1531 if (Layout::primaryNode())
1532 getIOstream().seekg(0, std::ios_base::beg);
1533
1534 internalChecksum() = 0;
1535 }
1536
1537
1538 //--------------------------------------------------------------------------------
1539 // Binary buffer reader/writer support
1541
1542 // Construct from a string
1544
1546
1547 void BinaryBufferReaderWriter::open(const std::string& s)
1548 {
1549 if (Layout::primaryNode())
1550 f.str(s);
1551 }
1552
1553 // Output the stream
1555 {
1556 std::string s;
1557
1558 if (Layout::primaryNode())
1559 s = f.str();
1560
1561 return s;
1562 }
1563
1564 // Output the stream
1566 {
1567 std::string s;
1568
1569 if (Layout::primaryNode())
1570 s = f.str();
1571
1573 return s;
1574 }
1575
1576 // Clear the stream
1578 {
1579 if (Layout::primaryNode())
1580 f.clear();
1581
1582 checksum = 0;
1583 }
1584
1585
1586
1587 //--------------------------------------------------------------------------------
1588 // Binary reader/writer support
1590
1591 BinaryFileReaderWriter::BinaryFileReaderWriter(const std::string& p, std::ios_base::openmode mode)
1592 {
1593 checksum = 0;
1594 open(p, mode);
1595 }
1596
1597 void BinaryFileReaderWriter::open(const std::string& p, std::ios_base::openmode mode)
1598 {
1599 checksum = 0;
1600 if (Layout::primaryNode())
1601 f.open(p.c_str(), mode | std::ios_base::binary);
1602
1603 if (! is_open())
1604 QDP_error_exit("BinaryFileReaderWriter: error opening file %s",p.c_str());
1605 }
1606
1607 // Close
1609 {
1610 if (is_open())
1611 {
1612 if (Layout::primaryNode())
1613 f.close();
1614 }
1615 }
1616
1617 // Propagate status to all nodes
1619 {
1620 bool s = QDP_isInitialized();
1621
1622 if (s)
1623 {
1624 if (Layout::primaryNode())
1625 s = f.is_open();
1626
1628 }
1629
1630 return s;
1631 }
1632
1634 {
1635 if (is_open())
1636 {
1637 if (Layout::primaryNode())
1638 f.flush();
1639 }
1640 }
1641
1642 // Shutdown
1644
1645
1646} // namespace QDP
std::string str() const
Return entire buffer as a string.
Definition qdp_io.cc:1565
void clear()
Clear the buffer.
Definition qdp_io.cc:1577
void open(const std::string &s)
Construct from a string.
Definition qdp_io.cc:1547
std::string strPrimaryNode() const
Return entire buffer as a string.
Definition qdp_io.cc:1554
~BinaryBufferReaderWriter()
Closes the buffer.
Definition qdp_io.cc:1545
std::string str() const
Return entire buffer as a string.
Definition qdp_io.cc:942
std::string strPrimaryNode() const
Return entire buffer as a string.
Definition qdp_io.cc:931
void clear()
Clear the buffer.
Definition qdp_io.cc:954
~BinaryBufferReader()
Closes the buffer.
Definition qdp_io.cc:922
void open(const std::string &s)
Construct from a string.
Definition qdp_io.cc:924
~BinaryBufferWriter()
Closes the buffer.
Definition qdp_io.cc:1352
void open(const std::string &s)
Construct from a string.
Definition qdp_io.cc:1354
void clear()
Clear the buffer.
Definition qdp_io.cc:1384
std::string str() const
Return entire buffer as a string.
Definition qdp_io.cc:1372
std::string strPrimaryNode() const
Return entire buffer as a string.
Definition qdp_io.cc:1361
void close()
Closes the last file opened.
Definition qdp_io.cc:1608
void open(const std::string &p, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
Opens a file for reading.
Definition qdp_io.cc:1597
bool is_open()
Queries whether the file is open.
Definition qdp_io.cc:1618
void flush()
Flushes the buffer.
Definition qdp_io.cc:1633
bool is_open()
Queries whether the file is open.
Definition qdp_io.cc:991
void open(const std::string &p)
Opens a file for reading.
Definition qdp_io.cc:970
void close()
Closes the last file opened.
Definition qdp_io.cc:981
bool is_open()
Queries whether the file is open.
Definition qdp_io.cc:1422
void close()
Closes the last file opened.
Definition qdp_io.cc:1409
void flush()
Flushes the buffer.
Definition qdp_io.cc:1437
void open(const std::string &p)
Definition qdp_io.cc:1399
virtual QDPUtil::n_uint32_t & internalChecksum()=0
Get the current checksum to modify.
std::iostream::off_type off_type
Definition qdp_io.h:1502
virtual std::iostream & getIOstream()=0
Get the internal input/output stream.
virtual pos_type currentPosition()
Current position.
Definition qdp_io.cc:1482
virtual void resetChecksum()
Reset the current checksum.
Definition qdp_io.cc:1476
virtual void seek(pos_type off)
Set the current position.
Definition qdp_io.cc:1493
virtual bool fail()
Checks status of the previous IO operation.
Definition qdp_io.cc:1453
virtual void rewind()
Rewind object to the beginning.
Definition qdp_io.cc:1529
virtual void seekBegin(off_type off)
Set the position relative from the start.
Definition qdp_io.cc:1502
virtual void seekRelative(off_type off)
Set the position relative to the current position.
Definition qdp_io.cc:1511
std::iostream::pos_type pos_type
Definition qdp_io.h:1501
virtual QDPUtil::n_uint32_t getChecksum()
Get the current checksum.
Definition qdp_io.cc:1465
virtual void seekEnd(off_type off)
Set the position relative from the end.
Definition qdp_io.cc:1520
Binary input base class.
Definition qdp_io.h:372
std::istream::pos_type pos_type
Definition qdp_io.h:374
virtual void read(std::string &result, size_t nbytes)
Read some max number of characters - 1 upto and excluding a newline.
Definition qdp_io.cc:630
virtual void seek(pos_type off)
Set the current position.
Definition qdp_io.cc:564
virtual void readArray(char *output, size_t nbytes, size_t nmemb)
Read data on the primary node and broadcast to all nodes.
Definition qdp_io.cc:706
virtual void seekEnd(off_type off)
Set the position relative from the end.
Definition qdp_io.cc:591
virtual void rewind()
Rewind object to the beginning.
Definition qdp_io.cc:600
virtual void seekRelative(off_type off)
Set the position relative to the current position.
Definition qdp_io.cc:582
void readPrimitive(T &output)
The universal data-reader.
Definition qdp_io.cc:701
virtual void resetChecksum()
Reset the current checksum.
Definition qdp_io.cc:547
virtual QDPUtil::n_uint32_t getChecksum()
Get the current checksum.
Definition qdp_io.cc:536
virtual pos_type currentPosition()
Current position.
Definition qdp_io.cc:553
virtual QDPUtil::n_uint32_t & internalChecksum()=0
Get the current checksum to modify.
virtual void readArrayLittleEndian(char *output, size_t nbytes, size_t nmemb)
Definition qdp_io.cc:734
virtual bool fail()
Checks status of the previous IO operation.
Definition qdp_io.cc:524
virtual void seekBegin(off_type off)
Set the position relative from the start.
Definition qdp_io.cc:573
virtual void readArrayPrimaryNodeLittleEndian(char *output, size_t nbytes, size_t nmemb)
Definition qdp_io.cc:754
virtual void readDesc(std::string &result)
Definition qdp_io.cc:610
virtual std::istream & getIstream()=0
Get the internal input stream.
std::istream::off_type off_type
Definition qdp_io.h:375
virtual void readArrayPrimaryNode(char *output, size_t nbytes, size_t nmemb)
Read data on the primary node only.
Definition qdp_io.cc:715
Binary writer base class.
Definition qdp_io.h:1010
virtual void writeArrayPrimaryNode(const char *output, size_t nbytes, size_t nmemb)
Write data on the primary node only.
Definition qdp_io.cc:1159
virtual void writeDesc(const std::string &output)
Writes a fixed length of characters like an array.
Definition qdp_io.cc:1089
virtual void rewind()
Rewind object to the beginning.
Definition qdp_io.cc:1080
BinaryWriter()
Default constructor.
Definition qdp_io.cc:1012
void writePrimitive(const T &output)
The universal data-writer.
Definition qdp_io.cc:1154
virtual void writeArray(const char *output, size_t nbytes, size_t nmemb)
Write data from the primary node.
Definition qdp_io.cc:1182
virtual pos_type currentPosition()
Current position.
Definition qdp_io.cc:1033
virtual QDPUtil::n_uint32_t & internalChecksum()=0
Get the current checksum to modify.
virtual std::ostream & getOstream()=0
Get the internal output stream.
virtual void seekBegin(off_type off)
Set the position relative from the start.
Definition qdp_io.cc:1053
std::ostream::pos_type pos_type
Definition qdp_io.h:1012
virtual bool fail()
Checks status of the previous IO operation.
Definition qdp_io.cc:1015
virtual void seek(pos_type off)
Set the current position.
Definition qdp_io.cc:1044
virtual void write(const std::string &output)
Definition qdp_io.cc:1096
std::ostream::off_type off_type
Definition qdp_io.h:1013
virtual void seekRelative(off_type off)
Set the position relative to the current position.
Definition qdp_io.cc:1062
virtual QDPUtil::n_uint32_t getChecksum()
Get the current checksum.
Definition qdp_io.cc:1188
virtual void resetChecksum()
Reset the current checksum.
Definition qdp_io.cc:1027
virtual void seekEnd(off_type off)
Set the position relative from the end.
Definition qdp_io.cc:1071
std::string strPrimaryNode() const
Return entire buffer as a string.
Definition qdp_io.cc:196
~TextBufferReader()
Shutdown the buffer.
Definition qdp_io.cc:219
void open(const std::string &p)
Opens a buffer for reading.
Definition qdp_io.cc:189
std::string str() const
Return entire buffer as a string.
Definition qdp_io.cc:207
std::string strPrimaryNode() const
Return entire buffer as a string.
Definition qdp_io.cc:442
~TextBufferWriter()
Shutdown the buffer.
Definition qdp_io.cc:465
std::string str() const
Return entire buffer as a string.
Definition qdp_io.cc:453
void open(const std::string &s)
Construct from a string.
Definition qdp_io.cc:435
bool is_open()
Queries whether the file is open.
Definition qdp_io.cc:247
void open(const std::string &p)
Opens a file for reading.
Definition qdp_io.cc:228
void close()
Closes the last file opened.
Definition qdp_io.cc:237
void flush()
Flushes the buffer.
Definition qdp_io.cc:508
void open(const std::string &p)
Definition qdp_io.cc:474
void close()
Closes the last file opened.
Definition qdp_io.cc:483
bool is_open()
Queries whether the file is open.
Definition qdp_io.cc:493
Text input class.
Definition qdp_io.h:42
virtual bool fail()
Checks status of the previous IO operation.
Definition qdp_io.cc:18
virtual std::istream & getIstream()=0
Get the internal input stream.
virtual void read(std::string &result)
Definition qdp_io.cc:30
void readPrimitive(T &output)
The universal data-reader.
Definition qdp_io.cc:111
Text output base class.
Definition qdp_io.h:203
virtual void write(const std::string &output)
Definition qdp_io.cc:356
void writePrimitive(const T &output)
The universal data-writer.
Definition qdp_io.cc:422
virtual std::ostream & getOstream()=0
Get the internal output stream.
virtual bool fail()
Checks status of the previous IO operation.
Definition qdp_io.cc:271
TextWriter & operator<<(TextWriter &txt, const std::string &output)
Definition qdp_io.cc:283
TextReader & operator>>(TextReader &txt, std::string &input)
Definition qdp_io.cc:121
void write(BinaryWriter &bin, const std::string &output)
Definition qdp_io.cc:1204
void readDesc(BinaryReader &bin, std::string &input)
Definition qdp_io.cc:774
void writeDesc(BinaryWriter &bin, const std::string &output)
Definition qdp_io.cc:1200
void read(BinaryReader &bin, std::string &input, size_t maxBytes)
Definition qdp_io.cc:778
n_uint32_t crc32(n_uint32_t crc, const unsigned char *buf, size_t len)
crc32
Definition qdp_crc32.cc:184
unsigned int n_uint32_t
void byte_swap(void *ptr, size_t size, size_t nmemb)
Byte-swap an array of data each of size nmemb.
bool big_endian()
Is the native byte order big endian?
bool primaryNode()
Returns whether this is the primary node.
void broadcast(T &dest)
Broadcast from primary node to all other nodes.
Yet another random number generator.
bool QDP_isInitialized()
Is the machine initialized?
void QDP_error_exit(const char *format,...)
Simple error display and abort routine.
Definition qdp_util.cc:93
Primary include file for QDP.
Byte manipulation.