2016-12-22 12:41:56 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./lib/qcd/hmc/GenericHmcRunner.h
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
2017-01-05 13:09:32 +00:00
|
|
|
Copyright (C) 2016
|
2016-12-22 12:41:56 +00:00
|
|
|
|
|
|
|
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_RESOURCE_MANAGER_H
|
|
|
|
#define HMC_RESOURCE_MANAGER_H
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
// One function per Checkpointer, use a macro to simplify
|
2017-01-05 15:52:52 +00:00
|
|
|
#define RegisterLoadCheckPointerFunction(NAME) \
|
|
|
|
void Load##NAME##Checkpointer(const CheckpointerParameters& Params_) { \
|
|
|
|
if (!have_CheckPointer) { \
|
|
|
|
std::cout << GridLogDebug << "Loading Checkpointer " << #NAME \
|
|
|
|
<< std::endl; \
|
2017-01-16 16:07:12 +00:00
|
|
|
CP = std::unique_ptr<CheckpointerBaseModule>( \
|
|
|
|
new NAME##CPModule<ImplementationPolicy>(Params_)); \
|
2017-01-05 15:52:52 +00:00
|
|
|
have_CheckPointer = true; \
|
|
|
|
} else { \
|
|
|
|
std::cout << GridLogError << "Checkpointer already loaded " \
|
|
|
|
<< std::endl; \
|
|
|
|
exit(1); \
|
|
|
|
} \
|
2017-01-05 13:09:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 12:41:56 +00:00
|
|
|
namespace Grid {
|
|
|
|
namespace QCD {
|
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
// HMC Resource manager
|
2017-01-16 10:18:09 +00:00
|
|
|
template <class ImplementationPolicy>
|
|
|
|
class HMCResourceManager {
|
2017-01-16 16:07:12 +00:00
|
|
|
typedef HMCModuleBase< QCD::BaseHmcCheckpointer<ImplementationPolicy> > CheckpointerBaseModule;
|
2017-01-18 16:31:51 +00:00
|
|
|
typedef HMCModuleBase< QCD::HmcObservable<typename ImplementationPolicy::Field> > ObservableBaseModule;
|
2017-01-20 12:56:20 +00:00
|
|
|
typedef ActionModuleBase< QCD::Action<typename ImplementationPolicy::Field>, GridModule > ActionBaseModule;
|
2017-01-16 16:07:12 +00:00
|
|
|
|
2017-01-16 10:18:09 +00:00
|
|
|
// Named storage for grid pairs (std + red-black)
|
2017-01-05 13:09:32 +00:00
|
|
|
std::unordered_map<std::string, GridModule> Grids;
|
|
|
|
RNGModule RNGs;
|
|
|
|
|
2017-01-16 10:18:09 +00:00
|
|
|
// SmearingModule<ImplementationPolicy> Smearing;
|
2017-01-18 16:31:51 +00:00
|
|
|
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;
|
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
bool have_RNG;
|
|
|
|
bool have_CheckPointer;
|
2016-12-22 12:41:56 +00:00
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
public:
|
|
|
|
HMCResourceManager() : have_RNG(false), have_CheckPointer(false) {}
|
2017-01-16 10:18:09 +00:00
|
|
|
|
2017-01-16 16:07:12 +00:00
|
|
|
template <class ReaderClass >
|
|
|
|
void initialize(ReaderClass &Read){
|
|
|
|
// assumes we are starting from the main node
|
|
|
|
|
|
|
|
// Geometry
|
|
|
|
GridModuleParameters GridPar(Read);
|
|
|
|
GridFourDimModule GridMod( GridPar) ;
|
|
|
|
AddGrid("gauge", GridMod);
|
|
|
|
|
|
|
|
// Checkpointer
|
2017-01-25 18:09:46 +00:00
|
|
|
auto &CPfactory = HMC_CPModuleFactory<cp_string, ImplementationPolicy, ReaderClass >::getInstance();
|
2017-01-16 16:07:12 +00:00
|
|
|
Read.push("Checkpointer");
|
|
|
|
std::string cp_type;
|
2017-01-20 12:56:20 +00:00
|
|
|
read(Read,"name", cp_type);
|
2017-01-16 16:07:12 +00:00
|
|
|
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);
|
|
|
|
|
2017-01-18 16:31:51 +00:00
|
|
|
// Observables
|
|
|
|
auto &ObsFactory = HMC_ObservablesModuleFactory<observable_string, ReaderClass>::getInstance();
|
|
|
|
Read.push(observable_string);// here must check if existing...
|
|
|
|
do {
|
|
|
|
std::string obs_type;
|
2017-01-20 12:56:20 +00:00
|
|
|
read(Read,"name", obs_type);
|
2017-01-18 16:31:51 +00:00
|
|
|
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));
|
|
|
|
Read.pop();
|
|
|
|
|
|
|
|
// Loop on levels
|
2017-01-23 14:57:38 +00:00
|
|
|
if(!Read.push("Actions")){
|
|
|
|
std::cout << "Actions not found" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-01-18 16:31:51 +00:00
|
|
|
|
2017-01-23 14:57:38 +00:00
|
|
|
if(!Read.push("Level")){// push must check if the node exist
|
|
|
|
std::cout << "Level not found" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-01-20 12:56:20 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
fill_ActionsLevel(Read);
|
|
|
|
}
|
|
|
|
while(Read.push("Level"));
|
|
|
|
|
2017-01-18 16:31:51 +00:00
|
|
|
Read.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-19 10:00:02 +00:00
|
|
|
|
2017-01-18 16:31:51 +00:00
|
|
|
template <class RepresentationPolicy>
|
|
|
|
void GetActionSet(ActionSet<typename ImplementationPolicy::Field, RepresentationPolicy>& Aset){
|
|
|
|
Aset.resize(multipliers.size());
|
|
|
|
|
2017-01-20 12:56:20 +00:00
|
|
|
for(auto it = ActionsList.begin(); it != ActionsList.end(); it++){
|
|
|
|
(*it).second->acquireResource(Grids["gauge"]);
|
2017-01-18 16:31:51 +00:00
|
|
|
Aset[(*it).first-1].push_back((*it).second->getPtr());
|
2017-01-20 12:56:20 +00:00
|
|
|
}
|
2017-01-16 16:07:12 +00:00
|
|
|
}
|
2017-01-16 10:18:09 +00:00
|
|
|
|
2017-01-18 16:31:51 +00:00
|
|
|
|
|
|
|
|
2017-01-16 10:18:09 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// Grids
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
void AddGrid(std::string s, GridModule& M) {
|
2016-12-22 12:41:56 +00:00
|
|
|
// Check for name clashes
|
|
|
|
auto search = Grids.find(s);
|
2017-01-05 13:09:32 +00:00
|
|
|
if (search != Grids.end()) {
|
|
|
|
std::cout << GridLogError << "Grid with name \"" << search->first
|
|
|
|
<< "\" already present. Terminating\n";
|
|
|
|
exit(1);
|
2016-12-22 12:41:56 +00:00
|
|
|
}
|
|
|
|
Grids[s] = std::move(M);
|
|
|
|
}
|
|
|
|
|
2017-01-16 10:18:09 +00:00
|
|
|
// Add a named grid set, 4d shortcut
|
2016-12-22 12:41:56 +00:00
|
|
|
void AddFourDimGrid(std::string s) {
|
|
|
|
GridFourDimModule Mod;
|
2017-01-05 13:09:32 +00:00
|
|
|
AddGrid(s, Mod);
|
2016-12-22 12:41:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 10:18:09 +00:00
|
|
|
|
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
GridCartesian* GetCartesian(std::string s = "") {
|
2016-12-22 12:41:56 +00:00
|
|
|
if (s.empty()) s = Grids.begin()->first;
|
2017-01-05 13:09:32 +00:00
|
|
|
std::cout << GridLogDebug << "Getting cartesian grid from: " << s
|
|
|
|
<< std::endl;
|
2016-12-22 12:41:56 +00:00
|
|
|
return Grids[s].get_full();
|
|
|
|
}
|
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
GridRedBlackCartesian* GetRBCartesian(std::string s = "") {
|
2016-12-22 12:41:56 +00:00
|
|
|
if (s.empty()) s = Grids.begin()->first;
|
2017-01-05 13:09:32 +00:00
|
|
|
std::cout << GridLogDebug << "Getting rb-cartesian grid from: " << s
|
|
|
|
<< std::endl;
|
2016-12-22 12:41:56 +00:00
|
|
|
return Grids[s].get_rb();
|
|
|
|
}
|
|
|
|
|
2017-01-16 10:18:09 +00:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// Random number generators
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
void AddRNGs(std::string s = "") {
|
|
|
|
// Couple the RNGs to the GridModule tagged by s
|
|
|
|
// the default is the first grid registered
|
|
|
|
assert(Grids.size() > 0 && !have_RNG);
|
2016-12-22 12:41:56 +00:00
|
|
|
if (s.empty()) s = Grids.begin()->first;
|
2017-01-05 13:09:32 +00:00
|
|
|
std::cout << GridLogDebug << "Adding RNG to grid: " << s << std::endl;
|
2016-12-22 12:41:56 +00:00
|
|
|
RNGs.set_pRNG(new GridParallelRNG(GetCartesian(s)));
|
|
|
|
have_RNG = true;
|
|
|
|
}
|
|
|
|
|
2017-01-16 10:18:09 +00:00
|
|
|
void SetRNGSeeds(RNGModuleParameters& Params) { RNGs.set_RNGSeeds(Params); }
|
2016-12-22 12:41:56 +00:00
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
GridSerialRNG& GetSerialRNG() { return RNGs.get_sRNG(); }
|
2017-01-16 10:18:09 +00:00
|
|
|
|
2016-12-22 12:41:56 +00:00
|
|
|
GridParallelRNG& GetParallelRNG() {
|
|
|
|
assert(have_RNG);
|
|
|
|
return RNGs.get_pRNG();
|
|
|
|
}
|
2017-01-16 10:18:09 +00:00
|
|
|
|
2016-12-22 12:41:56 +00:00
|
|
|
void SeedFixedIntegers() {
|
|
|
|
assert(have_RNG);
|
|
|
|
RNGs.seed();
|
|
|
|
}
|
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// Checkpointers
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
2017-01-16 10:18:09 +00:00
|
|
|
BaseHmcCheckpointer<ImplementationPolicy>* GetCheckPointer() {
|
2017-01-05 13:09:32 +00:00
|
|
|
if (have_CheckPointer)
|
2017-01-16 16:07:12 +00:00
|
|
|
return CP->getPtr();
|
2017-01-16 10:18:09 +00:00
|
|
|
else {
|
|
|
|
std::cout << GridLogError << "Error: no checkpointer defined"
|
|
|
|
<< std::endl;
|
2017-01-05 13:09:32 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 10:18:09 +00:00
|
|
|
RegisterLoadCheckPointerFunction(Binary);
|
|
|
|
RegisterLoadCheckPointerFunction(Nersc);
|
2017-01-25 11:18:44 +00:00
|
|
|
#ifdef HAVE_LIME
|
2017-01-16 10:18:09 +00:00
|
|
|
RegisterLoadCheckPointerFunction(ILDG);
|
2017-01-25 11:18:44 +00:00
|
|
|
#endif
|
2017-01-18 16:31:51 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-19 10:00:02 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// 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);
|
2017-01-23 14:57:38 +00:00
|
|
|
std::cout << ActionFactory.getBuilderList() << std::endl; // temporary
|
2017-01-19 10:00:02 +00:00
|
|
|
ActionsList.emplace(m, ActionFactory.create(action_type, Read));
|
|
|
|
} while (Read.nextElement("Action"));
|
2017-01-23 14:57:38 +00:00
|
|
|
ActionsList.find(m)->second->print_parameters();
|
|
|
|
Read.pop();
|
2017-01-19 10:00:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
};
|
2016-12-22 12:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
#endif // HMC_RESOURCE_MANAGER_H
|