forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComposableTasking.html
454 lines (454 loc) · 38.4 KB
/
ComposableTasking.html
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Cookbook » Composable Tasking | Taskflow QuickStart</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,400i,600,600i%7CSource+Code+Pro:400,400i,600" />
<link rel="stylesheet" href="m-dark+documentation.compiled.css" />
<link rel="icon" href="favicon.ico" type="image/vnd.microsoft.icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#22272e" />
</head>
<body>
<header><nav id="navigation">
<div class="m-container">
<div class="m-row">
<span id="m-navbar-brand" class="m-col-t-8 m-col-m-none m-left-m">
<a href="https://taskflow.github.io"><img src="taskflow_logo.png" alt="" />Taskflow</a> <span class="m-breadcrumb">|</span> <a href="index.html" class="m-thin">QuickStart</a>
</span>
<div class="m-col-t-4 m-hide-m m-text-right m-nopadr">
<a href="#search" class="m-doc-search-icon" title="Search" onclick="return showSearch()"><svg style="height: 0.9rem;" viewBox="0 0 16 16">
<path id="m-doc-search-icon-path" d="m6 0c-3.31 0-6 2.69-6 6 0 3.31 2.69 6 6 6 1.49 0 2.85-0.541 3.89-1.44-0.0164 0.338 0.147 0.759 0.5 1.15l3.22 3.79c0.552 0.614 1.45 0.665 2 0.115 0.55-0.55 0.499-1.45-0.115-2l-3.79-3.22c-0.392-0.353-0.812-0.515-1.15-0.5 0.895-1.05 1.44-2.41 1.44-3.89 0-3.31-2.69-6-6-6zm0 1.56a4.44 4.44 0 0 1 4.44 4.44 4.44 4.44 0 0 1-4.44 4.44 4.44 4.44 0 0 1-4.44-4.44 4.44 4.44 0 0 1 4.44-4.44z"/>
</svg></a>
<a id="m-navbar-show" href="#navigation" title="Show navigation"></a>
<a id="m-navbar-hide" href="#" title="Hide navigation"></a>
</div>
<div id="m-navbar-collapse" class="m-col-t-12 m-show-m m-col-m-none m-right-m">
<div class="m-row">
<ol class="m-col-t-6 m-col-m-none">
<li><a href="pages.html">Handbook</a></li>
<li><a href="namespaces.html">Namespaces</a></li>
</ol>
<ol class="m-col-t-6 m-col-m-none" start="3">
<li><a href="annotated.html">Classes</a></li>
<li><a href="files.html">Files</a></li>
<li class="m-show-m"><a href="#search" class="m-doc-search-icon" title="Search" onclick="return showSearch()"><svg style="height: 0.9rem;" viewBox="0 0 16 16">
<use href="#m-doc-search-icon-path" />
</svg></a></li>
</ol>
</div>
</div>
</div>
</div>
</nav></header>
<main><article>
<div class="m-container m-container-inflatable">
<div class="m-row">
<div class="m-col-l-10 m-push-l-1">
<h1>
<span class="m-breadcrumb"><a href="Cookbook.html">Cookbook</a> »</span>
Composable Tasking
</h1>
<nav class="m-block m-default">
<h3>Contents</h3>
<ul>
<li><a href="#ComposeATaskflow">Compose a Taskflow</a></li>
<li><a href="#CreateAModuleTask">Create a Module Task</a></li>
<li><a href="#CreateACustomComposableGraph">Create a Custom Composable Graph</a></li>
</ul>
</nav>
<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><section id="ComposeATaskflow"><h2><a href="#ComposeATaskflow">Compose a Taskflow</a></h2><p>A powerful feature of <a href="classtf_1_1Taskflow.html" class="m-doc">tf::<wbr />Taskflow</a> 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><pre class="m-code"><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="c1">// f1 has three independent tasks</span>
<span class="w"> </span><span class="mi">2</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">f1</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="mi">3</span><span class="o">:</span><span class="w"> </span><span class="n">f1</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">"F1"</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="mi">4</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f1A</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f1</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">"F1 TaskA</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w"> </span><span class="mi">5</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f1B</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f1</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">"F1 TaskB</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w"> </span><span class="mi">6</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f1C</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f1</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">"F1 TaskC</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w"> </span><span class="mi">7</span><span class="o">:</span><span class="w"> </span>
<span class="w"> </span><span class="mi">8</span><span class="o">:</span><span class="w"> </span><span class="n">f1A</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">"f1A"</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="mi">9</span><span class="o">:</span><span class="w"> </span><span class="n">f1B</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">"f1B"</span><span class="p">);</span><span class="w"></span>
<span class="mi">10</span><span class="o">:</span><span class="w"> </span><span class="n">f1C</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">"f1C"</span><span class="p">);</span><span class="w"></span>
<span class="mi">11</span><span class="o">:</span><span class="w"> </span><span class="n">f1A</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f1C</span><span class="p">);</span><span class="w"></span>
<span class="mi">12</span><span class="o">:</span><span class="w"> </span><span class="n">f1B</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f1C</span><span class="p">);</span><span class="w"></span>
<span class="mi">13</span><span class="o">:</span><span class="w"></span>
<span class="mi">14</span><span class="o">:</span><span class="w"> </span><span class="c1">// f2A ---</span>
<span class="mi">15</span><span class="o">:</span><span class="w"> </span><span class="c1">// |----> f2C ----> f1_module_task ----> f2D</span>
<span class="mi">16</span><span class="o">:</span><span class="w"> </span><span class="c1">// f2B --- </span>
<span class="mi">17</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">f2</span><span class="p">;</span><span class="w"></span>
<span class="mi">18</span><span class="o">:</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">"F2"</span><span class="p">);</span><span class="w"></span>
<span class="mi">19</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f2A</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">" F2 TaskA</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="mi">20</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f2B</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">" F2 TaskB</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="mi">21</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f2C</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">" F2 TaskC</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="mi">22</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f2D</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">" F2 TaskD</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="mi">23</span><span class="o">:</span><span class="w"> </span>
<span class="mi">24</span><span class="o">:</span><span class="w"> </span><span class="n">f2A</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">"f2A"</span><span class="p">);</span><span class="w"></span>
<span class="mi">25</span><span class="o">:</span><span class="w"> </span><span class="n">f2B</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">"f2B"</span><span class="p">);</span><span class="w"></span>
<span class="mi">26</span><span class="o">:</span><span class="w"> </span><span class="n">f2C</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">"f2C"</span><span class="p">);</span><span class="w"></span>
<span class="mi">27</span><span class="o">:</span><span class="w"> </span><span class="n">f2D</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">"f2D"</span><span class="p">);</span><span class="w"></span>
<span class="mi">28</span><span class="o">:</span><span class="w"></span>
<span class="mi">29</span><span class="o">:</span><span class="w"> </span><span class="n">f2A</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f2C</span><span class="p">);</span><span class="w"></span>
<span class="mi">30</span><span class="o">:</span><span class="w"> </span><span class="n">f2B</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f2C</span><span class="p">);</span><span class="w"></span>
<span class="mi">31</span><span class="o">:</span><span class="w"></span>
<span class="mi">32</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f1_module_task</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">composed_of</span><span class="p">(</span><span class="n">f1</span><span class="p">).</span><span class="n">name</span><span class="p">(</span><span class="s">"module"</span><span class="p">);</span><span class="w"></span>
<span class="mi">33</span><span class="o">:</span><span class="w"> </span><span class="n">f2C</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f1_module_task</span><span class="p">);</span><span class="w"></span>
<span class="mi">34</span><span class="o">:</span><span class="w"> </span><span class="n">f1_module_task</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f2D</span><span class="p">);</span><span class="w"></span>
<span class="mi">35</span><span class="o">:</span><span class="w"></span>
<span class="mi">36</span><span class="o">:</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">dump</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="p">);</span><span class="w"></span></pre><div class="m-graph"><svg style="width: 24.312rem; height: 20.000rem;" viewBox="0.00 0.00 389.00 320.31">
<g transform="scale(1 1) rotate(0) translate(4 316.3087)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster_p0x7ffee9223970</title>
<polygon points="8,-8 8,-304.3087 212,-304.3087 212,-8 8,-8"/>
<text text-anchor="middle" x="110" y="-287.5087">Taskflow: F2</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7ffee92238d0</title>
<polygon points="220,-152.7696 220,-304.3087 373,-304.3087 373,-152.7696 220,-152.7696"/>
<text text-anchor="middle" x="296.5" y="-287.5087">Taskflow: F1</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402b60</title>
<ellipse cx="148" cy="-251.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="148" y="-248.1239">f2A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402d80</title>
<ellipse cx="110" cy="-179.1543" rx="30.3115" ry="18.2703"/>
<text text-anchor="middle" x="110" y="-175.3543">f2C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402b60->p0x7f816f402d80</title>
<path d="M138.8013,-234.3084C134.3533,-225.7906 128.9185,-215.383 123.9732,-205.9128"/>
<polygon points="126.9771,-204.1039 119.2457,-196.8598 120.7722,-207.3441 126.9771,-204.1039"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402fa0</title>
<polygon points="203.5,-124.7696 20.5,-124.7696 16.5,-120.7696 16.5,-88.7696 199.5,-88.7696 203.5,-92.7696 203.5,-124.7696"/>
<polyline points="199.5,-120.7696 16.5,-120.7696 "/>
<polyline points="199.5,-120.7696 199.5,-88.7696 "/>
<polyline points="199.5,-120.7696 203.5,-124.7696 "/>
<text text-anchor="middle" x="110" y="-102.9696">module [Taskflow: F1]</text>
</g>
<g class="m-edge">
<title>p0x7f816f402d80->p0x7f816f402fa0</title>
<path d="M110,-160.5135C110,-152.8556 110,-143.8146 110,-135.3544"/>
<polygon points="113.5001,-135.0966 110,-125.0967 106.5001,-135.0967 113.5001,-135.0966"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402c70</title>
<ellipse cx="71" cy="-251.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="71" y="-248.1239">f2B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402c70->p0x7f816f402d80</title>
<path d="M80.4408,-234.3084C85.0058,-225.7906 90.5837,-215.383 95.6591,-205.9128"/>
<polygon points="98.872,-207.3271 100.511,-196.8598 92.7023,-204.0205 98.872,-207.3271"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402e90</title>
<ellipse cx="110" cy="-34.3848" rx="31.2258" ry="18.2703"/>
<text text-anchor="middle" x="110" y="-30.5848">f2D</text>
</g>
<g class="m-edge">
<title>p0x7f816f402fa0->p0x7f816f402e90</title>
<path d="M110,-88.5038C110,-80.7623 110,-71.5567 110,-62.9533"/>
<polygon points="113.5001,-62.8964 110,-52.8964 106.5001,-62.8965 113.5001,-62.8964"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402830</title>
<ellipse cx="335" cy="-251.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="335" y="-248.1239">f1A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402a50</title>
<ellipse cx="296" cy="-179.1543" rx="30.3115" ry="18.2703"/>
<text text-anchor="middle" x="296" y="-175.3543">f1C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402830->p0x7f816f402a50</title>
<path d="M325.5592,-234.3084C320.9942,-225.7906 315.4163,-215.383 310.3409,-205.9128"/>
<polygon points="313.2977,-204.0205 305.489,-196.8598 307.128,-207.3271 313.2977,-204.0205"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402940</title>
<ellipse cx="258" cy="-251.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="258" y="-248.1239">f1B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402940->p0x7f816f402a50</title>
<path d="M267.1987,-234.3084C271.6467,-225.7906 277.0815,-215.383 282.0268,-205.9128"/>
<polygon points="285.2278,-207.3441 286.7543,-196.8598 279.0229,-204.1039 285.2278,-207.3441"/>
</g>
</g>
</svg>
</div><p>Debrief:</p><ul><li>Lines 1-12 create a taskflow of three tasks f1A, f1B, and f1C with f1A and f1B preceding f1C</li><li>Lines 17-30 create a taskflow of four tasks f2A, f2B, f2C, and f2D</li><li>Line 32 creates a module task from taskflow f1 through the method <a href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" class="m-doc">Taskflow::<wbr />composed_of</a></li><li>Line 33 enforces task f2C to run before the module task</li><li>Line 34 enforces the module task to run before task f2D</li></ul></section><section id="CreateAModuleTask"><h2><a href="#CreateAModuleTask">Create a Module Task</a></h2><p>The task created from <a href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" class="m-doc">Taskflow::<wbr />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="m-graph"><svg style="width: 24.312rem; height: 24.500rem;" viewBox="0.00 0.00 389.00 392.31">
<g transform="scale(1 1) rotate(0) translate(4 388.3087)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster_p0x7ffee9223970</title>
<polygon points="8,-8 8,-376.3087 212,-376.3087 212,-8 8,-8"/>
<text text-anchor="middle" x="110" y="-359.5087">Taskflow: F2</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7ffee92238d0</title>
<polygon points="220,-224.7696 220,-376.3087 373,-376.3087 373,-224.7696 220,-224.7696"/>
<text text-anchor="middle" x="296.5" y="-359.5087">Taskflow: F1</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402b60</title>
<ellipse cx="148" cy="-323.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="148" y="-320.1239">f2A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402d80</title>
<ellipse cx="110" cy="-251.1543" rx="30.3115" ry="18.2703"/>
<text text-anchor="middle" x="110" y="-247.3543">f2C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402b60->p0x7f816f402d80</title>
<path d="M138.8013,-306.3084C134.3533,-297.7906 128.9185,-287.383 123.9732,-277.9128"/>
<polygon points="126.9771,-276.1039 119.2457,-268.8598 120.7722,-279.3441 126.9771,-276.1039"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402fa0</title>
<polygon points="203.5,-196.7696 20.5,-196.7696 16.5,-192.7696 16.5,-160.7696 199.5,-160.7696 203.5,-164.7696 203.5,-196.7696"/>
<polyline points="199.5,-192.7696 16.5,-192.7696 "/>
<polyline points="199.5,-192.7696 199.5,-160.7696 "/>
<polyline points="199.5,-192.7696 203.5,-196.7696 "/>
<text text-anchor="middle" x="110" y="-174.9696">module [Taskflow: F1]</text>
</g>
<g class="m-edge">
<title>p0x7f816f402d80->p0x7f816f402fa0</title>
<path d="M110,-232.5135C110,-224.8556 110,-215.8146 110,-207.3544"/>
<polygon points="113.5001,-207.0966 110,-197.0967 106.5001,-207.0967 113.5001,-207.0966"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402c70</title>
<ellipse cx="71" cy="-323.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="71" y="-320.1239">f2B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402c70->p0x7f816f402d80</title>
<path d="M80.4408,-306.3084C85.0058,-297.7906 90.5837,-287.383 95.6591,-277.9128"/>
<polygon points="98.872,-279.3271 100.511,-268.8598 92.7023,-276.0205 98.872,-279.3271"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402fa1</title>
<polygon points="203.5,-124.7696 20.5,-124.7696 16.5,-120.7696 16.5,-88.7696 199.5,-88.7696 203.5,-92.7696 203.5,-124.7696"/>
<polyline points="199.5,-120.7696 16.5,-120.7696 "/>
<polyline points="199.5,-120.7696 199.5,-88.7696 "/>
<polyline points="199.5,-120.7696 203.5,-124.7696 "/>
<text text-anchor="middle" x="110" y="-102.9696">module [Taskflow: F1]</text>
</g>
<g class="m-edge">
<title>p0x7f816f402fa0->p0x7f816f402fa1</title>
<path d="M110,-160.6009C110,-152.9005 110,-143.7439 110,-135.1862"/>
<polygon points="113.5001,-135.1828 110,-125.1828 106.5001,-135.1829 113.5001,-135.1828"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402e90</title>
<ellipse cx="110" cy="-34.3848" rx="31.2258" ry="18.2703"/>
<text text-anchor="middle" x="110" y="-30.5848">f2D</text>
</g>
<g class="m-edge">
<title>p0x7f816f402fa1->p0x7f816f402e90</title>
<path d="M110,-88.5038C110,-80.7623 110,-71.5567 110,-62.9533"/>
<polygon points="113.5001,-62.8964 110,-52.8964 106.5001,-62.8965 113.5001,-62.8964"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402830</title>
<ellipse cx="335" cy="-323.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="335" y="-320.1239">f1A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402a50</title>
<ellipse cx="296" cy="-251.1543" rx="30.3115" ry="18.2703"/>
<text text-anchor="middle" x="296" y="-247.3543">f1C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402830->p0x7f816f402a50</title>
<path d="M325.5592,-306.3084C320.9942,-297.7906 315.4163,-287.383 310.3409,-277.9128"/>
<polygon points="313.2977,-276.0205 305.489,-268.8598 307.128,-279.3271 313.2977,-276.0205"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402940</title>
<ellipse cx="258" cy="-323.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="258" y="-320.1239">f1B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402940->p0x7f816f402a50</title>
<path d="M267.1987,-306.3084C271.6467,-297.7906 277.0815,-287.383 282.0268,-277.9128"/>
<polygon points="285.2278,-279.3441 286.7543,-268.8598 279.0229,-276.1039 285.2278,-279.3441"/>
</g>
</g>
</svg>
</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="m-graph"><svg style="width: 37.125rem; height: 20.000rem;" viewBox="0.00 0.00 594.00 320.31">
<g transform="scale(1 1) rotate(0) translate(4 316.3087)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster_p0x7ffee9223970</title>
<polygon points="8,-8 8,-304.3087 417,-304.3087 417,-8 8,-8"/>
<text text-anchor="middle" x="212.5" y="-287.5087">Taskflow: F2</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7ffee92238d0</title>
<polygon points="425,-152.7696 425,-304.3087 578,-304.3087 578,-152.7696 425,-152.7696"/>
<text text-anchor="middle" x="501.5" y="-287.5087">Taskflow: F1</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402b60</title>
<ellipse cx="155" cy="-251.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="155" y="-248.1239">f2A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402d80</title>
<ellipse cx="132" cy="-179.1543" rx="30.3115" ry="18.2703"/>
<text text-anchor="middle" x="132" y="-175.3543">f2C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402b60->p0x7f816f402d80</title>
<path d="M149.1961,-233.5611C146.6435,-225.4849 143.59,-215.824 140.7692,-206.8991"/>
<polygon points="144.0734,-205.7395 137.7223,-197.2592 137.3988,-207.8491 144.0734,-205.7395"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402fa0</title>
<polygon points="203.5,-124.7696 20.5,-124.7696 16.5,-120.7696 16.5,-88.7696 199.5,-88.7696 203.5,-92.7696 203.5,-124.7696"/>
<polyline points="199.5,-120.7696 16.5,-120.7696 "/>
<polyline points="199.5,-120.7696 199.5,-88.7696 "/>
<polyline points="199.5,-120.7696 203.5,-124.7696 "/>
<text text-anchor="middle" x="110" y="-102.9696">module [Taskflow: F1]</text>
</g>
<g class="m-edge">
<title>p0x7f816f402d80->p0x7f816f402fa0</title>
<path d="M126.4485,-160.8886C124.0069,-152.8552 121.0861,-143.2453 118.3879,-134.3677"/>
<polygon points="121.7303,-133.3287 115.4735,-124.7787 115.0328,-135.3644 121.7303,-133.3287"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402fa1</title>
<polygon points="408.5,-124.7696 225.5,-124.7696 221.5,-120.7696 221.5,-88.7696 404.5,-88.7696 408.5,-92.7696 408.5,-124.7696"/>
<polyline points="404.5,-120.7696 221.5,-120.7696 "/>
<polyline points="404.5,-120.7696 404.5,-88.7696 "/>
<polyline points="404.5,-120.7696 408.5,-124.7696 "/>
<text text-anchor="middle" x="315" y="-102.9696">module [Taskflow: F1]</text>
</g>
<g class="m-edge">
<title>p0x7f816f402d80->p0x7f816f402fa1</title>
<path d="M157.4454,-169.0895C183.8377,-158.6502 225.9296,-142.0009 259.9698,-128.5365"/>
<polygon points="261.326,-131.764 269.3376,-124.8311 258.7512,-125.2547 261.326,-131.764"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402c70</title>
<ellipse cx="78" cy="-251.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="78" y="-248.1239">f2B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402c70->p0x7f816f402d80</title>
<path d="M90.5244,-235.0462C97.3399,-225.8617 105.9025,-214.3229 113.4753,-204.1179"/>
<polygon points="116.395,-206.0567 119.5435,-195.9405 110.7736,-201.8853 116.395,-206.0567"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402e90</title>
<ellipse cx="212" cy="-34.3848" rx="31.2258" ry="18.2703"/>
<text text-anchor="middle" x="212" y="-30.5848">f2D</text>
</g>
<g class="m-edge">
<title>p0x7f816f402fa0->p0x7f816f402e90</title>
<path d="M135.4758,-88.6905C150.0764,-78.3292 168.4374,-65.2992 183.5142,-54.5999"/>
<polygon points="185.8288,-57.2491 191.9584,-48.6074 181.7776,-51.5405 185.8288,-57.2491"/>
</g>
<g class="m-edge">
<title>p0x7f816f402fa1->p0x7f816f402e90</title>
<path d="M289.2744,-88.6905C274.5307,-78.3292 255.9897,-65.2992 240.7651,-54.5999"/>
<polygon points="242.4322,-51.4936 232.2381,-48.6074 238.4074,-57.2208 242.4322,-51.4936"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402830</title>
<ellipse cx="540" cy="-251.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="540" y="-248.1239">f1A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402a50</title>
<ellipse cx="501" cy="-179.1543" rx="30.3115" ry="18.2703"/>
<text text-anchor="middle" x="501" y="-175.3543">f1C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402830->p0x7f816f402a50</title>
<path d="M530.5592,-234.3084C525.9942,-225.7906 520.4163,-215.383 515.3409,-205.9128"/>
<polygon points="518.2977,-204.0205 510.489,-196.8598 512.128,-207.3271 518.2977,-204.0205"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402940</title>
<ellipse cx="463" cy="-251.9239" rx="29.8983" ry="18.2703"/>
<text text-anchor="middle" x="463" y="-248.1239">f1B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402940->p0x7f816f402a50</title>
<path d="M472.1987,-234.3084C476.6467,-225.7906 482.0815,-215.383 487.0268,-205.9128"/>
<polygon points="490.2278,-207.3441 491.7543,-196.8598 484.0229,-204.1039 490.2278,-207.3441"/>
</g>
</g>
</svg>
</div></section><section id="CreateACustomComposableGraph"><h2><a href="#CreateACustomComposableGraph">Create a Custom Composable Graph</a></h2><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 a method <code>T::graph()</code> that returns a reference to a <a href="classtf_1_1Graph.html" class="m-doc">tf::<wbr />Graph</a> object. The following example defines a custom graph object that can be assembled in a taskflow throw composition:</p><pre class="m-code"><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">CustomGraph</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="mi">2</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Graph</span><span class="w"> </span><span class="n">graph</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="mi">3</span><span class="o">:</span><span class="w"> </span><span class="n">CustomGraph</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="mi">4</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">FlowBuilder</span><span class="w"> </span><span class="n">builder</span><span class="p">(</span><span class="n">graph</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="mi">5</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">task</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){</span><span class="w"></span>
<span class="w"> </span><span class="mi">6</span><span class="o">:</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">"a task</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span><span class="w"> </span><span class="c1">// static task</span>
<span class="w"> </span><span class="mi">7</span><span class="o">:</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w"> </span><span class="mi">8</span><span class="o">:</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w"> </span><span class="mi">9</span><span class="o">:</span><span class="w"> </span><span class="c1">// returns a reference to the graph for taskflow composition</span>
<span class="mi">10</span><span class="o">:</span><span class="w"> </span><span class="n">Graph</span><span class="o">&</span><span class="w"> </span><span class="n">graph</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">graph</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="mi">11</span><span class="o">:</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="mi">12</span><span class="o">:</span><span class="w"></span>
<span class="mi">13</span><span class="o">:</span><span class="w"> </span><span class="n">CustomGraph</span><span class="w"> </span><span class="n">obj</span><span class="p">;</span><span class="w"></span>
<span class="mi">14</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">comp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">composed_of</span><span class="p">(</span><span class="n">obj</span><span class="p">);</span><span class="w"></span></pre><p>Debrief:</p><ul><li>Lines 1-11 define a custom graph interface to participate in taskflow composition</li><li>Line 2 defines the graph object using <a href="classtf_1_1Graph.html" class="m-doc">tf::<wbr />Graph</a></li><li>Lines 3-8 defines the constructor that constructs the task graph using <a href="classtf_1_1FlowBuilder.html" class="m-doc">tf::<wbr />FlowBuilder</a></li><li>Line 10 defines the required method for taskflow composition</li><li>Lines 13-14 creates a module task for the declared graph object in the taskflow</li></ul><p>The composition method <a href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" class="m-doc">tf::<wbr />Taskflow::<wbr />composed_of</a> requires the target to define the <code>graph()</code> method that returns a reference to a <a href="classtf_1_1Graph.html" class="m-doc">tf::<wbr />Graph</a> object defined by the target. At runtime, the executor will run dependent tasks in that graph using the same work-stealing scheduling algorithm as other taskflows. Taskflow leverages this powerful feature to design high-level algorithms, such as <a href="classtf_1_1Pipeline.html" class="m-doc">tf::<wbr />Pipeline</a>.</p><aside class="m-note m-info"><h4>Note</h4><p>While Taskflow gives you the flexibility to create a composable graph object, you should consider using <a href="classtf_1_1Graph.html" class="m-doc">tf::<wbr />Graph</a> as an opaque data structure just to interact with the library. Additionally, as other module tasks, Taskflow does not own the lifetime of a custom composable graph object but keeps a soft mapping to it. You should keep the graph object alive during its execution.</p></aside></section>
</div>
</div>
</div>
</article></main>
<div class="m-doc-search" id="search">
<a href="#!" onclick="return hideSearch()"></a>
<div class="m-container">
<div class="m-row">
<div class="m-col-m-8 m-push-m-2">
<div class="m-doc-search-header m-text m-small">
<div><span class="m-label m-default">Tab</span> / <span class="m-label m-default">T</span> to search, <span class="m-label m-default">Esc</span> to close</div>
<div id="search-symbolcount">…</div>
</div>
<div class="m-doc-search-content">
<form>
<input type="search" name="q" id="search-input" placeholder="Loading …" disabled="disabled" autofocus="autofocus" autocomplete="off" spellcheck="false" />
</form>
<noscript class="m-text m-danger m-text-center">Unlike everything else in the docs, the search functionality <em>requires</em> JavaScript.</noscript>
<div id="search-help" class="m-text m-dim m-text-center">
<p class="m-noindent">Search for symbols, directories, files, pages or
modules. You can omit any prefix from the symbol or file path; adding a
<code>:</code> or <code>/</code> suffix lists all members of given symbol or
directory.</p>
<p class="m-noindent">Use <span class="m-label m-dim">↓</span>
/ <span class="m-label m-dim">↑</span> to navigate through the list,
<span class="m-label m-dim">Enter</span> to go.
<span class="m-label m-dim">Tab</span> autocompletes common prefix, you can
copy a link to the result using <span class="m-label m-dim">⌘</span>
<span class="m-label m-dim">L</span> while <span class="m-label m-dim">⌘</span>
<span class="m-label m-dim">M</span> produces a Markdown link.</p>
</div>
<div id="search-notfound" class="m-text m-warning m-text-center">Sorry, nothing was found.</div>
<ul id="search-results"></ul>
</div>
</div>
</div>
</div>
</div>
<script src="search-v2.js"></script>
<script src="searchdata-v2.js" async="async"></script>
<footer><nav>
<div class="m-container">
<div class="m-row">
<div class="m-col-l-10 m-push-l-1">
<p>Taskflow handbook is part of the <a href="https://taskflow.github.io">Taskflow project</a>, copyright © <a href="https://tsung-wei-huang.github.io/">Dr. Tsung-Wei Huang</a>, 2018–2022.<br />Generated by <a href="https://doxygen.org/">Doxygen</a> 1.8.14 and <a href="https://mcss.mosra.cz/">m.css</a>.</p>
</div>
</div>
</div>
</nav></footer>
</body>
</html>