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 <cuda/memory_resource>
17 
18 #include <rmm/cuda_stream_view.hpp>
19 #include <rmm/device_buffer.hpp>
20 #include <rmm/resource_ref.hpp>
21 
22 #include <rapidsmpf/error.hpp>
23 #include <rapidsmpf/memory/host_memory_resource.hpp>
24 
25 namespace rapidsmpf {
26 
27 
31 class HostBuffer {
32  public:
47  std::size_t size,
49  cuda::mr::any_resource<cuda::mr::host_accessible> mr
50  );
51 
52  ~HostBuffer() noexcept;
53 
54  // Non copyable
55  HostBuffer(HostBuffer const&) = delete;
56  HostBuffer& operator=(HostBuffer const&) = delete;
57 
66  HostBuffer(HostBuffer&& other) noexcept;
67 
79  HostBuffer& operator=(HostBuffer&& other);
80 
87  void deallocate_async() noexcept;
88 
94  [[nodiscard]] rmm::cuda_stream_view stream() const noexcept;
95 
101  [[nodiscard]] std::size_t size() const noexcept;
102 
108  [[nodiscard]] bool empty() const noexcept;
109 
115  [[nodiscard]] std::byte* data() noexcept;
116 
122  [[nodiscard]] std::byte const* data() const noexcept;
123 
132  [[nodiscard]] std::vector<std::uint8_t> copy_to_uint8_vector() const;
133 
145  void set_stream(rmm::cuda_stream_view stream);
146 
160  std::vector<std::uint8_t> const& data,
161  rmm::cuda_stream_view stream,
163  );
164 
178  std::vector<std::uint8_t>&& data, rmm::cuda_stream_view stream
179  );
180 
205  std::unique_ptr<rmm::device_buffer> pinned_host_buffer,
206  rmm::cuda_stream_view stream
207  );
208 
209  private:
219  HostBuffer(
220  std::span<std::byte> span,
221  rmm::cuda_stream_view stream,
222  std::function<void(rmm::cuda_stream_view)> deallocate_fn
223  );
224 
225  rmm::cuda_stream_view stream_;
226  std::span<std::byte> span_{};
229  std::function<void(rmm::cuda_stream_view)> deallocate_fn_{};
230 };
231 
232 } // namespace rapidsmpf
Block of host memory.
Definition: host_buffer.hpp:31
rmm::cuda_stream_view stream() const noexcept
Get the CUDA stream associated with this buffer.
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.
static HostBuffer from_owned_vector(std::vector< std::uint8_t > &&data, rmm::cuda_stream_view stream)
Construct a HostBuffer by taking ownership of a std::vector<std::uint8_t>.
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.
HostBuffer(std::size_t size, rmm::cuda_stream_view stream, cuda::mr::any_resource< cuda::mr::host_accessible > mr)
Allocate a new host buffer.
bool empty() const noexcept
Check whether the buffer is empty.
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>.
static HostBuffer from_rmm_device_buffer(std::unique_ptr< rmm::device_buffer > pinned_host_buffer, rmm::cuda_stream_view stream)
Construct a HostBuffer by taking ownership of an rmm::device_buffer.
void set_stream(rmm::cuda_stream_view stream)
Set the associated CUDA stream.
cuda::mr::resource_ref< cuda::mr::host_accessible > host_async_resource_ref
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:14