OpenVDB 13.1.0
Loading...
Searching...
No Matches
DualUnifiedBuffer Class Reference

buffer, used for instance by the GridHandle, to allocate unified memory that can be resized and shared between multiple devices and the host. More...

#include <nanovdb/cuda/UnifiedBuffer.h>

Public Types

using PtrT = std::shared_ptr<DualUnifiedBuffer>

Public Member Functions

 DualUnifiedBuffer ()
 Default constructor of an empty buffer.
 DualUnifiedBuffer (size_t size, size_t capacity)
 Constructor that specifies both the size and capacity.
 DualUnifiedBuffer (size_t size)
 Similar to the constructor above except the size and capacity are equal, so no future growth is supported.
 DualUnifiedBuffer (uint64_t size, uint64_t capacity, int device, cudaStream_t stream=0)
 Constructor that specifies the size, capacity, and device (for prefetching)
 DualUnifiedBuffer (uint64_t size, int device, cudaStream_t stream=0)
 Constructor with a specified device.
 DualUnifiedBuffer (const DualUnifiedBuffer &)=delete
 Disallow copy-construction.
 DualUnifiedBuffer (DualUnifiedBuffer &&other) noexcept
 Move copy-constructor.
 ~DualUnifiedBuffer ()
 Destructor.
void clear ()
 Free all memory and reset this instance to empty.
DualUnifiedBufferoperator= (const DualUnifiedBuffer &)=delete
 Disallow copy assignment operation.
DualUnifiedBufferoperator= (DualUnifiedBuffer &&other)
 Allow move assignment operation.
void init (size_t size, size_t capacity)
 initialize buffer as a new with the specified size and capacity
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 this method simply redefines size. Otherwise a new page-table is defined, with the specified advice, and the old block is copied to the new block.
void advise (ptrdiff_t byteOffset, size_t size, int dev, cudaMemoryAdvise adv) const
 Apply a single advise to a memory block.
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.
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 specified.
void deviceUpload (int device=0, cudaStream_t stream=cudaStreamPerThread, bool sync=false) const
 Prefetches all data to the specified device.
void deviceUpload (int device, void *stream, bool sync) const
void deviceUpload (void *stream, bool sync) const
 Prefetches all data to the current device, as given by cudaGetDevice.
void deviceDownload (cudaStream_t stream=0, bool sync=false) const
 Prefetches all data to the host.
void deviceDownload (void *stream, bool sync) const
 Legacy.
void deviceDownload (int dummmy, void *stream, bool sync) const
void * data () const
 Returns a raw pointer to the unified memory managed by this instance.
template<typename T>
T * data (ptrdiff_t count=0) const
 Returns an offset pointer of a specific type from the allocated unified memory.
void * data (ptrdiff_t byteOffset) const
 Returns a byte offset void pointer from the unified memory.
void * deviceData () const
 Legacy.
void * deviceData (int) const
size_t size () const
 Size of the allocated pages in this instance.
size_t capacity () const
 Capacity of this instance, i.e. room in page table.
bool empty () const
 Returns true if this allocator is empty, i.e. has no allocated memory.
bool isEmpty () const

Static Public Member Functions

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 reference buffer. If a reference buffer is provided and its non-empty, it is used to defined the capacity of the new buffer.
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 capacity it is used. Also the buffer is prefetched to the host.
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 buffer has a capacity it is used. Also the buffer is prefetched to the host or (current) device.
static DualUnifiedBuffer create (size_t size, size_t capacity)
 Factory methods that create an DualUnifiedBuffer instance and returns it with move semantics.
static DualUnifiedBuffer create (size_t size)
 Factory methods that create an DualUnifiedBuffer instance and returns it with move semantics.
static PtrT createPtr (size_t size, size_t capacity)
 Factory methods that create a shared pointer to an DualUnifiedBuffer instance.
static PtrT createPtr (size_t size)
 Factory methods that create a shared pointer to an DualUnifiedBuffer instance.

Detailed Description

buffer, used for instance by the GridHandle, to allocate unified memory that can be resized and shared between multiple devices and the host.

Note
This is the implementation behind the deprecated UnifiedBuffer alias below, renamed so the [[deprecated]] attribute reaches only code that spells the public name: DistributedPointsToGrid's signature defaults reference this implementation, so default-using callers stay warning-free until the defaults change at removal. The Dual prefix marks the deprecated dual-accessor buffer family (this buffer satisfies that concept from a single managed allocation). Transitional – do not adopt this name; it is deleted together with the alias. The header keeps its long-standing name and include path for the same reason: renaming a header breaks existing includes outright, and the old path is where external code will find the alias and its migration message.

Member Typedef Documentation

◆ PtrT

using PtrT = std::shared_ptr<DualUnifiedBuffer>

Constructor & Destructor Documentation

◆ DualUnifiedBuffer() [1/7]

DualUnifiedBuffer ( )
inline

Default constructor of an empty buffer.

◆ DualUnifiedBuffer() [2/7]

DualUnifiedBuffer ( size_t size,
size_t capacity )
inline

Constructor that specifies both the size and capacity.

Parameters
sizesize of the buffer in bytes, indication what is actually used
capacitynumber of bytes in the virtual page table, i.e max size for growing
Note
Capacity can be over-estimated to allow for future growth. Memory is not allocated with this constructor, only a page table. Allocation happens on usage or when calling prefetch

◆ DualUnifiedBuffer() [3/7]

DualUnifiedBuffer ( size_t size)
inline

Similar to the constructor above except the size and capacity are equal, so no future growth is supported.

◆ DualUnifiedBuffer() [4/7]

DualUnifiedBuffer ( uint64_t size,
uint64_t capacity,
int device,
cudaStream_t stream = 0 )
inline

Constructor that specifies the size, capacity, and device (for prefetching)

Parameters
size
capacity
device
stream

◆ DualUnifiedBuffer() [5/7]

DualUnifiedBuffer ( uint64_t size,
int device,
cudaStream_t stream = 0 )
inline

Constructor with a specified device.

Parameters
size
device
stream

◆ DualUnifiedBuffer() [6/7]

Disallow copy-construction.

◆ DualUnifiedBuffer() [7/7]

DualUnifiedBuffer ( DualUnifiedBuffer && other)
inlinenoexcept

Move copy-constructor.

◆ ~DualUnifiedBuffer()

~DualUnifiedBuffer ( )
inline

Destructor.

Member Function Documentation

◆ advise() [1/2]

void advise ( ptrdiff_t byteOffset,
size_t size,
int dev,
cudaMemoryAdvise adv ) const
inline

Apply a single advise to a memory block.

Parameters
byteOffsetoffset in bytes marking the beginning of the memory block to be advised
sizesize in bytes of the memory block to be advised.
devthe device ID on which to apply the advice provided in adv, cudaCpuDeviceId = -1, 0, 1, ...
advadvice to be applied to the resized range

◆ advise() [2/2]

void advise ( ptrdiff_t byteOffset,
size_t size,
int dev,
std::initializer_list< cudaMemoryAdvise > list ) const
inline

Apply a list of advices to a memory block.

Parameters
byteOffsetoffset in bytes marking the beginning of the memory block to be advised
sizesize in bytes of the memory block to be advised.
devthe device ID to prefetch to, cudaCpuDeviceId = -1, 0, 1, ...
listlist of cuda advises

◆ capacity()

size_t capacity ( ) const
inline

Capacity of this instance, i.e. room in page table.

Returns
number of bytes reserved, but not necessarily allocated, by this instance

◆ clear()

void clear ( )
inline

Free all memory and reset this instance to empty.

◆ create() [1/5]

DualUnifiedBuffer create ( size_t size)
inlinestatic

Factory methods that create an DualUnifiedBuffer instance and returns it with move semantics.

◆ create() [2/5]

DualUnifiedBuffer create ( size_t size,
const DualUnifiedBuffer * reference )
inlinestatic

Factory method that created a buffer on the host of the specified size. If the reference buffer has a capacity it is used. Also the buffer is prefetched to the host.

Parameters
sizebyte size of buffer initiated on the host
referenceoptional reference buffer from which the capacity is derived

◆ create() [3/5]

DualUnifiedBuffer create ( size_t size,
const DualUnifiedBuffer * reference,
bool host,
void * stream = nullptr )
inlinestatic

Factory method that created a buffer on the host or device of the specified size. If the reference buffer has a capacity it is used. Also the buffer is prefetched to the host or (current) device.

Parameters
sizebyte size of buffer initiated on the device or host
referenceoptional reference buffer from which the capacity is derived
hostIf true the buffer will be prefetched to the host, else to the current device
streamoptional cuda stream

◆ create() [4/5]

DualUnifiedBuffer create ( size_t size,
const DualUnifiedBuffer * reference,
int device,
cudaStream_t stream )
inlinestatic

Legacy factory method that mirrors DeviceBuffer. It creates a DualUnifiedBuffer from a size and a reference buffer. If a reference buffer is provided and its non-empty, it is used to defined the capacity of the new buffer.

Parameters
sizeSize on bytes of the new buffer
referencereference buffer optionally used to define the capacity
deviceDevice whose preferred memory location is set for the new buffer
streamcuda stream
Returns
An instance of a new DualUnifiedBuffer using move semantics

◆ create() [5/5]

DualUnifiedBuffer create ( size_t size,
size_t capacity )
inlinestatic

Factory methods that create an DualUnifiedBuffer instance and returns it with move semantics.

◆ createPtr() [1/2]

PtrT createPtr ( size_t size)
inlinestatic

Factory methods that create a shared pointer to an DualUnifiedBuffer instance.

◆ createPtr() [2/2]

PtrT createPtr ( size_t size,
size_t capacity )
inlinestatic

Factory methods that create a shared pointer to an DualUnifiedBuffer instance.

◆ data() [1/3]

void * data ( ) const
inline

Returns a raw pointer to the unified memory managed by this instance.

Warning
Note that the pointer can be NULL!

◆ data() [2/3]

void * data ( ptrdiff_t byteOffset) const
inline

Returns a byte offset void pointer from the unified memory.

Parameters
byteOffsetNumber of bytes to skip (or offset) the return pointer
Warning
assumes that this instance is not empty!

◆ data() [3/3]

template<typename T>
T * data ( ptrdiff_t count = 0) const
inline

Returns an offset pointer of a specific type from the allocated unified memory.

Template Parameters
TType of the pointer returned
Parameters
countNumbers of elements of parameter type T to skip (or offset) the return pointer
Warning
assumes that this instance is not empty!

◆ deviceData() [1/2]

void * deviceData ( ) const
inline

Legacy.

Returns

◆ deviceData() [2/2]

void * deviceData ( int ) const
inline

◆ deviceDownload() [1/3]

void deviceDownload ( cudaStream_t stream = 0,
bool sync = false ) const
inline

Prefetches all data to the host.

Parameters
streamcuda stream
syncif false the memory copy is asynchronous

◆ deviceDownload() [2/3]

void deviceDownload ( int dummmy,
void * stream,
bool sync ) const
inline

◆ deviceDownload() [3/3]

void deviceDownload ( void * stream,
bool sync ) const
inline

Legacy.

Parameters
stream
sync

◆ deviceUpload() [1/3]

void deviceUpload ( int device,
void * stream,
bool sync ) const
inline

◆ deviceUpload() [2/3]

void deviceUpload ( int device = 0,
cudaStream_t stream = cudaStreamPerThread,
bool sync = false ) const
inline

Prefetches all data to the specified device.

Parameters
devicedevice ID, cudaCpuDeviceId = -1, 0, 1, ...
streamcuda stream
syncif false the memory copy is asynchronous
Note
Legacy method included for compatibility with DeviceBuffer

◆ deviceUpload() [3/3]

void deviceUpload ( void * stream,
bool sync ) const
inline

Prefetches all data to the current device, as given by cudaGetDevice.

Parameters
streamcuda stream
syncif false the memory copy is asynchronous
Note
Legacy method included for compatibility with DeviceBuffer

◆ empty()

bool empty ( ) const
inline

Returns true if this allocator is empty, i.e. has no allocated memory.

◆ init()

void init ( size_t size,
size_t capacity )
inline

initialize buffer as a new with the specified size and capacity

Parameters
sizesize of memory block to be used in bytes
capacitysize of page table in bytes

◆ isEmpty()

bool isEmpty ( ) const
inline

◆ operator=() [1/2]

DualUnifiedBuffer & operator= ( const DualUnifiedBuffer & )
delete

Disallow copy assignment operation.

◆ operator=() [2/2]

DualUnifiedBuffer & operator= ( DualUnifiedBuffer && other)
inline

Allow move assignment operation.

◆ prefetch()

void prefetch ( ptrdiff_t byteOffset = 0,
size_t size = 0,
int dev = cudaCpuDeviceId,
cudaStream_t stream = cudaStreamPerThread ) const
inline

Prefetches data to the specified device, i.e. ensure the device has an up-to-date copy of the memory specified.

Parameters
byteOffsetoffset in bytes marking the beginning of the memory block to be prefetched
sizesize in bytes of the memory block to be prefetched. The default value of zero means copy all this->size() bytes.
devthe device ID to prefetch to, cudaCpuDeviceId = -1, 0, 1, ...
streamcuda stream

◆ resize()

void resize ( size_t size,
int dev = cudaCpuDeviceId,
std::initializer_list< cudaMemoryAdvise > list = {cudaMemAdviseSetPreferredLocation} )
inline

Resize the memory block managed by this buffer. If the current capacity is larger than the new size this method simply redefines size. Otherwise a new page-table is defined, with the specified advice, and the old block is copied to the new block.

Parameters
sizesize of the new memory block
devthe device ID on which to apply each advice provided in list, cudaCpuDeviceId = -1, 0, 1, ...
listadvices to be applied to the resized range

◆ size()

size_t size ( ) const
inline

Size of the allocated pages in this instance.

Returns
number bytes allocated by this instance