host_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/detail/cuda_memory_resource.hpp>
8 #include <rmm/detail/export.hpp>
9 #include <rmm/detail/nvtx/ranges.hpp>
10 #include <rmm/resource_ref.hpp>
11 
12 #include <cstddef>
13 
14 namespace RMM_NAMESPACE {
15 namespace mr {
47 class [[deprecated("host_memory_resource is deprecated in 25.12 and will be removed in 26.02.")]]
49  public:
50  host_memory_resource() = default;
51  virtual ~host_memory_resource() = default;
55  default;
56  host_memory_resource& operator=(host_memory_resource&&) noexcept =
57  default;
58 
59 #ifdef RMM_ENABLE_LEGACY_MR_INTERFACE
74 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
75  [[deprecated(
76  "This function is deprecated. Use allocate_sync(std::size_t bytes, std::size_t alignment) "
77  "instead.")]]
78 #endif
79  void* allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t))
80  {
81  RMM_FUNC_RANGE();
82  return do_allocate(bytes, alignment);
83  }
84 
100 #ifdef RMM_DEPRECATE_LEGACY_MR_INTERFACE
101  [[deprecated(
102  "This function is deprecated. Use deallocate_sync(void* ptr, std::size_t bytes, std::size_t "
103  "alignment) instead.")]]
104 #endif
105  void deallocate(void* ptr,
106  std::size_t bytes,
107  std::size_t alignment = alignof(std::max_align_t)) noexcept
108  {
109  RMM_FUNC_RANGE();
110  do_deallocate(ptr, bytes, alignment);
111  }
112 #endif // RMM_ENABLE_LEGACY_MR_INTERFACE
113 
126  void* allocate_sync(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t))
127  {
128  RMM_FUNC_RANGE();
129  return do_allocate(bytes, alignment);
130  }
131 
145  void deallocate_sync(void* ptr,
146  std::size_t bytes,
147  std::size_t alignment = alignof(std::max_align_t)) noexcept
148  {
149  RMM_FUNC_RANGE();
150  do_deallocate(ptr, bytes, alignment);
151  }
152 
165  [[nodiscard]] bool is_equal(host_memory_resource const& other) const noexcept
166  {
167  return do_is_equal(other);
168  }
169 
177  [[nodiscard]] bool operator==(host_memory_resource const& other) const noexcept
178  {
179  return do_is_equal(other);
180  }
181 
189  [[nodiscard]] bool operator!=(host_memory_resource const& other) const noexcept
190  {
191  return !do_is_equal(other);
192  }
193 
199  friend void get_property(host_memory_resource const&, cuda::mr::host_accessible) noexcept {}
200 
201  private:
214  virtual void* do_allocate(std::size_t bytes,
215  std::size_t alignment = alignof(std::max_align_t)) = 0;
216 
230  virtual void do_deallocate(void* ptr,
231  std::size_t bytes,
232  std::size_t alignment = alignof(std::max_align_t)) noexcept = 0;
233 
246  [[nodiscard]] virtual bool do_is_equal(host_memory_resource const& other) const noexcept
247  {
248  return this == &other;
249  }
250 };
251 
252 // static property checks
253 static_assert(
254  rmm::detail::polyfill::resource_with<host_memory_resource, cuda::mr::host_accessible>);
255  // end of group
257 
258 } // namespace mr
259 } // namespace RMM_NAMESPACE
Base class for host memory allocation.
Definition: host_memory_resource.hpp:48
bool is_equal(host_memory_resource const &other) const noexcept
Compare this resource to another.
Definition: host_memory_resource.hpp:165
void * allocate_sync(std::size_t bytes, std::size_t alignment=alignof(std::max_align_t))
Allocates memory on the host of size at least bytes bytes.
Definition: host_memory_resource.hpp:126
host_memory_resource(host_memory_resource &&) noexcept=default
Default move constructor.
host_memory_resource(host_memory_resource const &)=default
Default copy constructor.
bool operator==(host_memory_resource const &other) const noexcept
Comparison operator with another host_memory_resource.
Definition: host_memory_resource.hpp:177
friend void get_property(host_memory_resource const &, cuda::mr::host_accessible) noexcept
Enables the cuda::mr::host_accessible property.
Definition: host_memory_resource.hpp:199
void deallocate_sync(void *ptr, std::size_t bytes, std::size_t alignment=alignof(std::max_align_t)) noexcept
Deallocate memory pointed to by ptr.
Definition: host_memory_resource.hpp:145
bool operator!=(host_memory_resource const &other) const noexcept
Comparison operator with another host_memory_resource.
Definition: host_memory_resource.hpp:189