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
116 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
117  [[deprecated(
118  "This function is deprecated. Use allocate(cuda_stream_view stream, std::size_t bytes, "
119  "std::size_t alignment) instead.")]]
120 #endif
121  void* allocate(std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
122  {
123  RMM_FUNC_RANGE();
124  return do_allocate(bytes, stream);
125  }
126 
145 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
146  [[deprecated(
147  "This function is deprecated. Use deallocate(cuda_stream_view stream, void* ptr, std::size_t "
148  "bytes, std::size_t alignment) instead.")]]
149 #endif
150  void deallocate(void* ptr,
151  std::size_t bytes,
152  cuda_stream_view stream = cuda_stream_view{}) noexcept
153  {
154  RMM_FUNC_RANGE();
155  do_deallocate(ptr, bytes, stream);
156  }
157 
175 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
176  [[deprecated(
177  "This function is deprecated. Use allocate_sync(std::size_t bytes, std::size_t alignment) "
178  "instead.")]]
179 #endif
180  void* allocate(std::size_t bytes, [[maybe_unused]] std::size_t alignment)
181  {
182  RMM_FUNC_RANGE();
183  return do_allocate(bytes, cuda_stream_view{});
184  }
185 
201 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
202  [[deprecated(
203  "This function is deprecated. Use deallocate_sync(void* ptr, std::size_t bytes, std::size_t "
204  "alignment) instead.")]]
205 #endif
206  void deallocate(void* ptr, std::size_t bytes, [[maybe_unused]] std::size_t alignment) noexcept
207  {
208  RMM_FUNC_RANGE();
209  do_deallocate(ptr, bytes, cuda_stream_view{});
210  }
211 
230 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
231  [[deprecated(
232  "This function is deprecated. Use allocate(cuda_stream_view stream, std::size_t bytes, "
233  "std::size_t alignment) instead.")]]
234 #endif
235  void* allocate_async(std::size_t bytes,
236  [[maybe_unused]] std::size_t alignment,
237  cuda_stream_view stream)
238  {
239  RMM_FUNC_RANGE();
240  return do_allocate(bytes, stream);
241  }
242 
260 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
261  [[deprecated(
262  "This function is deprecated. Use allocate(cuda_stream_view stream, std::size_t bytes, "
263  "std::size_t alignment) instead.")]]
264 #endif
265  void* allocate_async(std::size_t bytes, cuda_stream_view stream)
266  {
267  RMM_FUNC_RANGE();
268  return do_allocate(bytes, stream);
269  }
270 
287 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
288  [[deprecated(
289  "This function is deprecated. Use deallocate(cuda_stream_view stream, void* ptr, std::size_t "
290  "bytes, std::size_t alignment) instead.")]]
291 #endif
292  void deallocate_async(void* ptr,
293  std::size_t bytes,
294  [[maybe_unused]] std::size_t alignment,
295  cuda_stream_view stream) noexcept
296  {
297  RMM_FUNC_RANGE();
298  do_deallocate(ptr, bytes, stream);
299  }
300 
316 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
317  [[deprecated(
318  "This function is deprecated. Use deallocate(cuda_stream_view stream, void* ptr, std::size_t "
319  "bytes, std::size_t alignment) instead.")]]
320 #endif
321  void deallocate_async(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept
322  {
323  RMM_FUNC_RANGE();
324  do_deallocate(ptr, bytes, stream);
325  }
326 #endif // RMM_ENABLE_LEGACY_MR_INTERFACE
327 
340  void* allocate_sync(std::size_t bytes, std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
341  {
342  RMM_EXPECTS(
344  "Alignment must be less than or equal to 256 and a power of two",
346  return do_allocate(bytes, cuda_stream_view{});
347  }
348 
358  void* ptr,
359  std::size_t bytes,
360  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
361  {
362  do_deallocate(ptr, bytes, cuda_stream_view{});
363  }
364 
379  std::size_t bytes,
380  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
381  {
382  RMM_EXPECTS(
384  "Alignment must be less than or equal to 256 and a power of two",
386  return do_allocate(bytes, stream);
387  }
388 
399  void* ptr,
400  std::size_t bytes,
401  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
402  {
403  do_deallocate(ptr, bytes, stream);
404  }
405 
419  [[nodiscard]] bool is_equal(device_memory_resource const& other) const noexcept
420  {
421  return do_is_equal(other);
422  }
423 
431  [[nodiscard]] bool operator==(device_memory_resource const& other) const noexcept
432  {
433  return do_is_equal(other);
434  }
435 
443  [[nodiscard]] bool operator!=(device_memory_resource const& other) const noexcept
444  {
445  return !do_is_equal(other);
446  }
447 
453  friend void get_property(device_memory_resource const&, cuda::mr::device_accessible) noexcept {}
454 
455  private:
468  virtual void* do_allocate(std::size_t bytes, cuda_stream_view stream) = 0;
469 
481  virtual void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept = 0;
482 
497  [[nodiscard]] virtual bool do_is_equal(device_memory_resource const& other) const noexcept
498  {
499  return this == &other;
500  }
501 };
502 
503 // static property checks
504 static_assert(rmm::detail::polyfill::resource<device_memory_resource>);
505 static_assert(rmm::detail::polyfill::async_resource<device_memory_resource>);
506 static_assert(
507  rmm::detail::polyfill::resource_with<device_memory_resource, cuda::mr::device_accessible>);
508 static_assert(
509  rmm::detail::polyfill::async_resource_with<device_memory_resource, cuda::mr::device_accessible>);
510  // end of group
512 } // namespace mr
513 } // 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:340
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:453
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:398
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:378
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:357
bool operator==(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:431
bool operator!=(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:443
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:419
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:33
bool is_supported_alignment(std::size_t alignment) noexcept
Returns whether or not alignment is a valid memory alignment.