per_device_resource.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2026, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <rmm/cuda_device.hpp>
9 #include <rmm/detail/export.hpp>
10 #include <rmm/detail/runtime_shutdown.hpp>
12 #include <rmm/resource_ref.hpp>
13 
14 #include <cuda/memory_resource>
15 
16 #include <map>
17 #include <mutex>
18 #include <utility>
19 
32 namespace RMM_NAMESPACE {
33 namespace mr {
39 namespace detail {
40 
41 // These symbols must have default visibility so that when they are
42 // referenced in multiple different DSOs the linker correctly
43 // determines that there is only a single unique reference to the
44 // function symbols (and hence they return unique static references
45 // across different DSOs). See also
46 // https://github.com/rapidsai/rmm/issues/826
47 // Although currently the entire RMM namespace is RMM_EXPORT, we
48 // explicitly mark these functions as exported in case the namespace
49 // export changes.
58 {
59  static cuda_memory_resource mr{};
60  return mr;
61 }
62 
66 RMM_EXPORT inline std::mutex& ref_map_lock()
67 {
68  static std::mutex ref_map_lock;
69  return ref_map_lock;
70 }
71 
72 // This symbol must have default visibility, see: https://github.com/rapidsai/rmm/issues/826
76 RMM_EXPORT inline auto& get_ref_map()
77 {
78  static std::map<cuda_device_id::value_type, cuda::mr::any_resource<cuda::mr::device_accessible>>
79  device_id_to_resource;
80  // Register the process-exit hook immediately after constructing the map.
81  rmm::detail::register_process_exit_hook();
82  return device_id_to_resource;
83 }
84 
85 } // namespace detail
86 
111 {
112  std::lock_guard<std::mutex> lock{detail::ref_map_lock()};
113  auto& map = detail::get_ref_map();
114  // If a resource was never set for `id`, set to the initial resource
115  auto const found = map.find(device_id.value());
116  if (found == map.end()) {
117  auto item = map.emplace(device_id.value(), detail::initial_resource());
118  return device_async_resource_ref{item.first->second};
119  }
120  return device_async_resource_ref{found->second};
121 }
122 
151 inline cuda::mr::any_resource<cuda::mr::device_accessible> set_per_device_resource(
152  cuda_device_id device_id, cuda::mr::any_resource<cuda::mr::device_accessible> new_resource)
153 {
154  std::lock_guard<std::mutex> lock{detail::ref_map_lock()};
155  auto& map = detail::get_ref_map();
156  auto const old_itr = map.find(device_id.value());
157  if (old_itr == map.end()) {
158  map.emplace(device_id.value(), std::move(new_resource));
159  return {detail::initial_resource()};
160  }
161  return std::exchange(old_itr->second, std::move(new_resource));
162 }
163 
188 {
190 }
191 
215 inline cuda::mr::any_resource<cuda::mr::device_accessible> set_current_device_resource(
216  cuda::mr::any_resource<cuda::mr::device_accessible> new_resource)
217 {
218  return set_per_device_resource(rmm::get_current_cuda_device(), std::move(new_resource));
219 }
220 
238 inline cuda::mr::any_resource<cuda::mr::device_accessible> reset_per_device_resource(
239  cuda_device_id device_id)
240 {
241  return set_per_device_resource(device_id, {detail::initial_resource()});
242 }
243 
258 inline cuda::mr::any_resource<cuda::mr::device_accessible> reset_current_device_resource()
259 {
261 }
262  // end of group
264 } // namespace mr
265 } // namespace RMM_NAMESPACE
Memory resource that uses cudaMalloc/Free for allocation/deallocation.
Definition: cuda_memory_resource.hpp:25
cuda_device_id get_current_cuda_device()
Returns a cuda_device_id for the current device.
device_async_resource_ref get_current_device_resource_ref()
Get the device_async_resource_ref for the current device.
Definition: per_device_resource.hpp:187
device_async_resource_ref get_per_device_resource_ref(cuda_device_id device_id)
Get the device_async_resource_ref for the specified device.
Definition: per_device_resource.hpp:110
cuda::mr::any_resource< cuda::mr::device_accessible > reset_current_device_resource()
Reset the memory resource for the current device to the initial resource.
Definition: per_device_resource.hpp:258
cuda::mr::any_resource< cuda::mr::device_accessible > set_per_device_resource(cuda_device_id device_id, cuda::mr::any_resource< cuda::mr::device_accessible > new_resource)
Set the memory resource for the specified device.
Definition: per_device_resource.hpp:151
cuda::mr::resource_ref< cuda::mr::device_accessible > device_async_resource_ref
Alias for a cuda::mr::resource_ref with the property cuda::mr::device_accessible.
Definition: resource_ref.hpp:30
cuda::mr::any_resource< cuda::mr::device_accessible > set_current_device_resource(cuda::mr::any_resource< cuda::mr::device_accessible > new_resource)
Set the memory resource for the current device.
Definition: per_device_resource.hpp:215
cuda::mr::any_resource< cuda::mr::device_accessible > reset_per_device_resource(cuda_device_id device_id)
Reset the memory resource for the specified device to the initial resource.
Definition: per_device_resource.hpp:238
auto & get_ref_map()
Reference to the map from device id -> any_resource.
Definition: per_device_resource.hpp:76
std::mutex & ref_map_lock()
Reference to the lock.
Definition: per_device_resource.hpp:66
cuda_memory_resource & initial_resource()
Returns a reference to the initial resource.
Definition: per_device_resource.hpp:57
Strong type for a CUDA device identifier.
Definition: cuda_device.hpp:27
constexpr value_type value() const noexcept
The wrapped integer value.
Definition: cuda_device.hpp:43