1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-06-12 20:27:06 +01:00

Added all required functionalities, time for cleaning

All actions to be added
This commit is contained in:
Guido Cossu
2017-01-18 16:31:51 +00:00
parent 924130833e
commit 23e0561dd6
15 changed files with 454 additions and 91 deletions

View File

@ -35,17 +35,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
namespace Grid {
namespace QCD {
class HMCBase{
// very ugly here but possibly resolved if we have a base Reader class
template < class ReaderClass >
class HMCRunnerBase {
public:
virtual void Run() = 0;
virtual void initialize(ReaderClass& ) = 0;
};
template <class Implementation,
template <typename, typename, typename> class Integrator,
class RepresentationsPolicy = NoHirep>
class HMCWrapperTemplate: public HMCBase {
class RepresentationsPolicy = NoHirep, class ReaderClass = XmlReader>
class HMCWrapperTemplate: public HMCRunnerBase<ReaderClass> {
public:
INHERIT_FIELD_TYPES(Implementation);
typedef Implementation ImplPolicy; // visible from outside
@ -55,11 +58,24 @@ class HMCWrapperTemplate: public HMCBase {
HMCparameters Parameters;
HMCResourceManager<Implementation> Resources;
// The set of actions
// The set of actions (keep here for lower level users, for now)
ActionSet<Field, RepresentationsPolicy> TheAction;
// A vector of HmcObservable that can be injected from outside
std::vector<HmcObservable<typename Implementation::Field> *> ObservablesList;
HMCWrapperTemplate() = default;
HMCWrapperTemplate(HMCparameters Par){
Parameters = Par;
}
void initialize(ReaderClass & TheReader){
std::cout << "Initialization of the HMC" << std::endl;
Resources.initialize(TheReader);
// eventually add smearing
Resources.GetActionSet(TheAction);
}
void ReadCommandLine(int argc, char **argv) {
std::string arg;
@ -147,12 +163,8 @@ class HMCWrapperTemplate: public HMCBase {
HybridMonteCarlo<TheIntegrator> HMC(Parameters, MDynamics,
Resources.GetSerialRNG(),
Resources.GetParallelRNG(), U);
for (int obs = 0; obs < ObservablesList.size(); obs++)
HMC.AddObservable(ObservablesList[obs]);
HMC.AddObservable(Resources.GetCheckPointer());
Resources.GetParallelRNG(),
Resources.GetObservables(), U);
// Run it
HMC.evolve();
@ -172,6 +184,11 @@ template <class RepresentationsPolicy,
using GenericHMCRunnerHirep =
HMCWrapperTemplate<PeriodicGimplR, Integrator, RepresentationsPolicy>;
template <class Implementation, class RepresentationsPolicy,
template <typename, typename, typename> class Integrator>
using GenericHMCRunnerTemplate = HMCWrapperTemplate<Implementation, Integrator, RepresentationsPolicy>;
typedef HMCWrapperTemplate<ScalarImplR, MinimumNorm2, ScalarFields>
ScalarGenericHMCRunner;

View File

@ -87,6 +87,8 @@ struct HMCparameters: Serializable {
};
// Move this to a different file
template <class Field>
class HmcObservable {
public:
@ -95,13 +97,13 @@ class HmcObservable {
};
// this is only defined for a gauge theory
template <class Gimpl>
class PlaquetteLogger : public HmcObservable<typename Gimpl::Field> {
template <class Impl>
class PlaquetteLogger : public HmcObservable<typename Impl::Field> {
private:
std::string Stem;
public:
INHERIT_GIMPL_TYPES(Gimpl);
INHERIT_GIMPL_TYPES(Impl);
PlaquetteLogger(std::string cf) { Stem = cf; };
void TrajectoryComplete(int traj, GaugeField &U, GridSerialRNG &sRNG,
@ -117,9 +119,10 @@ class PlaquetteLogger : public HmcObservable<typename Gimpl::Field> {
RealD peri_plaq = WilsonLoops<PeriodicGimplR>::avgPlaquette(U);
RealD peri_rect = WilsonLoops<PeriodicGimplR>::avgRectangle(U);
RealD impl_plaq = WilsonLoops<Gimpl>::avgPlaquette(U);
RealD impl_rect = WilsonLoops<Gimpl>::avgRectangle(U);
RealD impl_plaq = WilsonLoops<Impl>::avgPlaquette(U);
RealD impl_rect = WilsonLoops<Impl>::avgRectangle(U);
// Fixme reorganise this output
of << traj << " " << impl_plaq << " " << impl_rect << " " << peri_plaq
<< " " << peri_rect << std::endl;
std::cout << GridLogMessage << "traj"
@ -135,6 +138,8 @@ class PlaquetteLogger : public HmcObservable<typename Gimpl::Field> {
<< " " << peri_plaq << " " << peri_rect << std::endl;
}
};
//////////////////////////////////////////////////////////////
template <class IntegratorType>
class HybridMonteCarlo {
@ -142,13 +147,16 @@ class HybridMonteCarlo {
const HMCparameters Params;
typedef typename IntegratorType::Field Field;
typedef std::vector< HmcObservable<Field> * > ObsListType;
GridSerialRNG &sRNG; // Fixme: need a RNG management strategy.
GridParallelRNG &pRNG; // Fixme: need a RNG management strategy.
//pass these from the resource manager
GridSerialRNG &sRNG;
GridParallelRNG &pRNG;
Field &Ucur;
IntegratorType &TheIntegrator;
std::vector<HmcObservable<Field> *> Observables;
ObsListType Observables;
/////////////////////////////////////////////////////////
// Metropolis step
@ -209,15 +217,12 @@ class HybridMonteCarlo {
/////////////////////////////////////////
// Constructor
/////////////////////////////////////////
HybridMonteCarlo(HMCparameters Pams, IntegratorType &_Int,
GridSerialRNG &_sRNG, GridParallelRNG &_pRNG, Field &_U)
: Params(Pams), TheIntegrator(_Int), sRNG(_sRNG), pRNG(_pRNG), Ucur(_U) {}
HybridMonteCarlo(HMCparameters _Pams, IntegratorType &_Int,
GridSerialRNG &_sRNG, GridParallelRNG &_pRNG,
ObsListType _Obs, Field &_U)
: Params(_Pams), TheIntegrator(_Int), sRNG(_sRNG), pRNG(_pRNG), Observables(_Obs), Ucur(_U) {}
~HybridMonteCarlo(){};
void AddObservable(HmcObservable<Field> *obs) {
Observables.push_back(obs);
}
void evolve(void) {
Real DeltaH;
@ -262,6 +267,7 @@ class HybridMonteCarlo {
std::cout << GridLogMessage << ":::::::::::::::::::::::::::::::::::::::::::" << std::endl;
}
}
};
} // QCD

View File

@ -33,6 +33,7 @@ directory
namespace Grid {
namespace QCD {
// call these: resources
// Some modules for the basic setup
@ -166,7 +167,6 @@ public:
};
/*
///////////////////////////////////////////////////////////////////
/// Smearing module

View File

@ -56,15 +56,23 @@ namespace QCD {
template <class ImplementationPolicy>
class HMCResourceManager {
typedef HMCModuleBase< QCD::BaseHmcCheckpointer<ImplementationPolicy> > CheckpointerBaseModule;
typedef HMCModuleBase< QCD::HmcObservable<typename ImplementationPolicy::Field> > ObservableBaseModule;
typedef HMCModuleBase< QCD::Action<typename ImplementationPolicy::Field> > ActionBaseModule;
// Named storage for grid pairs (std + red-black)
std::unordered_map<std::string, GridModule> Grids;
RNGModule RNGs;
// SmearingModule<ImplementationPolicy> Smearing;
std::unique_ptr<CheckpointerBaseModule > CP;
std::unique_ptr<CheckpointerBaseModule> CP;
// A vector of HmcObservable modules
std::vector<std::unique_ptr<ObservableBaseModule> > ObservablesList;
// A vector of HmcObservable modules
std::multimap<int, std::unique_ptr<ActionBaseModule> > ActionsList;
std::vector<int> multipliers;
bool have_RNG;
bool have_CheckPointer;
@ -96,9 +104,68 @@ class HMCResourceManager {
RNGModuleParameters RNGpar(Read);
SetRNGSeeds(RNGpar);
// HMC here
// Observables
auto &ObsFactory = HMC_ObservablesModuleFactory<observable_string, ReaderClass>::getInstance();
Read.push(observable_string);// here must check if existing...
do {
std::string obs_type;
Read.readDefault("name", obs_type);
std::cout << "Registered types " << std::endl;
std::cout << ObsFactory.getBuilderList() << std::endl;
ObservablesList.emplace_back(ObsFactory.create(obs_type, Read));
ObservablesList[ObservablesList.size() - 1]->print_parameters();
} while (Read.nextElement(observable_string));
std::cout << "Size of ObservablesList " << ObservablesList.size()
<< std::endl;
Read.pop();
// Loop on levels
Read.push("Actions");
Read.push("Level");// push must check if the node exist
do {
fill_ActionsLevel(Read);
} while(Read.nextElement("Level"));
Read.pop();
}
// this private
template <class ReaderClass >
void fill_ActionsLevel(ReaderClass &Read){
// Actions set
int m;
Read.readDefault("multiplier",m);
multipliers.push_back(m);
std::cout << "Level : " << multipliers.size() << " with multiplier : " << m << std::endl;
// here gauge
Read.push("Action");
do{
auto &ActionFactory = HMC_LGTActionModuleFactory<gauge_string, ReaderClass>::getInstance();
std::string action_type;
Read.readDefault("name", action_type);
std::cout << "Registered types " << std::endl;
std::cout << ActionFactory.getBuilderList() << std::endl;
ActionsList.emplace(m, ActionFactory.create(action_type, Read));
} while (Read.nextElement("Action"));
ActionsList.find(m)->second->print_parameters();
}
template <class RepresentationPolicy>
void GetActionSet(ActionSet<typename ImplementationPolicy::Field, RepresentationPolicy>& Aset){
Aset.resize(multipliers.size());
for(auto it = ActionsList.begin(); it != ActionsList.end(); it++)
Aset[(*it).first-1].push_back((*it).second->getPtr());
}
//////////////////////////////////////////////////////////////
// Grids
//////////////////////////////////////////////////////////////
@ -181,6 +248,29 @@ class HMCResourceManager {
RegisterLoadCheckPointerFunction(Binary);
RegisterLoadCheckPointerFunction(Nersc);
RegisterLoadCheckPointerFunction(ILDG);
////////////////////////////////////////////////////////
// Observables
////////////////////////////////////////////////////////
void AddObservable(ObservableBaseModule *O){
// acquire resource
ObservablesList.push_back(std::unique_ptr<ObservableBaseModule>(std::move(O)));
}
std::vector<HmcObservable<typename ImplementationPolicy::Field>* > GetObservables(){
std::vector<HmcObservable<typename ImplementationPolicy::Field>* > out;
for (auto &i : ObservablesList){
out.push_back(i->getPtr());
}
// Add the checkpointer to the observables
out.push_back(GetCheckPointer());
return out;
}
};
}
}

View File

@ -0,0 +1,138 @@
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/qcd/hmc/GenericHmcRunner.h
Copyright (C) 2015
Copyright (C) 2016
Author: Guido Cossu <guido.cossu@ed.ac.uk>
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 HMC_RUNNER_MODULE
#define HMC_RUNNER_MODULE
namespace Grid {
// the reader class is necessary here for the automatic initialization of the resources
// if we had a virtual reader would have been unecessary
template <class HMCType, class ReaderClass >
class HMCModule
: public Parametrized< QCD::HMCparameters >,
public HMCModuleBase< QCD::HMCRunnerBase<ReaderClass> > {
public:
typedef HMCModuleBase< QCD::HMCRunnerBase<ReaderClass> > Base;
typedef typename Base::Product Product;
std::unique_ptr<HMCType> HMCPtr;
HMCModule(QCD::HMCparameters Par) : Parametrized<QCD::HMCparameters>(Par) {}
template <class ReaderCl>
HMCModule(Reader<ReaderCl>& R) : Parametrized<QCD::HMCparameters>(R, "HMC"){};
Product* getPtr() {
if (!HMCPtr) initialize();
return HMCPtr.get();
}
private:
virtual void initialize() = 0;
};
// Factory
template <char const *str, class ReaderClass >
class HMCRunnerModuleFactory
: public Factory < HMCModuleBase< QCD::HMCRunnerBase<ReaderClass> > , Reader<ReaderClass> > {
public:
typedef Reader<ReaderClass> TheReader;
// use SINGLETON FUNCTOR MACRO HERE
HMCRunnerModuleFactory(const HMCRunnerModuleFactory& e) = delete;
void operator=(const HMCRunnerModuleFactory& e) = delete;
static HMCRunnerModuleFactory& getInstance(void) {
static HMCRunnerModuleFactory e;
return e;
}
private:
HMCRunnerModuleFactory(void) = default;
std::string obj_type() const {
return std::string(str);
}
};
///////////////
// macro for these
template < class ImplementationPolicy, class RepresentationPolicy, class ReaderClass >
class HMCLeapFrog: public HMCModule< QCD::GenericHMCRunnerTemplate<ImplementationPolicy, RepresentationPolicy, QCD::LeapFrog>, ReaderClass >{
typedef HMCModule< QCD::GenericHMCRunnerTemplate<ImplementationPolicy, RepresentationPolicy, QCD::LeapFrog>, ReaderClass > HMCBaseMod;
using HMCBaseMod::HMCBaseMod;
// aquire resource
virtual void initialize(){
this->HMCPtr.reset(new QCD::GenericHMCRunnerTemplate<ImplementationPolicy, RepresentationPolicy, QCD::LeapFrog>(this->Par_) );
}
};
template < class ImplementationPolicy, class RepresentationPolicy, class ReaderClass >
class HMCMinimumNorm2: public HMCModule< QCD::GenericHMCRunnerTemplate<ImplementationPolicy, RepresentationPolicy, QCD::MinimumNorm2>, ReaderClass >{
typedef HMCModule< QCD::GenericHMCRunnerTemplate<ImplementationPolicy, RepresentationPolicy, QCD::MinimumNorm2>, ReaderClass > HMCBaseMod;
using HMCBaseMod::HMCBaseMod;
// aquire resource
virtual void initialize(){
std::cout << "Initializing the pointer" << std::endl;
this->HMCPtr.reset(new QCD::GenericHMCRunnerTemplate<ImplementationPolicy, RepresentationPolicy, QCD::MinimumNorm2>(this->Par_));
}
};
template < class ImplementationPolicy, class RepresentationPolicy, class ReaderClass >
class HMCForceGradient: public HMCModule< QCD::GenericHMCRunnerTemplate<ImplementationPolicy, RepresentationPolicy, QCD::ForceGradient>, ReaderClass >{
typedef HMCModule< QCD::GenericHMCRunnerTemplate<ImplementationPolicy, RepresentationPolicy, QCD::ForceGradient>, ReaderClass > HMCBaseMod;
using HMCBaseMod::HMCBaseMod;
// aquire resource
virtual void initialize(){
this->HMCPtr.reset(new QCD::GenericHMCRunnerTemplate<ImplementationPolicy, RepresentationPolicy, QCD::ForceGradient>(this->Par_) );
}
};
extern char hmc_string[];
//////////////////////////////////////////////////////////////
}
#endif

View File

@ -37,4 +37,4 @@ directory
#include <Grid/qcd/hmc/checkpointers/CheckPointerModules.h>
#endif // CHECKPOINTERS_H
#endif // CHECKPOINTERS_H