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
61 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
62  [[deprecated(
63  "This function is deprecated. Use allocate(cuda_stream_view stream, std::size_t bytes, "
64  "std::size_t alignment) instead.")]]
65 #endif
66  [[nodiscard]] void*
67  allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view)
68  {
69  return do_allocate(bytes, alignment);
70  }
71 
83 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
84  [[deprecated(
85  "This function is deprecated. Use allocate(cuda_stream_view stream, std::size_t bytes, "
86  "std::size_t alignment) instead.")]]
87 #endif
88  [[nodiscard]] void*
89  allocate_async(std::size_t bytes, cuda_stream_view)
90  {
91  return do_allocate(bytes);
92  }
93 
104 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
105  [[deprecated(
106  "This function is deprecated. Use deallocate(cuda_stream_view stream, void* ptr, std::size_t "
107  "bytes, std::size_t alignment) instead.")]]
108 #endif
109  void deallocate_async(void* ptr,
110  std::size_t bytes,
111  std::size_t alignment,
112  cuda_stream_view) noexcept
113  {
114  do_deallocate(ptr, bytes);
115  }
116 
117  // Explicitly inherit the allocate and deallocate functions from the host_memory_resource class.
118  // Due to inheritance and name hiding rules, we need to declare these with "using" when we
119  // override allocate and deallocate for CCCL 3.1.0+ compatibility.
120  // Note: These methods are deprecated. Use allocate_sync/deallocate_sync or the stream-based
121  // overloads instead.
122  using host_memory_resource::allocate;
123  using host_memory_resource::deallocate;
124 #endif // RMM_ENABLE_LEGACY_MR_INTERFACE
125 
138  std::size_t bytes,
139  std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT)
140  {
141  return do_allocate(bytes, alignment);
142  }
143 
154  void* ptr,
155  std::size_t bytes,
156  std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT) noexcept
157  {
158  return do_deallocate(ptr, bytes, alignment);
159  }
160 
166  friend void get_property(pinned_memory_resource const&, cuda::mr::device_accessible) noexcept {}
167 
168  private:
181  void* do_allocate(std::size_t bytes,
182  std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT) override
183  {
184  // don't allocate anything if the user requested zero bytes
185  if (0 == bytes) { return nullptr; }
186 
187  RMM_EXPECTS(rmm::is_supported_alignment(alignment),
188  "Allocation alignment is not a power of 2.",
190 
191  return rmm::detail::aligned_host_allocate(bytes, alignment, [](std::size_t size) {
192  void* ptr{nullptr};
193  RMM_CUDA_TRY_ALLOC(cudaMallocHost(&ptr, size), size);
194  return ptr;
195  });
196  }
197 
211  void do_deallocate(void* ptr,
212  std::size_t bytes,
213  std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT) noexcept override
214  {
215  if (nullptr == ptr) { return; }
216  rmm::detail::aligned_host_deallocate(
217  ptr, bytes, alignment, [](void* ptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeHost(ptr)); });
218  }
219 };
220 
221 // static property checks
222 static_assert(rmm::detail::polyfill::async_resource_with<pinned_memory_resource,
223  cuda::mr::host_accessible,
224  cuda::mr::device_accessible>);
225  // end of group
227 } // namespace mr
228 } // 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:166
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:137
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:153
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:27