From 87e8aad5a05dac8678f195f0d3f6fa09208db35c Mon Sep 17 00:00:00 2001 From: Guido Cossu Date: Mon, 16 Jan 2017 16:07:12 +0000 Subject: [PATCH] Added support for input file HMC modules (missing the actions yet) --- lib/Grid.h | 5 +- lib/qcd/hmc/HMCModules.h | 22 +-- lib/qcd/hmc/HMCResourceManager.h | 66 ++++---- lib/qcd/hmc/checkpointers/BaseCheckpointer.h | 4 +- .../hmc/checkpointers/CheckPointerModules.h | 150 ++++++++++++++++++ lib/qcd/hmc/checkpointers/CheckPointers.h | 40 +++++ lib/qcd/modules/Factory.h | 11 +- lib/qcd/modules/Modules.cc | 35 ++++ lib/qcd/modules/Modules.h | 69 ++++++-- tests/hmc/Test_hmc_Factories.cc | 88 ++++++++++ tests/hmc/Test_hmc_WilsonGauge_Binary.cc | 35 +--- 11 files changed, 418 insertions(+), 107 deletions(-) create mode 100644 lib/qcd/hmc/checkpointers/CheckPointerModules.h create mode 100644 lib/qcd/hmc/checkpointers/CheckPointers.h create mode 100644 lib/qcd/modules/Modules.cc create mode 100644 tests/hmc/Test_hmc_Factories.cc diff --git a/lib/Grid.h b/lib/Grid.h index 8c3698a3..e6a445bb 100644 --- a/lib/Grid.h +++ b/lib/Grid.h @@ -84,10 +84,7 @@ Author: paboyle #include #include #include -#include -#include -#include -#include +#include #include #include #include diff --git a/lib/qcd/hmc/HMCModules.h b/lib/qcd/hmc/HMCModules.h index d4d7f84d..68872e74 100644 --- a/lib/qcd/hmc/HMCModules.h +++ b/lib/qcd/hmc/HMCModules.h @@ -34,7 +34,7 @@ namespace Grid { namespace QCD { - +// Some modules for the basic setup /////////////////////////////////////////////////// @@ -165,6 +165,9 @@ public: } }; + + +/* /////////////////////////////////////////////////////////////////// /// Smearing module template @@ -177,24 +180,9 @@ class StoutSmearingModule: public SmearingModule{ SmearedConfiguration SmearingPolicy; }; +*/ -//////////////////////////////////////////////////////////////////////// -// Checkpoint module, owns the Checkpointer -template -class CheckPointModule { - std::unique_ptr > cp_; - public: - void set_Checkpointer(BaseHmcCheckpointer* cp) { - cp_.reset(cp); - }; - - BaseHmcCheckpointer* get_CheckPointer() { - return cp_.get(); - } - - void initialize(CheckpointerParameters& P) { cp_.initialize(P); } -}; } // namespace QCD } // namespace Grid diff --git a/lib/qcd/hmc/HMCResourceManager.h b/lib/qcd/hmc/HMCResourceManager.h index ad535ca8..3d115ced 100644 --- a/lib/qcd/hmc/HMCResourceManager.h +++ b/lib/qcd/hmc/HMCResourceManager.h @@ -38,8 +38,8 @@ with this program; if not, write to the Free Software Foundation, Inc., if (!have_CheckPointer) { \ std::cout << GridLogDebug << "Loading Checkpointer " << #NAME \ << std::endl; \ - CP.set_Checkpointer( \ - new NAME##HmcCheckpointer(Params_)); \ + CP = std::unique_ptr( \ + new NAME##CPModule(Params_)); \ have_CheckPointer = true; \ } else { \ std::cout << GridLogError << "Checkpointer already loaded " \ @@ -48,24 +48,6 @@ with this program; if not, write to the Free Software Foundation, Inc., } \ } -/* -// One function per Checkpointer using the reader, use a macro to simplify -#define RegisterLoadCheckPointerReaderFunction(NAME) \ - template \ - void Load##NAME##Checkpointer(Reader& Reader_) { \ - if (!have_CheckPointer) { \ - std::cout << GridLogDebug << "Loading Checkpointer " << #NAME \ - << std::endl; \ - CP.set_Checkpointer(new NAME##HmcCheckpointer( \ - CheckpointerParameters(Reader_))); \ - have_CheckPointer = true; \ - } else { \ - std::cout << GridLogError << "Checkpointer already loaded " \ - << std::endl; \ - exit(1); \ - } \ - } -*/ namespace Grid { namespace QCD { @@ -73,20 +55,49 @@ namespace QCD { // HMC Resource manager template class HMCResourceManager { + typedef HMCModuleBase< QCD::BaseHmcCheckpointer > CheckpointerBaseModule; + + // Named storage for grid pairs (std + red-black) std::unordered_map Grids; RNGModule RNGs; // SmearingModule Smearing; - CheckPointModule CP; - + std::unique_ptr CP; + bool have_RNG; bool have_CheckPointer; public: HMCResourceManager() : have_RNG(false), have_CheckPointer(false) {} - // Here need a constructor for using the Reader class + template + void initialize(ReaderClass &Read){ + // assumes we are starting from the main node + + // Geometry + GridModuleParameters GridPar(Read); + GridFourDimModule GridMod( GridPar) ; + AddGrid("gauge", GridMod); + + // Checkpointer + auto &CPfactory = HMC_CPModuleFactory::getInstance(); + Read.push("Checkpointer"); + std::string cp_type; + Read.readDefault("name", cp_type); + std::cout << "Registered types " << std::endl; + std::cout << CPfactory.getBuilderList() << std::endl; + + CP = CPfactory.create(cp_type, Read); + CP->print_parameters(); + Read.pop(); + have_CheckPointer = true; + + RNGModuleParameters RNGpar(Read); + SetRNGSeeds(RNGpar); + + // HMC here + } ////////////////////////////////////////////////////////////// // Grids @@ -159,7 +170,7 @@ class HMCResourceManager { BaseHmcCheckpointer* GetCheckPointer() { if (have_CheckPointer) - return CP.get_CheckPointer(); + return CP->getPtr(); else { std::cout << GridLogError << "Error: no checkpointer defined" << std::endl; @@ -170,13 +181,6 @@ class HMCResourceManager { RegisterLoadCheckPointerFunction(Binary); RegisterLoadCheckPointerFunction(Nersc); RegisterLoadCheckPointerFunction(ILDG); - -/* - RegisterLoadCheckPointerReaderFunction(Binary); - RegisterLoadCheckPointerReaderFunction(Nersc); - RegisterLoadCheckPointerReaderFunction(ILDG); -*/ - }; } } diff --git a/lib/qcd/hmc/checkpointers/BaseCheckpointer.h b/lib/qcd/hmc/checkpointers/BaseCheckpointer.h index 54aa486d..9be9efca 100644 --- a/lib/qcd/hmc/checkpointers/BaseCheckpointer.h +++ b/lib/qcd/hmc/checkpointers/BaseCheckpointer.h @@ -48,8 +48,8 @@ class CheckpointerParameters : Serializable { format(f){}; - template ::value, int>::type = 0 > - CheckpointerParameters(ReaderClass &Reader) { + template + CheckpointerParameters(Reader &Reader) { read(Reader, "Checkpointer", *this); } diff --git a/lib/qcd/hmc/checkpointers/CheckPointerModules.h b/lib/qcd/hmc/checkpointers/CheckPointerModules.h new file mode 100644 index 00000000..565ec0c5 --- /dev/null +++ b/lib/qcd/hmc/checkpointers/CheckPointerModules.h @@ -0,0 +1,150 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./lib/qcd/action/gauge/WilsonGaugeAction.h + +Copyright (C) 2016 + +Author: Guido Cossu + +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 CP_MODULES_H +#define CP_MODULES_H + + +// FIXME Reorganize QCD namespace + + +namespace Grid { + +//////////////////////////////////////////////////////////////////////// +// Checkpoint module, owns the Checkpointer +//////////////////////////////////////////////////////////////////////// + +template +class CheckPointerModule: public Parametrized, public HMCModuleBase< QCD::BaseHmcCheckpointer > { + public: + std::unique_ptr > CheckPointPtr; + typedef QCD::CheckpointerParameters APar; + typedef HMCModuleBase< QCD::BaseHmcCheckpointer > Base; + typedef typename Base::Product Product; + + CheckPointerModule(APar Par): Parametrized(Par) {} + template + CheckPointerModule(Reader& Reader) : Parametrized(Reader){}; + + virtual void print_parameters(){ + std::cout << this->Par_ << std::endl; + } + + Product* getPtr() { + if (!CheckPointPtr) initialize(); + + return CheckPointPtr.get(); + } + + private: + virtual void initialize() = 0; + +}; + + + +template +class HMC_CPModuleFactory + : public Factory < HMCModuleBase< QCD::BaseHmcCheckpointer > , Reader > { + public: + typedef Reader TheReader; + // use SINGLETON FUNCTOR MACRO HERE + HMC_CPModuleFactory(const HMC_CPModuleFactory& e) = delete; + void operator=(const HMC_CPModuleFactory& e) = delete; + static HMC_CPModuleFactory& getInstance(void) { + static HMC_CPModuleFactory e; + return e; + } + + private: + HMC_CPModuleFactory(void) = default; + std::string obj_type() const { + return std::string(str); + } +}; + + + +///////////////////////////////////////////////////////////////////// +// Concrete classes +///////////////////////////////////////////////////////////////////// +namespace QCD{ + +template +class BinaryCPModule: public CheckPointerModule< ImplementationPolicy> { + typedef CheckPointerModule< ImplementationPolicy> CPBase; + using CPBase::CPBase; // for constructors + + // acquire resource + virtual void initialize(){ + this->CheckPointPtr.reset(new BinaryHmcCheckpointer(this->Par_)); + } + +}; + + +template +class NerscCPModule: public CheckPointerModule< ImplementationPolicy> { + typedef CheckPointerModule< ImplementationPolicy> CPBase; + using CPBase::CPBase; // for constructors + + // acquire resource + virtual void initialize(){ + this->CheckPointPtr.reset(new NerscHmcCheckpointer(this->Par_)); + } + +}; + +template +class ILDGCPModule: public CheckPointerModule< ImplementationPolicy> { + typedef CheckPointerModule< ImplementationPolicy> CPBase; + using CPBase::CPBase; // for constructors + + // acquire resource + virtual void initialize(){ + this->CheckPointPtr.reset(new ILDGHmcCheckpointer(this->Par_)); + } + +}; + + + +}// QCD temporarily here + + +extern char cp_string[]; + +static Registrar, HMC_CPModuleFactory > __CPBinarymodXMLInit("Binary"); +static Registrar , HMC_CPModuleFactory > __CPNerscmodXMLInit("Nersc"); +static Registrar , HMC_CPModuleFactory > __CPILDGmodXMLInit("ILDG"); + + + +}// Grid +#endif //CP_MODULES_H \ No newline at end of file diff --git a/lib/qcd/hmc/checkpointers/CheckPointers.h b/lib/qcd/hmc/checkpointers/CheckPointers.h new file mode 100644 index 00000000..99da6e63 --- /dev/null +++ b/lib/qcd/hmc/checkpointers/CheckPointers.h @@ -0,0 +1,40 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./lib/qcd/action/gauge/WilsonGaugeAction.h + +Copyright (C) 2016 + +Author: Guido Cossu + +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 CHECKPOINTERS_H +#define CHECKPOINTERS_H + +#include +#include +#include +#include +#include + + +#endif // CHECKPOINTERS_H \ No newline at end of file diff --git a/lib/qcd/modules/Factory.h b/lib/qcd/modules/Factory.h index cf27593c..f733abb6 100644 --- a/lib/qcd/modules/Factory.h +++ b/lib/qcd/modules/Factory.h @@ -50,6 +50,7 @@ public: const ProductCreator& name) const; private: std::map builder_; + virtual std::string obj_type() const = 0; }; /****************************************************************************** @@ -79,20 +80,24 @@ std::vector Factory::getBuilderList(void) const // factory ///////////////////////////////////////////////////////////////////// template std::unique_ptr Factory::create(const std::string type, - const ProductCreator& name) const + const ProductCreator& input) const { Func func; + std::cout << GridLogDebug << "Creating object of type "<< type << std::endl; try { func = builder_.at(type); } catch (std::out_of_range &) { - //HADRON_ERROR("object of type '" + type + "' unknown"); + //HADRON_ERROR("object of type '" + type + "' unknown"); + std::cout << GridLogError << "Error" << std::endl; + std::cout << GridLogError << obj_type() << " object of name [" << type << "] unknown" << std::endl; + exit(1); } - return func(name); + return func(input); } } diff --git a/lib/qcd/modules/Modules.cc b/lib/qcd/modules/Modules.cc new file mode 100644 index 00000000..faa987e0 --- /dev/null +++ b/lib/qcd/modules/Modules.cc @@ -0,0 +1,35 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./lib/qcd/modules/Modules.cc + +Copyright (C) 2016 + +Author: Guido Cossu + +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 */ + +namespace Grid{ + +char gauge_string[] = "gauge"; +char cp_string[] = "CheckPointer"; + +} diff --git a/lib/qcd/modules/Modules.h b/lib/qcd/modules/Modules.h index c535c1a4..8a45658d 100644 --- a/lib/qcd/modules/Modules.h +++ b/lib/qcd/modules/Modules.h @@ -50,6 +50,16 @@ public: Parametrized(Reader & Reader){ read(Reader, section_name(), Par_); } + + void set_parameters(Parameters Par){ + Par_ = Par; + } + + + void print_parameters(){ + std::cout << Par_ << std::endl; + } + protected: Parameters Par_; @@ -70,6 +80,7 @@ class HMCModuleBase{ public: typedef Prod Product; virtual Prod* getPtr() = 0; +virtual void print_parameters(){}; //default to nothing }; @@ -87,6 +98,8 @@ class ActionModule typedef HMCModuleBase< QCD::Action > Base; typedef typename Base::Product Product; + + std::unique_ptr ActionPtr; ActionModule(APar Par) : Parametrized(Par) {} @@ -94,6 +107,10 @@ class ActionModule template ActionModule(Reader& Reader) : Parametrized(Reader){}; + virtual void print_parameters(){ + std::cout << this->Par_ << std::endl; + } + Product* getPtr() { if (!ActionPtr) initialize(); @@ -102,8 +119,16 @@ class ActionModule private: virtual void initialize() = 0; + }; + + + + + + + namespace QCD{ class WilsonGaugeActionParameters : Serializable { @@ -118,11 +143,11 @@ class WilsonGaugeActionParameters : Serializable { template class WilsonGModule: public ActionModule, WilsonGaugeActionParameters> { typedef ActionModule, WilsonGaugeActionParameters> ActionBase; - using ActionBase::ActionBase; + using ActionBase::ActionBase; // for constructors // acquire resource virtual void initialize(){ - ActionBase::ActionPtr.reset(new WilsonGaugeAction(ActionBase::Par_.beta)); + this->ActionPtr.reset(new WilsonGaugeAction(this->Par_.beta)); } }; @@ -137,31 +162,38 @@ typedef WilsonGModule WilsonGMod; + +//////////////////////////////////////// +// Factories specialisations +//////////////////////////////////////// + + + // use the same classed defined by Antonin, does not make sense to rewrite // Factory is perfectly fine // Registar must be changed because I do not want to use the ModuleFactory -/* -define -*/ +// ref to LatticeGaugeField must be changed +typedef HMCModuleBase< QCD::Action< QCD::LatticeGaugeField > > HMC_LGTActionModBase; -typedef HMCModuleBase< QCD::Action< QCD::LatticeGaugeField > > HMCModBase; - -template -class HMCActionModuleFactory - : public Factory < HMCModBase , Reader > { +template +class HMC_LGTActionModuleFactory + : public Factory < HMC_LGTActionModBase , Reader > { public: - typedef Reader TheReader; + typedef Reader TheReader; // use SINGLETON FUNCTOR MACRO HERE - HMCActionModuleFactory(const HMCActionModuleFactory& e) = delete; - void operator=(const HMCActionModuleFactory& e) = delete; - static HMCActionModuleFactory& getInstance(void) { - static HMCActionModuleFactory e; + HMC_LGTActionModuleFactory(const HMC_LGTActionModuleFactory& e) = delete; + void operator=(const HMC_LGTActionModuleFactory& e) = delete; + static HMC_LGTActionModuleFactory& getInstance(void) { + static HMC_LGTActionModuleFactory e; return e; } private: - HMCActionModuleFactory(void) = default; + HMC_LGTActionModuleFactory(void) = default; + std::string obj_type() const { + return std::string(str); + } }; /* @@ -178,6 +210,8 @@ when needed a pointer is released + + template class Registrar { public: @@ -188,9 +222,10 @@ class Registrar { } }; -Registrar > __WGmodInit("WilsonGaugeAction"); +extern char gauge_string[]; +static Registrar > __WGmodXMLInit("Wilson"); } diff --git a/tests/hmc/Test_hmc_Factories.cc b/tests/hmc/Test_hmc_Factories.cc new file mode 100644 index 00000000..940367e2 --- /dev/null +++ b/tests/hmc/Test_hmc_Factories.cc @@ -0,0 +1,88 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/Test_hmc_WilsonFermionGauge.cc + +Copyright (C) 2015 + +Author: Peter Boyle +Author: neo +Author: Guido Cossu + +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 */ +#include + +int main(int argc, char **argv) { + using namespace Grid; + using namespace Grid::QCD; + + Grid_init(&argc, &argv); + int threads = GridThread::GetThreads(); + // here make a routine to print all the relevant information on the run + std::cout << GridLogMessage << "Grid is setup to use " << threads << " threads" << std::endl; + + // Typedefs to simplify notation + typedef GenericHMCRunner HMCWrapper; // Uses the default minimum norm + typedef Grid::XmlReader InputFileReader; + + // Reader + InputFileReader Reader("input.wilson_gauge.params.xml"); + + HMCWrapper TheHMC; + + TheHMC.Resources.initialize(Reader); + + // Construct observables + // here there is too much indirection + PlaquetteLogger PlaqLog("Plaquette"); + TheHMC.ObservablesList.push_back(&PlaqLog); + ////////////////////////////////////////////// + + ///////////////////////////////////////////////////////////// + // Collect actions, here use more encapsulation + // need wrappers of the fermionic classes + // that have a complex construction + + // standard + RealD beta = 5.6 ; + WilsonGaugeActionR Waction(beta); + + ActionLevel Level1(1); + Level1.push_back(&Waction); + //Level1.push_back(WGMod.getPtr()); + TheHMC.TheAction.push_back(Level1); + ///////////////////////////////////////////////////////////// + + // Nest MDparameters in the HMCparameters->HMCPayload + // make it serializable + TheHMC.MDparameters.MDsteps = 20; + TheHMC.MDparameters.trajL = 1.0; + + // eventually smearing here + // ... + //////////////////////////////////////////////////////////////// + + TheHMC.ReadCommandLine(argc, argv); // these must be parameters from file + TheHMC.Run(); // no smearing + + Grid_finalize(); + +} // main diff --git a/tests/hmc/Test_hmc_WilsonGauge_Binary.cc b/tests/hmc/Test_hmc_WilsonGauge_Binary.cc index 73b95f15..9c809e8b 100644 --- a/tests/hmc/Test_hmc_WilsonGauge_Binary.cc +++ b/tests/hmc/Test_hmc_WilsonGauge_Binary.cc @@ -41,22 +41,13 @@ int main(int argc, char **argv) { // Typedefs to simplify notation typedef GenericHMCRunner HMCWrapper; // Uses the default minimum norm - typedef Grid::XmlReader InputFileReader; - - // Reader, now not necessary - InputFileReader Reader("input.wilson_gauge.params.xml"); - HMCWrapper TheHMC; // Grid from the command line TheHMC.Resources.AddFourDimGrid("gauge"); + // Possibile to create the module by hand + // hardcoding parameters or using a Reader - // Grid from the Reader - /* - GridModuleParameters GridPar(Reader); - GridFourDimModule GridMod( GridPar) ; - TheHMC.Resources.AddGrid("gauge", GridMod); - */ // Checkpointer definition CheckpointerParameters CPparams; @@ -65,14 +56,8 @@ int main(int argc, char **argv) { CPparams.saveInterval = 5; CPparams.format = "IEEE64BIG"; - // can also use the reader constructor - // CheckpointerParameters CPparams(Reader); TheHMC.Resources.LoadBinaryCheckpointer(CPparams); - - // Fill resources - // Seeds for the random number generators - // Can also initialize using the Reader RNGModuleParameters RNGpar; RNGpar.SerialSeed = {1,2,3,4,5}; RNGpar.ParallelSeed = {6,7,8,9,10}; @@ -88,18 +73,6 @@ int main(int argc, char **argv) { // Collect actions, here use more encapsulation // need wrappers of the fermionic classes // that have a complex construction - - // Gauge action - // as module - /* - WilsonGMod::Parameters WPar; - WPar.beta = 6.0; - WilsonGMod WGMod(WPar); - auto testAction = WGMod.getPtr();// test to pass to the action set - HMCModuleBase>* HMB = &WGMod; - */ - - // standard RealD beta = 5.6 ; WilsonGaugeActionR Waction(beta); @@ -115,10 +88,6 @@ int main(int argc, char **argv) { TheHMC.MDparameters.MDsteps = 20; TheHMC.MDparameters.trajL = 1.0; - // eventually smearing here - // ... - //////////////////////////////////////////////////////////////// - TheHMC.ReadCommandLine(argc, argv); // these must be parameters from file TheHMC.Run(); // no smearing