tf::Taskflow class

main entry to create a task dependency graph

A taskflow manages a task dependency graph where each task represents a callable object (e.g., lambda, std::function) and an edge represents a dependency between two tasks. A task is one of the following five types:

  1. static task: the callable constructible from std::function<void()>
  2. dynamic task: the callable constructible from std::function<void(tf::Subflow&)>
  3. condition task: the callable constructible from std::function<int()>
  4. module task: the task constructed from tf::Taskflow::composed_of
  5. cudaFlow task: the callable constructible from std::function<void(tf::cudaFlow&)> or std::function<void(tf::cudaFlowCapturer&)>

The following example creates a simple taskflow graph of four static tasks, A, B, C, and D, where A runs before B and C and D runs after B and C.

tf::Executor executor;
tf::Taskflow taskflow("simple");

auto [A, B, C, D] = taskflow.emplace(
  []() { std::cout << "TaskA\n"; },
  []() { std::cout << "TaskB\n"; },
  []() { std::cout << "TaskC\n"; },
  []() { std::cout << "TaskD\n"; }
);

A.precede(B, C);  // A runs before B and C
D.succeed(B, C);  // D runs after  B and C
                                   
executor.run(taskflow).wait();

Please refer to Cookbook to learn more about each task type.

Base classes

class FlowBuilder
building methods of a task dependency graph

Constructors, destructors, conversion operators

Taskflow(const std::string& name)
constructs a taskflow with the given name
Taskflow()
constructs a taskflow

Public functions

void dump(std::ostream& ostream) const
dumps the taskflow to a DOT format through an output stream using the stream insertion operator <<
auto dump() const -> std::string
dumps the taskflow to a std::string of DOT format
auto num_tasks() const -> size_t
queries the number of tasks
auto empty() const -> bool
queries the emptiness of the taskflow
void name(const std::string&)
sets the name of the taskflow
auto name() const -> const std::string&
queries the name of the taskflow
void clear()
clears the associated task dependency graph
template<typename V>
void for_each_task(V&& visitor) const
applies an visitor callable to each task in the taskflow

Function documentation

template<typename V>
void tf::Taskflow::for_each_task(V&& visitor) const

applies an visitor callable to each task in the taskflow

The visitor is a callable that takes an argument of type tf::Task and returns nothing. The following example iterates each task in a taskflow and prints its name:

taskflow.for_each_task([](tf::Task task){
  std::cout << task.name() << '\n';
});