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 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16 
19 
20 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
21 #include <Kokkos_Macros.hpp>
22 static_assert(false,
23  "Including non-public Kokkos header files is not allowed.");
24 #endif
25 #ifndef KOKKOS_LAYOUT_HPP
26 #define KOKKOS_LAYOUT_HPP
27 
28 #include <cstddef>
29 #include <impl/Kokkos_Traits.hpp>
30 
31 namespace Kokkos {
32 
33 enum { ARRAY_LAYOUT_MAX_RANK = 8 };
34 
35 //----------------------------------------------------------------------------
50 struct LayoutLeft {
53 
54  size_t dimension[ARRAY_LAYOUT_MAX_RANK];
55  // we don't have a constructor to set the stride directly
56  // but we will deprecate the class anyway (or at least using an instance of
57  // this class) when switching the internal implementation to use mdspan
58  size_t stride;
59 
60  enum : bool { is_extent_constructible = true };
61 
62  LayoutLeft(LayoutLeft const&) = default;
63  LayoutLeft(LayoutLeft&&) = default;
64  LayoutLeft& operator=(LayoutLeft const&) = default;
65  LayoutLeft& operator=(LayoutLeft&&) = default;
66 
67  KOKKOS_INLINE_FUNCTION
68  explicit constexpr LayoutLeft(size_t N0 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
69  size_t N1 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
70  size_t N2 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
71  size_t N3 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
72  size_t N4 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
73  size_t N5 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
74  size_t N6 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
75  size_t N7 = KOKKOS_IMPL_CTOR_DEFAULT_ARG)
76  : dimension{N0, N1, N2, N3, N4, N5, N6, N7},
77  stride(KOKKOS_IMPL_CTOR_DEFAULT_ARG) {}
78 
79  friend bool operator==(const LayoutLeft& left, const LayoutLeft& right) {
80  for (unsigned int rank = 0; rank < ARRAY_LAYOUT_MAX_RANK; ++rank)
81  if (left.dimension[rank] != right.dimension[rank]) return false;
82  return true;
83  }
84 
85  friend bool operator!=(const LayoutLeft& left, const LayoutLeft& right) {
86  return !(left == right);
87  }
88 };
89 
90 //----------------------------------------------------------------------------
104 struct LayoutRight {
107 
108  size_t dimension[ARRAY_LAYOUT_MAX_RANK];
109  // we don't have a constructor to set the stride directly
110  // but we will deprecate the class anyway (or at least using an instance of
111  // this class) when switching the internal implementation to use mdspan
112  size_t stride;
113 
114  enum : bool { is_extent_constructible = true };
115 
116  LayoutRight(LayoutRight const&) = default;
117  LayoutRight(LayoutRight&&) = default;
118  LayoutRight& operator=(LayoutRight const&) = default;
119  LayoutRight& operator=(LayoutRight&&) = default;
120 
121  KOKKOS_INLINE_FUNCTION
122  explicit constexpr LayoutRight(size_t N0 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
123  size_t N1 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
124  size_t N2 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
125  size_t N3 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
126  size_t N4 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
127  size_t N5 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
128  size_t N6 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
129  size_t N7 = KOKKOS_IMPL_CTOR_DEFAULT_ARG)
130  : dimension{N0, N1, N2, N3, N4, N5, N6, N7},
131  stride{KOKKOS_IMPL_CTOR_DEFAULT_ARG} {}
132 
133  friend bool operator==(const LayoutRight& left, const LayoutRight& right) {
134  for (unsigned int rank = 0; rank < ARRAY_LAYOUT_MAX_RANK; ++rank)
135  if (left.dimension[rank] != right.dimension[rank]) return false;
136  return true;
137  }
138 
139  friend bool operator!=(const LayoutRight& left, const LayoutRight& right) {
140  return !(left == right);
141  }
142 };
143 
144 //----------------------------------------------------------------------------
148 struct LayoutStride {
151 
152  size_t dimension[ARRAY_LAYOUT_MAX_RANK];
153  size_t stride[ARRAY_LAYOUT_MAX_RANK];
154 
155  enum : bool { is_extent_constructible = false };
156 
157  LayoutStride(LayoutStride const&) = default;
158  LayoutStride(LayoutStride&&) = default;
159  LayoutStride& operator=(LayoutStride const&) = default;
160  LayoutStride& operator=(LayoutStride&&) = default;
161 
169  template <typename iTypeOrder, typename iTypeDimen>
170  KOKKOS_INLINE_FUNCTION static LayoutStride order_dimensions(
171  int const rank, iTypeOrder const* const order,
172  iTypeDimen const* const dimen) {
173  LayoutStride tmp;
174  // Verify valid rank order:
175  int check_input = ARRAY_LAYOUT_MAX_RANK < rank ? 0 : int(1 << rank) - 1;
176  for (int r = 0; r < ARRAY_LAYOUT_MAX_RANK; ++r) {
177  tmp.dimension[r] = KOKKOS_IMPL_CTOR_DEFAULT_ARG;
178  tmp.stride[r] = 0;
179  }
180  for (int r = 0; r < rank; ++r) {
181  check_input &= ~int(1 << order[r]);
182  }
183  if (0 == check_input) {
184  size_t n = 1;
185  for (int r = 0; r < rank; ++r) {
186  tmp.stride[order[r]] = n;
187  n *= (dimen[order[r]]);
188  tmp.dimension[r] = dimen[r];
189  }
190  }
191  return tmp;
192  }
193 
194  KOKKOS_INLINE_FUNCTION
195  explicit constexpr LayoutStride(
196  size_t N0 = KOKKOS_IMPL_CTOR_DEFAULT_ARG, size_t S0 = 0,
197  size_t N1 = KOKKOS_IMPL_CTOR_DEFAULT_ARG, size_t S1 = 0,
198  size_t N2 = KOKKOS_IMPL_CTOR_DEFAULT_ARG, size_t S2 = 0,
199  size_t N3 = KOKKOS_IMPL_CTOR_DEFAULT_ARG, size_t S3 = 0,
200  size_t N4 = KOKKOS_IMPL_CTOR_DEFAULT_ARG, size_t S4 = 0,
201  size_t N5 = KOKKOS_IMPL_CTOR_DEFAULT_ARG, size_t S5 = 0,
202  size_t N6 = KOKKOS_IMPL_CTOR_DEFAULT_ARG, size_t S6 = 0,
203  size_t N7 = KOKKOS_IMPL_CTOR_DEFAULT_ARG, size_t S7 = 0)
204  : dimension{N0, N1, N2, N3, N4, N5, N6, N7},
205  stride{S0, S1, S2, S3, S4, S5, S6, S7} {}
206 
207  friend bool operator==(const LayoutStride& left, const LayoutStride& right) {
208  for (unsigned int rank = 0; rank < ARRAY_LAYOUT_MAX_RANK; ++rank)
209  if (left.dimension[rank] != right.dimension[rank] ||
210  left.stride[rank] != right.stride[rank])
211  return false;
212  return true;
213  }
214 
215  friend bool operator!=(const LayoutStride& left, const LayoutStride& right) {
216  return !(left == right);
217  }
218 };
219 
220 // ===================================================================================
221 
223 
224 enum class Iterate {
225  Default,
226  Left, // Left indices stride fastest
227  Right // Right indices stride fastest
228 };
229 
230 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
231 template <typename Layout, class Enable = void>
232 struct KOKKOS_DEPRECATED is_layouttiled : std::false_type {};
233 #endif
234 
235 namespace Impl {
236 // For use with view_copy
237 template <typename... Layout>
238 struct layout_iterate_type_selector {
239  static const Kokkos::Iterate outer_iteration_pattern =
240  Kokkos::Iterate::Default;
241  static const Kokkos::Iterate inner_iteration_pattern =
242  Kokkos::Iterate::Default;
243 };
244 
245 template <>
246 struct layout_iterate_type_selector<Kokkos::LayoutRight> {
247  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Right;
248  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Right;
249 };
250 
251 template <>
252 struct layout_iterate_type_selector<Kokkos::LayoutLeft> {
253  static const Kokkos::Iterate outer_iteration_pattern = Kokkos::Iterate::Left;
254  static const Kokkos::Iterate inner_iteration_pattern = Kokkos::Iterate::Left;
255 };
256 
257 template <>
258 struct layout_iterate_type_selector<Kokkos::LayoutStride> {
259  static const Kokkos::Iterate outer_iteration_pattern =
260  Kokkos::Iterate::Default;
261  static const Kokkos::Iterate inner_iteration_pattern =
262  Kokkos::Iterate::Default;
263 };
264 } // namespace Impl
265 
266 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
267 template <typename... Layout>
268 using layout_iterate_type_selector KOKKOS_DEPRECATED =
269  Impl::layout_iterate_type_selector<Layout...>;
270 #endif
271 
272 } // namespace Kokkos
273 
274 #endif // #ifndef KOKKOS_LAYOUT_HPP
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...
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.