docs/SubflowTasking.html
It is very common for a parallel program to spawn task dependency graphs at runtime. In Taskflow, we call this subflow tasking.
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.
1: tf::Taskflow taskflow; 2: tf::Executor executor; 3: 4: tf::Task A = taskflow.emplace([] () {}).name("A");// static task A 5: tf::Task C = taskflow.emplace([] () {}).name("C");// static task C 6: tf::Task D = taskflow.emplace([] () {}).name("D");// static task D 7: 8: tf::Task B = taskflow.emplace([] (tf::Subflow& subflow) { 9:tf::Task B1 = subflow.emplace([] () {}).name("B1");// subflow task B110:tf::Task B2 = subflow.emplace([] () {}).name("B2");// subflow task B211:tf::Task B3 = subflow.emplace([] () {}).name("B3");// subflow task B312:B1.precede(B3);// B1 runs before B313:B2.precede(B3);// B2 runs before B314: }).name("B");15:16: A.precede(B);// B runs after A17: A.precede(C);// C runs after A18: B.precede(D);// D runs after B19: C.precede(D);// D runs after C20:21: executor.run(taskflow).get();// execute the graph to spawn the subflow
Taskflowcluster_p0x7ffee9781810Taskflowcluster_p0x7f9866c01b70Subflow: Bp0x7f9866c01820Ap0x7f9866c01b70Bp0x7f9866c01820->p0x7f9866c01b70p0x7f9866c01930Cp0x7f9866c01820->p0x7f9866c01930p0x7f9866c01a40Dp0x7f9866c01b70->p0x7f9866c01a40p0x7f9866c01930->p0x7f9866c01a40p0x7f9866d01880B1p0x7f9866d01ac0B3p0x7f9866d01880->p0x7f9866d01ac0p0x7f9866d01ac0->p0x7f9866c01b70p0x7f9866d019a0B2p0x7f9866d019a0->p0x7f9866d01ac0
Debrief:
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.
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.
tf::Taskflow taskflow;tf::Executor executor;taskflow.emplace([&](tf::Subflow& sf){sf.retain(true);// retain the subflow after join for visualizationauto A = sf.emplace([](){ std::cout \<\< "A\n"; });auto B = sf.emplace([](){ std::cout \<\< "B\n"; });auto C = sf.emplace([](){ std::cout \<\< "C\n"; });A.precede(B, C);// A runs before B and C});// subflow implicitly joins hereexecutor.run(taskflow).wait();// The subflow graph is now retained and can be visualized using taskflow.dump(...)taskflow.dump(std::cout);
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:
int spawn(int n, tf::Subflow& sbf) {if (n \< 2) return n;int res1, res2;sbf.emplace([&res1, n] (tf::Subflow& sbf) { res1 = spawn(n - 1, sbf); } );sbf.emplace([&res2, n] (tf::Subflow& sbf) { res2 = spawn(n - 2, sbf); } );sbf.join();// join to materialize the subflow immediatelyreturn res1 + res2;}taskflow.emplace([&res] (tf::Subflow& sbf) { res = spawn(5, sbf);});executor.run(taskflow).wait();
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:
Taskflowcluster_p0x7ffd972c0cd0Taskflow: fibonaccicluster_p0xa445c0Subflow: 5cluster_p0x7fe918000b90Subflow: 4cluster_p0x7fe910000b90Subflow: 3cluster_p0x7fe918000fe0Subflow: 2cluster_p0x7fe910000c48Subflow: 2cluster_p0x7fe918000c48Subflow: 3cluster_p0x7fe918000d00Subflow: 2p0xa445c05p0x7fe918000b904p0x7fe918000b90->p0xa445c0p0x7fe910000b903p0x7fe910000b90->p0x7fe918000b90p0x7fe918000fe02p0x7fe918000fe0->p0x7fe910000b90p0x7fe9180011501p0x7fe918001150->p0x7fe918000fe0p0x7fe9180012080p0x7fe918001208->p0x7fe918000fe0p0x7fe9180010981p0x7fe918001098->p0x7fe910000b90p0x7fe910000c482p0x7fe910000c48->p0x7fe918000b90p0x7fe910000d001p0x7fe910000d00->p0x7fe910000c48p0x7fe910000db80p0x7fe910000db8->p0x7fe910000c48p0x7fe918000c483p0x7fe918000c48->p0xa445c0p0x7fe918000d002p0x7fe918000d00->p0x7fe918000c48p0x7fe918000e701p0x7fe918000e70->p0x7fe918000d00p0x7fe918000f280p0x7fe918000f28->p0x7fe918000d00p0x7fe918000db81p0x7fe918000db8->p0x7fe918000c48
A subflow can be nested or recursive. You can create another subflow from the execution of a subflow and so on.
1: tf::Taskflow taskflow; 2: 3: tf::Task A = taskflow.emplace([] (tf::Subflow& sf){ 4:std::cout \<\< "A spawns A1 & subflow A2\n"; 5:tf::Task A1 = sf.emplace([] () { 6:std::cout \<\< "subtask A1\n"; 7:}).name("A1"); 8: 9:tf::Task A2 = sf.emplace([] (tf::Subflow& sf2){10:std::cout \<\< "A2 spawns A2\_1 & A2\_2\n";11:tf::Task A2\_1 = sf2.emplace([] () {12:std::cout \<\< "subtask A2\_1\n";13:}).name("A2\_1");14:tf::Task A2\_2 = sf2.emplace([] () {15:std::cout \<\< "subtask A2\_2\n";16:}).name("A2\_2");17:A2\_1.precede(A2\_2);18:}).name("A2");19:A1.precede(A2);20: }).name("A");21:22: // execute the graph to spawn the subflow23: tf::Executor().run(taskflow).get();
Taskflowcluster_p0x7ffeeca03810Taskflowcluster_p0x7fbc40c02830Subflow: Acluster_p0x7fbc40d00240Subflow: A2p0x7fbc40c02830Ap0x7fbc40d00120A1p0x7fbc40d00240A2p0x7fbc40d00120->p0x7fbc40d00240p0x7fbc40d00240->p0x7fbc40c02830p0x7fbc40d00360A2_1p0x7fbc40d00470A2_2p0x7fbc40d00360->p0x7fbc40d00470p0x7fbc40d00470->p0x7fbc40d00240
Debrief: