This repository was archived by the owner on Sep 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathinclusive_scan.hpp
More file actions
251 lines (194 loc) · 8.67 KB
/
inclusive_scan.hpp
File metadata and controls
251 lines (194 loc) · 8.67 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
// SPDX-FileCopyrightText: Intel Corporation
//
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <optional>
#include <sycl/sycl.hpp>
#include <oneapi/dpl/execution>
#include <oneapi/dpl/numeric>
#include <oneapi/dpl/async>
#include <dr/concepts/concepts.hpp>
#include <dr/detail/onedpl_direct_iterator.hpp>
#include <dr/shp/algorithms/execution_policy.hpp>
#include <dr/shp/allocators.hpp>
#include <dr/shp/detail.hpp>
#include <dr/shp/init.hpp>
#include <dr/shp/vector.hpp>
#include <dr/shp/views/views.hpp>
namespace dr::shp {
template <typename ExecutionPolicy, dr::distributed_contiguous_range R,
dr::distributed_contiguous_range O, typename BinaryOp,
typename U = rng::range_value_t<R>>
void inclusive_scan_impl_(ExecutionPolicy &&policy, R &&r, O &&o,
BinaryOp &&binary_op, std::optional<U> init = {}) {
using T = rng::range_value_t<O>;
static_assert(
std::is_same_v<std::remove_cvref_t<ExecutionPolicy>, device_policy>);
auto zipped_view = dr::shp::views::zip(r, o);
auto zipped_segments = zipped_view.zipped_segments();
if constexpr (std::is_same_v<std::remove_cvref_t<ExecutionPolicy>,
device_policy>) {
std::vector<sycl::event> events;
dr::shp::device_allocator<T> allocator(__detail::queue(0));
dr::shp::vector<T, dr::shp::device_allocator<T>> partial_sums(
std::size_t(zipped_segments.size()), allocator);
std::size_t segment_id = 0;
for (auto &&segs : zipped_segments) {
auto &&[in_segment, out_segment] = segs;
auto &&q = __detail::queue(dr::ranges::rank(in_segment));
auto &&local_policy = __detail::dpl_policy(dr::ranges::rank(in_segment));
auto dist = rng::distance(in_segment);
assert(dist > 0);
auto first = rng::begin(in_segment);
auto last = rng::end(in_segment);
auto d_first = rng::begin(out_segment);
sycl::event event;
if (segment_id == 0 && init.has_value()) {
event = oneapi::dpl::experimental::inclusive_scan_async(
local_policy, dr::__detail::direct_iterator(first),
dr::__detail::direct_iterator(last),
dr::__detail::direct_iterator(d_first), binary_op, init.value());
} else {
event = oneapi::dpl::experimental::inclusive_scan_async(
local_policy, dr::__detail::direct_iterator(first),
dr::__detail::direct_iterator(last),
dr::__detail::direct_iterator(d_first), binary_op);
}
auto dst_iter = dr::ranges::local(partial_sums).data() + segment_id;
auto src_iter = dr::ranges::local(out_segment).data();
rng::advance(src_iter, dist - 1);
auto e = q.submit([&](auto &&h) {
h.depends_on(event);
h.single_task([=]() {
rng::range_value_t<O> value = *src_iter;
*dst_iter = value;
});
});
events.push_back(e);
segment_id++;
}
__detail::wait(events);
events.clear();
auto &&local_policy = __detail::dpl_policy(0);
auto first = dr::ranges::local(partial_sums).data();
auto last = first + partial_sums.size();
oneapi::dpl::experimental::inclusive_scan_async(local_policy, first, last,
first, binary_op)
.wait();
std::size_t idx = 0;
for (auto &&segs : zipped_segments) {
auto &&[in_segment, out_segment] = segs;
if (idx > 0) {
auto &&q = __detail::queue(dr::ranges::rank(out_segment));
auto first = rng::begin(out_segment);
dr::__detail::direct_iterator d_first(first);
auto d_sum =
dr::ranges::__detail::local(partial_sums).begin() + idx - 1;
sycl::event e = dr::__detail::parallel_for(
q, sycl::range<>(rng::distance(out_segment)),
[=](auto idx) { d_first[idx] = binary_op(d_first[idx], *d_sum); });
events.push_back(e);
}
idx++;
}
__detail::wait(events);
} else {
assert(false);
}
}
template <typename ExecutionPolicy, dr::distributed_contiguous_range R,
dr::distributed_contiguous_range O, typename BinaryOp, typename T>
void inclusive_scan(ExecutionPolicy &&policy, R &&r, O &&o,
BinaryOp &&binary_op, T init) {
inclusive_scan_impl_(std::forward<ExecutionPolicy>(policy),
std::forward<R>(r), std::forward<O>(o),
std::forward<BinaryOp>(binary_op), std::optional(init));
}
template <typename ExecutionPolicy, dr::distributed_contiguous_range R,
dr::distributed_contiguous_range O, typename BinaryOp>
void inclusive_scan(ExecutionPolicy &&policy, R &&r, O &&o,
BinaryOp &&binary_op) {
inclusive_scan_impl_(std::forward<ExecutionPolicy>(policy),
std::forward<R>(r), std::forward<O>(o),
std::forward<BinaryOp>(binary_op));
}
template <typename ExecutionPolicy, dr::distributed_contiguous_range R,
dr::distributed_contiguous_range O>
void inclusive_scan(ExecutionPolicy &&policy, R &&r, O &&o) {
inclusive_scan(std::forward<ExecutionPolicy>(policy), std::forward<R>(r),
std::forward<O>(o), std::plus<rng::range_value_t<R>>());
}
// Distributed iterator versions
template <typename ExecutionPolicy, dr::distributed_iterator Iter,
dr::distributed_iterator OutputIter, typename BinaryOp, typename T>
OutputIter inclusive_scan(ExecutionPolicy &&policy, Iter first, Iter last,
OutputIter d_first, BinaryOp &&binary_op, T init) {
auto dist = rng::distance(first, last);
auto d_last = d_first;
rng::advance(d_last, dist);
inclusive_scan(std::forward<ExecutionPolicy>(policy),
rng::subrange(first, last), rng::subrange(d_first, d_last),
std::forward<BinaryOp>(binary_op), init);
return d_last;
}
template <typename ExecutionPolicy, dr::distributed_iterator Iter,
dr::distributed_iterator OutputIter, typename BinaryOp>
OutputIter inclusive_scan(ExecutionPolicy &&policy, Iter first, Iter last,
OutputIter d_first, BinaryOp &&binary_op) {
auto dist = rng::distance(first, last);
auto d_last = d_first;
rng::advance(d_last, dist);
inclusive_scan(std::forward<ExecutionPolicy>(policy),
rng::subrange(first, last), rng::subrange(d_first, d_last),
std::forward<BinaryOp>(binary_op));
return d_last;
}
template <typename ExecutionPolicy, dr::distributed_iterator Iter,
dr::distributed_iterator OutputIter>
OutputIter inclusive_scan(ExecutionPolicy &&policy, Iter first, Iter last,
OutputIter d_first) {
auto dist = rng::distance(first, last);
auto d_last = d_first;
rng::advance(d_last, dist);
inclusive_scan(std::forward<ExecutionPolicy>(policy),
rng::subrange(first, last), rng::subrange(d_first, d_last));
return d_last;
}
// Execution policy-less versions
template <dr::distributed_contiguous_range R,
dr::distributed_contiguous_range O>
void inclusive_scan(R &&r, O &&o) {
inclusive_scan(dr::shp::par_unseq, std::forward<R>(r), std::forward<O>(o));
}
template <dr::distributed_contiguous_range R,
dr::distributed_contiguous_range O, typename BinaryOp>
void inclusive_scan(R &&r, O &&o, BinaryOp &&binary_op) {
inclusive_scan(dr::shp::par_unseq, std::forward<R>(r), std::forward<O>(o),
std::forward<BinaryOp>(binary_op));
}
template <dr::distributed_contiguous_range R,
dr::distributed_contiguous_range O, typename BinaryOp, typename T>
void inclusive_scan(R &&r, O &&o, BinaryOp &&binary_op, T init) {
inclusive_scan(dr::shp::par_unseq, std::forward<R>(r), std::forward<O>(o),
std::forward<BinaryOp>(binary_op), init);
}
// Distributed iterator versions
template <dr::distributed_iterator Iter, dr::distributed_iterator OutputIter>
OutputIter inclusive_scan(Iter first, Iter last, OutputIter d_first) {
return inclusive_scan(dr::shp::par_unseq, first, last, d_first);
}
template <dr::distributed_iterator Iter, dr::distributed_iterator OutputIter,
typename BinaryOp>
OutputIter inclusive_scan(Iter first, Iter last, OutputIter d_first,
BinaryOp &&binary_op) {
return inclusive_scan(dr::shp::par_unseq, first, last, d_first,
std::forward<BinaryOp>(binary_op));
}
template <dr::distributed_iterator Iter, dr::distributed_iterator OutputIter,
typename BinaryOp, typename T>
OutputIter inclusive_scan(Iter first, Iter last, OutputIter d_first,
BinaryOp &&binary_op, T init) {
return inclusive_scan(dr::shp::par_unseq, first, last, d_first,
std::forward<BinaryOp>(binary_op), init);
}
} // namespace dr::shp