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 void operator()([[maybe_unused]] index_type<
void> unused_id,
typename Reducer::value_type& a)
110 a = reducer(a, functor(DiscreteElement
<>()));
113 template <std::size_t N =
sizeof...(DDims), std::enable_if_t<(N == 0),
bool> =
true>
114 KOKKOS_FUNCTION
void operator()(
115 use_annotated_operator,
116 [[maybe_unused]] index_type<
void> unused_id,
117 typename Reducer::value_type& a)
const
120 a = reducer(a, functor(DiscreteElement
<>()));
123 template <std::size_t N =
sizeof...(DDims), std::enable_if_t<(N > 0),
bool> =
true>
124 void operator()(index_type<DDims>... ids,
typename Reducer::value_type& a)
const
126 a = reducer(a, functor(DiscreteElement<DDims...>(ids...)));
130 template <std::size_t N =
sizeof...(DDims), std::enable_if_t<(N > 0),
bool> =
true>
131 KOKKOS_FUNCTION
void operator()(
132 use_annotated_operator,
133 index_type<DDims>... ids,
134 typename Reducer::value_type& a)
const
136 a = reducer(a, functor(DiscreteElement<DDims...>(ids...)));
141
142
143
144
145
146
147
148
149
150template <
class ExecSpace,
class... DDims,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
151T transform_reduce_kokkos(
152 std::string
const& label,
153 ExecSpace
const& execution_space,
156 BinaryReductionOp
const& reduce,
157 UnaryTransformOp
const& transform)
noexcept
160 Kokkos::parallel_reduce(
162 ddc_to_kokkos_execution_policy(execution_space, domain),
163 TransformReducerKokkosLambdaAdapter<
166 DDims...>(reduce, transform),
167 ddc_to_kokkos_reducer_t<BinaryReductionOp>(result));
174
175
176
177
178
179
180
181
182
183template <
class ExecSpace,
class... DDims,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
185 std::string
const& label,
186 ExecSpace
const& execution_space,
189 BinaryReductionOp&& reduce,
190 UnaryTransformOp&& transform)
noexcept
192 return detail::transform_reduce_kokkos(
197 std::forward<BinaryReductionOp>(reduce),
198 std::forward<UnaryTransformOp>(transform));
202
203
204
205
206
207
208
209
210template <
class ExecSpace,
class... DDims,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
212 ExecSpace
const& execution_space,
215 BinaryReductionOp&& reduce,
216 UnaryTransformOp&& transform)
noexcept
218 return detail::transform_reduce_kokkos(
219 "ddc_parallel_transform_reduce_default",
223 std::forward<BinaryReductionOp>(reduce),
224 std::forward<UnaryTransformOp>(transform));
228
229
230
231
232
233
234
235
236template <
class... DDims,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
238 std::string
const& label,
241 BinaryReductionOp&& reduce,
242 UnaryTransformOp&& transform)
noexcept
244 return parallel_transform_reduce(
246 Kokkos::DefaultExecutionSpace(),
249 std::forward<BinaryReductionOp>(reduce),
250 std::forward<UnaryTransformOp>(transform));
254
255
256
257
258
259
260
261template <
class... DDims,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
265 BinaryReductionOp&& reduce,
266 UnaryTransformOp&& transform)
noexcept
268 return parallel_transform_reduce(
269 "ddc_parallel_transform_reduce_default",
270 Kokkos::DefaultExecutionSpace(),
273 std::forward<BinaryReductionOp>(reduce),
274 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.