Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
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
20namespace Tpetra {
21
24namespace Details {
25
29auto view_alloc_no_init (const std::string& label) ->
30 decltype (Kokkos::view_alloc (label, Kokkos::WithoutInitializing));
31
36template<class ElementType, class DeviceType>
37void
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 constexpr(Kokkos::SpaceAccessibility<Kokkos::HostSpace, typename DeviceType::memory_space>::accessible) {
46 // DualView only references one View, so we pass in the same View twice
47 dv = dual_view_type (hostView, hostView);
48 } else {
49 typename Kokkos::DualView<ElementType*, DeviceType>::t_dev devView;
50 if (dv.extent (0) == hostView.extent (0))
51 devView = dv.view_device();
52 else
53 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 execution_space().fence();
58 }
59}
60
61template<class ElementType, class DeviceType>
62void
63makeDualViewFromArrayView (Kokkos::DualView<ElementType*, DeviceType>& dv,
64 const Teuchos::ArrayView<const ElementType>& av,
65 const std::string& label)
66{
67 using execution_space = typename DeviceType::execution_space;
68 using dual_view_type = Kokkos::DualView<ElementType*, DeviceType>;
69 using host_view_type = typename dual_view_type::t_host;
70 using const_host_view_type = typename host_view_type::const_type;
71
72 const auto size = av.size ();
73 const ElementType* ptr = (size == 0) ? nullptr : av.getRawPtr ();
74 const_host_view_type inView (ptr, size);
75 host_view_type hostView (view_alloc_no_init (label), size);
76 // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
77 Kokkos::deep_copy (execution_space(), hostView, inView);
78
79 makeDualViewFromOwningHostView (dv, hostView);
80}
81
82template<class ElementType, class DeviceType>
83void
84makeDualViewFromVector (Kokkos::DualView<ElementType*, DeviceType>& dv,
85 const std::vector<ElementType>& vec,
86 const std::string& label)
87{
88 using dual_view_type = Kokkos::DualView<ElementType*, DeviceType>;
89 using execution_space = typename DeviceType::execution_space;
90 using host_view_type = typename dual_view_type::t_host;
91 using const_host_view_type = typename host_view_type::const_type;
92
93 const auto size = vec.size ();
94 const ElementType* ptr = (size == 0) ? nullptr : vec.data ();
95 const_host_view_type inView (ptr, size);
96 host_view_type hostView (view_alloc_no_init (label), size);
97 // DEEP_COPY REVIEW - DEVICE-TO-HOSTMIRROR
98 Kokkos::deep_copy (execution_space(), hostView, inView);
99
100 makeDualViewFromOwningHostView (dv, hostView);
101}
102
103template<class ElementType, class DeviceType>
104void
105printDualView (std::ostream& out,
106 const Kokkos::DualView<ElementType*, DeviceType>& dv,
107 const std::string& name)
108{
109 out << name << ": ";
110 const size_t size = size_t (dv.extent (0));
111 const auto hostView = dv.view_host ();
112
113 out << "[";
114 for (size_t k = 0; k < size; ++k) {
115 out << hostView[k];
116 if (k + size_t (1) < size) {
117 out << ",";
118 }
119 }
120 out << "]";
121}
122
123} // namespace Details
124
125} // namespace Tpetra
126
127#endif // TPETRA_DETAILS_DUALVIEWUTIL_HPP
Nonmember function that computes a residual Computes R = B - A * X.
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's constructor,...
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.
Namespace Tpetra contains the class and methods constituting the Tpetra library.