Hedgehog  0.0.0
A library to generate hybrid pipeline workflow systems
abstract_task.h
1 // NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
2 // software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
3 // derivative works of the software or any portion of the software, and you may copy and distribute such modifications
4 // or works. Modified works should carry a notice stating that you changed the software and should note the date and
5 // nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
6 // source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
7 // EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
8 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
9 // WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
10 // CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
11 // THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
12 // are solely responsible for determining the appropriateness of using and distributing the software and you assume
13 // all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
14 // with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
15 // operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
16 // damage to property. The software developed by NIST employees is not subject to copyright protection within the
17 // United States.
18 
19 
20 #ifndef HEDGEHOG_TASK_H
21 #define HEDGEHOG_TASK_H
22 
23 #include "../behavior/io/multi_receivers.h"
24 #include "../behavior/io/sender.h"
25 #include "../behavior/execute.h"
26 #include "memory_manager/abstract_memory_manager.h"
27 #include "../core/node/core_node.h"
28 #include "../core/defaults/core_default_task.h"
29 
31 namespace hh {
75 template<class TaskOutput, class ...TaskInputs>
76 class AbstractTask :
77  public behavior::MultiReceivers<TaskInputs...>,
78  public behavior::Sender<TaskOutput>,
79  public virtual behavior::Node,
80  public behavior::Execute<TaskInputs> ... {
81 
82  static_assert(traits::isUnique<TaskInputs...>, "A Task can't accept multiple inputs with the same type.");
83  static_assert(sizeof... (TaskInputs) >= 1, "A node need to have one output type and at least one output type.");
84 
85  std::shared_ptr<core::CoreTask<TaskOutput, TaskInputs...>> taskCore_ = nullptr;
86  std::shared_ptr<AbstractMemoryManager<TaskOutput>> mm_ = nullptr;
87 
88  public:
93  taskCore_ = std::make_shared<core::CoreDefaultTask<TaskOutput, TaskInputs...>>("Task",
94  1,
95  core::NodeType::Task,
96  this,
97  false);
98  }
99 
103  explicit AbstractTask(std::string_view const &name) {
104  taskCore_ =
105  std::make_shared<core::CoreDefaultTask<TaskOutput, TaskInputs...>>(name, 1, core::NodeType::Task, this, false);
106  }
107 
111  explicit AbstractTask(std::string_view const &name, size_t numberThreads) {
112  taskCore_ =
113  std::make_shared<core::CoreDefaultTask<TaskOutput, TaskInputs...>>(name,
115  core::NodeType::Task,
116  this,
117  false);
118  }
119 
124  explicit AbstractTask(std::string_view const &name, size_t numberThreads, bool automaticStart) {
125  taskCore_ =
126  std::make_shared<core::CoreDefaultTask<TaskOutput, TaskInputs...>>
127  (name, numberThreads, core::NodeType::Task, this, automaticStart);
128  }
129 
136  AbstractTask(std::string_view const name, size_t numberThreads, core::NodeType nodeType, bool automaticStart) {
137  taskCore_ = std::make_shared<core::CoreDefaultTask<TaskOutput, TaskInputs...>>
139  }
140 
144  taskCore_ = std::make_shared<core::CoreDefaultTask<TaskOutput, TaskInputs...>>
145  (rhs->name(),
146  rhs->numberThreads(),
147  rhs->nodeType(),
148  this,
149  rhs->automaticStart());
150  }
151 
153  virtual ~AbstractTask() = default;
154 
157  void addResult(std::shared_ptr<TaskOutput> output) { this->taskCore_->sendAndNotify(output); }
158 
161  std::string_view name() { return this->taskCore_->name(); }
162 
165  size_t numberThreads() { return this->taskCore_->numberThreads(); }
166 
169  bool automaticStart() { return this->taskCore_->automaticStart(); }
170 
173  core::NodeType nodeType() { return this->taskCore_->type(); }
174 
177  int deviceId() { return this->taskCore_->deviceId(); }
178 
181  int graphId() { return this->taskCore_->graphId(); }
182 
185  std::shared_ptr<core::CoreNode> core() final { return taskCore_; }
186 
189  std::shared_ptr<AbstractMemoryManager<TaskOutput>> const & memoryManager() const { return mm_; }
190 
193  virtual std::shared_ptr<AbstractTask<TaskOutput, TaskInputs...>> copy() { return nullptr; }
194 
196  virtual void initialize() {}
197 
199  virtual void shutdown() {}
200 
204  static_assert(
205  traits::is_managed_memory_v<TaskOutput>,
206  "The type given to the memory manager should inherit \"MemoryData\", and be default constructible!");
207  mm_ = mm;
208  }
209 
212  std::shared_ptr<TaskOutput> getManagedMemory() {
213  if (mm_ == nullptr) {
214  std::cerr
215  << "For the task:\"" << this->name()
216  << "\"To get managed memory, you need first to connect a memory manager to the task via "
217  "\"connectMemoryManager()\""
218  << std::endl;
219  exit(42);
220  }
221 
222  auto start = std::chrono::high_resolution_clock::now();
223  this->taskCore_->nvtxProfiler()->startRangeWaitingForMemory();
224  auto data = mm_->getManagedMemory();
225  this->taskCore_->nvtxProfiler()->endRangeWaitingForMem();
226  auto finish = std::chrono::high_resolution_clock::now();
227 
228  this->core()->incrementWaitForMemoryDuration(std::chrono::duration_cast<std::chrono::microseconds>(finish - start));
229 
230  return data;
231  }
232 
236  bool canTerminate() override { return !this->taskCore_->hasNotifierConnected() && this->taskCore_->receiversEmpty(); }
237 };
238 }
239 #endif //HEDGEHOG_TASK_H
virtual void shutdown()
Shutdown method called after AbstractTask::Execute loop, when AbstractTask::canTerminate evaluates to...
Base node for computation.
Definition: abstract_task.h:76
AbstractTask(std::string_view const name, size_t numberThreads, core::NodeType nodeType, bool automaticStart)
Internal constructor needed to create nodes that derive from AbstractTask.
AbstractTask()
AbstractTask default constructor.
Definition: abstract_task.h:92
int deviceId()
Task&#39;s device ID accessor.
virtual void initialize()
Initialize method called before AbstractTask::Execute loop.
core::NodeType nodeType()
Task&#39;s node type accessor.
AbstractTask(std::string_view const &name, size_t numberThreads, bool automaticStart)
Create a task with a custom name, the task&#39;s cluster size, and the automatic start.
void addResult(std::shared_ptr< TaskOutput > output)
Add an output data.
AbstractTask(AbstractTask< TaskOutput, TaskInputs ... > *rhs)
Copy constructor.
Hedgehog main namespace.
Core of the task node.
Definition: core_task.h:56
Sender Behavior definition, node has an output type.
Definition: sender.h:32
Abstract interface for Hedgehog&#39;s Memory manager.
bool canTerminate() override
Method called to test the task;s termination, by default, the test is: no input nodes connected and...
std::shared_ptr< core::CoreNode > core() final
Task&#39;s core accessor.
void connectMemoryManager(std::shared_ptr< AbstractMemoryManager< TaskOutput >> mm)
Connect a memory manager to the task.
Node Behavior definition.
Definition: node.h:39
Core of the default Task to be use.
int graphId()
Task&#39;s graph ID accessor.
std::shared_ptr< AbstractMemoryManager< TaskOutput > > mm_
Task&#39;s memory manager.
Definition: abstract_task.h:86
virtual std::shared_ptr< AbstractTask< TaskOutput, TaskInputs... > > copy()
Default copy overload, fail if cluster need to be copied.
NodeType
Hedgehog node&#39;s type.
Definition: core_node.h:40
MultiReceivers Behavior definition, node has a list of input types.
std::shared_ptr< TaskOutput > getManagedMemory()
Memory manager accessor.
AbstractTask(std::string_view const &name)
Create a task with a custom name.
virtual ~AbstractTask()=default
Default destructor.
std::shared_ptr< AbstractMemoryManager< TaskOutput > > const & memoryManager() const
Task&#39;s memory manager accessor.
std::shared_ptr< core::CoreTask< TaskOutput, TaskInputs... > > taskCore_
Task Core.
Definition: abstract_task.h:85
Execute Behavior definition, node that has an execution for an Input data type.
Definition: execute.h:30
AbstractTask(std::string_view const &name, size_t numberThreads)
Create a task with a custom name and the task&#39;s cluster size.
std::string_view name()
Task&#39;s name accessor.
bool automaticStart()
Task&#39;s automatic start accessor.
size_t numberThreads()
Task&#39;s number of threads accessor.