Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
abstract_task.h
Go to the documentation of this file.
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
21#ifndef HEDGEHOG_ABSTRACT_TASK_H
22#define HEDGEHOG_ABSTRACT_TASK_H
23
24#include <memory>
25#include <string>
26#include <ostream>
27#include <utility>
28
29#include "../../behavior/copyable.h"
30#include "../../behavior/cleanable.h"
31#include "../../behavior/task_node.h"
32#include "../../behavior/multi_execute.h"
33#include "../../behavior/input_output/multi_receivers.h"
34#include "../../behavior/input_output/task_multi_senders.h"
35
36#include "../../core/nodes/core_task.h"
37
39namespace hh {
40
107template<size_t Separator, class ...AllTypes>
109 : public behavior::TaskNode,
111 public behavior::Cleanable,
112 public behavior::Copyable<AbstractTask<Separator, AllTypes...>>,
113 public tool::BehaviorMultiReceiversTypeDeducer_t<tool::Inputs<Separator, AllTypes...>>,
114 public tool::BehaviorMultiExecuteTypeDeducer_t<tool::Inputs<Separator, AllTypes...>>,
115 public tool::BehaviorTaskMultiSendersTypeDeducer_t<tool::Outputs<Separator, AllTypes...>> {
116 private:
117 std::shared_ptr<hh::core::CoreTask<Separator, AllTypes...>> const coreTask_ = nullptr;
118 public:
120 AbstractTask() : AbstractTask("Task", 1, false) {};
121
130 explicit AbstractTask(std::string const &name, size_t const numberThreads = 1, bool const automaticStart = false)
131 : behavior::TaskNode(std::make_shared<core::CoreTask<Separator, AllTypes...>>(this,
132 name,
134 automaticStart)),
135 behavior::Copyable<AbstractTask<Separator, AllTypes...>>(numberThreads),
136 tool::BehaviorTaskMultiSendersTypeDeducer_t<tool::Outputs<Separator, AllTypes...>>(
137 (std::dynamic_pointer_cast<hh::core::CoreTask<Separator, AllTypes...>>(this->core()))),
138 coreTask_(std::dynamic_pointer_cast<core::CoreTask<Separator, AllTypes...>>(this->core())) {
139 if (numberThreads == 0) { throw std::runtime_error("A task needs at least one thread."); }
140 if (coreTask_ == nullptr) { throw std::runtime_error("The core used by the task should be a CoreTask."); }
141 }
142
148 : behavior::TaskNode(std::move(coreTask)),
149 behavior::Copyable<AbstractTask<Separator, AllTypes...>>(
150 std::dynamic_pointer_cast<core::CoreTask<Separator, AllTypes...>>(this->core())->numberThreads()),
151 tool::BehaviorTaskMultiSendersTypeDeducer_t<tool::Outputs<Separator, AllTypes...>>(
152 std::dynamic_pointer_cast<hh::core::CoreTask<Separator, AllTypes...>>(this->core())),
153 coreTask_(std::dynamic_pointer_cast<core::CoreTask<Separator, AllTypes...>>(this->core())) {
154 }
155
157 ~AbstractTask() override = default;
158
161 [[nodiscard]] size_t graphId() const { return coreTask_->graphId(); }
162
165 [[nodiscard]] bool canTerminate() const override {
166 return !coreTask_->hasNotifierConnected() && coreTask_->receiversEmpty();
167 }
168
169 protected:
172 std::shared_ptr<hh::core::CoreTask<Separator, AllTypes...>> const &coreTask() const { return coreTask_; }
173
176 int deviceId() { return coreTask_->deviceId(); }
177};
178}
179#endif //HEDGEHOG_ABSTRACT_TASK_H
Hedgehog main namespace.
Base node for computation.
bool canTerminate() const override
Default termination rule, it terminates if there is no predecessor connection and there is no input d...
AbstractTask()
Default constructor (Task with one thread named "Task")
AbstractTask(std::shared_ptr< hh::core::CoreTask< Separator, AllTypes... > > coreTask)
Construct a task from a user-defined core.
std::shared_ptr< hh::core::CoreTask< Separator, AllTypes... > > const & coreTask() const
Accessor to the core task.
std::shared_ptr< hh::core::CoreTask< Separator, AllTypes... > > const coreTask_
Task core.
int deviceId()
Accessor to device id linked to the task (default 0 for CPU task)
AbstractTask(std::string const &name, size_t const numberThreads=1, bool const automaticStart=false)
AbstractTask main constructor.
size_t graphId() const
Belonging graph id accessor.
~AbstractTask() override=default
Default task destructor.
Behavior abstraction for nodes that expose termination condition.
Definition: can_terminate.h:30
Cleanable interface.
Definition: cleanable.h:29
Copy interface used to copy a node when either a group of nodes is created or a node is duplicated wh...
Definition: copyable.h:37
size_t numberThreads() const
Number of threads accessor.
Definition: copyable.h:51
Copyable(size_t const numberThreads)
Copyable constructor, set the number of threads for a node.
Definition: copyable.h:44
std::string name() const
Node's name accessor.
Definition: node.h:53
std::shared_ptr< hh::core::abstraction::NodeAbstraction > const & core() const
Core accessor.
Definition: node.h:49
Behavior abstraction for TaskNode.
Definition: task_node.h:35
TaskNode(std::shared_ptr< hh::core::abstraction::TaskNodeAbstraction > core)
Constructor using a core.
Definition: task_node.h:44
Task core.
Definition: core_task.h:71