From d3f857b1c9391fe2abc2b2153dc9e0ac7d1d3ff2 Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Wed, 4 Apr 2018 16:36:37 +0100 Subject: [PATCH] Hadrons: proper metadata for eigenpacks --- extras/Hadrons/EigenPack.hpp | 70 +++++++++++++------ extras/Hadrons/Module.hpp | 21 +++++- .../Modules/MSolver/LocalCoherenceLanczos.hpp | 10 +-- lib/parallelIO/IldgIO.h | 2 +- lib/serialisation/MacroMagic.h | 2 +- lib/serialisation/XmlIO.cc | 14 +++- lib/serialisation/XmlIO.h | 4 +- 7 files changed, 89 insertions(+), 34 deletions(-) diff --git a/extras/Hadrons/EigenPack.hpp b/extras/Hadrons/EigenPack.hpp index 2bd3b622..df180e9b 100644 --- a/extras/Hadrons/EigenPack.hpp +++ b/extras/Hadrons/EigenPack.hpp @@ -44,9 +44,23 @@ class EigenPack { public: typedef F Field; + struct PackRecord: Serializable + { + GRID_SERIALIZABLE_CLASS_MEMBERS(PackRecord, + std::string, operatorPar, + std::string, solverPar); + }; + 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; + PackRecord record; public: EigenPack(void) = default; virtual ~EigenPack(void) = default; @@ -68,10 +82,10 @@ public: makeFilenames(evecFilename, evalFilename, fileStem, traj); XmlReader xmlReader(evalFilename); - basicRead(evec, evecFilename, evec.size()); LOG(Message) << "Reading " << eval.size() << " eigenvalues from '" << evalFilename << "'" << std::endl; Grid::read(xmlReader, "evals", eval); + basicRead(evec, evecFilename, evec.size()); } virtual void write(const std::string fileStem, const int traj = -1) @@ -80,10 +94,10 @@ public: makeFilenames(evecFilename, evalFilename, fileStem, traj); XmlWriter xmlWriter(evalFilename); - basicWrite(evecFilename, evec, evec.size()); LOG(Message) << "Writing " << eval.size() << " eigenvalues to '" << evalFilename << "'" << std::endl; Grid::write(xmlWriter, "evals", eval); + basicWrite(evecFilename, evec, evec.size()); } protected: void makeFilenames(std::string &evecFilename, std::string &evalFilename, @@ -96,31 +110,43 @@ protected: } template - static void basicRead(std::vector &evec, const std::string filename, - const unsigned int size) + void basicRead(std::vector &evec, const std::string filename, + const unsigned int size) { - emptyUserRecord record; ScidacReader binReader; binReader.open(filename); + binReader.readScidacFileRecord(evec[0]._grid, record); for(int k = 0; k < size; ++k) { - binReader.readScidacFieldRecord(evec[k], record); + VecRecord vecRecord; + + binReader.readScidacFieldRecord(evec[k], vecRecord); + if (vecRecord.index != k) + { + HADRON_ERROR(Io, "Eigenvector " + std::to_string(k) + " has a" + + " wrong index (expected " + std::to_string(vecRecord.index) + + ") in file '" + filename + "'"); + } } binReader.close(); } template - static void basicWrite(const std::string filename, std::vector &evec, - const unsigned int size) + void basicWrite(const std::string filename, std::vector &evec, + const unsigned int size) { - emptyUserRecord record; ScidacWriter binWriter(evec[0]._grid->IsBoss()); binWriter.open(filename); + binWriter.writeScidacFileRecord(evec[0]._grid, record); for(int k = 0; k < size; ++k) { - binWriter.writeScidacFieldRecord(evec[k], record); + VecRecord vecRecord; + + vecRecord.index = k; + vecRecord.eval = eval[k]; + binWriter.writeScidacFieldRecord(evec[k], vecRecord); } binWriter.close(); } @@ -160,12 +186,12 @@ public: this->makeFilenames(evecFineFilename, evalFineFilename, fileStem + "_fine", traj); XmlReader xmlFineReader(evalFineFilename); - LOG(Message) << "Reading " << this->evec.size() << " fine eigenvectors from '" - << evecFineFilename << "'" << std::endl; - this->basicRead(this->evec, evecFineFilename, this->evec.size()); LOG(Message) << "Reading " << this->eval.size() << " fine eigenvalues from '" << evalFineFilename << "'" << std::endl; Grid::read(xmlFineReader, "evals", this->eval); + LOG(Message) << "Reading " << this->evec.size() << " fine eigenvectors from '" + << evecFineFilename << "'" << std::endl; + this->basicRead(this->evec, evecFineFilename, this->evec.size()); } void readCoarse(const std::string fileStem, const int traj = -1) @@ -175,12 +201,12 @@ public: this->makeFilenames(evecCoarseFilename, evalCoarseFilename, fileStem + "_coarse", traj); XmlReader xmlCoarseReader(evalCoarseFilename); - LOG(Message) << "Reading " << evecCoarse.size() << " coarse eigenvectors from '" - << evecCoarseFilename << "'" << std::endl; - this->basicRead(evecCoarse, evecCoarseFilename, evecCoarse.size()); LOG(Message) << "Reading " << evalCoarse.size() << " coarse eigenvalues from '" << evalCoarseFilename << "'" << std::endl; Grid::read(xmlCoarseReader, "evals", evalCoarse); + LOG(Message) << "Reading " << evecCoarse.size() << " coarse eigenvectors from '" + << evecCoarseFilename << "'" << std::endl; + this->basicRead(evecCoarse, evecCoarseFilename, evecCoarse.size()); } virtual void read(const std::string fileStem, const int traj = -1) @@ -196,12 +222,12 @@ public: this->makeFilenames(evecFineFilename, evalFineFilename, fileStem + "_fine", traj); XmlWriter xmlFineWriter(evalFineFilename); - LOG(Message) << "Writing " << this->evec.size() << " fine eigenvectors to '" - << evecFineFilename << "'" << std::endl; - this->basicWrite(evecFineFilename, this->evec, this->evec.size()); LOG(Message) << "Writing " << this->eval.size() << " fine eigenvalues to '" << evalFineFilename << "'" << std::endl; Grid::write(xmlFineWriter, "evals", this->eval); + LOG(Message) << "Writing " << this->evec.size() << " fine eigenvectors to '" + << evecFineFilename << "'" << std::endl; + this->basicWrite(evecFineFilename, this->evec, this->evec.size()); } void writeCoarse(const std::string fileStem, const int traj = -1) @@ -211,12 +237,12 @@ public: this->makeFilenames(evecCoarseFilename, evalCoarseFilename, fileStem + "_coarse", traj); XmlWriter xmlCoarseWriter(evalCoarseFilename); - LOG(Message) << "Writing " << evecCoarse.size() << " coarse eigenvectors to '" - << evecCoarseFilename << "'" << std::endl; - this->basicWrite(evecCoarseFilename, evecCoarse, evecCoarse.size()); LOG(Message) << "Writing " << evalCoarse.size() << " coarse eigenvalues to '" << evalCoarseFilename << "'" << std::endl; Grid::write(xmlCoarseWriter, "evals", evalCoarse); + LOG(Message) << "Writing " << evecCoarse.size() << " coarse eigenvectors to '" + << evecCoarseFilename << "'" << std::endl; + this->basicWrite(evecCoarseFilename, evecCoarse, evecCoarse.size()); } virtual void write(const std::string fileStem, const int traj = -1) diff --git a/extras/Hadrons/Module.hpp b/extras/Hadrons/Module.hpp index 85c27472..df13b8cb 100644 --- a/extras/Hadrons/Module.hpp +++ b/extras/Hadrons/Module.hpp @@ -172,6 +172,8 @@ public: // parse parameters virtual void parseParameters(XmlReader &reader, const std::string name) = 0; virtual void saveParameters(XmlWriter &writer, const std::string name) = 0; + // parameter string + virtual std::string parString(void) const = 0; // setup virtual void setup(void) {}; virtual void execute(void) = 0; @@ -200,9 +202,11 @@ public: // parse parameters virtual void parseParameters(XmlReader &reader, const std::string name); virtual void saveParameters(XmlWriter &writer, const std::string name); + // parameter string + virtual std::string parString(void) const; // parameter access - const P & par(void) const; - void setPar(const P &par); + const P & par(void) const; + void setPar(const P &par); private: P par_; }; @@ -225,6 +229,8 @@ public: push(writer, "options"); pop(writer); }; + // parameter string (empty) + virtual std::string parString(void) const {return "";}; }; /****************************************************************************** @@ -247,6 +253,17 @@ void Module

::saveParameters(XmlWriter &writer, const std::string name) write(writer, name, par_); } +template +std::string Module

::parString(void) const +{ + std::string xmlstring; + XmlWriter writer("",""); + + write(writer, par_.SerialisableClassName(), par_); + + return writer.string(); +} + template const P & Module

::par(void) const { diff --git a/extras/Hadrons/Modules/MSolver/LocalCoherenceLanczos.hpp b/extras/Hadrons/Modules/MSolver/LocalCoherenceLanczos.hpp index 60b897c8..f4333f80 100644 --- a/extras/Hadrons/Modules/MSolver/LocalCoherenceLanczos.hpp +++ b/extras/Hadrons/Modules/MSolver/LocalCoherenceLanczos.hpp @@ -148,6 +148,8 @@ void TLocalCoherenceLanczos::execute(void) auto &coarsePar = par().coarseParams; auto &epack = envGetDerived(BasePack, CoarsePack, getName()); + epack.record.operatorPar = vm().getModule(par().action)->parString(); + epack.record.solverPar = parString(); envGetTmp(LCL, solver); LOG(Message) << "Performing fine grid IRL -- Nstop= " << finePar.Nstop << ", Nk= " << finePar.Nk << ", Nm= " @@ -173,10 +175,10 @@ void TLocalCoherenceLanczos::execute(void) coarsePar.MinRes); solver.testCoarse(coarsePar.resid*100.0, par().smoother, par().coarseRelaxTol); - } - if (!par().output.empty()) - { - epack.writeCoarse(par().output, vm().getTrajectory()); + if (!par().output.empty()) + { + epack.writeCoarse(par().output, vm().getTrajectory()); + } } } diff --git a/lib/parallelIO/IldgIO.h b/lib/parallelIO/IldgIO.h index d1a684f3..9a5271d1 100644 --- a/lib/parallelIO/IldgIO.h +++ b/lib/parallelIO/IldgIO.h @@ -328,7 +328,7 @@ class GridLimeWriter : public BinaryIO { XmlWriter WR("",""); write(WR,object_name,object); - xmlstring = WR.XmlString(); + xmlstring = WR.docString(); } // std::cout << "WriteLimeObject" << record_name <\ static inline void write(Writer &WR,const std::string &s, const cname &obj){ \ diff --git a/lib/serialisation/XmlIO.cc b/lib/serialisation/XmlIO.cc index 1828d7fc..b0d463c5 100644 --- a/lib/serialisation/XmlIO.cc +++ b/lib/serialisation/XmlIO.cc @@ -44,7 +44,7 @@ XmlWriter::XmlWriter(const std::string &fileName, std::string toplev) : fileName XmlWriter::~XmlWriter(void) { if ( fileName_ != std::string("") ) { - doc_.save_file(fileName_.c_str(), " "); + doc_.save_file(fileName_.c_str(), indent_.c_str()); } } @@ -57,10 +57,18 @@ void XmlWriter::pop(void) { node_ = node_.parent(); } -std::string XmlWriter::XmlString(void) + +std::string XmlWriter::docString(void) { std::ostringstream oss; - doc_.save(oss); + doc_.save(oss, indent_.c_str()); + return oss.str(); +} + +std::string XmlWriter::string(void) +{ + std::ostringstream oss; + doc_.save(oss, indent_.c_str(), pugi::format_default | pugi::format_no_declaration); return oss.str(); } diff --git a/lib/serialisation/XmlIO.h b/lib/serialisation/XmlIO.h index e37eb8d9..c12ef6e5 100644 --- a/lib/serialisation/XmlIO.h +++ b/lib/serialisation/XmlIO.h @@ -55,8 +55,10 @@ namespace Grid void writeDefault(const std::string &s, const U &x); template void writeDefault(const std::string &s, const std::vector &x); - std::string XmlString(void); + std::string docString(void); + std::string string(void); private: + const std::string indent_{" "}; pugi::xml_document doc_; pugi::xml_node node_; std::string fileName_;