13 #include <unordered_map>
18 #include <rapidsmpf/buffer/buffer.hpp>
19 #include <rapidsmpf/buffer/spill_manager.hpp>
20 #include <rapidsmpf/error.hpp>
21 #include <rapidsmpf/rmm_resource_adaptor.hpp>
22 #include <rapidsmpf/statistics.hpp>
23 #include <rapidsmpf/utils.hpp>
56 o.mem_type_, std::exchange(o.br_,
nullptr), std::exchange(o.size_, 0)
67 mem_type_ = o.mem_type_;
68 br_ = std::exchange(o.br_,
nullptr);
69 size_ = std::exchange(o.size_, 0);
82 [[nodiscard]] constexpr std::size_t
size() const noexcept {
91 [[nodiscard]] constexpr MemoryType
mem_type() const noexcept {
118 MemoryType mem_type_;
166 std::optional<Duration> periodic_spill_check = std::chrono::milliseconds{1},
167 std::shared_ptr<rmm::cuda_stream_pool>
stream_pool = std::make_shared<
193 return memory_available_.at(mem_type);
203 return memory_reserved_[
static_cast<std::size_t
>(mem_type)];
213 return memory_reserved_[
static_cast<std::size_t
>(mem_type)];
235 std::pair<MemoryReservation, std::size_t>
reserve(
236 MemoryType mem_type,
size_t size,
bool allow_overbooking
252 MemoryType mem_type,
size_t size,
bool allow_overbooking
265 template <std::ranges::input_range Range>
266 requires std::convertible_to<std::ranges::range_value_t<Range>, MemoryType>
269 for (
auto const& mem_type : mem_types) {
270 auto [res, _] =
reserve(mem_type, size,
false);
271 if (res.size() == size) {
272 return std::move(res);
275 RAPIDSMPF_FAIL(
"failed to reserve memory", std::runtime_error);
429 std::unordered_map<MemoryType, MemoryAvailable> memory_available_;
431 std::array<std::size_t, MEMORY_TYPES.size()> memory_reserved_ = {};
432 std::shared_ptr<rmm::cuda_stream_pool> stream_pool_;
434 std::shared_ptr<Statistics> statistics_;
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::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.
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_and_spill(MemoryType mem_type, size_t size, bool allow_overbooking)
Reserve memory and spill if necessary.
rmm::device_async_resource_ref device_mr() const noexcept
Get the RMM device 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.
std::size_t & memory_reserved(MemoryType mem_type)
Get a reference to the current reserved memory of the specified memory type.
rmm::cuda_stream_pool const & stream_pool() const
Returns the CUDA stream pool used by this buffer resource.
std::unique_ptr< std::vector< uint8_t > > move_to_host_vector(std::unique_ptr< Buffer > buffer, MemoryReservation &reservation)
Move a Buffer into a host vector.
std::unique_ptr< Buffer > move(std::unique_ptr< rmm::device_buffer > data, rmm::cuda_stream_view stream)
Move device buffer data into a Buffer.
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.
constexpr BufferResource * br() const noexcept
Get the buffer resource associated with this reservation.
~MemoryReservation() noexcept
Destructor for the memory reservation.
constexpr std::size_t size() const noexcept
Get the remaining size of the reserved memory.
MemoryReservation & operator=(MemoryReservation &&o) noexcept
Move assignment operator for MemoryReservation.
MemoryReservation(MemoryReservation &&o)
Move constructor for MemoryReservation.
void clear() noexcept
Clear the remaining size of the reservation.
constexpr MemoryType mem_type() const noexcept
Get the type of memory associated with this reservation.
MemoryReservation(MemoryReservation const &)=delete
A memory reservation is not copyable.
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::device_accessible > > device_async_resource_ref