mirror of
				https://github.com/paboyle/Grid.git
				synced 2025-11-01 04:24:32 +00:00 
			
		
		
		
	IO improvements to fail on IO error
This commit is contained in:
		| @@ -35,37 +35,27 @@ Author: paboyle <paboyle@ph.ed.ac.uk> | |||||||
| #endif | #endif | ||||||
| #include <arpa/inet.h> | #include <arpa/inet.h> | ||||||
| #include <algorithm> | #include <algorithm> | ||||||
| // 64bit endian swap is a portability pain |  | ||||||
| #ifndef __has_builtin         // Optional of course. |  | ||||||
| #define __has_builtin(x) 0  // Compatibility with non-clang compilers. |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| #if HAVE_DECL_BE64TOH  |  | ||||||
| #undef Grid_ntohll |  | ||||||
| #define Grid_ntohll be64toh |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| #if HAVE_DECL_NTOHLL | inline uint32_t byte_reverse32(uint32_t f) {  | ||||||
| #undef  Grid_ntohll |       f = ((f&0xFF)<<24) | ((f&0xFF00)<<8) | ((f&0xFF0000)>>8) | ((f&0xFF000000UL)>>24) ;  | ||||||
| #define Grid_ntohll ntohll |       return f; | ||||||
| #endif | } | ||||||
|  | inline uint64_t byte_reverse64(uint64_t f) {  | ||||||
| #ifndef Grid_ntohll |   uint64_t g; | ||||||
|  |   g = ((f&0xFF)<<24) | ((f&0xFF00)<<8) | ((f&0xFF0000)>>8) | ((f&0xFF000000UL)>>24) ;  | ||||||
|  |   g = g << 32; | ||||||
|  |   f = f >> 32; | ||||||
|  |   g|= ((f&0xFF)<<24) | ((f&0xFF00)<<8) | ((f&0xFF0000)>>8) | ((f&0xFF000000UL)>>24) ;  | ||||||
|  |   return g; | ||||||
|  | } | ||||||
|  |  | ||||||
| #if BYTE_ORDER == BIG_ENDIAN  | #if BYTE_ORDER == BIG_ENDIAN  | ||||||
|  | inline uint64_t Grid_ntohll(uint64_t A) { return A; } | ||||||
| #define Grid_ntohll(A) (A) |  | ||||||
|  |  | ||||||
| #else | #else | ||||||
|  | inline uint64_t Grid_ntohll(uint64_t A) {  | ||||||
| #if __has_builtin(__builtin_bswap64) |   return byte_reverse64(A); | ||||||
| #define Grid_ntohll(A) __builtin_bswap64(A) | } | ||||||
| #else |  | ||||||
| #error |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| namespace Grid {  | namespace Grid {  | ||||||
| @@ -195,7 +185,7 @@ class BinaryIO { | |||||||
|       std::vector<int> site({x,y,z,t}); |       std::vector<int> site({x,y,z,t}); | ||||||
|  |  | ||||||
|       if (grid->IsBoss()) { |       if (grid->IsBoss()) { | ||||||
|         fin.read((char *)&file_object, sizeof(file_object)); |         fin.read((char *)&file_object, sizeof(file_object));assert( fin.fail()==0); | ||||||
|         bytes += sizeof(file_object); |         bytes += sizeof(file_object); | ||||||
|         if (ieee32big) be32toh_v((void *)&file_object, sizeof(file_object)); |         if (ieee32big) be32toh_v((void *)&file_object, sizeof(file_object)); | ||||||
|         if (ieee32) le32toh_v((void *)&file_object, sizeof(file_object)); |         if (ieee32) le32toh_v((void *)&file_object, sizeof(file_object)); | ||||||
| @@ -211,11 +201,13 @@ class BinaryIO { | |||||||
|     std::cout<<GridLogPerformance<<"readObjectSerial: read "<< bytes <<" bytes in "<<timer.Elapsed() <<" " |     std::cout<<GridLogPerformance<<"readObjectSerial: read "<< bytes <<" bytes in "<<timer.Elapsed() <<" " | ||||||
|        << (double)bytes/ (double)timer.useconds() <<" MB/s "  <<std::endl; |        << (double)bytes/ (double)timer.useconds() <<" MB/s "  <<std::endl; | ||||||
|  |  | ||||||
|  |     grid->Broadcast(0,(void *)&csum,sizeof(csum)); | ||||||
|     return csum; |     return csum; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   template<class vobj,class fobj,class munger>  |   template<class vobj,class fobj,class munger>  | ||||||
|   static inline uint32_t writeObjectSerial(Lattice<vobj> &Umu,std::string file,munger munge,int offset,const std::string & format) |   static inline uint32_t writeObjectSerial(Lattice<vobj> &Umu,std::string file,munger munge,int offset, | ||||||
|  | 					   const std::string & format) | ||||||
|   { |   { | ||||||
|     typedef typename vobj::scalar_object sobj; |     typedef typename vobj::scalar_object sobj; | ||||||
|  |  | ||||||
| @@ -261,7 +253,7 @@ class BinaryIO { | |||||||
| 	if(ieee64)    htole64_v((void *)&file_object,sizeof(file_object)); | 	if(ieee64)    htole64_v((void *)&file_object,sizeof(file_object)); | ||||||
|  |  | ||||||
| 	// NB could gather an xstrip as an optimisation. | 	// NB could gather an xstrip as an optimisation. | ||||||
|   fout.write((char *)&file_object,sizeof(file_object)); | 	fout.write((char *)&file_object,sizeof(file_object));assert( fout.fail()==0); | ||||||
| 	bytes+=sizeof(file_object); | 	bytes+=sizeof(file_object); | ||||||
|       } |       } | ||||||
|     }}}} |     }}}} | ||||||
| @@ -269,6 +261,7 @@ class BinaryIO { | |||||||
|     std::cout<<GridLogPerformance<<"writeObjectSerial: wrote "<< bytes <<" bytes in "<<timer.Elapsed() <<" " |     std::cout<<GridLogPerformance<<"writeObjectSerial: wrote "<< bytes <<" bytes in "<<timer.Elapsed() <<" " | ||||||
| 	     << (double)bytes/timer.useconds() <<" MB/s "  <<std::endl; | 	     << (double)bytes/timer.useconds() <<" MB/s "  <<std::endl; | ||||||
|      |      | ||||||
|  |     grid->Broadcast(0,(void *)&csum,sizeof(csum)); | ||||||
|     return csum; |     return csum; | ||||||
|   } |   } | ||||||
|    |    | ||||||
| @@ -313,7 +306,7 @@ class BinaryIO { | |||||||
|  |  | ||||||
|       if ( grid->IsBoss() ) { |       if ( grid->IsBoss() ) { | ||||||
| 	Uint32Checksum((uint32_t *)&saved[0],bytes,csum); | 	Uint32Checksum((uint32_t *)&saved[0],bytes,csum); | ||||||
|   fout.write((char *)&saved[0],bytes); | 	fout.write((char *)&saved[0],bytes);assert( fout.fail()==0); | ||||||
|       } |       } | ||||||
|        |        | ||||||
|     } |     } | ||||||
| @@ -321,7 +314,7 @@ class BinaryIO { | |||||||
|     if ( grid->IsBoss() ) { |     if ( grid->IsBoss() ) { | ||||||
|       serial.GetState(saved,0); |       serial.GetState(saved,0); | ||||||
|       Uint32Checksum((uint32_t *)&saved[0],bytes,csum); |       Uint32Checksum((uint32_t *)&saved[0],bytes,csum); | ||||||
|       fout.write((char *)&saved[0],bytes); |       fout.write((char *)&saved[0],bytes);assert( fout.fail()==0); | ||||||
|     } |     } | ||||||
|     grid->Broadcast(0,(void *)&csum,sizeof(csum)); |     grid->Broadcast(0,(void *)&csum,sizeof(csum)); | ||||||
|     return csum; |     return csum; | ||||||
| @@ -355,7 +348,7 @@ class BinaryIO { | |||||||
|       int l_idx=parallel.generator_idx(o_idx,i_idx); |       int l_idx=parallel.generator_idx(o_idx,i_idx); | ||||||
|  |  | ||||||
|       if ( grid->IsBoss() ) { |       if ( grid->IsBoss() ) { | ||||||
|   fin.read((char *)&saved[0],bytes); | 	fin.read((char *)&saved[0],bytes);assert( fin.fail()==0); | ||||||
| 	Uint32Checksum((uint32_t *)&saved[0],bytes,csum); | 	Uint32Checksum((uint32_t *)&saved[0],bytes,csum); | ||||||
|       } |       } | ||||||
|  |  | ||||||
| @@ -368,7 +361,7 @@ class BinaryIO { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     if ( grid->IsBoss() ) { |     if ( grid->IsBoss() ) { | ||||||
|       fin.read((char *)&saved[0],bytes); |       fin.read((char *)&saved[0],bytes);assert( fin.fail()==0); | ||||||
|       serial.SetState(saved,0); |       serial.SetState(saved,0); | ||||||
|       Uint32Checksum((uint32_t *)&saved[0],bytes,csum); |       Uint32Checksum((uint32_t *)&saved[0],bytes,csum); | ||||||
|     } |     } | ||||||
| @@ -380,7 +373,8 @@ class BinaryIO { | |||||||
|  |  | ||||||
|  |  | ||||||
|   template<class vobj,class fobj,class munger> |   template<class vobj,class fobj,class munger> | ||||||
|   static inline uint32_t readObjectParallel(Lattice<vobj> &Umu,std::string file,munger munge,int offset,const std::string &format) |   static inline uint32_t readObjectParallel(Lattice<vobj> &Umu,std::string file,munger munge,int offset, | ||||||
|  | 					    const std::string &format) | ||||||
|   { |   { | ||||||
|     typedef typename vobj::scalar_object sobj; |     typedef typename vobj::scalar_object sobj; | ||||||
|  |  | ||||||
| @@ -489,7 +483,7 @@ class BinaryIO { | |||||||
|       if (myrank == iorank) { |       if (myrank == iorank) { | ||||||
|    |    | ||||||
| 	fin.seekg(offset+g_idx*sizeof(fileObj)); | 	fin.seekg(offset+g_idx*sizeof(fileObj)); | ||||||
|   fin.read((char *)&fileObj,sizeof(fileObj)); | 	fin.read((char *)&fileObj,sizeof(fileObj));assert( fin.fail()==0); | ||||||
| 	bytes+=sizeof(fileObj); | 	bytes+=sizeof(fileObj); | ||||||
|    |    | ||||||
| 	if(ieee32big) be32toh_v((void *)&fileObj,sizeof(fileObj)); | 	if(ieee32big) be32toh_v((void *)&fileObj,sizeof(fileObj)); | ||||||
| @@ -529,7 +523,8 @@ class BinaryIO { | |||||||
|   // Parallel writer |   // Parallel writer | ||||||
|   ////////////////////////////////////////////////////////// |   ////////////////////////////////////////////////////////// | ||||||
|   template<class vobj,class fobj,class munger> |   template<class vobj,class fobj,class munger> | ||||||
|   static inline uint32_t writeObjectParallel(Lattice<vobj> &Umu,std::string file,munger munge,int offset,const std::string & format) |   static inline uint32_t writeObjectParallel(Lattice<vobj> &Umu,std::string file,munger munge,int offset, | ||||||
|  | 					     const std::string & format) | ||||||
|   { |   { | ||||||
|     typedef typename vobj::scalar_object sobj; |     typedef typename vobj::scalar_object sobj; | ||||||
|     GridBase *grid = Umu._grid; |     GridBase *grid = Umu._grid; | ||||||
| @@ -658,7 +653,7 @@ class BinaryIO { | |||||||
| 	if(ieee64)    htole64_v((void *)&fileObj,sizeof(fileObj)); | 	if(ieee64)    htole64_v((void *)&fileObj,sizeof(fileObj)); | ||||||
| 	 | 	 | ||||||
| 	fout.seekp(offset+g_idx*sizeof(fileObj)); | 	fout.seekp(offset+g_idx*sizeof(fileObj)); | ||||||
|   fout.write((char *)&fileObj,sizeof(fileObj)); | 	fout.write((char *)&fileObj,sizeof(fileObj));assert( fout.fail()==0); | ||||||
| 	bytes+=sizeof(fileObj); | 	bytes+=sizeof(fileObj); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -242,7 +242,6 @@ class NerscIO : public BinaryIO { | |||||||
|   static inline unsigned int writeHeader(NerscField &field,std::string file) |   static inline unsigned int writeHeader(NerscField &field,std::string file) | ||||||
|   { |   { | ||||||
|     std::ofstream fout(file,std::ios::out|std::ios::in); |     std::ofstream fout(file,std::ios::out|std::ios::in); | ||||||
|    |  | ||||||
|     fout.seekp(0,std::ios::beg); |     fout.seekp(0,std::ios::beg); | ||||||
|     dump_nersc_header(field, fout); |     dump_nersc_header(field, fout); | ||||||
|     field.data_start = fout.tellp(); |     field.data_start = fout.tellp(); | ||||||
| @@ -264,10 +263,13 @@ static inline int readHeader(std::string file,GridBase *grid,  NerscField &field | |||||||
|   getline(fin,line); // read one line and insist is  |   getline(fin,line); // read one line and insist is  | ||||||
|  |  | ||||||
|   removeWhitespace(line); |   removeWhitespace(line); | ||||||
|  |   std::cout << "* " << line << std::endl; | ||||||
|  |  | ||||||
|   assert(line==std::string("BEGIN_HEADER")); |   assert(line==std::string("BEGIN_HEADER")); | ||||||
|  |  | ||||||
|   do { |   do { | ||||||
|     getline(fin,line); // read one line |     getline(fin,line); // read one line | ||||||
|  |     std::cout << "* "<<line<< std::endl; | ||||||
|     int eq = line.find("="); |     int eq = line.find("="); | ||||||
|     if(eq >0) { |     if(eq >0) { | ||||||
|       std::string key=line.substr(0,eq); |       std::string key=line.substr(0,eq); | ||||||
| @@ -345,24 +347,24 @@ static inline void readConfiguration(Lattice<iLorentzColourMatrix<vsimd> > &Umu, | |||||||
|   // munger is a function of <floating point, Real, data_type> |   // munger is a function of <floating point, Real, data_type> | ||||||
|   if ( header.data_type == std::string("4D_SU3_GAUGE") ) { |   if ( header.data_type == std::string("4D_SU3_GAUGE") ) { | ||||||
|     if ( ieee32 || ieee32big ) { |     if ( ieee32 || ieee32big ) { | ||||||
|       //      csum=BinaryIO::readObjectSerial<iLorentzColourMatrix<vsimd>, LorentzColour2x3F>  |       csum=BinaryIO::readObjectSerial<iLorentzColourMatrix<vsimd>, LorentzColour2x3F>  | ||||||
| 	csum=BinaryIO::readObjectParallel<iLorentzColourMatrix<vsimd>, LorentzColour2x3F>  | 	//	csum=BinaryIO::readObjectParallel<iLorentzColourMatrix<vsimd>, LorentzColour2x3F>  | ||||||
| 	(Umu,file,Nersc3x2munger<LorentzColour2x3F,LorentzColourMatrix>(), offset,format); | 	(Umu,file,Nersc3x2munger<LorentzColour2x3F,LorentzColourMatrix>(), offset,format); | ||||||
|     } |     } | ||||||
|     if ( ieee64 || ieee64big ) { |     if ( ieee64 || ieee64big ) { | ||||||
|       //csum=BinaryIO::readObjectSerial<iLorentzColourMatrix<vsimd>, LorentzColour2x3D>  |       csum=BinaryIO::readObjectSerial<iLorentzColourMatrix<vsimd>, LorentzColour2x3D>  | ||||||
|       csum=BinaryIO::readObjectParallel<iLorentzColourMatrix<vsimd>, LorentzColour2x3D>  | 	//   csum=BinaryIO::readObjectParallel<iLorentzColourMatrix<vsimd>, LorentzColour2x3D>  | ||||||
|       	(Umu,file,Nersc3x2munger<LorentzColour2x3D,LorentzColourMatrix>(),offset,format); |       	(Umu,file,Nersc3x2munger<LorentzColour2x3D,LorentzColourMatrix>(),offset,format); | ||||||
|     } |     } | ||||||
|   } else if ( header.data_type == std::string("4D_SU3_GAUGE_3x3") ) { |   } else if ( header.data_type == std::string("4D_SU3_GAUGE_3x3") ) { | ||||||
|     if ( ieee32 || ieee32big ) { |     if ( ieee32 || ieee32big ) { | ||||||
|       //csum=BinaryIO::readObjectSerial<iLorentzColourMatrix<vsimd>,LorentzColourMatrixF> |       csum=BinaryIO::readObjectSerial<iLorentzColourMatrix<vsimd>,LorentzColourMatrixF> | ||||||
|       csum=BinaryIO::readObjectParallel<iLorentzColourMatrix<vsimd>,LorentzColourMatrixF> | 	//csum=BinaryIO::readObjectParallel<iLorentzColourMatrix<vsimd>,LorentzColourMatrixF> | ||||||
| 	(Umu,file,NerscSimpleMunger<LorentzColourMatrixF,LorentzColourMatrix>(),offset,format); | 	(Umu,file,NerscSimpleMunger<LorentzColourMatrixF,LorentzColourMatrix>(),offset,format); | ||||||
|     } |     } | ||||||
|     if ( ieee64 || ieee64big ) { |     if ( ieee64 || ieee64big ) { | ||||||
|       //      csum=BinaryIO::readObjectSerial<iLorentzColourMatrix<vsimd>,LorentzColourMatrixD> |       csum=BinaryIO::readObjectSerial<iLorentzColourMatrix<vsimd>,LorentzColourMatrixD> | ||||||
|       csum=BinaryIO::readObjectParallel<iLorentzColourMatrix<vsimd>,LorentzColourMatrixD> | 	// csum=BinaryIO::readObjectParallel<iLorentzColourMatrix<vsimd>,LorentzColourMatrixD> | ||||||
| 	(Umu,file,NerscSimpleMunger<LorentzColourMatrixD,LorentzColourMatrix>(),offset,format); | 	(Umu,file,NerscSimpleMunger<LorentzColourMatrixD,LorentzColourMatrix>(),offset,format); | ||||||
|     } |     } | ||||||
|   } else { |   } else { | ||||||
| @@ -371,12 +373,17 @@ static inline void readConfiguration(Lattice<iLorentzColourMatrix<vsimd> > &Umu, | |||||||
|  |  | ||||||
|   NerscStatistics<GaugeField>(Umu,clone); |   NerscStatistics<GaugeField>(Umu,clone); | ||||||
|  |  | ||||||
|  |   std::cout<<GridLogMessage <<"NERSC Configuration "<<file<<" checksum "<<std::hex<<            csum<< std::dec | ||||||
|  | 	                                                  <<" header   "<<std::hex<<header.checksum<<std::dec <<std::endl; | ||||||
|  |   std::cout<<GridLogMessage <<"NERSC Configuration "<<file<<" plaquette "<<clone.plaquette | ||||||
|  | 	                                                  <<" header    "<<header.plaquette<<std::endl; | ||||||
|  |   std::cout<<GridLogMessage <<"NERSC Configuration "<<file<<" link_trace "<<clone.link_trace | ||||||
|  | 	                                                  <<" header    "<<header.link_trace<<std::endl; | ||||||
|   assert(fabs(clone.plaquette -header.plaquette ) < 1.0e-5 ); |   assert(fabs(clone.plaquette -header.plaquette ) < 1.0e-5 ); | ||||||
|   assert(fabs(clone.link_trace-header.link_trace) < 1.0e-6 ); |   assert(fabs(clone.link_trace-header.link_trace) < 1.0e-6 ); | ||||||
|  |  | ||||||
|   assert(csum == header.checksum ); |   assert(csum == header.checksum ); | ||||||
|  |  | ||||||
|   std::cout<<GridLogMessage <<"Read NERSC Configuration "<<file<< " and plaquette, link trace, and checksum agree"<<std::endl; |   std::cout<<GridLogMessage <<"NERSC Configuration "<<file<< " and plaquette, link trace, and checksum agree"<<std::endl; | ||||||
| } | } | ||||||
|  |  | ||||||
| template<class vsimd> | template<class vsimd> | ||||||
| @@ -416,19 +423,8 @@ static inline void writeConfiguration(Lattice<iLorentzColourMatrix<vsimd> > &Umu | |||||||
|     Nersc3x2unmunger<fobj2D,sobj> munge; |     Nersc3x2unmunger<fobj2D,sobj> munge; | ||||||
|     BinaryIO::Uint32Checksum<vobj,fobj2D>(Umu, munge,header.checksum); |     BinaryIO::Uint32Checksum<vobj,fobj2D>(Umu, munge,header.checksum); | ||||||
|     offset = writeHeader(header,file); |     offset = writeHeader(header,file); | ||||||
|     csum=BinaryIO::writeObjectSerial<vobj,fobj2D>(Umu,file,munge,offset,header.floating_point); |     //    csum=BinaryIO::writeObjectSerial<vobj,fobj2D>(Umu,file,munge,offset,header.floating_point); | ||||||
|  |     csum=BinaryIO::writeObjectParallel<vobj,fobj2D>(Umu,file,munge,offset,header.floating_point); | ||||||
|     std::string file1 = file+"para"; |  | ||||||
|     int offset1 = writeHeader(header,file1); |  | ||||||
|     int csum1=BinaryIO::writeObjectParallel<vobj,fobj2D>(Umu,file1,munge,offset,header.floating_point); |  | ||||||
|     //int csum1=BinaryIO::writeObjectSerial<vobj,fobj2D>(Umu,file1,munge,offset,header.floating_point); |  | ||||||
|  |  | ||||||
|      |  | ||||||
|     std::cout << GridLogMessage << " TESTING PARALLEL WRITE offsets " << offset1 << " "<< offset << std::endl; |  | ||||||
|     std::cout << GridLogMessage << " TESTING PARALLEL WRITE csums   " << csum1 << " "<<std::hex<< csum << std::dec<< std::endl; |  | ||||||
|  |  | ||||||
|     assert(offset1==offset);   |  | ||||||
|     assert(csum1==csum);   |  | ||||||
|  |  | ||||||
|   } else {  |   } else {  | ||||||
|     header.floating_point = std::string("IEEE64BIG"); |     header.floating_point = std::string("IEEE64BIG"); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user