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/error.hpp>
9 #include <rmm/detail/export.hpp>
10 
11 #include <cuda/memory_resource>
12 #include <cuda/stream_ref>
13 
14 #include <cstddef>
15 
16 namespace RMM_NAMESPACE {
17 namespace mr {
26 class cuda_memory_resource final {
27  public:
28  cuda_memory_resource() = default;
29  ~cuda_memory_resource() = default;
33  default;
35  default;
36 
49  void* allocate([[maybe_unused]] cuda::stream_ref stream,
50  std::size_t bytes,
51  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
52  {
53  void* ptr{nullptr};
54  RMM_CUDA_TRY_ALLOC(cudaMalloc(&ptr, bytes), bytes);
55  return ptr;
56  }
57 
69  void deallocate([[maybe_unused]] cuda::stream_ref stream,
70  void* ptr,
71  [[maybe_unused]] std::size_t bytes,
72  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
73  {
74  RMM_ASSERT_CUDA_SUCCESS_SAFE_SHUTDOWN(cudaFree(ptr));
75  }
76 
84  void* allocate_sync(std::size_t bytes, std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
85  {
86  auto* ptr = allocate(cuda::stream_ref{cudaStream_t{nullptr}}, bytes, alignment);
87  RMM_CUDA_TRY(cudaStreamSynchronize(cudaStream_t{nullptr}));
88  return ptr;
89  }
90 
98  void deallocate_sync(void* ptr,
99  std::size_t bytes,
100  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
101  {
102  deallocate(cuda::stream_ref{cudaStream_t{nullptr}}, ptr, bytes, alignment);
103  }
104 
110  RMM_CONSTEXPR_FRIEND void get_property(cuda_memory_resource const&,
111  cuda::mr::device_accessible) noexcept
112  {
113  }
114 
122  [[nodiscard]] bool operator==(cuda_memory_resource const&) const noexcept { return true; }
123 
127  [[nodiscard]] bool operator!=(cuda_memory_resource const&) const noexcept { return false; }
128 };
129 
130 // static property checks
131 static_assert(cuda::mr::synchronous_resource<cuda_memory_resource>);
132 static_assert(cuda::mr::resource<cuda_memory_resource>);
133 static_assert(
134  cuda::mr::synchronous_resource_with<cuda_memory_resource, cuda::mr::device_accessible>);
135 static_assert(cuda::mr::resource_with<cuda_memory_resource, cuda::mr::device_accessible>);
136  // end of group
138 } // namespace mr
139 } // namespace RMM_NAMESPACE
Memory resource that uses cudaMalloc/Free for allocation/deallocation.
Definition: cuda_memory_resource.hpp:26
cuda_memory_resource(cuda_memory_resource const &)=default
Default copy constructor.
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:110
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.
void deallocate([[maybe_unused]] cuda::stream_ref stream, void *ptr, [[maybe_unused]] std::size_t bytes, [[maybe_unused]] std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
Deallocate memory pointed to by ptr.
Definition: cuda_memory_resource.hpp:69
bool operator==(cuda_memory_resource const &) const noexcept
Compare this resource to another.
Definition: cuda_memory_resource.hpp:122
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.
Definition: cuda_memory_resource.hpp:98
void * allocate([[maybe_unused]] cuda::stream_ref stream, std::size_t bytes, [[maybe_unused]] std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT)
Allocates memory of size at least bytes.
Definition: cuda_memory_resource.hpp:49
void * allocate_sync(std::size_t bytes, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT)
Allocates memory of size at least bytes synchronously.
Definition: cuda_memory_resource.hpp:84
bool operator!=(cuda_memory_resource const &) const noexcept
Compare this resource to another.
Definition: cuda_memory_resource.hpp:127
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:25