mirror of
https://github.com/paboyle/Grid.git
synced 2024-11-10 07:55:35 +00:00
All functionalities ready.
Todo: add all the fermion action modules
This commit is contained in:
parent
851f2ad8ef
commit
f96fac0aee
@ -84,9 +84,10 @@ Author: paboyle <paboyle@ph.ed.ac.uk>
|
||||
#include <Grid/parallelIO/BinaryIO.h>
|
||||
#include <Grid/parallelIO/IldgIO.h>
|
||||
#include <Grid/parallelIO/NerscIO.h>
|
||||
#include <Grid/qcd/modules/mods.h>
|
||||
|
||||
#include <Grid/qcd/hmc/checkpointers/CheckPointers.h>
|
||||
#include <Grid/qcd/hmc/HMCModules.h>
|
||||
#include <Grid/qcd/modules/mods.h>
|
||||
#include <Grid/qcd/hmc/HMCResourceManager.h>
|
||||
#include <Grid/qcd/hmc/HmcRunner.h>
|
||||
#include <Grid/qcd/hmc/GenericHMCrunner.h>
|
||||
|
@ -56,7 +56,7 @@ 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;
|
||||
typedef ActionModuleBase< QCD::Action<typename ImplementationPolicy::Field>, GridModule > ActionBaseModule;
|
||||
|
||||
// Named storage for grid pairs (std + red-black)
|
||||
std::unordered_map<std::string, GridModule> Grids;
|
||||
@ -91,7 +91,7 @@ class HMCResourceManager {
|
||||
auto &CPfactory = HMC_CPModuleFactory<cp_string, QCD::PeriodicGimplR, ReaderClass >::getInstance();
|
||||
Read.push("Checkpointer");
|
||||
std::string cp_type;
|
||||
Read.readDefault("name", cp_type);
|
||||
read(Read,"name", cp_type);
|
||||
std::cout << "Registered types " << std::endl;
|
||||
std::cout << CPfactory.getBuilderList() << std::endl;
|
||||
|
||||
@ -108,7 +108,7 @@ class HMCResourceManager {
|
||||
Read.push(observable_string);// here must check if existing...
|
||||
do {
|
||||
std::string obs_type;
|
||||
Read.readDefault("name", obs_type);
|
||||
read(Read,"name", obs_type);
|
||||
std::cout << "Registered types " << std::endl;
|
||||
std::cout << ObsFactory.getBuilderList() << std::endl;
|
||||
|
||||
@ -123,9 +123,12 @@ class HMCResourceManager {
|
||||
Read.push("Actions");
|
||||
|
||||
Read.push("Level");// push must check if the node exist
|
||||
do {
|
||||
fill_ActionsLevel(Read);
|
||||
} while(Read.nextElement("Level"));
|
||||
do
|
||||
{
|
||||
fill_ActionsLevel(Read);
|
||||
}
|
||||
while(Read.push("Level"));
|
||||
|
||||
Read.pop();
|
||||
}
|
||||
|
||||
@ -135,8 +138,10 @@ class HMCResourceManager {
|
||||
void GetActionSet(ActionSet<typename ImplementationPolicy::Field, RepresentationPolicy>& Aset){
|
||||
Aset.resize(multipliers.size());
|
||||
|
||||
for(auto it = ActionsList.begin(); it != ActionsList.end(); it++)
|
||||
for(auto it = ActionsList.begin(); it != ActionsList.end(); it++){
|
||||
(*it).second->acquireResource(Grids["gauge"]);
|
||||
Aset[(*it).first-1].push_back((*it).second->getPtr());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -262,15 +267,11 @@ private:
|
||||
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();
|
||||
|
||||
ActionsList.find(m)->second->print_parameters();
|
||||
}
|
||||
|
||||
|
||||
|
@ -34,7 +34,7 @@ directory
|
||||
#include <Grid/qcd/hmc/checkpointers/NerscCheckpointer.h>
|
||||
#include <Grid/qcd/hmc/checkpointers/BinaryCheckpointer.h>
|
||||
#include <Grid/qcd/hmc/checkpointers/ILDGCheckpointer.h>
|
||||
#include <Grid/qcd/hmc/checkpointers/CheckPointerModules.h>
|
||||
//#include <Grid/qcd/hmc/checkpointers/CheckPointerModules.h>
|
||||
|
||||
|
||||
#endif // CHECKPOINTERS_H
|
||||
|
@ -37,16 +37,26 @@ for the HMC execution
|
||||
namespace Grid {
|
||||
|
||||
//////////////////////////////////////////////
|
||||
// Actions
|
||||
// Actions
|
||||
//////////////////////////////////////////////
|
||||
|
||||
template <class Product, class Resource>
|
||||
class ActionModuleBase: public HMCModuleBase<Product>{
|
||||
public:
|
||||
typedef Resource Res;
|
||||
virtual void acquireResource(Resource& ){};
|
||||
|
||||
};
|
||||
|
||||
|
||||
template <class ActionType, class APar>
|
||||
class ActionModule
|
||||
: public Parametrized<APar>,
|
||||
public HMCModuleBase<QCD::Action<typename ActionType::GaugeField> > {
|
||||
public ActionModuleBase< QCD::Action<typename ActionType::GaugeField> , QCD::GridModule > {
|
||||
public:
|
||||
typedef HMCModuleBase< QCD::Action<typename ActionType::GaugeField> > Base;
|
||||
typedef ActionModuleBase< QCD::Action<typename ActionType::GaugeField>, QCD::GridModule > Base;
|
||||
typedef typename Base::Product Product;
|
||||
typedef APar Parameters;
|
||||
|
||||
std::unique_ptr<ActionType> ActionPtr;
|
||||
|
||||
@ -55,8 +65,9 @@ class ActionModule
|
||||
template <class ReaderClass>
|
||||
ActionModule(Reader<ReaderClass>& Reader) : Parametrized<APar>(Reader){};
|
||||
|
||||
|
||||
virtual void print_parameters(){
|
||||
std::cout << this->Par_ << std::endl;
|
||||
Parametrized<APar>::print_parameters();
|
||||
}
|
||||
|
||||
Product* getPtr() {
|
||||
@ -67,12 +78,12 @@ class ActionModule
|
||||
|
||||
private:
|
||||
virtual void initialize() = 0;
|
||||
|
||||
};
|
||||
|
||||
|
||||
////////////////
|
||||
//////////////////////////
|
||||
// Modules
|
||||
////////////////
|
||||
//////////////////////////
|
||||
|
||||
namespace QCD{
|
||||
|
||||
@ -177,6 +188,63 @@ class DBW2GModule: public ActionModule<DBW2GaugeAction<Impl>, BetaGaugeActionPar
|
||||
|
||||
};
|
||||
|
||||
/////////////////////////////////////////
|
||||
// Fermion Actions
|
||||
/////////////////////////////////////////
|
||||
|
||||
|
||||
template <class Impl >
|
||||
class TwoFlavourFModule: public ActionModule<TwoFlavourPseudoFermionAction<Impl>, NoParameters> {
|
||||
typedef ActionModule<TwoFlavourPseudoFermionAction<Impl>, NoParameters> ActionBase;
|
||||
using ActionBase::ActionBase; // for constructors
|
||||
|
||||
std::unique_ptr<HMCModuleBase<OperatorFunction<typename Impl::FermionField> > >solver_mod;
|
||||
std::unique_ptr<FermionOperatorModuleBase<FermionOperator<Impl>> > fop_mod;
|
||||
public:
|
||||
|
||||
virtual void acquireResource(typename ActionBase::Res& GridMod){
|
||||
fop_mod->AddGridPair(GridMod);
|
||||
}
|
||||
|
||||
template <class ReaderClass>
|
||||
TwoFlavourFModule(Reader<ReaderClass>& Reader) : ActionBase(Reader){
|
||||
std::cout << "Constructing TwoFlavourFModule" << std::endl;
|
||||
auto& SolverFactory = HMC_SolverModuleFactory<solver_string, typename Impl::FermionField, ReaderClass>::getInstance();
|
||||
Reader.push("Solver");
|
||||
std::string solv_name;
|
||||
read(Reader,"name", solv_name);
|
||||
solver_mod = SolverFactory.create(solv_name, Reader);
|
||||
std::cout << "Registered types " << std::endl;
|
||||
std::cout << SolverFactory.getBuilderList() << std::endl;
|
||||
solver_mod->print_parameters();
|
||||
Reader.pop();
|
||||
|
||||
|
||||
|
||||
auto &FOFactory = HMC_FermionOperatorModuleFactory<fermionop_string, Impl, ReaderClass>::getInstance();
|
||||
Reader.push("Operator");
|
||||
std::string op_name;
|
||||
read(Reader,"name", op_name);
|
||||
fop_mod = FOFactory.create(op_name, Reader);
|
||||
std::cout << "Registered types " << std::endl;
|
||||
std::cout << FOFactory.getBuilderList() << std::endl;
|
||||
|
||||
fop_mod->print_parameters();
|
||||
Reader.pop();
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
private:
|
||||
// acquire resource
|
||||
virtual void initialize() {
|
||||
this->ActionPtr.reset(new TwoFlavourPseudoFermionAction<Impl>(*fop_mod->getPtr(), *solver_mod->getPtr(), *solver_mod->getPtr()));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -206,15 +274,15 @@ typedef PlaqPlusRectangleGModule<PeriodicGimplR> PlaqPlusRectangleGMod;
|
||||
// Factory is perfectly fine
|
||||
// Registar must be changed because I do not want to use the ModuleFactory
|
||||
|
||||
// explicit ref to LatticeGaugeField must be changed of put in the factory
|
||||
typedef HMCModuleBase< QCD::Action< QCD::LatticeGaugeField > > HMC_LGTActionModBase;
|
||||
// explicit ref to LatticeGaugeField must be changed or put in the factory
|
||||
typedef ActionModuleBase< QCD::Action< QCD::LatticeGaugeField >, QCD::GridModule > HMC_LGTActionModBase;
|
||||
|
||||
template <char const *str, class ReaderClass >
|
||||
class HMC_LGTActionModuleFactory
|
||||
: public Factory < HMC_LGTActionModBase , Reader<ReaderClass> > {
|
||||
: public Factory < HMC_LGTActionModBase , Reader<ReaderClass> > {
|
||||
public:
|
||||
typedef Reader<ReaderClass> TheReader;
|
||||
// use SINGLETON FUNCTOR MACRO HERE
|
||||
typedef Reader<ReaderClass> TheReader;
|
||||
// use SINGLETON FUNCTOR MACRO HERE
|
||||
HMC_LGTActionModuleFactory(const HMC_LGTActionModuleFactory& e) = delete;
|
||||
void operator=(const HMC_LGTActionModuleFactory& e) = delete;
|
||||
static HMC_LGTActionModuleFactory& getInstance(void) {
|
||||
@ -225,7 +293,7 @@ class HMC_LGTActionModuleFactory
|
||||
private:
|
||||
HMC_LGTActionModuleFactory(void) = default;
|
||||
std::string obj_type() const {
|
||||
return std::string(str);
|
||||
return std::string(str);
|
||||
}
|
||||
};
|
||||
|
||||
@ -237,10 +305,14 @@ static Registrar<QCD::DBW2GMod, HMC_LGTActionModuleFactory<gauge_st
|
||||
static Registrar<QCD::RBCGMod, HMC_LGTActionModuleFactory<gauge_string, XmlReader> > __RBCGmodXMLInit("RBC");
|
||||
static Registrar<QCD::PlaqPlusRectangleGMod, HMC_LGTActionModuleFactory<gauge_string, XmlReader> > __PPRectGmodXMLInit("PlaqPlusRect");
|
||||
|
||||
|
||||
// FIXME more general implementation
|
||||
static Registrar<QCD::TwoFlavourFModule<QCD::WilsonImplR> , HMC_LGTActionModuleFactory<gauge_string, XmlReader> > __TwoFlavourFmodXMLInit("TwoFlavours");
|
||||
|
||||
// add here the registration for other implementations and readers
|
||||
|
||||
|
||||
}
|
||||
} // Grid
|
||||
|
||||
|
||||
#endif //HMC_MODULES_H
|
@ -1,11 +1,12 @@
|
||||
/*************************************************************************************
|
||||
Grid physics library, www.github.com/paboyle/Grid
|
||||
Source file: extras/Hadrons/Factory.hpp
|
||||
Source file: Factory.h
|
||||
|
||||
Copyright (C) 2015
|
||||
Copyright (C) 2016
|
||||
|
||||
Author: Antonin Portelli <antonin.portelli@me.com>
|
||||
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
|
||||
@ -28,6 +29,9 @@ See the full license in the file "LICENSE" in the top level distribution directo
|
||||
|
||||
namespace Grid{
|
||||
|
||||
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* abstract factory class *
|
||||
******************************************************************************/
|
||||
@ -35,8 +39,8 @@ template <typename T, typename ProductCreator>
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
typedef std::function< std::unique_ptr<T> (const ProductCreator&)> Func;
|
||||
public:
|
||||
typedef std::function< std::unique_ptr<T> (const ProductCreator&) > Func;
|
||||
|
||||
// constructor
|
||||
Factory(void) = default;
|
||||
// destructor
|
||||
@ -47,7 +51,7 @@ public:
|
||||
std::vector<std::string> getBuilderList(void) const;
|
||||
// factory
|
||||
std::unique_ptr<T> create(const std::string type,
|
||||
const ProductCreator& name) const;
|
||||
const ProductCreator& input) const;
|
||||
private:
|
||||
std::map<std::string, Func> builder_;
|
||||
virtual std::string obj_type() const = 0;
|
||||
@ -80,7 +84,7 @@ std::vector<std::string> Factory<T, ProductCreator>::getBuilderList(void) const
|
||||
// factory /////////////////////////////////////////////////////////////////////
|
||||
template <typename T, typename ProductCreator>
|
||||
std::unique_ptr<T> Factory<T, ProductCreator>::create(const std::string type,
|
||||
const ProductCreator& input) const
|
||||
const ProductCreator& input) const
|
||||
{
|
||||
Func func;
|
||||
|
||||
|
146
lib/qcd/modules/FermionOperatorModules.h
Normal file
146
lib/qcd/modules/FermionOperatorModules.h
Normal file
@ -0,0 +1,146 @@
|
||||
/*************************************************************************************
|
||||
|
||||
Grid physics library, www.github.com/paboyle/Grid
|
||||
|
||||
Source file: ./lib/qcd/modules/FermionOperatorModules.h
|
||||
|
||||
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 FERMIONOPERATOR_MODULES_H
|
||||
#define FERMIONOPERATOR_MODULES_H
|
||||
|
||||
namespace Grid {
|
||||
|
||||
////////////////////////////////////
|
||||
// Fermion operators
|
||||
/////////////////////////////////////
|
||||
template < class Product>
|
||||
class FermionOperatorModuleBase : public HMCModuleBase<Product>{
|
||||
public:
|
||||
virtual void AddGridPair(QCD::GridModule&) = 0;
|
||||
};
|
||||
|
||||
template <template <typename> class FOType, class FermionImpl, class FOPar>
|
||||
class FermionOperatorModule
|
||||
: public Parametrized<FOPar>,
|
||||
public FermionOperatorModuleBase<QCD::FermionOperator<FermionImpl> > {
|
||||
|
||||
protected:
|
||||
std::unique_ptr< FOType<FermionImpl> > FOPtr;
|
||||
std::vector< std::reference_wrapper<QCD::GridModule> > GridRefs;
|
||||
public:
|
||||
typedef HMCModuleBase< QCD::FermionOperator<FermionImpl> > Base;
|
||||
typedef typename Base::Product Product;
|
||||
|
||||
FermionOperatorModule(FOPar Par) : Parametrized<FOPar>(Par) {}
|
||||
|
||||
template <class ReaderClass>
|
||||
FermionOperatorModule(Reader<ReaderClass>& Reader) : Parametrized<FOPar>(Reader){};
|
||||
|
||||
void AddGridPair(QCD::GridModule &Mod){
|
||||
if (GridRefs.size()>2){
|
||||
std::cout << GridLogError << "Adding too many Grids to the FermionOperatorModule" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
GridRefs.push_back(Mod);
|
||||
}
|
||||
|
||||
virtual void print_parameters(){
|
||||
std::cout << this->Par_ << std::endl;
|
||||
}
|
||||
|
||||
Product* getPtr() {
|
||||
if (!FOPtr) initialize();
|
||||
|
||||
return FOPtr.get();
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void initialize() = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
// Factory
|
||||
template <char const *str, class FermionImpl, class ReaderClass >
|
||||
class HMC_FermionOperatorModuleFactory
|
||||
: public Factory < FermionOperatorModuleBase<QCD::FermionOperator<FermionImpl> > , Reader<ReaderClass> > {
|
||||
public:
|
||||
// use SINGLETON FUNCTOR MACRO HERE
|
||||
typedef Reader<ReaderClass> TheReader;
|
||||
|
||||
HMC_FermionOperatorModuleFactory(const HMC_FermionOperatorModuleFactory& e) = delete;
|
||||
void operator=(const HMC_FermionOperatorModuleFactory& e) = delete;
|
||||
static HMC_FermionOperatorModuleFactory& getInstance(void) {
|
||||
static HMC_FermionOperatorModuleFactory e;
|
||||
return e;
|
||||
}
|
||||
|
||||
private:
|
||||
HMC_FermionOperatorModuleFactory(void) = default;
|
||||
std::string obj_type() const {
|
||||
return std::string(str);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
extern char fermionop_string[];
|
||||
namespace QCD{
|
||||
|
||||
// Modules
|
||||
class WilsonFermionParameters : Serializable {
|
||||
public:
|
||||
GRID_SERIALIZABLE_CLASS_MEMBERS(WilsonFermionParameters,
|
||||
RealD, mass);
|
||||
};
|
||||
|
||||
|
||||
template <class FermionImpl >
|
||||
class WilsonFermionModule: public FermionOperatorModule<WilsonFermion, FermionImpl, WilsonFermionParameters> {
|
||||
typedef FermionOperatorModule<WilsonFermion, FermionImpl, WilsonFermionParameters> FermBase;
|
||||
using FermBase::FermBase; // for constructors
|
||||
|
||||
// acquire resource
|
||||
virtual void initialize(){
|
||||
typename FermionImpl::GaugeField U(this->GridRefs[0].get().get_full());
|
||||
this->FOPtr.reset(new WilsonFermion<FermionImpl>(U, *(this->GridRefs[0].get().get_full()), *(this->GridRefs[0].get().get_rb()), this->Par_.mass));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Now a specific registration with a fermion field
|
||||
static Registrar< WilsonFermionModule<WilsonImplR>,
|
||||
HMC_FermionOperatorModuleFactory<fermionop_string, WilsonImplR, XmlReader> > __WilsonFOPmodXMLInit("Wilson");
|
||||
|
||||
|
||||
} // QCD
|
||||
|
||||
|
||||
|
||||
|
||||
} // Grid
|
||||
|
||||
|
||||
#endif //SOLVER_MODULES_H
|
@ -29,13 +29,11 @@ directory
|
||||
|
||||
namespace Grid{
|
||||
|
||||
char gauge_string[] = "gauge";
|
||||
char cp_string[] = "CheckPointer";
|
||||
char hmc_string[] = "HMC";
|
||||
char gauge_string[] = "gauge";
|
||||
char cp_string[] = "CheckPointer";
|
||||
char hmc_string[] = "HMC";
|
||||
char observable_string[] = "Observable";
|
||||
|
||||
|
||||
|
||||
|
||||
char solver_string[] = "Solver";
|
||||
char fermionop_string[] = "FermionOperator";
|
||||
|
||||
}
|
||||
|
@ -43,6 +43,16 @@ class NoParameters{};
|
||||
/*
|
||||
Base class for modules with parameters
|
||||
*/
|
||||
|
||||
|
||||
|
||||
class ObjectInfo: Serializable {
|
||||
public:
|
||||
GRID_SERIALIZABLE_CLASS_MEMBERS(ObjectInfo,
|
||||
std::string, name);
|
||||
};
|
||||
|
||||
|
||||
template < class P >
|
||||
class Parametrized{
|
||||
public:
|
||||
@ -51,29 +61,28 @@ public:
|
||||
Parametrized(Parameters Par):Par_(Par){};
|
||||
|
||||
template <class ReaderClass>
|
||||
Parametrized(Reader<ReaderClass> & Reader, std::string section_name = "parameters"){
|
||||
read(Reader, section_name, Par_);
|
||||
Parametrized(Reader<ReaderClass> & R, std::string section_name = "parameters"){
|
||||
read(R, section_name, Par_);
|
||||
}
|
||||
|
||||
void set_parameters(Parameters Par){
|
||||
Par_ = Par;
|
||||
Par_ = Par;
|
||||
}
|
||||
|
||||
|
||||
void print_parameters(){
|
||||
std::cout << Par_ << std::endl;
|
||||
std::cout << Par_ << std::endl;
|
||||
}
|
||||
|
||||
protected:
|
||||
Parameters Par_;
|
||||
|
||||
private:
|
||||
std::string section_name;
|
||||
std::string section_name;
|
||||
};
|
||||
|
||||
|
||||
template <>
|
||||
class Parametrized<NoParameters>{
|
||||
public:
|
||||
typedef NoParameters Parameters;
|
||||
|
||||
Parametrized(Parameters Par){};
|
||||
@ -92,6 +101,9 @@ class Parametrized<NoParameters>{
|
||||
/*
|
||||
Lowest level abstract module class
|
||||
*/
|
||||
|
||||
|
||||
|
||||
template <class Prod>
|
||||
class HMCModuleBase {
|
||||
public:
|
||||
@ -99,6 +111,8 @@ class HMCModuleBase {
|
||||
|
||||
virtual Prod* getPtr() = 0;
|
||||
|
||||
// add a getReference?
|
||||
|
||||
virtual void print_parameters(){}; // default to nothing
|
||||
};
|
||||
|
||||
@ -205,8 +219,12 @@ class Registrar {
|
||||
public:
|
||||
Registrar(std::string className) {
|
||||
// register the class factory function
|
||||
TheFactory::getInstance().registerBuilder(className, [&](typename TheFactory::TheReader Reader)
|
||||
{ return std::unique_ptr<T>(new T(Reader));});
|
||||
TheFactory::getInstance().registerBuilder(className,
|
||||
[&](typename TheFactory::TheReader Reader)
|
||||
{
|
||||
return std::unique_ptr<T>(new T(Reader));
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
152
lib/qcd/modules/SolverModules.h
Normal file
152
lib/qcd/modules/SolverModules.h
Normal file
@ -0,0 +1,152 @@
|
||||
/*************************************************************************************
|
||||
|
||||
Grid physics library, www.github.com/paboyle/Grid
|
||||
|
||||
Source file: ./lib/qcd/modules/SolverModules.h
|
||||
|
||||
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 SOLVER_MODULES_H
|
||||
#define SOLVER_MODULES_H
|
||||
|
||||
namespace Grid {
|
||||
|
||||
//////////////////////////////////////////////
|
||||
// Operator Functions (Solvers)
|
||||
//////////////////////////////////////////////
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <template <typename> class SolverType, class Field, class SPar>
|
||||
class SolverModule
|
||||
: public Parametrized<SPar>,
|
||||
public HMCModuleBase<OperatorFunction<Field> > {
|
||||
public:
|
||||
typedef HMCModuleBase< OperatorFunction<Field> > Base;
|
||||
typedef typename Base::Product Product;
|
||||
|
||||
std::unique_ptr< SolverType<Field> > SolverPtr;
|
||||
|
||||
SolverModule(SPar Par) : Parametrized<SPar>(Par) {}
|
||||
|
||||
template <class ReaderClass>
|
||||
SolverModule(Reader<ReaderClass>& Reader) : Parametrized<SPar>(Reader){};
|
||||
|
||||
virtual void print_parameters(){
|
||||
std::cout << this->Par_ << std::endl;
|
||||
}
|
||||
|
||||
Product* getPtr() {
|
||||
if (!SolverPtr) initialize();
|
||||
|
||||
return SolverPtr.get();
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void initialize() = 0;
|
||||
};
|
||||
|
||||
|
||||
// Factory
|
||||
template <char const *str, class Field, class ReaderClass >
|
||||
class HMC_SolverModuleFactory
|
||||
: public Factory < HMCModuleBase<OperatorFunction<Field> > , Reader<ReaderClass> > {
|
||||
public:
|
||||
// use SINGLETON FUNCTOR MACRO HERE
|
||||
typedef Reader<ReaderClass> TheReader;
|
||||
|
||||
HMC_SolverModuleFactory(const HMC_SolverModuleFactory& e) = delete;
|
||||
void operator=(const HMC_SolverModuleFactory& e) = delete;
|
||||
static HMC_SolverModuleFactory& getInstance(void) {
|
||||
static HMC_SolverModuleFactory e;
|
||||
return e;
|
||||
}
|
||||
|
||||
private:
|
||||
HMC_SolverModuleFactory(void) = default;
|
||||
std::string obj_type() const {
|
||||
return std::string(str);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
class SolverParameters : Serializable {
|
||||
public:
|
||||
GRID_SERIALIZABLE_CLASS_MEMBERS(SolverParameters,
|
||||
RealD, tolerance,
|
||||
RealD, max_iterations);
|
||||
// add error on no convergence?
|
||||
};
|
||||
|
||||
|
||||
class SolverObjName: Serializable {
|
||||
public:
|
||||
GRID_SERIALIZABLE_CLASS_MEMBERS(SolverObjName,
|
||||
std::string, name,
|
||||
SolverParameters, parameters);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <class Field >
|
||||
class ConjugateGradientModule: public SolverModule<ConjugateGradient, Field, SolverParameters> {
|
||||
typedef SolverModule<ConjugateGradient, Field, SolverParameters> SolverBase;
|
||||
using SolverBase::SolverBase; // for constructors
|
||||
|
||||
// acquire resource
|
||||
virtual void initialize(){
|
||||
this->SolverPtr.reset(new ConjugateGradient<Field>(this->Par_.tolerance, this->Par_.max_iterations, true));
|
||||
}
|
||||
};
|
||||
|
||||
template <class Field >
|
||||
class ConjugateResidualModule: public SolverModule<ConjugateResidual, Field, SolverParameters> {
|
||||
typedef SolverModule<ConjugateResidual, Field, SolverParameters> SolverBase;
|
||||
using SolverBase::SolverBase; // for constructors
|
||||
|
||||
// acquire resource
|
||||
virtual void initialize(){
|
||||
this->SolverPtr.reset(new ConjugateResidual<Field>(this->Par_.tolerance, this->Par_.max_iterations));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
extern char solver_string[];
|
||||
// Now a specific registration with a fermion field
|
||||
static Registrar< ConjugateGradientModule<QCD::WilsonFermionR::FermionField>,
|
||||
HMC_SolverModuleFactory<solver_string, QCD::WilsonFermionR::FermionField, XmlReader> > __CGWFmodXMLInit("ConjugateGradientWF");
|
||||
static Registrar< ConjugateResidualModule<QCD::WilsonFermionR::FermionField>,
|
||||
HMC_SolverModuleFactory<solver_string, QCD::WilsonFermionR::FermionField, XmlReader> > __CRWFmodXMLInit("ConjugateResidualWF");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // Grid
|
||||
|
||||
|
||||
#endif //SOLVER_MODULES_H
|
@ -34,7 +34,12 @@ directory
|
||||
#include <Grid/qcd/modules/Factory.h>
|
||||
#include <Grid/qcd/modules/Modules.h>
|
||||
|
||||
|
||||
#include <Grid/qcd/hmc/checkpointers/CheckPointerModules.h>
|
||||
#include <Grid/qcd/modules/SolverModules.h>
|
||||
#include <Grid/qcd/modules/FermionOperatorModules.h>
|
||||
#include <Grid/qcd/modules/ActionModules.h>
|
||||
|
||||
|
||||
|
||||
#endif //MODS_H
|
||||
|
@ -105,7 +105,7 @@ namespace Grid {
|
||||
public:
|
||||
Reader(void);
|
||||
virtual ~Reader(void) = default;
|
||||
void push(const std::string &s);
|
||||
bool push(const std::string &s);
|
||||
void pop(void);
|
||||
template <typename U>
|
||||
typename std::enable_if<std::is_base_of<Serializable, U>::value, void>::type
|
||||
@ -160,15 +160,15 @@ namespace Grid {
|
||||
|
||||
// Generic reader interface
|
||||
template <typename T>
|
||||
inline void push(Reader<T> &r, const std::string &s)
|
||||
inline bool push(Reader<T> &r, const std::string &s)
|
||||
{
|
||||
r.push(s);
|
||||
return r.push(s);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void push(Reader<T> &r, const char *s)
|
||||
inline bool push(Reader<T> &r, const char *s)
|
||||
{
|
||||
r.push(std::string(s));
|
||||
return r.push(std::string(s));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@ -236,9 +236,9 @@ namespace Grid {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Reader<T>::push(const std::string &s)
|
||||
bool Reader<T>::push(const std::string &s)
|
||||
{
|
||||
upcast->push(s);
|
||||
return upcast->push(s);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
@ -60,7 +60,7 @@ namespace Grid {
|
||||
public:
|
||||
BinaryReader(const std::string &fileName);
|
||||
virtual ~BinaryReader(void) = default;
|
||||
void push(const std::string &s) {};
|
||||
bool push(const std::string &s) {return true;}
|
||||
void pop(void) {};
|
||||
template <typename U>
|
||||
void readDefault(const std::string &s, U &output);
|
||||
|
@ -68,9 +68,10 @@ TextReader::TextReader(const string &fileName)
|
||||
}
|
||||
}
|
||||
|
||||
void TextReader::push(const string &s)
|
||||
bool TextReader::push(const string &s)
|
||||
{
|
||||
level_++;
|
||||
return true;
|
||||
};
|
||||
|
||||
void TextReader::pop(void)
|
||||
|
@ -63,7 +63,7 @@ namespace Grid
|
||||
public:
|
||||
TextReader(const std::string &fileName);
|
||||
virtual ~TextReader(void) = default;
|
||||
void push(const std::string &s);
|
||||
bool push(const std::string &s);
|
||||
void pop(void);
|
||||
template <typename U>
|
||||
void readDefault(const std::string &s, U &output);
|
||||
|
@ -70,10 +70,13 @@ XmlReader::XmlReader(const string &fileName)
|
||||
node_ = doc_.child("grid");
|
||||
}
|
||||
|
||||
void XmlReader::push(const string &s)
|
||||
bool XmlReader::push(const string &s)
|
||||
{
|
||||
node_ = node_.child(s.c_str());
|
||||
// add error check
|
||||
if (node_ == NULL)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void XmlReader::pop(void)
|
||||
|
@ -66,7 +66,7 @@ namespace Grid
|
||||
public:
|
||||
XmlReader(const std::string &fileName);
|
||||
virtual ~XmlReader(void) = default;
|
||||
void push(const std::string &s);
|
||||
bool push(const std::string &s);
|
||||
void pop(void);
|
||||
bool nextElement(const std::string &s);
|
||||
template <typename U>
|
||||
|
@ -29,9 +29,14 @@ directory
|
||||
#include <Grid/Grid.h>
|
||||
|
||||
namespace Grid{
|
||||
// ifdefs ?? Local makefile suggestion , policy as make parameter
|
||||
|
||||
// Put this section in a separate header
|
||||
|
||||
// ifdefs ?? Local makefile suggestion , policy as make parameter
|
||||
typedef QCD::PeriodicGimplR ImplementationPolicy;
|
||||
typedef QCD::NoHirep RepresentationPolicy;
|
||||
typedef QCD::WilsonFermionR FermionImplementation;
|
||||
|
||||
|
||||
static Registrar< HMCLeapFrog<ImplementationPolicy, RepresentationPolicy, XmlReader> , HMCRunnerModuleFactory<hmc_string, XmlReader> > __HMCLFmodXMLInit("LeapFrog");
|
||||
static Registrar< HMCMinimumNorm2<ImplementationPolicy, RepresentationPolicy, XmlReader> , HMCRunnerModuleFactory<hmc_string, XmlReader> > __HMCMN2modXMLInit("MinimumNorm2");
|
||||
@ -53,6 +58,8 @@ int main(int argc, char **argv) {
|
||||
// Reader, file should come from command line
|
||||
InputFileReader Reader("input.wilson_gauge.params.xml");
|
||||
|
||||
|
||||
|
||||
// Test HMC factory (put in an external file)
|
||||
auto &HMCfactory = HMCRunnerModuleFactory<hmc_string, InputFileReader >::getInstance();
|
||||
// Simplify this step (IntergratorName field?)
|
||||
@ -61,6 +68,38 @@ int main(int argc, char **argv) {
|
||||
// Construct the module
|
||||
auto myHMCmodule = HMCfactory.create(HMCpar.MD.name, Reader);
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
// Test solver creation
|
||||
auto &SolverFactory = HMC_SolverModuleFactory<solver_string, FermionImplementation::FermionField, XmlReader>::getInstance();
|
||||
Reader.push("Solver");
|
||||
std::string name;
|
||||
read(Reader, "name",name);
|
||||
auto SModule = SolverFactory.create(name, Reader);
|
||||
std::cout << "Registered types " << std::endl;
|
||||
std::cout << SolverFactory.getBuilderList() << std::endl;
|
||||
SModule->print_parameters();
|
||||
Reader.pop();
|
||||
*/
|
||||
|
||||
/*
|
||||
// Test fermion operator module creation
|
||||
auto &FOFactory = HMC_FermionOperatorModuleFactory<fermionop_string, WilsonImplR, XmlReader>::getInstance();
|
||||
Reader.push("Operator");
|
||||
std::string op_name;
|
||||
Reader.readDefault("name",op_name);
|
||||
auto FOModule = FOFactory.create(op_name, Reader);
|
||||
std::cout << "Registered types " << std::endl;
|
||||
std::cout << FOFactory.getBuilderList() << std::endl;
|
||||
GridFourDimModule GMod;
|
||||
FOModule->AddGridPair(GMod);
|
||||
FOModule->print_parameters();
|
||||
Reader.pop();
|
||||
*/
|
||||
|
||||
|
||||
myHMCmodule->getPtr()->initialize(Reader);
|
||||
myHMCmodule->getPtr()->Run();
|
||||
|
||||
|
@ -85,7 +85,7 @@ int main(int argc, char **argv) {
|
||||
RealD beta = 5.6 ;
|
||||
WilsonGaugeActionR Waction(beta);
|
||||
|
||||
// temporarily need a gauge field
|
||||
// temporarily need a gauge field
|
||||
auto GridPtr = TheHMC.Resources.GetCartesian();
|
||||
auto GridRBPtr = TheHMC.Resources.GetRBCartesian();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user