-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathp3a_reduce.hpp
573 lines (539 loc) · 15.7 KB
/
p3a_reduce.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#pragma once
#include "mpicpp.hpp"
#include "p3a_execution.hpp"
#include "p3a_grid3.hpp"
#include "p3a_dynamic_array.hpp"
#include "p3a_counting_iterator.hpp"
#include "p3a_functional.hpp"
#include "p3a_simd.hpp"
namespace p3a {
namespace details {
class int128 {
std::int64_t m_high;
std::uint64_t m_low;
public:
P3A_ALWAYS_INLINE inline int128() = default;
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
int128(std::int64_t high_arg, std::uint64_t low_arg)
:m_high(high_arg)
,m_low(low_arg)
{}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
int128(std::int64_t value)
:int128(
std::int64_t(-1) * (value < 0),
std::uint64_t(value))
{}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
std::int64_t high() const { return m_high; }
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
std::uint64_t low() const { return m_low; }
};
template <class T, class BinaryReductionOp>
class kokkos_reducer {
public:
using reducer = kokkos_reducer<T, BinaryReductionOp>;
using value_type = T;
using result_view_type = Kokkos::View<value_type, Kokkos::HostSpace>;
private:
value_type m_init;
BinaryReductionOp m_binary_op;
result_view_type m_result_view;
public:
kokkos_reducer(
value_type init_arg,
BinaryReductionOp binary_op_arg,
value_type& result_arg)
:m_init(init_arg)
,m_binary_op(binary_op_arg)
,m_result_view(&result_arg)
{
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline
void join(value_type& dest, value_type const& src) const
{
dest = m_binary_op(dest, src);
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline
void init(value_type& val) const
{
val = m_init;
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline
value_type& reference() const
{
return *m_result_view.data();
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline
result_view_type view() const
{
return m_result_view;
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline constexpr
bool references_scalar() const
{
return true;
}
};
template <class T, class BinaryReductionOp, class UnaryTransformOp, class Integral>
class kokkos_reduce_functor {
BinaryReductionOp m_binary_op;
UnaryTransformOp m_unary_op;
public:
kokkos_reduce_functor(
BinaryReductionOp binary_op_arg,
UnaryTransformOp unary_op_arg)
:m_binary_op(binary_op_arg)
,m_unary_op(unary_op_arg)
{
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline void operator()(Integral i, T& updated) const
{
updated = m_binary_op(updated, m_unary_op(i));
}
};
template <
class ExecutionSpace,
class Integral,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
[[nodiscard]]
std::enable_if_t<std::is_integral_v<Integral>, T>
kokkos_transform_reduce(
p3a::counting_iterator<Integral> first,
p3a::counting_iterator<Integral> last,
T init,
BinaryReductionOp binary_op,
UnaryTransformOp unary_op)
{
using functor = kokkos_reduce_functor<
T, BinaryReductionOp, UnaryTransformOp, Integral>;
using reducer_type = kokkos_reducer<T, BinaryReductionOp>;
using kokkos_policy_type =
Kokkos::RangePolicy<
ExecutionSpace,
Kokkos::IndexType<Integral>>;
T result = init;
reducer_type reducer(init, binary_op, result);
Kokkos::parallel_reduce(
"p3a::details::kokkos_transform_reduce(1D)",
kokkos_policy_type(*first, *last),
functor(binary_op, unary_op),
reducer);
return result;
}
template <
class ExecutionSpace,
class Iterator,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
[[nodiscard]]
T kokkos_transform_reduce(
Iterator first, Iterator last,
T init,
BinaryReductionOp binary_op,
UnaryTransformOp unary_op)
{
using difference_type = typename std::iterator_traits<Iterator>::difference_type;
difference_type const n = last - first;
using new_transform_type = kokkos_iterator_functor<Iterator, UnaryTransformOp>;
return kokkos_transform_reduce(
p3a::counting_iterator<difference_type>(0),
p3a::counting_iterator<difference_type>(n),
init,
binary_op,
new_transform_type(first, unary_op));
}
template <class T, class BinaryReductionOp, class UnaryTransformOp, class Integral>
class kokkos_3d_reduce_functor {
BinaryReductionOp m_binary_op;
UnaryTransformOp m_unary_op;
public:
kokkos_3d_reduce_functor(
BinaryReductionOp binary_op_arg,
UnaryTransformOp unary_op_arg)
:m_binary_op(binary_op_arg)
,m_unary_op(unary_op_arg)
{
}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline
void operator()(Integral i, Integral j, Integral k, T& updated) const
{
updated = m_binary_op(updated, m_unary_op(i, j, k));
}
};
template <
class ExecutionSpace,
class Integral,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
[[nodiscard]]
std::enable_if_t<std::is_integral_v<Integral>, T>
kokkos_transform_reduce(
p3a::counting_iterator3<Integral> first,
p3a::counting_iterator3<Integral> last,
T init,
BinaryReductionOp binary_op,
UnaryTransformOp unary_op)
{
using new_transform = kokkos_3d_functor<Integral, UnaryTransformOp>;
using functor_type = kokkos_3d_reduce_functor<
T, BinaryReductionOp, new_transform, Integral>;
using reducer = kokkos_reducer<T, BinaryReductionOp>;
using kokkos_policy =
Kokkos::MDRangePolicy<
ExecutionSpace,
Kokkos::IndexType<Integral>,
Kokkos::Rank<3, Kokkos::Iterate::Left, Kokkos::Iterate::Left>>;
T result = init;
Kokkos::parallel_reduce(
"p3a::details::kokkos_transform_reduce(3D)",
kokkos_policy(
{first.vector.x(), first.vector.y(), first.vector.z()},
{last.vector.x(), last.vector.y(), last.vector.z()}),
functor_type(binary_op, new_transform(unary_op)),
reducer(init, binary_op, result));
return result;
}
template <class T, class BinaryReductionOp, class UnaryTransformOp>
class simd_reduce_wrapper {
T m_init;
BinaryReductionOp m_binary_op;
UnaryTransformOp m_unary_op;
public:
simd_reduce_wrapper(
T init_arg,
BinaryReductionOp binary_op_arg,
UnaryTransformOp unary_op_arg)
:m_init(init_arg)
,m_binary_op(binary_op_arg)
,m_unary_op(unary_op_arg)
{
}
template <class Indices, class Abi>
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline
auto operator()(Indices const& indices, p3a::simd_mask<T, Abi> const& mask) const
{
auto const simd_result = m_unary_op(indices, mask);
return p3a::reduce(where(mask, simd_result), m_init, m_binary_op);
}
};
template <
class SimdAbi,
class ExecutionSpace,
class Integral,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
[[nodiscard]]
std::enable_if_t<std::is_integral_v<Integral>, T>
kokkos_simd_transform_reduce(
p3a::counting_iterator<Integral> first,
p3a::counting_iterator<Integral> last,
T init,
BinaryReductionOp binary_op,
UnaryTransformOp unary_op)
{
Integral const extent = *last - *first;
if (extent == 0) return init;
using transform_a = simd_reduce_wrapper<T, BinaryReductionOp, UnaryTransformOp>;
using transform_b = simd_functor<T, SimdAbi, Integral, transform_a>;
using functor = kokkos_reduce_functor<
T, BinaryReductionOp, transform_b, Integral>;
using reducer = kokkos_reducer<T, BinaryReductionOp>;
using kokkos_policy =
Kokkos::RangePolicy<
ExecutionSpace,
Kokkos::IndexType<Integral>>;
T result = init;
Integral constexpr width = Integral(p3a::simd_mask<T, SimdAbi>::size());
Integral const quotient = extent / width;
Kokkos::parallel_reduce(
"p3a::details::kokkos_simd_transform_reduce(1D)",
kokkos_policy(0, quotient + 1),
functor(binary_op,
transform_b(
transform_a(init, binary_op, unary_op),
*first,
*last)),
reducer(init, binary_op, result));
return result;
}
template <
class SimdAbi,
class ExecutionSpace,
class Integral,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
[[nodiscard]]
std::enable_if_t<std::is_integral_v<Integral>, T>
kokkos_simd_transform_reduce(
p3a::counting_iterator3<Integral> first,
p3a::counting_iterator3<Integral> last,
T init,
BinaryReductionOp binary_op,
UnaryTransformOp unary_op)
{
auto const extents = last.vector - first.vector;
if (extents.volume() == 0) return init;
using transform_a = simd_reduce_wrapper<T, BinaryReductionOp, UnaryTransformOp>;
using transform_b = simd_3d_functor<T, SimdAbi, Integral, transform_a>;
using transform_c = kokkos_3d_functor<Integral, transform_b>;
using functor = kokkos_3d_reduce_functor<
T, BinaryReductionOp, transform_c, Integral>;
using reducer = kokkos_reducer<T, BinaryReductionOp>;
using kokkos_policy =
Kokkos::MDRangePolicy<
ExecutionSpace,
Kokkos::IndexType<Integral>,
Kokkos::Rank<3, Kokkos::Iterate::Left, Kokkos::Iterate::Left>>;
T result = init;
Integral constexpr width = Integral(p3a::simd_mask<T, SimdAbi>::size());
Integral const quotient = extents.x() / width;
Kokkos::parallel_reduce(
"p3a::details::kokkos_simd_transform_reduce(3D)",
kokkos_policy(
{Integral(0), first.vector.y(), first.vector.z()},
{Integral(quotient + 1), last.vector.y(), last.vector.z()}),
functor(binary_op,
transform_c(
transform_b(
transform_a(init, binary_op, unary_op),
first.vector.x(),
last.vector.x()))),
reducer(init, binary_op, result));
return result;
}
}
template <
class ExecutionPolicy,
class Iterator,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
[[nodiscard]] T transform_reduce(
ExecutionPolicy policy,
Iterator first, Iterator last,
T init,
BinaryReductionOp binary_op,
UnaryTransformOp unary_op)
{
return details::kokkos_transform_reduce<typename ExecutionPolicy::kokkos_execution_space>(
first, last, init, binary_op, unary_op);
}
template <
class Iterator,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
[[nodiscard]] T transform_reduce(
execution::sequenced_policy policy,
Iterator first, Iterator last,
T init,
BinaryReductionOp binary_op,
UnaryTransformOp unary_op)
{
for_each(policy, first, last,
[&] (typename std::iterator_traits<Iterator>::reference r) {
init = binary_op(init, unary_op(r));
});
return init;
}
template <
class ExecutionPolicy,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
[[nodiscard]] T transform_reduce(
ExecutionPolicy policy,
subgrid3 subgrid,
T init,
BinaryReductionOp binary_op,
UnaryTransformOp unary_op)
{
return transform_reduce(
policy,
counting_iterator3<int>{subgrid.lower()},
counting_iterator3<int>{subgrid.upper()},
init,
binary_op,
unary_op);
}
template <
class T,
class BinaryReductionOp,
class UnaryTransformOp>
[[nodiscard]] T transform_reduce(
execution::sequenced_policy policy,
subgrid3 subgrid,
T init,
BinaryReductionOp binary_op,
UnaryTransformOp unary_op)
{
for_each(policy,
counting_iterator3<int>{subgrid.lower()},
counting_iterator3<int>{subgrid.upper()},
[&] (p3a::vector3<int> const& v) {
init = binary_op(init, unary_op(v));
});
return init;
}
template <
class ExecutionPolicy,
class Iterator,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
[[nodiscard]] T simd_transform_reduce(
ExecutionPolicy policy,
Iterator first, Iterator last,
T init,
BinaryReductionOp binary_op,
UnaryTransformOp unary_op)
{
return details::kokkos_simd_transform_reduce<
typename ExecutionPolicy::simd_abi_type,
typename ExecutionPolicy::kokkos_execution_space>(
first, last, init, binary_op, unary_op);
}
template <
class ExecutionPolicy,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
[[nodiscard]] T simd_transform_reduce(
ExecutionPolicy policy,
subgrid3 subgrid,
T init,
BinaryReductionOp binary_op,
UnaryTransformOp unary_op)
{
return simd_transform_reduce(
policy,
counting_iterator3<int>{subgrid.lower()},
counting_iterator3<int>{subgrid.upper()},
init,
binary_op,
unary_op);
}
namespace details {
template <
class Allocator,
class ExecutionPolicy>
class fixed_point_double_sum {
public:
using values_type = dynamic_array<double, Allocator, ExecutionPolicy>;
private:
mpicpp::comm m_comm;
values_type m_values;
public:
fixed_point_double_sum() = default;
explicit fixed_point_double_sum(mpicpp::comm&& comm_arg)
:m_comm(std::move(comm_arg))
{}
fixed_point_double_sum(fixed_point_double_sum&&) = default;
fixed_point_double_sum& operator=(fixed_point_double_sum&&) = default;
fixed_point_double_sum(fixed_point_double_sum const&) = delete;
fixed_point_double_sum& operator=(fixed_point_double_sum const&) = delete;
public:
[[nodiscard]] P3A_NEVER_INLINE
double compute();
[[nodiscard]] P3A_ALWAYS_INLINE inline constexpr
values_type& values() { return m_values; }
};
extern template class fixed_point_double_sum<device_allocator<double>, execution::parallel_policy>;
}
template <class T, class Allocator, class ExecutionPolicy>
class associative_sum;
namespace details {
template <class Iterator, class SizeType, class UnaryOp>
class associative_sum_iterator_functor {
Iterator first;
double* values;
UnaryOp unary_op;
public:
associative_sum_iterator_functor(
Iterator first_arg,
double* values_arg,
UnaryOp unary_op_arg)
:first(first_arg)
,values(values_arg)
,unary_op(unary_op_arg)
{}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE void operator()(SizeType i) const {
values[i] = unary_op(first[i]);
}
};
template <class UnaryOp>
class associative_sum_subgrid_functor {
subgrid3 grid;
double* values;
UnaryOp unary_op;
public:
associative_sum_subgrid_functor(
subgrid3 grid_arg,
double* values_arg,
UnaryOp unary_op_arg)
:grid(grid_arg)
,values(values_arg)
,unary_op(unary_op_arg)
{}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE void operator()(vector3<int> const& grid_point) const {
int const index = grid.index(grid_point);
values[index] = unary_op(grid_point);
}
};
}
template <
class Allocator,
class ExecutionPolicy>
class associative_sum<double, Allocator, ExecutionPolicy> {
details::fixed_point_double_sum<Allocator, ExecutionPolicy> m_fixed_point;
public:
associative_sum() = default;
explicit associative_sum(mpicpp::comm&& comm_arg)
:m_fixed_point(std::move(comm_arg))
{}
associative_sum(associative_sum&&) = default;
associative_sum& operator=(associative_sum&&) = default;
associative_sum(associative_sum const&) = delete;
associative_sum& operator=(associative_sum const&) = delete;
template <class Iterator, class UnaryOp>
[[nodiscard]]
double transform_reduce(
Iterator first,
Iterator last,
UnaryOp unary_op)
{
auto const n = (last - first);
m_fixed_point.values().resize(n);
auto const policy = m_fixed_point.values().get_execution_policy();
auto const values = m_fixed_point.values().begin();
using size_type = std::remove_const_t<decltype(n)>;
for_each(policy,
counting_iterator<size_type>(0),
counting_iterator<size_type>(n),
details::associative_sum_iterator_functor<Iterator, size_type, UnaryOp>(first, values, unary_op));
return m_fixed_point.compute();
}
template <class UnaryOp>
[[nodiscard]]
double transform_reduce(
subgrid3 grid,
UnaryOp unary_op)
{
m_fixed_point.values().resize(grid.size());
auto const policy = m_fixed_point.values().get_execution_policy();
auto const values = m_fixed_point.values().begin();
for_each(policy, grid, details::associative_sum_subgrid_functor<UnaryOp>(grid, values, unary_op));
return m_fixed_point.compute();
}
};
template <class T>
using device_associative_sum =
associative_sum<T, device_allocator<T>, execution::parallel_policy>;
}