All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
arena_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2024, 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 #pragma once
17 
18 #include <rmm/aligned.hpp>
19 #include <rmm/detail/error.hpp>
20 #include <rmm/detail/export.hpp>
21 #include <rmm/detail/format.hpp>
22 #include <rmm/detail/logging_assert.hpp>
23 #include <rmm/logger.hpp>
24 #include <rmm/mr/device/detail/arena.hpp>
26 #include <rmm/resource_ref.hpp>
27 
28 #include <cuda_runtime_api.h>
29 
30 #include <cstddef>
31 #include <map>
32 #include <shared_mutex>
33 #include <thread>
34 
35 namespace RMM_NAMESPACE {
36 namespace mr {
82 template <typename Upstream>
84  public:
94  std::optional<std::size_t> arena_size = std::nullopt,
95  bool dump_log_on_failure = false)
96  : global_arena_{upstream_mr, arena_size}, dump_log_on_failure_{dump_log_on_failure}
97  {
98  if (dump_log_on_failure_) {
99  logger_ = std::make_shared<logger>("arena_memory_dump", "rmm_arena_memory_dump.log");
100  // Set the level to `debug` for more detailed output.
101  logger_->set_level(level_enum::info);
102  }
103  }
104 
115  explicit arena_memory_resource(Upstream* upstream_mr,
116  std::optional<std::size_t> arena_size = std::nullopt,
117  bool dump_log_on_failure = false)
119  to_device_async_resource_ref_checked(upstream_mr), arena_size, dump_log_on_failure}
120  {
121  }
122 
123  ~arena_memory_resource() override = default;
124 
125  // Disable copy (and move) semantics.
127  arena_memory_resource& operator=(arena_memory_resource const&) = delete;
128  arena_memory_resource(arena_memory_resource&&) noexcept = delete;
129  arena_memory_resource& operator=(arena_memory_resource&&) noexcept = delete;
130 
131  private:
132  using global_arena = rmm::mr::detail::arena::global_arena;
133  using arena = rmm::mr::detail::arena::arena;
134 
146  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
147  {
148  if (bytes <= 0) { return nullptr; }
149 #ifdef RMM_ARENA_USE_SIZE_CLASSES
150  bytes = rmm::mr::detail::arena::align_to_size_class(bytes);
151 #else
153 #endif
154  auto& arena = get_arena(stream);
155 
156  {
157  std::shared_lock lock(mtx_);
158  void* pointer = arena.allocate(bytes);
159  if (pointer != nullptr) { return pointer; }
160  }
161 
162  {
163  std::unique_lock lock(mtx_);
164  defragment();
165  void* pointer = arena.allocate(bytes);
166  if (pointer == nullptr) {
167  if (dump_log_on_failure_) { dump_memory_log(bytes); }
168  RMM_FAIL("Maximum pool size exceeded", rmm::out_of_memory);
169  }
170  return pointer;
171  }
172  }
173 
177  void defragment()
178  {
179  RMM_CUDA_TRY(cudaDeviceSynchronize());
180  for (auto& thread_arena : thread_arenas_) {
181  thread_arena.second->clean();
182  }
183  for (auto& stream_arena : stream_arenas_) {
184  stream_arena.second.clean();
185  }
186  }
187 
196  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
197  {
198  if (ptr == nullptr || bytes <= 0) { return; }
199 #ifdef RMM_ARENA_USE_SIZE_CLASSES
200  bytes = rmm::mr::detail::arena::align_to_size_class(bytes);
201 #else
203 #endif
204  auto& arena = get_arena(stream);
205 
206  {
207  std::shared_lock lock(mtx_);
208  // If the memory being freed does not belong to the arena, the following will return false.
209  if (arena.deallocate(ptr, bytes, stream)) { return; }
210  }
211 
212  {
213  // Since we are returning this memory to another stream, we need to make sure the current
214  // stream is caught up.
215  stream.synchronize_no_throw();
216 
217  std::unique_lock lock(mtx_);
218  deallocate_from_other_arena(ptr, bytes, stream);
219  }
220  }
221 
230  void deallocate_from_other_arena(void* ptr, std::size_t bytes, cuda_stream_view stream)
231  {
232  if (use_per_thread_arena(stream)) {
233  for (auto const& thread_arena : thread_arenas_) {
234  if (thread_arena.second->deallocate(ptr, bytes)) { return; }
235  }
236  } else {
237  for (auto& stream_arena : stream_arenas_) {
238  if (stream_arena.second.deallocate(ptr, bytes)) { return; }
239  }
240  }
241 
242  if (!global_arena_.deallocate(ptr, bytes)) {
243  // It's possible to use per thread default streams along with another pool of streams.
244  // This means that it's possible for an allocation to move from a thread or stream arena
245  // back into the global arena during a defragmentation and then move down into another arena
246  // type. For instance, thread arena -> global arena -> stream arena. If this happens and
247  // there was an allocation from it while it was a thread arena, we now have to check to
248  // see if the allocation is part of a stream arena, and vice versa.
249  // Only do this in exceptional cases to not affect performance and have to check all
250  // arenas all the time.
251  if (use_per_thread_arena(stream)) {
252  for (auto& stream_arena : stream_arenas_) {
253  if (stream_arena.second.deallocate(ptr, bytes)) { return; }
254  }
255  } else {
256  for (auto const& thread_arena : thread_arenas_) {
257  if (thread_arena.second->deallocate(ptr, bytes)) { return; }
258  }
259  }
260  RMM_FAIL("allocation not found");
261  }
262  }
263 
270  arena& get_arena(cuda_stream_view stream)
271  {
272  if (use_per_thread_arena(stream)) { return get_thread_arena(); }
273  return get_stream_arena(stream);
274  }
275 
281  arena& get_thread_arena()
282  {
283  auto const thread_id = std::this_thread::get_id();
284  {
285  std::shared_lock lock(map_mtx_);
286  auto const iter = thread_arenas_.find(thread_id);
287  if (iter != thread_arenas_.end()) { return *iter->second; }
288  }
289  {
290  std::unique_lock lock(map_mtx_);
291  auto thread_arena = std::make_shared<arena>(global_arena_);
292  thread_arenas_.emplace(thread_id, thread_arena);
293  thread_local detail::arena::arena_cleaner cleaner{thread_arena};
294  return *thread_arena;
295  }
296  }
297 
303  arena& get_stream_arena(cuda_stream_view stream)
304  {
305  RMM_LOGGING_ASSERT(!use_per_thread_arena(stream));
306  {
307  std::shared_lock lock(map_mtx_);
308  auto const iter = stream_arenas_.find(stream.value());
309  if (iter != stream_arenas_.end()) { return iter->second; }
310  }
311  {
312  std::unique_lock lock(map_mtx_);
313  stream_arenas_.emplace(stream.value(), global_arena_);
314  return stream_arenas_.at(stream.value());
315  }
316  }
317 
323  void dump_memory_log(size_t bytes)
324  {
325  logger_->info("**************************************************");
326  logger_->info("Ran out of memory trying to allocate %s.", rmm::detail::format_bytes(bytes));
327  logger_->info("**************************************************");
328  logger_->info("Global arena:");
329  global_arena_.dump_memory_log(logger_);
330  logger_->flush();
331  }
332 
339  static bool use_per_thread_arena(cuda_stream_view stream)
340  {
341  return stream.is_per_thread_default();
342  }
343 
345  global_arena global_arena_;
348  std::map<std::thread::id, std::shared_ptr<arena>> thread_arenas_;
351  std::map<cudaStream_t, arena> stream_arenas_;
353  bool dump_log_on_failure_{};
355  std::shared_ptr<logger> logger_{};
357  mutable std::shared_mutex map_mtx_;
359  mutable std::shared_mutex mtx_;
360 };
361  // end of group
363 } // namespace mr
364 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
A suballocator that emphasizes fragmentation avoidance and scalable concurrency support.
Definition: arena_memory_resource.hpp:83
arena_memory_resource(Upstream *upstream_mr, std::optional< std::size_t > arena_size=std::nullopt, bool dump_log_on_failure=false)
Construct an arena_memory_resource.
Definition: arena_memory_resource.hpp:115
arena_memory_resource(device_async_resource_ref upstream_mr, std::optional< std::size_t > arena_size=std::nullopt, bool dump_log_on_failure=false)
Construct an arena_memory_resource.
Definition: arena_memory_resource.hpp:93
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:94
Exception thrown when RMM runs out of memory.
Definition: error.hpp:87
cuda::mr::async_resource_ref< cuda::mr::device_accessible > device_async_resource_ref
Alias for a cuda::mr::async_resource_ref with the property cuda::mr::device_accessible.
Definition: resource_ref.hpp:41
device_async_resource_ref to_device_async_resource_ref_checked(Resource *res)
Convert pointer to memory resource into device_async_resource_ref, checking for nullptr
Definition: resource_ref.hpp:79
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:43
constexpr std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
Align up to nearest multiple of specified power of 2.
Definition: aligned.hpp:77