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, 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 #include <utility>
30 
31 namespace RMM_NAMESPACE {
32 namespace mr {
33 
48  public:
49  // Disable clang-tidy complaining about the easily swappable size and alignment parameters
50  // of allocate and deallocate
51  // NOLINTBEGIN(bugprone-easily-swappable-parameters)
52 
66  static void* allocate(std::size_t bytes,
67  [[maybe_unused]] std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT)
68  {
69  RMM_FUNC_RANGE();
70 
71  // don't allocate anything if the user requested zero bytes
72  if (0 == bytes) { return nullptr; }
73 
74  return rmm::detail::aligned_host_allocate(bytes, alignment, [](std::size_t size) {
75  void* ptr{nullptr};
76  RMM_CUDA_TRY_ALLOC(cudaHostAlloc(&ptr, size, cudaHostAllocDefault));
77  return ptr;
78  });
79  }
80 
88  static void deallocate(void* ptr,
89  std::size_t bytes,
90  std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT) noexcept
91  {
92  RMM_FUNC_RANGE();
93 
94  rmm::detail::aligned_host_deallocate(
95  ptr, bytes, alignment, [](void* ptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeHost(ptr)); });
96  }
97 
112  static void* allocate_async(std::size_t bytes, [[maybe_unused]] cuda::stream_ref stream)
113  {
114  RMM_FUNC_RANGE();
115 
116  return allocate(bytes);
117  }
118 
134  static void* allocate_async(std::size_t bytes,
135  std::size_t alignment,
136  [[maybe_unused]] cuda::stream_ref stream)
137  {
138  RMM_FUNC_RANGE();
139 
140  return allocate(bytes, alignment);
141  }
142 
152  static void deallocate_async(void* ptr,
153  std::size_t bytes,
154  [[maybe_unused]] cuda::stream_ref stream) noexcept
155  {
156  RMM_FUNC_RANGE();
157 
158  return deallocate(ptr, bytes);
159  }
160 
172  static void deallocate_async(void* ptr,
173  std::size_t bytes,
174  std::size_t alignment,
175  [[maybe_unused]] cuda::stream_ref stream) noexcept
176  {
177  RMM_FUNC_RANGE();
178 
179  return deallocate(ptr, bytes, alignment);
180  }
181  // NOLINTEND(bugprone-easily-swappable-parameters)
182 
186  bool operator==(const pinned_host_memory_resource&) const { return true; }
187 
192  bool operator!=(const pinned_host_memory_resource&) const { return false; }
193 
199  friend void get_property(pinned_host_memory_resource const&, cuda::mr::device_accessible) noexcept
200  {
201  }
202 
208  friend void get_property(pinned_host_memory_resource const&, cuda::mr::host_accessible) noexcept
209  {
210  }
211 };
212 
213 static_assert(cuda::mr::async_resource_with<pinned_host_memory_resource,
214  cuda::mr::device_accessible,
215  cuda::mr::host_accessible>);
216  // end of group
218 } // namespace mr
219 } // namespace RMM_NAMESPACE
Memory resource class for allocating pinned host memory.
Definition: pinned_host_memory_resource.hpp:47
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:186
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:152
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:112
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:66
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:88
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:172
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:134
bool operator!=(cuda_stream_view lhs, cuda_stream_view rhs)
Inequality comparison operator for streams.
Definition: cuda_stream_view.hpp:189
static constexpr std::size_t RMM_DEFAULT_HOST_ALIGNMENT
Default alignment used for host memory allocated by RMM.
Definition: aligned.hpp:37