12#include <Kokkos_Core.hpp>
14#include "detail/kokkos.hpp"
16#include "ddc_to_kokkos_execution_policy.hpp"
17#include "discrete_domain.hpp"
18#include "discrete_element.hpp"
25template <
class Reducer>
26struct ddc_to_kokkos_reducer;
31 using type = Kokkos::Sum<T>;
37 using type = Kokkos::Prod<T>;
43 using type = Kokkos::LAnd<T>;
49 using type = Kokkos::LOr<T>;
55 using type = Kokkos::BAnd<T>;
61 using type = Kokkos::BOr<T>;
67 static_assert(std::is_same_v<T, T>,
"This reducer is not yet implemented");
73 using type = Kokkos::Min<T>;
79 using type = Kokkos::Max<T>;
85 using type = Kokkos::MinMax<T>;
89template <
class Reducer>
90using ddc_to_kokkos_reducer_t =
typename ddc_to_kokkos_reducer<Reducer>::type;
92template <
class Reducer,
class Functor,
class Support,
class IndexSequence>
93class TransformReducerKokkosLambdaAdapter;
95template <
class Reducer,
class Functor,
class Support, std::size_t... Idx>
96class TransformReducerKokkosLambdaAdapter<Reducer, Functor, Support, std::index_sequence<Idx...>>
98 template <std::size_t I>
99 using index_type = DiscreteElementType;
108 TransformReducerKokkosLambdaAdapter(Reducer
const& r, Functor
const& f, Support
const& support)
115 template <std::size_t N =
sizeof...(Idx), std::enable_if_t<(N == 0),
bool> =
true>
116 KOKKOS_FUNCTION
void operator()(
117 [[maybe_unused]] index_type<0> unused_id,
118 typename Reducer::value_type& a)
const
120 a = reducer(a, functor(DiscreteElement
<>()));
123 template <std::size_t N =
sizeof...(Idx), std::enable_if_t<(N > 0),
bool> =
true>
124 KOKKOS_FUNCTION
void operator()(index_type<Idx>... ids,
typename Reducer::value_type& a)
const
126 a = reducer(a, functor(m_support(
typename Support::discrete_vector_type(ids...))));
131
132
133
134
135
136
137
138
139
140template <
class ExecSpace,
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
141T transform_reduce_kokkos(
142 std::string
const& label,
143 ExecSpace
const& execution_space,
144 Support
const& domain,
146 BinaryReductionOp
const& reduce,
147 UnaryTransformOp
const& transform)
noexcept
150 Kokkos::parallel_reduce(
152 ddc_to_kokkos_execution_policy(execution_space, domain),
153 TransformReducerKokkosLambdaAdapter<
157 std::make_index_sequence<Support::rank()>>(reduce, transform, domain),
158 ddc_to_kokkos_reducer_t<BinaryReductionOp>(result));
165
166
167
168
169
170
171
172
173
174template <
class ExecSpace,
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
176 std::string
const& label,
177 ExecSpace
const& execution_space,
178 Support
const& domain,
180 BinaryReductionOp&& reduce,
181 UnaryTransformOp&& transform)
noexcept
183 return detail::transform_reduce_kokkos(
188 std::forward<BinaryReductionOp>(reduce),
189 std::forward<UnaryTransformOp>(transform));
193
194
195
196
197
198
199
200
201template <
class ExecSpace,
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
203 ExecSpace
const& execution_space,
204 Support
const& domain,
206 BinaryReductionOp&& reduce,
207 UnaryTransformOp&& transform)
noexcept
209 return detail::transform_reduce_kokkos(
210 "ddc_parallel_transform_reduce_default",
214 std::forward<BinaryReductionOp>(reduce),
215 std::forward<UnaryTransformOp>(transform));
219
220
221
222
223
224
225
226
227template <
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
229 std::string
const& label,
230 Support
const& domain,
232 BinaryReductionOp&& reduce,
233 UnaryTransformOp&& transform)
noexcept
235 return parallel_transform_reduce(
237 Kokkos::DefaultExecutionSpace(),
240 std::forward<BinaryReductionOp>(reduce),
241 std::forward<UnaryTransformOp>(transform));
245
246
247
248
249
250
251
252template <
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
254 Support
const& domain,
256 BinaryReductionOp&& reduce,
257 UnaryTransformOp&& transform)
noexcept
259 return parallel_transform_reduce(
260 "ddc_parallel_transform_reduce_default",
261 Kokkos::DefaultExecutionSpace(),
264 std::forward<BinaryReductionOp>(reduce),
265 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.