buffer.hpp
1 
5 #pragma once
6 
7 #include <atomic>
8 #include <cstddef>
9 #include <functional>
10 #include <memory>
11 #include <variant>
12 
13 #include <cuda_runtime.h>
14 
15 #include <rmm/cuda_stream_view.hpp>
16 #include <rmm/device_buffer.hpp>
17 
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/statistics.hpp>
23 #include <rapidsmpf/utils/misc.hpp>
24 
25 namespace rapidsmpf {
26 
47 class Buffer {
48  friend class BufferResource;
49 
50  public:
52  using DeviceBufferT = std::unique_ptr<rmm::device_buffer>;
53 
55  using HostBufferT = std::unique_ptr<HostBuffer>;
56 
64  static constexpr std::array<MemoryType, 1> device_buffer_types{MemoryType::DEVICE};
65 
73  static constexpr std::array<MemoryType, 2> host_buffer_types{
75  };
76 
85  [[nodiscard]] std::byte const* data() const;
86 
129  template <typename F>
130  auto write_access(F&& f)
131  -> std::invoke_result_t<F, std::byte*, rmm::cuda_stream_view> {
132  using Fn = std::remove_reference_t<F>;
133  static_assert(
134  std::is_invocable_v<Fn, std::byte*, rmm::cuda_stream_view>,
135  "write_access() expects callable R(std::byte*, rmm::cuda_stream_view)"
136  );
137  using R = std::invoke_result_t<Fn, std::byte*, rmm::cuda_stream_view>;
138 
139  auto* ptr = const_cast<std::byte*>(data());
140  if constexpr (std::is_void_v<R>) {
141  std::invoke(std::forward<F>(f), ptr, stream_);
142  latest_write_event_.record(stream_);
143  } else {
144  auto ret = std::invoke(std::forward<F>(f), ptr, stream_);
145  latest_write_event_.record(stream_);
146  return ret;
147  }
148  }
149 
179  std::byte* exclusive_data_access();
180 
184  void unlock();
185 
193  [[nodiscard]] MemoryType constexpr mem_type() const {
194  return mem_type_;
195  }
196 
205  [[nodiscard]] constexpr rmm::cuda_stream_view stream() const noexcept {
206  return stream_;
207  }
208 
214  [[nodiscard]] CudaEvent const& latest_write_event() const noexcept {
215  return latest_write_event_;
216  }
217 
243 
278  [[nodiscard]] bool is_latest_write_done() const;
279 
281  Buffer(Buffer&&) = delete;
282  Buffer(Buffer const&) = delete;
283  Buffer& operator=(Buffer& o) = delete;
284  Buffer& operator=(Buffer&& o) = delete;
285 
286  private:
309  Buffer(
310  std::unique_ptr<HostBuffer> host_buffer,
313  );
314 
337  Buffer(std::unique_ptr<rmm::device_buffer> device_buffer, MemoryType mem_type);
338 
344  void throw_if_locked() const;
345 
354  [[nodiscard]] DeviceBufferT release_device_buffer();
355 
364  [[nodiscard]] HostBufferT release_host_buffer();
365 
366  public:
367  std::size_t const size;
368 
369  private:
370  MemoryType const mem_type_;
371  std::variant<DeviceBufferT, HostBufferT> storage_;
372  rmm::cuda_stream_view stream_;
373  CudaEvent latest_write_event_;
374  std::atomic<bool> lock_;
375 };
376 
394  std::shared_ptr<Statistics> statistics,
395  Buffer& dst,
396  Buffer const& src,
397  std::size_t size,
398  std::ptrdiff_t dst_offset = 0,
399  std::ptrdiff_t src_offset = 0
400 );
401 
402 } // namespace rapidsmpf
Class managing buffer resources.
Buffer representing device or host memory.
Definition: buffer.hpp:47
auto write_access(F &&f) -> std::invoke_result_t< F, std::byte *, rmm::cuda_stream_view >
Provides stream-ordered write access to the buffer.
Definition: buffer.hpp:130
static constexpr std::array< MemoryType, 1 > device_buffer_types
Memory types suitable for constructing a device backed buffer.
Definition: buffer.hpp:64
CudaEvent const & latest_write_event() const noexcept
Get the CUDA event that tracks the latest write into the buffer.
Definition: buffer.hpp:214
std::byte * exclusive_data_access()
Acquire non-stream-ordered exclusive access to the buffer's memory.
void rebind_stream(rmm::cuda_stream_view new_stream)
Rebind the buffer to a new CUDA stream.
std::unique_ptr< rmm::device_buffer > DeviceBufferT
Storage type for a device buffer.
Definition: buffer.hpp:52
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.
Definition: buffer.hpp:367
static constexpr std::array< MemoryType, 2 > host_buffer_types
Memory types suitable for constructing a host backed buffer.
Definition: buffer.hpp:73
Buffer(Buffer &&)=delete
Delete move and copy constructors and assignment operators.
std::unique_ptr< HostBuffer > HostBufferT
Storage type for a host buffer.
Definition: buffer.hpp:55
constexpr rmm::cuda_stream_view stream() const noexcept
Get the associated CUDA stream.
Definition: buffer.hpp:205
void unlock()
Release the exclusive lock acquired by exclusive_data_access().
constexpr MemoryType mem_type() const
Get the memory type of the buffer.
Definition: buffer.hpp:193
RAII wrapper for a CUDA event with convenience methods.
Definition: cuda_event.hpp:35
void record(rmm::cuda_stream_view stream)
Record the event on a CUDA stream.
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:13
MemoryType
Enum representing the type of memory sorted in decreasing order of preference.
Definition: memory_type.hpp:16
@ PINNED_HOST
Pinned host memory.
@ HOST
Host memory.
@ DEVICE
Device memory.
void buffer_copy(std::shared_ptr< Statistics > statistics, Buffer &dst, Buffer const &src, std::size_t size, std::ptrdiff_t dst_offset=0, std::ptrdiff_t src_offset=0)
Asynchronously copy data between buffers.