BioDynaMo  v1.05.119-a4ff3934
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | Friends | List of all members
bdm::InPlaceExecutionContext Class Reference

#include <in_place_exec_ctxt.h>

Inheritance diagram for bdm::InPlaceExecutionContext:
[legend]
Collaboration diagram for bdm::InPlaceExecutionContext:
[legend]

Classes

struct  ThreadSafeAgentUidMap
 

Public Member Functions

 InPlaceExecutionContext (const std::shared_ptr< ThreadSafeAgentUidMap > &map)
 
 ~InPlaceExecutionContext () override
 
void SetupIterationAll (const std::vector< ExecutionContext * > &all_exec_ctxts) override
 
void TearDownIterationAll (const std::vector< ExecutionContext * > &all_exec_ctxts) override
 
void SetupAgentOpsAll (const std::vector< ExecutionContext * > &all_exec_ctxts) override
 
void TearDownAgentOpsAll (const std::vector< ExecutionContext * > &all_exec_ctxts) override
 
void Execute (Agent *agent, AgentHandle ah, const std::vector< Operation * > &operations) override
 
void ForEachNeighbor (Functor< void, Agent * > &lambda, const Agent &query, void *criteria) override
 
void ForEachNeighbor (Functor< void, Agent *, real_t > &lambda, const Agent &query, real_t squared_radius) override
 
void ForEachNeighbor (Functor< void, Agent *, real_t > &lambda, const Real3 &query_position, real_t squared_radius) override
 
void AddAgent (Agent *new_agent) override
 Adds the agent to the simulation (threadsafe, takes ownership). Note that we avoid the use of smart pointers for the agents to avoid unnecessary overhead during construction of the agent (performance argument). usage example: More...
 
void RemoveAgent (const AgentUid &uid) override
 
AgentGetAgent (const AgentUid &uid) override
 
const AgentGetConstAgent (const AgentUid &uid) override
 
- Public Member Functions inherited from bdm::ExecutionContext
virtual ~ExecutionContext ()=default
 

Protected Member Functions

bool IsNeighborCacheValid (real_t query_squared_radius) const
 
virtual void AddAgentsToRm (const std::vector< ExecutionContext * > &all_exec_ctxts)
 
virtual void RemoveAgentsFromRm (const std::vector< ExecutionContext * > &all_exec_ctxts)
 

Protected Attributes

std::shared_ptr< ThreadSafeAgentUidMapnew_agent_map_
 Lookup table AgentUid -> AgentPointer for new created agents. More...
 
ThreadInfotinfo_
 
std::vector< Agent * > new_agents_
 Pointer to new agents. More...
 
std::vector< AgentUidremove_
 
std::atomic_flag mutex_ = ATOMIC_FLAG_INIT
 prevent race conditions for cached Agents More...
 
std::vector< std::pair< Agent *, real_t > > neighbor_cache_
 
real_t cached_squared_search_radius_ = 0.0
 The radius that was used to cache neighbors in neighbor_cache_ More...
 
bool cache_neighbors_ = false
 Cache the value of Param::cache_neighbors. More...
 

Private Attributes

std::vector< AgentPointer<> > critical_region_
 Used to determine which agents must not be updated from different threads. More...
 
std::vector< AgentPointer<> > critical_region_2_
 Used to determine which agents must not be updated from different threads. More...
 
std::vector< Spinlock * > locks_
 

Friends

class Environment
 
class in_place_exec_ctxt_detail::InPlaceExecutionContext_NeighborCacheValidity_Test
 

Detailed Description

This execution context updates agents in place.
Let's assume we have two agents A, B in our simulation that we want to update to the next timestep A*, B*. If we have one thread it will first update A and afterwards B and write the updates directly to the same data structure. Therefore, before we start updating B the array looks like this: A*, B. B already observes the updated A.
Operations in method Execute are executed in order given by the user. Subsequent operations observe the changes of earlier operations.
In-place updates can lead to race conditions if agents not only modify themselves, but also neighbors. Therefore, a protection mechanism has been added.

See also
Param::thread_safety_mechanism New agents will only be visible at the next iteration.
Also removal of an agent happens at the end of each iteration.

Definition at line 54 of file in_place_exec_ctxt.h.

Constructor & Destructor Documentation

◆ InPlaceExecutionContext()

bdm::InPlaceExecutionContext::InPlaceExecutionContext ( const std::shared_ptr< ThreadSafeAgentUidMap > &  map)
explicit

Definition at line 113 of file in_place_exec_ctxt.cc.

◆ ~InPlaceExecutionContext()

bdm::InPlaceExecutionContext::~InPlaceExecutionContext ( )
override

Definition at line 120 of file in_place_exec_ctxt.cc.

Member Function Documentation

◆ AddAgent()

void bdm::InPlaceExecutionContext::AddAgent ( Agent new_agent)
overridevirtual

Adds the agent to the simulation (threadsafe, takes ownership). Note that we avoid the use of smart pointers for the agents to avoid unnecessary overhead during construction of the agent (performance argument). usage example:

ctxt->AddAgent(new Agent());

or

auto* agent = new Agent();
// modify agent
ctxt->AddAgent(agent);
Parameters
new_agentThe agent to be added to the simulation

Implements bdm::ExecutionContext.

Definition at line 232 of file in_place_exec_ctxt.cc.

◆ AddAgentsToRm()

void bdm::InPlaceExecutionContext::AddAgentsToRm ( const std::vector< ExecutionContext * > &  all_exec_ctxts)
protectedvirtual

Definition at line 319 of file in_place_exec_ctxt.cc.

◆ Execute()

void bdm::InPlaceExecutionContext::Execute ( Agent agent,
AgentHandle  ah,
const std::vector< Operation * > &  operations 
)
overridevirtual

Execute a series of operations on an agent in the order given in the argument

Implements bdm::ExecutionContext.

Definition at line 148 of file in_place_exec_ctxt.cc.

◆ ForEachNeighbor() [1/3]

void bdm::InPlaceExecutionContext::ForEachNeighbor ( Functor< void, Agent * > &  lambda,
const Agent query,
void *  criteria 
)
overridevirtual

Applies the lambda lambda for each neighbor of the given query agent within the given criteria. Does not support caching.

Implements bdm::ExecutionContext.

Definition at line 251 of file in_place_exec_ctxt.cc.

◆ ForEachNeighbor() [2/3]

void bdm::InPlaceExecutionContext::ForEachNeighbor ( Functor< void, Agent *, real_t > &  lambda,
const Agent query,
real_t  squared_radius 
)
overridevirtual

Applies the lambda lambda for each neighbor of the given query agent within the given search radius squared_radius

Implements bdm::ExecutionContext.

Definition at line 260 of file in_place_exec_ctxt.cc.

◆ ForEachNeighbor() [3/3]

void bdm::InPlaceExecutionContext::ForEachNeighbor ( Functor< void, Agent *, real_t > &  lambda,
const Real3 query_position,
real_t  squared_radius 
)
overridevirtual

Applies the lambda lambda for each neighbor of the given query_position within the given search radius sqrt(squared_radius)

Implements bdm::ExecutionContext.

Definition at line 290 of file in_place_exec_ctxt.cc.

◆ GetAgent()

Agent * bdm::InPlaceExecutionContext::GetAgent ( const AgentUid uid)
overridevirtual

Implements bdm::ExecutionContext.

Definition at line 300 of file in_place_exec_ctxt.cc.

◆ GetConstAgent()

const Agent * bdm::InPlaceExecutionContext::GetConstAgent ( const AgentUid uid)
overridevirtual

Implements bdm::ExecutionContext.

Definition at line 311 of file in_place_exec_ctxt.cc.

◆ IsNeighborCacheValid()

bool bdm::InPlaceExecutionContext::IsNeighborCacheValid ( real_t  query_squared_radius) const
protected

Check whether or not the neighbors in neighbor_cache_ were queried with the same squared radius (cached_squared_search_radius_) as currently being queried with (query_squared_radius_)

Definition at line 237 of file in_place_exec_ctxt.cc.

◆ RemoveAgent()

void bdm::InPlaceExecutionContext::RemoveAgent ( const AgentUid uid)
overridevirtual

Implements bdm::ExecutionContext.

Definition at line 315 of file in_place_exec_ctxt.cc.

◆ RemoveAgentsFromRm()

void bdm::InPlaceExecutionContext::RemoveAgentsFromRm ( const std::vector< ExecutionContext * > &  all_exec_ctxts)
protectedvirtual

Definition at line 356 of file in_place_exec_ctxt.cc.

◆ SetupAgentOpsAll()

void bdm::InPlaceExecutionContext::SetupAgentOpsAll ( const std::vector< ExecutionContext * > &  all_exec_ctxts)
overridevirtual

This function is called before all agent operations are executed.
This function is not thread-safe. NB: Invalidates references and pointers to agents.

Implements bdm::ExecutionContext.

Definition at line 142 of file in_place_exec_ctxt.cc.

◆ SetupIterationAll()

void bdm::InPlaceExecutionContext::SetupIterationAll ( const std::vector< ExecutionContext * > &  all_exec_ctxts)
overridevirtual

This function is called at the beginning of each iteration to setup all execution contexts. This function is not thread-safe. NB: Invalidates references and pointers to agents.

Implements bdm::ExecutionContext.

Definition at line 126 of file in_place_exec_ctxt.cc.

◆ TearDownAgentOpsAll()

void bdm::InPlaceExecutionContext::TearDownAgentOpsAll ( const std::vector< ExecutionContext * > &  all_exec_ctxts)
overridevirtual

This function is called after all agent operations were executed.
This function is not thread-safe.
NB: Invalidates references and pointers to agents.

Implements bdm::ExecutionContext.

Definition at line 145 of file in_place_exec_ctxt.cc.

◆ TearDownIterationAll()

void bdm::InPlaceExecutionContext::TearDownIterationAll ( const std::vector< ExecutionContext * > &  all_exec_ctxts)
overridevirtual

This function is called at the end of each iteration to tear down all execution contexts. This function is not thread-safe.
NB: Invalidates references and pointers to agents.

Implements bdm::ExecutionContext.

Definition at line 133 of file in_place_exec_ctxt.cc.

Friends And Related Function Documentation

◆ Environment

friend class Environment
friend

Definition at line 136 of file in_place_exec_ctxt.h.

◆ in_place_exec_ctxt_detail::InPlaceExecutionContext_NeighborCacheValidity_Test

friend class in_place_exec_ctxt_detail:: InPlaceExecutionContext_NeighborCacheValidity_Test
friend

Definition at line 138 of file in_place_exec_ctxt.h.

Member Data Documentation

◆ cache_neighbors_

bool bdm::InPlaceExecutionContext::cache_neighbors_ = false
protected

Cache the value of Param::cache_neighbors.

Definition at line 158 of file in_place_exec_ctxt.h.

◆ cached_squared_search_radius_

real_t bdm::InPlaceExecutionContext::cached_squared_search_radius_ = 0.0
protected

The radius that was used to cache neighbors in neighbor_cache_

Definition at line 156 of file in_place_exec_ctxt.h.

◆ critical_region_

std::vector<AgentPointer<> > bdm::InPlaceExecutionContext::critical_region_
private

Used to determine which agents must not be updated from different threads.

Definition at line 173 of file in_place_exec_ctxt.h.

◆ critical_region_2_

std::vector<AgentPointer<> > bdm::InPlaceExecutionContext::critical_region_2_
private

Used to determine which agents must not be updated from different threads.

Definition at line 175 of file in_place_exec_ctxt.h.

◆ locks_

std::vector<Spinlock*> bdm::InPlaceExecutionContext::locks_
private

Definition at line 177 of file in_place_exec_ctxt.h.

◆ mutex_

std::atomic_flag bdm::InPlaceExecutionContext::mutex_ = ATOMIC_FLAG_INIT
protected

prevent race conditions for cached Agents

Definition at line 152 of file in_place_exec_ctxt.h.

◆ neighbor_cache_

std::vector<std::pair<Agent*, real_t> > bdm::InPlaceExecutionContext::neighbor_cache_
protected

Definition at line 154 of file in_place_exec_ctxt.h.

◆ new_agent_map_

std::shared_ptr<ThreadSafeAgentUidMap> bdm::InPlaceExecutionContext::new_agent_map_
protected

Lookup table AgentUid -> AgentPointer for new created agents.

Definition at line 140 of file in_place_exec_ctxt.h.

◆ new_agents_

std::vector<Agent*> bdm::InPlaceExecutionContext::new_agents_
protected

Pointer to new agents.

Definition at line 145 of file in_place_exec_ctxt.h.

◆ remove_

std::vector<AgentUid> bdm::InPlaceExecutionContext::remove_
protected

Contains unique ids of agents that will be removed at the end of each iteration. AgentUids are separated by numa node.

Definition at line 149 of file in_place_exec_ctxt.h.

◆ tinfo_

ThreadInfo* bdm::InPlaceExecutionContext::tinfo_
protected

Definition at line 142 of file in_place_exec_ctxt.h.


The documentation for this class was generated from the following files:
bdm::ExecutionContext::AddAgent
virtual void AddAgent(Agent *new_agent)=0
Adds the agent to the simulation (threadsafe, takes ownership). Note that we avoid the use of smart p...
bdm::Simulation::GetExecutionContext
ExecutionContext * GetExecutionContext()
Returns a thread local execution context.
Definition: simulation.cc:271
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:68