host_buffer.hpp
1 
5 #pragma once
6 
7 #include <cstddef>
8 #include <cstdint>
9 #include <cstdlib>
10 #include <cstring>
11 #include <functional>
12 #include <memory>
13 #include <span>
14 #include <vector>
15 
16 #include <rmm/cuda_stream_view.hpp>
17 #include <rmm/device_buffer.hpp>
18 #include <rmm/resource_ref.hpp>
19 
20 #include <rapidsmpf/error.hpp>
21 #include <rapidsmpf/memory/host_memory_resource.hpp>
22 #include <rapidsmpf/memory/pinned_memory_resource.hpp>
23 
24 namespace rapidsmpf {
25 
26 
30 class HostBuffer {
31  public:
41  using OwnedStorageDeleter = std::function<void(void*)>;
42 
55  );
56 
57  ~HostBuffer() noexcept;
58 
59  // Non copyable
60  HostBuffer(HostBuffer const&) = delete;
61  HostBuffer& operator=(HostBuffer const&) = delete;
62 
71  HostBuffer(HostBuffer&& other) noexcept;
72 
84  HostBuffer& operator=(HostBuffer&& other);
85 
92  void deallocate_async() noexcept;
93 
99  [[nodiscard]] rmm::cuda_stream_view stream() const noexcept;
100 
106  [[nodiscard]] std::size_t size() const noexcept;
107 
113  [[nodiscard]] bool empty() const noexcept;
114 
120  [[nodiscard]] std::byte* data() noexcept;
121 
127  [[nodiscard]] std::byte const* data() const noexcept;
128 
137  [[nodiscard]] std::vector<std::uint8_t> copy_to_uint8_vector() const;
138 
152  std::vector<std::uint8_t> const& data,
153  rmm::cuda_stream_view stream,
155  );
156 
171  std::vector<std::uint8_t>&& data,
172  rmm::cuda_stream_view stream,
174  );
175 
197  std::unique_ptr<rmm::device_buffer> pinned_host_buffer,
198  rmm::cuda_stream_view stream,
200  );
201 
202  private:
211  HostBuffer(
212  std::span<std::byte> span,
213  rmm::cuda_stream_view stream,
215  std::unique_ptr<void, OwnedStorageDeleter> owned_storage
216  );
217 
218  rmm::cuda_stream_view stream_;
220  std::span<std::byte> span_{};
222  std::unique_ptr<void, OwnedStorageDeleter> owned_storage_{nullptr, [](void*) {}};
223 };
224 
225 } // namespace rapidsmpf
Block of host memory.
Definition: host_buffer.hpp:30
rmm::cuda_stream_view stream() const noexcept
Get the CUDA stream associated with this buffer.
static HostBuffer from_owned_vector(std::vector< std::uint8_t > &&data, rmm::cuda_stream_view stream, rmm::host_async_resource_ref mr)
Construct a HostBuffer by taking ownership of a std::vector<std::uint8_t>.
std::size_t size() const noexcept
Get the size of the buffer in bytes.
void deallocate_async() noexcept
Stream-ordered deallocates the buffer, if allocated.
std::vector< std::uint8_t > copy_to_uint8_vector() const
Copy the contents of the buffer into a host std::vector.
std::byte * data() noexcept
Get a pointer to the buffer data.
static HostBuffer from_rmm_device_buffer(std::unique_ptr< rmm::device_buffer > pinned_host_buffer, rmm::cuda_stream_view stream, PinnedMemoryResource &mr)
Construct a HostBuffer by taking ownership of an rmm::device_buffer.
bool empty() const noexcept
Check whether the buffer is empty.
std::function< void(void *)> OwnedStorageDeleter
Type-erased deleter for owned storage.
Definition: host_buffer.hpp:41
HostBuffer(std::size_t size, rmm::cuda_stream_view stream, rmm::host_async_resource_ref mr)
Allocate a new host buffer.
static HostBuffer from_uint8_vector(std::vector< std::uint8_t > const &data, rmm::cuda_stream_view stream, rmm::host_async_resource_ref mr)
Construct a HostBuffer by copying data from a std::vector<std::uint8_t>.
Memory resource that provides pinned (page-locked) host memory using a pool.
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::host_accessible > > host_async_resource_ref