All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
pinned_host_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024-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/detail/aligned.hpp>
20 #include <rmm/detail/error.hpp>
21 #include <rmm/detail/export.hpp>
22 #include <rmm/detail/nvtx/ranges.hpp>
23 
24 #include <cuda/memory_resource>
25 #include <cuda/stream_ref>
26 #include <cuda_runtime_api.h>
27 
28 #include <cstddef>
29 
30 namespace RMM_NAMESPACE {
31 namespace mr {
32 
47  public:
48  // Disable clang-tidy complaining about the easily swappable size and alignment parameters
49  // of allocate and deallocate
50  // NOLINTBEGIN(bugprone-easily-swappable-parameters)
51 
65  static void* allocate(std::size_t bytes,
66  [[maybe_unused]] std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT)
67  {
68  RMM_FUNC_RANGE();
69 
70  // don't allocate anything if the user requested zero bytes
71  if (0 == bytes) { return nullptr; }
72 
73  return rmm::detail::aligned_host_allocate(bytes, alignment, [](std::size_t size) {
74  void* ptr{nullptr};
75  RMM_CUDA_TRY_ALLOC(cudaHostAlloc(&ptr, size, cudaHostAllocDefault), size);
76  return ptr;
77  });
78  }
79 
87  static void deallocate(void* ptr,
88  std::size_t bytes,
89  std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT) noexcept
90  {
91  RMM_FUNC_RANGE();
92 
93  rmm::detail::aligned_host_deallocate(
94  ptr, bytes, alignment, [](void* ptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeHost(ptr)); });
95  }
96 
111  static void* allocate_async(std::size_t bytes, [[maybe_unused]] cuda::stream_ref stream)
112  {
113  RMM_FUNC_RANGE();
114 
115  return allocate(bytes);
116  }
117 
133  static void* allocate_async(std::size_t bytes,
134  std::size_t alignment,
135  [[maybe_unused]] cuda::stream_ref stream)
136  {
137  RMM_FUNC_RANGE();
138 
139  return allocate(bytes, alignment);
140  }
141 
151  static void deallocate_async(void* ptr,
152  std::size_t bytes,
153  [[maybe_unused]] cuda::stream_ref stream) noexcept
154  {
155  RMM_FUNC_RANGE();
156 
157  return deallocate(ptr, bytes);
158  }
159 
171  static void deallocate_async(void* ptr,
172  std::size_t bytes,
173  std::size_t alignment,
174  [[maybe_unused]] cuda::stream_ref stream) noexcept
175  {
176  RMM_FUNC_RANGE();
177 
178  return deallocate(ptr, bytes, alignment);
179  }
180  // NOLINTEND(bugprone-easily-swappable-parameters)
181 
185  bool operator==(const pinned_host_memory_resource&) const { return true; }
186 
191  bool operator!=(const pinned_host_memory_resource&) const { return false; }
192 
198  friend void get_property(pinned_host_memory_resource const&, cuda::mr::device_accessible) noexcept
199  {
200  }
201 
207  friend void get_property(pinned_host_memory_resource const&, cuda::mr::host_accessible) noexcept
208  {
209  }
210 };
211 
212 static_assert(cuda::mr::async_resource_with<pinned_host_memory_resource,
213  cuda::mr::device_accessible,
214  cuda::mr::host_accessible>);
215  // end of group
217 } // namespace mr
218 } // namespace RMM_NAMESPACE
Memory resource class for allocating pinned host memory.
Definition: pinned_host_memory_resource.hpp:46
bool operator==(const pinned_host_memory_resource &) const
true if the specified resource is the same type as this resource.
Definition: pinned_host_memory_resource.hpp:185
static void deallocate_async(void *ptr, std::size_t bytes, [[maybe_unused]] cuda::stream_ref stream) noexcept
Deallocate memory pointed to by ptr of size bytes bytes.
Definition: pinned_host_memory_resource.hpp:151
static void * allocate_async(std::size_t bytes, [[maybe_unused]] cuda::stream_ref stream)
Allocates pinned host memory of size at least bytes bytes.
Definition: pinned_host_memory_resource.hpp:111
static void * allocate(std::size_t bytes, [[maybe_unused]] std::size_t alignment=rmm::RMM_DEFAULT_HOST_ALIGNMENT)
Allocates pinned host memory of size at least bytes bytes.
Definition: pinned_host_memory_resource.hpp:65
static void deallocate(void *ptr, std::size_t bytes, std::size_t alignment=rmm::RMM_DEFAULT_HOST_ALIGNMENT) noexcept
Deallocate memory pointed to by ptr of size bytes bytes.
Definition: pinned_host_memory_resource.hpp:87
static void deallocate_async(void *ptr, std::size_t bytes, std::size_t alignment, [[maybe_unused]] cuda::stream_ref stream) noexcept
Deallocate memory pointed to by ptr of size bytes bytes and alignment alignment bytes.
Definition: pinned_host_memory_resource.hpp:171
static void * allocate_async(std::size_t bytes, std::size_t alignment, [[maybe_unused]] cuda::stream_ref stream)
Allocates pinned host memory of size at least bytes bytes and alignment alignment.
Definition: pinned_host_memory_resource.hpp:133
bool operator!=(cuda_stream_view lhs, cuda_stream_view rhs)
Inequality comparison operator for streams.
Definition: cuda_stream_view.hpp:187
static constexpr std::size_t RMM_DEFAULT_HOST_ALIGNMENT
Default alignment used for host memory allocated by RMM.
Definition: aligned.hpp:37