2016-02-25 12:07:21 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: programs/Hadrons/Application.cc
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
|
|
|
|
|
|
|
Author: Antonin Portelli <antonin.portelli@me.com>
|
|
|
|
|
|
|
|
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.
|
|
|
|
*******************************************************************************/
|
2015-10-27 17:33:18 +00:00
|
|
|
|
|
|
|
#include <Hadrons/Application.hpp>
|
2016-05-09 14:49:06 +01:00
|
|
|
#include <Hadrons/GeneticScheduler.hpp>
|
2015-10-27 17:33:18 +00:00
|
|
|
|
|
|
|
using namespace Grid;
|
2016-05-03 03:31:21 +01:00
|
|
|
using namespace QCD;
|
2015-10-27 17:33:18 +00:00
|
|
|
using namespace Hadrons;
|
|
|
|
|
2016-05-07 21:26:56 +01:00
|
|
|
#define BIG_SEP "==============="
|
|
|
|
#define SEP "---------------"
|
|
|
|
|
2015-10-27 17:33:18 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Application implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
2016-01-14 04:22:37 +00:00
|
|
|
Application::Application(const std::string parameterFileName)
|
|
|
|
: parameterFileName_(parameterFileName)
|
|
|
|
, env_(Environment::getInstance())
|
2015-12-23 14:21:35 +00:00
|
|
|
, modFactory_(ModuleFactory::getInstance())
|
2015-10-27 17:33:18 +00:00
|
|
|
{
|
2015-12-23 14:30:33 +00:00
|
|
|
LOG(Message) << "Modules available:" << std::endl;
|
2016-05-04 00:30:29 +01:00
|
|
|
auto list = modFactory_.getBuilderList();
|
2015-12-23 14:21:35 +00:00
|
|
|
for (auto &m: list)
|
|
|
|
{
|
2015-12-23 14:30:33 +00:00
|
|
|
LOG(Message) << " " << m << std::endl;
|
2015-12-23 14:21:35 +00:00
|
|
|
}
|
2016-05-04 20:17:27 +01:00
|
|
|
auto dim = GridDefaultLatt(), mpi = GridDefaultMpi(), loc(dim);
|
|
|
|
locVol_ = 1;
|
|
|
|
for (unsigned int d = 0; d < dim.size(); ++d)
|
|
|
|
{
|
|
|
|
loc[d] /= mpi[d];
|
|
|
|
locVol_ *= loc[d];
|
|
|
|
}
|
|
|
|
LOG(Message) << "Global lattice: " << dim << std::endl;
|
|
|
|
LOG(Message) << "MPI partition : " << mpi << std::endl;
|
|
|
|
LOG(Message) << "Local lattice : " << loc << std::endl;
|
2015-10-27 17:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// destructor //////////////////////////////////////////////////////////////////
|
|
|
|
Application::~Application(void)
|
2016-02-23 16:33:00 +00:00
|
|
|
{}
|
2015-10-27 17:33:18 +00:00
|
|
|
|
|
|
|
// execute /////////////////////////////////////////////////////////////////////
|
|
|
|
void Application::run(void)
|
2015-11-05 14:28:14 +00:00
|
|
|
{
|
2015-12-07 18:26:38 +00:00
|
|
|
parseParameterFile();
|
|
|
|
schedule();
|
2015-12-23 14:21:35 +00:00
|
|
|
configLoop();
|
2015-12-07 18:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse parameter file ////////////////////////////////////////////////////////
|
2016-04-30 08:17:04 +01:00
|
|
|
class ObjectId: Serializable
|
2015-12-23 14:21:35 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-04-30 08:17:04 +01:00
|
|
|
GRID_SERIALIZABLE_CLASS_MEMBERS(ObjectId,
|
2015-12-23 14:21:35 +00:00
|
|
|
std::string, name,
|
|
|
|
std::string, type);
|
|
|
|
};
|
|
|
|
|
2015-12-07 18:26:38 +00:00
|
|
|
void Application::parseParameterFile(void)
|
|
|
|
{
|
|
|
|
XmlReader reader(parameterFileName_);
|
2016-04-30 08:17:04 +01:00
|
|
|
ObjectId id;
|
2015-11-05 14:28:14 +00:00
|
|
|
|
2015-12-23 14:30:33 +00:00
|
|
|
LOG(Message) << "Reading '" << parameterFileName_ << "'..." << std::endl;
|
2015-12-23 14:21:35 +00:00
|
|
|
read(reader, "parameters", par_);
|
|
|
|
push(reader, "modules");
|
|
|
|
push(reader, "module");
|
|
|
|
do
|
|
|
|
{
|
|
|
|
read(reader, "id", id);
|
|
|
|
module_[id.name] = modFactory_.create(id.type, id.name);
|
|
|
|
module_[id.name]->parseParameters(reader, "options");
|
2015-12-23 14:30:33 +00:00
|
|
|
std::vector<std::string> output = module_[id.name]->getOutput();
|
2015-12-23 14:21:35 +00:00
|
|
|
for (auto &n: output)
|
|
|
|
{
|
|
|
|
associatedModule_[n] = id.name;
|
|
|
|
}
|
2016-01-14 04:23:51 +00:00
|
|
|
input_[id.name] = module_[id.name]->getInput();
|
2015-12-23 14:21:35 +00:00
|
|
|
} while (reader.nextElement("module"));
|
|
|
|
pop(reader);
|
|
|
|
pop(reader);
|
2016-04-30 08:17:04 +01:00
|
|
|
env_.setSeed(strToVec<int>(par_.seed));
|
2015-12-07 18:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// schedule computation ////////////////////////////////////////////////////////
|
2016-05-07 21:19:38 +01:00
|
|
|
#define MEM_MSG(size)\
|
|
|
|
sizeString((size)*locVol_) << " (" << sizeString(size) << "/site)"
|
|
|
|
|
2015-12-07 18:26:38 +00:00
|
|
|
void Application::schedule(void)
|
|
|
|
{
|
2016-05-09 14:49:06 +01:00
|
|
|
// memory peak function
|
2016-05-07 21:19:38 +01:00
|
|
|
auto memPeak = [this](const std::vector<std::string> &program)
|
|
|
|
{
|
|
|
|
unsigned int memPeak;
|
|
|
|
bool msg;
|
|
|
|
|
|
|
|
msg = HadronsLogMessage.isActive();
|
|
|
|
HadronsLogMessage.Active(false);
|
|
|
|
env_.dryRun(true);
|
|
|
|
memPeak = execute(program);
|
|
|
|
env_.dryRun(false);
|
|
|
|
env_.freeAll();
|
|
|
|
HadronsLogMessage.Active(true);
|
|
|
|
|
|
|
|
return memPeak;
|
|
|
|
};
|
|
|
|
|
2016-05-09 14:49:06 +01:00
|
|
|
// create dependency graph
|
|
|
|
Graph<std::string> moduleGraph;
|
2015-12-02 19:33:34 +00:00
|
|
|
|
2015-12-23 14:30:33 +00:00
|
|
|
LOG(Message) << "Scheduling computation..." << std::endl;
|
2015-12-23 14:21:35 +00:00
|
|
|
for (auto &m: module_)
|
2015-11-05 14:28:14 +00:00
|
|
|
{
|
2015-12-23 14:30:33 +00:00
|
|
|
std::vector<std::string> input = m.second->getInput();
|
2015-12-23 14:21:35 +00:00
|
|
|
for (auto &n: input)
|
2015-12-07 18:26:38 +00:00
|
|
|
{
|
2015-12-23 14:21:35 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
moduleGraph.addEdge(associatedModule_.at(n), m.first);
|
|
|
|
}
|
2015-12-23 14:30:33 +00:00
|
|
|
catch (std::out_of_range &)
|
2015-12-23 14:21:35 +00:00
|
|
|
{
|
|
|
|
HADRON_ERROR("unknown object '" + n + "'");
|
|
|
|
}
|
2015-12-07 18:26:38 +00:00
|
|
|
}
|
2015-12-02 19:33:34 +00:00
|
|
|
}
|
2015-12-07 18:26:38 +00:00
|
|
|
|
2016-05-09 14:49:06 +01:00
|
|
|
// constrained topological sort using a genetic algorithm
|
|
|
|
constexpr unsigned int maxGen = 200, maxCstGen = 50;
|
|
|
|
unsigned int k = 0, gen, prevPeak, nCstPeak = 0;
|
2015-12-23 14:30:33 +00:00
|
|
|
std::vector<Graph<std::string>> con = moduleGraph.getConnectedComponents();
|
2016-05-09 14:49:06 +01:00
|
|
|
GeneticScheduler<std::string>::Parameters par;
|
|
|
|
std::random_device rd;
|
|
|
|
|
|
|
|
par.popSize = 20;
|
|
|
|
par.mutationRate = .1;
|
|
|
|
par.seed = rd();
|
|
|
|
CartesianCommunicator::BroadcastWorld(0, &(par.seed), sizeof(par.seed));
|
2015-12-07 18:26:38 +00:00
|
|
|
for (unsigned int i = 0; i < con.size(); ++i)
|
2015-12-02 19:33:34 +00:00
|
|
|
{
|
2016-05-09 14:49:06 +01:00
|
|
|
GeneticScheduler<std::string> scheduler(con[i], memPeak, par);
|
|
|
|
|
|
|
|
gen = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
scheduler.nextGeneration();
|
|
|
|
if (gen != 0)
|
|
|
|
{
|
|
|
|
if (prevPeak == scheduler.getMinValue())
|
|
|
|
{
|
|
|
|
nCstPeak++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nCstPeak = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prevPeak = scheduler.getMinValue();
|
|
|
|
if (gen % 10 == 0)
|
|
|
|
{
|
|
|
|
LOG(Iterative) << "Generation " << gen << ": "
|
|
|
|
<< MEM_MSG(scheduler.getMinValue()) << std::endl;
|
|
|
|
}
|
|
|
|
gen++;
|
|
|
|
} while ((gen < maxGen) and (nCstPeak < maxCstGen));
|
|
|
|
auto &t = scheduler.getMinSchedule();
|
2016-05-07 21:19:38 +01:00
|
|
|
LOG(Message) << "Program " << i + 1 << " (memory peak: "
|
2016-05-09 14:49:06 +01:00
|
|
|
<< MEM_MSG(scheduler.getMinValue()) << "):" << std::endl;
|
2016-04-30 08:17:04 +01:00
|
|
|
for (unsigned int j = 0; j < t.size(); ++j)
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-04-30 08:17:04 +01:00
|
|
|
program_.push_back(t[j]);
|
|
|
|
LOG(Message) << std::setw(4) << std::right << k + 1 << ": "
|
2015-12-23 14:30:33 +00:00
|
|
|
<< program_[k] << std::endl;
|
2015-12-23 14:21:35 +00:00
|
|
|
k++;
|
2015-12-02 19:33:34 +00:00
|
|
|
}
|
2015-12-23 14:21:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// program execution ///////////////////////////////////////////////////////////
|
|
|
|
void Application::configLoop(void)
|
|
|
|
{
|
|
|
|
auto range = par_.configs.range;
|
|
|
|
|
|
|
|
for (unsigned int t = range.start; t < range.end; t += range.step)
|
|
|
|
{
|
2016-05-07 21:26:56 +01:00
|
|
|
LOG(Message) << BIG_SEP << " Starting measurement for trajectory " << t
|
|
|
|
<< " " << BIG_SEP << std::endl;
|
2016-05-04 01:07:00 +01:00
|
|
|
env_.setTrajectory(t);
|
2016-01-14 04:23:51 +00:00
|
|
|
execute(program_);
|
|
|
|
env_.freeAll();
|
2015-12-23 14:21:35 +00:00
|
|
|
}
|
2016-05-09 14:49:06 +01:00
|
|
|
LOG(Message) << BIG_SEP << " End of measurement " << BIG_SEP << std::endl;
|
2015-12-23 14:21:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 04:23:51 +00:00
|
|
|
unsigned int Application::execute(const std::vector<std::string> &program)
|
2015-12-23 14:21:35 +00:00
|
|
|
{
|
2016-05-09 14:49:06 +01:00
|
|
|
unsigned int memPeak = 0, sizeBefore, sizeAfter;
|
2016-05-05 03:11:03 +01:00
|
|
|
std::vector<std::set<std::string>> freeProg;
|
2016-05-09 14:49:06 +01:00
|
|
|
bool continueCollect, nothingFreed;
|
2016-01-14 04:23:51 +00:00
|
|
|
|
2016-05-05 03:11:03 +01:00
|
|
|
// build garbage collection schedule
|
2016-04-30 08:17:04 +01:00
|
|
|
freeProg.resize(program.size());
|
|
|
|
for (auto &n: associatedModule_)
|
|
|
|
{
|
|
|
|
auto pred = [&n, this](const std::string &s)
|
|
|
|
{
|
|
|
|
auto &in = input_[s];
|
|
|
|
auto it = std::find(in.begin(), in.end(), n.first);
|
|
|
|
|
|
|
|
return (it != in.end()) or (s == n.second);
|
|
|
|
};
|
|
|
|
auto it = std::find_if(program.rbegin(), program.rend(), pred);
|
|
|
|
if (it != program.rend())
|
|
|
|
{
|
2016-05-05 03:11:03 +01:00
|
|
|
freeProg[program.rend() - it - 1].insert(n.first);
|
2016-04-30 08:17:04 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-07 21:19:38 +01:00
|
|
|
|
2016-05-05 03:11:03 +01:00
|
|
|
// program execution
|
2016-01-14 04:23:51 +00:00
|
|
|
for (unsigned int i = 0; i < program.size(); ++i)
|
2015-12-23 14:21:35 +00:00
|
|
|
{
|
2016-05-05 03:11:03 +01:00
|
|
|
// execute module
|
2016-05-07 21:26:56 +01:00
|
|
|
LOG(Message) << SEP << " Measurement step " << i+1 << "/"
|
|
|
|
<< program.size() << " (module '" << program[i] << "') "
|
|
|
|
<< SEP << std::endl;
|
2016-05-05 03:11:03 +01:00
|
|
|
(*module_[program[i]])();
|
2016-05-09 14:49:06 +01:00
|
|
|
sizeBefore = env_.getTotalSize();
|
2016-05-05 03:11:03 +01:00
|
|
|
// print used memory after execution
|
2016-05-09 14:49:06 +01:00
|
|
|
LOG(Message) << "Allocated objects: " << MEM_MSG(sizeBefore)
|
|
|
|
<< std::endl;
|
|
|
|
if (sizeBefore > memPeak)
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-09 14:49:06 +01:00
|
|
|
memPeak = sizeBefore;
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
2016-05-05 03:11:03 +01:00
|
|
|
// garbage collection for step i
|
2016-05-04 20:17:27 +01:00
|
|
|
LOG(Message) << "Garbage collection..." << std::endl;
|
2016-05-09 14:49:06 +01:00
|
|
|
nothingFreed = true;
|
2016-05-05 03:11:03 +01:00
|
|
|
do
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-05 03:11:03 +01:00
|
|
|
continueCollect = false;
|
|
|
|
auto toFree = freeProg[i];
|
|
|
|
for (auto &n: toFree)
|
|
|
|
{
|
|
|
|
// continue garbage collection while there are still
|
|
|
|
// objects without owners
|
|
|
|
continueCollect = continueCollect or !env_.hasOwners(n);
|
2016-05-07 21:19:38 +01:00
|
|
|
if(env_.freeObject(n))
|
2016-05-05 03:11:03 +01:00
|
|
|
{
|
|
|
|
// if an object has been freed, remove it from
|
|
|
|
// the garbage collection schedule
|
|
|
|
freeProg[i].erase(n);
|
2016-05-09 14:49:06 +01:00
|
|
|
nothingFreed = false;
|
2016-05-05 03:11:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (continueCollect);
|
|
|
|
// any remaining objects in step i garbage collection schedule
|
|
|
|
// is scheduled for step i + 1
|
|
|
|
if (i + 1 < program.size())
|
|
|
|
{
|
|
|
|
for (auto &n: freeProg[i])
|
|
|
|
{
|
|
|
|
freeProg[i + 1].insert(n);
|
|
|
|
}
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
2016-05-09 14:49:06 +01:00
|
|
|
// print used memory after garbage collection if necessary
|
|
|
|
sizeAfter = env_.getTotalSize();
|
|
|
|
if (sizeBefore != sizeAfter)
|
|
|
|
{
|
|
|
|
LOG(Message) << "Allocated objects: " << MEM_MSG(sizeAfter)
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG(Message) << "Nothing to free" << std::endl;
|
|
|
|
}
|
2015-11-05 14:28:14 +00:00
|
|
|
}
|
2016-01-14 04:23:51 +00:00
|
|
|
|
|
|
|
return memPeak;
|
2015-11-05 14:28:14 +00:00
|
|
|
}
|
2016-05-04 20:17:27 +01:00
|
|
|
|
|
|
|
// pretty size formatting //////////////////////////////////////////////////////
|
|
|
|
std::string Application::sizeString(long unsigned int bytes)
|
|
|
|
|
|
|
|
{
|
|
|
|
constexpr unsigned int bufSize = 256;
|
|
|
|
const char *suffixes[7] = {"", "K", "M", "G", "T", "P", "E"};
|
|
|
|
char buf[256];
|
|
|
|
long unsigned int s = 0;
|
|
|
|
double count = bytes;
|
|
|
|
|
|
|
|
while (count >= 1024 && s < 7)
|
|
|
|
{
|
|
|
|
s++;
|
|
|
|
count /= 1024;
|
|
|
|
}
|
|
|
|
if (count - floor(count) == 0.0)
|
|
|
|
{
|
|
|
|
snprintf(buf, bufSize, "%d %sB", (int)count, suffixes[s]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(buf, bufSize, "%.1f %sB", count, suffixes[s]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string(buf);
|
|
|
|
}
|