Public Types | Public Member Functions | Public Attributes | Static Public Attributes | Friends | List of all members
rapidsmpf::Buffer Class Reference

Buffer representing device or host memory. More...

#include <buffer.hpp>

Public Types

using DeviceBufferT = std::unique_ptr< rmm::device_buffer >
 Storage type for a device buffer.
 
using HostBufferT = std::unique_ptr< HostBuffer >
 Storage type for a host buffer.
 

Public Member Functions

std::byte const * data () const
 Access the underlying memory buffer (host or device memory). More...
 
template<typename F >
auto write_access (F &&f) -> std::invoke_result_t< F, std::byte *, rmm::cuda_stream_view >
 Provides stream-ordered write access to the buffer. More...
 
std::byte * exclusive_data_access ()
 Acquire non-stream-ordered exclusive access to the buffer's memory. More...
 
void unlock ()
 Release the exclusive lock acquired by exclusive_data_access().
 
constexpr MemoryType mem_type () const
 Get the memory type of the buffer. More...
 
constexpr rmm::cuda_stream_view stream () const noexcept
 Get the associated CUDA stream. More...
 
bool is_latest_write_done () const
 Check whether the buffer's most recent write has completed. More...
 
 Buffer (Buffer &&)=delete
 Delete move and copy constructors and assignment operators.
 
 Buffer (Buffer const &)=delete
 
Bufferoperator= (Buffer &o)=delete
 
Bufferoperator= (Buffer &&o)=delete
 

Public Attributes

std::size_t const size
 The size of the buffer in bytes.
 

Static Public Attributes

static constexpr std::array< MemoryType, 1 > device_buffer_types {MemoryType::DEVICE}
 Memory types suitable for constructing a device backed buffer. More...
 
static constexpr std::array< MemoryType, 2 > host_buffer_types
 Memory types suitable for constructing a host backed buffer. More...
 

Friends

class BufferResource
 

Detailed Description

Buffer representing device or host memory.

A Buffer holds either device memory or host memory, determined by its memory type at construction. See device_buffer_types and host_buffer_types for the sets of memory types that result in device-backed and host-backed storage.

Buffers are stream ordered and have an associated CUDA stream (see stream()). All work that reads or writes the buffer must either be enqueued on that stream or be synchronized with it before accessing the memory. For example, when passing the buffer to a non-stream aware API (e.g., MPI or host-only code), the caller must ensure that the most recent write has completed before the hand off. This can be done by synchronizing the buffer's stream or by checking is_latest_write_done().

To obtain an rmm::device_buffer from a Buffer, first ensure that the buffer's memory type is one of the types listed in device_buffer_types (moving the buffer if necessary), then call release_device_buffer().

Note
The constructors are private. Buffers are created through BufferResource.

Definition at line 46 of file buffer.hpp.

Member Function Documentation

◆ data()

std::byte const* rapidsmpf::Buffer::data ( ) const

Access the underlying memory buffer (host or device memory).

Returns
A const pointer to the underlying host or device memory.
Exceptions
std::logic_errorif the buffer does not manage any memory.
std::logic_errorIf the buffer is locked.

◆ exclusive_data_access()

std::byte* rapidsmpf::Buffer::exclusive_data_access ( )

Acquire non-stream-ordered exclusive access to the buffer's memory.

Alternative to write_access(). Acquires an internal exclusive lock so that any other access through the Buffer API (including write_access()) will fail with std::logic_error while the lock is held. The lock remains held until unlock() is called. This lock is not a concurrency mechanism; it only prevents accidental access to the Buffer through the rest of the Buffer API while locked.

Use this when integrating with non-stream-aware consumer APIs that require a raw pointer and cannot be expressed as work on a CUDA stream (e.g., MPI, blocking host I/O).

Note
Prefer write_access(...) if you can express the operation as a single callable on a stream, even if that requires manually synchronizing the stream before the callable returns.
Returns
Pointer to the underlying storage.
Exceptions
std::logic_errorIf the buffer is already locked.
std::logic_errorIf is_latest_write_done() != true.
See also
write_access(), is_locked(), unlock()

◆ is_latest_write_done()

bool rapidsmpf::Buffer::is_latest_write_done ( ) const

Check whether the buffer's most recent write has completed.

Returns whether the CUDA event that tracks the most recent write into this buffer has been signaled.

Use this to guard non-stream-ordered consumer-APIs that do not accept a CUDA stream (e.g., MPI sends/receives, host-side reads).

Note
This is a non-blocking, point-in-time status check and is subject to TOCTOU races: another thread may enqueue additional writes after this returns true. Ensure no further writes are enqueued, or establish stronger synchronization (e.g., synchronize the buffer's stream) before using the buffer.
Returns
true if the last recorded write event has completed; false otherwise.
Exceptions
std::logic_errorIf the buffer is locked.
// Example: send the buffer via MPI (non-stream-ordered).
if (buffer.is_latest_write_done()) {
MPI_Isend(buffer.data(), buffer.size(), MPI_BYTE, dst, tag, comm, &req);
} else {
// Ensure completion before handing to MPI.
buffer.stream().synchronize();
MPI_Isend(buffer.data(), buffer.size(), MPI_BYTE, dst, tag, comm, &req);
}

◆ mem_type()

constexpr MemoryType rapidsmpf::Buffer::mem_type ( ) const
inlineconstexpr

Get the memory type of the buffer.

Returns
The memory type of the buffer.
Exceptions
std::logic_errorif the buffer is not initialized.

Definition at line 188 of file buffer.hpp.

◆ stream()

constexpr rmm::cuda_stream_view rapidsmpf::Buffer::stream ( ) const
inlineconstexprnoexcept

Get the associated CUDA stream.

All operations must either use this stream or synchronize with it before accessing the underlying data (both host and device memory).

Returns
The associated CUDA stream.

Definition at line 200 of file buffer.hpp.

◆ write_access()

template<typename F >
auto rapidsmpf::Buffer::write_access ( F &&  f) -> std::invoke_result_t<F, std::byte*, rmm::cuda_stream_view>
inline

Provides stream-ordered write access to the buffer.

Calls f with a pointer to the buffer's memory and the buffer's stream (i.e., this->stream()).

The callable must be invocable as:

  • R(std::byte*, rmm::cuda_stream_view).

All work performed by f must be stream-ordered on the buffer's stream. Enqueuing work on any other stream without synchronizing with the buffer's stream before and after the call is undefined behavior. In other words, f must behave as a single stream-ordered operation, similar to issuing one cudaMemcpyAsync on the buffer's stream. For non-stream-aware integrations, use exclusive_data_access().

After f returns, an event is recorded on the buffer's stream, establishing the new "latest write" for this buffer.

Warning
The pointer is valid only for the duration of the call. Using it outside of f is undefined behavior.
Template Parameters
FCallable type.
Parameters
fCallable that accepts (std::byte*, rmm::cuda_stream_view).
Returns
Whatever f returns (void if none).
Exceptions
std::logic_errorIf the buffer is locked.
// Snippet: copy data from `src_ptr` into `buffer` on the buffer's stream.
buffer.write_access([&](std::byte* buffer_ptr, rmm::cuda_stream_view stream) {
assert(buffer.stream().value() == stream.value());
RAPIDSMPF_CUDA_TRY_ALLOC(cudaMemcpyAsync(
buffer_ptr,
src_ptr,
num_bytes,
cudaMemcpyDefault,
));
});
constexpr rmm::cuda_stream_view stream() const noexcept
Get the associated CUDA stream.
Definition: buffer.hpp:200
cudaStream_t value() const noexcept

Definition at line 129 of file buffer.hpp.

Member Data Documentation

◆ device_buffer_types

constexpr std::array<MemoryType, 1> rapidsmpf::Buffer::device_buffer_types {MemoryType::DEVICE}
staticconstexpr

Memory types suitable for constructing a device backed buffer.

A buffer may use DeviceBufferT only if its memory type is listed here. This ensures that the buffer is backed by memory that behaves as device accessible memory.

Definition at line 63 of file buffer.hpp.

◆ host_buffer_types

constexpr std::array<MemoryType, 2> rapidsmpf::Buffer::host_buffer_types
staticconstexpr
Initial value:
{
MemoryType::HOST, MemoryType::PINNED_HOST
}

Memory types suitable for constructing a host backed buffer.

A buffer may use HostBufferT only if its memory type is listed here. This ensures that the buffer is backed by memory that behaves as host accessible memory.

Definition at line 72 of file buffer.hpp.


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