2015-10-27 17:33:18 +00:00
|
|
|
/*
|
|
|
|
* Application.cc, part of Grid
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Antonin Portelli
|
|
|
|
*
|
|
|
|
* Grid 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Grid 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 Grid. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Hadrons/Application.hpp>
|
2015-11-05 14:28:14 +00:00
|
|
|
#include <Hadrons/Graph.hpp>
|
2015-10-27 17:33:18 +00:00
|
|
|
|
|
|
|
using namespace Grid;
|
|
|
|
using namespace Hadrons;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* 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;
|
2015-12-23 14:21:35 +00:00
|
|
|
auto list = modFactory_.getModuleList();
|
|
|
|
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
|
|
|
}
|
2015-10-27 17:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// destructor //////////////////////////////////////////////////////////////////
|
|
|
|
Application::~Application(void)
|
|
|
|
{
|
2015-12-23 14:30:33 +00:00
|
|
|
LOG(Message) << "Grid is finalizing now" << std::endl;
|
2015-10-27 17:33:18 +00:00
|
|
|
Grid_finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 ////////////////////////////////////////////////////////
|
2015-12-23 14:21:35 +00:00
|
|
|
class ModuleId: Serializable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GRID_SERIALIZABLE_CLASS_MEMBERS(ModuleId,
|
|
|
|
std::string, name,
|
|
|
|
std::string, type);
|
|
|
|
};
|
|
|
|
|
2015-12-07 18:26:38 +00:00
|
|
|
void Application::parseParameterFile(void)
|
|
|
|
{
|
|
|
|
XmlReader reader(parameterFileName_);
|
2015-12-23 14:21:35 +00:00
|
|
|
ModuleId 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);
|
2015-12-07 18:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// schedule computation ////////////////////////////////////////////////////////
|
|
|
|
void Application::schedule(void)
|
|
|
|
{
|
2015-12-23 14:30:33 +00:00
|
|
|
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
|
|
|
|
|
|
|
// create dependency graph
|
|
|
|
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
|
|
|
|
2015-12-23 14:21:35 +00:00
|
|
|
// topological sort
|
2016-01-14 04:23:51 +00:00
|
|
|
unsigned int k = 0;
|
2015-12-07 18:26:38 +00:00
|
|
|
|
2015-12-23 14:30:33 +00:00
|
|
|
std::vector<Graph<std::string>> con = moduleGraph.getConnectedComponents();
|
|
|
|
LOG(Message) << "Program:" << std::endl;
|
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
|
|
|
{
|
2015-12-23 14:30:33 +00:00
|
|
|
std::vector<std::vector<std::string>> t = con[i].allTopoSort();
|
2016-01-14 04:23:51 +00:00
|
|
|
int memPeak, minMemPeak = -1;
|
|
|
|
unsigned int bestInd;
|
|
|
|
bool msg;
|
2015-12-23 14:21:35 +00:00
|
|
|
|
2016-01-14 04:23:51 +00:00
|
|
|
env_.dryRun(true);
|
|
|
|
for (unsigned int p = 0; p < t.size(); ++p)
|
2015-12-07 18:26:38 +00:00
|
|
|
{
|
2016-01-14 04:23:51 +00:00
|
|
|
msg = HadronsLogMessage.isActive();
|
|
|
|
HadronsLogMessage.Active(false);
|
|
|
|
memPeak = execute(t[p]);
|
|
|
|
if ((memPeak < minMemPeak) or (minMemPeak < 0))
|
|
|
|
{
|
|
|
|
minMemPeak = memPeak;
|
|
|
|
bestInd = p;
|
|
|
|
}
|
|
|
|
HadronsLogMessage.Active(msg);
|
|
|
|
env_.freeAll();
|
|
|
|
}
|
|
|
|
env_.dryRun(false);
|
|
|
|
for (unsigned int j = 0; j < t[bestInd].size(); ++j)
|
|
|
|
{
|
|
|
|
program_.push_back(t[bestInd][j]);
|
2015-12-23 14:30:33 +00:00
|
|
|
LOG(Message) << std::setw(4) << std::right << k << ": "
|
|
|
|
<< 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)
|
|
|
|
{
|
2015-12-23 14:30:33 +00:00
|
|
|
LOG(Message) << "Starting measurement for trajectory " << t
|
|
|
|
<< std::endl;
|
2016-01-14 04:23:51 +00:00
|
|
|
execute(program_);
|
|
|
|
env_.freeAll();
|
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-01-14 04:23:51 +00:00
|
|
|
unsigned int memPeak = 0;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < program.size(); ++i)
|
2015-12-23 14:21:35 +00:00
|
|
|
{
|
2016-01-14 04:23:51 +00:00
|
|
|
LOG(Message) << "Measurement step (" << i+1 << "/" << program.size()
|
2015-12-23 14:30:33 +00:00
|
|
|
<< ")" << std::endl;
|
2016-01-14 04:23:51 +00:00
|
|
|
(*module_[program[i]])(env_);
|
|
|
|
LOG(Message) << "allocated propagators: " << env_.nProp() << std::endl;
|
|
|
|
if (env_.nProp() > memPeak)
|
|
|
|
{
|
|
|
|
memPeak = env_.nProp();
|
|
|
|
}
|
|
|
|
for (auto &n: associatedModule_)
|
|
|
|
{
|
|
|
|
bool canFree = true;
|
|
|
|
|
|
|
|
for (unsigned int j = i + 1; j < program.size(); ++j)
|
|
|
|
{
|
|
|
|
auto &in = input_[program[j]];
|
|
|
|
auto it = std::find(in.begin(), in.end(), n.first);
|
|
|
|
canFree = canFree and (it == in.end());
|
|
|
|
}
|
|
|
|
if (canFree and env_.propExists(n.first))
|
|
|
|
{
|
|
|
|
LOG(Message) << "freeing '" << n.first << "'" << std::endl;
|
|
|
|
env_.free(n.first);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|