forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter3.dox
167 lines (130 loc) · 5.68 KB
/
Chapter3.dox
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
namespace tf {
/** @page chapter3 C3: Dynamic Tasking
It is very common for a parallel program to
spawn task dependency graphs at runtime.
In Cpp-Taskflow, we call this <em>dynamic tasking</em>.
@section C3_CreateASubflow Create a Subflow
Dynamic tasks are those created during the execution of a graph.
These tasks are spawned from a parent task and are grouped together to a
@em subflow dependency graph.
Cpp-Taskflow has an unified interface for static and dynamic tasking.
To create a subflow, emplace a callable
that takes an argument of type tf::Subflow.
A tf::Subflow object will be created and forwarded to
the execution context of the task.
All methods you find in tf::Taskflow are applicable for tf::Subflow.
@code{.cpp}
1: tf::Taskflow taskflow;
2: tf::Executor executor;
3:
4: tf::Task A = taskflow.emplace([] () {}).name("A"); // static task A
5: tf::Task C = taskflow.emplace([] () {}).name("C"); // static task C
6: tf::Task D = taskflow.emplace([] () {}).name("D"); // static task D
7:
8: tf::Task B = taskflow.emplace([] (tf::Subflow& subflow) {
9: tf::Task B1 = subflow.emplace([] () {}).name("B1"); // dynamic task B1
10: tf::Task B2 = subflow.emplace([] () {}).name("B2"); // dynamic task B2
11: tf::Task B3 = subflow.emplace([] () {}).name("B3"); // dynamic task B3
12: B1.precede(B3); // B1 runs bofore B3
13: B2.precede(B3); // B2 runs before B3
14: }).name("B");
15:
16: A.precede(B); // B runs after A
17: A.precede(C); // C runs after A
18: B.precede(D); // D runs after B
19: C.precede(D); // D runs after C
20:
21: executor.run(taskflow).get(); // execute the graph to spawn the subflow
22: taskflow.dump(std::cout); // dump the taskflow to a DOT format
@endcode
@image html images/subflow_join.svg width=35%
Debrief:
@li Line 1-2 creates a taskflow and an executor
@li Line 4-6 creates three tasks, A, C, and D
@li Line 8-14 creates a task B that spawns a task dependency graph of three tasks B1, B2, and B3
@li Line 16-19 adds dependencies among A, B, C, and D
@li Line 21 submits the graph to an executor and waits until it finishes
@li Line 22 dumps the entire task dependency graph
Line 8-14 is the main block to enable dynamic tasking.
Cpp-Taskflow uses a @std_variant date type to
unify the interface of static tasking and dynamic tasking.
The runtime will create a tf::Subflow passing it to task B,
and spawn a dependency graph as described by the associated callable.
This new subflow graph will be added to the topology of its parent task B.
Due to the property of dynamic tasking,
we cannot dump its structure before execution.
We will need to run the graph first to spawn the graph and then
call tf::Taskflow::dump.
@section DetachASubflow Detach a Subflow
By default, a spawned subflow joins its parent task.
That is, all nodes of zero outgoing edges in the subflow will precede the parent task.
This forces a subflow to follow the dependency constraints after its parent task.
Having said that,
you can detach a subflow from its parent task, allowing its execution to flow independently.
@code{.cpp}
1: tf::Taskflow taskflow;
2:
3: tf::Task A = taskflow.emplace([] () {}).name("A"); // static task A
4: tf::Task C = taskflow.emplace([] () {}).name("C"); // static task C
5: tf::Task D = taskflow.emplace([] () {}).name("D"); // static task D
6:
7: tf::Task B = taskflow.emplace([] (tf::Subflow& subflow) {
8: tf::Task B1 = subflow.emplace([] () {}).name("B1"); // dynamic task B1
9: tf::Task B2 = subflow.emplace([] () {}).name("B2"); // dynamic task B2
10: tf::Task B3 = subflow.emplace([] () {}).name("B3"); // dynamic task B3
11: B1.precede(B3); // B1 runs bofore B3
12: B2.precede(B3); // B2 runs before B3
13: subflow.detach(); // detach this subflow
14: }).name("B");
15:
16: A.precede(B); // B runs after A
17: A.precede(C); // C runs after A
18: B.precede(D); // D runs after B
19: C.precede(D); // D runs after C
20:
21: tf::Executor().run(taskflow).get(); // execute the graph to spawn the subflow
22: taskflow.dump(std::cout); // dump the taskflow to DOT format
@endcode
The figure below demonstrates a detached subflow based on the previous example.
A detached subflow will eventually join the topology of its parent task.
@image html images/subflow_detach.svg width=45%
@section NestedSubflow Nested Subflow
A subflow can be nested or recursive.
You can create another subflow from the execution of a subflow and so on.
@code{.cpp}
1: tf::Taskflow taskflow;
2:
3: tf::Task A = taskflow.emplace([] (tf::Subflow& sbf){
4: std::cout << "A spawns A1 & subflow A2\n";
5: tf::Task A1 = sbf.emplace([] () {
6: std::cout << "subtask A1\n";
7: }).name("A1");
8:
9: tf::Task A2 = sbf.emplace([] (tf::Subflow& sbf2){
10: std::cout << "A2 spawns A2_1 & A2_2\n";
11: tf::Task A2_1 = sbf2.emplace([] () {
12: std::cout << "subtask A2_1\n";
13: }).name("A2_1");
14: tf::Task A2_2 = sbf2.emplace([] () {
15: std::cout << "subtask A2_2\n";
16: }).name("A2_2");
17: A2_1.precede(A2_2);
18: }).name("A2");
19: A1.precede(A2);
20: }).name("A");
21:
22: // execute the graph to spawn the subflow
23: tf::Executor().run(taskflow).get();
24: taskflow.dump(std::cout);
@endcode
@image html images/nested_subflow.svg width=55%
Debrief:
@li Line 1 creates a taskflow object
@li Line 3-20 creates a task to spawn a subflow of two tasks A1 and A2
@li Line 9-18 spawns another subflow of two tasks A2_1 and A2_2 out of its parent task A2
@li Line 23-24 dispatches the graph asynchronously and dump its structure when it finishes
Similarly, you can detach a nested subflow from its parent subflow.
A detached subflow will run independently and eventually join the topology
of its parent subflow.
*/
}