Kokkos Core Kernels Package  Version of the Day
Kokkos_Array.hpp
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Kokkos v. 2.0
6 // Copyright (2014) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact H. Carter Edwards (hcedwar@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef KOKKOS_ARRAY_HPP
45 #define KOKKOS_ARRAY_HPP
46 
47 #include <type_traits>
48 #include <algorithm>
49 #include <limits>
50 #include <cstddef>
51 
52 namespace Kokkos {
53 
57 template< class T = void
58  , size_t N = ~size_t(0)
59  , class Proxy = void
60  >
61 struct Array {
62 public:
69  T m_internal_implementation_private_member_data[N];
70 public:
71 
72  typedef T & reference ;
73  typedef typename std::add_const<T>::type & const_reference ;
74  typedef size_t size_type ;
75  typedef ptrdiff_t difference_type ;
76  typedef T value_type ;
77  typedef T * pointer ;
78  typedef typename std::add_const<T>::type * const_pointer ;
79 
80  KOKKOS_INLINE_FUNCTION static constexpr size_type size() { return N ; }
81  KOKKOS_INLINE_FUNCTION static constexpr bool empty(){ return false ; }
82 
83  template< typename iType >
84  KOKKOS_INLINE_FUNCTION
85  reference operator[]( const iType & i )
86  {
87  static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) , "Must be integral argument" );
88  return m_internal_implementation_private_member_data[i];
89  }
90 
91  template< typename iType >
92  KOKKOS_INLINE_FUNCTION
93  const_reference operator[]( const iType & i ) const
94  {
95  static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) , "Must be integral argument" );
96  return m_internal_implementation_private_member_data[i];
97  }
98 
99  KOKKOS_INLINE_FUNCTION pointer data()
100  {
101  return & m_internal_implementation_private_member_data[0];
102  }
103  KOKKOS_INLINE_FUNCTION const_pointer data() const
104  {
105  return & m_internal_implementation_private_member_data[0];
106  }
107 
108  // Do not default unless move and move-assignment are also defined
109  // ~Array() = default ;
110  // Array() = default ;
111  // Array( const Array & ) = default ;
112  // Array & operator = ( const Array & ) = default ;
113 
114  // Some supported compilers are not sufficiently C++11 compliant
115  // for default move constructor and move assignment operator.
116  // Array( Array && ) = default ;
117  // Array & operator = ( Array && ) = default ;
118 };
119 
120 
121 template< class T , class Proxy >
122 struct Array<T,0,Proxy> {
123 public:
124 
125  typedef typename std::add_const<T>::type & 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 typename std::add_const<T>::type value_type ;
130  typedef typename std::add_const<T>::type * pointer ;
131  typedef typename std::add_const<T>::type * const_pointer ;
132 
133  KOKKOS_INLINE_FUNCTION static constexpr size_type size() { return 0 ; }
134  KOKKOS_INLINE_FUNCTION static constexpr bool empty() { return true ; }
135 
136  template< typename iType >
137  KOKKOS_INLINE_FUNCTION
138  value_type operator[]( const iType & )
139  {
140  static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) , "Must be integer argument" );
141  return value_type();
142  }
143 
144  template< typename iType >
145  KOKKOS_INLINE_FUNCTION
146  value_type operator[]( const iType & ) const
147  {
148  static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) , "Must be integer argument" );
149  return value_type();
150  }
151 
152  KOKKOS_INLINE_FUNCTION pointer data() { return pointer(0) ; }
153  KOKKOS_INLINE_FUNCTION const_pointer data() const { return const_pointer(0); }
154 
155  ~Array() = default ;
156  Array() = default ;
157  Array( const Array & ) = default ;
158  Array & operator = ( const Array & ) = default ;
159 
160  // Some supported compilers are not sufficiently C++11 compliant
161  // for default move constructor and move assignment operator.
162  // Array( Array && ) = default ;
163  // Array & operator = ( Array && ) = default ;
164 };
165 
166 
167 template<>
168 struct Array<void,~size_t(0),void>
169 {
170  struct contiguous {};
171  struct strided {};
172 };
173 
174 template< class T >
175 struct Array< T , ~size_t(0) , Array<>::contiguous >
176 {
177 private:
178  T * m_elem ;
179  size_t m_size ;
180 public:
181 
182  typedef T & reference ;
183  typedef typename std::add_const<T>::type & const_reference ;
184  typedef size_t size_type ;
185  typedef ptrdiff_t difference_type ;
186  typedef T value_type ;
187  typedef T * pointer ;
188  typedef typename std::add_const<T>::type * const_pointer ;
189 
190  KOKKOS_INLINE_FUNCTION constexpr size_type size() const { return m_size ; }
191  KOKKOS_INLINE_FUNCTION constexpr bool empty() const { return 0 != m_size ; }
192 
193  template< typename iType >
194  KOKKOS_INLINE_FUNCTION
195  reference operator[]( const iType & i )
196  {
197  static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) , "Must be integral argument" );
198  return m_elem[i];
199  }
200 
201  template< typename iType >
202  KOKKOS_INLINE_FUNCTION
203  const_reference operator[]( const iType & i ) const
204  {
205  static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) , "Must be integral argument" );
206  return m_elem[i];
207  }
208 
209  KOKKOS_INLINE_FUNCTION pointer data() { return m_elem ; }
210  KOKKOS_INLINE_FUNCTION const_pointer data() const { return m_elem ; }
211 
212  ~Array() = default ;
213  Array() = delete ;
214  Array( const Array & rhs ) = delete ;
215 
216  // Some supported compilers are not sufficiently C++11 compliant
217  // for default move constructor and move assignment operator.
218  // Array( Array && rhs ) = default ;
219  // Array & operator = ( Array && rhs ) = delete ;
220 
221  KOKKOS_INLINE_FUNCTION
222  Array & operator = ( const Array & rhs )
223  {
224  const size_t n = std::min( m_size , rhs.size() );
225  for ( size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
226  return *this ;
227  }
228 
229  template< size_t N , class P >
230  KOKKOS_INLINE_FUNCTION
231  Array & operator = ( const Array<T,N,P> & rhs )
232  {
233  const size_t n = std::min( m_size , rhs.size() );
234  for ( size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
235  return *this ;
236  }
237 
238  KOKKOS_INLINE_FUNCTION constexpr Array( pointer arg_ptr , size_type arg_size , size_type = 0 )
239  : m_elem(arg_ptr), m_size(arg_size) {}
240 };
241 
242 template< class T >
243 struct Array< T , ~size_t(0) , Array<>::strided >
244 {
245 private:
246  T * m_elem ;
247  size_t m_size ;
248  size_t m_stride ;
249 public:
250 
251  typedef T & reference ;
252  typedef typename std::add_const<T>::type & const_reference ;
253  typedef size_t size_type ;
254  typedef ptrdiff_t difference_type ;
255  typedef T value_type ;
256  typedef T * pointer ;
257  typedef typename std::add_const<T>::type * const_pointer ;
258 
259  KOKKOS_INLINE_FUNCTION constexpr size_type size() const { return m_size ; }
260  KOKKOS_INLINE_FUNCTION constexpr bool empty() const { return 0 != m_size ; }
261 
262  template< typename iType >
263  KOKKOS_INLINE_FUNCTION
264  reference operator[]( const iType & i )
265  {
266  static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) , "Must be integral argument" );
267  return m_elem[i*m_stride];
268  }
269 
270  template< typename iType >
271  KOKKOS_INLINE_FUNCTION
272  const_reference operator[]( const iType & i ) const
273  {
274  static_assert( ( std::is_integral<iType>::value || std::is_enum<iType>::value ) , "Must be integral argument" );
275  return m_elem[i*m_stride];
276  }
277 
278  KOKKOS_INLINE_FUNCTION pointer data() { return m_elem ; }
279  KOKKOS_INLINE_FUNCTION const_pointer data() const { return m_elem ; }
280 
281  ~Array() = default ;
282  Array() = delete ;
283  Array( const Array & ) = delete ;
284 
285 
286  // Some supported compilers are not sufficiently C++11 compliant
287  // for default move constructor and move assignment operator.
288  // Array( Array && rhs ) = default ;
289  // Array & operator = ( Array && rhs ) = delete ;
290 
291  KOKKOS_INLINE_FUNCTION
292  Array & operator = ( const Array & rhs )
293  {
294  const size_t n = std::min( m_size , rhs.size() );
295  for ( size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
296  return *this ;
297  }
298 
299  template< size_t N , class P >
300  KOKKOS_INLINE_FUNCTION
301  Array & operator = ( const Array<T,N,P> & rhs )
302  {
303  const size_t n = std::min( m_size , rhs.size() );
304  for ( size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
305  return *this ;
306  }
307 
308  KOKKOS_INLINE_FUNCTION constexpr Array( pointer arg_ptr , size_type arg_size , size_type arg_stride )
309  : m_elem(arg_ptr), m_size(arg_size), m_stride(arg_stride) {}
310 };
311 
312 } // namespace Kokkos
313 
314 #endif /* #ifndef KOKKOS_ARRAY_HPP */
315 
Derived from the C++17 &#39;std::array&#39;. Dropping the iterator interface.