thread_safe_resource_adaptor.hpp
转到此文件的文档。
1 /*
2  * Copyright (c) 2020-2024, NVIDIA CORPORATION.
3  *
4  * 根据 Apache License, Version 2.0 (“许可证”) 获得许可;
5  * 除非符合许可证,否则您不得使用此文件。
6  * 您可以获取许可证的副本
7  *
8  * https://apache.ac.cn/licenses/LICENSE-2.0
9  *
10  * 除非适用法律要求或书面同意,否则根据许可证分发的软件
11  * 按“原样”分发,不附带任何明示或暗示的担保或条件。
12  * 有关管理权限和
13  * 许可证下的限制,请参阅许可证。
14  */
15 
16 #pragma once
17 
18 #include <rmm/cuda_stream_view.hpp>
19 #include <rmm/detail/error.hpp>
20 #include <rmm/detail/export.hpp>
22 #include <rmm/resource_ref.hpp>
23 
24 #include <cstddef>
25 #include <mutex>
26 
27 namespace RMM_NAMESPACE {
28 namespace mr {
43 /// @addtogroup memory_resources
44 /// @{template <typename Upstream>
45  public
46  /// Type of lock used to synchronize access.
46  using lock_t = std::lock_guard<std::mutex>;
47 
56  /// @brief Constructs a new thread safe resource adaptor using `upstream` to satisfy
56  /// allocation requests.
56  thread_safe_resource_adaptor(device_async_resource_ref upstream) : upstream_{upstream} {}
57 
68  /// @brief Constructs a new thread safe resource adaptor using `upstream` to satisfy
68  /// allocation requests.
69  thread_safe_resource_adaptor(Upstream* upstream)
69  : upstream_{to_device_async_resource_ref_checked(upstream)}
70  {
71  }
72 
74  ~thread_safe_resource_adaptor() override = default;
79 
83  /// @brief Returns a rmm::device_async_resource_ref to the upstream resource
84  {
85  return upstream_;
86  }
87 
100  /// @brief Allocates memory from the upstream resource. Synchronizes access to the
100  /// upstream resource.
100  /// @copydoc device_memory_resource::do_allocate(std::size_t bytes, cuda_stream_view stream)
100  private
100  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
101  {
102  lock_t lock(mtx);
103  return get_upstream_resource().allocate_async(bytes, stream);
104  }
105 
113  /// @brief Deallocates memory from the upstream resource. Synchronizes access to the
113  /// upstream resource.
113  /// @copydoc device_memory_resource::do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream)
113  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
114  {
115  lock_t lock(mtx);
116  get_upstream_resource().deallocate_async(ptr, bytes, stream);
117  }
118 
126  /// @brief Compares for equality.
126  /// @copydoc device_memory_resource::do_is_equal(device_memory_resource const& other) const noexcept
126  bool do_is_equal(device_memory_resource const& other) const noexcept override
127  {
128  if (this == &other) { return true; }
129  auto cast = dynamic_cast<thread_safe_resource_adaptor<Upstream> const*>(&other);
130  if (cast == nullptr) { return false; }
131  return get_upstream_resource() == cast->get_upstream_resource();
132  }
133 
134  std::mutex mutable mtx; // 用于线程安全访问上游资源的互斥锁
136  upstream_;
137 };
138  /// @} end of group
140 } // namespace mr
141 } // namespace RMM_NAMESPACE
rmm::cuda_stream_view
CUDA 流的强类型非拥有包装器,带有默认构造函数。
rmm::mr::device_memory_resource
所有 librmm 设备内存分配的基类。
定义: device_memory_resource.hpp:92
140 } // namespace mr
rmm::mr::thread_safe_resource_adaptor
rmm::cuda_stream_view
适配上游内存资源适配器以实现线程安全的资源。
用于同步访问的锁类型。
定义: thread_safe_resource_adaptor.hpp:46
rmm::mr::thread_safe_resource_adaptor::thread_safe_resource_adaptor
使用 `upstream` 构造一个新的线程安全资源适配器来满足分配请求。
定义: thread_safe_resource_adaptor.hpp:56
rmm::mr::thread_safe_resource_adaptor::get_upstream_resource