FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
taskflow/taskflow/algorithm/transform.hpp at master · taskflow/taskflow · GitHub
taskflow
/
taskflow
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
1.4k
Star
12.2k
Code
Issues
20
Pull requests
16
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
taskflow
/
taskflow
/
algorithm
/
transform.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
211 lines (183 loc) · 6.28 KB
Breadcrumbs
taskflow
/
taskflow
/
algorithm
/
transform.hpp
Copy path
File metadata and controls
211 lines (183 loc) · 6.28 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
#
pragma
once
#
include
"
../taskflow.hpp
"
namespace
tf
{
//
Function: make_transform_task
template
<InputIteratorLike B, InputIteratorLike E,
typename
O,
typename
C,
PartitionerLike P = DefaultPartitioner>
requires
UnaryTransformLike<
C,
std::
decay_t
<std::
unwrap_ref_decay_t
<B>>,
std::
decay_t
<std::
unwrap_ref_decay_t
<O>>
>
auto
make_transform_task
(B first1, E last1, O d_first, C c, P part = P()) {
using
namespace
std
::string_literals
;
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
O_t = std::
decay_t
<std::
unwrap_ref_decay_t
<O>>;
return
[=] (Runtime& rt)
mutable
{
//
fetch the stateful values
B_t beg = first1;
E_t end = last1;
O_t d_beg = d_first;
size_t
W = rt.
executor
().
num_workers
();
size_t
N =
std::distance
(beg, end);
if
(N ==
0
) {
return
;
}
//
only myself - no need to spawn another graph
if
(W <=
1
|| N <= part.
chunk_size
()) {
part
([=]()
mutable
{
std::transform
(beg, end, d_beg, c); })();
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;) {
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);
std::advance
(d_beg, part_b - prev_e);
for
(
size_t
x = part_b; x<part_e; x++) {
*d_beg++ =
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);
std::advance
(d_beg, part_b - prev_e);
for
(
size_t
x = part_b; x<part_e; x++) {
*d_beg++ =
c
(*beg++);
}
prev_e = part_e;
});
});
(++w == W) ?
task
() : rt.
silent_async
(task);
}
}
};
}
//
Function: make_transform_task
template
<InputIteratorLike
B1
, InputIteratorLike
E1
, InputIteratorLike
B2
,
typename
O,
typename
C,
PartitionerLike P = DefaultPartitioner>
requires
BinaryTransformLike<
C,
std::
decay_t
<std::
unwrap_ref_decay_t
<
B1
>>,
std::
decay_t
<std::
unwrap_ref_decay_t
<
B2
>>,
std::
decay_t
<std::
unwrap_ref_decay_t
<O>>
>
auto
make_transform_task
(
B1
first1,
E1
last1,
B2
first2, O d_first, C c, P part = P()) {
using
namespace
std
::string_literals
;
using
B1_t = std::
decay_t
<std::
unwrap_ref_decay_t
<
B1
>>;
using
E1_t = std::
decay_t
<std::
unwrap_ref_decay_t
<
E1
>>;
using
B2_t = std::
decay_t
<std::
unwrap_ref_decay_t
<
B2
>>;
using
O_t = std::
decay_t
<std::
unwrap_ref_decay_t
<O>>;
return
[=] (Runtime& rt)
mutable
{
//
fetch the stateful values
B1_t beg1 = first1;
E1_t end1 = last1;
B2_t beg2 = first2;
O_t d_beg = d_first;
size_t
W = rt.
executor
().
num_workers
();
size_t
N =
std::distance
(beg1, end1);
if
(N ==
0
) {
return
;
}
//
only myself - no need to spawn another graph
if
(W <=
1
|| N <= part.
chunk_size
()) {
part
([=]()
mutable
{
std::transform
(beg1, end1, beg2, d_beg, c); })();
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;) {
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
(beg1, part_b - prev_e);
std::advance
(beg2, part_b - prev_e);
std::advance
(d_beg, part_b - prev_e);
for
(
size_t
x = part_b; x<part_e; x++) {
*d_beg++ =
c
(*beg1++, *beg2++);
}
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
(beg1, part_b - prev_e);
std::advance
(beg2, part_b - prev_e);
std::advance
(d_beg, part_b - prev_e);
for
(
size_t
x = part_b; x<part_e; x++) {
*d_beg++ =
c
(*beg1++, *beg2++);
}
prev_e = part_e;
});
});
(++w == W) ?
task
() : rt.
silent_async
(task);
}
}
};
}
//
----------------------------------------------------------------------------
//
transform
//
----------------------------------------------------------------------------
//
Function: transform
template
<InputIteratorLike B, InputIteratorLike E,
typename
O,
typename
C, PartitionerLike P>
requires
UnaryTransformLike<
C,
std::
decay_t
<std::
unwrap_ref_decay_t
<B>>,
std::
decay_t
<std::
unwrap_ref_decay_t
<O>>
>
Task
FlowBuilder::transform
(B first1, E last1, O d_first, C c, P part) {
return
emplace
(
make_transform_task
(first1, last1, d_first, c, part)
);
}
//
----------------------------------------------------------------------------
//
transform2
//
----------------------------------------------------------------------------
//
Function: transform
template
<
InputIteratorLike
B1
,
InputIteratorLike
E1
,
InputIteratorLike
B2
,
typename
O,
typename
C,
PartitionerLike P
>
requires
BinaryTransformLike<
C,
std::
decay_t
<std::
unwrap_ref_decay_t
<
B1
>>,
std::
decay_t
<std::
unwrap_ref_decay_t
<
B2
>>,
std::
decay_t
<std::
unwrap_ref_decay_t
<O>>
>
Task
FlowBuilder::transform
(
B1
first1,
E1
last1,
B2
first2, O d_first, C c, P part
) {
return
emplace
(
make_transform_task
(
first1, last1, first2, d_first, c, part
));
}
}
//
end of namespace tf -----------------------------------------------------
Back
|
FazBrowse Home
|
New Git URL