17 #ifndef NANOVDB_CUDA_DEVICE_BUFFER_H_HAS_BEEN_INCLUDED
18 #define NANOVDB_CUDA_DEVICE_BUFFER_H_HAS_BEEN_INCLUDED
20 #include "../HostBuffer.h"
36 uint8_t *mCpuData, *mGpuData;
65 , mCpuData(other.mCpuData)
66 , mGpuData(other.mGpuData)
69 other.mCpuData =
nullptr;
70 other.mGpuData =
nullptr;
81 mCpuData = other.mCpuData;
82 mGpuData = other.mGpuData;
84 other.mCpuData =
nullptr;
85 other.mGpuData =
nullptr;
97 void init(uint64_t size,
bool host =
true,
void*
stream =
nullptr);
101 uint8_t*
data()
const {
return mCpuData; }
122 uint64_t
size()
const {
return mSize; }
126 bool empty()
const {
return mSize == 0; }
150 if (mSize>0) this->
clear(stream);
153 cudaCheck(cudaMallocHost((
void**)&mCpuData, size));
154 checkPtr(mCpuData,
"CudaDeviceBuffer::init: failed to allocate host buffer");
157 checkPtr(mGpuData,
"CudaDeviceBuffer::init: failed to allocate device buffer");
164 checkPtr(mCpuData,
"uninitialized cpu data");
165 if (mGpuData ==
nullptr) {
168 checkPtr(mGpuData,
"uninitialized gpu data");
169 cudaCheck(cudaMemcpyAsync(mGpuData, mCpuData, mSize, cudaMemcpyHostToDevice, reinterpret_cast<cudaStream_t>(stream)));
170 if (sync)
cudaCheck(cudaStreamSynchronize(reinterpret_cast<cudaStream_t>(stream)));
175 checkPtr(mGpuData,
"uninitialized gpu data");
176 if (mCpuData ==
nullptr) {
177 cudaCheck(cudaMallocHost((
void**)&mCpuData, mSize));
179 checkPtr(mCpuData,
"uninitialized cpu data");
180 cudaCheck(cudaMemcpyAsync(mCpuData, mGpuData, mSize, cudaMemcpyDeviceToHost, reinterpret_cast<cudaStream_t>(stream)));
181 if (sync)
cudaCheck(cudaStreamSynchronize(reinterpret_cast<cudaStream_t>(stream)));
187 if (mCpuData)
cudaCheck(cudaFreeHost(mCpuData));
188 mCpuData = mGpuData =
nullptr;
194 #endif // end of NANOVDB_CUDA_DEVICE_BUFFER_H_HAS_BEEN_INCLUDED
uint64_t size() const
Returns the size in bytes of the raw memory buffer managed by this allocator.
CudaDeviceBuffer(uint64_t size=0, bool host=true, void *stream=nullptr)
Constructor.
void clear(void *stream=nullptr)
De-allocate all memory managed by this allocator and set all pointers to NULL.
cudaError_t cudaFreeAsync(void *d_ptr, cudaStream_t)
Dummy implementation of cudaFreeAsync that calls cudaFree.
#define checkPtr(ptr, msg)
CudaDeviceBuffer & operator=(CudaDeviceBuffer &&other) noexcept
Move copy assignment operation.
void init(uint64_t size, bool host=true, void *stream=nullptr)
Initialize buffer.
#define NANOVDB_ASSERT(x)
bool isEmpty() const
Returns true if this allocator is empty, i.e. has no allocated memory.
~CudaDeviceBuffer()
Destructor frees memory on both the host and device.
bool empty() const
Returns true if this allocator is empty, i.e. has no allocated memory.
Simple memory buffer using un-managed pinned host memory when compiled with NVCC. Obviously this clas...
cudaError_t cudaMallocAsync(void **d_ptr, size_t size, cudaStream_t)
Dummy implementation of cudaMallocAsync that calls cudaMalloc.
constexpr enabler dummy
An instance to use in EnableIf.
static constexpr bool hasDeviceDual
uint8_t * data() const
Retuns a raw pointer to the host/CPU buffer managed by this allocator.
void deviceUpload(void *stream=nullptr, bool sync=true) const
Upload this buffer from the host to the device, i.e. CPU -> GPU.
CudaDeviceBuffer(CudaDeviceBuffer &&other) noexcept
Move copy-constructor.
CudaDeviceBuffer & operator=(const CudaDeviceBuffer &)=delete
Disallow copy assignment operation.
uint8_t * deviceData() const
Retuns a raw pointer to the device/GPU buffer managed by this allocator.
void deviceDownload(void *stream=nullptr, bool sync=true) const
Upload this buffer from the device to the host, i.e. GPU -> CPU.
static CudaDeviceBuffer create(uint64_t size, const CudaDeviceBuffer *dummy=nullptr, bool host=true, void *stream=nullptr)
Static factory method that return an instance of this buffer.