cuda_async_view_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-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 #include <rmm/detail/runtime_capabilities.hpp>
11 
12 #include <cuda/memory_resource>
13 #include <cuda/stream_ref>
14 #include <cuda_runtime_api.h>
15 
16 #include <cstddef>
17 
18 namespace RMM_NAMESPACE {
19 namespace mr {
31  public:
42  cuda_async_view_memory_resource(cudaMemPool_t pool_handle)
43  : cuda_pool_handle_{[pool_handle]() {
44  RMM_EXPECTS(nullptr != pool_handle, "Unexpected null pool handle.");
45  return pool_handle;
46  }()}
47  {
48  // Check if cudaMallocAsync Memory pool supported
49  RMM_EXPECTS(rmm::detail::runtime_async_alloc::is_supported(),
50  "cudaMallocAsync not supported with this CUDA driver/runtime version");
51  }
52 
58  [[nodiscard]] cudaMemPool_t pool_handle() const noexcept { return cuda_pool_handle_; }
59 
63  default;
65  default;
67  default;
69  default;
70 
81  void* allocate(cuda::stream_ref stream,
82  std::size_t bytes,
83  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
84  {
85  void* ptr{nullptr};
86  if (bytes > 0) {
87  RMM_CUDA_TRY_ALLOC(cudaMallocFromPoolAsync(&ptr, bytes, pool_handle(), stream.get()), bytes);
88  }
89  return ptr;
90  }
91 
101  void deallocate(cuda::stream_ref stream,
102  void* ptr,
103  [[maybe_unused]] std::size_t bytes,
104  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
105  {
106  if (ptr != nullptr) { RMM_ASSERT_CUDA_SUCCESS_SAFE_SHUTDOWN(cudaFreeAsync(ptr, stream.get())); }
107  }
108 
116  void* allocate_sync(std::size_t bytes, std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
117  {
118  auto* ptr = allocate(cuda::stream_ref{cudaStream_t{nullptr}}, bytes, alignment);
119  RMM_CUDA_TRY(cudaStreamSynchronize(cudaStream_t{nullptr}));
120  return ptr;
121  }
122 
130  void deallocate_sync(void* ptr,
131  std::size_t bytes,
132  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
133  {
134  deallocate(cuda::stream_ref{cudaStream_t{nullptr}}, ptr, bytes, alignment);
135  }
136 
144  [[nodiscard]] bool operator==(cuda_async_view_memory_resource const& other) const noexcept
145  {
146  return pool_handle() == other.pool_handle();
147  }
148 
152  [[nodiscard]] bool operator!=(cuda_async_view_memory_resource const& other) const noexcept
153  {
154  return !operator==(other);
155  }
156 
163  RMM_CONSTEXPR_FRIEND void get_property(cuda_async_view_memory_resource const&,
164  cuda::mr::device_accessible) noexcept
165  {
166  }
167 
168  private:
169  cudaMemPool_t cuda_pool_handle_{};
170 };
171 
172 // static property checks
173 static_assert(cuda::mr::synchronous_resource<cuda_async_view_memory_resource>);
174 static_assert(cuda::mr::resource<cuda_async_view_memory_resource>);
175 static_assert(cuda::mr::synchronous_resource_with<cuda_async_view_memory_resource,
176  cuda::mr::device_accessible>);
177 static_assert(
178  cuda::mr::resource_with<cuda_async_view_memory_resource, cuda::mr::device_accessible>);
179  // end of group
181 } // namespace mr
182 } // namespace RMM_NAMESPACE
Memory resource that uses cudaMallocAsync/cudaFreeAsync for allocation/deallocation.
Definition: cuda_async_view_memory_resource.hpp:30
void deallocate(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_async_view_memory_resource.hpp:101
bool operator!=(cuda_async_view_memory_resource const &other) const noexcept
Compare this resource to another.
Definition: cuda_async_view_memory_resource.hpp:152
cuda_async_view_memory_resource & operator=(cuda_async_view_memory_resource &&)=default
Default move assignment operator.
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_async_view_memory_resource.hpp:130
cuda_async_view_memory_resource(cuda_async_view_memory_resource &&)=default
Default move constructor.
cudaMemPool_t pool_handle() const noexcept
Returns the underlying native handle to the CUDA pool.
Definition: cuda_async_view_memory_resource.hpp:58
void * allocate(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_async_view_memory_resource.hpp:81
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_async_view_memory_resource.hpp:116
cuda_async_view_memory_resource(cudaMemPool_t pool_handle)
Constructs a cuda_async_view_memory_resource which uses an existing CUDA memory pool....
Definition: cuda_async_view_memory_resource.hpp:42
bool operator==(cuda_async_view_memory_resource const &other) const noexcept
Compare this resource to another.
Definition: cuda_async_view_memory_resource.hpp:144
cuda_async_view_memory_resource(cuda_async_view_memory_resource const &)=default
Default copy constructor.
friend void get_property(cuda_async_view_memory_resource const &, cuda::mr::device_accessible) noexcept
Enables the cuda::mr::device_accessible property.
Definition: cuda_async_view_memory_resource.hpp:163
cuda_async_view_memory_resource & operator=(cuda_async_view_memory_resource const &)=default
Default copy assignment operator.
bool operator==(cuda_stream_view lhs, cuda_stream_view rhs)
Equality comparison operator for streams.
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:25