-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathp3a_for_each.hpp
362 lines (332 loc) · 9.75 KB
/
p3a_for_each.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
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
#pragma once
#include <utility>
#include <iterator>
#include "p3a_execution.hpp"
#include "p3a_functions.hpp"
#include "p3a_grid3.hpp"
#include "p3a_simd.hpp"
#include "p3a_counting_iterator.hpp"
#include <Kokkos_Core.hpp>
namespace p3a {
namespace details {
template <class ExecutionSpace, class Integral, class Functor>
void kokkos_for_each(
p3a::counting_iterator<Integral> first,
p3a::counting_iterator<Integral> last,
Functor functor)
{
Kokkos::parallel_for("p3a::details::kokkos_for_each(1D)",
Kokkos::RangePolicy<
ExecutionSpace,
Kokkos::IndexType<Integral>>(*first, *last),
functor);
}
template <class OriginalIterator, class OriginalFunctor>
class kokkos_iterator_functor {
OriginalIterator m_first;
OriginalFunctor m_functor;
public:
using difference_type = typename std::iterator_traits<OriginalIterator>::difference_type;
kokkos_iterator_functor(
OriginalIterator first_arg,
OriginalFunctor functor_arg)
:m_first(first_arg)
,m_functor(functor_arg)
{}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE auto operator()(difference_type i) const
{
return m_functor(m_first[i]);
}
};
template <class ExecutionSpace, class Iterator, class Functor>
void kokkos_for_each(
Iterator first,
Iterator last,
Functor functor)
{
using difference_type = typename std::iterator_traits<Iterator>::difference_type;
difference_type const n = last - first;
kokkos_for_each<ExecutionSpace>(
p3a::counting_iterator<difference_type>(0),
p3a::counting_iterator<difference_type>(n),
kokkos_iterator_functor<Iterator, Functor>(first, functor));
}
template <class Integral, class OriginalFunctor>
class kokkos_3d_functor {
OriginalFunctor m_functor;
public:
kokkos_3d_functor(
OriginalFunctor functor_arg)
:m_functor(functor_arg)
{
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline
auto operator()(Integral i, Integral j, Integral k) const
{
return m_functor(p3a::vector3<Integral>(i, j, k));
}
};
template <class ExecutionSpace, class Integral, class Functor>
void kokkos_for_each(
p3a::counting_iterator3<Integral> first,
p3a::counting_iterator3<Integral> last,
Functor functor)
{
auto const limits = last.vector - first.vector;
if (limits.volume() == 0) return;
using kokkos_policy =
Kokkos::MDRangePolicy<
ExecutionSpace,
Kokkos::IndexType<Integral>,
Kokkos::Rank<3, Kokkos::Iterate::Left, Kokkos::Iterate::Left>>;
Kokkos::parallel_for("p3a::details::kokkos_for_each(3D)",
kokkos_policy(
{first.vector.x(), first.vector.y(), first.vector.z()},
{last.vector.x(), last.vector.y(), last.vector.z()}),
kokkos_3d_functor<Integral, Functor>(functor));
}
}
template <class ExecutionPolicy, class Iterator, class Functor>
void for_each(
ExecutionPolicy,
Iterator first,
Iterator last,
Functor functor)
{
details::kokkos_for_each<typename ExecutionPolicy::kokkos_execution_space>(
first, last, functor);
}
template <class ForwardIt, class UnaryFunction>
P3A_ALWAYS_INLINE inline constexpr
void for_each(
execution::sequenced_policy,
ForwardIt first,
ForwardIt const& last,
UnaryFunction const& f)
{
for (; first != last; ++first) {
f(*first);
}
}
template <class Functor, class Integral>
P3A_ALWAYS_INLINE inline constexpr void for_each(
execution::sequenced_policy,
counting_iterator3<Integral> const& first,
counting_iterator3<Integral> const& last,
Functor const& functor)
{
for (Integral k = first.vector.z(); k < last.vector.z(); ++k) {
for (Integral j = first.vector.y(); j < last.vector.y(); ++j) {
for (Integral i = first.vector.x(); i < last.vector.x(); ++i) {
functor(vector3<Integral>(i, j, k));
}
}
}
}
template <class Functor, class Integral>
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline constexpr
void for_each(
execution::hot_policy,
counting_iterator3<Integral> const& first,
counting_iterator3<Integral> const& last,
Functor const& functor)
{
for (Integral k = first.vector.z(); k < last.vector.z(); ++k) {
for (Integral j = first.vector.y(); j < last.vector.y(); ++j) {
for (Integral i = first.vector.x(); i < last.vector.x(); ++i) {
functor(vector3<Integral>(i, j, k));
}
}
}
}
template <class Functor>
P3A_ALWAYS_INLINE inline constexpr void for_each(
execution::sequenced_policy policy,
subgrid3 const& subgrid,
Functor const& functor)
{
for_each(policy,
counting_iterator3<int>{subgrid.lower()},
counting_iterator3<int>{subgrid.upper()},
functor);
}
template <class Functor>
P3A_ALWAYS_INLINE inline constexpr void for_each(
execution::sequenced_policy policy,
grid3 const& grid,
Functor const& functor)
{
for_each(policy,
counting_iterator3<int>{vector3<int>::zero()},
counting_iterator3<int>{grid.extents()},
functor);
}
template <class Functor>
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline constexpr
void for_each(
execution::hot_policy policy,
grid3 const& grid,
Functor const& functor)
{
for_each(policy,
counting_iterator3<int>{vector3<int>::zero()},
counting_iterator3<int>{grid.extents()},
functor);
}
template <class ExecutionPolicy, class Functor>
void for_each(
ExecutionPolicy policy,
subgrid3 subgrid,
Functor functor)
{
for_each(policy,
counting_iterator3<int>{subgrid.lower()},
counting_iterator3<int>{subgrid.upper()},
functor);
}
template <class Functor>
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline constexpr
void for_each(
execution::hot_policy policy,
subgrid3 subgrid,
Functor const& functor)
{
for_each(policy,
counting_iterator3<int>{subgrid.lower()},
counting_iterator3<int>{subgrid.upper()},
functor);
}
template <class ExecutionPolicy, class Functor>
void for_each(
ExecutionPolicy policy,
grid3 grid,
Functor functor)
{
for_each(policy,
counting_iterator3<int>{vector3<int>::zero()},
counting_iterator3<int>{grid.extents()},
functor);
}
namespace details {
template <
class T,
class SimdAbi,
class Integral,
class OriginalFunctor>
class simd_functor {
OriginalFunctor m_functor;
Integral m_first_i;
Integral m_last_i;
public:
simd_functor(
OriginalFunctor functor_arg,
Integral first_i_arg,
Integral last_i_arg)
:m_functor(functor_arg)
,m_first_i(first_i_arg)
,m_last_i(last_i_arg)
{
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline auto operator()(Integral i) const
{
using mask_type = p3a::simd_mask<T, SimdAbi>;
auto constexpr width = Integral(mask_type::size());
auto const real_i = i * width + m_first_i;
auto const lane_count = p3a::min(width, m_last_i - real_i);
auto mask = mask_type(true);
for (std::size_t i = std::size_t(lane_count); i < mask_type::size(); ++i) {
mask[i] = false;
}
return m_functor(real_i, mask);
}
};
template <
class T,
class SimdAbi,
class Integral,
class OriginalFunctor>
class simd_3d_functor {
OriginalFunctor m_functor;
Integral m_first_i;
Integral m_last_i;
public:
simd_3d_functor(
OriginalFunctor functor_arg,
Integral first_i_arg,
Integral last_i_arg)
:m_functor(functor_arg)
,m_first_i(first_i_arg)
,m_last_i(last_i_arg)
{
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline
auto operator()(vector3<Integral> const& p) const
{
using mask_type = simd_mask<T, SimdAbi>;
auto constexpr width = Integral(mask_type::size());
auto const real_i = p.x() * width + m_first_i;
auto const lane_count = p3a::min(width, m_last_i - real_i);
auto mask = mask_type(true);
for (std::size_t i = std::size_t(lane_count); i < mask_type::size(); ++i) {
mask[i] = false;
}
return m_functor(vector3<Integral>(real_i, p.y(), p.z()), mask);
}
};
}
template <class T, class ExecutionPolicy, class Integral, class Functor>
void simd_for_each(
ExecutionPolicy policy,
p3a::counting_iterator<Integral> first,
p3a::counting_iterator<Integral> last,
Functor functor)
{
using simd_abi_type = typename ExecutionPolicy::simd_abi_type;
Integral const extent = *last - *first;
Integral constexpr width = Integral(p3a::simd<T, simd_abi_type>::size());
Integral const quotient = extent / width;
for_each(policy,
counting_iterator<Integral>(0),
counting_iterator<Integral>(quotient + 1),
details::simd_functor<T, simd_abi_type, Integral, Functor>(functor, *first, *last));
}
template <class T, class ExecutionPolicy, class Integral, class Functor>
void simd_for_each(
ExecutionPolicy policy,
counting_iterator3<Integral> first,
counting_iterator3<Integral> last,
Functor functor)
{
auto const extents = last.vector - first.vector;
using simd_abi_type = typename ExecutionPolicy::simd_abi_type;
using new_functor = details::simd_3d_functor<T, simd_abi_type, Integral, Functor>;
Integral constexpr width = Integral(p3a::simd<T, simd_abi_type>::size());
Integral const quotient = extents.x() / width;
p3a::for_each(policy,
counting_iterator3<Integral>(Integral(0), first.vector.y(), first.vector.z()),
counting_iterator3<Integral>(Integral(quotient + 1), last.vector.y(), last.vector.z()),
new_functor(functor, first.vector.x(), last.vector.x()));
}
template <class T, class ExecutionPolicy, class Functor>
P3A_NEVER_INLINE void simd_for_each(
ExecutionPolicy policy,
subgrid3 subgrid,
Functor functor)
{
simd_for_each<T>(policy,
counting_iterator3<int>{subgrid.lower()},
counting_iterator3<int>{subgrid.upper()},
functor);
}
template <class T, class ExecutionPolicy, class Functor>
void simd_for_each(
ExecutionPolicy policy,
grid3 grid,
Functor functor)
{
simd_for_each<T>(policy,
counting_iterator3<int>{vector3<int>::zero()},
counting_iterator3<int>{grid.extents()},
functor);
}
}