2016-02-25 12:07:21 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: programs/Hadrons/Environment.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-11-05 14:28:14 +00:00
|
|
|
|
2016-11-28 07:02:15 +00:00
|
|
|
#include <Grid/Hadrons/Environment.hpp>
|
|
|
|
#include <Grid/Hadrons/Module.hpp>
|
|
|
|
#include <Grid/Hadrons/ModuleFactory.hpp>
|
2015-11-05 14:28:14 +00:00
|
|
|
|
|
|
|
using namespace Grid;
|
2016-01-14 04:23:51 +00:00
|
|
|
using namespace QCD;
|
2015-11-05 14:28:14 +00:00
|
|
|
using namespace Hadrons;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Environment implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
|
|
|
Environment::Environment(void)
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
|
|
|
grid4d_.reset(SpaceTimeGrid::makeFourDimGrid(
|
|
|
|
GridDefaultLatt(), GridDefaultSimd(Nd, vComplex::Nsimd()),
|
|
|
|
GridDefaultMpi()));
|
2016-02-25 11:56:16 +00:00
|
|
|
gridRb4d_.reset(SpaceTimeGrid::makeFourDimRedBlackGrid(grid4d_.get()));
|
2016-05-10 19:07:41 +01:00
|
|
|
auto loc = getGrid()->LocalDimensions();
|
|
|
|
locVol_ = 1;
|
|
|
|
for (unsigned int d = 0; d < loc.size(); ++d)
|
|
|
|
{
|
|
|
|
locVol_ *= loc[d];
|
|
|
|
}
|
2016-02-25 11:56:16 +00:00
|
|
|
rng4d_.reset(new GridParallelRNG(grid4d_.get()));
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
2015-12-23 14:21:35 +00:00
|
|
|
|
2016-01-14 04:23:51 +00:00
|
|
|
// dry run /////////////////////////////////////////////////////////////////////
|
|
|
|
void Environment::dryRun(const bool isDry)
|
|
|
|
{
|
|
|
|
dryRun_ = isDry;
|
|
|
|
}
|
|
|
|
|
2016-04-30 08:17:04 +01:00
|
|
|
bool Environment::isDryRun(void) const
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
|
|
|
return dryRun_;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:07:00 +01:00
|
|
|
// trajectory number ///////////////////////////////////////////////////////////
|
|
|
|
void Environment::setTrajectory(const unsigned int traj)
|
|
|
|
{
|
|
|
|
traj_ = traj;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Environment::getTrajectory(void) const
|
|
|
|
{
|
|
|
|
return traj_;
|
|
|
|
}
|
|
|
|
|
2016-02-25 11:56:16 +00:00
|
|
|
// grids ///////////////////////////////////////////////////////////////////////
|
2016-05-07 21:19:38 +01:00
|
|
|
void Environment::createGrid(const unsigned int Ls)
|
|
|
|
{
|
2016-05-09 14:49:06 +01:00
|
|
|
if (grid5d_.find(Ls) == grid5d_.end())
|
|
|
|
{
|
|
|
|
auto g = getGrid();
|
|
|
|
|
|
|
|
grid5d_[Ls].reset(SpaceTimeGrid::makeFiveDimGrid(Ls, g));
|
|
|
|
gridRb5d_[Ls].reset(SpaceTimeGrid::makeFiveDimRedBlackGrid(Ls, g));
|
|
|
|
}
|
2016-05-07 21:19:38 +01:00
|
|
|
}
|
|
|
|
|
2016-04-30 08:17:04 +01:00
|
|
|
GridCartesian * Environment::getGrid(const unsigned int Ls) const
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (Ls == 1)
|
|
|
|
{
|
|
|
|
return grid4d_.get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return grid5d_.at(Ls).get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(std::out_of_range &)
|
|
|
|
{
|
2016-05-07 21:19:38 +01:00
|
|
|
HADRON_ERROR("no grid with Ls= " << Ls);
|
2016-04-30 08:17:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GridRedBlackCartesian * Environment::getRbGrid(const unsigned int Ls) const
|
2016-02-25 11:56:16 +00:00
|
|
|
{
|
2016-04-30 08:17:04 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (Ls == 1)
|
|
|
|
{
|
|
|
|
return gridRb4d_.get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return gridRb5d_.at(Ls).get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(std::out_of_range &)
|
|
|
|
{
|
|
|
|
HADRON_ERROR("no red-black 5D grid with Ls= " << Ls);
|
|
|
|
}
|
2016-02-25 11:56:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
// random number generator /////////////////////////////////////////////////////
|
|
|
|
void Environment::setSeed(const std::vector<int> &seed)
|
|
|
|
{
|
|
|
|
rng4d_->SeedFixedIntegers(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
GridParallelRNG * Environment::get4dRng(void) const
|
|
|
|
{
|
|
|
|
return rng4d_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// module management ///////////////////////////////////////////////////////////
|
2016-11-28 07:02:15 +00:00
|
|
|
void Environment::pushModule(Environment::ModPt &pt)
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
2016-11-28 07:02:15 +00:00
|
|
|
std::string name = pt->getName();
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
if (!hasModule(name))
|
|
|
|
{
|
|
|
|
std::vector<unsigned int> inputAddress;
|
2016-12-04 23:53:48 +00:00
|
|
|
unsigned int address;
|
2016-06-06 17:45:37 +01:00
|
|
|
ModuleInfo m;
|
2016-11-28 07:02:15 +00:00
|
|
|
|
|
|
|
m.data = std::move(pt);
|
2016-06-06 17:45:37 +01:00
|
|
|
m.type = typeIdPt(*m.data.get());
|
|
|
|
m.name = name;
|
|
|
|
auto input = m.data->getInput();
|
2016-05-10 19:07:41 +01:00
|
|
|
for (auto &in: input)
|
|
|
|
{
|
|
|
|
if (!hasObject(in))
|
|
|
|
{
|
|
|
|
addObject(in , -1);
|
|
|
|
}
|
2016-06-06 17:45:37 +01:00
|
|
|
m.input.push_back(objectAddress_[in]);
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
2016-06-06 17:45:37 +01:00
|
|
|
auto output = m.data->getOutput();
|
|
|
|
module_.push_back(std::move(m));
|
2016-12-04 23:53:48 +00:00
|
|
|
address = static_cast<unsigned int>(module_.size() - 1);
|
|
|
|
moduleAddress_[name] = address;
|
2016-05-10 19:07:41 +01:00
|
|
|
for (auto &out: output)
|
|
|
|
{
|
|
|
|
if (!hasObject(out))
|
|
|
|
{
|
2016-12-04 23:53:48 +00:00
|
|
|
addObject(out, address);
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
if (object_[objectAddress_[out]].module < 0)
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
2016-12-04 23:53:48 +00:00
|
|
|
object_[objectAddress_[out]].module = address;
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("object '" + out
|
2016-11-28 07:02:15 +00:00
|
|
|
+ "' is already produced by module '"
|
|
|
|
+ module_[object_[getObjectAddress(out)].module].name
|
|
|
|
+ "' (while pushing module '" + name + "')");
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("module '" + name + "' already exists");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 07:02:15 +00:00
|
|
|
void Environment::createModule(const std::string name, const std::string type,
|
|
|
|
XmlReader &reader)
|
|
|
|
{
|
|
|
|
auto &factory = ModuleFactory::getInstance();
|
|
|
|
auto pt = factory.create(type, name);
|
|
|
|
|
|
|
|
pt->parseParameters(reader, "options");
|
|
|
|
pushModule(pt);
|
|
|
|
}
|
|
|
|
|
2016-05-12 11:59:28 +01:00
|
|
|
ModuleBase * Environment::getModule(const unsigned int address) const
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
|
|
|
if (hasModule(address))
|
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
return module_[address].data.get();
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("no module with address " + std::to_string(address));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 11:59:28 +01:00
|
|
|
ModuleBase * Environment::getModule(const std::string name) const
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
|
|
|
return getModule(getModuleAddress(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Environment::getModuleAddress(const std::string name) const
|
|
|
|
{
|
|
|
|
if (hasModule(name))
|
|
|
|
{
|
|
|
|
return moduleAddress_.at(name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("no module with name '" + name + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Environment::getModuleName(const unsigned int address) const
|
|
|
|
{
|
|
|
|
if (hasModule(address))
|
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
return module_[address].name;
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("no module with address " + std::to_string(address));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Environment::getModuleType(const unsigned int address) const
|
|
|
|
{
|
|
|
|
if (hasModule(address))
|
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
return module_[address].type->name();
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("no module with address " + std::to_string(address));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Environment::getModuleType(const std::string name) const
|
|
|
|
{
|
|
|
|
return getModuleType(getModuleAddress(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Environment::hasModule(const unsigned int address) const
|
|
|
|
{
|
|
|
|
return (address < module_.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Environment::hasModule(const std::string name) const
|
|
|
|
{
|
|
|
|
return (moduleAddress_.find(name) != moduleAddress_.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
Graph<unsigned int> Environment::makeModuleGraph(void) const
|
|
|
|
{
|
|
|
|
Graph<unsigned int> moduleGraph;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < module_.size(); ++i)
|
|
|
|
{
|
2016-05-10 20:13:28 +01:00
|
|
|
moduleGraph.addVertex(i);
|
2016-06-06 17:45:37 +01:00
|
|
|
for (auto &j: module_[i].input)
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
moduleGraph.addEdge(object_[j].module, i);
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return moduleGraph;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BIG_SEP "==============="
|
|
|
|
#define SEP "---------------"
|
|
|
|
#define MEM_MSG(size)\
|
|
|
|
sizeString((size)*locVol_) << " (" << sizeString(size) << "/site)"
|
|
|
|
|
2016-12-04 23:53:48 +00:00
|
|
|
Environment::Size
|
|
|
|
Environment::executeProgram(const std::vector<unsigned int> &p)
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
2016-12-04 23:53:48 +00:00
|
|
|
Size memPeak = 0, sizeBefore, sizeAfter;
|
2016-05-10 19:07:41 +01:00
|
|
|
std::vector<std::set<unsigned int>> freeProg;
|
|
|
|
bool continueCollect, nothingFreed;
|
|
|
|
|
|
|
|
// build garbage collection schedule
|
|
|
|
freeProg.resize(p.size());
|
|
|
|
for (unsigned int i = 0; i < object_.size(); ++i)
|
|
|
|
{
|
|
|
|
auto pred = [i, this](const unsigned int j)
|
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
auto &in = module_[j].input;
|
2016-05-10 19:07:41 +01:00
|
|
|
auto it = std::find(in.begin(), in.end(), i);
|
|
|
|
|
2016-06-06 17:45:37 +01:00
|
|
|
return (it != in.end()) or (j == object_[i].module);
|
2016-05-10 19:07:41 +01:00
|
|
|
};
|
|
|
|
auto it = std::find_if(p.rbegin(), p.rend(), pred);
|
|
|
|
if (it != p.rend())
|
|
|
|
{
|
|
|
|
freeProg[p.rend() - it - 1].insert(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// program execution
|
|
|
|
for (unsigned int i = 0; i < p.size(); ++i)
|
|
|
|
{
|
|
|
|
// execute module
|
2016-05-10 19:19:38 +01:00
|
|
|
if (!isDryRun())
|
|
|
|
{
|
|
|
|
LOG(Message) << SEP << " Measurement step " << i+1 << "/"
|
2016-06-06 17:45:37 +01:00
|
|
|
<< p.size() << " (module '" << module_[p[i]].name
|
2016-05-10 19:19:38 +01:00
|
|
|
<< "') " << SEP << std::endl;
|
|
|
|
}
|
2016-06-06 17:45:37 +01:00
|
|
|
(*module_[p[i]].data)();
|
2016-05-10 19:07:41 +01:00
|
|
|
sizeBefore = getTotalSize();
|
|
|
|
// print used memory after execution
|
2016-05-10 19:19:38 +01:00
|
|
|
if (!isDryRun())
|
|
|
|
{
|
|
|
|
LOG(Message) << "Allocated objects: " << MEM_MSG(sizeBefore)
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2016-05-10 19:07:41 +01:00
|
|
|
if (sizeBefore > memPeak)
|
|
|
|
{
|
|
|
|
memPeak = sizeBefore;
|
|
|
|
}
|
|
|
|
// garbage collection for step i
|
2016-05-10 19:19:38 +01:00
|
|
|
if (!isDryRun())
|
|
|
|
{
|
|
|
|
LOG(Message) << "Garbage collection..." << std::endl;
|
|
|
|
}
|
2016-05-10 19:07:41 +01:00
|
|
|
nothingFreed = true;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
continueCollect = false;
|
|
|
|
auto toFree = freeProg[i];
|
|
|
|
for (auto &j: toFree)
|
|
|
|
{
|
|
|
|
// continue garbage collection while there are still
|
|
|
|
// objects without owners
|
|
|
|
continueCollect = continueCollect or !hasOwners(j);
|
|
|
|
if(freeObject(j))
|
|
|
|
{
|
|
|
|
// if an object has been freed, remove it from
|
|
|
|
// the garbage collection schedule
|
|
|
|
freeProg[i].erase(j);
|
|
|
|
nothingFreed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (continueCollect);
|
|
|
|
// any remaining objects in step i garbage collection schedule
|
|
|
|
// is scheduled for step i + 1
|
|
|
|
if (i + 1 < p.size())
|
|
|
|
{
|
|
|
|
for (auto &j: freeProg[i])
|
|
|
|
{
|
|
|
|
freeProg[i + 1].insert(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// print used memory after garbage collection if necessary
|
2016-05-10 19:19:38 +01:00
|
|
|
if (!isDryRun())
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
sizeAfter = getTotalSize();
|
2016-05-10 19:19:38 +01:00
|
|
|
if (sizeBefore != sizeAfter)
|
|
|
|
{
|
|
|
|
LOG(Message) << "Allocated objects: " << MEM_MSG(sizeAfter)
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG(Message) << "Nothing to free" << std::endl;
|
|
|
|
}
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return memPeak;
|
|
|
|
}
|
|
|
|
|
2016-12-04 23:53:48 +00:00
|
|
|
Environment::Size Environment::executeProgram(const std::vector<std::string> &p)
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
|
|
|
std::vector<unsigned int> pAddress;
|
|
|
|
|
|
|
|
for (auto &n: p)
|
|
|
|
{
|
|
|
|
pAddress.push_back(getModuleAddress(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
return executeProgram(pAddress);
|
|
|
|
}
|
|
|
|
|
2016-06-06 17:45:37 +01:00
|
|
|
// general memory management ///////////////////////////////////////////////////
|
|
|
|
void Environment::addObject(const std::string name, const int moduleAddress)
|
2016-04-30 08:17:04 +01:00
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
ObjInfo info;
|
2016-05-10 19:07:41 +01:00
|
|
|
|
2016-06-06 17:45:37 +01:00
|
|
|
info.name = name;
|
|
|
|
info.module = moduleAddress;
|
|
|
|
object_.push_back(std::move(info));
|
2016-12-04 23:53:48 +00:00
|
|
|
objectAddress_[name] = static_cast<unsigned int>(object_.size() - 1);
|
2016-05-07 21:19:38 +01:00
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
void Environment::registerObject(const unsigned int address,
|
|
|
|
const unsigned int size, const unsigned int Ls)
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
if (!hasRegisteredObject(address))
|
|
|
|
{
|
|
|
|
if (hasObject(address))
|
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
object_[address].size = size;
|
|
|
|
object_[address].Ls = Ls;
|
|
|
|
object_[address].isRegistered = true;
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("no object with address " + std::to_string(address));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("object with address " + std::to_string(address)
|
|
|
|
+ " already registered");
|
|
|
|
}
|
2016-05-04 20:17:27 +01:00
|
|
|
}
|
2016-02-25 11:56:16 +00:00
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
void Environment::registerObject(const std::string name,
|
|
|
|
const unsigned int size, const unsigned int Ls)
|
2016-05-04 20:17:27 +01:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
registerObject(getObjectAddress(name), size, Ls);
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
unsigned int Environment::getObjectAddress(const std::string name) const
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
if (hasObject(name))
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return objectAddress_.at(name);
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
HADRON_ERROR("no object with name '" + name + "'");
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
std::string Environment::getObjectName(const unsigned int address) const
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
if (hasObject(address))
|
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
return object_[address].name;
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("no object with address " + std::to_string(address));
|
|
|
|
}
|
2016-05-07 21:19:38 +01:00
|
|
|
}
|
2016-05-04 20:17:27 +01:00
|
|
|
|
2016-06-06 17:45:37 +01:00
|
|
|
std::string Environment::getObjectType(const unsigned int address) const
|
|
|
|
{
|
|
|
|
if (hasRegisteredObject(address))
|
|
|
|
{
|
|
|
|
return object_[address].type->name();
|
|
|
|
}
|
|
|
|
else if (hasObject(address))
|
|
|
|
{
|
|
|
|
HADRON_ERROR("object with address " + std::to_string(address)
|
|
|
|
+ " exists but is not registered");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("no object with address " + std::to_string(address));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Environment::getObjectType(const std::string name) const
|
|
|
|
{
|
|
|
|
return getObjectType(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
2016-12-04 23:53:48 +00:00
|
|
|
Environment::Size Environment::getObjectSize(const unsigned int address) const
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
if (hasRegisteredObject(address))
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return object_[address].size;
|
|
|
|
}
|
|
|
|
else if (hasObject(address))
|
|
|
|
{
|
|
|
|
HADRON_ERROR("object with address " + std::to_string(address)
|
2016-06-06 17:45:37 +01:00
|
|
|
+ " exists but is not registered");
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
HADRON_ERROR("no object with address " + std::to_string(address));
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-04 23:53:48 +00:00
|
|
|
Environment::Size Environment::getObjectSize(const std::string name) const
|
2016-04-30 08:17:04 +01:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return getObjectSize(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Environment::getObjectLs(const unsigned int address) const
|
|
|
|
{
|
|
|
|
if (hasRegisteredObject(address))
|
2016-04-30 08:17:04 +01:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return object_[address].Ls;
|
|
|
|
}
|
|
|
|
else if (hasObject(address))
|
|
|
|
{
|
|
|
|
HADRON_ERROR("object with address " + std::to_string(address)
|
2016-06-06 17:45:37 +01:00
|
|
|
+ " exists but is not registered");
|
2016-04-30 08:17:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
HADRON_ERROR("no object with address " + std::to_string(address));
|
2016-04-30 08:17:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
unsigned int Environment::getObjectLs(const std::string name) const
|
2016-05-07 21:19:38 +01:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return getObjectLs(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Environment::hasObject(const unsigned int address) const
|
|
|
|
{
|
|
|
|
return (address < object_.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Environment::hasObject(const std::string name) const
|
|
|
|
{
|
|
|
|
auto it = objectAddress_.find(name);
|
2016-05-07 21:19:38 +01:00
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
return ((it != objectAddress_.end()) and hasObject(it->second));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Environment::hasRegisteredObject(const unsigned int address) const
|
|
|
|
{
|
|
|
|
if (hasObject(address))
|
2016-05-07 21:19:38 +01:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return object_[address].isRegistered;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
2016-05-07 21:19:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
bool Environment::hasRegisteredObject(const std::string name) const
|
2016-05-07 21:19:38 +01:00
|
|
|
{
|
|
|
|
if (hasObject(name))
|
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return hasRegisteredObject(getObjectAddress(name));
|
2016-05-07 21:19:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return false;
|
2016-05-07 21:19:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
bool Environment::isObject5d(const unsigned int address) const
|
|
|
|
{
|
|
|
|
return (getObjectLs(address) > 1);
|
|
|
|
}
|
|
|
|
|
2016-05-07 21:19:38 +01:00
|
|
|
bool Environment::isObject5d(const std::string name) const
|
|
|
|
{
|
|
|
|
return (getObjectLs(name) > 1);
|
|
|
|
}
|
|
|
|
|
2016-12-04 23:53:48 +00:00
|
|
|
Environment::Size Environment::getTotalSize(void) const
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
2016-12-04 23:53:48 +00:00
|
|
|
Environment::Size size = 0;
|
2016-05-10 19:07:41 +01:00
|
|
|
|
|
|
|
for (auto &o: object_)
|
|
|
|
{
|
|
|
|
if (o.isRegistered)
|
|
|
|
{
|
|
|
|
size += o.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Environment::addOwnership(const unsigned int owner,
|
|
|
|
const unsigned int property)
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
if (hasObject(property))
|
|
|
|
{
|
|
|
|
object_[property].owners.insert(owner);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("no object with address " + std::to_string(property));
|
|
|
|
}
|
|
|
|
if (hasObject(owner))
|
|
|
|
{
|
|
|
|
object_[owner].properties.insert(property);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HADRON_ERROR("no object with address " + std::to_string(owner));
|
|
|
|
}
|
2016-05-05 03:11:03 +01:00
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
void Environment::addOwnership(const std::string owner,
|
|
|
|
const std::string property)
|
|
|
|
{
|
|
|
|
addOwnership(getObjectAddress(owner), getObjectAddress(property));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Environment::hasOwners(const unsigned int address) const
|
2016-05-05 03:11:03 +01:00
|
|
|
{
|
2016-05-09 14:49:06 +01:00
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
if (hasObject(address))
|
2016-05-05 03:11:03 +01:00
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
return (!object_[address].owners.empty());
|
2016-05-05 03:11:03 +01:00
|
|
|
}
|
2016-05-09 14:49:06 +01:00
|
|
|
else
|
2016-01-14 04:23:51 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
HADRON_ERROR("no object with address " + std::to_string(address));
|
2016-01-14 04:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
bool Environment::hasOwners(const std::string name) const
|
2016-02-25 11:56:16 +00:00
|
|
|
{
|
2016-05-10 19:07:41 +01:00
|
|
|
return hasOwners(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Environment::freeObject(const unsigned int address)
|
|
|
|
{
|
|
|
|
if (!hasOwners(address))
|
2016-05-05 03:11:03 +01:00
|
|
|
{
|
2016-12-05 00:08:45 +00:00
|
|
|
if (!isDryRun() and object_[address].isRegistered)
|
2016-05-05 03:11:03 +01:00
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
LOG(Message) << "Destroying object '" << object_[address].name
|
|
|
|
<< "'" << std::endl;
|
2016-05-05 03:11:03 +01:00
|
|
|
}
|
2016-06-06 17:45:37 +01:00
|
|
|
for (auto &p: object_[address].properties)
|
2016-05-07 21:19:38 +01:00
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
object_[p].owners.erase(address);
|
2016-05-07 21:19:38 +01:00
|
|
|
}
|
2016-06-06 17:45:37 +01:00
|
|
|
object_[address].size = 0;
|
|
|
|
object_[address].Ls = 0;
|
|
|
|
object_[address].isRegistered = false;
|
|
|
|
object_[address].type = nullptr;
|
|
|
|
object_[address].owners.clear();
|
|
|
|
object_[address].properties.clear();
|
|
|
|
object_[address].data.reset(nullptr);
|
2016-05-05 03:11:03 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-25 11:56:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
bool Environment::freeObject(const std::string name)
|
|
|
|
{
|
|
|
|
return freeObject(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
2016-05-05 03:11:03 +01:00
|
|
|
void Environment::freeAll(void)
|
2016-02-25 11:56:16 +00:00
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
for (unsigned int i = 0; i < object_.size(); ++i)
|
|
|
|
{
|
|
|
|
freeObject(i);
|
|
|
|
}
|
2016-02-25 11:56:16 +00:00
|
|
|
}
|
2016-05-10 19:07:41 +01:00
|
|
|
|
|
|
|
void Environment::printContent(void)
|
|
|
|
{
|
|
|
|
LOG(Message) << "Modules: " << std::endl;
|
|
|
|
for (unsigned int i = 0; i < module_.size(); ++i)
|
|
|
|
{
|
2016-11-27 07:47:22 +00:00
|
|
|
LOG(Message) << std::setw(4) << i << ": "
|
2016-06-06 17:45:37 +01:00
|
|
|
<< getModuleName(i) << " ("
|
|
|
|
<< getModuleType(i) << ")" << std::endl;
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
LOG(Message) << "Objects: " << std::endl;
|
|
|
|
for (unsigned int i = 0; i < object_.size(); ++i)
|
|
|
|
{
|
2016-11-27 07:47:22 +00:00
|
|
|
LOG(Message) << std::setw(4) << i << ": "
|
2016-06-06 17:45:37 +01:00
|
|
|
<< getObjectName(i) << std::endl;
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
}
|