SSAGES  0.8.5
Software Suite for Advanced General Ensemble Simulations
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
SSAGES::ResourceHandler Class Reference

Class that handles SSAGES resources for a simulation. More...

#include <ResourceHandler.h>

Public Member Functions

 ResourceHandler (mxx::comm &&world, mxx::comm &&comm, size_t walkerid, const std::vector< class Method * > &methods, class CVManager *cvmanager)
 Constructor. More...
 
std::string GetInput () const
 Returns inputs of the walker for the current processor. More...
 
MPI_Comm GetLocalComm () const
 Returns local (walker-specific) MPI communicator. More...
 
MPI_Comm GetWorldComm () const
 Returns world (all processors) MPI communicator. More...
 
const mxx::comm & GetLocalMxxComm () const
 Returns local (walker-specific) mxx communicator. More...
 
const mxx::comm & GetWorldMxxComm () const
 Returns world (all processors) mxx communicator. More...
 
size_t GetWalkerID () const
 Returns processor-specific walker ID. More...
 
size_t GetNumWalkers () const
 Returns the total number of walkers across all processors. More...
 
void ConfigureHook (class Hook *hook)
 Configure the Hook from given resources. More...
 
 ~ResourceHandler ()
 Destructor.
 

Static Public Member Functions

static ResourceHandlerBuild (const Json::Value &json, const MPI_Comm &world)
 Build a new ResourceHandler from JSON. More...
 

Private Attributes

mxx::comm world_
 MPI communicator containing all processors.
 
mxx::comm comm_
 MPI communicator containing processors for specific walker.
 
size_t walkerid_
 Walker ID for specific driver.
 
size_t nwalkers_
 Number of walkers.
 
class Snapshotsnapshot_
 Snapshot of system state (pointer).
 
std::vector< class Method * > methods_
 Vector of advanced sampling methods.
 
class Loggerlogger_
 CV logger.
 
class CVManagercvmanager_
 Collective variable manager.
 
class Hookhook_
 Hook pointer.
 
std::vector< std::string > inputs_
 Input file vector.
 

Detailed Description

Class that handles SSAGES resources for a simulation.

ResourceHandler is responsible for intialzing the major components of a simulation including the engine, methods, and collective variables. It is also responsible for the lifetime of the objects it creates.

Each simulation engine must implement a driver which calls this resource handler and passes it the appropriate hook.

Definition at line 44 of file ResourceHandler.h.

Constructor & Destructor Documentation

SSAGES::ResourceHandler::ResourceHandler ( mxx::comm &&  world,
mxx::comm &&  comm,
size_t  walkerid,
const std::vector< class Method * > &  methods,
class CVManager cvmanager 
)

Constructor.

Parameters
worldMPI communicator containing all processors.
commMPI communicator containing walker-specific processors.
walkeridID of the walker for the current processor.
methodsVector of pointers to methods.
cvmanagerPointer to CV manager.
Note
ResourceHandler will be responsible for lifetime of methods and CV manager.

Definition at line 35 of file ResourceHandler.cpp.

References comm_, and snapshot_.

Referenced by Build().

37  :
38  world_(std::move(world)), comm_(std::move(comm)), walkerid_(walkerid), nwalkers_(1),
39  methods_(methods), cvmanager_(cvmanager), hook_(nullptr), inputs_(0)
40  {
41  snapshot_ = new Snapshot(comm_, walkerid);
42  }
size_t nwalkers_
Number of walkers.
std::vector< std::string > inputs_
Input file vector.
class CVManager * cvmanager_
Collective variable manager.
mxx::comm comm_
MPI communicator containing processors for specific walker.
class Snapshot * snapshot_
Snapshot of system state (pointer).
class Hook * hook_
Hook pointer.
mxx::comm world_
MPI communicator containing all processors.
std::vector< class Method * > methods_
Vector of advanced sampling methods.
size_t walkerid_
Walker ID for specific driver.

Here is the caller graph for this function:

Member Function Documentation

ResourceHandler * SSAGES::ResourceHandler::Build ( const Json::Value &  json,
const MPI_Comm &  world 
)
static

Build a new ResourceHandler from JSON.

Parameters
jsonJSON root node containing driver (and children) specifications.
worldMPI communicator containing all processors.
Returns
Pointer to newly created driver.
Note
Object lifetime is caller's responsibility!

Definition at line 54 of file ResourceHandler.cpp.

References SSAGES::CVManager::AddCVtoMap(), SSAGES::Logger::Build(), SSAGES::CollectiveVariable::BuildCV(), SSAGES::Method::BuildMethod(), Json::Requirement::GetErrors(), Json::Requirement::HasErrors(), SSAGES::CVManager::LookupCV(), Json::ObjectRequirement::Parse(), ResourceHandler(), and Json::ObjectRequirement::Validate().

55  {
56  ObjectRequirement validator;
57  Value schema;
58  CharReaderBuilder rbuilder;
59  CharReader* reader = rbuilder.newCharReader();
60 
61  // Parse and validate top level schema. This just
62  // makes sure the proper fields exist and the correct
63  // types are specified in the input files.
64  reader->parse(JsonSchema::Simulation.c_str(),
65  JsonSchema::Simulation.c_str() + JsonSchema::Simulation.size(),
66  &schema, nullptr);
67  validator.Parse(schema, "#");
68  validator.Validate(json, "#");
69  if(validator.HasErrors())
70  throw BuildException(validator.GetErrors());
71 
72  // Get number of desired walkers and create array of input files.
73  auto nwalkers = json.get("walkers", 1).asUInt();
74  if(json["input"].isArray() && json["input"].size() != nwalkers)
75  throw BuildException({"#/input: Number of inputs do not match requested walkers."});
76 
77  std::vector<std::string> inputs;
78  for(size_t i = 0; i < nwalkers; ++i)
79  {
80  if(json["input"].isArray())
81  inputs.push_back(json["input"][static_cast<int>(i)].asString());
82  else
83  inputs.push_back(json["input"].asString());
84  }
85 
86  // Get basic MPI info and verify that the total number of
87  // cores are divisble by number of walkers.
88  auto wcomm = mxx::comm(world);
89  if(wcomm.size() % nwalkers != 0)
90  throw BuildException({"#/walkers: Allocated processors not divisible by number of walkers."});
91 
92  // Split communicators.
93  int ppw = wcomm.size()/nwalkers;
94  int walkerid = wcomm.rank()/ppw;
95  auto comm = wcomm.split(walkerid);
96 
97  // Build collective variables.
98  auto* cvmanager = new CVManager();
99  int icv = 0;
100  for(auto& cv : json["CVs"])
101  {
102  // Register name with CV manager, if it exists.
103  // Otherwise, register index as name to CV manager.
104  if(cv.isMember("name"))
105  {
106  CVManager::AddCVtoMap(cv["name"].asString(), icv);
107  }
108  else
109  {
110  CVManager::AddCVtoMap(std::to_string(icv), icv);
111  }
112 
113  // Build CV and add to manager.
114  cvmanager->AddCV(CollectiveVariable::BuildCV(cv, "#/CVs"));
115  ++icv;
116  }
117 
118  // Build methods.
119  std::vector<Method*> methods;
120  for(auto& m : json["methods"])
121  {
122  // Error will be thrown if lookup fails.
123  for(auto& cv : m["cvs"])
124  {
125  CVManager::LookupCV(cv, "#/methods");
126  }
127  methods.push_back(Method::BuildMethod(m, world, comm, "#/methods"));
128  }
129 
130  // Build logger.
131  Logger* l = nullptr;
132  if(json.isMember("logger"))
133  l = Logger::Build(json["logger"], world, comm, "#/logger");
134 
135  auto* rh = new ResourceHandler(std::move(world), std::move(comm), walkerid, methods, cvmanager);
136  rh->inputs_ = inputs;
137  rh->nwalkers_ = nwalkers;
138  rh->logger_ = l;
139  return rh;
140  }
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
static int LookupCV(const Json::Value &cv, const std::string &path)
Get CV id from map.
Definition: CVManager.h:112
static void AddCVtoMap(const std::string &name, unsigned int id)
Register CV name with map.
Definition: CVManager.h:101
std::vector< std::string > GetErrors()
Get list of error messages.
Definition: Requirement.h:92
Requirements on an object.
static Method * BuildMethod(const Json::Value &json, const MPI_Comm &world, const MPI_Comm &comm, const std::string &path)
Build a derived method from JSON node.
Definition: Method.cpp:39
ResourceHandler(mxx::comm &&world, mxx::comm &&comm, size_t walkerid, const std::vector< class Method * > &methods, class CVManager *cvmanager)
Constructor.
static Logger * Build(const Json::Value &json, const MPI_Comm &world, const MPI_Comm &comm, const std::string &path)
Build a Logger from JSON node.
Definition: Logger.cpp:75
static CollectiveVariable * BuildCV(const Json::Value &json, const std::string &path)
Set up collective variable.
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.

Here is the call graph for this function:

void SSAGES::ResourceHandler::ConfigureHook ( class Hook hook)

Configure the Hook from given resources.

Parameters
hookPointer to specified Hook.

Definition at line 44 of file ResourceHandler.cpp.

References SSAGES::Hook::AddListener(), cvmanager_, hook_, logger_, methods_, SSAGES::Hook::SetCVManager(), SSAGES::Hook::SetSnapshot(), and snapshot_.

45  {
46  hook->SetSnapshot(snapshot_);
47  hook->SetCVManager(cvmanager_);
48  for(auto& m : methods_)
49  hook->AddListener(m);
50  if(logger_ != nullptr) hook->AddListener(logger_);
51  hook_ = hook;
52  }
class CVManager * cvmanager_
Collective variable manager.
class Logger * logger_
CV logger.
class Snapshot * snapshot_
Snapshot of system state (pointer).
class Hook * hook_
Hook pointer.
std::vector< class Method * > methods_
Vector of advanced sampling methods.

Here is the call graph for this function:

std::string SSAGES::ResourceHandler::GetInput ( ) const
inline

Returns inputs of the walker for the current processor.

Returns
Walker inputs of current processor.

Definition at line 95 of file ResourceHandler.h.

96  {
97  return inputs_[walkerid_];
98  }
std::vector< std::string > inputs_
Input file vector.
size_t walkerid_
Walker ID for specific driver.
MPI_Comm SSAGES::ResourceHandler::GetLocalComm ( ) const
inline

Returns local (walker-specific) MPI communicator.

Returns
Walker-specific MPI communicator.

Definition at line 104 of file ResourceHandler.h.

105  {
106  return comm_;
107  }
mxx::comm comm_
MPI communicator containing processors for specific walker.
const mxx::comm& SSAGES::ResourceHandler::GetLocalMxxComm ( ) const
inline

Returns local (walker-specific) mxx communicator.

Returns
Walker-specific mxx communicator.

Definition at line 122 of file ResourceHandler.h.

123  {
124  return comm_;
125  }
mxx::comm comm_
MPI communicator containing processors for specific walker.
size_t SSAGES::ResourceHandler::GetNumWalkers ( ) const
inline

Returns the total number of walkers across all processors.

Returns
Total number of walkers.

Definition at line 149 of file ResourceHandler.h.

150  {
151  return nwalkers_;
152  }
size_t nwalkers_
Number of walkers.
size_t SSAGES::ResourceHandler::GetWalkerID ( ) const
inline

Returns processor-specific walker ID.

Returns
Walker ID.

Definition at line 140 of file ResourceHandler.h.

141  {
142  return walkerid_;
143  }
size_t walkerid_
Walker ID for specific driver.
MPI_Comm SSAGES::ResourceHandler::GetWorldComm ( ) const
inline

Returns world (all processors) MPI communicator.

Returns
Overall MPI communicator.

Definition at line 113 of file ResourceHandler.h.

114  {
115  return world_;
116  }
mxx::comm world_
MPI communicator containing all processors.
const mxx::comm& SSAGES::ResourceHandler::GetWorldMxxComm ( ) const
inline

Returns world (all processors) mxx communicator.

Returns
Overall mxx communicator.

Definition at line 131 of file ResourceHandler.h.

132  {
133  return world_;
134  }
mxx::comm world_
MPI communicator containing all processors.

The documentation for this class was generated from the following files: