BioDynaMo  v1.05.0-137fdb15
in_place_exec_ctxt.h
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 
15 #ifndef CORE_EXECUTION_CONTEXT_IN_PLACE_EXEC_CTXT_H_
16 #define CORE_EXECUTION_CONTEXT_IN_PLACE_EXEC_CTXT_H_
17 
18 #include <atomic>
19 #include <functional>
20 #include <memory>
21 #include <utility>
22 #include <vector>
23 
26 #include "core/agent/agent_uid.h"
30 #include "core/functor.h"
32 #include "core/util/spinlock.h"
33 #include "core/util/thread_info.h"
34 
35 namespace bdm {
36 
37 namespace in_place_exec_ctxt_detail {
38 class InPlaceExecutionContext_NeighborCacheValidity_Test;
39 }
40 
55  public:
57  using value_type = Agent*;
58  using Batch = std::vector<value_type>;
61 
62  void Insert(const AgentUid& uid, const value_type& value);
63  const value_type& operator[](const AgentUid& key);
64  uint64_t Size() const;
65  void Resize(uint64_t new_size);
66  void DeleteOldCopies();
67 
69  constexpr static uint64_t kBatchSize = 10240;
70  uint64_t num_batches_ = 0;
71  std::atomic<Batch**> batches_;
72  std::vector<Batch**> old_copies_;
73  };
74 
75  explicit InPlaceExecutionContext(
76  const std::shared_ptr<ThreadSafeAgentUidMap>& map);
77 
78  virtual ~InPlaceExecutionContext();
79 
84  void SetupIterationAll(
85  const std::vector<ExecutionContext*>& all_exec_ctxts) override;
86 
92  const std::vector<ExecutionContext*>& all_exec_ctxts) override;
93 
97  void SetupAgentOpsAll(
98  const std::vector<ExecutionContext*>& all_exec_ctxts) override;
99 
103  void TearDownAgentOpsAll(
104  const std::vector<ExecutionContext*>& all_exec_ctxts) override;
105 
108  void Execute(Agent* agent, AgentHandle ah,
109  const std::vector<Operation*>& operations) override;
110 
113  void ForEachNeighbor(Functor<void, Agent*>& lambda, const Agent& query,
114  void* criteria) override;
115 
119  const Agent& query, real_t squared_radius) override;
120 
124  const Real3& query_position,
125  real_t squared_radius) override;
126 
127  void AddAgent(Agent* new_agent) override;
128 
129  void RemoveAgent(const AgentUid& uid) override;
130 
131  Agent* GetAgent(const AgentUid& uid) override;
132 
133  const Agent* GetConstAgent(const AgentUid& uid) override;
134 
135  protected:
136  friend class Environment;
137  friend class in_place_exec_ctxt_detail::
138  InPlaceExecutionContext_NeighborCacheValidity_Test;
140  std::shared_ptr<ThreadSafeAgentUidMap> new_agent_map_;
141 
143 
145  std::vector<Agent*> new_agents_;
146 
149  std::vector<AgentUid> remove_;
150 
152  std::atomic_flag mutex_ = ATOMIC_FLAG_INIT;
153 
154  std::vector<std::pair<Agent*, real_t>> neighbor_cache_;
158  bool cache_neighbors_ = false;
159 
163  bool IsNeighborCacheValid(real_t query_squared_radius);
164 
165  virtual void AddAgentsToRm(
166  const std::vector<ExecutionContext*>& all_exec_ctxts);
167 
168  virtual void RemoveAgentsFromRm(
169  const std::vector<ExecutionContext*>& all_exec_ctxts);
170 
171  private:
173  std::vector<AgentPointer<>> critical_region_;
175  std::vector<AgentPointer<>> critical_region_2_;
176 
177  std::vector<Spinlock*> locks_;
178 };
179 
180 } // namespace bdm
181 
182 #endif // CORE_EXECUTION_CONTEXT_IN_PLACE_EXEC_CTXT_H_
bdm::InPlaceExecutionContext::new_agents_
std::vector< Agent * > new_agents_
Pointer to new agents.
Definition: in_place_exec_ctxt.h:145
bdm::InPlaceExecutionContext::remove_
std::vector< AgentUid > remove_
Definition: in_place_exec_ctxt.h:149
bdm::InPlaceExecutionContext::AddAgent
void AddAgent(Agent *new_agent) override
Definition: in_place_exec_ctxt.cc:232
spinlock.h
agent_uid_map.h
bdm::InPlaceExecutionContext::AddAgentsToRm
virtual void AddAgentsToRm(const std::vector< ExecutionContext * > &all_exec_ctxts)
Definition: in_place_exec_ctxt.cc:319
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap
Definition: in_place_exec_ctxt.h:56
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::Resize
void Resize(uint64_t new_size)
Definition: in_place_exec_ctxt.cc:83
agent_uid.h
agent_handle.h
bdm
Definition: agent.cc:39
operation.h
thread_info.h
bdm::InPlaceExecutionContext::cache_neighbors_
bool cache_neighbors_
Cache the value of Param::cache_neighbors.
Definition: in_place_exec_ctxt.h:158
execution_context.h
bdm::real_t
double real_t
Definition: real_t.h:21
bdm::ExecutionContext
Definition: execution_context.h:31
agent_pointer.h
bdm::InPlaceExecutionContext
Definition: in_place_exec_ctxt.h:54
bdm::InPlaceExecutionContext::cached_squared_search_radius_
real_t cached_squared_search_radius_
The radius that was used to cache neighbors in neighbor_cache_
Definition: in_place_exec_ctxt.h:156
bdm::InPlaceExecutionContext::tinfo_
ThreadInfo * tinfo_
Definition: in_place_exec_ctxt.h:142
bdm::InPlaceExecutionContext::mutex_
std::atomic_flag mutex_
prevent race conditions for cached Agents
Definition: in_place_exec_ctxt.h:152
bdm::InPlaceExecutionContext::Execute
void Execute(Agent *agent, AgentHandle ah, const std::vector< Operation * > &operations) override
Definition: in_place_exec_ctxt.cc:148
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::old_copies_
std::vector< Batch ** > old_copies_
Definition: in_place_exec_ctxt.h:72
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::InPlaceExecutionContext::ThreadSafeAgentUidMap::Batch
std::vector< value_type > Batch
Definition: in_place_exec_ctxt.h:58
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::DeleteOldCopies
void DeleteOldCopies()
Definition: in_place_exec_ctxt.cc:104
bdm::InPlaceExecutionContext::InPlaceExecutionContext
InPlaceExecutionContext(const std::shared_ptr< ThreadSafeAgentUidMap > &map)
Definition: in_place_exec_ctxt.cc:113
bdm::InPlaceExecutionContext::TearDownIterationAll
void TearDownIterationAll(const std::vector< ExecutionContext * > &all_exec_ctxts) override
Definition: in_place_exec_ctxt.cc:133
bdm::InPlaceExecutionContext::neighbor_cache_
std::vector< std::pair< Agent *, real_t > > neighbor_cache_
Definition: in_place_exec_ctxt.h:154
bdm::Functor< void, Agent * >
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::ThreadSafeAgentUidMap
ThreadSafeAgentUidMap()
Definition: in_place_exec_ctxt.cc:29
bdm::Spinlock
Definition: spinlock.h:22
bdm::InPlaceExecutionContext::ForEachNeighbor
void ForEachNeighbor(Functor< void, Agent * > &lambda, const Agent &query, void *criteria) override
Definition: in_place_exec_ctxt.cc:251
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::Insert
void Insert(const AgentUid &uid, const value_type &value)
Definition: in_place_exec_ctxt.cc:45
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::kBatchSize
constexpr static uint64_t kBatchSize
Definition: in_place_exec_ctxt.h:69
bdm::InPlaceExecutionContext::TearDownAgentOpsAll
void TearDownAgentOpsAll(const std::vector< ExecutionContext * > &all_exec_ctxts) override
Definition: in_place_exec_ctxt.cc:145
math_array.h
bdm::InPlaceExecutionContext::IsNeighborCacheValid
bool IsNeighborCacheValid(real_t query_squared_radius)
Definition: in_place_exec_ctxt.cc:237
bdm::InPlaceExecutionContext::~InPlaceExecutionContext
virtual ~InPlaceExecutionContext()
Definition: in_place_exec_ctxt.cc:120
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::Size
uint64_t Size() const
Definition: in_place_exec_ctxt.cc:79
bdm::InPlaceExecutionContext::RemoveAgent
void RemoveAgent(const AgentUid &uid) override
Definition: in_place_exec_ctxt.cc:315
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::~ThreadSafeAgentUidMap
~ThreadSafeAgentUidMap()
Definition: in_place_exec_ctxt.cc:34
bdm::InPlaceExecutionContext::critical_region_
std::vector< AgentPointer<> > critical_region_
Used to determine which agents must not be updated from different threads.
Definition: in_place_exec_ctxt.h:173
bdm::AgentUid
Definition: agent_uid.h:25
bdm::InPlaceExecutionContext::locks_
std::vector< Spinlock * > locks_
Definition: in_place_exec_ctxt.h:177
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::num_batches_
uint64_t num_batches_
Definition: in_place_exec_ctxt.h:70
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::batches_
std::atomic< Batch ** > batches_
Definition: in_place_exec_ctxt.h:71
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::operator[]
const value_type & operator[](const AgentUid &key)
Definition: in_place_exec_ctxt.cc:62
bdm::InPlaceExecutionContext::critical_region_2_
std::vector< AgentPointer<> > critical_region_2_
Used to determine which agents must not be updated from different threads.
Definition: in_place_exec_ctxt.h:175
bdm::MathArray< real_t, 3 >
bdm::Environment
Definition: environment.h:30
bdm::InPlaceExecutionContext::GetAgent
Agent * GetAgent(const AgentUid &uid) override
Definition: in_place_exec_ctxt.cc:300
bdm::InPlaceExecutionContext::ThreadSafeAgentUidMap::lock_
Spinlock lock_
Definition: in_place_exec_ctxt.h:68
bdm::InPlaceExecutionContext::GetConstAgent
const Agent * GetConstAgent(const AgentUid &uid) override
Definition: in_place_exec_ctxt.cc:311
bdm::InPlaceExecutionContext::RemoveAgentsFromRm
virtual void RemoveAgentsFromRm(const std::vector< ExecutionContext * > &all_exec_ctxts)
Definition: in_place_exec_ctxt.cc:356
bdm::InPlaceExecutionContext::new_agent_map_
std::shared_ptr< ThreadSafeAgentUidMap > new_agent_map_
Lookup table AgentUid -> AgentPointer for new created agents.
Definition: in_place_exec_ctxt.h:140
functor.h
bdm::ThreadInfo
This class stores information about each thread. (e.g. to which NUMA node it belongs to....
Definition: thread_info.h:31
bdm::AgentHandle
Definition: agent_handle.h:29
bdm::InPlaceExecutionContext::SetupAgentOpsAll
void SetupAgentOpsAll(const std::vector< ExecutionContext * > &all_exec_ctxts) override
Definition: in_place_exec_ctxt.cc:142