Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
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 ()
 
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.

42{ Name(name); }
const std::string & Name() const
Gets the name of task graph.

◆ 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

Definition at line 53 of file task_graph.hpp.

53{ taskflow_.clear(); }

◆ 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 208 of file task_graph.hpp.

208{ return Task(taskflow_.composed_of(other_graph.UnderlyingTaskflow())); }

◆ 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 101 of file task_graph.hpp.

101{ return Task(taskflow_.placeholder()); }

◆ Dump()

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

Dumps the task graph to a DOT format string.

Returns
String representation of the graph in DOT format

Definition at line 214 of file task_graph.hpp.

214{ return taskflow_.dump(); }

◆ EmplaceTask() [1/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 71 of file task_graph.hpp.

71 {
72 return Task(taskflow_.emplace(std::forward<C>(callable)));
73 }

◆ EmplaceTask() [2/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

◆ 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 93 of file task_graph.hpp.

93 {
94 return {EmplaceTask(std::forward<Cs>(callables))...};
95 }
Task EmplaceTask(C &&callable)
Creates a static task with the given callable.

◆ Empty()

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

Checks if this graph has no tasks.

Returns
True if the graph is empty, false otherwise

Definition at line 227 of file task_graph.hpp.

227{ return taskflow_.empty(); }

◆ 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 122 of file task_graph.hpp.

122 {
123 return Task(taskflow_.for_each(std::ranges::begin(range), std::ranges::end(range), std::forward<C>(callable)));
124 }

◆ ForEachIndex()

template<std::integral I, typename C >
requires std::invocable<C, I>
Task helios::async::TaskGraph::ForEachIndex ( start,
end,
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.

138 {
139 return Task(taskflow_.for_each_index(start, end, step, std::forward<C>(callable)));
140 }

◆ 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 253 of file task_graph.hpp.

253 {
254 taskflow_.for_each_task([&visitor](const tf::Task& tf_task) {
255 Task helios_task(tf_task);
256 std::invoke(visitor, helios_task);
257 });
258}

◆ 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 262 of file task_graph.hpp.

262 {
263 std::vector<tf::Task> tf_tasks;
264 if constexpr (std::ranges::sized_range<R>) {
265 tf_tasks.reserve(std::ranges::size(tasks));
266 }
267
268 for (const auto& task : tasks) {
269 tf_tasks.push_back(task.UnderlyingTask());
270 }
271
272 taskflow_.linearize(tf_tasks);
273}

◆ Name() [1/2]

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

Gets the name of task graph.

Returns
Name of the graph

Definition at line 239 of file task_graph.hpp.

239{ return taskflow_.name(); }

◆ 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 285 of file task_graph.hpp.

285 {
286 HELIOS_ASSERT(!name.empty(), "Failed to set task graph name: 'name' cannot be empty!");
287 taskflow_.name(std::string(name));
288}
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:140

◆ 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 171 of file task_graph.hpp.

171 {
172 return Task(
173 taskflow_.reduce(std::ranges::begin(range), std::ranges::end(range), init, std::forward<BinaryOp>(binary_op)));
174 }

◆ 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 199 of file task_graph.hpp.

199 {
200 taskflow_.remove_dependency(from.UnderlyingTask(), to.UnderlyingTask());
201 }

◆ RemoveTask()

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

Removes a task from this graph.

Parameters
taskTask to remove

Definition at line 192 of file task_graph.hpp.

192{ taskflow_.erase(task.UnderlyingTask()); }

◆ 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 277 of file task_graph.hpp.

277 {
278 if constexpr (std::same_as<std::remove_cvref_t<Compare>, std::less<>>) {
279 return Task(taskflow_.sort(std::ranges::begin(range), std::ranges::end(range)));
280 } else {
281 return Task(taskflow_.sort(std::ranges::begin(range), std::ranges::end(range), std::forward<Compare>(comparator)));
282 }
283}

◆ TaskCount()

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

Gets the number of tasks in this graph.

Returns
Count of tasks

Definition at line 233 of file task_graph.hpp.

233{ return taskflow_.num_tasks(); }

◆ 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 154 of file task_graph.hpp.

154 {
155 return Task(taskflow_.transform(std::ranges::begin(input_range), std::ranges::end(input_range),
156 std::ranges::begin(output_range), std::forward<TransformFunc>(transform_func)));
157 }

Friends And Related Symbol Documentation

◆ Executor

friend class Executor
friend

Definition at line 248 of file task_graph.hpp.

◆ SubTaskGraph

friend class SubTaskGraph
friend

Definition at line 247 of file task_graph.hpp.