BioDynaMo
v1.05.120-25dc9790
|
Go to the documentation of this file.
15 #ifndef CORE_RESOURCE_MANAGER_H_
16 #define CORE_RESOURCE_MANAGER_H_
27 #include <unordered_map>
62 if (
agents_.size() != other.agents_.size()) {
64 "Restored ResourceManager has different number of NUMA nodes.");
69 for (
auto& numa_agents :
agents_) {
70 for (
auto* agent : numa_agents) {
74 agents_ = std::move(other.agents_);
81 for (
auto& numa_agents :
agents_) {
82 for (
auto* agent : numa_agents) {
94 uid_ah_map_.resize(agent_uid_generator->GetHighestIndex() + 1);
108 return agents_[ah.GetNumaNode()][ah.GetElementIdx()];
119 void SwapAgents(std::vector<std::vector<Agent*>>* agents);
130 "You tried to add a continuum model that was not properly "
132 "Continuum id is negative but should be positive. "
134 std::to_string(tmp));
136 auto continuum_id =
static_cast<uint64_t
>(tmp);
140 "You tried to add a continuum model with an already existing "
141 "substance id. Please choose a different substance id.");
149 size_t substance_id) {
156 delete search->second;
159 Log::Error(
"ResourceManager::RemoveContinuum",
160 "You tried to remove a continuum model that does not exist.");
171 Log::Error(
"ResourceManager::GetDiffusionGrid",
172 "You tried to get a diffusion grid but the substance id "
173 "does not correspond to a diffusion grid.");
182 return search->second;
185 "You tried to request continuum model '", continuum_id,
186 "', but it does not exist! Make sure that it's the correct id "
187 "and that the continuum model is registered.");
201 Log::Error(
"ResourceManager::GetDiffusionGrid",
202 "You tried to get a diffusion grid but the substance name "
203 "does not correspond to a diffusion grid.");
213 auto& cm = el.second;
214 if (cm->GetContinuumName() == continuum_name) {
219 "You tried to request a continuum model named '", continuum_name,
220 "', but it does not exist! Make sure that it's spelled "
221 "correctly and that the continuum model is registered.");
231 template <
typename TFunctor>
247 template <
typename TFunctor>
257 if (numa_node == -1) {
258 size_t num_agents = 0;
259 for (
auto& numa_agents :
agents_) {
260 num_agents += numa_agents.size();
264 return agents_[numa_node].size();
282 for (
auto& numa_agents :
agents_) {
283 for (
auto* agent : numa_agents) {
284 if (!filter || (filter && (*filter)(agent))) {
295 auto& numa_agents =
agents_[n];
297 auto* a = numa_agents[i];
298 if (!filter || (filter && (*filter)(a))) {
340 for (
auto& numa_agents :
agents_) {
341 numa_agents.reserve(capacity);
352 if (additional == 0) {
353 return agents_[numa_node].size();
355 auto current =
agents_[numa_node].size();
356 if (current + additional <
agents_[numa_node].size()) {
357 agents_[numa_node].reserve((current + additional) * 1.5);
359 agents_[numa_node].resize(current + additional);
375 for (
auto& numa_agents :
agents_) {
376 for (
auto* agent : numa_agents) {
400 auto uid = agent->
GetUid();
404 agents_[numa_node].push_back(agent);
407 agents_[numa_node].size() - 1u)));
417 auto new_size = highest_idx * 1.5 + 1;
434 const std::vector<Agent*>& new_agents) {
436 for (
auto* agent : new_agents) {
437 auto uid = agent->GetUid();
441 agents_[numa_node][offset + i] = agent;
446 for (
auto* agent : new_agents) {
451 if (new_agents.size() != 0) {
466 auto& numa_agents =
agents_[ah.GetNumaNode()];
467 Agent* agent =
nullptr;
468 if (ah.GetElementIdx() == numa_agents.size() - 1) {
469 agent = numa_agents.back();
470 numa_agents.pop_back();
473 agent = numa_agents[ah.GetElementIdx()];
474 auto* reordered = numa_agents.back();
475 numa_agents[ah.GetElementIdx()] = reordered;
476 numa_agents.pop_back();
490 void RemoveAgents(
const std::vector<std::vector<AgentUid>*>& uids);
502 std::vector<std::vector<Agent*>>
agents_;
530 os <<
"\033[1mAgents per numa node\033[0m" << std::endl;
532 for (
auto& numa_agents : rm.
agents_) {
533 os <<
"numa node " << cnt++ <<
" -> size: " << numa_agents.size()
541 #endif // CORE_RESOURCE_MANAGER_H_
bool ContainsAgent(const AgentUid &uid) const
virtual void EndOfIteration()
std::vector< std::vector< Agent * > > agents_lb_
Container used during load balancing.
size_t GetNumAgents(int numa_node=-1) const
static ThreadInfo * GetInstance()
virtual void LoadBalance()
Agent * GetAgent(const AgentUid &uid)
const TypeIndex * GetTypeIndex() const
void RemoveAgents(const std::vector< std::vector< AgentUid > * > &uids)
AgentHandle GetAgentHandle(const AgentUid &uid) const
DiffusionGrid * GetDiffusionGrid(size_t substance_id) const
void Reserve(size_t capacity)
ResourceManager(TRootIOCtor *r)
std::vector< std::vector< uint64_t > > to_right
void SwapAgents(std::vector< std::vector< Agent * >> *agents)
const AgentUid & GetUid() const
void AddDiffusionGrid(DiffusionGrid *dgrid)
ResourceManager & operator=(ResourceManager &&other) noexcept
void ReuseAgentUid(const AgentUid &uid)
Continuum class to interface with BioDynaMo for hybrid simulations.
DiffusionGrid * GetDiffusionGrid(const std::string &substance_name) const
ParallelRemovalAuxData parallel_remove_
auxiliary data required for parallel agent removal
virtual void ForEachAgent(const std::function< void(Agent *)> &function, Functor< bool, Agent * > *filter=nullptr)
Contains code required by all agents.
void RebuildAgentUidMap()
AgentUid::Index_t GetHighestIndex() const
Thread-safe.
void RemoveContinuum(size_t continuum_id)
virtual void AddAgents(typename AgentHandle::NumaNode_t numa_node, uint64_t offset, const std::vector< Agent * > &new_agents)
AgentUidMap< AgentHandle > uid_ah_map_
Maps an AgentUid to its storage location in agents_ .
void RemoveAgent(const AgentUid &uid)
void AddAgent(Agent *agent, typename AgentHandle::NumaNode_t numa_node=0)
Add an agent to the ResourceManager (not thread-safe). This function might invalidate agent reference...
Continuum * GetContinuum(size_t continuum_id) const
Return the continuum model which holds the substance of specified id.
std::vector< std::vector< uint64_t > > not_to_left
BDM_CLASS_DEF_NV(ResourceManager, 2)
Agent * GetAgent(AgentHandle ah)
int GetContinuumId() const
Returns the ID of the continuum.
virtual ~ResourceManager()
std::vector< std::vector< Agent * > > agents_
Pointer container for all agents.
Continuum * GetContinuum(const std::string &continuum_name) const
void Remove(Agent *agent)
void RemoveDiffusionGrid(size_t substance_id)
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
static void Error(const std::string &location, const Args &... parts)
Prints error message.
AgentUidGenerator * GetAgentUidGenerator()
uint64_t GrowAgentContainer(size_t additional, size_t numa_node)
std::unordered_map< uint64_t, Continuum * > continuum_models_
Maps a continuum ID to the pointer to the continuum models.
virtual void ForEachAgent(const std::function< void(Agent *, AgentHandle)> &function, Functor< bool, Agent * > *filter=nullptr)
void ForEachDiffusionGrid(TFunctor &&f) const
void MarkEnvironmentOutOfSync() const
std::ostream & operator<<(std::ostream &o, const MathArray< T, N > &arr)
void ForEachContinuum(TFunctor &&f) const
virtual void ForEachAgentParallel(Functor< void, Agent * > &function, Functor< bool, Agent * > *filter=nullptr)
ThreadInfo * thread_info_
NumaNode_t GetNumaNode() const
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
void AddContinuum(Continuum *cm)
ElementIdx_t GetElementIdx() const
friend std::ostream & operator<<(std::ostream &os, const ResourceManager &rm)
This class stores information about each thread. (e.g. to which NUMA node it belongs to....
void Reserve(uint64_t capacity)
size_t GetAgentVectorCapacity(int numa_node)