-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathSubflowTasking.html
More file actions
254 lines (252 loc) · 24.9 KB
/
Copy pathSubflowTasking.html
File metadata and controls
254 lines (252 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!-- HTML header for doxygen 1.13.1-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.13.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Taskflow: A General-purpose Task-parallel Programming System: Subflow Tasking</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="custom.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="taskflow_logo.png"/></td>
<td id="projectalign">
<div id="projectname"><a href="https://github.com/taskflow/taskflow" style="color:inherit; text-decoration:none;">Taskflow: A General-purpose Task-parallel Programming System</a>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.13.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
$(function() { codefold.init(0); });
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
$(function() {
initMenu('',true,false,'search.php','Search',true);
$(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
$(function(){initNavTree('SubflowTasking.html',''); initResizable(true); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div><div class="header">
<div class="headertitle"><div class="title">Subflow Tasking</div></div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul>
<li class="level1">
<a href="#CreateASubflow">Create a Subflow</a>
</li>
<li class="level1">
<a href="#RetainASubflow">Retain a Subflow</a>
</li>
<li class="level1">
<a href="#JoinASubflow">Join a Subflow Explicitly</a>
</li>
<li class="level1">
<a href="#CreateANestedSubflow">Create a Nested Subflow</a>
</li>
</ul>
</div>
<div class="textblock"><p>It is very common for a parallel program to spawn task dependency graphs at runtime. In Taskflow, we call this <em>subflow tasking</em>.</p>
<h1><a class="anchor" id="CreateASubflow"></a>
Create a Subflow</h1>
<p>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 <em>subflow</em> dependency graph. To create a subflow, emplace a callable that takes an argument of type <a class="el" href="classtf_1_1Subflow.html" title="class to construct a subflow graph from the execution of a dynamic task">tf::Subflow</a>. A <a class="el" href="classtf_1_1Subflow.html" title="class to construct a subflow graph from the execution of a dynamic task">tf::Subflow</a> object will be created and forwarded to the execution context of the task. All methods you find in <a class="el" href="classtf_1_1Taskflow.html" title="class to create a taskflow object">tf::Taskflow</a> are applicable for <a class="el" href="classtf_1_1Subflow.html" title="class to construct a subflow graph from the execution of a dynamic task">tf::Subflow</a>.</p>
<div class="fragment"><div class="line"> 1: <a class="code hl_class" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow;</div>
<div class="line"> 2: <a class="code hl_class" href="classtf_1_1Executor.html">tf::Executor</a> executor;</div>
<div class="line"> 3:</div>
<div class="line"> 4: <a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> A = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () {}).name(<span class="stringliteral">"A"</span>); <span class="comment">// static task A</span></div>
<div class="line"> 5: <a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> C = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () {}).name(<span class="stringliteral">"C"</span>); <span class="comment">// static task C</span></div>
<div class="line"> 6: <a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> D = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () {}).name(<span class="stringliteral">"D"</span>); <span class="comment">// static task D</span></div>
<div class="line"> 7:</div>
<div class="line"> 8: tf::Task B = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] (tf::Subflow& subflow) { </div>
<div class="line"> 9: tf::Task B1 = subflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () {}).name(<span class="stringliteral">"B1"</span>); <span class="comment">// subflow task B1</span></div>
<div class="line">10: tf::Task B2 = subflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () {}).name(<span class="stringliteral">"B2"</span>); <span class="comment">// subflow task B2</span></div>
<div class="line">11: tf::Task B3 = subflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () {}).name(<span class="stringliteral">"B3"</span>); <span class="comment">// subflow task B3</span></div>
<div class="line">12: B1.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B3); <span class="comment">// B1 runs before B3</span></div>
<div class="line">13: B2.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B3); <span class="comment">// B2 runs before B3</span></div>
<div class="line">14: }).name(<span class="stringliteral">"B"</span>);</div>
<div class="line">15:</div>
<div class="line">16: A.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B); <span class="comment">// B runs after A</span></div>
<div class="line">17: A.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(C); <span class="comment">// C runs after A</span></div>
<div class="line">18: B.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(D); <span class="comment">// D runs after B</span></div>
<div class="line">19: C.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(D); <span class="comment">// D runs after C</span></div>
<div class="line">20:</div>
<div class="line">21: executor.<a class="code hl_function" href="classtf_1_1Executor.html#a519777f5783981d534e9e53b99712069">run</a>(taskflow).get(); <span class="comment">// execute the graph to spawn the subflow</span></div>
<div class="ttc" id="aclasstf_1_1Executor_html"><div class="ttname"><a href="classtf_1_1Executor.html">tf::Executor</a></div><div class="ttdoc">class to create an executor</div><div class="ttdef"><b>Definition</b> executor.hpp:62</div></div>
<div class="ttc" id="aclasstf_1_1Executor_html_a519777f5783981d534e9e53b99712069"><div class="ttname"><a href="classtf_1_1Executor.html#a519777f5783981d534e9e53b99712069">tf::Executor::run</a></div><div class="ttdeci">tf::Future< void > run(Taskflow &taskflow)</div><div class="ttdoc">runs a taskflow once</div></div>
<div class="ttc" id="aclasstf_1_1FlowBuilder_html_a4d52a7fe2814b264846a2085e931652c"><div class="ttname"><a href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">tf::FlowBuilder::emplace</a></div><div class="ttdeci">Task emplace(C &&callable)</div><div class="ttdoc">creates a static task</div><div class="ttdef"><b>Definition</b> flow_builder.hpp:1571</div></div>
<div class="ttc" id="aclasstf_1_1Task_html"><div class="ttname"><a href="classtf_1_1Task.html">tf::Task</a></div><div class="ttdoc">class to create a task handle over a taskflow node</div><div class="ttdef"><b>Definition</b> task.hpp:569</div></div>
<div class="ttc" id="aclasstf_1_1Task_html_a8c78c453295a553c1c016e4062da8588"><div class="ttname"><a href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">tf::Task::precede</a></div><div class="ttdeci">Task & precede(Ts &&... tasks)</div><div class="ttdoc">adds precedence links from this to other tasks</div><div class="ttdef"><b>Definition</b> task.hpp:1258</div></div>
<div class="ttc" id="aclasstf_1_1Taskflow_html"><div class="ttname"><a href="classtf_1_1Taskflow.html">tf::Taskflow</a></div><div class="ttdoc">class to create a taskflow object</div><div class="ttdef"><b>Definition</b> taskflow.hpp:64</div></div>
</div><!-- fragment --><div class="dotgraph">
<iframe scrolling="no" frameborder="0" src="dot_subflow-join.svg" width="496" height="320"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>Debrief: </p><ul>
<li>Lines 1-2 create a taskflow and an executor </li>
<li>Lines 4-6 create three tasks, A, C, and D </li>
<li>Lines 8-14 create a task B that spawns a task dependency graph of three tasks B1, B2, and B3 </li>
<li>Lines 16-19 add dependencies among A, B, C, and D </li>
<li>Line 21 submits the graph to an executor and waits until it finishes</li>
</ul>
<p>Lines 8-14 are the main block to enable subflow tasking at task B. The runtime will create a <a class="el" href="classtf_1_1Subflow.html" title="class to construct a subflow graph from the execution of a dynamic task">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.</p>
<h1><a class="anchor" id="RetainASubflow"></a>
Retain a Subflow</h1>
<p>By default, a <a class="el" href="classtf_1_1Subflow.html" title="class to construct a subflow graph from the execution of a dynamic task">tf::Subflow</a> 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 <a class="el" href="classtf_1_1Taskflow.html#ac433018262e44b12c4cc9f0c4748d758" title="dumps the taskflow to a DOT format through a std::ostream target">tf::Taskflow::dump</a>, users can disable this default cleanup behavior by calling <a class="el" href="classtf_1_1Subflow.html#ac585638d8ca8fb2f34c4826cb0d4f39f" title="specifies whether to keep the subflow after it is joined">tf::Subflow::retain</a> on <code>true</code>. This instructs the runtime to retain the subflow's task graph even after it has joined, enabling further inspection or visualization.</p>
<div class="fragment"><div class="line"><a class="code hl_class" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow;</div>
<div class="line"><a class="code hl_class" href="classtf_1_1Executor.html">tf::Executor</a> executor;</div>
<div class="line"> </div>
<div class="line">taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([&](<a class="code hl_class" href="classtf_1_1Subflow.html">tf::Subflow</a>& sf){</div>
<div class="line"> sf.<a class="code hl_function" href="classtf_1_1Subflow.html#ac585638d8ca8fb2f34c4826cb0d4f39f">retain</a>(<span class="keyword">true</span>); <span class="comment">// retain the subflow after join for visualization</span></div>
<div class="line"> <span class="keyword">auto</span> A = sf.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([](){ std::cout << <span class="stringliteral">"A\n"</span>; });</div>
<div class="line"> <span class="keyword">auto</span> B = sf.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([](){ std::cout << <span class="stringliteral">"B\n"</span>; });</div>
<div class="line"> <span class="keyword">auto</span> C = sf.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([](){ std::cout << <span class="stringliteral">"C\n"</span>; });</div>
<div class="line"> A.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B, C); <span class="comment">// A runs before B and C</span></div>
<div class="line">}); <span class="comment">// subflow implicitly joins here</span></div>
<div class="line"> </div>
<div class="line">executor.<a class="code hl_function" href="classtf_1_1Executor.html#a519777f5783981d534e9e53b99712069">run</a>(taskflow).wait();</div>
<div class="line"> </div>
<div class="line"><span class="comment">// The subflow graph is now retained and can be visualized using taskflow.dump(...)</span></div>
<div class="line">taskflow.<a class="code hl_function" href="classtf_1_1Taskflow.html#ac433018262e44b12c4cc9f0c4748d758">dump</a>(std::cout);</div>
<div class="ttc" id="aclasstf_1_1Subflow_html"><div class="ttname"><a href="classtf_1_1Subflow.html">tf::Subflow</a></div><div class="ttdoc">class to construct a subflow graph from the execution of a dynamic task</div><div class="ttdef"><b>Definition</b> flow_builder.hpp:1735</div></div>
<div class="ttc" id="aclasstf_1_1Subflow_html_ac585638d8ca8fb2f34c4826cb0d4f39f"><div class="ttname"><a href="classtf_1_1Subflow.html#ac585638d8ca8fb2f34c4826cb0d4f39f">tf::Subflow::retain</a></div><div class="ttdeci">void retain(bool flag) noexcept</div><div class="ttdoc">specifies whether to keep the subflow after it is joined</div><div class="ttdef"><b>Definition</b> flow_builder.hpp:1844</div></div>
<div class="ttc" id="aclasstf_1_1Taskflow_html_ac433018262e44b12c4cc9f0c4748d758"><div class="ttname"><a href="classtf_1_1Taskflow.html#ac433018262e44b12c4cc9f0c4748d758">tf::Taskflow::dump</a></div><div class="ttdeci">void dump(std::ostream &ostream) const</div><div class="ttdoc">dumps the taskflow to a DOT format through a std::ostream target</div><div class="ttdef"><b>Definition</b> taskflow.hpp:433</div></div>
</div><!-- fragment --><h1><a class="anchor" id="JoinASubflow"></a>
Join a Subflow Explicitly</h1>
<p>By default, a subflow <em>implicitly</em> 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:</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> spawn(<span class="keywordtype">int</span> n, <a class="code hl_class" href="classtf_1_1Subflow.html">tf::Subflow</a>& sbf) {</div>
<div class="line"> <span class="keywordflow">if</span> (n < 2) <span class="keywordflow">return</span> n;</div>
<div class="line"> <span class="keywordtype">int</span> res1, res2;</div>
<div class="line"> sbf.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([&res1, n] (<a class="code hl_class" href="classtf_1_1Subflow.html">tf::Subflow</a>& sbf) { res1 = spawn(n - 1, sbf); } );</div>
<div class="line"> sbf.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([&res2, n] (<a class="code hl_class" href="classtf_1_1Subflow.html">tf::Subflow</a>& sbf) { res2 = spawn(n - 2, sbf); } );</div>
<div class="line"> sbf.<a class="code hl_function" href="classtf_1_1Subflow.html#a59fcac1323e70d920088dd37bd0be245">join</a>(); <span class="comment">// join to materialize the subflow immediately</span></div>
<div class="line"> <span class="keywordflow">return</span> res1 + res2;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line">taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([&res] (tf::Subflow& sbf) { </div>
<div class="line"> res = spawn(5, sbf); </div>
<div class="line">});</div>
<div class="line"> </div>
<div class="line">executor.<a class="code hl_function" href="classtf_1_1Executor.html#a519777f5783981d534e9e53b99712069">run</a>(taskflow).wait();</div>
<div class="ttc" id="aclasstf_1_1Subflow_html_a59fcac1323e70d920088dd37bd0be245"><div class="ttname"><a href="classtf_1_1Subflow.html#a59fcac1323e70d920088dd37bd0be245">tf::Subflow::join</a></div><div class="ttdeci">void join()</div><div class="ttdoc">enables the subflow to join its parent task</div></div>
</div><!-- fragment --><p>The code above computes the fifth Fibonacci number using recursive subflow. Calling <a class="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>
<div class="dotgraph">
<iframe scrolling="no" frameborder="0" src="dot_fibonacci_7.svg" width="894" height="752"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<dl class="section note"><dt>Note</dt><dd>Using <a class="el" href="classtf_1_1Subflow.html" title="class to construct a subflow graph from the execution of a dynamic task">tf::Subflow</a> to implement recursive parallelism like finding Fibonacci numbers may not be as efficient as <a class="el" href="classtf_1_1Runtime.html" title="class to create a runtime task">tf::Runtime</a> due to additional task graph overhead. For more details, readers can refer to <a class="el" href="ExamplesFibonacciNumber.html">Fibonacci Number</a></dd></dl>
<h1><a class="anchor" id="CreateANestedSubflow"></a>
Create a Nested Subflow</h1>
<p>A subflow can be nested or recursive. You can create another subflow from the execution of a subflow and so on.</p>
<div class="fragment"><div class="line"> 1: <a class="code hl_class" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow;</div>
<div class="line"> 2:</div>
<div class="line"> 3: <a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> A = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] (<a class="code hl_class" href="classtf_1_1Subflow.html">tf::Subflow</a>& sf){</div>
<div class="line"> 4: std::cout << <span class="stringliteral">"A spawns A1 & subflow A2\n"</span>;</div>
<div class="line"> 5: <a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> A1 = sf.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () {</div>
<div class="line"> 6: std::cout << <span class="stringliteral">"subtask A1\n"</span>;</div>
<div class="line"> 7: }).name(<span class="stringliteral">"A1"</span>);</div>
<div class="line"> 8:</div>
<div class="line"> 9: <a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> A2 = sf.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] (<a class="code hl_class" href="classtf_1_1Subflow.html">tf::Subflow</a>& sf2){</div>
<div class="line">10: std::cout << <span class="stringliteral">"A2 spawns A2_1 & A2_2\n"</span>;</div>
<div class="line">11: <a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> A2_1 = sf2.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () {</div>
<div class="line">12: std::cout << <span class="stringliteral">"subtask A2_1\n"</span>;</div>
<div class="line">13: }).name(<span class="stringliteral">"A2_1"</span>);</div>
<div class="line">14: <a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> A2_2 = sf2.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () {</div>
<div class="line">15: std::cout << <span class="stringliteral">"subtask A2_2\n"</span>;</div>
<div class="line">16: }).name(<span class="stringliteral">"A2_2"</span>);</div>
<div class="line">17: A2_1.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(A2_2);</div>
<div class="line">18: }).name(<span class="stringliteral">"A2"</span>);</div>
<div class="line">19: A1.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(A2);</div>
<div class="line">20: }).name(<span class="stringliteral">"A"</span>);</div>
<div class="line">21:</div>
<div class="line">22: <span class="comment">// execute the graph to spawn the subflow</span></div>
<div class="line">23: tf::Executor().run(taskflow).get();</div>
</div><!-- fragment --><div class="dotgraph">
<iframe scrolling="no" frameborder="0" src="dot_nested_subflow.svg" width="542" height="303"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>Debrief: </p><ul>
<li>Line 1 creates a taskflow object </li>
<li>Lines 3-20 create a task to spawn a subflow of two tasks A1 and A2 </li>
<li>Lines 9-18 spawn another subflow of two tasks A2_1 and A2_2 out of its parent task A2 </li>
<li>Lines 23 runs the defined taskflow graph</li>
</ul>
<dl class="section note"><dt>Note</dt><dd>To properly visualize subflows, you must call <a class="el" href="classtf_1_1Subflow.html#ac585638d8ca8fb2f34c4826cb0d4f39f" title="specifies whether to keep the subflow after it is joined">tf::Subflow::retain</a> on each subflow and execute the taskflow once to ensure all associated subflows are spawned. </dd></dl>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.13.1-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="Cookbook.html">Cookbook</a></li>
<li class="footer">
Maintained by <a href="https://tsung-wei-huang.github.io/">Dr. Tsung-Wei Huang</a>
—
Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.1
</li>
</ul>
</div>