Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
helios::async::TaskGraph Class Reference

Represents a task dependency graph that can be executed by an Executor. More...

#include <task_graph.hpp>

Public Member Functions

 TaskGraph (std::string_view name="TaskGraph")
 Constructs a task graph with the given name.
 TaskGraph (const TaskGraph &)=delete
 TaskGraph (TaskGraph &&) noexcept=default
 ~TaskGraph ()=default
TaskGraphoperator= (const TaskGraph &)=delete
TaskGraphoperator= (TaskGraph &&) noexcept=default
void Clear ()
 Clears all tasks and dependencies from this graph.
template<typename Visitor>
requires std::invocable<Visitor, Task&>
void ForEachTask (const Visitor &visitor) const
 Applies a visitor function to each task in this graph.
template<StaticTask C>
Task EmplaceTask (C &&callable)
 Creates a static task with the given callable.
template<SubTask C>
Task EmplaceTask (C &&callable)
 Creates a dynamic task (subflow) with the given callable.
template<AnyTask... Cs>
requires (sizeof...(Cs) > 1)
auto EmplaceTasks (Cs &&... callables) -> std::array< Task, sizeof...(Cs)>
 Creates multiple tasks from a list of callables.
Task CreatePlaceholder ()
 Creates a placeholder task with no assigned work.
template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, Task>
void Linearize (const R &tasks)
 Creates linear dependencies between tasks in the given range.
template<std::ranges::range R, typename C>
requires std::invocable<C, std::ranges::range_reference_t<R>>
Task ForEach (const R &range, C &&callable)
 Creates a parallel for-each task over the given range.
template<std::integral I, typename C>
requires std::invocable<C, I>
Task ForEachIndex (I start, I end, I step, C &&callable)
 Creates a parallel for-each task over an index range.
template<std::ranges::range InputRange, std::ranges::range OutputRange, std::invocable< std::ranges::range_reference_t< InputRange > > TransformFunc>
Task Transform (const InputRange &input_range, OutputRange &output_range, TransformFunc &&transform_func)
 Creates a parallel transform task that applies a function to each element.
template<std::ranges::range R, typename T, typename BinaryOp>
requires std::invocable<BinaryOp, T, std::ranges::range_reference_t<R>>
Task Reduce (const R &range, T &init, BinaryOp &&binary_op)
 Creates a parallel reduction task that combines elements using a binary operation.
template<std::ranges::random_access_range R, typename Compare = std::less<>>
requires std::predicate<Compare, std::ranges::range_reference_t<R>, std::ranges::range_reference_t<R>>
Task Sort (R &range, Compare &&comparator=Compare{})
 Creates a parallel sort task for the given range.
void RemoveTask (const Task &task)
 Removes a task from this graph.
void RemoveDependency (const Task &from, const Task &to)
 Removes a dependency relationship between two tasks.
Task Compose (TaskGraph &other_graph)
 Creates a module task that encapsulates another task graph.
std::string Dump () const
 Dumps the task graph to a DOT format string.
void Name (std::string_view name)
 Sets the name of this task graph.
bool Empty () const
 Checks if this graph has no tasks.
size_t TaskCount () const
 Gets the number of tasks in this graph.
const std::string & Name () const
 Gets the name of task graph.

Friends

class SubTaskGraph
class Executor

Detailed Description

Represents a task dependency graph that can be executed by an Executor.

Wraps tf::Taskflow and provides methods to create tasks, manage dependencies, and build complex computational graphs. Tasks within the graph can have dependencies that determine execution order.

Note
Not thread-safe. Do not modify a graph while it's being executed.

Definition at line 36 of file task_graph.hpp.

Constructor & Destructor Documentation

◆ TaskGraph() [1/3]

helios::async::TaskGraph::TaskGraph ( std::string_view name = "TaskGraph")
inlineexplicit

Constructs a task graph with the given name.

Parameters
nameName for the task graph (default: "TaskGraph")

Definition at line 42 of file task_graph.hpp.

◆ TaskGraph() [2/3]

helios::async::TaskGraph::TaskGraph ( const TaskGraph & )
delete

◆ TaskGraph() [3/3]

helios::async::TaskGraph::TaskGraph ( TaskGraph && )
defaultnoexcept

◆ ~TaskGraph()

helios::async::TaskGraph::~TaskGraph ( )
default

Member Function Documentation

◆ Clear()

void helios::async::TaskGraph::Clear ( )
inline

Clears all tasks and dependencies from this graph.

Definition at line 51 of file task_graph.hpp.

◆ Compose()

Task helios::async::TaskGraph::Compose ( TaskGraph & other_graph)
inline

Creates a module task that encapsulates another task graph.

Parameters
other_graphTask graph to compose into this graph
Returns
Task handle representing the composed graph

Definition at line 217 of file task_graph.hpp.

◆ CreatePlaceholder()

Task helios::async::TaskGraph::CreatePlaceholder ( )
inline

Creates a placeholder task with no assigned work.

Returns
Task handle that can later be assigned work

Definition at line 99 of file task_graph.hpp.

◆ Dump()

std::string helios::async::TaskGraph::Dump ( ) const
inlinenodiscard

Dumps the task graph to a DOT format string.

Returns
String representation of the graph in DOT format

Definition at line 225 of file task_graph.hpp.

◆ EmplaceTask() [1/2]

template<SubTask C>
Task helios::async::TaskGraph::EmplaceTask ( C && callable)

Creates a dynamic task (subflow) with the given callable.

Requires sub_task_graph.hpp to be included.

Template Parameters
CCallable type that accepts a SubTaskGraph reference
Parameters
callableFunction to execute when the task runs
Returns
Task handle for the created task

◆ EmplaceTask() [2/2]

template<SubTask C>
Task helios::async::TaskGraph::EmplaceTask ( C && callable)
inline

Creates a static task with the given callable.

Template Parameters
CCallable type
Parameters
callableFunction to execute when the task runs
Returns
Task handle for the created task

Definition at line 69 of file task_graph.hpp.

◆ EmplaceTasks()

template<AnyTask... Cs>
requires (sizeof...(Cs) > 1)
auto helios::async::TaskGraph::EmplaceTasks ( Cs &&... callables) -> std::array< Task, sizeof...(Cs)>
inline

Creates multiple tasks from a list of callables.

Template Parameters
CsCallable types
Parameters
callablesFunctions to execute when the tasks run
Returns
Array of task handles for the created tasks

Definition at line 91 of file task_graph.hpp.

◆ Empty()

bool helios::async::TaskGraph::Empty ( ) const
inlinenodiscard

Checks if this graph has no tasks.

Returns
True if the graph is empty, false otherwise

Definition at line 238 of file task_graph.hpp.

◆ ForEach()

template<std::ranges::range R, typename C>
requires std::invocable<C, std::ranges::range_reference_t<R>>
Task helios::async::TaskGraph::ForEach ( const R & range,
C && callable )
inline

Creates a parallel for-each task over the given range.

Template Parameters
RRange type
CCallable type
Parameters
rangeInput range to iterate over
callableFunction to apply to each element
Returns
Task handle for the parallel operation

Definition at line 120 of file task_graph.hpp.

◆ ForEachIndex()

template<std::integral I, typename C>
requires std::invocable<C, I>
Task helios::async::TaskGraph::ForEachIndex ( I start,
I end,
I step,
C && callable )
inline

Creates a parallel for-each task over an index range.

Template Parameters
IIntegral type
CCallable type
Parameters
startStarting index (inclusive)
endEnding index (exclusive)
stepStep size
callableFunction to apply to each index
Returns
Task handle for the parallel operation

Definition at line 138 of file task_graph.hpp.

◆ ForEachTask()

template<typename Visitor>
requires std::invocable<Visitor, Task&>
void helios::async::TaskGraph::ForEachTask ( const Visitor & visitor) const
inline

Applies a visitor function to each task in this graph.

Template Parameters
VisitorCallable type that accepts a Task reference
Parameters
visitorFunction to call for each task

Definition at line 269 of file task_graph.hpp.

◆ Linearize()

template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, Task>
void helios::async::TaskGraph::Linearize ( const R & tasks)
inline

Creates linear dependencies between tasks in the given range.

Template Parameters
RRange type containing Task objects
Parameters
tasksRange of tasks to linearize (first->second->third->...)

Definition at line 279 of file task_graph.hpp.

◆ Name() [1/2]

const std::string & helios::async::TaskGraph::Name ( ) const
inlinenodiscard

Gets the name of task graph.

Returns
Name of the graph

Definition at line 250 of file task_graph.hpp.

◆ Name() [2/2]

void helios::async::TaskGraph::Name ( std::string_view name)
inline

Sets the name of this task graph.

Warning
Triggers an assertion if the name is empty.
Parameters
nameName for the graph (must not be empty)

Definition at line 308 of file task_graph.hpp.

◆ operator=() [1/2]

TaskGraph & helios::async::TaskGraph::operator= ( const TaskGraph & )
delete

◆ operator=() [2/2]

TaskGraph & helios::async::TaskGraph::operator= ( TaskGraph && )
defaultnoexcept

◆ Reduce()

template<std::ranges::range R, typename T, typename BinaryOp>
requires std::invocable<BinaryOp, T, std::ranges::range_reference_t<R>>
Task helios::async::TaskGraph::Reduce ( const R & range,
T & init,
BinaryOp && binary_op )
inline

Creates a parallel reduction task that combines elements using a binary operation.

Template Parameters
RRange type
TResult type
BinaryOpBinary operation type
Parameters
rangeRange of elements to reduce
initInitial value and storage for the result
binary_opBinary function to combine elements
Returns
Task handle for the parallel operation

Definition at line 178 of file task_graph.hpp.

◆ RemoveDependency()

void helios::async::TaskGraph::RemoveDependency ( const Task & from,
const Task & to )
inline

Removes a dependency relationship between two tasks.

Parameters
fromSource task (dependent)
toTarget task (successor)

Definition at line 208 of file task_graph.hpp.

◆ RemoveTask()

void helios::async::TaskGraph::RemoveTask ( const Task & task)
inline

Removes a task from this graph.

Parameters
taskTask to remove

Definition at line 201 of file task_graph.hpp.

◆ Sort()

template<std::ranges::random_access_range R, typename Compare>
requires std::predicate<Compare, std::ranges::range_reference_t<R>, std::ranges::range_reference_t<R>>
Task helios::async::TaskGraph::Sort ( R & range,
Compare && comparator = Compare{} )
inline

Creates a parallel sort task for the given range.

Template Parameters
RRandom access range type
CompareComparator type
Parameters
rangeRange of elements to sort
comparatorComparison function (default: std::less<>)
Returns
Task handle for the parallel operation

Definition at line 297 of file task_graph.hpp.

◆ TaskCount()

size_t helios::async::TaskGraph::TaskCount ( ) const
inlinenodiscard

Gets the number of tasks in this graph.

Returns
Count of tasks

Definition at line 244 of file task_graph.hpp.

◆ Transform()

template<std::ranges::range InputRange, std::ranges::range OutputRange, std::invocable< std::ranges::range_reference_t< InputRange > > TransformFunc>
Task helios::async::TaskGraph::Transform ( const InputRange & input_range,
OutputRange & output_range,
TransformFunc && transform_func )
inline

Creates a parallel transform task that applies a function to each element.

Template Parameters
InputRangeInput range type
OutputRangeOutput range type
TransformFuncTransform function type
Parameters
input_rangeRange of input elements
output_rangeRange to store transformed results
transform_funcFunction to apply to each input element
Returns
Task handle for the parallel operation

Definition at line 157 of file task_graph.hpp.

◆ Executor

friend class Executor
friend

Definition at line 264 of file task_graph.hpp.

◆ SubTaskGraph

friend class SubTaskGraph
friend

Definition at line 263 of file task_graph.hpp.