DDC 0.10.0
Loading...
Searching...
No Matches
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 <array>
8#include <cstddef>
9#include <utility>
10
11#include <Kokkos_Macros.hpp>
12
13#include "discrete_vector.hpp"
14
15namespace ddc {
16
17namespace detail {
18
19template <class Support, std::size_t N, class Functor, class... Is>
20void host_for_each_serial(
21 Support const& domain,
22 std::array<DiscreteVectorElement, N> const& size,
23 Functor const& f,
24 Is const&... is) noexcept
25{
26 static constexpr std::size_t I = sizeof...(Is);
27 if constexpr (I == N) {
28 f(domain(typename Support::discrete_vector_type(is...)));
29 } else {
30 for (DiscreteVectorElement ii = 0; ii < size[I]; ++ii) {
31 host_for_each_serial(domain, size, f, is..., ii);
32 }
33 }
34}
35
36template <class Support, std::size_t N, class Functor, class... Is>
37KOKKOS_FUNCTION void device_for_each_serial(
38 Support const& domain,
39 std::array<DiscreteVectorElement, N> const& size,
40 Functor const& f,
41 Is const&... is) noexcept
42{
43 static constexpr std::size_t I = sizeof...(Is);
44 if constexpr (I == N) {
45 f(domain(typename Support::discrete_vector_type(is...)));
46 } else {
47 for (DiscreteVectorElement ii = 0; ii < size[I]; ++ii) {
48 device_for_each_serial(domain, size, f, is..., ii);
49 }
50 }
51}
52
53} // namespace detail
54
55#if defined(DDC_BUILD_DEPRECATED_CODE)
56/** iterates over a nD domain in serial
57 * @param[in] domain the domain over which to iterate
58 * @param[in] f a functor taking an index as parameter
59 */
60template <class Support, class Functor>
61[[deprecated("Use host_for_each instead")]]
62void for_each(Support const& domain, Functor&& f) noexcept
63{
65}
66#endif
67
68/** iterates over a nD domain in serial
69 * @param[in] domain the domain over which to iterate
70 * @param[in] f a functor taking an index as parameter
71 */
72template <class Support, class Functor>
73void host_for_each(Support const& domain, Functor&& f) noexcept
74{
75 detail::host_for_each_serial(domain, detail::array(domain.extents()), std::forward<Functor>(f));
76}
77
78/** iterates over a nD domain in serial
79 * @param[in] domain the domain over which to iterate
80 * @param[in] f a functor taking an index as parameter
81 */
82template <class Support, class Functor>
83KOKKOS_FUNCTION void device_for_each(Support const& domain, Functor&& f) noexcept
84{
85 detail::device_for_each_serial(
86 domain,
87 detail::array(domain.extents()),
88 std::forward<Functor>(f));
89}
90
91} // namespace ddc
The top-level namespace of DDC.
void host_for_each(Support const &domain, Functor &&f) noexcept
iterates over a nD domain in serial
Definition for_each.hpp:73
KOKKOS_FUNCTION void device_for_each(Support const &domain, Functor &&f) noexcept
iterates over a nD domain in serial
Definition for_each.hpp:83