statistics_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/detail/export.hpp>
10 #include <rmm/resource_ref.hpp>
11 
12 #include <cstddef>
13 #include <mutex>
14 #include <shared_mutex>
15 #include <stack>
16 
17 namespace RMM_NAMESPACE {
18 namespace mr {
46 template <typename Upstream>
48  public:
49  using read_lock_t =
50  std::shared_lock<std::shared_mutex>;
51  using write_lock_t =
52  std::unique_lock<std::shared_mutex>;
56  struct counter {
57  int64_t value{0};
58  int64_t peak{0};
59  int64_t total{0};
60 
67  counter& operator+=(int64_t val)
68  {
69  value += val;
70  total += val;
71  peak = std::max(value, peak);
72  return *this;
73  }
74 
81  counter& operator-=(int64_t val)
82  {
83  value -= val;
84  return *this;
85  }
86 
99  {
100  peak = std::max(value + val.peak, peak);
101  value += val.value;
102  total += val.total;
103  }
104  };
105 
112  statistics_resource_adaptor(device_async_resource_ref upstream) : upstream_{upstream} {}
113 
122  statistics_resource_adaptor(Upstream* upstream)
123  : upstream_{to_device_async_resource_ref_checked(upstream)}
124  {
125  }
126 
127  statistics_resource_adaptor() = delete;
128  ~statistics_resource_adaptor() override = default;
130  statistics_resource_adaptor& operator=(statistics_resource_adaptor const&) = delete;
132  default;
134  default;
135 
139  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
140  {
141  return upstream_;
142  }
143 
151  counter get_bytes_counter() const noexcept
152  {
153  read_lock_t lock(mtx_);
154 
155  return counter_stack_.top().first;
156  }
157 
166  {
167  read_lock_t lock(mtx_);
168 
169  return counter_stack_.top().second;
170  }
171 
179  std::pair<counter, counter> push_counters()
180  {
181  write_lock_t lock(mtx_);
182  auto ret = counter_stack_.top();
183  counter_stack_.push({counter{}, counter{}});
184  return ret;
185  }
186 
194  std::pair<counter, counter> pop_counters()
195  {
196  write_lock_t lock(mtx_);
197  if (counter_stack_.size() < 2) { throw std::out_of_range("cannot pop the last counter pair"); }
198  auto ret = counter_stack_.top();
199  counter_stack_.pop();
200  // Update the new top pair of counters
201  counter_stack_.top().first.add_counters_from_tracked_sub_block(ret.first);
202  counter_stack_.top().second.add_counters_from_tracked_sub_block(ret.second);
203  return ret;
204  }
205 
206  private:
220  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
221  {
222  void* ptr = get_upstream_resource().allocate(stream, bytes);
223 
224  // increment the stats
225  {
226  write_lock_t lock(mtx_);
227 
228  // Increment the allocation_count_ while we have the lock
229  counter_stack_.top().first += bytes;
230  counter_stack_.top().second += 1;
231  }
232 
233  return ptr;
234  }
235 
243  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
244  {
245  get_upstream_resource().deallocate(stream, ptr, bytes);
246 
247  {
248  write_lock_t lock(mtx_);
249 
250  // Decrement the current allocated counts.
251  counter_stack_.top().first -= bytes;
252  counter_stack_.top().second -= 1;
253  }
254  }
255 
263  bool do_is_equal(device_memory_resource const& other) const noexcept override
264  {
265  if (this == &other) { return true; }
266  auto cast = dynamic_cast<statistics_resource_adaptor<Upstream> const*>(&other);
267  if (cast == nullptr) { return false; }
268  return get_upstream_resource() == cast->get_upstream_resource();
269  }
270 
271  // Stack of counter pairs <bytes, allocations>
272  // Invariant: the stack always contains at least one entry
273  std::stack<std::pair<counter, counter>> counter_stack_{{std::make_pair(counter{}, counter{})}};
274  std::shared_mutex mutable mtx_; // mutex for thread safe access to allocations_
275  // the upstream resource used for satisfying allocation requests
276  device_async_resource_ref upstream_;
277 };
278  // end of group
280 } // namespace mr
281 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:83
Resource that uses Upstream to allocate memory and tracks statistics on memory allocations.
Definition: statistics_resource_adaptor.hpp:47
statistics_resource_adaptor(device_async_resource_ref upstream)
Construct a new statistics resource adaptor using upstream to satisfy allocation requests.
Definition: statistics_resource_adaptor.hpp:112
statistics_resource_adaptor(statistics_resource_adaptor &&) noexcept=default
Default move constructor.
std::pair< counter, counter > push_counters()
Push a pair of zero counters on the stack, which becomes the new counters returned by get_bytes_count...
Definition: statistics_resource_adaptor.hpp:179
std::unique_lock< std::shared_mutex > write_lock_t
Type of lock used to synchronize write access.
Definition: statistics_resource_adaptor.hpp:52
statistics_resource_adaptor(Upstream *upstream)
Construct a new statistics resource adaptor using upstream to satisfy allocation requests.
Definition: statistics_resource_adaptor.hpp:122
std::shared_lock< std::shared_mutex > read_lock_t
Type of lock used to synchronize read access.
Definition: statistics_resource_adaptor.hpp:50
std::pair< counter, counter > pop_counters()
Pop a pair of counters from the stack.
Definition: statistics_resource_adaptor.hpp:194
counter get_allocations_counter() const noexcept
Returns a counter struct for this adaptor containing the current, peak, and total number of allocatio...
Definition: statistics_resource_adaptor.hpp:165
counter get_bytes_counter() const noexcept
Returns a counter struct for this adaptor containing the current, peak, and total number of allocated...
Definition: statistics_resource_adaptor.hpp:151
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:72
detail::cccl_async_resource_ref< cuda::mr::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:32
Management of per-device device_memory_resources.
Utility struct for counting the current, peak, and total value of a number.
Definition: statistics_resource_adaptor.hpp:56
counter & operator-=(int64_t val)
Subtract val from the current value and update the peak value if necessary.
Definition: statistics_resource_adaptor.hpp:81
int64_t value
Current value.
Definition: statistics_resource_adaptor.hpp:57
int64_t peak
Max value of value
Definition: statistics_resource_adaptor.hpp:58
void add_counters_from_tracked_sub_block(const counter &val)
Add val to the current value and update the peak value if necessary.
Definition: statistics_resource_adaptor.hpp:98
counter & operator+=(int64_t val)
Add val to the current value and update the peak value if necessary.
Definition: statistics_resource_adaptor.hpp:67
int64_t total
Sum of all added values.
Definition: statistics_resource_adaptor.hpp:59