Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_Details_DualViewUtil.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TPETRA_DETAILS_DUALVIEWUTIL_HPP
11 #define TPETRA_DETAILS_DUALVIEWUTIL_HPP
12 
13 #include "TpetraCore_config.h"
14 #include "Kokkos_DualView.hpp"
15 #include "Teuchos_ArrayView.hpp"
16 #include <ostream>
17 #include <string>
18 
20 namespace Tpetra {
21 
24 namespace Details {
25 
29 auto view_alloc_no_init(const std::string& label) -> decltype(Kokkos::view_alloc(label, Kokkos::WithoutInitializing));
30 
35 template <class ElementType, class DeviceType>
36 void makeDualViewFromOwningHostView(Kokkos::DualView<ElementType*, DeviceType>& dv,
37  const typename Kokkos::DualView<ElementType*, DeviceType>::t_host& hostView) {
38  using execution_space = typename DeviceType::execution_space;
39  using dual_view_type = Kokkos::DualView<ElementType*, DeviceType>;
40 
41  if constexpr (Kokkos::SpaceAccessibility<Kokkos::HostSpace, typename DeviceType::memory_space>::accessible) {
42  // DualView only references one View, so we pass in the same View twice
43  dv = dual_view_type(hostView, hostView);
44  } else {
45  typename Kokkos::DualView<ElementType*, DeviceType>::t_dev devView;
46  if (dv.extent(0) == hostView.extent(0))
47  devView = dv.view_device();
48  else
49  devView = Kokkos::create_mirror_view(DeviceType(), hostView);
50  // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
51  Kokkos::deep_copy(execution_space(), devView, hostView);
52  dv = dual_view_type(devView, hostView);
53  execution_space().fence();
54  }
55 }
56 
57 template <class ElementType, class DeviceType>
58 void makeDualViewFromArrayView(Kokkos::DualView<ElementType*, DeviceType>& dv,
59  const Teuchos::ArrayView<const ElementType>& av,
60  const std::string& label) {
61  using execution_space = typename DeviceType::execution_space;
62  using dual_view_type = Kokkos::DualView<ElementType*, DeviceType>;
63  using host_view_type = typename dual_view_type::t_host;
64  using const_host_view_type = typename host_view_type::const_type;
65 
66  const auto size = av.size();
67  const ElementType* ptr = (size == 0) ? nullptr : av.getRawPtr();
68  const_host_view_type inView(ptr, size);
69  host_view_type hostView(view_alloc_no_init(label), size);
70  // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
71  Kokkos::deep_copy(execution_space(), hostView, inView);
72 
73  makeDualViewFromOwningHostView(dv, hostView);
74 }
75 
76 template <class ElementType, class DeviceType>
77 void makeDualViewFromVector(Kokkos::DualView<ElementType*, DeviceType>& dv,
78  const std::vector<ElementType>& vec,
79  const std::string& label) {
80  using dual_view_type = Kokkos::DualView<ElementType*, DeviceType>;
81  using execution_space = typename DeviceType::execution_space;
82  using host_view_type = typename dual_view_type::t_host;
83  using const_host_view_type = typename host_view_type::const_type;
84 
85  const auto size = vec.size();
86  const ElementType* ptr = (size == 0) ? nullptr : vec.data();
87  const_host_view_type inView(ptr, size);
88  host_view_type hostView(view_alloc_no_init(label), size);
89  // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
90  Kokkos::deep_copy(execution_space(), hostView, inView);
91 
92  makeDualViewFromOwningHostView(dv, hostView);
93 }
94 
95 template <class ElementType, class DeviceType>
96 void printDualView(std::ostream& out,
97  const Kokkos::DualView<ElementType*, DeviceType>& dv,
98  const std::string& name) {
99  out << name << ": ";
100  const size_t size = size_t(dv.extent(0));
101  const auto hostView = dv.view_host();
102 
103  out << "[";
104  for (size_t k = 0; k < size; ++k) {
105  out << hostView[k];
106  if (k + size_t(1) < size) {
107  out << ",";
108  }
109  }
110  out << "]";
111 }
112 
113 } // namespace Details
114 
115 } // namespace Tpetra
116 
117 #endif // TPETRA_DETAILS_DUALVIEWUTIL_HPP
void deep_copy(MultiVector< DS, DL, DG, DN > &dst, const MultiVector< SS, SL, SG, SN > &src)
Copy the contents of the MultiVector src into dst.
auto view_alloc_no_init(const std::string &label) -> decltype(Kokkos::view_alloc(label, Kokkos::WithoutInitializing))
Use in place of the string label as the first argument of Kokkos::View&#39;s constructor, in case you want to allocate without initializing.
void makeDualViewFromOwningHostView(Kokkos::DualView< ElementType *, DeviceType > &dv, const typename Kokkos::DualView< ElementType *, DeviceType >::t_host &hostView)
Initialize dv such that its host View is hostView.