pinned_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <rmm/aligned.hpp>
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/detail/aligned.hpp>
21 #include <rmm/detail/error.hpp>
22 #include <rmm/detail/export.hpp>
24 
25 #include <cstddef>
26 
27 namespace RMM_NAMESPACE {
28 namespace mr {
42  public:
43  pinned_memory_resource() = default;
44  ~pinned_memory_resource() override = default;
48  default;
50  default;
51 
62  [[nodiscard]] void* allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view)
63  {
64  return do_allocate(bytes, alignment);
65  }
66 
76  [[nodiscard]] void* allocate_async(std::size_t bytes, cuda_stream_view)
77  {
78  return do_allocate(bytes);
79  }
80 
89  void deallocate_async(void* ptr,
90  std::size_t bytes,
91  std::size_t alignment,
92  cuda_stream_view) noexcept
93  {
94  do_deallocate(ptr, rmm::align_up(bytes, alignment));
95  }
96 
102  friend void get_property(pinned_memory_resource const&, cuda::mr::device_accessible) noexcept {}
103 
104  private:
117  void* do_allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) override
118  {
119  // don't allocate anything if the user requested zero bytes
120  if (0 == bytes) { return nullptr; }
121 
122  // If the requested alignment isn't supported, use default
123  alignment =
125 
126  return rmm::detail::aligned_host_allocate(bytes, alignment, [](std::size_t size) {
127  void* ptr{nullptr};
128  RMM_CUDA_TRY_ALLOC(cudaMallocHost(&ptr, size), size);
129  return ptr;
130  });
131  }
132 
146  void do_deallocate(void* ptr,
147  std::size_t bytes,
148  std::size_t alignment = alignof(std::max_align_t)) noexcept override
149  {
150  if (nullptr == ptr) { return; }
151  rmm::detail::aligned_host_deallocate(
152  ptr, bytes, alignment, [](void* ptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeHost(ptr)); });
153  }
154 
155 #if CCCL_MAJOR_VERSION > 3 || (CCCL_MAJOR_VERSION == 3 && CCCL_MINOR_VERSION >= 1)
156 
157  public:
158  // Explicitly inherit the allocate and deallocate functions from the host_memory_resource class.
159  // Due to inheritance and name hiding rules, we need to declare these with "using" when we
160  // override allocate and deallocate for CCCL 3.1.0+ compatibility.
161  using host_memory_resource::allocate;
162  using host_memory_resource::deallocate;
163 
175  void* allocate(cuda_stream_view stream, std::size_t bytes, std::size_t alignment)
176  {
177  return this->allocate_async(bytes, alignment, stream);
178  }
179 
189  void deallocate(cuda_stream_view stream,
190  void* ptr,
191  std::size_t bytes,
192  std::size_t alignment) noexcept
193  {
194  return this->deallocate_async(ptr, bytes, alignment, stream);
195  }
196 
197 #endif
198 };
199 
200 // static property checks
201 static_assert(rmm::detail::polyfill::async_resource_with<pinned_memory_resource,
202  cuda::mr::host_accessible,
203  cuda::mr::device_accessible>);
204  // end of group
206 } // namespace mr
207 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:39
Base class for host memory allocation.
Definition: host_memory_resource.hpp:57
A host_memory_resource that uses cudaMallocHost to allocate pinned/page-locked host memory.
Definition: pinned_memory_resource.hpp:41
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:102
void * allocate_async(std::size_t bytes, cuda_stream_view)
Pretend to support the allocate_async interface, falling back to stream 0.
Definition: pinned_memory_resource.hpp:76
pinned_memory_resource & operator=(pinned_memory_resource const &)=default
Default copy assignment operator.
pinned_memory_resource(pinned_memory_resource &&)=default
Default move constructor.
pinned_memory_resource & operator=(pinned_memory_resource &&)=default
Default move assignment operator.
void deallocate_async(void *ptr, std::size_t bytes, std::size_t alignment, cuda_stream_view) noexcept
Pretend to support the deallocate_async interface, falling back to stream 0.
Definition: pinned_memory_resource.hpp:89
void * allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view)
Pretend to support the allocate_async interface, falling back to stream 0.
Definition: pinned_memory_resource.hpp:62
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:37
std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
Align up to nearest multiple of specified power of 2.