fixed_size_memory_resource.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/aligned.hpp>
9 #include <rmm/detail/error.hpp>
10 #include <rmm/detail/export.hpp>
11 #include <rmm/detail/logging_assert.hpp>
12 #include <rmm/detail/thrust_namespace.h>
13 #include <rmm/mr/device/detail/fixed_size_free_list.hpp>
14 #include <rmm/mr/device/detail/stream_ordered_memory_resource.hpp>
15 #include <rmm/resource_ref.hpp>
16 
17 #include <cuda_runtime_api.h>
18 #include <thrust/iterator/counting_iterator.h>
19 #include <thrust/iterator/transform_iterator.h>
20 
21 #include <algorithm>
22 #include <cstddef>
23 #include <utility>
24 #include <vector>
25 
26 namespace RMM_NAMESPACE {
27 namespace mr {
39 template <typename Upstream>
41  : public detail::stream_ordered_memory_resource<fixed_size_memory_resource<Upstream>,
42  detail::fixed_size_free_list> {
43  public:
44  friend class detail::stream_ordered_memory_resource<fixed_size_memory_resource<Upstream>,
45  detail::fixed_size_free_list>;
46 
47  static constexpr std::size_t default_block_size = 1 << 20;
48 
51  static constexpr std::size_t default_blocks_to_preallocate = 128;
52 
64  explicit fixed_size_memory_resource(
65  device_async_resource_ref upstream_mr,
66  // NOLINTNEXTLINE bugprone-easily-swappable-parameters
67  std::size_t block_size = default_block_size,
68  std::size_t blocks_to_preallocate = default_blocks_to_preallocate)
69  : upstream_mr_{upstream_mr},
70  block_size_{align_up(block_size, CUDA_ALLOCATION_ALIGNMENT)},
71  upstream_chunk_size_{block_size_ * blocks_to_preallocate}
72  {
73  // allocate initial blocks and insert into free list
74  this->insert_blocks(std::move(blocks_from_upstream(cuda_stream_legacy)), cuda_stream_legacy);
75  }
76 
88  explicit fixed_size_memory_resource(
89  Upstream* upstream_mr,
90  // NOLINTNEXTLINE bugprone-easily-swappable-parameters
91  std::size_t block_size = default_block_size,
92  std::size_t blocks_to_preallocate = default_blocks_to_preallocate)
93  : upstream_mr_{to_device_async_resource_ref_checked(upstream_mr)},
94  block_size_{align_up(block_size, CUDA_ALLOCATION_ALIGNMENT)},
95  upstream_chunk_size_{block_size_ * blocks_to_preallocate}
96  {
97  // allocate initial blocks and insert into free list
98  this->insert_blocks(std::move(blocks_from_upstream(cuda_stream_legacy)), cuda_stream_legacy);
99  }
100 
105  ~fixed_size_memory_resource() override { release(); }
106 
107  fixed_size_memory_resource() = delete;
110  fixed_size_memory_resource& operator=(fixed_size_memory_resource const&) = delete;
112 
116  [[nodiscard]] device_async_resource_ref get_upstream_resource() const noexcept
117  {
118  return upstream_mr_;
119  }
120 
126  [[nodiscard]] std::size_t get_block_size() const noexcept { return block_size_; }
127 
128  protected:
129  using free_list = detail::fixed_size_free_list;
130  using block_type = free_list::block_type;
131  using typename detail::stream_ordered_memory_resource<fixed_size_memory_resource<Upstream>,
132  detail::fixed_size_free_list>::split_block;
133  using lock_guard = std::lock_guard<std::mutex>;
134 
141  [[nodiscard]] std::size_t get_maximum_allocation_size() const { return get_block_size(); }
142 
154  block_type expand_pool(std::size_t size, free_list& blocks, cuda_stream_view stream)
155  {
156  blocks.insert(std::move(blocks_from_upstream(stream)));
157  return blocks.get_block(size);
158  }
159 
167  {
168  void* ptr = get_upstream_resource().allocate(stream, upstream_chunk_size_);
169  block_type block{ptr};
170  upstream_blocks_.push_back(block);
171 
172  auto num_blocks = upstream_chunk_size_ / block_size_;
173 
174  auto block_gen = [ptr, this](int index) {
175  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
176  return block_type{static_cast<char*>(ptr) + index * block_size_};
177  };
178  auto first =
179  thrust::make_transform_iterator(thrust::make_counting_iterator(std::size_t{0}), block_gen);
180  return free_list(first, first + num_blocks);
181  }
182 
193  split_block allocate_from_block(block_type const& block, std::size_t size)
194  {
195  return {block, block_type{nullptr}};
196  }
197 
206  block_type free_block(void* ptr, std::size_t size) noexcept
207  {
208  // Deallocating a fixed-size block just inserts it in the free list, which is
209  // handled by the parent class
210  RMM_LOGGING_ASSERT(align_up(size, CUDA_ALLOCATION_ALIGNMENT) <= block_size_);
211  return block_type{ptr};
212  }
213 
218  void release()
219  {
220  lock_guard lock(this->get_mutex());
221 
222  for (auto block : upstream_blocks_) {
223  get_upstream_resource().deallocate_sync(block.pointer(), upstream_chunk_size_);
224  }
225  upstream_blocks_.clear();
226  }
227 
228 #ifdef RMM_DEBUG_PRINT
229  void print()
230  {
231  lock_guard lock(this->get_mutex());
232 
233  auto const [free, total] = rmm::available_device_memory();
234  std::cout << "GPU free memory: " << free << " total: " << total << "\n";
235 
236  std::cout << "upstream_blocks: " << upstream_blocks_.size() << "\n";
237  std::size_t upstream_total{0};
238 
239  for (auto blocks : upstream_blocks_) {
240  blocks.print();
241  upstream_total += upstream_chunk_size_;
242  }
243  std::cout << "total upstream: " << upstream_total << " B\n";
244 
245  this->print_free_blocks();
246  }
247 #endif
248 
257  std::pair<std::size_t, std::size_t> free_list_summary(free_list const& blocks)
258  {
259  return blocks.is_empty() ? std::make_pair(std::size_t{0}, std::size_t{0})
260  : std::make_pair(block_size_, blocks.size() * block_size_);
261  }
262 
263  private:
264  device_async_resource_ref upstream_mr_; // The resource from which to allocate new blocks
265 
266  std::size_t block_size_; // size of blocks this MR allocates
267  std::size_t upstream_chunk_size_; // size of chunks allocated from heap MR
268 
269  // blocks allocated from heap: so they can be easily freed
270  std::vector<block_type> upstream_blocks_;
271 };
272  // end of group
274 } // namespace mr
275 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
A device_memory_resource which allocates memory blocks of a single fixed size.
Definition: fixed_size_memory_resource.hpp:42
std::pair< std::size_t, std::size_t > free_list_summary(free_list const &blocks)
Get the largest available block size and total free size in the specified free list.
Definition: fixed_size_memory_resource.hpp:257
detail::fixed_size_free_list free_list
The free list type.
Definition: fixed_size_memory_resource.hpp:129
block_type free_block(void *ptr, std::size_t size) noexcept
Finds, frees and returns the block associated with pointer.
Definition: fixed_size_memory_resource.hpp:206
std::size_t get_block_size() const noexcept
Get the size of blocks allocated by this memory resource.
Definition: fixed_size_memory_resource.hpp:126
std::size_t get_maximum_allocation_size() const
Get the (fixed) size of allocations supported by this memory resource.
Definition: fixed_size_memory_resource.hpp:141
free_list::block_type block_type
The type of block managed by the free list.
Definition: fixed_size_memory_resource.hpp:130
device_async_resource_ref get_upstream_resource() const noexcept
device_async_resource_ref to the upstream resource
Definition: fixed_size_memory_resource.hpp:116
block_type expand_pool(std::size_t size, free_list &blocks, cuda_stream_view stream)
Allocate a block from upstream to supply the suballocation pool.
Definition: fixed_size_memory_resource.hpp:154
free_list blocks_from_upstream(cuda_stream_view stream)
Allocate blocks from upstream to expand the suballocation pool.
Definition: fixed_size_memory_resource.hpp:166
std::lock_guard< std::mutex > lock_guard
Type of lock used to synchronize access.
Definition: fixed_size_memory_resource.hpp:133
void release()
free all memory allocated using the upstream resource.
Definition: fixed_size_memory_resource.hpp:218
split_block allocate_from_block(block_type const &block, std::size_t size)
Splits block if necessary to return a pointer to memory of size bytes.
Definition: fixed_size_memory_resource.hpp:193
std::pair< std::size_t, std::size_t > available_device_memory()
Returns the available and total device memory in bytes for the current device.
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
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:31
std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
Align up to nearest multiple of specified power of 2.