-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathfill.hpp
More file actions
151 lines (132 loc) · 4.56 KB
/
Copy pathfill.hpp
File metadata and controls
151 lines (132 loc) · 4.56 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
#pragma once
#include "../taskflow.hpp"
#include <iterator>
#include <type_traits>
namespace tf {
template <typename B, typename E, typename V, PartitionerLike P = DefaultPartitioner>
auto make_fill_task(B first, E last, V value, 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 {
B_t beg = first;
E_t end = last;
size_t W = rt.executor().num_workers();
size_t N = std::distance(beg, end);
if(N == 0) {
return;
}
// the workload should be sequential
if (W <= 1 || N <= part.chunk_size()) {
part([=]() mutable { std::fill(beg, end, value); })();
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++) {
*beg++ = value;
}
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++) {
*beg++ = value;
}
prev_e = part_e;
});
});
(++w == W) ? task() : rt.silent_async(task);
}
}
};
}
template <typename B, std::integral C, typename V, PartitionerLike P = DefaultPartitioner>
auto make_fill_n_task(B first, C count, V value, P part = P()) {
using B_t = std::decay_t<std::unwrap_ref_decay_t<B>>;
return [=](Runtime &rt) mutable {
B_t beg = first;
size_t W = rt.executor().num_workers();
size_t N = count;
if(N == 0) {
return;
}
// the workload should be sequential
if (W <= 1 || N <= part.chunk_size()) {
part([=]() mutable { std::fill_n(beg, count, value); })();
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++) {
*beg++ = value;
}
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++) {
*beg++ = value;
}
prev_e = part_e;
});
});
(++w == W) ? task() : rt.silent_async(task);
}
}
};
}
template <typename B, typename E, typename V, PartitionerLike P>
Task FlowBuilder::fill(B first, E last, V value, P part) {
return emplace(make_fill_task(first, last, value, part));
}
template <typename B, std::integral C, typename V, PartitionerLike P>
Task FlowBuilder::fill_n(B first, C count, V value, P part) {
return emplace(make_fill_n_task(first, count, value, part));
}
} // namespace tf ---------------------------------------------------------------------------------