From f60fbcfc4d4bef2a8bc0e2bfacdc8d0bffeff650 Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Fri, 14 Sep 2018 12:47:55 +0100 Subject: [PATCH] Hadrons: mixed precision CG, to be tested --- Hadrons/Modules.hpp | 1 + .../Modules/MSolver/MixedPrecisionRBPrecCG.cc | 8 + .../MSolver/MixedPrecisionRBPrecCG.hpp | 170 ++++++++++++++++++ Hadrons/modules.inc | 2 + 4 files changed, 181 insertions(+) create mode 100644 Hadrons/Modules/MSolver/MixedPrecisionRBPrecCG.cc create mode 100644 Hadrons/Modules/MSolver/MixedPrecisionRBPrecCG.hpp diff --git a/Hadrons/Modules.hpp b/Hadrons/Modules.hpp index 61baa799..bb3a9b87 100644 --- a/Hadrons/Modules.hpp +++ b/Hadrons/Modules.hpp @@ -45,6 +45,7 @@ See the full license in the file "LICENSE" in the top level distribution directo #include #include #include +#include #include #include #include diff --git a/Hadrons/Modules/MSolver/MixedPrecisionRBPrecCG.cc b/Hadrons/Modules/MSolver/MixedPrecisionRBPrecCG.cc new file mode 100644 index 00000000..6e9087a1 --- /dev/null +++ b/Hadrons/Modules/MSolver/MixedPrecisionRBPrecCG.cc @@ -0,0 +1,8 @@ +#include + +using namespace Grid; +using namespace Hadrons; +using namespace MSolver; + +template class Grid::Hadrons::MSolver::TMixedPrecisionRBPrecCG; +template class Grid::Hadrons::MSolver::TMixedPrecisionRBPrecCG; diff --git a/Hadrons/Modules/MSolver/MixedPrecisionRBPrecCG.hpp b/Hadrons/Modules/MSolver/MixedPrecisionRBPrecCG.hpp new file mode 100644 index 00000000..3898b8ec --- /dev/null +++ b/Hadrons/Modules/MSolver/MixedPrecisionRBPrecCG.hpp @@ -0,0 +1,170 @@ +#ifndef Hadrons_MSolver_MixedPrecisionRBPrecCG_hpp_ +#define Hadrons_MSolver_MixedPrecisionRBPrecCG_hpp_ + +#include +#include +#include +#include +#include +#include + +BEGIN_HADRONS_NAMESPACE + +/****************************************************************************** + * Mixed precision schur red-black preconditioned CG * + ******************************************************************************/ +BEGIN_MODULE_NAMESPACE(MSolver) + +class MixedPrecisionRBPrecCGPar: Serializable +{ +public: + GRID_SERIALIZABLE_CLASS_MEMBERS(MixedPrecisionRBPrecCGPar, + std::string , innerAction, + std::string , outerAction, + unsigned int, maxInnerIteration, + unsigned int, maxOuterIteration, + double , residual, + std::string , eigenPack); +}; + +template +class TMixedPrecisionRBPrecCG: public Module +{ +public: + FG_TYPE_ALIASES(FImplInner, Inner); + FG_TYPE_ALIASES(FImplOuter, Outer); + SOLVER_TYPE_ALIASES(FImplOuter,); + typedef HADRONS_DEFAULT_SCHUR_OP SchurFMatInner; + typedef HADRONS_DEFAULT_SCHUR_OP SchurFMatOuter; +private: + template + class OperatorFunctionWrapper: public OperatorFunction + { + public: + OperatorFunctionWrapper(LinearFunction &fn): fn_(fn) {}; + virtual ~OperatorFunctionWrapper(void) = default; + virtual void operator()(LinearOperatorBase &op, + const Field &in, Field &out) + { + fn_(in, out); + } + private: + LinearFunction &fn_; + }; +public: + // constructor + TMixedPrecisionRBPrecCG(const std::string name); + // destructor + virtual ~TMixedPrecisionRBPrecCG(void) {}; + // dependency relation + virtual std::vector getInput(void); + virtual std::vector getReference(void); + virtual std::vector getOutput(void); + // setup + virtual void setup(void); + // execution + virtual void execute(void); +}; + +MODULE_REGISTER_TMP(MixedPrecisionRBPrecCG, + ARG(TMixedPrecisionRBPrecCG), MSolver); +MODULE_REGISTER_TMP(ZMixedPrecisionRBPrecCG, + ARG(TMixedPrecisionRBPrecCG), MSolver); + +/****************************************************************************** + * TMixedPrecisionRBPrecCG implementation * + ******************************************************************************/ +// constructor ///////////////////////////////////////////////////////////////// +template +TMixedPrecisionRBPrecCG +::TMixedPrecisionRBPrecCG(const std::string name) +: Module(name) +{} + +// dependencies/products /////////////////////////////////////////////////////// +template +std::vector TMixedPrecisionRBPrecCG +::getInput(void) +{ + std::vector in; + + return in; +} + +template +std::vector TMixedPrecisionRBPrecCG +::getReference(void) +{ + std::vector ref = {par().innerAction, par().outerAction}; + + if (!par().eigenPack.empty()) + { + ref.push_back(par().eigenPack); + } + + return ref; +} + +template +std::vector TMixedPrecisionRBPrecCG +::getOutput(void) +{ + std::vector out = {getName(), getName() + "_subtract"}; + + return out; +} + +// setup /////////////////////////////////////////////////////////////////////// +template +void TMixedPrecisionRBPrecCG +::setup(void) +{ + LOG(Message) << "Setting up Schur red-black preconditioned mixed-precision " + << "CG for inner/outer action '" << par().innerAction + << "'/'" << par().outerAction << "', residual " + << par().residual << ", and maximum inner/outer iteration " + << par().maxInnerIteration << "/" << par().maxOuterIteration + << std::endl; + + auto Ls = env().getObjectLs(par().innerAction); + auto &imat = envGet(FMatInner, par().innerAction); + auto &omat = envGet(FMatOuter, par().outerAction); + auto guesserPt = makeGuesser(par().eigenPack); + + auto makeSolver = [&imat, &omat, guesserPt, Ls, this](bool subGuess) + { + return [&imat, &omat, guesserPt, subGuess, Ls, this] + (FermionFieldOuter &sol, const FermionFieldOuter &source) + { + typedef typename FermionFieldInner::vector_type VTypeInner; + + SchurFMatInner simat(imat); + SchurFMatOuter somat(omat); + MixedPrecisionConjugateGradient + mpcg(par().residual, par().maxInnerIteration, + par().maxOuterIteration, + env().template getGrid(Ls), + simat, somat); + OperatorFunctionWrapper wmpcg(mpcg); + HADRONS_DEFAULT_SCHUR_SOLVE schurSolver(wmpcg); + schurSolver.subtractGuess(subGuess); + schurSolver(omat, source, sol, *guesserPt); + }; + }; + auto solver = makeSolver(false); + envCreate(Solver, getName(), Ls, solver, omat); + auto solver_subtract = makeSolver(true); + envCreate(Solver, getName() + "_subtract", Ls, solver_subtract, omat); +} + +// execution /////////////////////////////////////////////////////////////////// +template +void TMixedPrecisionRBPrecCG +::execute(void) +{} + +END_MODULE_NAMESPACE + +END_HADRONS_NAMESPACE + +#endif // Hadrons_MSolver_MixedPrecisionRBPrecCG_hpp_ diff --git a/Hadrons/modules.inc b/Hadrons/modules.inc index afcbad9b..7a7645f5 100644 --- a/Hadrons/modules.inc +++ b/Hadrons/modules.inc @@ -19,6 +19,7 @@ modules_cc =\ Modules/MSink/Smear.cc \ Modules/MSolver/A2AVectors.cc \ Modules/MSolver/RBPrecCG.cc \ + Modules/MSolver/MixedPrecisionRBPrecCG.cc \ Modules/MSolver/LocalCoherenceLanczos.cc \ Modules/MGauge/StoutSmearing.cc \ Modules/MGauge/Unit.cc \ @@ -80,6 +81,7 @@ modules_hpp =\ Modules/MSource/SeqConserved.hpp \ Modules/MSink/Smear.hpp \ Modules/MSink/Point.hpp \ + Modules/MSolver/MixedPrecisionRBPrecCG.hpp \ Modules/MSolver/LocalCoherenceLanczos.hpp \ Modules/MSolver/Guesser.hpp \ Modules/MSolver/RBPrecCG.hpp \