13 #include <cuda_runtime.h>
18 #include <rapidsmpf/cuda_event.hpp>
19 #include <rapidsmpf/error.hpp>
20 #include <rapidsmpf/memory/host_buffer.hpp>
21 #include <rapidsmpf/memory/memory_type.hpp>
22 #include <rapidsmpf/utils.hpp>
73 MemoryType::HOST, MemoryType::PINNED_HOST
84 [[nodiscard]] std::byte
const*
data()
const;
128 template <
typename F>
130 -> std::invoke_result_t<F, std::byte*, rmm::cuda_stream_view> {
131 using Fn = std::remove_reference_t<F>;
133 std::is_invocable_v<Fn, std::byte*, rmm::cuda_stream_view>,
134 "write_access() expects callable R(std::byte*, rmm::cuda_stream_view)"
136 using R = std::invoke_result_t<Fn, std::byte*, rmm::cuda_stream_view>;
138 auto* ptr =
const_cast<std::byte*
>(
data());
139 if constexpr (std::is_void_v<R>) {
140 std::invoke(std::forward<F>(f), ptr, stream_);
141 latest_write_event_.
record(stream_);
143 auto ret = std::invoke(std::forward<F>(f), ptr, stream_);
144 latest_write_event_.
record(stream_);
188 [[nodiscard]] MemoryType constexpr
mem_type()
const {
262 std::unique_ptr<HostBuffer> host_buffer,
286 Buffer(std::unique_ptr<rmm::device_buffer> device_buffer, MemoryType
mem_type);
293 void throw_if_locked()
const;
319 MemoryType
const mem_type_;
320 std::variant<DeviceBufferT, HostBufferT> storage_;
323 std::atomic<bool> lock_;
343 std::ptrdiff_t dst_offset = 0,
344 std::ptrdiff_t src_offset = 0
Class managing buffer resources.
Buffer representing device or host memory.
auto write_access(F &&f) -> std::invoke_result_t< F, std::byte *, rmm::cuda_stream_view >
Provides stream-ordered write access to the buffer.
static constexpr std::array< MemoryType, 1 > device_buffer_types
Memory types suitable for constructing a device backed buffer.
std::byte * exclusive_data_access()
Acquire non-stream-ordered exclusive access to the buffer's memory.
std::unique_ptr< rmm::device_buffer > DeviceBufferT
Storage type for a device buffer.
bool is_latest_write_done() const
Check whether the buffer's most recent write has completed.
std::byte const * data() const
Access the underlying memory buffer (host or device memory).
std::size_t const size
The size of the buffer in bytes.
static constexpr std::array< MemoryType, 2 > host_buffer_types
Memory types suitable for constructing a host backed buffer.
Buffer(Buffer &&)=delete
Delete move and copy constructors and assignment operators.
std::unique_ptr< HostBuffer > HostBufferT
Storage type for a host buffer.
constexpr rmm::cuda_stream_view stream() const noexcept
Get the associated CUDA stream.
void unlock()
Release the exclusive lock acquired by exclusive_data_access().
constexpr MemoryType mem_type() const
Get the memory type of the buffer.
RAII wrapper for a CUDA event with convenience methods.
void record(rmm::cuda_stream_view stream)
Record the event on a CUDA stream.