pinned_memory_resource.hpp
查看此文件的文档。
1 /*
2  * Copyright (c) 2020-2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://apache.ac.cn/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <rmm/aligned.hpp>
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/detail/aligned.hpp>
21 #include <rmm/detail/error.hpp>
22 #include <rmm/detail/export.hpp>
24 
25 #include <cstddef>
26 
27 namespace RMM_NAMESPACE {
28 namespace mr {
42  public
43  pinned_memory_resource() = default;
44  ~pinned_memory_resource() override = default;
48  default;
50  default;
51 
62  [[nodiscard]] void* allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view)
63  {
64  return do_allocate(bytes, alignment);
65  }
66 
76  [[nodiscard]] void* allocate_async(std::size_t bytes, cuda_stream_view)
77  {
78  return do_allocate(bytes);
79  }
80 
89  void deallocate_async(void* ptr, std::size_t bytes, std::size_t alignment, cuda_stream_view)
90  {
91  do_deallocate(ptr, rmm::align_up(bytes, alignment));
92  }
93 
99  friend void get_property(pinned_memory_resource const&, cuda::mr::device_accessible) noexcept {}
100 
101  private
114  void* do_allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) override
115  {
116  // don't allocate anything if the user requested zero bytes
117  if (0 == bytes) { return nullptr; }
118 
119  // If the requested alignment isn't supported, use default
120  alignment =
122 
123  return rmm::detail::aligned_host_allocate(bytes, alignment, [](std::size_t size) {
124  void* ptr{nullptr};
125  RMM_CUDA_TRY_ALLOC(cudaMallocHost(&ptr, size), size);
126  return ptr;
127  });
128  }
129 
143  void do_deallocate(void* ptr,
144  std::size_t bytes,
145  std::size_t alignment = alignof(std::max_align_t)) override
146  {
147  if (nullptr == ptr) { return; }
148  rmm::detail::aligned_host_deallocate(
149  ptr, bytes, alignment, [](void* ptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeHost(ptr)); });
150  }
151 };
152 static_assert(cuda::mr::async_resource_with<pinned_memory_resource,
153  cuda::mr::host_accessible,
154  cuda::mr::device_accessible>); // end of group
156 } // namespace mr
157 } // namespace RMM_NAMESPACE
具有默认构造函数的、强类型、非拥有型的 CUDA 流包装器。
定义: cuda_stream_view.hpp:39
用于主机内存分配的基类。
定义: host_memory_resource.hpp:56
使用 cudaMallocHost 分配锁定/页锁定主机内存的 host_memory_resource。
定义: pinned_memory_resource.hpp:41
friend void get_property(pinned_memory_resource const &, cuda::mr::device_accessible) noexcept
启用 cuda::mr::device_accessible 属性。
定义: pinned_memory_resource.hpp:99
void * allocate_async(std::size_t bytes, cuda_stream_view)
假装支持 allocate_async 接口,实际回退到流 0。
定义: pinned_memory_resource.hpp:76
pinned_memory_resource & operator=(pinned_memory_resource const &)=default
默认拷贝赋值运算符。
pinned_memory_resource(pinned_memory_resource &&)=default
默认移动构造函数。
pinned_memory_resource & operator=(pinned_memory_resource &&)=default
默认移动赋值运算符。
void * allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view)
假装支持 allocate_async 接口,实际回退到流 0。
定义: pinned_memory_resource.hpp:62
void deallocate_async(void *ptr, std::size_t bytes, std::size_t alignment, cuda_stream_view)
假装支持 deallocate_async 接口,实际回退到流 0。
定义: pinned_memory_resource.hpp:89
pinned_memory_resource(pinned_memory_resource const &)=default
默认拷贝构造函数。
static constexpr std::size_t RMM_DEFAULT_HOST_ALIGNMENT
RMM 分配主机内存时使用的默认对齐。
定义: aligned.hpp:37
constexpr bool is_supported_alignment(std::size_t alignment) noexcept
返回对齐是否是有效的内存对齐。
定义: aligned.hpp:64
constexpr std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
向上对齐到指定 2 的幂的最近倍数。
定义: aligned.hpp:77