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
45 #define KOKKOS_ARRAY
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 private:
63  T m_elem[N];
64 public:
65 
66  typedef T & reference ;
67  typedef typename std::add_const<T>::type & const_reference ;
68  typedef size_t size_type ;
69  typedef ptrdiff_t difference_type ;
70  typedef T value_type ;
71  typedef T * pointer ;
72  typedef typename std::add_const<T>::type * const_pointer ;
73 
74  KOKKOS_INLINE_FUNCTION static constexpr size_type size() { return N ; }
75  KOKKOS_INLINE_FUNCTION static constexpr bool empty(){ return false ; }
76 
77  template< typename iType >
78  KOKKOS_INLINE_FUNCTION
79  reference operator[]( const iType & i )
80  {
81  static_assert( std::is_integral<iType>::value , "Must be integral argument" );
82  return m_elem[i];
83  }
84 
85  template< typename iType >
86  KOKKOS_INLINE_FUNCTION
87  const_reference operator[]( const iType & i ) const
88  {
89  static_assert( std::is_integral<iType>::value , "Must be integral argument" );
90  return m_elem[i];
91  }
92 
93  KOKKOS_INLINE_FUNCTION pointer data() { return & m_elem[0] ; }
94  KOKKOS_INLINE_FUNCTION const_pointer data() const { return & m_elem[0] ; }
95 
96  ~Array() = default ;
97  Array() = default ;
98  Array( const Array & ) = default ;
99  Array & operator = ( const Array & ) = default ;
100 
101  // Some supported compilers are not sufficiently C++11 compliant
102  // for default move constructor and move assignment operator.
103  // Array( Array && ) = default ;
104  // Array & operator = ( Array && ) = default ;
105 };
106 
107 
108 template< class T , class Proxy >
109 struct Array<T,0,Proxy> {
110 public:
111 
112  typedef typename std::add_const<T>::type & reference ;
113  typedef typename std::add_const<T>::type & const_reference ;
114  typedef size_t size_type ;
115  typedef ptrdiff_t difference_type ;
116  typedef typename std::add_const<T>::type value_type ;
117  typedef typename std::add_const<T>::type * pointer ;
118  typedef typename std::add_const<T>::type * const_pointer ;
119 
120  KOKKOS_INLINE_FUNCTION static constexpr size_type size() { return 0 ; }
121  KOKKOS_INLINE_FUNCTION static constexpr bool empty() { return true ; }
122 
123  template< typename iType >
124  KOKKOS_INLINE_FUNCTION
125  value_type operator[]( const iType & )
126  {
127  static_assert( std::is_integral<iType>::value , "Must be integer argument" );
128  return value_type();
129  }
130 
131  template< typename iType >
132  KOKKOS_INLINE_FUNCTION
133  value_type operator[]( const iType & ) const
134  {
135  static_assert( std::is_integral<iType>::value , "Must be integer argument" );
136  return value_type();
137  }
138 
139  KOKKOS_INLINE_FUNCTION pointer data() { return pointer(0) ; }
140  KOKKOS_INLINE_FUNCTION const_pointer data() const { return const_pointer(0); }
141 
142  ~Array() = default ;
143  Array() = default ;
144  Array( const Array & ) = default ;
145  Array & operator = ( const Array & ) = default ;
146 
147  // Some supported compilers are not sufficiently C++11 compliant
148  // for default move constructor and move assignment operator.
149  // Array( Array && ) = default ;
150  // Array & operator = ( Array && ) = default ;
151 };
152 
153 
154 template<>
155 struct Array<void,~size_t(0),void>
156 {
157  struct contiguous {};
158  struct strided {};
159 };
160 
161 template< class T >
162 struct Array< T , ~size_t(0) , Array<>::contiguous >
163 {
164 private:
165  T * m_elem ;
166  size_t m_size ;
167 public:
168 
169  typedef T & reference ;
170  typedef typename std::add_const<T>::type & const_reference ;
171  typedef size_t size_type ;
172  typedef ptrdiff_t difference_type ;
173  typedef T value_type ;
174  typedef T * pointer ;
175  typedef typename std::add_const<T>::type * const_pointer ;
176 
177  KOKKOS_INLINE_FUNCTION constexpr size_type size() const { return m_size ; }
178  KOKKOS_INLINE_FUNCTION constexpr bool empty() const { return 0 != m_size ; }
179 
180  template< typename iType >
181  KOKKOS_INLINE_FUNCTION
182  reference operator[]( const iType & i )
183  {
184  static_assert( std::is_integral<iType>::value , "Must be integral argument" );
185  return m_elem[i];
186  }
187 
188  template< typename iType >
189  KOKKOS_INLINE_FUNCTION
190  const_reference operator[]( const iType & i ) const
191  {
192  static_assert( std::is_integral<iType>::value , "Must be integral argument" );
193  return m_elem[i];
194  }
195 
196  KOKKOS_INLINE_FUNCTION pointer data() { return m_elem ; }
197  KOKKOS_INLINE_FUNCTION const_pointer data() const { return m_elem ; }
198 
199  ~Array() = default ;
200  Array() = delete ;
201  Array( const Array & rhs ) = delete ;
202 
203  // Some supported compilers are not sufficiently C++11 compliant
204  // for default move constructor and move assignment operator.
205  // Array( Array && rhs ) = default ;
206  // Array & operator = ( Array && rhs ) = delete ;
207 
208  KOKKOS_INLINE_FUNCTION
209  Array & operator = ( const Array & rhs )
210  {
211  const size_t n = std::min( m_size , rhs.size() );
212  for ( size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
213  return *this ;
214  }
215 
216  template< size_t N , class P >
217  KOKKOS_INLINE_FUNCTION
218  Array & operator = ( const Array<T,N,P> & rhs )
219  {
220  const size_t n = std::min( m_size , rhs.size() );
221  for ( size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
222  return *this ;
223  }
224 
225  KOKKOS_INLINE_FUNCTION constexpr Array( pointer arg_ptr , size_type arg_size , size_type = 0 )
226  : m_elem(arg_ptr), m_size(arg_size) {}
227 };
228 
229 template< class T >
230 struct Array< T , ~size_t(0) , Array<>::strided >
231 {
232 private:
233  T * m_elem ;
234  size_t m_size ;
235  size_t m_stride ;
236 public:
237 
238  typedef T & reference ;
239  typedef typename std::add_const<T>::type & const_reference ;
240  typedef size_t size_type ;
241  typedef ptrdiff_t difference_type ;
242  typedef T value_type ;
243  typedef T * pointer ;
244  typedef typename std::add_const<T>::type * const_pointer ;
245 
246  KOKKOS_INLINE_FUNCTION constexpr size_type size() const { return m_size ; }
247  KOKKOS_INLINE_FUNCTION constexpr bool empty() const { return 0 != m_size ; }
248 
249  template< typename iType >
250  KOKKOS_INLINE_FUNCTION
251  reference operator[]( const iType & i )
252  {
253  static_assert( std::is_integral<iType>::value , "Must be integral argument" );
254  return m_elem[i*m_stride];
255  }
256 
257  template< typename iType >
258  KOKKOS_INLINE_FUNCTION
259  const_reference operator[]( const iType & i ) const
260  {
261  static_assert( std::is_integral<iType>::value , "Must be integral argument" );
262  return m_elem[i*m_stride];
263  }
264 
265  KOKKOS_INLINE_FUNCTION pointer data() { return m_elem ; }
266  KOKKOS_INLINE_FUNCTION const_pointer data() const { return m_elem ; }
267 
268  ~Array() = default ;
269  Array() = delete ;
270  Array( const Array & ) = delete ;
271 
272 
273  // Some supported compilers are not sufficiently C++11 compliant
274  // for default move constructor and move assignment operator.
275  // Array( Array && rhs ) = default ;
276  // Array & operator = ( Array && rhs ) = delete ;
277 
278  KOKKOS_INLINE_FUNCTION
279  Array & operator = ( const Array & rhs )
280  {
281  const size_t n = std::min( m_size , rhs.size() );
282  for ( size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
283  return *this ;
284  }
285 
286  template< size_t N , class P >
287  KOKKOS_INLINE_FUNCTION
288  Array & operator = ( const Array<T,N,P> & rhs )
289  {
290  const size_t n = std::min( m_size , rhs.size() );
291  for ( size_t i = 0 ; i < n ; ++i ) m_elem[i] = rhs[i] ;
292  return *this ;
293  }
294 
295  KOKKOS_INLINE_FUNCTION constexpr Array( pointer arg_ptr , size_type arg_size , size_type arg_stride )
296  : m_elem(arg_ptr), m_size(arg_size), m_stride(arg_stride) {}
297 };
298 
299 } // namespace Kokkos
300 
301 #endif /* #ifndef KOKKOS_ARRAY */
302 
Derived from the C++17 &#39;std::array&#39;. Dropping the iterator interface.