10000 In-situ context/simulation cloning by rwcarlsen · Pull Request #1243 · cyclus/cyclus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

In-situ context/simulation cloning #1243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
33 changes: 27 additions & 6 deletions src/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,37 @@ SimInfo::SimInfo(int dur, boost::uuids::uuid parent_sim,
Context::Context(Timer* ti, Recorder* rec)
: ti_(ti),
rec_(rec),
db_(NULL),
solver_(NULL),
trans_id_(0),
si_(0) {}

Context::~Context() {
if (solver_ != NULL) {
delete solver_;
// deleting the solver prevents multiple simulations in memory from
// sharing a single solver. Sharing a solver is okay because they dont
// have inter-timestep mutable/evolving state. In the future, this
// sharing could be eliminated (and the 'delete solver_' below can be
// reinstated) by carefully rebuilding/cloning configured
// solvers.
//delete solver_;
}

// initiate deletion of agents that don't have parents.
// dealloc will propagate through hierarchy as agents delete their children
// Because agents remove themselves from the agent_list_ when destructed, we
// can't delete them while iterating over the list - so build up a to-delete
// list in a separate step.
//
// Also - previously, when a context was destructed, it would only delete the
// root-agents (agents without parents). Later on, in order to enable agents to
// deploy other agents and then leave the simulation, we changed agents to not
// destruct their children when they are destructed. So the context destructor
// needs to destruct non-root agents too.
std::vector<Agent*> to_del;
std::set<Agent*>::iterator it;
for (it = agent_list_.begin(); it != agent_list_.end(); ++it) {
if ((*it)->parent() == NULL) {
to_del.push_back(*it);
}
to_del.push_back(*it);
}

for (int i = 0; i < to_del.size(); ++i) {
DelAgent(to_del[i]);
}
Expand Down Expand Up @@ -240,10 +253,18 @@ Datum* Context::NewDatum(std::string title) {
return rec_->NewDatum(title);
}

Context* Context::GetClone() {
return ti_->SnapdContext();
}

void Context::Snapshot() {
ti_->Snapshot();
}

void Context::CloneSim() {
ti_->CloneSim();
}

void Context::KillSim() {
ti_->KillSim();
}
Expand Down
20 changes: 20 additions & 0 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ class Context {
/// the beginning of the next timestep.
void Snapshot();

/// Schedules a clone of simulation context/state for
/// the beginning of the next timestep. The clone can be retrieved by
/// calling GetClone().
void CloneSim();

/// Schedules the simulation to be terminated at the end of this timestep.
void KillSim();

Expand Down Expand Up @@ -282,6 +287,20 @@ class Context {
return n_specs_[impl];
}

inline QueryableBackend* db() {
return db_;
}

/// Flush records all buffered data to the output database.
void Flush() {
rec_->Flush();
}

// Returns a clone of the context as generated at the last call of Snapshot
// (with all agents and everything copied. This is NULL unless Snapshot has
// been called at least once previously.
Context* GetClone();

private:
/// Registers an agent as a participant in the simulation.
inline void RegisterAgent(Agent* a) {
Expand Down Expand Up @@ -310,6 +329,7 @@ class Context {
Timer* ti_;
ExchangeSolver* solver_;
Recorder* rec_;
QueryableBackend* db_;
int trans_id_;
};

Expand Down
32 changes: 19 additions & 13 deletions src/query_backend.h
< 9E88 td id="diff-0c35462efc3e00b667c6884ee8f6e7daf4a35731a26668f011424ca5d64446d7R814" data-line-number="814" class="blob-num blob-num-addition js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ enum DbTypes {
RES_MAP_STRING_PRODUCT, // ["cyclus::toolkit::ResMap<std::string, cyclus::Product>", 2, [], ["RES_MAP", "STRING", "PRODUCT"], false]
RES_MAP_VL_STRING_MATERIAL, // ["cyclus::toolkit::ResMap<std::string, cyclus::Material>", 2, [], ["RES_MAP", "VL_STRING", "MATERIAL"], false]
RES_MAP_VL_STRING_PRODUCT, // ["cyclus::toolkit::ResMap<std::string, cyclus::Product>", 2, [], ["RES_MAP", "VL_STRING", "PRODUCT"], false]

// std::vector<std::vector<int> >
VECTOR_VECTOR_INT,
VL_VECTOR_VECTOR_INT,
VECTOR_VL_VECTOR_INT,
VL_VECTOR_VL_VECTOR_INT,
};

/// Represents operation codes for condition checking.
Expand Down Expand Up @@ -660,7 +666,7 @@ class Sha1 {
for (unsigned int i = 0; i < x.size(); ++i)
hash_.process_bytes(x[i].c_str(), x[i].size());
}

inline void Update(const std::vector<cyclus::Blob>& x) {
for (unsigned int i = 0; i < x.size(); ++i)
hash_.process_bytes(x[i].str().c_str(), x[i].str().size());
Expand All @@ -677,7 +683,7 @@ class Sha1 {
for (; it != x.end(); ++it)
hash_.process_bytes(&(*it), sizeof(int));
}

inline void Update(const std::set<bool>& x) {
std::set<bool>::iterator it = x.begin();
for (; it != x.end(); ++it)
Expand All @@ -689,7 +695,7 @@ class Sha1 {
for (; it != x.end(); ++it)
hash_.process_bytes(&(*it), sizeof(double));
}

inline void Update(const std::set<float>& x) {
std::set<float>::iterator it = x.begin();
for (; it != x.end(); ++it)
Expand Down Expand Up @@ -719,19 +725,19 @@ class Sha1 {
for (; it != x.end(); ++it)
hash_.process_bytes(&(*it), sizeof(int));
}

inline void Update(const std::list<bool>& x) {
std::list<bool>::const_iterator it = x.begin();
for (; it != x.end(); ++it)
hash_.process_bytes(&(*it), sizeof(bool));
}

inline void Update(const std::list<double>& x) {
std::list<double>::const_iterator it = x.begin();
for (; it != x.end(); ++it)
hash_.process_bytes(&(*it), sizeof(double));
}

inline void Update(const std::list<float>& x) {
std::list<float>::const_iterator it = x.begin();
for (; it != x.end(); ++it)
Expand All @@ -743,7 +749,7 @@ class Sha1 {
for (; it != x.end(); ++it)
hash_.process_bytes(it->c_str(), it->size());
}

inline void Update(const std::list<cyclus::Blob>& x) {
std::list<cyclus::Blob>::const_iterator it = x.begin();
for (; it != x.end(); ++it)
Expand Down Expand Up @@ -773,7 +779,7 @@ class Sha1 {
hash_.process_bytes(&(it->second), sizeof(int));
}
}

inline void Update(const std::map<int, bool>& x) {
std::map<int, bool>::const_iterator it = x.begin();
for (; it != x.end(); ++it) {
Expand All @@ -797,15 +803,15 @@ class Sha1 {
hash_.process_bytes(&(it->second), sizeof(float));
}
}

inline void Update(const std::map<int, cyclus::Blob>& x) {
std::map<int, cyclus::Blob>::const_iterator it = x.begin();
for (; it != x.end(); ++it) {
hash_.process_bytes(&(it->first), sizeof(int));
hash_.process_bytes(it->second.str().c_str(), it->second.str().size());
}
}

inline void Update(const std::map<int, boost::uuids::uuid>& x) {
std::map<int, boost::uuids::uuid>::const_iterator it = x.begin();
for (; it != x.end(); ++it) {
Expand Down Expand Up @@ -845,23 +851,23 @@ class Sha1 {
hash_.process_bytes(&(it->second), sizeof(float));
}
}

inline void Update(const std::map<std::string, bool>& x) {
std::map<std::string, bool>::const_iterator it = x.begin();
for (; it != x.end(); ++it) {
hash_.process_bytes(it->first.c_str(), it->first.size());
hash_.process_bytes(&(it->second), sizeof(bool));
}
}

inline void Update(const std::map<std::string, cyclus::Blob>& x) {
std::map<std::string, cyclus::Blob>::const_iterator it = x.begin();
for (; it != x.end(); ++it) {
hash_.process_bytes(it->first.c_str(), it->first.size());
hash_.process_bytes(it->second.str().c_str(), it->second.str().size());
}
}

inline void Update(const std::map<std::string, boost::uuids::uuid>& x) {
std::map<std::string, boost::uuids::uuid>::const_iterator it = x.begin();
for (; it != x.end(); ++it) {
Expand Down
Loading
0