forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalTasking.xml
More file actions
489 lines (489 loc) · 71.9 KB
/
Copy pathConditionalTasking.xml
File metadata and controls
489 lines (489 loc) · 71.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<doxygen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="compound.xsd" version="1.12.0" xml:lang="en-US">
<compounddef id="ConditionalTasking" kind="page">
<compoundname>ConditionalTasking</compoundname>
<title>Conditional Tasking</title>
<tableofcontents>
<tocsect>
<name>Create a Condition Task</name>
<reference>ConditionalTasking_1CreateAConditionTask</reference>
</tocsect>
<tocsect>
<name>Understand our Task-level Scheduling</name>
<reference>ConditionalTasking_1TaskSchedulingPolicy</reference>
<tableofcontents>
<tocsect>
<name>Example</name>
<reference>ConditionalTasking_1TaskLevelSchedulingExample</reference>
</tocsect>
</tableofcontents>
</tocsect>
<tocsect>
<name>Avoid Common Pitfalls</name>
<reference>ConditionalTasking_1AvoidCommonPitfalls</reference>
</tocsect>
<tocsect>
<name>Implement Control-flow Graphs</name>
<reference>ConditionalTasking_1ImplementControlFlowGraphs</reference>
<tableofcontents>
<tocsect>
<name>Implement If-Else Control Flow</name>
<reference>ConditionalTasking_1ImplementIfElseControlFlow</reference>
</tocsect>
<tocsect>
<name>Implement Switch Control Flow</name>
<reference>ConditionalTasking_1ImplementSwitchControlFlow</reference>
</tocsect>
<tocsect>
<name>Implement Do-While-Loop Control Flow</name>
<reference>ConditionalTasking_1ImplementDoWhileLoopControlFlow</reference>
</tocsect>
<tocsect>
<name>Implement While-Loop Control Flow</name>
<reference>ConditionalTasking_1ImplementWhileLoopControlFlow</reference>
</tocsect>
</tableofcontents>
</tocsect>
<tocsect>
<name>Create a Multi-condition Task</name>
<reference>ConditionalTasking_1CreateAMultiConditionTask</reference>
</tocsect>
</tableofcontents>
<briefdescription>
</briefdescription>
<detaileddescription>
<para>Parallel workloads often require making control-flow decisions across dependent tasks. Taskflow supports a very efficient interface of conditional tasking for users to implement general control flow such as dynamic flow, cycles and conditionals that are otherwise difficult to do with existing frameworks.</para>
<sect1 id="ConditionalTasking_1CreateAConditionTask">
<title>Create a Condition Task</title><para>A condition task evaluates a set of instructions and returns an integer index of the next successor task to execute. The index is defined with respect to the order of its successor construction. The following example creates an if-else block using a single condition task.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><sp/>1:<sp/><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><sp/>2:</highlight></codeline>
<codeline><highlight class="normal"><sp/>3:<sp/></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[init,<sp/>cond,<sp/>yes,<sp/>no]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/>4:<sp/><sp/>[]<sp/>()<sp/>{<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/>5:<sp/><sp/>[]<sp/>()<sp/>{<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/>6:<sp/><sp/>[]<sp/>()<sp/>{<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"yes\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/>7:<sp/><sp/>[]<sp/>()<sp/>{<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"no\n"</highlight><highlight class="normal">;<sp/>}</highlight></codeline>
<codeline><highlight class="normal"><sp/>8:<sp/>);</highlight></codeline>
<codeline><highlight class="normal"><sp/>9:</highlight></codeline>
<codeline><highlight class="normal">10:<sp/>cond.<ref refid="classtf_1_1Task_1a331b1b726555072e7c7d10941257f664" kindref="member">succeed</ref>(init)</highlight></codeline>
<codeline><highlight class="normal">11:<sp/><sp/><sp/><sp/><sp/>.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(yes,<sp/>no);<sp/><sp/></highlight><highlight class="comment">//<sp/>executes<sp/>yes<sp/>if<sp/>cond<sp/>returns<sp/>0</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">12:<sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/><sp/></highlight><highlight class="comment">//<sp/>executes<sp/>no<sp/><sp/>if<sp/>cond<sp/>returns<sp/>1</highlight></codeline>
</programlisting></para>
<para><dotfile name="conditional-tasking-if-else.dot"></dotfile>
</para>
<para>Line 5 creates a condition task <computeroutput>cond</computeroutput> and line 11 creates two dependencies from <computeroutput>cond</computeroutput> to two other tasks, <computeroutput>yes</computeroutput> and <computeroutput>no</computeroutput>. With this order, when <computeroutput>cond</computeroutput> returns 0, the execution moves on to task <computeroutput>yes</computeroutput>. When <computeroutput>cond</computeroutput> returns 1, the execution moves on to task <computeroutput>no</computeroutput>.</para>
<para><simplesect kind="attention"><para>It is your responsibility to ensure the return of a condition task goes to a correct successor task. If the return falls beyond the range of the successors, the executor will not schedule any tasks.</para>
</simplesect>
Condition task can go cyclic to describe <emphasis>iterative</emphasis> control flow. The example below implements a simple yet commonly used feedback loop through a condition task (line 7-10) that returns a random binary value. If the return value from <computeroutput>cond</computeroutput> is <computeroutput>0</computeroutput>, it loops back to itself, or otherwise to <computeroutput>stop</computeroutput>.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><sp/>1:<sp/><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><sp/>2:<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/>3:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>init<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"init"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><sp/>4:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>stop<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"stop"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><sp/>5:</highlight></codeline>
<codeline><highlight class="normal"><sp/>6:<sp/></highlight><highlight class="comment">//<sp/>creates<sp/>a<sp/>condition<sp/>task<sp/>that<sp/>returns<sp/>0<sp/>or<sp/>1</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><sp/>7:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>cond<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){</highlight></codeline>
<codeline><highlight class="normal"><sp/>8:<sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"flipping<sp/>a<sp/>coin\n"</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal"><sp/>9:<sp/><sp/><sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/><ref refid="cpp/numeric/random/rand" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::rand</ref>()<sp/>%<sp/>2;</highlight></codeline>
<codeline><highlight class="normal">10:<sp/>}).name(</highlight><highlight class="stringliteral">"cond"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal">11:</highlight></codeline>
<codeline><highlight class="normal">12:<sp/></highlight><highlight class="comment">//<sp/>creates<sp/>a<sp/>feedback<sp/>loop<sp/>{0:<sp/>cond,<sp/>1:<sp/>stop}</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">13:<sp/>init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
<codeline><highlight class="normal">14:<sp/>cond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond,<sp/>stop);<sp/><sp/></highlight><highlight class="comment">//<sp/>returns<sp/>0<sp/>to<sp/>'cond'<sp/>or<sp/>1<sp/>to<sp/>'stop'</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">15:</highlight></codeline>
<codeline><highlight class="normal">16:<sp/>executor.run(taskflow).wait();</highlight></codeline>
</programlisting></para>
<para><dotfile name="conditional-tasking-1.dot"></dotfile>
</para>
<para>A taskflow of complex control flow often just takes a few lines of code to implement, and different control flow blocks may run in parallel. The code below creates another taskflow with three condition tasks.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>A<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"A"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>B<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"B"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>C<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"C"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>D<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"D"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>E<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"E"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>F<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"F"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>G<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"G"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>H<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"H"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>I<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"I"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>K<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"K"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>L<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"L"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>M<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"M"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>cond_1<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/><ref refid="cpp/numeric/random/rand" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::rand</ref>()%2;<sp/>}).name(</highlight><highlight class="stringliteral">"cond_1"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>cond_2<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/><ref refid="cpp/numeric/random/rand" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::rand</ref>()%2;<sp/>}).name(</highlight><highlight class="stringliteral">"cond_2"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>cond_3<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/><ref refid="cpp/numeric/random/rand" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::rand</ref>()%2;<sp/>}).name(</highlight><highlight class="stringliteral">"cond_3"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">A.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B,<sp/>F);</highlight></codeline>
<codeline><highlight class="normal">B.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(C);</highlight></codeline>
<codeline><highlight class="normal">C.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(D);</highlight></codeline>
<codeline><highlight class="normal">D.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond_1);</highlight></codeline>
<codeline><highlight class="normal">E.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(K);</highlight></codeline>
<codeline><highlight class="normal">F.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond_2);</highlight></codeline>
<codeline><highlight class="normal">H.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(I);</highlight></codeline>
<codeline><highlight class="normal">I.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond_3);</highlight></codeline>
<codeline><highlight class="normal">L.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(M);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">cond_1.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B,<sp/>E);<sp/><sp/><sp/><sp/><sp/><sp/><sp/></highlight><highlight class="comment">//<sp/>return<sp/>0<sp/>to<sp/>'B'<sp/>or<sp/>1<sp/>to<sp/>'E'</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">cond_2.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(G,<sp/>H);<sp/><sp/><sp/><sp/><sp/><sp/><sp/></highlight><highlight class="comment">//<sp/>return<sp/>0<sp/>to<sp/>'G'<sp/>or<sp/>1<sp/>to<sp/>'H'</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">cond_3.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond_3,<sp/>L);<sp/><sp/></highlight><highlight class="comment">//<sp/>return<sp/>0<sp/>to<sp/>'cond_3'<sp/>or<sp/>1<sp/>to<sp/>'L'</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">taskflow.<ref refid="classtf_1_1Taskflow_1ac433018262e44b12c4cc9f0c4748d758" kindref="member">dump</ref>(<ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref>);</highlight></codeline>
</programlisting></para>
<para>The above code creates three condition tasks: (1) a condition task <computeroutput>cond_1</computeroutput> that loops back to <computeroutput>B</computeroutput> on returning <computeroutput>0</computeroutput>, or proceeds to <computeroutput>E</computeroutput> on returning <computeroutput>1</computeroutput>, (2) a condition task <computeroutput>cond_2</computeroutput> that goes to <computeroutput>G</computeroutput> on returning <computeroutput>0</computeroutput>, or <computeroutput>H</computeroutput> on returning <computeroutput>1</computeroutput>, (3) a condition task <computeroutput>cond_3</computeroutput> that loops back to itself on returning <computeroutput>0</computeroutput>, or proceeds to <computeroutput>L</computeroutput> on returning <computeroutput>1</computeroutput> </para>
<para><dotfile name="conditional-tasking-2.dot"></dotfile>
</para>
<para>You can use condition tasks to create cycles as long as the graph does not introduce task race during execution. However, cycles are not allowed in non-condition tasks.</para>
<para><simplesect kind="attention"><para>Conditional tasking lets you make in-task control-flow decisions to enable <emphasis>end-to-end</emphasis> parallelism, instead of resorting to client-side partition or synchronizing your task graph at the decision points of control flow.</para>
</simplesect>
</para>
</sect1>
<sect1 id="ConditionalTasking_1TaskSchedulingPolicy">
<title>Understand our Task-level Scheduling</title><para>In order to understand how an executor schedules condition tasks, we define two dependency types, <emphasis>strong dependency</emphasis> and <emphasis>weak dependency</emphasis>. A strong dependency is a preceding link from a non-condition task to another task. A weak dependency is a preceding link from a condition task to another task. The number of dependencies of a task is the sum of strong dependency and weak dependency. The table below lists the strong dependency and weak dependency numbers of each task in the previous example.</para>
<para> <table rows="16" cols="4"><row>
<entry thead="yes" align='center'><para>task </para>
</entry><entry thead="yes" align='center'><para>strong dependency </para>
</entry><entry thead="yes" align='center'><para>weak dependency </para>
</entry><entry thead="yes"><para>dependencies </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>A </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no"><para>0 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>B </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no"><para>2 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>C </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>D </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>E </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>F </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>G </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>H </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>I </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>K </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>L </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>M </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>cond_1 </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>cond_2 </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no" align='center'><para>0 </para>
</entry><entry thead="no"><para>1 </para>
</entry></row>
<row>
<entry thead="no" align='center'><para>cond_3 </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no" align='center'><para>1 </para>
</entry><entry thead="no"><para>2 </para>
</entry></row>
</table>
</para>
<para>You can query the number of strong dependencies, the number of weak dependencies, and the number of dependencies of a task.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal">1:<sp/><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal">2:<sp/></highlight></codeline>
<codeline><highlight class="normal">3:<sp/><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>task<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){});</highlight></codeline>
<codeline><highlight class="normal">4:<sp/></highlight></codeline>
<codeline><highlight class="normal">5:<sp/></highlight><highlight class="comment">//<sp/>...<sp/>add<sp/>more<sp/>tasks<sp/>and<sp/>preceding<sp/>links</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">6:</highlight></codeline>
<codeline><highlight class="normal">7:<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/>task.<ref refid="classtf_1_1Task_1adefb65d68a64bd8a75364a8801cfec44" kindref="member">num_predecessors</ref>()<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal">8:<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/>task.<ref refid="classtf_1_1Task_1a0b7b789c9b8a21927a992f6ccc11de81" kindref="member">num_strong_dependencies</ref>()<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;<sp/></highlight></codeline>
<codeline><highlight class="normal">9:<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/>task.<ref refid="classtf_1_1Task_1ad5e874b7cc77df1e7dc875d436ff7b72" kindref="member">num_weak_dependencies</ref>()<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;</highlight></codeline>
</programlisting></para>
<para>When you submit a task to an executor, the scheduler starts with tasks of <emphasis>zero dependencies</emphasis> (both zero strong and weak dependencies) and continues to execute successive tasks whenever their <emphasis>strong dependencies</emphasis> are met. However, the scheduler skips this rule when executing a condition task and jumps directly to its successors indexed by the return value.</para>
<para><dotfile name="task_level_scheduling.dot"></dotfile>
</para>
<para>Each task has an <emphasis>atomic</emphasis> join counter to keep track of strong dependencies that are met at runtime. When a task completes, the join counter is restored to the task's strong dependency number in the graph, such that the subsequent execution can reuse the counter again.</para>
<sect2 id="ConditionalTasking_1TaskLevelSchedulingExample">
<title>Example</title><para>Let's take a look at an example to understand how task-level scheduling works. Suppose we have the following taskflow of one condition task <computeroutput>cond</computeroutput> that forms a loop to itself on returning <computeroutput>0</computeroutput> and moves on to <computeroutput>stop</computeroutput> on returning <computeroutput>1</computeroutput>:</para>
<para><dotfile name="conditional-tasking-1.dot"></dotfile>
</para>
<para>The scheduler starts with <computeroutput>init</computeroutput> task because it has no dependencies (both strong and weak dependencies). Then, the scheduler moves on to the condition task <computeroutput>cond</computeroutput>. If <computeroutput>cond</computeroutput> returns <computeroutput>0</computeroutput>, the scheduler enqueues <computeroutput>cond</computeroutput> and runs it again. If <computeroutput>cond</computeroutput> returns <computeroutput>1</computeroutput>, the scheduler enqueues <computeroutput>stop</computeroutput> and then moves on.</para>
</sect2>
</sect1>
<sect1 id="ConditionalTasking_1AvoidCommonPitfalls">
<title>Avoid Common Pitfalls</title><para>Condition tasks are handy in creating dynamic and cyclic control flows, but they are also easy to make mistakes. It is your responsibility to ensure a taskflow is properly conditioned. Top things to avoid include <emphasis>no source tasks</emphasis> to start with and <emphasis>task race</emphasis>. The figure below shows common pitfalls and their remedies.</para>
<para><dotfile name="conditional-tasking-pitfalls.dot"></dotfile>
</para>
<para>In the <computeroutput>error1</computeroutput> scenario, there is no source task for the scheduler to start with, and the simplest fix is to add a task <computeroutput>S</computeroutput> that has no dependencies. In the <computeroutput>error2</computeroutput> scenario, <computeroutput>D</computeroutput> might be scheduled twice by <computeroutput>E</computeroutput> through the strong dependency and <computeroutput>C</computeroutput> through the weak dependency (on returning <computeroutput>1</computeroutput>). To fix this problem, you can add an auxiliary task <computeroutput>D-aux</computeroutput> to break the mixed use of strong dependency and weak dependency. In the risky scenario, task <computeroutput>X</computeroutput> may be raced by <computeroutput>M</computeroutput> and <computeroutput>P</computeroutput> if <computeroutput>M</computeroutput> returns <computeroutput>0</computeroutput> and P returns <computeroutput>1</computeroutput>.</para>
<para><simplesect kind="attention"><para>It is your responsibility to ensure a written taskflow graph is properly conditioned. We suggest that you <ref refid="ConditionalTasking_1TaskSchedulingPolicy" kindref="member">Understand our Task-level Scheduling</ref> and infer if task race exists in the execution of your graph.</para>
</simplesect>
</para>
</sect1>
<sect1 id="ConditionalTasking_1ImplementControlFlowGraphs">
<title>Implement Control-flow Graphs</title><sect2 id="ConditionalTasking_1ImplementIfElseControlFlow">
<title>Implement If-Else Control Flow</title><para>You can use conditional tasking to implement if-else control flow. The following example creates a nested if-else control flow diagram that executes three condition tasks to check the range of <computeroutput>i</computeroutput>.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>create<sp/>three<sp/>condition<sp/>tasks<sp/>for<sp/>nested<sp/>control<sp/>flow</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>initi<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](){<sp/>i=3;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>cond1<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i>1<sp/>?<sp/>1<sp/>:<sp/>0;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>cond2<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i>2<sp/>?<sp/>1<sp/>:<sp/>0;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>cond3<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i>3<sp/>?<sp/>1<sp/>:<sp/>0;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>equl1<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=1\n"</highlight><highlight class="normal">;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>equl2<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=2\n"</highlight><highlight class="normal">;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>equl3<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=3\n"</highlight><highlight class="normal">;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>grtr3<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i>3\n"</highlight><highlight class="normal">;<sp/>});<sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">initi.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond1);</highlight></codeline>
<codeline><highlight class="normal">cond1.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(equl1,<sp/>cond2);<sp/><sp/></highlight><highlight class="comment">//<sp/>goes<sp/>to<sp/>cond2<sp/>if<sp/>i>1</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">cond2.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(equl2,<sp/>cond3);<sp/><sp/></highlight><highlight class="comment">//<sp/>goes<sp/>to<sp/>cond3<sp/>if<sp/>i>2</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">cond3.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(equl3,<sp/>grtr3);<sp/><sp/></highlight><highlight class="comment">//<sp/>goes<sp/>to<sp/>grtr3<sp/>if<sp/>i>3</highlight></codeline>
</programlisting></para>
<para><dotfile name="conditional-tasking-nested-if-else.dot"></dotfile>
</para>
</sect2>
<sect2 id="ConditionalTasking_1ImplementSwitchControlFlow">
<title>Implement Switch Control Flow</title><para>You can use conditional tasking to implement <emphasis>switch</emphasis> control flow. The following example creates a switch control flow diagram that executes one of the three cases at random using four condition tasks.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[source,<sp/>swcond,<sp/>case1,<sp/>case2,<sp/>case3,<sp/>target]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"source\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"switch\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>rand()%3;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>1\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>2\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>3\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"target\n"</highlight><highlight class="normal">;<sp/>}</highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">source.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(swcond);</highlight></codeline>
<codeline><highlight class="normal">swcond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(case1,<sp/>case2,<sp/>case3);</highlight></codeline>
<codeline><highlight class="normal">target.<ref refid="classtf_1_1Task_1a331b1b726555072e7c7d10941257f664" kindref="member">succeed</ref>(case1,<sp/>case2,<sp/>case3);</highlight></codeline>
</programlisting></para>
<para><dotfile name="conditional-tasking-switch.dot"></dotfile>
</para>
<para>Assuming <computeroutput>swcond</computeroutput> returns 1, the program outputs:</para>
<para><programlisting filename=".shell-session"><codeline><highlight class="normal">source</highlight></codeline>
<codeline><highlight class="normal">switch</highlight></codeline>
<codeline><highlight class="normal">case<sp/>2</highlight></codeline>
<codeline><highlight class="normal">target</highlight></codeline>
</programlisting></para>
<para>Keep in mind, both switch and case tasks must be described as condition tasks. The following implementation is a common mistake in which case tasks are not described as condition tasks.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="comment">//<sp/>wrong<sp/>implementation<sp/>of<sp/>switch<sp/>control<sp/>flow<sp/>using<sp/>only<sp/>one<sp/>condition<sp/>task</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[source,<sp/>swcond,<sp/>case1,<sp/>case2,<sp/>case3,<sp/>target]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"source\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"switch\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>rand()%3;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>1\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>2\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"case<sp/>3\n"</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"target\n"</highlight><highlight class="normal">;<sp/>}<sp/><sp/></highlight><highlight class="comment">//<sp/>target<sp/>has<sp/>three<sp/>strong<sp/>dependencies</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">source.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(swcond);</highlight></codeline>
<codeline><highlight class="normal">swcond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(case1,<sp/>case2,<sp/>case3);</highlight></codeline>
<codeline><highlight class="normal">target.<ref refid="classtf_1_1Task_1a331b1b726555072e7c7d10941257f664" kindref="member">succeed</ref>(case1,<sp/>case2,<sp/>case3);</highlight></codeline>
</programlisting></para>
<para><dotfile name="conditional-tasking-switch-wrong.dot"></dotfile>
</para>
<para>In this faulty implementation, task <computeroutput>target</computeroutput> has three strong dependencies but only one of them will be met. This is because <computeroutput>swcond</computeroutput> is a condition task, and only one case task will be executed depending on the return of <computeroutput>swcond</computeroutput>.</para>
</sect2>
<sect2 id="ConditionalTasking_1ImplementDoWhileLoopControlFlow">
<title>Implement Do-While-Loop Control Flow</title><para>You can use conditional tasking to implement <emphasis>do-while-loop</emphasis> control flow. The following example creates a do-while-loop control flow diagram that repeatedly increments variable <computeroutput>i</computeroutput> five times using one condition task.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[init,<sp/>body,<sp/>cond,<sp/>done]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=0\n"</highlight><highlight class="normal">;<sp/>i=0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i++<sp/>=><sp/>i="</highlight><highlight class="normal">;<sp/>i++;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/>i<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i<5<sp/>?<sp/>0<sp/>:<sp/>1;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"done\n"</highlight><highlight class="normal">;<sp/>}</highlight></codeline>
<codeline><highlight class="normal">);<sp/><sp/></highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(body);</highlight></codeline>
<codeline><highlight class="normal">body.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
<codeline><highlight class="normal">cond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(body,<sp/>done);</highlight></codeline>
</programlisting></para>
<para><dotfile name="conditional-tasking-do-while.dot"></dotfile>
</para>
<para>The program outputs:</para>
<para><programlisting filename=".shell-session"><codeline><highlight class="normal">i=0</highlight></codeline>
<codeline><highlight class="normal">i++<sp/>=><sp/>i=1</highlight></codeline>
<codeline><highlight class="normal">i++<sp/>=><sp/>i=2</highlight></codeline>
<codeline><highlight class="normal">i++<sp/>=><sp/>i=3</highlight></codeline>
<codeline><highlight class="normal">i++<sp/>=><sp/>i=4</highlight></codeline>
<codeline><highlight class="normal">i++<sp/>=><sp/>i=5</highlight></codeline>
<codeline><highlight class="normal">done</highlight></codeline>
</programlisting></para>
</sect2>
<sect2 id="ConditionalTasking_1ImplementWhileLoopControlFlow">
<title>Implement While-Loop Control Flow</title><para>You can use conditional tasking to implement <emphasis>while-loop</emphasis> control flow. The following example creates a while-loop control flow diagram that repeatedly increments variable <computeroutput>i</computeroutput> five times using two condition task.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[init,<sp/>cond,<sp/>body,<sp/>back,<sp/>done]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=0\n"</highlight><highlight class="normal">;<sp/>i=0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"while<sp/>i<5\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i<sp/><<sp/>5<sp/>?<sp/>0<sp/>:<sp/>1;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i++="</highlight><highlight class="normal"><sp/><<<sp/>i++<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"back\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"done\n"</highlight><highlight class="normal">;<sp/>}</highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
<codeline><highlight class="normal">cond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(body,<sp/>done);</highlight></codeline>
<codeline><highlight class="normal">body.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(back);</highlight></codeline>
<codeline><highlight class="normal">back.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
</programlisting></para>
<para><dotfile name="conditional-tasking-while.dot"></dotfile>
</para>
<para>The program outputs:</para>
<para><programlisting filename=".shell-session"><codeline><highlight class="normal">i=0</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i<5</highlight></codeline>
<codeline><highlight class="normal">i++=0</highlight></codeline>
<codeline><highlight class="normal">back</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i<5</highlight></codeline>
<codeline><highlight class="normal">i++=1</highlight></codeline>
<codeline><highlight class="normal">back</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i<5</highlight></codeline>
<codeline><highlight class="normal">i++=2</highlight></codeline>
<codeline><highlight class="normal">back</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i<5</highlight></codeline>
<codeline><highlight class="normal">i++=3</highlight></codeline>
<codeline><highlight class="normal">back</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i<5</highlight></codeline>
<codeline><highlight class="normal">i++=4</highlight></codeline>
<codeline><highlight class="normal">back</highlight></codeline>
<codeline><highlight class="normal">while<sp/>i<5</highlight></codeline>
<codeline><highlight class="normal">done</highlight></codeline>
</programlisting></para>
<para>Notice that, when you implement a while-loop block, you cannot direct a dependency from the body task to the loop condition task. Doing so will introduce a strong dependency between the body task and the loop condition task, and the loop condition task will never be executed. The following code shows a common faulty implementation of while-loop control flow.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="comment">//<sp/>wrong<sp/>implementation<sp/>of<sp/>while-loop<sp/>using<sp/>only<sp/>one<sp/>condition<sp/>task</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>[init,<sp/>cond,<sp/>body,<sp/>done]<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i=0\n"</highlight><highlight class="normal">;<sp/>i=0;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"while<sp/>i<5\n"</highlight><highlight class="normal">;<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i<sp/><<sp/>5<sp/>?<sp/>0<sp/>:<sp/>1;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"i++="</highlight><highlight class="normal"><sp/><<<sp/>i++<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;<sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"done\n"</highlight><highlight class="normal">;<sp/>}</highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
<codeline><highlight class="normal">cond.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(body,<sp/>done);</highlight></codeline>
<codeline><highlight class="normal">body.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(cond);</highlight></codeline>
</programlisting></para>
<para><dotfile name="conditional-tasking-while-wrong.dot"></dotfile>
</para>
<para>In the taskflow diagram above, the scheduler starts with <computeroutput>init</computeroutput> and then decrements the strong dependency of the loop condition task, <computeroutput>while i<5</computeroutput>. After this, there remains one strong dependency, i.e., introduced by the loop body task, <computeroutput>i++</computeroutput>. However, task <computeroutput>i++</computeroutput> will not be executed until the loop condition task returns <computeroutput>0</computeroutput>, causing a deadlock.</para>
</sect2>
</sect1>
<sect1 id="ConditionalTasking_1CreateAMultiConditionTask">
<title>Create a Multi-condition Task</title><para>A <emphasis>multi-condition task</emphasis> is a generalized version of conditional tasking. In some cases, applications need to jump to multiple branches from a parent task. This can be done by creating a <emphasis>multi-condition task</emphasis> which allows a task to select one or more successor tasks to execute. Similar to a condition task, a multi-condition task returns a vector of integer indices that indicate the successors to execute when the multi-condition task completes. The index is defined with respect to the order of successors preceded by a multi-condition task. For example, the following code creates a multi-condition task, <computeroutput>A</computeroutput>, that informs the scheduler to run on its two successors, <computeroutput>B</computeroutput> and <computeroutput>D</computeroutput>.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref><sp/>executor;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>A<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&]()<sp/>-><sp/><ref refid="classtf_1_1SmallVector" kindref="compound">tf::SmallVector<int></ref><sp/>{<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"A\n"</highlight><highlight class="normal">;<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>{0,<sp/>2};</highlight></codeline>
<codeline><highlight class="normal">}).name(</highlight><highlight class="stringliteral">"A"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>B<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"B\n"</highlight><highlight class="normal">;<sp/>}).name(</highlight><highlight class="stringliteral">"B"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>C<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"C\n"</highlight><highlight class="normal">;<sp/>}).name(</highlight><highlight class="stringliteral">"C"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>D<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&](){<sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"D\n"</highlight><highlight class="normal">;<sp/>}).name(</highlight><highlight class="stringliteral">"D"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">A.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B,<sp/>C,<sp/>D);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a519777f5783981d534e9e53b99712069" kindref="member">run</ref>(taskflow).wait();</highlight></codeline>
</programlisting></para>
<para><dotfile name="multi-condition-task-1.dot"></dotfile>
</para>
<para><simplesect kind="attention"><para>The return type of a multi-condition task is <ref refid="classtf_1_1SmallVector" kindref="compound">tf::SmallVector</ref>, which provides C++ vector-style functionalities but comes with small buffer optimization.</para>
</simplesect>
One important application of conditional tasking is implementing <emphasis>iterative control flow</emphasis>. You can use multi-condition tasks to create multiple loops that run concurrently. The following code creates a sequential chain of four loops in which each loop increments a counter variable ten times. When the program completes, the value of the counter variable is <computeroutput>40</computeroutput>.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref><sp/>executor;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="cpp/atomic/atomic" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::atomic<int></ref><sp/>counter{0};</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>loop<sp/>=<sp/>[&,<sp/>c<sp/>=<sp/>int(0)]()<sp/></highlight><highlight class="keyword">mutable</highlight><highlight class="normal"><sp/>-><sp/><ref refid="classtf_1_1SmallVector" kindref="compound">tf::SmallVector<int></ref><sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>counter.fetch_add(1,<sp/>std::memory_order_relaxed);</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>{++c<sp/><<sp/>10<sp/>?<sp/>0<sp/>:<sp/>1};</highlight></codeline>
<codeline><highlight class="normal">};</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>init<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([](){}).name(</highlight><highlight class="stringliteral">"init"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>A<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(loop).<ref refid="classtf_1_1Task_1a08ada0425b490997b6ff7f310107e5e3" kindref="member">name</ref>(</highlight><highlight class="stringliteral">"A"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>B<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(loop).<ref refid="classtf_1_1Task_1a08ada0425b490997b6ff7f310107e5e3" kindref="member">name</ref>(</highlight><highlight class="stringliteral">"B"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>C<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(loop).<ref refid="classtf_1_1Task_1a08ada0425b490997b6ff7f310107e5e3" kindref="member">name</ref>(</highlight><highlight class="stringliteral">"C"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>D<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>(loop).<ref refid="classtf_1_1Task_1a08ada0425b490997b6ff7f310107e5e3" kindref="member">name</ref>(</highlight><highlight class="stringliteral">"D"</highlight><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(A);</highlight></codeline>
<codeline><highlight class="normal">A.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(A,<sp/>B);</highlight></codeline>
<codeline><highlight class="normal">B.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(B,<sp/>C);</highlight></codeline>
<codeline><highlight class="normal">C.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(C,<sp/>D);</highlight></codeline>
<codeline><highlight class="normal">D.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(D);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a519777f5783981d534e9e53b99712069" kindref="member">run</ref>(taskflow).wait();<sp/><sp/></highlight><highlight class="comment">//<sp/>counter<sp/>==<sp/>40</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">taskflow.<ref refid="classtf_1_1Taskflow_1ac433018262e44b12c4cc9f0c4748d758" kindref="member">dump</ref>(<ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref>);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"counter<sp/>==<sp/>"</highlight><highlight class="normal"><sp/><<<sp/>counter<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;</highlight></codeline>
</programlisting></para>
<para><dotfile name="multi-condition-task-2.dot"></dotfile>
</para>
<para><simplesect kind="attention"><para>It is your responsibility to ensure the return of a multi-condition task goes to a correct successor task. If a returned index falls outside the successor range of a multi-condition task, the scheduler will skip that index without doing anything. </para>
</simplesect>
</para>
</sect1>
</detaileddescription>
<location file="doxygen/cookbook/conditional_tasking.dox"/>
</compounddef>
</doxygen>