-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathExamplesDynamicDependencyGraph.html
More file actions
248 lines (246 loc) · 17.9 KB
/
Copy pathExamplesDynamicDependencyGraph.html
File metadata and controls
248 lines (246 loc) · 17.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
<!-- 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: Dynamic Dependency Graph</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('ExamplesDynamicDependencyGraph.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">Dynamic Dependency Graph</div></div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul>
<li class="level1">
<a href="#DynamicDependencyGraphProblem">Problem Formulation</a>
</li>
<li class="level1">
<a href="#DynamicDependencyGraphStaticFails">Why Static Construction Fails</a>
</li>
<li class="level1">
<a href="#DynamicDependencyGraphImplementation">Implementation</a>
</li>
<li class="level1">
<a href="#DynamicDependencyGraphBranchingTopology">Branching Based on Runtime Conditions</a>
</li>
</ul>
</div>
<div class="textblock"><p>We study a workload whose task graph topology is determined entirely at runtime, where the number of tasks, their work, and which task depends on which are all read from external data rather than hard-coded. This example showcases dependent-async tasks as the right tool when static task graph construction is not possible.</p>
<h1><a class="anchor" id="DynamicDependencyGraphProblem"></a>
Problem Formulation</h1>
<p>Consider a computational workflow described by an external source such as a configuration file, a database, or the output of a previous computation. Each workflow step has a unique identifier, a list of steps that must complete before it starts, and a function to execute.</p>
<p>For concreteness we represent this as a vector of <code>Step</code> structures:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>Step {</div>
<div class="line"> <span class="keywordtype">int</span> id;</div>
<div class="line"> std::vector<int> deps; <span class="comment">// ids of predecessor steps</span></div>
<div class="line"> std::function<void()> work;</div>
<div class="line">};</div>
</div><!-- fragment --><p>The steps might be loaded at startup from a JSON file, a database query, or any external source. The key point is that neither their count nor their dependency graph is known when the program is compiled.</p>
<h1><a class="anchor" id="DynamicDependencyGraphStaticFails"></a>
Why Static Construction Fails</h1>
<p>The standard Taskflow pattern builds the entire graph with <a class="el" href="classtf_1_1FlowBuilder.html#a4d52a7fe2814b264846a2085e931652c" title="creates a static task">tf::Taskflow::emplace</a>, wires all edges, then calls <a class="el" href="classtf_1_1Executor.html#a519777f5783981d534e9e53b99712069" title="runs a taskflow once">tf::Executor::run</a>. This requires the full graph to be known before execution begins. When the graph depends on external data, all the data must be loaded first, then the entire taskflow is built, then execution starts. This means <em>all</em> the data must be loaded before <em>any</em> computation begins.</p>
<p>For large workflows this is wasteful: if step 0 has no dependencies, it could start executing the moment it is read, while the remaining steps are still being loaded. Dependent-async tasks solve this by scheduling each task the moment it is created, overlapping graph construction with task execution.</p>
<h1><a class="anchor" id="DynamicDependencyGraphImplementation"></a>
Implementation</h1>
<p>We process the workflow one step at a time. For each step we create a dependent-async task whose predecessors are the <a class="el" href="classtf_1_1AsyncTask.html" title="class to hold a dependent asynchronous task with shared ownership">tf::AsyncTask</a> handles of previously created steps. The executor begins running each task the moment its predecessors finish, overlapping graph construction with task execution:</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_1Executor.html">tf::Executor</a> executor;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// workflow loaded from an external source at runtime</span></div>
<div class="line"> <span class="comment">// (hard-coded here for illustration)</span></div>
<div class="line"> <span class="comment">//</span></div>
<div class="line"> <span class="comment">// 0 ──► 1 ──► 3</span></div>
<div class="line"> <span class="comment">// │ ▲</span></div>
<div class="line"> <span class="comment">// └──► 2 ─────┘</span></div>
<div class="line"> <span class="comment">//</span></div>
<div class="line"> <span class="keyword">struct </span>Step {</div>
<div class="line"> <span class="keywordtype">int</span> id;</div>
<div class="line"> std::vector<int> deps;</div>
<div class="line"> };</div>
<div class="line"> </div>
<div class="line"> std::vector<Step> workflow = {</div>
<div class="line"> {0, {} }, <span class="comment">// no predecessors</span></div>
<div class="line"> {1, {0} }, <span class="comment">// runs after step 0</span></div>
<div class="line"> {2, {0} }, <span class="comment">// runs after step 0</span></div>
<div class="line"> {3, {1, 2} }, <span class="comment">// runs after steps 1 and 2</span></div>
<div class="line"> };</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// map from step id to its AsyncTask handle</span></div>
<div class="line"> std::vector<tf::AsyncTask> handles(workflow.size());</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">for</span>(<span class="keyword">const</span> <span class="keyword">auto</span>& step : workflow) {</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// collect predecessor handles</span></div>
<div class="line"> std::vector<tf::AsyncTask> preds;</div>
<div class="line"> <span class="keywordflow">for</span>(<span class="keywordtype">int</span> dep : step.deps) {</div>
<div class="line"> preds.push_back(handles[dep]);</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// create and immediately schedule the task,</span></div>
<div class="line"> <span class="comment">// which runs as soon as all predecessors complete</span></div>
<div class="line"> handles[step.id] = executor.<a class="code hl_function" href="classtf_1_1Executor.html#a0af0918b7179f9e42945fb407e0bad65">silent_dependent_async</a>(</div>
<div class="line"> [<span class="keywordtype">id</span> = step.id]() {</div>
<div class="line"> printf(<span class="stringliteral">"executing step %d\n"</span>, id);</div>
<div class="line"> },</div>
<div class="line"> preds.begin(), preds.end()</div>
<div class="line"> );</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> executor.<a class="code hl_function" href="classtf_1_1Executor.html#ab9aa252f70e9a40020a1e5a89d485b85">wait_for_all</a>();</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line">}</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_a0af0918b7179f9e42945fb407e0bad65"><div class="ttname"><a href="classtf_1_1Executor.html#a0af0918b7179f9e42945fb407e0bad65">tf::Executor::silent_dependent_async</a></div><div class="ttdeci">tf::AsyncTask silent_dependent_async(F &&func, Tasks &&... tasks)</div><div class="ttdoc">runs the given function asynchronously when the given predecessors finish</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 --><p>The key loop processes one step at a time. By the time we create the task for step 3, step 0 may already be running (or even done), because its task was submitted with no predecessors and began executing immediately. The graph is built and executed simultaneously rather than sequentially.</p>
<h1><a class="anchor" id="DynamicDependencyGraphBranchingTopology"></a>
Branching Based on Runtime Conditions</h1>
<p>The topology itself can branch based on properties evaluated during execution. In the example below, the graph structure after step 0 depends on a condition that is only known after step 0 completes:</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"><span class="comment">// step 0: load or compute something whose result drives the rest of the graph</span></div>
<div class="line"><span class="keyword">auto</span> [step0, fu0] = executor.<a class="code hl_function" href="classtf_1_1Executor.html#a3278e2611e43b80b65ef10a3391ddcc3">dependent_async</a>([]() -> <span class="keywordtype">int</span> {</div>
<div class="line"> <span class="keywordflow">return</span> compute_initial_result();</div>
<div class="line">});</div>
<div class="line"> </div>
<div class="line"><span class="comment">// wait cooperatively until step 0 finishes</span></div>
<div class="line">executor.<a class="code hl_function" href="classtf_1_1Executor.html#a0fc6eb19f168dc4a9cd0a7c6187c1d2d">corun_until</a>([&]() { <span class="keywordflow">return</span> step0.is_done(); });</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> result = fu0.get();</div>
<div class="line"> </div>
<div class="line"><span class="keywordflow">if</span>(result > 0) {</div>
<div class="line"> <span class="comment">// branch A: two parallel tasks followed by a merge</span></div>
<div class="line"> <span class="keyword">auto</span> [A1, fuA1] = executor.<a class="code hl_function" href="classtf_1_1Executor.html#a3278e2611e43b80b65ef10a3391ddcc3">dependent_async</a>([]() {</div>
<div class="line"> printf(<span class="stringliteral">"branch A: task 1\n"</span>);</div>
<div class="line"> }, step0);</div>
<div class="line"> </div>
<div class="line"> <span class="keyword">auto</span> [A2, fuA2] = executor.<a class="code hl_function" href="classtf_1_1Executor.html#a3278e2611e43b80b65ef10a3391ddcc3">dependent_async</a>([]() {</div>
<div class="line"> printf(<span class="stringliteral">"branch A: task 2\n"</span>);</div>
<div class="line"> }, step0);</div>
<div class="line"> </div>
<div class="line"> executor.<a class="code hl_function" href="classtf_1_1Executor.html#a0af0918b7179f9e42945fb407e0bad65">silent_dependent_async</a>([]() {</div>
<div class="line"> printf(<span class="stringliteral">"branch A: merge\n"</span>);</div>
<div class="line"> }, A1, A2);</div>
<div class="line">}</div>
<div class="line"><span class="keywordflow">else</span> {</div>
<div class="line"> <span class="comment">// branch B: a single deeper processing step</span></div>
<div class="line"> executor.<a class="code hl_function" href="classtf_1_1Executor.html#a0af0918b7179f9e42945fb407e0bad65">silent_dependent_async</a>([]() {</div>
<div class="line"> printf(<span class="stringliteral">"branch B: deep processing\n"</span>);</div>
<div class="line"> }, step0);</div>
<div class="line">}</div>
<div class="line"> </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_a0fc6eb19f168dc4a9cd0a7c6187c1d2d"><div class="ttname"><a href="classtf_1_1Executor.html#a0fc6eb19f168dc4a9cd0a7c6187c1d2d">tf::Executor::corun_until</a></div><div class="ttdeci">void corun_until(P &&predicate)</div><div class="ttdoc">keeps running the work-stealing loop until the predicate returns true</div></div>
<div class="ttc" id="aclasstf_1_1Executor_html_a3278e2611e43b80b65ef10a3391ddcc3"><div class="ttname"><a href="classtf_1_1Executor.html#a3278e2611e43b80b65ef10a3391ddcc3">tf::Executor::dependent_async</a></div><div class="ttdeci">auto dependent_async(F &&func, Tasks &&... tasks)</div><div class="ttdoc">runs the given function asynchronously when the given predecessors finish</div></div>
</div><!-- fragment --><p>This pattern is impossible to express with a static <a class="el" href="classtf_1_1Taskflow.html" title="class to create a taskflow object">tf::Taskflow</a>, where there is no way to know which branch to build before <code>compute_initial_result</code> has run. Dependent-async tasks make the runtime decision a natural part of the program's control flow.</p>
<dl class="section note"><dt>Note</dt><dd><a class="el" href="classtf_1_1Executor.html#a0fc6eb19f168dc4a9cd0a7c6187c1d2d" title="keeps running the work-stealing loop until the predicate returns true">tf::Executor::corun_until</a> is used here instead of <code>fu0.get()</code> to keep the calling thread active in the work-stealing loop. If the calling thread is itself one of the executor's workers, calling <code>fu0.get()</code> would block the thread, reducing the available parallelism for the tasks it could otherwise be executing. See <a class="el" href="DependentAsyncTasking.html">Asynchronous Tasking with Dependencies</a> for a full discussion. </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="Examples.html">Learning from Examples</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>