DDC 0.5.0
Loading...
Searching...
No Matches
parallel_for_each.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <cstddef>
8#include <string>
9#include <type_traits>
10#include <utility>
11
12#include <Kokkos_Core.hpp>
13
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
19namespace ddc {
20
21namespace detail {
22
23template <class F, class Support, class IndexSequence>
24class ForEachKokkosLambdaAdapter;
25
26template <class F, class Support, std::size_t... Idx>
27class ForEachKokkosLambdaAdapter<F, Support, std::index_sequence<Idx...>>
28{
29 template <std::size_t I>
30 using index_type = DiscreteElementType;
31
32 F m_f;
33
34 Support m_support;
35
36public:
37 explicit ForEachKokkosLambdaAdapter(F const& f, Support const& support)
38 : m_f(f)
39 , m_support(support)
40 {
41 }
42
43 template <std::size_t N = sizeof...(Idx), std::enable_if_t<(N == 0), bool> = true>
44 KOKKOS_FUNCTION void operator()([[maybe_unused]] index_type<0> unused_id) const
45 {
46 m_f(DiscreteElement<>());
47 }
48
49 template <std::size_t N = sizeof...(Idx), std::enable_if_t<(N > 0), bool> = true>
50 KOKKOS_FUNCTION void operator()(index_type<Idx>... ids) const
51 {
52 m_f(m_support(typename Support::discrete_vector_type(ids...)));
53 }
54};
55
56template <class ExecSpace, class Support, class Functor>
57void for_each_kokkos(
58 std::string const& label,
59 ExecSpace const& execution_space,
60 Support const& domain,
61 Functor const& f) noexcept
62{
63 Kokkos::parallel_for(
64 label,
65 ddc_to_kokkos_execution_policy(execution_space, domain),
66 ForEachKokkosLambdaAdapter<
67 Functor,
68 Support,
69 std::make_index_sequence<Support::rank()>>(f, domain));
70}
71
72} // namespace detail
73
74/** iterates over a nD domain using a given `Kokkos` execution space
75 * @param[in] label name for easy identification of the parallel_for_each algorithm
76 * @param[in] execution_space a Kokkos execution space where the loop will be executed on
77 * @param[in] domain the domain over which to iterate
78 * @param[in] f a functor taking an index as parameter
79 */
80template <class ExecSpace, class Support, class Functor>
82 std::string const& label,
83 ExecSpace const& execution_space,
84 Support const& domain,
85 Functor&& f) noexcept
86{
87 detail::for_each_kokkos(label, execution_space, domain, std::forward<Functor>(f));
88}
89
90/** iterates over a nD domain using a given `Kokkos` execution space
91 * @param[in] execution_space a Kokkos execution space where the loop will be executed on
92 * @param[in] domain the domain over which to iterate
93 * @param[in] f a functor taking an index as parameter
94 */
95template <class ExecSpace, class Support, class Functor>
96std::enable_if_t<Kokkos::is_execution_space_v<ExecSpace>> parallel_for_each(
97 ExecSpace const& execution_space,
98 Support const& domain,
99 Functor&& f) noexcept
100{
101 detail::for_each_kokkos(
102 "ddc_for_each_default",
103 execution_space,
104 domain,
105 std::forward<Functor>(f));
106}
107
108/** iterates over a nD domain using the `Kokkos` default execution space
109 * @param[in] label name for easy identification of the parallel_for_each algorithm
110 * @param[in] domain the domain over which to iterate
111 * @param[in] f a functor taking an index as parameter
112 */
113template <class Support, class Functor>
114void parallel_for_each(std::string const& label, Support const& domain, Functor&& f) noexcept
115{
116 parallel_for_each(label, Kokkos::DefaultExecutionSpace(), domain, std::forward<Functor>(f));
117}
118
119/** iterates over a nD domain using the `Kokkos` default execution space
120 * @param[in] domain the domain over which to iterate
121 * @param[in] f a functor taking an index as parameter
122 */
123template <class Support, class Functor>
124void parallel_for_each(Support const& domain, Functor&& f) noexcept
125{
126 parallel_for_each(
127 "ddc_for_each_default",
128 Kokkos::DefaultExecutionSpace(),
129 domain,
130 std::forward<Functor>(f));
131}
132
133} // namespace ddc
KOKKOS_DEFAULTED_FUNCTION constexpr DiscreteElement()=default
The top-level namespace of DDC.
void parallel_for_each(std::string const &label, Support const &domain, Functor &&f) noexcept
iterates over a nD domain using the Kokkos default execution space
void parallel_for_each(Support const &domain, Functor &&f) noexcept
iterates over a nD domain using the Kokkos default execution space
std::enable_if_t< Kokkos::is_execution_space_v< ExecSpace > > parallel_for_each(ExecSpace const &execution_space, Support const &domain, Functor &&f) noexcept
iterates over a nD domain using a given Kokkos execution space
void parallel_for_each(std::string const &label, ExecSpace const &execution_space, Support const &domain, Functor &&f) noexcept
iterates over a nD domain using a given Kokkos execution space