16#ifndef NANOVDB_CUDA_DEVICE_BUFFER_H_HAS_BEEN_INCLUDED
17#define NANOVDB_CUDA_DEVICE_BUFFER_H_HAS_BEEN_INCLUDED
21#include <cuda_runtime_api.h>
23#if defined(DEBUG) || defined(_DEBUG)
24 static inline void gpuAssert(cudaError_t code,
const char* file,
int line,
bool abort =
true)
26 if (code != cudaSuccess) {
27 fprintf(stderr,
"CUDA Runtime Error: %s %s %d\n", cudaGetErrorString(code), file, line);
28 if (abort) exit(code);
31 static inline void ptrAssert(
void* ptr,
const char* msg,
const char* file,
int line,
bool abort =
true)
34 fprintf(stderr,
"NULL pointer error: %s %s %d\n", msg, file, line);
38 fprintf(stderr,
"Pointer misalignment error: %s %s %d\n", msg, file, line);
43 static inline void gpuAssert(cudaError_t,
const char*,
int,
bool =
true){}
44 static inline void ptrAssert(
void*,
const char*,
const char*,
int,
bool =
true){}
49#define cudaCheck(ans) \
51 gpuAssert((ans), __FILE__, __LINE__); \
54#define checkPtr(ptr, msg) \
56 ptrAssert((ptr), (msg), __FILE__, __LINE__); \
71 uint8_t *mCpuData, *mGpuData;
86 , mCpuData(other.mCpuData)
87 , mGpuData(other.mGpuData)
90 other.mCpuData =
nullptr;
91 other.mGpuData =
nullptr;
100 mCpuData = other.mCpuData;
101 mGpuData = other.mGpuData;
103 other.mCpuData =
nullptr;
104 other.mGpuData =
nullptr;
115 uint8_t*
data()
const {
return mCpuData; }
121 void deviceUpload(
void* stream = 0,
bool sync =
true)
const;
127 uint64_t
size()
const {
return mSize; }
130 bool empty()
const {
return mSize == 0; }
162 checkPtr(mCpuData,
"failed to allocate host data");
167 checkPtr(mCpuData,
"uninitialized cpu data");
168 if (mGpuData ==
nullptr)
169 cudaCheck(cudaMalloc((
void**)&mGpuData, mSize));
170 checkPtr(mGpuData,
"uninitialized gpu data");
171 cudaCheck(cudaMemcpyAsync(mGpuData, mCpuData, mSize, cudaMemcpyHostToDevice,
reinterpret_cast<cudaStream_t
>(stream)));
173 cudaCheck(cudaStreamSynchronize(
reinterpret_cast<cudaStream_t
>(stream)));
178 checkPtr(mCpuData,
"uninitialized cpu data");
179 checkPtr(mGpuData,
"uninitialized gpu data");
180 cudaCheck(cudaMemcpyAsync(mCpuData, mGpuData, mSize, cudaMemcpyDeviceToHost,
reinterpret_cast<cudaStream_t
>(stream)));
182 cudaCheck(cudaStreamSynchronize(
reinterpret_cast<cudaStream_t
>(stream)));
191 mCpuData = mGpuData =
nullptr;
#define cudaCheck(ans)
Definition: CudaDeviceBuffer.h:49
static void ptrAssert(void *, const char *, const char *, int, bool=true)
Definition: CudaDeviceBuffer.h:44
static void gpuAssert(cudaError_t, const char *, int, bool=true)
Definition: CudaDeviceBuffer.h:43
#define checkPtr(ptr, msg)
Definition: CudaDeviceBuffer.h:54
HostBuffer - a buffer that contains a shared or private bump pool to either externally or internally ...
#define NANOVDB_DATA_ALIGNMENT
Definition: NanoVDB.h:137
Simple memory buffer using un-managed pinned host memory when compiled with NVCC. Obviously this clas...
Definition: CudaDeviceBuffer.h:69
CudaDeviceBuffer & operator=(CudaDeviceBuffer &&other) noexcept
Move copy assignment operation.
Definition: CudaDeviceBuffer.h:96
void init(uint64_t size)
Definition: CudaDeviceBuffer.h:152
CudaDeviceBuffer(uint64_t size=0)
Definition: CudaDeviceBuffer.h:74
void deviceDownload(void *stream=0, bool sync=true) const
Copy grid from the GPU/device to the CPU/host. If sync is false the memory copy is asynchronous!
Definition: CudaDeviceBuffer.h:176
uint8_t * data() const
Definition: CudaDeviceBuffer.h:115
CudaDeviceBuffer(CudaDeviceBuffer &&other) noexcept
Move copy-constructor.
Definition: CudaDeviceBuffer.h:84
bool empty() const
Returns true if this allocator is empty, i.e. has no allocated memory.
Definition: CudaDeviceBuffer.h:130
uint64_t size() const
Returns the size in bytes of the raw memory buffer managed by this allocator.
Definition: CudaDeviceBuffer.h:127
~CudaDeviceBuffer()
Destructor frees memory on both the host and device.
Definition: CudaDeviceBuffer.h:108
uint8_t * deviceData() const
Definition: CudaDeviceBuffer.h:116
void deviceUpload(void *stream=0, bool sync=true) const
Copy grid from the CPU/host to the GPU/device. If sync is false the memory copy is asynchronous!
Definition: CudaDeviceBuffer.h:165
static CudaDeviceBuffer create(uint64_t size, const CudaDeviceBuffer *context=nullptr)
Definition: CudaDeviceBuffer.h:147
void clear()
De-allocate all memory managed by this allocator and set all pointer to NULL.
Definition: CudaDeviceBuffer.h:185
CudaDeviceBuffer & operator=(const CudaDeviceBuffer &)=delete
Disallow copy assignment operation.
CudaDeviceBuffer(const CudaDeviceBuffer &)=delete
Disallow copy-construction.
Definition: NanoVDB.h:208
Definition: HostBuffer.h:100
static const bool hasDeviceDual
Definition: HostBuffer.h:101