managed_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 {
27  public:
28  managed_memory_resource() = default;
29  ~managed_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  // FIXME: Unlike cudaMalloc, cudaMallocManaged will throw an error for 0
54  // size allocations.
55  if (bytes == 0) { return nullptr; }
56 
57  void* ptr{nullptr};
58  RMM_CUDA_TRY_ALLOC(cudaMallocManaged(&ptr, bytes), bytes);
59  return ptr;
60  }
61 
73  void deallocate([[maybe_unused]] cuda::stream_ref stream,
74  void* ptr,
75  [[maybe_unused]] std::size_t bytes,
76  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
77  {
78  RMM_ASSERT_CUDA_SUCCESS_SAFE_SHUTDOWN(cudaFree(ptr));
79  }
80 
88  void* allocate_sync(std::size_t bytes, std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
89  {
90  auto* ptr = allocate(cuda::stream_ref{cudaStream_t{nullptr}}, bytes, alignment);
91  RMM_CUDA_TRY(cudaStreamSynchronize(cudaStream_t{nullptr}));
92  return ptr;
93  }
94 
102  void deallocate_sync(void* ptr,
103  std::size_t bytes,
104  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
105  {
106  deallocate(cuda::stream_ref{cudaStream_t{nullptr}}, ptr, bytes, alignment);
107  }
108 
114  RMM_CONSTEXPR_FRIEND void get_property(managed_memory_resource const&,
115  cuda::mr::device_accessible) noexcept
116  {
117  }
118 
124  RMM_CONSTEXPR_FRIEND void get_property(managed_memory_resource const&,
125  cuda::mr::host_accessible) noexcept
126  {
127  }
128 
136  [[nodiscard]] bool operator==(managed_memory_resource const&) const noexcept { return true; }
137 
141  [[nodiscard]] bool operator!=(managed_memory_resource const&) const noexcept { return false; }
142 };
143 
144 // static property checks
145 static_assert(cuda::mr::synchronous_resource<managed_memory_resource>);
146 static_assert(cuda::mr::resource<managed_memory_resource>);
147 static_assert(
148  cuda::mr::synchronous_resource_with<managed_memory_resource, cuda::mr::device_accessible>);
149 static_assert(
150  cuda::mr::synchronous_resource_with<managed_memory_resource, cuda::mr::host_accessible>);
151 static_assert(cuda::mr::resource_with<managed_memory_resource, cuda::mr::device_accessible>);
152 static_assert(cuda::mr::resource_with<managed_memory_resource, cuda::mr::host_accessible>);
153  // end of group
155 } // namespace mr
156 } // namespace RMM_NAMESPACE
Memory resource that uses cudaMallocManaged/Free for allocation/deallocation.
Definition: managed_memory_resource.hpp:26
managed_memory_resource(managed_memory_resource &&)=default
Default move constructor.
void * allocate_sync(std::size_t bytes, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT)
Allocates memory of size at least bytes synchronously.
Definition: managed_memory_resource.hpp:88
bool operator==(managed_memory_resource const &) const noexcept
Compare this resource to another.
Definition: managed_memory_resource.hpp:136
friend void get_property(managed_memory_resource const &, cuda::mr::host_accessible) noexcept
Enables the cuda::mr::host_accessible property.
Definition: managed_memory_resource.hpp:124
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: managed_memory_resource.hpp:49
friend void get_property(managed_memory_resource const &, cuda::mr::device_accessible) noexcept
Enables the cuda::mr::device_accessible property.
Definition: managed_memory_resource.hpp:114
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: managed_memory_resource.hpp:102
managed_memory_resource & operator=(managed_memory_resource &&)=default
Default move assignment operator.
managed_memory_resource & operator=(managed_memory_resource const &)=default
Default copy 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: managed_memory_resource.hpp:73
bool operator!=(managed_memory_resource const &) const noexcept
Compare this resource to another.
Definition: managed_memory_resource.hpp:141
managed_memory_resource(managed_memory_resource const &)=default
Default copy constructor.
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:25