device_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
8 #include <rmm/detail/aligned.hpp>
9 #include <rmm/detail/cccl_adaptors.hpp>
10 #include <rmm/detail/cuda_memory_resource.hpp>
11 #include <rmm/detail/error.hpp>
12 #include <rmm/detail/export.hpp>
13 #include <rmm/detail/nvtx/ranges.hpp>
14 
15 #include <cstddef>
16 
17 namespace RMM_NAMESPACE {
18 namespace mr {
84  public:
85  device_memory_resource() = default;
86  virtual ~device_memory_resource() = default;
89  default;
91  default;
93  default;
94 
95 #ifdef RMM_ENABLE_LEGACY_MR_INTERFACE
114  void* allocate(std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
115  {
116  RMM_FUNC_RANGE();
117  return do_allocate(bytes, stream);
118  }
119 
136  void deallocate(void* ptr,
137  std::size_t bytes,
138  cuda_stream_view stream = cuda_stream_view{}) noexcept
139  {
140  RMM_FUNC_RANGE();
141  do_deallocate(ptr, bytes, stream);
142  }
143 
159  void* allocate(std::size_t bytes, [[maybe_unused]] std::size_t alignment)
160  {
161  RMM_FUNC_RANGE();
162  return do_allocate(bytes, cuda_stream_view{});
163  }
164 
178  void deallocate(void* ptr, std::size_t bytes, [[maybe_unused]] std::size_t alignment) noexcept
179  {
180  RMM_FUNC_RANGE();
181  do_deallocate(ptr, bytes, cuda_stream_view{});
182  }
183 
200  void* allocate_async(std::size_t bytes,
201  [[maybe_unused]] std::size_t alignment,
202  cuda_stream_view stream)
203  {
204  RMM_FUNC_RANGE();
205  return do_allocate(bytes, stream);
206  }
207 
223  void* allocate_async(std::size_t bytes, cuda_stream_view stream)
224  {
225  RMM_FUNC_RANGE();
226  return do_allocate(bytes, stream);
227  }
228 
243  void deallocate_async(void* ptr,
244  std::size_t bytes,
245  [[maybe_unused]] std::size_t alignment,
246  cuda_stream_view stream) noexcept
247  {
248  RMM_FUNC_RANGE();
249  do_deallocate(ptr, bytes, stream);
250  }
251 
265  void deallocate_async(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept
266  {
267  RMM_FUNC_RANGE();
268  do_deallocate(ptr, bytes, stream);
269  }
270 #endif // RMM_ENABLE_LEGACY_MR_INTERFACE
271 
284  void* allocate_sync(std::size_t bytes, std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
285  {
286  RMM_EXPECTS(
288  "Alignment must be less than or equal to 256 and a power of two",
290  return do_allocate(bytes, cuda_stream_view{});
291  }
292 
302  void* ptr,
303  std::size_t bytes,
304  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
305  {
306  do_deallocate(ptr, bytes, cuda_stream_view{});
307  }
308 
323  std::size_t bytes,
324  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
325  {
326  RMM_EXPECTS(
328  "Alignment must be less than or equal to 256 and a power of two",
330  return do_allocate(bytes, stream);
331  }
332 
343  void* ptr,
344  std::size_t bytes,
345  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
346  {
347  do_deallocate(ptr, bytes, stream);
348  }
349 
363  [[nodiscard]] bool is_equal(device_memory_resource const& other) const noexcept
364  {
365  return do_is_equal(other);
366  }
367 
375  [[nodiscard]] bool operator==(device_memory_resource const& other) const noexcept
376  {
377  return do_is_equal(other);
378  }
379 
387  [[nodiscard]] bool operator!=(device_memory_resource const& other) const noexcept
388  {
389  return !do_is_equal(other);
390  }
391 
397  friend void get_property(device_memory_resource const&, cuda::mr::device_accessible) noexcept {}
398 
399  private:
412  virtual void* do_allocate(std::size_t bytes, cuda_stream_view stream) = 0;
413 
425  virtual void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept = 0;
426 
441  [[nodiscard]] virtual bool do_is_equal(device_memory_resource const& other) const noexcept
442  {
443  return this == &other;
444  }
445 };
446 
447 // static property checks
448 static_assert(rmm::detail::polyfill::resource<device_memory_resource>);
449 static_assert(rmm::detail::polyfill::async_resource<device_memory_resource>);
450 static_assert(
451  rmm::detail::polyfill::resource_with<device_memory_resource, cuda::mr::device_accessible>);
452 static_assert(
453  rmm::detail::polyfill::async_resource_with<device_memory_resource, cuda::mr::device_accessible>);
454  // end of group
456 } // namespace mr
457 } // namespace RMM_NAMESPACE
Exception thrown when an RMM allocation fails.
Definition: error.hpp:44
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
void * allocate_sync(std::size_t bytes, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:284
friend void get_property(device_memory_resource const &, cuda::mr::device_accessible) noexcept
Enables the cuda::mr::device_accessible property.
Definition: device_memory_resource.hpp:397
device_memory_resource(device_memory_resource &&) noexcept=default
Default move constructor.
void deallocate(cuda_stream_view stream, void *ptr, std::size_t bytes, [[maybe_unused]] std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
Deallocate memory pointed to by ptr on the specified stream.
Definition: device_memory_resource.hpp:342
void * allocate(cuda_stream_view stream, std::size_t bytes, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT)
Allocates memory of size at least bytes on the specified stream.
Definition: device_memory_resource.hpp:322
void deallocate_sync(void *ptr, std::size_t bytes, [[maybe_unused]] std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:301
bool operator==(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:375
bool operator!=(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:387
device_memory_resource(device_memory_resource const &)=default
Default copy constructor.
bool is_equal(device_memory_resource const &other) const noexcept
Compare this resource to another.
Definition: device_memory_resource.hpp:363
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:31
bool is_supported_alignment(std::size_t alignment) noexcept
Returns whether or not alignment is a valid memory alignment.