cuda_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 
7 #include <rmm/aligned.hpp>
8 #include <rmm/detail/export.hpp>
9 
10 #include <cuda/memory_resource>
11 #include <cuda/stream_ref>
12 
13 #include <cstddef>
14 
15 namespace RMM_NAMESPACE {
16 namespace mr {
25 class RMM_EXPORT cuda_memory_resource final {
26  public:
27  cuda_memory_resource() = default;
28  ~cuda_memory_resource() = default;
32  default;
34  default;
35 
48  void* allocate([[maybe_unused]] cuda::stream_ref stream,
49  std::size_t bytes,
50  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT);
51 
63  void deallocate([[maybe_unused]] cuda::stream_ref stream,
64  void* ptr,
65  std::size_t bytes,
66  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept;
67 
75  void* allocate_sync(std::size_t bytes, std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT);
76 
84  void deallocate_sync(void* ptr,
85  std::size_t bytes,
86  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept;
87 
93  RMM_CONSTEXPR_FRIEND void get_property(cuda_memory_resource const&,
94  cuda::mr::device_accessible) noexcept
95  {
96  }
97 
105  [[nodiscard]] bool operator==(cuda_memory_resource const&) const noexcept;
106 
110  [[nodiscard]] bool operator!=(cuda_memory_resource const&) const noexcept;
111 };
112 
113 // static property checks
114 static_assert(cuda::mr::synchronous_resource<cuda_memory_resource>);
115 static_assert(cuda::mr::resource<cuda_memory_resource>);
116 static_assert(
117  cuda::mr::synchronous_resource_with<cuda_memory_resource, cuda::mr::device_accessible>);
118 static_assert(cuda::mr::resource_with<cuda_memory_resource, cuda::mr::device_accessible>);
119  // end of group
121 } // namespace mr
122 } // namespace RMM_NAMESPACE
Memory resource that uses cudaMalloc/Free for allocation/deallocation.
Definition: cuda_memory_resource.hpp:25
cuda_memory_resource(cuda_memory_resource const &)=default
Default copy constructor.
void * allocate([[maybe_unused]] cuda::stream_ref stream, std::size_t bytes, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT)
Allocates memory of size at least bytes.
void deallocate([[maybe_unused]] cuda::stream_ref stream, void *ptr, std::size_t bytes, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
Deallocate memory pointed to by ptr.
friend void get_property(cuda_memory_resource const &, cuda::mr::device_accessible) noexcept
Enables the cuda::mr::device_accessible property.
Definition: cuda_memory_resource.hpp:93
cuda_memory_resource & operator=(cuda_memory_resource const &)=default
Default copy assignment operator.
cuda_memory_resource & operator=(cuda_memory_resource &&)=default
Default move assignment operator.
bool operator==(cuda_memory_resource const &) const noexcept
Compare this resource to another.
cuda_memory_resource(cuda_memory_resource &&)=default
Default move constructor.
void deallocate_sync(void *ptr, std::size_t bytes, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
Deallocate memory pointed to by ptr synchronously.
void * allocate_sync(std::size_t bytes, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT)
Allocates memory of size at least bytes synchronously.
bool operator!=(cuda_memory_resource const &) const noexcept
Compare this resource to another.
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:25