-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathStaticTasking.html
More file actions
290 lines (288 loc) · 32.3 KB
/
Copy pathStaticTasking.html
File metadata and controls
290 lines (288 loc) · 32.3 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!-- 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: Static 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('StaticTasking.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">Static Tasking</div></div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul>
<li class="level1">
<a href="#CreateATaskDependencyGraph">Create a Task Dependency Graph</a>
</li>
<li class="level1">
<a href="#VisualizeATaskDependencyGraph">Visualize a Task Dependency Graph</a>
</li>
<li class="level1">
<a href="#TraverseAdjacentTasks">Traverse Adjacent Tasks</a>
</li>
<li class="level1">
<a href="#AttachUserDataToATask">Attach User Data to a Task</a>
</li>
<li class="level1">
<a href="#UnderstandTheLifetimeOfATask">Understand the Lifetime of a Task</a>
</li>
<li class="level1">
<a href="#MoveATaskflow">Move a Taskflow</a>
</li>
</ul>
</div>
<div class="textblock"><p>Static tasking is the most basic programming model in Taskflow. It follows the construct-and-run model, where you define a taskflow graph first and submit it to an executor for execution.</p>
<h1><a class="anchor" id="CreateATaskDependencyGraph"></a>
Create a Task Dependency Graph</h1>
<p>A task in Taskflow is a <em>callable</em> object for which the operation <a href="https://en.cppreference.com/w/cpp/utility/functional/invoke">std::invoke</a> is applicable. It can be either a functor, a lambda expression, a bind expression, or a class objects with <code>operator()</code> overloaded. All tasks are created from <a class="el" href="classtf_1_1Taskflow.html" title="class to create a taskflow object">tf::Taskflow</a>, the class that manages a task dependency graph. Taskflow provides two methods, <a class="el" href="classtf_1_1FlowBuilder.html#acab0b4ac82260f47fdb36a3244ee3aaf" title="creates a placeholder task">tf::Taskflow::placeholder</a> and <a class="el" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c" title="creates a static task">tf::Taskflow::emplace</a> to create a task.</p>
<p>For example, the code below creates a taskflow. It first defines a placeholder task without assigned work, then creates a task directly from a given callable and obtains its task handle. Finally, it creates multiple tasks in one call using C++ structured binding.</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_1Task.html">tf::Task</a> A = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#acab0b4ac82260f47fdb36a3244ee3aaf">placeholder</a>();</div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> B = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([](){ std::cout << <span class="stringliteral">"task B\n"</span>; });</div>
<div class="line"><span class="keyword">auto</span> [D, E, F] = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>(</div>
<div class="line"> [](){ std::cout << <span class="stringliteral">"Task D\n"</span>; },</div>
<div class="line"> [](){ std::cout << <span class="stringliteral">"Task E\n"</span>; },</div>
<div class="line"> [](){ std::cout << <span class="stringliteral">"Task F\n"</span>; }</div>
<div class="line">);</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_1FlowBuilder_html_acab0b4ac82260f47fdb36a3244ee3aaf"><div class="ttname"><a href="classtf_1_1FlowBuilder.html#acab0b4ac82260f47fdb36a3244ee3aaf">tf::FlowBuilder::placeholder</a></div><div class="ttdeci">Task placeholder()</div><div class="ttdoc">creates a placeholder task</div><div class="ttdef"><b>Definition</b> flow_builder.hpp:1635</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_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 --><p>Each time you create a task, the taskflow creates a node and returns a task handle of type <a class="el" href="classtf_1_1Task.html" title="class to create a task handle over a taskflow node">tf::Task</a>. A task handle is a copy-cheap wrapper over the node pointer to a task in taskflow. The handle provides a set of methods for you to access and modify the attributes of a task, such as building dependencies, assigning a name, changing the work, querying task statistics, and so on.</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_1Task.html">tf::Task</a> A = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () { std::cout << <span class="stringliteral">"create a task A\n"</span>; });</div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> B = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () { std::cout << <span class="stringliteral">"create a task B\n"</span>; });</div>
<div class="line"> </div>
<div class="line">A.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">"Task A"</span>);</div>
<div class="line">A.<a class="code hl_function" href="classtf_1_1Task.html#a2f6f4cec42d016e5eb89390f362ffe99">work</a>([] () { std::cout << <span class="stringliteral">"reassign A to a new callable\n"</span>; });</div>
<div class="line">A.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B);</div>
<div class="line"> </div>
<div class="line">std::cout << A.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>() << <span class="charliteral">'\n'</span>; <span class="comment">// Task A</span></div>
<div class="line">std::cout << A.<a class="code hl_function" href="classtf_1_1Task.html#a1a0afc89e8a6a416c511e74d82df135d">num_successors</a>() << <span class="charliteral">'\n'</span>; <span class="comment">// 1</span></div>
<div class="line">std::cout << A.<a class="code hl_function" href="classtf_1_1Task.html#adefb65d68a64bd8a75364a8801cfec44">num_predecessors</a>() << <span class="charliteral">'\n'</span>; <span class="comment">// 0</span></div>
<div class="line"> </div>
<div class="line">std::cout << B.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>() << <span class="charliteral">'\n'</span>; <span class="comment">// (empty name)</span></div>
<div class="line">std::cout << B.<a class="code hl_function" href="classtf_1_1Task.html#a1a0afc89e8a6a416c511e74d82df135d">num_successors</a>() << <span class="charliteral">'\n'</span>; <span class="comment">// 0</span></div>
<div class="line">std::cout << B.<a class="code hl_function" href="classtf_1_1Task.html#adefb65d68a64bd8a75364a8801cfec44">num_predecessors</a>() << <span class="charliteral">'\n'</span>; <span class="comment">// 1</span></div>
<div class="ttc" id="aclasstf_1_1Task_html_a08ada0425b490997b6ff7f310107e5e3"><div class="ttname"><a href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">tf::Task::name</a></div><div class="ttdeci">const std::string & name() const</div><div class="ttdoc">queries the name of the task</div><div class="ttdef"><b>Definition</b> task.hpp:1388</div></div>
<div class="ttc" id="aclasstf_1_1Task_html_a1a0afc89e8a6a416c511e74d82df135d"><div class="ttname"><a href="classtf_1_1Task.html#a1a0afc89e8a6a416c511e74d82df135d">tf::Task::num_successors</a></div><div class="ttdeci">size_t num_successors() const</div><div class="ttdoc">queries the number of successors of the task</div><div class="ttdef"><b>Definition</b> task.hpp:1408</div></div>
<div class="ttc" id="aclasstf_1_1Task_html_a2f6f4cec42d016e5eb89390f362ffe99"><div class="ttname"><a href="classtf_1_1Task.html#a2f6f4cec42d016e5eb89390f362ffe99">tf::Task::work</a></div><div class="ttdeci">Task & work(C &&callable)</div><div class="ttdoc">assigns a callable</div><div class="ttdef"><b>Definition</b> task.hpp:1491</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_1Task_html_adefb65d68a64bd8a75364a8801cfec44"><div class="ttname"><a href="classtf_1_1Task.html#adefb65d68a64bd8a75364a8801cfec44">tf::Task::num_predecessors</a></div><div class="ttdeci">size_t num_predecessors() const</div><div class="ttdoc">queries the number of predecessors of the task</div><div class="ttdef"><b>Definition</b> task.hpp:1393</div></div>
</div><!-- fragment --><p>The code above creates a taskflow of two tasks <code>A</code> and <code>B</code>. It then assigns a name and a new callable to task <code>A</code>, and establishes a precedence link to task <code>B</code>. Finally, it queries the task attributes, including names, successor counts, and predecessor counts of <code>A</code> and <code>B</code>.</p>
<p>Taskflow uses 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. For example, the callable to construct a task must be copyable, and thus the code below won't compile:</p>
<div class="fragment"><div class="line">taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([ptr=std::make_unique<int>(1)](){</div>
<div class="line"> std::cout << <span class="stringliteral">"captured unique pointer is not copyable"</span>;</div>
<div class="line">});</div>
</div><!-- fragment --><h1><a class="anchor" id="VisualizeATaskDependencyGraph"></a>
Visualize a Task Dependency Graph</h1>
<p>You can dump a taskflow to a DOT format and visualize the graph using free online tools such as <a href="https://dreampuf.github.io/GraphvizOnline/">GraphvizOnline</a> and <a href="http://www.webgraphviz.com/">WebGraphviz</a>. For example, the code below dumps the taskflow through the standard output <code>std::cout</code>.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include <taskflow/taskflow.hpp></span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main() {</div>
<div class="line"> </div>
<div class="line"> <a class="code hl_class" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// create a task dependency graph</span></div>
<div class="line"> <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>([] () { std::cout << <span class="stringliteral">"Task A\n"</span>; });</div>
<div class="line"> <a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> B = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () { std::cout << <span class="stringliteral">"Task B\n"</span>; });</div>
<div class="line"> <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>([] () { std::cout << <span class="stringliteral">"Task C\n"</span>; });</div>
<div class="line"> tf::Task D = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([] () { std::cout << <span class="stringliteral">"Task D\n"</span>; });</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// add dependency links</span></div>
<div class="line"> A.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(B); </div>
<div class="line"> A.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(C);</div>
<div class="line"> B.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(D);</div>
<div class="line"> C.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(D);</div>
<div class="line"> </div>
<div class="line"> taskflow.<a class="code hl_function" href="classtf_1_1Taskflow.html#ac433018262e44b12c4cc9f0c4748d758">dump</a>(std::cout);</div>
<div class="line">}</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 --><div class="dotgraph">
<iframe scrolling="no" frameborder="0" src="dot_simple.svg" width="323" height="131"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>Visualization helps you understand how tasks and dependencies are structured, making it easier to analyze and debug your taskflow programs.</p>
<h1><a class="anchor" id="TraverseAdjacentTasks"></a>
Traverse Adjacent Tasks</h1>
<p>You can iterate the successor list and the predecessor list of a task by using <a class="el" href="classtf_1_1Task.html#aff13a503d4a3c994eb08cb6f22e1b427" title="applies an visitor callable to each successor of the task">tf::Task::for_each_successor</a> and <a class="el" href="classtf_1_1Task.html#a31d8069d4c0b10b55e68d260c4d28c1f" title="applies an visitor callable to each predecessor of the task">tf::Task::for_each_predecessor</a>, respectively. Both methods take a unary function that takes an argument of type <a class="el" href="classtf_1_1Task.html" title="class to create a task handle over a taskflow node">tf::Task</a> pointing to the task that is being visited.</p>
<div class="fragment"><div class="line"><span class="comment">// traverse all successors of my_task</span></div>
<div class="line">my_task.for_each_successor([s=0] (<a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> successor) <span class="keyword">mutable</span> {</div>
<div class="line"> std::cout << <span class="stringliteral">"successor "</span> << s++ << <span class="charliteral">'\n'</span>;</div>
<div class="line">});</div>
<div class="line"><span class="comment">// traverse all predecessors of my_task</span></div>
<div class="line">my_task.for_each_predecessor([d=0] (<a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> predecessor) <span class="keyword">mutable</span> {</div>
<div class="line"> std::cout << <span class="stringliteral">"predecessor "</span> << d++ << <span class="charliteral">'\n'</span>;</div>
<div class="line">});</div>
</div><!-- fragment --><p>Together with <a class="el" href="classtf_1_1Taskflow.html#a6350d0d6653ae9ae7b94c35e42fffe07" title="applies a visitor to each task in this taskflow">tf::Taskflow::for_each_task</a>, you can traverse a taskflow graph. For example, the code below traverse a taskflow and outputs the successor and the predecessor information of each task:</p>
<div class="fragment"><div class="line"><span class="comment">// traverse every task in taskflow using the given unary function</span></div>
<div class="line">taskflow.<a class="code hl_function" href="classtf_1_1Taskflow.html#a6350d0d6653ae9ae7b94c35e42fffe07">for_each_task</a>([](<a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> task){</div>
<div class="line"> <span class="comment">// print the name of the task</span></div>
<div class="line"> std::cout << “Task ” << task.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>() << ‘\n’;</div>
<div class="line"> <span class="comment">// traverse all successors of the task</span></div>
<div class="line"> task.<a class="code hl_function" href="classtf_1_1Task.html#aff13a503d4a3c994eb08cb6f22e1b427">for_each_successor</a>([](<a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> s) <span class="keyword">mutable</span> {</div>
<div class="line"> std::cout << task.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>() << “->” << s.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>() << ‘ ’;</div>
<div class="line"> });</div>
<div class="line"> std::cout << “\n”;</div>
<div class="line"> <span class="comment">// traverse all predecessors of the task</span></div>
<div class="line"> task.<a class="code hl_function" href="classtf_1_1Task.html#a31d8069d4c0b10b55e68d260c4d28c1f">for_each_predecessor</a>([] (<a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> p) <span class="keyword">mutable</span> {</div>
<div class="line"> std::cout << p.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>() << “->” << task.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>() << ‘ ’;</div>
<div class="line"> });</div>
<div class="line">});</div>
<div class="ttc" id="aclasstf_1_1Task_html_a31d8069d4c0b10b55e68d260c4d28c1f"><div class="ttname"><a href="classtf_1_1Task.html#a31d8069d4c0b10b55e68d260c4d28c1f">tf::Task::for_each_predecessor</a></div><div class="ttdeci">void for_each_predecessor(V &&visitor) const</div><div class="ttdoc">applies an visitor callable to each predecessor of the task</div><div class="ttdef"><b>Definition</b> task.hpp:1460</div></div>
<div class="ttc" id="aclasstf_1_1Task_html_aff13a503d4a3c994eb08cb6f22e1b427"><div class="ttname"><a href="classtf_1_1Task.html#aff13a503d4a3c994eb08cb6f22e1b427">tf::Task::for_each_successor</a></div><div class="ttdeci">void for_each_successor(V &&visitor) const</div><div class="ttdoc">applies an visitor callable to each successor of the task</div><div class="ttdef"><b>Definition</b> task.hpp:1452</div></div>
<div class="ttc" id="aclasstf_1_1Taskflow_html_a6350d0d6653ae9ae7b94c35e42fffe07"><div class="ttname"><a href="classtf_1_1Taskflow.html#a6350d0d6653ae9ae7b94c35e42fffe07">tf::Taskflow::for_each_task</a></div><div class="ttdeci">void for_each_task(V &&visitor) const</div><div class="ttdoc">applies a visitor to each task in this taskflow</div><div class="ttdef"><b>Definition</b> taskflow.hpp:402</div></div>
</div><!-- fragment --><p>If the task contains a subflow (see <a class="el" href="SubflowTasking.html">Subflow Tasking</a>), you can use <a class="el" href="classtf_1_1Task.html#a20a23c08612084e96bda764e06842c3a" title="applies an visitor callable to each subflow task">tf::Task::for_each_subflow_task</a> to iterate all tasks associated with that subflow.</p>
<div class="fragment"><div class="line">my_task.for_each_subflow_task([](<a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> stask){</div>
<div class="line"> std::cout << <span class="stringliteral">"subflow task "</span> << stask.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>() << <span class="charliteral">'\n'</span>;</div>
<div class="line">});</div>
</div><!-- fragment --><h1><a class="anchor" id="AttachUserDataToATask"></a>
Attach User Data to a Task</h1>
<p>You can attach custom data to a task using <a class="el" href="classtf_1_1Task.html#afd82ab6d6518d1142a72c4d2c97ff114" title="assigns pointer to user data">tf::Task::data(void*)</a> and access it using <a class="el" href="classtf_1_1Task.html#a320827cb70295a6fe2cc37691405409c" title="queries pointer to user data">tf::Task::data()</a>. Each node in a taskflow is associated with a C-styled data pointer (i.e., <code>void*</code>) you can use to point to user data and access it in the body of a task callable. The following example attaches an integer to a task and accesses that integer through capturing the data in the callable.</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> my_data = 5;</div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> task = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#acab0b4ac82260f47fdb36a3244ee3aaf">placeholder</a>();</div>
<div class="line">task.<a class="code hl_function" href="classtf_1_1Task.html#afd82ab6d6518d1142a72c4d2c97ff114">data</a>(&my_data)</div>
<div class="line"> .<a class="code hl_function" href="classtf_1_1Task.html#a2f6f4cec42d016e5eb89390f362ffe99">work</a>([task](){</div>
<div class="line"> <span class="keywordtype">int</span> my_date = *<span class="keyword">static_cast<</span><span class="keywordtype">int</span>*<span class="keyword">></span>(task.<a class="code hl_function" href="classtf_1_1Task.html#afd82ab6d6518d1142a72c4d2c97ff114">data</a>());</div>
<div class="line"> std::cout << <span class="stringliteral">"my_data: "</span> << my_data; </div>
<div class="line"> });</div>
<div class="ttc" id="aclasstf_1_1Task_html_afd82ab6d6518d1142a72c4d2c97ff114"><div class="ttname"><a href="classtf_1_1Task.html#afd82ab6d6518d1142a72c4d2c97ff114">tf::Task::data</a></div><div class="ttdeci">Task & data(void *data)</div><div class="ttdoc">assigns pointer to user data</div><div class="ttdef"><b>Definition</b> task.hpp:1520</div></div>
</div><!-- fragment --><p>Notice that you need to create a placeholder task first before assigning it a work callable. Only this way can you capture that task in the lambda and access its attached data in the lambda body. Also, as Taskflow does not manage any user data, it is your responsibility to ensure any attached data stays alive during the??</p>
<h1><a class="anchor" id="UnderstandTheLifetimeOfATask"></a>
Understand the Lifetime of a Task</h1>
<p>A task belongs to a single graph at a time and remains alive as long as that graph exists. The lifetime of a task is particularly important when referring to its callable, including any captured values. When the graph is destroyed or cleaned up, all associated tasks are also destroyed. Consequently, it is your responsibility to keep relevant taskflows alive during their execution. For example, the code below can crash because the taskflow may be destroyed before the executor finishes running it, leaving the executor with dangling references to the task graph.</p>
<div class="fragment"><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"> <a class="code hl_class" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow;</div>
<div class="line"> taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>(</div>
<div class="line"> [](){ std::cout << <span class="stringliteral">"Task A\n"</span>; },</div>
<div class="line"> [](){ std::cout << <span class="stringliteral">"Task B\n"</span>; },</div>
<div class="line"> [](){ std::cout << <span class="stringliteral">"Task C\n"</span>; },</div>
<div class="line"> [](){ std::cout << <span class="stringliteral">"Task D\n"</span>; }</div>
<div class="line"> );</div>
<div class="line"> executor.<a class="code hl_function" href="classtf_1_1Executor.html#a519777f5783981d534e9e53b99712069">run</a>(taskflow);</div>
<div class="line">} <span class="comment">// taskflow is destroyed after the compound statement</span></div>
<div class="line">executor.<a class="code hl_function" href="classtf_1_1Executor.html#ab9aa252f70e9a40020a1e5a89d485b85">wait_for_all</a>();</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_1Executor_html_ab9aa252f70e9a40020a1e5a89d485b85"><div class="ttname"><a href="classtf_1_1Executor.html#ab9aa252f70e9a40020a1e5a89d485b85">tf::Executor::wait_for_all</a></div><div class="ttdeci">void wait_for_all()</div><div class="ttdoc">waits for all tasks to complete</div></div>
</div><!-- fragment --><h1><a class="anchor" id="MoveATaskflow"></a>
Move a Taskflow</h1>
<p>You can construct or assign a taskflow using C++ move semantics. Moving a taskflow to another will result in transferring the underlying graph data structures from one to the other.</p>
<div class="fragment"><div class="line"><a class="code hl_class" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow1, taskflow3;</div>
<div class="line"> </div>
<div class="line">taskflow1.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([](){});</div>
<div class="line"> </div>
<div class="line"><span class="comment">// constructs taskflow2 from taskflow1 using C++ move semantics</span></div>
<div class="line"><a class="code hl_class" href="classtf_1_1Taskflow.html">tf::Taskflow</a> taskflow2(std::move(taskflow1));</div>
<div class="line">assert(taskflow2.num_tasks() == 1 && taskflow1.<a class="code hl_function" href="classtf_1_1Taskflow.html#af4f03bca084deb5c2228ac8936d33649">num_tasks</a>() == 0);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// assigns taskflow2 to taskflow3 using C++ move semantics</span></div>
<div class="line">taskflow3 = std::move(taskflow2);</div>
<div class="line">assert(taskflow3.<a class="code hl_function" href="classtf_1_1Taskflow.html#af4f03bca084deb5c2228ac8936d33649">num_tasks</a>() == 1 && taskflow2.num_tasks() == 0);</div>
<div class="ttc" id="aclasstf_1_1Taskflow_html_af4f03bca084deb5c2228ac8936d33649"><div class="ttname"><a href="classtf_1_1Taskflow.html#af4f03bca084deb5c2228ac8936d33649">tf::Taskflow::num_tasks</a></div><div class="ttdeci">size_t num_tasks() const</div><div class="ttdoc">queries the number of tasks in this taskflow</div><div class="ttdef"><b>Definition</b> taskflow.hpp:376</div></div>
</div><!-- fragment --><p>You can only move a taskflow to another taskflow when it is not being used, such as being executed by an executor. Moving a taskflow that is being used may result in undefined behavior. </p>
</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>