limiting_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, 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/detail/aligned.hpp>
19 #include <rmm/detail/error.hpp>
21 
22 #include <cstddef>
23 
24 namespace rmm::mr {
42 template <typename Upstream>
44  public:
55  limiting_resource_adaptor(Upstream* upstream,
56  std::size_t allocation_limit,
57  std::size_t alignment = rmm::detail::CUDA_ALLOCATION_ALIGNMENT)
58  : allocation_limit_{allocation_limit},
59  allocated_bytes_(0),
60  alignment_(alignment),
61  upstream_{upstream}
62  {
63  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
64  }
65 
66  limiting_resource_adaptor() = delete;
67  ~limiting_resource_adaptor() override = default;
70  default;
71  limiting_resource_adaptor& operator=(limiting_resource_adaptor const&) = delete;
73  default;
74 
78  [[nodiscard]] Upstream* get_upstream() const noexcept { return upstream_; }
79 
86  [[nodiscard]] bool supports_streams() const noexcept override
87  {
88  return upstream_->supports_streams();
89  }
90 
96  [[nodiscard]] bool supports_get_mem_info() const noexcept override
97  {
98  return upstream_->supports_get_mem_info();
99  }
100 
110  [[nodiscard]] std::size_t get_allocated_bytes() const { return allocated_bytes_; }
111 
119  [[nodiscard]] std::size_t get_allocation_limit() const { return allocation_limit_; }
120 
121  private:
135  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
136  {
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_) {
140  try {
141  return upstream_->allocate(bytes, stream);
142  } catch (...) {
143  allocated_bytes_ -= proposed_size;
144  throw;
145  }
146  }
147 
148  allocated_bytes_ -= proposed_size;
149  RMM_FAIL("Exceeded memory limit", rmm::out_of_memory);
150  }
151 
159  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
160  {
161  std::size_t allocated_size = rmm::detail::align_up(bytes, alignment_);
162  upstream_->deallocate(ptr, bytes, stream);
163  allocated_bytes_ -= allocated_size;
164  }
165 
173  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
174  {
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);
179  }
180 
189  [[nodiscard]] std::pair<std::size_t, std::size_t> do_get_mem_info(
190  [[maybe_unused]] cuda_stream_view stream) const override
191  {
192  return {allocation_limit_ - allocated_bytes_, allocation_limit_};
193  }
194 
195  // maximum bytes this allocator is allowed to allocate.
196  std::size_t allocation_limit_;
197 
198  // number of currently-allocated bytes
199  std::atomic<std::size_t> allocated_bytes_;
200 
201  // todo: should be some way to ask the upstream...
202  std::size_t alignment_;
203 
204  Upstream* upstream_;
206 };
207 
217 template <typename Upstream>
219  std::size_t allocation_limit)
220 {
221  return limiting_resource_adaptor<Upstream>{upstream, allocation_limit};
222 }
223  // end of group
225 } // namespace rmm::mr
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