device_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
8 #include <rmm/detail/aligned.hpp>
9 #include <rmm/detail/error.hpp>
10 #include <rmm/detail/export.hpp>
11 #include <rmm/detail/nvtx/ranges.hpp>
12 
13 #include <cuda/memory_resource>
14 
15 #include <cstddef>
16 #include <memory>
17 
18 namespace RMM_NAMESPACE {
19 namespace mr {
85  public:
86  device_memory_resource() = default;
87  virtual ~device_memory_resource() = default;
90  default;
92  default;
94  default;
95 
110  void* allocate_sync(std::size_t bytes, std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
111  {
112  RMM_EXPECTS(
114  "Alignment must be less than or equal to 256 and a power of two",
116  auto const stream = cuda_stream_view{};
117  void* ptr = do_allocate(bytes, stream);
118  stream.synchronize();
119  return ptr;
120  }
121 
134  void* ptr,
135  std::size_t bytes,
136  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
137  {
138  do_deallocate(ptr, bytes, cuda_stream_view{});
139  }
140 
155  std::size_t bytes,
156  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
157  {
158  RMM_EXPECTS(
160  "Alignment must be less than or equal to 256 and a power of two",
162  return do_allocate(bytes, stream);
163  }
164 
175  void* ptr,
176  std::size_t bytes,
177  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
178  {
179  do_deallocate(ptr, bytes, stream);
180  }
181 
195  [[nodiscard]] bool is_equal(device_memory_resource const& other) const noexcept
196  {
197  return do_is_equal(other);
198  }
199 
207  [[nodiscard]] bool operator==(device_memory_resource const& other) const noexcept
208  {
209  return do_is_equal(other);
210  }
211 
219  [[nodiscard]] bool operator!=(device_memory_resource const& other) const noexcept
220  {
221  return !do_is_equal(other);
222  }
223 
229  friend void get_property(device_memory_resource const&, cuda::mr::device_accessible) noexcept {}
230 
231  private:
244  virtual void* do_allocate(std::size_t bytes, cuda_stream_view stream) = 0;
245 
257  virtual void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept = 0;
258 
273  [[nodiscard]] virtual bool do_is_equal(device_memory_resource const& other) const noexcept
274  {
275  return this == std::addressof(other);
276  }
277 };
278 
279 // static property checks
280 static_assert(cuda::mr::synchronous_resource<device_memory_resource>);
281 static_assert(cuda::mr::resource<device_memory_resource>);
282 static_assert(
283  cuda::mr::synchronous_resource_with<device_memory_resource, cuda::mr::device_accessible>);
284 static_assert(cuda::mr::resource_with<device_memory_resource, cuda::mr::device_accessible>);
285  // end of group
287 } // namespace mr
288 } // 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:84
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:229
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:174
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:154
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:133
bool operator==(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:207
bool operator!=(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:219
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:195
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.