cuda_async_view_memory_resource.hpp
前往此文件的文档。
1 /*
2  * 版权所有 (c) 2021-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 #pragma once
17 
18 #include <rmm/cuda_device.hpp>
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/detail/error.hpp>
21 #include <rmm/detail/export.hpp>
22 #include <rmm/detail/thrust_namespace.h>
24 
25 #include <cuda_runtime_api.h>
26 
27 #include <cstddef>
28 
29 namespace RMM_NAMESPACE {
30 namespace mr {
42  public
53  cuda_async_view_memory_resource(cudaMemPool_t pool_handle)
54  : cuda_pool_handle_{[pool_handle]() {
55  RMM_EXPECTS(nullptr != pool_handle, "意外的空内存池句柄。");
56  return pool_handle;
57  }()}
58  {
59  // 检查是否支持 cudaMallocAsync 内存池
60  auto const device = rmm::get_current_cuda_device();
61  int cuda_pool_supported{};
62  auto result =
63  cudaDeviceGetAttribute(&cuda_pool_supported, cudaDevAttrMemoryPoolsSupported, device.value());
64  RMM_EXPECTS(result == cudaSuccess && cuda_pool_supported,
65  "此 CUDA 驱动程序/运行时版本不支持 cudaMallocAsync");
66  }
67 
73  [[nodiscard]] cudaMemPool_t pool_handle() const noexcept { return cuda_pool_handle_; }
74 
78  default;
80  default;
82  default;
84  default;
85 
86  private
87  cudaMemPool_t cuda_pool_handle_{};
88 
98  void* do_allocate(std::size_t bytes, rmm::cuda_stream_view stream) override
99  {
100  void* ptr{nullptr};
101  if (bytes > 0) {
102  RMM_CUDA_TRY_ALLOC(cudaMallocFromPoolAsync(&ptr, bytes, pool_handle(), stream.value()),
103  bytes);
104  }
105  return ptr;
106  }
107 
116  void do_deallocate(void* ptr,
117  [[maybe_unused]] std::size_t bytes,
118  rmm::cuda_stream_view stream) override
119  {
120  if (ptr != nullptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeAsync(ptr, stream.value())); }
121  }
122 
130  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
131  {
132  return dynamic_cast<cuda_async_view_memory_resource const*>(&other) != nullptr;
133  }
134 };
135  // 组结束
137 } // 命名空间 mr
138 } // 命名空间 RMM_NAMESPACE
具有默认构造函数的 CUDA 流的强类型非拥有包装器。
定义: cuda_stream_view.hpp:39
constexpr cudaStream_t value() const noexcept
获取包装的流。
定义: cuda_stream_view.hpp:73
使用 cudaMallocAsync/cudaFreeAsync 进行分配/释放的 device_memory_resource 派生类...
定义: cuda_async_view_memory_resource.hpp:41
cuda_async_view_memory_resource & operator=(cuda_async_view_memory_resource &&)=default
默认移动赋值运算符。
cuda_async_view_memory_resource(cuda_async_view_memory_resource &&)=default
默认移动构造函数。
cudaMemPool_t pool_handle() const noexcept
返回 CUDA 内存池的底层原生句柄。
定义: cuda_async_view_memory_resource.hpp:73
cuda_async_view_memory_resource(cudaMemPool_t pool_handle)
构造一个 cuda_async_view_memory_resource,它使用现有的 CUDA 内存池...。
定义: cuda_async_view_memory_resource.hpp:53
cuda_async_view_memory_resource(cuda_async_view_memory_resource const &)=default
默认复制构造函数。
cuda_async_view_memory_resource & operator=(cuda_async_view_memory_resource const &)=default
默认复制赋值运算符。
所有 librmm 设备内存分配的基类。
定义: device_memory_resource.hpp:92
cuda_device_id get_current_cuda_device()
返回当前设备的 cuda_device_id。
定义: cuda_device.hpp:99