10 #include "Tpetra_Details_StaticView.hpp"
22 constexpr
size_t minimum_initial_size =
sizeof (double) * 30 * 2;
28 #ifdef KOKKOS_ENABLE_CUDA
30 void* cuda_memory_ =
nullptr;
31 size_t cuda_memory_size_ = 0;
33 void finalize_cuda_memory ()
35 if (cuda_memory_ !=
nullptr) {
36 Kokkos::kokkos_free<Kokkos::CudaSpace> (cuda_memory_);
37 cuda_memory_ =
nullptr;
38 cuda_memory_size_ = 0;
42 void* cuda_uvm_memory_ =
nullptr;
43 size_t cuda_uvm_memory_size_ = 0;
45 void finalize_cuda_uvm_memory ()
47 if (cuda_uvm_memory_ !=
nullptr) {
48 Kokkos::kokkos_free<Kokkos::CudaUVMSpace> (cuda_uvm_memory_);
49 cuda_uvm_memory_ =
nullptr;
50 cuda_uvm_memory_size_ = 0;
54 void* cuda_host_pinned_memory_ =
nullptr;
55 size_t cuda_host_pinned_memory_size_ = 0;
57 void finalize_cuda_host_pinned_memory ()
59 if (cuda_host_pinned_memory_ !=
nullptr) {
60 Kokkos::kokkos_free<Kokkos::CudaHostPinnedSpace> (cuda_host_pinned_memory_);
61 cuda_host_pinned_memory_ =
nullptr;
62 cuda_host_pinned_memory_size_ = 0;
65 #endif // KOKKOS_ENABLE_CUDA
67 #ifdef KOKKOS_ENABLE_HIP
69 void* hip_memory_ =
nullptr;
70 size_t hip_memory_size_ = 0;
72 void finalize_hip_memory ()
74 if (hip_memory_ !=
nullptr) {
75 Kokkos::kokkos_free<Kokkos::HIPSpace> (hip_memory_);
76 hip_memory_ =
nullptr;
81 void* hip_host_pinned_memory_ =
nullptr;
82 size_t hip_host_pinned_memory_size_ = 0;
84 void finalize_hip_host_pinned_memory ()
86 if (hip_host_pinned_memory_ !=
nullptr) {
87 Kokkos::kokkos_free<Kokkos::HIPHostPinnedSpace> (hip_host_pinned_memory_);
88 hip_host_pinned_memory_ =
nullptr;
89 hip_host_pinned_memory_size_ = 0;
92 #endif // KOKKOS_ENABLE_HIP
94 #ifdef KOKKOS_ENABLE_SYCL
96 void* sycl_memory_ =
nullptr;
97 size_t sycl_memory_size_ = 0;
99 void finalize_sycl_memory ()
101 if (sycl_memory_ !=
nullptr) {
102 Kokkos::kokkos_free<Kokkos::Experimental::SYCLDeviceUSMSpace> (sycl_memory_);
103 sycl_memory_ =
nullptr;
104 sycl_memory_size_ = 0;
108 void* sycl_shared_memory_ =
nullptr;
109 size_t sycl_shared_memory_size_ = 0;
111 void finalize_sycl_shared_memory ()
113 if (sycl_shared_memory_ !=
nullptr) {
114 Kokkos::kokkos_free<Kokkos::Experimental::SYCLSharedUSMSpace> (sycl_shared_memory_);
115 sycl_shared_memory_ =
nullptr;
116 sycl_shared_memory_size_ = 0;
119 #endif // KOKKOS_ENABLE_SYCL
121 void* host_memory_ =
nullptr;
122 size_t host_memory_size_ = 0;
124 void finalize_host_memory ()
126 if (host_memory_ !=
nullptr) {
127 Kokkos::kokkos_free<Kokkos::HostSpace> (host_memory_);
128 host_memory_ =
nullptr;
129 host_memory_size_ = 0;
135 #ifdef KOKKOS_ENABLE_CUDA
138 StaticKokkosAllocation<Kokkos::CudaSpace>::
139 resize (Kokkos::CudaSpace ,
142 using memory_space = Kokkos::CudaSpace;
143 static bool created_finalize_hook =
false;
145 if (size > cuda_memory_size_) {
146 if (cuda_memory_ !=
nullptr) {
147 Kokkos::kokkos_free<memory_space> (cuda_memory_);
149 const size_t req_size = size > minimum_initial_size ? size : minimum_initial_size;
150 cuda_memory_ = Kokkos::kokkos_malloc<memory_space> (req_size);
151 cuda_memory_size_ = size;
153 if (! created_finalize_hook) {
154 Kokkos::push_finalize_hook (finalize_cuda_memory);
155 created_finalize_hook =
true;
162 StaticKokkosAllocation<Kokkos::CudaUVMSpace>::
163 resize (Kokkos::CudaUVMSpace ,
166 using memory_space = Kokkos::CudaUVMSpace;
167 static bool created_finalize_hook =
false;
169 const size_t req_size = size > minimum_initial_size ? size : minimum_initial_size;
170 if (req_size > cuda_uvm_memory_size_) {
171 if (cuda_uvm_memory_ !=
nullptr) {
172 Kokkos::kokkos_free<memory_space> (cuda_uvm_memory_);
174 cuda_uvm_memory_ = Kokkos::kokkos_malloc<memory_space> (req_size);
175 cuda_uvm_memory_size_ = req_size;
177 if (! created_finalize_hook) {
178 Kokkos::push_finalize_hook (finalize_cuda_uvm_memory);
179 created_finalize_hook =
true;
182 return cuda_uvm_memory_;
186 StaticKokkosAllocation<Kokkos::CudaHostPinnedSpace>::
187 resize (Kokkos::CudaHostPinnedSpace ,
190 using memory_space = Kokkos::CudaHostPinnedSpace;
191 static bool created_finalize_hook =
false;
193 const size_t req_size = size > minimum_initial_size ? size : minimum_initial_size;
194 if (req_size > cuda_host_pinned_memory_size_) {
195 if (cuda_host_pinned_memory_ !=
nullptr) {
196 Kokkos::kokkos_free<memory_space> (cuda_host_pinned_memory_);
198 cuda_host_pinned_memory_ = Kokkos::kokkos_malloc<memory_space> (req_size);
199 cuda_host_pinned_memory_size_ = req_size;
201 if (! created_finalize_hook) {
202 Kokkos::push_finalize_hook (finalize_cuda_host_pinned_memory);
203 created_finalize_hook =
true;
206 return cuda_host_pinned_memory_;
209 #endif // KOKKOS_ENABLE_CUDA
211 #ifdef KOKKOS_ENABLE_HIP
214 StaticKokkosAllocation<Kokkos::HIPSpace>::
215 resize (Kokkos::HIPSpace ,
218 using memory_space = Kokkos::HIPSpace;
219 static bool created_finalize_hook =
false;
221 if (size > hip_memory_size_) {
222 if (hip_memory_ !=
nullptr) {
223 Kokkos::kokkos_free<memory_space> (hip_memory_);
225 const size_t req_size = size > minimum_initial_size ? size : minimum_initial_size;
226 hip_memory_ = Kokkos::kokkos_malloc<memory_space> (req_size);
227 hip_memory_size_ = size;
229 if (! created_finalize_hook) {
230 Kokkos::push_finalize_hook (finalize_hip_memory);
231 created_finalize_hook =
true;
238 StaticKokkosAllocation<Kokkos::HIPHostPinnedSpace>::
239 resize (Kokkos::HIPHostPinnedSpace ,
242 using memory_space = Kokkos::HIPHostPinnedSpace;
243 static bool created_finalize_hook =
false;
245 const size_t req_size = size > minimum_initial_size ? size : minimum_initial_size;
246 if (req_size > hip_host_pinned_memory_size_) {
247 if (hip_host_pinned_memory_ !=
nullptr) {
248 Kokkos::kokkos_free<memory_space> (hip_host_pinned_memory_);
250 hip_host_pinned_memory_ = Kokkos::kokkos_malloc<memory_space> (req_size);
251 hip_host_pinned_memory_size_ = req_size;
253 if (! created_finalize_hook) {
254 Kokkos::push_finalize_hook (finalize_hip_host_pinned_memory);
255 created_finalize_hook =
true;
258 return hip_host_pinned_memory_;
261 #endif // KOKKOS_ENABLE_HIP
263 #ifdef KOKKOS_ENABLE_SYCL
267 StaticKokkosAllocation<Kokkos::Experimental::SYCLDeviceUSMSpace>::
268 resize (Kokkos::Experimental::SYCLDeviceUSMSpace ,
271 using memory_space = Kokkos::Experimental::SYCLDeviceUSMSpace;
272 static bool created_finalize_hook =
false;
274 if (size > sycl_memory_size_) {
275 if (sycl_memory_ !=
nullptr) {
276 Kokkos::kokkos_free<memory_space> (sycl_memory_);
278 const size_t req_size = size > minimum_initial_size ? size : minimum_initial_size;
279 sycl_memory_ = Kokkos::kokkos_malloc<memory_space> (req_size);
280 sycl_memory_size_ = size;
282 if (! created_finalize_hook) {
283 Kokkos::push_finalize_hook (finalize_sycl_memory);
284 created_finalize_hook =
true;
292 StaticKokkosAllocation<Kokkos::Experimental::SYCLSharedUSMSpace>::
293 resize (Kokkos::Experimental::SYCLSharedUSMSpace ,
296 using memory_space = Kokkos::Experimental::SYCLSharedUSMSpace;
297 static bool created_finalize_hook =
false;
299 const size_t req_size = size > minimum_initial_size ? size : minimum_initial_size;
300 if (req_size > sycl_shared_memory_size_) {
301 if (sycl_shared_memory_ !=
nullptr) {
302 Kokkos::kokkos_free<memory_space> (sycl_shared_memory_);
304 sycl_shared_memory_ = Kokkos::kokkos_malloc<memory_space> (req_size);
305 sycl_shared_memory_size_ = req_size;
307 if (! created_finalize_hook) {
308 Kokkos::push_finalize_hook (finalize_sycl_shared_memory);
309 created_finalize_hook =
true;
312 return sycl_shared_memory_;
315 #endif // KOKKOS_ENABLE_SYCL
318 StaticKokkosAllocation<Kokkos::HostSpace>::
319 resize (Kokkos::HostSpace ,
322 using memory_space = Kokkos::HostSpace;
323 static bool created_finalize_hook =
false;
325 const size_t req_size = size > minimum_initial_size ? size : minimum_initial_size;
326 if (req_size > host_memory_size_) {
327 if (host_memory_ !=
nullptr) {
328 Kokkos::kokkos_free<memory_space> (host_memory_);
330 host_memory_ = Kokkos::kokkos_malloc<memory_space> (req_size);
331 host_memory_size_ = req_size;
333 if (! created_finalize_hook) {
334 Kokkos::push_finalize_hook (finalize_host_memory);
335 created_finalize_hook =
true;