limiting_resource_adaptor.hpp
前往此文件的文档。
1 /*
2  * Copyright (c) 2021-2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://apache.ac.cn/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <rmm/aligned.hpp>
19 #include <rmm/detail/error.hpp>
20 #include <rmm/detail/export.hpp>
21 #include <rmm/detail/format.hpp>
24 #include <rmm/resource_ref.hpp>
25 
26 #include <atomic>
27 #include <cstddef>
28 
29 namespace RMM_NAMESPACE {
30 namespace mr {
48 /*!
49  * @brief Resource that uses Upstream to allocate memory and limits the total allocations possible.
50  */
59  template <typename Upstream>
61  public
62  /*!
63  * @brief Construct a new limiting resource adaptor using upstream to satisfy allocation requests and
64  * limiting total allocations by allocation_limit.
65  *
66  * @throws rmm::bad_arg if allocation_limit == 0
67  * @param upstream The upstream resource used for satisfying allocation requests.
68  * @param allocation_limit The maximum number of bytes this resource is allowed to allocate.
69  * @param alignment The alignment to use for allocations. Defaults to CUDA_ALLOCATION_ALIGNMENT.
70  */
72  std::size_t allocation_limit,
73  std::size_t alignment = CUDA_ALLOCATION_ALIGNMENT)
74  : upstream_{upstream},
75  allocation_limit_{allocation_limit},
76  allocated_bytes_(0),
77  alignment_(alignment)
78  {
79  }
80 
81  /*!
82  * @brief Construct a new limiting resource adaptor using upstream to satisfy allocation requests and
83  * limiting total allocations by allocation_limit.
84  *
85  * @throws rmm::bad_arg if allocation_limit == 0 or upstream is nullptr
86  * @param upstream The upstream resource used for satisfying allocation requests.
87  * @param allocation_limit The maximum number of bytes this resource is allowed to allocate.
88  * @param alignment The alignment to use for allocations. Defaults to CUDA_ALLOCATION_ALIGNMENT.
89  */
90  limiting_resource_adaptor(Upstream* upstream,
91  std::size_t allocation_limit,
92  std::size_t alignment = CUDA_ALLOCATION_ALIGNMENT)
93  : upstream_{to_device_async_resource_ref_checked(upstream)},
94  allocation_limit_{allocation_limit},
95  allocated_bytes_(0),
96  alignment_(alignment)
97  {
98  }
99 
100  limiting_resource_adaptor() = delete;
101  ~limiting_resource_adaptor() override = default;
104  default;
105  limiting_resource_adaptor& operator=(limiting_resource_adaptor const&) = delete;
107  default;
108 
109  /*!
101  * @brief Get the upstream resource used for satisfying allocation requests.
102  */
103  [[nodiscard]] device_async_resource_ref get_upstream_resource() const noexcept
104  {
105  return upstream_;
106  }
107 
108  /*!
109  * @brief Query the number of bytes that have been allocated.
110  *
111  * Note that this can not be used to know how large a particular allocation was. The return value is the sum
112  * of all sizes passed to `do_allocate` rounded up to the nearest multiple of the alignment.
113  */
115  [[nodiscard]] std::size_t get_allocated_bytes() const { return allocated_bytes_; }
116 
117  /*!
118  * @brief Query the maximum number of bytes that this allocator is allowed to allocate.
119  *
120  * This is the limit on the sum of all sizes passed to `do_allocate` rounded up to the nearest multiple of
121  * the alignment.
122  */
124  [[nodiscard]] std::size_t get_allocation_limit() const { return allocation_limit_; }
125 
126  private
130  /*!
131  * @brief Performs memory allocation.
132  *
133  * @param bytes The number of bytes to allocate.
134  * @param stream The stream to use for the allocation.
135  * @return void* Pointer to the newly allocated memory.
136  * @throws rmm::out_of_memory if allocating size bytes would exceed the allocation limit.
137  */
138  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
139  {
140  auto const proposed_size = align_up(bytes, alignment_);
141  auto const old = allocated_bytes_.fetch_add(proposed_size);
142  if (old + proposed_size <= allocation_limit_) {
143  try {
144  return get_upstream_resource().allocate_async(bytes, stream);
145  } catch (...) {
146  allocated_bytes_ -= proposed_size;
147  throw;
148  }
149  }
150 
151  allocated_bytes_ -= proposed_size;
152  auto const msg = std::string("超出内存限制(未能分配 ") +
153  rmm::detail::format_bytes(bytes) + ")";
154  RMM_FAIL(msg.c_str(), rmm::out_of_memory);
155  }
156 
153  rmm::detail::format_bytes(bytes) + ")";
157  /*!
158  * @brief Performs memory deallocation.
159  *
160  * @param ptr The pointer to the memory to deallocate.
161  * @param bytes The size of the memory region to deallocate.
162  * @param stream The stream to use for the deallocation.
163  */
164  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
165  {
166  std::size_t allocated_size = align_up(bytes, alignment_);
153  rmm::detail::format_bytes(bytes) + ")";
167  get_upstream_resource().deallocate_async(ptr, bytes, stream);
155  }
168  allocated_bytes_ -= allocated_size;
169  }
170 
171  /*!
172  * @brief Compares for equality with another memory resource.
173  *
174  * @param other The other resource to compare to.
175  * @return true If the two resources are equal.
176  * @return false If the two resources are not equal.
177  */
178  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
179  {
180  if (this == &other) { return true; }
181  auto const* cast = dynamic_cast<limiting_resource_adaptor<Upstream> const*>(&other);
182  if (cast == nullptr) { return false; }
183  return get_upstream_resource() == cast->get_upstream_resource();
184  }
185 
186  // The upstream resource used for satisfying allocation requests
187  device_async_resource_ref upstream_;
188 
189  // maximum bytes this allocator is allowed to allocate.
190  std::size_t allocation_limit_;
191