6#ifndef IPPL_PARALLEL_DISPATCH_H
7#define IPPL_PARALLEL_DISPATCH_H
9#include <Kokkos_Core.hpp>
15#include "Utility/IpplException.h"
23 template <
unsigned Dim,
class... PolicyArgs>
26 using policy_type = Kokkos::MDRangePolicy<PolicyArgs..., Kokkos::Rank<Dim>>;
28 using index_type =
typename policy_type::array_index_type;
36 template <
class... PolicyArgs>
38 using policy_type = Kokkos::RangePolicy<PolicyArgs...>;
39 using index_type =
typename policy_type::index_type;
54 template <
class... PolicyArgs,
typename View>
55 typename RangePolicy<View::rank,
typename View::execution_space, PolicyArgs...>::policy_type
57 constexpr unsigned Dim = View::rank;
58 using exec_space =
typename View::execution_space;
59 using policy_type =
typename RangePolicy<Dim, exec_space, PolicyArgs...>::policy_type;
60 if constexpr (Dim == 1) {
61 return policy_type(shift, view.size() - shift);
63 using index_type =
typename RangePolicy<Dim, exec_space, PolicyArgs...>::index_type;
64 Kokkos::Array<index_type, Dim> begin, end;
65 for (
unsigned int d = 0; d < Dim; d++) {
67 end[d] = view.extent(d) - shift;
69 return policy_type(begin, end);
72 throw IpplException(
"detail::getRangePolicy",
"Unreachable state");
87 template <
size_t Dim,
class... PolicyArgs>
89 const Kokkos::Array<
typename RangePolicy<Dim, PolicyArgs...>::index_type, Dim>& begin,
90 const Kokkos::Array<
typename RangePolicy<Dim, PolicyArgs...>::index_type, Dim>& end) {
91 using policy_type =
typename RangePolicy<Dim, PolicyArgs...>::policy_type;
92 if constexpr (Dim == 1) {
93 return policy_type(begin[0], end[0]);
95 return policy_type(begin, end);
98 throw IpplException(
"detail::createRangePolicy",
"Unreachable state");
108 template <
unsigned Dim,
typename T =
size_t>
113 decltype(std::tuple_cat(std::declval<
typename Coords<1, T>::type>(),
114 std::declval<
typename Coords<Dim - 1, T>::type>()));
117 template <
typename T>
119 using type = std::tuple<T>;
122 enum e_functor_type {
128 template <e_functor_type,
typename,
typename,
typename,
typename...>
140 template <
typename Functor,
typename Policy,
typename... T,
typename... Acc>
151 KOKKOS_INLINE_FUNCTION
void operator()(T... x, Acc&... res)
const {
152 using index_type =
typename Policy::index_type;
153 typename Policy::index_array_type args = {
static_cast<index_type
>(x)...};
158 template <
typename Functor,
typename Policy,
typename... T>
162 KOKKOS_INLINE_FUNCTION
void operator()(T... x)
const {
163 using index_type =
typename Policy::index_type;
164 typename Policy::index_array_type args = {
static_cast<index_type
>(x)...};
173 template <
typename... T>
175 static constexpr int rank = 1;
177 template <
typename... T>
179 static constexpr int rank = Kokkos::MDRangePolicy<T...>::rank;
181 template <
typename T>
183 {
typename T::value_type() };
185 template <
typename T>
189 template <HasMemberValueType T>
191 using type =
typename T::value_type;
202 template <e_functor_type Type,
typename Policy,
typename... Acc,
typename Functor>
203 auto functorize(
const Functor& f) {
206 using index_type =
typename PolicyProperties::index_type;
208 typename Coords<Dim, index_type>::type, Acc...>{f};
214 template <
class ExecPolicy,
class FunctorType>
215 void parallel_for(
const std::string& name,
const ExecPolicy& policy,
216 const FunctorType& functor) {
217 Kokkos::parallel_for(name, policy, detail::functorize<detail::FOR, ExecPolicy>(functor));
220 template <
class ExecPolicy,
class FunctorType,
class... ReducerArgument>
221 void parallel_reduce(
const std::string& name,
const ExecPolicy& policy,
222 const FunctorType& functor, ReducerArgument&&... reducer) {
223 Kokkos::parallel_reduce(
225 detail::functorize<detail::REDUCE, ExecPolicy,
226 typename detail::ExtractReducerReturnType<ReducerArgument>::type...>(
228 std::forward<ReducerArgument>(reducer)...);
Definition IpplException.h:6
Definition ParallelDispatch.h:182
RangePolicy< Dim, PolicyArgs... >::policy_type createRangePolicy(const Kokkos::Array< typename RangePolicy< Dim, PolicyArgs... >::index_type, Dim > &begin, const Kokkos::Array< typename RangePolicy< Dim, PolicyArgs... >::index_type, Dim > &end)
Definition ParallelDispatch.h:88
RangePolicy< View::rank, typenameView::execution_space, PolicyArgs... >::policy_type getRangePolicy(const View &view, int shift=0)
Definition ParallelDispatch.h:56
Definition ParallelDispatch.h:24
Definition ParallelDispatch.h:109
KOKKOS_INLINE_FUNCTION void operator()(T... x, Acc &... res) const
Definition ParallelDispatch.h:151
Definition ParallelDispatch.h:129