18 #include <rmm/detail/aligned.hpp>
19 #include <rmm/detail/error.hpp>
42 template <
typename Upstream>
56 std::size_t allocation_limit,
57 std::size_t alignment = rmm::detail::CUDA_ALLOCATION_ALIGNMENT)
58 : allocation_limit_{allocation_limit},
60 alignment_(alignment),
63 RMM_EXPECTS(
nullptr != upstream,
"Unexpected null upstream resource pointer.");
78 [[nodiscard]] Upstream*
get_upstream() const noexcept {
return upstream_; }
88 return upstream_->supports_streams();
98 return upstream_->supports_get_mem_info();
137 auto const proposed_size = rmm::detail::align_up(bytes, alignment_);
138 auto const old = allocated_bytes_.fetch_add(proposed_size);
139 if (old + proposed_size <= allocation_limit_) {
141 return upstream_->allocate(bytes, stream);
143 allocated_bytes_ -= proposed_size;
148 allocated_bytes_ -= proposed_size;
159 void do_deallocate(
void* ptr, std::size_t bytes, cuda_stream_view stream)
override
161 std::size_t allocated_size = rmm::detail::align_up(bytes, alignment_);
162 upstream_->deallocate(ptr, bytes, stream);
163 allocated_bytes_ -= allocated_size;
173 [[nodiscard]]
bool do_is_equal(device_memory_resource
const& other)
const noexcept
override
175 if (
this == &other) {
return true; }
176 auto const* cast =
dynamic_cast<limiting_resource_adaptor<Upstream> const*
>(&other);
177 if (cast !=
nullptr) {
return upstream_->is_equal(*cast->get_upstream()); }
178 return upstream_->is_equal(other);
189 [[nodiscard]] std::pair<std::size_t, std::size_t> do_get_mem_info(
190 [[maybe_unused]] cuda_stream_view stream)
const override
192 return {allocation_limit_ - allocated_bytes_, allocation_limit_};
196 std::size_t allocation_limit_;
199 std::atomic<std::size_t> allocated_bytes_;
202 std::size_t alignment_;
217 template <
typename Upstream>
219 std::size_t allocation_limit)
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:89
Resource that uses Upstream to allocate memory and limits the total allocations possible.
Definition: limiting_resource_adaptor.hpp:43
limiting_resource_adaptor(limiting_resource_adaptor &&) noexcept=default
Default move constructor.
std::size_t get_allocated_bytes() const
Query the number of bytes that have been allocated. Note that this can not be used to know how large ...
Definition: limiting_resource_adaptor.hpp:110
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: limiting_resource_adaptor.hpp:96
bool supports_streams() const noexcept override
Checks whether the upstream resource supports streams.
Definition: limiting_resource_adaptor.hpp:86
limiting_resource_adaptor(Upstream *upstream, std::size_t allocation_limit, std::size_t alignment=rmm::detail::CUDA_ALLOCATION_ALIGNMENT)
Construct a new limiting resource adaptor using upstream to satisfy allocation requests and limiting ...
Definition: limiting_resource_adaptor.hpp:55
std::size_t get_allocation_limit() const
Query the maximum number of bytes that this allocator is allowed to allocate. This is the limit on th...
Definition: limiting_resource_adaptor.hpp:119
Upstream * get_upstream() const noexcept
Pointer to the upstream resource.
Definition: limiting_resource_adaptor.hpp:78
Exception thrown when RMM runs out of memory.
Definition: error.hpp:89
limiting_resource_adaptor< Upstream > make_limiting_adaptor(Upstream *upstream, std::size_t allocation_limit)
Convenience factory to return a limiting_resource_adaptor around the upstream resource upstream.
Definition: limiting_resource_adaptor.hpp:218