1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-04 19:25:56 +01:00

Hadrons: first full implementation of the module memory profiler

This commit is contained in:
Antonin Portelli 2017-12-12 19:32:58 +00:00
parent f3a77f4b7f
commit 259d504ef0
8 changed files with 166 additions and 13 deletions

View File

@ -92,9 +92,10 @@ void Application::run(void)
{
parseParameterFile(parameterFileName_);
}
//vm().checkGraph();
env().printContent();
vm().printContent();
env().printContent();
//vm().checkGraph();
vm().memoryProfile();
if (!scheduled_)
{
schedule();

View File

@ -369,6 +369,16 @@ void Environment::freeAll(void)
}
}
void Environment::protectObjects(const bool protect)
{
protect_ = protect;
}
bool Environment::objectsProtected(void) const
{
return protect_;
}
// print environment content ///////////////////////////////////////////////////
void Environment::printContent(void) const
{
@ -376,6 +386,7 @@ void Environment::printContent(void) const
for (unsigned int i = 0; i < object_.size(); ++i)
{
LOG(Debug) << std::setw(4) << i << ": "
<< getObjectName(i) << std::endl;
<< getObjectName(i) << " ("
<< sizeString(getObjectSize(i)) << ")" << std::endl;
}
}

View File

@ -142,11 +142,14 @@ public:
void freeObject(const unsigned int address);
void freeObject(const std::string name);
void freeAll(void);
void protectObjects(const bool protect);
bool objectsProtected(void) const;
// print environment content
void printContent(void) const;
private:
// general
unsigned long int locVol_;
bool protect_{true};
// grids
std::vector<int> dim_;
GridPt grid4d_;
@ -195,8 +198,8 @@ void Holder<T>::reset(T *pt)
// general memory management ///////////////////////////////////////////////////
template <typename B, typename T, typename ... Ts>
void Environment::createDerivedObject(const std::string name,
const Environment::Storage storage,
const unsigned int Ls,
const Environment::Storage storage,
const unsigned int Ls,
Ts && ... args)
{
if (!hasObject(name))
@ -206,7 +209,7 @@ void Environment::createDerivedObject(const std::string name,
unsigned int address = getObjectAddress(name);
if (!object_[address].data)
if (!object_[address].data or !objectsProtected())
{
MemoryStats memStats;

View File

@ -36,7 +36,7 @@ See the full license in the file "LICENSE" in the top level distribution directo
#include <cxxabi.h>
#ifndef SITE_SIZE_TYPE
#define SITE_SIZE_TYPE unsigned int
#define SITE_SIZE_TYPE size_t
#endif
#define BEGIN_HADRONS_NAMESPACE \

View File

@ -160,12 +160,12 @@ public:
// parse parameters
virtual void parseParameters(XmlReader &reader, const std::string name) = 0;
virtual void saveParameters(XmlWriter &writer, const std::string name) = 0;
// execution
void operator()(void);
protected:
// setup
virtual void setup(void) {};
virtual void execute(void) = 0;
// execution
void operator()(void);
protected:
// environment shortcut
DEFINE_ENV_ALIAS;
// virtual machine shortcut

View File

@ -134,7 +134,7 @@ std::vector<std::string> TMeson<FImpl1, FImpl2>::getReference(void)
template <typename FImpl1, typename FImpl2>
std::vector<std::string> TMeson<FImpl1, FImpl2>::getOutput(void)
{
std::vector<std::string> output = {getName()};
std::vector<std::string> output = {};
return output;
}

View File

@ -390,3 +390,123 @@ void VirtualMachine::printContent(void) const
<< getModuleName(i) << std::endl;
}
}
// memory profile //////////////////////////////////////////////////////////////
VirtualMachine::MemoryProfile VirtualMachine::memoryProfile(void) const
{
bool protect = env().objectsProtected();
bool hmsg = HadronsLogMessage.isActive();
bool gmsg = GridLogMessage.isActive();
bool err = HadronsLogError.isActive();
MemoryProfile profile;
auto program = makeModuleGraph().topoSort();
profile.module.resize(getNModule());
env().protectObjects(false);
GridLogMessage.Active(false);
HadronsLogMessage.Active(false);
HadronsLogError.Active(false);
for (auto it = program.rbegin(); it != program.rend(); ++it)
{
auto a = *it;
if (profile.module[a].empty())
{
LOG(Debug) << "Profiling memory for module '" << module_[a].name << "' (" << a << ")..." << std::endl;
memoryProfile(profile, a);
env().freeAll();
}
}
env().protectObjects(protect);
GridLogMessage.Active(gmsg);
HadronsLogMessage.Active(hmsg);
HadronsLogError.Active(err);
LOG(Debug) << "Memory profile:" << std::endl;
LOG(Debug) << "----------------" << std::endl;
for (unsigned int a = 0; a < profile.module.size(); ++a)
{
LOG(Debug) << getModuleName(a) << " (" << a << ")" << std::endl;
for (auto &o: profile.module[a])
{
LOG(Debug) << "|__ " << env().getObjectName(o.first) << " ("
<< sizeString(o.second) << ")" << std::endl;
}
LOG(Debug) << std::endl;
}
LOG(Debug) << "----------------" << std::endl;
return profile;
}
void VirtualMachine::resizeProfile(MemoryProfile &profile) const
{
if (env().getMaxAddress() > profile.object.size())
{
MemoryPrint empty;
empty.size = 0;
empty.module = -1;
profile.object.resize(env().getMaxAddress(), empty);
}
}
void VirtualMachine::updateProfile(MemoryProfile &profile,
const unsigned int address) const
{
resizeProfile(profile);
for (unsigned int a = 0; a < env().getMaxAddress(); ++a)
{
if (env().hasCreatedObject(a) and (profile.object[a].module == -1))
{
profile.object[a].size = env().getObjectSize(a);
profile.object[a].module = address;
profile.module[address][a] = profile.object[a].size;
}
}
}
void VirtualMachine::cleanEnvironment(MemoryProfile &profile) const
{
resizeProfile(profile);
for (unsigned int a = 0; a < env().getMaxAddress(); ++a)
{
if (env().hasCreatedObject(a) and (profile.object[a].module == -1))
{
env().freeObject(a);
}
}
}
void VirtualMachine::memoryProfile(MemoryProfile &profile,
const unsigned int address) const
{
auto m = getModule(address);
LOG(Debug) << "Setting up module '" << m->getName() << "' (" << address << ")..." << std::endl;
try
{
m->setup();
updateProfile(profile, address);
}
catch (Exceptions::Definition &)
{
cleanEnvironment(profile);
for (auto &in: m->getInput())
{
memoryProfile(profile, env().getObjectModule(in));
}
for (auto &ref: m->getReference())
{
memoryProfile(profile, env().getObjectModule(ref));
}
m->setup();
updateProfile(profile, address);
}
}
void VirtualMachine::memoryProfile(MemoryProfile &profile,
const std::string name) const
{
memoryProfile(profile, getModuleAddress(name));
}

View File

@ -51,8 +51,18 @@ class VirtualMachine
{
SINGLETON_DEFCTOR(VirtualMachine);
public:
typedef SITE_SIZE_TYPE Size;
typedef std::unique_ptr<ModuleBase> ModPt;
typedef SITE_SIZE_TYPE Size;
typedef std::unique_ptr<ModuleBase> ModPt;
struct MemoryPrint
{
Size size;
unsigned int module;
};
struct MemoryProfile
{
std::vector<std::map<unsigned int, Size>> module;
std::vector<MemoryPrint> object;
};
private:
struct ModuleInfo
{
@ -100,12 +110,20 @@ public:
void checkGraph(void) const;
// print VM content
void printContent(void) const;
// memory profile
MemoryProfile memoryProfile(void) const;
// general execution
Size executeProgram(const std::vector<unsigned int> &p);
Size executeProgram(const std::vector<std::string> &p);
private:
// environment shortcut
DEFINE_ENV_ALIAS;
// memory profile
void resizeProfile(MemoryProfile &profile) const;
void updateProfile(MemoryProfile &profile, const unsigned int address) const;
void cleanEnvironment(MemoryProfile &profile) const;
void memoryProfile(MemoryProfile &profile, const std::string name) const;
void memoryProfile(MemoryProfile &profile, const unsigned int address) const;
private:
// general
bool dryRun_{false}, memoryProfile_{false};