limiting_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2026, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/aligned.hpp>
8 #include <rmm/detail/error.hpp>
9 #include <rmm/detail/export.hpp>
10 #include <rmm/detail/format.hpp>
13 #include <rmm/resource_ref.hpp>
14 
15 #include <atomic>
16 #include <cstddef>
17 #include <memory>
18 
19 namespace RMM_NAMESPACE {
20 namespace mr {
38 template <typename Upstream>
40  public:
50  std::size_t allocation_limit,
51  std::size_t alignment = CUDA_ALLOCATION_ALIGNMENT)
52  : upstream_{upstream},
53  allocation_limit_{allocation_limit},
54  allocated_bytes_(0),
55  alignment_(alignment)
56  {
57  }
58 
69  limiting_resource_adaptor(Upstream* upstream,
70  std::size_t allocation_limit,
71  std::size_t alignment = CUDA_ALLOCATION_ALIGNMENT)
72  : upstream_{to_device_async_resource_ref_checked(upstream)},
73  allocation_limit_{allocation_limit},
74  allocated_bytes_(0),
75  alignment_(alignment)
76  {
77  }
78 
79  limiting_resource_adaptor() = delete;
80  ~limiting_resource_adaptor() override = default;
83  default;
84  limiting_resource_adaptor& operator=(limiting_resource_adaptor const&) = delete;
86  default;
87 
91  [[nodiscard]] device_async_resource_ref get_upstream_resource() const noexcept
92  {
93  return upstream_;
94  }
95 
105  [[nodiscard]] std::size_t get_allocated_bytes() const { return allocated_bytes_; }
106 
114  [[nodiscard]] std::size_t get_allocation_limit() const { return allocation_limit_; }
115 
116  private:
130  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
131  {
132  auto const proposed_size = align_up(bytes, alignment_);
133  auto const old = allocated_bytes_.fetch_add(proposed_size);
134  if (old + proposed_size <= allocation_limit_) {
135  try {
136  return get_upstream_resource().allocate(stream, bytes);
137  } catch (...) {
138  allocated_bytes_ -= proposed_size;
139  throw;
140  }
141  }
142 
143  allocated_bytes_ -= proposed_size;
144  auto const msg = std::string("Exceeded memory limit (failed to allocate ") +
145  rmm::detail::format_bytes(bytes) + ")";
146  RMM_FAIL(msg.c_str(), rmm::out_of_memory);
147  }
148 
156  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
157  {
158  std::size_t allocated_size = align_up(bytes, alignment_);
159  get_upstream_resource().deallocate(stream, ptr, bytes);
160  allocated_bytes_ -= allocated_size;
161  }
162 
170  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
171  {
172  if (this == std::addressof(other)) { return true; }
173  auto const* cast = dynamic_cast<limiting_resource_adaptor<Upstream> const*>(&other);
174  if (cast == nullptr) { return false; }
175  return get_upstream_resource() == cast->get_upstream_resource();
176  }
177 
178  // The upstream resource used for satisfying allocation requests
179  device_async_resource_ref upstream_;
180 
181  // maximum bytes this allocator is allowed to allocate.
182  std::size_t allocation_limit_;
183 
184  // number of currently-allocated bytes
185  std::atomic<std::size_t> allocated_bytes_;
186 
187  // todo: should be some way to ask the upstream...
188  std::size_t alignment_;
189 };
190  // end of group
192 } // namespace mr
193 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:83
Resource that uses Upstream to allocate memory and limits the total allocations possible.
Definition: limiting_resource_adaptor.hpp:39
limiting_resource_adaptor(device_async_resource_ref upstream, std::size_t allocation_limit, std::size_t alignment=CUDA_ALLOCATION_ALIGNMENT)
Construct a new limiting resource adaptor using upstream to satisfy allocation requests and limiting ...
Definition: limiting_resource_adaptor.hpp:49
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:105
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:114
limiting_resource_adaptor(Upstream *upstream, std::size_t allocation_limit, std::size_t alignment=CUDA_ALLOCATION_ALIGNMENT)
Construct a new limiting resource adaptor using upstream to satisfy allocation requests and limiting ...
Definition: limiting_resource_adaptor.hpp:69
Exception thrown when RMM runs out of memory.
Definition: error.hpp:76
device_async_resource_ref to_device_async_resource_ref_checked(Resource *res)
Convert pointer to memory resource into device_async_resource_ref, checking for nullptr
Definition: resource_ref.hpp:72
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::device_accessible > > device_async_resource_ref
Alias for a cuda::mr::async_resource_ref with the property cuda::mr::device_accessible.
Definition: resource_ref.hpp:32
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:25
std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
Align up to nearest multiple of specified power of 2.
Management of per-device device_memory_resources.