RMM  23.12
RAPIDS Memory Manager
failure_callback_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-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/error.hpp>
20 
21 #include <cstddef>
22 #include <functional>
23 #include <utility>
24 
25 namespace rmm::mr {
47 using failure_callback_t = std::function<bool(std::size_t, void*)>;
48 
90 template <typename Upstream, typename ExceptionType = rmm::out_of_memory>
92  public:
93  using exception_type = ExceptionType;
94 
106  failure_callback_t callback,
107  void* callback_arg)
108  : upstream_{upstream}, callback_{std::move(callback)}, callback_arg_{callback_arg}
109  {
110  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
111  }
112 
114  ~failure_callback_resource_adaptor() override = default;
118  default;
120  default;
121 
125  Upstream* get_upstream() const noexcept { return upstream_; }
126 
133  [[nodiscard]] bool supports_streams() const noexcept override
134  {
135  return upstream_->supports_streams();
136  }
137 
143  [[nodiscard]] bool supports_get_mem_info() const noexcept override
144  {
145  return upstream_->supports_get_mem_info();
146  }
147 
148  private:
160  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
161  {
162  void* ret{};
163 
164  while (true) {
165  try {
166  ret = upstream_->allocate(bytes, stream);
167  break;
168  } catch (exception_type const& e) {
169  if (!callback_(bytes, callback_arg_)) { throw; }
170  }
171  }
172  return ret;
173  }
174 
182  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
183  {
184  upstream_->deallocate(ptr, bytes, stream);
185  }
186 
194  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
195  {
196  if (this == &other) { return true; }
197  auto cast = dynamic_cast<failure_callback_resource_adaptor<Upstream> const*>(&other);
198  return cast != nullptr ? upstream_->is_equal(*cast->get_upstream())
199  : upstream_->is_equal(other);
200  }
201 
210  [[nodiscard]] std::pair<std::size_t, std::size_t> do_get_mem_info(
211  cuda_stream_view stream) const override
212  {
213  return upstream_->get_mem_info(stream);
214  }
215 
216  Upstream* upstream_; // the upstream resource used for satisfying allocation requests
217  failure_callback_t callback_;
218  void* callback_arg_;
219 };
220  // end of group
222 } // 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
A device memory resource that calls a callback function when allocations throw a specified exception ...
Definition: failure_callback_resource_adaptor.hpp:91
Upstream * get_upstream() const noexcept
Pointer to the upstream resource.
Definition: failure_callback_resource_adaptor.hpp:125
ExceptionType exception_type
The type of exception this object catches/throws.
Definition: failure_callback_resource_adaptor.hpp:93
failure_callback_resource_adaptor(Upstream *upstream, failure_callback_t callback, void *callback_arg)
Construct a new failure_callback_resource_adaptor using upstream to satisfy allocation requests.
Definition: failure_callback_resource_adaptor.hpp:105
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: failure_callback_resource_adaptor.hpp:143
failure_callback_resource_adaptor(failure_callback_resource_adaptor &&) noexcept=default
Default move constructor.
bool supports_streams() const noexcept override
Checks whether the upstream resource supports streams.
Definition: failure_callback_resource_adaptor.hpp:133
std::function< bool(std::size_t, void *)> failure_callback_t
Callback function type used by failure_callback_resource_adaptor.
Definition: failure_callback_resource_adaptor.hpp:47