/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: Hadrons/Modules/MSolver/MixedPrecisionRBPrecCG.hpp Copyright (C) 2015-2019 Author: Antonin Portelli 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 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: FERM_TYPE_ALIASES(FImplInner, Inner); FERM_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: using OperatorFunction::operator(); 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 getRbGrid(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_