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