BioDynaMo  v1.05.159-3be850bb
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) const;
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  ~InPlaceExecutionContext() override;
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) const;
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_
Contains code required by all agents.
Definition: agent.h:79
void SetupAgentOpsAll(const std::vector< ExecutionContext * > &all_exec_ctxts) override
std::atomic_flag mutex_
prevent race conditions for cached Agents
std::vector< Agent * > new_agents_
Pointer to new agents.
void TearDownIterationAll(const std::vector< ExecutionContext * > &all_exec_ctxts) override
virtual void RemoveAgentsFromRm(const std::vector< ExecutionContext * > &all_exec_ctxts)
const Agent * GetConstAgent(const AgentUid &uid) override
bool IsNeighborCacheValid(real_t query_squared_radius) const
void AddAgent(Agent *new_agent) override
Adds the agent to the simulation (threadsafe, takes ownership). Note that we avoid the use of smart p...
std::vector< Spinlock * > locks_
Agent * GetAgent(const AgentUid &uid) override
std::vector< std::pair< Agent *, real_t > > neighbor_cache_
std::shared_ptr< ThreadSafeAgentUidMap > new_agent_map_
Lookup table AgentUid -> AgentPointer for new created agents.
virtual void AddAgentsToRm(const std::vector< ExecutionContext * > &all_exec_ctxts)
void RemoveAgent(const AgentUid &uid) override
bool cache_neighbors_
Cache the value of Param::cache_neighbors.
void ForEachNeighbor(Functor< void, Agent * > &lambda, const Agent &query, void *criteria) override
void TearDownAgentOpsAll(const std::vector< ExecutionContext * > &all_exec_ctxts) override
real_t cached_squared_search_radius_
The radius that was used to cache neighbors in neighbor_cache_
std::vector< AgentUid > remove_
void Execute(Agent *agent, AgentHandle ah, const std::vector< Operation * > &operations) override
void SetupIterationAll(const std::vector< ExecutionContext * > &all_exec_ctxts) override
InPlaceExecutionContext(const std::shared_ptr< ThreadSafeAgentUidMap > &map)
std::vector< AgentPointer<> > critical_region_
Used to determine which agents must not be updated from different threads.
std::vector< AgentPointer<> > critical_region_2_
Used to determine which agents must not be updated from different threads.
This class stores information about each thread. (e.g. to which NUMA node it belongs to....
Definition: thread_info.h:31
Definition: agent.cc:39
double real_t
Definition: real_t.h:21
void Insert(const AgentUid &uid, const value_type &value)
const value_type & operator[](const AgentUid &key) const