17#ifndef NANOVDB_CUDA_UNIFIEDBUFFER_H_HAS_BEEN_INCLUDED
18#define NANOVDB_CUDA_UNIFIEDBUFFER_H_HAS_BEEN_INCLUDED
22#include <initializer_list>
48 size_t mSize, mCapacity;
51 using PtrT = std::shared_ptr<DualUnifiedBuffer>;
63 assert(mSize <= mCapacity);
64 cudaCheck(cudaMallocManaged(&mPtr, mCapacity, cudaMemAttachGlobal));
77 assert(mSize <= mCapacity);
78 cudaCheck(cudaMallocManaged(&mPtr, mCapacity, cudaMemAttachGlobal));
96 , mCapacity(other.mCapacity)
99 other.mSize = other.mCapacity = 0;
149 int device = cudaCpuDeviceId;
150 if (!host) cudaGetDevice(&device);
151 return create(
size, reference, device, (cudaStream_t)stream);
159 mSize = mCapacity = 0;
171 mCapacity = other.mCapacity;
172 other.mPtr =
nullptr;
173 other.mSize = other.mCapacity = 0;
194 void resize(
size_t size,
int dev = cudaCpuDeviceId, std::initializer_list<cudaMemoryAdvise> list = {cudaMemAdviseSetPreferredLocation})
196 if (
size <= mCapacity) {
200 cudaCheck(cudaMallocManaged(&ptr,
size, cudaMemAttachGlobal));
203 cudaCheck(cudaMemcpy(ptr, mPtr, std::min(mSize,
size), cudaMemcpyDefault));
207 mSize = mCapacity =
size;
216 void advise(ptrdiff_t byteOffset,
size_t size,
int dev, cudaMemoryAdvise adv)
const
226 void advise(ptrdiff_t byteOffset,
size_t size,
int dev, std::initializer_list<cudaMemoryAdvise> list)
const
237 void prefetch(ptrdiff_t byteOffset = 0,
size_t size = 0,
int dev = cudaCpuDeviceId, cudaStream_t stream = cudaStreamPerThread)
const
249 void deviceUpload(
int device = 0, cudaStream_t stream = cudaStreamPerThread,
bool sync =
false)
const
252 if (sync)
cudaCheck(cudaStreamSynchronize(stream));
274 if (sync)
cudaCheck(cudaStreamSynchronize(stream));
289 void*
data()
const {
return mPtr;}
295 template <
typename T>
296 T*
data(ptrdiff_t count = 0)
const {
298 return reinterpret_cast<T*
>(mPtr) + count;
304 void*
data(ptrdiff_t byteOffset)
const {
316 size_t size()
const {
return mSize;}
324 inline bool empty()
const {
return mPtr ==
nullptr; }
343using UnifiedBuffer [[deprecated(
"managed grid storage is moving to cuda::Buffer<std::byte, cuda::ManagedResource> (cuda/Buffer.h): pass it to the multi-GPU builders, or build into a host handle and use cuda::copyTo (cuda/HandleStorage.h); see the multi-GPU examples")]] = DualUnifiedBuffer;
HostBuffer - a buffer that contains a shared or private bump pool to either externally or internally ...
void prefetch(ptrdiff_t byteOffset=0, size_t size=0, int dev=cudaCpuDeviceId, cudaStream_t stream=cudaStreamPerThread) const
Prefetches data to the specified device, i.e. ensure the device has an up-to-date copy of the memory ...
Definition UnifiedBuffer.h:237
static DualUnifiedBuffer create(size_t size, const DualUnifiedBuffer *reference, bool host, void *stream=nullptr)
Factory method that created a buffer on the host or device of the specified size. If the reference bu...
Definition UnifiedBuffer.h:147
DualUnifiedBuffer()
Default constructor of an empty buffer.
Definition UnifiedBuffer.h:54
DualUnifiedBuffer & operator=(const DualUnifiedBuffer &)=delete
Disallow copy assignment operation.
size_t size() const
Size of the allocated pages in this instance.
Definition UnifiedBuffer.h:316
static PtrT createPtr(size_t size)
Factory methods that create a shared pointer to an DualUnifiedBuffer instance.
Definition UnifiedBuffer.h:116
std::shared_ptr< DualUnifiedBuffer > PtrT
Definition UnifiedBuffer.h:51
T * data(ptrdiff_t count=0) const
Returns an offset pointer of a specific type from the allocated unified memory.
Definition UnifiedBuffer.h:296
DualUnifiedBuffer(const DualUnifiedBuffer &)=delete
Disallow copy-construction.
DualUnifiedBuffer(uint64_t size, int device, cudaStream_t stream=0)
Constructor with a specified device.
Definition UnifiedBuffer.h:87
DualUnifiedBuffer(DualUnifiedBuffer &&other) noexcept
Move copy-constructor.
Definition UnifiedBuffer.h:93
void deviceUpload(int device, void *stream, bool sync) const
Definition UnifiedBuffer.h:254
void * deviceData() const
Legacy.
Definition UnifiedBuffer.h:311
void deviceDownload(cudaStream_t stream=0, bool sync=false) const
Prefetches all data to the host.
Definition UnifiedBuffer.h:271
bool empty() const
Returns true if this allocator is empty, i.e. has no allocated memory.
Definition UnifiedBuffer.h:324
void deviceDownload(int dummmy, void *stream, bool sync) const
Definition UnifiedBuffer.h:283
void * deviceData(int) const
Definition UnifiedBuffer.h:312
DualUnifiedBuffer & operator=(DualUnifiedBuffer &&other)
Allow move assignment operation.
Definition UnifiedBuffer.h:166
DualUnifiedBuffer(size_t size, size_t capacity)
Constructor that specifies both the size and capacity.
Definition UnifiedBuffer.h:61
size_t capacity() const
Capacity of this instance, i.e. room in page table.
Definition UnifiedBuffer.h:320
DualUnifiedBuffer(uint64_t size, uint64_t capacity, int device, cudaStream_t stream=0)
Constructor that specifies the size, capacity, and device (for prefetching)
Definition UnifiedBuffer.h:75
void deviceUpload(void *stream, bool sync) const
Prefetches all data to the current device, as given by cudaGetDevice.
Definition UnifiedBuffer.h:260
DualUnifiedBuffer(size_t size)
Similar to the constructor above except the size and capacity are equal, so no future growth is suppo...
Definition UnifiedBuffer.h:68
void deviceDownload(void *stream, bool sync) const
Legacy.
Definition UnifiedBuffer.h:280
void * data() const
Returns a raw pointer to the unified memory managed by this instance.
Definition UnifiedBuffer.h:289
~DualUnifiedBuffer()
Destructor.
Definition UnifiedBuffer.h:103
void init(size_t size, size_t capacity)
initialize buffer as a new with the specified size and capacity
Definition UnifiedBuffer.h:180
static DualUnifiedBuffer create(size_t size)
Factory methods that create an DualUnifiedBuffer instance and returns it with move semantics.
Definition UnifiedBuffer.h:110
void deviceUpload(int device=0, cudaStream_t stream=cudaStreamPerThread, bool sync=false) const
Prefetches all data to the specified device.
Definition UnifiedBuffer.h:249
void resize(size_t size, int dev=cudaCpuDeviceId, std::initializer_list< cudaMemoryAdvise > list={cudaMemAdviseSetPreferredLocation})
Resize the memory block managed by this buffer. If the current capacity is larger than the new size t...
Definition UnifiedBuffer.h:194
void advise(ptrdiff_t byteOffset, size_t size, int dev, cudaMemoryAdvise adv) const
Apply a single advise to a memory block.
Definition UnifiedBuffer.h:216
static DualUnifiedBuffer create(size_t size, const DualUnifiedBuffer *reference)
Factory method that created a buffer on the host of the specified size. If the reference buffer has a...
Definition UnifiedBuffer.h:139
void clear()
Free all memory and reset this instance to empty.
Definition UnifiedBuffer.h:155
bool isEmpty() const
Definition UnifiedBuffer.h:325
void * data(ptrdiff_t byteOffset) const
Returns a byte offset void pointer from the unified memory.
Definition UnifiedBuffer.h:304
static PtrT createPtr(size_t size, size_t capacity)
Factory methods that create a shared pointer to an DualUnifiedBuffer instance.
Definition UnifiedBuffer.h:115
static DualUnifiedBuffer create(size_t size, size_t capacity)
Factory methods that create an DualUnifiedBuffer instance and returns it with move semantics.
Definition UnifiedBuffer.h:109
void advise(ptrdiff_t byteOffset, size_t size, int dev, std::initializer_list< cudaMemoryAdvise > list) const
Apply a list of advices to a memory block.
Definition UnifiedBuffer.h:226
static DualUnifiedBuffer create(size_t size, const DualUnifiedBuffer *reference, int device, cudaStream_t stream)
Legacy factory method that mirrors DeviceBuffer. It creates a DualUnifiedBuffer from a size and a ref...
Definition UnifiedBuffer.h:126
Definition GridHandle.h:37
cudaError_t memPrefetchAsync(const void *devPtr, size_t count, int dstDevice, cudaStream_t stream)
Compatbility wrapper for cudaMemPrefetchAsync/cudaMemPrefetchAsync.
Definition Util.h:271
cudaError_t memAdvise(const void *devPtr, size_t count, cudaMemoryAdvise advice, int device)
Compatbility wrapper for cudaMemAdvise/cudaMemAdvise.
Definition Util.h:266
static DstT * PtrAdd(void *p, int64_t offset)
Adds a byte offset to a non-const pointer to produce another non-const pointer.
Definition Util.h:524
Defines a simple memory pool used to call cub functions that use dynamic temporary storage.
Definition GridHandle.h:31
#define NANOVDB_ASSERT(x)
Definition Util.h:53
Cuda specific utility functions.
#define cudaCheck(ans)
Definition Util.h:49
static constexpr bool hasDeviceDual
Definition UnifiedBuffer.h:350
Definition HostBuffer.h:101