Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_Layout.hpp
Go to the documentation of this file.
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 Christian R. Trott (crtrott@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
46 
47 #ifndef KOKKOS_LAYOUT_HPP
48 #define KOKKOS_LAYOUT_HPP
49 
50 #include <cstddef>
51 #include <impl/Kokkos_Traits.hpp>
52 #include <impl/Kokkos_Tags.hpp>
53 
54 namespace Kokkos {
55 
56 enum { ARRAY_LAYOUT_MAX_RANK = 8 };
57 
58 //----------------------------------------------------------------------------
73 struct LayoutLeft {
76 
77  size_t dimension[ ARRAY_LAYOUT_MAX_RANK ];
78 
79  enum { is_extent_constructible = true };
80 
81  LayoutLeft( LayoutLeft const & ) = default ;
82  LayoutLeft( LayoutLeft && ) = default ;
83  LayoutLeft & operator = ( LayoutLeft const & ) = default ;
84  LayoutLeft & operator = ( LayoutLeft && ) = default ;
85 
86  KOKKOS_INLINE_FUNCTION
87  explicit constexpr
88  LayoutLeft( size_t N0 = 0 , size_t N1 = 0 , size_t N2 = 0 , size_t N3 = 0
89  , size_t N4 = 0 , size_t N5 = 0 , size_t N6 = 0 , size_t N7 = 0 )
90  : dimension { N0 , N1 , N2 , N3 , N4 , N5 , N6 , N7 } {}
91 };
92 
93 //----------------------------------------------------------------------------
107 struct LayoutRight {
110 
111  size_t dimension[ ARRAY_LAYOUT_MAX_RANK ];
112 
113  enum { is_extent_constructible = true };
114 
115  LayoutRight( LayoutRight const & ) = default ;
116  LayoutRight( LayoutRight && ) = default ;
117  LayoutRight & operator = ( LayoutRight const & ) = default ;
118  LayoutRight & operator = ( LayoutRight && ) = default ;
119 
120  KOKKOS_INLINE_FUNCTION
121  explicit constexpr
122  LayoutRight( size_t N0 = 0 , size_t N1 = 0 , size_t N2 = 0 , size_t N3 = 0
123  , size_t N4 = 0 , size_t N5 = 0 , size_t N6 = 0 , size_t N7 = 0 )
124  : dimension { N0 , N1 , N2 , N3 , N4 , N5 , N6 , N7 } {}
125 };
126 
127 //----------------------------------------------------------------------------
131 struct LayoutStride {
132 
135 
136  size_t dimension[ ARRAY_LAYOUT_MAX_RANK ] ;
137  size_t stride[ ARRAY_LAYOUT_MAX_RANK ] ;
138 
139  enum { is_extent_constructible = false };
140 
141  LayoutStride( LayoutStride const & ) = default ;
142  LayoutStride( LayoutStride && ) = default ;
143  LayoutStride & operator = ( LayoutStride const & ) = default ;
144  LayoutStride & operator = ( LayoutStride && ) = default ;
145 
153  template< typename iTypeOrder , typename iTypeDimen >
154  KOKKOS_INLINE_FUNCTION static
156  , iTypeOrder const * const order
157  , iTypeDimen const * const dimen )
158  {
159  LayoutStride tmp ;
160  // Verify valid rank order:
161  int check_input = ARRAY_LAYOUT_MAX_RANK < rank ? 0 : int( 1 << rank ) - 1 ;
162  for ( int r = 0 ; r < ARRAY_LAYOUT_MAX_RANK ; ++r ) {
163  tmp.dimension[r] = 0 ;
164  tmp.stride[r] = 0 ;
165  }
166  for ( int r = 0 ; r < rank ; ++r ) {
167  check_input &= ~int( 1 << order[r] );
168  }
169  if ( 0 == check_input ) {
170  size_t n = 1 ;
171  for ( int r = 0 ; r < rank ; ++r ) {
172  tmp.stride[ order[r] ] = n ;
173  n *= ( dimen[order[r]] );
174  tmp.dimension[r] = dimen[r];
175  }
176  }
177  return tmp ;
178  }
179 
180  KOKKOS_INLINE_FUNCTION
181  explicit constexpr
182  LayoutStride( size_t N0 = 0 , size_t S0 = 0
183  , size_t N1 = 0 , size_t S1 = 0
184  , size_t N2 = 0 , size_t S2 = 0
185  , size_t N3 = 0 , size_t S3 = 0
186  , size_t N4 = 0 , size_t S4 = 0
187  , size_t N5 = 0 , size_t S5 = 0
188  , size_t N6 = 0 , size_t S6 = 0
189  , size_t N7 = 0 , size_t S7 = 0
190  )
191  : dimension { N0 , N1 , N2 , N3 , N4 , N5 , N6 , N7 }
192  , stride { S0 , S1 , S2 , S3 , S4 , S5 , S6 , S7 }
193  {}
194 };
195 
196 // ==========================================================================
197 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
198 
199 //----------------------------------------------------------------------------
216 template < unsigned ArgN0 , unsigned ArgN1 ,
217  bool IsPowerOfTwo = ( Impl::is_integral_power_of_two(ArgN0) &&
218  Impl::is_integral_power_of_two(ArgN1) )
219  >
220 struct LayoutTileLeft {
221 
222  static_assert( Impl::is_integral_power_of_two(ArgN0) &&
223  Impl::is_integral_power_of_two(ArgN1)
224  , "LayoutTileLeft must be given power-of-two tile dimensions" );
225 
227  typedef LayoutTileLeft<ArgN0,ArgN1,IsPowerOfTwo> array_layout ;
228 
229  enum { N0 = ArgN0 };
230  enum { N1 = ArgN1 };
231 
232  size_t dimension[ ARRAY_LAYOUT_MAX_RANK ] ;
233 
234  enum { is_extent_constructible = true };
235 
236  LayoutTileLeft( LayoutTileLeft const & ) = default ;
237  LayoutTileLeft( LayoutTileLeft && ) = default ;
238  LayoutTileLeft & operator = ( LayoutTileLeft const & ) = default ;
239  LayoutTileLeft & operator = ( LayoutTileLeft && ) = default ;
240 
241  KOKKOS_INLINE_FUNCTION
242  explicit constexpr
243  LayoutTileLeft( size_t argN0 = 0 , size_t argN1 = 0 , size_t argN2 = 0 , size_t argN3 = 0
244  , size_t argN4 = 0 , size_t argN5 = 0 , size_t argN6 = 0 , size_t argN7 = 0
245  )
246  : dimension { argN0 , argN1 , argN2 , argN3 , argN4 , argN5 , argN6 , argN7 } {}
247 };
248 
249 #endif // KOKKOS_ENABLE_DEPRECATED_CODE
250 // ===================================================================================
251 
253 
254 enum class Iterate
255 {
256  Default,
257  Left, // Left indices stride fastest
258  Right // Right indices stride fastest
259 };
260 
261 // To check for LayoutTiled
262 // This is to hide extra compile-time 'identifier' info within the LayoutTiled class by not relying on template specialization to include the ArgN*'s
263 template < typename LayoutTiledCheck, class Enable = void >
264 struct is_layouttiled : std::false_type {};
265 
266 #ifndef KOKKOS_ENABLE_DEPRECATED_CODE
267 template < typename LayoutTiledCheck >
268 struct is_layouttiled< LayoutTiledCheck, typename std::enable_if<LayoutTiledCheck::is_array_layout_tiled>::type > : std::true_type {};
269 
270 namespace Experimental {
271 
273 // Must have Rank >= 2
274 template < Kokkos::Iterate OuterP, Kokkos::Iterate InnerP,
275  unsigned ArgN0 , unsigned ArgN1 , unsigned ArgN2 = 0, unsigned ArgN3 = 0, unsigned ArgN4 = 0, unsigned ArgN5 = 0, unsigned ArgN6 = 0, unsigned ArgN7 = 0,
276  bool IsPowerOfTwo =
277  ( Kokkos::Impl::is_integral_power_of_two(ArgN0) &&
278  Kokkos::Impl::is_integral_power_of_two(ArgN1) &&
279  (Kokkos::Impl::is_integral_power_of_two(ArgN2) || (ArgN2 == 0) ) &&
280  (Kokkos::Impl::is_integral_power_of_two(ArgN3) || (ArgN3 == 0) ) &&
281  (Kokkos::Impl::is_integral_power_of_two(ArgN4) || (ArgN4 == 0) ) &&
282  (Kokkos::Impl::is_integral_power_of_two(ArgN5) || (ArgN5 == 0) ) &&
283  (Kokkos::Impl::is_integral_power_of_two(ArgN6) || (ArgN6 == 0) ) &&
284  (Kokkos::Impl::is_integral_power_of_two(ArgN7) || (ArgN7 == 0) )
285  )
286  >
287 struct LayoutTiled {
288 
289  static_assert( IsPowerOfTwo
290  , "LayoutTiled must be given power-of-two tile dimensions" );
291 
292 #if 0
293  static_assert( (Impl::is_integral_power_of_two(ArgN0) ) &&
294  (Impl::is_integral_power_of_two(ArgN1) ) &&
295  (Impl::is_integral_power_of_two(ArgN2) || (ArgN2 == 0) ) &&
296  (Impl::is_integral_power_of_two(ArgN3) || (ArgN3 == 0) ) &&
297  (Impl::is_integral_power_of_two(ArgN4) || (ArgN4 == 0) ) &&
298  (Impl::is_integral_power_of_two(ArgN5) || (ArgN5 == 0) ) &&
299  (Impl::is_integral_power_of_two(ArgN6) || (ArgN6 == 0) ) &&
300  (Impl::is_integral_power_of_two(ArgN7) || (ArgN7 == 0) )
301  , "LayoutTiled must be given power-of-two tile dimensions" );
302 #endif
303 
305  static constexpr Iterate outer_pattern = OuterP;
306  static constexpr Iterate inner_pattern = InnerP;
307 
308  enum { N0 = ArgN0 };
309  enum { N1 = ArgN1 };
310  enum { N2 = ArgN2 };
311  enum { N3 = ArgN3 };
312  enum { N4 = ArgN4 };
313  enum { N5 = ArgN5 };
314  enum { N6 = ArgN6 };
315  enum { N7 = ArgN7 };
316 
317  size_t dimension[ ARRAY_LAYOUT_MAX_RANK ] ;
318 
319  enum { is_extent_constructible = true };
320 
321  LayoutTiled( LayoutTiled const & ) = default ;
322  LayoutTiled( LayoutTiled && ) = default ;
323  LayoutTiled & operator = ( LayoutTiled const & ) = default ;
324  LayoutTiled & operator = ( LayoutTiled && ) = default ;
325 
326  KOKKOS_INLINE_FUNCTION
327  explicit constexpr
328  LayoutTiled( size_t argN0 = 0 , size_t argN1 = 0 , size_t argN2 = 0 , size_t argN3 = 0
329  , size_t argN4 = 0 , size_t argN5 = 0 , size_t argN6 = 0 , size_t argN7 = 0
330  )
331  : dimension { argN0 , argN1 , argN2 , argN3 , argN4 , argN5 , argN6 , argN7 } {}
332 };
333 
334 } // namespace Experimental
335 #endif
336 
337 
338 // For use with view_copy
339 template < typename ... Layout >
340 struct layout_iterate_type_selector {
341  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Default ;
342  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Default ;
343 };
344 
345 template <>
346 struct layout_iterate_type_selector< Kokkos::LayoutRight > {
347  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Right ;
348  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Right ;
349 };
350 
351 template <>
352 struct layout_iterate_type_selector< Kokkos::LayoutLeft > {
353  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Left ;
354  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Left ;
355 };
356 
357 template <>
358 struct layout_iterate_type_selector< Kokkos::LayoutStride > {
359  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Default ;
360  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Default ;
361 };
362 
363 #ifndef KOKKOS_ENABLE_DEPRECATED_CODE
364 template < unsigned ArgN0 , unsigned ArgN1 , unsigned ArgN2 , unsigned ArgN3 , unsigned ArgN4 , unsigned ArgN5 , unsigned ArgN6 , unsigned ArgN7 >
365 struct layout_iterate_type_selector< Kokkos::Experimental::LayoutTiled<Kokkos::Iterate::Left, Kokkos::Iterate::Left, ArgN0, ArgN1, ArgN2, ArgN3, ArgN4, ArgN5, ArgN6, ArgN7, true> > {
366  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Left ;
367  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Left ;
368 };
369 
370 template < unsigned ArgN0 , unsigned ArgN1 , unsigned ArgN2 , unsigned ArgN3 , unsigned ArgN4 , unsigned ArgN5 , unsigned ArgN6 , unsigned ArgN7 >
371 struct layout_iterate_type_selector< Kokkos::Experimental::LayoutTiled<Kokkos::Iterate::Right, Kokkos::Iterate::Left, ArgN0, ArgN1, ArgN2, ArgN3, ArgN4, ArgN5, ArgN6, ArgN7, true> > {
372  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Right ;
373  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Left ;
374 };
375 
376 template < unsigned ArgN0 , unsigned ArgN1 , unsigned ArgN2 , unsigned ArgN3 , unsigned ArgN4 , unsigned ArgN5 , unsigned ArgN6 , unsigned ArgN7 >
377 struct layout_iterate_type_selector< Kokkos::Experimental::LayoutTiled<Kokkos::Iterate::Left, Kokkos::Iterate::Right, ArgN0, ArgN1, ArgN2, ArgN3, ArgN4, ArgN5, ArgN6, ArgN7, true> > {
378  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Left ;
379  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Right ;
380 };
381 
382 template < unsigned ArgN0 , unsigned ArgN1 , unsigned ArgN2 , unsigned ArgN3 , unsigned ArgN4 , unsigned ArgN5 , unsigned ArgN6 , unsigned ArgN7 >
383 struct layout_iterate_type_selector< Kokkos::Experimental::LayoutTiled<Kokkos::Iterate::Right, Kokkos::Iterate::Right, ArgN0, ArgN1, ArgN2, ArgN3, ArgN4, ArgN5, ArgN6, ArgN7, true> > {
384  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Right ;
385  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Right ;
386 };
387 #endif
388 
389 } // namespace Kokkos
390 
391 #endif // #ifndef KOKKOS_LAYOUT_HPP
392 
LayoutLeft array_layout
Tag this class as a kokkos array layout.
Memory layout tag indicating left-to-right (Fortran scheme) striding of multi-indices.
Memory layout tag indicated arbitrarily strided multi-index mapping into contiguous memory...
LayoutStride array_layout
Tag this class as a kokkos array layout.
Memory layout tag indicating right-to-left (C or lexigraphical scheme) striding of multi-indices...
static KOKKOS_INLINE_FUNCTION LayoutStride order_dimensions(int const rank, iTypeOrder const *const order, iTypeDimen const *const dimen)
Compute strides from ordered dimensions.
KOKKOS_INLINE_FUNCTION constexpr unsigned rank(const View< D, P...> &V)
Temporary free function rank() until rank() is implemented in the View.
LayoutRight array_layout
Tag this class as a kokkos array layout.