From 0fbf445edd90be7ac6363a77bc93c8b7325c45fe Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Wed, 6 Dec 2017 16:51:48 +0100 Subject: [PATCH] Hadrons: object creation that get properly captured by the memory profiler --- extras/Hadrons/Environment.hpp | 32 +++++++++++++++------ extras/Hadrons/Module.hpp | 27 +++++++++-------- extras/Hadrons/Modules/MAction/DWF.hpp | 5 ++-- extras/Hadrons/Modules/MAction/Wilson.hpp | 5 ++-- extras/Hadrons/Modules/MSolver/RBPrecCG.hpp | 2 +- extras/Hadrons/VirtualMachine.cc | 3 +- 6 files changed, 44 insertions(+), 30 deletions(-) diff --git a/extras/Hadrons/Environment.hpp b/extras/Hadrons/Environment.hpp index 9d482923..5058a820 100644 --- a/extras/Hadrons/Environment.hpp +++ b/extras/Hadrons/Environment.hpp @@ -100,11 +100,16 @@ public: // general memory management void addObject(const std::string name, const int moduleAddress = -1); - template + template + void createDerivedObject(const std::string name, + const Environment::Storage storage, + const unsigned int Ls, + Ts && ... args); + template void createObject(const std::string name, - const Storage storage, + const Environment::Storage storage, const unsigned int Ls, - P &&pt); + Ts && ... args); void setObjectModule(const unsigned int objAddress, const int modAddress); template @@ -195,11 +200,11 @@ void Holder::reset(T *pt) * Environment template implementation * ******************************************************************************/ // general memory management /////////////////////////////////////////////////// -template -void Environment::createObject(const std::string name, +template +void Environment::createDerivedObject(const std::string name, const Environment::Storage storage, const unsigned int Ls, - P &&pt) + Ts && ... args) { if (!hasObject(name)) { @@ -210,13 +215,13 @@ void Environment::createObject(const std::string name, if (!object_[address].data) { - MemoryStats memStats; + MemoryStats memStats; MemoryProfiler::stats = &memStats; object_[address].storage = storage; object_[address].Ls = Ls; - object_[address].data.reset(new Holder(pt)); - object_[address].size = memStats.totalAllocated; + object_[address].data.reset(new Holder(new T(std::forward(args)...))); + object_[address].size = memStats.maxAllocated; object_[address].type = &typeid(T); MemoryProfiler::stats = nullptr; } @@ -226,6 +231,15 @@ void Environment::createObject(const std::string name, } } +template +void Environment::createObject(const std::string name, + const Environment::Storage storage, + const unsigned int Ls, + Ts && ... args) +{ + createDerivedObject(name, storage, Ls, std::forward(args)...); +} + template T * Environment::getObject(const unsigned int address) const { diff --git a/extras/Hadrons/Module.hpp b/extras/Hadrons/Module.hpp index d1910c9b..14d98bfb 100644 --- a/extras/Hadrons/Module.hpp +++ b/extras/Hadrons/Module.hpp @@ -98,39 +98,42 @@ static ns##mod##ModuleRegistrar ns##mod##ModuleRegistrarInstance; #define envHasType(type, name)\ env().template isObjectOfType(name) -#define envCreate(type, name, Ls, pt)\ -env().template createObject(name, Environment::Storage::object, Ls, pt) +#define envCreate(type, name, Ls, ...)\ +env().template createObject(name, Environment::Storage::object, Ls, __VA_ARGS__) + +#define envCreateDerived(base, type, name, Ls, ...)\ +env().template createDerivedObject(name, Environment::Storage::object, Ls, __VA_ARGS__) #define envCreateLat4(type, name)\ -envCreate(type, name, 1, new type(env().getGrid())) +envCreate(type, name, 1, env().getGrid()) #define envCreateLat5(type, name, Ls)\ -envCreate(type, name, Ls, new type(env().getGrid(Ls))) +envCreate(type, name, Ls, env().getGrid(Ls)) #define envCreateLat(...)\ MACRO_REDIRECT(__VA_ARGS__, envCreateLat5, envCreateLat4)(__VA_ARGS__) -#define envCache(type, name, Ls, pt)\ -env().template createObject(name, Environment::Storage::cache, Ls, pt) +#define envCache(type, name, Ls, ...)\ +env().template createObject(name, Environment::Storage::cache, Ls, __VA_ARGS__) #define envCacheLat4(type, name)\ -envCache(type, name, 1, new type(env().getGrid())) +envCache(type, name, 1, env().getGrid()) #define envCacheLat5(type, name, Ls)\ -envCache(type, name, Ls, new type(env().getGrid(Ls))) +envCache(type, name, Ls, env().getGrid(Ls)) #define envCacheLat(...)\ MACRO_REDIRECT(__VA_ARGS__, envCacheLat5, envCacheLat4)(__VA_ARGS__) -#define envTmp(type, name, Ls, pt)\ +#define envTmp(type, name, Ls, ...)\ env().template createObject(getName() + "_tmp_" + name, \ - Environment::Storage::temporary, Ls, pt) + Environment::Storage::temporary, Ls, __VA_ARGS__) #define envTmpLat4(type, name)\ -envTmp(type, name, 1, new type(env().getGrid())) +envTmp(type, name, 1, env().getGrid()) #define envTmpLat5(type, name, Ls)\ -envTmp(type, name, Ls, new type(env().getGrid(Ls))) +envTmp(type, name, Ls, env().getGrid(Ls)) #define envTmpLat(...)\ MACRO_REDIRECT(__VA_ARGS__, envTmpLat5, envTmpLat4)(__VA_ARGS__) diff --git a/extras/Hadrons/Modules/MAction/DWF.hpp b/extras/Hadrons/Modules/MAction/DWF.hpp index 36c70073..e7d28476 100644 --- a/extras/Hadrons/Modules/MAction/DWF.hpp +++ b/extras/Hadrons/Modules/MAction/DWF.hpp @@ -118,9 +118,8 @@ void TDWF::setup(void) auto &grb5 = *env().getRbGrid(par().Ls); std::vector boundary = strToVec(par().boundary); typename DomainWallFermion::ImplParams implParams(boundary); - envCreate(FMat, getName(), par().Ls, - new DomainWallFermion(U, g5, grb5, g4, grb4, par().mass, - par().M5, implParams)); + envCreateDerived(FMat, DomainWallFermion, getName(), par().Ls, U, g5, + grb5, g4, grb4, par().mass, par().M5, implParams); } // execution /////////////////////////////////////////////////////////////////// diff --git a/extras/Hadrons/Modules/MAction/Wilson.hpp b/extras/Hadrons/Modules/MAction/Wilson.hpp index 7fe1f44e..591a3fed 100644 --- a/extras/Hadrons/Modules/MAction/Wilson.hpp +++ b/extras/Hadrons/Modules/MAction/Wilson.hpp @@ -111,9 +111,8 @@ void TWilson::setup(void) auto &gridRb = *env().getRbGrid(); std::vector boundary = strToVec(par().boundary); typename WilsonFermion::ImplParams implParams(boundary); - envCreate(FMat, getName(), 1, new WilsonFermion(U, grid, gridRb, - par().mass, - implParams)); + envCreateDerived(FMat, WilsonFermion, getName(), 1, U, grid, gridRb, + par().mass, implParams); } // execution /////////////////////////////////////////////////////////////////// diff --git a/extras/Hadrons/Modules/MSolver/RBPrecCG.hpp b/extras/Hadrons/Modules/MSolver/RBPrecCG.hpp index d8a4b95f..d6c21412 100644 --- a/extras/Hadrons/Modules/MSolver/RBPrecCG.hpp +++ b/extras/Hadrons/Modules/MSolver/RBPrecCG.hpp @@ -114,7 +114,7 @@ void TRBPrecCG::setup(void) schurSolver(mat, source, sol); }; - envCreate(SolverFn, getName(), Ls, new SolverFn(solver)); + envCreate(SolverFn, getName(), Ls, solver); env().addOwnership(getName(), par().action); } diff --git a/extras/Hadrons/VirtualMachine.cc b/extras/Hadrons/VirtualMachine.cc index f09e2710..ae1d5b6b 100644 --- a/extras/Hadrons/VirtualMachine.cc +++ b/extras/Hadrons/VirtualMachine.cc @@ -251,8 +251,7 @@ Graph VirtualMachine::makeModuleGraph(void) const // general execution /////////////////////////////////////////////////////////// #define BIG_SEP "===============" #define SEP "---------------" -#define MEM_MSG(size)\ -sizeString((size)*env().getLocalVolume()) << " (" << sizeString(size) << "/site)" +#define MEM_MSG(size) sizeString(size) VirtualMachine::Size VirtualMachine::executeProgram(const std::vector &p)