thrust_allocator_adaptor.hpp
转到此文件的文档。
1 /*
2  * 版权所有 (c) 2019-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  * 有关管理权限和
14  * 许可证下的限制的具体语言,请参阅许可证。
15  */
16 
17 #pragma once
18 
19 #include <rmm/cuda_device.hpp>
20 #include <rmm/detail/cuda_memory_resource.hpp>
21 #include <rmm/detail/export.hpp>
22 #include <rmm/detail/thrust_namespace.h>
24 #include <rmm/resource_ref.hpp>
25 
26 #include <thrust/device_malloc_allocator.h>
27 #include <thrust/device_ptr.h>
28 #include <thrust/memory.h>
29 
30 namespace RMM_NAMESPACE {
31 namespace mr {
50 /**
51  * @brief 与 Thrust 容器和算法兼容的分配器。
52  *
53  * 此分配器使用 `rmm::device_async_resource_ref` 包装上游设备内存资源,用于在设备上分配内存。
54  *
55  * 分配器通过 `T` 参数化,`T` 是将为其分配内存的元素的类型。
56  *
63  * 此分配器满足 `thrust::Allocator` 和 `std::allocator` 的要求(带有流/资源的扩展)。
64  *
65  * 与标准的 `thrust::device_malloc_allocator` 或 `thrust::cuda::malloc_allocator` 不同,此
66  * 分配器使用指定的流和资源执行分配和释放。
67  */
72 template <typename T>
73 class thrust_allocator : public thrust::device_malloc_allocator<T> {
80  public
81  using Base = thrust::device_malloc_allocator<T>;
89  using pointer = typename Base::pointer;
90  using size_type = typename Base::size_type;
91 
92  /** @brief 提供使用另一种类型实例化的 thrust_allocator 的类型。*/
93  template <typename U>
99  struct rebind {
101  };
102 
103  /** @brief 默认构造函数使用默认内存资源和默认流创建分配器。 */
1000  thrust_allocator() = default;
112  /** @brief 使用默认设备内存资源和指定流构造 thrust_allocator。 */
113  explicit thrust_allocator(cuda_stream_view stream) : _stream{stream} {}
114 
115  /** @brief 使用设备内存资源和流构造 thrust_allocator。 */
117  : _stream{stream}, _mr(mr)
118  {
119  }
126  /** @brief 复制构造函数。复制资源指针和流。 */
127  template <typename U>
129  : _mr(other.resource()), _stream{other.stream()}, _device{other._device}
130  {
134  }
135 
136  /** @brief 分配类型 T 的对象
142  */
149  {
150  cuda_set_device_raii dev{_device};
151  return thrust::device_pointer_cast(
152  static_cast<T*>(_mr.allocate_async(num * sizeof(T), _stream)));
153  }
154 
155  /** @brief 释放类型 T 的对象
157  */
158  void deallocate(pointer ptr, size_type num)
159  {
160  cuda_set_device_raii dev{_device};
161  return _mr.deallocate_async(thrust::raw_pointer_cast(ptr), num * sizeof(T), _stream);
162  }
163 
164  /** @brief 指向上游资源的 rmm::device_async_resource_ref */
165  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
166  {
167  return _mr;
168  }
169 
170  /** @brief 此分配器使用的流。 */
171  [[nodiscard]] cuda_stream_view stream() const noexcept { return _stream; }
172 
165  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
173  /** @brief 启用 cuda::mr::device_accessible 属性。*/
174  friend void get_property(thrust_allocator const&, cuda::mr::device_accessible) noexcept {}
175 
176  private
177  cuda_stream_view _stream{};
179  cuda_device_id _device{get_current_cuda_device()};
180 }; // end of group
181 } // namespace mr
182 } // namespace RMM_NAMESPACE
rmm::cuda_stream_view
定义: cuda_stream_view.hpp:39
rmm::mr::thrust_allocator
与使用 device_async_resource_ref 进行内存分配的 Thrust 容器和算法兼容的分配器...
165  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
定义: thrust_allocator_adaptor.hpp:51
rmm::mr::thrust_allocator::thrust_allocator
使用设备内存资源和流构造 thrust_allocator。
定义: thrust_allocator_adaptor.hpp:89
rmm::mr::thrust_allocator::pointer
指针类型。
定义: thrust_allocator_adaptor.hpp:54
thrust_allocator(thrust_allocator< U > const &other)
定义: thrust_allocator_adaptor.hpp:100
rmm::mr::thrust_allocator::allocate
pointer allocate(size_type num)
定义: thrust_allocator_adaptor.hpp:111
rmm::mr::thrust_allocator::stream
cuda_stream_view stream() const noexcept
165  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
此分配器使用的流。
定义: thrust_allocator_adaptor.hpp:142
rmm::mr::thrust_allocator::get_upstream_resource
定义: thrust_allocator_adaptor.hpp:134
thrust_allocator()=default
默认构造函数使用默认内存资源和默认流创建分配器。
typename Base::size_type size_type
大小类型。
定义: thrust_allocator_adaptor.hpp:55
thrust::device_malloc_allocator< T > Base
此分配器的基类型。
定义: thrust_allocator_adaptor.hpp:53
friend void get_property(thrust_allocator const &, cuda::mr::device_accessible) noexcept
rmm::mr::thrust_allocator::deallocate
void deallocate(pointer ptr, size_type num)
定义: thrust_allocator_adaptor.hpp:125
thrust_allocator(cuda_stream_view stream)