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