Public Member Functions | Static Public Member Functions | List of all members
rapidsmpf::HostBuffer Class Reference

Block of host memory. More...

#include <host_buffer.hpp>

Public Member Functions

 HostBuffer (std::size_t size, rmm::cuda_stream_view stream, cuda::mr::any_resource< cuda::mr::host_accessible > mr)
 Allocate a new host buffer. More...
 
 HostBuffer (HostBuffer const &)=delete
 
HostBufferoperator= (HostBuffer const &)=delete
 
 HostBuffer (HostBuffer &&other) noexcept
 Move constructor. More...
 
HostBufferoperator= (HostBuffer &&other)
 Move assignment operator. More...
 
void deallocate_async () noexcept
 Stream-ordered deallocates the buffer, if allocated. More...
 
rmm::cuda_stream_view stream () const noexcept
 Get the CUDA stream associated with this buffer. More...
 
std::size_t size () const noexcept
 Get the size of the buffer in bytes. More...
 
bool empty () const noexcept
 Check whether the buffer is empty. More...
 
std::byte * data () noexcept
 Get a pointer to the buffer data. More...
 
std::byte const * data () const noexcept
 Get a const pointer to the buffer data. More...
 
std::vector< std::uint8_t > copy_to_uint8_vector () const
 Copy the contents of the buffer into a host std::vector. More...
 
void set_stream (rmm::cuda_stream_view stream)
 Set the associated CUDA stream. More...
 

Static Public Member Functions

static HostBuffer from_uint8_vector (std::vector< std::uint8_t > const &data, rmm::cuda_stream_view stream, rmm::host_async_resource_ref mr)
 Construct a HostBuffer by copying data from a std::vector<std::uint8_t>. More...
 
static HostBuffer from_owned_vector (std::vector< std::uint8_t > &&data, rmm::cuda_stream_view stream)
 Construct a HostBuffer by taking ownership of a std::vector<std::uint8_t>. More...
 
static HostBuffer from_rmm_device_buffer (std::unique_ptr< rmm::device_buffer > pinned_host_buffer, rmm::cuda_stream_view stream)
 Construct a HostBuffer by taking ownership of an rmm::device_buffer. More...
 

Detailed Description

Block of host memory.

Definition at line 31 of file host_buffer.hpp.

Constructor & Destructor Documentation

◆ HostBuffer() [1/2]

rapidsmpf::HostBuffer::HostBuffer ( std::size_t  size,
rmm::cuda_stream_view  stream,
cuda::mr::any_resource< cuda::mr::host_accessible >  mr 
)

Allocate a new host buffer.

If size is greater than zero, memory is allocated using the provided memory resource and stream. If size is zero, the buffer is created empty.

Parameters
sizeNumber of bytes to allocate.
streamCUDA stream on which allocation and deallocation occur.
mrHost-accessible memory resource used for allocation. Taken by value so the buffer shares ownership of the resource (e.g. bumps the refcount when constructed from a shared-ownership resource); an implicit conversion from rmm::host_async_resource_ref is also supported.

◆ HostBuffer() [2/2]

rapidsmpf::HostBuffer::HostBuffer ( HostBuffer &&  other)
noexcept

Move constructor.

Transfers ownership of the underlying memory. The moved-from object becomes empty.

Parameters
otherThe buffer to move from.

Member Function Documentation

◆ copy_to_uint8_vector()

std::vector<std::uint8_t> rapidsmpf::HostBuffer::copy_to_uint8_vector ( ) const

Copy the contents of the buffer into a host std::vector.

This is primarily intended for debugging or testing. It performs a stream synchronization before and after the copy.

Returns
A vector containing the copied bytes.

◆ data() [1/2]

std::byte const* rapidsmpf::HostBuffer::data ( ) const
noexcept

Get a const pointer to the buffer data.

Returns
Const pointer to the underlying memory.

◆ data() [2/2]

std::byte* rapidsmpf::HostBuffer::data ( )
noexcept

Get a pointer to the buffer data.

Returns
Pointer to the underlying memory.

◆ deallocate_async()

void rapidsmpf::HostBuffer::deallocate_async ( )
noexcept

Stream-ordered deallocates the buffer, if allocated.

After deallocation the buffer becomes empty. It is safe to call this method multiple times.

◆ empty()

bool rapidsmpf::HostBuffer::empty ( ) const
noexcept

Check whether the buffer is empty.

Returns
True if no memory is allocated.

◆ from_owned_vector()

static HostBuffer rapidsmpf::HostBuffer::from_owned_vector ( std::vector< std::uint8_t > &&  data,
rmm::cuda_stream_view  stream 
)
static

Construct a HostBuffer by taking ownership of a std::vector<std::uint8_t>.

The buffer takes ownership of the vector's memory. The vector is moved into internal storage and will be destroyed when the HostBuffer is destroyed.

Parameters
dataVector to take ownership of (will be moved).
streamCUDA stream to associate with this buffer.
Returns
A new HostBuffer owning the vector's memory.

◆ from_rmm_device_buffer()

static HostBuffer rapidsmpf::HostBuffer::from_rmm_device_buffer ( std::unique_ptr< rmm::device_buffer pinned_host_buffer,
rmm::cuda_stream_view  stream 
)
static

Construct a HostBuffer by taking ownership of an rmm::device_buffer.

The buffer takes ownership of the device buffer. The caller must ensure that the device buffer contains host-accessible memory (e.g., pinned host memory allocated via a managed or pinned memory resource).

Warning
The caller is responsible for ensuring the device buffer's memory is host-accessible. Using this with non-host-accessible device memory will result in a std::invalid_argument exception.
Parameters
pinned_host_bufferDevice buffer to take ownership of.
streamCUDA stream to associate with this buffer.
Returns
A new HostBuffer owning the device buffer's memory.
Exceptions
std::invalid_argumentif pinned_host_buffer is null.
std::logic_errorif pinned_host_buffer is not of pinned memory host memory type (see warning for details).
Warning
The caller is responsible to ensure pinned_host_buffer is of pinned host memory type. If non-pinned host buffer leads to an irrecoverable condition.

◆ from_uint8_vector()

static HostBuffer rapidsmpf::HostBuffer::from_uint8_vector ( std::vector< std::uint8_t > const &  data,
rmm::cuda_stream_view  stream,
rmm::host_async_resource_ref  mr 
)
static

Construct a HostBuffer by copying data from a std::vector<std::uint8_t>.

A new buffer is allocated using the provided memory resource and stream. This helper is intended for tests and small debug utilities.

Parameters
dataSource vector containing the bytes to copy.
streamCUDA stream used for allocation and copy.
mrHost memory resource used to allocate the buffer.
Returns
A new HostBuffer containing a copy of data.

◆ operator=()

HostBuffer& rapidsmpf::HostBuffer::operator= ( HostBuffer &&  other)

Move assignment operator.

Transfers ownership of the underlying memory. The current buffer must be empty before assignment.

Parameters
otherThe buffer to move from.
Returns
Reference to this object.
Exceptions
std::invalid_argumentif this buffer is already initialized.

◆ set_stream()

void rapidsmpf::HostBuffer::set_stream ( rmm::cuda_stream_view  stream)

Set the associated CUDA stream.

This function only updates the buffer's associated CUDA stream. It does not perform any stream synchronization or establish ordering guarantees between the previous stream and stream. Callers must ensure that any work previously enqueued on the old stream that may access the buffer has completed or is otherwise properly synchronized before switching streams.

Parameters
streamThe new CUDA stream.

◆ size()

std::size_t rapidsmpf::HostBuffer::size ( ) const
noexcept

Get the size of the buffer in bytes.

Returns
Number of bytes in the buffer.

◆ stream()

rmm::cuda_stream_view rapidsmpf::HostBuffer::stream ( ) const
noexcept

Get the CUDA stream associated with this buffer.

Returns
CUDA stream view.

The documentation for this class was generated from the following file: