<divclass="textblock"><p>It is very common for a parallel program to spawn task dependency graphs at runtime. In Taskflow, we call this <em>dynamic tasking</em>.</p>
<h1><aclass="anchor" id="C3_CreateASubflow"></a>
Create a Subflow</h1>
<p>Dynamic tasks are those created during the execution of a graph. These tasks are spawned from a parent task and are grouped together to a <em>subflow</em> dependency graph. Taskflow has an unified interface for static and dynamic tasking. To create a subflow, emplace a callable that takes an argument of type <aclass="el" href="classtf_1_1Subflow.html" title="building methods of a subflow graph in dynamic tasking ">tf::Subflow</a>. A <aclass="el" href="classtf_1_1Subflow.html" title="building methods of a subflow graph in dynamic tasking ">tf::Subflow</a> object will be created and forwarded to the execution context of the task. All methods you find in <aclass="el" href="classtf_1_1Taskflow.html" title="main entry to create a task dependency graph ">tf::Taskflow</a> are applicable for <aclass="el" href="classtf_1_1Subflow.html" title="building methods of a subflow graph in dynamic tasking ">tf::Subflow</a>.</p>
<divclass="fragment"><divclass="line"> 1: <aclass="code" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow;</div><divclass="line"> 2: <aclass="code" href="classtf_1_1Executor.html">tf::Executor</a> executor;</div><divclass="line"> 3:</div><divclass="line"> 4: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> A = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"A"</span>); <spanclass="comment">// static task A</span></div><divclass="line"> 5: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> C = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"C"</span>); <spanclass="comment">// static task C</span></div><divclass="line"> 6: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> D = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"D"</span>); <spanclass="comment">// static task D</span></div><divclass="line"> 7:</div><divclass="line"> 8: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> B = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] (<aclass="code" href="classtf_1_1Subflow.html">tf::Subflow</a>& subflow) { </div><divclass="line"> 9: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> B1 = subflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"B1"</span>); <spanclass="comment">// dynamic task B1</span></div><divclass="line">10: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> B2 = subflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"B2"</span>); <spanclass="comment">// dynamic task B2</span></div><divclass="line">11: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> B3 = subflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"B3"</span>); <spanclass="comment">// dynamic task B3</span></div><divclass="line">12: B1.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B3); <spanclass="comment">// B1 runs bofore B3</span></div><divclass="line">13: B2.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B3); <spanclass="comment">// B2 runs before B3</span></div><divclass="line">14: }).name(<spanclass="stringliteral">"B"</span>);</div><divclass="line">15:</div><divclass="line">16: A.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B); <spanclass="comment">// B runs after A</span></div><divclass="line">17: A.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(C); <spanclass="comment">// C runs after A</span></div><divclass="line">18: B.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(D); <spanclass="comment">// D runs after B</span></div><divclass="line">19: C.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(D); <spanclass="comment">// D runs after C</span></div><divclass="line">20:</div><divclass="line">21: executor.<aclass="code" href="classtf_1_1Executor.html#a81f35d5b0a20ac0646447eb80d97c0aa">run</a>(taskflow).get(); <spanclass="comment">// execute the graph to spawn the subflow</span></div><divclass="line">22: taskflow.<aclass="code" href="classtf_1_1Taskflow.html#ac433018262e44b12c4cc9f0c4748d758">dump</a>(<aclass="codeRef" doxygen="/home/tsung-wei/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a>); <spanclass="comment">// dump the taskflow to a DOT format</span></div></div><!-- fragment --><divclass="image">
<li>Line 1-2 creates a taskflow and an executor </li>
<li>Line 4-6 creates three tasks, A, C, and D </li>
<li>Line 8-14 creates a task B that spawns a task dependency graph of three tasks B1, B2, and B3 </li>
<li>Line 16-19 adds dependencies among A, B, C, and D </li>
<li>Line 21 submits the graph to an executor and waits until it finishes </li>
<li>Line 22 dumps the entire task dependency graph</li>
</ul>
<p>Line 8-14 is the main block to enable dynamic tasking. Taskflow uses a <ahref="https://en.cppreference.com/w/cpp/utility/variant">std::variant</a> date type to unify the interface of static tasking and dynamic tasking. The runtime will create a <aclass="el" href="classtf_1_1Subflow.html" title="building methods of a subflow graph in dynamic tasking ">tf::Subflow</a> 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. Due to the property of dynamic tasking, we cannot dump its structure before execution. We will need to run the graph first to spawn the graph and then call <aclass="el" href="classtf_1_1Taskflow.html#ac433018262e44b12c4cc9f0c4748d758" title="dumps the taskflow to a std::ostream in DOT format ">tf::Taskflow::dump</a>.</p>
<h1><aclass="anchor" id="JoinASubflow"></a>
Join a Subflow</h1>
<p>By default, a subflow joins its parent task when the program leaves its execution context. All nodes of zero outgoing edges in the subflow precede its parent task. You can explicitly join a subflow within its execution context to carry out recursive patterns. A famous implementation is fibonacci recursion.</p>
<divclass="fragment"><divclass="line"><spanclass="keywordtype">int</span> spawn(<spanclass="keywordtype">int</span> n, <aclass="code" href="classtf_1_1Subflow.html">tf::Subflow</a>& sbf) {</div><divclass="line"><spanclass="keywordflow">if</span> (n < 2) <spanclass="keywordflow">return</span> n;</div><divclass="line"><spanclass="keywordtype">int</span> res1, res2;</div><divclass="line"> sbf.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([&res1, n] (<aclass="code" href="classtf_1_1Subflow.html">tf::Subflow</a>& sbf) { res1 = spawn(n - 1, sbf); } );</div><divclass="line"> sbf.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([&res2, n] (<aclass="code" href="classtf_1_1Subflow.html">tf::Subflow</a>& sbf) { res2 = spawn(n - 2, sbf); } );</div><divclass="line"> sbf.<aclass="code" href="classtf_1_1Subflow.html#a59fcac1323e70d920088dd37bd0be245">join</a>(); <spanclass="comment">// join to materialize the subflow immediately</span></div><divclass="line"><spanclass="keywordflow">return</span> res1 + res2;</div><divclass="line">}</div><divclass="line"></div><divclass="line">taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([&res] (<aclass="code" href="classtf_1_1Subflow.html">tf::Subflow</a>& sbf) { </div><divclass="line"> res = spawn(10, sbf); </div><divclass="line">});</div><divclass="line"></div><divclass="line">executor.<aclass="code" href="classtf_1_1Executor.html#a81f35d5b0a20ac0646447eb80d97c0aa">run</a>(taskflow).wait();</div></div><!-- fragment --><p>The code above computes the 10th fibonacci number using recursive subflow. Calling <aclass="el" href="classtf_1_1Subflow.html#a59fcac1323e70d920088dd37bd0be245" title="enables the subflow to join its parent task ">tf::Subflow::join</a><em>immediately</em> materializes the subflow by executing all associated tasks to recursively compute fibonacci numbers. The taskflow graph is shown below:</p>
<p>Our implementation to join subflows is <em>recursive</em> in order to preserve the thread context in each subflow task. Having a deep recursion of subflows may cause stack overflow.</p>
<h1><aclass="anchor" id="DetachASubflow"></a>
Detach a Subflow</h1>
<p>In contract to joined subflow, you can detach a subflow from its parent task, allowing its execution to flow independently.</p>
<divclass="fragment"><divclass="line"> 1: <aclass="code" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow;</div><divclass="line"> 2:</div><divclass="line"> 3: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> A = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"A"</span>); <spanclass="comment">// static task A</span></div><divclass="line"> 4: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> C = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"C"</span>); <spanclass="comment">// static task C</span></div><divclass="line"> 5: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> D = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"D"</span>); <spanclass="comment">// static task D</span></div><divclass="line"> 6:</div><divclass="line"> 7: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> B = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] (<aclass="code" href="classtf_1_1Subflow.html">tf::Subflow</a>& subflow) { </div><divclass="line"> 8: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> B1 = subflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"B1"</span>); <spanclass="comment">// dynamic task B1</span></div><divclass="line"> 9: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> B2 = subflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"B2"</span>); <spanclass="comment">// dynamic task B2</span></div><divclass="line">10: <aclass="code" href="classtf_1_1Task.html">tf::Task</a> B3 = subflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([] () {}).name(<spanclass="stringliteral">"B3"</span>); <spanclass="comment">// dynamic task B3</span></div><divclass="line">11: B1.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B3); <spanclass="comment">// B1 runs bofore B3</span></div><divclass="line">12: B2.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B3); <spanclass="comment">// B2 runs before B3</span></div><divclass="line">13: subflow.<aclass="code" href="classtf_1_1Subflow.html#acfdedc7e9676126e9a38ecf7b5a37864">detach</a>(); <spanclass="comment">// detach this subflow</span></div><divclass="line">14: }).name(<spanclass="stringliteral">"B"</span>);</div><divclass="line">15:</div><divclass="line">16: A.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B); <spanclass="comment">// B runs after A</span></div><divclass="line">17: A.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(C); <spanclass="comment">// C runs after A</span></div><divclass="line">18: B.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(D); <spanclass="comment">// D runs after B</span></div><divclass="line">19: C.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(D); <spanclass="comment">// D runs after C</span></div><divclass="line">20:</div><divclass="line">21: <aclass="code" href="classtf_1_1Executor.html">tf::Executor</a> executor;</div><divclass="line">22: executor.<aclass="code" href="classtf_1_1Executor.html#a81f35d5b0a20ac0646447eb80d97c0aa">run</a>(taskflow).wait(); <spanclass="comment">// execute the graph to spawn the subflow</span></div><divclass="line">22: taskflow.<aclass="code" href="classtf_1_1Taskflow.html#ac433018262e44b12c4cc9f0c4748d758">dump</a>(<aclass="codeRef" doxygen="/home/tsung-wei/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a>); <spanclass="comment">// dump the taskflow to DOT format</span></div></div><!-- fragment --><p>The figure below demonstrates a detached subflow based on the previous example. A detached subflow will eventually join the topology of its parent task.</p>
<p>Detached subflow becomes an independent graph attached to the top-most taskflow. Running a taskflow multiple times will accumulate all detached tasks in the graph. For example, running the above taskflow 10 times results in a total of 34 tasks.</p>
<divclass="fragment"><divclass="line">executor.<aclass="code" href="classtf_1_1Executor.html#adca6cd0ce1bd7e6fa2ed2a55c9ae15e6">run_n</a>(taskflow, 10).wait();</div><divclass="line">assert(taskflow.<aclass="code" href="classtf_1_1Taskflow.html#af4f03bca084deb5c2228ac8936d33649">num_tasks</a>() == 34);</div><divclass="line">taskflow.<aclass="code" href="classtf_1_1Taskflow.html#ac433018262e44b12c4cc9f0c4748d758">dump</a>(<aclass="codeRef" doxygen="/home/tsung-wei/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a>);</div></div><!-- fragment --><p>The dumped graph is shown as follows:</p>
<li>Line 3-20 creates a task to spawn a subflow of two tasks A1 and A2 </li>
<li>Line 9-18 spawns another subflow of two tasks A2_1 and A2_2 out of its parent task A2 </li>
<li>Line 23-24 dispatches the graph asynchronously and dump its structure when it finishes</li>
</ul>
<p>Similarly, you can detach a nested subflow from its parent subflow. A detached subflow will run independently and eventually join the topology of its parent subflow. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<divid="nav-path" class="navpath"><!-- id is needed for treeview function! -->