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);
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);
114 OPM_GPU_SAFE_CALL(cudaMemcpy(ptr.get(), &value,
sizeof(T), cudaMemcpyHostToDevice));
135 OPM_GPU_SAFE_CALL(cudaMemcpy(&result, value,
sizeof(T), cudaMemcpyDeviceToHost));
165template <
class T,
class Deleter>
187 OPM_GPU_SAFE_CALL(cudaMemcpy(ptr, &value,
sizeof(T), cudaMemcpyHostToDevice));
214template <
class T,
class Deleter>
216copyToGPU(
const T& value,
const std::unique_ptr<T, Deleter>& ptr)
233 PointerView(
const PointerView& other) =
default;
235 PointerView(
const std::shared_ptr<T>& ptr)
240 template <
class Deleter>
241 PointerView(
const std::unique_ptr<T, Deleter>& ptr)
251 OPM_HOST_DEVICE T* get()
const
256 OPM_HOST_DEVICE
const T& operator*()
const
261 OPM_HOST_DEVICE T& operator*()
266 OPM_HOST_DEVICE T* operator->()
const
281class PointerView<void>
284 PointerView(
const PointerView& other) =
default;
286 PointerView(
const std::shared_ptr<void>& ptr)
291 template <
class Deleter>
292 PointerView(
const std::unique_ptr<void, Deleter>& ptr)
297 PointerView(
void* ptr)
302 OPM_HOST_DEVICE
void* get()
const
307 OPM_HOST_DEVICE
void* operator->()
const
318make_view(
const std::shared_ptr<T>& ptr)
323template <
class T,
class Deleter>
325make_view(
const std::unique_ptr<T, Deleter>& ptr)
327 return PointerView<T>(ptr);
335class ValueAsPointer {
337 using element_type = T;
339 ValueAsPointer(
const T& t) : value(t) {}
341 OPM_HOST_DEVICE T* operator->() {
345 OPM_HOST_DEVICE T* get() {
349 OPM_HOST_DEVICE
const T* operator->()
const {
353 OPM_HOST_DEVICE
const T* get()
const {
357 OPM_HOST_DEVICE T& operator*() {
361 OPM_HOST_DEVICE
const T& operator*()
const {
A view towards a smart pointer to GPU-allocated memory.
Definition gpu_smart_pointer.hpp:231
void copyToGPU(const T &value, T *ptr)
Copies a value from the host to GPU-allocated memory.
Definition gpu_smart_pointer.hpp:182
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
T copyFromGPU(const T *value)
Copies a value from GPU-allocated memory to the host.
Definition gpu_smart_pointer.hpp:129
auto make_gpu_unique_ptr()
Creates a unique pointer managing GPU-allocated memory of the specified element type.
Definition gpu_smart_pointer.hpp:89
bool isGPUPointer(const T *ptr)
Checks whether the given pointer is associated with GPU device memory.
Definition is_gpu_pointer.hpp:40