Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_Details_StaticView.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_STATICVIEW_HPP
11#define TPETRA_DETAILS_STATICVIEW_HPP
12
13#include "TpetraCore_config.h"
14//#include "Tpetra_Details_Behavior.hpp"
15#include "Kokkos_DualView.hpp"
16
17namespace Tpetra {
18namespace Details {
19namespace Impl {
20
21template<class MemorySpace>
22class StaticKokkosAllocation {
23public:
24 StaticKokkosAllocation () = delete;
25 ~StaticKokkosAllocation () = delete;
26 StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
27 StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
28 StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
29 StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
30
31 // Allocation automatically registers deallocation to happen at
32 // Kokkos::finalize. Reallocation only happens if needed.
33 static void* resize (MemorySpace space, const size_t size);
34};
35
36#ifdef KOKKOS_ENABLE_CUDA
37template<>
38class StaticKokkosAllocation<Kokkos::CudaSpace> {
39public:
40 StaticKokkosAllocation () = delete;
41 ~StaticKokkosAllocation () = delete;
42 StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
43 StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
44 StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
45 StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
46
47 static void* resize (Kokkos::CudaSpace space, const size_t size);
48};
49
50template<>
51class StaticKokkosAllocation<Kokkos::CudaUVMSpace> {
52public:
53 StaticKokkosAllocation () = delete;
54 ~StaticKokkosAllocation () = delete;
55 StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
56 StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
57 StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
58 StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
59
60 static void* resize (Kokkos::CudaUVMSpace space, const size_t size);
61};
62
63template<>
64class StaticKokkosAllocation<Kokkos::CudaHostPinnedSpace> {
65public:
66 StaticKokkosAllocation () = delete;
67 ~StaticKokkosAllocation () = delete;
68 StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
69 StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
70 StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
71 StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
72
73 static void* resize (Kokkos::CudaHostPinnedSpace space, const size_t size);
74};
75#endif // KOKKOS_ENABLE_CUDA
76
77#ifdef KOKKOS_ENABLE_HIP
78template<>
79class StaticKokkosAllocation<Kokkos::HIPSpace> {
80public:
81 StaticKokkosAllocation () = delete;
82 ~StaticKokkosAllocation () = delete;
83 StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
84 StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
85 StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
86 StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
87
88 static void* resize (Kokkos::HIPSpace space, const size_t size);
89};
90
91template<>
92class StaticKokkosAllocation<Kokkos::HIPHostPinnedSpace> {
93public:
94 StaticKokkosAllocation () = delete;
95 ~StaticKokkosAllocation () = delete;
96 StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
97 StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
98 StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
99 StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
100
101 static void* resize (Kokkos::HIPHostPinnedSpace space, const size_t size);
102};
103#endif // KOKKOS_ENABLE_HIP
104
105template<>
106class StaticKokkosAllocation<Kokkos::HostSpace> {
107public:
108 StaticKokkosAllocation () = delete;
109 ~StaticKokkosAllocation () = delete;
110 StaticKokkosAllocation (const StaticKokkosAllocation&) = delete;
111 StaticKokkosAllocation& operator= (const StaticKokkosAllocation&) = delete;
112 StaticKokkosAllocation (StaticKokkosAllocation&&) = delete;
113 StaticKokkosAllocation& operator= (StaticKokkosAllocation&&) = delete;
114
115 static void* resize (Kokkos::HostSpace space, const size_t size);
116};
117
118template<class ValueType, class MemorySpace>
119ValueType*
120getStaticKokkosMemory (MemorySpace space,
121 const size_t num_entries,
122 const size_t value_size = sizeof (ValueType))
123{
124 void* ptr = StaticKokkosAllocation<MemorySpace>::resize
125 (space, num_entries * value_size);
126 return reinterpret_cast<ValueType*> (ptr);
127}
128
129} // namespace Impl
130
131template<class ValueType, class DeviceType>
132Kokkos::View<ValueType*, DeviceType>
133getStatic1dView (const size_t size)
134{
135 using Impl::getStaticKokkosMemory;
136 using mem_space = typename DeviceType::memory_space;
137 using view_type = Kokkos::View<ValueType*, DeviceType>;
138
139 ValueType* ptr = getStaticKokkosMemory<ValueType> (mem_space (), size);
140 return view_type (ptr, size);
141}
142
143template<class ValueType, class DeviceType>
144Kokkos::View<ValueType**, Kokkos::LayoutLeft, DeviceType>
145getStatic2dView (const size_t num_rows, const size_t num_cols)
146{
147 using Impl::getStaticKokkosMemory;
148 using mem_space = typename DeviceType::memory_space;
149 using view_type = Kokkos::View<ValueType**, Kokkos::LayoutLeft, DeviceType>;
150
151 const size_t size = num_rows * num_cols;
152 ValueType* ptr = getStaticKokkosMemory<ValueType> (mem_space (), size);
153 return view_type (ptr, num_rows, num_cols);
154}
155
156template<class ValueType, class DeviceType>
157Kokkos::DualView<ValueType**, Kokkos::LayoutLeft, DeviceType>
158getStatic2dDualView (const size_t num_rows, const size_t num_cols)
159{
160 using dual_view_type =
161 Kokkos::DualView<ValueType**, Kokkos::LayoutLeft, DeviceType>;
162 using d_view_type = typename dual_view_type::t_dev;
163 using h_view_type = typename dual_view_type::t_host;
164
165 auto d_view = getStatic2dView<ValueType, DeviceType> (num_rows, num_cols);
166 // Preserve the invariant that Kokkos::create_mirror_view returns
167 // the input View here, if and only if it would have returned it if
168 // the allocating View constructor were called.
169 h_view_type h_view;
170 if (std::is_same<typename d_view_type::memory_space,
171 typename h_view_type::memory_space>::value) {
172 h_view = Kokkos::create_mirror_view (d_view);
173 }
174 else {
175 h_view = getStatic2dView<ValueType,
176 typename h_view_type::device_type> (num_rows, num_cols);
177 }
178
179 return dual_view_type (d_view, h_view);
180}
181
182
183} // namespace Details
184} // namespace Tpetra
185
186#endif // TPETRA_DETAILS_STATICVIEW_HPP
Nonmember function that computes a residual Computes R = B - A * X.
Namespace Tpetra contains the class and methods constituting the Tpetra library.