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