-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathfor_each.hpp
More file actions
251 lines (213 loc) · 7.36 KB
/
Copy pathfor_each.hpp
File metadata and controls
251 lines (213 loc) · 7.36 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
#pragma once
#include "../taskflow.hpp"
namespace tf {
// Function: make_for_each_task
template <typename B, typename E, typename C, PartitionerLike P = DefaultPartitioner>
auto make_for_each_task(B b, E e, C c, P part = P()) {
using B_t = std::decay_t<std::unwrap_ref_decay_t<B>>;
using E_t = std::decay_t<std::unwrap_ref_decay_t<E>>;
return [=] (Runtime& rt) mutable {
// fetch the stateful values
B_t beg = b;
E_t end = e;
size_t W = rt.executor().num_workers();
size_t N = std::distance(beg, end);
if(N == 0) {
return;
}
// the workload is sequentially doable
if(W <= 1 || N <= part.chunk_size()) {
part([=]() mutable { std::for_each(beg, end, c); })();
return;
}
// use no more workers than the iteration count
if(N < W) {
W = N;
}
// static partitioner
if constexpr(part.type() == PartitionerType::STATIC) {
for(size_t w=0, curr_b=0; w<W && curr_b < N;) {
auto chunk_size = part.adjusted_chunk_size(N, W, w);
auto task = part([=] () mutable {
part.loop(N, W, curr_b, chunk_size,
[=, prev_e=size_t{0}](size_t part_b, size_t part_e) mutable {
std::advance(beg, part_b - prev_e);
for(size_t x = part_b; x<part_e; x++) {
c(*beg++);
}
prev_e = part_e;
}
);
});
(++w == W || (curr_b += chunk_size) >= N) ? task() : rt.silent_async(task);
}
}
// dynamic partitioner
else {
auto next = std::make_shared<std::atomic<size_t>>(0);
for(size_t w=0; w<W;) {
auto task = part([=] () mutable {
part.loop(N, W, *next,
[=, prev_e=size_t{0}](size_t part_b, size_t part_e) mutable {
std::advance(beg, part_b - prev_e);
for(size_t x = part_b; x<part_e; x++) {
c(*beg++);
}
prev_e = part_e;
}
);
});
(++w == W) ? task() : rt.silent_async(task);
}
}
};
}
// Function: make_for_each_index_task
template <typename B, typename E, typename S, typename C, PartitionerLike P = DefaultPartitioner>
auto make_for_each_index_task(B b, E e, S s, C c, P part = P()){
using B_t = std::decay_t<std::unwrap_ref_decay_t<B>>;
using E_t = std::decay_t<std::unwrap_ref_decay_t<E>>;
using S_t = std::decay_t<std::unwrap_ref_decay_t<S>>;
return [=] (Runtime& rt) mutable {
// fetch the iterator values
B_t beg = b;
E_t end = e;
S_t inc = s;
// nothing to be done if the range is invalid
if(is_index_range_invalid(beg, end, inc)) {
return;
}
size_t W = rt.executor().num_workers();
size_t N = distance(beg, end, inc);
if(N == 0) {
return;
}
// only myself - no need to spawn another graph
if(W <= 1 || N <= part.chunk_size()) {
part([=]() mutable {
for(size_t x=0; x<N; x++, beg+=inc) {
c(beg);
}
})();
return;
}
if(N < W) {
W = N;
}
// static partitioner
if constexpr(P::type() == PartitionerType::STATIC) {
for(size_t w=0, curr_b=0; w<W && curr_b < N;) {
auto chunk_size = part.adjusted_chunk_size(N, W, w);
auto task = part([=] () mutable {
part.loop(N, W, curr_b, chunk_size, [=] (size_t part_b, size_t part_e) {
auto idx = static_cast<B_t>(part_b) * inc + beg;
for(size_t x=part_b; x<part_e; x++, idx += inc) {
c(idx);
}
});
});
(++w == W || (curr_b += chunk_size) >= N) ? task() : rt.silent_async(task);
}
}
// dynamic partitioner
else {
auto next = std::make_shared<std::atomic<size_t>>(0);
for(size_t w=0; w<W;) {
auto task = part([=] () mutable {
part.loop(N, W, *next, [=] (size_t part_b, size_t part_e) mutable {
auto idx = static_cast<B_t>(part_b) * inc + beg;
for(size_t x=part_b; x<part_e; x++, idx += inc) {
c(idx);
}
});
});
(++w == W) ? task() : rt.silent_async(task);
}
}
};
}
// Function: make_for_each_by_index_task
template <IndexRangesLike R, typename C, PartitionerLike P = DefaultPartitioner>
auto make_for_each_by_index_task(R range, C c, P part = P()){
using range_type = std::decay_t<std::unwrap_ref_decay_t<R>>;
return [=] (Runtime& rt) mutable {
// fetch the iterator values
range_type r = range;
if constexpr (range_type::rank == 1) {
// nothing to be done if the range is invalid
if(is_index_range_invalid(r.begin(), r.end(), r.step_size())) {
return;
}
}
size_t W = rt.executor().num_workers();
size_t N = r.size();
// nothing to do if the active iteration space is empty
if(N == 0) {
return;
}
// only myself - no need to spawn another graph
if(W <= 1 || N <= part.chunk_size()) {
part([=]() mutable { c(r); })();
return;
}
if(N < W) {
W = N;
}
// static partitioner
if constexpr(part.type() == PartitionerType::STATIC) {
for(size_t w=0, curr_b=0; w<W && curr_b < N;) {
// 1D ranges balance the remainder exactly across workers (no hyperplane alignment needed);
// N-D ranges need a single chunk size shared by all workers, snapped to the nearest
// hyperplane boundary so lower_slice returns one box per inner iteration in
// the common case.
size_t chunk_size;
if constexpr (range_type::rank == 1) {
chunk_size = part.adjusted_chunk_size(N, W, w);
}
else {
chunk_size = r.ceil(
part.chunk_size() == 0 ? (N + W - 1) / W : part.chunk_size()
);
}
auto task = part([=] () mutable { part.loop(r, N, W, curr_b, chunk_size, c); });
(++w == W || (curr_b += chunk_size) >= N) ? task() : rt.silent_async(task);
}
}
// dynamic partitioner
else {
auto next = std::make_shared<std::atomic<size_t>>(0);
for(size_t w=0; w<W;) {
auto task = part([=] () mutable { part.loop(r, N, W, *next, c); });
(++w == W) ? task() : rt.silent_async(task);
}
}
};
}
// ------------------------------------------------------------------------------------------------
// for_each
// ------------------------------------------------------------------------------------------------
// Function: for_each
template <typename B, typename E, typename C, PartitionerLike P>
Task FlowBuilder::for_each(B beg, E end, C c, P part) {
return emplace(
make_for_each_task(beg, end, c, part)
);
}
// ------------------------------------------------------------------------------------------------
// for_each_index
// ------------------------------------------------------------------------------------------------
// Function: for_each_index
template <typename B, typename E, typename S, typename C, PartitionerLike P>
Task FlowBuilder::for_each_index(B beg, E end, S inc, C c, P part){
return emplace(
make_for_each_index_task(beg, end, inc, c, part)
);
}
// Function: for_each_by_index
template <IndexRangesLike R, typename C, PartitionerLike P>
Task FlowBuilder::for_each_by_index(R range, C c, P part){
return emplace(
make_for_each_by_index_task(range, c, part)
);
}
} // end of namespace tf -------------------------------------------------------------------------