It is very common for a parallel program to spawn task dependency graphs at runtime. In Taskflow, we call this subflow tasking.
Create a Subflow
Subflow tasks are those created during the execution of a graph. These tasks are spawned from a parent task and are grouped together to a subflow dependency graph. To create a subflow, emplace a callable that takes an argument of type tf::Subflow. A tf::Subflow object will be created and forwarded to the execution context of the task. All methods you find in tf::Taskflow are applicable for tf::Subflow.
3:
7:
8: tf::Task B = taskflow.
emplace([] (tf::Subflow& subflow) {
9: tf::Task B1 = subflow.
emplace([] () {}).name(
"B1");
10: tf::Task B2 = subflow.
emplace([] () {}).name(
"B2");
11: tf::Task B3 = subflow.
emplace([] () {}).name(
"B3");
14: }).name("B");
15:
20:
21: executor.
run(taskflow).get();
class to create an executor
Definition executor.hpp:62
tf::Future< void > run(Taskflow &taskflow)
runs a taskflow once
Task emplace(C &&callable)
creates a static task
Definition flow_builder.hpp:1352
class to create a task handle over a taskflow node
Definition task.hpp:263
Task & precede(Ts &&... tasks)
adds precedence links from this to other tasks
Definition task.hpp:947
class to create a taskflow object
Definition taskflow.hpp:64
Debrief:
- Lines 1-2 create a taskflow and an executor
- Lines 4-6 create three tasks, A, C, and D
- Lines 8-14 create a task B that spawns a task dependency graph of three tasks B1, B2, and B3
- Lines 16-19 add dependencies among A, B, C, and D
- Line 21 submits the graph to an executor and waits until it finishes
Lines 8-14 are the main block to enable subflow tasking at task B. The runtime will create a tf::Subflow passing it to task B, and spawn a dependency graph as described by the associated callable. This new subflow graph will be added to the topology of its parent task B.
Retain a Subflow
By default, a tf::Subflow automatically clears its internal task graph once it is joined. After a subflow joins, its structure and associated resources are no longer accessible. This behavior is designed to reduce memory usage, particularly in applications that recursively spawn many subflows. For applications that require post-processing, such as visualizing the subflow through tf::Taskflow::dump, users can disable this default cleanup behavior by calling tf::Subflow::retain on true. This instructs the runtime to retain the subflow's task graph even after it has joined, enabling further inspection or visualization.
auto A = sf.
emplace([](){ std::cout <<
"A\n"; });
auto B = sf.
emplace([](){ std::cout <<
"B\n"; });
auto C = sf.
emplace([](){ std::cout <<
"C\n"; });
});
executor.
run(taskflow).wait();
taskflow.
dump(std::cout);
class to construct a subflow graph from the execution of a dynamic task
Definition flow_builder.hpp:1516
void retain(bool flag) noexcept
specifies whether to keep the subflow after it is joined
Definition flow_builder.hpp:1625
void dump(std::ostream &ostream) const
dumps the taskflow to a DOT format through a std::ostream target
Definition taskflow.hpp:433
Join a Subflow Explicitly
By default, a subflow implicitly joins its parent task when execution leaves its context. All terminal nodes (i.e., nodes with no outgoing edges) in the subflow are guaranteed to precede the parent task. Upon joining, the subflow's task graph and associated resources are automatically cleaned up. If your application needs to access variables defined within the subflow after it joins, you can explicitly join the subflow and handle post-processing accordingly. A common use case is parallelizing recursive computations such as the Fibonacci sequence:
if (n < 2) return n;
int res1, res2;
return res1 + res2;
}
taskflow.
emplace([&res] (tf::Subflow& sbf) {
res = spawn(5, sbf);
});
executor.
run(taskflow).wait();
void join()
enables the subflow to join its parent task
The code above computes the fifth Fibonacci number using recursive subflow. Calling tf::Subflow::join immediately materializes the subflow by executing all associated tasks to recursively compute Fibonacci numbers. The taskflow graph is shown below:
- Attention
- Using tf::Subflow to implement recursive parallelism like finding Fibonacci numbers may not be as efficient as tf::Runtime due to additional task graph overhead. For more details, readers can refer to Fibonacci Number
Create a Nested Subflow
A subflow can be nested or recursive. You can create another subflow from the execution of a subflow and so on.
2:
4: std::cout << "A spawns A1 & subflow A2\n";
6: std::cout << "subtask A1\n";
7: }).name("A1");
8:
10: std::cout << "A2 spawns A2_1 & A2_2\n";
12: std::cout << "subtask A2_1\n";
13: }).name("A2_1");
15: std::cout << "subtask A2_2\n";
16: }).name("A2_2");
18: }).name("A2");
20: }).name("A");
21:
22:
23: tf::Executor().run(taskflow).get();
Debrief:
- Line 1 creates a taskflow object
- Lines 3-20 create a task to spawn a subflow of two tasks A1 and A2
- Lines 9-18 spawn another subflow of two tasks A2_1 and A2_2 out of its parent task A2
- Lines 23 runs the defined taskflow graph
- Attention
- To properly visualize subflows, you must call tf::Subflow::retain on each subflow and execute the taskflow once to ensure all associated subflows are spawned.