OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
ParallelReduceTools.h
Go to the documentation of this file.
1#ifndef PARALLEL_REDUCE_TOOLS_H
2#define PARALLEL_REDUCE_TOOLS_H
3
4#include <utility> // for std::index_sequence
5#include <variant> // for std::variant
6
7namespace ParticleBinning {
8
9 // Possibility to manually choose between different reduction methods!
10 enum class HistoReductionMode {
11 Standard, // Default auto selection
12 ParallelReduce, // Force usage of parallel_reduce if binCount <= maxArrSize
13 TeamBased, // Force team-based/atomic reduction if gpu enabled
15 };
16
38 template <typename SizeType, typename IndexType, IndexType N>
40 SizeType the_array[N];
41
42 KOKKOS_INLINE_FUNCTION
44 for (IndexType i = 0; i < N; i++) {
45 the_array[i] = 0;
46 }
47 }
48 KOKKOS_INLINE_FUNCTION
50 for (IndexType i = 0; i < N; i++) {
51 the_array[i] = rhs.the_array[i];
52 }
53 }
54 KOKKOS_INLINE_FUNCTION
56 if (this != &rhs) {
57 for (IndexType i = 0; i < N; ++i) {
58 the_array[i] = rhs.the_array[i];
59 }
60 }
61 return *this;
62 }
63 KOKKOS_INLINE_FUNCTION
65 for (IndexType i = 0; i < N; i++) {
66 the_array[i] += src.the_array[i];
67 }
68 return *this;
69 }
70 };
71
72 /*
73 Define logic for maxArrSize different reducer array types where N \in [1, ..., maxArrSize]
74 */
75
88 template <typename IndexType>
89 constexpr IndexType maxArrSize = 5;
90
105 template <typename SizeType, typename IndexType, typename Sequence>
107
120 template <typename SizeType, typename IndexType, IndexType... Sizes>
121 struct ReductionVariantHelper<SizeType, IndexType, std::integer_sequence<IndexType, Sizes...>> {
122 using type = std::variant<ArrayReduction<SizeType, IndexType, Sizes + 1>...>;
123 };
124
136 template <typename SizeType, typename IndexType>
138 SizeType, IndexType,
139 std::make_integer_sequence<IndexType, maxArrSize<IndexType>>>::type;
140
163 template <typename SizeType, typename IndexType, IndexType N>
165 if constexpr (N > maxArrSize<IndexType>) {
166 throw std::out_of_range("binCount is out of the allowed range");
167 } else if (binCount == N) {
169 } else {
170 return createReductionObjectHelper<SizeType, IndexType, N + 1>(binCount);
171 }
172 }
173
202 template <typename SizeType, typename IndexType>
204 return createReductionObjectHelper<SizeType, IndexType, 1>(binCount);
205 }
206
236 template <typename SizeType, typename IndexType>
241 SizeType* the_array;
242
251 static IndexType binCountStatic;
252
253#ifndef OPALX_DEVICE_COMPILATION
258 the_array = new SizeType[binCountStatic];
259 for (IndexType i = 0; i < binCountStatic; i++) {
260 the_array[i] = 0;
261 }
262 }
263
270 the_array = new SizeType[binCountStatic];
271 for (IndexType i = 0; i < binCountStatic; i++) {
272 the_array[i] = rhs.the_array[i];
273 }
274 }
275
280
288 the_array = new SizeType[binCountStatic];
289 if (this != &rhs) {
290 for (IndexType i = 0; i < binCountStatic; ++i) {
291 the_array[i] = rhs.the_array[i];
292 }
293 }
294 return *this;
295 }
296
305 for (IndexType i = 0; i < binCountStatic; i++) {
306 the_array[i] += src.the_array[i];
307 }
308 return *this;
309 }
310#else
318 KOKKOS_INLINE_FUNCTION
320 (void)src; // Silence unused parameter warning on Clang
321 Kokkos::abort(
322 "Error: HostArrayReduction is not supported on device backends "
323 "(CUDA/HIP/SYCL)!\n Note: It exists only for compilation compatibility.");
324 return *this;
325 }
326#endif
327 };
328
345 template <typename SizeType, typename IndexType>
347
356 template <typename T, unsigned Dim>
357 T vnorm(const VField_t<T, Dim>& field, int p = 2) {
358 T sum = 0;
359 ippl::parallel_reduce(
360 "VectorFieldNormReduce", field.getFieldRangePolicy(),
361 KOKKOS_LAMBDA(const ippl::RangePolicy<Dim>::index_array_type& idx, T& loc_sum) {
362 ippl::Vector<T, Dim> e = apply(field, idx);
363 loc_sum += std::pow(e.dot(e), p / 2.0);
364 },
365 Kokkos::Sum<T>(sum));
366 return std::pow(sum, 1.0 / p);
367 }
368
369} // namespace ParticleBinning
370
371namespace Kokkos {
384 template <typename SizeType, typename IndexType, IndexType N>
385 struct reduction_identity<ParticleBinning::ArrayReduction<SizeType, IndexType, N>> {
386 KOKKOS_FORCEINLINE_FUNCTION static ParticleBinning::ArrayReduction<SizeType, IndexType, N>
390 };
391
397 template <typename SizeType, typename IndexType>
398 struct reduction_identity<ParticleBinning::HostArrayReduction<SizeType, IndexType>> {
399 KOKKOS_FORCEINLINE_FUNCTION static ParticleBinning::HostArrayReduction<SizeType, IndexType>
403 };
404} // namespace Kokkos
405
406#endif // PARALLEL_REDUCE_TOOLS_H
double T
Definition OPALTypes.h:8
ippl::Field< ippl::Vector< T, Dim >, Dim, ViewArgs... > VField_t
Definition PBunchDefs.h:31
typename ReductionVariantHelper< SizeType, IndexType, std::make_integer_sequence< IndexType, maxArrSize< IndexType > > >::type ReductionVariant
Type alias for a std::variant containing all possible ArrayReduction types up to maxArrSize.
ReductionVariant< SizeType, IndexType > createReductionObject(IndexType binCount)
Factory function to create ArrayReduction objects with runtime-specified size.
ReductionVariant< SizeType, IndexType > createReductionObjectHelper(IndexType binCount)
Recursive helper function to create ArrayReduction objects with compile-time size matching.
constexpr IndexType maxArrSize
Maximum allowed array size for compile-time array reduction types.
T vnorm(const VField_t< T, Dim > &field, int p=2)
Computes the (p)-norm of a vector field (for debugging purpose).
Primary template for generating std::variant types containing ArrayReduction specializations.
STL namespace.
static KOKKOS_FORCEINLINE_FUNCTION ParticleBinning::ArrayReduction< SizeType, IndexType, N > sum()
static KOKKOS_FORCEINLINE_FUNCTION ParticleBinning::HostArrayReduction< SizeType, IndexType > sum()
A templated structure for performing array-based reductions in parallel computations.
KOKKOS_INLINE_FUNCTION ArrayReduction()
KOKKOS_INLINE_FUNCTION ArrayReduction & operator=(const ArrayReduction &rhs)
KOKKOS_INLINE_FUNCTION ArrayReduction & operator+=(const ArrayReduction &src)
KOKKOS_INLINE_FUNCTION ArrayReduction(const ArrayReduction &rhs)
Host-only array reduction structure for dynamic array sizes in Kokkos::parallel_reduce.
HostArrayReduction & operator=(const HostArrayReduction &rhs)
Assignment operator that performs a deep copy of the array from another instance.
~HostArrayReduction()
Destructor that deallocates the dynamically allocated array.
SizeType * the_array
Pointer to the dynamically allocated array for reduction operations.
HostArrayReduction(const HostArrayReduction &rhs)
Copy constructor that performs a deep copy of the array from another instance.
HostArrayReduction & operator+=(const HostArrayReduction &src)
Element-wise addition operator that adds the elements of another HostArrayReduction instance.
static IndexType binCountStatic
Static variable defining the array size for all instances of this template specialization.
HostArrayReduction()
Default constructor that allocates and zero-initializes the array.