cuda_async_view_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2025, 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>
11 #include <rmm/detail/thrust_namespace.h>
13 
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 
71  private:
72  cudaMemPool_t cuda_pool_handle_{};
73 
83  void* do_allocate(std::size_t bytes, rmm::cuda_stream_view stream) override
84  {
85  void* ptr{nullptr};
86  if (bytes > 0) {
87  RMM_CUDA_TRY_ALLOC(cudaMallocFromPoolAsync(&ptr, bytes, pool_handle(), stream.value()),
88  bytes);
89  }
90  return ptr;
91  }
92 
101  void do_deallocate(void* ptr,
102  [[maybe_unused]] std::size_t bytes,
103  rmm::cuda_stream_view stream) noexcept override
104  {
105  if (ptr != nullptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeAsync(ptr, stream.value())); }
106  }
107 
115  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
116  {
117  return dynamic_cast<cuda_async_view_memory_resource const*>(&other) != nullptr;
118  }
119 };
120  // end of group
122 } // namespace mr
123 } // 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:30
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:58
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
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:83