44 #ifndef KOKKOS_ARRAY_HPP
45 #define KOKKOS_ARRAY_HPP
47 #include <Kokkos_Macros.hpp>
48 #include <impl/Kokkos_Error.hpp>
50 #include <type_traits>
58 #ifdef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
60 template <typename Integral, bool Signed = std::is_signed<Integral>::value>
61 struct ArrayBoundsCheck;
63 template <
typename Integral>
64 struct ArrayBoundsCheck<Integral, true> {
65 KOKKOS_INLINE_FUNCTION
66 ArrayBoundsCheck(Integral i,
size_t N) {
68 #ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
69 std::string s =
"Kokkos::Array: index ";
70 s += std::to_string(i);
72 Kokkos::Impl::throw_runtime_exception(s);
74 Kokkos::abort(
"Kokkos::Array: negative index in device code");
77 ArrayBoundsCheck<Integral, false>(i, N);
81 template <
typename Integral>
82 struct ArrayBoundsCheck<Integral, false> {
83 KOKKOS_INLINE_FUNCTION
84 ArrayBoundsCheck(Integral i,
size_t N) {
85 if (
size_t(i) >= N) {
86 #ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
87 std::string s =
"Kokkos::Array: index ";
88 s += std::to_string(i);
90 s += std::to_string(N);
91 Kokkos::Impl::throw_runtime_exception(s);
93 Kokkos::abort(
"Kokkos::Array: index >= size");
100 #define KOKKOS_ARRAY_BOUNDS_CHECK(i, N) \
101 Kokkos::Impl::ArrayBoundsCheck<decltype(i)>(i, N)
103 #else // !defined( KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK )
105 #define KOKKOS_ARRAY_BOUNDS_CHECK(i, N) (void)0
107 #endif // !defined( KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK )
112 template<
class T = void
113 ,
size_t N =KOKKOS_INVALID_INDEX
124 T m_internal_implementation_private_member_data[N];
127 typedef T & reference ;
128 typedef typename std::add_const<T>::type & const_reference ;
129 typedef size_t size_type ;
130 typedef ptrdiff_t difference_type ;
131 typedef T value_type ;
132 typedef T * pointer ;
133 typedef typename std::add_const<T>::type * const_pointer ;
135 KOKKOS_INLINE_FUNCTION
static constexpr size_type size() {
return N ; }
136 KOKKOS_INLINE_FUNCTION
static constexpr
bool empty(){
return false ; }
137 KOKKOS_INLINE_FUNCTION constexpr size_type max_size()
const {
return N ; }
139 template<
typename iType >
140 KOKKOS_INLINE_FUNCTION
141 reference operator[](
const iType & i )
143 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
144 KOKKOS_ARRAY_BOUNDS_CHECK(i, N);
145 return m_internal_implementation_private_member_data[i];
148 template<
typename iType >
149 KOKKOS_INLINE_FUNCTION
150 const_reference operator[](
const iType & i )
const
152 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
153 KOKKOS_ARRAY_BOUNDS_CHECK(i, N);
154 return m_internal_implementation_private_member_data[i];
157 KOKKOS_INLINE_FUNCTION pointer data()
159 return & m_internal_implementation_private_member_data[0];
161 KOKKOS_INLINE_FUNCTION const_pointer data()
const
163 return & m_internal_implementation_private_member_data[0];
166 #ifdef KOKKOS_IMPL_ROCM_CLANG_WORKAROUND
168 KOKKOS_INLINE_FUNCTION
172 Array & operator = (
const Array & ) = default ;
179 KOKKOS_INLINE_FUNCTION
180 Array(
const std::initializer_list<T>& vals) {
181 for(
int i=0; i<N; i++) {
182 m_internal_implementation_private_member_data[i] = vals.begin()[i];
189 template<
class T ,
class Proxy >
190 struct Array<T,0,Proxy> {
193 typedef typename std::add_const<T>::type & reference ;
194 typedef typename std::add_const<T>::type & const_reference ;
195 typedef size_t size_type ;
196 typedef ptrdiff_t difference_type ;
197 typedef typename std::add_const<T>::type value_type ;
198 typedef typename std::add_const<T>::type * pointer ;
199 typedef typename std::add_const<T>::type * const_pointer ;
201 KOKKOS_INLINE_FUNCTION
static constexpr size_type size() {
return 0 ; }
202 KOKKOS_INLINE_FUNCTION
static constexpr
bool empty() {
return true ; }
203 KOKKOS_INLINE_FUNCTION constexpr size_type max_size()
const {
return 0 ; }
205 template<
typename iType >
206 KOKKOS_INLINE_FUNCTION
207 value_type operator[](
const iType & )
209 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integer argument" );
213 template<
typename iType >
214 KOKKOS_INLINE_FUNCTION
215 value_type operator[](
const iType & )
const
217 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integer argument" );
221 KOKKOS_INLINE_FUNCTION pointer data() {
return pointer(0) ; }
222 KOKKOS_INLINE_FUNCTION const_pointer data()
const {
return const_pointer(0); }
224 #ifdef KOKKOS_CUDA_9_DEFAULTED_BUG_WORKAROUND
225 KOKKOS_INLINE_FUNCTION ~Array() {}
226 KOKKOS_INLINE_FUNCTION Array() {}
227 KOKKOS_INLINE_FUNCTION Array(
const Array & ) {}
228 KOKKOS_INLINE_FUNCTION Array & operator = (
const Array & ) {}
230 KOKKOS_INLINE_FUNCTION ~Array() =
default;
231 KOKKOS_INLINE_FUNCTION Array() =
default;
232 KOKKOS_INLINE_FUNCTION Array(
const Array & ) =
default;
233 KOKKOS_INLINE_FUNCTION Array & operator = (
const Array & ) =
default;
244 struct Array<void,KOKKOS_INVALID_INDEX,void>
246 struct contiguous {};
251 struct Array< T ,KOKKOS_INVALID_INDEX , Array<>::contiguous >
258 typedef T & reference ;
259 typedef typename std::add_const<T>::type & const_reference ;
260 typedef size_t size_type ;
261 typedef ptrdiff_t difference_type ;
262 typedef T value_type ;
263 typedef T * pointer ;
264 typedef typename std::add_const<T>::type * const_pointer ;
266 KOKKOS_INLINE_FUNCTION constexpr size_type size()
const {
return m_size ; }
267 KOKKOS_INLINE_FUNCTION constexpr
bool empty()
const {
return 0 != m_size ; }
268 KOKKOS_INLINE_FUNCTION constexpr size_type max_size()
const {
return m_size ; }
270 template<
typename iType >
271 KOKKOS_INLINE_FUNCTION
272 reference operator[](
const iType & i )
274 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
275 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
279 template<
typename iType >
280 KOKKOS_INLINE_FUNCTION
281 const_reference operator[](
const iType & i )
const
283 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
284 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
288 KOKKOS_INLINE_FUNCTION pointer data() {
return m_elem ; }
289 KOKKOS_INLINE_FUNCTION const_pointer data()
const {
return m_elem ; }
291 #ifdef KOKKOS_CUDA_9_DEFAULTED_BUG_WORKAROUND
292 KOKKOS_INLINE_FUNCTION ~Array() {}
294 KOKKOS_INLINE_FUNCTION ~Array() =
default;
297 Array(
const Array & rhs ) = delete ;
304 KOKKOS_INLINE_FUNCTION
305 Array & operator = (
const Array & rhs )
307 const size_t n = std::min( m_size , rhs.size() );
308 for (
size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
312 template<
size_t N ,
class P >
313 KOKKOS_INLINE_FUNCTION
314 Array & operator = (
const Array<T,N,P> & rhs )
316 const size_t n = std::min( m_size , rhs.size() );
317 for (
size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
321 KOKKOS_INLINE_FUNCTION constexpr Array( pointer arg_ptr , size_type arg_size , size_type = 0 )
322 : m_elem(arg_ptr), m_size(arg_size) {}
326 struct Array< T ,KOKKOS_INVALID_INDEX , Array<>::strided >
334 typedef T & reference ;
335 typedef typename std::add_const<T>::type & const_reference ;
336 typedef size_t size_type ;
337 typedef ptrdiff_t difference_type ;
338 typedef T value_type ;
339 typedef T * pointer ;
340 typedef typename std::add_const<T>::type * const_pointer ;
342 KOKKOS_INLINE_FUNCTION constexpr size_type size()
const {
return m_size ; }
343 KOKKOS_INLINE_FUNCTION constexpr
bool empty()
const {
return 0 != m_size ; }
344 KOKKOS_INLINE_FUNCTION constexpr size_type max_size()
const {
return m_size ; }
346 template<
typename iType >
347 KOKKOS_INLINE_FUNCTION
348 reference operator[](
const iType & i )
350 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
351 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
352 return m_elem[i*m_stride];
355 template<
typename iType >
356 KOKKOS_INLINE_FUNCTION
357 const_reference operator[](
const iType & i )
const
359 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
360 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
361 return m_elem[i*m_stride];
364 KOKKOS_INLINE_FUNCTION pointer data() {
return m_elem ; }
365 KOKKOS_INLINE_FUNCTION const_pointer data()
const {
return m_elem ; }
367 #ifdef KOKKOS_CUDA_9_DEFAULTED_BUG_WORKAROUND
368 KOKKOS_INLINE_FUNCTION ~Array() {}
370 KOKKOS_INLINE_FUNCTION ~Array() =
default;
373 Array(
const Array & ) = delete ;
381 KOKKOS_INLINE_FUNCTION
382 Array & operator = (
const Array & rhs )
384 const size_t n = std::min( m_size , rhs.size() );
385 for (
size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
389 template<
size_t N ,
class P >
390 KOKKOS_INLINE_FUNCTION
391 Array & operator = (
const Array<T,N,P> & rhs )
393 const size_t n = std::min( m_size , rhs.size() );
394 for (
size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
398 KOKKOS_INLINE_FUNCTION constexpr Array( pointer arg_ptr , size_type arg_size , size_type arg_stride )
399 : m_elem(arg_ptr), m_size(arg_size), m_stride(arg_stride) {}
Derived from the C++17 'std::array'. Dropping the iterator interface.