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 std;
|
|
|
|
using namespace Grid;
|
|
|
|
using namespace Hadrons;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Application implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
|
|
|
Application::Application(int argc, char *argv[])
|
2015-12-23 14:21:35 +00:00
|
|
|
: env_(Environment::getInstance())
|
|
|
|
, modFactory_(ModuleFactory::getInstance())
|
2015-10-27 17:33:18 +00:00
|
|
|
{
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
cerr << "usage: " << argv[0] << " <parameter file> [Grid options]";
|
|
|
|
cerr << endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
parameterFileName_ = argv[1];
|
|
|
|
Grid_init(&argc, &argv);
|
|
|
|
HadronsLogError.Active(GridLogError.isActive());
|
|
|
|
HadronsLogWarning.Active(GridLogWarning.isActive());
|
|
|
|
HadronsLogMessage.Active(GridLogMessage.isActive());
|
|
|
|
HadronsLogDebug.Active(GridLogDebug.isActive());
|
|
|
|
LOG(Message) << "Grid initialized" << endl;
|
2015-12-23 14:21:35 +00:00
|
|
|
LOG(Message) << "Modules available:" << endl;
|
|
|
|
auto list = modFactory_.getModuleList();
|
|
|
|
for (auto &m: list)
|
|
|
|
{
|
|
|
|
LOG(Message) << " " << m << endl;
|
|
|
|
}
|
2015-10-27 17:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// destructor //////////////////////////////////////////////////////////////////
|
|
|
|
Application::~Application(void)
|
|
|
|
{
|
|
|
|
LOG(Message) << "Grid is finalizing now" << endl;
|
|
|
|
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-07 18:26:38 +00:00
|
|
|
LOG(Message) << "Reading '" << parameterFileName_ << "'..." << 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");
|
|
|
|
vector<string> output = module_[id.name]->getOutput();
|
|
|
|
for (auto &n: output)
|
|
|
|
{
|
|
|
|
associatedModule_[n] = id.name;
|
|
|
|
}
|
|
|
|
} while (reader.nextElement("module"));
|
|
|
|
pop(reader);
|
|
|
|
pop(reader);
|
2015-12-07 18:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// schedule computation ////////////////////////////////////////////////////////
|
|
|
|
void Application::schedule(void)
|
|
|
|
{
|
|
|
|
Graph<string> moduleGraph;
|
2015-12-02 19:33:34 +00:00
|
|
|
|
2015-12-07 18:26:38 +00:00
|
|
|
LOG(Message) << "Scheduling computation..." << 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:21:35 +00:00
|
|
|
vector<string> input = m.second->getInput();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
catch (out_of_range &)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
map<string, map<string, bool>> m;
|
|
|
|
unsigned int k = 0;
|
2015-12-07 18:26:38 +00:00
|
|
|
|
2015-12-23 14:21:35 +00:00
|
|
|
vector<Graph<string>> con = moduleGraph.getConnectedComponents();
|
2015-12-07 18:26:38 +00:00
|
|
|
LOG(Message) << "Program:" << endl;
|
|
|
|
for (unsigned int i = 0; i < con.size(); ++i)
|
2015-12-02 19:33:34 +00:00
|
|
|
{
|
2015-12-07 18:26:38 +00:00
|
|
|
vector<vector<string>> t = con[i].allTopoSort();
|
2015-12-23 14:21:35 +00:00
|
|
|
|
|
|
|
m = makeDependencyMatrix(t);
|
|
|
|
for (unsigned int j = 0; j < t[0].size(); ++j)
|
2015-12-07 18:26:38 +00:00
|
|
|
{
|
2015-12-23 14:21:35 +00:00
|
|
|
program_.push_back(t[0][j]);
|
|
|
|
LOG(Message) << setw(4) << right << k << ": "
|
|
|
|
<< program_[k] << endl;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
LOG(Message) << "Starting measurement for trajectory " << t << endl;
|
|
|
|
execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::execute(void)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < program_.size(); ++i)
|
|
|
|
{
|
|
|
|
LOG(Message) << "Measurement step (" << i+1 << "/" << program_.size()
|
|
|
|
<< ")" << endl;
|
|
|
|
(*module_[program_[i]])(env_);
|
2015-11-05 14:28:14 +00:00
|
|
|
}
|
|
|
|
}
|