thread_safe_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2025, 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 <mutex>
15 
16 namespace RMM_NAMESPACE {
17 namespace mr {
32 template <typename Upstream>
34  public:
35  using lock_t = std::lock_guard<std::mutex>;
36 
45  thread_safe_resource_adaptor(device_async_resource_ref upstream) : upstream_{upstream} {}
46 
57  thread_safe_resource_adaptor(Upstream* upstream)
58  : upstream_{to_device_async_resource_ref_checked(upstream)}
59  {
60  }
61 
63  ~thread_safe_resource_adaptor() override = default;
68 
73  {
74  return upstream_;
75  }
76 
77  private:
89  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
90  {
91  lock_t lock(mtx);
92  return get_upstream_resource().allocate(stream, bytes);
93  }
94 
102  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
103  {
104  lock_t lock(mtx);
105  get_upstream_resource().deallocate(stream, ptr, bytes);
106  }
107 
115  bool do_is_equal(device_memory_resource const& other) const noexcept override
116  {
117  if (this == &other) { return true; }
118  auto cast = dynamic_cast<thread_safe_resource_adaptor<Upstream> const*>(&other);
119  if (cast == nullptr) { return false; }
120  return get_upstream_resource() == cast->get_upstream_resource();
121  }
122 
123  std::mutex mutable mtx; // mutex for thread safe access to upstream
125  upstream_;
126 };
127  // end of group
129 } // namespace mr
130 } // 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:33
std::lock_guard< std::mutex > lock_t
Type of lock used to synchronize access.
Definition: thread_safe_resource_adaptor.hpp:35
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:45
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:72
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:57
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