RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
for_each.hpp
Go to the documentation of this file.
1 
11 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
12 // Copyright (c) Lawrence Livermore National Security, LLC and other
13 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
14 // files for dates and other details. No copyright assignment is required
15 // to contribute to RAJA.
16 //
17 // SPDX-License-Identifier: (BSD-3-Clause)
18 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
19 
20 #ifndef RAJA_util_for_each_HPP
21 #define RAJA_util_for_each_HPP
22 
23 #include "RAJA/config.hpp"
24 
25 #include <iterator>
26 #include <type_traits>
27 
28 #include "camp/list.hpp"
29 
31 
32 #include "RAJA/util/macros.hpp"
33 #include "RAJA/util/types.hpp"
34 
35 namespace RAJA
36 {
37 
38 namespace detail
39 {
40 
41 // runtime loop applying func to each element in the range in order
43 template<typename Iter, typename UnaryFunc>
44 constexpr RAJA_HOST_DEVICE RAJA_INLINE UnaryFunc for_each(Iter begin,
45  Iter end,
46  UnaryFunc func)
47 {
48  for (; begin != end; ++begin)
49  {
50  func(*begin);
51  }
52 
53  return func;
54 }
55 
56 // compile time expansion applying func to a each type in the list in order
58 template<typename UnaryFunc, typename... Ts>
59 constexpr RAJA_HOST_DEVICE RAJA_INLINE UnaryFunc
60 for_each_type(camp::list<Ts...> const&, UnaryFunc func)
61 {
62  // braced init lists are evaluated in order
63  int seq_unused_array[] = {0, (func(Ts {}), 0)...};
64  RAJA_UNUSED_VAR(seq_unused_array);
65 
66  return func;
67 }
68 
69 // compile time expansion applying func to a each type in the tuple in order
71 template<typename Tuple, typename UnaryFunc, camp::idx_t... Is>
72 constexpr RAJA_HOST_DEVICE RAJA_INLINE UnaryFunc
73 for_each_tuple(Tuple&& t, UnaryFunc func, camp::idx_seq<Is...>)
74 {
75  using camp::get;
76  // braced init lists are evaluated in order
77  int seq_unused_array[] = {0, (func(get<Is>(std::forward<Tuple>(t))), 0)...};
78  RAJA_UNUSED_VAR(seq_unused_array);
79 
80  return func;
81 }
82 
83 } // namespace detail
84 
91 template<typename Container, typename UnaryFunc>
92 constexpr RAJA_HOST_DEVICE RAJA_INLINE
93  concepts::enable_if_t<UnaryFunc, type_traits::is_range<Container>>
94  for_each(Container&& c, UnaryFunc func)
95 {
96  using std::begin;
97  using std::end;
98 
99  return detail::for_each(begin(c), end(c), std::move(func));
100 }
101 
107 template<typename UnaryFunc, typename... Ts>
108 constexpr RAJA_HOST_DEVICE RAJA_INLINE UnaryFunc
109 for_each_type(camp::list<Ts...> const& c, UnaryFunc func)
110 {
111  return detail::for_each_type(c, std::move(func));
112 }
113 
119 template<typename Tuple, typename UnaryFunc>
120 constexpr RAJA_HOST_DEVICE RAJA_INLINE UnaryFunc for_each_tuple(Tuple&& t,
121  UnaryFunc func)
122 {
123  return detail::for_each_tuple(
124  std::forward<Tuple>(t), std::move(func),
125  camp::make_idx_seq_t<std::tuple_size<camp::decay<Tuple>>::value> {});
126 }
127 
128 } // namespace RAJA
129 
130 #endif
Header file for RAJA algorithm definitions.
Header file for common RAJA internal macro definitions.
RAJA_HOST_DEVICE RAJA_INLINE void RAJA_UNUSED_VAR(T &&...) noexcept
Definition: macros.hpp:120
#define RAJA_HOST_DEVICE
Definition: macros.hpp:65
#define RAJA_SUPPRESS_HD_WARN
Definition: macros.hpp:68
constexpr RAJA_SUPPRESS_HD_WARN RAJA_HOST_DEVICE RAJA_INLINE UnaryFunc for_each(Iter begin, Iter end, UnaryFunc func)
Definition: for_each.hpp:44
constexpr RAJA_SUPPRESS_HD_WARN RAJA_HOST_DEVICE RAJA_INLINE UnaryFunc for_each_tuple(Tuple &&t, UnaryFunc func, camp::idx_seq< Is... >)
Definition: for_each.hpp:73
constexpr RAJA_SUPPRESS_HD_WARN RAJA_HOST_DEVICE RAJA_INLINE UnaryFunc for_each_type(camp::list< Ts... > const &, UnaryFunc func)
Definition: for_each.hpp:60
Definition: AlignedRangeIndexSetBuilders.cpp:35
RAJA_HOST_DEVICE constexpr RAJA_INLINE RAJA::zip_tuple_element_t< I, zip_tuple< is_val, Ts... > > & get(zip_tuple< is_val, Ts... > &z) noexcept
Definition: zip_tuple.hpp:56
constexpr RAJA_SUPPRESS_HD_WARN RAJA_HOST_DEVICE RAJA_INLINE UnaryFunc for_each_type(camp::list< Ts... > const &c, UnaryFunc func)
Apply func to each type in the given list in order using a compile-time expansion in O(N) operations ...
Definition: for_each.hpp:109
constexpr RAJA_SUPPRESS_HD_WARN RAJA_HOST_DEVICE RAJA_INLINE concepts::enable_if_t< UnaryFunc, type_traits::is_range< Container > > for_each(Container &&c, UnaryFunc func)
Apply func to all the elements in the given range in order using a sequential for loop in O(N) operat...
Definition: for_each.hpp:94
constexpr RAJA_SUPPRESS_HD_WARN RAJA_HOST_DEVICE RAJA_INLINE UnaryFunc for_each_tuple(Tuple &&t, UnaryFunc func)
Apply func to each object in the given tuple or tuple like type in order using a compile-time expansi...
Definition: for_each.hpp:120
Header file for RAJA type definitions.