This chapter demonstrates how to create a static task dependency graph. Static tasking captures the static parallel structure of a decomposition and is defined only by the program itself. It has a flat task hierarchy and cannot spawn new tasks from a running dependency graph.
Create a Task Dependency Graph
A task in Cpp-Taskflow is a callable object for which the operation std::invoke is applicable. It can be either a functor, a lambda expression, a bind expression, or a class objects with operator() overloaded. All tasks are created from tf::Taskflow, the class that manages a task dependency graph. Cpp-Taskflow provides two methods, tf::Taskflow::placeholder and tf::Taskflow::emplace to create a task.
Line 2 creates a placeholder task without work (i.e., callable)
Line 3 creates a task from a given callable object and returns a task handle
Line 5-9 creates three tasks in one call using C++ structured binding coupled with std::tuple
Each time you create a task, the taskflow object creates a node in the task graph and returns a task handle of type tf::Task. A task handle is a lightweight object that wraps up a particular node in a graph and provides a set of methods for you to assign different attributes to the task such as adding dependencies, naming, and assigning a new work.
Line 5-6 assigns a name and a work to task A, and add a precedence link to task B
Line 7 adds a dependency link from A to B
Line 9-14 dumps the task attributes
Cpp-Taskflow uses the general-purpose polymorphic function wrapper std::function to store and invoke a callable in a task. You need to follow its contract to create a task.
Visualize a Task Dependency Graph
You can dump a taskflow to a DOT format and visualize the graph using free online tools such as GraphvizOnline and WebGraphviz.
22: taskflow.dump(std::cout); // dump the taskflow graph
23:
24: tasks[0].work([](){ std::cout << "got a new work!\n"; });
25: tasks[1].work([](){ std::cout << "got a new work!\n"; });
26:
27: return 0;
28: }
The output of this program looks like the following:
This is Task 0: num_dependents=0, num_successors=1
This is Task 1: num_dependents=1, num_successors=0
digraph Taskflow {
"This is Task 1";
"This is Task 0";
"This is Task 0" -> "This is Task 1";
}
Debrief:
Line 5 creates a taskflow object
Line 7-10 creates two placeholder tasks with no works and stores the corresponding task handles in a vector
Line 12-13 names the two tasks with human-readable strings
Line 14 adds a dependency link from the first task to the second task
Line 16-20 prints out the name of each task, the number of dependents, and the number of successors
Line 22 dumps the task dependency graph to a GraphViz Online format (dot)
Line 24-25 assigns a new target to each task
You can change the name and work of a task at anytime before running the graph. The later assignment overwrites the previous values.
Lifetime of A Task
A task lives with its graph and belongs to only a graph at a time, and is not destroyed until the graph gets cleaned up. The lifetime of a task refers to the user-given callable object, including captured values. As long as the graph is alive, all the associated tasks exist. It is your responsibility to keep tasks and graph alive during their execution.