RMM  23.12
RAPIDS Memory Manager
per_device_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2021, 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  * http://www.apache.org/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 
17 #pragma once
18 
19 #include <rmm/cuda_device.hpp>
20 #include <rmm/detail/export.hpp>
23 
24 #include <map>
25 #include <mutex>
26 
74 namespace rmm::mr {
80 namespace detail {
81 
90 {
91  static cuda_memory_resource mr{};
92  return &mr;
93 }
94 
98 inline std::mutex& map_lock()
99 {
100  static std::mutex map_lock;
101  return map_lock;
102 }
103 
104 // This symbol must have default visibility, see: https://github.com/rapidsai/rmm/issues/826
108 RMM_EXPORT inline auto& get_map()
109 {
110  static std::map<cuda_device_id::value_type, device_memory_resource*> device_id_to_resource;
111  return device_id_to_resource;
112 }
113 
114 } // namespace detail
115 
138 {
139  std::lock_guard<std::mutex> lock{detail::map_lock()};
140  auto& map = detail::get_map();
141  // If a resource was never set for `id`, set to the initial resource
142  auto const found = map.find(device_id.value());
143  return (found == map.end()) ? (map[device_id.value()] = detail::initial_resource())
144  : found->second;
145 }
146 
175  device_memory_resource* new_mr)
176 {
177  std::lock_guard<std::mutex> lock{detail::map_lock()};
178  auto& map = detail::get_map();
179  auto const old_itr = map.find(device_id.value());
180  // If a resource didn't previously exist for `id`, return pointer to initial_resource
181  auto* old_mr = (old_itr == map.end()) ? detail::initial_resource() : old_itr->second;
182  map[device_id.value()] = (new_mr == nullptr) ? detail::initial_resource() : new_mr;
183  return old_mr;
184 }
185 
208 {
210 }
211 
237 {
239 } // end of group
241 } // namespace rmm::mr
device_memory_resource derived class that uses cudaMalloc/Free for allocation/deallocation.
Definition: cuda_memory_resource.hpp:35
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:89
cuda_device_id get_current_cuda_device()
Returns a cuda_device_id for the current device.
Definition: cuda_device.hpp:86
device_memory_resource * set_current_device_resource(device_memory_resource *new_mr)
Set the memory resource for the current device.
Definition: per_device_resource.hpp:236
device_memory_resource * get_current_device_resource()
Get the memory resource for the current device.
Definition: per_device_resource.hpp:207
device_memory_resource * set_per_device_resource(cuda_device_id device_id, device_memory_resource *new_mr)
Set the device_memory_resource for the specified device.
Definition: per_device_resource.hpp:174
device_memory_resource * get_per_device_resource(cuda_device_id device_id)
Get the resource for the specified device.
Definition: per_device_resource.hpp:137
std::mutex & map_lock()
Reference to the lock.
Definition: per_device_resource.hpp:98
device_memory_resource * initial_resource()
Returns a pointer to the initial resource.
Definition: per_device_resource.hpp:89
RMM_EXPORT auto & get_map()
Reference to the map from device id -> resource.
Definition: per_device_resource.hpp:108
Strong type for a CUDA device identifier.
Definition: cuda_device.hpp:33
constexpr value_type value() const noexcept
The wrapped integer value.
Definition: cuda_device.hpp:44