1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-09-21 01:25:48 +01:00
Grid/lib/qcd/hmc/HMCResourceManager.h

169 lines
6.1 KiB
C
Raw Normal View History

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
#define RegisterLoadCheckPointerFunction(NAME) \
void Load##NAME##Checkpointer(const CheckpointerParameters& Params_) { \
if (!have_CheckPointer) { \
std::cout << GridLogDebug << "Loading Checkpointer " << #NAME \
<< std::endl; \
CP.set_Checkpointer( \
new NAME##HmcCheckpointer<ImplementationPolicy>(Params_)); \
have_CheckPointer = true; \
} else { \
std::cout << GridLogError << "Checkpointer already loaded " \
<< std::endl; \
exit(1); \
} \
2017-01-05 13:09:32 +00:00
}
// One function per Checkpointer using the reader, use a macro to simplify
#define RegisterLoadCheckPointerReaderFunction(NAME) \
template <class Reader> \
void Load##NAME##Checkpointer(Reader& Reader_) { \
if (!have_CheckPointer) { \
std::cout << GridLogDebug << "Loading Checkpointer " << #NAME \
<< std::endl; \
CP.set_Checkpointer(new NAME##HmcCheckpointer<ImplementationPolicy>( \
CheckpointerParameters(Reader_))); \
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
template <class ImplementationPolicy>
class HMCResourceManager{
// Storage for grid pairs (std + red-black)
std::unordered_map<std::string, GridModule> Grids;
RNGModule RNGs;
//SmearingModule<ImplementationPolicy> Smearing;
CheckPointModule<ImplementationPolicy> CP;
2016-12-22 12:41:56 +00:00
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) {}
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);
}
// Add a named grid set
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-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-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;
}
void AddRNGSeeds(const std::vector<int> S, const std::vector<int> P) {
2017-01-05 13:09:32 +00:00
RNGs.set_RNGSeeds(S, P);
2016-12-22 12:41:56 +00:00
}
2017-01-05 13:09:32 +00:00
GridSerialRNG& GetSerialRNG() { return RNGs.get_sRNG(); }
2016-12-22 12:41:56 +00:00
GridParallelRNG& GetParallelRNG() {
assert(have_RNG);
return RNGs.get_pRNG();
}
2017-01-05 13:09:32 +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
//////////////////////////////////////////////////////
BaseHmcCheckpointer<ImplementationPolicy>* get_CheckPointer(){
if (have_CheckPointer)
return CP.get_CheckPointer();
else{
std::cout << GridLogError << "Error: no checkpointer defined" << std::endl;
exit(1);
}
}
RegisterLoadCheckPointerFunction (Binary);
RegisterLoadCheckPointerFunction (Nersc);
RegisterLoadCheckPointerFunction (ILDG);
RegisterLoadCheckPointerReaderFunction (Binary);
RegisterLoadCheckPointerReaderFunction (Nersc);
RegisterLoadCheckPointerReaderFunction (ILDG);
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