FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-taskflow/taskflow/algorithm/reduce.hpp at master · aimoonchen/cpp-taskflow · GitHub
aimoonchen
/
cpp-taskflow
Public
forked from
taskflow/taskflow
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp-taskflow
/
taskflow
/
algorithm
/
reduce.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
546 lines (419 loc) · 15.7 KB
Breadcrumbs
cpp-taskflow
/
taskflow
/
algorithm
/
reduce.hpp
Copy path
File metadata and controls
546 lines (419 loc) · 15.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#
pragma
once
#
include
"
../taskflow.hpp
"
namespace
tf
{
//
Function: make_reduce_task
template
<
typename
B,
typename
E,
typename
T,
typename
O,
typename
P = DefaultPartitioner>
auto
make_reduce_task
(B b, E e, T& init, O bop, P part = P()) {
using
namespace
std
::string_literals
;
using
B_t = std::
decay_t
<
unwrap_ref_decay_t
<B>>;
using
E_t = std::
decay_t
<
unwrap_ref_decay_t
<E>>;
return
[=, &r=init] (Runtime& rt)
mutable
{
//
fetch the iterator values
B_t beg = b;
E_t end = e;
size_t
W = rt.
executor
().
num_workers
();
size_t
N =
std::distance
(beg, end);
//
only myself - no need to spawn another graph
if
(W <=
1
|| N <= part.
chunk_size
()) {
part
([&](){
for
(; beg!=end; r =
bop
(r, *beg++)); })();
return
;
}
PreemptionGuard
preemption_guard
(rt);
if
(N < W) {
W = N;
}
auto
mutex = std::make_shared<std::mutex>();
//
static partitioner
if
constexpr
(part.
type
() == PartitionerType::
STATIC
) {
for
(
size_t
w=
0
, curr_b=
0
; w<W && curr_b < N;) {
//
we force chunk size to be at least two because the temporary
//
variable sum need to avoid copy at the first step
auto
chunk_size =
std::max
(
size_t
{
2
}, part.
adjusted_chunk_size
(N, W, w));
auto
task =
part
([=, &r] ()
mutable
{
std::advance
(beg, curr_b);
if
(N - curr_b ==
1
) {
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop
(r, *beg);
return
;
}
auto
beg1 = beg++;
auto
beg2 = beg++;
T sum =
bop
(*beg1, *beg2);
//
loop reduce
part.
loop
(N, W, curr_b, chunk_size,
[=, &sum, prev_e=curr_b+
2
](
size_t
part_b,
size_t
part_e)
mutable
{
if
(part_b > prev_e) {
std::advance
(beg, part_b - prev_e);
}
else
{
part_b = prev_e;
}
for
(
size_t
x=part_b; x<part_e; x++, beg++) {
sum =
bop
(sum, *beg);
}
prev_e = part_e;
}
);
//
final reduce
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop
(r, sum);
});
(++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
([=, &r] ()
mutable
{
//
pre-reduce
size_t
s0 = next->
fetch_add
(
2
, std::memory_order_relaxed);
if
(s0 >= N) {
return
;
}
std::advance
(beg, s0);
if
(N - s0 ==
1
) {
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop
(r, *beg);
return
;
}
auto
beg1 = beg++;
auto
beg2 = beg++;
T sum =
bop
(*beg1, *beg2);
//
loop reduce
part.
loop
(N, W, *next,
[=, &sum, prev_e=s0+
2
](
size_t
curr_b,
size_t
curr_e)
mutable
{
std::advance
(beg, curr_b - prev_e);
for
(
size_t
x=curr_b; x<curr_e; x++, beg++) {
sum =
bop
(sum, *beg);
}
prev_e = curr_e;
}
);
//
final reduce
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop
(r, sum);
});
(++w == W) ?
task
() : rt.
silent_async
(task);
}
}
};
}
//
Function: make_transform_reduce_task
template
<
typename
B,
typename
E,
typename
T,
typename
BOP
,
typename
UOP
,
typename
P = DefaultPartitioner
>
auto
make_transform_reduce_task
(B b, E e, T& init,
BOP
bop,
UOP
uop, P part = P()) {
using
namespace
std
::string_literals
;
using
B_t = std::
decay_t
<
unwrap_ref_decay_t
<B>>;
using
E_t = std::
decay_t
<
unwrap_ref_decay_t
<E>>;
return
[=, &r=init] (Runtime& rt)
mutable
{
//
fetch the iterator values
B_t beg = b;
E_t end = e;
size_t
W = rt.
executor
().
num_workers
();
size_t
N =
std::distance
(beg, end);
//
only myself - no need to spawn another graph
if
(W <=
1
|| N <= part.
chunk_size
()) {
part
([&](){
for
(; beg!=end; r =
bop
(
std::move
(r),
uop
(*beg++))); })();
return
;
}
PreemptionGuard
preemption_guard
(rt);
if
(N < W) {
W = N;
}
auto
mutex = std::make_shared<std::mutex>();
//
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
([=, &r] ()
mutable
{
std::advance
(beg, curr_b);
if
(N - curr_b ==
1
) {
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop
(
std::move
(r),
uop
(*beg));
return
;
}
//
auto beg1 = beg++;
//
auto beg2 = beg++;
//
T sum = bop(uop(*beg1), uop(*beg2));
T sum = (chunk_size ==
1
) ?
uop
(*beg++) :
bop
(
uop
(*beg++),
uop
(*beg++));
//
loop reduce
part.
loop
(N, W, curr_b, chunk_size,
[=, &sum, prev_e=curr_b+(chunk_size ==
1
?
1
:
2
)]
(
size_t
part_b,
size_t
part_e)
mutable
{
if
(part_b > prev_e) {
std::advance
(beg, part_b - prev_e);
}
else
{
part_b = prev_e;
}
for
(
size_t
x=part_b; x<part_e; x++, beg++) {
sum =
bop
(
std::move
(sum),
uop
(*beg));
}
prev_e = part_e;
}
);
//
final reduce
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop
(
std::move
(r),
std::move
(sum));
});
(++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
([=, &r] ()
mutable
{
//
pre-reduce
size_t
s0 = next->
fetch_add
(
2
, std::memory_order_relaxed);
if
(s0 >= N) {
return
;
}
std::advance
(beg, s0);
if
(N - s0 ==
1
) {
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop
(
std::move
(r),
uop
(*beg));
return
;
}
auto
beg1 = beg++;
auto
beg2 = beg++;
T sum =
bop
(
uop
(*beg1),
uop
(*beg2));
//
loop reduce
part.
loop
(N, W, *next,
[=, &sum, prev_e=s0+
2
](
size_t
curr_b,
size_t
curr_e)
mutable
{
std::advance
(beg, curr_b - prev_e);
for
(
size_t
x=curr_b; x<curr_e; x++, beg++) {
sum =
bop
(
std::move
(sum),
uop
(*beg));
}
prev_e = curr_e;
}
);
//
final reduce
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop
(
std::move
(r),
std::move
(sum));
});
(++w == W) ?
task
() : rt.
silent_async
(task);
}
}
};
}
//
Function: make_transform_reduce_task with two binary operation
template
<
typename
B1
,
typename
E1
,
typename
B2
,
typename
T,
typename
BOP_R
,
typename
BOP_T
,
typename
P = DefaultPartitioner,
std::
enable_if_t
<!is_partitioner_v<std::
decay_t
<
BOP_T
>>,
void
>* =
nullptr
>
auto
make_transform_reduce_task
(
B1
b1,
E1
e1
,
B2
b2, T& init,
BOP_R
bop_r,
BOP_T
bop_t
, P part = P()
) {
using
namespace
std
::string_literals
;
using
B1_t = std::
decay_t
<
unwrap_ref_decay_t
<
B1
>>;
using
E1_t = std::
decay_t
<
unwrap_ref_decay_t
<
E1
>>;
using
B2_t = std::
decay_t
<
unwrap_ref_decay_t
<
B2
>>;
return
[=, &r=init] (Runtime& rt)
mutable
{
//
fetch the iterator values
B1_t beg1 = b1;
E1_t end1 =
e1
;
B2_t beg2 = b2;
size_t
W = rt.
executor
().
num_workers
();
size_t
N =
std::distance
(beg1, end1);
//
only myself - no need to spawn another graph
if
(W <=
1
|| N <= part.
chunk_size
()) {
part
([&](){
for
(; beg1!=end1; r =
bop_r
(
std::move
(r),
bop_t
(*beg1++, *beg2++))); })();
return
;
}
PreemptionGuard
preemption_guard
(rt);
if
(N < W) {
W = N;
}
auto
mutex = std::make_shared<std::mutex>();
//
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
([=, &r] ()
mutable
{
std::advance
(beg1, curr_b);
std::advance
(beg2, curr_b);
if
(N - curr_b ==
1
) {
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop_r
(
std::move
(r),
bop_t
(*beg1, *beg2));
return
;
}
T sum = (chunk_size ==
1
) ?
bop_t
(*beg1++, *beg2++) :
bop_r
(
bop_t
(*beg1++, *beg2++),
bop_t
(*beg1++, *beg2++));
//
loop reduce
part.
loop
(N, W, curr_b, chunk_size,
[=, &sum, prev_e=curr_b+(chunk_size ==
1
?
1
:
2
)]
(
size_t
part_b,
size_t
part_e)
mutable
{
if
(part_b > prev_e) {
std::advance
(beg1, part_b - prev_e);
std::advance
(beg2, part_b - prev_e);
}
else
{
part_b = prev_e;
}
for
(
size_t
x=part_b; x<part_e; x++, beg1++, beg2++) {
sum =
bop_r
(
std::move
(sum),
bop_t
(*beg1, *beg2));
}
prev_e = part_e;
}
);
//
final reduce
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop_r
(
std::move
(r),
std::move
(sum));
});
(++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
([=, &r] ()
mutable
{
//
pre-reduce
size_t
s0 = next->
fetch_add
(
2
, std::memory_order_relaxed);
if
(s0 >= N) {
return
;
}
std::advance
(beg1, s0);
std::advance
(beg2, s0);
if
(N - s0 ==
1
) {
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop_r
(
std::move
(r),
bop_t
(*beg1, *beg2));
return
;
}
auto
beg11 = beg1++;
auto
beg12 = beg1++;
auto
beg21 = beg2++;
auto
beg22 = beg2++;
T sum =
bop_r
(
bop_t
(*beg11, *beg21),
bop_t
(*beg12, *beg22));
//
loop reduce
part.
loop
(N, W, *next,
[=, &sum, prev_e=s0+
2
](
size_t
curr_b,
size_t
curr_e)
mutable
{
std::advance
(beg1, curr_b - prev_e);
std::advance
(beg2, curr_b - prev_e);
for
(
size_t
x=curr_b; x<curr_e; x++, beg1++, beg2++) {
sum =
bop_r
(
std::move
(sum),
bop_t
(*beg1, *beg2));
}
prev_e = curr_e;
}
);
//
final reduce
std::lock_guard<std::mutex>
lock
(*mutex);
r =
bop_r
(
std::move
(r),
std::move
(sum));
});
(++w == W) ?
task
() : rt.
silent_async
(task);
}
}
};
}
//
Function: make_reduce_by_index_task
template
<
typename
R,
typename
T,
typename
L,
typename
G,
typename
P = DefaultPartitioner>
auto
make_reduce_by_index_task
(R range, T& init, L lop, G gop, P part = P()) {
using
range_type = std::
decay_t
<
unwrap_ref_decay_t
<R>>;
return
[=, &init] (Runtime& rt)
mutable
{
//
fetch the iterator values
range_type r = range;
//
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
();
//
only myself - no need to spawn another graph
if
(W <=
1
|| N <= part.
chunk_size
()) {
part
([&](){ init =
lop
(r,
std::move
(init)); })();
return
;
}
PreemptionGuard
preemption_guard
(rt);
if
(N < W) {
W = N;
}
auto
mutex = std::make_shared<std::mutex>();
//
static partitioner
if
constexpr
(part.
type
() == PartitionerType::
STATIC
) {
for
(
size_t
w=
0
, curr_b=
0
; w<W && curr_b < N;) {
//
we force chunk size to be at least two because the temporary
//
variable sum need to avoid copy at the first step
auto
chunk_size = part.
adjusted_chunk_size
(N, W, w);
auto
task =
part
([=, &init] ()
mutable
{
//
temporary result so far
std::optional<T> tmp;
//
loop reduce
part.
loop
(N, W, curr_b, chunk_size, [=, &tmp](
size_t
part_b,
size_t
part_e)
mutable
{
tmp =
lop
(r.
discrete_domain
(part_b, part_e),
std::move
(tmp));
});
//
final reduce - tmp is guaranteed to have value
//
assert(tmp.has_value());
std::lock_guard<std::mutex>
lock
(*mutex);
init =
gop
(
std::move
(init),
std::move
(*tmp));
});
(++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
([=, &init] ()
mutable
{
//
temporary result so far
std::optional<T> tmp;
//
loop reduce
part.
loop
(N, W, *next, [=, &tmp](
size_t
part_b,
size_t
part_e)
mutable
{
tmp =
lop
(r.
discrete_domain
(part_b, part_e),
std::move
(tmp));
});
//
final reduce - need to check if the running total has value since
//
this is a dynamic scheduler; the worker may not actually acquire any work
if
(tmp) {
std::lock_guard<std::mutex>
lock
(*mutex);
init =
gop
(
std::move
(init),
std::move
(*tmp));
}
});
(++w == W) ?
task
() : rt.
silent_async
(task);
}
}
};
}
//
------------------------------------------------------------------------------------------------
//
default reduction
//
------------------------------------------------------------------------------------------------
//
Function: reduce
template
<
typename
B,
typename
E,
typename
T,
typename
O,
typename
P>
Task
FlowBuilder::reduce
(B beg, E end, T& init, O bop, P part) {
return
emplace
(
make_reduce_task
(beg, end, init, bop, part));
}
//
------------------------------------------------------------------------------------------------
//
default transform and reduction
//
------------------------------------------------------------------------------------------------
//
Function: transform_reduce
template
<
typename
B,
typename
E,
typename
T,
typename
BOP
,
typename
UOP
,
typename
P,
std::
enable_if_t
<is_partitioner_v<std::
decay_t
<P>>,
void
>*
>
Task
FlowBuilder::transform_reduce
(
B beg, E end, T& init,
BOP
bop,
UOP
uop, P part
) {
return
emplace
(
make_transform_reduce_task
(beg, end, init, bop, uop, part));
}
//
Function: transform_reduce
template
<
typename
B1
,
typename
E1
,
typename
B2
,
typename
T,
typename
BOP_R
,
typename
BOP_T
,
typename
P,
std::
enable_if_t
<!is_partitioner_v<std::
decay_t
<
BOP_T
>>,
void
>*
>
Task
FlowBuilder::transform_reduce
(
B1
beg1,
E1
end1,
B2
beg2, T& init,
BOP_R
bop_r,
BOP_T
bop_t
, P part
) {
return
emplace
(
make_transform_reduce_task
(beg1, end1, beg2, init, bop_r,
bop_t
, part));
}
//
------------------------------------------------------------------------------------------------
//
default reduce_by_key
//
------------------------------------------------------------------------------------------------
//
Function: make_index_reduce_task
template
<
typename
R,
typename
T,
typename
L,
typename
G,
typename
P>
Task
FlowBuilder::reduce_by_index
(R range, T& init, L lop, G gop, P part) {
return
emplace
(
make_reduce_by_index_task
(range, init, lop, gop, part));
}
}
//
end of namespace tf -------------------------------------------------------------------------
Back
|
FazBrowse Home
|
New Git URL