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