15 #include <cuda_runtime.h>
20 #include <rapidsmpf/cuda_event.hpp>
21 #include <rapidsmpf/error.hpp>
22 #include <rapidsmpf/utils.hpp>
27 enum class MemoryType : int {
33 constexpr MemoryType LowestSpillType = MemoryType::HOST;
37 constexpr std::array<MemoryType, 2> MEMORY_TYPES{{MemoryType::DEVICE, MemoryType::HOST}};
66 using StorageT = std::variant<DeviceStorageT, HostStorageT>;
96 [[nodiscard]] std::byte
const*
data()
const;
140 template <
typename F>
142 -> std::invoke_result_t<F, std::byte*, rmm::cuda_stream_view> {
143 using Fn = std::remove_reference_t<F>;
145 std::is_invocable_v<Fn, std::byte*, rmm::cuda_stream_view>,
146 "write_access() expects callable R(std::byte*, rmm::cuda_stream_view)"
148 using R = std::invoke_result_t<Fn, std::byte*, rmm::cuda_stream_view>;
150 auto* ptr =
const_cast<std::byte*
>(
data());
151 if constexpr (std::is_void_v<R>) {
152 std::invoke(std::forward<F>(f), ptr, stream_);
153 latest_write_event_.
record(stream_);
155 auto ret = std::invoke(std::forward<F>(f), ptr, stream_);
156 latest_write_event_.
record(stream_);
200 [[nodiscard]] MemoryType constexpr
mem_type()
const {
203 [](
HostStorageT const&) -> MemoryType {
return MemoryType::HOST; },
204 [](
DeviceStorageT const&) -> MemoryType {
return MemoryType::DEVICE; }
298 Buffer(std::unique_ptr<rmm::device_buffer> device_buffer);
305 void throw_if_locked()
const;
356 std::atomic_bool lock_;
376 std::ptrdiff_t dst_offset = 0,
377 std::ptrdiff_t src_offset = 0
Class managing buffer resources.
Buffer representing device or host memory.
std::unique_ptr< rmm::device_buffer > DeviceStorageT
Storage type for the device buffer.
auto write_access(F &&f) -> std::invoke_result_t< F, std::byte *, rmm::cuda_stream_view >
Provides stream-ordered write access to the buffer.
std::unique_ptr< std::vector< uint8_t > > HostStorageT
Storage type for the host buffer.
HostStorageT const & host() const
Access the underlying host memory buffer (const).
std::byte * exclusive_data_access()
Acquire non-stream-ordered exclusive access to the buffer's memory.
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.
Buffer(Buffer &&)=delete
Delete move and copy constructors and assignment operators.
std::variant< DeviceStorageT, HostStorageT > StorageT
Storage type in Buffer, which could be either host or device memory.
constexpr rmm::cuda_stream_view stream() const noexcept
Get the associated CUDA stream.
DeviceStorageT const & device() const
Access the underlying device memory buffer (const).
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.
Helper for overloaded lambdas using std::visit.