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/utils.hpp>
23 
24 namespace rapidsmpf {
25 
46 class Buffer {
47  friend class BufferResource;
48 
49  public:
51  using DeviceBufferT = std::unique_ptr<rmm::device_buffer>;
52 
54  using HostBufferT = std::unique_ptr<HostBuffer>;
55 
63  static constexpr std::array<MemoryType, 1> device_buffer_types{MemoryType::DEVICE};
64 
72  static constexpr std::array<MemoryType, 2> host_buffer_types{
73  MemoryType::HOST, MemoryType::PINNED_HOST
74  };
75 
84  [[nodiscard]] std::byte const* data() const;
85 
128  template <typename F>
129  auto write_access(F&& f)
130  -> std::invoke_result_t<F, std::byte*, rmm::cuda_stream_view> {
131  using Fn = std::remove_reference_t<F>;
132  static_assert(
133  std::is_invocable_v<Fn, std::byte*, rmm::cuda_stream_view>,
134  "write_access() expects callable R(std::byte*, rmm::cuda_stream_view)"
135  );
136  using R = std::invoke_result_t<Fn, std::byte*, rmm::cuda_stream_view>;
137 
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_);
142  } else {
143  auto ret = std::invoke(std::forward<F>(f), ptr, stream_);
144  latest_write_event_.record(stream_);
145  return ret;
146  }
147  }
148 
174  std::byte* exclusive_data_access();
175 
179  void unlock();
180 
188  [[nodiscard]] MemoryType constexpr mem_type() const {
189  return mem_type_;
190  }
191 
200  [[nodiscard]] constexpr rmm::cuda_stream_view stream() const noexcept {
201  return stream_;
202  }
203 
233  [[nodiscard]] bool is_latest_write_done() const;
234 
236  Buffer(Buffer&&) = delete;
237  Buffer(Buffer const&) = delete;
238  Buffer& operator=(Buffer& o) = delete;
239  Buffer& operator=(Buffer&& o) = delete;
240 
241  private:
261  Buffer(
262  std::unique_ptr<HostBuffer> host_buffer,
264  MemoryType mem_type
265  );
266 
286  Buffer(std::unique_ptr<rmm::device_buffer> device_buffer, MemoryType mem_type);
287 
293  void throw_if_locked() const;
294 
303  [[nodiscard]] DeviceBufferT release_device_buffer();
304 
313  [[nodiscard]] HostBufferT release_host_buffer();
314 
315  public:
316  std::size_t const size;
317 
318  private:
319  MemoryType const mem_type_;
320  std::variant<DeviceBufferT, HostBufferT> storage_;
321  rmm::cuda_stream_view stream_;
322  CudaEvent latest_write_event_;
323  std::atomic<bool> lock_;
324 };
325 
339 void buffer_copy(
340  Buffer& dst,
341  Buffer const& src,
342  std::size_t size,
343  std::ptrdiff_t dst_offset = 0,
344  std::ptrdiff_t src_offset = 0
345 );
346 
347 } // namespace rapidsmpf
Class managing buffer resources.
Buffer representing device or host memory.
Definition: buffer.hpp:46
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:129
static constexpr std::array< MemoryType, 1 > device_buffer_types
Memory types suitable for constructing a device backed buffer.
Definition: buffer.hpp:63
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.
Definition: buffer.hpp:51
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:316
static constexpr std::array< MemoryType, 2 > host_buffer_types
Memory types suitable for constructing a host backed buffer.
Definition: buffer.hpp:72
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:54
constexpr rmm::cuda_stream_view stream() const noexcept
Get the associated CUDA stream.
Definition: buffer.hpp:200
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:188
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.