OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
ParallelReduceTools.h
Go to the documentation of this file.
1#ifndef PARALLEL_REDUCE_TOOLS_H
2#define PARALLEL_REDUCE_TOOLS_H
3
4#include <variant> // for std::variant
5// #include <memory>
6#include <utility> // for std::index_sequence
7
8namespace ParticleBinning {
9
10 // Possibility to manually choose between different reduction methods!
11 enum class HistoReductionMode {
12 Standard, // Default auto selection
13 ParallelReduce, // Force usage of parallel_reduce if binCount <= maxArrSize
14 TeamBased, // Force team-based/atomic reduction if gpu enabled
16 };
17
18
37 template<typename SizeType, typename IndexType, IndexType N>
39 SizeType the_array[N];
40
41 KOKKOS_INLINE_FUNCTION
43 for (IndexType i = 0; i < N; i++ ) { the_array[i] = 0; }
44 }
45 KOKKOS_INLINE_FUNCTION
47 for (IndexType i = 0; i < N; i++ ){ the_array[i] = rhs.the_array[i]; }
48 }
49 KOKKOS_INLINE_FUNCTION
51 if (this != &rhs) {
52 for (IndexType i = 0; i < N; ++i) { the_array[i] = rhs.the_array[i]; }
53 }
54 return *this;
55 }
56 KOKKOS_INLINE_FUNCTION
58 for (IndexType i = 0; i < N; i++ ) { the_array[i] += src.the_array[i]; }
59 return *this;
60 }
61 };
62
63
64 /*
65 Define logic for maxArrSize different reducer array types where N \in [1, ..., maxArrSize]
66 */
67
80 template<typename IndexType>
81 constexpr IndexType maxArrSize = 5;
82
96 template<typename SizeType, typename IndexType, typename Sequence>
98
110 template<typename SizeType, typename IndexType, IndexType... Sizes>
111 struct ReductionVariantHelper<SizeType, IndexType, std::integer_sequence<IndexType, Sizes...>> {
112 using type = std::variant<ArrayReduction<SizeType, IndexType, Sizes + 1>...>;
113 };
114
125 template<typename SizeType, typename IndexType>
127
147 template<typename SizeType, typename IndexType, IndexType N>
149 if constexpr (N > maxArrSize<IndexType>) {
150 throw std::out_of_range("binCount is out of the allowed range");
151 } else if (binCount == N) {
153 } else {
155 }
156 }
157
185 template<typename SizeType, typename IndexType>
189
190
216 template<typename SizeType, typename IndexType>
218
222 SizeType* the_array;
223
230 static IndexType binCountStatic;
231
232#ifndef KOKKOS_ENABLE_CUDA
237 the_array = new SizeType[binCountStatic];
238 for (IndexType i = 0; i < binCountStatic; i++ ) { the_array[i] = 0; }
239 }
240
247 the_array = new SizeType[binCountStatic];
248 for (IndexType i = 0; i < binCountStatic; i++ ){ the_array[i] = rhs.the_array[i]; }
249 }
250
255
263 the_array = new SizeType[binCountStatic];
264 if (this != &rhs) {
265 for (IndexType i = 0; i < binCountStatic; ++i) { the_array[i] = rhs.the_array[i]; }
266 }
267 return *this;
268 }
269
277 for (IndexType i = 0; i < binCountStatic; i++ ) { the_array[i] += src.the_array[i]; }
278 return *this;
279 }
280#else
287 KOKKOS_INLINE_FUNCTION
289 Kokkos::abort("Error: HostArrayReduction is not supported on CUDA!\n Note: It exists only for compilation compatibility.");
290 return *this;
291 }
292#endif
293 };
294
309 template<typename SizeType, typename IndexType>
311
312
321 template<typename T, unsigned Dim>
322 T vnorm(const VField_t<T, Dim>& field, int p = 2) {
323 T sum = 0;
324 ippl::parallel_reduce("VectorFieldNormReduce", field.getFieldRangePolicy(),
325 KOKKOS_LAMBDA(const ippl::RangePolicy<Dim>::index_array_type& idx, T& loc_sum) {
326 ippl::Vector<T, Dim> e = apply(field, idx);
327 loc_sum += std::pow(e.dot(e), p/2.0);
328 }, Kokkos::Sum<T>(sum));
329 return std::pow(sum, 1.0/p);
330 }
331
332}
333
334namespace Kokkos {
347 template<typename SizeType, typename IndexType, IndexType N>
348 struct reduction_identity<ParticleBinning::ArrayReduction<SizeType, IndexType, N>> {
352 };
353
358 template<typename SizeType, typename IndexType>
359 struct reduction_identity<ParticleBinning::HostArrayReduction<SizeType, IndexType>> {
363 };
364}
365
366#endif // PARALLEL_REDUCE_TOOLS_H
ippl::Field< ippl::Vector< T, Dim >, Dim, ViewArgs... > VField_t
Definition PBunchDefs.h:33
double T
Definition datatypes.h:7
STL namespace.
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.
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.
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.
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.
static KOKKOS_FORCEINLINE_FUNCTION ParticleBinning::ArrayReduction< SizeType, IndexType, N > sum()
static KOKKOS_FORCEINLINE_FUNCTION ParticleBinning::HostArrayReduction< SizeType, IndexType > sum()