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>
28 #include <cuda_runtime_api.h>
32 #include <shared_mutex>
35 namespace RMM_NAMESPACE {
82 template <
typename Upstream>
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}
98 if (dump_log_on_failure_) {
99 logger_ = std::make_shared<logger>(
"arena_memory_dump",
"rmm_arena_memory_dump.log");
101 logger_->set_level(level_enum::info);
116 std::optional<std::size_t> arena_size = std::nullopt,
117 bool dump_log_on_failure =
false)
132 using global_arena = rmm::mr::detail::arena::global_arena;
133 using arena = rmm::mr::detail::arena::arena;
148 if (bytes <= 0) {
return nullptr; }
149 #ifdef RMM_ARENA_USE_SIZE_CLASSES
150 bytes = rmm::mr::detail::arena::align_to_size_class(bytes);
154 auto& arena = get_arena(stream);
157 std::shared_lock lock(mtx_);
158 void* pointer = arena.allocate(bytes);
159 if (pointer !=
nullptr) {
return pointer; }
163 std::unique_lock lock(mtx_);
165 void* pointer = arena.allocate(bytes);
166 if (pointer ==
nullptr) {
167 if (dump_log_on_failure_) { dump_memory_log(bytes); }
179 RMM_CUDA_TRY(cudaDeviceSynchronize());
180 for (
auto& thread_arena : thread_arenas_) {
181 thread_arena.second->clean();
183 for (
auto& stream_arena : stream_arenas_) {
184 stream_arena.second.clean();
196 void do_deallocate(
void* ptr, std::size_t bytes, cuda_stream_view stream)
override
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);
204 auto& arena = get_arena(stream);
207 std::shared_lock lock(mtx_);
209 if (arena.deallocate(ptr, bytes, stream)) {
return; }
215 stream.synchronize_no_throw();
217 std::unique_lock lock(mtx_);
218 deallocate_from_other_arena(ptr, bytes, stream);
230 void deallocate_from_other_arena(
void* ptr, std::size_t bytes, cuda_stream_view stream)
232 if (use_per_thread_arena(stream)) {
233 for (
auto const& thread_arena : thread_arenas_) {
234 if (thread_arena.second->deallocate(ptr, bytes)) {
return; }
237 for (
auto& stream_arena : stream_arenas_) {
238 if (stream_arena.second.deallocate(ptr, bytes)) {
return; }
242 if (!global_arena_.deallocate(ptr, bytes)) {
251 if (use_per_thread_arena(stream)) {
252 for (
auto& stream_arena : stream_arenas_) {
253 if (stream_arena.second.deallocate(ptr, bytes)) {
return; }
256 for (
auto const& thread_arena : thread_arenas_) {
257 if (thread_arena.second->deallocate(ptr, bytes)) {
return; }
260 RMM_FAIL(
"allocation not found");
270 arena& get_arena(cuda_stream_view stream)
272 if (use_per_thread_arena(stream)) {
return get_thread_arena(); }
273 return get_stream_arena(stream);
281 arena& get_thread_arena()
283 auto const thread_id = std::this_thread::get_id();
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; }
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;
303 arena& get_stream_arena(cuda_stream_view stream)
305 RMM_LOGGING_ASSERT(!use_per_thread_arena(stream));
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; }
312 std::unique_lock lock(map_mtx_);
313 stream_arenas_.emplace(stream.value(), global_arena_);
314 return stream_arenas_.at(stream.value());
323 void dump_memory_log(
size_t bytes)
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_);
339 static bool use_per_thread_arena(cuda_stream_view stream)
341 return stream.is_per_thread_default();
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_;
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