44 #ifndef KOKKOS_ARRAY_HPP 45 #define KOKKOS_ARRAY_HPP 47 #include <Kokkos_Macros.hpp> 49 #include <type_traits> 56 #ifdef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK 58 template <typename Integral, bool Signed = std::is_signed<Integral>::value>
59 struct ArrayBoundsCheck;
61 template <
typename Integral>
62 struct ArrayBoundsCheck<Integral, true> {
63 KOKKOS_INLINE_FUNCTION
64 ArrayBoundsCheck(Integral i,
size_t N) {
66 #ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST 67 std::string s =
"Kokkos::Array: index ";
68 s += std::to_string(i);
70 Kokkos::Impl::throw_runtime_exception(s);
72 Kokkos::abort(
"Kokkos::Array: negative index in device code");
75 ArrayBoundsCheck<Integral, false>(i, N);
79 template <
typename Integral>
80 struct ArrayBoundsCheck<Integral, false> {
81 KOKKOS_INLINE_FUNCTION
82 ArrayBoundsCheck(Integral i,
size_t N) {
83 if (
size_t(i) >= N) {
84 #ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST 85 std::string s =
"Kokkos::Array: index ";
86 s += std::to_string(i);
88 s += std::to_string(N);
89 Kokkos::Impl::throw_runtime_exception(s);
91 Kokkos::abort(
"Kokkos::Array: index >= size");
98 #define KOKKOS_ARRAY_BOUNDS_CHECK(i, N) \ 99 Kokkos::Impl::ArrayBoundsCheck<decltype(i)>(i, N) 101 #else // !defined( KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK ) 103 #define KOKKOS_ARRAY_BOUNDS_CHECK(i, N) (void)0 105 #endif // !defined( KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK ) 110 template<
class T = void
111 ,
size_t N =KOKKOS_INVALID_INDEX
122 T m_internal_implementation_private_member_data[N];
125 typedef T & reference ;
126 typedef typename std::add_const<T>::type & const_reference ;
127 typedef size_t size_type ;
128 typedef ptrdiff_t difference_type ;
129 typedef T value_type ;
130 typedef T * pointer ;
131 typedef typename std::add_const<T>::type * const_pointer ;
133 KOKKOS_INLINE_FUNCTION
static constexpr size_type size() {
return N ; }
134 KOKKOS_INLINE_FUNCTION
static constexpr
bool empty(){
return false ; }
136 template<
typename iType >
137 KOKKOS_INLINE_FUNCTION
138 reference operator[](
const iType & i )
140 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
141 KOKKOS_ARRAY_BOUNDS_CHECK(i, N);
142 return m_internal_implementation_private_member_data[i];
145 template<
typename iType >
146 KOKKOS_INLINE_FUNCTION
147 const_reference operator[](
const iType & i )
const 149 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
150 KOKKOS_ARRAY_BOUNDS_CHECK(i, N);
151 return m_internal_implementation_private_member_data[i];
154 KOKKOS_INLINE_FUNCTION pointer data()
156 return & m_internal_implementation_private_member_data[0];
158 KOKKOS_INLINE_FUNCTION const_pointer data()
const 160 return & m_internal_implementation_private_member_data[0];
163 #ifdef KOKKOS_ROCM_CLANG_WORKAROUND 165 KOKKOS_INLINE_FUNCTION
169 Array & operator = (
const Array & ) = default ;
176 KOKKOS_INLINE_FUNCTION
177 Array(
const std::initializer_list<T>& vals) {
178 for(
int i=0; i<N; i++) {
179 m_internal_implementation_private_member_data[i] = vals.begin()[i];
186 template<
class T ,
class Proxy >
187 struct Array<T,0,Proxy> {
190 typedef typename std::add_const<T>::type & reference ;
191 typedef typename std::add_const<T>::type & const_reference ;
192 typedef size_t size_type ;
193 typedef ptrdiff_t difference_type ;
194 typedef typename std::add_const<T>::type value_type ;
195 typedef typename std::add_const<T>::type * pointer ;
196 typedef typename std::add_const<T>::type * const_pointer ;
198 KOKKOS_INLINE_FUNCTION
static constexpr size_type size() {
return 0 ; }
199 KOKKOS_INLINE_FUNCTION
static constexpr
bool empty() {
return true ; }
201 template<
typename iType >
202 KOKKOS_INLINE_FUNCTION
203 value_type operator[](
const iType & )
205 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integer argument" );
209 template<
typename iType >
210 KOKKOS_INLINE_FUNCTION
211 value_type operator[](
const iType & )
const 213 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integer argument" );
217 KOKKOS_INLINE_FUNCTION pointer data() {
return pointer(0) ; }
218 KOKKOS_INLINE_FUNCTION const_pointer data()
const {
return const_pointer(0); }
220 #ifdef KOKKOS_CUDA_9_DEFAULTED_BUG_WORKAROUND 221 KOKKOS_INLINE_FUNCTION ~Array() {}
222 KOKKOS_INLINE_FUNCTION Array() {}
223 KOKKOS_INLINE_FUNCTION Array(
const Array & ) {}
224 KOKKOS_INLINE_FUNCTION Array & operator = (
const Array & ) {}
226 KOKKOS_INLINE_FUNCTION ~Array() =
default;
227 KOKKOS_INLINE_FUNCTION Array() =
default;
228 KOKKOS_INLINE_FUNCTION Array(
const Array & ) =
default;
229 KOKKOS_INLINE_FUNCTION Array & operator = (
const Array & ) =
default;
240 struct Array<void,KOKKOS_INVALID_INDEX,void>
242 struct contiguous {};
247 struct Array< T ,KOKKOS_INVALID_INDEX , Array<>::contiguous >
254 typedef T & reference ;
255 typedef typename std::add_const<T>::type & const_reference ;
256 typedef size_t size_type ;
257 typedef ptrdiff_t difference_type ;
258 typedef T value_type ;
259 typedef T * pointer ;
260 typedef typename std::add_const<T>::type * const_pointer ;
262 KOKKOS_INLINE_FUNCTION constexpr size_type size()
const {
return m_size ; }
263 KOKKOS_INLINE_FUNCTION constexpr
bool empty()
const {
return 0 != m_size ; }
265 template<
typename iType >
266 KOKKOS_INLINE_FUNCTION
267 reference operator[](
const iType & i )
269 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
270 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
274 template<
typename iType >
275 KOKKOS_INLINE_FUNCTION
276 const_reference operator[](
const iType & i )
const 278 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
279 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
283 KOKKOS_INLINE_FUNCTION pointer data() {
return m_elem ; }
284 KOKKOS_INLINE_FUNCTION const_pointer data()
const {
return m_elem ; }
286 #ifdef KOKKOS_CUDA_9_DEFAULTED_BUG_WORKAROUND 287 KOKKOS_INLINE_FUNCTION ~Array() {}
289 KOKKOS_INLINE_FUNCTION ~Array() =
default;
292 Array(
const Array & rhs ) = delete ;
299 KOKKOS_INLINE_FUNCTION
300 Array & operator = (
const Array & rhs )
302 const size_t n = std::min( m_size , rhs.size() );
303 for (
size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
307 template<
size_t N ,
class P >
308 KOKKOS_INLINE_FUNCTION
309 Array & operator = (
const Array<T,N,P> & rhs )
311 const size_t n = std::min( m_size , rhs.size() );
312 for (
size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
316 KOKKOS_INLINE_FUNCTION constexpr Array( pointer arg_ptr , size_type arg_size , size_type = 0 )
317 : m_elem(arg_ptr), m_size(arg_size) {}
321 struct Array< T ,KOKKOS_INVALID_INDEX , Array<>::strided >
329 typedef T & reference ;
330 typedef typename std::add_const<T>::type & const_reference ;
331 typedef size_t size_type ;
332 typedef ptrdiff_t difference_type ;
333 typedef T value_type ;
334 typedef T * pointer ;
335 typedef typename std::add_const<T>::type * const_pointer ;
337 KOKKOS_INLINE_FUNCTION constexpr size_type size()
const {
return m_size ; }
338 KOKKOS_INLINE_FUNCTION constexpr
bool empty()
const {
return 0 != m_size ; }
340 template<
typename iType >
341 KOKKOS_INLINE_FUNCTION
342 reference operator[](
const iType & i )
344 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
345 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
346 return m_elem[i*m_stride];
349 template<
typename iType >
350 KOKKOS_INLINE_FUNCTION
351 const_reference operator[](
const iType & i )
const 353 static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) ,
"Must be integral argument" );
354 KOKKOS_ARRAY_BOUNDS_CHECK(i, m_size);
355 return m_elem[i*m_stride];
358 KOKKOS_INLINE_FUNCTION pointer data() {
return m_elem ; }
359 KOKKOS_INLINE_FUNCTION const_pointer data()
const {
return m_elem ; }
361 #ifdef KOKKOS_CUDA_9_DEFAULTED_BUG_WORKAROUND 362 KOKKOS_INLINE_FUNCTION ~Array() {}
364 KOKKOS_INLINE_FUNCTION ~Array() =
default;
367 Array(
const Array & ) = delete ;
375 KOKKOS_INLINE_FUNCTION
376 Array & operator = (
const Array & rhs )
378 const size_t n = std::min( m_size , rhs.size() );
379 for (
size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
383 template<
size_t N ,
class P >
384 KOKKOS_INLINE_FUNCTION
385 Array & operator = (
const Array<T,N,P> & rhs )
387 const size_t n = std::min( m_size , rhs.size() );
388 for (
size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
392 KOKKOS_INLINE_FUNCTION constexpr Array( pointer arg_ptr , size_type arg_size , size_type arg_stride )
393 : m_elem(arg_ptr), m_size(arg_size), m_stride(arg_stride) {}
Derived from the C++17 'std::array'. Dropping the iterator interface.