/************************************************************************************* 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(); if ( _LorentzVector ) stream << "_LorentzVector"<<_LorentzN; if ( _LorentzMatrix ) stream << "_LorentzMatrix"<<_LorentzN; if ( _SpinVector ) stream << "_SpinVector"<<_SpinN; if ( _SpinMatrix ) stream << "_SpinMatrix"<<_SpinN; if ( _ColourVector ) stream << "_ColourVector"<<_ColourN; if ( _ColourMatrix ) stream << "_ColourMatrix"<<_ColourN; if ( _ColourScalar && _LorentzScalar && _SpinScalar ) stream << "_Complex"; typesize = sizeof(typename vobj::scalar_type); if ( _ColourMatrix ) typesize*= _ColourN*_ColourN; else typesize*= _ColourN; if ( _SpinMatrix ) typesize*= _SpinN*_SpinN; else typesize*= _SpinN; colors = _ColourN; spins = _SpinN; datacount = _LorentzN; return stream.str(); } template 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 ) { uint64_t file_bytes =limeReaderBytes(LimeR); // std::cout << GridLogMessage << limeReaderType(LimeR) << " "<< file_bytes <<" bytes "<_gsites; // std::cout << "R sizeof(sobj)= " <_gsites< munge; BinaryIO::readLatticeObject< vobj, sobj >(field, filename, munge, offset, format,nersc_csum,scidac_csuma,scidac_csumb); ///////////////////////////////////////////// // Insist checksum is next record ///////////////////////////////////////////// readLimeObject(scidacChecksum_,std::string("scidacChecksum"),std::string(SCIDAC_CHECKSUM)); ///////////////////////////////////////////// // 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 ) { // std::cout << GridLogMessage<< " readLimeObject seeking "<< record_name <<" found record :" < xmlc(nbytes+1,'\0'); limeReaderReadData((void *)&xmlc[0], &nbytes, LimeR); // std::cout << GridLogMessage<< " readLimeObject matches XML " << &xmlc[0] <(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(); } // std::cout << "WriteLimeObject" << record_name <(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); // std::cout << " File offset is now"< 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); // std::cout << "W sizeof(sobj)" <_gsites<(); 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) { 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 ScidacReader : public GridLimeReader { public: template void readScidacFileRecord(GridBase *grid,SerialisableUserFile &_userFile) { scidacFile _scidacFile(grid); readLimeObject(_scidacFile,_scidacFile.SerialisableClassName(),std::string(SCIDAC_PRIVATE_FILE_XML)); readLimeObject(_userFile,_userFile.SerialisableClassName(),std::string(SCIDAC_FILE_XML)); } //////////////////////////////////////////////// // Write generic lattice field in scidac format //////////////////////////////////////////////// template void readScidacFieldRecord(Lattice &field,userRecord &_userRecord) { typedef typename vobj::scalar_object sobj; GridBase * grid = field._grid; //////////////////////////////////////// // fill the Grid header //////////////////////////////////////// FieldMetaData header; scidacRecord _scidacRecord; scidacFile _scidacFile; ////////////////////////////////////////////// // Fill the Lime file record by record ////////////////////////////////////////////// readLimeObject(header ,std::string("FieldMetaData"),std::string(GRID_FORMAT)); // Open message readLimeObject(_userRecord,_userRecord.SerialisableClassName(),std::string(SCIDAC_RECORD_XML)); readLimeObject(_scidacRecord,_scidacRecord.SerialisableClassName(),std::string(SCIDAC_PRIVATE_RECORD_XML)); readLimeLatticeBinaryObject(field,std::string(ILDG_BINARY_DATA)); } void skipPastBinaryRecord(void) { std::string rec_name(ILDG_BINARY_DATA); while ( limeReaderNextRecord(LimeR) == LIME_SUCCESS ) { if ( !strncmp(limeReaderType(LimeR), rec_name.c_str(),strlen(rec_name.c_str()) ) ) { skipPastObjectRecord(std::string(SCIDAC_CHECKSUM)); return; } } } void skipPastObjectRecord(std::string rec_name) { while ( limeReaderNextRecord(LimeR) == LIME_SUCCESS ) { if ( !strncmp(limeReaderType(LimeR), rec_name.c_str(),strlen(rec_name.c_str()) ) ) { return; } } } void skipScidacFieldRecord() { skipPastObjectRecord(std::string(GRID_FORMAT)); skipPastObjectRecord(std::string(SCIDAC_RECORD_XML)); skipPastObjectRecord(std::string(SCIDAC_PRIVATE_RECORD_XML)); skipPastBinaryRecord(); } }; 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; //////////////////////////////////////// // 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 ! "<