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