BioDynaMo  v1.03.58-27764645
operation.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_OPERATION_H_
16 #define CORE_OPERATION_OPERATION_H_
17 
18 #include <functional>
19 #include <set>
20 #include <string>
21 #include <vector>
22 
23 #include "core/functor.h"
24 #include "core/util/log.h"
25 
26 namespace bdm {
27 
28 class Agent;
29 
31 
32 inline std::string OpComputeTargetString(OpComputeTarget t) {
33  switch (t) {
35  return "kCpu";
37  return "kCuda";
39  return "kOpenCl";
40  default:
41  return "Invalid";
42  }
43 }
44 
45 struct OperationImpl {
46  virtual ~OperationImpl() = default;
47 
50  virtual void SetUp() {}
51 
54  virtual void TearDown() {}
55 
56  virtual void operator()(Agent *agent) = 0;
57 
58  virtual void operator()() = 0;
59 
62  virtual OperationImpl *Clone() = 0;
63 
65  bool IsGpuOperation() { return target_ == kCuda || target_ == kOpenCl; }
66 
68  virtual bool IsStandalone() = 0;
69 
72 };
73 
76  void operator()() override {
77  Log::Fatal("AgentOperationImpl",
78  "AgentOperationImpl do not support this function operator");
79  }
80 
81  bool IsStandalone() override { return false; }
82 };
83 
86  void operator()(Agent *agent) override {
87  Log::Fatal("StandaloneOperationImpl",
88  "StandaloneOperationImpl do not support this function operator");
89  }
90 
91  bool IsStandalone() override { return true; }
92 };
93 
98 struct Operation {
103  explicit Operation(const std::string &name);
104 
110  Operation(const std::string &name, uint32_t frequency);
111 
112  ~Operation();
113 
114  Operation *Clone();
115 
121  void operator()(Agent *agent);
122 
126  void operator()();
127 
133  void AddOperationImpl(OpComputeTarget target, OperationImpl *impl);
134 
136  template <typename T>
138  T *implementation = nullptr;
139  // Go over the available implementations and return the requested one
140  for (auto *imp : implementations_) {
141  if (dynamic_cast<T *>(imp)) {
142  implementation = dynamic_cast<T *>(imp);
143  }
144  }
145  return implementation;
146  }
147 
157 
164 
165  bool IsStandalone() {
166  return implementations_[active_target_]->IsStandalone();
167  }
168 
170  void SetUp();
171 
173  void TearDown();
174 
178  if (filter == nullptr) {
179  return false;
180  }
181  return exclude_filters_.find(filter) != exclude_filters_.end();
182  }
183 
187  const std::set<Functor<bool, Agent *> *> &exclude_filters) {
188  exclude_filters_ = exclude_filters;
189  }
190 
195  size_t frequency_ = 1;
197  std::string name_;
201  std::vector<OperationImpl *> implementations_;
202 
204  std::set<Functor<bool, Agent *> *> exclude_filters_;
205 };
206 
207 } // namespace bdm
208 
209 #endif // CORE_OPERATION_OPERATION_H_
bdm::Operation::IsExcluded
bool IsExcluded(Functor< bool, Agent * > *filter)
Definition: operation.h:177
bdm::StandaloneOperationImpl::operator()
void operator()(Agent *agent) override
Definition: operation.h:86
bdm::OperationImpl::IsStandalone
virtual bool IsStandalone()=0
Returns whether or not this operations is a stand-alone operation.
bdm::Operation::operator()
void operator()()
Definition: operation.cc:51
bdm
Definition: agent.cc:39
bdm::OpComputeTargetString
std::string OpComputeTargetString(OpComputeTarget t)
Definition: operation.h:32
bdm::OperationImpl
Definition: operation.h:45
bdm::Operation::exclude_filters_
std::set< Functor< bool, Agent * > * > exclude_filters_
If this is an agent operation don't run it for this list of filters.
Definition: operation.h:204
bdm::AgentOperationImpl::IsStandalone
bool IsStandalone() override
Returns whether or not this operations is a stand-alone operation.
Definition: operation.h:81
bdm::Operation::SetUp
void SetUp()
Forwards call to implementation's Setup function.
Definition: operation.cc:76
bdm::Operation::Operation
Operation(const std::string &name)
Definition: operation.cc:19
bdm::OperationImpl::Clone
virtual OperationImpl * Clone()=0
bdm::Operation::implementations_
std::vector< OperationImpl * > implementations_
The different operation implementations for each supported compute target.
Definition: operation.h:201
bdm::kCpu
@ kCpu
Definition: operation.h:30
bdm::Operation::GetImplementation
T * GetImplementation()
Returns the implementation corresponding to the template argument.
Definition: operation.h:137
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::Operation::Clone
Operation * Clone()
Definition: operation.cc:32
bdm::Operation::frequency_
size_t frequency_
Definition: operation.h:195
bdm::Functor
Definition: functor.h:24
bdm::Operation::name_
std::string name_
Operation name / unique identifier.
Definition: operation.h:197
bdm::AgentOperationImpl
Interface for implementing an operation.
Definition: operation.h:75
bdm::StandaloneOperationImpl::IsStandalone
bool IsStandalone() override
Returns whether or not this operations is a stand-alone operation.
Definition: operation.h:91
bdm::OpComputeTarget
OpComputeTarget
Definition: operation.h:30
bdm::OperationImpl::TearDown
virtual void TearDown()
Definition: operation.h:54
bdm::Operation::TearDown
void TearDown()
Forwards call to implementation's TearDown function.
Definition: operation.cc:78
bdm::Log::Fatal
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
Definition: log.h:115
bdm::kOpenCl
@ kOpenCl
Definition: operation.h:30
bdm::OperationImpl::SetUp
virtual void SetUp()
Definition: operation.h:50
log.h
bdm::Operation::IsStandalone
bool IsStandalone()
Definition: operation.h:165
bdm::Operation::~Operation
~Operation()
Definition: operation.cc:24
bdm::OperationImpl::operator()
virtual void operator()()=0
bdm::kCuda
@ kCuda
Definition: operation.h:30
bdm::Operation::IsComputeTargetSupported
bool IsComputeTargetSupported(OpComputeTarget target)
Definition: operation.cc:61
bdm::OperationImpl::IsGpuOperation
bool IsGpuOperation()
Returns whether or not this operation is supposed to run on a GPU.
Definition: operation.h:65
bdm::Operation
Definition: operation.h:98
bdm::StandaloneOperationImpl
Interface for implementing an operation that should run on a GPU.
Definition: operation.h:85
bdm::Operation::SetExcludeFilters
void SetExcludeFilters(const std::set< Functor< bool, Agent * > * > &exclude_filters)
Definition: operation.h:186
bdm::OperationImpl::target_
OpComputeTarget target_
The target that this operation implementation is supposed to run on.
Definition: operation.h:71
functor.h
bdm::Operation::SelectComputeTarget
void SelectComputeTarget(OpComputeTarget target)
Definition: operation.cc:68
bdm::Operation::active_target_
OpComputeTarget active_target_
The compute target that this operation will be executed on.
Definition: operation.h:199
bdm::AgentOperationImpl::operator()
void operator()() override
Definition: operation.h:76
bdm::OperationImpl::~OperationImpl
virtual ~OperationImpl()=default
bdm::Operation::AddOperationImpl
void AddOperationImpl(OpComputeTarget target, OperationImpl *impl)
Definition: operation.cc:53