12#include <Kokkos_Core.hpp>
14#include "ddc/ddc_to_kokkos_execution_policy.hpp"
15#include "ddc/detail/kokkos.hpp"
16#include "ddc/discrete_domain.hpp"
17#include "ddc/discrete_element.hpp"
18#include "ddc/reducer.hpp"
24template <
class Reducer>
25struct ddc_to_kokkos_reducer;
30 using type = Kokkos::Sum<T>;
36 using type = Kokkos::Prod<T>;
42 using type = Kokkos::LAnd<T>;
48 using type = Kokkos::LOr<T>;
54 using type = Kokkos::BAnd<T>;
60 using type = Kokkos::BOr<T>;
66 static_assert(std::is_same_v<T, T>,
"This reducer is not yet implemented");
72 using type = Kokkos::Min<T>;
78 using type = Kokkos::Max<T>;
84 using type = Kokkos::MinMax<T>;
88template <
class Reducer>
89using ddc_to_kokkos_reducer_t =
typename ddc_to_kokkos_reducer<Reducer>::type;
91template <
class Reducer,
class Functor,
class... DDims>
92class TransformReducerKokkosLambdaAdapter
95 using index_type = DiscreteElementType;
102 TransformReducerKokkosLambdaAdapter(Reducer
const& r, Functor
const& f) : reducer(r), functor(f)
106 template <std::size_t N =
sizeof...(DDims), std::enable_if_t<(N == 0),
bool> =
true>
107 KOKKOS_FUNCTION
void operator()(
108 [[maybe_unused]] index_type<
void> unused_id,
109 typename Reducer::value_type& a)
const
112 a = reducer(a, functor(DiscreteElement
<>()));
115 template <std::size_t N =
sizeof...(DDims), std::enable_if_t<(N > 0),
bool> =
true>
116 KOKKOS_FUNCTION
void operator()(index_type<DDims>... ids,
typename Reducer::value_type& a)
const
118 a = reducer(a, functor(DiscreteElement<DDims...>(ids...)));
123
124
125
126
127
128
129
130
131
132template <
class ExecSpace,
class... DDims,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
133T transform_reduce_kokkos(
134 std::string
const& label,
135 ExecSpace
const& execution_space,
138 BinaryReductionOp
const& reduce,
139 UnaryTransformOp
const& transform)
noexcept
142 Kokkos::parallel_reduce(
144 ddc_to_kokkos_execution_policy(execution_space, domain),
145 TransformReducerKokkosLambdaAdapter<
148 DDims...>(reduce, transform),
149 ddc_to_kokkos_reducer_t<BinaryReductionOp>(result));
156
157
158
159
160
161
162
163
164
165template <
class ExecSpace,
class... DDims,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
167 std::string
const& label,
168 ExecSpace
const& execution_space,
171 BinaryReductionOp&& reduce,
172 UnaryTransformOp&& transform)
noexcept
174 return detail::transform_reduce_kokkos(
179 std::forward<BinaryReductionOp>(reduce),
180 std::forward<UnaryTransformOp>(transform));
184
185
186
187
188
189
190
191
192template <
class ExecSpace,
class... DDims,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
194 ExecSpace
const& execution_space,
197 BinaryReductionOp&& reduce,
198 UnaryTransformOp&& transform)
noexcept
200 return detail::transform_reduce_kokkos(
201 "ddc_parallel_transform_reduce_default",
205 std::forward<BinaryReductionOp>(reduce),
206 std::forward<UnaryTransformOp>(transform));
210
211
212
213
214
215
216
217
218template <
class... DDims,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
220 std::string
const& label,
223 BinaryReductionOp&& reduce,
224 UnaryTransformOp&& transform)
noexcept
226 return parallel_transform_reduce(
228 Kokkos::DefaultExecutionSpace(),
231 std::forward<BinaryReductionOp>(reduce),
232 std::forward<UnaryTransformOp>(transform));
236
237
238
239
240
241
242
243template <
class... DDims,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
247 BinaryReductionOp&& reduce,
248 UnaryTransformOp&& transform)
noexcept
250 return parallel_transform_reduce(
251 "ddc_parallel_transform_reduce_default",
252 Kokkos::DefaultExecutionSpace(),
255 std::forward<BinaryReductionOp>(reduce),
256 std::forward<UnaryTransformOp>(transform));
friend class DiscreteDomain
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteElement()=default
The top-level namespace of DDC.
std::enable_if_t< Kokkos::is_execution_space_v< ExecSpace >, T > parallel_transform_reduce(ExecSpace const &execution_space, DiscreteDomain< DDims... > const &domain, T neutral, BinaryReductionOp &&reduce, UnaryTransformOp &&transform) noexcept
A reduction over a nD domain using a given Kokkos execution space.
T parallel_transform_reduce(std::string const &label, DiscreteDomain< DDims... > const &domain, T neutral, BinaryReductionOp &&reduce, UnaryTransformOp &&transform) noexcept
A reduction over a nD domain using the Kokkos default execution space.
T parallel_transform_reduce(std::string const &label, ExecSpace const &execution_space, DiscreteDomain< DDims... > const &domain, T neutral, BinaryReductionOp &&reduce, UnaryTransformOp &&transform) noexcept
A reduction over a nD domain using a given Kokkos execution space.
T parallel_transform_reduce(DiscreteDomain< DDims... > const &domain, T neutral, BinaryReductionOp &&reduce, UnaryTransformOp &&transform) noexcept
A reduction over a nD domain using the Kokkos default execution space.