BioDynaMo  v1.05.119-a4ff3934
copy_execution_context.cc
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
16 #include "core/agent/agent.h"
17 #include "core/resource_manager.h"
18 #include "core/simulation.h"
19 
20 namespace bdm {
21 namespace experimental {
22 
23 // -----------------------------------------------------------------------------
25  auto size = sim->GetAllExecCtxts().size();
26  auto map = std::make_shared<
28  std::vector<ExecutionContext*> exec_ctxts(size);
29  auto agents = std::make_shared<std::vector<std::vector<Agent*>>>();
30 #pragma omp parallel for schedule(static, 1)
31  for (uint64_t i = 0; i < size; i++) {
32  exec_ctxts[i] = new CopyExecutionContext(map, agents);
33  }
34  sim->SetAllExecCtxts(exec_ctxts);
35 }
36 
37 // -----------------------------------------------------------------------------
39  const std::shared_ptr<ThreadSafeAgentUidMap>& map,
40  std::shared_ptr<std::vector<std::vector<Agent*>>> agents)
41  : InPlaceExecutionContext(map), agents_(agents) {
42  auto* tinfo = ThreadInfo::GetInstance();
43 #pragma omp master
44  agents_->resize(tinfo->GetNumaNodes());
45 
46  auto* param = Simulation::GetActive()->GetParam();
47  if (param->execution_order == Param::ExecutionOrder::kForEachOpForEachAgent) {
48  Log::Fatal("CopyExecutionContext",
49  "CopyExecutionContext does not support Param::execution_order = "
50  "Param::ExecutionOrder::kForEachOpForEachAgent");
51  }
52 }
53 
54 // -----------------------------------------------------------------------------
56 
57 // -----------------------------------------------------------------------------
59  const std::vector<ExecutionContext*>& all_exec_ctxts) {
61 
63  for (uint64_t n = 0; n < agents_->size(); ++n) {
64  agents_->at(n).reserve(rm->GetAgentVectorCapacity(n));
65  agents_->at(n).resize(rm->GetNumAgents(n));
66  }
67 
68  auto* scheduler = Simulation::GetActive()->GetScheduler();
69  if (scheduler->GetAgentFilters().size() != 0) {
70  Log::Fatal("CopyExecutionContext",
71  "CopyExecutionContext does not support simulations with agent "
72  "filters yet (see Scheduler::SetAgentFilters)");
73  }
74 }
75 
76 // -----------------------------------------------------------------------------
78  const std::vector<ExecutionContext*>& all_exec_ctxts) {
80 
81  auto del = L2F([](Agent* a) { delete a; });
82  rm->ForEachAgentParallel(del);
83  rm->SwapAgents(agents_.get());
84 }
85 
86 // -----------------------------------------------------------------------------
88  const std::vector<Operation*>& operations) {
89  auto* copy = agent->NewCopy();
90  InPlaceExecutionContext::Execute(copy, ah, operations);
91  assert(ah.GetNumaNode() < agents_->size());
92  assert(ah.GetElementIdx() < agents_->at(ah.GetNumaNode()).size());
93  (*agents_.get())[ah.GetNumaNode()][ah.GetElementIdx()] = copy;
94 }
95 
96 } // namespace experimental
97 } // namespace bdm
bdm::experimental::CopyExecutionContext::TearDownAgentOpsAll
void TearDownAgentOpsAll(const std::vector< ExecutionContext * > &all_exec_ctxts) override
Definition: copy_execution_context.cc:77
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap
Definition: in_place_exec_ctxt.h:56
bdm::ThreadInfo::GetInstance
static ThreadInfo * GetInstance()
Definition: thread_info.cc:21
bdm
Definition: agent.cc:39
bdm::experimental::CopyExecutionContext::Execute
void Execute(Agent *agent, AgentHandle ah, const std::vector< Operation * > &operations) override
Definition: copy_execution_context.cc:87
bdm::experimental::CopyExecutionContext::CopyExecutionContext
CopyExecutionContext(const std::shared_ptr< ThreadSafeAgentUidMap > &map, std::shared_ptr< std::vector< std::vector< Agent * >>> agents)
Definition: copy_execution_context.cc:38
bdm::InPlaceExecutionContext
Definition: in_place_exec_ctxt.h:54
bdm::InPlaceExecutionContext::Execute
void Execute(Agent *agent, AgentHandle ah, const std::vector< Operation * > &operations) override
Definition: in_place_exec_ctxt.cc:148
bdm::L2F
LambdaFunctor< decltype(&TLambda::operator())> L2F(const TLambda &l)
Definition: functor.h:110
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::InPlaceExecutionContext::SetupIterationAll
void SetupIterationAll(const std::vector< ExecutionContext * > &all_exec_ctxts) override
Definition: in_place_exec_ctxt.cc:126
bdm::Simulation::GetScheduler
Scheduler * GetScheduler()
Definition: simulation.cc:262
bdm::experimental::CopyExecutionContext::SetupIterationAll
void SetupIterationAll(const std::vector< ExecutionContext * > &all_exec_ctxts) override
Definition: copy_execution_context.cc:58
bdm::Simulation::GetResourceManager
ResourceManager * GetResourceManager()
Returns the ResourceManager instance.
Definition: simulation.cc:244
bdm::Simulation::SetAllExecCtxts
void SetAllExecCtxts(const std::vector< ExecutionContext * > &exec_ctxts)
Definition: simulation.cc:363
agent.h
bdm::Agent::NewCopy
virtual Agent * NewCopy() const =0
Create a copy of this object.
copy_execution_context.h
bdm::Log::Fatal
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
Definition: log.h:115
simulation.h
bdm::experimental::CopyExecutionContext::agents_
std::shared_ptr< std::vector< std::vector< Agent * > > > agents_
Definition: copy_execution_context.h:60
bdm::Simulation::GetParam
const Param * GetParam() const
Returns the simulation parameters.
Definition: simulation.cc:254
bdm::Simulation::GetAllExecCtxts
std::vector< ExecutionContext * > & GetAllExecCtxts()
Returns all thread local execution contexts.
Definition: simulation.cc:275
bdm::AgentHandle::GetNumaNode
NumaNode_t GetNumaNode() const
Definition: agent_handle.h:44
resource_manager.h
bdm::experimental::CopyExecutionContext::Use
static void Use(Simulation *sim)
Use the CopyExecutionContext for simulation sim.
Definition: copy_execution_context.cc:24
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68
bdm::AgentHandle::GetElementIdx
ElementIdx_t GetElementIdx() const
Definition: agent_handle.h:45
bdm::Simulation
Definition: simulation.h:50
bdm::AgentHandle
Definition: agent_handle.h:29
bdm::experimental::CopyExecutionContext::~CopyExecutionContext
~CopyExecutionContext() override