/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: ./lib/parallelIO/IldgIO.h Copyright (C) 2015 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. See the full license in the file "LICENSE" in the top level distribution directory *************************************************************************************/ /* END LEGAL */ #ifndef GRID_ILDG_IO_H #define GRID_ILDG_IO_H #ifdef HAVE_LIME #include #include #include #include #include #include #include #include //C-Lime is a must have for this functionality extern "C" { #include "lime.h" } namespace Grid { namespace QCD { ///////////////////////////////// // Encode word types as strings ///////////////////////////////// template inline std::string ScidacWordMnemonic(void){ return std::string("unknown"); } template<> inline std::string ScidacWordMnemonic (void){ return std::string("D"); } template<> inline std::string ScidacWordMnemonic (void){ return std::string("F"); } template<> inline std::string ScidacWordMnemonic< int32_t>(void){ return std::string("I32_t"); } template<> inline std::string ScidacWordMnemonic(void){ return std::string("U32_t"); } template<> inline std::string ScidacWordMnemonic< int64_t>(void){ return std::string("I64_t"); } template<> inline std::string ScidacWordMnemonic(void){ return std::string("U64_t"); } ///////////////////////////////////////// // Encode a generic tensor as a string ///////////////////////////////////////// template std::string ScidacRecordTypeString(int &colors, int &spins, int & typesize,int &datacount) { typedef typename getPrecision::real_scalar_type stype; int _ColourN = indexRank(); int _ColourScalar = isScalar(); int _ColourVector = isVector(); int _ColourMatrix = isMatrix(); int _SpinN = indexRank(); int _SpinScalar = isScalar(); int _SpinVector = isVector(); int _SpinMatrix = isMatrix(); int _LorentzN = indexRank(); int _LorentzScalar = isScalar(); int _LorentzVector = isVector(); int _LorentzMatrix = isMatrix(); std::stringstream stream; stream << "GRID_"; stream << ScidacWordMnemonic(); // std::cout << " Lorentz N/S/V/M : " << _LorentzN<<" "<<_LorentzScalar<<"/"<<_LorentzVector<<"/"<<_LorentzMatrix< std::string ScidacRecordTypeString(Lattice & lat,int &colors, int &spins, int & typesize,int &datacount) { return ScidacRecordTypeString(colors,spins,typesize,datacount); }; //////////////////////////////////////////////////////////// // Helper to fill out metadata //////////////////////////////////////////////////////////// template void ScidacMetaData(Lattice & field, FieldMetaData &header, scidacRecord & _scidacRecord, scidacFile & _scidacFile) { typedef typename getPrecision::real_scalar_type stype; ///////////////////////////////////// // Pull Grid's metadata ///////////////////////////////////// PrepareMetaData(field,header); ///////////////////////////////////// // Scidac Private File structure ///////////////////////////////////// _scidacFile = scidacFile(field._grid); ///////////////////////////////////// // Scidac Private Record structure ///////////////////////////////////// scidacRecord sr; sr.datatype = ScidacRecordTypeString(field,sr.colors,sr.spins,sr.typesize,sr.datacount); sr.date = header.creation_date; sr.precision = ScidacWordMnemonic(); sr.recordtype = GRID_IO_FIELD; _scidacRecord = sr; std::cout << GridLogMessage << "Build SciDAC datatype " < void readLimeLatticeBinaryObject(Lattice &field,std::string record_name) { typedef typename vobj::scalar_object sobj; scidacChecksum scidacChecksum_; uint32_t nersc_csum,scidac_csuma,scidac_csumb; std::string format = getFormatString(); while ( limeReaderNextRecord(LimeR) == LIME_SUCCESS ) { std::cout << GridLogMessage << limeReaderType(LimeR) < munge; BinaryIO::readLatticeObject< sobj, sobj >(field, filename, munge, offset, format,nersc_csum,scidac_csuma,scidac_csumb); ///////////////////////////////////////////// // Insist checksum is next record ///////////////////////////////////////////// readLimeObject(scidacChecksum_,std::string("scidacChecksum"),record_name); ///////////////////////////////////////////// // Verify checksums ///////////////////////////////////////////// scidacChecksumVerify(scidacChecksum_,scidac_csuma,scidac_csumb); return; } } } //////////////////////////////////////////// // Read a generic serialisable object //////////////////////////////////////////// template void readLimeObject(serialisable_object &object,std::string object_name,std::string record_name) { std::string xmlstring; // should this be a do while; can we miss a first record?? while ( limeReaderNextRecord(LimeR) == LIME_SUCCESS ) { uint64_t nbytes = limeReaderBytes(LimeR);//size of this record (configuration) if ( strncmp(limeReaderType(LimeR), record_name.c_str(),strlen(record_name.c_str()) ) ) { std::vector xmlc(nbytes+1,'\0'); limeReaderReadData((void *)&xmlc[0], &nbytes, LimeR); XmlReader RD(&xmlc[0],""); read(RD,object_name,object); return; } } assert(0); } }; class GridLimeWriter : public BinaryIO { public: /////////////////////////////////////////////////// // FIXME: format for RNG? Now just binary out instead /////////////////////////////////////////////////// FILE *File; LimeWriter *LimeW; std::string filename; void open(std::string &_filename) { filename= _filename; File = fopen(filename.c_str(), "w"); LimeW = limeCreateWriter(File); assert(LimeW != NULL ); } ///////////////////////////////////////////// // Close the file ///////////////////////////////////////////// void close(void) { fclose(File); // limeDestroyWriter(LimeW); } /////////////////////////////////////////////////////// // Lime utility functions /////////////////////////////////////////////////////// int createLimeRecordHeader(std::string message, int MB, int ME, size_t PayloadSize) { LimeRecordHeader *h; h = limeCreateHeader(MB, ME, const_cast(message.c_str()), PayloadSize); assert(limeWriteRecordHeader(h, LimeW) >= 0); limeDestroyHeader(h); return LIME_SUCCESS; } //////////////////////////////////////////// // Write a generic serialisable object //////////////////////////////////////////// template void writeLimeObject(int MB,int ME,serialisable_object &object,std::string object_name,std::string record_name) { std::string xmlstring; { XmlWriter WR("",""); write(WR,object_name,object); xmlstring = WR.XmlString(); } uint64_t nbytes = xmlstring.size(); int err; LimeRecordHeader *h = limeCreateHeader(MB, ME,(char *)record_name.c_str(), nbytes); assert(h!= NULL); err=limeWriteRecordHeader(h, LimeW); assert(err>=0); err=limeWriteRecordData(&xmlstring[0], &nbytes, LimeW); assert(err>=0); err=limeWriterCloseRecord(LimeW); assert(err>=0); limeDestroyHeader(h); } //////////////////////////////////////////// // Write a generic lattice field and csum //////////////////////////////////////////// template void writeLimeLatticeBinaryObject(Lattice &field,std::string record_name) { //////////////////////////////////////////// // Create record header //////////////////////////////////////////// typedef typename vobj::scalar_object sobj; int err; uint32_t nersc_csum,scidac_csuma,scidac_csumb; uint64_t PayloadSize = sizeof(sobj) * field._grid->_gsites; createLimeRecordHeader(record_name, 0, 0, PayloadSize); //////////////////////////////////////////////////////////////////// // NB: FILE and iostream are jointly writing disjoint sequences in the // the same file through different file handles (integer units). // // These are both buffered, so why I think this code is right is as follows. // // i) write record header to FILE *File, telegraphing the size. // ii) ftell reads the offset from FILE *File . // iii) iostream / MPI Open independently seek this offset. Write sequence direct to disk. // Closes iostream and flushes. // iv) fseek on FILE * to end of this disjoint section. // v) Continue writing scidac record. //////////////////////////////////////////////////////////////////// off_t offset = ftell(File); std::string format = getFormatString(); BinarySimpleMunger munge; BinaryIO::writeLatticeObject(field, filename, munge, offset, format,nersc_csum,scidac_csuma,scidac_csumb); err=limeWriterCloseRecord(LimeW); assert(err>=0); //////////////////////////////////////// // Write checksum element, propagaing forward from the BinaryIO // Always pair a checksum with a binary object, and close message //////////////////////////////////////// scidacChecksum checksum; std::stringstream streama; streama << std::hex << scidac_csuma; std::stringstream streamb; streamb << std::hex << scidac_csumb; checksum.suma= streama.str(); checksum.sumb= streamb.str(); std::cout << GridLogMessage<<" writing scidac checksums "< void writeScidacFileRecord(GridBase *grid,SerialisableUserFile &_userFile) { scidacFile _scidacFile(grid); writeLimeObject(1,0,_scidacFile,_scidacFile.SerialisableClassName(),std::string(SCIDAC_PRIVATE_FILE_XML)); writeLimeObject(0,1,_userFile,_userFile.SerialisableClassName(),std::string(SCIDAC_FILE_XML)); } //////////////////////////////////////////////// // Write generic lattice field in scidac format //////////////////////////////////////////////// template void writeScidacFieldRecord(Lattice &field,userRecord _userRecord) { typedef typename vobj::scalar_object sobj; uint64_t nbytes; GridBase * grid = field._grid; //////////////////////////////////////// // fill the Grid header //////////////////////////////////////// FieldMetaData header; scidacRecord _scidacRecord; scidacFile _scidacFile; ScidacMetaData(field,header,_scidacRecord,_scidacFile); ////////////////////////////////////////////// // Fill the Lime file record by record ////////////////////////////////////////////// writeLimeObject(1,0,header ,std::string("FieldMetaData"),std::string(GRID_FORMAT)); // Open message writeLimeObject(0,0,_userRecord,_userRecord.SerialisableClassName(),std::string(SCIDAC_RECORD_XML)); writeLimeObject(0,0,_scidacRecord,_scidacRecord.SerialisableClassName(),std::string(SCIDAC_PRIVATE_RECORD_XML)); writeLimeLatticeBinaryObject(field,std::string(ILDG_BINARY_DATA)); // Closes message with checksum } }; class IldgWriter : public ScidacWriter { public: /////////////////////////////////// // A little helper /////////////////////////////////// void writeLimeIldgLFN(std::string &LFN) { uint64_t PayloadSize = LFN.size(); int err; createLimeRecordHeader(ILDG_DATA_LFN, 0 , 0, PayloadSize); err=limeWriteRecordData(const_cast(LFN.c_str()), &PayloadSize,LimeW); assert(err>=0); err=limeWriterCloseRecord(LimeW); assert(err>=0); } //////////////////////////////////////////////////////////////// // Special ILDG operations ; gauge configs only. // Don't require scidac records EXCEPT checksum // Use Grid MetaData object if present. //////////////////////////////////////////////////////////////// template void writeConfiguration(Lattice > &Umu,int sequence,std::string LFN,std::string description) { GridBase * grid = Umu._grid; typedef Lattice > GaugeField; typedef iLorentzColourMatrix vobj; typedef typename vobj::scalar_object sobj; uint64_t nbytes; //////////////////////////////////////// // fill the Grid header //////////////////////////////////////// FieldMetaData header; scidacRecord _scidacRecord; scidacFile _scidacFile; ScidacMetaData(Umu,header,_scidacRecord,_scidacFile); std::string format = header.floating_point; header.ensemble_id = description; header.ensemble_label = description; header.sequence_number = sequence; header.ildg_lfn = LFN; assert ( (format == std::string("IEEE32BIG")) ||(format == std::string("IEEE64BIG")) ); ////////////////////////////////////////////////////// // Fill ILDG header data struct ////////////////////////////////////////////////////// ildgFormat ildgfmt ; ildgfmt.field = std::string("su3gauge"); if ( format == std::string("IEEE32BIG") ) { ildgfmt.precision = 32; } else { ildgfmt.precision = 64; } ildgfmt.version = 1.0; ildgfmt.lx = header.dimension[0]; ildgfmt.ly = header.dimension[1]; ildgfmt.lz = header.dimension[2]; ildgfmt.lt = header.dimension[3]; assert(header.nd==4); assert(header.nd==header.dimension.size()); ////////////////////////////////////////////////////////////////////////////// // Fill the USQCD info field ////////////////////////////////////////////////////////////////////////////// usqcdInfo info; info.version=1.0; info.plaq = header.plaquette; info.linktr = header.link_trace; std::cout << GridLogMessage << " Writing config; IldgIO "< void readConfiguration(Lattice > &Umu, FieldMetaData &FieldMetaData_) { typedef Lattice > GaugeField; typedef typename GaugeField::vector_object vobj; typedef typename vobj::scalar_object sobj; typedef LorentzColourMatrixF fobj; typedef LorentzColourMatrixD dobj; GridBase *grid = Umu._grid; std::vector dims = Umu._grid->FullDimensions(); assert(dims.size()==4); // Metadata holders ildgFormat ildgFormat_ ; std::string ildgLFN_ ; scidacChecksum scidacChecksum_; usqcdInfo usqcdInfo_ ; // track what we read from file int found_ildgFormat =0; int found_ildgLFN =0; int found_scidacChecksum=0; int found_usqcdInfo =0; int found_ildgBinary =0; int found_FieldMetaData =0; uint32_t nersc_csum; uint32_t scidac_csuma; uint32_t scidac_csumb; // Binary format std::string format; ////////////////////////////////////////////////////////////////////////// // Loop over all records // -- Order is poorly guaranteed except ILDG header preceeds binary section. // -- Run like an event loop. // -- Impose trust hierarchy. Grid takes precedence & look for ILDG, and failing // that Scidac. // -- Insist on Scidac checksum record. ////////////////////////////////////////////////////////////////////////// while ( limeReaderNextRecord(LimeR) == LIME_SUCCESS ) { uint64_t nbytes = limeReaderBytes(LimeR);//size of this record (configuration) ////////////////////////////////////////////////////////////////// // If not BINARY_DATA read a string and parse ////////////////////////////////////////////////////////////////// if ( strncmp(limeReaderType(LimeR), ILDG_BINARY_DATA,strlen(ILDG_BINARY_DATA) ) ) { // Copy out the string std::vector xmlc(nbytes+1,'\0'); limeReaderReadData((void *)&xmlc[0], &nbytes, LimeR); std::cout << GridLogMessage<< "Non binary record :" < munge; BinaryIO::readLatticeObject< vobj, dobj >(Umu, filename, munge, offset, format,nersc_csum,scidac_csuma,scidac_csumb); } else { GaugeSimpleMunger munge; BinaryIO::readLatticeObject< vobj, fobj >(Umu, filename, munge, offset, format,nersc_csum,scidac_csuma,scidac_csumb); } found_ildgBinary = 1; } } ////////////////////////////////////////////////////// // Minimally must find binary segment and checksum // Since this is an ILDG reader require ILDG format ////////////////////////////////////////////////////// assert(found_ildgBinary); assert(found_ildgFormat); assert(found_scidacChecksum); // Must find something with the lattice dimensions assert(found_FieldMetaData||found_ildgFormat); if ( found_FieldMetaData ) { std::cout << GridLogMessage<<"Grid MetaData was record found: configuration was probably written by Grid ! Yay ! "<