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/cccl_adaptors.hpp>
10 #include <rmm/detail/error.hpp>
11 #include <rmm/detail/export.hpp>
12 #include <rmm/detail/nvtx/ranges.hpp>
14 
15 #include <cuda/stream_ref>
16 #include <cuda_runtime_api.h>
17 
18 #include <cstddef>
19 
20 namespace RMM_NAMESPACE {
21 namespace mr {
22 
37  public:
38  pinned_host_memory_resource() = default;
39  ~pinned_host_memory_resource() override = default;
41  default;
43  default;
45  default;
47  default;
48 
49  private:
65  void* do_allocate(std::size_t bytes, [[maybe_unused]] cuda_stream_view stream) override
66  {
67  // don't allocate anything if the user requested zero bytes
68  if (0 == bytes) { return nullptr; }
69 
70  // TODO: Use the alignment parameter as an argument to do_allocate
71  std::size_t constexpr alignment = rmm::CUDA_ALLOCATION_ALIGNMENT;
72  return rmm::detail::aligned_host_allocate(bytes, alignment, [](std::size_t size) {
73  void* ptr{nullptr};
74  RMM_CUDA_TRY_ALLOC(cudaHostAlloc(&ptr, size, cudaHostAllocDefault), size);
75  return ptr;
76  });
77  }
78 
89  void do_deallocate(void* ptr,
90  std::size_t bytes,
91  [[maybe_unused]] cuda_stream_view stream) noexcept override
92  {
93  // TODO: Use the alignment parameter as an argument to do_deallocate
94  std::size_t constexpr alignment = rmm::CUDA_ALLOCATION_ALIGNMENT;
95  rmm::detail::aligned_host_deallocate(ptr, bytes, alignment, [](void* ptr) {
96  RMM_ASSERT_CUDA_SUCCESS_SAFE_SHUTDOWN(cudaFreeHost(ptr));
97  });
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(cuda::mr::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:84
Memory resource class for allocating pinned host memory.
Definition: pinned_host_memory_resource.hpp:36
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:25