managed_memory_resource.hpp
转到此文件的文档。
1 /*
2  * Copyright (c) 2019-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/cuda_stream_view.hpp>
19 #include <rmm/detail/error.hpp>
20 #include <rmm/detail/export.hpp>
22 
23 #include <cstddef>
24 
25 namespace RMM_NAMESPACE {
26 namespace mr {
37  public
38  managed_memory_resource() = default;
39  ~managed_memory_resource() override = default;
43  default;
45  default;
46 
47  private
59  void* do_allocate(std::size_t bytes, [[maybe_unused]] cuda_stream_view stream) override
60  {
61  // FIXME: Unlike cudaMalloc, cudaMallocManaged will throw an error for 0
62  // size allocations.
63  if (bytes == 0) { return nullptr; }
64 
65  void* ptr{nullptr};
66  RMM_CUDA_TRY_ALLOC(cudaMallocManaged(&ptr, bytes), bytes);
67  return ptr;
68  }
69 
80  void do_deallocate(void* ptr,
81  [[maybe_unused]] std::size_t bytes,
82  [[maybe_unused]] cuda_stream_view stream) override
83  {
84  RMM_ASSERT_CUDA_SUCCESS(cudaFree(ptr));
85  }
86 
97  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
98  {
99  return dynamic_cast<managed_memory_resource const*>(&other) != nullptr;
100  }
101 };
102  // end of group
104 } // namespace mr
105 } // namespace RMM_NAMESPACE
用于 CUDA 流的强类型非拥有封装器,带默认构造函数。
定义: cuda_stream_view.hpp:39
所有 librmm 设备内存分配的基类。
定义: device_memory_resource.hpp:92
使用 cudaMallocManaged/Free 进行分配/解除分配的 device_memory_resource 派生类。
定义: managed_memory_resource.hpp:36
managed_memory_resource(managed_memory_resource &&)=default
默认移动构造函数。
managed_memory_resource & operator=(managed_memory_resource &&)=default
默认移动赋值运算符。
managed_memory_resource & operator=(managed_memory_resource const &)=default
默认复制赋值运算符。
managed_memory_resource(managed_memory_resource const &)=default
默认复制构造函数。