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 Support,
class IndexSequence>
92class TransformReducerKokkosLambdaAdapter;
94template <
class Reducer,
class Functor,
class Support, std::size_t... Idx>
95class TransformReducerKokkosLambdaAdapter<Reducer, Functor, Support, std::index_sequence<Idx...>>
97 template <std::size_t I>
98 using index_type = DiscreteElementType;
107 TransformReducerKokkosLambdaAdapter(Reducer
const& r, Functor
const& f, Support
const& support)
114 template <std::size_t N =
sizeof...(Idx), std::enable_if_t<(N == 0),
bool> =
true>
115 KOKKOS_FUNCTION
void operator()(
116 [[maybe_unused]] index_type<0> unused_id,
117 typename Reducer::value_type& a)
const
119 a = reducer(a, functor(DiscreteElement
<>()));
122 template <std::size_t N =
sizeof...(Idx), std::enable_if_t<(N > 0),
bool> =
true>
123 KOKKOS_FUNCTION
void operator()(index_type<Idx>... ids,
typename Reducer::value_type& a)
const
125 a = reducer(a, functor(m_support(
typename Support::discrete_vector_type(ids...))));
130
131
132
133
134
135
136
137
138
139template <
class ExecSpace,
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
140T transform_reduce_kokkos(
141 std::string
const& label,
142 ExecSpace
const& execution_space,
143 Support
const& domain,
145 BinaryReductionOp
const& reduce,
146 UnaryTransformOp
const& transform)
noexcept
149 Kokkos::parallel_reduce(
151 ddc_to_kokkos_execution_policy(execution_space, domain),
152 TransformReducerKokkosLambdaAdapter<
156 std::make_index_sequence<Support::rank()>>(reduce, transform, domain),
157 ddc_to_kokkos_reducer_t<BinaryReductionOp>(result));
164
165
166
167
168
169
170
171
172
173template <
class ExecSpace,
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
175 std::string
const& label,
176 ExecSpace
const& execution_space,
177 Support
const& domain,
179 BinaryReductionOp&& reduce,
180 UnaryTransformOp&& transform)
noexcept
182 return detail::transform_reduce_kokkos(
187 std::forward<BinaryReductionOp>(reduce),
188 std::forward<UnaryTransformOp>(transform));
192
193
194
195
196
197
198
199
200template <
class ExecSpace,
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
202 ExecSpace
const& execution_space,
203 Support
const& domain,
205 BinaryReductionOp&& reduce,
206 UnaryTransformOp&& transform)
noexcept
208 return detail::transform_reduce_kokkos(
209 "ddc_parallel_transform_reduce_default",
213 std::forward<BinaryReductionOp>(reduce),
214 std::forward<UnaryTransformOp>(transform));
218
219
220
221
222
223
224
225
226template <
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
228 std::string
const& label,
229 Support
const& domain,
231 BinaryReductionOp&& reduce,
232 UnaryTransformOp&& transform)
noexcept
234 return parallel_transform_reduce(
236 Kokkos::DefaultExecutionSpace(),
239 std::forward<BinaryReductionOp>(reduce),
240 std::forward<UnaryTransformOp>(transform));
244
245
246
247
248
249
250
251template <
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
253 Support
const& domain,
255 BinaryReductionOp&& reduce,
256 UnaryTransformOp&& transform)
noexcept
258 return parallel_transform_reduce(
259 "ddc_parallel_transform_reduce_default",
260 Kokkos::DefaultExecutionSpace(),
263 std::forward<BinaryReductionOp>(reduce),
264 std::forward<UnaryTransformOp>(transform));
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteElement()=default
The top-level namespace of DDC.
T parallel_transform_reduce(Support 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, Support 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, Support const &domain, T neutral, BinaryReductionOp &&reduce, UnaryTransformOp &&transform) noexcept
A reduction over a nD domain using the Kokkos default execution space.
std::enable_if_t< Kokkos::is_execution_space_v< ExecSpace >, T > parallel_transform_reduce(ExecSpace const &execution_space, Support const &domain, T neutral, BinaryReductionOp &&reduce, UnaryTransformOp &&transform) noexcept
A reduction over a nD domain using a given Kokkos execution space.