All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
limiting_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2024, 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  * http://www.apache.org/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>
23 #include <rmm/resource_ref.hpp>
24 
25 #include <cstddef>
26 
27 namespace RMM_NAMESPACE {
28 namespace mr {
46 template <typename Upstream>
48  public:
58  std::size_t allocation_limit,
59  std::size_t alignment = CUDA_ALLOCATION_ALIGNMENT)
60  : upstream_{upstream},
61  allocation_limit_{allocation_limit},
62  allocated_bytes_(0),
63  alignment_(alignment)
64  {
65  }
66 
77  limiting_resource_adaptor(Upstream* upstream,
78  std::size_t allocation_limit,
79  std::size_t alignment = CUDA_ALLOCATION_ALIGNMENT)
80  : upstream_{to_device_async_resource_ref_checked(upstream)},
81  allocation_limit_{allocation_limit},
82  allocated_bytes_(0),
83  alignment_(alignment)
84  {
85  }
86 
87  limiting_resource_adaptor() = delete;
88  ~limiting_resource_adaptor() override = default;
91  default;
92  limiting_resource_adaptor& operator=(limiting_resource_adaptor const&) = delete;
94  default;
95 
99  [[nodiscard]] device_async_resource_ref get_upstream_resource() const noexcept
100  {
101  return upstream_;
102  }
103 
113  [[nodiscard]] std::size_t get_allocated_bytes() const { return allocated_bytes_; }
114 
122  [[nodiscard]] std::size_t get_allocation_limit() const { return allocation_limit_; }
123 
124  private:
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  RMM_FAIL("Exceeded memory limit", rmm::out_of_memory);
153  }
154 
162  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
163  {
164  std::size_t allocated_size = align_up(bytes, alignment_);
165  get_upstream_resource().deallocate_async(ptr, bytes, stream);
166  allocated_bytes_ -= allocated_size;
167  }
168 
176  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
177  {
178  if (this == &other) { return true; }
179  auto const* cast = dynamic_cast<limiting_resource_adaptor<Upstream> const*>(&other);
180  if (cast == nullptr) { return false; }
181  return get_upstream_resource() == cast->get_upstream_resource();
182  }
183 
184  // The upstream resource used for satisfying allocation requests
185  device_async_resource_ref upstream_;
186 
187  // maximum bytes this allocator is allowed to allocate.
188  std::size_t allocation_limit_;
189 
190  // number of currently-allocated bytes
191  std::atomic<std::size_t> allocated_bytes_;
192 
193  // todo: should be some way to ask the upstream...
194  std::size_t alignment_;
195 };
196 
206 template <typename Upstream>
207 [[deprecated(
208  "make_limiting_adaptor is deprecated in RMM 24.10. Use the limiting_resource_adaptor constructor "
209  "instead.")]]
211  std::size_t allocation_limit)
212 {
213  return limiting_resource_adaptor<Upstream>{upstream, allocation_limit};
214 }
215  // end of group
217 } // namespace mr
218 } // namespace RMM_NAMESPACE
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:94
Resource that uses Upstream to allocate memory and limits the total allocations possible.
Definition: limiting_resource_adaptor.hpp:47
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:57
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:113
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:122
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:77
Exception thrown when RMM runs out of memory.
Definition: error.hpp:87
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:210
cuda::mr::async_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:41
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:79
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:43
constexpr std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
Align up to nearest multiple of specified power of 2.
Definition: aligned.hpp:77
Management of per-device device_memory_resources.