RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
Span.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_SPAN_HPP
21 #define RAJA_SPAN_HPP
22 
23 #include <type_traits>
24 
25 #include "RAJA/util/concepts.hpp"
26 #include "RAJA/util/macros.hpp"
27 
28 namespace RAJA
29 {
30 
60 template<typename IterType, typename IndexType>
61 struct Span
62 {
63  using element_type = typename std::iterator_traits<IterType>::value_type;
64  using value_type = camp::decay<element_type>;
65  using size_type = IndexType;
66  using difference_type = std::ptrdiff_t;
68  using const_reference = const element_type&;
69  using iterator = IterType;
70  using const_iterator = IterType;
71 
72  static_assert(type_traits::is_integral<IndexType>::value,
73  "IndexType must model Integral");
74  static_assert(type_traits::is_random_access_iterator<IterType>::value,
75  "IterType must model RandomAccessIterator");
76 
78  : m_begin {begin},
79  m_end {end}
80  {}
81 
83  : m_begin {begin},
84  m_end {begin + size}
85  {}
86 
87  constexpr RAJA_HOST_DEVICE RAJA_INLINE iterator begin() { return m_begin; }
88 
89  constexpr RAJA_HOST_DEVICE RAJA_INLINE iterator end() { return m_end; }
90 
91  constexpr RAJA_HOST_DEVICE RAJA_INLINE const_iterator begin() const
92  {
93  return m_begin;
94  }
95 
96  constexpr RAJA_HOST_DEVICE RAJA_INLINE const_iterator end() const
97  {
98  return m_end;
99  }
100 
101  constexpr RAJA_HOST_DEVICE RAJA_INLINE const_iterator cbegin() const
102  {
103  return m_begin;
104  }
105 
106  constexpr RAJA_HOST_DEVICE RAJA_INLINE const_iterator cend() const
107  {
108  return m_end;
109  }
110 
111  constexpr RAJA_HOST_DEVICE RAJA_INLINE friend iterator begin(Span& s)
112  {
113  return s.begin();
114  }
115 
116  constexpr RAJA_HOST_DEVICE RAJA_INLINE friend iterator end(Span& s)
117  {
118  return s.end();
119  }
120 
121  constexpr RAJA_HOST_DEVICE RAJA_INLINE friend const_iterator begin(
122  const Span& s)
123  {
124  return s.begin();
125  }
126 
127  constexpr RAJA_HOST_DEVICE RAJA_INLINE friend const_iterator end(
128  const Span& s)
129  {
130  return s.end();
131  }
132 
133  constexpr RAJA_HOST_DEVICE RAJA_INLINE friend const_iterator cbegin(
134  const Span& s)
135  {
136  return s.cbegin();
137  }
138 
139  constexpr RAJA_HOST_DEVICE RAJA_INLINE friend const_iterator cend(
140  const Span& s)
141  {
142  return s.cend();
143  }
144 
145  constexpr RAJA_HOST_DEVICE RAJA_INLINE reference front() const
146  {
147  return *begin();
148  }
149 
150  constexpr RAJA_HOST_DEVICE RAJA_INLINE reference back() const
151  {
152  return *(end() - 1);
153  }
154 
155  constexpr RAJA_HOST_DEVICE RAJA_INLINE reference operator[](size_type i) const
156  {
157  return data()[i];
158  }
159 
160  constexpr RAJA_HOST_DEVICE RAJA_INLINE iterator data() const
161  {
162  return m_begin;
163  }
164 
165  constexpr RAJA_HOST_DEVICE RAJA_INLINE size_type size() const
166  {
167  return static_cast<size_type>(m_end - m_begin);
168  }
169 
170  constexpr RAJA_HOST_DEVICE RAJA_INLINE bool empty() const
171  {
172  return size() == static_cast<size_type>(0);
173  }
174 
175  constexpr RAJA_HOST_DEVICE RAJA_INLINE Span first(size_type count) const
176  {
177  return slice(0, count);
178  }
179 
180  constexpr RAJA_HOST_DEVICE RAJA_INLINE Span last(size_type count) const
181  {
182  return slice(size() - count, count);
183  }
184 
185  constexpr RAJA_HOST_DEVICE RAJA_INLINE Span subspan(size_type begin,
186  size_type length) const
187  {
188  return slice(begin, length);
189  }
190 
191  constexpr RAJA_HOST_DEVICE RAJA_INLINE Span slice(size_type begin,
192  size_type length) const
193  {
194  auto start = m_begin + begin;
195  auto end = start + length > m_end ? m_end : start + length;
196  return Span(start, end);
197  }
198 
199 private:
200  iterator m_begin;
201  iterator m_end;
202 };
203 
224 template<typename IterType, typename IndexType>
226  IterType begin,
227  IndexType size)
228 {
229  return Span<IterType, IndexType>(begin, size);
230 }
231 
232 template<typename Iter>
233 constexpr RAJA_HOST_DEVICE RAJA_INLINE auto make_span(Iter& iterable)
234 {
235  using std::begin;
236  using std::distance;
237  using std::end;
238  return Span<typename Iter::iterator,
239  decltype(distance(begin(iterable), end(iterable)))>(
240  begin(iterable), end(iterable));
241 }
242 
243 } // end namespace RAJA
244 
245 #endif /* RAJA_SPAN_HPP */
Header file for RAJA concept definitions.
Header file for common RAJA internal macro definitions.
#define RAJA_HOST_DEVICE
Definition: macros.hpp:65
Definition: AlignedRangeIndexSetBuilders.cpp:35
constexpr RAJA_HOST_DEVICE RAJA_INLINE Span< IterType, IndexType > make_span(IterType begin, IndexType size)
Creates a span from a random access iterator and length.
Definition: Span.hpp:225
A view to a sequence of objects.
Definition: Span.hpp:62
IterType const_iterator
Definition: Span.hpp:70
constexpr RAJA_HOST_DEVICE RAJA_INLINE Span subspan(size_type begin, size_type length) const
Definition: Span.hpp:185
constexpr RAJA_HOST_DEVICE RAJA_INLINE friend iterator begin(Span &s)
Definition: Span.hpp:111
constexpr RAJA_HOST_DEVICE RAJA_INLINE Span last(size_type count) const
Definition: Span.hpp:180
constexpr RAJA_HOST_DEVICE RAJA_INLINE friend const_iterator cbegin(const Span &s)
Definition: Span.hpp:133
constexpr RAJA_HOST_DEVICE RAJA_INLINE bool empty() const
Definition: Span.hpp:170
constexpr RAJA_HOST_DEVICE RAJA_INLINE const_iterator end() const
Definition: Span.hpp:96
constexpr RAJA_HOST_DEVICE RAJA_INLINE iterator begin()
Definition: Span.hpp:87
constexpr RAJA_HOST_DEVICE Span(iterator begin, iterator end)
Definition: Span.hpp:77
constexpr RAJA_HOST_DEVICE RAJA_INLINE reference operator[](size_type i) const
Definition: Span.hpp:155
constexpr RAJA_HOST_DEVICE RAJA_INLINE size_type size() const
Definition: Span.hpp:165
camp::decay< element_type > value_type
Definition: Span.hpp:64
IterType iterator
Definition: Span.hpp:69
constexpr RAJA_HOST_DEVICE RAJA_INLINE const_iterator cbegin() const
Definition: Span.hpp:101
IndexType size_type
Definition: Span.hpp:65
constexpr RAJA_HOST_DEVICE Span(iterator begin, size_type size)
Definition: Span.hpp:82
constexpr RAJA_HOST_DEVICE RAJA_INLINE Span slice(size_type begin, size_type length) const
Definition: Span.hpp:191
constexpr RAJA_HOST_DEVICE RAJA_INLINE const_iterator cend() const
Definition: Span.hpp:106
constexpr RAJA_HOST_DEVICE RAJA_INLINE friend const_iterator begin(const Span &s)
Definition: Span.hpp:121
constexpr RAJA_HOST_DEVICE RAJA_INLINE const_iterator begin() const
Definition: Span.hpp:91
const element_type & const_reference
Definition: Span.hpp:68
constexpr RAJA_HOST_DEVICE RAJA_INLINE friend const_iterator end(const Span &s)
Definition: Span.hpp:127
typename std::iterator_traits< IterType >::value_type element_type
Definition: Span.hpp:63
constexpr RAJA_HOST_DEVICE RAJA_INLINE friend const_iterator cend(const Span &s)
Definition: Span.hpp:139
constexpr RAJA_HOST_DEVICE RAJA_INLINE reference front() const
Definition: Span.hpp:145
constexpr RAJA_HOST_DEVICE RAJA_INLINE reference back() const
Definition: Span.hpp:150
constexpr RAJA_HOST_DEVICE RAJA_INLINE Span first(size_type count) const
Definition: Span.hpp:175
constexpr RAJA_HOST_DEVICE RAJA_INLINE iterator end()
Definition: Span.hpp:89
constexpr RAJA_HOST_DEVICE RAJA_INLINE friend iterator end(Span &s)
Definition: Span.hpp:116
std::ptrdiff_t difference_type
Definition: Span.hpp:66
element_type & reference
Definition: Span.hpp:67
constexpr RAJA_HOST_DEVICE RAJA_INLINE iterator data() const
Definition: Span.hpp:160