2016-12-15 18:26:39 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
2016-02-25 12:07:21 +00:00
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
2016-12-15 18:26:39 +00:00
|
|
|
Source file: extras/Hadrons/Application.cc
|
2016-02-25 12:07:21 +00:00
|
|
|
|
|
|
|
Copyright (C) 2015
|
2016-12-15 18:26:39 +00:00
|
|
|
Copyright (C) 2016
|
2016-02-25 12:07:21 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2016-12-15 18:26:39 +00:00
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
|
|
|
*************************************************************************************/
|
2016-12-15 18:21:52 +00:00
|
|
|
/* END LEGAL */
|
2015-10-27 17:33:18 +00:00
|
|
|
|
2016-11-28 07:02:15 +00:00
|
|
|
#include <Grid/Hadrons/Application.hpp>
|
|
|
|
#include <Grid/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 *
|
|
|
|
******************************************************************************/
|
2016-11-28 07:02:15 +00:00
|
|
|
// constructors ////////////////////////////////////////////////////////////////
|
|
|
|
Application::Application(void)
|
2015-10-27 17:33:18 +00:00
|
|
|
{
|
2015-12-23 14:30:33 +00:00
|
|
|
LOG(Message) << "Modules available:" << std::endl;
|
2016-05-10 19:07:41 +01:00
|
|
|
auto list = ModuleFactory::getInstance().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
|
|
|
}
|
|
|
|
|
2016-11-28 07:02:15 +00:00
|
|
|
Application::Application(const Application::GlobalPar &par)
|
|
|
|
: Application()
|
|
|
|
{
|
|
|
|
setPar(par);
|
|
|
|
}
|
|
|
|
|
|
|
|
Application::Application(const std::string parameterFileName)
|
|
|
|
: Application()
|
|
|
|
{
|
|
|
|
parameterFileName_ = parameterFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
|
|
|
void Application::setPar(const Application::GlobalPar &par)
|
|
|
|
{
|
|
|
|
par_ = par;
|
2017-01-21 21:40:23 +00:00
|
|
|
env().setSeed(strToVec<int>(par_.seed));
|
2016-11-28 07:02:15 +00:00
|
|
|
}
|
2015-10-27 17:33:18 +00:00
|
|
|
|
2016-12-14 18:01:56 +00:00
|
|
|
const Application::GlobalPar & Application::getPar(void)
|
|
|
|
{
|
|
|
|
return par_;
|
|
|
|
}
|
|
|
|
|
2015-10-27 17:33:18 +00:00
|
|
|
// execute /////////////////////////////////////////////////////////////////////
|
|
|
|
void Application::run(void)
|
2015-11-05 14:28:14 +00:00
|
|
|
{
|
2017-12-05 14:31:59 +00:00
|
|
|
if (!parameterFileName_.empty() and (vm().getNModule() == 0))
|
2016-11-28 07:02:15 +00:00
|
|
|
{
|
2016-12-14 18:01:56 +00:00
|
|
|
parseParameterFile(parameterFileName_);
|
2016-11-28 07:02:15 +00:00
|
|
|
}
|
2017-12-05 14:31:59 +00:00
|
|
|
//vm().checkGraph();
|
2017-12-01 19:38:23 +00:00
|
|
|
env().printContent();
|
2017-12-05 14:31:59 +00:00
|
|
|
vm().printContent();
|
2016-12-14 19:40:36 +00:00
|
|
|
if (!scheduled_)
|
|
|
|
{
|
|
|
|
schedule();
|
|
|
|
}
|
|
|
|
printSchedule();
|
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);
|
|
|
|
};
|
|
|
|
|
2016-12-14 18:01:56 +00:00
|
|
|
void Application::parseParameterFile(const std::string parameterFileName)
|
2015-12-07 18:26:38 +00:00
|
|
|
{
|
2016-12-14 18:01:56 +00:00
|
|
|
XmlReader reader(parameterFileName);
|
2016-12-05 02:44:36 +00:00
|
|
|
GlobalPar par;
|
2016-04-30 08:17:04 +01:00
|
|
|
ObjectId id;
|
2015-11-05 14:28:14 +00:00
|
|
|
|
2016-12-14 19:40:36 +00:00
|
|
|
LOG(Message) << "Building application from '" << parameterFileName << "'..." << std::endl;
|
2016-12-05 02:44:36 +00:00
|
|
|
read(reader, "parameters", par);
|
|
|
|
setPar(par);
|
2017-12-01 19:38:23 +00:00
|
|
|
if (!push(reader, "modules"))
|
|
|
|
{
|
|
|
|
HADRON_ERROR("Cannot open node 'modules' in parameter file '" + parameterFileName + "'");
|
|
|
|
}
|
|
|
|
if (!push(reader, "module"))
|
|
|
|
{
|
|
|
|
HADRON_ERROR("Cannot open node 'modules/module' in parameter file '" + parameterFileName + "'");
|
|
|
|
}
|
2015-12-23 14:21:35 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
read(reader, "id", id);
|
2017-12-05 14:31:59 +00:00
|
|
|
vm().createModule(id.name, id.type, reader);
|
2015-12-23 14:21:35 +00:00
|
|
|
} while (reader.nextElement("module"));
|
|
|
|
pop(reader);
|
|
|
|
pop(reader);
|
2015-12-07 18:26:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 18:01:56 +00:00
|
|
|
void Application::saveParameterFile(const std::string parameterFileName)
|
|
|
|
{
|
|
|
|
XmlWriter writer(parameterFileName);
|
|
|
|
ObjectId id;
|
2017-12-05 14:31:59 +00:00
|
|
|
const unsigned int nMod = vm().getNModule();
|
2016-12-14 18:01:56 +00:00
|
|
|
|
2016-12-14 19:40:36 +00:00
|
|
|
LOG(Message) << "Saving application to '" << parameterFileName << "'..." << std::endl;
|
2016-12-14 18:01:56 +00:00
|
|
|
write(writer, "parameters", getPar());
|
|
|
|
push(writer, "modules");
|
|
|
|
for (unsigned int i = 0; i < nMod; ++i)
|
|
|
|
{
|
|
|
|
push(writer, "module");
|
2017-12-05 14:31:59 +00:00
|
|
|
id.name = vm().getModuleName(i);
|
|
|
|
id.type = vm().getModule(i)->getRegisteredName();
|
2016-12-14 18:01:56 +00:00
|
|
|
write(writer, "id", id);
|
2017-12-05 14:31:59 +00:00
|
|
|
vm().getModule(i)->saveParameters(writer, "options");
|
2016-12-14 18:01:56 +00:00
|
|
|
pop(writer);
|
|
|
|
}
|
|
|
|
pop(writer);
|
|
|
|
pop(writer);
|
|
|
|
}
|
|
|
|
|
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)"
|
|
|
|
|
2016-12-14 19:40:36 +00:00
|
|
|
#define DEFINE_MEMPEAK \
|
2017-05-01 17:49:08 +01:00
|
|
|
GeneticScheduler<unsigned int>::ObjFunc memPeak = \
|
|
|
|
[this](const std::vector<unsigned int> &program)\
|
2016-12-14 19:40:36 +00:00
|
|
|
{\
|
|
|
|
unsigned int memPeak;\
|
|
|
|
bool msg;\
|
|
|
|
\
|
|
|
|
msg = HadronsLogMessage.isActive();\
|
|
|
|
HadronsLogMessage.Active(false);\
|
2017-12-05 14:31:59 +00:00
|
|
|
vm().dryRun(true);\
|
|
|
|
memPeak = vm().executeProgram(program);\
|
|
|
|
vm().dryRun(false);\
|
2017-01-21 21:40:23 +00:00
|
|
|
env().freeAll();\
|
2017-12-03 18:45:15 +00:00
|
|
|
HadronsLogMessage.Active(msg);\
|
2016-12-14 19:40:36 +00:00
|
|
|
\
|
|
|
|
return memPeak;\
|
|
|
|
}
|
|
|
|
|
2015-12-07 18:26:38 +00:00
|
|
|
void Application::schedule(void)
|
|
|
|
{
|
2016-12-14 19:40:36 +00:00
|
|
|
DEFINE_MEMPEAK;
|
2016-05-07 21:19:38 +01:00
|
|
|
|
2016-12-06 23:59:58 +00:00
|
|
|
// build module dependency graph
|
|
|
|
LOG(Message) << "Building module graph..." << std::endl;
|
2017-12-05 14:31:59 +00:00
|
|
|
auto graph = vm().makeModuleGraph();
|
2017-12-01 19:38:23 +00:00
|
|
|
LOG(Debug) << "Module graph:" << std::endl;
|
|
|
|
LOG(Debug) << graph << std::endl;
|
2016-12-14 19:40:36 +00:00
|
|
|
auto con = graph.getConnectedComponents();
|
2016-12-06 23:59:58 +00:00
|
|
|
|
|
|
|
// constrained topological sort using a genetic algorithm
|
2017-12-03 18:45:15 +00:00
|
|
|
// LOG(Message) << "Scheduling computation..." << std::endl;
|
|
|
|
// LOG(Message) << " #module= " << graph.size() << std::endl;
|
|
|
|
// LOG(Message) << " population size= " << par_.genetic.popSize << std::endl;
|
|
|
|
// LOG(Message) << " max. generation= " << par_.genetic.maxGen << std::endl;
|
|
|
|
// LOG(Message) << " max. cst. generation= " << par_.genetic.maxCstGen << std::endl;
|
|
|
|
// LOG(Message) << " mutation rate= " << par_.genetic.mutationRate << std::endl;
|
2016-12-06 23:59:58 +00:00
|
|
|
|
2017-12-03 18:45:15 +00:00
|
|
|
// unsigned int k = 0, gen, prevPeak, nCstPeak = 0;
|
|
|
|
// std::random_device rd;
|
|
|
|
// GeneticScheduler<unsigned int>::Parameters par;
|
2016-12-06 23:59:58 +00:00
|
|
|
|
2017-12-03 18:45:15 +00:00
|
|
|
// par.popSize = par_.genetic.popSize;
|
|
|
|
// par.mutationRate = par_.genetic.mutationRate;
|
|
|
|
// par.seed = rd();
|
|
|
|
// memPeak_ = 0;
|
|
|
|
// 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
|
|
|
{
|
2017-12-03 18:45:15 +00:00
|
|
|
// GeneticScheduler<unsigned int> scheduler(con[i], memPeak, par);
|
2016-05-09 14:49:06 +01:00
|
|
|
|
2017-12-03 18:45:15 +00:00
|
|
|
// gen = 0;
|
|
|
|
// do
|
|
|
|
// {
|
|
|
|
// LOG(Debug) << "Generation " << gen << ":" << std::endl;
|
|
|
|
// scheduler.nextGeneration();
|
|
|
|
// if (gen != 0)
|
|
|
|
// {
|
|
|
|
// if (prevPeak == scheduler.getMinValue())
|
|
|
|
// {
|
|
|
|
// nCstPeak++;
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// nCstPeak = 0;
|
|
|
|
// }
|
|
|
|
// }
|
2016-12-06 23:59:58 +00:00
|
|
|
|
2017-12-03 18:45:15 +00:00
|
|
|
// prevPeak = scheduler.getMinValue();
|
|
|
|
// if (gen % 10 == 0)
|
|
|
|
// {
|
|
|
|
// LOG(Iterative) << "Generation " << gen << ": "
|
|
|
|
// << MEM_MSG(scheduler.getMinValue()) << std::endl;
|
|
|
|
// }
|
2016-12-06 23:59:58 +00:00
|
|
|
|
2017-12-03 18:45:15 +00:00
|
|
|
// gen++;
|
|
|
|
// } while ((gen < par_.genetic.maxGen)
|
|
|
|
// and (nCstPeak < par_.genetic.maxCstGen));
|
|
|
|
// auto &t = scheduler.getMinSchedule();
|
|
|
|
// if (scheduler.getMinValue() > memPeak_)
|
|
|
|
// {
|
|
|
|
// memPeak_ = scheduler.getMinValue();
|
|
|
|
// }
|
|
|
|
auto t = con[i].topoSort();
|
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]);
|
2015-12-02 19:33:34 +00:00
|
|
|
}
|
2015-12-23 14:21:35 +00:00
|
|
|
}
|
2016-12-14 19:40:36 +00:00
|
|
|
scheduled_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::saveSchedule(const std::string filename)
|
|
|
|
{
|
|
|
|
TextWriter writer(filename);
|
|
|
|
std::vector<std::string> program;
|
|
|
|
|
|
|
|
if (!scheduled_)
|
|
|
|
{
|
|
|
|
HADRON_ERROR("Computation not scheduled");
|
|
|
|
}
|
|
|
|
LOG(Message) << "Saving current schedule to '" << filename << "'..."
|
|
|
|
<< std::endl;
|
|
|
|
for (auto address: program_)
|
|
|
|
{
|
2017-12-05 14:31:59 +00:00
|
|
|
program.push_back(vm().getModuleName(address));
|
2016-12-14 19:40:36 +00:00
|
|
|
}
|
|
|
|
write(writer, "schedule", program);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::loadSchedule(const std::string filename)
|
|
|
|
{
|
|
|
|
DEFINE_MEMPEAK;
|
|
|
|
|
|
|
|
TextReader reader(filename);
|
|
|
|
std::vector<std::string> program;
|
|
|
|
|
|
|
|
LOG(Message) << "Loading schedule from '" << filename << "'..."
|
|
|
|
<< std::endl;
|
|
|
|
read(reader, "schedule", program);
|
|
|
|
program_.clear();
|
|
|
|
for (auto &name: program)
|
|
|
|
{
|
2017-12-05 14:31:59 +00:00
|
|
|
program_.push_back(vm().getModuleAddress(name));
|
2016-12-14 19:40:36 +00:00
|
|
|
}
|
|
|
|
scheduled_ = true;
|
|
|
|
memPeak_ = memPeak(program_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::printSchedule(void)
|
|
|
|
{
|
|
|
|
if (!scheduled_)
|
|
|
|
{
|
|
|
|
HADRON_ERROR("Computation not scheduled");
|
|
|
|
}
|
|
|
|
LOG(Message) << "Schedule (memory peak: " << MEM_MSG(memPeak_) << "):"
|
|
|
|
<< std::endl;
|
|
|
|
for (unsigned int i = 0; i < program_.size(); ++i)
|
|
|
|
{
|
|
|
|
LOG(Message) << std::setw(4) << i + 1 << ": "
|
2017-12-05 14:31:59 +00:00
|
|
|
<< vm().getModuleName(program_[i]) << std::endl;
|
2016-12-14 19:40:36 +00:00
|
|
|
}
|
2015-12-23 14:21:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
// loop on configurations //////////////////////////////////////////////////////
|
2015-12-23 14:21:35 +00:00
|
|
|
void Application::configLoop(void)
|
|
|
|
{
|
2016-05-11 15:01:52 +01:00
|
|
|
auto range = par_.trajCounter;
|
2015-12-23 14:21:35 +00:00
|
|
|
|
|
|
|
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;
|
2017-12-05 14:31:59 +00:00
|
|
|
vm().setTrajectory(t);
|
|
|
|
vm().executeProgram(program_);
|
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;
|
2017-01-21 21:40:23 +00:00
|
|
|
env().freeAll();
|
2015-12-23 14:21:35 +00:00
|
|
|
}
|
2017-11-22 23:27:19 +00:00
|
|
|
|
|
|
|
// memory profile //////////////////////////////////////////////////////////////
|
|
|
|
void Application::memoryProfile(void)
|
|
|
|
{
|
2017-12-05 14:31:59 +00:00
|
|
|
auto graph = vm().makeModuleGraph();
|
2017-11-22 23:27:19 +00:00
|
|
|
auto program = graph.topoSort();
|
|
|
|
bool msg;
|
|
|
|
|
|
|
|
msg = HadronsLogMessage.isActive();
|
|
|
|
HadronsLogMessage.Active(false);
|
|
|
|
|
|
|
|
HadronsLogMessage.Active(msg);
|
|
|
|
}
|