diff --git a/Hadrons/EigenPack.hpp b/Hadrons/EigenPack.hpp index 44836ef3..0be998b7 100644 --- a/Hadrons/EigenPack.hpp +++ b/Hadrons/EigenPack.hpp @@ -46,22 +46,24 @@ LOG(Message) << record.operatorXml << std::endl;\ LOG(Message) << "* solver" << std::endl;\ LOG(Message) << record.solverXml << std::endl; +struct PackRecord +{ + std::string operatorXml, solverXml; +}; + +struct VecRecord: Serializable +{ + GRID_SERIALIZABLE_CLASS_MEMBERS(VecRecord, + unsigned int, index, + double, eval); + VecRecord(void): index(0), eval(0.) {} +}; + template class EigenPack { public: typedef F Field; - struct PackRecord - { - std::string operatorXml, solverXml; - }; - struct VecRecord: Serializable - { - GRID_SERIALIZABLE_CLASS_MEMBERS(VecRecord, - unsigned int, index, - double, eval); - VecRecord(void): index(0), eval(0.) {} - }; public: std::vector eval; std::vector evec; @@ -115,6 +117,39 @@ public: basicWrite(evecFilename(fileStem, -1, traj), evec, eval, evec.size()); } } + + static void readHeader(PackRecord &record, ScidacReader &binReader) + { + std::string recordXml; + + binReader.readLimeObject(recordXml, SCIDAC_FILE_XML); + XmlReader xmlReader(recordXml, true, "eigenPackPar"); + xmlReader.push(); + xmlReader.readCurrentSubtree(record.operatorXml); + xmlReader.nextElement(); + xmlReader.readCurrentSubtree(record.solverXml); + } + + template + static void readElement(T &evec, VecRecord &vecRecord, ScidacReader &binReader) + { + binReader.readScidacFieldRecord(evec, vecRecord); + } + + static void writeHeader(ScidacWriter &binWriter, PackRecord &record) + { + XmlWriter xmlWriter("", "eigenPackPar"); + + xmlWriter.pushXmlString(record.operatorXml); + xmlWriter.pushXmlString(record.solverXml); + binWriter.writeLimeObject(1, 1, xmlWriter, "parameters", SCIDAC_FILE_XML); + } + + template + static void writeElement(ScidacWriter &binWriter, T &evec, VecRecord &vecRecord) + { + binWriter.writeScidacFieldRecord(evec, vecRecord, DEFAULT_ASCII_PREC); + } protected: std::string evecFilename(const std::string stem, const int vec, const int traj) { @@ -130,24 +165,6 @@ protected: } } - void readHeader(ScidacReader &binReader) - { - std::string recordXml; - - binReader.readLimeObject(recordXml, SCIDAC_FILE_XML); - XmlReader xmlReader(recordXml, true, "eigenPackPar"); - xmlReader.push(); - xmlReader.readCurrentSubtree(record.operatorXml); - xmlReader.nextElement(); - xmlReader.readCurrentSubtree(record.solverXml); - } - - template - void readElement(T &evec, VecRecord &vecRecord, ScidacReader &binReader) - { - binReader.readScidacFieldRecord(evec, vecRecord); - } - template void basicRead(std::vector &evec, std::vector &eval, const std::string filename, const unsigned int size) @@ -155,7 +172,7 @@ protected: ScidacReader binReader; binReader.open(filename); - readHeader(binReader); + readHeader(record, binReader); for(int k = 0; k < size; ++k) { VecRecord vecRecord; @@ -181,7 +198,7 @@ protected: VecRecord vecRecord; binReader.open(filename); - readHeader(binReader); + readHeader(record, binReader); LOG(Message) << "Reading eigenvector " << index << std::endl; readElement(evec, vecRecord, binReader); if (vecRecord.index != index) @@ -194,21 +211,6 @@ protected: binReader.close(); } - void writeHeader(ScidacWriter &binWriter) - { - XmlWriter xmlWriter("", "eigenPackPar"); - - xmlWriter.pushXmlString(record.operatorXml); - xmlWriter.pushXmlString(record.solverXml); - binWriter.writeLimeObject(1, 1, xmlWriter, "parameters", SCIDAC_FILE_XML); - } - - template - void writeElement(ScidacWriter &binWriter, T &evec, VecRecord &vecRecord) - { - binWriter.writeScidacFieldRecord(evec, vecRecord, DEFAULT_ASCII_PREC); - } - template void basicWrite(const std::string filename, std::vector &evec, const std::vector &eval, const unsigned int size) @@ -217,7 +219,7 @@ protected: makeFileDir(filename, evec[0]._grid); binWriter.open(filename); - writeHeader(binWriter); + writeHeader(binWriter, record); for(int k = 0; k < size; ++k) { VecRecord vecRecord; @@ -239,7 +241,7 @@ protected: makeFileDir(filename, evec._grid); binWriter.open(filename); - writeHeader(binWriter); + writeHeader(binWriter, record); vecRecord.index = index; vecRecord.eval = eval; LOG(Message) << "Writing eigenvector " << index << std::endl; diff --git a/Hadrons/Makefile.am b/Hadrons/Makefile.am index fe851aee..4ec45c03 100644 --- a/Hadrons/Makefile.am +++ b/Hadrons/Makefile.am @@ -1,5 +1,6 @@ +SUBDIRS = . Utilities + lib_LIBRARIES = libHadrons.a -bin_PROGRAMS = HadronsXmlRun include modules.inc @@ -31,6 +32,3 @@ nobase_libHadrons_a_HEADERS = \ ModuleFactory.hpp \ Solver.hpp \ VirtualMachine.hpp - -HadronsXmlRun_SOURCES = HadronsXmlRun.cc -HadronsXmlRun_LDADD = libHadrons.a -lGrid diff --git a/Hadrons/Utilities/EigenPackCast.cc b/Hadrons/Utilities/EigenPackCast.cc new file mode 100644 index 00000000..cf1480dc --- /dev/null +++ b/Hadrons/Utilities/EigenPackCast.cc @@ -0,0 +1,123 @@ +#include +#include + +using namespace Grid; +using namespace QCD; +using namespace Hadrons; + +template +void convert(const std::string outFilename, const std::string inFilename, + const unsigned int size, const bool multiFile) +{ + assert(outFilename != inFilename); + + typedef EigenPack EPOut; + typedef EigenPack EPIn; + typedef typename FOut::vector_type VTypeOut; + typedef typename FIn::vector_type VTypeIn; + + std::shared_ptr gIn, gOut; + auto dim = GridDefaultLatt(); + unsigned int nd = dim.size(); + auto simdOut = GridDefaultSimd(nd, VTypeOut::Nsimd()); + auto simdIn = GridDefaultSimd(nd, VTypeIn::Nsimd()); + + gOut.reset(SpaceTimeGrid::makeFourDimGrid(dim, simdOut, GridDefaultMpi())); + gIn.reset(SpaceTimeGrid::makeFourDimGrid(dim, simdIn, GridDefaultMpi())); + + FOut bufOut(gOut.get()); + FIn bufIn(gIn.get()); + + if (multiFile) + { + for(unsigned int k = 0; k < size; ++k) + { + ScidacWriter binWriter(gOut->IsBoss()); + ScidacReader binReader; + PackRecord record; + VecRecord vecRecord; + std::string outV = outFilename + "/v" + std::to_string(k) + ".bin"; + std::string inV = inFilename + "/v" + std::to_string(k) + ".bin"; + + makeFileDir(outV, gOut.get()); + binWriter.open(outV); + binReader.open(inV); + EPIn::readHeader(record, binReader); + EPOut::writeHeader(binWriter, record); + EPIn::readElement(bufIn, vecRecord, binReader); + precisionChange(bufOut, bufIn); + EPOut::writeElement(binWriter, bufOut, vecRecord); + binWriter.close(); + binReader.close(); + } + } + else + { + ScidacWriter binWriter(gOut->IsBoss()); + ScidacReader binReader; + PackRecord record; + + makeFileDir(outFilename, gOut.get()); + binWriter.open(outFilename); + binReader.open(inFilename); + EPIn::readHeader(record, binReader); + EPOut::writeHeader(binWriter, record); + for(unsigned int k = 0; k < size; ++k) + { + VecRecord vecRecord; + + EPIn::readElement(bufIn, vecRecord, binReader); + precisionChange(bufOut, bufIn); + EPOut::writeElement(binWriter, bufOut, vecRecord); + } + binWriter.close(); + binReader.close(); + } +} + +#ifndef FOUT +#warning "FOUT undefined (set to WilsonImplF::FermionField by default)" +#define FOUT WilsonImplF::FermionField +#endif +#ifndef FIN +#warning "FIN undefined (set to WilsonImplD::FermionField by default)" +#define FIN WilsonImplD::FermionField +#endif + +int main(int argc, char *argv[]) +{ + // parse command line + std::string outFilename, inFilename; + unsigned int size; + bool multiFile; + + if (argc < 5) + { + std::cerr << "usage: " << argv[0] << " [Grid options]"; + std::cerr << std::endl; + std::exit(EXIT_FAILURE); + } + outFilename = argv[1]; + inFilename = argv[2]; + size = std::stoi(std::string(argv[3])); + multiFile = (std::string(argv[4]) != "0"); + + // initialization + Grid_init(&argc, &argv); + + // execution + try + { + convert(outFilename, inFilename, size, multiFile); + } + catch (const std::exception& e) + { + Exceptions::abort(e); + } + + // epilogue + LOG(Message) << "Grid is finalizing now" << std::endl; + Grid_finalize(); + + return EXIT_SUCCESS; +} diff --git a/Hadrons/HadronsXmlRun.cc b/Hadrons/Utilities/HadronsXmlRun.cc similarity index 100% rename from Hadrons/HadronsXmlRun.cc rename to Hadrons/Utilities/HadronsXmlRun.cc diff --git a/Hadrons/Utilities/Makefile.am b/Hadrons/Utilities/Makefile.am new file mode 100644 index 00000000..62c29ef9 --- /dev/null +++ b/Hadrons/Utilities/Makefile.am @@ -0,0 +1,10 @@ +AM_LDFLAGS += -L../../Hadrons + +bin_PROGRAMS = HadronsXmlRun FermionEigenPackCastToSingle + +HadronsXmlRun_SOURCES = HadronsXmlRun.cc +HadronsXmlRun_LDADD = -lHadrons -lGrid + +FermionEigenPackCastToSingle_SOURCES = EigenPackCast.cc +FermionEigenPackCastToSingle_CXXFLAGS = $(AM_CXXFLAGS) -DFIN=WilsonImplD::FermionField -DFOUT=WilsonImplF::FermionField +FermionEigenPackCastToSingle_LDADD = -lHadrons -lGrid diff --git a/configure.ac b/configure.ac index f57b00ad..d362ed5a 100644 --- a/configure.ac +++ b/configure.ac @@ -562,6 +562,7 @@ AC_CONFIG_FILES(tests/qdpxx/Makefile) AC_CONFIG_FILES(tests/testu01/Makefile) AC_CONFIG_FILES(benchmarks/Makefile) AC_CONFIG_FILES(Hadrons/Makefile) +AC_CONFIG_FILES(Hadrons/Utilities/Makefile) AC_OUTPUT echo ""