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/cuda_device.hpp>
9 #include <rmm/detail/error.hpp>
10 #include <rmm/detail/export.hpp>
12 
13 #include <cuda_runtime_api.h>
14 
15 #include <cstddef>
16 
17 namespace RMM_NAMESPACE {
18 namespace mr {
30  public:
41  cuda_async_view_memory_resource(cudaMemPool_t pool_handle)
42  : cuda_pool_handle_{[pool_handle]() {
43  RMM_EXPECTS(nullptr != pool_handle, "Unexpected null pool handle.");
44  return pool_handle;
45  }()}
46  {
47  // Check if cudaMallocAsync Memory pool supported
48  RMM_EXPECTS(rmm::detail::runtime_async_alloc::is_supported(),
49  "cudaMallocAsync not supported with this CUDA driver/runtime version");
50  }
51 
57  [[nodiscard]] cudaMemPool_t pool_handle() const noexcept { return cuda_pool_handle_; }
58 
62  default;
64  default;
66  default;
68  default;
69 
70  private:
71  cudaMemPool_t cuda_pool_handle_{};
72 
82  void* do_allocate(std::size_t bytes, rmm::cuda_stream_view stream) override
83  {
84  void* ptr{nullptr};
85  if (bytes > 0) {
86  RMM_CUDA_TRY_ALLOC(cudaMallocFromPoolAsync(&ptr, bytes, pool_handle(), stream.value()),
87  bytes);
88  }
89  return ptr;
90  }
91 
100  void do_deallocate(void* ptr,
101  [[maybe_unused]] std::size_t bytes,
102  rmm::cuda_stream_view stream) noexcept override
103  {
104  if (ptr != nullptr) {
105  RMM_ASSERT_CUDA_SUCCESS_SAFE_SHUTDOWN(cudaFreeAsync(ptr, stream.value()));
106  }
107  }
108 
116  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
117  {
118  return dynamic_cast<cuda_async_view_memory_resource const*>(&other) != nullptr;
119  }
120 };
121  // end of group
123 } // namespace mr
124 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
cudaStream_t value() const noexcept
Get the wrapped stream.
device_memory_resource derived class that uses cudaMallocAsync/cudaFreeAsync for allocation/deallocat...
Definition: cuda_async_view_memory_resource.hpp:29
cuda_async_view_memory_resource & operator=(cuda_async_view_memory_resource &&)=default
Default move assignment operator.
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:57
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:41
cuda_async_view_memory_resource(cuda_async_view_memory_resource const &)=default
Default copy constructor.
cuda_async_view_memory_resource & operator=(cuda_async_view_memory_resource const &)=default
Default copy assignment operator.
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:82