1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-11 22:50:45 +01:00

Cleaning up the checkpointers interface

This commit is contained in:
Guido Cossu 2017-01-05 15:52:52 +00:00
parent 1bb8578173
commit 1189ebc8b5
7 changed files with 172 additions and 207 deletions

View File

@ -96,7 +96,6 @@ class StoutSmearingModule: public SmearingModule<ImplementationPolicy>{
SmearedConfiguration<ImplementationPolicy> SmearingPolicy;
};
// Checkpoint module, owns the Checkpointer
template <class ImplementationPolicy>
class CheckPointModule {
@ -106,19 +105,14 @@ public:
void set_Checkpointer(BaseHmcCheckpointer<ImplementationPolicy>* cp) {
cp_.reset(cp);
};
BaseHmcCheckpointer<ImplementationPolicy>* get_CheckPointer() {
std::cout << "Checkpointer Pointer requested : " << cp_.get() << std::endl;
return cp_.get();
}
void initialize(CheckpointerParameters& P){
cp_.initialize(P);
}
void initialize(CheckpointerParameters& P) { cp_.initialize(P); }
};
} // namespace QCD
} // namespace Grid

View File

@ -34,7 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
// One function per Checkpointer, use a macro to simplify
#define RegisterLoadCheckPointerFunction(NAME) \
void Load##NAME##Checkpointer(CheckpointerParameters& Params_) { \
void Load##NAME##Checkpointer(const CheckpointerParameters& Params_) { \
if (!have_CheckPointer) { \
std::cout << GridLogDebug << "Loading Checkpointer " << #NAME \
<< std::endl; \
@ -48,7 +48,22 @@ 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 <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); \
} \
}
namespace Grid {
namespace QCD {
@ -141,7 +156,11 @@ class HMCResourceManager{
RegisterLoadCheckPointerFunction (Binary);
RegisterLoadCheckPointerFunction (Nersc);
RegisterLoadCheckPointerFunction (ILDG)
RegisterLoadCheckPointerFunction (ILDG);
RegisterLoadCheckPointerReaderFunction (Binary);
RegisterLoadCheckPointerReaderFunction (Nersc);
RegisterLoadCheckPointerReaderFunction (ILDG);
};
}

View File

@ -34,13 +34,22 @@ namespace QCD {
class CheckpointerParameters : Serializable {
public:
GRID_SERIALIZABLE_CLASS_MEMBERS(CheckpointerParameters, std::string,
configStem, std::string, rngStem, int,
SaveInterval, std::string, format, );
GRID_SERIALIZABLE_CLASS_MEMBERS(CheckpointerParameters,
std::string, config_prefix,
std::string, rng_prefix,
int, saveInterval,
std::string, format, );
CheckpointerParameters(std::string cf = "cfg", std::string rn = "rng",
int savemodulo = 1, const std::string &f = "IEEE64BIG")
: configStem(cf), rngStem(rn), SaveInterval(savemodulo), format(f){};
: config_prefix(cf), rng_prefix(rn), saveInterval(savemodulo), format(f){};
template<class ReaderClass>
CheckpointerParameters(ReaderClass &Reader){
read(Reader, "Checkpointer", *this);
}
};
//////////////////////////////////////////////////////////////////////////////
@ -48,7 +57,22 @@ class CheckpointerParameters : Serializable {
template <class Impl>
class BaseHmcCheckpointer : public HmcObservable<typename Impl::Field> {
public:
virtual void initialize(CheckpointerParameters &Params) = 0;
void build_filenames(int traj, CheckpointerParameters &Params,
std::string &conf_file, std::string &rng_file) {
{
std::ostringstream os;
os << Params.rng_prefix << "." << traj;
rng_file = os.str();
}
{
std::ostringstream os;
os << Params.config_prefix << "." << traj;
conf_file = os.str();
}
}
virtual void initialize(const CheckpointerParameters &Params) = 0;
virtual void CheckpointRestore(int traj, typename Impl::Field &U,
GridSerialRNG &sRNG,

View File

@ -2,11 +2,11 @@
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/qcd/hmc/NerscCheckpointer.h
Source file: ./lib/qcd/hmc/BinaryCheckpointer.h
Copyright (C) 2015
Copyright (C) 2016
Author: paboyle <paboyle@ph.ed.ac.uk>
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
@ -36,7 +36,6 @@ directory
namespace Grid {
namespace QCD {
// Simple checkpointer, only binary file
template <class Impl>
class BinaryHmcCheckpointer : public BaseHmcCheckpointer<Impl> {
@ -52,11 +51,11 @@ class BinaryHmcCheckpointer : public BaseHmcCheckpointer<Impl> {
typedef typename getPrecision<sobj>::real_scalar_type sobj_stype;
typedef typename sobj::DoublePrecision sobj_double;
BinaryHmcCheckpointer(CheckpointerParameters& Params_){
BinaryHmcCheckpointer(const CheckpointerParameters &Params_) {
initialize(Params_);
}
void initialize(CheckpointerParameters& Params_){ Params = Params_; }
void initialize(const CheckpointerParameters &Params_) { Params = Params_; }
void truncate(std::string file) {
std::ofstream fout(file, std::ios::out);
@ -65,19 +64,9 @@ class BinaryHmcCheckpointer : public BaseHmcCheckpointer<Impl> {
void TrajectoryComplete(int traj, Field &U, GridSerialRNG &sRNG,
GridParallelRNG &pRNG) {
if ((traj % Params.SaveInterval) == 0) {
std::string rng;
{
std::ostringstream os;
os << Params.rngStem << "." << traj;
rng = os.str();
}
std::string config;
{
std::ostringstream os;
os << Params.configStem << "." << traj;
config = os.str();
}
if ((traj % Params.saveInterval) == 0) {
std::string config, rng;
this->build_filenames(traj, Params, config, rng);
BinaryIO::BinarySimpleUnmunger<sobj_double, sobj> munge;
truncate(rng);
@ -93,18 +82,8 @@ class BinaryHmcCheckpointer : public BaseHmcCheckpointer<Impl> {
void CheckpointRestore(int traj, Field &U, GridSerialRNG &sRNG,
GridParallelRNG &pRNG) {
std::string rng;
{
std::ostringstream os;
os << Params.rngStem << "." << traj;
rng = os.str();
}
std::string config;
{
std::ostringstream os;
os << Params.configStem << "." << traj;
config = os.str();
}
std::string config, rng;
this->build_filenames(traj, Params, config, rng);
BinaryIO::BinarySimpleMunger<sobj_double, sobj> munge;
BinaryIO::readRNGSerial(sRNG, pRNG, rng, 0);

View File

@ -40,23 +40,16 @@ namespace QCD {
// Only for Gauge fields
template <class Implementation>
class ILDGHmcCheckpointer
: public BaseHmcCheckpointer<Implementation> {
class ILDGHmcCheckpointer : public BaseHmcCheckpointer<Implementation> {
private:
CheckpointerParameters Params;
/*
std::string configStem;
std::string rngStem;
int SaveInterval;
std::string format;
*/
public:
INHERIT_GIMPL_TYPES(Implementation);
ILDGHmcCheckpointer(CheckpointerParameters &Params_) { initialize(Params_); }
ILDGHmcCheckpointer(const CheckpointerParameters &Params_) { initialize(Params_); }
void initialize(CheckpointerParameters &Params_) {
void initialize(const CheckpointerParameters &Params_) {
Params = Params_;
// check here that the format is valid
@ -78,19 +71,9 @@ class ILDGHmcCheckpointer
void TrajectoryComplete(int traj, GaugeField &U, GridSerialRNG &sRNG,
GridParallelRNG &pRNG) {
if ((traj % Params.SaveInterval) == 0) {
std::string rng;
{
std::ostringstream os;
os << Params.rngStem << "." << traj;
rng = os.str();
}
std::string config;
{
std::ostringstream os;
os << Params.configStem << "." << traj;
config = os.str();
}
if ((traj % Params.saveInterval) == 0) {
std::string config, rng;
this->build_filenames(traj, Params, config, rng);
ILDGIO IO(config, ILDGwrite);
BinaryIO::writeRNGSerial(sRNG, pRNG, rng, 0);
@ -103,18 +86,8 @@ class ILDGHmcCheckpointer
void CheckpointRestore(int traj, GaugeField &U, GridSerialRNG &sRNG,
GridParallelRNG &pRNG) {
std::string rng;
{
std::ostringstream os;
os << Params.rngStem << "." << traj;
rng = os.str();
}
std::string config;
{
std::ostringstream os;
os << Params.configStem << "." << traj;
config = os.str();
}
std::string config, rng;
this->build_filenames(traj, Params, config, rng);
ILDGIO IO(config, ILDGread);
BinaryIO::readRNGSerial(sRNG, pRNG, rng, 0);

View File

@ -22,16 +22,16 @@ Author: paboyle <paboyle@ph.ed.ac.uk>
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
See the full license in the file "LICENSE" in the top level distribution
directory
*************************************************************************************/
/* END LEGAL */
#ifndef NERSC_CHECKPOINTER
#define NERSC_CHECKPOINTER
#include <string>
#include <iostream>
#include <sstream>
#include <string>
namespace Grid {
namespace QCD {
@ -43,32 +43,20 @@ namespace Grid{
CheckpointerParameters Params;
public:
INHERIT_GIMPL_TYPES(Gimpl);//
INHERIT_GIMPL_TYPES(Gimpl); // only for gauge configurations
NerscHmcCheckpointer(CheckpointerParameters& Params_){
initialize(Params_);
}
NerscHmcCheckpointer(const CheckpointerParameters &Params_) { initialize(Params_); }
void initialize(CheckpointerParameters &Params_) {
void initialize(const CheckpointerParameters &Params_) {
Params = Params_;
Params.format = "IEEE64BIG"; // fixed, overwrite any other choice
}
void TrajectoryComplete(int traj, GaugeField &U, GridSerialRNG &sRNG,
GridParallelRNG &pRNG) {
if ((traj % Params.SaveInterval) == 0) {
std::string rng;
{
std::ostringstream os;
os << Params.rngStem << "." << traj;
rng = os.str();
}
std::string config;
{
std::ostringstream os;
os << Params.configStem << "." << traj;
config = os.str();
}
if ((traj % Params.saveInterval) == 0) {
std::string config, rng;
this->build_filenames(traj, Params, config, rng);
int precision32 = 1;
int tworow = 0;
@ -79,24 +67,14 @@ namespace Grid{
void CheckpointRestore(int traj, GaugeField &U, GridSerialRNG &sRNG,
GridParallelRNG &pRNG) {
std::string rng;
{
std::ostringstream os;
os << Params.rngStem << "." << traj;
rng = os.str();
}
std::string config;
{
std::ostringstream os;
os << Params.configStem << "." << traj;
config = os.str();
}
std::string config, rng;
this->build_filenames(traj, Params, config, rng);
NerscField header;
NerscIO::readRNGState(sRNG, pRNG, header, rng);
NerscIO::readConfiguration(U, header, config);
};
};
}}
}
}
#endif

View File

@ -34,7 +34,7 @@ namespace Grid {
namespace QCD {
//Change here the type of reader
typedef Grid::TextReader InputFileReader;
typedef Grid::XmlReader InputFileReader;
class HMCRunnerParameters : Serializable {
@ -42,11 +42,11 @@ namespace Grid {
GRID_SERIALIZABLE_CLASS_MEMBERS(HMCRunnerParameters,
double, beta,
int, MDsteps,
double, TrajectorLength,
int, SaveInterval,
std::string, format,
std::string, conf_prefix,
std::string, rng_prefix,
double, TrajectoryLength,
//int, SaveInterval,
//std::string, format,
//std::string, conf_prefix,
//std::string, rng_prefix,
std::string, serial_seeds,
std::string, parallel_seeds,
);
@ -66,6 +66,7 @@ int main(int argc, char **argv) {
// Typedefs to simplify notation
typedef GenericHMCRunner<MinimumNorm2> HMCWrapper; // Uses the default minimum norm
// 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;
//////////////////////////////////////////////////////////////
@ -74,21 +75,18 @@ int main(int argc, char **argv) {
// now working with the text reader but I should drop this support
// i need a structured format where every object is able
// to locate the required data: XML, JSON, YAML.
InputFileReader Reader("input.wilson_gauge.params.xml");
HMCRunnerParameters HMCPar;
InputFileReader Reader("input.wilson_gauge.params");
read(Reader, "HMC", HMCPar);
std::cout << GridLogMessage << HMCPar << std::endl;
// Seeds for the random number generators
// generalise, ugly now
std::vector<int> SerSeed = strToVec<int>(HMCPar.serial_seeds);
std::vector<int> ParSeed = strToVec<int>(HMCPar.parallel_seeds);
CheckpointerParameters CP_params(HMCPar.conf_prefix, HMCPar.rng_prefix,
HMCPar.SaveInterval, HMCPar.format);
HMCWrapper TheHMC;
TheHMC.Resources.AddFourDimGrid("gauge");
TheHMC.Resources.LoadBinaryCheckpointer(CP_params);
TheHMC.Resources.LoadBinaryCheckpointer(Reader);
/////////////////////////////////////////////////////////////
// Collect actions, here use more encapsulation
@ -112,7 +110,7 @@ int main(int argc, char **argv) {
// here we can simplify a lot if the input file is structured
// just pass the input file reader
TheHMC.Resources.AddRNGSeeds(SerSeed, ParSeed);
TheHMC.MDparameters.set(HMCPar.MDsteps, HMCPar.TrajectorLength);
TheHMC.MDparameters.set(HMCPar.MDsteps, HMCPar.TrajectoryLength);
// eventually smearing here
// ...