-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathComposableTasking.html
More file actions
258 lines (256 loc) · 28.2 KB
/
Copy pathComposableTasking.html
File metadata and controls
258 lines (256 loc) · 28.2 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
<!-- 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: Composable 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>
<script type="text/javascript" src="darkmode_toggle.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('ComposableTasking.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">Composable Tasking</div></div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul>
<li class="level1">
<a href="#ComposeATaskflow">Compose a Taskflow</a>
</li>
<li class="level1">
<a href="#CreateAModuleTaskFromATaskflow">Create a Module Task from a Taskflow</a>
</li>
<li class="level1">
<a href="#CreateACustomComposableGraph">Create a Custom Composable Graph</a>
</li>
<li class="level1">
<a href="#CreateAnAdoptedModuleTask">Create an Adopted Module Task</a>
</li>
</ul>
</div>
<div class="textblock"><p>Composition is a key to improve the programmability of a complex workflow. This chapter describes how to create a large parallel graph through composition of modular and reusable blocks that are easier to optimize.</p>
<h1><a class="anchor" id="ComposeATaskflow"></a>
Compose a Taskflow</h1>
<p>A powerful feature of Taskflow is its <em>composable</em> interface. You can break down a large parallel workload into smaller pieces each designed to run a specific task dependency graph. This largely facilitates the <em>modularity</em> of writing a parallel task program.</p>
<div class="fragment"><div class="line"><span class="comment">// f1 has three independent tasks</span></div>
<div class="line"><a class="code hl_class" href="classtf_1_1Taskflow.html">tf::Taskflow</a> f1;</div>
<div class="line">f1.<a class="code hl_function" href="classtf_1_1Taskflow.html#ad5706e5819aa01a63c4aa2e3485546b9">name</a>(<span class="stringliteral">"F1"</span>);</div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> f1A = f1.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([&](){ std::cout << <span class="stringliteral">"F1 TaskA\n"</span>; });</div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> f1B = f1.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([&](){ std::cout << <span class="stringliteral">"F1 TaskB\n"</span>; });</div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> f1C = f1.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([&](){ std::cout << <span class="stringliteral">"F1 TaskC\n"</span>; });</div>
<div class="line"> </div>
<div class="line">f1A.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">"f1A"</span>);</div>
<div class="line">f1B.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">"f1B"</span>);</div>
<div class="line">f1C.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">"f1C"</span>);</div>
<div class="line">f1A.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(f1C);</div>
<div class="line">f1B.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(f1C);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// f2A ---</span></div>
<div class="line"><span class="comment">// |----> f2C ----> f1_module_task ----> f2D</span></div>
<div class="line"><span class="comment">// f2B --- </span></div>
<div class="line">tf::Taskflow f2;</div>
<div class="line">f2.<a class="code hl_function" href="classtf_1_1Taskflow.html#ad5706e5819aa01a63c4aa2e3485546b9">name</a>(<span class="stringliteral">"F2"</span>);</div>
<div class="line">tf::Task f2A = f2.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([&](){ std::cout << <span class="stringliteral">" F2 TaskA\n"</span>; });</div>
<div class="line">tf::Task f2B = f2.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([&](){ std::cout << <span class="stringliteral">" F2 TaskB\n"</span>; });</div>
<div class="line">tf::Task f2C = f2.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([&](){ std::cout << <span class="stringliteral">" F2 TaskC\n"</span>; });</div>
<div class="line">tf::Task f2D = f2.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([&](){ std::cout << <span class="stringliteral">" F2 TaskD\n"</span>; });</div>
<div class="line"> </div>
<div class="line">f2A.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">"f2A"</span>);</div>
<div class="line">f2B.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">"f2B"</span>);</div>
<div class="line">f2C.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">"f2C"</span>);</div>
<div class="line">f2D.<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">"f2D"</span>);</div>
<div class="line"> </div>
<div class="line">f2A.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(f2C);</div>
<div class="line">f2B.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(f2C);</div>
<div class="line"> </div>
<div class="line">tf::Task f1_module_task = f2.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a">composed_of</a>(f1).<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">"module"</span>);</div>
<div class="line">f2C.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(f1_module_task);</div>
<div class="line">f1_module_task.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(f2D);</div>
<div class="line"> </div>
<div class="line">f2.<a class="code hl_function" href="classtf_1_1Taskflow.html#ac433018262e44b12c4cc9f0c4748d758">dump</a>(std::cout);</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:1781</div></div>
<div class="ttc" id="aclasstf_1_1FlowBuilder_html_ac6f22228d4c2ea2e643c4b0d42c0e92a"><div class="ttname"><a href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a">tf::FlowBuilder::composed_of</a></div><div class="ttdeci">Task composed_of(T &object)</div><div class="ttdoc">creates a module task for the target object</div><div class="ttdef"><b>Definition</b> flow_builder.hpp:1831</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_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:1435</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:1305</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 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 class="ttc" id="aclasstf_1_1Taskflow_html_ad5706e5819aa01a63c4aa2e3485546b9"><div class="ttname"><a href="classtf_1_1Taskflow.html#ad5706e5819aa01a63c4aa2e3485546b9">tf::Taskflow::name</a></div><div class="ttdeci">void name(const std::string &)</div><div class="ttdoc">assigns a new name to this taskflow</div><div class="ttdef"><b>Definition</b> taskflow.hpp:386</div></div>
</div><!-- fragment --><div class="dotgraph">
<iframe scrolling="no" frameborder="0" src="dot_composition_static_1.svg" width="443" height="423"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>The above example first constructs a taskflow consisting of three tasks, <code>f1A</code>, <code>f1B</code>, and <code>f1C</code>, where <code>f1A</code> and <code>f1B</code> execute before <code>f1C</code>. It then creates a second taskflow with four tasks, <code>f2A</code>, <code>f2B</code>, <code>f2C</code>, and <code>f2D</code>. The first taskflow is encapsulated as a module task using <a class="el" href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" title="creates a module task for the target object">Taskflow::composed_of</a>, allowing it to be embedded within the second taskflow. Dependencies are then established so that <code>f2C</code> must complete before the module task begins, and the module task must finish before <code>f2D</code> executes, thereby integrating the two taskflows into a single execution graph with well-defined ordering constraints.</p>
<h1><a class="anchor" id="CreateAModuleTaskFromATaskflow"></a>
Create a Module Task from a Taskflow</h1>
<p>The task created from <a class="el" href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" title="creates a module task for the target object">Taskflow::composed_of</a> is a <em>module</em> task that runs on a pre-defined taskflow. A module task does not own the taskflow but maintains a soft mapping to the taskflow. You can create multiple module tasks from the same taskflow but only one module task can run at one time. For example, the following composition is valid. Even though the two module tasks <code>module1</code> and <code>module2</code> refer to the same taskflow <code>F1</code>, the dependency link prevents <code>F1</code> from multiple executions at the same time.</p>
<div class="dotgraph">
<iframe scrolling="no" frameborder="0" src="dot_composition_static_2.svg" width="443" height="519"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>However, the following composition is <em>invalid</em>. Both module tasks refer to the same taskflow. They can not run at the same time because they are associated with the same graph.</p>
<div class="dotgraph">
<iframe scrolling="no" frameborder="0" src="dot_composition_static_invalid.svg" width="658" height="423"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<h1><a class="anchor" id="CreateACustomComposableGraph"></a>
Create a Custom Composable Graph</h1>
<p>Taskflow allows you to create a custom graph object that can participate in the scheduling using composition. To become a module task, your class <code>T</code> must define the method <code>T::graph()</code> that returns a reference to the <a class="el" href="classtf_1_1Graph.html" title="class to create a graph object">tf::Graph</a> object managed by <code>T</code>. The following example defines a custom graph object that can be assembled in a taskflow through composition:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>CustomGraph {</div>
<div class="line"> <a class="code hl_class" href="classtf_1_1Graph.html">tf::Graph</a> graph;</div>
<div class="line"> CustomGraph() {</div>
<div class="line"> <a class="code hl_class" href="classtf_1_1FlowBuilder.html">tf::FlowBuilder</a> builder(graph); <span class="comment">// inherit all task builders in tf::Taskflow</span></div>
<div class="line"> <a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> task = builder.emplace([](){</div>
<div class="line"> std::cout << <span class="stringliteral">"a task\n"</span>; <span class="comment">// static task</span></div>
<div class="line"> });</div>
<div class="line"> }</div>
<div class="line"> <span class="comment">// returns a reference to the graph for taskflow composition</span></div>
<div class="line"> Graph& graph() { <span class="keywordflow">return</span> graph; }</div>
<div class="line">};</div>
<div class="line">CustomGraph obj;</div>
<div class="line">tf::Task comp = taskflow.composed_of(obj);</div>
<div class="ttc" id="aclasstf_1_1FlowBuilder_html"><div class="ttname"><a href="classtf_1_1FlowBuilder.html">tf::FlowBuilder</a></div><div class="ttdoc">class to build a task dependency graph</div><div class="ttdef"><b>Definition</b> flow_builder.hpp:114</div></div>
<div class="ttc" id="aclasstf_1_1Graph_html"><div class="ttname"><a href="classtf_1_1Graph.html">tf::Graph</a></div><div class="ttdoc">class to create a graph object</div><div class="ttdef"><b>Definition</b> graph.hpp:47</div></div>
</div><!-- fragment --><p>The above code defines a custom graph that can participate in taskflow composition. The graph object is represented using <a class="el" href="classtf_1_1Graph.html" title="class to create a graph object">tf::Graph</a>, and its constructor builds the internal task graph through <a class="el" href="classtf_1_1FlowBuilder.html" title="class to build a task dependency graph">tf::FlowBuilder</a>. To support composition, the graph implements the required interface method (<code><a class="el" href="classtf_1_1Graph.html" title="class to create a graph object">Graph</a>& graph()</code>) that exposes its internal structure to the Taskflow runtime. Or, you can simply expose the <code>graph</code> and pass it to <a class="el" href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" title="creates a module task for the target object">tf::FlowBuilder::composed_of</a> without defining an additional struct.</p>
<div class="fragment"><div class="line"><a class="code hl_class" href="classtf_1_1Graph.html">tf::Graph</a> graph;</div>
<div class="line"><a class="code hl_class" href="classtf_1_1FlowBuilder.html">tf::FlowBuilder</a> builder(graph); <span class="comment">// inherit all task builders in tf::Taskflow</span></div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> task = builder.emplace([](){</div>
<div class="line"> std::cout << <span class="stringliteral">"a task\n"</span>; <span class="comment">// static task</span></div>
<div class="line">});</div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> comp = taskflow.<a class="code hl_function" href="classtf_1_1Task.html#ab38be520fe700cb4ca1f312308a95585">composed_of</a>(obj);</div>
<div class="ttc" id="aclasstf_1_1Task_html_ab38be520fe700cb4ca1f312308a95585"><div class="ttname"><a href="classtf_1_1Task.html#ab38be520fe700cb4ca1f312308a95585">tf::Task::composed_of</a></div><div class="ttdeci">Task & composed_of(T &object)</div><div class="ttdoc">creates a module task from a taskflow</div><div class="ttdef"><b>Definition</b> task.hpp:1337</div></div>
</div><!-- fragment --><p>With this interface in place, the custom graph can then be instantiated as a module task within a larger taskflow, enabling it to be seamlessly composed and scheduled alongside other tasks.</p>
<dl class="section note"><dt>Note</dt><dd>Users are responsible for ensuring the given target remains valid throughout its execution. The executor does not assume ownership of the target object.</dd></dl>
<h1><a class="anchor" id="CreateAnAdoptedModuleTask"></a>
Create an Adopted Module Task</h1>
<p>Unlike <a class="el" href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" title="creates a module task for the target object">tf::FlowBuilder::composed_of</a>, which holds a <em>reference</em> to an externally-owned graph and requires the caller to manage its lifetime, <a class="el" href="classtf_1_1FlowBuilder.html#a35f6503341d7066ef642e6b1a9d81c2c" title="creates a module task from a graph by taking over its ownership">tf::FlowBuilder::adopt</a> transfers <em>ownership</em> of a <a class="el" href="classtf_1_1Graph.html" title="class to create a graph object">tf::Graph</a> into the task via move semantics. Once adopted, the graph is owned and managed by the executor for the duration of its execution, and the caller must not access the moved-from graph afterward. The following example creates a graph through <a class="el" href="classtf_1_1FlowBuilder.html" title="class to build a task dependency graph">tf::FlowBuilder</a> and moves it to a taskflow as an adopted module task:</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"> </div>
<div class="line"><span class="comment">// build a graph independently using FlowBuilder</span></div>
<div class="line"><a class="code hl_class" href="classtf_1_1Graph.html">tf::Graph</a> g;</div>
<div class="line"><a class="code hl_class" href="classtf_1_1FlowBuilder.html">tf::FlowBuilder</a> fb(g);</div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> t1 = fb.emplace([]{ std::cout << <span class="stringliteral">"task A\n"</span>; }).name(<span class="stringliteral">"A"</span>);</div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> t2 = fb.emplace([]{ std::cout << <span class="stringliteral">"task B\n"</span>; }).name(<span class="stringliteral">"B"</span>);</div>
<div class="line">t1.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(t2);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// adopt the graph into the taskflow — ownership transfers here</span></div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> module = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a35f6503341d7066ef642e6b1a9d81c2c">adopt</a>(std::move(g)).<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">"adopted module task"</span>);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// g is now in a moved-from state and must not be used</span></div>
<div class="line"><a class="code hl_class" href="classtf_1_1Executor.html">tf::Executor</a> executor;</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_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_a35f6503341d7066ef642e6b1a9d81c2c"><div class="ttname"><a href="classtf_1_1FlowBuilder.html#a35f6503341d7066ef642e6b1a9d81c2c">tf::FlowBuilder::adopt</a></div><div class="ttdeci">Task adopt(Graph &&graph)</div><div class="ttdoc">creates a module task from a graph by taking over its ownership</div><div class="ttdef"><b>Definition</b> flow_builder.hpp:1838</div></div>
</div><!-- fragment --><p>The adopted module task can participate in the taskflow's dependency graph just like any other module task created via <a class="el" href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" title="creates a module task for the target object">tf::FlowBuilder::composed_of</a>. You can chain dependencies before and after it in the usual way:</p>
<div class="fragment"><div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> before = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([]{ std::cout << <span class="stringliteral">"before\n"</span>; }).name(<span class="stringliteral">"before"</span>);</div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> after = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([]{ std::cout << <span class="stringliteral">"after\n"</span>; }).name(<span class="stringliteral">"after"</span>);</div>
<div class="line"> </div>
<div class="line"><a class="code hl_class" href="classtf_1_1Graph.html">tf::Graph</a> g;</div>
<div class="line"><a class="code hl_class" href="classtf_1_1FlowBuilder.html">tf::FlowBuilder</a>{g}.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c">emplace</a>([]{ std::cout << <span class="stringliteral">"inside adopted graph\n"</span>; });</div>
<div class="line">tf::Task module = taskflow.<a class="code hl_function" href="classtf_1_1FlowBuilder.html#a35f6503341d7066ef642e6b1a9d81c2c">adopt</a>(std::move(g)).<a class="code hl_function" href="classtf_1_1Task.html#a08ada0425b490997b6ff7f310107e5e3">name</a>(<span class="stringliteral">"module"</span>);</div>
<div class="line"> </div>
<div class="line">before.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(module);</div>
<div class="line"><span class="keyword">module</span>.precede(after);</div>
<div class="line"> </div>
<div class="line">tf::Executor executor;</div>
<div class="line">executor.<a class="code hl_function" href="classtf_1_1Executor.html#a519777f5783981d534e9e53b99712069">run</a>(taskflow).wait();</div>
</div><!-- fragment --><dl class="section note"><dt>Note</dt><dd>The key distinction between <a class="el" href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" title="creates a module task for the target object">tf::Taskflow::composed_of</a> and <a class="el" href="classtf_1_1FlowBuilder.html#a35f6503341d7066ef642e6b1a9d81c2c" title="creates a module task from a graph by taking over its ownership">tf::Taskflow::adopt</a> is ownership. Use <code>composed_of</code> when the graph is long-lived and shared across multiple taskflows or multiple runs. Use <code>adopt</code> when you want to transfer a graph into the taskflow and let the executor manage its lifetime. </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>