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/Environment.hpp
|
2016-02-25 12:07:21 +00:00
|
|
|
|
2017-12-26 13:16:47 +00:00
|
|
|
Copyright (C) 2015-2018
|
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-11-05 14:28:14 +00:00
|
|
|
|
|
|
|
#ifndef Hadrons_Environment_hpp_
|
|
|
|
#define Hadrons_Environment_hpp_
|
|
|
|
|
2018-08-28 15:00:40 +01:00
|
|
|
#include <Hadrons/Global.hpp>
|
2016-12-04 23:53:48 +00:00
|
|
|
|
2015-11-05 14:28:14 +00:00
|
|
|
BEGIN_HADRONS_NAMESPACE
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Global environment *
|
|
|
|
******************************************************************************/
|
2016-06-06 17:45:37 +01:00
|
|
|
class Object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Object(void) = default;
|
|
|
|
virtual ~Object(void) = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Holder: public Object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Holder(void) = default;
|
|
|
|
Holder(T *pt);
|
|
|
|
virtual ~Holder(void) = default;
|
|
|
|
T & get(void) const;
|
|
|
|
T * getPt(void) const;
|
|
|
|
void reset(T *pt);
|
|
|
|
private:
|
|
|
|
std::unique_ptr<T> objPt_{nullptr};
|
|
|
|
};
|
|
|
|
|
2017-12-05 14:31:59 +00:00
|
|
|
#define DEFINE_ENV_ALIAS \
|
|
|
|
inline Environment & env(void) const\
|
|
|
|
{\
|
|
|
|
return Environment::getInstance();\
|
|
|
|
}
|
|
|
|
|
2015-11-05 14:28:14 +00:00
|
|
|
class Environment
|
|
|
|
{
|
2015-12-23 14:21:35 +00:00
|
|
|
SINGLETON(Environment);
|
2016-01-14 04:23:51 +00:00
|
|
|
public:
|
2016-12-04 23:53:48 +00:00
|
|
|
typedef SITE_SIZE_TYPE Size;
|
|
|
|
typedef std::unique_ptr<GridCartesian> GridPt;
|
|
|
|
typedef std::unique_ptr<GridRedBlackCartesian> GridRbPt;
|
|
|
|
typedef std::unique_ptr<GridParallelRNG> RngPt;
|
2017-12-01 19:38:23 +00:00
|
|
|
enum class Storage {object, cache, temporary};
|
2016-05-07 21:19:38 +01:00
|
|
|
private:
|
|
|
|
struct ObjInfo
|
|
|
|
{
|
2016-12-04 23:53:48 +00:00
|
|
|
Size size{0};
|
2017-12-01 19:38:23 +00:00
|
|
|
Storage storage{Storage::object};
|
2016-12-04 23:53:48 +00:00
|
|
|
unsigned int Ls{0};
|
2018-03-14 14:54:25 +00:00
|
|
|
const std::type_info *type{nullptr}, *derivedType{nullptr};
|
2016-06-06 17:45:37 +01:00
|
|
|
std::string name;
|
|
|
|
int module{-1};
|
|
|
|
std::unique_ptr<Object> data{nullptr};
|
2016-05-07 21:19:38 +01:00
|
|
|
};
|
2016-01-14 04:23:51 +00:00
|
|
|
public:
|
2016-02-25 11:56:16 +00:00
|
|
|
// grids
|
2016-05-07 21:19:38 +01:00
|
|
|
void createGrid(const unsigned int Ls);
|
2018-03-13 13:51:09 +00:00
|
|
|
void createCoarseGrid(const std::vector<int> &blockSize,
|
|
|
|
const unsigned int Ls = 1);
|
2016-04-30 08:17:04 +01:00
|
|
|
GridCartesian * getGrid(const unsigned int Ls = 1) const;
|
|
|
|
GridRedBlackCartesian * getRbGrid(const unsigned int Ls = 1) const;
|
2018-03-13 13:51:09 +00:00
|
|
|
GridCartesian * getCoarseGrid(const std::vector<int> &blockSize,
|
|
|
|
const unsigned int Ls = 1) const;
|
2017-06-06 17:45:30 +01:00
|
|
|
std::vector<int> getDim(void) const;
|
|
|
|
int getDim(const unsigned int mu) const;
|
2017-01-11 18:37:49 +00:00
|
|
|
unsigned int getNd(void) const;
|
2018-05-28 10:39:17 +01:00
|
|
|
double getVolume(void) const;
|
2016-05-10 19:07:41 +01:00
|
|
|
// random number generator
|
|
|
|
GridParallelRNG * get4dRng(void) const;
|
2016-05-04 20:17:27 +01:00
|
|
|
// general memory management
|
2016-06-06 17:45:37 +01:00
|
|
|
void addObject(const std::string name,
|
2016-12-21 23:25:36 +00:00
|
|
|
const int moduleAddress = -1);
|
2017-12-06 15:51:48 +00:00
|
|
|
template <typename B, typename T, typename ... Ts>
|
|
|
|
void createDerivedObject(const std::string name,
|
|
|
|
const Environment::Storage storage,
|
|
|
|
const unsigned int Ls,
|
|
|
|
Ts && ... args);
|
|
|
|
template <typename T, typename ... Ts>
|
2017-11-22 23:27:19 +00:00
|
|
|
void createObject(const std::string name,
|
2017-12-06 15:51:48 +00:00
|
|
|
const Environment::Storage storage,
|
2017-11-22 23:27:19 +00:00
|
|
|
const unsigned int Ls,
|
2017-12-06 15:51:48 +00:00
|
|
|
Ts && ... args);
|
2017-12-05 14:31:59 +00:00
|
|
|
void setObjectModule(const unsigned int objAddress,
|
|
|
|
const int modAddress);
|
2018-03-13 16:10:16 +00:00
|
|
|
template <typename B, typename T>
|
|
|
|
T * getDerivedObject(const unsigned int address) const;
|
|
|
|
template <typename B, typename T>
|
|
|
|
T * getDerivedObject(const std::string name) const;
|
2016-06-06 17:45:37 +01:00
|
|
|
template <typename T>
|
|
|
|
T * getObject(const unsigned int address) const;
|
|
|
|
template <typename T>
|
|
|
|
T * getObject(const std::string name) const;
|
2017-12-05 14:31:59 +00:00
|
|
|
unsigned int getMaxAddress(void) const;
|
2016-05-10 19:07:41 +01:00
|
|
|
unsigned int getObjectAddress(const std::string name) const;
|
|
|
|
std::string getObjectName(const unsigned int address) const;
|
2016-06-06 17:45:37 +01:00
|
|
|
std::string getObjectType(const unsigned int address) const;
|
|
|
|
std::string getObjectType(const std::string name) const;
|
2016-12-04 23:53:48 +00:00
|
|
|
Size getObjectSize(const unsigned int address) const;
|
|
|
|
Size getObjectSize(const std::string name) const;
|
2017-12-05 14:31:59 +00:00
|
|
|
Storage getObjectStorage(const unsigned int address) const;
|
|
|
|
Storage getObjectStorage(const std::string name) const;
|
|
|
|
int getObjectModule(const unsigned int address) const;
|
|
|
|
int getObjectModule(const std::string name) const;
|
2016-05-10 19:07:41 +01:00
|
|
|
unsigned int getObjectLs(const unsigned int address) const;
|
2016-05-07 21:19:38 +01:00
|
|
|
unsigned int getObjectLs(const std::string name) const;
|
2016-05-10 19:07:41 +01:00
|
|
|
bool hasObject(const unsigned int address) const;
|
|
|
|
bool hasObject(const std::string name) const;
|
2016-12-21 23:25:36 +00:00
|
|
|
bool hasCreatedObject(const unsigned int address) const;
|
|
|
|
bool hasCreatedObject(const std::string name) const;
|
2016-05-10 19:07:41 +01:00
|
|
|
bool isObject5d(const unsigned int address) const;
|
2016-05-07 21:19:38 +01:00
|
|
|
bool isObject5d(const std::string name) const;
|
2017-06-06 17:45:30 +01:00
|
|
|
template <typename T>
|
|
|
|
bool isObjectOfType(const unsigned int address) const;
|
|
|
|
template <typename T>
|
|
|
|
bool isObjectOfType(const std::string name) const;
|
2016-12-04 23:53:48 +00:00
|
|
|
Environment::Size getTotalSize(void) const;
|
2017-12-12 13:08:01 +00:00
|
|
|
void freeObject(const unsigned int address);
|
|
|
|
void freeObject(const std::string name);
|
2016-02-25 11:56:16 +00:00
|
|
|
void freeAll(void);
|
2017-12-12 19:32:58 +00:00
|
|
|
void protectObjects(const bool protect);
|
|
|
|
bool objectsProtected(void) const;
|
2017-12-05 14:31:59 +00:00
|
|
|
// print environment content
|
|
|
|
void printContent(void) const;
|
2016-05-05 03:11:03 +01:00
|
|
|
private:
|
2016-05-10 19:07:41 +01:00
|
|
|
// general
|
2018-05-28 10:39:17 +01:00
|
|
|
double vol_;
|
2017-12-12 19:32:58 +00:00
|
|
|
bool protect_{true};
|
2016-05-10 19:07:41 +01:00
|
|
|
// grids
|
2017-06-06 17:45:30 +01:00
|
|
|
std::vector<int> dim_;
|
2016-05-10 19:07:41 +01:00
|
|
|
GridPt grid4d_;
|
|
|
|
std::map<unsigned int, GridPt> grid5d_;
|
|
|
|
GridRbPt gridRb4d_;
|
|
|
|
std::map<unsigned int, GridRbPt> gridRb5d_;
|
2018-03-13 13:51:09 +00:00
|
|
|
std::map<std::vector<int>, GridPt> gridCoarse4d_;
|
|
|
|
std::map<std::vector<int>, GridPt> gridCoarse5d_;
|
2017-01-11 18:37:49 +00:00
|
|
|
unsigned int nd_;
|
2016-05-10 19:07:41 +01:00
|
|
|
// random number generator
|
|
|
|
RngPt rng4d_;
|
2016-06-06 17:45:37 +01:00
|
|
|
// object store
|
2016-05-10 19:07:41 +01:00
|
|
|
std::vector<ObjInfo> object_;
|
|
|
|
std::map<std::string, unsigned int> objectAddress_;
|
2015-11-05 14:28:14 +00:00
|
|
|
};
|
|
|
|
|
2016-05-04 20:17:27 +01:00
|
|
|
/******************************************************************************
|
2016-11-27 07:47:22 +00:00
|
|
|
* Holder template implementation *
|
2016-05-04 20:17:27 +01:00
|
|
|
******************************************************************************/
|
2016-11-27 07:47:22 +00:00
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
2016-06-06 17:45:37 +01:00
|
|
|
template <typename T>
|
|
|
|
Holder<T>::Holder(T *pt)
|
|
|
|
: objPt_(pt)
|
|
|
|
{}
|
|
|
|
|
2016-11-27 07:47:22 +00:00
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
2016-06-06 17:45:37 +01:00
|
|
|
template <typename T>
|
|
|
|
T & Holder<T>::get(void) const
|
|
|
|
{
|
2018-03-13 16:10:16 +00:00
|
|
|
return *objPt_.get();
|
2016-06-06 17:45:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T * Holder<T>::getPt(void) const
|
|
|
|
{
|
|
|
|
return objPt_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void Holder<T>::reset(T *pt)
|
|
|
|
{
|
|
|
|
objPt_.reset(pt);
|
|
|
|
}
|
|
|
|
|
2016-11-27 07:47:22 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Environment template implementation *
|
|
|
|
******************************************************************************/
|
2017-12-05 14:31:59 +00:00
|
|
|
// general memory management ///////////////////////////////////////////////////
|
2017-12-06 15:51:48 +00:00
|
|
|
template <typename B, typename T, typename ... Ts>
|
|
|
|
void Environment::createDerivedObject(const std::string name,
|
2017-12-12 19:32:58 +00:00
|
|
|
const Environment::Storage storage,
|
|
|
|
const unsigned int Ls,
|
2017-12-06 15:51:48 +00:00
|
|
|
Ts && ... args)
|
2016-06-06 17:45:37 +01:00
|
|
|
{
|
2017-11-22 23:27:19 +00:00
|
|
|
if (!hasObject(name))
|
2016-06-06 17:45:37 +01:00
|
|
|
{
|
2017-11-22 23:27:19 +00:00
|
|
|
addObject(name);
|
2016-06-06 17:45:37 +01:00
|
|
|
}
|
2017-11-22 23:27:19 +00:00
|
|
|
|
|
|
|
unsigned int address = getObjectAddress(name);
|
|
|
|
|
2017-12-12 19:32:58 +00:00
|
|
|
if (!object_[address].data or !objectsProtected())
|
2016-06-06 17:45:37 +01:00
|
|
|
{
|
2017-12-06 15:51:48 +00:00
|
|
|
MemoryStats memStats;
|
2017-12-07 13:40:58 +00:00
|
|
|
|
|
|
|
if (!MemoryProfiler::stats)
|
|
|
|
{
|
|
|
|
MemoryProfiler::stats = &memStats;
|
|
|
|
}
|
2018-03-14 14:54:25 +00:00
|
|
|
size_t initMem = MemoryProfiler::stats->currentlyAllocated;
|
|
|
|
object_[address].storage = storage;
|
|
|
|
object_[address].Ls = Ls;
|
2017-12-06 15:51:48 +00:00
|
|
|
object_[address].data.reset(new Holder<B>(new T(std::forward<Ts>(args)...)));
|
2018-03-14 14:54:25 +00:00
|
|
|
object_[address].size = MemoryProfiler::stats->maxAllocated - initMem;
|
|
|
|
object_[address].type = &typeid(B);
|
|
|
|
object_[address].derivedType = &typeid(T);
|
2017-12-07 13:40:58 +00:00
|
|
|
if (MemoryProfiler::stats == &memStats)
|
|
|
|
{
|
|
|
|
MemoryProfiler::stats = nullptr;
|
|
|
|
}
|
2016-06-06 17:45:37 +01:00
|
|
|
}
|
2017-12-19 20:28:32 +00:00
|
|
|
// object already exists, no error if it is a cache, error otherwise
|
2018-03-14 14:54:25 +00:00
|
|
|
else if ((object_[address].storage != Storage::cache) or
|
|
|
|
(object_[address].storage != storage) or
|
|
|
|
(object_[address].name != name) or
|
|
|
|
(object_[address].type != &typeid(B)) or
|
|
|
|
(object_[address].derivedType != &typeid(T)))
|
2016-06-06 17:45:37 +01:00
|
|
|
{
|
2018-07-26 16:47:45 +01:00
|
|
|
HADRONS_ERROR_REF(ObjectDefinition, "object '" + name + "' already allocated", address);
|
2016-06-06 17:45:37 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-07 21:19:38 +01:00
|
|
|
|
2017-12-06 15:51:48 +00:00
|
|
|
template <typename T, typename ... Ts>
|
|
|
|
void Environment::createObject(const std::string name,
|
|
|
|
const Environment::Storage storage,
|
|
|
|
const unsigned int Ls,
|
|
|
|
Ts && ... args)
|
|
|
|
{
|
|
|
|
createDerivedObject<T, T>(name, storage, Ls, std::forward<Ts>(args)...);
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:10:16 +00:00
|
|
|
template <typename B, typename T>
|
|
|
|
T * Environment::getDerivedObject(const unsigned int address) const
|
2016-05-04 20:17:27 +01:00
|
|
|
{
|
2017-11-22 23:27:19 +00:00
|
|
|
if (hasObject(address))
|
2016-05-04 20:17:27 +01:00
|
|
|
{
|
2017-12-12 13:08:01 +00:00
|
|
|
if (hasCreatedObject(address))
|
2016-05-04 20:17:27 +01:00
|
|
|
{
|
2018-03-13 16:10:16 +00:00
|
|
|
if (auto h = dynamic_cast<Holder<B> *>(object_[address].data.get()))
|
2017-12-12 13:08:01 +00:00
|
|
|
{
|
2018-03-13 16:10:16 +00:00
|
|
|
if (&typeid(T) == &typeid(B))
|
|
|
|
{
|
|
|
|
return dynamic_cast<T *>(h->getPt());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (auto hder = dynamic_cast<T *>(h->getPt()))
|
|
|
|
{
|
|
|
|
return hder;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-08 18:45:06 +01:00
|
|
|
HADRONS_ERROR_REF(ObjectType, "object with address " +
|
2018-07-26 16:47:45 +01:00
|
|
|
std::to_string(address) +
|
2018-03-13 16:10:16 +00:00
|
|
|
" cannot be casted to '" + typeName(&typeid(T)) +
|
2018-07-26 16:47:45 +01:00
|
|
|
"' (has type '" + typeName(&typeid(h->get())) + "')", address);
|
2018-03-13 16:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-12 13:08:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-06 12:11:00 +01:00
|
|
|
HADRONS_ERROR_REF(ObjectType, "object with address " +
|
2018-07-26 16:47:45 +01:00
|
|
|
std::to_string(address) +
|
2018-03-13 16:10:16 +00:00
|
|
|
" does not have type '" + typeName(&typeid(B)) +
|
2018-07-26 16:47:45 +01:00
|
|
|
"' (has type '" + getObjectType(address) + "')", address);
|
2017-12-12 13:08:01 +00:00
|
|
|
}
|
2016-05-04 20:17:27 +01:00
|
|
|
}
|
2016-05-10 10:41:20 +01:00
|
|
|
else
|
2016-05-04 20:17:27 +01:00
|
|
|
{
|
2018-07-26 16:47:45 +01:00
|
|
|
HADRONS_ERROR_REF(ObjectDefinition, "object with address " +
|
|
|
|
std::to_string(address) + " is empty", address);
|
2016-05-04 20:17:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-26 16:47:45 +01:00
|
|
|
HADRONS_ERROR_REF(ObjectDefinition, "no object with address " +
|
|
|
|
std::to_string(address), address);
|
2016-05-04 20:17:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:10:16 +00:00
|
|
|
template <typename B, typename T>
|
|
|
|
T * Environment::getDerivedObject(const std::string name) const
|
|
|
|
{
|
|
|
|
return getDerivedObject<B, T>(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T * Environment::getObject(const unsigned int address) const
|
|
|
|
{
|
|
|
|
return getDerivedObject<T, T>(address);
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:07:41 +01:00
|
|
|
template <typename T>
|
2016-06-06 17:45:37 +01:00
|
|
|
T * Environment::getObject(const std::string name) const
|
2016-05-10 19:07:41 +01:00
|
|
|
{
|
2016-06-06 17:45:37 +01:00
|
|
|
return getObject<T>(getObjectAddress(name));
|
2016-05-10 19:07:41 +01:00
|
|
|
}
|
|
|
|
|
2017-06-06 17:45:30 +01:00
|
|
|
template <typename T>
|
|
|
|
bool Environment::isObjectOfType(const unsigned int address) const
|
|
|
|
{
|
2017-11-22 23:27:19 +00:00
|
|
|
if (hasObject(address))
|
2017-06-06 17:45:30 +01:00
|
|
|
{
|
|
|
|
if (auto h = dynamic_cast<Holder<T> *>(object_[address].data.get()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-26 16:47:45 +01:00
|
|
|
HADRONS_ERROR_REF(ObjectDefinition, "no object with address "
|
|
|
|
+ std::to_string(address), address);
|
2017-06-06 17:45:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool Environment::isObjectOfType(const std::string name) const
|
|
|
|
{
|
|
|
|
return isObjectOfType<T>(getObjectAddress(name));
|
|
|
|
}
|
|
|
|
|
2015-11-05 14:28:14 +00:00
|
|
|
END_HADRONS_NAMESPACE
|
|
|
|
|
|
|
|
#endif // Hadrons_Environment_hpp_
|