polymorphic_allocator.hpp
转到此文件的文档。
1 /*
2  * 版权所有 (c) 2020-2025, NVIDIA CORPORATION.
3  *
4  * 根据 Apache 许可,版本 2.0 (以下简称"许可") 获得许可;
5  * 您不得使用此文件,除非符合许可规定。
6  * 您可以在以下位置获得许可的副本:
7  *
8  * https://apache.ac.cn/licenses/LICENSE-2.0
9  *
10  * 除非适用法律要求或书面同意,否则根据许可分发的软件按"原样"提供,
11  * 没有任何明示或默示的担保或条件。
12  * 有关许可下特定语言的管理权限和限制,请参阅许可。
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/detail/export.hpp>
22 #include <rmm/resource_ref.hpp>
23 
24 #include <cstddef>
25 #include <memory>
26 
27 namespace RMM_NAMESPACE {
28 namespace mr {
48 template <typename T>
50  public
51  using value_type = T;
57  polymorphic_allocator() = default;
58 
67 
74  template <typename U>
76  : mr_{other.get_upstream_resource()}
77  {
78  }
79 
87  value_type* allocate(std::size_t num, cuda_stream_view stream)
88  {
89  return static_cast<value_type*>(
90  get_upstream_resource().allocate_async(num * sizeof(T), stream));
91  }
92 
103  void deallocate(value_type* ptr, std::size_t num, cuda_stream_view stream)
104  {
105  get_upstream_resource().deallocate_async(ptr, num * sizeof(T), stream);
106  }
107 
112  {
113  return mr_;
114  }
115 
116  private
119 };
120 
132 template <typename T, typename U>
134 {
135  return lhs.get_upstream_resource() == rhs.get_upstream_resource();
136 }
137 
150 template <typename T, typename U>
152 {
153  return not(lhs == rhs);
154 }
155 
178 template <typename Allocator>
180  public
181  using value_type =
182  typename std::allocator_traits<Allocator>::value_type;
184 
185  stream_allocator_adaptor() = delete;
186 
196  stream_allocator_adaptor(Allocator const& allocator, cuda_stream_view stream)
197  : alloc_{allocator}, stream_{stream}
198  {
199  }
200 
209  template <typename OtherAllocator>
211  : stream_allocator_adaptor{other.underlying_allocator(), other.stream()}
212  {
213  }
214 
220  template <typename T>
221  struct rebind {
222  using other = stream_allocator_adaptor<typename std::allocator_traits<
223  Allocator>::template rebind_alloc<T>>;
224  };
225 
233  value_type* allocate(std::size_t num) { return alloc_.allocate(num, stream()); }
234 
244  void deallocate(value_type* ptr, std::size_t num) { alloc_.deallocate(ptr, num, stream()); }
245 
249  [[nodiscard]] cuda_stream_view stream() const noexcept { return stream_; }
250 
254  [[nodiscard]] Allocator underlying_allocator() const noexcept { return alloc_; }
255 
256  private
257  Allocator alloc_;
258  cuda_stream_view stream_;
259 };
260 
272 template <typename A, typename O>
274 {
275  return lhs.underlying_allocator() == rhs.underlying_allocator();
276 }
277 
289 template <typename A, typename O>
291 {
292  return not(lhs == rhs);
293 }
294  // 组结束
296 } // 命名空间 mr
297 } // 命名空间 RMM_NAMESPACE
CUDA 流的强类型非拥有包装器,带有默认构造函数。
定义: cuda_stream_view.hpp:39
使用 rmm::mr::device_memory_resource 满足(解除)分配的流有序分配器。
定义: polymorphic_allocator.hpp:49
T value_type
T,此分配器分配的对象的类型。
定义: polymorphic_allocator.hpp:51
rmm::device_async_resource_ref get_upstream_resource() const noexcept
rmm::device_async_resource_ref 到上游资源。
定义: polymorphic_allocator.hpp:111
polymorphic_allocator()=default
使用 rmm::mr::get_current_device_resource_ref(...) 的返回值构造一个 polymorphic_allocator。
void deallocate(value_type *ptr, std::size_t num, cuda_stream_view stream)
解除分配 ptr 指向的存储。
定义: polymorphic_allocator.hpp:103
value_type * allocate(std::size_t num, cuda_stream_view stream)
使用底层内存资源为 num 个类型为 T 的对象分配存储。
定义: polymorphic_allocator.hpp:87
polymorphic_allocator(device_async_resource_ref mr)
使用提供的内存资源构造一个 polymorphic_allocator。
定义: polymorphic_allocator.hpp:66
polymorphic_allocator(polymorphic_allocator< U > const &other) noexcept
使用 other 的底层内存资源构造一个 polymorphic_allocator。
定义: polymorphic_allocator.hpp:75
使流有序分配器适应,以提供标准分配器接口。
定义: polymorphic_allocator.hpp:179
value_type * allocate(std::size_t num)
使用 stream() 上的底层分配器为 num 个类型为 T 的对象分配存储。
定义: polymorphic_allocator.hpp:233
typename std::allocator_traits< Allocator >::value_type value_type
定义: polymorphic_allocator.hpp:183
void deallocate(value_type *ptr, std::size_t num)
使用 stream() 上的底层分配器解除分配 ptr 指向的存储。
定义: polymorphic_allocator.hpp:244
Allocator underlying_allocator() const noexcept
底层分配器。
定义: polymorphic_allocator.hpp:254
stream_allocator_adaptor(Allocator const &allocator, cuda_stream_view stream)
使用 a 作为底层分配器构造一个 stream_allocator_adaptor。
定义: polymorphic_allocator.hpp:196
stream_allocator_adaptor(stream_allocator_adaptor< OtherAllocator > const &other)
使用 other.underlying_allocator() 和 other... 构造一个 stream_allocator_adaptor。
定义: polymorphic_allocator.hpp:210
cuda_stream_view stream() const noexcept
调用底层分配器所使用的流。
定义: polymorphic_allocator.hpp:249
bool operator==(stream_allocator_adaptor< A > const &lhs, stream_allocator_adaptor< O > const &rhs)
比较两个 stream_allocator_adaptor 是否相等。
定义: polymorphic_allocator.hpp:273
bool operator!=(stream_allocator_adaptor< A > const &lhs, stream_allocator_adaptor< O > const &rhs)
比较两个 stream_allocator_adaptor 是否不相等。
定义: polymorphic_allocator.hpp:290
cuda::mr::async_resource_ref< cuda::mr::device_accessible > device_async_resource_ref
带有 cuda::mr::device_accessible 属性的 cuda::mr::async_resource_ref 的别名。
定义: resource_ref.hpp:40
device_async_resource_ref get_current_device_resource_ref()
获取当前设备的 device_async_resource_ref。
定义: per_device_resource.hpp:411
每设备 device_memory_resource 的管理。
将分配器重新绑定到指定的类型。
定义: polymorphic_allocator.hpp:221