Debian Codegraph @

packages/teuchos/kokkoscompat/src/KokkosCompat_View.hpp

raw
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
17 #ifndef KOKKOSCOMPAT_VIEW_HPP
18 #define KOKKOSCOMPAT_VIEW_HPP
19
20 /// \file KokkosCompat_View.hpp
21 /// \brief Compatibility between Kokkos Array View and Teuchos memory management classes.
22 ///
23 /// \warning This file and everything in it should be considered an
24 /// implementation detail of Tpetra. We make no promises of
25 /// backwards compatibility for any interface in this file, nor do
26 /// we even promise that this header file will continue to exist.
27
28 #include <TeuchosKokkosCompat_config.h>
29 #include <Kokkos_Core.hpp>
30 #include <Teuchos_ArrayView.hpp>
31
32 namespace Kokkos {
33 namespace Compat {
34 // Convert Kokkos::View to Teuchos::ArrayView
35 template <typename ViewType>
36 Teuchos::ArrayView<typename ViewType::value_type>
37 getArrayView (const ViewType& a) {
38 const auto size = a.size ();
39 return Teuchos::ArrayView<typename ViewType::value_type>
40 (size == 0 ? nullptr : a.data (), size);
41 }
42 template <typename ViewType>
43 Teuchos::ArrayView<const typename ViewType::value_type>
44 getConstArrayView (const ViewType& a) {
45 const auto size = a.size ();
46 return Teuchos::ArrayView<const typename ViewType::value_type>
47 (size == 0 ? nullptr : a.data (), size);
48 }
49
50 // Convert Teuchos::ArrayView to Kokkos::View through deep_copy
51 template <typename D, typename T>
52 Kokkos::View<T*,D>
53 getKokkosViewDeepCopy (const Teuchos::ArrayView<T>& a)
54 {
55 typedef typename std::conditional<
56 SpaceAccessibility< D, Kokkos::HostSpace>::accessible,
57 typename D::execution_space, Kokkos::HostSpace>::type
58 HostDevice;
59 typedef Kokkos::View<T*, D> view_type;
60 typedef Kokkos::View<T*, typename view_type::array_layout, HostDevice,
61 Kokkos::MemoryUnmanaged> unmanaged_host_view_type;
62 if (a.size () == 0) {
63 return view_type ();
64 }
65 view_type v ("", a.size ());
66 unmanaged_host_view_type hv (a.getRawPtr (), a.size ());
67 Kokkos::deep_copy (v, hv);
68 return v;
69 }
70
71 template <typename D, typename T>
72 Kokkos::View<const T*,D>
73 getKokkosViewDeepCopy(const Teuchos::ArrayView<const T>& a)
74 {
75 typedef typename std::conditional<
76 SpaceAccessibility< D, Kokkos::HostSpace>::accessible,
77 typename D::execution_space, Kokkos::HostSpace>::type
78 HostDevice;
79 typedef Kokkos::View<T*, D> view_type;
80 typedef Kokkos::View<const T*, typename view_type::array_layout, HostDevice,
81 Kokkos::MemoryUnmanaged> unmanaged_host_view_type;
82 if (a.size () == 0) {
83 return view_type ();
84 }
85 view_type v ("", a.size ());
86 unmanaged_host_view_type hv (a.getRawPtr (), a.size ());
87 Kokkos::deep_copy (v, hv);
88 return v;
89 }
90
91 // Reallocate Kokkos::View to a new size
92 // Currently this is only here to hide the Cuda device
93 template <typename T, typename D>
94 void
95 realloc(Kokkos::View<T*,D>& v, const typename D::size_type size) {
96 Kokkos::realloc(v,size);
97 }
98
99 template <class ViewType>
100 ViewType
101 create_view (const std::string& label, size_t size) {
102 static_assert(Kokkos::is_view<ViewType>::value==true,"Kokkos::Compat::create_view() called with non-view argument.");
103 static_assert(ViewType::rank==1,"Kokkos::Compat::create_view() called with non-rank-1 view argument.");
104 return ViewType (label, size);
105 }
106
107 // Custom deallocator for Teuchos::ArrayRCP. It doesn't actually
108 // deallocate the ArrayRCP's data (which belongs to the View passed
109 // into Deallocator's constructor, not to the ArrayRCP). Instead,
110 // it just keeps a reference to the View. That way, the data won't
111 // go away until the View's reference count goes to zero.
112 template<class ViewType>
113 class Deallocator {
114 public:
115 typedef ViewType view_type;
116 typedef typename ViewType::value_type ptr_t;
117 typedef typename view_type::value_type value_type;
118
119 // Constructor takes the View that owns the memory.
120 Deallocator (const ViewType& view__) : view_ (view__) {}
121
122 // "Deallocation function" doesn't actually deallocate its input
123 // pointer; the View is responsible for deallocation of its
124 // memory.
125 void free (value_type*) {}
126 view_type view() { return view_;}
127 private:
128 view_type view_; // View that owns the memory
129
130 };
131
132 // Create deallocator for a given view
133 template <class ViewType>
134 Deallocator<ViewType> deallocator(const ViewType& view) {
135 return Deallocator<ViewType>(view);
136 }
137
138 // Create a "persisting view" from a Kokkos::View
139 template <typename ViewType>
140 Teuchos::ArrayRCP<typename ViewType::value_type>
141 persistingView(const ViewType& view) {
142 // mfh (05 Sep 2013) We use a custom deallocator that keeps the
143 // view, but otherwise does nothing. It doesn't deallocate the
144 // view; it just maintains the reference count for the lifetime
145 // of the deallocator object (and therefore, for the lifetime of
146 // the ArrayRCP). It seems weird that this is a nonowning
147 // ArrayRCP (the last argument is false). However, that's OK,
148 // because (a) the ArrayRCP (actually, the underlying
149 // RCPNodeTmpl; see Teuchos_RCPNode.hpp) keeps the deallocator
150 // object regardless of whether the ArrayRCP is owning, and (b)
151 // if we make this an owning ArrayRCP, the RCPNode tracking
152 // system (in a debug build: Teuchos_ENABLE_DEBUG:BOOL=ON) will
153 // report problems, because multiple owning ArrayRCPs have the
154 // same raw pointer but not the same underlying RCPNode. That
155 // would be a problem if those ArrayRCPs shared memory that was
156 // allocated (e.g.,) using new or malloc, but it's not a problem
157 // here, because the custom deallocator does not free anything.
158 // Nevertheless, it's better not to trouble the tracking system.
159 return Teuchos::arcp (view.span () == 0 ? nullptr : view.data (),
160 0, view.span (),
161 deallocator(view), false);
162 }
163
164 // Create a "persisting view" from a Kokkos::View
165 template <typename ViewType>
166 Teuchos::ArrayRCP<typename ViewType::value_type>
167 persistingView(
168 const ViewType& view,
169 typename Teuchos::ArrayRCP<typename ViewType::value_type>::size_type offset,
170 typename Teuchos::ArrayRCP<typename ViewType::value_type>::size_type size)
171 {
172 // This check is related to #4234.
173 typename ViewType::value_type* const ptr =
174 (size == 0 && offset == 0) ? nullptr : (view.data () + offset);
175 return Teuchos::arcp (ptr, 0, size, deallocator (view), false);
176 }
177
178 template <class ViewType, typename Ordinal>
179 ViewType
180 subview_range (const ViewType& view,
181 const Ordinal begin,
182 const Ordinal end)
183 {
184 static_assert(Kokkos::is_view<ViewType>::value==true,"Kokkos::Compat::subview_range() called with non-view argument.");
185 static_assert(ViewType::rank==1,"Kokkos::Compat::subview_range() called with non-rank-1 view argument.");
186 return Kokkos::subview (view, std::make_pair (begin, end));
187 }
188
189 template <class ViewType, typename Ordinal>
190 ViewType
191 subview_offset (const ViewType& view,
192 const Ordinal offset,
193 const Ordinal size)
194 {
195 static_assert(Kokkos::is_view<ViewType>::value==true,"Kokkos::Compat::subview_offset() called with non-view argument.");
196 static_assert(ViewType::rank==1,"Kokkos::Compat::subview_offset() called with non-rank-1 view argument.");
197 return Kokkos::subview (view, std::make_pair (offset, offset+size));
198 }
199
200 template <class DstViewType, class SrcViewType,
201 typename Ordinal>
202 void
203 deep_copy_range (const DstViewType& dst,
204 const SrcViewType& src,
205 const Ordinal dst_begin,
206 const Ordinal src_begin,
207 const Ordinal src_end)
208 {
209 static_assert(Kokkos::is_view<DstViewType>::value==true,"Kokkos::Compat::deep_copy_range() called with non-view argument.");
210 static_assert(Kokkos::is_view<SrcViewType>::value==true,"Kokkos::Compat::deep_copy_range() called with non-view argument.");
211 static_assert(DstViewType::rank==1 && SrcViewType::rank==1,"Kokkos::Compat::deep_copy_range() called with non-rank-1 view argument.");
212 const Ordinal size = src_end - src_begin;
213 const Ordinal dst_end = dst_begin + size;
214 DstViewType dst_sub = Kokkos::subview(
215 dst, std::make_pair (dst_begin, dst_end));
216 SrcViewType src_sub = Kokkos::subview(
217 src, std::make_pair (src_begin, src_end));
218 Kokkos::deep_copy(dst_sub, src_sub);
219 }
220
221 template <class DstViewType, class SrcViewType,
222 typename Ordinal>
223 void
224 deep_copy_offset (const DstViewType& dst,
225 const SrcViewType& src,
226 const Ordinal dst_offset,
227 const Ordinal src_offset,
228 const Ordinal size)
229 {
230 static_assert(Kokkos::is_view<DstViewType>::value==true,"Kokkos::Compat::deep_copy_offset() called with non-view argument.");
231 static_assert(Kokkos::is_view<SrcViewType>::value==true,"Kokkos::Compat::deep_copy_offset() called with non-view argument.");
232 static_assert(DstViewType::rank==1 && SrcViewType::rank==1,"Kokkos::Compat::deep_copy_offset() called with non-rank-1 view argument.");
233 const Ordinal dst_end = dst_offset + size;
234 const Ordinal src_end = src_offset + size;
235 DstViewType dst_sub = Kokkos::subview(
236 dst, std::make_pair (dst_offset, dst_end));
237 SrcViewType src_sub = Kokkos::subview(
238 src, std::make_pair (src_offset, src_end));
239 Kokkos::deep_copy(typename SrcViewType::execution_space(), dst_sub, src_sub);
240 }
241
242 template <class ViewType>
243 typename ViewType::const_type
244 create_const_view(const ViewType& view) {
245 static_assert(Kokkos::is_view<ViewType>::value==true,"Kokkos::Compat::create_const_view() called with non-view argument.");
246 return view;
247 }
248
249 } // namespace Compat
250 } // namespace Kokkos
251
252 #endif // KOKKOSCOMPAT_VIEW_HPP