pinned_host_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/aligned.hpp>
8 #include <rmm/detail/aligned.hpp>
9 #include <rmm/detail/error.hpp>
10 #include <rmm/detail/export.hpp>
11 
12 #include <cuda/memory_resource>
13 #include <cuda/stream_ref>
14 #include <cuda_runtime_api.h>
15 
16 #include <cstddef>
17 
18 namespace RMM_NAMESPACE {
19 namespace mr {
20 
35  public:
36  pinned_host_memory_resource() = default;
37  ~pinned_host_memory_resource() = default;
39  default;
41  default;
43  default;
45  default;
46 
63  void* allocate([[maybe_unused]] cuda::stream_ref stream,
64  std::size_t bytes,
65  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
66  {
67  // don't allocate anything if the user requested zero bytes
68  if (0 == bytes) { return nullptr; }
69 
70  std::size_t constexpr alloc_alignment = rmm::CUDA_ALLOCATION_ALIGNMENT;
71  return rmm::detail::aligned_host_allocate(bytes, alloc_alignment, [](std::size_t size) {
72  void* ptr{nullptr};
73  RMM_CUDA_TRY_ALLOC(cudaHostAlloc(&ptr, size, cudaHostAllocDefault), size);
74  return ptr;
75  });
76  }
77 
89  void deallocate([[maybe_unused]] cuda::stream_ref stream,
90  void* ptr,
91  std::size_t bytes,
92  [[maybe_unused]] std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
93  {
94  std::size_t constexpr alloc_alignment = rmm::CUDA_ALLOCATION_ALIGNMENT;
95  rmm::detail::aligned_host_deallocate(ptr, bytes, alloc_alignment, [](void* ptr) {
96  RMM_ASSERT_CUDA_SUCCESS_SAFE_SHUTDOWN(cudaFreeHost(ptr));
97  });
98  }
99 
107  void* allocate_sync(std::size_t bytes, std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
108  {
109  auto* ptr = allocate(cuda::stream_ref{cudaStream_t{nullptr}}, bytes, alignment);
110  RMM_CUDA_TRY(cudaStreamSynchronize(cudaStream_t{nullptr}));
111  return ptr;
112  }
113 
121  void deallocate_sync(void* ptr,
122  std::size_t bytes,
123  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
124  {
125  deallocate(cuda::stream_ref{cudaStream_t{nullptr}}, ptr, bytes, alignment);
126  }
127 
133  RMM_CONSTEXPR_FRIEND void get_property(pinned_host_memory_resource const&,
134  cuda::mr::device_accessible) noexcept
135  {
136  }
137 
143  RMM_CONSTEXPR_FRIEND void get_property(pinned_host_memory_resource const&,
144  cuda::mr::host_accessible) noexcept
145  {
146  }
147 
155  [[nodiscard]] bool operator==(pinned_host_memory_resource const&) const noexcept { return true; }
156 
160  [[nodiscard]] bool operator!=(pinned_host_memory_resource const&) const noexcept { return false; }
161 };
162 
163 // static property checks
164 static_assert(cuda::mr::synchronous_resource<pinned_host_memory_resource>);
165 static_assert(cuda::mr::resource<pinned_host_memory_resource>);
166 static_assert(
167  cuda::mr::synchronous_resource_with<pinned_host_memory_resource, cuda::mr::device_accessible>);
168 static_assert(
169  cuda::mr::synchronous_resource_with<pinned_host_memory_resource, cuda::mr::host_accessible>);
170 static_assert(cuda::mr::resource_with<pinned_host_memory_resource, cuda::mr::device_accessible>);
171 static_assert(cuda::mr::resource_with<pinned_host_memory_resource, cuda::mr::host_accessible>);
172  // end of group
174 } // namespace mr
175 } // namespace RMM_NAMESPACE
Memory resource class for allocating pinned host memory.
Definition: pinned_host_memory_resource.hpp:34
bool operator==(pinned_host_memory_resource const &) const noexcept
Compare this resource to another.
Definition: pinned_host_memory_resource.hpp:155
bool operator!=(pinned_host_memory_resource const &) const noexcept
Compare this resource to another.
Definition: pinned_host_memory_resource.hpp:160
void * allocate([[maybe_unused]] cuda::stream_ref stream, std::size_t bytes, [[maybe_unused]] std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT)
Allocates pinned host memory of size at least bytes bytes.
Definition: pinned_host_memory_resource.hpp:63
pinned_host_memory_resource(pinned_host_memory_resource &&)=default
Default move constructor.
friend void get_property(pinned_host_memory_resource const &, cuda::mr::host_accessible) noexcept
Enables the cuda::mr::host_accessible property.
Definition: pinned_host_memory_resource.hpp:143
void * allocate_sync(std::size_t bytes, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT)
Allocates pinned host memory of size at least bytes bytes synchronously.
Definition: pinned_host_memory_resource.hpp:107
pinned_host_memory_resource & operator=(pinned_host_memory_resource &&)=default
Default move assignment operator.
void deallocate([[maybe_unused]] cuda::stream_ref stream, void *ptr, std::size_t bytes, [[maybe_unused]] std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
Deallocate memory pointed to by ptr.
Definition: pinned_host_memory_resource.hpp:89
void deallocate_sync(void *ptr, std::size_t bytes, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
Deallocate memory pointed to by ptr synchronously.
Definition: pinned_host_memory_resource.hpp:121
pinned_host_memory_resource(pinned_host_memory_resource const &)=default
Default copy constructor.
friend void get_property(pinned_host_memory_resource const &, cuda::mr::device_accessible) noexcept
Enables the cuda::mr::device_accessible property.
Definition: pinned_host_memory_resource.hpp:133
pinned_host_memory_resource & operator=(pinned_host_memory_resource const &)=default
Default copy assignment operator.
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:25