thread_safe_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 
8 #include <rmm/detail/error.hpp>
9 #include <rmm/detail/export.hpp>
11 #include <rmm/resource_ref.hpp>
12 
13 #include <cstddef>
14 #include <memory>
15 #include <mutex>
16 
17 namespace RMM_NAMESPACE {
18 namespace mr {
33 template <typename Upstream>
35  public:
36  using lock_t = std::lock_guard<std::mutex>;
37 
46  thread_safe_resource_adaptor(device_async_resource_ref upstream) : upstream_{upstream} {}
47 
58  thread_safe_resource_adaptor(Upstream* upstream)
59  : upstream_{to_device_async_resource_ref_checked(upstream)}
60  {
61  }
62 
64  ~thread_safe_resource_adaptor() override = default;
69 
74  {
75  return upstream_;
76  }
77 
78  private:
90  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
91  {
92  lock_t lock(mtx);
93  return get_upstream_resource().allocate(stream, bytes);
94  }
95 
103  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
104  {
105  lock_t lock(mtx);
106  get_upstream_resource().deallocate(stream, ptr, bytes);
107  }
108 
116  bool do_is_equal(device_memory_resource const& other) const noexcept override
117  {
118  if (this == std::addressof(other)) { return true; }
119  auto cast = dynamic_cast<thread_safe_resource_adaptor<Upstream> const*>(&other);
120  if (cast == nullptr) { return false; }
121  return get_upstream_resource() == cast->get_upstream_resource();
122  }
123 
124  std::mutex mutable mtx; // mutex for thread safe access to upstream
126  upstream_;
127 };
128  // end of group
130 } // namespace mr
131 } // 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
Resource that adapts Upstream memory resource adaptor to be thread safe.
Definition: thread_safe_resource_adaptor.hpp:34
std::lock_guard< std::mutex > lock_t
Type of lock used to synchronize access.
Definition: thread_safe_resource_adaptor.hpp:36
thread_safe_resource_adaptor(device_async_resource_ref upstream)
Construct a new thread safe resource adaptor using upstream to satisfy allocation requests.
Definition: thread_safe_resource_adaptor.hpp:46
rmm::device_async_resource_ref get_upstream_resource() const noexcept
rmm::device_async_resource_ref to the upstream resource
Definition: thread_safe_resource_adaptor.hpp:73
thread_safe_resource_adaptor(Upstream *upstream)
Construct a new thread safe resource adaptor using upstream to satisfy allocation requests.
Definition: thread_safe_resource_adaptor.hpp:58
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