BioDynaMo  v1.05.120-25dc9790
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 <cstdint>
19 #include <functional>
20 #include <set>
21 #include <string>
22 #include <vector>
23 
24 #include "core/functor.h"
25 #include "core/util/log.h"
26 
27 namespace bdm {
28 
29 class Agent;
30 
32 
33 inline std::string OpComputeTargetString(OpComputeTarget t) {
34  switch (t) {
36  return "kCpu";
38  return "kCuda";
40  return "kOpenCl";
41  default:
42  return "Invalid";
43  }
44 }
45 
46 struct OperationImpl {
47  virtual ~OperationImpl() = default;
48 
51  virtual void SetUp() {}
52 
55  virtual void TearDown() {}
56 
57  virtual void operator()(Agent *agent) = 0;
58 
59  virtual void operator()() = 0;
60 
63  virtual OperationImpl *Clone() = 0;
64 
66  bool IsGpuOperation() const { return target_ == kCuda || target_ == kOpenCl; }
67 
69  virtual bool IsStandalone() = 0;
70 
73 };
74 
77  void operator()() override {
78  Log::Fatal("AgentOperationImpl",
79  "AgentOperationImpl do not support this function operator");
80  }
81 
82  bool IsStandalone() override { return false; }
83 };
84 
87  void operator()(Agent *agent) override {
88  Log::Fatal("StandaloneOperationImpl",
89  "StandaloneOperationImpl do not support this function operator");
90  }
91 
92  bool IsStandalone() override { return true; }
93 };
94 
99 struct Operation {
104  explicit Operation(const std::string &name);
105 
111  Operation(const std::string &name, uint32_t frequency);
112 
113  ~Operation();
114 
115  Operation *Clone();
116 
122  void operator()(Agent *agent);
123 
127  void operator()();
128 
134  void AddOperationImpl(OpComputeTarget target, OperationImpl *impl);
135 
137  template <typename T>
139  T *implementation = nullptr;
140  // Go over the available implementations and return the requested one
141  for (auto *imp : implementations_) {
142  if (dynamic_cast<T *>(imp)) {
143  implementation = dynamic_cast<T *>(imp);
144  }
145  }
146  return implementation;
147  }
148 
158 
165 
166  bool IsStandalone() {
167  return implementations_[active_target_]->IsStandalone();
168  }
169 
171  void SetUp();
172 
174  void TearDown();
175 
179  if (filter == nullptr) {
180  return false;
181  }
182  return exclude_filters_.find(filter) != exclude_filters_.end();
183  }
184 
188  const std::set<Functor<bool, Agent *> *> &exclude_filters) {
189  exclude_filters_ = exclude_filters;
190  }
191 
196  size_t frequency_ = 1;
198  std::string name_;
202  std::vector<OperationImpl *> implementations_;
203 
205  std::set<Functor<bool, Agent *> *> exclude_filters_;
206 };
207 
208 } // namespace bdm
209 
210 #endif // CORE_OPERATION_OPERATION_H_
bdm::Operation::IsExcluded
bool IsExcluded(Functor< bool, Agent * > *filter)
Definition: operation.h:178
bdm::StandaloneOperationImpl::operator()
void operator()(Agent *agent) override
Definition: operation.h:87
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:33
bdm::OperationImpl
Definition: operation.h:46
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:205
bdm::AgentOperationImpl::IsStandalone
bool IsStandalone() override
Returns whether or not this operations is a stand-alone operation.
Definition: operation.h:82
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:202
bdm::kCpu
@ kCpu
Definition: operation.h:31
bdm::Operation::GetImplementation
T * GetImplementation()
Returns the implementation corresponding to the template argument.
Definition: operation.h:138
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:196
bdm::Functor
Definition: functor.h:24
bdm::Operation::name_
std::string name_
Operation name / unique identifier.
Definition: operation.h:198
bdm::AgentOperationImpl
Interface for implementing an operation.
Definition: operation.h:76
bdm::StandaloneOperationImpl::IsStandalone
bool IsStandalone() override
Returns whether or not this operations is a stand-alone operation.
Definition: operation.h:92
bdm::OpComputeTarget
OpComputeTarget
Definition: operation.h:31
bdm::OperationImpl::IsGpuOperation
bool IsGpuOperation() const
Returns whether or not this operation is supposed to run on a GPU.
Definition: operation.h:66
bdm::OperationImpl::TearDown
virtual void TearDown()
Definition: operation.h:55
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:31
bdm::OperationImpl::SetUp
virtual void SetUp()
Definition: operation.h:51
log.h
bdm::Operation::IsStandalone
bool IsStandalone()
Definition: operation.h:166
bdm::Operation::~Operation
~Operation()
Definition: operation.cc:24
bdm::OperationImpl::operator()
virtual void operator()()=0
bdm::kCuda
@ kCuda
Definition: operation.h:31
bdm::Operation::IsComputeTargetSupported
bool IsComputeTargetSupported(OpComputeTarget target)
Definition: operation.cc:61
bdm::Operation
Definition: operation.h:99
bdm::StandaloneOperationImpl
Interface for implementing an operation that should run on a GPU.
Definition: operation.h:86
bdm::Operation::SetExcludeFilters
void SetExcludeFilters(const std::set< Functor< bool, Agent * > * > &exclude_filters)
Definition: operation.h:187
bdm::OperationImpl::target_
OpComputeTarget target_
The target that this operation implementation is supposed to run on.
Definition: operation.h:72
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:200
bdm::AgentOperationImpl::operator()
void operator()() override
Definition: operation.h:77
bdm::OperationImpl::~OperationImpl
virtual ~OperationImpl()=default
bdm::Operation::AddOperationImpl
void AddOperationImpl(OpComputeTarget target, OperationImpl *impl)
Definition: operation.cc:53