pinned_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/aligned.hpp>
9 #include <rmm/detail/aligned.hpp>
10 #include <rmm/detail/error.hpp>
11 #include <rmm/detail/export.hpp>
12 #include <rmm/error.hpp>
14 
15 #include <cstddef>
16 
17 namespace RMM_NAMESPACE {
18 namespace mr {
34 class [[deprecated(
35  "pinned_memory_resource is deprecated in 25.12 and will be removed in 26.02. "
37  : public host_memory_resource {
38  public:
39  pinned_memory_resource() = default;
40  ~pinned_memory_resource() override = default;
44  default;
46  default;
47 
48 #ifdef RMM_ENABLE_LEGACY_MR_INTERFACE
59  [[nodiscard]] void* allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view)
60  {
61  return do_allocate(bytes, alignment);
62  }
63 
73  [[nodiscard]] void* allocate_async(std::size_t bytes, cuda_stream_view)
74  {
75  return do_allocate(bytes);
76  }
77 
86  void deallocate_async(void* ptr,
87  std::size_t bytes,
88  std::size_t alignment,
89  cuda_stream_view) noexcept
90  {
91  do_deallocate(ptr, bytes);
92  }
93 
94  // Explicitly inherit the allocate and deallocate functions from the host_memory_resource class.
95  // Due to inheritance and name hiding rules, we need to declare these with "using" when we
96  // override allocate and deallocate for CCCL 3.1.0+ compatibility.
97  using host_memory_resource::allocate;
98  using host_memory_resource::deallocate;
99 #endif // RMM_ENABLE_LEGACY_MR_INTERFACE
100 
113  std::size_t bytes,
114  std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT)
115  {
116  return do_allocate(bytes, alignment);
117  }
118 
129  void* ptr,
130  std::size_t bytes,
131  std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT) noexcept
132  {
133  return do_deallocate(ptr, bytes, alignment);
134  }
135 
141  friend void get_property(pinned_memory_resource const&, cuda::mr::device_accessible) noexcept {}
142 
143  private:
156  void* do_allocate(std::size_t bytes,
157  std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT) override
158  {
159  // don't allocate anything if the user requested zero bytes
160  if (0 == bytes) { return nullptr; }
161 
162  RMM_EXPECTS(rmm::is_supported_alignment(alignment),
163  "Allocation alignment is not a power of 2.",
165 
166  return rmm::detail::aligned_host_allocate(bytes, alignment, [](std::size_t size) {
167  void* ptr{nullptr};
168  RMM_CUDA_TRY_ALLOC(cudaMallocHost(&ptr, size), size);
169  return ptr;
170  });
171  }
172 
186  void do_deallocate(void* ptr,
187  std::size_t bytes,
188  std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT) noexcept override
189  {
190  if (nullptr == ptr) { return; }
191  rmm::detail::aligned_host_deallocate(
192  ptr, bytes, alignment, [](void* ptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeHost(ptr)); });
193  }
194 };
195 
196 // static property checks
197 static_assert(rmm::detail::polyfill::async_resource_with<pinned_memory_resource,
198  cuda::mr::host_accessible,
199  cuda::mr::device_accessible>);
200  // end of group
202 } // namespace mr
203 } // namespace RMM_NAMESPACE
Exception thrown when an RMM allocation fails.
Definition: error.hpp:44
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
Base class for host memory allocation.
Definition: host_memory_resource.hpp:48
Memory resource class for allocating pinned host memory.
Definition: pinned_host_memory_resource.hpp:37
A host_memory_resource that uses cudaMallocHost to allocate pinned/page-locked host memory.
Definition: pinned_memory_resource.hpp:37
friend void get_property(pinned_memory_resource const &, cuda::mr::device_accessible) noexcept
Enables the cuda::mr::device_accessible property.
Definition: pinned_memory_resource.hpp:141
pinned_memory_resource & operator=(pinned_memory_resource const &)=default
Default copy assignment operator.
void * allocate(cuda_stream_view stream, std::size_t bytes, std::size_t alignment=rmm::RMM_DEFAULT_HOST_ALIGNMENT)
Pretend to support the allocate_async interface, falling back to stream 0.
Definition: pinned_memory_resource.hpp:112
pinned_memory_resource(pinned_memory_resource &&)=default
Default move constructor.
pinned_memory_resource & operator=(pinned_memory_resource &&)=default
Default move assignment operator.
void deallocate(cuda_stream_view stream, void *ptr, std::size_t bytes, std::size_t alignment=rmm::RMM_DEFAULT_HOST_ALIGNMENT) noexcept
Pretend to support the deallocate_async interface, falling back to stream 0.
Definition: pinned_memory_resource.hpp:128
pinned_memory_resource(pinned_memory_resource const &)=default
Default copy constructor.
bool is_supported_alignment(std::size_t alignment) noexcept
Returns whether or not alignment is a valid memory alignment.
static constexpr std::size_t RMM_DEFAULT_HOST_ALIGNMENT
Default alignment used for host memory allocated by RMM.
Definition: aligned.hpp:25