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 //----------------------------------------------------------------------------
213 template < unsigned ArgN0 , unsigned ArgN1 ,
214  bool IsPowerOfTwo = ( Impl::is_integral_power_of_two(ArgN0) &&
215  Impl::is_integral_power_of_two(ArgN1) )
216  >
218 
219  static_assert( Impl::is_integral_power_of_two(ArgN0) &&
220  Impl::is_integral_power_of_two(ArgN1)
221  , "LayoutTileLeft must be given power-of-two tile dimensions" );
222 
225 
226  enum { N0 = ArgN0 };
227  enum { N1 = ArgN1 };
228 
229  size_t dimension[ ARRAY_LAYOUT_MAX_RANK ] ;
230 
231  enum { is_extent_constructible = true };
232 
233  LayoutTileLeft( LayoutTileLeft const & ) = default ;
234  LayoutTileLeft( LayoutTileLeft && ) = default ;
235  LayoutTileLeft & operator = ( LayoutTileLeft const & ) = default ;
236  LayoutTileLeft & operator = ( LayoutTileLeft && ) = default ;
237 
238  KOKKOS_INLINE_FUNCTION
239  explicit constexpr
240  LayoutTileLeft( size_t argN0 = 0 , size_t argN1 = 0 , size_t argN2 = 0 , size_t argN3 = 0
241  , size_t argN4 = 0 , size_t argN5 = 0 , size_t argN6 = 0 , size_t argN7 = 0
242  )
243  : dimension { argN0 , argN1 , argN2 , argN3 , argN4 , argN5 , argN6 , argN7 } {}
244 };
245 
246 
248 
249 enum class Iterate
250 {
251  Default,
252  Left, // Left indices stride fastest
253  Right // Right indices stride fastest
254 };
255 
256 // To check for LayoutTiled
257 // This is to hide extra compile-time 'identifier' info within the LayoutTiled class by not relying on template specialization to include the ArgN*'s
258 template < typename LayoutTiledCheck, class Enable = void >
259 struct is_layouttiled : std::false_type {};
260 
261 #ifndef KOKKOS_ENABLE_DEPRECATED_CODE
262 template < typename LayoutTiledCheck >
263 struct is_layouttiled< LayoutTiledCheck, typename std::enable_if<LayoutTiledCheck::is_array_layout_tiled>::type > : std::true_type {};
264 
265 namespace Experimental {
266 
268 // Must have Rank >= 2
269 template < Kokkos::Iterate OuterP, Kokkos::Iterate InnerP,
270  unsigned ArgN0 , unsigned ArgN1 , unsigned ArgN2 = 0, unsigned ArgN3 = 0, unsigned ArgN4 = 0, unsigned ArgN5 = 0, unsigned ArgN6 = 0, unsigned ArgN7 = 0,
271  bool IsPowerOfTwo =
272  ( Impl::is_integral_power_of_two(ArgN0) &&
273  Impl::is_integral_power_of_two(ArgN1) &&
274  (Impl::is_integral_power_of_two(ArgN2) || (ArgN2 == 0) ) &&
275  (Impl::is_integral_power_of_two(ArgN3) || (ArgN3 == 0) ) &&
276  (Impl::is_integral_power_of_two(ArgN4) || (ArgN4 == 0) ) &&
277  (Impl::is_integral_power_of_two(ArgN5) || (ArgN5 == 0) ) &&
278  (Impl::is_integral_power_of_two(ArgN6) || (ArgN6 == 0) ) &&
279  (Impl::is_integral_power_of_two(ArgN7) || (ArgN7 == 0) )
280  )
281  >
282 struct LayoutTiled {
283 
284  static_assert( IsPowerOfTwo
285  , "LayoutTiled must be given power-of-two tile dimensions" );
286 
287 #if 0
288  static_assert( (Impl::is_integral_power_of_two(ArgN0) ) &&
289  (Impl::is_integral_power_of_two(ArgN1) ) &&
290  (Impl::is_integral_power_of_two(ArgN2) || (ArgN2 == 0) ) &&
291  (Impl::is_integral_power_of_two(ArgN3) || (ArgN3 == 0) ) &&
292  (Impl::is_integral_power_of_two(ArgN4) || (ArgN4 == 0) ) &&
293  (Impl::is_integral_power_of_two(ArgN5) || (ArgN5 == 0) ) &&
294  (Impl::is_integral_power_of_two(ArgN6) || (ArgN6 == 0) ) &&
295  (Impl::is_integral_power_of_two(ArgN7) || (ArgN7 == 0) )
296  , "LayoutTiled must be given power-of-two tile dimensions" );
297 #endif
298 
300  static constexpr Iterate outer_pattern = OuterP;
301  static constexpr Iterate inner_pattern = InnerP;
302 
303  enum { N0 = ArgN0 };
304  enum { N1 = ArgN1 };
305  enum { N2 = ArgN2 };
306  enum { N3 = ArgN3 };
307  enum { N4 = ArgN4 };
308  enum { N5 = ArgN5 };
309  enum { N6 = ArgN6 };
310  enum { N7 = ArgN7 };
311 
312  size_t dimension[ ARRAY_LAYOUT_MAX_RANK ] ;
313 
314  enum { is_extent_constructible = true };
315 
316  LayoutTiled( LayoutTiled const & ) = default ;
317  LayoutTiled( LayoutTiled && ) = default ;
318  LayoutTiled & operator = ( LayoutTiled const & ) = default ;
319  LayoutTiled & operator = ( LayoutTiled && ) = default ;
320 
321  KOKKOS_INLINE_FUNCTION
322  explicit constexpr
323  LayoutTiled( size_t argN0 = 0 , size_t argN1 = 0 , size_t argN2 = 0 , size_t argN3 = 0
324  , size_t argN4 = 0 , size_t argN5 = 0 , size_t argN6 = 0 , size_t argN7 = 0
325  )
326  : dimension { argN0 , argN1 , argN2 , argN3 , argN4 , argN5 , argN6 , argN7 } {}
327 };
328 
329 } // namespace Experimental
330 #endif
331 
332 
333 // For use with view_copy
334 template < typename ... Layout >
335 struct layout_iterate_type_selector {
336  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Default ;
337  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Default ;
338 };
339 
340 template <>
341 struct layout_iterate_type_selector< Kokkos::LayoutRight > {
342  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Right ;
343  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Right ;
344 };
345 
346 template <>
347 struct layout_iterate_type_selector< Kokkos::LayoutLeft > {
348  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Left ;
349  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Left ;
350 };
351 
352 template <>
353 struct layout_iterate_type_selector< Kokkos::LayoutStride > {
354  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Default ;
355  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Default ;
356 };
357 
358 #ifndef KOKKOS_ENABLE_DEPRECATED_CODE
359 template < unsigned ArgN0 , unsigned ArgN1 , unsigned ArgN2 , unsigned ArgN3 , unsigned ArgN4 , unsigned ArgN5 , unsigned ArgN6 , unsigned ArgN7 >
360 struct layout_iterate_type_selector< Kokkos::Experimental::LayoutTiled<Kokkos::Iterate::Left, Kokkos::Iterate::Left, ArgN0, ArgN1, ArgN2, ArgN3, ArgN4, ArgN5, ArgN6, ArgN7, true> > {
361  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Left ;
362  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Left ;
363 };
364 
365 template < unsigned ArgN0 , unsigned ArgN1 , unsigned ArgN2 , unsigned ArgN3 , unsigned ArgN4 , unsigned ArgN5 , unsigned ArgN6 , unsigned ArgN7 >
366 struct layout_iterate_type_selector< Kokkos::Experimental::LayoutTiled<Kokkos::Iterate::Right, Kokkos::Iterate::Left, ArgN0, ArgN1, ArgN2, ArgN3, ArgN4, ArgN5, ArgN6, ArgN7, true> > {
367  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Right ;
368  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Left ;
369 };
370 
371 template < unsigned ArgN0 , unsigned ArgN1 , unsigned ArgN2 , unsigned ArgN3 , unsigned ArgN4 , unsigned ArgN5 , unsigned ArgN6 , unsigned ArgN7 >
372 struct layout_iterate_type_selector< Kokkos::Experimental::LayoutTiled<Kokkos::Iterate::Left, Kokkos::Iterate::Right, ArgN0, ArgN1, ArgN2, ArgN3, ArgN4, ArgN5, ArgN6, ArgN7, true> > {
373  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Left ;
374  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Right ;
375 };
376 
377 template < unsigned ArgN0 , unsigned ArgN1 , unsigned ArgN2 , unsigned ArgN3 , unsigned ArgN4 , unsigned ArgN5 , unsigned ArgN6 , unsigned ArgN7 >
378 struct layout_iterate_type_selector< Kokkos::Experimental::LayoutTiled<Kokkos::Iterate::Right, Kokkos::Iterate::Right, ArgN0, ArgN1, ArgN2, ArgN3, ArgN4, ArgN5, ArgN6, ArgN7, true> > {
379  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Right ;
380  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Right ;
381 };
382 #endif
383 
384 } // namespace Kokkos
385 
386 #endif // #ifndef KOKKOS_LAYOUT_HPP
387 
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.
LayoutTileLeft< ArgN0, ArgN1, IsPowerOfTwo > array_layout
Tag this class as a kokkos array layout.
Memory layout tag indicating left-to-right (Fortran scheme) striding of multi-indices by tiles...
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.