12#include <Kokkos_Core.hpp>
14#include "ddc_to_kokkos_execution_policy.hpp"
15#include "discrete_element.hpp"
22template <
class Reducer>
23struct ddc_to_kokkos_reducer;
28 using type = Kokkos::Sum<T>;
34 using type = Kokkos::Prod<T>;
40 using type = Kokkos::LAnd<T>;
46 using type = Kokkos::LOr<T>;
52 using type = Kokkos::BAnd<T>;
58 using type = Kokkos::BOr<T>;
64 static_assert(std::is_same_v<T, T>,
"This reducer is not yet implemented");
70 using type = Kokkos::Min<T>;
76 using type = Kokkos::Max<T>;
82 using type = Kokkos::MinMax<T>;
86template <
class Reducer>
87using ddc_to_kokkos_reducer_t =
typename ddc_to_kokkos_reducer<Reducer>::type;
89template <
class Reducer,
class Functor,
class Support,
class IndexSequence>
90class TransformReducerKokkosLambdaAdapter;
92template <
class Reducer,
class Functor,
class Support, std::size_t... Idx>
93class TransformReducerKokkosLambdaAdapter<Reducer, Functor, Support, std::index_sequence<Idx...>>
95 template <std::size_t I>
96 using index_type = DiscreteElementType;
105 TransformReducerKokkosLambdaAdapter(Reducer
const& r, Functor
const& f, Support
const& support)
112 template <std::size_t N =
sizeof...(Idx), std::enable_if_t<(N == 0),
bool> =
true>
113 KOKKOS_FUNCTION
void operator()(
114 [[maybe_unused]] index_type<0> unused_id,
115 typename Reducer::value_type& a)
const
117 a = m_reducer(a, m_functor(DiscreteElement
<>()));
120 template <std::size_t N =
sizeof...(Idx), std::enable_if_t<(N > 0),
bool> =
true>
121 KOKKOS_FUNCTION
void operator()(index_type<Idx>... ids,
typename Reducer::value_type& a)
const
123 a = m_reducer(a, m_functor(m_support(
typename Support::discrete_vector_type(ids...))));
128
129
130
131
132
133
134
135
136
137template <
class ExecSpace,
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
138T transform_reduce_kokkos(
139 std::string
const& label,
140 ExecSpace
const& execution_space,
141 Support
const& domain,
143 BinaryReductionOp
const& reduce,
144 UnaryTransformOp
const& transform)
noexcept
147 Kokkos::parallel_reduce(
149 ddc_to_kokkos_execution_policy(execution_space, domain),
150 TransformReducerKokkosLambdaAdapter<
154 std::make_index_sequence<Support::rank()>>(reduce, transform, domain),
155 ddc_to_kokkos_reducer_t<BinaryReductionOp>(result));
162
163
164
165
166
167
168
169
170
171template <
class ExecSpace,
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
173 std::string
const& label,
174 ExecSpace
const& execution_space,
175 Support
const& domain,
177 BinaryReductionOp&& reduce,
178 UnaryTransformOp&& transform)
noexcept
180 return detail::transform_reduce_kokkos(
185 std::forward<BinaryReductionOp>(reduce),
186 std::forward<UnaryTransformOp>(transform));
190
191
192
193
194
195
196
197
198template <
class ExecSpace,
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
200 ExecSpace
const& execution_space,
201 Support
const& domain,
203 BinaryReductionOp&& reduce,
204 UnaryTransformOp&& transform)
noexcept
206 return detail::transform_reduce_kokkos(
207 "ddc_parallel_transform_reduce_default",
211 std::forward<BinaryReductionOp>(reduce),
212 std::forward<UnaryTransformOp>(transform));
216
217
218
219
220
221
222
223
224template <
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
226 std::string
const& label,
227 Support
const& domain,
229 BinaryReductionOp&& reduce,
230 UnaryTransformOp&& transform)
noexcept
232 return parallel_transform_reduce(
234 Kokkos::DefaultExecutionSpace(),
237 std::forward<BinaryReductionOp>(reduce),
238 std::forward<UnaryTransformOp>(transform));
242
243
244
245
246
247
248
249template <
class Support,
class T,
class BinaryReductionOp,
class UnaryTransformOp>
251 Support
const& domain,
253 BinaryReductionOp&& reduce,
254 UnaryTransformOp&& transform)
noexcept
256 return parallel_transform_reduce(
257 "ddc_parallel_transform_reduce_default",
258 Kokkos::DefaultExecutionSpace(),
261 std::forward<BinaryReductionOp>(reduce),
262 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.