-
Notifications
You must be signed in to change notification settings - Fork 182
/
bulk_schedule.hpp
254 lines (217 loc) · 7.98 KB
/
bulk_schedule.hpp
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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <algorithm>
#include <unifex/bind_back.hpp>
#include <unifex/execution_policy.hpp>
#include <unifex/get_execution_policy.hpp>
#include <unifex/get_stop_token.hpp>
#include <unifex/receiver_concepts.hpp>
#include <unifex/scheduler_concepts.hpp>
#include <unifex/sender_concepts.hpp>
#include <unifex/tag_invoke.hpp>
#include <unifex/detail/prologue.hpp>
namespace unifex {
// Size of chunk used for cancellation allowing for some vectorisation
constexpr size_t bulk_cancellation_chunk_size = 16;
namespace _bulk_schedule {
template <typename Integral, typename Receiver>
struct _schedule_receiver {
class type;
};
template <typename Integral, typename Receiver>
using schedule_receiver = typename _schedule_receiver<Integral, Receiver>::type;
template <typename Integral, typename Receiver>
class _schedule_receiver<Integral, Receiver>::type {
public:
template <typename Receiver2>
explicit type(Integral count, Receiver2&& r)
: count_(std::move(count))
, receiver_((Receiver2 &&) r) {}
void
set_value() noexcept(is_nothrow_receiver_of_v<Receiver>&&
is_nothrow_next_receiver_v<Receiver, Integral>) {
using policy_t = decltype(get_execution_policy(receiver_));
auto stop_token = get_stop_token(receiver_);
const bool stop_possible =
!is_stop_never_possible_v<decltype(stop_token)> &&
stop_token.stop_possible();
if (stop_possible) {
for (Integral chunk_start(0); chunk_start < count_;
chunk_start += bulk_cancellation_chunk_size) {
if (stop_token.stop_requested()) {
unifex::set_done(std::move(receiver_));
return;
}
Integral chunk_end = std::min(
chunk_start + static_cast<Integral>(bulk_cancellation_chunk_size),
count_);
if constexpr (is_one_of_v<
policy_t,
unsequenced_policy,
parallel_unsequenced_policy>) {
UNIFEX_DIAGNOSTIC_PUSH
// Vectorisable version
#if defined(__clang__)
// When optimizing for size (e.g. with -Oz), Clang will not
// vectorize this loop, and will emit a warning. There's
// nothing to be done about the warning, though, so just
// suppress it.
# pragma clang diagnostic ignored "-Wpass-failed"
# pragma clang loop vectorize(enable) interleave(enable)
#elif defined(__GNUC__)
# pragma GCC ivdep
#elif defined(_MSC_VER)
# pragma loop(ivdep)
#endif
for (Integral i(chunk_start); i < chunk_end; ++i) {
unifex::set_next(receiver_, Integral(i));
}
UNIFEX_DIAGNOSTIC_POP
} else {
// Sequenced version
for (Integral i(chunk_start); i < chunk_end; ++i) {
unifex::set_next(receiver_, Integral(i));
}
}
}
} else {
if constexpr (is_one_of_v<
policy_t,
unsequenced_policy,
parallel_unsequenced_policy>) {
UNIFEX_DIAGNOSTIC_PUSH
// Vectorisable version
#if defined(__clang__)
// When optimizing for size (e.g. with -Oz), Clang will not
// vectorize this loop, and will emit a warning. There's
// nothing to be done about the warning, though, so just
// suppress it.
# pragma clang diagnostic ignored "-Wpass-failed"
# pragma clang loop vectorize(enable) interleave(enable)
#elif defined(__GNUC__)
# pragma GCC ivdep
#elif defined(_MSC_VER)
# pragma loop(ivdep)
#endif
for (Integral i(0); i < count_; ++i) {
unifex::set_next(receiver_, Integral(i));
}
UNIFEX_DIAGNOSTIC_POP
} else {
// Sequenced version
for (Integral i(0); i < count_; ++i) {
unifex::set_next(receiver_, Integral(i));
}
}
}
unifex::set_value(std::move(receiver_));
}
template(typename Error) //
(requires receiver<Receiver, Error>) //
void set_error(Error&& e) noexcept {
unifex::set_error(std::move(receiver_), (Error &&) e);
}
void set_done() noexcept { unifex::set_done(std::move(receiver_)); }
private:
Integral count_;
Receiver receiver_;
};
template <typename Scheduler, typename Integral>
struct _default_sender {
class type;
};
template <typename Scheduler, typename Integral>
using default_sender = typename _default_sender<Scheduler, Integral>::type;
template <typename Scheduler, typename Integral>
class _default_sender<Scheduler, Integral>::type {
using schedule_sender_t =
decltype(unifex::schedule(UNIFEX_DECLVAL(const Scheduler&)));
public:
template <
template <typename...>
class Variant,
template <typename...>
class Tuple>
using value_types = Variant<Tuple<>>;
template <
template <typename...>
class Variant,
template <typename...>
class Tuple>
using next_types = Variant<Tuple<Integral>>;
template <template <typename...> class Variant>
using error_types = sender_error_types_t<schedule_sender_t, Variant>;
static constexpr bool sends_done = true;
static constexpr blocking_kind blocking =
sender_traits<schedule_sender_t>::blocking;
static constexpr bool is_always_scheduler_affine =
sender_traits<schedule_sender_t>::is_always_scheduler_affine;
template <typename Scheduler2>
explicit type(Scheduler2&& s, Integral count)
: scheduler_(static_cast<Scheduler2&&>(s))
, count_(std::move(count)) {}
template(typename Self, typename BulkReceiver) //
(requires same_as<remove_cvref_t<Self>, type> AND
receiver_of<BulkReceiver> AND is_next_receiver_v<
BulkReceiver,
Integral>) //
friend auto tag_invoke(
tag_t<unifex::connect>, Self&& s, BulkReceiver&& r) {
return unifex::connect(
unifex::schedule(static_cast<Self&&>(s).scheduler_),
schedule_receiver<Integral, remove_cvref_t<BulkReceiver>>{
static_cast<Self&&>(s).count_, static_cast<BulkReceiver&&>(r)});
}
friend blocking_kind tag_invoke(tag_t<blocking>, const type& self) noexcept {
return unifex::blocking(self.scheduler_);
}
private:
Scheduler scheduler_;
Integral count_;
};
struct _fn {
template(typename Scheduler, typename Integral) //
(requires tag_invocable<_fn, Scheduler, Integral>) //
auto
operator()(Scheduler&& s, Integral n) const
noexcept(is_nothrow_tag_invocable_v<_fn, Scheduler, Integral>)
-> tag_invoke_result_t<_fn, Scheduler, Integral> {
return tag_invoke(_fn{}, (Scheduler &&) s, std::move(n));
}
template(typename Scheduler, typename Integral) //
(requires scheduler<Scheduler> AND(
!tag_invocable<_fn, Scheduler, Integral>)) //
auto
operator()(Scheduler&& s, Integral n) const
noexcept(std::is_nothrow_constructible_v<
remove_cvref_t<Scheduler>,
Scheduler>&& std::is_nothrow_move_constructible_v<Integral>)
-> default_sender<remove_cvref_t<Scheduler>, Integral> {
return default_sender<remove_cvref_t<Scheduler>, Integral>{
(Scheduler &&) s, std::move(n)};
}
template <typename Integral>
constexpr auto operator()(Integral n) const
noexcept(std::is_nothrow_invocable_v<tag_t<bind_back>, _fn, Integral>)
-> bind_back_result_t<_fn, Integral> {
return bind_back(*this, n);
}
};
} // namespace _bulk_schedule
inline constexpr _bulk_schedule::_fn bulk_schedule{};
} // namespace unifex
#include <unifex/detail/epilogue.hpp>