failure_callback_resource_adaptor.hpp
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>
19 #include <rmm/mr/device/device_memory_resource.hpp>
20 
21 #include <cstddef>
22 #include <functional>
23 #include <utility>
24 
25 namespace rmm::mr {
26 
42 using failure_callback_t = std::function<bool(std::size_t, void*)>;
43 
85 template <typename Upstream, typename ExceptionType = rmm::out_of_memory>
87  public:
88  using exception_type = ExceptionType;
89 
101  failure_callback_t callback,
102  void* callback_arg)
103  : upstream_{upstream}, callback_{std::move(callback)}, callback_arg_{callback_arg}
104  {
105  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
106  }
107 
109  ~failure_callback_resource_adaptor() override = default;
110  failure_callback_resource_adaptor(failure_callback_resource_adaptor const&) = delete;
111  failure_callback_resource_adaptor& operator=(failure_callback_resource_adaptor const&) = delete;
112  failure_callback_resource_adaptor(failure_callback_resource_adaptor&&) noexcept = default;
113  failure_callback_resource_adaptor& operator=(failure_callback_resource_adaptor&&) noexcept =
114  default;
115 
121  Upstream* get_upstream() const noexcept { return upstream_; }
122 
129  [[nodiscard]] bool supports_streams() const noexcept override
130  {
131  return upstream_->supports_streams();
132  }
133 
139  [[nodiscard]] bool supports_get_mem_info() const noexcept override
140  {
141  return upstream_->supports_get_mem_info();
142  }
143 
144  private:
156  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
157  {
158  void* ret{};
159 
160  while (true) {
161  try {
162  ret = upstream_->allocate(bytes, stream);
163  break;
164  } catch (exception_type const& e) {
165  if (!callback_(bytes, callback_arg_)) { throw; }
166  }
167  }
168  return ret;
169  }
170 
180  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
181  {
182  upstream_->deallocate(ptr, bytes, stream);
183  }
184 
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 
221 } // namespace rmm::mr
rmm::cuda_stream_view
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:34
rmm::mr::failure_callback_resource_adaptor::get_upstream
Upstream * get_upstream() const noexcept
Return pointer to the upstream resource.
Definition: failure_callback_resource_adaptor.hpp:121
rmm::mr::failure_callback_resource_adaptor::exception_type
ExceptionType exception_type
The type of exception this object catches/throws.
Definition: failure_callback_resource_adaptor.hpp:88
rmm::mr::failure_callback_resource_adaptor::supports_get_mem_info
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: failure_callback_resource_adaptor.hpp:139
rmm::mr::failure_callback_resource_adaptor::supports_streams
bool supports_streams() const noexcept override
Checks whether the upstream resource supports streams.
Definition: failure_callback_resource_adaptor.hpp:129
rmm::mr::failure_callback_resource_adaptor
A device memory resource that calls a callback function when allocations throw a specified exception ...
Definition: failure_callback_resource_adaptor.hpp:86
rmm::mr::device_memory_resource
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:82
rmm::mr::failure_callback_resource_adaptor::failure_callback_resource_adaptor
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:100