16 #ifndef OPM_SIMULATORS_LINALG_GPUISTL_GPU_SMART_POINTER_HPP 17 #define OPM_SIMULATORS_LINALG_GPUISTL_GPU_SMART_POINTER_HPP 19 #include <cuda_runtime.h> 23 #include <opm/common/utility/gpuDecorators.hpp> 24 #include <opm/simulators/linalg/gpuistl/detail/gpu_safe_call.hpp> 25 #include <opm/simulators/linalg/gpuistl/detail/is_gpu_pointer.hpp> 51 OPM_GPU_SAFE_CALL(cudaMalloc(&ptr,
sizeof(T)));
52 auto deleter = [](T* ptrToDelete) { OPM_GPU_WARN_IF_ERROR(cudaFree(ptrToDelete)); };
53 return std::shared_ptr<T>(ptr, deleter);
71 auto ptr = make_gpu_shared_ptr<T>();
72 OPM_GPU_SAFE_CALL(cudaMemcpy(ptr.get(), &value,
sizeof(T), cudaMemcpyHostToDevice));
92 OPM_GPU_SAFE_CALL(cudaMalloc(&ptr,
sizeof(T)));
94 auto deleter = [](T* ptrToDelete) { OPM_GPU_WARN_IF_ERROR(cudaFree(ptrToDelete)); };
95 return std::unique_ptr<T, decltype(deleter)>(ptr, deleter);
109 template <
typename T>
113 auto ptr = make_gpu_unique_ptr<T>();
114 OPM_GPU_SAFE_CALL(cudaMemcpy(ptr.get(), &value,
sizeof(T), cudaMemcpyHostToDevice));
127 void operator()(T* ptr)
const noexcept
129 OPM_GPU_WARN_IF_ERROR(cudaFree(ptr));
141 void operator()(T* ptr)
const noexcept
143 if (ptr !=
nullptr) {
145 OPM_GPU_WARN_IF_ERROR(cudaFree(ptr));
165 template <
typename T>
166 std::unique_ptr<T[], GpuArrayDeleter<T>>
170 OPM_GPU_SAFE_CALL(cudaMalloc(&ptr, numElements *
sizeof(T)));
171 return std::unique_ptr<T[], GpuArrayDeleter<T>>(ptr);
192 template <
typename T,
class... Args>
193 std::unique_ptr<T, GpuManagedDeleter<T>>
197 OPM_GPU_SAFE_CALL(cudaMallocManaged(&raw,
sizeof(T)));
200 ptr =
new (raw) T(std::forward<Args>(args)...);
202 OPM_GPU_WARN_IF_ERROR(cudaFree(raw));
205 return std::unique_ptr<T, GpuManagedDeleter<T>>(ptr);
225 OPM_GPU_SAFE_CALL(cudaMemcpy(&result, value,
sizeof(T), cudaMemcpyDeviceToHost));
255 template <
class T,
class Deleter>
277 OPM_GPU_SAFE_CALL(cudaMemcpy(ptr, &value,
sizeof(T), cudaMemcpyHostToDevice));
290 copyToGPU(
const T& value,
const std::shared_ptr<T>& ptr)
304 template <
class T,
class Deleter>
306 copyToGPU(
const T& value,
const std::unique_ptr<T, Deleter>& ptr)
330 template <
class Deleter>
331 PointerView(
const std::unique_ptr<T, Deleter>& ptr)
341 OPM_HOST_DEVICE T*
get()
const 346 OPM_HOST_DEVICE
const T& operator*()
const 351 OPM_HOST_DEVICE T& operator*()
356 OPM_HOST_DEVICE T* operator->()
const 381 template <
class Deleter>
382 PointerView(
const std::unique_ptr<void, Deleter>& ptr)
392 OPM_HOST_DEVICE
void*
get()
const 397 OPM_HOST_DEVICE
void* operator->()
const 408 make_view(
const std::shared_ptr<T>& ptr)
413 template <
class T,
class Deleter>
415 make_view(
const std::unique_ptr<T, Deleter>& ptr)
417 return PointerView<T>(ptr);
427 using element_type = T;
433 OPM_HOST_DEVICE T* operator->() {
437 OPM_HOST_DEVICE T*
get() {
441 OPM_HOST_DEVICE
const T* operator->()
const {
445 OPM_HOST_DEVICE
const T*
get()
const {
449 OPM_HOST_DEVICE T& operator*() {
453 OPM_HOST_DEVICE
const T& operator*()
const {
A view towards a smart pointer to GPU-allocated memory.
Definition: gpu_smart_pointer.hpp:320
Deleter that releases a GPU array allocation made with cudaMalloc.
Definition: gpu_smart_pointer.hpp:126
Deleter for objects living in unified (managed) GPU memory.
Definition: gpu_smart_pointer.hpp:140
std::unique_ptr< T, GpuManagedDeleter< T > > make_gpu_managed_unique_ptr(Args &&... args)
Creates a unique pointer managing GPU unified (managed) memory for a single object.
Definition: gpu_smart_pointer.hpp:194
auto make_gpu_unique_ptr()
Creates a unique pointer managing GPU-allocated memory of the specified element type.
Definition: gpu_smart_pointer.hpp:89
T copyFromGPU(const T *value)
Copies a value from GPU-allocated memory to the host.
Definition: gpu_smart_pointer.hpp:219
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUD...
Definition: ThermalGasWaterFlowProblem.hpp:94
A value stored with a pointer interface.
Definition: gpu_smart_pointer.hpp:425
std::unique_ptr< T[], GpuArrayDeleter< T > > make_gpu_unique_ptr_array(std::size_t numElements)
Creates a unique pointer managing a GPU-allocated array of numElements elements.
Definition: gpu_smart_pointer.hpp:167
bool isGPUPointer(const T *ptr)
Checks whether the given pointer is associated with GPU device memory.
Definition: is_gpu_pointer.hpp:40
std::shared_ptr< T > make_gpu_shared_ptr()
Creates a shared pointer managing GPU-allocated memory of the specified element type.
Definition: gpu_smart_pointer.hpp:48
void copyToGPU(const T &value, T *ptr)
Copies a value from the host to GPU-allocated memory.
Definition: gpu_smart_pointer.hpp:272