/******************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: programs/Hadrons/Environment.hpp Copyright (C) 2015 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. *******************************************************************************/ #ifndef Hadrons_Environment_hpp_ #define Hadrons_Environment_hpp_ #include #include BEGIN_HADRONS_NAMESPACE /****************************************************************************** * Global environment * ******************************************************************************/ // forward declaration of Module class ModuleBase; class Object { public: Object(void) = default; virtual ~Object(void) = default; }; template class Holder: public Object { public: Holder(void) = default; Holder(T *pt); virtual ~Holder(void) = default; T & get(void) const; T * getPt(void) const; void reset(T *pt); private: std::unique_ptr objPt_{nullptr}; }; class Environment { SINGLETON(Environment); public: typedef std::unique_ptr ModPt; typedef std::unique_ptr GridPt; typedef std::unique_ptr GridRbPt; typedef FermionOperator FMat; typedef std::unique_ptr FMatPt; typedef std::function Solver; typedef std::unique_ptr RngPt; typedef std::unique_ptr LatticePt; private: struct ModuleInfo { const std::type_info *type{nullptr}; std::string name; std::unique_ptr data{nullptr}; std::vector input; }; struct ObjInfo { unsigned int size{0}, Ls{0}; bool isRegistered{false}; const std::type_info *type{nullptr}; std::string name; int module{-1}; std::set owners, properties; std::unique_ptr data{nullptr}; }; public: // dry run void dryRun(const bool isDry); bool isDryRun(void) const; // trajectory number void setTrajectory(const unsigned int traj); unsigned int getTrajectory(void) const; // grids void createGrid(const unsigned int Ls); GridCartesian * getGrid(const unsigned int Ls = 1) const; GridRedBlackCartesian * getRbGrid(const unsigned int Ls = 1) const; // random number generator void setSeed(const std::vector &seed); GridParallelRNG * get4dRng(void) const; // module management void createModule(const std::string name, const std::string type, XmlReader &reader); ModuleBase * getModule(const unsigned int address) const; ModuleBase * getModule(const std::string name) const; template M * getModule(const unsigned int address) const; template M * getModule(const std::string name) const; unsigned int getModuleAddress(const std::string name) const; std::string getModuleName(const unsigned int address) const; std::string getModuleType(const unsigned int address) const; std::string getModuleType(const std::string name) const; bool hasModule(const unsigned int address) const; bool hasModule(const std::string name) const; Graph makeModuleGraph(void) const; unsigned int executeProgram(const std::vector &p); unsigned int executeProgram(const std::vector &p); // general memory management void addObject(const std::string name, const int moduleAddress); void registerObject(const unsigned int address, const unsigned int size, const unsigned int Ls = 1); void registerObject(const std::string name, const unsigned int size, const unsigned int Ls = 1); template unsigned int lattice4dSize(void) const; template void registerLattice(const unsigned int address, const unsigned int Ls = 1); template void registerLattice(const std::string name, const unsigned int Ls = 1); template void setObject(const unsigned int address, T *object); template void setObject(const std::string name, T *object); template T * getObject(const unsigned int address) const; template T * getObject(const std::string name) const; template T * createLattice(const unsigned int address); template T * createLattice(const std::string name); unsigned int getObjectAddress(const std::string name) const; std::string getObjectName(const unsigned int address) const; std::string getObjectType(const unsigned int address) const; std::string getObjectType(const std::string name) const; unsigned int getObjectSize(const unsigned int address) const; unsigned int getObjectSize(const std::string name) const; unsigned int getObjectLs(const unsigned int address) const; unsigned int getObjectLs(const std::string name) const; bool hasObject(const unsigned int address) const; bool hasObject(const std::string name) const; bool hasRegisteredObject(const unsigned int address) const; bool hasRegisteredObject(const std::string name) const; bool isObject5d(const unsigned int address) const; bool isObject5d(const std::string name) const; long unsigned int getTotalSize(void) const; void addOwnership(const unsigned int owner, const unsigned int property); void addOwnership(const std::string owner, const std::string property); bool hasOwners(const unsigned int address) const; bool hasOwners(const std::string name) const; bool freeObject(const unsigned int address); bool freeObject(const std::string name); void freeAll(void); void printContent(void); private: // general bool dryRun_{false}; unsigned int traj_, locVol_; // grids GridPt grid4d_; std::map grid5d_; GridRbPt gridRb4d_; std::map gridRb5d_; // random number generator RngPt rng4d_; // module and related maps std::vector module_; std::map moduleAddress_; // lattice store std::map lattice_; // fermion matrix store std::map fMat_; // solver store & solver/action map std::map solver_; std::map solverAction_; // object store std::vector object_; std::map objectAddress_; }; /****************************************************************************** * template implementation * ******************************************************************************/ template Holder::Holder(T *pt) : objPt_(pt) {} template T & Holder::get(void) const { return &objPt_.get(); } template T * Holder::getPt(void) const { return objPt_.get(); } template void Holder::reset(T *pt) { objPt_.reset(pt); } template M * Environment::getModule(const unsigned int address) const { if (auto *pt = dynamic_cast(getModule(address))) { return pt; } else { HADRON_ERROR("module '" + module_[address].name + "' does not have type " + typeid(M).name() + "(object type: " + getModuleType(address) + ")"); } } template M * Environment::getModule(const std::string name) const { return getModule(getModuleAddress(name)); } template unsigned int Environment::lattice4dSize(void) const { return sizeof(typename T::vector_object)/getGrid()->Nsimd(); } template void Environment::registerLattice(const unsigned int address, const unsigned int Ls) { createGrid(Ls); registerObject(address, Ls*lattice4dSize(), Ls); } template void Environment::registerLattice(const std::string name, const unsigned int Ls) { createGrid(Ls); registerObject(name, Ls*lattice4dSize(), Ls); } template void Environment::setObject(const unsigned int address, T *object) { if (hasRegisteredObject(address)) { object_[address].data.reset(new Holder(object)); object_[address].type = &typeid(T); } else if (hasObject(address)) { HADRON_ERROR("object with address " + std::to_string(address) + " exists but is not registered"); } else { HADRON_ERROR("no object with address " + std::to_string(address)); } } template void Environment::setObject(const std::string name, T *object) { setObject(getObjectAddress(name), object); } template T * Environment::getObject(const unsigned int address) const { if (hasRegisteredObject(address)) { if (auto h = dynamic_cast *>(object_[address].data.get())) { return h->getPt(); } else { HADRON_ERROR("object with address " + std::to_string(address) + " does not have type '" + typeid(T).name() + "' (has type '" + getObjectType(address) + "')"); } } else if (hasObject(address)) { HADRON_ERROR("object with address " + std::to_string(address) + " exists but is not registered"); } else { HADRON_ERROR("no object with address " + std::to_string(address)); } } template T * Environment::getObject(const std::string name) const { return getObject(getObjectAddress(name)); } template T * Environment::createLattice(const unsigned int address) { GridCartesian *g = getGrid(getObjectLs(address)); setObject(address, new T(g)); return getObject(address); } template T * Environment::createLattice(const std::string name) { return createLattice(getObjectAddress(name)); } END_HADRONS_NAMESPACE #endif // Hadrons_Environment_hpp_