Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
hh::AbstractTask< Separator, AllTypes > Class Template Reference

Base node for computation. More...

#include "abstract_task.h"

Inheritance diagram for hh::AbstractTask< Separator, AllTypes >:
Inheritance graph
Collaboration diagram for hh::AbstractTask< Separator, AllTypes >:
Collaboration graph

Public Member Functions

 AbstractTask ()
 Default constructor (Task with one thread named "Task")
 
 AbstractTask (std::string const &name, size_t const numberThreads=1, bool const automaticStart=false)
 AbstractTask main constructor.
 
 AbstractTask (std::shared_ptr< hh::core::CoreTask< Separator, AllTypes... > > coreTask)
 Construct a task from a user-defined core.
 
 ~AbstractTask () override=default
 Default task destructor.
 
size_t graphId () const
 Belonging graph id accessor.
 
bool canTerminate () const override
 Default termination rule, it terminates if there is no predecessor connection and there is no input data.
 
- Public Member Functions inherited from hh::behavior::TaskNode
 TaskNode (std::shared_ptr< hh::core::abstraction::TaskNodeAbstraction > core)
 Constructor using a core.
 
 ~TaskNode () override=default
 Default destructor.
 
std::shared_ptr< AbstractMemoryManager > const & memoryManager () const
 Memory manager accessor.
 
void connectMemoryManager (std::shared_ptr< AbstractMemoryManager > mm)
 Connect a memory manager to a task.
 
std::shared_ptr< ManagedMemorygetManagedMemory ()
 Get a managed memory for the memory manager attached to the task, can block if none are available at the time of the call.
 
virtual void initialize ()
 initialize step for the task
 
virtual void shutdown ()
 shutdown step for the task
 
virtual std::string extraPrintingInformation () const
 Print extra information for the task.
 
- Public Member Functions inherited from hh::behavior::Node
 Node (std::shared_ptr< hh::core::abstraction::NodeAbstraction > core)
 Constructor's node.
 
virtual ~Node ()=default
 Default destructor.
 
std::shared_ptr< hh::core::abstraction::NodeAbstraction > const & core () const
 Core accessor.
 
std::string name () const
 Node's name accessor.
 
- Public Member Functions inherited from hh::behavior::CanTerminate
 CanTerminate ()=default
 Default constructor.
 
virtual ~CanTerminate ()=default
 Default destructor.
 
virtual bool canTerminate () const =0
 Termination condition.
 
- Public Member Functions inherited from hh::behavior::Cleanable
 Cleanable ()=default
 Default constructor.
 
 ~Cleanable ()=default
 Default destructor.
 
virtual void clean ()
 Clean a node.
 
- Public Member Functions inherited from hh::behavior::Copyable< AbstractTask< Separator, AllTypes... > >
 Copyable (size_t const numberThreads)
 Copyable constructor, set the number of threads for a node.
 
virtual ~Copyable ()=default
 Default destructor.
 
size_t numberThreads () const
 Number of threads accessor.
 
std::vector< AbstractTask< Separator, AllTypes... > const * > group () const
 Get the group of nodes that hold the current nodes.
 
virtual std::shared_ptr< AbstractTask< Separator, AllTypes... > > copy ()
 Copy method called to either create a group of node or duplicate a node when an execution pipeline is created.
 

Protected Member Functions

std::shared_ptr< hh::core::CoreTask< Separator, AllTypes... > > const & coreTask () const
 Accessor to the core task.
 
int deviceId ()
 Accessor to device id linked to the task (default 0 for CPU task)
 

Private Attributes

std::shared_ptr< hh::core::CoreTask< Separator, AllTypes... > > const coreTask_ = nullptr
 Task core.
 

Detailed Description

template<size_t Separator, class ... AllTypes>
class hh::AbstractTask< Separator, AllTypes >

Base node for computation.

Hedgehog Graph's node made for processing data from the their overloaded execute method from Execute::execute.

An AbstractTask can be bound to multiple threads, forming a group of tasks. The base AbstractTask will be copied n-1 times and each of them will be bound to a thread. The AbstractTask::copy method must be overloaded to use this functionality. Also, if the AbstractTask is part of a Graph that will be duplicated with an AbstractExecutionPipeline, AbstractTask::copy method needs to be overloaded.

A MemoryManager could be linked to a task:

// Implementation of a basic Task
class StaticSizeTToManagedMemory : public hh::AbstractTask<1, size_t, StaticManagedMemory> {
public:
explicit StaticSizeTToManagedMemory(size_t numberThread = 1)
: hh::AbstractTask<1, size_t, StaticManagedMemory>("StaticManagedMemory", numberThread, false) {}
~StaticSizeTToManagedMemory() override = default;
void execute([[maybe_unused]] std::shared_ptr<size_t> data) override {
this->addResult(std::dynamic_pointer_cast<StaticManagedMemory>(this->getManagedMemory()));
}
std::shared_ptr<hh::AbstractTask<1, size_t, StaticManagedMemory>> copy() override {
return std::make_shared<StaticSizeTToManagedMemory>(this->numberThreads());
}
};
// Implementation of a managed memory
class StaticManagedMemory : public hh::ManagedMemory {
int *array_ = nullptr;
public:
explicit StaticManagedMemory(size_t const sizeAlloc) { array_ = new int[sizeAlloc]; }
~StaticManagedMemory() override { delete[] array_; }
};
// Instantiation and connection with a memory manager
auto staticTask = std::make_shared<StaticSizeTToManagedMemory>(2);
auto staticMM = std::make_shared<hh::StaticMemoryManager<StaticManagedMemory, size_t>>(2, 2);
staticTask->connectMemoryManager(staticMM);
Hedgehog main namespace.
Abstraction used to manage an user type with a memory manager.
Base node for computation.

The default order of execution is:

  1. The group is created, for each task:
  2. Threads are spawned for each instance in the group,
  3. The AbstractTask::initialize is called,
  4. The memory manager, if it exists, is bound to the task (the device Id is shared, and is initialized),
  5. Execute is called, while AbstractTask::canTerminate is True:
    • If the task is set to start automatically Execute::execute is called with nullptr,
    • If not, the task will wait for a data to come and Execute::execute is called with the received data,
  6. AbstractTask::shutdown is called, and signals to the linked nodes to wake up.

Only Execute::execute method needs to be overloaded for each AbstractTask input type.

Virtual functions
Attention
In case of a cycle in the graph AbstractTask::canTerminate needs to be overloaded or the graph will deadlock. By default, CanTerminate::canTerminate will be true if there is no "input node" connected AND no data available in the task input queue (CF tutorial 3).
Template Parameters
SeparatorSeparator position between input types and output types
AllTypesList of input and output types

Definition at line 108 of file abstract_task.h.

Constructor & Destructor Documentation

◆ AbstractTask() [1/3]

template<size_t Separator, class ... AllTypes>
hh::AbstractTask< Separator, AllTypes >::AbstractTask ( )
inline

Default constructor (Task with one thread named "Task")

Definition at line 120 of file abstract_task.h.

◆ AbstractTask() [2/3]

template<size_t Separator, class ... AllTypes>
hh::AbstractTask< Separator, AllTypes >::AbstractTask ( std::string const &  name,
size_t const  numberThreads = 1,
bool const  automaticStart = false 
)
inlineexplicit

AbstractTask main constructor.

Construct a task with a name, its number of threads (default 1) and if the task should start automatically or not (default should not start automatically)

Parameters
nameTask name
numberThreadsTask number of threads
automaticStartFlag to start the execution of the task without data (sending automatically nullptr to each of the input types)
Exceptions
std::runtime_errorthe number of threads == 0 or the core is not of the right type (do not derive from CoreTask)

Definition at line 130 of file abstract_task.h.

Here is the call graph for this function:

◆ AbstractTask() [3/3]

template<size_t Separator, class ... AllTypes>
hh::AbstractTask< Separator, AllTypes >::AbstractTask ( std::shared_ptr< hh::core::CoreTask< Separator, AllTypes... > >  coreTask)
inlineexplicit

Construct a task from a user-defined core.

A custom core can be used to customize how the task behaves internally. For example, by default any input is stored in a std::queue, it can be changed to a std::priority_queue instead through the core.

Parameters
coreTaskCustom core used to change the behavior of the task

Definition at line 147 of file abstract_task.h.

Here is the call graph for this function:

◆ ~AbstractTask()

template<size_t Separator, class ... AllTypes>
hh::AbstractTask< Separator, AllTypes >::~AbstractTask ( )
overridedefault

Default task destructor.

Member Function Documentation

◆ canTerminate()

template<size_t Separator, class ... AllTypes>
bool hh::AbstractTask< Separator, AllTypes >::canTerminate ( ) const
inlineoverridevirtual

Default termination rule, it terminates if there is no predecessor connection and there is no input data.

Returns
True if the task can terminate, else false

Implements hh::behavior::CanTerminate.

Definition at line 165 of file abstract_task.h.

◆ coreTask()

template<size_t Separator, class ... AllTypes>
std::shared_ptr< hh::core::CoreTask< Separator, AllTypes... > > const & hh::AbstractTask< Separator, AllTypes >::coreTask ( ) const
inlineprotected

Accessor to the core task.

Returns
Core task

Definition at line 172 of file abstract_task.h.

◆ deviceId()

template<size_t Separator, class ... AllTypes>
int hh::AbstractTask< Separator, AllTypes >::deviceId ( )
inlineprotected

Accessor to device id linked to the task (default 0 for CPU task)

Returns
Device id linked to the task

Definition at line 176 of file abstract_task.h.

◆ graphId()

template<size_t Separator, class ... AllTypes>
size_t hh::AbstractTask< Separator, AllTypes >::graphId ( ) const
inline

Belonging graph id accessor.

Returns
Belonging graph id

Definition at line 161 of file abstract_task.h.

Member Data Documentation

◆ coreTask_

template<size_t Separator, class ... AllTypes>
std::shared_ptr<hh::core::CoreTask<Separator, AllTypes...> > const hh::AbstractTask< Separator, AllTypes >::coreTask_ = nullptr
private

Task core.

Definition at line 117 of file abstract_task.h.