Kokkos Core Kernels Package  Version of the Day
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 <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 <typename Specialize>
42 struct DynRankDimTraits {
43  enum : size_t { unspecified = KOKKOS_INVALID_INDEX };
44 
45  // Compute the rank of the view from the nonzero dimension arguments.
46  KOKKOS_INLINE_FUNCTION
47  static size_t computeRank(const size_t N0, const size_t N1, const size_t N2,
48  const size_t N3, const size_t N4, const size_t N5,
49  const size_t N6, const size_t /* N7 */) {
50  return (
51  (N6 == unspecified && N5 == unspecified && N4 == unspecified &&
52  N3 == unspecified && N2 == unspecified && N1 == unspecified &&
53  N0 == unspecified)
54  ? 0
55  : ((N6 == unspecified && N5 == unspecified && N4 == unspecified &&
56  N3 == unspecified && N2 == unspecified && N1 == unspecified)
57  ? 1
58  : ((N6 == unspecified && N5 == unspecified &&
59  N4 == unspecified && N3 == unspecified &&
60  N2 == unspecified)
61  ? 2
62  : ((N6 == unspecified && N5 == unspecified &&
63  N4 == unspecified && N3 == unspecified)
64  ? 3
65  : ((N6 == unspecified && N5 == unspecified &&
66  N4 == unspecified)
67  ? 4
68  : ((N6 == unspecified &&
69  N5 == unspecified)
70  ? 5
71  : ((N6 == unspecified)
72  ? 6
73  : 7)))))));
74  }
75 
76  // Compute the rank of the view from the nonzero layout arguments.
77  template <typename Layout>
78  KOKKOS_INLINE_FUNCTION static size_t computeRank(const Layout& layout) {
79  return computeRank(layout.dimension[0], layout.dimension[1],
80  layout.dimension[2], layout.dimension[3],
81  layout.dimension[4], layout.dimension[5],
82  layout.dimension[6], layout.dimension[7]);
83  }
84 
85  // Extra overload to match that for specialize types v2
86  template <typename Layout, typename... P>
87  KOKKOS_INLINE_FUNCTION static size_t computeRank(
88  const Kokkos::Impl::ViewCtorProp<P...>& /* prop */,
89  const Layout& layout) {
90  return computeRank(layout);
91  }
92 
93  // Create the layout for the rank-7 view.
94  // Non-strided Layout
95  template <typename Layout>
96  KOKKOS_INLINE_FUNCTION static std::enable_if_t<
97  (std::is_same<Layout, Kokkos::LayoutRight>::value ||
98  std::is_same<Layout, Kokkos::LayoutLeft>::value),
99  Layout>
100  createLayout(const Layout& layout) {
101  return Layout(layout.dimension[0] != unspecified ? layout.dimension[0] : 1,
102  layout.dimension[1] != unspecified ? layout.dimension[1] : 1,
103  layout.dimension[2] != unspecified ? layout.dimension[2] : 1,
104  layout.dimension[3] != unspecified ? layout.dimension[3] : 1,
105  layout.dimension[4] != unspecified ? layout.dimension[4] : 1,
106  layout.dimension[5] != unspecified ? layout.dimension[5] : 1,
107  layout.dimension[6] != unspecified ? layout.dimension[6] : 1,
108  layout.dimension[7] != unspecified ? layout.dimension[7] : 1);
109  }
110 
111  // LayoutStride
112  template <typename Layout>
113  KOKKOS_INLINE_FUNCTION static std::enable_if_t<
114  (std::is_same<Layout, Kokkos::LayoutStride>::value), Layout>
115  createLayout(const Layout& layout) {
116  return Layout(layout.dimension[0] != unspecified ? layout.dimension[0] : 1,
117  layout.stride[0],
118  layout.dimension[1] != unspecified ? layout.dimension[1] : 1,
119  layout.stride[1],
120  layout.dimension[2] != unspecified ? layout.dimension[2] : 1,
121  layout.stride[2],
122  layout.dimension[3] != unspecified ? layout.dimension[3] : 1,
123  layout.stride[3],
124  layout.dimension[4] != unspecified ? layout.dimension[4] : 1,
125  layout.stride[4],
126  layout.dimension[5] != unspecified ? layout.dimension[5] : 1,
127  layout.stride[5],
128  layout.dimension[6] != unspecified ? layout.dimension[6] : 1,
129  layout.stride[6],
130  layout.dimension[7] != unspecified ? layout.dimension[7] : 1,
131  layout.stride[7]);
132  }
133 
134  // Extra overload to match that for specialize types
135  template <typename Traits, typename... P>
136  KOKKOS_INLINE_FUNCTION static std::enable_if_t<
137  (std::is_same<typename Traits::array_layout,
138  Kokkos::LayoutRight>::value ||
139  std::is_same<typename Traits::array_layout, Kokkos::LayoutLeft>::value ||
140  std::is_same<typename Traits::array_layout,
141  Kokkos::LayoutStride>::value),
142  typename Traits::array_layout>
143  createLayout(const Kokkos::Impl::ViewCtorProp<P...>& /* prop */,
144  const typename Traits::array_layout& layout) {
145  return createLayout(layout);
146  }
147 
148  // Create a view from the given dimension arguments.
149  // This is only necessary because the shmem constructor doesn't take a layout.
150  // NDE shmem View's are not compatible with the added view_alloc value_type
151  // / fad_dim deduction functionality
152  template <typename ViewType, typename ViewArg>
153  static ViewType createView(const ViewArg& arg, const size_t N0,
154  const size_t N1, const size_t N2, const size_t N3,
155  const size_t N4, const size_t N5, const size_t N6,
156  const size_t N7) {
157  return ViewType(arg, N0 != unspecified ? N0 : 1, N1 != unspecified ? N1 : 1,
158  N2 != unspecified ? N2 : 1, N3 != unspecified ? N3 : 1,
159  N4 != unspecified ? N4 : 1, N5 != unspecified ? N5 : 1,
160  N6 != unspecified ? N6 : 1, N7 != unspecified ? N7 : 1);
161  }
162 };
163 
164 // Non-strided Layout
165 template <typename Layout, typename iType>
166 KOKKOS_INLINE_FUNCTION static std::enable_if_t<
167  (std::is_same<Layout, Kokkos::LayoutRight>::value ||
168  std::is_same<Layout, Kokkos::LayoutLeft>::value) &&
169  std::is_integral<iType>::value,
170  Layout>
171 reconstructLayout(const Layout& layout, iType dynrank) {
172  return Layout(dynrank > 0 ? layout.dimension[0] : KOKKOS_INVALID_INDEX,
173  dynrank > 1 ? layout.dimension[1] : KOKKOS_INVALID_INDEX,
174  dynrank > 2 ? layout.dimension[2] : KOKKOS_INVALID_INDEX,
175  dynrank > 3 ? layout.dimension[3] : KOKKOS_INVALID_INDEX,
176  dynrank > 4 ? layout.dimension[4] : KOKKOS_INVALID_INDEX,
177  dynrank > 5 ? layout.dimension[5] : KOKKOS_INVALID_INDEX,
178  dynrank > 6 ? layout.dimension[6] : KOKKOS_INVALID_INDEX,
179  dynrank > 7 ? layout.dimension[7] : KOKKOS_INVALID_INDEX);
180 }
181 
182 // LayoutStride
183 template <typename Layout, typename iType>
184 KOKKOS_INLINE_FUNCTION static std::enable_if_t<
185  (std::is_same<Layout, Kokkos::LayoutStride>::value) &&
186  std::is_integral<iType>::value,
187  Layout>
188 reconstructLayout(const Layout& layout, iType dynrank) {
189  return Layout(dynrank > 0 ? layout.dimension[0] : KOKKOS_INVALID_INDEX,
190  dynrank > 0 ? layout.stride[0] : (0),
191  dynrank > 1 ? layout.dimension[1] : KOKKOS_INVALID_INDEX,
192  dynrank > 1 ? layout.stride[1] : (0),
193  dynrank > 2 ? layout.dimension[2] : KOKKOS_INVALID_INDEX,
194  dynrank > 2 ? layout.stride[2] : (0),
195  dynrank > 3 ? layout.dimension[3] : KOKKOS_INVALID_INDEX,
196  dynrank > 3 ? layout.stride[3] : (0),
197  dynrank > 4 ? layout.dimension[4] : KOKKOS_INVALID_INDEX,
198  dynrank > 4 ? layout.stride[4] : (0),
199  dynrank > 5 ? layout.dimension[5] : KOKKOS_INVALID_INDEX,
200  dynrank > 5 ? layout.stride[5] : (0),
201  dynrank > 6 ? layout.dimension[6] : KOKKOS_INVALID_INDEX,
202  dynrank > 6 ? layout.stride[6] : (0),
203  dynrank > 7 ? layout.dimension[7] : KOKKOS_INVALID_INDEX,
204  dynrank > 7 ? layout.stride[7] : (0));
205 }
206 
208 // Enhanced debug checking - most infrastructure matches that of functions in
209 // Kokkos_ViewMapping; additional checks for extra arguments beyond rank are 0
210 template <unsigned, typename iType0, class MapType>
211 KOKKOS_INLINE_FUNCTION bool dyn_rank_view_verify_operator_bounds(
212  const iType0&, const MapType&) {
213  return true;
214 }
215 
216 template <unsigned R, typename iType0, class MapType, typename iType1,
217  class... Args>
218 KOKKOS_INLINE_FUNCTION bool dyn_rank_view_verify_operator_bounds(
219  const iType0& rank, const MapType& map, const iType1& i, Args... args) {
220  if (static_cast<iType0>(R) < rank) {
221  return (size_t(i) < map.extent(R)) &&
222  dyn_rank_view_verify_operator_bounds<R + 1>(rank, map, args...);
223  } else if (i != 0) {
224  KOKKOS_IMPL_DO_NOT_USE_PRINTF(
225  "DynRankView Debug Bounds Checking Error: at rank %u\n Extra "
226  "arguments beyond the rank must be zero \n",
227  R);
228  return (false) &&
229  dyn_rank_view_verify_operator_bounds<R + 1>(rank, map, args...);
230  } else {
231  return (true) &&
232  dyn_rank_view_verify_operator_bounds<R + 1>(rank, map, args...);
233  }
234 }
235 
236 template <unsigned, class MapType>
237 inline void dyn_rank_view_error_operator_bounds(char*, int, const MapType&) {}
238 
239 template <unsigned R, class MapType, class iType, class... Args>
240 inline void dyn_rank_view_error_operator_bounds(char* buf, int len,
241  const MapType& map,
242  const iType& i, Args... args) {
243  const int n = snprintf(
244  buf, len, " %ld < %ld %c", static_cast<unsigned long>(i),
245  static_cast<unsigned long>(map.extent(R)), (sizeof...(Args) ? ',' : ')'));
246  dyn_rank_view_error_operator_bounds<R + 1>(buf + n, len - n, map, args...);
247 }
248 
249 // op_rank = rank of the operator version that was called
250 template <typename MemorySpace, typename iType0, typename iType1, class MapType,
251  class... Args>
252 KOKKOS_INLINE_FUNCTION void dyn_rank_view_verify_operator_bounds(
253  const iType0& op_rank, const iType1& rank,
254  const Kokkos::Impl::SharedAllocationTracker& tracker, const MapType& map,
255  Args... args) {
256  if (static_cast<iType0>(rank) > op_rank) {
257  Kokkos::abort(
258  "DynRankView Bounds Checking Error: Need at least rank arguments to "
259  "the operator()");
260  }
261 
262  if (!dyn_rank_view_verify_operator_bounds<0>(rank, map, args...)) {
263  KOKKOS_IF_ON_HOST(
264  (enum {LEN = 1024}; char buffer[LEN];
265  const std::string label = tracker.template get_label<MemorySpace>();
266  int n = snprintf(buffer, LEN, "DynRankView bounds error of view %s (",
267  label.c_str());
268  dyn_rank_view_error_operator_bounds<0>(buffer + n, LEN - n, map,
269  args...);
270  Kokkos::Impl::throw_runtime_exception(std::string(buffer));))
271 
272  KOKKOS_IF_ON_DEVICE(
273  ((void)tracker; Kokkos::abort("DynRankView bounds error");))
274  }
275 }
276 
279 
280 } // namespace Impl
281 
282 namespace Impl {
283 
284 template <class DstTraits, class SrcTraits>
285 class ViewMapping<
286  DstTraits, SrcTraits,
287  std::enable_if_t<(std::is_same<typename DstTraits::memory_space,
288  typename SrcTraits::memory_space>::value &&
289  std::is_void<typename DstTraits::specialize>::value &&
290  std::is_void<typename SrcTraits::specialize>::value &&
291  (std::is_same<typename DstTraits::array_layout,
292  typename SrcTraits::array_layout>::value ||
293  ((std::is_same<typename DstTraits::array_layout,
294  Kokkos::LayoutLeft>::value ||
295  std::is_same<typename DstTraits::array_layout,
296  Kokkos::LayoutRight>::value ||
297  std::is_same<typename DstTraits::array_layout,
298  Kokkos::LayoutStride>::value) &&
299  (std::is_same<typename SrcTraits::array_layout,
300  Kokkos::LayoutLeft>::value ||
301  std::is_same<typename SrcTraits::array_layout,
302  Kokkos::LayoutRight>::value ||
303  std::is_same<typename SrcTraits::array_layout,
304  Kokkos::LayoutStride>::value)))),
305  Kokkos::Impl::ViewToDynRankViewTag>> {
306  private:
307  enum {
308  is_assignable_value_type =
309  std::is_same<typename DstTraits::value_type,
310  typename SrcTraits::value_type>::value ||
311  std::is_same<typename DstTraits::value_type,
312  typename SrcTraits::const_value_type>::value
313  };
314 
315  enum {
316  is_assignable_layout =
317  std::is_same<typename DstTraits::array_layout,
318  typename SrcTraits::array_layout>::value ||
319  std::is_same<typename DstTraits::array_layout,
320  Kokkos::LayoutStride>::value
321  };
322 
323  public:
324  enum { is_assignable = is_assignable_value_type && is_assignable_layout };
325 
326  using DstType = ViewMapping<DstTraits, typename DstTraits::specialize>;
327  using SrcType = ViewMapping<SrcTraits, typename SrcTraits::specialize>;
328 
329  template <typename DT, typename... DP, typename ST, typename... SP>
330  KOKKOS_INLINE_FUNCTION static void assign(
331  Kokkos::DynRankView<DT, DP...>& dst, const Kokkos::View<ST, SP...>& src) {
332  static_assert(
333  is_assignable_value_type,
334  "View assignment must have same value type or const = non-const");
335 
336  static_assert(
337  is_assignable_layout,
338  "View assignment must have compatible layout or have rank <= 1");
339 
340  // Removed dimension checks...
341 
342  using dst_offset_type = typename DstType::offset_type;
343  dst.m_map.m_impl_offset = dst_offset_type(
344  std::integral_constant<unsigned, 0>(),
345  src.layout()); // Check this for integer input1 for padding, etc
346  dst.m_map.m_impl_handle = Kokkos::Impl::ViewDataHandle<DstTraits>::assign(
347  src.m_map.m_impl_handle, src.m_track.m_tracker);
348  dst.m_track.assign(src.m_track.m_tracker, DstTraits::is_managed);
349  dst.m_rank = src.Rank;
350  }
351 };
352 
353 } // namespace Impl
354 
355 /* \class DynRankView
356  * \brief Container that creates a Kokkos view with rank determined at runtime.
357  * Essentially this is a rank 7 view
358  *
359  * Changes from View
360  * 1. The rank of the DynRankView is returned by the method rank()
361  * 2. Max rank of a DynRankView is 7
362  * 3. subview called with 'subview(...)' or 'subdynrankview(...)' (backward
363  * compatibility)
364  * 4. Every subview is returned with LayoutStride
365  * 5. Copy and Copy-Assign View to DynRankView
366  * 6. deep_copy between Views and DynRankViews
367  * 7. rank( view ); returns the rank of View or DynRankView
368  *
369  */
370 
371 template <class>
372 struct is_dyn_rank_view : public std::false_type {};
373 
374 template <class D, class... P>
375 struct is_dyn_rank_view<Kokkos::DynRankView<D, P...>> : public std::true_type {
376 };
377 
378 template <typename DataType, class... Properties>
379 class DynRankView : public ViewTraits<DataType, Properties...> {
380  static_assert(!std::is_array<DataType>::value &&
381  !std::is_pointer<DataType>::value,
382  "Cannot template DynRankView with array or pointer datatype - "
383  "must be pod");
384 
385  private:
386  template <class, class...>
387  friend class DynRankView;
388  template <class, class...>
389  friend class Kokkos::Impl::ViewMapping;
390 
391  public:
392  using drvtraits = ViewTraits<DataType, Properties...>;
393 
394  using view_type = View<DataType*******, Properties...>;
395 
396  using traits = ViewTraits<DataType*******, Properties...>;
397 
398  private:
399  using map_type =
400  Kokkos::Impl::ViewMapping<traits, typename traits::specialize>;
401  using track_type = Kokkos::Impl::SharedAllocationTracker;
402 
403  track_type m_track;
404  map_type m_map;
405  unsigned m_rank;
406 
407  public:
408  KOKKOS_INLINE_FUNCTION
409  view_type& DownCast() const { return (view_type&)(*this); }
410  KOKKOS_INLINE_FUNCTION
411  const view_type& ConstDownCast() const { return (const view_type&)(*this); }
412 
413  // Types below - at least the HostMirror requires the value_type, NOT the rank
414  // 7 data_type of the traits
415 
417  using array_type = DynRankView<
418  typename drvtraits::scalar_array_type, typename drvtraits::array_layout,
419  typename drvtraits::device_type, typename drvtraits::memory_traits>;
420 
422  using const_type = DynRankView<
423  typename drvtraits::const_data_type, typename drvtraits::array_layout,
424  typename drvtraits::device_type, typename drvtraits::memory_traits>;
425 
427  using non_const_type = DynRankView<
428  typename drvtraits::non_const_data_type, typename drvtraits::array_layout,
429  typename drvtraits::device_type, typename drvtraits::memory_traits>;
430 
432  using HostMirror = DynRankView<typename drvtraits::non_const_data_type,
433  typename drvtraits::array_layout,
434  typename drvtraits::host_mirror_space>;
435 
436  //----------------------------------------
437  // Domain rank and extents
438 
439  // enum { Rank = map_type::Rank }; //Will be dyn rank of 7 always, keep the
440  // enum?
441 
442  template <typename iType>
443  KOKKOS_INLINE_FUNCTION constexpr std::enable_if_t<
444  std::is_integral<iType>::value, size_t>
445  extent(const iType& r) const {
446  return m_map.extent(r);
447  }
448 
449  template <typename iType>
450  KOKKOS_INLINE_FUNCTION constexpr std::enable_if_t<
451  std::is_integral<iType>::value, int>
452  extent_int(const iType& r) const {
453  return static_cast<int>(m_map.extent(r));
454  }
455 
456  KOKKOS_INLINE_FUNCTION constexpr typename traits::array_layout layout() const;
457 
458  //----------------------------------------
459  /* Deprecate all 'dimension' functions in favor of
460  * ISO/C++ vocabulary 'extent'.
461  */
462 
463  KOKKOS_INLINE_FUNCTION constexpr size_t size() const {
464  return m_map.extent(0) * m_map.extent(1) * m_map.extent(2) *
465  m_map.extent(3) * m_map.extent(4) * m_map.extent(5) *
466  m_map.extent(6) * m_map.extent(7);
467  }
468 
469  KOKKOS_INLINE_FUNCTION constexpr size_t stride_0() const {
470  return m_map.stride_0();
471  }
472  KOKKOS_INLINE_FUNCTION constexpr size_t stride_1() const {
473  return m_map.stride_1();
474  }
475  KOKKOS_INLINE_FUNCTION constexpr size_t stride_2() const {
476  return m_map.stride_2();
477  }
478  KOKKOS_INLINE_FUNCTION constexpr size_t stride_3() const {
479  return m_map.stride_3();
480  }
481  KOKKOS_INLINE_FUNCTION constexpr size_t stride_4() const {
482  return m_map.stride_4();
483  }
484  KOKKOS_INLINE_FUNCTION constexpr size_t stride_5() const {
485  return m_map.stride_5();
486  }
487  KOKKOS_INLINE_FUNCTION constexpr size_t stride_6() const {
488  return m_map.stride_6();
489  }
490  KOKKOS_INLINE_FUNCTION constexpr size_t stride_7() const {
491  return m_map.stride_7();
492  }
493 
494  template <typename iType>
495  KOKKOS_INLINE_FUNCTION void stride(iType* const s) const {
496  m_map.stride(s);
497  }
498 
499  //----------------------------------------
500  // Range span is the span which contains all members.
501 
502  using reference_type = typename map_type::reference_type;
503  using pointer_type = typename map_type::pointer_type;
504 
505  enum {
506  reference_type_is_lvalue_reference =
507  std::is_lvalue_reference<reference_type>::value
508  };
509 
510  KOKKOS_INLINE_FUNCTION constexpr size_t span() const { return m_map.span(); }
511  KOKKOS_INLINE_FUNCTION constexpr bool span_is_contiguous() const {
512  return m_map.span_is_contiguous();
513  }
514  KOKKOS_INLINE_FUNCTION constexpr pointer_type data() const {
515  return m_map.data();
516  }
517  KOKKOS_INLINE_FUNCTION constexpr bool is_allocated() const {
518  return (m_map.data() != nullptr);
519  }
520 
521  //----------------------------------------
522  // Allow specializations to query their specialized map
523  KOKKOS_INLINE_FUNCTION
524  const Kokkos::Impl::ViewMapping<traits, typename traits::specialize>&
525  impl_map() const {
526  return m_map;
527  }
528 
529  //----------------------------------------
530 
531  private:
532  enum {
533  is_layout_left =
534  std::is_same<typename traits::array_layout, Kokkos::LayoutLeft>::value,
535 
536  is_layout_right =
537  std::is_same<typename traits::array_layout, Kokkos::LayoutRight>::value,
538 
539  is_layout_stride = std::is_same<typename traits::array_layout,
540  Kokkos::LayoutStride>::value,
541 
542  is_default_map = std::is_void<typename traits::specialize>::value &&
543  (is_layout_left || is_layout_right || is_layout_stride)
544  };
545 
546 // Bounds checking macros
547 #if defined(KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK)
548 
549 // rank of the calling operator - included as first argument in ARG
550 #define KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(ARG) \
551  Kokkos::Impl::runtime_check_memory_access_violation< \
552  typename traits::memory_space>( \
553  "Kokkos::DynRankView ERROR: attempt to access inaccessible memory " \
554  "space"); \
555  Kokkos::Impl::dyn_rank_view_verify_operator_bounds< \
556  typename traits::memory_space> \
557  ARG;
558 
559 #else
560 
561 #define KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(ARG) \
562  Kokkos::Impl::runtime_check_memory_access_violation< \
563  typename traits::memory_space>( \
564  "Kokkos::DynRankView ERROR: attempt to access inaccessible memory " \
565  "space");
566 
567 #endif
568 
569  public:
570  KOKKOS_INLINE_FUNCTION
571  constexpr unsigned rank() const { return m_rank; }
572 
573  // operators ()
574  // Rank 0
575  KOKKOS_INLINE_FUNCTION
576  reference_type operator()() const {
577  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY((0, this->rank(), m_track, m_map))
578  return impl_map().reference();
579  // return m_map.reference(0,0,0,0,0,0,0);
580  }
581 
582  // Rank 1
583  // This assumes a contiguous underlying memory (i.e. no padding, no
584  // striding...)
585  template <typename iType>
586  KOKKOS_INLINE_FUNCTION std::enable_if_t<
587  std::is_same<typename drvtraits::value_type,
588  typename drvtraits::scalar_array_type>::value &&
589  std::is_integral<iType>::value,
590  reference_type>
591  operator[](const iType& i0) const {
592  // Phalanx is violating this, since they use the operator to access ALL
593  // elements in the allocation KOKKOS_IMPL_VIEW_OPERATOR_VERIFY( (1 ,
594  // this->rank(), m_track, m_map) )
595  return data()[i0];
596  }
597 
598  // This assumes a contiguous underlying memory (i.e. no padding, no
599  // striding... AND a Trilinos/Sacado scalar type )
600  template <typename iType>
601  KOKKOS_INLINE_FUNCTION std::enable_if_t<
602  !std::is_same<typename drvtraits::value_type,
603  typename drvtraits::scalar_array_type>::value &&
604  std::is_integral<iType>::value,
605  reference_type>
606  operator[](const iType& i0) const {
607  // auto map = impl_map();
608  const size_t dim_scalar = m_map.dimension_scalar();
609  const size_t bytes = this->span() / dim_scalar;
610 
611  using tmp_view_type = Kokkos::View<
612  DataType*, typename traits::array_layout, typename traits::device_type,
613  Kokkos::MemoryTraits<traits::memory_traits::is_unmanaged |
614  traits::memory_traits::is_random_access |
615  traits::memory_traits::is_atomic>>;
616  tmp_view_type rankone_view(this->data(), bytes, dim_scalar);
617  return rankone_view(i0);
618  }
619 
620  // Rank 1 parenthesis
621  template <typename iType>
622  KOKKOS_INLINE_FUNCTION
623  std::enable_if_t<(std::is_void<typename traits::specialize>::value &&
624  std::is_integral<iType>::value),
625  reference_type>
626  operator()(const iType& i0) const {
627  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY((1, this->rank(), m_track, m_map, i0))
628  return m_map.reference(i0);
629  }
630 
631  template <typename iType>
632  KOKKOS_INLINE_FUNCTION
633  std::enable_if_t<!(std::is_void<typename traits::specialize>::value &&
634  std::is_integral<iType>::value),
635  reference_type>
636  operator()(const iType& i0) const {
637  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY((1, this->rank(), m_track, m_map, i0))
638  return m_map.reference(i0, 0, 0, 0, 0, 0, 0);
639  }
640 
641  // Rank 2
642  template <typename iType0, typename iType1>
643  KOKKOS_INLINE_FUNCTION std::enable_if_t<
644  (std::is_void<typename traits::specialize>::value &&
645  std::is_integral<iType0>::value && std::is_integral<iType1>::value),
646  reference_type>
647  operator()(const iType0& i0, const iType1& i1) const {
648  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY((2, this->rank(), m_track, m_map, i0, i1))
649  return m_map.reference(i0, i1);
650  }
651 
652  template <typename iType0, typename iType1>
653  KOKKOS_INLINE_FUNCTION
654  std::enable_if_t<!(std::is_void<typename drvtraits::specialize>::value &&
655  std::is_integral<iType0>::value),
656  reference_type>
657  operator()(const iType0& i0, const iType1& i1) const {
658  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY((2, this->rank(), m_track, m_map, i0, i1))
659  return m_map.reference(i0, i1, 0, 0, 0, 0, 0);
660  }
661 
662  // Rank 3
663  template <typename iType0, typename iType1, typename iType2>
664  KOKKOS_INLINE_FUNCTION std::enable_if_t<
665  (std::is_void<typename traits::specialize>::value &&
666  std::is_integral<iType0>::value && std::is_integral<iType1>::value &&
667  std::is_integral<iType2>::value),
668  reference_type>
669  operator()(const iType0& i0, const iType1& i1, const iType2& i2) const {
670  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
671  (3, this->rank(), m_track, m_map, i0, i1, i2))
672  return m_map.reference(i0, i1, i2);
673  }
674 
675  template <typename iType0, typename iType1, typename iType2>
676  KOKKOS_INLINE_FUNCTION
677  std::enable_if_t<!(std::is_void<typename drvtraits::specialize>::value &&
678  std::is_integral<iType0>::value),
679  reference_type>
680  operator()(const iType0& i0, const iType1& i1, const iType2& i2) const {
681  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
682  (3, this->rank(), m_track, m_map, i0, i1, i2))
683  return m_map.reference(i0, i1, i2, 0, 0, 0, 0);
684  }
685 
686  // Rank 4
687  template <typename iType0, typename iType1, typename iType2, typename iType3>
688  KOKKOS_INLINE_FUNCTION std::enable_if_t<
689  (std::is_void<typename traits::specialize>::value &&
690  std::is_integral<iType0>::value && std::is_integral<iType1>::value &&
691  std::is_integral<iType2>::value && std::is_integral<iType3>::value),
692  reference_type>
693  operator()(const iType0& i0, const iType1& i1, const iType2& i2,
694  const iType3& i3) const {
695  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
696  (4, this->rank(), m_track, m_map, i0, i1, i2, i3))
697  return m_map.reference(i0, i1, i2, i3);
698  }
699 
700  template <typename iType0, typename iType1, typename iType2, typename iType3>
701  KOKKOS_INLINE_FUNCTION
702  std::enable_if_t<!(std::is_void<typename drvtraits::specialize>::value &&
703  std::is_integral<iType0>::value),
704  reference_type>
705  operator()(const iType0& i0, const iType1& i1, const iType2& i2,
706  const iType3& i3) const {
707  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
708  (4, this->rank(), m_track, m_map, i0, i1, i2, i3))
709  return m_map.reference(i0, i1, i2, i3, 0, 0, 0);
710  }
711 
712  // Rank 5
713  template <typename iType0, typename iType1, typename iType2, typename iType3,
714  typename iType4>
715  KOKKOS_INLINE_FUNCTION std::enable_if_t<
716  (std::is_void<typename traits::specialize>::value &&
717  std::is_integral<iType0>::value && std::is_integral<iType1>::value &&
718  std::is_integral<iType2>::value && std::is_integral<iType3>::value &&
719  std::is_integral<iType4>::value),
720  reference_type>
721  operator()(const iType0& i0, const iType1& i1, const iType2& i2,
722  const iType3& i3, const iType4& i4) const {
723  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
724  (5, this->rank(), m_track, m_map, i0, i1, i2, i3, i4))
725  return m_map.reference(i0, i1, i2, i3, i4);
726  }
727 
728  template <typename iType0, typename iType1, typename iType2, typename iType3,
729  typename iType4>
730  KOKKOS_INLINE_FUNCTION
731  std::enable_if_t<!(std::is_void<typename drvtraits::specialize>::value &&
732  std::is_integral<iType0>::value),
733  reference_type>
734  operator()(const iType0& i0, const iType1& i1, const iType2& i2,
735  const iType3& i3, const iType4& i4) const {
736  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
737  (5, this->rank(), m_track, m_map, i0, i1, i2, i3, i4))
738  return m_map.reference(i0, i1, i2, i3, i4, 0, 0);
739  }
740 
741  // Rank 6
742  template <typename iType0, typename iType1, typename iType2, typename iType3,
743  typename iType4, typename iType5>
744  KOKKOS_INLINE_FUNCTION std::enable_if_t<
745  (std::is_void<typename traits::specialize>::value &&
746  std::is_integral<iType0>::value && std::is_integral<iType1>::value &&
747  std::is_integral<iType2>::value && std::is_integral<iType3>::value &&
748  std::is_integral<iType4>::value && std::is_integral<iType5>::value),
749  reference_type>
750  operator()(const iType0& i0, const iType1& i1, const iType2& i2,
751  const iType3& i3, const iType4& i4, const iType5& i5) const {
752  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
753  (6, this->rank(), m_track, m_map, i0, i1, i2, i3, i4, i5))
754  return m_map.reference(i0, i1, i2, i3, i4, i5);
755  }
756 
757  template <typename iType0, typename iType1, typename iType2, typename iType3,
758  typename iType4, typename iType5>
759  KOKKOS_INLINE_FUNCTION
760  std::enable_if_t<!(std::is_void<typename drvtraits::specialize>::value &&
761  std::is_integral<iType0>::value),
762  reference_type>
763  operator()(const iType0& i0, const iType1& i1, const iType2& i2,
764  const iType3& i3, const iType4& i4, const iType5& i5) const {
765  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
766  (6, this->rank(), m_track, m_map, i0, i1, i2, i3, i4, i5))
767  return m_map.reference(i0, i1, i2, i3, i4, i5, 0);
768  }
769 
770  // Rank 7
771  template <typename iType0, typename iType1, typename iType2, typename iType3,
772  typename iType4, typename iType5, typename iType6>
773  KOKKOS_INLINE_FUNCTION std::enable_if_t<
774  (std::is_integral<iType0>::value && std::is_integral<iType1>::value &&
775  std::is_integral<iType2>::value && std::is_integral<iType3>::value &&
776  std::is_integral<iType4>::value && std::is_integral<iType5>::value &&
777  std::is_integral<iType6>::value),
778  reference_type>
779  operator()(const iType0& i0, const iType1& i1, const iType2& i2,
780  const iType3& i3, const iType4& i4, const iType5& i5,
781  const iType6& i6) const {
782  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
783  (7, this->rank(), m_track, m_map, i0, i1, i2, i3, i4, i5, i6))
784  return m_map.reference(i0, i1, i2, i3, i4, i5, i6);
785  }
786 
787  // Rank 0
788  KOKKOS_INLINE_FUNCTION
789  reference_type access() const {
790  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY((0, this->rank(), m_track, m_map))
791  return impl_map().reference();
792  // return m_map.reference(0,0,0,0,0,0,0);
793  }
794 
795  // Rank 1
796  // Rank 1 parenthesis
797  template <typename iType>
798  KOKKOS_INLINE_FUNCTION
799  std::enable_if_t<(std::is_void<typename traits::specialize>::value &&
800  std::is_integral<iType>::value),
801  reference_type>
802  access(const iType& i0) const {
803  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY((1, this->rank(), m_track, m_map, i0))
804  return m_map.reference(i0);
805  }
806 
807  template <typename iType>
808  KOKKOS_INLINE_FUNCTION
809  std::enable_if_t<!(std::is_void<typename traits::specialize>::value &&
810  std::is_integral<iType>::value),
811  reference_type>
812  access(const iType& i0) const {
813  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY((1, this->rank(), m_track, m_map, i0))
814  return m_map.reference(i0, 0, 0, 0, 0, 0, 0);
815  }
816 
817  // Rank 2
818  template <typename iType0, typename iType1>
819  KOKKOS_INLINE_FUNCTION std::enable_if_t<
820  (std::is_void<typename traits::specialize>::value &&
821  std::is_integral<iType0>::value && std::is_integral<iType1>::value),
822  reference_type>
823  access(const iType0& i0, const iType1& i1) const {
824  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY((2, this->rank(), m_track, m_map, i0, i1))
825  return m_map.reference(i0, i1);
826  }
827 
828  template <typename iType0, typename iType1>
829  KOKKOS_INLINE_FUNCTION
830  std::enable_if_t<!(std::is_void<typename drvtraits::specialize>::value &&
831  std::is_integral<iType0>::value),
832  reference_type>
833  access(const iType0& i0, const iType1& i1) const {
834  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY((2, this->rank(), m_track, m_map, i0, i1))
835  return m_map.reference(i0, i1, 0, 0, 0, 0, 0);
836  }
837 
838  // Rank 3
839  template <typename iType0, typename iType1, typename iType2>
840  KOKKOS_INLINE_FUNCTION std::enable_if_t<
841  (std::is_void<typename traits::specialize>::value &&
842  std::is_integral<iType0>::value && std::is_integral<iType1>::value &&
843  std::is_integral<iType2>::value),
844  reference_type>
845  access(const iType0& i0, const iType1& i1, const iType2& i2) const {
846  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
847  (3, this->rank(), m_track, m_map, i0, i1, i2))
848  return m_map.reference(i0, i1, i2);
849  }
850 
851  template <typename iType0, typename iType1, typename iType2>
852  KOKKOS_INLINE_FUNCTION
853  std::enable_if_t<!(std::is_void<typename drvtraits::specialize>::value &&
854  std::is_integral<iType0>::value),
855  reference_type>
856  access(const iType0& i0, const iType1& i1, const iType2& i2) const {
857  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
858  (3, this->rank(), m_track, m_map, i0, i1, i2))
859  return m_map.reference(i0, i1, i2, 0, 0, 0, 0);
860  }
861 
862  // Rank 4
863  template <typename iType0, typename iType1, typename iType2, typename iType3>
864  KOKKOS_INLINE_FUNCTION std::enable_if_t<
865  (std::is_void<typename traits::specialize>::value &&
866  std::is_integral<iType0>::value && std::is_integral<iType1>::value &&
867  std::is_integral<iType2>::value && std::is_integral<iType3>::value),
868  reference_type>
869  access(const iType0& i0, const iType1& i1, const iType2& i2,
870  const iType3& i3) const {
871  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
872  (4, this->rank(), m_track, m_map, i0, i1, i2, i3))
873  return m_map.reference(i0, i1, i2, i3);
874  }
875 
876  template <typename iType0, typename iType1, typename iType2, typename iType3>
877  KOKKOS_INLINE_FUNCTION
878  std::enable_if_t<!(std::is_void<typename drvtraits::specialize>::value &&
879  std::is_integral<iType0>::value),
880  reference_type>
881  access(const iType0& i0, const iType1& i1, const iType2& i2,
882  const iType3& i3) const {
883  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
884  (4, this->rank(), m_track, m_map, i0, i1, i2, i3))
885  return m_map.reference(i0, i1, i2, i3, 0, 0, 0);
886  }
887 
888  // Rank 5
889  template <typename iType0, typename iType1, typename iType2, typename iType3,
890  typename iType4>
891  KOKKOS_INLINE_FUNCTION std::enable_if_t<
892  (std::is_void<typename traits::specialize>::value &&
893  std::is_integral<iType0>::value && std::is_integral<iType1>::value &&
894  std::is_integral<iType2>::value && std::is_integral<iType3>::value &&
895  std::is_integral<iType4>::value),
896  reference_type>
897  access(const iType0& i0, const iType1& i1, const iType2& i2, const iType3& i3,
898  const iType4& i4) const {
899  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
900  (5, this->rank(), m_track, m_map, i0, i1, i2, i3, i4))
901  return m_map.reference(i0, i1, i2, i3, i4);
902  }
903 
904  template <typename iType0, typename iType1, typename iType2, typename iType3,
905  typename iType4>
906  KOKKOS_INLINE_FUNCTION
907  std::enable_if_t<!(std::is_void<typename drvtraits::specialize>::value &&
908  std::is_integral<iType0>::value),
909  reference_type>
910  access(const iType0& i0, const iType1& i1, const iType2& i2,
911  const iType3& i3, const iType4& i4) const {
912  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
913  (5, this->rank(), m_track, m_map, i0, i1, i2, i3, i4))
914  return m_map.reference(i0, i1, i2, i3, i4, 0, 0);
915  }
916 
917  // Rank 6
918  template <typename iType0, typename iType1, typename iType2, typename iType3,
919  typename iType4, typename iType5>
920  KOKKOS_INLINE_FUNCTION std::enable_if_t<
921  (std::is_void<typename traits::specialize>::value &&
922  std::is_integral<iType0>::value && std::is_integral<iType1>::value &&
923  std::is_integral<iType2>::value && std::is_integral<iType3>::value &&
924  std::is_integral<iType4>::value && std::is_integral<iType5>::value),
925  reference_type>
926  access(const iType0& i0, const iType1& i1, const iType2& i2, const iType3& i3,
927  const iType4& i4, const iType5& i5) const {
928  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
929  (6, this->rank(), m_track, m_map, i0, i1, i2, i3, i4, i5))
930  return m_map.reference(i0, i1, i2, i3, i4, i5);
931  }
932 
933  template <typename iType0, typename iType1, typename iType2, typename iType3,
934  typename iType4, typename iType5>
935  KOKKOS_INLINE_FUNCTION
936  std::enable_if_t<!(std::is_void<typename drvtraits::specialize>::value &&
937  std::is_integral<iType0>::value),
938  reference_type>
939  access(const iType0& i0, const iType1& i1, const iType2& i2,
940  const iType3& i3, const iType4& i4, const iType5& i5) const {
941  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
942  (6, this->rank(), m_track, m_map, i0, i1, i2, i3, i4, i5))
943  return m_map.reference(i0, i1, i2, i3, i4, i5, 0);
944  }
945 
946  // Rank 7
947  template <typename iType0, typename iType1, typename iType2, typename iType3,
948  typename iType4, typename iType5, typename iType6>
949  KOKKOS_INLINE_FUNCTION std::enable_if_t<
950  (std::is_integral<iType0>::value && std::is_integral<iType1>::value &&
951  std::is_integral<iType2>::value && std::is_integral<iType3>::value &&
952  std::is_integral<iType4>::value && std::is_integral<iType5>::value &&
953  std::is_integral<iType6>::value),
954  reference_type>
955  access(const iType0& i0, const iType1& i1, const iType2& i2, const iType3& i3,
956  const iType4& i4, const iType5& i5, const iType6& i6) const {
957  KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(
958  (7, this->rank(), m_track, m_map, i0, i1, i2, i3, i4, i5, i6))
959  return m_map.reference(i0, i1, i2, i3, i4, i5, i6);
960  }
961 
962 #undef KOKKOS_IMPL_VIEW_OPERATOR_VERIFY
963 
964  //----------------------------------------
965  // Standard constructor, destructor, and assignment operators...
966 
967  KOKKOS_DEFAULTED_FUNCTION
968  ~DynRankView() = default;
969 
970  KOKKOS_INLINE_FUNCTION
971  DynRankView() : m_track(), m_map(), m_rank() {} // Default ctor
972 
973  KOKKOS_INLINE_FUNCTION
974  DynRankView(const DynRankView& rhs)
975  : m_track(rhs.m_track), m_map(rhs.m_map), m_rank(rhs.m_rank) {}
976 
977  KOKKOS_INLINE_FUNCTION
978  DynRankView(DynRankView&& rhs)
979  : m_track(rhs.m_track), m_map(rhs.m_map), m_rank(rhs.m_rank) {}
980 
981  KOKKOS_INLINE_FUNCTION
982  DynRankView& operator=(const DynRankView& rhs) {
983  m_track = rhs.m_track;
984  m_map = rhs.m_map;
985  m_rank = rhs.m_rank;
986  return *this;
987  }
988 
989  KOKKOS_INLINE_FUNCTION
990  DynRankView& operator=(DynRankView&& rhs) {
991  m_track = rhs.m_track;
992  m_map = rhs.m_map;
993  m_rank = rhs.m_rank;
994  return *this;
995  }
996 
997  //----------------------------------------
998  // Compatible view copy constructor and assignment
999  // may assign unmanaged from managed.
1000  template <class RT, class... RP>
1001  KOKKOS_INLINE_FUNCTION DynRankView(const DynRankView<RT, RP...>& rhs)
1002  : m_track(rhs.m_track, traits::is_managed), m_map(), m_rank(rhs.m_rank) {
1003  using SrcTraits = typename DynRankView<RT, RP...>::traits;
1004  using Mapping = Kokkos::Impl::ViewMapping<traits, SrcTraits,
1005  typename traits::specialize>;
1006  static_assert(Mapping::is_assignable,
1007  "Incompatible DynRankView copy construction");
1008  Mapping::assign(m_map, rhs.m_map, rhs.m_track);
1009  }
1010 
1011  template <class RT, class... RP>
1012  KOKKOS_INLINE_FUNCTION DynRankView& operator=(
1013  const DynRankView<RT, RP...>& rhs) {
1014  using SrcTraits = typename DynRankView<RT, RP...>::traits;
1015  using Mapping = Kokkos::Impl::ViewMapping<traits, SrcTraits,
1016  typename traits::specialize>;
1017  static_assert(Mapping::is_assignable,
1018  "Incompatible DynRankView copy construction");
1019  Mapping::assign(m_map, rhs.m_map, rhs.m_track);
1020  m_track.assign(rhs.m_track, traits::is_managed);
1021  m_rank = rhs.rank();
1022  return *this;
1023  }
1024 
1025  // Copy/Assign View to DynRankView
1026  template <class RT, class... RP>
1027  KOKKOS_INLINE_FUNCTION DynRankView(const View<RT, RP...>& rhs)
1028  : m_track(), m_map(), m_rank(rhs.Rank) {
1029  using SrcTraits = typename View<RT, RP...>::traits;
1030  using Mapping =
1031  Kokkos::Impl::ViewMapping<traits, SrcTraits,
1033  static_assert(Mapping::is_assignable,
1034  "Incompatible View to DynRankView copy construction");
1035  Mapping::assign(*this, rhs);
1036  }
1037 
1038  template <class RT, class... RP>
1039  KOKKOS_INLINE_FUNCTION DynRankView& operator=(const View<RT, RP...>& rhs) {
1040  using SrcTraits = typename View<RT, RP...>::traits;
1041  using Mapping =
1042  Kokkos::Impl::ViewMapping<traits, SrcTraits,
1044  static_assert(Mapping::is_assignable,
1045  "Incompatible View to DynRankView copy assignment");
1046  Mapping::assign(*this, rhs);
1047  return *this;
1048  }
1049 
1050  //----------------------------------------
1051  // Allocation tracking properties
1052 
1053  KOKKOS_INLINE_FUNCTION
1054  int use_count() const { return m_track.use_count(); }
1055 
1056  inline const std::string label() const {
1057  return m_track.template get_label<typename traits::memory_space>();
1058  }
1059 
1060  //----------------------------------------
1061  // Allocation according to allocation properties and array layout
1062  // unused arg_layout dimensions must be set to KOKKOS_INVALID_INDEX so that
1063  // rank deduction can properly take place
1064  template <class... P>
1065  explicit inline DynRankView(
1066  const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
1067  std::enable_if_t<!Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
1068  typename traits::array_layout> const& arg_layout)
1069  : m_track(),
1070  m_map(),
1071  m_rank(Impl::DynRankDimTraits<typename traits::specialize>::
1072  template computeRank<typename traits::array_layout, P...>(
1073  arg_prop, arg_layout)) {
1074  // Copy the input allocation properties with possibly defaulted properties
1075  auto prop_copy = Impl::with_properties_if_unset(
1076  arg_prop, std::string{}, typename traits::device_type::memory_space{},
1077  typename traits::device_type::execution_space{});
1078  using alloc_prop = decltype(prop_copy);
1079 
1080  static_assert(traits::is_managed,
1081  "View allocation constructor requires managed memory");
1082 
1083  if (alloc_prop::initialize &&
1084  !alloc_prop::execution_space::impl_is_initialized()) {
1085  // If initializing view data then
1086  // the execution space must be initialized.
1087  Kokkos::Impl::throw_runtime_exception(
1088  "Constructing DynRankView and initializing data with uninitialized "
1089  "execution space");
1090  }
1091 
1092 //------------------------------------------------------------
1093 #if defined(KOKKOS_ENABLE_CUDA)
1094  // If allocating in CudaUVMSpace must fence before and after
1095  // the allocation to protect against possible concurrent access
1096  // on the CPU and the GPU.
1097  // Fence using the trait's executon space (which will be Kokkos::Cuda)
1098  // to avoid incomplete type errors from usng Kokkos::Cuda directly.
1099  if (std::is_same<Kokkos::CudaUVMSpace,
1100  typename traits::device_type::memory_space>::value) {
1101  typename traits::device_type::memory_space::execution_space().fence(
1102  "Kokkos::DynRankView<>::DynRankView: fence before UVM allocation");
1103  }
1104 #endif
1105  //------------------------------------------------------------
1106 
1107  Kokkos::Impl::SharedAllocationRecord<>* record = m_map.allocate_shared(
1108  prop_copy,
1109  Impl::DynRankDimTraits<typename traits::specialize>::
1110  template createLayout<traits, P...>(arg_prop, arg_layout),
1111  Impl::ViewCtorProp<P...>::has_execution_space);
1112 
1113 //------------------------------------------------------------
1114 #if defined(KOKKOS_ENABLE_CUDA)
1115  if (std::is_same<Kokkos::CudaUVMSpace,
1116  typename traits::device_type::memory_space>::value) {
1117  typename traits::device_type::memory_space::execution_space().fence(
1118  "Kokkos::DynRankView<>::DynRankView: fence after UVM allocation");
1119  }
1120 #endif
1121  //------------------------------------------------------------
1122 
1123  // Setup and initialization complete, start tracking
1124  m_track.assign_allocated_record_to_uninitialized(record);
1125  }
1126 
1127  // Wrappers
1128  template <class... P>
1129  explicit KOKKOS_INLINE_FUNCTION DynRankView(
1130  const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
1131  std::enable_if_t<Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
1132  typename traits::array_layout> const& arg_layout)
1133  : m_track() // No memory tracking
1134  ,
1135  m_map(arg_prop,
1136  Impl::DynRankDimTraits<typename traits::specialize>::
1137  template createLayout<traits, P...>(arg_prop, arg_layout)),
1138  m_rank(Impl::DynRankDimTraits<typename traits::specialize>::
1139  template computeRank<typename traits::array_layout, P...>(
1140  arg_prop, arg_layout)) {
1141  static_assert(
1142  std::is_same<pointer_type,
1143  typename Impl::ViewCtorProp<P...>::pointer_type>::value,
1144  "Constructing DynRankView to wrap user memory must supply matching "
1145  "pointer type");
1146  }
1147 
1148  //----------------------------------------
1149  // Constructor(s)
1150 
1151  // Simple dimension-only layout
1152  template <class... P>
1153  explicit inline DynRankView(
1154  const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
1155  std::enable_if_t<!Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
1156  size_t> const arg_N0 = KOKKOS_INVALID_INDEX,
1157  const size_t arg_N1 = KOKKOS_INVALID_INDEX,
1158  const size_t arg_N2 = KOKKOS_INVALID_INDEX,
1159  const size_t arg_N3 = KOKKOS_INVALID_INDEX,
1160  const size_t arg_N4 = KOKKOS_INVALID_INDEX,
1161  const size_t arg_N5 = KOKKOS_INVALID_INDEX,
1162  const size_t arg_N6 = KOKKOS_INVALID_INDEX,
1163  const size_t arg_N7 = KOKKOS_INVALID_INDEX)
1164  : DynRankView(arg_prop, typename traits::array_layout(
1165  arg_N0, arg_N1, arg_N2, arg_N3, arg_N4,
1166  arg_N5, arg_N6, arg_N7)) {}
1167 
1168  template <class... P>
1169  explicit KOKKOS_INLINE_FUNCTION DynRankView(
1170  const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
1171  std::enable_if_t<Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
1172  size_t> const arg_N0 = KOKKOS_INVALID_INDEX,
1173  const size_t arg_N1 = KOKKOS_INVALID_INDEX,
1174  const size_t arg_N2 = KOKKOS_INVALID_INDEX,
1175  const size_t arg_N3 = KOKKOS_INVALID_INDEX,
1176  const size_t arg_N4 = KOKKOS_INVALID_INDEX,
1177  const size_t arg_N5 = KOKKOS_INVALID_INDEX,
1178  const size_t arg_N6 = KOKKOS_INVALID_INDEX,
1179  const size_t arg_N7 = KOKKOS_INVALID_INDEX)
1180  : DynRankView(arg_prop, typename traits::array_layout(
1181  arg_N0, arg_N1, arg_N2, arg_N3, arg_N4,
1182  arg_N5, arg_N6, arg_N7)) {}
1183 
1184  // Allocate with label and layout
1185  template <typename Label>
1186  explicit inline DynRankView(
1187  const Label& arg_label,
1188  std::enable_if_t<Kokkos::Impl::is_view_label<Label>::value,
1189  typename traits::array_layout> const& arg_layout)
1190  : DynRankView(Kokkos::Impl::ViewCtorProp<std::string>(arg_label),
1191  arg_layout) {}
1192 
1193  // Allocate label and layout, must disambiguate from subview constructor
1194  template <typename Label>
1195  explicit inline DynRankView(
1196  const Label& arg_label,
1197  std::enable_if_t<Kokkos::Impl::is_view_label<Label>::value, const size_t>
1198  arg_N0 = KOKKOS_INVALID_INDEX,
1199  const size_t arg_N1 = KOKKOS_INVALID_INDEX,
1200  const size_t arg_N2 = KOKKOS_INVALID_INDEX,
1201  const size_t arg_N3 = KOKKOS_INVALID_INDEX,
1202  const size_t arg_N4 = KOKKOS_INVALID_INDEX,
1203  const size_t arg_N5 = KOKKOS_INVALID_INDEX,
1204  const size_t arg_N6 = KOKKOS_INVALID_INDEX,
1205  const size_t arg_N7 = KOKKOS_INVALID_INDEX)
1206  : DynRankView(
1207  Kokkos::Impl::ViewCtorProp<std::string>(arg_label),
1208  typename traits::array_layout(arg_N0, arg_N1, arg_N2, arg_N3,
1209  arg_N4, arg_N5, arg_N6, arg_N7)) {}
1210 
1211  //----------------------------------------
1212  // Memory span required to wrap these dimensions.
1213  static constexpr size_t required_allocation_size(
1214  const size_t arg_N0 = 0, const size_t arg_N1 = 0, const size_t arg_N2 = 0,
1215  const size_t arg_N3 = 0, const size_t arg_N4 = 0, const size_t arg_N5 = 0,
1216  const size_t arg_N6 = 0, const size_t arg_N7 = 0) {
1217  return map_type::memory_span(typename traits::array_layout(
1218  arg_N0, arg_N1, arg_N2, arg_N3, arg_N4, arg_N5, arg_N6, arg_N7));
1219  }
1220 
1221  explicit KOKKOS_INLINE_FUNCTION DynRankView(
1222  pointer_type arg_ptr, const size_t arg_N0 = KOKKOS_INVALID_INDEX,
1223  const size_t arg_N1 = KOKKOS_INVALID_INDEX,
1224  const size_t arg_N2 = KOKKOS_INVALID_INDEX,
1225  const size_t arg_N3 = KOKKOS_INVALID_INDEX,
1226  const size_t arg_N4 = KOKKOS_INVALID_INDEX,
1227  const size_t arg_N5 = KOKKOS_INVALID_INDEX,
1228  const size_t arg_N6 = KOKKOS_INVALID_INDEX,
1229  const size_t arg_N7 = KOKKOS_INVALID_INDEX)
1230  : DynRankView(Kokkos::Impl::ViewCtorProp<pointer_type>(arg_ptr), arg_N0,
1231  arg_N1, arg_N2, arg_N3, arg_N4, arg_N5, arg_N6, arg_N7) {}
1232 
1233  explicit KOKKOS_INLINE_FUNCTION DynRankView(
1234  pointer_type arg_ptr, typename traits::array_layout& arg_layout)
1235  : DynRankView(Kokkos::Impl::ViewCtorProp<pointer_type>(arg_ptr),
1236  arg_layout) {}
1237 
1238  //----------------------------------------
1239  // Shared scratch memory constructor
1240 
1241  static inline size_t shmem_size(const size_t arg_N0 = KOKKOS_INVALID_INDEX,
1242  const size_t arg_N1 = KOKKOS_INVALID_INDEX,
1243  const size_t arg_N2 = KOKKOS_INVALID_INDEX,
1244  const size_t arg_N3 = KOKKOS_INVALID_INDEX,
1245  const size_t arg_N4 = KOKKOS_INVALID_INDEX,
1246  const size_t arg_N5 = KOKKOS_INVALID_INDEX,
1247  const size_t arg_N6 = KOKKOS_INVALID_INDEX,
1248  const size_t arg_N7 = KOKKOS_INVALID_INDEX) {
1249  const size_t num_passed_args =
1250  (arg_N0 != KOKKOS_INVALID_INDEX) + (arg_N1 != KOKKOS_INVALID_INDEX) +
1251  (arg_N2 != KOKKOS_INVALID_INDEX) + (arg_N3 != KOKKOS_INVALID_INDEX) +
1252  (arg_N4 != KOKKOS_INVALID_INDEX) + (arg_N5 != KOKKOS_INVALID_INDEX) +
1253  (arg_N6 != KOKKOS_INVALID_INDEX) + (arg_N7 != KOKKOS_INVALID_INDEX);
1254 
1255  if (std::is_void<typename traits::specialize>::value &&
1256  num_passed_args != traits::rank_dynamic) {
1257  Kokkos::abort(
1258  "Kokkos::View::shmem_size() rank_dynamic != number of arguments.\n");
1259  }
1260  {}
1261 
1262  return map_type::memory_span(typename traits::array_layout(
1263  arg_N0, arg_N1, arg_N2, arg_N3, arg_N4, arg_N5, arg_N6, arg_N7));
1264  }
1265 
1266  explicit KOKKOS_INLINE_FUNCTION DynRankView(
1267  const typename traits::execution_space::scratch_memory_space& arg_space,
1268  const typename traits::array_layout& arg_layout)
1269  : DynRankView(
1270  Kokkos::Impl::ViewCtorProp<pointer_type>(
1271  reinterpret_cast<pointer_type>(
1272  arg_space.get_shmem(map_type::memory_span(
1273  Impl::DynRankDimTraits<typename traits::specialize>::
1274  createLayout(arg_layout) // is this correct?
1275  )))),
1276  arg_layout) {}
1277 
1278  explicit KOKKOS_INLINE_FUNCTION DynRankView(
1279  const typename traits::execution_space::scratch_memory_space& arg_space,
1280  const size_t arg_N0 = KOKKOS_INVALID_INDEX,
1281  const size_t arg_N1 = KOKKOS_INVALID_INDEX,
1282  const size_t arg_N2 = KOKKOS_INVALID_INDEX,
1283  const size_t arg_N3 = KOKKOS_INVALID_INDEX,
1284  const size_t arg_N4 = KOKKOS_INVALID_INDEX,
1285  const size_t arg_N5 = KOKKOS_INVALID_INDEX,
1286  const size_t arg_N6 = KOKKOS_INVALID_INDEX,
1287  const size_t arg_N7 = KOKKOS_INVALID_INDEX)
1288 
1289  : DynRankView(
1290  Kokkos::Impl::ViewCtorProp<pointer_type>(
1291  reinterpret_cast<pointer_type>(
1292  arg_space.get_shmem(map_type::memory_span(
1293  Impl::DynRankDimTraits<typename traits::specialize>::
1294  createLayout(typename traits::array_layout(
1295  arg_N0, arg_N1, arg_N2, arg_N3, arg_N4, arg_N5,
1296  arg_N6, arg_N7)))))),
1297  typename traits::array_layout(arg_N0, arg_N1, arg_N2, arg_N3,
1298  arg_N4, arg_N5, arg_N6, arg_N7)) {}
1299 };
1300 
1301 template <typename D, class... P>
1302 KOKKOS_INLINE_FUNCTION constexpr unsigned rank(
1303  const DynRankView<D, P...>& DRV) {
1304  return DRV.rank();
1305 } // needed for transition to common constexpr method in view and dynrankview
1306  // to return rank
1307 
1308 //----------------------------------------------------------------------------
1309 // Subview mapping.
1310 // Deduce destination view type from source view traits and subview arguments
1311 
1312 namespace Impl {
1313 
1314 struct DynRankSubviewTag {};
1315 
1316 } // namespace Impl
1317 
1318 namespace Impl {
1319 
1320 template <class SrcTraits, class... Args>
1321 class ViewMapping<
1322  std::enable_if_t<(std::is_void<typename SrcTraits::specialize>::value &&
1323  (std::is_same<typename SrcTraits::array_layout,
1324  Kokkos::LayoutLeft>::value ||
1325  std::is_same<typename SrcTraits::array_layout,
1326  Kokkos::LayoutRight>::value ||
1327  std::is_same<typename SrcTraits::array_layout,
1328  Kokkos::LayoutStride>::value)),
1329  Kokkos::Impl::DynRankSubviewTag>,
1330  SrcTraits, Args...> {
1331  private:
1332  enum {
1333  RZ = false,
1334  R0 = bool(is_integral_extent<0, Args...>::value),
1335  R1 = bool(is_integral_extent<1, Args...>::value),
1336  R2 = bool(is_integral_extent<2, Args...>::value),
1337  R3 = bool(is_integral_extent<3, Args...>::value),
1338  R4 = bool(is_integral_extent<4, Args...>::value),
1339  R5 = bool(is_integral_extent<5, Args...>::value),
1340  R6 = bool(is_integral_extent<6, Args...>::value)
1341  };
1342 
1343  enum {
1344  rank = unsigned(R0) + unsigned(R1) + unsigned(R2) + unsigned(R3) +
1345  unsigned(R4) + unsigned(R5) + unsigned(R6)
1346  };
1347 
1348  using array_layout = Kokkos::LayoutStride;
1349 
1350  using value_type = typename SrcTraits::value_type;
1351 
1352  using data_type = value_type*******;
1353 
1354  public:
1355  using traits_type = Kokkos::ViewTraits<data_type, array_layout,
1356  typename SrcTraits::device_type,
1357  typename SrcTraits::memory_traits>;
1358 
1359  using type =
1360  Kokkos::View<data_type, array_layout, typename SrcTraits::device_type,
1361  typename SrcTraits::memory_traits>;
1362 
1363  template <class MemoryTraits>
1364  struct apply {
1365  static_assert(Kokkos::is_memory_traits<MemoryTraits>::value, "");
1366 
1367  using traits_type =
1368  Kokkos::ViewTraits<data_type, array_layout,
1369  typename SrcTraits::device_type, MemoryTraits>;
1370 
1371  using type = Kokkos::View<data_type, array_layout,
1372  typename SrcTraits::device_type, MemoryTraits>;
1373  };
1374 
1375  using dimension = typename SrcTraits::dimension;
1376 
1377  template <class Arg0 = int, class Arg1 = int, class Arg2 = int,
1378  class Arg3 = int, class Arg4 = int, class Arg5 = int,
1379  class Arg6 = int>
1380  struct ExtentGenerator {
1381  KOKKOS_INLINE_FUNCTION
1382  static SubviewExtents<7, rank> generator(
1383  const dimension& dim, Arg0 arg0 = Arg0(), Arg1 arg1 = Arg1(),
1384  Arg2 arg2 = Arg2(), Arg3 arg3 = Arg3(), Arg4 arg4 = Arg4(),
1385  Arg5 arg5 = Arg5(), Arg6 arg6 = Arg6()) {
1386  return SubviewExtents<7, rank>(dim, arg0, arg1, arg2, arg3, arg4, arg5,
1387  arg6);
1388  }
1389  };
1390 
1391  using ret_type = Kokkos::DynRankView<value_type, array_layout,
1392  typename SrcTraits::device_type,
1393  typename SrcTraits::memory_traits>;
1394 
1395  template <typename T, class... P>
1396  KOKKOS_INLINE_FUNCTION static ret_type subview(
1397  const unsigned src_rank, Kokkos::DynRankView<T, P...> const& src,
1398  Args... args) {
1399  using DstType = ViewMapping<traits_type, typename traits_type::specialize>;
1400 
1401  using DstDimType = std::conditional_t<
1402  (rank == 0), ViewDimension<>,
1403  std::conditional_t<
1404  (rank == 1), ViewDimension<0>,
1405  std::conditional_t<
1406  (rank == 2), ViewDimension<0, 0>,
1407  std::conditional_t<
1408  (rank == 3), ViewDimension<0, 0, 0>,
1409  std::conditional_t<
1410  (rank == 4), ViewDimension<0, 0, 0, 0>,
1411  std::conditional_t<
1412  (rank == 5), ViewDimension<0, 0, 0, 0, 0>,
1413  std::conditional_t<
1414  (rank == 6), ViewDimension<0, 0, 0, 0, 0, 0>,
1415  ViewDimension<0, 0, 0, 0, 0, 0, 0>>>>>>>>;
1416 
1417  using dst_offset_type = ViewOffset<DstDimType, Kokkos::LayoutStride>;
1418  using dst_handle_type = typename DstType::handle_type;
1419 
1420  ret_type dst;
1421 
1422  const SubviewExtents<7, rank> extents = ExtentGenerator<Args...>::generator(
1423  src.m_map.m_impl_offset.m_dim, args...);
1424 
1425  dst_offset_type tempdst(src.m_map.m_impl_offset, extents);
1426 
1427  dst.m_track = src.m_track;
1428 
1429  dst.m_map.m_impl_offset.m_dim.N0 = tempdst.m_dim.N0;
1430  dst.m_map.m_impl_offset.m_dim.N1 = tempdst.m_dim.N1;
1431  dst.m_map.m_impl_offset.m_dim.N2 = tempdst.m_dim.N2;
1432  dst.m_map.m_impl_offset.m_dim.N3 = tempdst.m_dim.N3;
1433  dst.m_map.m_impl_offset.m_dim.N4 = tempdst.m_dim.N4;
1434  dst.m_map.m_impl_offset.m_dim.N5 = tempdst.m_dim.N5;
1435  dst.m_map.m_impl_offset.m_dim.N6 = tempdst.m_dim.N6;
1436 
1437  dst.m_map.m_impl_offset.m_stride.S0 = tempdst.m_stride.S0;
1438  dst.m_map.m_impl_offset.m_stride.S1 = tempdst.m_stride.S1;
1439  dst.m_map.m_impl_offset.m_stride.S2 = tempdst.m_stride.S2;
1440  dst.m_map.m_impl_offset.m_stride.S3 = tempdst.m_stride.S3;
1441  dst.m_map.m_impl_offset.m_stride.S4 = tempdst.m_stride.S4;
1442  dst.m_map.m_impl_offset.m_stride.S5 = tempdst.m_stride.S5;
1443  dst.m_map.m_impl_offset.m_stride.S6 = tempdst.m_stride.S6;
1444 
1445  dst.m_map.m_impl_handle =
1446  dst_handle_type(src.m_map.m_impl_handle +
1447  src.m_map.m_impl_offset(
1448  extents.domain_offset(0), extents.domain_offset(1),
1449  extents.domain_offset(2), extents.domain_offset(3),
1450  extents.domain_offset(4), extents.domain_offset(5),
1451  extents.domain_offset(6)));
1452 
1453  dst.m_rank =
1454  (src_rank > 0 ? unsigned(R0) : 0) + (src_rank > 1 ? unsigned(R1) : 0) +
1455  (src_rank > 2 ? unsigned(R2) : 0) + (src_rank > 3 ? unsigned(R3) : 0) +
1456  (src_rank > 4 ? unsigned(R4) : 0) + (src_rank > 5 ? unsigned(R5) : 0) +
1457  (src_rank > 6 ? unsigned(R6) : 0);
1458 
1459  return dst;
1460  }
1461 };
1462 
1463 } // namespace Impl
1464 
1465 template <class V, class... Args>
1466 using Subdynrankview =
1467  typename Kokkos::Impl::ViewMapping<Kokkos::Impl::DynRankSubviewTag, V,
1468  Args...>::ret_type;
1469 
1470 template <class D, class... P, class... Args>
1471 KOKKOS_INLINE_FUNCTION Subdynrankview<ViewTraits<D*******, P...>, Args...>
1472 subdynrankview(const Kokkos::DynRankView<D, P...>& src, Args... args) {
1473  if (src.rank() > sizeof...(Args)) // allow sizeof...(Args) >= src.rank(),
1474  // ignore the remaining args
1475  {
1476  Kokkos::abort(
1477  "subdynrankview: num of args must be >= rank of the source "
1478  "DynRankView");
1479  }
1480 
1481  using metafcn =
1482  Kokkos::Impl::ViewMapping<Kokkos::Impl::DynRankSubviewTag,
1483  Kokkos::ViewTraits<D*******, P...>, Args...>;
1484 
1485  return metafcn::subview(src.rank(), src, args...);
1486 }
1487 
1488 // Wrapper to allow subview function name
1489 template <class D, class... P, class... Args>
1490 KOKKOS_INLINE_FUNCTION Subdynrankview<ViewTraits<D*******, P...>, Args...>
1491 subview(const Kokkos::DynRankView<D, P...>& src, Args... args) {
1492  return subdynrankview(src, args...);
1493 }
1494 
1495 } // namespace Kokkos
1496 
1497 namespace Kokkos {
1498 
1499 // overload == and !=
1500 template <class LT, class... LP, class RT, class... RP>
1501 KOKKOS_INLINE_FUNCTION bool operator==(const DynRankView<LT, LP...>& lhs,
1502  const DynRankView<RT, RP...>& rhs) {
1503  // Same data, layout, dimensions
1504  using lhs_traits = ViewTraits<LT, LP...>;
1505  using rhs_traits = ViewTraits<RT, RP...>;
1506 
1507  return std::is_same<typename lhs_traits::const_value_type,
1508  typename rhs_traits::const_value_type>::value &&
1509  std::is_same<typename lhs_traits::array_layout,
1510  typename rhs_traits::array_layout>::value &&
1511  std::is_same<typename lhs_traits::memory_space,
1512  typename rhs_traits::memory_space>::value &&
1513  lhs.rank() == rhs.rank() && lhs.data() == rhs.data() &&
1514  lhs.span() == rhs.span() && lhs.extent(0) == rhs.extent(0) &&
1515  lhs.extent(1) == rhs.extent(1) && lhs.extent(2) == rhs.extent(2) &&
1516  lhs.extent(3) == rhs.extent(3) && lhs.extent(4) == rhs.extent(4) &&
1517  lhs.extent(5) == rhs.extent(5) && lhs.extent(6) == rhs.extent(6) &&
1518  lhs.extent(7) == rhs.extent(7);
1519 }
1520 
1521 template <class LT, class... LP, class RT, class... RP>
1522 KOKKOS_INLINE_FUNCTION bool operator!=(const DynRankView<LT, LP...>& lhs,
1523  const DynRankView<RT, RP...>& rhs) {
1524  return !(operator==(lhs, rhs));
1525 }
1526 
1527 } // namespace Kokkos
1528 
1529 //----------------------------------------------------------------------------
1530 //----------------------------------------------------------------------------
1531 namespace Kokkos {
1532 namespace Impl {
1533 
1534 template <class OutputView, class Enable = void>
1535 struct DynRankViewFill {
1536  using const_value_type = typename OutputView::traits::const_value_type;
1537 
1538  const OutputView output;
1539  const_value_type input;
1540 
1541  KOKKOS_INLINE_FUNCTION
1542  void operator()(const size_t i0) const {
1543  const size_t n1 = output.extent(1);
1544  const size_t n2 = output.extent(2);
1545  const size_t n3 = output.extent(3);
1546  const size_t n4 = output.extent(4);
1547  const size_t n5 = output.extent(5);
1548  const size_t n6 = output.extent(6);
1549 
1550  for (size_t i1 = 0; i1 < n1; ++i1) {
1551  for (size_t i2 = 0; i2 < n2; ++i2) {
1552  for (size_t i3 = 0; i3 < n3; ++i3) {
1553  for (size_t i4 = 0; i4 < n4; ++i4) {
1554  for (size_t i5 = 0; i5 < n5; ++i5) {
1555  for (size_t i6 = 0; i6 < n6; ++i6) {
1556  output.access(i0, i1, i2, i3, i4, i5, i6) = input;
1557  }
1558  }
1559  }
1560  }
1561  }
1562  }
1563  }
1564 
1565  DynRankViewFill(const OutputView& arg_out, const_value_type& arg_in)
1566  : output(arg_out), input(arg_in) {
1567  using execution_space = typename OutputView::execution_space;
1568  using Policy = Kokkos::RangePolicy<execution_space>;
1569 
1570  Kokkos::parallel_for("Kokkos::DynRankViewFill", Policy(0, output.extent(0)),
1571  *this);
1572  }
1573 };
1574 
1575 template <class OutputView>
1576 struct DynRankViewFill<OutputView, std::enable_if_t<OutputView::Rank == 0>> {
1577  DynRankViewFill(const OutputView& dst,
1578  const typename OutputView::const_value_type& src) {
1579  Kokkos::Impl::DeepCopy<typename OutputView::memory_space,
1581  dst.data(), &src, sizeof(typename OutputView::const_value_type));
1582  }
1583 };
1584 
1585 template <class OutputView, class InputView,
1586  class ExecSpace = typename OutputView::execution_space>
1587 struct DynRankViewRemap {
1588  const OutputView output;
1589  const InputView input;
1590  const size_t n0;
1591  const size_t n1;
1592  const size_t n2;
1593  const size_t n3;
1594  const size_t n4;
1595  const size_t n5;
1596  const size_t n6;
1597  const size_t n7;
1598 
1599  DynRankViewRemap(const ExecSpace& exec_space, const OutputView& arg_out,
1600  const InputView& arg_in)
1601  : output(arg_out),
1602  input(arg_in),
1603  n0(std::min((size_t)arg_out.extent(0), (size_t)arg_in.extent(0))),
1604  n1(std::min((size_t)arg_out.extent(1), (size_t)arg_in.extent(1))),
1605  n2(std::min((size_t)arg_out.extent(2), (size_t)arg_in.extent(2))),
1606  n3(std::min((size_t)arg_out.extent(3), (size_t)arg_in.extent(3))),
1607  n4(std::min((size_t)arg_out.extent(4), (size_t)arg_in.extent(4))),
1608  n5(std::min((size_t)arg_out.extent(5), (size_t)arg_in.extent(5))),
1609  n6(std::min((size_t)arg_out.extent(6), (size_t)arg_in.extent(6))),
1610  n7(std::min((size_t)arg_out.extent(7), (size_t)arg_in.extent(7))) {
1611  using Policy = Kokkos::RangePolicy<ExecSpace>;
1612 
1613  Kokkos::parallel_for("Kokkos::DynRankViewRemap", Policy(exec_space, 0, n0),
1614  *this);
1615  }
1616 
1617  DynRankViewRemap(const OutputView& arg_out, const InputView& arg_in)
1618  : output(arg_out),
1619  input(arg_in),
1620  n0(std::min((size_t)arg_out.extent(0), (size_t)arg_in.extent(0))),
1621  n1(std::min((size_t)arg_out.extent(1), (size_t)arg_in.extent(1))),
1622  n2(std::min((size_t)arg_out.extent(2), (size_t)arg_in.extent(2))),
1623  n3(std::min((size_t)arg_out.extent(3), (size_t)arg_in.extent(3))),
1624  n4(std::min((size_t)arg_out.extent(4), (size_t)arg_in.extent(4))),
1625  n5(std::min((size_t)arg_out.extent(5), (size_t)arg_in.extent(5))),
1626  n6(std::min((size_t)arg_out.extent(6), (size_t)arg_in.extent(6))),
1627  n7(std::min((size_t)arg_out.extent(7), (size_t)arg_in.extent(7))) {
1628  using Policy = Kokkos::RangePolicy<ExecSpace>;
1629 
1630  Kokkos::parallel_for("Kokkos::DynRankViewRemap", Policy(0, n0), *this);
1631  }
1632 
1633  KOKKOS_INLINE_FUNCTION
1634  void operator()(const size_t i0) const {
1635  for (size_t i1 = 0; i1 < n1; ++i1) {
1636  for (size_t i2 = 0; i2 < n2; ++i2) {
1637  for (size_t i3 = 0; i3 < n3; ++i3) {
1638  for (size_t i4 = 0; i4 < n4; ++i4) {
1639  for (size_t i5 = 0; i5 < n5; ++i5) {
1640  for (size_t i6 = 0; i6 < n6; ++i6) {
1641  output.access(i0, i1, i2, i3, i4, i5, i6) =
1642  input.access(i0, i1, i2, i3, i4, i5, i6);
1643  }
1644  }
1645  }
1646  }
1647  }
1648  }
1649  }
1650 };
1651 
1652 } /* namespace Impl */
1653 } /* namespace Kokkos */
1654 
1655 namespace Kokkos {
1656 
1657 namespace Impl {
1658 
1659 /* \brief Returns a View of the requested rank, aliasing the
1660  underlying memory, to facilitate implementation of deep_copy() and
1661  other routines that are defined on View */
1662 template <unsigned N, typename T, typename... Args>
1663 KOKKOS_FUNCTION auto as_view_of_rank_n(
1664  DynRankView<T, Args...> v,
1665  typename std::enable_if<std::is_same<
1666  typename ViewTraits<T, Args...>::specialize, void>::value>::type* =
1667  nullptr) {
1668  if (v.rank() != N) {
1669  KOKKOS_IF_ON_HOST(
1670  const std::string message =
1671  "Converting DynRankView of rank " + std::to_string(v.rank()) +
1672  " to a View of mis-matched rank " + std::to_string(N) + "!";
1673  Kokkos::abort(message.c_str());)
1674  KOKKOS_IF_ON_DEVICE(
1675  Kokkos::abort("Converting DynRankView to a View of mis-matched rank!");)
1676  }
1677 
1678  return View<typename RankDataType<T, N>::type, Args...>(
1679  v.data(), v.impl_map().layout());
1680 }
1681 
1682 template <typename Function, typename... Args>
1683 void apply_to_view_of_static_rank(Function&& f, DynRankView<Args...> a) {
1684  switch (rank(a)) {
1685  case 0: f(as_view_of_rank_n<0>(a)); break;
1686  case 1: f(as_view_of_rank_n<1>(a)); break;
1687  case 2: f(as_view_of_rank_n<2>(a)); break;
1688  case 3: f(as_view_of_rank_n<3>(a)); break;
1689  case 4: f(as_view_of_rank_n<4>(a)); break;
1690  case 5: f(as_view_of_rank_n<5>(a)); break;
1691  case 6: f(as_view_of_rank_n<6>(a)); break;
1692  case 7: f(as_view_of_rank_n<7>(a)); break;
1693  default:
1694  KOKKOS_IF_ON_HOST(
1695  Kokkos::abort(
1696  std::string(
1697  "Trying to apply a function to a view of unexpected rank " +
1698  std::to_string(rank(a)))
1699  .c_str());)
1700  KOKKOS_IF_ON_DEVICE(
1701  Kokkos::abort(
1702  "Trying to apply a function to a view of unexpected rank");)
1703  }
1704 }
1705 
1706 } // namespace Impl
1707 
1708 template <typename D, class... P>
1709 KOKKOS_INLINE_FUNCTION constexpr auto DynRankView<D, P...>::layout() const ->
1710  typename traits::array_layout {
1711  switch (rank()) {
1712  case 0: return Impl::as_view_of_rank_n<0>(*this).layout();
1713  case 1: return Impl::as_view_of_rank_n<1>(*this).layout();
1714  case 2: return Impl::as_view_of_rank_n<2>(*this).layout();
1715  case 3: return Impl::as_view_of_rank_n<3>(*this).layout();
1716  case 4: return Impl::as_view_of_rank_n<4>(*this).layout();
1717  case 5: return Impl::as_view_of_rank_n<5>(*this).layout();
1718  case 6: return Impl::as_view_of_rank_n<6>(*this).layout();
1719  case 7: return Impl::as_view_of_rank_n<7>(*this).layout();
1720  default:
1721  KOKKOS_IF_ON_HOST(
1722  Kokkos::abort(
1723  std::string(
1724  "Calling DynRankView::layout on DRV of unexpected rank " +
1725  std::to_string(rank()))
1726  .c_str());)
1727  KOKKOS_IF_ON_DEVICE(
1728  Kokkos::abort(
1729  "Calling DynRankView::layout on DRV of unexpected rank");)
1730  }
1731  // control flow should never reach here
1732  return m_map.layout();
1733 }
1734 
1736 template <class ExecSpace, class DT, class... DP>
1737 inline void deep_copy(
1738  const ExecSpace& e, const DynRankView<DT, DP...>& dst,
1739  typename ViewTraits<DT, DP...>::const_value_type& value,
1740  std::enable_if_t<std::is_same<typename ViewTraits<DT, DP...>::specialize,
1741  void>::value>* = nullptr) {
1742  static_assert(
1743  std::is_same<typename ViewTraits<DT, DP...>::non_const_value_type,
1744  typename ViewTraits<DT, DP...>::value_type>::value,
1745  "deep_copy requires non-const type");
1746 
1747  Impl::apply_to_view_of_static_rank(
1748  [=](auto view) { deep_copy(e, view, value); }, dst);
1749 }
1750 
1751 template <class DT, class... DP>
1752 inline void deep_copy(
1753  const DynRankView<DT, DP...>& dst,
1754  typename ViewTraits<DT, DP...>::const_value_type& value,
1755  std::enable_if_t<std::is_same<typename ViewTraits<DT, DP...>::specialize,
1756  void>::value>* = nullptr) {
1757  Impl::apply_to_view_of_static_rank([=](auto view) { deep_copy(view, value); },
1758  dst);
1759 }
1760 
1762 template <class ExecSpace, class ST, class... SP>
1763 inline void deep_copy(
1764  const ExecSpace& e,
1765  typename ViewTraits<ST, SP...>::non_const_value_type& dst,
1766  const DynRankView<ST, SP...>& src,
1767  std::enable_if_t<std::is_same<typename ViewTraits<ST, SP...>::specialize,
1768  void>::value>* = 0) {
1769  deep_copy(e, dst, Impl::as_view_of_rank_n<0>(src));
1770 }
1771 
1772 template <class ST, class... SP>
1773 inline void deep_copy(
1774  typename ViewTraits<ST, SP...>::non_const_value_type& dst,
1775  const DynRankView<ST, SP...>& src,
1776  std::enable_if_t<std::is_same<typename ViewTraits<ST, SP...>::specialize,
1777  void>::value>* = 0) {
1778  deep_copy(dst, Impl::as_view_of_rank_n<0>(src));
1779 }
1780 
1781 //----------------------------------------------------------------------------
1787 template <class ExecSpace, class DstType, class SrcType>
1788 inline void deep_copy(
1789  const ExecSpace& exec_space, const DstType& dst, const SrcType& src,
1790  std::enable_if_t<
1791  (std::is_void<typename DstType::traits::specialize>::value &&
1792  std::is_void<typename SrcType::traits::specialize>::value &&
1793  (Kokkos::is_dyn_rank_view<DstType>::value ||
1794  Kokkos::is_dyn_rank_view<SrcType>::value))>* = nullptr) {
1795  static_assert(
1796  std::is_same<typename DstType::traits::value_type,
1797  typename DstType::traits::non_const_value_type>::value,
1798  "deep_copy requires non-const destination type");
1799 
1800  switch (rank(dst)) {
1801  case 0:
1802  deep_copy(exec_space, Impl::as_view_of_rank_n<0>(dst),
1803  Impl::as_view_of_rank_n<0>(src));
1804  break;
1805  case 1:
1806  deep_copy(exec_space, Impl::as_view_of_rank_n<1>(dst),
1807  Impl::as_view_of_rank_n<1>(src));
1808  break;
1809  case 2:
1810  deep_copy(exec_space, Impl::as_view_of_rank_n<2>(dst),
1811  Impl::as_view_of_rank_n<2>(src));
1812  break;
1813  case 3:
1814  deep_copy(exec_space, Impl::as_view_of_rank_n<3>(dst),
1815  Impl::as_view_of_rank_n<3>(src));
1816  break;
1817  case 4:
1818  deep_copy(exec_space, Impl::as_view_of_rank_n<4>(dst),
1819  Impl::as_view_of_rank_n<4>(src));
1820  break;
1821  case 5:
1822  deep_copy(exec_space, Impl::as_view_of_rank_n<5>(dst),
1823  Impl::as_view_of_rank_n<5>(src));
1824  break;
1825  case 6:
1826  deep_copy(exec_space, Impl::as_view_of_rank_n<6>(dst),
1827  Impl::as_view_of_rank_n<6>(src));
1828  break;
1829  case 7:
1830  deep_copy(exec_space, Impl::as_view_of_rank_n<7>(dst),
1831  Impl::as_view_of_rank_n<7>(src));
1832  break;
1833  default:
1834  Kokkos::Impl::throw_runtime_exception(
1835  "Calling DynRankView deep_copy with a view of unexpected rank " +
1836  std::to_string(rank(dst)));
1837  }
1838 }
1839 
1840 template <class DstType, class SrcType>
1841 inline void deep_copy(
1842  const DstType& dst, const SrcType& src,
1843  std::enable_if_t<
1844  (std::is_void<typename DstType::traits::specialize>::value &&
1845  std::is_void<typename SrcType::traits::specialize>::value &&
1846  (Kokkos::is_dyn_rank_view<DstType>::value ||
1847  Kokkos::is_dyn_rank_view<SrcType>::value))>* = nullptr) {
1848  static_assert(
1849  std::is_same<typename DstType::traits::value_type,
1850  typename DstType::traits::non_const_value_type>::value,
1851  "deep_copy requires non-const destination type");
1852 
1853  switch (rank(dst)) {
1854  case 0:
1855  deep_copy(Impl::as_view_of_rank_n<0>(dst),
1856  Impl::as_view_of_rank_n<0>(src));
1857  break;
1858  case 1:
1859  deep_copy(Impl::as_view_of_rank_n<1>(dst),
1860  Impl::as_view_of_rank_n<1>(src));
1861  break;
1862  case 2:
1863  deep_copy(Impl::as_view_of_rank_n<2>(dst),
1864  Impl::as_view_of_rank_n<2>(src));
1865  break;
1866  case 3:
1867  deep_copy(Impl::as_view_of_rank_n<3>(dst),
1868  Impl::as_view_of_rank_n<3>(src));
1869  break;
1870  case 4:
1871  deep_copy(Impl::as_view_of_rank_n<4>(dst),
1872  Impl::as_view_of_rank_n<4>(src));
1873  break;
1874  case 5:
1875  deep_copy(Impl::as_view_of_rank_n<5>(dst),
1876  Impl::as_view_of_rank_n<5>(src));
1877  break;
1878  case 6:
1879  deep_copy(Impl::as_view_of_rank_n<6>(dst),
1880  Impl::as_view_of_rank_n<6>(src));
1881  break;
1882  case 7:
1883  deep_copy(Impl::as_view_of_rank_n<7>(dst),
1884  Impl::as_view_of_rank_n<7>(src));
1885  break;
1886  default:
1887  Kokkos::Impl::throw_runtime_exception(
1888  "Calling DynRankView deep_copy with a view of unexpected rank " +
1889  std::to_string(rank(dst)));
1890  }
1891 }
1892 
1893 } // namespace Kokkos
1894 
1895 //----------------------------------------------------------------------------
1896 //----------------------------------------------------------------------------
1897 
1898 namespace Kokkos {
1899 namespace Impl {
1900 
1901 // Deduce Mirror Types
1902 template <class Space, class T, class... P>
1903 struct MirrorDRViewType {
1904  // The incoming view_type
1905  using src_view_type = typename Kokkos::DynRankView<T, P...>;
1906  // The memory space for the mirror view
1907  using memory_space = typename Space::memory_space;
1908  // Check whether it is the same memory space
1909  enum {
1910  is_same_memspace =
1911  std::is_same<memory_space, typename src_view_type::memory_space>::value
1912  };
1913  // The array_layout
1914  using array_layout = typename src_view_type::array_layout;
1915  // The data type (we probably want it non-const since otherwise we can't even
1916  // deep_copy to it.
1917  using data_type = typename src_view_type::non_const_data_type;
1918  // The destination view type if it is not the same memory space
1919  using dest_view_type = Kokkos::DynRankView<data_type, array_layout, Space>;
1920  // If it is the same memory_space return the existsing view_type
1921  // This will also keep the unmanaged trait if necessary
1922  using view_type =
1923  std::conditional_t<is_same_memspace, src_view_type, dest_view_type>;
1924 };
1925 
1926 template <class Space, class T, class... P>
1927 struct MirrorDRVType {
1928  // The incoming view_type
1929  using src_view_type = typename Kokkos::DynRankView<T, P...>;
1930  // The memory space for the mirror view
1931  using memory_space = typename Space::memory_space;
1932  // Check whether it is the same memory space
1933  enum {
1934  is_same_memspace =
1935  std::is_same<memory_space, typename src_view_type::memory_space>::value
1936  };
1937  // The array_layout
1938  using array_layout = typename src_view_type::array_layout;
1939  // The data type (we probably want it non-const since otherwise we can't even
1940  // deep_copy to it.
1941  using data_type = typename src_view_type::non_const_data_type;
1942  // The destination view type if it is not the same memory space
1943  using view_type = Kokkos::DynRankView<data_type, array_layout, Space>;
1944 };
1945 
1946 } // namespace Impl
1947 
1948 namespace Impl {
1949 template <class T, class... P, class... ViewCtorArgs>
1950 inline typename DynRankView<T, P...>::HostMirror create_mirror(
1951  const DynRankView<T, P...>& src,
1952  const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1953  std::enable_if_t<!Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space>* =
1954  nullptr) {
1955  using src_type = DynRankView<T, P...>;
1956  using dst_type = typename src_type::HostMirror;
1957 
1958  using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
1959 
1960  static_assert(
1961  !alloc_prop_input::has_label,
1962  "The view constructor arguments passed to Kokkos::create_mirror "
1963  "must not include a label!");
1964  static_assert(
1965  !alloc_prop_input::has_pointer,
1966  "The view constructor arguments passed to Kokkos::create_mirror must "
1967  "not include a pointer!");
1968  static_assert(
1969  !alloc_prop_input::allow_padding,
1970  "The view constructor arguments passed to Kokkos::create_mirror must "
1971  "not explicitly allow padding!");
1972 
1973  auto prop_copy = Impl::with_properties_if_unset(
1974  arg_prop, std::string(src.label()).append("_mirror"));
1975 
1976  return dst_type(prop_copy, Impl::reconstructLayout(src.layout(), src.rank()));
1977 }
1978 
1979 template <class T, class... P, class... ViewCtorArgs>
1980 inline auto create_mirror(
1981  const DynRankView<T, P...>& src,
1982  const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1983  std::enable_if_t<Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space>* =
1984  nullptr) {
1985  using dst_type = typename Impl::MirrorDRVType<
1986  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
1987  P...>::view_type;
1988 
1989  using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
1990 
1991  static_assert(
1992  !alloc_prop_input::has_label,
1993  "The view constructor arguments passed to Kokkos::create_mirror "
1994  "must not include a label!");
1995  static_assert(
1996  !alloc_prop_input::has_pointer,
1997  "The view constructor arguments passed to Kokkos::create_mirror must "
1998  "not include a pointer!");
1999  static_assert(
2000  !alloc_prop_input::allow_padding,
2001  "The view constructor arguments passed to Kokkos::create_mirror must "
2002  "not explicitly allow padding!");
2003 
2004  auto prop_copy = Impl::with_properties_if_unset(
2005  arg_prop, std::string(src.label()).append("_mirror"));
2006 
2007  return dst_type(prop_copy, Impl::reconstructLayout(src.layout(), src.rank()));
2008 }
2009 
2010 } // namespace Impl
2011 
2012 // Create a mirror in host space
2013 template <class T, class... P>
2014 inline typename DynRankView<T, P...>::HostMirror create_mirror(
2015  const DynRankView<T, P...>& src,
2016  std::enable_if_t<std::is_same<typename ViewTraits<T, P...>::specialize,
2017  void>::value>* = nullptr) {
2018  return Impl::create_mirror(src, Kokkos::Impl::ViewCtorProp<>{});
2019 }
2020 
2021 template <class T, class... P>
2022 inline typename DynRankView<T, P...>::HostMirror create_mirror(
2023  Kokkos::Impl::WithoutInitializing_t wi, const DynRankView<T, P...>& src,
2024  std::enable_if_t<std::is_same<typename ViewTraits<T, P...>::specialize,
2025  void>::value>* = nullptr) {
2026  return Impl::create_mirror(src, Kokkos::view_alloc(wi));
2027 }
2028 
2029 template <class T, class... P, class... ViewCtorArgs>
2030 inline typename DynRankView<T, P...>::HostMirror create_mirror(
2031  const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
2032  const DynRankView<T, P...>& src,
2033  std::enable_if_t<
2034  std::is_void<typename ViewTraits<T, P...>::specialize>::value &&
2035  !Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space>* = nullptr) {
2036  return Impl::create_mirror(src, arg_prop);
2037 }
2038 
2039 // Create a mirror in a new space
2040 template <class Space, class T, class... P,
2041  typename Enable = std::enable_if_t<
2042  Kokkos::is_space<Space>::value &&
2043  std::is_void<typename ViewTraits<T, P...>::specialize>::value>>
2044 typename Impl::MirrorDRVType<Space, T, P...>::view_type create_mirror(
2045  const Space&, const Kokkos::DynRankView<T, P...>& src) {
2046  return Impl::create_mirror(
2047  src, Kokkos::view_alloc(typename Space::memory_space{}));
2048 }
2049 
2050 template <class Space, class T, class... P>
2051 typename Impl::MirrorDRVType<Space, T, P...>::view_type create_mirror(
2052  Kokkos::Impl::WithoutInitializing_t wi, const Space&,
2053  const Kokkos::DynRankView<T, P...>& src,
2054  std::enable_if_t<std::is_same<typename ViewTraits<T, P...>::specialize,
2055  void>::value>* = nullptr) {
2056  return Impl::create_mirror(
2057  src, Kokkos::view_alloc(wi, typename Space::memory_space{}));
2058 }
2059 
2060 template <class T, class... P, class... ViewCtorArgs>
2061 inline auto create_mirror(
2062  const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
2063  const DynRankView<T, P...>& src,
2064  std::enable_if_t<
2065  std::is_void<typename ViewTraits<T, P...>::specialize>::value &&
2066  Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space>* = nullptr) {
2067  using ReturnType = typename Impl::MirrorDRVType<
2068  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
2069  P...>::view_type;
2070  return ReturnType{Impl::create_mirror(src, arg_prop)};
2071 }
2072 
2073 namespace Impl {
2074 template <class T, class... P, class... ViewCtorArgs>
2075 inline std::enable_if_t<
2076  !Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space &&
2077  std::is_same<
2078  typename DynRankView<T, P...>::memory_space,
2079  typename DynRankView<T, P...>::HostMirror::memory_space>::value &&
2080  std::is_same<
2081  typename DynRankView<T, P...>::data_type,
2082  typename DynRankView<T, P...>::HostMirror::data_type>::value,
2083  typename DynRankView<T, P...>::HostMirror>
2084 create_mirror_view(const DynRankView<T, P...>& src,
2085  const typename Impl::ViewCtorProp<ViewCtorArgs...>&) {
2086  return src;
2087 }
2088 
2089 template <class T, class... P, class... ViewCtorArgs>
2090 inline std::enable_if_t<
2091  !Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space &&
2092  !(std::is_same<
2093  typename DynRankView<T, P...>::memory_space,
2094  typename DynRankView<T, P...>::HostMirror::memory_space>::value &&
2095  std::is_same<
2096  typename DynRankView<T, P...>::data_type,
2097  typename DynRankView<T, P...>::HostMirror::data_type>::value),
2098  typename DynRankView<T, P...>::HostMirror>
2099 create_mirror_view(
2100  const DynRankView<T, P...>& src,
2101  const typename Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop) {
2102  return Kokkos::Impl::create_mirror(src, arg_prop);
2103 }
2104 
2105 template <class T, class... P, class... ViewCtorArgs,
2106  class = std::enable_if_t<
2107  Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space>>
2108 inline std::enable_if_t<
2109  Kokkos::is_space<
2110  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space>::value &&
2111  Impl::MirrorDRViewType<
2112  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
2113  P...>::is_same_memspace,
2114  typename Impl::MirrorDRViewType<
2115  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
2116  P...>::view_type>
2117 create_mirror_view(const Kokkos::DynRankView<T, P...>& src,
2118  const typename Impl::ViewCtorProp<ViewCtorArgs...>&) {
2119  return src;
2120 }
2121 
2122 template <class T, class... P, class... ViewCtorArgs,
2123  class = std::enable_if_t<
2124  Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space>>
2125 inline std::enable_if_t<
2126  Kokkos::is_space<
2127  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space>::value &&
2128  !Impl::MirrorDRViewType<
2129  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
2130  P...>::is_same_memspace,
2131  typename Impl::MirrorDRViewType<
2132  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
2133  P...>::view_type>
2134 create_mirror_view(
2135  const Kokkos::DynRankView<T, P...>& src,
2136  const typename Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop) {
2137  return Kokkos::Impl::create_mirror(src, arg_prop);
2138 }
2139 } // namespace Impl
2140 
2141 // Create a mirror view in host space
2142 template <class T, class... P>
2143 inline std::enable_if_t<
2144  (std::is_same<
2145  typename DynRankView<T, P...>::memory_space,
2146  typename DynRankView<T, P...>::HostMirror::memory_space>::value &&
2147  std::is_same<typename DynRankView<T, P...>::data_type,
2148  typename DynRankView<T, P...>::HostMirror::data_type>::value),
2149  typename DynRankView<T, P...>::HostMirror>
2150 create_mirror_view(const Kokkos::DynRankView<T, P...>& src) {
2151  return src;
2152 }
2153 
2154 template <class T, class... P>
2155 inline std::enable_if_t<
2156  !(std::is_same<
2157  typename DynRankView<T, P...>::memory_space,
2158  typename DynRankView<T, P...>::HostMirror::memory_space>::value &&
2159  std::is_same<
2160  typename DynRankView<T, P...>::data_type,
2161  typename DynRankView<T, P...>::HostMirror::data_type>::value),
2162  typename DynRankView<T, P...>::HostMirror>
2163 create_mirror_view(const Kokkos::DynRankView<T, P...>& src) {
2164  return Kokkos::create_mirror(src);
2165 }
2166 
2167 template <class T, class... P>
2168 inline auto create_mirror_view(Kokkos::Impl::WithoutInitializing_t wi,
2169  const DynRankView<T, P...>& src) {
2170  return Impl::create_mirror_view(src, Kokkos::view_alloc(wi));
2171 }
2172 
2173 // Create a mirror view in a new space
2174 // FIXME_C++17 Improve SFINAE here.
2175 template <class Space, class T, class... P,
2176  class Enable = std::enable_if_t<Kokkos::is_space<Space>::value>>
2177 inline typename Impl::MirrorDRViewType<Space, T, P...>::view_type
2178 create_mirror_view(
2179  const Space&, const Kokkos::DynRankView<T, P...>& src,
2180  std::enable_if_t<
2181  Impl::MirrorDRViewType<Space, T, P...>::is_same_memspace>* = nullptr) {
2182  return src;
2183 }
2184 
2185 // FIXME_C++17 Improve SFINAE here.
2186 template <class Space, class T, class... P,
2187  class Enable = std::enable_if_t<Kokkos::is_space<Space>::value>>
2188 inline typename Impl::MirrorDRViewType<Space, T, P...>::view_type
2189 create_mirror_view(
2190  const Space& space, const Kokkos::DynRankView<T, P...>& src,
2191  std::enable_if_t<
2192  !Impl::MirrorDRViewType<Space, T, P...>::is_same_memspace>* = nullptr) {
2193  return Kokkos::create_mirror(space, src);
2194 }
2195 
2196 template <class Space, class T, class... P>
2197 inline auto create_mirror_view(Kokkos::Impl::WithoutInitializing_t wi,
2198  const Space&,
2199  const Kokkos::DynRankView<T, P...>& src) {
2200  return Impl::create_mirror_view(
2201  src, Kokkos::view_alloc(typename Space::memory_space{}, wi));
2202 }
2203 
2204 template <class T, class... P, class... ViewCtorArgs>
2205 inline auto create_mirror_view(
2206  const typename Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
2207  const Kokkos::DynRankView<T, P...>& src) {
2208  return Impl::create_mirror_view(src, arg_prop);
2209 }
2210 
2211 template <class... ViewCtorArgs, class T, class... P>
2212 auto create_mirror_view_and_copy(
2213  const Impl::ViewCtorProp<ViewCtorArgs...>&,
2214  const Kokkos::DynRankView<T, P...>& src,
2215  std::enable_if_t<
2216  std::is_void<typename ViewTraits<T, P...>::specialize>::value &&
2217  Impl::MirrorDRViewType<
2218  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
2219  P...>::is_same_memspace>* = nullptr) {
2220  using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
2221  static_assert(
2222  alloc_prop_input::has_memory_space,
2223  "The view constructor arguments passed to "
2224  "Kokkos::create_mirror_view_and_copy must include a memory space!");
2225  static_assert(!alloc_prop_input::has_pointer,
2226  "The view constructor arguments passed to "
2227  "Kokkos::create_mirror_view_and_copy must "
2228  "not include a pointer!");
2229  static_assert(!alloc_prop_input::allow_padding,
2230  "The view constructor arguments passed to "
2231  "Kokkos::create_mirror_view_and_copy must "
2232  "not explicitly allow padding!");
2233 
2234  // same behavior as deep_copy(src, src)
2235  if (!alloc_prop_input::has_execution_space)
2236  fence(
2237  "Kokkos::create_mirror_view_and_copy: fence before returning src view");
2238  return src;
2239 }
2240 
2241 template <class... ViewCtorArgs, class T, class... P>
2242 auto create_mirror_view_and_copy(
2243  const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
2244  const Kokkos::DynRankView<T, P...>& src,
2245  std::enable_if_t<
2246  std::is_void<typename ViewTraits<T, P...>::specialize>::value &&
2247  !Impl::MirrorDRViewType<
2248  typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
2249  P...>::is_same_memspace>* = nullptr) {
2250  using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
2251  static_assert(
2252  alloc_prop_input::has_memory_space,
2253  "The view constructor arguments passed to "
2254  "Kokkos::create_mirror_view_and_copy must include a memory space!");
2255  static_assert(!alloc_prop_input::has_pointer,
2256  "The view constructor arguments passed to "
2257  "Kokkos::create_mirror_view_and_copy must "
2258  "not include a pointer!");
2259  static_assert(!alloc_prop_input::allow_padding,
2260  "The view constructor arguments passed to "
2261  "Kokkos::create_mirror_view_and_copy must "
2262  "not explicitly allow padding!");
2263  using Space = typename alloc_prop_input::memory_space;
2264  using Mirror = typename Impl::MirrorDRViewType<Space, T, P...>::view_type;
2265 
2266  auto arg_prop_copy = Impl::with_properties_if_unset(
2267  arg_prop, std::string{}, WithoutInitializing,
2268  typename Space::execution_space{});
2269 
2270  std::string& label = Impl::get_property<Impl::LabelTag>(arg_prop_copy);
2271  if (label.empty()) label = src.label();
2272  auto mirror = typename Mirror::non_const_type{
2273  arg_prop_copy, Impl::reconstructLayout(src.layout(), src.rank())};
2274  if constexpr (alloc_prop_input::has_execution_space) {
2275  deep_copy(Impl::get_property<Impl::ExecutionSpaceTag>(arg_prop_copy),
2276  mirror, src);
2277  } else
2278  deep_copy(mirror, src);
2279  return mirror;
2280 }
2281 
2282 template <class Space, class T, class... P>
2283 auto create_mirror_view_and_copy(const Space&,
2284  const Kokkos::DynRankView<T, P...>& src,
2285  std::string const& name = "") {
2286  return create_mirror_view_and_copy(
2287  Kokkos::view_alloc(typename Space::memory_space{}, name), src);
2288 }
2289 
2290 } // namespace Kokkos
2291 
2292 //----------------------------------------------------------------------------
2293 //----------------------------------------------------------------------------
2294 
2295 namespace Kokkos {
2298 template <class... ViewCtorArgs, class T, class... P>
2299 inline void impl_resize(const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
2300  DynRankView<T, P...>& v, const size_t n0,
2301  const size_t n1, const size_t n2, const size_t n3,
2302  const size_t n4, const size_t n5, const size_t n6,
2303  const size_t n7) {
2304  using drview_type = DynRankView<T, P...>;
2305  using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
2306 
2308  "Can only resize managed views");
2309  static_assert(!alloc_prop_input::has_label,
2310  "The view constructor arguments passed to Kokkos::resize "
2311  "must not include a label!");
2312  static_assert(!alloc_prop_input::has_pointer,
2313  "The view constructor arguments passed to Kokkos::resize must "
2314  "not include a pointer!");
2315  static_assert(!alloc_prop_input::has_memory_space,
2316  "The view constructor arguments passed to Kokkos::resize must "
2317  "not include a memory space instance!");
2318 
2319  auto prop_copy = Impl::with_properties_if_unset(
2320  arg_prop, v.label(), typename drview_type::execution_space{});
2321 
2322  drview_type v_resized(prop_copy, n0, n1, n2, n3, n4, n5, n6, n7);
2323 
2324  if constexpr (alloc_prop_input::has_execution_space)
2325  Kokkos::Impl::DynRankViewRemap<drview_type, drview_type>(
2326  Impl::get_property<Impl::ExecutionSpaceTag>(prop_copy), v_resized, v);
2327  else
2328  Kokkos::Impl::DynRankViewRemap<drview_type, drview_type>(v_resized, v);
2329 
2330  v = v_resized;
2331 }
2332 
2333 template <class T, class... P>
2334 inline void resize(DynRankView<T, P...>& v,
2335  const size_t n0 = KOKKOS_INVALID_INDEX,
2336  const size_t n1 = KOKKOS_INVALID_INDEX,
2337  const size_t n2 = KOKKOS_INVALID_INDEX,
2338  const size_t n3 = KOKKOS_INVALID_INDEX,
2339  const size_t n4 = KOKKOS_INVALID_INDEX,
2340  const size_t n5 = KOKKOS_INVALID_INDEX,
2341  const size_t n6 = KOKKOS_INVALID_INDEX,
2342  const size_t n7 = KOKKOS_INVALID_INDEX) {
2343  impl_resize(Impl::ViewCtorProp<>{}, v, n0, n1, n2, n3, n4, n5, n6, n7);
2344 }
2345 
2346 template <class... ViewCtorArgs, class T, class... P>
2347 void resize(const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
2348  DynRankView<T, P...>& v,
2349  const size_t n0 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
2350  const size_t n1 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
2351  const size_t n2 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
2352  const size_t n3 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
2353  const size_t n4 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
2354  const size_t n5 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
2355  const size_t n6 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
2356  const size_t n7 = KOKKOS_IMPL_CTOR_DEFAULT_ARG) {
2357  impl_resize(arg_prop, v, n0, n1, n2, n3, n4, n5, n6, n7);
2358 }
2359 
2360 template <class I, class T, class... P>
2361 inline std::enable_if_t<Impl::is_view_ctor_property<I>::value> resize(
2362  const I& arg_prop, DynRankView<T, P...>& v,
2363  const size_t n0 = KOKKOS_INVALID_INDEX,
2364  const size_t n1 = KOKKOS_INVALID_INDEX,
2365  const size_t n2 = KOKKOS_INVALID_INDEX,
2366  const size_t n3 = KOKKOS_INVALID_INDEX,
2367  const size_t n4 = KOKKOS_INVALID_INDEX,
2368  const size_t n5 = KOKKOS_INVALID_INDEX,
2369  const size_t n6 = KOKKOS_INVALID_INDEX,
2370  const size_t n7 = KOKKOS_INVALID_INDEX) {
2371  impl_resize(Kokkos::view_alloc(arg_prop), v, n0, n1, n2, n3, n4, n5, n6, n7);
2372 }
2373 
2376 template <class... ViewCtorArgs, class T, class... P>
2377 inline void impl_realloc(DynRankView<T, P...>& v, const size_t n0,
2378  const size_t n1, const size_t n2, const size_t n3,
2379  const size_t n4, const size_t n5, const size_t n6,
2380  const size_t n7,
2381  const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop) {
2382  using drview_type = DynRankView<T, P...>;
2383  using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
2384 
2386  "Can only realloc managed views");
2387  static_assert(!alloc_prop_input::has_label,
2388  "The view constructor arguments passed to Kokkos::realloc must "
2389  "not include a label!");
2390  static_assert(!alloc_prop_input::has_pointer,
2391  "The view constructor arguments passed to Kokkos::realloc must "
2392  "not include a pointer!");
2393  static_assert(!alloc_prop_input::has_memory_space,
2394  "The view constructor arguments passed to Kokkos::realloc must "
2395  "not include a memory space instance!");
2396 
2397  auto arg_prop_copy = Impl::with_properties_if_unset(arg_prop, v.label());
2398 
2399  v = drview_type(); // Deallocate first, if the only view to allocation
2400  v = drview_type(arg_prop_copy, n0, n1, n2, n3, n4, n5, n6, n7);
2401 }
2402 
2403 template <class T, class... P, class... ViewCtorArgs>
2404 inline void realloc(const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
2405  DynRankView<T, P...>& v,
2406  const size_t n0 = KOKKOS_INVALID_INDEX,
2407  const size_t n1 = KOKKOS_INVALID_INDEX,
2408  const size_t n2 = KOKKOS_INVALID_INDEX,
2409  const size_t n3 = KOKKOS_INVALID_INDEX,
2410  const size_t n4 = KOKKOS_INVALID_INDEX,
2411  const size_t n5 = KOKKOS_INVALID_INDEX,
2412  const size_t n6 = KOKKOS_INVALID_INDEX,
2413  const size_t n7 = KOKKOS_INVALID_INDEX) {
2414  impl_realloc(v, n0, n1, n2, n3, n4, n5, n6, n7, arg_prop);
2415 }
2416 
2417 template <class T, class... P>
2418 inline void realloc(DynRankView<T, P...>& v,
2419  const size_t n0 = KOKKOS_INVALID_INDEX,
2420  const size_t n1 = KOKKOS_INVALID_INDEX,
2421  const size_t n2 = KOKKOS_INVALID_INDEX,
2422  const size_t n3 = KOKKOS_INVALID_INDEX,
2423  const size_t n4 = KOKKOS_INVALID_INDEX,
2424  const size_t n5 = KOKKOS_INVALID_INDEX,
2425  const size_t n6 = KOKKOS_INVALID_INDEX,
2426  const size_t n7 = KOKKOS_INVALID_INDEX) {
2427  impl_realloc(v, n0, n1, n2, n3, n4, n5, n6, n7, Impl::ViewCtorProp<>{});
2428 }
2429 
2430 template <class I, class T, class... P>
2431 inline std::enable_if_t<Impl::is_view_ctor_property<I>::value> realloc(
2432  const I& arg_prop, DynRankView<T, P...>& v,
2433  const size_t n0 = KOKKOS_INVALID_INDEX,
2434  const size_t n1 = KOKKOS_INVALID_INDEX,
2435  const size_t n2 = KOKKOS_INVALID_INDEX,
2436  const size_t n3 = KOKKOS_INVALID_INDEX,
2437  const size_t n4 = KOKKOS_INVALID_INDEX,
2438  const size_t n5 = KOKKOS_INVALID_INDEX,
2439  const size_t n6 = KOKKOS_INVALID_INDEX,
2440  const size_t n7 = KOKKOS_INVALID_INDEX) {
2441  impl_realloc(v, n0, n1, n2, n3, n4, n5, n6, n7, Kokkos::view_alloc(arg_prop));
2442 }
2443 
2444 } // namespace Kokkos
2445 
2446 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_DYNRANKVIEW
2447 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
2448 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_DYNRANKVIEW
2449 #endif
2450 #endif
View
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...
View to an array of data.
Memory management for host memory.
Memory layout tag indicating right-to-left (C or lexigraphical scheme) striding of multi-indices...
Assign compatible default mappings.
ReturnType
Execution policy for work over a range of an integral type.
Traits class for accessing attributes of a View.
Definition: dummy.cpp:17