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  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 template<class ElementType, class DeviceType>
57 void
58 makeDualViewFromArrayView (Kokkos::DualView<ElementType*, DeviceType>& dv,
59  const Teuchos::ArrayView<const ElementType>& av,
60  const std::string& label)
61 {
62  using execution_space = typename DeviceType::execution_space;
63  using dual_view_type = Kokkos::DualView<ElementType*, DeviceType>;
64  using host_view_type = typename dual_view_type::t_host;
65  using const_host_view_type = typename host_view_type::const_type;
66 
67  const auto size = av.size ();
68  const ElementType* ptr = (size == 0) ? nullptr : av.getRawPtr ();
69  const_host_view_type inView (ptr, size);
70  host_view_type hostView (view_alloc_no_init (label), size);
71  // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
72  Kokkos::deep_copy (execution_space(), hostView, inView);
73 
74  makeDualViewFromOwningHostView (dv, hostView);
75 }
76 
77 template<class ElementType, class DeviceType>
78 void
79 makeDualViewFromVector (Kokkos::DualView<ElementType*, DeviceType>& dv,
80  const std::vector<ElementType>& vec,
81  const std::string& label)
82 {
83  using dual_view_type = Kokkos::DualView<ElementType*, DeviceType>;
84  using execution_space = typename DeviceType::execution_space;
85  using host_view_type = typename dual_view_type::t_host;
86  using const_host_view_type = typename host_view_type::const_type;
87 
88  const auto size = vec.size ();
89  const ElementType* ptr = (size == 0) ? nullptr : vec.data ();
90  const_host_view_type inView (ptr, size);
91  host_view_type hostView (view_alloc_no_init (label), size);
92  // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
93  Kokkos::deep_copy (execution_space(), hostView, inView);
94 
95  makeDualViewFromOwningHostView (dv, hostView);
96 }
97 
98 template<class ElementType, class DeviceType>
99 void
100 printDualView (std::ostream& out,
101  const Kokkos::DualView<ElementType*, DeviceType>& dv,
102  const std::string& name)
103 {
104  out << name << ": ";
105  const size_t size = size_t (dv.extent (0));
106  const auto hostView = dv.view_host ();
107 
108  out << "[";
109  for (size_t k = 0; k < size; ++k) {
110  out << hostView[k];
111  if (k + size_t (1) < size) {
112  out << ",";
113  }
114  }
115  out << "]";
116 }
117 
118 } // namespace Details
119 
120 } // namespace Tpetra
121 
122 #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.