From 52148463418c22fd114dee45c84b11e303010897 Mon Sep 17 00:00:00 2001 From: Guido Cossu Date: Thu, 22 Dec 2016 12:41:56 +0000 Subject: [PATCH] Adding a resource manager --- lib/Grid.h | 2 + lib/qcd/action/ActionBase.h | 49 ++--- lib/qcd/action/gauge/WilsonGaugeAction.h | 17 +- lib/qcd/hmc/GenericHMCrunner.h | 257 +++++++++++------------ lib/qcd/hmc/HMC.h | 6 +- lib/qcd/hmc/HMCModules.h | 96 +++++++++ lib/qcd/hmc/HMCResourceManager.h | 110 ++++++++++ lib/qcd/hmc/integrators/Integrator.h | 19 +- tests/hmc/Test_hmc_WilsonGauge_Binary.cc | 188 ++++++----------- tests/solver/Test_dwf_cg_prec_LsVec.cc | 2 +- 10 files changed, 435 insertions(+), 311 deletions(-) create mode 100644 lib/qcd/hmc/HMCModules.h create mode 100644 lib/qcd/hmc/HMCResourceManager.h diff --git a/lib/Grid.h b/lib/Grid.h index 78b5a6bd..7eaf8511 100644 --- a/lib/Grid.h +++ b/lib/Grid.h @@ -87,6 +87,8 @@ Author: paboyle #include #include #include +#include +#include #include #include diff --git a/lib/qcd/action/ActionBase.h b/lib/qcd/action/ActionBase.h index 371ccd24..6ff4bcb1 100644 --- a/lib/qcd/action/ActionBase.h +++ b/lib/qcd/action/ActionBase.h @@ -36,14 +36,13 @@ template class Action { public: bool is_smeared = false; - // Boundary conditions? // Heatbath? - virtual void refresh(const GaugeField& U, - GridParallelRNG& pRNG) = 0; // refresh pseudofermions - virtual RealD S(const GaugeField& U) = 0; // evaluate the action - virtual void deriv(const GaugeField& U, - GaugeField& dSdU) = 0; // evaluate the action derivative - virtual std::string action_name() = 0; // return the action name - virtual ~Action(){}; + // Heatbath? + virtual void refresh(const GaugeField& U, const GridParallelRNG& pRNG) = 0; // refresh pseudofermions + virtual RealD S(const GaugeField& U) = 0; // evaluate the action + virtual void deriv(const GaugeField& U, GaugeField& dSdU) = 0; // evaluate the action derivative + virtual std::string action_name() = 0; // return the action name + virtual std::string LogParameters() = 0; // prints action parameters + virtual ~Action(){} }; // Indexing of tuple types @@ -60,32 +59,10 @@ struct Index> { static const std::size_t value = 1 + Index>::value; }; -/* -template -struct ActionLevel { - public: - typedef Action* - ActPtr; // now force the same colours as the rest of the code - - //Add supported representations here - - - unsigned int multiplier; - - std::vector actions; - - ActionLevel(unsigned int mul = 1) : actions(0), multiplier(mul) { - assert(mul >= 1); - }; - - void push_back(ActPtr ptr) { actions.push_back(ptr); } -}; -*/ - template struct ActionLevel { public: - unsigned int multiplier; + unsigned int multiplier; // Fundamental repr actions separated because of the smearing typedef Action* ActPtr; @@ -98,15 +75,13 @@ struct ActionLevel { std::vector& actions; - ActionLevel(unsigned int mul = 1) : actions(std::get<0>(actions_hirep)), multiplier(mul) { + explicit ActionLevel(unsigned int mul = 1) : actions(std::get<0>(actions_hirep)), multiplier(mul) { // initialize the hirep vectors to zero. - //apply(this->resize, actions_hirep, 0); //need a working resize + // apply(this->resize, actions_hirep, 0); //need a working resize assert(mul >= 1); - }; - - //void push_back(ActPtr ptr) { actions.push_back(ptr); } - + } + // void push_back(ActPtr ptr) { actions.push_back(ptr); } template < class GenField > void push_back(Action* ptr) { diff --git a/lib/qcd/action/gauge/WilsonGaugeAction.h b/lib/qcd/action/gauge/WilsonGaugeAction.h index aa61abf2..fd281e20 100644 --- a/lib/qcd/action/gauge/WilsonGaugeAction.h +++ b/lib/qcd/action/gauge/WilsonGaugeAction.h @@ -43,18 +43,22 @@ class WilsonGaugeAction : public Action { public: INHERIT_GIMPL_TYPES(Gimpl); - // typedef LorentzScalar GaugeLinkField; - private: RealD beta; public: - WilsonGaugeAction(RealD b) : beta(b){}; + explicit WilsonGaugeAction(RealD b) : beta(b){} - virtual std::string action_name(){return "WilsonGaugeAction";} + virtual std::string action_name() {return "WilsonGaugeAction";} + + virtual std::string LogParameters(){ + std::stringstream sstream; + sstream << GridLogMessage << "[WilsonGaugeAction] Beta: " << beta << std::endl; + return sstream.str(); + } virtual void refresh(const GaugeField &U, - GridParallelRNG &pRNG){}; // noop as no pseudoferms + const GridParallelRNG &pRNG){}; // noop as no pseudoferms virtual RealD S(const GaugeField &U) { RealD plaq = WilsonLoops::avgPlaquette(U); @@ -80,8 +84,7 @@ class WilsonGaugeAction : public Action { PokeIndex(dSdU, dSdU_mu, mu); } - - }; + } }; diff --git a/lib/qcd/hmc/GenericHMCrunner.h b/lib/qcd/hmc/GenericHMCrunner.h index 2530559f..1d328f8e 100644 --- a/lib/qcd/hmc/GenericHMCrunner.h +++ b/lib/qcd/hmc/GenericHMCrunner.h @@ -30,176 +30,167 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef GRID_GENERIC_HMC_RUNNER #define GRID_GENERIC_HMC_RUNNER + +#include + namespace Grid { namespace QCD { -// Virtual Class for HMC specific for gauge theories -// implement a specific theory by defining the BuildTheAction -template +template class Integrator, + class RepresentationsPolicy = NoHirep > class BinaryHmcRunnerTemplate { public: - INHERIT_FIELD_TYPES(Implementation); - typedef Implementation ImplPolicy; + INHERIT_FIELD_TYPES(Implementation); + typedef Implementation ImplPolicy; // visible from outside + template < typename S = NoSmearing > + using IntegratorType = Integrator; - enum StartType_t { ColdStart, - HotStart, - TepidStart, - CheckpointStart }; + enum StartType_t + { + ColdStart, + HotStart, + TepidStart, + CheckpointStart, + FilenameStart + }; - ActionSet TheAction; + struct HMCPayload + { + StartType_t StartType; + HMCparameters Parameters; - // A vector of HmcObservable - // that can be injected from outside - std::vector *> - ObservablesList; + HMCPayload() { StartType = HotStart; } + }; - IntegratorParameters MDparameters; + // These can be rationalised, some private + HMCPayload Payload; // Parameters + HMCResourceManager Resources; + IntegratorParameters MDparameters; - GridCartesian * UGrid; - GridRedBlackCartesian *UrbGrid; + ActionSet TheAction; + + // A vector of HmcObservable that can be injected from outside + std::vector *> ObservablesList; + + //GridCartesian * UGrid; // These two are unnecessary, eliminate - GridCartesian * FGrid; - GridRedBlackCartesian *FrbGrid; + // GridRedBlackCartesian *UrbGrid; + // GridCartesian * FGrid; + // GridRedBlackCartesian *FrbGrid; - std::vector SerialSeed; - std::vector ParallelSeed; + void ReadCommandLine(int argc, char ** argv) { + std::string arg; - void RNGSeeds(std::vector S, std::vector P) { - SerialSeed = S; - ParallelSeed = P; - } + if (GridCmdOptionExists(argv, argv + argc, "--StartType")) { + arg = GridCmdOptionPayload(argv, argv + argc, "--StartType"); + if (arg == "HotStart") { + Payload.StartType = HotStart; + } else if (arg == "ColdStart") { + Payload.StartType = ColdStart; + } else if (arg == "TepidStart") { + Payload.StartType = TepidStart; + } else if (arg == "CheckpointStart") { + Payload.StartType = CheckpointStart; + } else { + std::cout << GridLogError << "Unrecognized option in --StartType\n"; + std::cout + << GridLogError + << "Valid [HotStart, ColdStart, TepidStart, CheckpointStart]\n"; + assert(0); + } + } - virtual void BuildTheAction(int argc, char **argv) = 0; // necessary? + if (GridCmdOptionExists(argv, argv + argc, "--StartTrajectory")) { + arg = GridCmdOptionPayload(argv, argv + argc, "--StartTrajectory"); + std::vector ivec(0); + GridCmdOptionIntVector(arg, ivec); + Payload.Parameters.StartTrajectory = ivec[0]; + } - // A couple of wrapper classes - template - void Run(int argc, char **argv, IOCheckpointer &Checkpoint) { - NoSmearing S; - Runner(argc, argv, Checkpoint, S); - } + if (GridCmdOptionExists(argv, argv + argc, "--Trajectories")) { + arg = GridCmdOptionPayload(argv, argv + argc, "--Trajectories"); + std::vector ivec(0); + GridCmdOptionIntVector(arg, ivec); + Payload.Parameters.Trajectories = ivec[0]; + } - template - void Run(int argc, char **argv, IOCheckpointer &CP, SmearingPolicy &S) { - Runner(argc, argv, CP, S); - } - ////////////////////////////// + if (GridCmdOptionExists(argv, argv + argc, "--Thermalizations")) { + arg = GridCmdOptionPayload(argv, argv + argc, "--Thermalizations"); + std::vector ivec(0); + GridCmdOptionIntVector(arg, ivec); + Payload.Parameters.NoMetropolisUntil = ivec[0]; + } - + } + // A couple of wrapper functions + template void Run(IOCheckpointer &CP) { + NoSmearing S; + Runner(CP, S); + } - template - void Runner(int argc, - char ** argv, - IOCheckpointer &Checkpoint, - SmearingPolicy &Smearing) { - StartType_t StartType = HotStart; + template void Run(IOCheckpointer &CP, SmearingPolicy &S) { + Runner(CP, S); + } - std::string arg; + ////////////////////////////////////////////////////////////////// - if (GridCmdOptionExists(argv, argv + argc, "--StartType")) { - arg = GridCmdOptionPayload(argv, argv + argc, "--StartType"); - if (arg == "HotStart") { - StartType = HotStart; - } else if (arg == "ColdStart") { - StartType = ColdStart; - } else if (arg == "TepidStart") { - StartType = TepidStart; - } else if (arg == "CheckpointStart") { - StartType = CheckpointStart; - } else { - std::cout << GridLogError << "Unrecognized option in --StartType\n"; - std::cout - << GridLogError - << "Valid [HotStart, ColdStart, TepidStart, CheckpointStart]\n"; - assert(0); - } - } +private: + template + void Runner(IOCheckpointer &Checkpoint, SmearingPolicy &Smearing) { + auto UGrid = Resources.GetCartesian(); + Resources.AddRNGs(); + Field U(UGrid); - int StartTraj = 0; - if (GridCmdOptionExists(argv, argv + argc, "--StartTrajectory")) { - arg = GridCmdOptionPayload(argv, argv + argc, "--StartTrajectory"); - std::vector ivec(0); - GridCmdOptionIntVector(arg, ivec); - StartTraj = ivec[0]; - } + typedef IntegratorType TheIntegrator; + TheIntegrator MDynamics(UGrid, MDparameters, TheAction, Smearing); - int NumTraj = 1; - if (GridCmdOptionExists(argv, argv + argc, "--Trajectories")) { - arg = GridCmdOptionPayload(argv, argv + argc, "--Trajectories"); - std::vector ivec(0); - GridCmdOptionIntVector(arg, ivec); - NumTraj = ivec[0]; - } - - int NumThermalizations = 10; - if (GridCmdOptionExists(argv, argv + argc, "--Thermalizations")) { - arg = GridCmdOptionPayload(argv, argv + argc, "--Thermalizations"); - std::vector ivec(0); - GridCmdOptionIntVector(arg, ivec); - NumThermalizations = ivec[0]; - } - - GridSerialRNG sRNG; - GridParallelRNG pRNG(UGrid); - Field U(UGrid); - - - typedef MinimumNorm2 IntegratorType; // change here to change the algorithm - IntegratorType MDynamics(UGrid, MDparameters, TheAction, Smearing); - - HMCparameters HMCpar; - HMCpar.StartTrajectory = StartTraj; - HMCpar.Trajectories = NumTraj; - HMCpar.NoMetropolisUntil = NumThermalizations; - - if (StartType == HotStart) { + if (Payload.StartType == HotStart) { // Hot start - HMCpar.MetropolisTest = true; - sRNG.SeedFixedIntegers(SerialSeed); - pRNG.SeedFixedIntegers(ParallelSeed); - Implementation::HotConfiguration(pRNG, U); - } else if (StartType == ColdStart) { + Payload.Parameters.MetropolisTest = true; + Resources.SeedFixedIntegers(); + Implementation::HotConfiguration(Resources.GetParallelRNG(), U); + } else if (Payload.StartType == ColdStart) { // Cold start - HMCpar.MetropolisTest = true; - sRNG.SeedFixedIntegers(SerialSeed); - pRNG.SeedFixedIntegers(ParallelSeed); - Implementation::ColdConfiguration(pRNG, U); - } else if (StartType == TepidStart) { + Payload.Parameters.MetropolisTest = true; + Resources.SeedFixedIntegers(); + Implementation::ColdConfiguration(Resources.GetParallelRNG(), U); + } else if (Payload.StartType == TepidStart) { // Tepid start - HMCpar.MetropolisTest = true; - sRNG.SeedFixedIntegers(SerialSeed); - pRNG.SeedFixedIntegers(ParallelSeed); - Implementation::TepidConfiguration(pRNG, U); - } else if (StartType == CheckpointStart) { - HMCpar.MetropolisTest = true; + Payload.Parameters.MetropolisTest = true; + Resources.SeedFixedIntegers(); + Implementation::TepidConfiguration(Resources.GetParallelRNG(), U); + } else if (Payload.StartType == CheckpointStart) { + Payload.Parameters.MetropolisTest = true; // CheckpointRestart - Checkpoint.CheckpointRestore(StartTraj, U, sRNG, pRNG); - } + Checkpoint.CheckpointRestore(Payload.Parameters.StartTrajectory, U, Resources.GetSerialRNG(), Resources.GetParallelRNG()); + } - Smearing.set_Field(U); + Smearing.set_Field(U); - HybridMonteCarlo HMC(HMCpar, MDynamics, sRNG, pRNG, U); + HybridMonteCarlo HMC(Payload.Parameters, MDynamics, Resources.GetSerialRNG(), Resources.GetParallelRNG(), U); - for (int obs = 0; obs < ObservablesList.size(); obs++) - HMC.AddObservable(ObservablesList[obs]); + for (int obs = 0; obs < ObservablesList.size(); obs++) + HMC.AddObservable(ObservablesList[obs]); // Run it - HMC.evolve(); - } + HMC.evolve(); +} }; - // These are for gauge fields - typedef BinaryHmcRunnerTemplate BinaryHmcRunner; - typedef BinaryHmcRunnerTemplate BinaryHmcRunnerF; - typedef BinaryHmcRunnerTemplate BinaryHmcRunnerD; +// These are for gauge fields, default integrator MinimumNorm2 +template