failure_callback_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2026, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/detail/error.hpp>
8 #include <rmm/detail/export.hpp>
11 #include <rmm/resource_ref.hpp>
12 
13 #include <cstddef>
14 #include <functional>
15 #include <memory>
16 #include <utility>
17 
18 namespace RMM_NAMESPACE {
19 namespace mr {
41 using failure_callback_t = std::function<bool(std::size_t, void*)>;
42 
84 template <typename Upstream, typename ExceptionType = rmm::out_of_memory>
86  public:
87  using exception_type = ExceptionType;
88 
98  failure_callback_t callback,
99  void* callback_arg)
100  : upstream_{upstream}, callback_{std::move(callback)}, callback_arg_{callback_arg}
101  {
102  }
103 
115  failure_callback_t callback,
116  void* callback_arg)
117  : upstream_{to_device_async_resource_ref_checked(upstream)},
118  callback_{std::move(callback)},
119  callback_arg_{callback_arg}
120  {
121  }
122 
124  ~failure_callback_resource_adaptor() override = default;
128  default;
130  default;
131 
135  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
136  {
137  return upstream_;
138  }
139 
140  private:
152  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
153  {
154  void* ret{};
155 
156  while (true) {
157  try {
158  ret = get_upstream_resource().allocate(stream, bytes);
159  break;
160  } catch (exception_type const& e) {
161  if (!callback_(bytes, callback_arg_)) { throw; }
162  }
163  }
164  return ret;
165  }
166 
174  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
175  {
176  get_upstream_resource().deallocate(stream, ptr, bytes);
177  }
178 
186  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
187  {
188  if (this == std::addressof(other)) { return true; }
189  auto cast = dynamic_cast<failure_callback_resource_adaptor<Upstream> const*>(&other);
190  if (cast == nullptr) { return false; }
191  return get_upstream_resource() == cast->get_upstream_resource();
192  }
193 
194  // the upstream resource used for satisfying allocation requests
195  device_async_resource_ref upstream_;
196  failure_callback_t callback_;
197  void* callback_arg_;
198 };
199  // end of group
201 } // namespace mr
202 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:83
A device memory resource that calls a callback function when allocations throw a specified exception ...
Definition: failure_callback_resource_adaptor.hpp:85
failure_callback_resource_adaptor(device_async_resource_ref 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:97
ExceptionType exception_type
The type of exception this object catches/throws.
Definition: failure_callback_resource_adaptor.hpp:87
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:114
failure_callback_resource_adaptor(failure_callback_resource_adaptor &&) noexcept=default
Default move constructor.
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: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:72
detail::cccl_async_resource_ref< cuda::mr::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:32
RAPIDS Memory Manager - The top-level namespace for all RMM functionality.
Management of per-device device_memory_resources.