Kokkos Core Kernels Package  Version of the Day
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  LayoutLeft( LayoutLeft const & ) = default ;
80  LayoutLeft( LayoutLeft && ) = default ;
81  LayoutLeft & operator = ( LayoutLeft const & ) = default ;
82  LayoutLeft & operator = ( LayoutLeft && ) = default ;
83 
84  KOKKOS_INLINE_FUNCTION
85  explicit constexpr
86  LayoutLeft( size_t N0 = 0 , size_t N1 = 0 , size_t N2 = 0 , size_t N3 = 0
87  , size_t N4 = 0 , size_t N5 = 0 , size_t N6 = 0 , size_t N7 = 0 )
88  : dimension { N0 , N1 , N2 , N3 , N4 , N5 , N6 , N7 } {}
89 };
90 
91 //----------------------------------------------------------------------------
105 struct LayoutRight {
108 
109  size_t dimension[ ARRAY_LAYOUT_MAX_RANK ];
110 
111  LayoutRight( LayoutRight const & ) = default ;
112  LayoutRight( LayoutRight && ) = default ;
113  LayoutRight & operator = ( LayoutRight const & ) = default ;
114  LayoutRight & operator = ( LayoutRight && ) = default ;
115 
116  KOKKOS_INLINE_FUNCTION
117  explicit constexpr
118  LayoutRight( size_t N0 = 0 , size_t N1 = 0 , size_t N2 = 0 , size_t N3 = 0
119  , size_t N4 = 0 , size_t N5 = 0 , size_t N6 = 0 , size_t N7 = 0 )
120  : dimension { N0 , N1 , N2 , N3 , N4 , N5 , N6 , N7 } {}
121 };
122 
123 //----------------------------------------------------------------------------
127 struct LayoutStride {
128 
131 
132  size_t dimension[ ARRAY_LAYOUT_MAX_RANK ] ;
133  size_t stride[ ARRAY_LAYOUT_MAX_RANK ] ;
134 
135  LayoutStride( LayoutStride const & ) = default ;
136  LayoutStride( LayoutStride && ) = default ;
137  LayoutStride & operator = ( LayoutStride const & ) = default ;
138  LayoutStride & operator = ( LayoutStride && ) = default ;
139 
147  template< typename iTypeOrder , typename iTypeDimen >
148  KOKKOS_INLINE_FUNCTION static
150  , iTypeOrder const * const order
151  , iTypeDimen const * const dimen )
152  {
153  LayoutStride tmp ;
154  // Verify valid rank order:
155  int check_input = ARRAY_LAYOUT_MAX_RANK < rank ? 0 : int( 1 << rank ) - 1 ;
156  for ( int r = 0 ; r < ARRAY_LAYOUT_MAX_RANK ; ++r ) {
157  tmp.dimension[r] = 0 ;
158  tmp.stride[r] = 0 ;
159  }
160  for ( int r = 0 ; r < rank ; ++r ) {
161  check_input &= ~int( 1 << order[r] );
162  }
163  if ( 0 == check_input ) {
164  size_t n = 1 ;
165  for ( int r = 0 ; r < rank ; ++r ) {
166  tmp.stride[ order[r] ] = n ;
167  n *= ( dimen[order[r]] );
168  tmp.dimension[r] = dimen[r];
169  }
170  }
171  return tmp ;
172  }
173 
174  KOKKOS_INLINE_FUNCTION
175  explicit constexpr
176  LayoutStride( size_t N0 = 0 , size_t S0 = 0
177  , size_t N1 = 0 , size_t S1 = 0
178  , size_t N2 = 0 , size_t S2 = 0
179  , size_t N3 = 0 , size_t S3 = 0
180  , size_t N4 = 0 , size_t S4 = 0
181  , size_t N5 = 0 , size_t S5 = 0
182  , size_t N6 = 0 , size_t S6 = 0
183  , size_t N7 = 0 , size_t S7 = 0
184  )
185  : dimension { N0 , N1 , N2 , N3 , N4 , N5 , N6 , N7 }
186  , stride { S0 , S1 , S2 , S3 , S4 , S5 , S6 , S7 }
187  {}
188 };
189 
190 //----------------------------------------------------------------------------
207 template < unsigned ArgN0 , unsigned ArgN1 ,
208  bool IsPowerOfTwo = ( Impl::is_integral_power_of_two(ArgN0) &&
209  Impl::is_integral_power_of_two(ArgN1) )
210  >
212 
213  static_assert( Impl::is_integral_power_of_two(ArgN0) &&
214  Impl::is_integral_power_of_two(ArgN1)
215  , "LayoutTileLeft must be given power-of-two tile dimensions" );
216 
219 
220  enum { N0 = ArgN0 };
221  enum { N1 = ArgN1 };
222 
223  size_t dimension[ ARRAY_LAYOUT_MAX_RANK ] ;
224 
225  LayoutTileLeft( LayoutTileLeft const & ) = default ;
226  LayoutTileLeft( LayoutTileLeft && ) = default ;
227  LayoutTileLeft & operator = ( LayoutTileLeft const & ) = default ;
228  LayoutTileLeft & operator = ( LayoutTileLeft && ) = default ;
229 
230  KOKKOS_INLINE_FUNCTION
231  explicit constexpr
232  LayoutTileLeft( size_t argN0 = 0 , size_t argN1 = 0 , size_t argN2 = 0 , size_t argN3 = 0
233  , size_t argN4 = 0 , size_t argN5 = 0 , size_t argN6 = 0 , size_t argN7 = 0
234  )
235  : dimension { argN0 , argN1 , argN2 , argN3 , argN4 , argN5 , argN6 , argN7 } {}
236 };
237 
238 } // namespace Kokkos
239 
240 #endif // #ifndef KOKKOS_LAYOUT_HPP
241 
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...
LayoutRight array_layout
Tag this class as a kokkos array layout.
KOKKOS_INLINE_FUNCTION constexpr unsigned rank(const View< D, P... > &V)
Temporary free function rank() until rank() is implemented in the View.