45 #ifndef KOKKOS_ARRAY_HPP 46 #define KOKKOS_ARRAY_HPP 48 #include <Kokkos_Macros.hpp> 49 #include <impl/Kokkos_Error.hpp> 51 #include <type_traits> 59 #ifdef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK 61 template <typename Integral, bool Signed = std::is_signed<Integral>::value>
62 struct ArrayBoundsCheck;
64 template <
typename Integral>
65 struct ArrayBoundsCheck<Integral, true> {
66 KOKKOS_INLINE_FUNCTION
67 ArrayBoundsCheck(Integral i,
size_t N) {
69 #ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST 70 std::string s =
"Kokkos::Array: index ";
71 s += std::to_string(i);
73 Kokkos::Impl::throw_runtime_exception(s);
75 Kokkos::abort(
"Kokkos::Array: negative index in device code");
78 ArrayBoundsCheck<Integral, false>(i, N);
82 template <
typename Integral>
83 struct ArrayBoundsCheck<Integral, false> {
84 KOKKOS_INLINE_FUNCTION
85 ArrayBoundsCheck(Integral i,
size_t N) {
87 #ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST 88 std::string s =
"Kokkos::Array: index ";
89 s += std::to_string(i);
91 s += std::to_string(N);
92 Kokkos::Impl::throw_runtime_exception(s);
94 Kokkos::abort(
"Kokkos::Array: index >= size");
101 #define KOKKOS_ARRAY_BOUNDS_CHECK(i, N) \ 102 Kokkos::Impl::ArrayBoundsCheck<decltype(i)>(i, N) 104 #else // !defined( KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK ) 106 #define KOKKOS_ARRAY_BOUNDS_CHECK(i, N) (void)0 108 #endif // !defined( KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK ) 113 template <
class T =
void,
size_t N = KOKKOS_INVALID_INDEX,
class Proxy =
void>
122 T m_internal_implementation_private_member_data[N];
125 using reference = T&;
126 using const_reference =
typename std::add_const<T>::type&;
127 using size_type = size_t;
128 using difference_type = ptrdiff_t;
129 using value_type = T;
131 using const_pointer =
typename std::add_const<T>::type*;
133 KOKKOS_INLINE_FUNCTION
static constexpr size_type size() {
return N; }
134 KOKKOS_INLINE_FUNCTION
static constexpr
bool empty() {
return false; }
135 KOKKOS_INLINE_FUNCTION constexpr size_type max_size()
const {
return N; }
137 template <
typename iType>
138 KOKKOS_INLINE_FUNCTION reference operator[](
const iType& i) {
140 (std::is_integral<iType>::value || std::is_enum<iType>::value),
141 "Must be integral argument");
142 KOKKOS_ARRAY_BOUNDS_CHECK(i, N);
143 return m_internal_implementation_private_member_data[i];
146 template <
typename iType>
147 KOKKOS_INLINE_FUNCTION const_reference operator[](
const iType& i)
const {
149 (std::is_integral<iType>::value || std::is_enum<iType>::value),
150 "Must be integral argument");
151 KOKKOS_ARRAY_BOUNDS_CHECK(i, N);
152 return m_internal_implementation_private_member_data[i];
155 KOKKOS_INLINE_FUNCTION pointer data() {
156 return &m_internal_implementation_private_member_data[0];
158 KOKKOS_INLINE_FUNCTION const_pointer data()
const {
159 return &m_internal_implementation_private_member_data[0];
163 template <
class T,
class Proxy>
164 struct Array<T, 0, Proxy> {
166 using reference = T&;
167 using const_reference =
typename std::add_const<T>::type&;
168 using size_type = size_t;
169 using difference_type = ptrdiff_t;
170 using value_type = T;
172 using const_pointer =
typename std::add_const<T>::type*;
174 KOKKOS_INLINE_FUNCTION
static constexpr size_type size() {
return 0; }
175 KOKKOS_INLINE_FUNCTION
static constexpr
bool empty() {
return true; }
176 KOKKOS_INLINE_FUNCTION constexpr size_type max_size()
const {
return 0; }
178 template <
typename iType>
179 KOKKOS_INLINE_FUNCTION reference operator[](
const iType&) {
181 (std::is_integral<iType>::value || std::is_enum<iType>::value),
182 "Must be integer argument");
183 Kokkos::abort(
"Unreachable code");
184 return *
reinterpret_cast<pointer
>(-1);
187 template <
typename iType>
188 KOKKOS_INLINE_FUNCTION const_reference operator[](
const iType&)
const {
190 (std::is_integral<iType>::value || std::is_enum<iType>::value),
191 "Must be integer argument");
192 Kokkos::abort(
"Unreachable code");
193 return *
reinterpret_cast<const_pointer
>(-1);
196 KOKKOS_INLINE_FUNCTION pointer data() {
return pointer(0); }
197 KOKKOS_INLINE_FUNCTION const_pointer data()
const {
return const_pointer(0); }
199 KOKKOS_DEFAULTED_FUNCTION ~Array() =
default;
200 KOKKOS_DEFAULTED_FUNCTION Array() =
default;
201 KOKKOS_DEFAULTED_FUNCTION Array(
const Array&) =
default;
202 KOKKOS_DEFAULTED_FUNCTION Array& operator=(
const Array&) =
default;
211 struct Array<void, KOKKOS_INVALID_INDEX, void> {
212 struct contiguous {};
217 struct Array<T, KOKKOS_INVALID_INDEX, Array<>::contiguous> {
223 using reference = T&;
224 using const_reference =
typename std::add_const<T>::type&;
225 using size_type = size_t;
226 using difference_type = ptrdiff_t;
227 using value_type = T;
229 using const_pointer =
typename std::add_const<T>::type*;
231 KOKKOS_INLINE_FUNCTION constexpr size_type size()
const {
return m_size; }
232 KOKKOS_INLINE_FUNCTION constexpr
bool empty()
const {
return 0 != m_size; }
233 KOKKOS_INLINE_FUNCTION constexpr size_type max_size()
const {
return m_size; }
235 template <
typename iType>
236 KOKKOS_INLINE_FUNCTION reference operator[](
const iType& i) {
238 (std::is_integral<iType>::value || std::is_enum<iType>::value),
239 "Must be integral argument");
240 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
244 template <
typename iType>
245 KOKKOS_INLINE_FUNCTION const_reference operator[](
const iType& i)
const {
247 (std::is_integral<iType>::value || std::is_enum<iType>::value),
248 "Must be integral argument");
249 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
253 KOKKOS_INLINE_FUNCTION pointer data() {
return m_elem; }
254 KOKKOS_INLINE_FUNCTION const_pointer data()
const {
return m_elem; }
256 KOKKOS_DEFAULTED_FUNCTION ~Array() =
default;
257 KOKKOS_INLINE_FUNCTION_DELETED Array() =
delete;
258 KOKKOS_INLINE_FUNCTION_DELETED Array(
const Array& rhs) =
delete;
265 KOKKOS_INLINE_FUNCTION
266 Array& operator=(
const Array& rhs) {
267 const size_t n = std::min(m_size, rhs.size());
268 for (
size_t i = 0; i < n; ++i) m_elem[i] = rhs[i];
272 template <
size_t N,
class P>
273 KOKKOS_INLINE_FUNCTION Array& operator=(
const Array<T, N, P>& rhs) {
274 const size_t n = std::min(m_size, rhs.size());
275 for (
size_t i = 0; i < n; ++i) m_elem[i] = rhs[i];
279 KOKKOS_INLINE_FUNCTION constexpr Array(pointer arg_ptr, size_type arg_size,
281 : m_elem(arg_ptr), m_size(arg_size) {}
285 struct Array<T, KOKKOS_INVALID_INDEX, Array<>::strided> {
292 using reference = T&;
293 using const_reference =
typename std::add_const<T>::type&;
294 using size_type = size_t;
295 using difference_type = ptrdiff_t;
296 using value_type = T;
298 using const_pointer =
typename std::add_const<T>::type*;
300 KOKKOS_INLINE_FUNCTION constexpr size_type size()
const {
return m_size; }
301 KOKKOS_INLINE_FUNCTION constexpr
bool empty()
const {
return 0 != m_size; }
302 KOKKOS_INLINE_FUNCTION constexpr size_type max_size()
const {
return m_size; }
304 template <
typename iType>
305 KOKKOS_INLINE_FUNCTION reference operator[](
const iType& i) {
307 (std::is_integral<iType>::value || std::is_enum<iType>::value),
308 "Must be integral argument");
309 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
310 return m_elem[i * m_stride];
313 template <
typename iType>
314 KOKKOS_INLINE_FUNCTION const_reference operator[](
const iType& i)
const {
316 (std::is_integral<iType>::value || std::is_enum<iType>::value),
317 "Must be integral argument");
318 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
319 return m_elem[i * m_stride];
322 KOKKOS_INLINE_FUNCTION pointer data() {
return m_elem; }
323 KOKKOS_INLINE_FUNCTION const_pointer data()
const {
return m_elem; }
325 KOKKOS_DEFAULTED_FUNCTION ~Array() =
default;
326 KOKKOS_INLINE_FUNCTION_DELETED Array() =
delete;
327 KOKKOS_INLINE_FUNCTION_DELETED Array(
const Array&) =
delete;
334 KOKKOS_INLINE_FUNCTION
335 Array& operator=(
const Array& rhs) {
336 const size_t n = std::min(m_size, rhs.size());
337 for (
size_t i = 0; i < n; ++i) m_elem[i] = rhs[i];
341 template <
size_t N,
class P>
342 KOKKOS_INLINE_FUNCTION Array& operator=(
const Array<T, N, P>& rhs) {
343 const size_t n = std::min(m_size, rhs.size());
344 for (
size_t i = 0; i < n; ++i) m_elem[i] = rhs[i];
348 KOKKOS_INLINE_FUNCTION constexpr Array(pointer arg_ptr, size_type arg_size,
349 size_type arg_stride)
350 : m_elem(arg_ptr), m_size(arg_size), m_stride(arg_stride) {}
Derived from the C++17 'std::array'. Dropping the iterator interface.