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 
110 {
111  std::lock_guard<std::mutex> lock{detail::ref_map_lock()};
112  auto& map = detail::get_ref_map();
113  // If a resource was never set for `id`, set to the initial resource
114  auto const found = map.find(device_id.value());
115  if (found == map.end()) {
116  auto item = map.emplace(device_id.value(), detail::initial_resource());
117  return device_async_resource_ref{item.first->second};
118  }
119  return device_async_resource_ref{found->second};
120 }
121 
150 inline cuda::mr::any_resource<cuda::mr::device_accessible> set_per_device_resource(
151  cuda_device_id device_id, cuda::mr::any_resource<cuda::mr::device_accessible> new_resource)
152 {
153  std::lock_guard<std::mutex> lock{detail::ref_map_lock()};
154  auto& map = detail::get_ref_map();
155  auto const old_itr = map.find(device_id.value());
156  if (old_itr == map.end()) {
157  map.emplace(device_id.value(), std::move(new_resource));
158  return {detail::initial_resource()};
159  }
160  return std::exchange(old_itr->second, std::move(new_resource));
161 }
162 
193 [[deprecated("Use set_per_device_resource instead.")]] //
194 inline cuda::mr::any_resource<cuda::mr::device_accessible>
196 {
198  device_id, cuda::mr::any_resource<cuda::mr::device_accessible>{new_resource_ref});
199 }
200 
224 {
226 }
227 
251 inline cuda::mr::any_resource<cuda::mr::device_accessible> set_current_device_resource(
252  cuda::mr::any_resource<cuda::mr::device_accessible> new_resource)
253 {
254  return set_per_device_resource(rmm::get_current_cuda_device(), std::move(new_resource));
255 }
256 
284 [[deprecated("Use set_current_device_resource instead.")]] //
285 inline cuda::mr::any_resource<cuda::mr::device_accessible>
287 {
289  cuda::mr::any_resource<cuda::mr::device_accessible>{new_resource_ref});
290 }
291 
309 inline cuda::mr::any_resource<cuda::mr::device_accessible> reset_per_device_resource(
310  cuda_device_id device_id)
311 {
312  return set_per_device_resource(device_id, {detail::initial_resource()});
313 }
314 
329 inline cuda::mr::any_resource<cuda::mr::device_accessible> reset_current_device_resource()
330 {
332 }
333 
353 [[deprecated("Use reset_per_device_resource instead.")]] //
354 inline cuda::mr::any_resource<cuda::mr::device_accessible>
356 {
357  return reset_per_device_resource(device_id);
358 }
359 
376 [[deprecated("Use reset_current_device_resource instead.")]] //
377 inline cuda::mr::any_resource<cuda::mr::device_accessible>
379 {
381 } // end of group
383 } // namespace mr
384 } // namespace RMM_NAMESPACE
Memory resource that uses cudaMalloc/Free for allocation/deallocation.
Definition: cuda_memory_resource.hpp:26
cuda_device_id get_current_cuda_device()
Returns a cuda_device_id for the current device.
cuda::mr::any_resource< cuda::mr::device_accessible > set_current_device_resource_ref(device_async_resource_ref new_resource_ref)
Set the device_async_resource_ref for the current device.
Definition: per_device_resource.hpp:286
device_async_resource_ref get_current_device_resource_ref()
Get the device_async_resource_ref for the current device.
Definition: per_device_resource.hpp:223
cuda::mr::any_resource< cuda::mr::device_accessible > set_per_device_resource_ref(cuda_device_id device_id, device_async_resource_ref new_resource_ref)
Set the device_async_resource_ref for the specified device to new_resource_ref
Definition: per_device_resource.hpp:195
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:109
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:329
cuda::mr::any_resource< cuda::mr::device_accessible > reset_per_device_resource_ref(cuda_device_id device_id)
Reset the device_async_resource_ref for the specified device to the initial resource.
Definition: per_device_resource.hpp:355
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:150
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:251
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:309
cuda::mr::any_resource< cuda::mr::device_accessible > reset_current_device_resource_ref()
Reset the device_async_resource_ref for the current device to the initial resource.
Definition: per_device_resource.hpp:378
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