1#ifndef PARALLEL_REDUCE_TOOLS_H
2#define PARALLEL_REDUCE_TOOLS_H
38 template <
typename SizeType,
typename IndexType, IndexType N>
42 KOKKOS_INLINE_FUNCTION
44 for (IndexType i = 0; i < N; i++) {
48 KOKKOS_INLINE_FUNCTION
50 for (IndexType i = 0; i < N; i++) {
54 KOKKOS_INLINE_FUNCTION
57 for (IndexType i = 0; i < N; ++i) {
63 KOKKOS_INLINE_FUNCTION
65 for (IndexType i = 0; i < N; i++) {
88 template <
typename IndexType>
105 template <
typename SizeType,
typename IndexType,
typename Sequence>
120 template <
typename SizeType,
typename IndexType, IndexType... Sizes>
122 using type = std::variant<ArrayReduction<SizeType, IndexType, Sizes + 1>...>;
136 template <
typename SizeType,
typename IndexType>
139 std::make_integer_sequence<IndexType, maxArrSize<IndexType>>>::type;
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) {
170 return createReductionObjectHelper<SizeType, IndexType, N + 1>(binCount);
202 template <
typename SizeType,
typename IndexType>
204 return createReductionObjectHelper<SizeType, IndexType, 1>(binCount);
236 template <
typename SizeType,
typename IndexType>
253#ifndef OPALX_DEVICE_COMPILATION
318 KOKKOS_INLINE_FUNCTION
322 "Error: HostArrayReduction is not supported on device backends "
323 "(CUDA/HIP/SYCL)!\n Note: It exists only for compilation compatibility.");
345 template <
typename SizeType,
typename IndexType>
356 template <
typename T,
unsigned Dim>
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);
365 Kokkos::Sum<T>(sum));
366 return std::pow(sum, 1.0 / p);
384 template <
typename SizeType,
typename IndexType, IndexType N>
397 template <
typename SizeType,
typename IndexType>
ippl::Field< ippl::Vector< T, Dim >, Dim, ViewArgs... > VField_t
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.
std::variant< ArrayReduction< SizeType, IndexType, Sizes+1 >... > type
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.
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.