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... | |
| CudaEvent const & | latest_write_event () const noexcept |
| Get the CUDA event that tracks the latest write into the buffer. More... | |
| void | rebind_stream (rmm::cuda_stream_view new_stream) |
| Rebind the buffer to a new 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 | |
| Buffer & | operator= (Buffer &o)=delete |
| Buffer & | operator= (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 |
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().
BufferResource. Definition at line 47 of file buffer.hpp.
| std::byte const* rapidsmpf::Buffer::data | ( | ) | const |
Access the underlying memory buffer (host or device memory).
| std::logic_error | if the buffer does not manage any memory. |
| std::logic_error | If the buffer is locked. |
| 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).
Buffer does not track read access to its underlying storage, and so one should be aware of write-after-read anti-dependencies when obtaining exclusive access.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.| std::logic_error | If the buffer is already locked. |
| std::logic_error | If is_latest_write_done() != true. |
| 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).
true. Ensure no further writes are enqueued, or establish stronger synchronization (e.g., synchronize the buffer's stream) before using the buffer.Buffer. Pending stream-ordered reads from the Buffer are not tracked and therefore one should be aware of write-after-read anti-dependencies when using this check to pass from stream-ordered to non-stream-ordered code.true if the last recorded write event has completed; false otherwise.| std::logic_error | If the buffer is locked. |
|
inlinenoexcept |
Get the CUDA event that tracks the latest write into the buffer.
Definition at line 214 of file buffer.hpp.
|
inlineconstexpr |
Get the memory type of the buffer.
| std::logic_error | if the buffer is not initialized. |
Definition at line 193 of file buffer.hpp.
| void rapidsmpf::Buffer::rebind_stream | ( | rmm::cuda_stream_view | new_stream | ) |
Rebind the buffer to a new CUDA stream.
Changes the buffer's associated stream to new_stream and ensures proper synchronization: new_stream will wait for any pending work on the current stream before proceeding. The underlying storage stream (e.g., the stream of an rmm::device_buffer or HostBuffer) is also updated.
| new_stream | The new CUDA stream. |
| std::logic_error | If the buffer is locked. |
|
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).
Definition at line 205 of file buffer.hpp.
|
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.
f is undefined behavior.| F | Callable type. |
| f | Callable that accepts (std::byte*, rmm::cuda_stream_view). |
f returns (void if none).| std::logic_error | If the buffer is locked. |
Definition at line 130 of file buffer.hpp.
|
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 64 of file buffer.hpp.
|
staticconstexpr |
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 73 of file buffer.hpp.