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/cuda_memory_resource.hpp>
10 #include <rmm/detail/error.hpp>
11 #include <rmm/detail/export.hpp>
12 #include <rmm/detail/nvtx/ranges.hpp>
13 
14 #include <cstddef>
15 
16 namespace RMM_NAMESPACE {
17 namespace mr {
83  public:
84  device_memory_resource() = default;
85  virtual ~device_memory_resource() = default;
88  default;
90  default;
92  default;
93 
108  void* allocate_sync(std::size_t bytes, std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
109  {
110  RMM_EXPECTS(
112  "Alignment must be less than or equal to 256 and a power of two",
114  auto const stream = cuda_stream_view{};
115  void* ptr = do_allocate(bytes, stream);
116  stream.synchronize();
117  return ptr;
118  }
119 
132  void* ptr,
133  std::size_t bytes,
134  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
135  {
136  do_deallocate(ptr, bytes, cuda_stream_view{});
137  }
138 
153  std::size_t bytes,
154  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
155  {
156  RMM_EXPECTS(
158  "Alignment must be less than or equal to 256 and a power of two",
160  return do_allocate(bytes, stream);
161  }
162 
173  void* ptr,
174  std::size_t bytes,
175  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
176  {
177  do_deallocate(ptr, bytes, stream);
178  }
179 
193  [[nodiscard]] bool is_equal(device_memory_resource const& other) const noexcept
194  {
195  return do_is_equal(other);
196  }
197 
205  [[nodiscard]] bool operator==(device_memory_resource const& other) const noexcept
206  {
207  return do_is_equal(other);
208  }
209 
217  [[nodiscard]] bool operator!=(device_memory_resource const& other) const noexcept
218  {
219  return !do_is_equal(other);
220  }
221 
227  friend void get_property(device_memory_resource const&, cuda::mr::device_accessible) noexcept {}
228 
229  private:
242  virtual void* do_allocate(std::size_t bytes, cuda_stream_view stream) = 0;
243 
255  virtual void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept = 0;
256 
271  [[nodiscard]] virtual bool do_is_equal(device_memory_resource const& other) const noexcept
272  {
273  return this == &other;
274  }
275 };
276 
277 // static property checks
278 static_assert(rmm::detail::polyfill::resource<device_memory_resource>);
279 static_assert(rmm::detail::polyfill::async_resource<device_memory_resource>);
280 static_assert(
281  rmm::detail::polyfill::resource_with<device_memory_resource, cuda::mr::device_accessible>);
282 static_assert(
283  rmm::detail::polyfill::async_resource_with<device_memory_resource, cuda::mr::device_accessible>);
284  // end of group
286 } // namespace mr
287 } // 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:82
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:227
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:172
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:152
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 ptr.
Definition: device_memory_resource.hpp:131
bool operator==(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:205
bool operator!=(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:217
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:193
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:25
bool is_supported_alignment(std::size_t alignment) noexcept
Returns whether or not alignment is a valid memory alignment.
RAPIDS Memory Manager - The top-level namespace for all RMM functionality.