rmm_resource_adaptor.hpp
1 
6 #pragma once
7 
8 #include <cstddef>
9 #include <mutex>
10 #include <optional>
11 #include <stack>
12 #include <thread>
13 #include <unordered_map>
14 #include <unordered_set>
15 #include <utility>
16 
17 #include <rmm/error.hpp>
19 #include <rmm/resource_ref.hpp>
20 
21 #include <rapidsmpf/memory/scoped_memory_record.hpp>
22 
23 namespace rapidsmpf {
24 
33  public:
42  std::optional<rmm::device_async_resource_ref> fallback_mr = std::nullopt
43  )
44  : primary_mr_{std::move(primary_mr)}, fallback_mr_{std::move(fallback_mr)} {}
45 
46  RmmResourceAdaptor() = delete;
47  ~RmmResourceAdaptor() override = default;
48 
54  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept {
55  return primary_mr_;
56  }
57 
65  [[nodiscard]] std::optional<rmm::device_async_resource_ref>
66  get_fallback_resource() const noexcept {
67  return fallback_mr_;
68  }
69 
77  [[nodiscard]] ScopedMemoryRecord get_main_record() const;
78 
84  [[nodiscard]] std::int64_t current_allocated() const noexcept;
85 
86 
98 
122 
123  private:
137  void* do_allocate(std::size_t bytes, rmm::cuda_stream_view stream) override;
138 
146  void do_deallocate(
147  void* ptr, std::size_t bytes, rmm::cuda_stream_view stream
148  ) noexcept override;
149 
158  [[nodiscard]] bool do_is_equal(
159  rmm::mr::device_memory_resource const& other
160  ) const noexcept override;
161 
162  mutable std::mutex mutex_;
163  rmm::device_async_resource_ref primary_mr_;
164  std::optional<rmm::device_async_resource_ref> fallback_mr_;
165  std::unordered_set<void*> fallback_allocations_;
166 
168  ScopedMemoryRecord main_record_;
170  std::unordered_map<std::thread::id, std::stack<ScopedMemoryRecord>> record_stacks_;
172  std::unordered_map<void*, std::thread::id> allocating_threads_;
173 };
174 
175 
176 } // namespace rapidsmpf
A RMM memory resource adaptor tailored to RapidsMPF.
RmmResourceAdaptor(rmm::device_async_resource_ref primary_mr, std::optional< rmm::device_async_resource_ref > fallback_mr=std::nullopt)
Construct with specified primary and optional fallback memory resource.
void begin_scoped_memory_record()
Begin recording a new scoped memory usage record for the current thread.
ScopedMemoryRecord get_main_record() const
Returns a copy of the main memory record.
std::int64_t current_allocated() const noexcept
Get the total current allocated memory from both primary and fallback.
rmm::device_async_resource_ref get_upstream_resource() const noexcept
Get a reference to the primary upstream resource.
std::optional< rmm::device_async_resource_ref > get_fallback_resource() const noexcept
Get a reference to the fallback upstream resource.
ScopedMemoryRecord end_scoped_memory_record()
End the current scoped memory record and return it.
device_memory_resource(device_memory_resource const &)=default
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::device_accessible > > device_async_resource_ref
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:13
Memory statistics for a specific scope.