Hedgehog  0.0.0
A library to generate hybrid pipeline workflow systems
core_default_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_CORE_DEFAULT_TASK_H
21 #define HEDGEHOG_CORE_DEFAULT_TASK_H
22 
23 #include "../node/core_task.h"
24 
26 namespace hh::core {
27 
28 // Have to add -Woverloaded-virtual for clang because execute hides overloaded virtual function
29 #if defined (__clang__)
30 #pragma clang diagnostic push
31 #pragma clang diagnostic ignored "-Woverloaded-virtual"
32 #endif //__clang//
33 template<class TaskInput, class TaskOutput, class ...TaskInputs>
38 class DefaultCoreTaskExecute : public virtual CoreTask<TaskOutput, TaskInputs...> {
39  protected:
40  public:
47  DefaultCoreTaskExecute(std::string_view const &name,
48  size_t const numberThreads,
49  NodeType const type,
51  bool automaticStart) : CoreNode(name, type, numberThreads),
52  CoreNotifier(name, type, numberThreads),
53  CoreQueueSender<TaskOutput>(name, type, numberThreads),
54  CoreSlot(name, type, numberThreads),
55  CoreReceiver<TaskInputs>(name, type, numberThreads)...,
56  CoreTask<TaskOutput, TaskInputs...>(name,
57  numberThreads,
58  type,
59  task,
60  automaticStart) {}
61 
64  void callExecute(std::shared_ptr<TaskInput> data) final {
65  HLOG_SELF(2, "Call execute")
66  this->nvtxProfiler()->startRangeExecuting();
67  static_cast<behavior::Execute<TaskInput> *>(this->task())->execute(data);
68  this->nvtxProfiler()->endRangeExecuting();
69  }
70 };
71 #if defined (__clang__)
72 #pragma clang diagnostic pop
73 #endif //__clang//
74 
78 template<class TaskOutput, class ...TaskInputs>
80  : public DefaultCoreTaskExecute<TaskInputs, TaskOutput, TaskInputs...> ... {
81  public:
82  using DefaultCoreTaskExecute<TaskInputs, TaskOutput, TaskInputs...>::callExecute...;
89  CoreDefaultTask(std::string_view const &name,
90  size_t const numberThreads,
91  NodeType const type,
93  bool automaticStart) :
94  CoreNode(name, type, numberThreads),
95  CoreNotifier(name, type, numberThreads),
96  CoreQueueNotifier(name, type, numberThreads),
97  CoreQueueSender<TaskOutput>(name, type, numberThreads),
98  CoreSlot(name, type, numberThreads),
99  CoreReceiver<TaskInputs>(name, type, numberThreads)...,
100  CoreTask<TaskOutput, TaskInputs...>(name, numberThreads, type, task, automaticStart),
101  DefaultCoreTaskExecute<TaskInputs, TaskOutput, TaskInputs...>(name, numberThreads, type, task, automaticStart)...{
102  }
103 
105  virtual ~CoreDefaultTask() = default;
106 
109  std::shared_ptr<CoreNode> clone() override {
110  return this->createCopyFromThis()->core();
111  }
112 
114  void preRun() override {
115  this->nvtxProfiler()->startRangeInitializing();
116 
117  HLOG_SELF(0, "Initialize Memory manager for the task " << this->name() << " / " << this->id())
118  // Call User-defined initialize
119  this->task()->initialize();
120 
121  // Define the memory manager
122  if (this->task()->memoryManager() != nullptr) {
123  this->task()->memoryManager()->profiler(this->nvtxProfiler());
124  this->task()->memoryManager()->deviceId(this->deviceId());
125  this->task()->memoryManager()->initialize();
126  }
127 
128  this->nvtxProfiler()->endRangeInitializing();
129  }
130 
132  void postRun() override {
133  this->isActive(false);
134  this->nvtxProfiler()->startRangeShuttingDown();
135  // Call User-defined shutdown
136  this->task()->shutdown();
137  this->nvtxProfiler()->endRangeShuttingDown();
138  // Notify all linked node, the node (this) is terminated
139  this->notifyAllTerminated();
140  }
141 };
142 
143 }
144 #endif //HEDGEHOG_CORE_DEFAULT_TASK_H
NodeType type() const
Node type accessor.
Definition: core_node.h:132
virtual void shutdown()
Shutdown method called after AbstractTask::Execute loop, when AbstractTask::canTerminate evaluates to...
Receiver Interface, receive one data type from CoreSender.
Definition: core_receiver.h:44
void postRun() override
Defines what a CoreDefaultTask does after the execute loop.
Middle class used to propose a default definition of CoreExecute::callExecute for CoreDefaultTask...
Core Notifier interface, emit notification to CoreSlot.
Definition: core_notifier.h:34
virtual void initialize()
Initialize method called before AbstractTask::Execute loop.
bool isActive() const
Is active property accessor.
Definition: core_node.h:191
Core of the task node.
Definition: core_task.h:56
std::shared_ptr< AbstractTask< TaskOutput, TaskInputs... > > createCopyFromThis()
Create a copy from this instance.
Definition: core_task.h:260
Sender for nodes possessing a queue of data.
void notifyAllTerminated() override
Notify all slots that the node is terminated.
Slot interface, receive notification from CoreNotifier.
Definition: core_slot.h:34
Core of the default Task to be use.
void preRun() override
Defines what a CoreDefaultTask does before the execute loop.
NodeType
Hedgehog node&#39;s type.
Definition: core_node.h:40
Main Hedgehog core abstraction.
Definition: core_node.h:48
Notifier of CoreQueueSlot.
CoreDefaultTask(std::string_view const &name, size_t const numberThreads, NodeType const type, AbstractTask< TaskOutput, TaskInputs... > *task, bool automaticStart)
CoreDefaultTask constructor.
Hedgehog core namespace.
Definition: core_execute.h:25
std::shared_ptr< AbstractMemoryManager< TaskOutput > > const & memoryManager() const
Task&#39;s memory manager accessor.
std::string_view const & name() const
Node name accessor.
Definition: core_node.h:128
std::shared_ptr< CoreNode > clone() override
Clone overload for CoreDefaultTask.
Execute Behavior definition, node that has an execution for an Input data type.
Definition: execute.h:30
bool automaticStart() const
Automatic start property accessor.
Definition: core_task.h:98
AbstractTask< TaskOutput, TaskInputs... > * task() const
Node accessor.
Definition: core_task.h:110
size_t numberThreads() const
Number of threads associated accessor.
Definition: core_node.h:152
virtual int deviceId()
Device id accessor.
Definition: core_node.h:203
std::shared_ptr< NvtxProfiler > & nvtxProfiler()
NVTX profiler accessor.
Definition: core_task.h:102
void callExecute(std::shared_ptr< TaskInput > data) final
Definition of CoreExecute::callExecute for DefaultCoreTaskExecute.
DefaultCoreTaskExecute(std::string_view const &name, size_t const numberThreads, NodeType const type, AbstractTask< TaskOutput, TaskInputs... > *task, bool automaticStart)
DefaultCoreTaskExecute constructor.