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