pinned_host_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2025, 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/cccl_adaptors.hpp>
10 #include <rmm/detail/cuda_memory_resource.hpp>
11 #include <rmm/detail/error.hpp>
12 #include <rmm/detail/export.hpp>
13 #include <rmm/detail/nvtx/ranges.hpp>
15 
16 #include <cuda/stream_ref>
17 #include <cuda_runtime_api.h>
18 
19 #include <cstddef>
20 
21 namespace RMM_NAMESPACE {
22 namespace mr {
23 
38  public:
39  pinned_host_memory_resource() = default;
40  ~pinned_host_memory_resource() override = default;
42  default;
44  default;
46  default;
48  default;
49 
50  private:
66  void* do_allocate(std::size_t bytes, [[maybe_unused]] cuda_stream_view stream) override
67  {
68  // don't allocate anything if the user requested zero bytes
69  if (0 == bytes) { return nullptr; }
70 
71  // TODO: Use the alignment parameter as an argument to do_allocate
72  std::size_t constexpr alignment = rmm::CUDA_ALLOCATION_ALIGNMENT;
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 
90  void do_deallocate(void* ptr,
91  std::size_t bytes,
92  [[maybe_unused]] cuda_stream_view stream) noexcept override
93  {
94  // TODO: Use the alignment parameter as an argument to do_deallocate
95  std::size_t constexpr alignment = rmm::CUDA_ALLOCATION_ALIGNMENT;
96  rmm::detail::aligned_host_deallocate(
97  ptr, bytes, alignment, [](void* ptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeHost(ptr)); });
98  }
99 
110  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
111  {
112  return dynamic_cast<pinned_host_memory_resource const*>(&other) != nullptr;
113  }
114 
120  friend void get_property(pinned_host_memory_resource const&, cuda::mr::device_accessible) noexcept
121  {
122  }
123 
129  friend void get_property(pinned_host_memory_resource const&, cuda::mr::host_accessible) noexcept
130  {
131  }
132 };
133 
134 static_assert(rmm::detail::polyfill::async_resource_with<pinned_host_memory_resource,
135  cuda::mr::device_accessible,
136  cuda::mr::host_accessible>);
137  // end of group
139 } // namespace mr
140 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:83
Memory resource class for allocating pinned host memory.
Definition: pinned_host_memory_resource.hpp:37
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:129
pinned_host_memory_resource & operator=(pinned_host_memory_resource &&)=default
Default move assignment operator.
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:120
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:31