RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
CombiningAdapter.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_CombingAdapter_HPP
21 #define RAJA_CombingAdapter_HPP
22 
23 #include <type_traits>
24 
27 #include "RAJA/util/concepts.hpp"
28 #include "RAJA/util/macros.hpp"
29 #include "RAJA/util/Layout.hpp"
31 
32 namespace RAJA
33 {
34 
82 template<typename Lambda, typename Layout_>
84 {
85  using Layout = Layout_;
86 
87  using IndexRange = typename Layout::IndexRange;
88  using StrippedIdxLin = typename Layout::StrippedIdxLin;
89  using IndexLinear = typename Layout::IndexLinear;
90  using DimTuple = typename Layout::DimTuple;
91  using DimArr = typename Layout::DimArr;
92 
94 
95 private:
96  Lambda m_lambda;
97  Layout m_layout;
98 
100  template<camp::idx_t... RangeInts>
101  RAJA_HOST_DEVICE inline auto call_helper(IndexLinear linear_index,
102  camp::idx_seq<RangeInts...>)
103  -> decltype(m_lambda(
104  camp::val<camp::tuple_element_t<RangeInts, DimTuple>>()...))
105  {
106  DimTuple indices;
107  m_layout.toIndices(linear_index, camp::get<RangeInts>(indices)...);
108  return m_lambda(camp::get<RangeInts>(indices)...);
109  }
110 
113  template<camp::idx_t... RangeInts>
114  RAJA_HOST_DEVICE inline auto call_helper(IndexLinear linear_index,
115  camp::idx_seq<RangeInts...>) const
116  -> decltype(m_lambda(
117  camp::val<camp::tuple_element_t<RangeInts, DimTuple>>()...))
118  {
119  DimTuple indices;
120  m_layout.toIndices(linear_index, camp::get<RangeInts>(indices)...);
121  return m_lambda(camp::get<RangeInts>(indices)...);
122  }
123 
124 public:
128  template<typename C_Lambda, typename C_Layout>
129  RAJA_HOST_DEVICE CombiningAdapter(C_Lambda&& lambda, C_Layout&& layout)
130  : m_lambda(std::forward<C_Lambda>(lambda)),
131  m_layout(std::forward<C_Layout>(layout))
132  {}
133 
139  RAJA_HOST_DEVICE RAJA_INLINE auto operator()(IndexLinear linear_index)
140  -> decltype(call_helper(linear_index, IndexRange()))
141  {
142  return call_helper(linear_index, IndexRange());
143  }
144 
146  RAJA_HOST_DEVICE RAJA_INLINE auto operator()(IndexLinear linear_index) const
147  -> decltype(call_helper(linear_index, IndexRange()))
148  {
149  return call_helper(linear_index, IndexRange());
150  }
151 
157  RAJA_HOST_DEVICE RAJA_INLINE IndexLinear size() const
158  {
159  return m_layout.size_noproj();
160  }
161 
169  {
170  return RangeLinear(static_cast<IndexLinear>(0), size());
171  }
172 };
173 
213 template<typename Lambda, typename Layout>
215  Lambda&& lambda,
216  Layout&& layout)
217 // -> CombiningAdapter<camp::decay<Lambda>, camp::decay<Layout>>
218 {
219  return CombiningAdapter<camp::decay<Lambda>, camp::decay<Layout>>(
220  std::forward<Lambda>(lambda), std::forward<Layout>(layout));
221 }
224 template<typename Lambda, typename... IdxTs>
225 RAJA_INLINE auto make_CombiningAdapter(
226  Lambda&& lambda,
227  ::RAJA::TypedRangeSegment<IdxTs> const&... segs)
228 // -> decltype(make_CombiningAdapter_from_layout(std::forward<Lambda>(lambda),
229 // camp::val<RAJA::TypedOffsetLayout<
230 // typename std::common_type< strip_index_type_t<IdxTs>...
231 // >::type, IdxTs...>>()))
232 {
233  using std::begin;
234  using std::distance;
235  using std::end;
236  using IdxLin = typename std::common_type<strip_index_type_t<IdxTs>...>::type;
237  using Layout = RAJA::Layout<sizeof...(IdxTs), IdxLin>;
238  using OffsetLayout = RAJA::TypedOffsetLayout<IdxLin, camp::tuple<IdxTs...>>;
239 
240  Layout layout(static_cast<IdxLin>(distance(begin(segs), end(segs)))...);
241  OffsetLayout offset_layout = OffsetLayout::from_layout_and_offsets(
242  {{(distance(begin(segs), end(segs)) ? static_cast<IdxLin>(*begin(segs))
243  : static_cast<IdxLin>(0))...}},
244  std::move(layout));
245  return make_CombiningAdapter_from_layout(std::forward<Lambda>(lambda),
246  std::move(offset_layout));
247 }
250 template<typename Perm, typename Lambda, typename... IdxTs>
252  Lambda&& lambda,
253  ::RAJA::TypedRangeSegment<IdxTs> const&... segs)
254 // -> decltype(make_CombiningAdapter_from_layout(std::forward<Lambda>(lambda),
255 // camp::val<RAJA::TypedOffsetLayout<
256 // typename std::common_type< strip_index_type_t<IdxTs>...
257 // >::type, IdxTs...>>()))
258 {
259  using std::begin;
260  using std::distance;
261  using std::end;
262  using IdxLin = typename std::common_type<strip_index_type_t<IdxTs>...>::type;
263  using OffsetLayout = RAJA::TypedOffsetLayout<IdxLin, camp::tuple<IdxTs...>>;
264 
265  auto layout = make_permuted_layout<sizeof...(IdxTs), IdxLin>(
266  {{static_cast<IdxLin>(distance(begin(segs), end(segs)))...}},
268  OffsetLayout offset_layout = OffsetLayout::from_layout_and_offsets(
269  {{(distance(begin(segs), end(segs)) ? static_cast<IdxLin>(*begin(segs))
270  : static_cast<IdxLin>(0))...}},
271 
272  std::move(layout));
273  return make_CombiningAdapter_from_layout(std::forward<Lambda>(lambda),
274  std::move(offset_layout));
275 }
276 
277 } // end namespace RAJA
278 
279 #endif /* RAJA_CombingAdapter_HPP */
RAJA header file defining Layout, a N-dimensional index calculator.
RAJA header file defining Layout, a N-dimensional index calculator with offset indices.
Header file containing definitions of RAJA range segment classes.
Header file with aliases to camp types.
Header file for RAJA concept definitions.
Header file for common RAJA internal macro definitions.
#define RAJA_HOST_DEVICE
Definition: macros.hpp:65
#define RAJA_SUPPRESS_HD_WARN
Definition: macros.hpp:68
Definition: AlignedRangeIndexSetBuilders.cpp:35
RAJA_HOST_DEVICE RAJA_INLINE auto make_CombiningAdapter_from_layout(Lambda &&lambda, Layout &&layout)
Creates a CombiningAdapter class from a lambda and segments.
Definition: CombiningAdapter.hpp:214
RAJA_SUPPRESS_HD_WARN RAJA_INLINE auto make_CombiningAdapter(Lambda &&lambda, ::RAJA::TypedRangeSegment< IdxTs > const &... segs)
Definition: CombiningAdapter.hpp:225
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
RAJA_SUPPRESS_HD_WARN RAJA_INLINE auto make_PermutedCombiningAdapter(Lambda &&lambda, ::RAJA::TypedRangeSegment< IdxTs > const &... segs)
Definition: CombiningAdapter.hpp:251
camp::idx_seq< Ints... > Perm
Definition: PermutedLayout.hpp:101
constexpr RAJA_INLINE RAJA_HOST_DEVICE auto make_permuted_layout(std::array< IdxLin, Rank > sizes, std::array< camp::idx_t, Rank > permutation) -> Layout< Rank, IdxLin >
Creates a permuted Layout object.
Definition: PermutedLayout.hpp:66
Definition: ListSegment.hpp:416
A holder for adapting lambdas meant for multidimensional index spaces to allow their use in 1-dimensi...
Definition: CombiningAdapter.hpp:84
RAJA_HOST_DEVICE RAJA_INLINE auto operator()(IndexLinear linear_index) -> decltype(call_helper(linear_index, IndexRange()))
Definition: CombiningAdapter.hpp:139
Layout_ Layout
Definition: CombiningAdapter.hpp:85
RAJA_HOST_DEVICE RAJA_INLINE IndexLinear size() const
Definition: CombiningAdapter.hpp:157
RAJA_HOST_DEVICE CombiningAdapter(C_Lambda &&lambda, C_Layout &&layout)
Definition: CombiningAdapter.hpp:129
typename Layout::IndexRange IndexRange
Definition: CombiningAdapter.hpp:87
RAJA_HOST_DEVICE RAJA_INLINE auto operator()(IndexLinear linear_index) const -> decltype(call_helper(linear_index, IndexRange()))
Definition: CombiningAdapter.hpp:146
RAJA_HOST_DEVICE RAJA_INLINE RangeLinear getRange() const
Definition: CombiningAdapter.hpp:168
RAJA::TypedRangeSegment< IndexLinear > RangeLinear
Definition: CombiningAdapter.hpp:93
typename Layout::IndexLinear IndexLinear
Definition: CombiningAdapter.hpp:89
typename Layout::DimArr DimArr
Definition: CombiningAdapter.hpp:91
typename Layout::StrippedIdxLin StrippedIdxLin
Definition: CombiningAdapter.hpp:88
typename Layout::DimTuple DimTuple
Definition: CombiningAdapter.hpp:90
Definition: OffsetLayout.hpp:172
Definition: OffsetLayout.hpp:188
Segment class representing a contiguous range of typed indices.
Definition: RangeSegment.hpp:100
Definition: Layout.hpp:46