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) ->
30  decltype (Kokkos::view_alloc (label, Kokkos::WithoutInitializing));
31 
36 template<class ElementType, class DeviceType>
37 void
39  (Kokkos::DualView<ElementType*, DeviceType>& dv,
40  const typename Kokkos::DualView<ElementType*, DeviceType>::t_host& hostView)
41 {
42  using execution_space = typename DeviceType::execution_space;
43  using dual_view_type = Kokkos::DualView<ElementType*, DeviceType>;
44 
45  if (dv.extent (0) == hostView.extent (0)) {
46  // We don't need to reallocate the device View.
47  dv.clear_sync_state ();
48  dv.modify_host ();
49  dv.h_view = hostView;
50  dv.sync_device ();
51  }
52  else {
53  auto devView = Kokkos::create_mirror_view (DeviceType (), hostView);
54  // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
55  Kokkos::deep_copy (execution_space(), devView, hostView);
56  dv = dual_view_type (devView, hostView);
57  }
58 }
59 
60 template<class ElementType, class DeviceType>
61 void
62 makeDualViewFromArrayView (Kokkos::DualView<ElementType*, DeviceType>& dv,
63  const Teuchos::ArrayView<const ElementType>& av,
64  const std::string& label)
65 {
66  using execution_space = typename DeviceType::execution_space;
67  using dual_view_type = Kokkos::DualView<ElementType*, DeviceType>;
68  using host_view_type = typename dual_view_type::t_host;
69  using const_host_view_type = typename host_view_type::const_type;
70 
71  const auto size = av.size ();
72  const ElementType* ptr = (size == 0) ? nullptr : av.getRawPtr ();
73  const_host_view_type inView (ptr, size);
74  host_view_type hostView (view_alloc_no_init (label), size);
75  // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
76  Kokkos::deep_copy (execution_space(), hostView, inView);
77 
78  makeDualViewFromOwningHostView (dv, hostView);
79 }
80 
81 template<class ElementType, class DeviceType>
82 void
83 makeDualViewFromVector (Kokkos::DualView<ElementType*, DeviceType>& dv,
84  const std::vector<ElementType>& vec,
85  const std::string& label)
86 {
87  using dual_view_type = Kokkos::DualView<ElementType*, DeviceType>;
88  using execution_space = typename DeviceType::execution_space;
89  using host_view_type = typename dual_view_type::t_host;
90  using const_host_view_type = typename host_view_type::const_type;
91 
92  const auto size = vec.size ();
93  const ElementType* ptr = (size == 0) ? nullptr : vec.data ();
94  const_host_view_type inView (ptr, size);
95  host_view_type hostView (view_alloc_no_init (label), size);
96  // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
97  Kokkos::deep_copy (execution_space(), hostView, inView);
98 
99  makeDualViewFromOwningHostView (dv, hostView);
100 }
101 
102 template<class ElementType, class DeviceType>
103 void
104 printDualView (std::ostream& out,
105  const Kokkos::DualView<ElementType*, DeviceType>& dv,
106  const std::string& name)
107 {
108  out << name << ": ";
109  const size_t size = size_t (dv.extent (0));
110  const auto hostView = dv.view_host ();
111 
112  out << "[";
113  for (size_t k = 0; k < size; ++k) {
114  out << hostView[k];
115  if (k + size_t (1) < size) {
116  out << ",";
117  }
118  }
119  out << "]";
120 }
121 
122 } // namespace Details
123 
124 } // namespace Tpetra
125 
126 #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) ->
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.