BioDynaMo  v1.05.120-25dc9790
reduction_op.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_OPERATION_REDUCTION_OP_H_
16 #define CORE_OPERATION_REDUCTION_OP_H_
17 
18 #include <array>
19 #include <vector>
20 
21 #include "core/agent/agent.h"
23 #include "core/functor.h"
26 #include "core/util/thread_info.h"
27 
28 namespace bdm {
29 
33 template <typename T>
36 
37  public:
39  tl_results_.resize(ThreadInfo::GetInstance()->GetMaxThreads());
40  }
41 
42  ~ReductionOp() override {
43  delete agent_functor_;
44  delete reduce_functor_;
45  }
46 
47  void SetUp() override {
48  for (auto& el : tl_results_) {
49  el = T();
50  }
51  }
52 
54  Functor<T, const SharedData<T>&>* reduce_functor) {
55  agent_functor_ = agent_functor;
56  reduce_functor_ = reduce_functor;
57  }
58 
59  // This operator will be called for each agent in a parallel loop
60  void operator()(Agent* agent) override {
61  auto tid = ThreadInfo::GetInstance()->GetMyThreadId();
62  (*agent_functor_)(agent, &(tl_results_[tid]));
63  }
64 
65  const std::vector<T>& GetResults() const { return results_; }
66 
67  // At the end of each timestep we collect the partial result of each thread
68  // and reduce it to one single value
69  void TearDown() override {
70  results_.push_back((*reduce_functor_)(tl_results_));
71  }
72 
73  private:
74  // One element per timestep
75  std::vector<T> results_;
76  // The thread-local (partial) results
78 
79  // The functor containing the logic on what to execute for each agent
81  // The functor containing the logic on how to reduce the partial results into
82  // a single result value of type T
84 };
85 
86 template <typename T>
87 struct SumReduction : public Functor<T, const SharedData<T>&> {
88  T operator()(const SharedData<T>& tl_results) override {
89  T result = 0;
90  for (auto& el : tl_results) {
91  // The other elements are padding to avoid false sharing
92  result += el;
93  }
94  return result;
95  }
96 };
97 
98 } // namespace bdm
99 
100 #endif // CORE_OPERATION_REDUCTION_OP_H_
bdm::ReductionOp::TearDown
void TearDown() override
Definition: reduction_op.h:69
bdm::ReductionOp::BDM_OP_HEADER
BDM_OP_HEADER(ReductionOp)
bdm::ThreadInfo::GetInstance
static ThreadInfo * GetInstance()
Definition: thread_info.cc:21
bdm::SumReduction::operator()
T operator()(const SharedData< T > &tl_results) override
Definition: reduction_op.h:88
bdm::ReductionOp::results_
std::vector< T > results_
Definition: reduction_op.h:75
bdm::SharedData
The SharedData class avoids false sharing between threads.
Definition: shared_data.h:48
bdm
Definition: agent.cc:39
bdm::ReductionOp::ReductionOp
ReductionOp()
Definition: reduction_op.h:38
operation.h
thread_info.h
bdm::ReductionOp::SetUp
void SetUp() override
Definition: reduction_op.h:47
bdm::ReductionOp::operator()
void operator()(Agent *agent) override
Definition: reduction_op.h:60
bdm::ReductionOp::Initialize
void Initialize(Functor< void, Agent *, T * > *agent_functor, Functor< T, const SharedData< T > & > *reduce_functor)
Definition: reduction_op.h:53
bdm::ReductionOp::agent_functor_
Functor< void, Agent *, T * > * agent_functor_
Definition: reduction_op.h:80
bdm::SumReduction
Definition: reduction_op.h:87
operation_registry.h
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::Functor
Definition: functor.h:24
bdm::ReductionOp::reduce_functor_
Functor< T, const SharedData< T > & > * reduce_functor_
Definition: reduction_op.h:83
bdm::AgentOperationImpl
Interface for implementing an operation.
Definition: operation.h:76
agent.h
shared_data.h
bdm::ReductionOp
Definition: reduction_op.h:34
bdm::ReductionOp::~ReductionOp
~ReductionOp() override
Definition: reduction_op.h:42
bdm::ThreadInfo::GetMyThreadId
int GetMyThreadId() const
Definition: thread_info.h:39
functor.h
bdm::ReductionOp::GetResults
const std::vector< T > & GetResults() const
Definition: reduction_op.h:65
bdm::ReductionOp::tl_results_
SharedData< T > tl_results_
Definition: reduction_op.h:77