buffer_resource.hpp
1 
6 #pragma once
7 
8 #include <array>
9 #include <memory>
10 #include <mutex>
11 #include <optional>
12 #include <ranges>
13 #include <unordered_map>
14 #include <utility>
15 
16 #include <rmm/cuda_stream_pool.hpp>
17 
18 #include <rapidsmpf/error.hpp>
19 #include <rapidsmpf/memory/buffer.hpp>
20 #include <rapidsmpf/memory/host_memory_resource.hpp>
21 #include <rapidsmpf/memory/memory_reservation.hpp>
22 #include <rapidsmpf/memory/pinned_memory_resource.hpp>
23 #include <rapidsmpf/memory/spill_manager.hpp>
24 #include <rapidsmpf/rmm_resource_adaptor.hpp>
25 #include <rapidsmpf/statistics.hpp>
26 #include <rapidsmpf/utils.hpp>
27 
28 namespace rapidsmpf {
29 
41  public:
52  using MemoryAvailable = std::function<std::int64_t()>;
53 
77  std::shared_ptr<PinnedMemoryResource> pinned_mr = PinnedMemoryResource::Disabled,
78  std::unordered_map<MemoryType, MemoryAvailable> memory_available = {},
79  std::optional<Duration> periodic_spill_check = std::chrono::milliseconds{1},
80  std::shared_ptr<rmm::cuda_stream_pool> stream_pool = std::make_shared<
82  std::shared_ptr<Statistics> statistics = Statistics::disabled()
83  );
84 
85  ~BufferResource() noexcept = default;
86 
92  [[nodiscard]] rmm::device_async_resource_ref device_mr() const noexcept {
93  return device_mr_;
94  }
95 
101  [[nodiscard]] rmm::host_async_resource_ref host_mr() noexcept {
102  return host_mr_;
103  }
104 
111  RAPIDSMPF_EXPECTS(
112  pinned_mr_, "no pinned memory resource is available", std::invalid_argument
113  );
114  return *pinned_mr_;
115  }
116 
126  [[nodiscard]] MemoryAvailable const& memory_available(MemoryType mem_type) const {
127  return memory_available_.at(mem_type);
128  }
129 
136  [[nodiscard]] std::size_t memory_reserved(MemoryType mem_type) const {
137  return memory_reserved_[static_cast<std::size_t>(mem_type)];
138  }
139 
159  std::pair<MemoryReservation, std::size_t> reserve(
160  MemoryType mem_type, size_t size, bool allow_overbooking
161  );
162 
181  size_t size, bool allow_overbooking
182  );
183 
197  template <std::ranges::input_range Range>
198  requires std::convertible_to<std::ranges::range_value_t<Range>, MemoryType>
199  [[nodiscard]] MemoryReservation reserve_or_fail(size_t size, Range mem_types) {
200  // try to reserve memory from the given order
201  for (auto const& mem_type : mem_types) {
202  if (mem_type == MemoryType::PINNED_HOST
203  && pinned_mr_ == PinnedMemoryResource::Disabled)
204  {
205  // Pinned host memory is only available if the memory resource is
206  // available.
207  continue;
208  }
209  auto [res, _] = reserve(mem_type, size, false);
210  if (res.size() == size) {
211  return std::move(res);
212  }
213  }
214  RAPIDSMPF_FAIL("failed to reserve memory", std::runtime_error);
215  }
216 
226  [[nodiscard]] MemoryReservation reserve_or_fail(size_t size, MemoryType mem_type) {
227  return reserve_or_fail(size, std::ranges::single_view{mem_type});
228  }
229 
242  std::size_t release(MemoryReservation& reservation, std::size_t size);
243 
255  std::unique_ptr<Buffer> allocate(
256  std::size_t size, rmm::cuda_stream_view stream, MemoryReservation& reservation
257  );
258 
269  std::unique_ptr<Buffer> allocate(
270  rmm::cuda_stream_view stream, MemoryReservation&& reservation
271  );
272 
288  std::unique_ptr<Buffer> move(
289  std::unique_ptr<rmm::device_buffer> data, rmm::cuda_stream_view stream
290  );
291 
304  std::unique_ptr<Buffer> move(
305  std::unique_ptr<Buffer> buffer, MemoryReservation& reservation
306  );
307 
321  std::unique_ptr<rmm::device_buffer> move_to_device_buffer(
322  std::unique_ptr<Buffer> buffer, MemoryReservation& reservation
323  );
324 
338  std::unique_ptr<HostBuffer> move_to_host_buffer(
339  std::unique_ptr<Buffer> buffer, MemoryReservation& reservation
340  );
341 
350 
357 
364  std::shared_ptr<Statistics> statistics();
365 
366  private:
367  std::mutex mutex_;
369  std::shared_ptr<PinnedMemoryResource> pinned_mr_;
370  HostMemoryResource host_mr_;
371  std::unordered_map<MemoryType, MemoryAvailable> memory_available_;
372  // Zero initialized reserved counters.
373  std::array<std::size_t, MEMORY_TYPES.size()> memory_reserved_ = {};
374  std::shared_ptr<rmm::cuda_stream_pool> stream_pool_;
375  SpillManager spill_manager_;
376  std::shared_ptr<Statistics> statistics_;
377 };
378 
395  public:
404  constexpr LimitAvailableMemory(RmmResourceAdaptor const* mr, std::int64_t limit)
405  : limit{limit}, mr_{mr} {}
406 
416  std::int64_t operator()() const {
417  return limit - static_cast<std::int64_t>(mr_->current_allocated());
418  }
419 
420  public:
421  std::int64_t const limit;
422 
423  private:
424  RmmResourceAdaptor const* mr_;
425 };
426 
427 } // namespace rapidsmpf
Class managing buffer resources.
std::size_t release(MemoryReservation &reservation, std::size_t size)
Consume a portion of the reserved memory.
std::unique_ptr< Buffer > allocate(std::size_t size, rmm::cuda_stream_view stream, MemoryReservation &reservation)
Allocate a buffer of the specified memory type by the reservation.
std::shared_ptr< Statistics > statistics()
Gets a shared pointer to the statistics associated with this buffer resource.
BufferResource(rmm::device_async_resource_ref device_mr, std::shared_ptr< PinnedMemoryResource > pinned_mr=PinnedMemoryResource::Disabled, std::unordered_map< MemoryType, MemoryAvailable > memory_available={}, std::optional< Duration > periodic_spill_check=std::chrono::milliseconds{1}, std::shared_ptr< rmm::cuda_stream_pool > stream_pool=std::make_shared< rmm::cuda_stream_pool >(16, rmm::cuda_stream::flags::non_blocking), std::shared_ptr< Statistics > statistics=Statistics::disabled())
Constructs a buffer resource.
rmm::host_async_resource_ref host_mr() noexcept
Get the RMM host memory resource.
std::pair< MemoryReservation, std::size_t > reserve(MemoryType mem_type, size_t size, bool allow_overbooking)
Reserve an amount of the specified memory type.
MemoryReservation reserve_device_memory_and_spill(size_t size, bool allow_overbooking)
Reserve device memory and spill if necessary.
rmm::device_async_resource_ref device_mr() const noexcept
Get the RMM device memory resource.
rmm::host_async_resource_ref pinned_mr()
Get the RMM pinned host memory resource.
std::unique_ptr< Buffer > allocate(rmm::cuda_stream_view stream, MemoryReservation &&reservation)
Allocate a buffer consuming the entire reservation.
std::unique_ptr< Buffer > move(std::unique_ptr< Buffer > buffer, MemoryReservation &reservation)
Move a Buffer to the memory type specified by the reservation.
requires std::convertible_to< std::ranges::range_value_t< Range >, MemoryType > MemoryReservation reserve_or_fail(size_t size, Range mem_types)
Make a memory reservation or fail based on the given order of memory types.
std::size_t memory_reserved(MemoryType mem_type) const
Get the current reserved memory of the specified memory type.
std::unique_ptr< rmm::device_buffer > move_to_device_buffer(std::unique_ptr< Buffer > buffer, MemoryReservation &reservation)
Move a Buffer to a device buffer.
MemoryAvailable const & memory_available(MemoryType mem_type) const
Retrieves the memory availability function for a given memory type.
MemoryReservation reserve_or_fail(size_t size, MemoryType mem_type)
Make a memory reservation or fail.
std::function< std::int64_t()> MemoryAvailable
Callback function to determine available memory.
SpillManager & spill_manager()
Gets a reference to the spill manager used.
rmm::cuda_stream_pool const & stream_pool() const
Returns the CUDA stream pool used by this buffer resource.
std::unique_ptr< HostBuffer > move_to_host_buffer(std::unique_ptr< Buffer > buffer, MemoryReservation &reservation)
Move a Buffer into a host buffer.
std::unique_ptr< Buffer > move(std::unique_ptr< rmm::device_buffer > data, rmm::cuda_stream_view stream)
Move device buffer data into a Buffer.
Host memory resource using standard CPU allocation.
A functor for querying the remaining available memory within a defined limit from an RMM statistics r...
constexpr LimitAvailableMemory(RmmResourceAdaptor const *mr, std::int64_t limit)
Constructs a LimitAvailableMemory instance.
std::int64_t operator()() const
Returns the remaining available memory within the defined limit.
std::int64_t const limit
The memory limit.
Represents a reservation for future memory allocation.
static constexpr auto Disabled
Sentinel value used to disable pinned host memory.
A RMM memory resource adaptor tailored to RapidsMPF.
std::int64_t current_allocated() const noexcept
Get the total current allocated memory from both primary and fallback.
Manages memory spilling to free up device memory when needed.
static std::shared_ptr< Statistics > disabled()
Returns a shared pointer to a disabled (no-op) Statistics instance.
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::host_accessible > > host_async_resource_ref
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::device_accessible > > device_async_resource_ref