QDP++
qdp_qdpio.h
Go to the documentation of this file.
1// -*- C++ -*-
2
6
7#ifndef QDP_QDPIO_H
8#define QDP_QDPIO_H
9
10#include "qio.h"
11#include <sstream>
12#include "qdp_defs.h"
13#include "qdp_traits.h"
14#include <cstring>
15
16namespace QDP
17{
18
24
31
39
47
50 {
51 QDPIO_goodbit = 0x0000,
52 QDPIO_eofbit = 0x0001,
53 QDPIO_failbit = 0x0010,
54 QDPIO_badbit = 0x0100,
55 };
56
58 // Using the magic of C++ I can define the right type and precision
59 // strings i Need to pass to QIO using templates. To do this I need
60 // templated structures with static members.
61
63 template<typename T>
65 {
66 static char* tname;
67 static char* tprec;
68 };
69
71 template<typename T>
73 {
74 static char* tname;
75 static char* tprec;
76 };
77
79 template<typename T>
81 {
82 static char* tname;
83 static char* tprec;
84 };
85
87 template<typename T>
89 {
90 static char* tname;
91 static char* tprec;
92 };
93
95 template<typename T>
97 {
98 static char* tname;
99 static char* tprec;
100 };
101
103 template<typename T>
104 char* QIOStringTraits<T>::tname = (char *)"QDP_GenericType";
105
107 template<typename T>
108 char* QIOStringTraits< OLattice<T> >::tname = (char *)"Lattice";
109
111 template<typename T>
112 char* QIOStringTraits< OScalar<T> >::tname = (char *)"Scalar";
113
115 template<typename T>
116 char* QIOStringTraits< multi1d<OLattice<T> > >::tname = (char *)"Lattice";
117
119 template<typename T>
120 char* QIOStringTraits< multi1d<OScalar<T> > >::tname = (char *)"Scalar";
121
123 template<typename T>
124 char* QIOStringTraits<T>::tprec = (char *)"U";
125
126 // Full specialisations deferred to the qdp_qio_strings.cc file
127 template<>
129
130 template<>
132
133 template<>
135
136 template<>
138
139 template<>
141
142 template<>
144
145 template<>
147
148 template<>
150
151 template<>
153
154
155 // Generic version of the template
156 // Declares a static value
157 template<class T>
158 struct NumSpins {
159 static int value;
160 };
161
162 // Not definint the static value
163
164 // RScalar and RComplex are my 'base cases'
165 // Assuming they are nested within the Spin in terms of templates
166 template<class T>
167 struct NumSpins< RScalar<T> > {
168 static int value;
169 };
170
171 template<class T>
173
174
175 template<class T>
176 struct NumSpins< RComplex<T> > {
177 static int value;
178 };
179
180 template<class T>
182
183
184 // Things with spin structure cause early termination of the
185 // recursion
186 template<class T, int N>
187 struct NumSpins< PSpinVector<T,N> > {
188 static int value;
189 };
190
191 template<class T, int N>
193
194 template<class T, int N>
195 struct NumSpins< PSpinMatrix<T,N> > {
196 static int value;
197 };
198
199 template<class T, int N>
201
202 // PColor has the same template signature as PSpin
203 // So I have to specialize them too or we barf later on.
204 // with undefined symbols
205 template<class T, int N>
206 struct NumSpins< PColorVector<T,N> > {
207 static int value;
208 };
209
210 template<class T, int N>
212
213 template<class T, int N>
214 struct NumSpins< PColorMatrix<T,N> > {
215 static int value;
216 };
217
218 template<class T, int N>
220
221 // Generic recursion
222 template<template <class> class T1, class T2>
223 struct NumSpins< T1< T2 > > {
224 static int value;
225 };
226
227 template<template <class> class T1, class T2>
229
230 // Now for NumColors
231
232 // Generic version of the template
233 // Declares a static value
234 template<class T>
235 struct NumColors {
236 static int value;
237 };
238
239 // Not definint the static value
240
241 // RScalar and RComplex are my 'base cases'
242 // Assuming they are nested within the Spin in terms of templates
243 template<class T>
244 struct NumColors< RScalar<T> > {
245 static int value;
246 };
247
248 template<class T>
250
251
252 template<class T>
253 struct NumColors< RComplex<T> > {
254 static int value;
255 };
256
257 template<class T>
259
260
261 // Things with spin structure cause early termination of the
262 // recursion
263 template<class T, int N>
264 struct NumColors< PSpinVector<T,N> > {
265 static int value;
266 };
267
268 template<class T, int N>
270
271 template<class T, int N>
272 struct NumColors< PSpinMatrix<T,N> > {
273 static int value;
274 };
275
276 template<class T, int N>
278
279 // PColor has the same template signature as PSpin
280 // So I have to specialize them too or we barf later on.
281 // with undefined symbols
282 template<class T, int N>
283 struct NumColors< PColorVector<T,N> > {
284 static int value;
285 };
286
287 template<class T, int N>
289
290 template<class T, int N>
291 struct NumColors< PColorMatrix<T,N> > {
292 static int value;
293 };
294
295 template<class T, int N>
297
298 // Generic recursion
299 template<template <class> class T1, class T2>
300 struct NumColors< T1< T2 > > {
301 static int value;
302 };
303
304 template<template <class> class T1, class T2>
306
307 //--------------------------------------------------------------------------------
309
325
327 {
328 public:
331
332
335
337
343 QDPFileReader(XMLReader& xml, const std::string& path,
344 QDP_serialparallel_t qdp_serpar);
345
347
353 void open(XMLReader& xml, const std::string& path,
354 QDP_serialparallel_t qdp_serpar);
355
357 void close();
358
360
363 bool is_open();
364
366 void read(XMLReader& xml);
367
369 template<class T, class C>
371 {this->read(xml,static_cast<C&>(s1));}
372
374 template<class T>
375 void read(XMLReader& xml, OScalar<T>& s1);
376
378 template<class T>
379 void read(XMLReader& xml, OLattice<T>& s1);
380
382 template<class T>
383 void read(XMLReader& xml, multi1d< OScalar<T> >& s1);
384
386 template<class T>
387 void read(XMLReader& xml, multi1d< OLattice<T> >& s1);
388
390 void read(XMLReader& xml, BinaryBufferReader& s1);
391
393
396 bool eof() const;
397
399
402 bool bad() const;
403
405
408 void clear(QDP_iostate_t state = QDPIO_goodbit);
409
410 protected:
411 QIO_Reader *get() const {return qio_in;}
412
413 private:
414 QDP_iostate_t iostate;
415 bool iop;
416 QIO_Reader *qio_in;
417 };
418
419
420 // Convenience functions
421
423
428 template<class T>
429 void read(QDPFileReader& qsw, XMLReader& rec_xml, OScalar<T>& s1)
430 {
431 qsw.read(rec_xml,s1);
432 }
433
435
440 template<class T>
441 void read(QDPFileReader& qsw, XMLReader& rec_xml, OLattice<T>& s1)
442 {
443 qsw.read(rec_xml,s1);
444 }
445
447
452 template<class T>
453 void read(QDPFileReader& qsw, XMLReader& rec_xml, multi1d< OScalar<T> >& s1)
454 {
455 qsw.read(rec_xml,s1);
456 }
457
459
464 template<class T>
465 void read(QDPFileReader& qsw, XMLReader& rec_xml, multi1d< OLattice<T> >& s1)
466 {
467 qsw.read(rec_xml,s1);
468 }
469
471
476 inline
478 {
479 qsw.read(rec_xml,s1);
480 }
481
483 void close(QDPFileReader& qsw);
484
486 bool is_open(QDPFileReader& qsw);
487
488
489 //-------------------------------------------------
491
507
509 {
510 public:
513
516
518
525 QDPFileWriter(XMLBufferWriter& xml, const std::string& path,
526 QDP_volfmt_t qdp_volfmt,
527 QDP_serialparallel_t qdp_serpar);
528
530
538 QDPFileWriter(XMLBufferWriter& xml, const std::string& path,
539 QDP_volfmt_t qdp_volfmt,
540 QDP_serialparallel_t qdp_serpar,
541 const std::string& data_LFN);
542
544
551 void open(XMLBufferWriter& xml, const std::string& path,
552 QDP_volfmt_t qdp_volfmt,
553 QDP_serialparallel_t qdp_serpar);
554
556
564 void open(XMLBufferWriter& xml, const std::string& path,
565 QDP_volfmt_t qdp_volfmt,
566 QDP_serialparallel_t qdp_serpar,
567 const std::string& data_LFN);
568
570
578
579 QDPFileWriter(XMLBufferWriter& xml, const std::string& path,
580 QDP_volfmt_t qdp_volfmt,
581 QDP_serialparallel_t qdp_serpar,
582 QDP_filemode_t qdp_mode);
583
585
594
595 QDPFileWriter(XMLBufferWriter& xml, const std::string& path,
596 QDP_volfmt_t qdp_volfmt,
597 QDP_serialparallel_t qdp_serpar,
598 QDP_filemode_t qdp_mode,
599 const std::string& data_LFN);
600
602
611
612 void open(XMLBufferWriter& xml, const std::string& path,
613 QDP_volfmt_t qdp_volfmt,
614 QDP_serialparallel_t qdp_serpar,
615 QDP_filemode_t qdp_mode,
616 const std::string& data_LFN);
617
619 void close();
620
622
625 bool is_open();
626
628
632 template<class T, class C>
633 void write(XMLBufferWriter& xml, const QDPType<T,C>& s1)
634 {this->write(xml,static_cast<const C&>(s1));}
635
637
641 template<class T>
642 void write(XMLBufferWriter& xml, const OScalar<T>& s1);
643
645
649 template<class T>
650 void write(XMLBufferWriter& xml, const OLattice<T>& s1);
651
653
659 template<class T>
660 void write(XMLBufferWriter& xml, const OLattice<T>& s1,
661 const multi1d<int>& lower_left,
662 const multi1d<int>& upper_right);
663
665
669 template<class T>
670 void write(XMLBufferWriter& xml, const multi1d< OScalar<T> >& s1);
671
673
677 template<class T>
678 void write(XMLBufferWriter& xml, const multi1d< OLattice<T> >& s1);
679
680
682
688 template<class T>
689 void write(XMLBufferWriter& xml, const multi1d< OLattice<T> >& s1,
690 const multi1d<int>& lower_left,
691 const multi1d<int>& upper_right);
692
693
695
700
702
705 bool bad() const;
706
708
711 void clear(QDP_iostate_t state = QDPIO_goodbit);
712
713 protected:
714 QIO_Writer *get() const {return qio_out;}
715
716 private:
717 QDP_iostate_t iostate;
718 bool iop;
719 QIO_Writer *qio_out;
720 };
721
722
723 // Convenience functions
725
730 template<class T>
731 void write(QDPFileWriter& qsw, XMLBufferWriter& rec_xml, const OScalar<T>& s1)
732 {
733 qsw.write(rec_xml,s1);
734 }
735
737
742 template<class T>
743 void write(QDPFileWriter& qsw, XMLBufferWriter& rec_xml, const OLattice<T>& s1)
744 {
745 qsw.write(rec_xml,s1);
746 }
747
749
756 template<class T>
757 void write(QDPFileWriter& qsw, XMLBufferWriter& rec_xml, const OLattice<T>& s1, const multi1d<int>& lower_left, const multi1d<int>& upper_right)
758 {
759 qsw.write(rec_xml,s1, lower_left, upper_right);
760 }
761
763
768 template<class T>
769 void write(QDPFileWriter& qsw, XMLBufferWriter& rec_xml, const multi1d< OScalar<T> >& s1)
770 {
771 qsw.write(rec_xml,s1);
772 }
773
775
780 template<class T>
781 void write(QDPFileWriter& qsw, XMLBufferWriter& rec_xml, const multi1d< OLattice<T> >& s1)
782 {
783 qsw.write(rec_xml,s1);
784 }
785
787
794 template<class T>
795 void write(QDPFileWriter& qsw, XMLBufferWriter& rec_xml, const multi1d< OLattice<T> >& s1, const multi1d<int>& lower_left, const multi1d<int>& upper_right)
796 {
797 qsw.write(rec_xml,s1,lower_left, upper_right);
798 }
799
801
806 inline
808 {
809 qsw.write(rec_xml,s1);
810 }
811
813 void close(QDPFileWriter& qsw);
814
816 bool is_open(QDPFileWriter& qsw);
817
818
819
820 //-------------------------------------------------
821 // QIO support
822 //
823 // Scalar support
824
826
834 template<class T> void QDPOScalarFactoryPut(char *buf, size_t linear, int count, void *arg)
835 {
836 /* Translate arg */
837 T *field = (T *)arg;
838
839 void *dest = (void*)(field+linear);
840 memcpy(dest,(const void*)buf,count*sizeof(T));
841 }
842
843
845
850
851 template<typename T>
853 {
854 /* For now I may not be able to read Dirk's stuff, but I should
855 * be able to read what I wrote */
856 QIO_RecordInfo rec_info;
857 QIO_String* xml_c = QIO_string_create();
858 int status;
859
860
861 status=QIO_read_record_info(qio_in, &rec_info, xml_c);
862 if( status != QIO_SUCCESS) {
863 QDPIO::cerr << "Failed to read the Record Info" << std::endl;
864 QDP_abort(1);
865 }
866
867 switch( (QIO_get_precision(&rec_info))[0] ) {
868 case 'F' :
869 {
870 QDPIO::cout << "Single Precision Read" << std::endl;
872 status = QIO_read_record_data(qio_in,
874 sizeof(typename SinglePrecType<T>::Type_t),
875 sizeof(typename WordType< typename SinglePrecType<T>::Type_t >::Type_t),
876 (void *)(&(from_disk.elem())));
877 if (status != QIO_SUCCESS) {
878 QDPIO::cerr << "Failed to read data" << std::endl;
880 QDP_abort(1);
881 }
882 QDPIO::cout << "QIO_read_finished" << std::endl;
883 s1 = from_disk;
884 }
885 break;
886 case 'D' :
887 {
888 QDPIO::cout << "Reading Double Precision" << std::endl;
890 status = QIO_read_record_data(qio_in,
892 sizeof(typename DoublePrecType<T>::Type_t),
893 sizeof(typename WordType< typename DoublePrecType<T>::Type_t >::Type_t),
894 (void *)(&(from_disk.elem())));
895 if (status != QIO_SUCCESS) {
896 QDPIO::cerr << "Failed to read data" << std::endl;
898 QDP_abort(1);
899 }
900 QDPIO::cout << "QIO_read_finished" << std::endl;
901
902 s1 = from_disk;
903 }
904 break;
905 default:
906 {
907 QDPIO::cout << "Reading I or U Precision" << std::endl;
908 status = QIO_read_record_data(qio_in,
910 sizeof(T),
911 sizeof(typename WordType<T>::Type_t),
912 (void *)(&(s1.elem())));
913 if (status != QIO_SUCCESS) {
914 QDPIO::cerr << "Failed to read data" << std::endl;
916 QDP_abort(1);
917 }
918 QDPIO::cout << "QIO_read_finished" << std::endl;
919 }
920 break;
921 }
922
923 std::istringstream ss;
924 if (Layout::primaryNode()) {
925 std::string foo = QIO_string_ptr(xml_c);
926 ss.str(foo);
927 }
928 rec_xml.open(ss);
929
930 QIO_string_destroy(xml_c);
931 }
932
933
935
941
942 template<typename T>
944 {
945 /* For now I may not be able to read Dirk's stuff, but I should
946 * be able to read what I wrote */
947 QIO_RecordInfo rec_info;
948 QIO_String* xml_c = QIO_string_create();
949 int status;
950
951
952 status=QIO_read_record_info(qio_in, &rec_info, xml_c);
953 if( status != QIO_SUCCESS) {
954 QDPIO::cerr << "Failed to read the Record Info" << std::endl;
955 QDP_abort(1);
956 }
957
958 switch( (QIO_get_precision(&rec_info))[0] ) {
959 case 'F' :
960 {
961 QDPIO::cout << "Single Precision Read" << std::endl;
963 status = QIO_read_record_data(qio_in,
965 s1.size()*sizeof(typename SinglePrecType<T>::Type_t),
966 sizeof(typename WordType< typename SinglePrecType<T>::Type_t >::Type_t),
967 (void *)from_disk.slice());
968 if (status != QIO_SUCCESS) {
969 QDPIO::cerr << "Failed to read data" << std::endl;
971 QDP_abort(1);
972 }
973 QDPIO::cout << "QIO_read_finished" << std::endl;
974
975 // Cast appropriately
976 for(int i=0; i < from_disk.size(); i++) {
977 s1[i] = from_disk[i];
978 }
979
980 }
981 break;
982 case 'D' :
983 {
984 QDPIO::cout << "Reading Double Precision" << std::endl;
986 status = QIO_read_record_data(qio_in,
988 s1.size()*sizeof(typename DoublePrecType<T>::Type_t),
989 sizeof(typename WordType< typename DoublePrecType<T>::Type_t >::Type_t),
990 (void *)from_disk.slice());
991 if (status != QIO_SUCCESS) {
992 QDPIO::cerr << "Failed to read data" << std::endl;
994 QDP_abort(1);
995 }
996 QDPIO::cout << "QIO_read_finished" << std::endl;
997
998 // Cast appropriately
999 for(int i=0; i < from_disk.size(); i++) {
1000 s1[i] = from_disk[i];
1001 }
1002 }
1003 break;
1004 default:
1005 {
1006 QDPIO::cout << "Reading I or U Precision" << std::endl;
1007 status = QIO_read_record_data(qio_in,
1009 s1.size()*sizeof(T),
1010 sizeof(typename WordType<T>::Type_t),
1011 (void *)s1.slice());
1012 if (status != QIO_SUCCESS) {
1013 QDPIO::cerr << "Failed to read data" << std::endl;
1015 QDP_abort(1);
1016 }
1017 QDPIO::cout << "QIO_read_finished" << std::endl;
1018 }
1019 break;
1020 }
1021
1022 std::istringstream ss;
1023 if (Layout::primaryNode()) {
1024 std::string foo = QIO_string_ptr(xml_c);
1025 ss.str(foo);
1026 }
1027 rec_xml.open(ss);
1028
1029 QIO_string_destroy(xml_c);
1030 }
1031
1032
1033 // Reads a BinaryBufferReader object
1038// void QDPFileReader::read(XMLReader& rec_xml, BinaryBufferReader& s1);
1039
1040
1042
1050
1051 template<class T> void QDPOScalarFactoryGet(char *buf, size_t linear, int count, void *arg)
1052 {
1053 /* Translate arg */
1054 T *field = (T *)arg;
1055
1056 void *src = (void*)(field+linear);
1057 memcpy(buf,(const void*)src,count*sizeof(T));
1058 }
1059
1060
1062
1068 template<class T>
1070 {
1071 QIO_RecordInfo* info = QIO_create_record_info(QIO_GLOBAL, NULL, NULL, 0,
1075 sizeof(T), 1);
1076
1077 // Copy metadata string into simple qio string container
1078 QIO_String* xml_c = QIO_string_create();
1079
1080 if (Layout::primaryNode())
1081 QIO_string_set(xml_c, rec_xml.str().c_str());
1082
1083 if (xml_c == NULL)
1084 {
1085 QDPIO::cerr << "QDPFileWriter::write - error in creating XML string" << std::endl;
1086 QDP_abort(1);
1087 }
1088
1089 // Big call to QIO
1090
1091 if (QIO_write(get(), info, xml_c,
1093 sizeof(T),
1094 sizeof(typename WordType<T>::Type_t),
1095 (void *)(&(s1.elem()))) != QIO_SUCCESS)
1096 {
1097 QDPIO::cerr << "QDPFileWriter: error in write" << std::endl;
1099 }
1100
1101
1102 // Cleanup
1103 QIO_string_destroy(xml_c);
1105 }
1106
1107
1109
1115
1117
1123 template<class T>
1125 {
1126 QIO_RecordInfo* info = QIO_create_record_info(QIO_GLOBAL, NULL, NULL, 0,
1130 sizeof(T), s1.size());
1131
1132
1133 // Copy metadata string into simple qio string container
1134 QIO_String* xml_c = QIO_string_create();
1135 if (Layout::primaryNode())
1136 QIO_string_set(xml_c, rec_xml.str().c_str());
1137
1138 if (xml_c == NULL)
1139 {
1140 QDPIO::cerr << "QDPFileWriter::write - error in creating XML string" << std::endl;
1141 QDP_abort(1);
1142 }
1143
1144 // Big call to qio
1145 if (QIO_write(get(), info, xml_c,
1147 s1.size()*sizeof(T),
1148 sizeof(typename WordType<T>::Type_t),
1149 (void *)s1.slice()) != QIO_SUCCESS)
1150 {
1151 QDPIO::cerr << "QDPFileWriter: error in write" << std::endl;
1153 }
1154
1155 // Cleanup
1156 QIO_string_destroy(xml_c);
1158 }
1159
1160
1162
1166// void QDPFileWriter::write(XMLBufferWriter& rec_xml, BinaryBufferWriter& s1);
1167
1168
1169 //-------------------------------------------------
1170 // QIO support
1171 // NOTE: this is exactly the same bit of code as in scalar_specific.h
1172 // need to make common only on scalarsite.h like architectures
1173
1175
1183 template<class T> void QDPOLatticeFactoryPut(char *buf, size_t linear, int count, void *arg)
1184 {
1185 /* Translate arg */
1186 T *field = (T *)arg;
1187
1188 void *dest = (void*)(field+linear);
1189 memcpy(dest,(const void*)buf,count*sizeof(T));
1190 }
1191
1193
1203 template<class T> void QDPOLatticeFactoryPutArray(char *buf, size_t linear, int count, void *arg)
1204 {
1205 /* Translate arg */
1206 multi1d< OLattice<T> >& field = *(multi1d< OLattice<T> > *)arg;
1207
1208 for(int i=0; i < field.size(); ++i)
1209 {
1210 void *dest = (void*)&(field[i].elem(linear));
1211 memcpy(dest,(const void*)buf,sizeof(T));
1212 buf += sizeof(T);
1213 }
1214 }
1215
1216
1218
1224 template<class T>
1226 {
1227 /* For now I may not be able to read Dirk's stuff, but I should
1228 * be able to read what I wrote */
1229 QIO_RecordInfo rec_info;
1230 QIO_String* xml_c = QIO_string_create();
1231 int status;
1232
1233 status=QIO_read_record_info(qio_in, &rec_info, xml_c);
1234 if( status != QIO_SUCCESS) {
1235 QDPIO::cerr << "Failed to read the Record Info" << std::endl;
1236 QDP_abort(1);
1237 }
1238
1239 switch( (QIO_get_precision(&rec_info))[0] ) {
1240 case 'F' :
1241 {
1242 QDPIO::cout << "Single Precision Read" << std::endl;
1244 zero_rep(from_disk);
1245
1246
1247 status = QIO_read_record_data(qio_in,
1249 sizeof(typename SinglePrecType<T>::Type_t),
1250 sizeof(typename WordType< typename SinglePrecType<T>::Type_t >::Type_t),
1251 (void *)from_disk.getF());
1252
1253 if (status != QIO_SUCCESS) {
1254 QDPIO::cerr << "Failed to read data" << std::endl;
1256 QDP_abort(1);
1257 }
1258 QDPIO::cout << "QIO_read_finished" << std::endl;
1259 s1 = from_disk; // Cast
1260 }
1261 break;
1262 case 'D' :
1263 {
1264 QDPIO::cout << "Reading Double Precision" << std::endl;
1266 zero_rep(from_disk);
1267
1268 /* Disagnostics */
1269 status = QIO_read_record_data(qio_in,
1271 sizeof(typename DoublePrecType<T>::Type_t),
1272 sizeof(typename WordType< typename DoublePrecType<T>::Type_t >::Type_t),
1273 (void *)from_disk.getF());
1274
1275 if (status != QIO_SUCCESS) {
1276 QDPIO::cerr << "Failed to read data" << std::endl;
1278 QDP_abort(1);
1279 }
1280 QDPIO::cout << "QIO_read_finished" << std::endl;
1281
1282 // Cast appropriately
1283 s1= from_disk;
1284
1285 }
1286 break;
1287 default:
1288 {
1289 QDPIO::cout << "Reading I or U precisions" << std::endl;
1290 status = QIO_read_record_data(qio_in,
1292 sizeof(T),
1293 sizeof(typename WordType<T>::Type_t),
1294 (void *)s1.getF());
1295
1296 if (status != QIO_SUCCESS) {
1297 QDPIO::cerr << "Failed to read data" << std::endl;
1299 QDP_abort(1);
1300 }
1301 QDPIO::cout << "QIO_read_finished" << std::endl;
1302 }
1303 break;
1304 };
1305
1306 std::istringstream ss;
1307 if (Layout::primaryNode()) {
1308 std::string foo = QIO_string_ptr(xml_c);
1309 ss.str(foo);
1310 }
1311 rec_xml.open(ss);
1312
1313 QIO_string_destroy(xml_c);
1314 }
1315
1317
1323 template<typename T>
1325 {
1326 /* For now I may not be able to read Dirk's stuff, but I should
1327 * be able to read what I wrote */
1328 QIO_RecordInfo rec_info;
1329 QIO_String* xml_c = QIO_string_create();
1330 int status;
1331
1332
1333 status=QIO_read_record_info(qio_in, &rec_info, xml_c);
1334 if( status != QIO_SUCCESS) {
1335 QDPIO::cerr << "Failed to read the Record Info" << std::endl;
1336 QDP_abort(1);
1337 }
1338
1339 switch( (QIO_get_precision(&rec_info))[0] ) {
1340 case 'F' :
1341 {
1342 QDPIO::cout << "Single Precision Read" << std::endl;
1344 for(int i=0; i < s1.size(); i++) { zero_rep(from_disk[i]); }
1345
1346 status = QIO_read_record_data(qio_in,
1348 s1.size()*sizeof(typename SinglePrecType<T>::Type_t),
1349 sizeof(typename WordType< typename SinglePrecType<T>::Type_t >::Type_t),
1350 (void *)&from_disk);
1351 if (status != QIO_SUCCESS) {
1352 QDPIO::cerr << "Failed to read data" << std::endl;
1354 QDP_abort(1);
1355 }
1356 QDPIO::cout << "QIO_read_finished" << std::endl;
1357
1358 // Cast appropriately
1359 for(int i=0; i < from_disk.size(); i++) {
1360 s1[i] = from_disk[i];
1361 }
1362
1363 }
1364 break;
1365 case 'D' :
1366 {
1367 QDPIO::cout << "Reading Double Precision" << std::endl;
1369 for(int i=0; i < s1.size(); i++) { zero_rep(from_disk[i]); }
1370
1371 status = QIO_read_record_data(qio_in,
1373 s1.size()*sizeof(typename DoublePrecType<T>::Type_t),
1374 sizeof(typename WordType< typename DoublePrecType<T>::Type_t >::Type_t),
1375 (void *)&from_disk);
1376 if (status != QIO_SUCCESS) {
1377 QDPIO::cerr << "Failed to read data" << std::endl;
1379 QDP_abort(1);
1380 }
1381 QDPIO::cout << "QIO_read_finished" << std::endl;
1382
1383 // Cast appropriately
1384 for(int i=0; i < from_disk.size(); i++) {
1385 s1[i] = from_disk[i];
1386 }
1387 }
1388 break;
1389 default:
1390 {
1391 QDPIO::cout << "Reading I or U Precision" << std::endl;
1392 status = QIO_read_record_data(qio_in,
1394 s1.size()*sizeof(T),
1395 sizeof(typename WordType<T>::Type_t),
1396 (void *)&s1);
1397 if (status != QIO_SUCCESS) {
1398 QDPIO::cerr << "Failed to read data" << std::endl;
1400 QDP_abort(1);
1401 }
1402 QDPIO::cout << "QIO_read_finished" << std::endl;
1403 }
1404 break;
1405 }
1406
1407 std::istringstream ss;
1408 if (Layout::primaryNode()) {
1409 std::string foo = QIO_string_ptr(xml_c);
1410 ss.str(foo);
1411 }
1412
1413 try {
1414 rec_xml.open(ss);
1415 }
1416 catch(const std::string& e) {
1417 QDPIO::cout << "Handling exception" << std::endl;
1418 }
1419
1420 QIO_string_destroy(xml_c);
1421 }
1422
1424
1432
1433 template<class T> void QDPOLatticeFactoryGet(char *buf, size_t linear, int count, void *arg)
1434 {
1435 /* Translate arg */
1436 T *field = (T *)arg;
1437
1438 void *src = (void*)(field+linear);
1439 memcpy(buf,(const void*)src,count*sizeof(T));
1440 }
1441
1443
1452 template<class T> void QDPOLatticeFactoryGetArray(char *buf, size_t linear, int count, void *arg)
1453 {
1454 /* Translate arg */
1455 multi1d< OLattice<T> >& field = *(multi1d< OLattice<T> > *)arg;
1456
1457 for(int i=0; i < field.size(); ++i)
1458 {
1459 void *src = (void*)&(field[i].elem(linear));
1460 memcpy(buf,(const void*)src,sizeof(T));
1461 buf += sizeof(T);
1462 }
1463 }
1464
1465
1466
1468
1474 template<class T>
1476 {
1477 QIO_RecordInfo* info = QIO_create_record_info(QIO_FIELD, NULL, NULL,0,
1481 sizeof(T),1 );
1482
1483 // Copy metadata string into simple qio string container
1484 QIO_String* xml_c = QIO_string_create();
1485 if (Layout::primaryNode())
1486 QIO_string_set(xml_c, rec_xml.str().c_str());
1487
1488 if (xml_c == NULL)
1489 {
1490 QDPIO::cerr << "QDPFileWriter::write - error in creating XML string" << std::endl;
1491 QDP_abort(1);
1492 }
1493
1494 // Big call to qio
1495 if (QIO_write(get(), info, xml_c,
1497 sizeof(T),
1498 sizeof(typename WordType<T>::Type_t),
1499 (void *)s1.getF()) != QIO_SUCCESS)
1500 {
1501 QDPIO::cerr << "QDPFileWriter: error in write" << std::endl;
1503 }
1504
1505 // Cleanup
1506 QIO_string_destroy(xml_c);
1508 }
1509
1510
1512
1520 template<class T>
1521 void QDPFileWriter::write(XMLBufferWriter& rec_xml, const OLattice<T>& s1, const multi1d<int>& lower_left, const multi1d<int>& upper_right)
1522 {
1523
1524 // Sanity check...
1525 if( lower_left.size() != upper_right.size()) {
1526 QDPIO::cerr << "QDPFileWriter: Error! Lower left and upper right corner of hypercube to write have different dimensions" << std::endl;
1527 QDP_abort(1);
1528 }
1529
1531 (int *)lower_left.slice(),
1532 (int *)upper_right.slice(),
1533 lower_left.size(),
1537 sizeof(T),1 );
1538
1539 // Copy metadata string into simple qio string container
1540 QIO_String* xml_c = QIO_string_create();
1541 if (Layout::primaryNode())
1542 QIO_string_set(xml_c, rec_xml.str().c_str());
1543
1544 if (xml_c == NULL)
1545 {
1546 QDPIO::cerr << "QDPFileWriter::write - error in creating XML string" << std::endl;
1547 QDP_abort(1);
1548 }
1549
1550 // Big call to qio
1551 if (QIO_write(get(), info, xml_c,
1553 sizeof(T),
1554 sizeof(typename WordType<T>::Type_t),
1555 (void *)s1.getF()) != QIO_SUCCESS)
1556 {
1557 QDPIO::cerr << "QDPFileWriter: error in write" << std::endl;
1559 }
1560
1561 // Cleanup
1562 QIO_string_destroy(xml_c);
1564 }
1565
1566
1568
1574 template<class T>
1576 {
1578 NULL, NULL, 0,
1582 sizeof(T), s1.size() );
1583
1584 // Copy metadata string into simple qio string container
1585 QIO_String* xml_c = QIO_string_create();
1586 if (Layout::primaryNode())
1587 QIO_string_set(xml_c, rec_xml.str().c_str());
1588
1589 if (xml_c == NULL)
1590 {
1591 QDPIO::cerr << "QDPFileWriter::write - error in creating XML string" << std::endl;
1592 QDP_abort(1);
1593 }
1594
1595 // Big call to qio
1596 if (QIO_write(get(), info, xml_c,
1598 s1.size()*sizeof(T),
1599 sizeof(typename WordType<T>::Type_t),
1600 (void*)&s1) != QIO_SUCCESS)
1601 {
1602 QDPIO::cerr << "QDPFileWriter: error in write" << std::endl;
1604 }
1605
1606 // Cleanup
1607 QIO_string_destroy(xml_c);
1609 }
1610
1611
1612
1614
1623 template<class T>
1624 void QDPFileWriter::write(XMLBufferWriter& rec_xml, const multi1d< OLattice<T> >& s1, const multi1d<int>& lower_left, const multi1d<int>& upper_right)
1625 {
1626
1627 // Sanity check...
1628 if( lower_left.size() != upper_right.size()) {
1629 QDPIO::cerr << "QDPFileWriter: Error! Lower left and upper right corner of hypercube to write have different dimensions" << std::endl;
1630 QDP_abort(1);
1631 }
1632
1634 (int *)(lower_left.slice()),
1635 (int *)(upper_right.slice()),
1636 lower_left.size(),
1640 sizeof(T), s1.size() );
1641
1642 // Copy metadata string into simple qio string container
1643 QIO_String* xml_c = QIO_string_create();
1644 if (Layout::primaryNode())
1645 QIO_string_set(xml_c, rec_xml.str().c_str());
1646
1647 if (xml_c == NULL)
1648 {
1649 QDPIO::cerr << "QDPFileWriter::write - error in creating XML string" << std::endl;
1650 QDP_abort(1);
1651 }
1652
1653 // Big call to qio
1654 if (QIO_write(get(), info, xml_c,
1656 s1.size()*sizeof(T),
1657 sizeof(typename WordType<T>::Type_t),
1658 (void*)&s1) != QIO_SUCCESS)
1659 {
1660 QDPIO::cerr << "QDPFileWriter: error in write" << std::endl;
1662 }
1663
1664 // Cleanup
1665 QIO_string_destroy(xml_c);
1667 }
1668 // end of group qio
1670} // namespace QDP
1671
1672#endif
Binary buffer input class.
Definition qdp_io.h:905
Binary buffer output class.
Definition qdp_io.h:1390
Outer grid Lattice type.
Definition qdp_outer.h:264
T * getF() const
The backdoor.
Definition qdp_outer.h:390
Outer grid Scalar class *‍/.
Definition qdp_outer.h:37
Primitive color Matrix class.
Primitive color Vector class.
Primitive Spin Matrix class.
Primitive spin Vector class.
bool bad() const
Query whether an unrecoverable error has occurred.
Definition qdp_qdpio.cc:197
void close()
Closes the last file opened.
Definition qdp_qdpio.cc:181
void open(XMLReader &xml, const std::string &path, QDP_serialparallel_t qdp_serpar)
Opens a file for reading.
Definition qdp_qdpio.cc:113
~QDPFileReader()
Closes the last file opened.
Definition qdp_qdpio.cc:204
bool is_open()
Queries whether a file is open.
Definition qdp_qdpio.cc:193
void read(XMLReader &xml, QDPType< T, C > &s1)
Read a QDP object.
Definition qdp_qdpio.h:370
void read(XMLReader &xml)
Reads a record header only.
Definition qdp_qdpio.cc:221
QIO_Reader * get() const
Definition qdp_qdpio.h:411
bool eof() const
Query whether the end-of-file has been reached.
Definition qdp_qdpio.cc:195
void clear(QDP_iostate_t state=QDPIO_goodbit)
Sets a new value for the IO status, ignoring the existing value.
Definition qdp_qdpio.cc:199
QDPFileReader()
Partial constructor.
Definition qdp_qdpio.cc:106
QIO writer class.
Definition qdp_qdpio.h:509
bool bad() const
Query whether an unrecoverable error has occurred.
Definition qdp_qdpio.cc:501
QDPFileWriter()
Partial constructor.
Definition qdp_qdpio.cc:294
~QDPFileWriter()
Closes the last file opened.
Definition qdp_qdpio.cc:508
void write(XMLBufferWriter &xml, const QDPType< T, C > &s1)
Write a QDP object.
Definition qdp_qdpio.h:633
void clear(QDP_iostate_t state=QDPIO_goodbit)
Sets a new value for the control state ignoring the existing value.
Definition qdp_qdpio.cc:503
void close()
Closes the last file opened.
Definition qdp_qdpio.cc:487
void open(XMLBufferWriter &xml, const std::string &path, QDP_volfmt_t qdp_volfmt, QDP_serialparallel_t qdp_serpar)
Opens a file for writing and writes the file metadata.
Definition qdp_qdpio.cc:316
bool is_open()
Queries whether a file is open.
Definition qdp_qdpio.cc:499
QIO_Writer * get() const
Definition qdp_qdpio.h:714
QDPType - major type class/container for all QDP objects.
Definition qdp_qdptype.h:29
Reality complex.
Scalar reality (not complex).
Definition qdp_reality.h:28
Writes XML metadata to a buffer.
Definition qdp_xmlio.h:1024
std::string str() const
Return entire buffer as a string.
XML reader class.
Definition qdp_xmlio.h:44
void open(const std::string &filename)
Opens and reads an XML file.
Definition qdp_xmlio.cc:45
Container for a multi-dimensional 1D array.
Definition qdp_multi.h:25
int size() const
Size of array.
Definition qdp_multi.h:60
const T * slice() const
Return ref to a column slice.
Definition qdp_multi.h:225
void write(BinaryWriter &bin, const std::string &output)
Definition qdp_io.cc:1204
void read(BinaryReader &bin, std::string &input, size_t maxBytes)
Definition qdp_io.cc:778
void zero_rep(IScalar< T > &dest)
dest = 0
Definition qdp_inner.h:1841
OLattice< PScalar< PColorMatrix< RComplexFloat, 3 > > > C
void QDPOScalarFactoryPut(char *buf, size_t linear, int count, void *arg)
Function for moving data.
Definition qdp_qdpio.h:834
QDP_serialparallel_t
File access mode.
Definition qdp_qdpio.h:27
bool is_open(QDPFileReader &qsw)
Is a QDPFileReader open.
Definition qdp_qdpio.cc:213
char * QIOStringTraits< float >::tprec
char * QIOStringTraits< multi1d< LatticeColorMatrixF3 > >::tname
void QDPOLatticeFactoryPut(char *buf, size_t linear, int count, void *arg)
Writes a BinaryBufferWriter object.
Definition qdp_qdpio.h:1183
void close(QDPFileReader &qsw)
Close a QDPFileReader.
Definition qdp_qdpio.cc:207
QDP_filemode_t
File open mode.
Definition qdp_qdpio.h:42
int NumSpins< RScalar< T > >::value
Definition qdp_qdpio.h:172
void QDPOLatticeFactoryGet(char *buf, size_t linear, int count, void *arg)
Function for moving data.
Definition qdp_qdpio.h:1433
void QDPOScalarFactoryGet(char *buf, size_t linear, int count, void *arg)
Function for moving data.
Definition qdp_qdpio.h:1051
QDP_volfmt_t
File format.
Definition qdp_qdpio.h:34
QDP_iostate_t
QDPIO state.
Definition qdp_qdpio.h:50
void QDPOLatticeFactoryGetArray(char *buf, size_t linear, int count, void *arg)
Function for moving array data.
Definition qdp_qdpio.h:1452
void QDPOLatticeFactoryPutArray(char *buf, size_t linear, int count, void *arg)
Function for moving array data.
Definition qdp_qdpio.h:1203
@ QDPIO_PARALLEL
Definition qdp_qdpio.h:29
@ QDPIO_SERIAL
Definition qdp_qdpio.h:28
@ QDPIO_CREATE
Definition qdp_qdpio.h:43
@ QDPIO_OPEN
Definition qdp_qdpio.h:44
@ QDPIO_APPEND
Definition qdp_qdpio.h:45
@ QDPIO_PARTFILE
Definition qdp_qdpio.h:37
@ QDPIO_SINGLEFILE
Definition qdp_qdpio.h:35
@ QDPIO_MULTIFILE
Definition qdp_qdpio.h:36
@ QDPIO_goodbit
Definition qdp_qdpio.h:51
@ QDPIO_failbit
Definition qdp_qdpio.h:53
@ QDPIO_eofbit
Definition qdp_qdpio.h:52
@ QDPIO_badbit
Definition qdp_qdpio.h:54
bool primaryNode()
Returns whether this is the primary node.
StandardOutputStream cout
Definition qdp_stdio.cc:21
StandardOutputStream cerr
Definition qdp_stdio.cc:22
Yet another random number generator.
void QDP_abort(int status)
Panic button.
Traits classes.
#define QIO_GLOBAL
int QIO_read_record_data(QIO_Reader *in, void(*put)(char *buf, size_t index, int count, void *arg), size_t datum_size, int word_size, void *arg)
#define QIO_HYPER
#define QIO_SUCCESS
#define QIO_FIELD
int QIO_write(QIO_Writer *out, QIO_RecordInfo *record_info, QIO_String *xml_record, void(*get)(char *buf, size_t index, int count, void *arg), size_t datum_size, int word_size, void *arg)
int QIO_read_record_info(QIO_Reader *in, QIO_RecordInfo *record_info, QIO_String *xml_record)
void QIO_string_set(QIO_String *qs, const char *const string)
char * QIO_string_ptr(QIO_String *qs)
QIO_String * QIO_string_create(void)
void QIO_string_destroy(QIO_String *qs)
QIO_RecordInfo * QIO_create_record_info(int recordtype, int lower[], int upper[], int n, char *datatype, char *precision, int colors, int spins, int typesize, int datacount)
void QIO_destroy_record_info(QIO_RecordInfo *record_info)
char * QIO_get_precision(QIO_RecordInfo *record_info)
static int value
Definition qdp_qdpio.h:236
static int value
Definition qdp_qdpio.h:159
QIO Type and precision strings.
Definition qdp_qdpio.h:65
static char * tname
Definition qdp_qdpio.h:66
static char * tprec
Definition qdp_qdpio.h:67
Find the underlying word type of a field.
Definition qdp_traits.h:29