Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
17 namespace Tpetra {
18 namespace Details {
19 namespace Impl {
20 
21 template<class MemorySpace>
22 class StaticKokkosAllocation {
23 public:
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
37 template<>
38 class StaticKokkosAllocation<Kokkos::CudaSpace> {
39 public:
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 
50 template<>
51 class StaticKokkosAllocation<Kokkos::CudaUVMSpace> {
52 public:
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 
63 template<>
64 class StaticKokkosAllocation<Kokkos::CudaHostPinnedSpace> {
65 public:
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
78 template<>
79 class StaticKokkosAllocation<Kokkos::HIPSpace> {
80 public:
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 
91 template<>
92 class StaticKokkosAllocation<Kokkos::HIPHostPinnedSpace> {
93 public:
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 
105 template<>
106 class StaticKokkosAllocation<Kokkos::HostSpace> {
107 public:
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 
118 template<class ValueType, class MemorySpace>
119 ValueType*
120 getStaticKokkosMemory (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 
131 template<class ValueType, class DeviceType>
132 Kokkos::View<ValueType*, DeviceType>
133 getStatic1dView (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 
143 template<class ValueType, class DeviceType>
144 Kokkos::View<ValueType**, Kokkos::LayoutLeft, DeviceType>
145 getStatic2dView (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 
156 template<class ValueType, class DeviceType>
157 Kokkos::DualView<ValueType**, Kokkos::LayoutLeft, DeviceType>
158 getStatic2dDualView (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