Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_DynRankView.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 
22 
23 #ifndef KOKKOS_DYNRANKVIEW_HPP
24 #define KOKKOS_DYNRANKVIEW_HPP
25 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
26 #define KOKKOS_IMPL_PUBLIC_INCLUDE
27 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_DYNRANKVIEW
28 #endif
29 
30 #include <Kokkos_Core.hpp>
31 #include <KokkosExp_InterOp.hpp>
32 #include <impl/Kokkos_Error.hpp>
33 #include <type_traits>
34 
35 namespace Kokkos {
36 
37 template <typename DataType, class... Properties>
38 class DynRankView; // forward declare
39 
40 namespace Impl {
41 
42 template <class T, size_t Rank>
43 struct ViewDataTypeFromRank {
44  using type = typename ViewDataTypeFromRank<T, Rank - 1>::type*;
45 };
46 
47 template <class T>
48 struct ViewDataTypeFromRank<T, 0> {
49  using type = T;
50 };
51 
52 template <unsigned N, typename T, typename... Args>
53 KOKKOS_FUNCTION View<typename ViewDataTypeFromRank<T, N>::type, Args...>
54 as_view_of_rank_n(
55  DynRankView<T, Args...> v,
56  std::enable_if_t<std::is_same_v<typename ViewTraits<T, Args...>::specialize,
57  void>>* = nullptr);
58 
59 // TODO: Replace this specialized with a bool
60 template <typename Specialize>
61 struct DynRankDimTraits {
62  enum : size_t { unspecified = KOKKOS_INVALID_INDEX };
63 
64  // Compute the rank of the view from the nonzero dimension arguments.
65  KOKKOS_INLINE_FUNCTION
66  static size_t computeRank(const size_t N0, const size_t N1, const size_t N2,
67  const size_t N3, const size_t N4, const size_t N5,
68  const size_t N6, const size_t /* N7 */) {
69  return (
70  (N6 == unspecified && N5 == unspecified && N4 == unspecified &&
71  N3 == unspecified && N2 == unspecified && N1 == unspecified &&
72  N0 == unspecified)
73  ? 0
74  : ((N6 == unspecified && N5 == unspecified && N4 == unspecified &&
75  N3 == unspecified && N2 == unspecified && N1 == unspecified)
76  ? 1
77  : ((N6 == unspecified && N5 == unspecified &&
78  N4 == unspecified && N3 == unspecified &&
79  N2 == unspecified)
80  ? 2
81  : ((N6 == unspecified && N5 == unspecified &&
82  N4 == unspecified && N3 == unspecified)
83  ? 3
84  : ((N6 == unspecified && N5 == unspecified &&
85  N4 == unspecified)
86  ? 4
87  : ((N6 == unspecified &&
88  N5 == unspecified)
89  ? 5
90  : ((N6 == unspecified)
91  ? 6
92  : 7)))))));
93  }
94 
95  // Compute the rank of the view from the nonzero layout arguments.
96  template <typename Layout>
97  KOKKOS_INLINE_FUNCTION static size_t computeRank(const Layout& layout) {
98  return computeRank(layout.dimension[0], layout.dimension[1],
99  layout.dimension[2], layout.dimension[3],
100  layout.dimension[4], layout.dimension[5],
101  layout.dimension[6], layout.dimension[7]);
102  }
103 
104  // Extra overload to match that for specialize types v2
105  template <typename Layout, typename... P>
106  KOKKOS_INLINE_FUNCTION static size_t computeRank(
107  const Kokkos::Impl::ViewCtorProp<P...>& /* prop */,
108  const Layout& layout) {
109  return computeRank(layout);
110  }
111 
112  // Create the layout for the rank-7 view.
113  // Because the underlying View is rank-7, preserve "unspecified" for
114  // dimension 8.
115 
116  // Non-strided Layout
117  template <typename Layout>
118  KOKKOS_INLINE_FUNCTION static std::enable_if_t<
119  (std::is_same_v<Layout, Kokkos::LayoutRight> ||
120  std::is_same_v<Layout, Kokkos::LayoutLeft>),
121  Layout>
122  createLayout(const Layout& layout) {
123  Layout new_layout(
124  layout.dimension[0] != unspecified ? layout.dimension[0] : 1,
125  layout.dimension[1] != unspecified ? layout.dimension[1] : 1,
126  layout.dimension[2] != unspecified ? layout.dimension[2] : 1,
127  layout.dimension[3] != unspecified ? layout.dimension[3] : 1,
128  layout.dimension[4] != unspecified ? layout.dimension[4] : 1,
129  layout.dimension[5] != unspecified ? layout.dimension[5] : 1,
130  layout.dimension[6] != unspecified ? layout.dimension[6] : 1,
131  layout.dimension[7] != unspecified ? layout.dimension[7] : unspecified);
132 
133 #ifndef KOKKOS_ENABLE_IMPL_VIEW_LEGACY
134  // In order to have a valid LayoutRight stride when Sacado passes through
135  // extra integer arguments, we need to set it to the dimension[6] for the
136  // rank-7 view coming out of here. Only if the original layout was already
137  // rank-7 we can preserve the stride.
138  // FIXME_SACADO
139  if constexpr (!std::is_same_v<Specialize, void> &&
140  std::is_same_v<Layout, Kokkos::LayoutRight>) {
141  if (layout.dimension[6] == unspecified) {
142  new_layout.stride = unspecified;
143  } else {
144  new_layout.stride = layout.stride;
145  }
146  } else
147 #endif
148  new_layout.stride = layout.stride;
149  return new_layout;
150  }
151 
152  // LayoutStride
153  template <typename Layout>
154  KOKKOS_INLINE_FUNCTION static std::enable_if_t<
155  (std::is_same_v<Layout, Kokkos::LayoutStride>), Layout>
156  createLayout(const Layout& layout) {
157  return Layout(
158  layout.dimension[0] != unspecified ? layout.dimension[0] : 1,
159  layout.stride[0],
160  layout.dimension[1] != unspecified ? layout.dimension[1] : 1,
161  layout.stride[1],
162  layout.dimension[2] != unspecified ? layout.dimension[2] : 1,
163  layout.stride[2],
164  layout.dimension[3] != unspecified ? layout.dimension[3] : 1,
165  layout.stride[3],
166  layout.dimension[4] != unspecified ? layout.dimension[4] : 1,
167  layout.stride[4],
168  layout.dimension[5] != unspecified ? layout.dimension[5] : 1,
169  layout.stride[5],
170  layout.dimension[6] != unspecified ? layout.dimension[6] : 1,
171  layout.stride[6],
172  layout.dimension[7] != unspecified ? layout.dimension[7] : unspecified,
173  layout.stride[7]);
174  }
175 
176  // Extra overload to match that for specialize types
177  template <typename Traits, typename... P>
178  KOKKOS_INLINE_FUNCTION static std::enable_if_t<
179  (std::is_same_v<typename Traits::array_layout, Kokkos::LayoutRight> ||
180  std::is_same_v<typename Traits::array_layout, Kokkos::LayoutLeft> ||
181  std::is_same_v<typename Traits::array_layout, Kokkos::LayoutStride>),
182  typename Traits::array_layout>
183  createLayout([[maybe_unused]] const Kokkos::Impl::ViewCtorProp<P...>& prop,
184  typename Traits::array_layout layout) {
185 // FIXME_SACADO this is only needed for special extra int treatment
186 #ifndef KOKKOS_ENABLE_IMPL_VIEW_LEGACY
187  if constexpr (Traits::impl_is_customized &&
188  !Kokkos::Impl::ViewCtorProp<P...>::has_accessor_arg) {
189  auto rank = computeRank(prop, layout) - 1;
190  layout.dimension[rank] = unspecified;
191  }
192 #endif
193  return createLayout(layout);
194  }
195 
196  // Create a view from the given dimension arguments.
197  // This is only necessary because the shmem constructor doesn't take a layout.
198  // NDE shmem View's are not compatible with the added view_alloc value_type
199  // / fad_dim deduction functionality
200  template <typename ViewType, typename ViewArg>
201  static ViewType createView(const ViewArg& arg, const size_t N0,
202  const size_t N1, const size_t N2, const size_t N3,
203  const size_t N4, const size_t N5, const size_t N6,
204  const size_t N7) {
205  return ViewType(arg, N0 != unspecified ? N0 : 1, N1 != unspecified ? N1 : 1,
206  N2 != unspecified ? N2 : 1, N3 != unspecified ? N3 : 1,
207  N4 != unspecified ? N4 : 1, N5 != unspecified ? N5 : 1,
208  N6 != unspecified ? N6 : 1, N7 != unspecified ? N7 : 1);
209  }
210 };
211 
212 // Non-strided Layout
213 template <typename Layout, typename iType>
214 KOKKOS_INLINE_FUNCTION std::enable_if_t<
215  (std::is_same_v<Layout, Kokkos::LayoutRight> ||
216  std::is_same_v<Layout, Kokkos::LayoutLeft>)&&std::is_integral_v<iType>,
217  Layout>
218 reconstructLayout(const Layout& layout, iType dynrank) {
219  return Layout(dynrank > 0 ? layout.dimension[0] : KOKKOS_INVALID_INDEX,
220  dynrank > 1 ? layout.dimension[1] : KOKKOS_INVALID_INDEX,
221  dynrank > 2 ? layout.dimension[2] : KOKKOS_INVALID_INDEX,
222  dynrank > 3 ? layout.dimension[3] : KOKKOS_INVALID_INDEX,
223  dynrank > 4 ? layout.dimension[4] : KOKKOS_INVALID_INDEX,
224  dynrank > 5 ? layout.dimension[5] : KOKKOS_INVALID_INDEX,
225  dynrank > 6 ? layout.dimension[6] : KOKKOS_INVALID_INDEX,
226  dynrank > 7 ? layout.dimension[7] : KOKKOS_INVALID_INDEX);
227 }
228 
229 // LayoutStride
230 template <typename Layout, typename iType>
231 KOKKOS_INLINE_FUNCTION std::enable_if_t<
232  (std::is_same_v<Layout, Kokkos::LayoutStride>)&&std::is_integral_v<iType>,
233  Layout>
234 reconstructLayout(const Layout& layout, iType dynrank) {
235  return Layout(dynrank > 0 ? layout.dimension[0] : KOKKOS_INVALID_INDEX,
236  dynrank > 0 ? layout.stride[0] : (0),
237  dynrank > 1 ? layout.dimension[1] : KOKKOS_INVALID_INDEX,
238  dynrank > 1 ? layout.stride[1] : (0),
239  dynrank > 2 ? layout.dimension[2] : KOKKOS_INVALID_INDEX,
240  dynrank > 2 ? layout.stride[2] : (0),
241  dynrank > 3 ? layout.dimension[3] : KOKKOS_INVALID_INDEX,
242  dynrank > 3 ? layout.stride[3] : (0),
243  dynrank > 4 ? layout.dimension[4] : KOKKOS_INVALID_INDEX,
244  dynrank > 4 ? layout.stride[4] : (0),
245  dynrank > 5 ? layout.dimension[5] : KOKKOS_INVALID_INDEX,
246  dynrank > 5 ? layout.stride[5] : (0),
247  dynrank > 6 ? layout.dimension[6] : KOKKOS_INVALID_INDEX,
248  dynrank > 6 ? layout.stride[6] : (0),
249  dynrank > 7 ? layout.dimension[7] : KOKKOS_INVALID_INDEX,
250  dynrank > 7 ? layout.stride[7] : (0));
251 }
252 
254 // Enhanced debug checking - most infrastructure matches that of functions in
255 // Kokkos_ViewMapping; additional checks for extra arguments beyond rank are 0
256 template <unsigned, typename iType0, class MapType>
257 KOKKOS_INLINE_FUNCTION bool dyn_rank_view_verify_operator_bounds(
258  const iType0&, const MapType&) {
259  return true;
260 }
261 
262 template <unsigned R, typename iType0, class MapType, typename iType1,
263  class... Args>
264 KOKKOS_INLINE_FUNCTION bool dyn_rank_view_verify_operator_bounds(
265  const iType0& rank, const MapType& map, const iType1& i, Args... args) {
266  if (static_cast<iType0>(R) < rank) {
267  return (size_t(i) < map.extent(R)) &&
268  dyn_rank_view_verify_operator_bounds<R + 1>(rank, map, args...);
269  } else if (i != 0) {
270  Kokkos::printf(
271  "DynRankView Debug Bounds Checking Error: at rank %u\n Extra "
272  "arguments beyond the rank must be zero \n",
273  R);
274  return (false) &&
275  dyn_rank_view_verify_operator_bounds<R + 1>(rank, map, args...);
276  } else {
277  return (true) &&
278  dyn_rank_view_verify_operator_bounds<R + 1>(rank, map, args...);
279  }
280 }
281 
282 template <unsigned, class MapType>
283 inline void dyn_rank_view_error_operator_bounds(char*, int, const MapType&) {}
284 
285 template <unsigned R, class MapType, class iType, class... Args>
286 inline void dyn_rank_view_error_operator_bounds(char* buf, int len,
287  const MapType& map,
288  const iType& i, Args... args) {
289  const int n = snprintf(
290  buf, len, " %ld < %ld %c", static_cast<unsigned long>(i),
291  static_cast<unsigned long>(map.extent(R)), (sizeof...(Args) ? ',' : ')'));
292  dyn_rank_view_error_operator_bounds<R + 1>(buf + n, len - n, map, args...);
293 }
294 
295 // op_rank = rank of the operator version that was called
296 template <typename MemorySpace, typename iType0, typename iType1, class MapType,
297  class... Args>
298 KOKKOS_INLINE_FUNCTION void dyn_rank_view_verify_operator_bounds(
299  const iType0& op_rank, const iType1& rank,
300  const Kokkos::Impl::SharedAllocationTracker& tracker, const MapType& map,
301  Args... args) {
302  if (static_cast<iType0>(rank) > op_rank) {
303  Kokkos::abort(
304  "DynRankView Bounds Checking Error: Need at least rank arguments to "
305  "the operator()");
306  }
307 
308  if (!dyn_rank_view_verify_operator_bounds<0>(rank, map, args...)) {
309  KOKKOS_IF_ON_HOST(
310  (enum {LEN = 1024}; char buffer[LEN];
311  const std::string label = tracker.template get_label<MemorySpace>();
312  int n = snprintf(buffer, LEN, "DynRankView bounds error of view %s (",
313  label.c_str());
314  dyn_rank_view_error_operator_bounds<0>(buffer + n, LEN - n, map,
315  args...);
316  Kokkos::Impl::throw_runtime_exception(std::string(buffer));))
317 
318  KOKKOS_IF_ON_DEVICE(
319  ((void)tracker; Kokkos::abort("DynRankView bounds error");))
320  }
321 }
322 
325 
326 } // namespace Impl
327 
328 namespace Impl {
329 
330 template <class DstTraits, class SrcTraits>
331 class ViewMapping<
332  DstTraits, SrcTraits,
333  std::enable_if_t<
334  (std::is_same_v<typename DstTraits::memory_space,
335  typename SrcTraits::memory_space> &&
336  std::is_void_v<typename DstTraits::specialize> &&
337  std::is_void_v<typename SrcTraits::specialize> &&
338  (std::is_same_v<typename DstTraits::array_layout,
339  typename SrcTraits::array_layout> ||
340  ((std::is_same_v<typename DstTraits::array_layout,
341  Kokkos::LayoutLeft> ||
342  std::is_same_v<typename DstTraits::array_layout,
343  Kokkos::LayoutRight> ||
344  std::is_same_v<
345  typename DstTraits::array_layout,
346  Kokkos::LayoutStride>)&&(std::is_same_v<typename SrcTraits::
347  array_layout,
348  Kokkos::LayoutLeft> ||
349  std::is_same_v<
350  typename SrcTraits::array_layout,
351  Kokkos::LayoutRight> ||
352  std::is_same_v<
353  typename SrcTraits::array_layout,
354  Kokkos::LayoutStride>)))),
355  Kokkos::Impl::ViewToDynRankViewTag>> {
356  private:
357  enum {
358  is_assignable_value_type =
359  std::is_same_v<typename DstTraits::value_type,
360  typename SrcTraits::value_type> ||
361  std::is_same_v<typename DstTraits::value_type,
362  typename SrcTraits::const_value_type>
363  };
364 
365  enum {
366  is_assignable_layout =
367  std::is_same_v<typename DstTraits::array_layout,
368  typename SrcTraits::array_layout> ||
369  std::is_same_v<typename DstTraits::array_layout, Kokkos::LayoutStride>
370  };
371 
372  public:
373  enum { is_assignable = is_assignable_value_type && is_assignable_layout };
374 
375  using DstType = ViewMapping<DstTraits, typename DstTraits::specialize>;
376  using SrcType = ViewMapping<SrcTraits, typename SrcTraits::specialize>;
377 
378  template <typename DT, typename... DP, typename ST, typename... SP>
379  KOKKOS_INLINE_FUNCTION static void assign(
380  Kokkos::DynRankView<DT, DP...>& dst, const Kokkos::View<ST, SP...>& src) {
381  static_assert(
382  is_assignable_value_type,
383  "View assignment must have same value type or const = non-const");
384 
385  static_assert(
386  is_assignable_layout,
387  "View assignment must have compatible layout or have rank <= 1");
388 
389  // Removed dimension checks...
390 
391  using dst_offset_type = typename DstType::offset_type;
392  dst.m_map.m_impl_offset = dst_offset_type(
393  std::integral_constant<unsigned, 0>(),
394  src.layout()); // Check this for integer input1 for padding, etc
395  dst.m_map.m_impl_handle = Kokkos::Impl::ViewDataHandle<DstTraits>::assign(
396  src.m_map.m_impl_handle, src.m_track.m_tracker);
397  dst.m_track.m_tracker.assign(src.m_track.m_tracker, DstTraits::is_managed);
398  dst.m_rank = Kokkos::View<ST, SP...>::rank();
399  }
400 };
401 
402 } // namespace Impl
403 
404 /* \class DynRankView
405  * \brief Container that creates a Kokkos view with rank determined at runtime.
406  * Essentially this is a rank 7 view
407  *
408  * Changes from View
409  * 1. The rank of the DynRankView is returned by the method rank()
410  * 2. Max rank of a DynRankView is 7
411  * 3. subview called with 'subview(...)' or 'subdynrankview(...)' (backward
412  * compatibility)
413  * 4. Every subview is returned with LayoutStride
414  * 5. Copy and Copy-Assign View to DynRankView
415  * 6. deep_copy between Views and DynRankViews
416  * 7. rank( view ); returns the rank of View or DynRankView
417  *
418  */
419 
420 template <class>
421 struct is_dyn_rank_view : public std::false_type {};
422 
423 template <class D, class... P>
424 struct is_dyn_rank_view<Kokkos::DynRankView<D, P...>> : public std::true_type {
425 };
426 
427 template <class T>
428 inline constexpr bool is_dyn_rank_view_v = is_dyn_rank_view<T>::value;
429 
430 // Inherit privately from View, this way we don't import anything funky
431 // for example the rank member vs the rank() function of DynRankView
432 template <typename DataType, class... Properties>
433 class DynRankView : private View<DataType*******, Properties...> {
434  static_assert(!std::is_array_v<DataType> && !std::is_pointer_v<DataType>,
435  "Cannot template DynRankView with array or pointer datatype - "
436  "must be pod");
437 
438  private:
439  template <class, class...>
440  friend class DynRankView;
441  template <class, class...>
442  friend class Kokkos::Impl::ViewMapping;
443 
444  size_t m_rank{};
445 
446  public:
447  using drvtraits = ViewTraits<DataType, Properties...>;
448 
449  using view_type = View<DataType*******, Properties...>;
450 
451  private:
452 #ifdef KOKKOS_ENABLE_IMPL_VIEW_LEGACY
453  using drdtraits = Impl::DynRankDimTraits<typename view_type::specialize>;
454 #else
455  using drdtraits = Impl::DynRankDimTraits<
456  std::conditional_t<view_type::traits::impl_is_customized, bool, void>>;
457 #endif
458  public:
459  // typedefs from ViewTraits, overriden
460  using data_type = typename drvtraits::data_type;
461  using const_data_type = typename drvtraits::const_data_type;
462  using non_const_data_type = typename drvtraits::non_const_data_type;
463 
464  // typedefs from ViewTraits not overriden
465  using value_type = typename view_type::value_type;
466  using const_value_type = typename view_type::const_value_type;
467  using non_const_value_type = typename view_type::non_const_value_type;
468  using traits = typename view_type::traits;
469  using array_layout = typename view_type::array_layout;
470 
471  using execution_space = typename view_type::execution_space;
472  using memory_space = typename view_type::memory_space;
473  using device_type = typename view_type::device_type;
474 
475  using memory_traits = typename view_type::memory_traits;
476  using host_mirror_space = typename view_type::host_mirror_space;
477  using size_type = typename view_type::size_type;
478 
479  using reference_type = typename view_type::reference_type;
480  using pointer_type = typename view_type::pointer_type;
481 
482  using scalar_array_type = value_type;
483  using const_scalar_array_type = const_value_type;
484  using non_const_scalar_array_type = non_const_value_type;
485 #ifndef KOKKOS_ENABLE_IMPL_VIEW_LEGACY
486 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
487  using specialize KOKKOS_DEPRECATED = void;
488 #endif
489 #else
490  using specialize = typename view_type::specialize;
491 #endif
492 
493  // typedefs in View for mdspan compatibility
494  // cause issues with MSVC+CUDA
495  // using layout_type = typename view_type::layout_type;
496  using index_type = typename view_type::index_type;
497  using element_type = typename view_type::element_type;
498  using rank_type = typename view_type::rank_type;
499  using reference = reference_type;
500  using data_handle_type = pointer_type;
501 
502  KOKKOS_FUNCTION
503  view_type& DownCast() const { return (view_type&)(*this); }
504 
505  // FIXME: this function make NO sense, the above one already is marked const
506  // Maybe one would want to get back a view of const??
507  KOKKOS_FUNCTION
508  const view_type& ConstDownCast() const { return (const view_type&)(*this); }
509 
510  // FIXME: deprecate DownCast in favor of to_view
511  // KOKKOS_FUNCTION
512  // view_type to_view() const { return *this; }
513 
514  // Types below - at least the HostMirror requires the value_type, NOT the rank
515  // 7 data_type of the traits
516 
518  using array_type = DynRankView<
519  typename drvtraits::scalar_array_type, typename drvtraits::array_layout,
520  typename drvtraits::device_type, typename drvtraits::memory_traits>;
521 
523  using const_type = DynRankView<
524  typename drvtraits::const_data_type, typename drvtraits::array_layout,
525  typename drvtraits::device_type, typename drvtraits::memory_traits>;
526 
528  using non_const_type = DynRankView<
529  typename drvtraits::non_const_data_type, typename drvtraits::array_layout,
530  typename drvtraits::device_type, typename drvtraits::memory_traits>;
531 
533  using HostMirror = DynRankView<typename drvtraits::non_const_data_type,
534  typename drvtraits::array_layout,
535  typename drvtraits::host_mirror_space>;
536 
537  using host_mirror_type = HostMirror;
538  //----------------------------------------
539  // Domain rank and extents
540 
541  // enum { Rank = map_type::Rank }; //Will be dyn rank of 7 always, keep the
542  // enum?
543 
544  //----------------------------------------
545  /* Deprecate all 'dimension' functions in favor of
546  * ISO/C++ vocabulary 'extent'.
547  */
548 
549  //----------------------------------------
550 
551  private:
552  enum {
553  is_layout_left =
554  std::is_same_v<typename traits::array_layout, Kokkos::LayoutLeft>,
555 
556  is_layout_right =
557  std::is_same_v<typename traits::array_layout, Kokkos::LayoutRight>,
558 
559  is_layout_stride =
560  std::is_same_v<typename traits::array_layout, Kokkos::LayoutStride>,
561 
562  is_default_map = std::is_void_v<typename traits::specialize> &&
563  (is_layout_left || is_layout_right || is_layout_stride),
564 
565  is_default_access =
566  is_default_map && std::is_same_v<reference_type, element_type&>
567  };
568 
569 // Bounds checking macros
570 #if defined(KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK)
571 
572 // rank of the calling operator - included as first argument in ARG
573 #define KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(ARG) \
574  Kokkos::Impl::runtime_check_memory_access_violation< \
575  typename traits::memory_space>( \
576  "Kokkos::DynRankView ERROR: attempt to access inaccessible memory " \
577  "space"); \
578  Kokkos::Impl::dyn_rank_view_verify_operator_bounds< \
579  typename traits::memory_space> \
580  ARG;
581 
582 #else
583 
584 #define KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(ARG) \
585  Kokkos::Impl::runtime_check_memory_access_violation< \
586  typename traits::memory_space>( \
587  "Kokkos::DynRankView ERROR: attempt to access inaccessible memory " \
588  "space");
589 
590 #endif
591 
592  public:
593  KOKKOS_FUNCTION
594  constexpr unsigned rank() const { return m_rank; }
595 
596 #ifndef KOKKOS_ENABLE_IMPL_VIEW_LEGACY
597  using view_type::accessor; // FIXME: not tested
598  using view_type::mapping; // FIXME: not tested
599 #endif
600  using view_type::data;
601  using view_type::extent;
602  using view_type::extent_int; // FIXME: not tested
603  using view_type::impl_map; // FIXME: not tested
604  using view_type::is_allocated;
605  using view_type::label;
606  using view_type::size;
607  using view_type::span;
608  using view_type::span_is_contiguous; // FIXME: not tested
609  using view_type::stride; // FIXME: not tested
610  using view_type::stride_0; // FIXME: not tested
611  using view_type::stride_1; // FIXME: not tested
612  using view_type::stride_2; // FIXME: not tested
613  using view_type::stride_3; // FIXME: not tested
614  using view_type::stride_4; // FIXME: not tested
615  using view_type::stride_5; // FIXME: not tested
616  using view_type::stride_6; // FIXME: not tested
617  using view_type::stride_7; // FIXME: not tested
618  using view_type::use_count;
619 
620 #ifdef KOKKOS_ENABLE_CUDA
621  KOKKOS_FUNCTION reference_type
622  operator()(index_type i0 = 0, index_type i1 = 0, index_type i2 = 0,
623  index_type i3 = 0, index_type i4 = 0, index_type i5 = 0,
624  index_type i6 = 0) const {
625  return view_type::operator()(i0, i1, i2, i3, i4, i5, i6);
626  }
627 #else
628  // Adding shortcut operators for rank-0 to rank-3 for default layouts
629  // and access modalities.
630  // This removes performance overhead for always using rank-7 mapping.
631  // See https://github.com/kokkos/kokkos/issues/7604
632  // When boundschecking is enabled we still go through the underlying
633  // rank-7 View to leverage the error checks there.
634 
635  KOKKOS_FUNCTION reference_type operator()() const {
636 #ifdef KOKKOS_ENABLE_DEBUG
637  if (rank() != 0u)
638  Kokkos::abort(
639  "DynRankView rank 0 operator() called with invalid number of "
640  "arguments.");
641 #endif
642 #ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
643  if constexpr (is_default_access) {
644  return view_type::data()[0];
645  } else
646 #endif
647  {
648  return view_type::operator()(0, 0, 0, 0, 0, 0, 0);
649  }
650  }
651 
652  KOKKOS_FUNCTION reference_type operator()(index_type i0) const {
653 #ifdef KOKKOS_ENABLE_DEBUG
654  // FIXME: Should be equal, only access(...) allows mismatch of rank and
655  // index args
656  if (rank() > 1u)
657  Kokkos::abort(
658  "DynRankView rank 1 operator() called with invalid number of "
659  "arguments.");
660 #endif
661 #ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
662  if constexpr (is_default_access) {
663  if constexpr (is_layout_stride) {
664  return view_type::data()[i0 * view_type::stride(0)];
665  } else {
666  return view_type::data()[i0];
667  }
668  } else
669 #endif
670  {
671  return view_type::operator()(i0, 0, 0, 0, 0, 0, 0);
672  }
673 #if defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
674  !defined(KOKKOS_COMPILER_MSVC)
675  __builtin_unreachable();
676 #endif
677  }
678 
679  KOKKOS_FUNCTION reference_type operator()(index_type i0,
680  index_type i1) const {
681 #ifdef KOKKOS_ENABLE_DEBUG
682  // FIXME: Should be equal, only access(...) allows mismatch of rank and
683  // index args
684  if (rank() > 2u)
685  Kokkos::abort(
686  "DynRankView rank 2 operator() called with invalid number of "
687  "arguments.");
688 #endif
689 #ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
690  if constexpr (is_default_access) {
691  if constexpr (is_layout_left) {
692  return view_type::data()[i0 + i1 * view_type::stride(1)];
693  } else if constexpr (is_layout_right) {
694  return view_type::data()[i0 * view_type::extent(1) + i1];
695  } else {
696  return view_type::data()[i0 * view_type::stride(0) +
697  i1 * view_type::stride(1)];
698  }
699  } else
700 #endif
701  {
702  return view_type::operator()(i0, i1, 0, 0, 0, 0, 0);
703  }
704 #if defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
705  !defined(KOKKOS_COMPILER_MSVC)
706  __builtin_unreachable();
707 #endif
708  }
709 
710  KOKKOS_FUNCTION reference_type operator()(index_type i0, index_type i1,
711  index_type i2) const {
712 #ifdef KOKKOS_ENABLE_DEBUG
713  // FIXME: Should be equal, only access(...) allows mismatch of rank and
714  // index args
715  if (rank() > 3u)
716  Kokkos::abort(
717  "DynRankView rank 3 operator() called with invalid number of "
718  "arguments.");
719 #endif
720 #ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
721  if constexpr (is_default_access) {
722  if constexpr (is_layout_left) {
723  return view_type::data()[i0 + view_type::stride(1) *
724  (i1 + i2 * view_type::extent(1))];
725  } else if constexpr (is_layout_right) {
726  return view_type::data()[(i0 * view_type::extent(1) + i1) *
727  view_type::extent(2) +
728  i2];
729  } else {
730  return view_type::data()[i0 * view_type::stride(0) +
731  i1 * view_type::stride(1) +
732  i2 * view_type::stride(2)];
733  }
734  } else
735 #endif
736  {
737  return view_type::operator()(i0, i1, i2, 0, 0, 0, 0);
738  }
739 #if defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
740  !defined(KOKKOS_COMPILER_MSVC)
741  __builtin_unreachable();
742 #endif
743  }
744 
745  KOKKOS_FUNCTION reference_type operator()(index_type i0, index_type i1,
746  index_type i2, index_type i3,
747  index_type i4 = 0,
748  index_type i5 = 0,
749  index_type i6 = 0) const {
750  return view_type::operator()(i0, i1, i2, i3, i4, i5, i6);
751  }
752 #endif
753 
754 // This is an accomodation for Phalanx, that is usint the operator[] to access
755 // all elements in a linear fashion even when the rank is not 1
756 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
757  KOKKOS_FUNCTION reference_type operator[](index_type i0) const {
758  if constexpr (std::is_same_v<typename drvtraits::value_type,
759  typename drvtraits::scalar_array_type>) {
760  return view_type::data()[i0];
761  } else {
762  const size_t dim_scalar = view_type::impl_map().dimension_scalar();
763  const size_t bytes = view_type::span() / dim_scalar;
764 
765  using tmp_view_type =
766  Kokkos::View<DataType*, typename traits::array_layout,
767  typename traits::device_type,
768  Kokkos::MemoryTraits<traits::memory_traits::impl_value |
769  unsigned(Kokkos::Unmanaged)>>;
770  tmp_view_type rankone_view(view_type::data(), bytes, dim_scalar);
771  return rankone_view(i0);
772  }
773  }
774 #else
775  KOKKOS_FUNCTION reference_type operator[](index_type i0) const {
776 #ifdef KOKKOS_ENABLE_DEBUG
777  if (rank() != 1u)
778  Kokkos::abort("DynRankView operator[] can only be used for rank-1");
779 #endif
780  return view_type::operator()(i0, 0, 0, 0, 0, 0, 0);
781  }
782 #endif
783 
784  KOKKOS_FUNCTION reference_type access(index_type i0 = 0, index_type i1 = 0,
785  index_type i2 = 0, index_type i3 = 0,
786  index_type i4 = 0, index_type i5 = 0,
787  index_type i6 = 0) const {
788  return view_type::operator()(i0, i1, i2, i3, i4, i5, i6);
789  }
790 
791  //----------------------------------------
792  // Standard constructor, destructor, and assignment operators...
793 
794  KOKKOS_DEFAULTED_FUNCTION
795  ~DynRankView() = default;
796 
797  KOKKOS_DEFAULTED_FUNCTION DynRankView() = default;
798 
799  //----------------------------------------
800  // Compatible view copy constructor and assignment
801  // may assign unmanaged from managed.
802  // Make this conditionally explicit?
803  template <class RT, class... RP>
804  KOKKOS_FUNCTION DynRankView(const DynRankView<RT, RP...>& rhs)
805  : view_type(rhs), m_rank(rhs.m_rank) {}
806 
807  template <class RT, class... RP>
808  KOKKOS_FUNCTION DynRankView& operator=(const DynRankView<RT, RP...>& rhs) {
809  view_type::operator=(rhs);
810  m_rank = rhs.m_rank;
811  return *this;
812  }
813 
814 #ifndef KOKKOS_ENABLE_IMPL_VIEW_LEGACY
815  private:
816  template <class Ext>
817  KOKKOS_FUNCTION typename view_type::extents_type create_rank7_extents(
818  const Ext& ext) {
819  return typename view_type::extents_type(
820  ext.rank() > 0 ? ext.extent(0) : 1, ext.rank() > 1 ? ext.extent(1) : 1,
821  ext.rank() > 2 ? ext.extent(2) : 1, ext.rank() > 3 ? ext.extent(3) : 1,
822  ext.rank() > 4 ? ext.extent(4) : 1, ext.rank() > 5 ? ext.extent(5) : 1,
823  ext.rank() > 6 ? ext.extent(6) : 1);
824  }
825 
826  public:
827  // Copy/Assign View to DynRankView
828  template <class RT, class... RP>
829  KOKKOS_INLINE_FUNCTION DynRankView(const View<RT, RP...>& rhs,
830  size_t new_rank)
831  : view_type(rhs.data_handle(),
832  Impl::mapping_from_array_layout<
833  typename view_type::mdspan_type::mapping_type>(
834  drdtraits::createLayout(rhs.layout())),
835  rhs.accessor()),
836  m_rank(new_rank) {
837  if (new_rank > View<RT, RP...>::rank())
838  Kokkos::abort(
839  "Attempting to construct DynRankView from View and new rank, with "
840  "the new rank being too large.");
841  }
842 
843  template <class RT, class... RP>
844  KOKKOS_INLINE_FUNCTION DynRankView& operator=(const View<RT, RP...>& rhs) {
845  view_type::operator=(
846  view_type(rhs.data_handle(),
847  Impl::mapping_from_array_layout<
848  typename view_type::mdspan_type::mapping_type>(
849  drdtraits::createLayout(rhs.layout())),
850  rhs.accessor()));
851  m_rank = rhs.rank();
852  return *this;
853  }
854 #else
855  template <class RT, class... RP>
856  KOKKOS_FUNCTION DynRankView(const View<RT, RP...>& rhs, size_t new_rank) {
857  using SrcTraits = typename View<RT, RP...>::traits;
858  using Mapping =
859  Kokkos::Impl::ViewMapping<traits, SrcTraits,
861  static_assert(Mapping::is_assignable,
862  "Incompatible View to DynRankView copy assignment");
863  if (new_rank > View<RT, RP...>::rank())
864  Kokkos::abort(
865  "Attempting to construct DynRankView from View and new rank, with "
866  "the new rank being too large.");
867  Mapping::assign(*this, rhs);
868  m_rank = new_rank;
869  }
870 
871  template <class RT, class... RP>
872  KOKKOS_FUNCTION DynRankView& operator=(const View<RT, RP...>& rhs) {
873  using SrcTraits = typename View<RT, RP...>::traits;
874  using Mapping =
875  Kokkos::Impl::ViewMapping<traits, SrcTraits,
876  Kokkos::Impl::ViewToDynRankViewTag>;
877  static_assert(Mapping::is_assignable,
878  "Incompatible View to DynRankView copy assignment");
879  Mapping::assign(*this, rhs);
880  m_rank = View<RT, RP...>::rank();
881  return *this;
882  }
883 #endif
884 
885  template <class RT, class... RP>
886  KOKKOS_FUNCTION DynRankView(const View<RT, RP...>& rhs)
887  : DynRankView(rhs, View<RT, RP...>::rank()) {}
888 
889  //----------------------------------------
890  // Allocation tracking properties
891 
892  //----------------------------------------
893  // Allocation according to allocation properties and array layout
894  // unused arg_layout dimensions must be set to KOKKOS_INVALID_INDEX so that
895  // rank deduction can properly take place
896  // We need two variants to avoid calling host function from host device
897  // function warnings
898 
899 #ifndef KOKKOS_ENABLE_IMPL_VIEW_LEGACY
900  private:
901  // Need a host only and a host/device function to deal with labels.
902  template <class... P>
903  KOKKOS_FUNCTION auto attach_accessor_arg_if_needed(
904  const Impl::ViewCtorProp<P...>& arg_prop,
905  std::enable_if_t<((!std::is_same_v<P, std::string>)&&...),
906  const typename traits::array_layout&>
907  layout) {
908  if constexpr (traits::impl_is_customized) {
909  int r = 0;
910  while (r < 7 && layout.dimension[r] != KOKKOS_INVALID_INDEX) r++;
911 
912  // Can't use with_properties_if_unset since its a host only function!
913  return view_wrap(
914  static_cast<const Impl::ViewCtorProp<void, P>&>(arg_prop).value...,
915  Impl::AccessorArg_t{r > 0 ? size_t(layout.dimension[r - 1]) : 0ul});
916  } else {
917  return arg_prop;
918  }
919  }
920  template <class... P>
921  auto attach_accessor_arg_if_needed(
922  const Impl::ViewCtorProp<P...>& arg_prop,
923  std::enable_if_t<(std::is_same_v<P, std::string> || ...),
924  const typename traits::array_layout&>
925  layout) {
926  if constexpr (traits::impl_is_customized &&
927  !Impl::ViewCtorProp<P...>::has_accessor_arg) {
928  int r = 0;
929  while (r < 7 && layout.dimension[r] != KOKKOS_INVALID_INDEX) r++;
930  // Could use with_properties_if_unset, but rather keep same as above.
931  return view_alloc(
932  static_cast<const Impl::ViewCtorProp<void, P>&>(arg_prop).value...,
933  Impl::AccessorArg_t{r > 0 ? size_t(layout.dimension[r - 1]) : 0ul});
934  } else {
935  return arg_prop;
936  }
937  }
938 
939  public:
940 #endif
941 
942  // With NVCC 11.0 and 11.2 (and others likely) using GCC 8.5 a DynRankView
943  // test fails at runtime where construction from layout drops some extents.
944  // The bug goes away with O1.
945  // FIXME: NVCC GCC8 optimization bug DynRankView
946 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOS_COMPILER_GNU)
947 #if KOKKOS_COMPILER_GNU < 900
948 #define KOKKOS_IMPL_SKIP_OPTIMIZATION
949 #endif
950 #endif
951 
952 #ifdef KOKKOS_IMPL_SKIP_OPTIMIZATION
953 // Also need to suppress warning about unrecognized GCC optimize pragma
954 #pragma push
955 #pragma diag_suppress = unrecognized_gcc_pragma
956 #pragma GCC push_options
957 #pragma GCC optimize("O1")
958 #endif
959  template <class... P>
960  explicit KOKKOS_FUNCTION DynRankView(
961  const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
962  std::enable_if_t<Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
963  typename traits::array_layout const&>
964  arg_layout)
965 #ifndef KOKKOS_ENABLE_IMPL_VIEW_LEGACY
966  : view_type(attach_accessor_arg_if_needed(arg_prop, arg_layout),
967  drdtraits::template createLayout<traits, P...>(arg_prop,
968  arg_layout)),
969  m_rank(drdtraits::computeRank(arg_prop, arg_layout) -
970  (traits::impl_is_customized ? 1 : 0)){}
971 #else
972  : view_type(arg_prop, drdtraits::template createLayout<traits, P...>(
973  arg_prop, arg_layout)),
974  m_rank(drdtraits::computeRank(arg_prop, arg_layout)) {
975  }
976 #endif
977 
978  template <class... P>
979  explicit DynRankView(
980  const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
981  std::enable_if_t<!Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
982  typename traits::array_layout const&>
983  arg_layout)
984 #ifndef KOKKOS_ENABLE_IMPL_VIEW_LEGACY
985  : view_type(attach_accessor_arg_if_needed(arg_prop, arg_layout),
986  drdtraits::template createLayout<traits, P...>(arg_prop,
987  arg_layout)),
988  m_rank(drdtraits::computeRank(arg_prop, arg_layout) -
989  (traits::impl_is_customized &&
990  !Kokkos::Impl::ViewCtorProp<P...>::has_accessor_arg
991  ? 1
992  : 0)){}
993 #else
994  : view_type(arg_prop, drdtraits::template createLayout<traits, P...>(
995  arg_prop, arg_layout)),
996  m_rank(drdtraits::computeRank(arg_prop, arg_layout)) {
997  }
998 #endif
999 
1000 #ifdef KOKKOS_IMPL_SKIP_OPTIMIZATION
1001 #pragma GCC pop_options
1002 #pragma pop
1003 #undef KOKKOS_IMPL_SKIP_OPTIMIZATION
1004 #endif
1005 
1006  //----------------------------------------
1007  // Constructor(s)
1008 
1009  // Simple dimension-only layout
1010  // We need two variants to avoid calling host function from host device
1011  // function warnings
1012  template <class... P>
1013  explicit KOKKOS_FUNCTION DynRankView(
1014  const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
1015  std::enable_if_t<Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
1016  const size_t>
1017  arg_N0 = KOKKOS_INVALID_INDEX,
1018  const size_t arg_N1 = KOKKOS_INVALID_INDEX,
1019  const size_t arg_N2 = KOKKOS_INVALID_INDEX,
1020  const size_t arg_N3 = KOKKOS_INVALID_INDEX,
1021  const size_t arg_N4 = KOKKOS_INVALID_INDEX,
1022  const size_t arg_N5 = KOKKOS_INVALID_INDEX,
1023  const size_t arg_N6 = KOKKOS_INVALID_INDEX,
1024  const size_t arg_N7 = KOKKOS_INVALID_INDEX)
1025  : DynRankView(arg_prop, typename traits::array_layout(
1026  arg_N0, arg_N1, arg_N2, arg_N3, arg_N4,
1027  arg_N5, arg_N6, arg_N7)) {
1028  }
1029 
1030  template <class... P>
1031  explicit DynRankView(
1032  const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
1033  std::enable_if_t<!Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
1034  const size_t>
1035  arg_N0 = KOKKOS_INVALID_INDEX,
1036  const size_t arg_N1 = KOKKOS_INVALID_INDEX,
1037  const size_t arg_N2 = KOKKOS_INVALID_INDEX,
1038  const size_t arg_N3 = KOKKOS_INVALID_INDEX,
1039  const size_t arg_N4 = KOKKOS_INVALID_INDEX,
1040  const size_t arg_N5 = KOKKOS_INVALID_INDEX,
1041  const size_t arg_N6 = KOKKOS_INVALID_INDEX,
1042  const size_t arg_N7 = KOKKOS_INVALID_INDEX)
1043  : DynRankView(arg_prop, typename traits::array_layout(
1044  arg_N0, arg_N1, arg_N2, arg_N3, arg_N4,
1045  arg_N5, arg_N6, arg_N7)) {}
1046 
1047  // Allocate with label and layout
1048  template <typename Label>
1049  explicit inline DynRankView(
1050  const Label& arg_label,
1051  std::enable_if_t<Kokkos::Impl::is_view_label<Label>::value,
1052  typename traits::array_layout> const& arg_layout)
1053  : DynRankView(Kokkos::Impl::ViewCtorProp<std::string>(arg_label),
1054  arg_layout) {}
1055 
1056  // Allocate label and layout, must disambiguate from subview constructor
1057  template <typename Label>
1058  explicit inline DynRankView(
1059  const Label& arg_label,
1060  std::enable_if_t<Kokkos::Impl::is_view_label<Label>::value, const size_t>
1061  arg_N0 = KOKKOS_INVALID_INDEX,
1062  const size_t arg_N1 = KOKKOS_INVALID_INDEX,
1063  const size_t arg_N2 = KOKKOS_INVALID_INDEX,
1064  const size_t arg_N3 = KOKKOS_INVALID_INDEX,
1065  const size_t arg_N4 = KOKKOS_INVALID_INDEX,
1066  const size_t arg_N5 = KOKKOS_INVALID_INDEX,
1067  const size_t arg_N6 = KOKKOS_INVALID_INDEX,
1068  const size_t arg_N7 = KOKKOS_INVALID_INDEX)
1069  : DynRankView(
1070  Kokkos::Impl::ViewCtorProp<std::string>(arg_label),
1071  typename traits::array_layout(arg_N0, arg_N1, arg_N2, arg_N3,
1072  arg_N4, arg_N5, arg_N6, arg_N7)) {}
1073 
1074  //----------------------------------------
1075  // Memory span required to wrap these dimensions.
1076  // FIXME: this function needs to be tested
1077  static constexpr size_t required_allocation_size(
1078  const size_t arg_N0 = 1, const size_t arg_N1 = 1, const size_t arg_N2 = 1,
1079  const size_t arg_N3 = 1, const size_t arg_N4 = 1, const size_t arg_N5 = 1,
1080  const size_t arg_N6 = 1,
1081  [[maybe_unused]] const size_t arg_N7 = KOKKOS_INVALID_INDEX) {
1082  // FIXME: check that arg_N7 is not set by user (in debug mode)
1083  return view_type::required_allocation_size(arg_N0, arg_N1, arg_N2, arg_N3,
1084  arg_N4, arg_N5, arg_N6);
1085  }
1086 
1087  explicit KOKKOS_FUNCTION DynRankView(
1088  typename view_type::pointer_type arg_ptr,
1089  const size_t arg_N0 = KOKKOS_INVALID_INDEX,
1090  const size_t arg_N1 = KOKKOS_INVALID_INDEX,
1091  const size_t arg_N2 = KOKKOS_INVALID_INDEX,
1092  const size_t arg_N3 = KOKKOS_INVALID_INDEX,
1093  const size_t arg_N4 = KOKKOS_INVALID_INDEX,
1094  const size_t arg_N5 = KOKKOS_INVALID_INDEX,
1095  const size_t arg_N6 = KOKKOS_INVALID_INDEX,
1096  const size_t arg_N7 = KOKKOS_INVALID_INDEX)
1097  : DynRankView(
1098  Kokkos::Impl::ViewCtorProp<typename view_type::pointer_type>(
1099  arg_ptr),
1100  arg_N0, arg_N1, arg_N2, arg_N3, arg_N4, arg_N5, arg_N6, arg_N7) {}
1101 
1102  explicit KOKKOS_FUNCTION DynRankView(
1103  typename view_type::pointer_type arg_ptr,
1104  typename traits::array_layout& arg_layout)
1105  : DynRankView(
1106  Kokkos::Impl::ViewCtorProp<typename view_type::pointer_type>(
1107  arg_ptr),
1108  arg_layout) {}
1109 
1110  //----------------------------------------
1111  // Shared scratch memory constructor
1112 
1113  // Note: We must pass 7 valid args since view_type is rank 7
1114  static inline size_t shmem_size(
1115  const size_t arg_N0 = 1, const size_t arg_N1 = 1, const size_t arg_N2 = 1,
1116  const size_t arg_N3 = 1, const size_t arg_N4 = 1, const size_t arg_N5 = 1,
1117  const size_t arg_N6 = 1, const size_t arg_N7 = KOKKOS_INVALID_INDEX) {
1118  return view_type::shmem_size(arg_N0, arg_N1, arg_N2, arg_N3, arg_N4, arg_N5,
1119  arg_N6, arg_N7);
1120  }
1121 
1122  explicit KOKKOS_FUNCTION DynRankView(
1123  const typename traits::execution_space::scratch_memory_space& arg_space,
1124  const typename traits::array_layout& arg_layout)
1125  : view_type(arg_space, drdtraits::createLayout(arg_layout)),
1126  m_rank(drdtraits::computeRank(arg_layout)) {}
1127 
1128  explicit KOKKOS_FUNCTION DynRankView(
1129  const typename traits::execution_space::scratch_memory_space& arg_space,
1130  const size_t arg_N0 = KOKKOS_INVALID_INDEX,
1131  const size_t arg_N1 = KOKKOS_INVALID_INDEX,
1132  const size_t arg_N2 = KOKKOS_INVALID_INDEX,
1133  const size_t arg_N3 = KOKKOS_INVALID_INDEX,
1134  const size_t arg_N4 = KOKKOS_INVALID_INDEX,
1135  const size_t arg_N5 = KOKKOS_INVALID_INDEX,
1136  const size_t arg_N6 = KOKKOS_INVALID_INDEX,
1137  const size_t arg_N7 = KOKKOS_INVALID_INDEX)
1138 
1139  : DynRankView(arg_space, typename traits::array_layout(
1140  arg_N0, arg_N1, arg_N2, arg_N3, arg_N4,
1141  arg_N5, arg_N6, arg_N7)) {}
1142 
1143  KOKKOS_FUNCTION constexpr auto layout() const {
1144  switch (rank()) {
1145  case 0: return Impl::as_view_of_rank_n<0>(*this).layout();
1146  case 1: return Impl::as_view_of_rank_n<1>(*this).layout();
1147  case 2: return Impl::as_view_of_rank_n<2>(*this).layout();
1148  case 3: return Impl::as_view_of_rank_n<3>(*this).layout();
1149  case 4: return Impl::as_view_of_rank_n<4>(*this).layout();
1150  case 5: return Impl::as_view_of_rank_n<5>(*this).layout();
1151  case 6: return Impl::as_view_of_rank_n<6>(*this).layout();
1152  case 7: return Impl::as_view_of_rank_n<7>(*this).layout();
1153  default:
1154  KOKKOS_IF_ON_HOST(
1155  Kokkos::abort(
1156  std::string(
1157  "Calling DynRankView::layout on DRV of unexpected rank " +
1158  std::to_string(rank()))
1159  .c_str());)
1160  KOKKOS_IF_ON_DEVICE(
1161  Kokkos::abort(
1162  "Calling DynRankView::layout on DRV of unexpected rank");)
1163  }
1164  // control flow should never reach here
1165  return view_type::layout();
1166  }
1167 };
1168 
1169 template <typename D, class... P>
1170 KOKKOS_FUNCTION constexpr unsigned rank(const DynRankView<D, P...>& DRV) {
1171  return DRV.rank();
1172 } // needed for transition to common constexpr method in view and dynrankview
1173  // to return rank
1174 
1175 //----------------------------------------------------------------------------
1176 // Subview mapping.
1177 // Deduce destination view type from source view traits and subview arguments
1178 
1179 namespace Impl {
1180 
1181 struct DynRankSubviewTag {};
1182 
1183 } // namespace Impl
1184 
1185 template <class V, class... Args>
1186 using Subdynrankview =
1187  typename Kokkos::Impl::ViewMapping<Kokkos::Impl::DynRankSubviewTag, V,
1188  Args...>::ret_type;
1189 
1190 template <class... DRVArgs, class SubArg0 = int, class SubArg1 = int,
1191  class SubArg2 = int, class SubArg3 = int, class SubArg4 = int,
1192  class SubArg5 = int, class SubArg6 = int>
1193 KOKKOS_INLINE_FUNCTION auto subdynrankview(
1194  const DynRankView<DRVArgs...>& drv, SubArg0 arg0 = SubArg0{},
1195  SubArg1 arg1 = SubArg1{}, SubArg2 arg2 = SubArg2{},
1196  SubArg3 arg3 = SubArg3{}, SubArg4 arg4 = SubArg4{},
1197  SubArg5 arg5 = SubArg5{}, SubArg6 arg6 = SubArg6{}) {
1198  auto sub = subview(drv.DownCast(), arg0, arg1, arg2, arg3, arg4, arg5, arg6);
1199  using sub_t = decltype(sub);
1200  size_t new_rank = (drv.rank() > 0 && !std::is_integral_v<SubArg0> ? 1 : 0) +
1201  (drv.rank() > 1 && !std::is_integral_v<SubArg1> ? 1 : 0) +
1202  (drv.rank() > 2 && !std::is_integral_v<SubArg2> ? 1 : 0) +
1203  (drv.rank() > 3 && !std::is_integral_v<SubArg3> ? 1 : 0) +
1204  (drv.rank() > 4 && !std::is_integral_v<SubArg4> ? 1 : 0) +
1205  (drv.rank() > 5 && !std::is_integral_v<SubArg5> ? 1 : 0) +
1206  (drv.rank() > 6 && !std::is_integral_v<SubArg6> ? 1 : 0);
1207 
1208  using return_type =
1209  DynRankView<typename sub_t::value_type, Kokkos::LayoutStride,
1210  typename sub_t::device_type, typename sub_t::memory_traits>;
1211  return static_cast<return_type>(
1212  DynRankView<typename sub_t::value_type, typename sub_t::array_layout,
1213  typename sub_t::device_type, typename sub_t::memory_traits>(
1214  sub, new_rank));
1215 }
1216 template <class... DRVArgs, class SubArg0 = int, class SubArg1 = int,
1217  class SubArg2 = int, class SubArg3 = int, class SubArg4 = int,
1218  class SubArg5 = int, class SubArg6 = int>
1219 KOKKOS_INLINE_FUNCTION auto subview(
1220  const DynRankView<DRVArgs...>& drv, SubArg0 arg0 = SubArg0{},
1221  SubArg1 arg1 = SubArg1{}, SubArg2 arg2 = SubArg2{},
1222  SubArg3 arg3 = SubArg3{}, SubArg4 arg4 = SubArg4{},
1223  SubArg5 arg5 = SubArg5{}, SubArg6 arg6 = SubArg6{}) {
1224  return subdynrankview(drv, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
1225 }
1226 
1227 } // namespace Kokkos
1228 
1229 namespace Kokkos {
1230 
1231 // overload == and !=
1232 template <class LT, class... LP, class RT, class... RP>
1233 KOKKOS_INLINE_FUNCTION bool operator==(const DynRankView<LT, LP...>& lhs,
1234  const DynRankView<RT, RP...>& rhs) {
1235  // Same data, layout, dimensions
1236  using lhs_traits = ViewTraits<LT, LP...>;
1237  using rhs_traits = ViewTraits<RT, RP...>;
1238 
1239  return std::is_same_v<typename lhs_traits::const_value_type,
1240  typename rhs_traits::const_value_type> &&
1241  std::is_same_v<typename lhs_traits::array_layout,
1242  typename rhs_traits::array_layout> &&
1243  std::is_same_v<typename lhs_traits::memory_space,
1244  typename rhs_traits::memory_space> &&
1245  lhs.rank() == rhs.rank() && lhs.data() == rhs.data() &&
1246  lhs.span() == rhs.span() && lhs.extent(0) == rhs.extent(0) &&
1247  lhs.extent(1) == rhs.extent(1) && lhs.extent(2) == rhs.extent(2) &&
1248  lhs.extent(3) == rhs.extent(3) && lhs.extent(4) == rhs.extent(4) &&
1249  lhs.extent(5) == rhs.extent(5) && lhs.extent(6) == rhs.extent(6) &&
1250  lhs.extent(7) == rhs.extent(7);
1251 }
1252 
1253 template <class LT, class... LP, class RT, class... RP>
1254 KOKKOS_INLINE_FUNCTION bool operator!=(const DynRankView<LT, LP...>& lhs,
1255  const DynRankView<RT, RP...>& rhs) {
1256  return !(operator==(lhs, rhs));
1257 }
1258 
1259 } // namespace Kokkos
1260 
1261 //----------------------------------------------------------------------------
1262 //----------------------------------------------------------------------------
1263 namespace Kokkos {
1264 namespace Impl {
1265 
1266 template <class OutputView, class InputView,
1267  class ExecSpace = typename OutputView::execution_space>
1268 struct DynRankViewRemap {
1269  const OutputView output;
1270  const InputView input;
1271  const size_t n0;
1272  const size_t n1;
1273  const size_t n2;
1274  const size_t n3;
1275  const size_t n4;
1276  const size_t n5;
1277  const size_t n6;
1278  const size_t n7;
1279 
1280  DynRankViewRemap(const ExecSpace& exec_space, const OutputView& arg_out,
1281  const InputView& arg_in)
1282  : output(arg_out),
1283  input(arg_in),
1284  n0(std::min((size_t)arg_out.extent(0), (size_t)arg_in.extent(0))),
1285  n1(std::min((size_t)arg_out.extent(1), (size_t)arg_in.extent(1))),
1286  n2(std::min((size_t)arg_out.extent(2), (size_t)arg_in.extent(2))),
1287  n3(std::min((size_t)arg_out.extent(3), (size_t)arg_in.extent(3))),
1288  n4(std::min((size_t)arg_out.extent(4), (size_t)arg_in.extent(4))),
1289  n5(std::min((size_t)arg_out.extent(5), (size_t)arg_in.extent(5))),
1290  n6(std::min((size_t)arg_out.extent(6), (size_t)arg_in.extent(6))),
1291  n7(std::min((size_t)arg_out.extent(7), (size_t)arg_in.extent(7))) {
1292  using Policy = Kokkos::RangePolicy<ExecSpace>;
1293 
1294  Kokkos::parallel_for("Kokkos::DynRankViewRemap", Policy(exec_space, 0, n0),
1295  *this);
1296  }
1297 
1298  DynRankViewRemap(const OutputView& arg_out, const InputView& arg_in)
1299  : output(arg_out),
1300  input(arg_in),
1301  n0(std::min((size_t)arg_out.extent(0), (size_t)arg_in.extent(0))),
1302  n1(std::min((size_t)arg_out.extent(1), (size_t)arg_in.extent(1))),
1303  n2(std::min((size_t)arg_out.extent(2), (size_t)arg_in.extent(2))),
1304  n3(std::min((size_t)arg_out.extent(3), (size_t)arg_in.extent(3))),
1305  n4(std::min((size_t)arg_out.extent(4), (size_t)arg_in.extent(4))),
1306  n5(std::min((size_t)arg_out.extent(5), (size_t)arg_in.extent(5))),
1307  n6(std::min((size_t)arg_out.extent(6), (size_t)arg_in.extent(6))),
1308  n7(std::min((size_t)arg_out.extent(7), (size_t)arg_in.extent(7))) {
1309  using Policy = Kokkos::RangePolicy<ExecSpace>;
1310 
1311  Kokkos::parallel_for("Kokkos::DynRankViewRemap", Policy(0, n0), *this);
1312  }
1313 
1314  KOKKOS_INLINE_FUNCTION
1315  void operator()(const size_t i0) const {
1316  for (size_t i1 = 0; i1 < n1; ++i1) {
1317  for (size_t i2 = 0; i2 < n2; ++i2) {
1318  for (size_t i3 = 0; i3 < n3; ++i3) {
1319  for (size_t i4 = 0; i4 < n4; ++i4) {
1320  for (size_t i5 = 0; i5 < n5; ++i5) {
1321  for (size_t i6 = 0; i6 < n6; ++i6) {
1322  output.access(i0, i1, i2, i3, i4, i5, i6) =
1323  input.access(i0, i1, i2, i3, i4, i5, i6);
1324  }
1325  }
1326  }
1327  }
1328  }
1329  }
1330  }
1331 };
1332 
1333 } /* namespace Impl */
1334 } /* namespace Kokkos */
1335 
1336 namespace Kokkos {
1337 
1338 namespace Impl {
1339 
1340 /* \brief Returns a View of the requested rank, aliasing the
1341  underlying memory, to facilitate implementation of deep_copy() and
1342  other routines that are defined on View */
1343 template <unsigned N, typename T, typename... Args>
1344 KOKKOS_FUNCTION View<typename ViewDataTypeFromRank<T, N>::type, Args...>
1345 as_view_of_rank_n(
1346  DynRankView<T, Args...> v,
1347  std::enable_if_t<
1348  std::is_same_v<typename ViewTraits<T, Args...>::specialize, void>>*) {
1349  if (v.rank() != N) {
1350  KOKKOS_IF_ON_HOST(
1351  const std::string message =
1352  "Converting DynRankView of rank " + std::to_string(v.rank()) +
1353  " to a View of mis-matched rank " + std::to_string(N) + "!";
1354  Kokkos::abort(message.c_str());)
1355  KOKKOS_IF_ON_DEVICE(
1356  Kokkos::abort("Converting DynRankView to a View of mis-matched rank!");)
1357  }
1358 
1359  auto layout = v.DownCast().layout();
1360 
1361  if constexpr (std::is_same_v<decltype(layout), Kokkos::LayoutLeft> ||
1362  std::is_same_v<decltype(layout), Kokkos::LayoutRight> ||
1363  std::is_same_v<decltype(layout), Kokkos::LayoutStride>) {
1364  for (int i = N; i < 7; ++i)
1365  layout.dimension[i] = KOKKOS_IMPL_CTOR_DEFAULT_ARG;
1366  }
1367 
1368 #ifndef KOKKOS_ENABLE_IMPL_VIEW_LEGACY
1369  if constexpr (ViewTraits<T, Args...>::impl_is_customized) {
1370  return View<typename RankDataType<T, N>::type, Args...>(
1371  Kokkos::view_wrap(
1372  v.data(), Kokkos::Impl::AccessorArg_t{v.accessor().fad_size() + 1}),
1373  layout);
1374  } else
1375 #endif
1376  return View<typename RankDataType<T, N>::type, Args...>(v.data(), layout);
1377 }
1378 
1379 template <typename... Args>
1380 struct ApplyToViewOfStaticRank<DynRankView<Args...>> {
1381  template <typename Function>
1382  static void apply(Function&& f, DynRankView<Args...> a) {
1383  switch (rank(a)) {
1384  case 0: f(as_view_of_rank_n<0>(a)); break;
1385  case 1: f(as_view_of_rank_n<1>(a)); break;
1386  case 2: f(as_view_of_rank_n<2>(a)); break;
1387  case 3: f(as_view_of_rank_n<3>(a)); break;
1388  case 4: f(as_view_of_rank_n<4>(a)); break;
1389  case 5: f(as_view_of_rank_n<5>(a)); break;
1390  case 6: f(as_view_of_rank_n<6>(a)); break;
1391  case 7: f(as_view_of_rank_n<7>(a)); break;
1392  default:
1393  KOKKOS_IF_ON_HOST(
1394  Kokkos::abort(
1395  std::string(
1396  "Trying to apply a function to a view of unexpected rank " +
1397  std::to_string(rank(a)))
1398  .c_str());)
1399  KOKKOS_IF_ON_DEVICE(
1400  Kokkos::abort(
1401  "Trying to apply a function to a view of unexpected rank");)
1402  }
1403  }
1404 };
1405 
1406 } // namespace Impl
1407 
1409 template <class ExecSpace, class DT, class... DP>
1410 inline void deep_copy(
1411  const ExecSpace& e, const DynRankView<DT, DP...>& dst,
1412  typename ViewTraits<DT, DP...>::const_value_type& value,
1413  std::enable_if_t<std::is_same_v<typename ViewTraits<DT, DP...>::specialize,
1414  void>>* = nullptr) {
1415  static_assert(
1416  std::is_same_v<typename ViewTraits<DT, DP...>::non_const_value_type,
1417  typename ViewTraits<DT, DP...>::value_type>,
1418  "deep_copy requires non-const type");
1419 
1420  Impl::ApplyToViewOfStaticRank<DynRankView<DT, DP...>>::apply(
1421  [=](auto view) { deep_copy(e, view, value); }, dst);
1422 }
1423 
1424 template <class DT, class... DP>
1425 inline void deep_copy(
1426  const DynRankView<DT, DP...>& dst,
1427  typename ViewTraits<DT, DP...>::const_value_type& value,
1428  std::enable_if_t<std::is_same_v<typename ViewTraits<DT, DP...>::specialize,
1429  void>>* = nullptr) {
1430  Impl::ApplyToViewOfStaticRank<DynRankView<DT, DP...>>::apply(
1431  [=](auto view) { deep_copy(view, value); }, dst);
1432 }
1433 
1435 template <class ExecSpace, class ST, class... SP>
1436 inline void deep_copy(
1437  const ExecSpace& e,
1438  typename ViewTraits<ST, SP...>::non_const_value_type& dst,
1439  const DynRankView<ST, SP...>& src,
1440  std::enable_if_t<std::is_same_v<typename ViewTraits<ST, SP...>::specialize,
1441  void>>* = 0) {
1442  deep_copy(e, dst, Impl::as_view_of_rank_n<0>(src));
1443 }
1444 
1445 template <class ST, class... SP>
1446 inline void deep_copy(
1447  typename ViewTraits<ST, SP...>::non_const_value_type& dst,
1448  const DynRankView<ST, SP...>& src,
1449  std::enable_if_t<std::is_same_v<typename ViewTraits<ST, SP...>::specialize,
1450  void>>* = 0) {
1451  deep_copy(dst, Impl::as_view_of_rank_n<0>(src));
1452 }
1453 
1454 //----------------------------------------------------------------------------
1460 template <class ExecSpace, class DstType, class SrcType>
1461 inline void deep_copy(
1462  const ExecSpace& exec_space, const DstType& dst, const SrcType& src,
1463  std::enable_if_t<(std::is_void_v<typename DstType::traits::specialize> &&
1464  std::is_void_v<typename SrcType::traits::specialize> &&
1465  (Kokkos::is_dyn_rank_view<DstType>::value ||
1466  Kokkos::is_dyn_rank_view<SrcType>::value))>* = nullptr) {
1467  static_assert(std::is_same_v<typename DstType::traits::value_type,
1468  typename DstType::traits::non_const_value_type>,
1469  "deep_copy requires non-const destination type");
1470 
1471  switch (rank(dst)) {
1472  case 0:
1473  deep_copy(exec_space, Impl::as_view_of_rank_n<0>(dst),
1474  Impl::as_view_of_rank_n<0>(src));
1475  break;
1476  case 1:
1477  deep_copy(exec_space, Impl::as_view_of_rank_n<1>(dst),
1478  Impl::as_view_of_rank_n<1>(src));
1479  break;
1480  case 2:
1481  deep_copy(exec_space, Impl::as_view_of_rank_n<2>(dst),
1482  Impl::as_view_of_rank_n<2>(src));
1483  break;
1484  case 3:
1485  deep_copy(exec_space, Impl::as_view_of_rank_n<3>(dst),
1486  Impl::as_view_of_rank_n<3>(src));
1487  break;
1488  case 4:
1489  deep_copy(exec_space, Impl::as_view_of_rank_n<4>(dst),
1490  Impl::as_view_of_rank_n<4>(src));
1491  break;
1492  case 5:
1493  deep_copy(exec_space, Impl::as_view_of_rank_n<5>(dst),
1494  Impl::as_view_of_rank_n<5>(src));
1495  break;
1496  case 6:
1497  deep_copy(exec_space, Impl::as_view_of_rank_n<6>(dst),
1498  Impl::as_view_of_rank_n<6>(src));
1499  break;
1500  case 7:
1501  deep_copy(exec_space, Impl::as_view_of_rank_n<7>(dst),
1502  Impl::as_view_of_rank_n<7>(src));
1503  break;
1504  default:
1505  Kokkos::Impl::throw_runtime_exception(
1506  "Calling DynRankView deep_copy with a view of unexpected rank " +
1507  std::to_string(rank(dst)));
1508  }
1509 }
1510 
1511 template <class DstType, class SrcType>
1512 inline void deep_copy(
1513  const DstType& dst, const SrcType& src,
1514  std::enable_if_t<(std::is_void_v<typename DstType::traits::specialize> &&
1515  std::is_void_v<typename SrcType::traits::specialize> &&
1516  (Kokkos::is_dyn_rank_view<DstType>::value ||
1517  Kokkos::is_dyn_rank_view<SrcType>::value))>* = nullptr) {
1518  static_assert(std::is_same_v<typename DstType::traits::value_type,
1519  typename DstType::traits::non_const_value_type>,
1520  "deep_copy requires non-const destination type");
1521 
1522  switch (rank(dst)) {
1523  case 0:
1524  deep_copy(Impl::as_view_of_rank_n<0>(dst),
1525  Impl::as_view_of_rank_n<0>(src));
1526  break;
1527  case 1:
1528  deep_copy(Impl::as_view_of_rank_n<1>(dst),
1529  Impl::as_view_of_rank_n<1>(src));
1530  break;
1531  case 2:
1532  deep_copy(Impl::as_view_of_rank_n<2>(dst),
1533  Impl::as_view_of_rank_n<2>(src));
1534  break;
1535  case 3:
1536  deep_copy(Impl::as_view_of_rank_n<3>(dst),
1537  Impl::as_view_of_rank_n<3>(src));
1538  break;
1539  case 4:
1540  deep_copy(Impl::as_view_of_rank_n<4>(dst),
1541  Impl::as_view_of_rank_n<4>(src));
1542  break;
1543  case 5:
1544  deep_copy(Impl::as_view_of_rank_n<5>(dst),
1545  Impl::as_view_of_rank_n<5>(src));
1546  break;
1547  case 6:
1548  deep_copy(Impl::as_view_of_rank_n<6>(dst),
1549  Impl::as_view_of_rank_n<6>(src));
1550  break;
1551  case 7:
1552  deep_copy(Impl::as_view_of_rank_n<7>(dst),
1553  Impl::as_view_of_rank_n<7>(src));
1554  break;
1555  default:
1556  Kokkos::Impl::throw_runtime_exception(
1557  "Calling DynRankView deep_copy with a view of unexpected rank " +
1558  std::to_string(rank(dst)));
1559  }
1560 }
1561 
1562 } // namespace Kokkos
1563 
1564 //----------------------------------------------------------------------------
1565 //----------------------------------------------------------------------------
1566 
1567 namespace Kokkos {
1568 namespace Impl {
1569 
1570 // Deduce Mirror Types
1571 template <class Space, class T, class... P>
1572 struct MirrorDRViewType {
1573  // The incoming view_type
1574  using src_view_type = typename Kokkos::DynRankView<T, P...>;
1575  // The memory space for the mirror view
1576  using memory_space = typename Space::memory_space;
1577  // Check whether it is the same memory space
1578  enum {
1579  is_same_memspace =
1580  std::is_same_v<memory_space, typename src_view_type::memory_space>
1581  };
1582  // The array_layout
1583  using array_layout = typename src_view_type::array_layout;
1584  // The data type (we probably want it non-const since otherwise we can't even
1585  // deep_copy to it.
1586  using data_type = typename src_view_type::non_const_data_type;
1587  // The destination view type if it is not the same memory space
1588  using dest_view_type = Kokkos::DynRankView<data_type, array_layout, Space>;
1589  // If it is the same memory_space return the existsing view_type
1590  // This will also keep the unmanaged trait if necessary
1591  using view_type =
1592  std::conditional_t<is_same_memspace, src_view_type, dest_view_type>;
1593 };
1594 
1595 } // namespace Impl
1596 
1597 namespace Impl {
1598 
1599 // create a mirror
1600 // private interface that accepts arbitrary view constructor args passed by a
1601 // view_alloc
1602 template <class T, class... P, class... ViewCtorArgs>
1603 inline auto create_mirror(const DynRankView<T, P...>& src,
1604  const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop) {
1605  check_view_ctor_args_create_mirror<ViewCtorArgs...>();
1606 
1607  auto prop_copy = Impl::with_properties_if_unset(
1608  arg_prop, std::string(src.label()).append("_mirror"));
1609 
1610  if constexpr (Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space) {
1611  using dst_type = typename Impl::MirrorDRViewType<
1612  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
1613  P...>::dest_view_type;
1614  return dst_type(prop_copy,
1615  Impl::reconstructLayout(src.layout(), src.rank()));
1616  } else {
1617  using src_type = DynRankView<T, P...>;
1618  using dst_type = typename src_type::HostMirror;
1619 
1620  return dst_type(prop_copy,
1621  Impl::reconstructLayout(src.layout(), src.rank()));
1622  }
1623 #if defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
1624  !defined(KOKKOS_COMPILER_MSVC)
1625  __builtin_unreachable();
1626 #endif
1627 }
1628 
1629 } // namespace Impl
1630 
1631 // public interface
1632 template <class T, class... P,
1633  class Enable = std::enable_if_t<
1634  std::is_void_v<typename ViewTraits<T, P...>::specialize>>>
1635 inline auto create_mirror(const DynRankView<T, P...>& src) {
1636  return Impl::create_mirror(src, Kokkos::view_alloc());
1637 }
1638 
1639 // public interface that accepts a without initializing flag
1640 template <class T, class... P,
1641  class Enable = std::enable_if_t<
1642  std::is_void_v<typename ViewTraits<T, P...>::specialize>>>
1643 inline auto create_mirror(Kokkos::Impl::WithoutInitializing_t wi,
1644  const DynRankView<T, P...>& src) {
1645  return Impl::create_mirror(src, Kokkos::view_alloc(wi));
1646 }
1647 
1648 // public interface that accepts a space
1649 template <class Space, class T, class... P,
1650  class Enable = std::enable_if_t<
1651  Kokkos::is_space<Space>::value &&
1652  std::is_void_v<typename ViewTraits<T, P...>::specialize>>>
1653 inline auto create_mirror(const Space&,
1654  const Kokkos::DynRankView<T, P...>& src) {
1655  return Impl::create_mirror(
1656  src, Kokkos::view_alloc(typename Space::memory_space{}));
1657 }
1658 
1659 // public interface that accepts a space and a without initializing flag
1660 template <class Space, class T, class... P,
1661  class Enable = std::enable_if_t<
1662  Kokkos::is_space<Space>::value &&
1663  std::is_void_v<typename ViewTraits<T, P...>::specialize>>>
1664 inline auto create_mirror(Kokkos::Impl::WithoutInitializing_t wi, const Space&,
1665  const Kokkos::DynRankView<T, P...>& src) {
1666  return Impl::create_mirror(
1667  src, Kokkos::view_alloc(wi, typename Space::memory_space{}));
1668 }
1669 
1670 // public interface that accepts arbitrary view constructor args passed by a
1671 // view_alloc
1672 template <class T, class... P, class... ViewCtorArgs,
1673  typename Enable = std::enable_if_t<
1674  std::is_void_v<typename ViewTraits<T, P...>::specialize>>>
1675 inline auto create_mirror(const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1676  const DynRankView<T, P...>& src) {
1677  return Impl::create_mirror(src, arg_prop);
1678 }
1679 
1680 namespace Impl {
1681 
1682 // create a mirror view
1683 // private interface that accepts arbitrary view constructor args passed by a
1684 // view_alloc
1685 template <class T, class... P, class... ViewCtorArgs>
1686 inline auto create_mirror_view(
1687  const DynRankView<T, P...>& src,
1688  [[maybe_unused]] const typename Impl::ViewCtorProp<ViewCtorArgs...>&
1689  arg_prop) {
1690  if constexpr (!Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space) {
1691  if constexpr (std::is_same_v<typename DynRankView<T, P...>::memory_space,
1692  typename DynRankView<
1693  T, P...>::HostMirror::memory_space> &&
1694  std::is_same_v<
1695  typename DynRankView<T, P...>::data_type,
1696  typename DynRankView<T, P...>::HostMirror::data_type>) {
1697  return typename DynRankView<T, P...>::HostMirror(src);
1698  } else {
1699  return Kokkos::Impl::choose_create_mirror(src, arg_prop);
1700  }
1701  } else {
1702  if constexpr (Impl::MirrorDRViewType<typename Impl::ViewCtorProp<
1703  ViewCtorArgs...>::memory_space,
1704  T, P...>::is_same_memspace) {
1705  return typename Impl::MirrorDRViewType<
1706  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
1707  P...>::view_type(src);
1708  } else {
1709  return Kokkos::Impl::choose_create_mirror(src, arg_prop);
1710  }
1711  }
1712 #if defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
1713  !defined(KOKKOS_COMPILER_MSVC)
1714  __builtin_unreachable();
1715 #endif
1716 }
1717 
1718 } // namespace Impl
1719 
1720 // public interface
1721 template <class T, class... P>
1722 inline auto create_mirror_view(const Kokkos::DynRankView<T, P...>& src) {
1723  return Impl::create_mirror_view(src, Kokkos::view_alloc());
1724 }
1725 
1726 // public interface that accepts a without initializing flag
1727 template <class T, class... P>
1728 inline auto create_mirror_view(Kokkos::Impl::WithoutInitializing_t wi,
1729  const DynRankView<T, P...>& src) {
1730  return Impl::create_mirror_view(src, Kokkos::view_alloc(wi));
1731 }
1732 
1733 // public interface that accepts a space
1734 template <class Space, class T, class... P,
1735  class Enable = std::enable_if_t<Kokkos::is_space<Space>::value>>
1736 inline auto create_mirror_view(const Space&,
1737  const Kokkos::DynRankView<T, P...>& src) {
1738  return Impl::create_mirror_view(
1739  src, Kokkos::view_alloc(typename Space::memory_space()));
1740 }
1741 
1742 // public interface that accepts a space and a without initializing flag
1743 template <class Space, class T, class... P,
1744  typename Enable = std::enable_if_t<Kokkos::is_space<Space>::value>>
1745 inline auto create_mirror_view(Kokkos::Impl::WithoutInitializing_t wi,
1746  const Space&,
1747  const Kokkos::DynRankView<T, P...>& src) {
1748  return Impl::create_mirror_view(
1749  src, Kokkos::view_alloc(typename Space::memory_space{}, wi));
1750 }
1751 
1752 // public interface that accepts arbitrary view constructor args passed by a
1753 // view_alloc
1754 template <class T, class... P, class... ViewCtorArgs>
1755 inline auto create_mirror_view(
1756  const typename Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1757  const Kokkos::DynRankView<T, P...>& src) {
1758  return Impl::create_mirror_view(src, arg_prop);
1759 }
1760 
1761 // create a mirror view and deep copy it
1762 // public interface that accepts arbitrary view constructor args passed by a
1763 // view_alloc
1764 template <class... ViewCtorArgs, class T, class... P,
1765  class Enable = std::enable_if_t<
1766  std::is_void_v<typename ViewTraits<T, P...>::specialize>>>
1767 auto create_mirror_view_and_copy(
1768  [[maybe_unused]] const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1769  const Kokkos::DynRankView<T, P...>& src) {
1770  using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
1771 
1772  Impl::check_view_ctor_args_create_mirror_view_and_copy<ViewCtorArgs...>();
1773 
1774  if constexpr (Impl::MirrorDRViewType<
1775  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space,
1776  T, P...>::is_same_memspace) {
1777  // same behavior as deep_copy(src, src)
1778  if constexpr (!alloc_prop_input::has_execution_space)
1779  fence(
1780  "Kokkos::create_mirror_view_and_copy: fence before returning src "
1781  "view");
1782  return src;
1783  } else {
1784  using Space = typename alloc_prop_input::memory_space;
1785  using Mirror = typename Impl::MirrorDRViewType<Space, T, P...>::view_type;
1786 
1787  auto arg_prop_copy = Impl::with_properties_if_unset(
1788  arg_prop, std::string{}, WithoutInitializing,
1789  typename Space::execution_space{});
1790 
1791  std::string& label = Impl::get_property<Impl::LabelTag>(arg_prop_copy);
1792  if (label.empty()) label = src.label();
1793  auto mirror = typename Mirror::non_const_type{
1794  arg_prop_copy, Impl::reconstructLayout(src.layout(), src.rank())};
1795  if constexpr (alloc_prop_input::has_execution_space) {
1796  deep_copy(Impl::get_property<Impl::ExecutionSpaceTag>(arg_prop_copy),
1797  mirror, src);
1798  } else
1799  deep_copy(mirror, src);
1800  return mirror;
1801  }
1802 #if defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
1803  !defined(KOKKOS_COMPILER_MSVC)
1804  __builtin_unreachable();
1805 #endif
1806 }
1807 
1808 template <class Space, class T, class... P>
1809 auto create_mirror_view_and_copy(const Space&,
1810  const Kokkos::DynRankView<T, P...>& src,
1811  std::string const& name = "") {
1812  return create_mirror_view_and_copy(
1813  Kokkos::view_alloc(typename Space::memory_space{}, name), src);
1814 }
1815 
1816 } // namespace Kokkos
1817 
1818 //----------------------------------------------------------------------------
1819 //----------------------------------------------------------------------------
1820 
1821 namespace Kokkos {
1824 template <class... ViewCtorArgs, class T, class... P>
1825 inline void impl_resize(const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1826  DynRankView<T, P...>& v, const size_t n0,
1827  const size_t n1, const size_t n2, const size_t n3,
1828  const size_t n4, const size_t n5, const size_t n6,
1829  const size_t n7) {
1830  using drview_type = DynRankView<T, P...>;
1831  using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
1832 
1833  static_assert(Kokkos::ViewTraits<T, P...>::is_managed,
1834  "Can only resize managed views");
1835  static_assert(!alloc_prop_input::has_label,
1836  "The view constructor arguments passed to Kokkos::resize "
1837  "must not include a label!");
1838  static_assert(!alloc_prop_input::has_pointer,
1839  "The view constructor arguments passed to Kokkos::resize must "
1840  "not include a pointer!");
1841  static_assert(!alloc_prop_input::has_memory_space,
1842  "The view constructor arguments passed to Kokkos::resize must "
1843  "not include a memory space instance!");
1844 
1845  auto prop_copy = Impl::with_properties_if_unset(
1846  arg_prop, v.label(), typename drview_type::execution_space{});
1847 
1848  drview_type v_resized(prop_copy, n0, n1, n2, n3, n4, n5, n6, n7);
1849 
1850  if constexpr (alloc_prop_input::has_execution_space)
1851  Kokkos::Impl::DynRankViewRemap<drview_type, drview_type>(
1852  Impl::get_property<Impl::ExecutionSpaceTag>(prop_copy), v_resized, v);
1853  else {
1854  // NOLINTNEXTLINE(bugprone-unused-raii)
1855  Kokkos::Impl::DynRankViewRemap<drview_type, drview_type>(v_resized, v);
1856  Kokkos::fence("Kokkos::resize(DynRankView)");
1857  }
1858  v = v_resized;
1859 }
1860 
1861 template <class T, class... P>
1862 inline void resize(DynRankView<T, P...>& v,
1863  const size_t n0 = KOKKOS_INVALID_INDEX,
1864  const size_t n1 = KOKKOS_INVALID_INDEX,
1865  const size_t n2 = KOKKOS_INVALID_INDEX,
1866  const size_t n3 = KOKKOS_INVALID_INDEX,
1867  const size_t n4 = KOKKOS_INVALID_INDEX,
1868  const size_t n5 = KOKKOS_INVALID_INDEX,
1869  const size_t n6 = KOKKOS_INVALID_INDEX,
1870  const size_t n7 = KOKKOS_INVALID_INDEX) {
1871  impl_resize(Impl::ViewCtorProp<>{}, v, n0, n1, n2, n3, n4, n5, n6, n7);
1872 }
1873 
1874 template <class... ViewCtorArgs, class T, class... P>
1875 void resize(const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1876  DynRankView<T, P...>& v,
1877  const size_t n0 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1878  const size_t n1 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1879  const size_t n2 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1880  const size_t n3 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1881  const size_t n4 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1882  const size_t n5 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1883  const size_t n6 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1884  const size_t n7 = KOKKOS_IMPL_CTOR_DEFAULT_ARG) {
1885  impl_resize(arg_prop, v, n0, n1, n2, n3, n4, n5, n6, n7);
1886 }
1887 
1888 template <class I, class T, class... P>
1889 inline std::enable_if_t<Impl::is_view_ctor_property<I>::value> resize(
1890  const I& arg_prop, DynRankView<T, P...>& v,
1891  const size_t n0 = KOKKOS_INVALID_INDEX,
1892  const size_t n1 = KOKKOS_INVALID_INDEX,
1893  const size_t n2 = KOKKOS_INVALID_INDEX,
1894  const size_t n3 = KOKKOS_INVALID_INDEX,
1895  const size_t n4 = KOKKOS_INVALID_INDEX,
1896  const size_t n5 = KOKKOS_INVALID_INDEX,
1897  const size_t n6 = KOKKOS_INVALID_INDEX,
1898  const size_t n7 = KOKKOS_INVALID_INDEX) {
1899  impl_resize(Kokkos::view_alloc(arg_prop), v, n0, n1, n2, n3, n4, n5, n6, n7);
1900 }
1901 
1904 template <class... ViewCtorArgs, class T, class... P>
1905 inline void impl_realloc(DynRankView<T, P...>& v, const size_t n0,
1906  const size_t n1, const size_t n2, const size_t n3,
1907  const size_t n4, const size_t n5, const size_t n6,
1908  const size_t n7,
1909  const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop) {
1910  using drview_type = DynRankView<T, P...>;
1911  using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
1912 
1913  static_assert(Kokkos::ViewTraits<T, P...>::is_managed,
1914  "Can only realloc managed views");
1915  static_assert(!alloc_prop_input::has_label,
1916  "The view constructor arguments passed to Kokkos::realloc must "
1917  "not include a label!");
1918  static_assert(!alloc_prop_input::has_pointer,
1919  "The view constructor arguments passed to Kokkos::realloc must "
1920  "not include a pointer!");
1921  static_assert(!alloc_prop_input::has_memory_space,
1922  "The view constructor arguments passed to Kokkos::realloc must "
1923  "not include a memory space instance!");
1924 
1925  auto arg_prop_copy = Impl::with_properties_if_unset(arg_prop, v.label());
1926 
1927  v = drview_type(); // Deallocate first, if the only view to allocation
1928  v = drview_type(arg_prop_copy, n0, n1, n2, n3, n4, n5, n6, n7);
1929 }
1930 
1931 template <class T, class... P, class... ViewCtorArgs>
1932 inline void realloc(const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1933  DynRankView<T, P...>& v,
1934  const size_t n0 = KOKKOS_INVALID_INDEX,
1935  const size_t n1 = KOKKOS_INVALID_INDEX,
1936  const size_t n2 = KOKKOS_INVALID_INDEX,
1937  const size_t n3 = KOKKOS_INVALID_INDEX,
1938  const size_t n4 = KOKKOS_INVALID_INDEX,
1939  const size_t n5 = KOKKOS_INVALID_INDEX,
1940  const size_t n6 = KOKKOS_INVALID_INDEX,
1941  const size_t n7 = KOKKOS_INVALID_INDEX) {
1942  impl_realloc(v, n0, n1, n2, n3, n4, n5, n6, n7, arg_prop);
1943 }
1944 
1945 template <class T, class... P>
1946 inline void realloc(DynRankView<T, P...>& v,
1947  const size_t n0 = KOKKOS_INVALID_INDEX,
1948  const size_t n1 = KOKKOS_INVALID_INDEX,
1949  const size_t n2 = KOKKOS_INVALID_INDEX,
1950  const size_t n3 = KOKKOS_INVALID_INDEX,
1951  const size_t n4 = KOKKOS_INVALID_INDEX,
1952  const size_t n5 = KOKKOS_INVALID_INDEX,
1953  const size_t n6 = KOKKOS_INVALID_INDEX,
1954  const size_t n7 = KOKKOS_INVALID_INDEX) {
1955  impl_realloc(v, n0, n1, n2, n3, n4, n5, n6, n7, Impl::ViewCtorProp<>{});
1956 }
1957 
1958 template <class I, class T, class... P>
1959 inline std::enable_if_t<Impl::is_view_ctor_property<I>::value> realloc(
1960  const I& arg_prop, DynRankView<T, P...>& v,
1961  const size_t n0 = KOKKOS_INVALID_INDEX,
1962  const size_t n1 = KOKKOS_INVALID_INDEX,
1963  const size_t n2 = KOKKOS_INVALID_INDEX,
1964  const size_t n3 = KOKKOS_INVALID_INDEX,
1965  const size_t n4 = KOKKOS_INVALID_INDEX,
1966  const size_t n5 = KOKKOS_INVALID_INDEX,
1967  const size_t n6 = KOKKOS_INVALID_INDEX,
1968  const size_t n7 = KOKKOS_INVALID_INDEX) {
1969  impl_realloc(v, n0, n1, n2, n3, n4, n5, n6, n7, Kokkos::view_alloc(arg_prop));
1970 }
1971 
1972 namespace Experimental {
1973 template <class T, class... P>
1974 struct python_view_type<DynRankView<T, P...>> {
1975  using type = Kokkos::Impl::python_view_type_impl_t<
1976  typename DynRankView<T, P...>::array_type>;
1977 };
1978 } // namespace Experimental
1979 
1980 } // namespace Kokkos
1981 
1982 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_DYNRANKVIEW
1983 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
1984 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_DYNRANKVIEW
1985 #endif
1986 #endif
Memory layout tag indicating left-to-right (Fortran scheme) striding of multi-indices.
KOKKOS_INLINE_FUNCTION bool dyn_rank_view_verify_operator_bounds(const iType0 &, const MapType &)
Debug bounds-checking routines.
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...
Assign compatible default mappings.
Execution policy for work over a range of an integral type.