forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_compositions.cpp
More file actions
220 lines (167 loc) · 4.84 KB
/
Copy pathtest_compositions.cpp
File metadata and controls
220 lines (167 loc) · 4.84 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
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest.h>
#include <taskflow/taskflow.hpp>
// --------------------------------------------------------
// Testcase: Composition
// --------------------------------------------------------
TEST_CASE("Composition-1" * doctest::timeout(300)) {
for(unsigned w=1; w<=8; ++w) {
tf::Executor executor(w);
tf::Taskflow f0;
int cnt {0};
auto A = f0.emplace([&cnt](){ ++cnt; });
auto B = f0.emplace([&cnt](){ ++cnt; });
auto C = f0.emplace([&cnt](){ ++cnt; });
auto D = f0.emplace([&cnt](){ ++cnt; });
auto E = f0.emplace([&cnt](){ ++cnt; });
A.precede(B);
B.precede(C);
C.precede(D);
D.precede(E);
tf::Taskflow f1;
// module 1
std::tie(A, B, C, D, E) = f1.emplace(
[&cnt] () { ++cnt; },
[&cnt] () { ++cnt; },
[&cnt] () { ++cnt; },
[&cnt] () { ++cnt; },
[&cnt] () { ++cnt; }
);
A.precede(B);
B.precede(C);
C.precede(D);
D.precede(E);
auto m1_1 = f1.composed_of(f0);
E.precede(m1_1);
executor.run(f1).get();
REQUIRE(cnt == 10);
cnt = 0;
executor.run_n(f1, 100).get();
REQUIRE(cnt == 10 * 100);
auto m1_2 = f1.composed_of(f0);
m1_1.precede(m1_2);
for(int n=0; n<100; n++) {
cnt = 0;
executor.run_n(f1, n).get();
REQUIRE(cnt == 15*n);
}
cnt = 0;
for(int n=0; n<100; n++) {
executor.run(f1);
}
executor.wait_for_all();
REQUIRE(cnt == 1500);
}
}
// TESTCASE: composition-2
TEST_CASE("Composition-2" * doctest::timeout(300)) {
for(unsigned w=1; w<=8; ++w) {
tf::Executor executor(w);
int cnt {0};
// level 0 (+5)
tf::Taskflow f0;
auto A = f0.emplace([&cnt](){ ++cnt; }).name("f0A");
auto B = f0.emplace([&cnt](){ ++cnt; }).name("f0B");
auto C = f0.emplace([&cnt](){ ++cnt; }).name("f0C");
auto D = f0.emplace([&cnt](){ ++cnt; }).name("f0D");
auto E = f0.emplace([&cnt](){ ++cnt; }).name("f0E");
A.precede(B);
B.precede(C);
C.precede(D);
D.precede(E);
// level 1 (+10)
tf::Taskflow f1;
auto m1_1 = f1.composed_of(f0).name("m1_1");
auto m1_2 = f1.composed_of(f0).name("m1_2");
m1_1.precede(m1_2);
// level 2 (+20)
tf::Taskflow f2;
auto m2_1 = f2.composed_of(f1).name("m2_1");
auto m2_2 = f2.composed_of(f1).name("m2_2");
m2_1.precede(m2_2);
//f2.dump(std::cout);
// synchronous run
for(int n=0; n<100; n++) {
cnt = 0;
executor.run_n(f2, n).get();
REQUIRE(cnt == 20*n);
}
// asynchronous run
cnt = 0;
for(int n=0; n<100; n++) {
executor.run(f2);
}
executor.wait_for_all();
REQUIRE(cnt == 100*20);
}
}
// TESTCASE: composition-3
TEST_CASE("Composition-3" * doctest::timeout(300)) {
for(unsigned w=1; w<=8; ++w) {
tf::Executor executor(w);
int cnt {0};
// level 0 (+2)
tf::Taskflow f0;
auto A = f0.emplace([&cnt](){ ++cnt; });
auto B = f0.emplace([&cnt](){ ++cnt; });
A.precede(B);
// level 1 (+4)
tf::Taskflow f1;
auto m1_1 = f1.composed_of(f0);
auto m1_2 = f1.composed_of(f0);
m1_1.precede(m1_2);
// level 2 (+8)
tf::Taskflow f2;
auto m2_1 = f2.composed_of(f1);
auto m2_2 = f2.composed_of(f1);
m2_1.precede(m2_2);
// level 3 (+16)
tf::Taskflow f3;
auto m3_1 = f3.composed_of(f2);
auto m3_2 = f3.composed_of(f2);
m3_1.precede(m3_2);
// synchronous run
for(int n=0; n<100; n++) {
cnt = 0;
executor.run_n(f3, n).get();
REQUIRE(cnt == 16*n);
}
// asynchronous run
cnt = 0;
for(int n=0; n<100; n++) {
executor.run(f3);
}
executor.wait_for_all();
REQUIRE(cnt == 16*100);
}
}
// ----------------------------------------------------------------------------
// ParallelCompositions
// ----------------------------------------------------------------------------
TEST_CASE("ParallelCompositions") {
std::vector<tf::Taskflow> taskflows(100);
tf::Executor executor(4);
tf::Taskflow taskflow;
std::atomic<int> counter{0};
for(auto& tf : taskflows) {
for(size_t n=0; n<100; n++) {
auto [A, B, C, D, E, F, G, H] = tf.emplace(
[&](){ counter.fetch_add(1, std::memory_order_relaxed); },
[&](){ counter.fetch_add(1, std::memory_order_relaxed); },
[&](){ counter.fetch_add(1, std::memory_order_relaxed); },
[&](){ counter.fetch_add(1, std::memory_order_relaxed); },
[&](){ counter.fetch_add(1, std::memory_order_relaxed); },
[&](){ counter.fetch_add(1, std::memory_order_relaxed); },
[&](){ counter.fetch_add(1, std::memory_order_relaxed); },
[&](){ counter.fetch_add(1, std::memory_order_relaxed); }
);
A.precede(B);
A.precede(C);
D.precede(E);
D.precede(F);
}
taskflow.composed_of(tf);
}
executor.run(taskflow).wait();
REQUIRE(counter == 80000);
}