sam_headroom_memory_resource.hpp
查看此文件的文档。
1 /*
2  * 版权所有 (c) 2024-2025, 英伟达公司。
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/export.hpp>
23 #include <rmm/resource_ref.hpp>
24 
25 #include <algorithm>
26 #include <cstddef>
27 
28 namespace RMM_NAMESPACE {
29 namespace mr {
48 /**
49  * @brief Resource that uses system memory resource to allocate memory with a headroom.
55  * @brief 构建一个预留内存资源。
56  */
57 explicit sam_headroom_memory_resource(std::size_t headroom) : system_mr_{}, headroom_{headroom} {}
58 
60  ~sam_headroom_memory_resource() override = default;
65 
66  private
76  /**
77  * @brief Allocates memory of size at least bytes.
78  * @brief 分配至少 bytes 大小的内存。
79  *
80  * @param bytes The size of memory to allocate
81  * @param stream The stream to associate the allocation with
82  * @return void* Pointer to the allocated memory
83  */
84  void* do_allocate(std::size_t bytes, [[maybe_unused]] cuda_stream_view stream) override
85  {
86  void* pointer = system_mr_.allocate_async(bytes, rmm::CUDA_ALLOCATION_ALIGNMENT, stream);
87 
88  auto const free = rmm::available_device_memory().first;
89  auto const allocatable = free > headroom_ ? free - headroom_ : 0UL;
90  auto const gpu_portion =
91  rmm::align_down(std::min(allocatable, bytes), rmm::CUDA_ALLOCATION_ALIGNMENT);
92  auto const cpu_portion = bytes - gpu_portion;
93  if (gpu_portion != 0) {
94  RMM_CUDA_TRY(cudaMemAdvise(pointer,
95  gpu_portion,
96  cudaMemAdviseSetPreferredLocation,
97  rmm::get_current_cuda_device().value()));
98  }
99  if (cpu_portion != 0) {
100  RMM_CUDA_TRY(cudaMemAdvise(static_cast<char*>(pointer) + gpu_portion,
101  cpu_portion,
102  cudaMemAdviseSetPreferredLocation,
103  cudaCpuDeviceId));
104  }
105 
106  return pointer;
107  }
108 
110  /**
111  * @brief Deallocate memory pointed to by ptr.
112  * @brief 释放 ptr 指向的内存。
113  *
114  * @param ptr Pointer to be deallocated
115  * @param bytes The size of memory to deallocate
116  * @param stream The stream to associate the deallocation with
117  */
118  void do_deallocate(void* ptr,
119  [[maybe_unused]] std::size_t bytes,
120  [[maybe_unused]] cuda_stream_view stream) override
121  {
122  system_mr_.deallocate_async(ptr, rmm::CUDA_ALLOCATION_ALIGNMENT, stream);
123  }
124 
125  /**
126  * @brief Returns true if the two resources are equivalent.
127  * @brief 如果两个资源等效,则返回 true。
128  *
129  * @param other The other resource to compare to
130  * @return bool true if the resources are equivalent, false otherwise
131  */
132  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
133  {
134  if (this == &other) { return true; }
135  auto cast = dynamic_cast<sam_headroom_memory_resource const*>(&other);
136  if (cast == nullptr) { return false; }
137  return headroom_ == cast->headroom_;
138  }
139 
140  /// @brief system_memory_resource instance
141  system_memory_resource system_mr_;
142  /// @brief headroom amount
143  std::size_t headroom_;
144 }; // 组结束
145 } // namespace mr
146 } // namespace RMM_NAMESPACE
rmm::cuda_stream_view
定义: cuda_stream_view.hpp:39
rmm::mr::device_memory_resource
所有 librmm 设备内存分配的基类。
rmm::mr::device_memory_resource::allocate_async
void * allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view stream)
分配至少 bytes 大小的内存。