Skip to content

Commit

Permalink
Merge pull request #5949 from masterleinad/pass_functor_analysis_to_p…
Browse files Browse the repository at this point in the history
…arallel_reduce_openacc

Convert OpenACC ParallelReduce
  • Loading branch information
dalg24 committed Mar 6, 2023
2 parents b2ec19d + 7227127 commit a75aa23
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 97 deletions.
6 changes: 6 additions & 0 deletions core/src/Kokkos_Parallel_Reduce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,12 @@ template <>
struct implements_new_reduce_interface<Kokkos::HIP> : std::true_type {};
#endif

#ifdef KOKKOS_ENABLE_OPENACC
template <>
struct implements_new_reduce_interface<Kokkos::Experimental::OpenACC>
: std::true_type {};
#endif

template <typename CombinedFunctorReducerType, typename PolicyType,
typename ExecutionSpaceType, typename Enable>
class ParallelReduceWrapper {
Expand Down
53 changes: 21 additions & 32 deletions core/src/OpenACC/Kokkos_OpenACC_ParallelReduce_MDRange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,27 @@ struct OpenACCParallelReduceMDRangeHelper {
};
} // namespace Kokkos::Experimental::Impl

template <class Functor, class ReducerType, class... Traits>
class Kokkos::Impl::ParallelReduce<Functor, Kokkos::MDRangePolicy<Traits...>,
ReducerType, Kokkos::Experimental::OpenACC> {
using Policy = MDRangePolicy<Traits...>;
template <class CombinedFunctorReducerType, class... Traits>
class Kokkos::Impl::ParallelReduce<CombinedFunctorReducerType,
Kokkos::MDRangePolicy<Traits...>,
Kokkos::Experimental::OpenACC> {
using Policy = MDRangePolicy<Traits...>;
using FunctorType = typename CombinedFunctorReducerType::functor_type;
using ReducerType = typename CombinedFunctorReducerType::reducer_type;

using ReducerConditional =
if_c<std::is_same_v<InvalidType, ReducerType>, Functor, ReducerType>;
using ReducerTypeFwd = typename ReducerConditional::type;
using Analysis =
FunctorAnalysis<FunctorPatternInterface::REDUCE, Policy, ReducerTypeFwd>;
using Pointer = typename ReducerType::pointer_type;
using ValueType = typename ReducerType::value_type;

using Pointer = typename Analysis::pointer_type;
using ValueType = typename Analysis::value_type;

Functor m_functor;
CombinedFunctorReducerType m_functor_reducer;
Policy m_policy;
ReducerType m_reducer;
Pointer m_result_ptr;

public:
ParallelReduce(Functor const& functor, Policy const& policy,
ReducerType const& reducer)
: m_functor(functor),
m_policy(policy),
m_reducer(reducer),
m_result_ptr(reducer.view().data()) {}

template <class ViewType>
ParallelReduce(
const Functor& functor, const Policy& policy, const ViewType& result,
std::enable_if_t<Kokkos::is_view<ViewType>::value, void*> = nullptr)
: m_functor(functor),
ParallelReduce(const CombinedFunctorReducerType& functor_reducer,
const Policy& policy, const ViewType& result)
: m_functor_reducer(functor_reducer),
m_policy(policy),
m_reducer(InvalidType()),
m_result_ptr(result.data()) {}

void execute() const {
Expand All @@ -85,16 +72,18 @@ class Kokkos::Impl::ParallelReduce<Functor, Kokkos::MDRangePolicy<Traits...>,
}

ValueType val;
typename Analysis::Reducer final_reducer(
ReducerConditional::select(m_functor, m_reducer));
final_reducer.init(&val);
const ReducerType& reducer = m_functor_reducer.get_reducer();
reducer.init(&val);

Kokkos::Experimental::Impl::OpenACCParallelReduceMDRangeHelper(
Kokkos::Experimental::Impl::FunctorAdapter<Functor, Policy>(m_functor),
std::conditional_t<is_reducer_v<ReducerType>, ReducerType,
Sum<ValueType>>(val),
Kokkos::Experimental::Impl::FunctorAdapter<FunctorType, Policy>(
m_functor_reducer.get_functor()),
std::conditional_t<
std::is_same_v<FunctorType, typename ReducerType::functor_type>,
Sum<ValueType>, typename ReducerType::functor_type>(val),
m_policy);

reducer.final(&val);
*m_result_ptr = val;
}
};
Expand Down
57 changes: 23 additions & 34 deletions core/src/OpenACC/Kokkos_OpenACC_ParallelReduce_Range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,27 @@ struct OpenACCParallelReduceHelper {

} // namespace Kokkos::Experimental::Impl

template <class Functor, class ReducerType, class... Traits>
class Kokkos::Impl::ParallelReduce<Functor, Kokkos::RangePolicy<Traits...>,
ReducerType, Kokkos::Experimental::OpenACC> {
using Policy = RangePolicy<Traits...>;

using ReducerConditional =
if_c<std::is_same_v<InvalidType, ReducerType>, Functor, ReducerType>;
using ReducerTypeFwd = typename ReducerConditional::type;
using Analysis =
FunctorAnalysis<FunctorPatternInterface::REDUCE, Policy, ReducerTypeFwd>;

using Pointer = typename Analysis::pointer_type;
using ValueType = typename Analysis::value_type;

Functor m_functor;
template <class CombinedFunctorReducerType, class... Traits>
class Kokkos::Impl::ParallelReduce<CombinedFunctorReducerType,
Kokkos::RangePolicy<Traits...>,
Kokkos::Experimental::OpenACC> {
using Policy = RangePolicy<Traits...>;
using FunctorType = typename CombinedFunctorReducerType::functor_type;
using ReducerType = typename CombinedFunctorReducerType::reducer_type;

using Pointer = typename ReducerType::pointer_type;
using ValueType = typename ReducerType::value_type;

CombinedFunctorReducerType m_functor_reducer;
Policy m_policy;
ReducerType m_reducer;
Pointer m_result_ptr;

public:
ParallelReduce(Functor const& functor, Policy const& policy,
ReducerType const& reducer)
: m_functor(functor),
m_policy(policy),
m_reducer(reducer),
m_result_ptr(reducer.view().data()) {}

template <class ViewType>
ParallelReduce(
const Functor& functor, const Policy& policy, const ViewType& result,
std::enable_if_t<Kokkos::is_view<ViewType>::value, void*> = nullptr)
: m_functor(functor),
ParallelReduce(CombinedFunctorReducerType const& functor_reducer,
Policy const& policy, ViewType const& result)
: m_functor_reducer(functor_reducer),
m_policy(policy),
m_reducer(InvalidType()),
m_result_ptr(result.data()) {}

void execute() const {
Expand All @@ -83,16 +70,18 @@ class Kokkos::Impl::ParallelReduce<Functor, Kokkos::RangePolicy<Traits...>,
}

ValueType val;
typename Analysis::Reducer final_reducer(
ReducerConditional::select(m_functor, m_reducer));
final_reducer.init(&val);
ReducerType const& reducer = m_functor_reducer.get_reducer();
reducer.init(&val);

Kokkos::Experimental::Impl::OpenACCParallelReduceHelper(
Kokkos::Experimental::Impl::FunctorAdapter<Functor, Policy>(m_functor),
std::conditional_t<is_reducer_v<ReducerType>, ReducerType,
Sum<ValueType>>(val),
Kokkos::Experimental::Impl::FunctorAdapter<FunctorType, Policy>(
m_functor_reducer.get_functor()),
std::conditional_t<
std::is_same_v<FunctorType, typename ReducerType::functor_type>,
Sum<ValueType>, typename ReducerType::functor_type>(val),
m_policy);

reducer.final(&val);
*m_result_ptr = val;
}
};
Expand Down
50 changes: 19 additions & 31 deletions core/src/OpenACC/Kokkos_OpenACC_ParallelReduce_Team.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,21 @@ struct OpenACCParallelReduceTeamHelper {

} // namespace Kokkos::Experimental::Impl

template <class FunctorType, class ReducerType, class... Properties>
class Kokkos::Impl::ParallelReduce<FunctorType,
template <class CombinedFunctorReducerType, class... Properties>
class Kokkos::Impl::ParallelReduce<CombinedFunctorReducerType,
Kokkos::TeamPolicy<Properties...>,
ReducerType, Kokkos::Experimental::OpenACC> {
Kokkos::Experimental::OpenACC> {
private:
using Policy =
TeamPolicyInternal<Kokkos::Experimental::OpenACC, Properties...>;
using FunctorType = typename CombinedFunctorReducerType::functor_type;
using ReducerType = typename CombinedFunctorReducerType::reducer_type;

using ReducerConditional =
Kokkos::Impl::if_c<std::is_same<InvalidType, ReducerType>::value,
FunctorType, ReducerType>;
using ReducerTypeFwd = typename ReducerConditional::type;
using Analysis =
FunctorAnalysis<FunctorPatternInterface::REDUCE, Policy, ReducerTypeFwd>;
using value_type = typename Analysis::value_type;
using pointer_type = typename Analysis::pointer_type;
using value_type = typename ReducerType::value_type;
using pointer_type = typename ReducerType::pointer_type;

FunctorType m_functor;
CombinedFunctorReducerType m_functor_reducer;
Policy m_policy;
ReducerType m_reducer;
pointer_type m_result_ptr;

public:
Expand All @@ -68,35 +63,28 @@ class Kokkos::Impl::ParallelReduce<FunctorType,
auto vector_length = m_policy.impl_vector_length();

value_type tmp;
typename Analysis::Reducer final_reducer(
ReducerConditional::select(m_functor, m_reducer));
final_reducer.init(&tmp);
const ReducerType& reducer = m_functor_reducer.get_reducer();
reducer.init(&tmp);

Kokkos::Experimental::Impl::OpenACCParallelReduceTeamHelper(
Kokkos::Experimental::Impl::FunctorAdapter<FunctorType, Policy>(
m_functor),
std::conditional_t<is_reducer_v<ReducerType>, ReducerType,
Sum<value_type>>(tmp),
m_functor_reducer.get_functor()),
std::conditional_t<
std::is_same_v<FunctorType, typename ReducerType::functor_type>,
Sum<value_type>, typename ReducerType::functor_type>(tmp),
m_policy);

reducer.final(&tmp);

m_result_ptr[0] = tmp;
}

template <class ViewType>
ParallelReduce(const FunctorType& arg_functor, const Policy& arg_policy,
const ViewType& arg_result_view,
std::enable_if_t<Kokkos::is_view_v<ViewType>>* = nullptr)
: m_functor(arg_functor),
ParallelReduce(const CombinedFunctorReducerType& arg_functor_reducer,
const Policy& arg_policy, const ViewType& arg_result_view)
: m_functor_reducer(arg_functor_reducer),
m_policy(arg_policy),
m_reducer(InvalidType()),
m_result_ptr(arg_result_view.data()) {}

ParallelReduce(const FunctorType& arg_functor, const Policy& arg_policy,
const ReducerType& reducer)
: m_functor(arg_functor),
m_policy(arg_policy),
m_reducer(reducer),
m_result_ptr(reducer.view().data()) {}
};

namespace Kokkos {
Expand Down

0 comments on commit a75aa23

Please sign in to comment.