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
72  void* allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t))
73  {
74  RMM_FUNC_RANGE();
75  return do_allocate(bytes, alignment);
76  }
77 
91  void deallocate(void* ptr,
92  std::size_t bytes,
93  std::size_t alignment = alignof(std::max_align_t)) noexcept
94  {
95  RMM_FUNC_RANGE();
96  do_deallocate(ptr, bytes, alignment);
97  }
98 #endif // RMM_ENABLE_LEGACY_MR_INTERFACE
99 
112  void* allocate_sync(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t))
113  {
114  RMM_FUNC_RANGE();
115  return do_allocate(bytes, alignment);
116  }
117 
131  void deallocate_sync(void* ptr,
132  std::size_t bytes,
133  std::size_t alignment = alignof(std::max_align_t)) noexcept
134  {
135  RMM_FUNC_RANGE();
136  do_deallocate(ptr, bytes, alignment);
137  }
138 
151  [[nodiscard]] bool is_equal(host_memory_resource const& other) const noexcept
152  {
153  return do_is_equal(other);
154  }
155 
163  [[nodiscard]] bool operator==(host_memory_resource const& other) const noexcept
164  {
165  return do_is_equal(other);
166  }
167 
175  [[nodiscard]] bool operator!=(host_memory_resource const& other) const noexcept
176  {
177  return !do_is_equal(other);
178  }
179 
185  friend void get_property(host_memory_resource const&, cuda::mr::host_accessible) noexcept {}
186 
187  private:
200  virtual void* do_allocate(std::size_t bytes,
201  std::size_t alignment = alignof(std::max_align_t)) = 0;
202 
216  virtual void do_deallocate(void* ptr,
217  std::size_t bytes,
218  std::size_t alignment = alignof(std::max_align_t)) noexcept = 0;
219 
232  [[nodiscard]] virtual bool do_is_equal(host_memory_resource const& other) const noexcept
233  {
234  return this == &other;
235  }
236 };
237 
238 // static property checks
239 static_assert(
240  rmm::detail::polyfill::resource_with<host_memory_resource, cuda::mr::host_accessible>);
241  // end of group
243 
244 } // namespace mr
245 } // 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:151
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:112
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:163
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:185
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:131
bool operator!=(host_memory_resource const &other) const noexcept
Comparison operator with another host_memory_resource.
Definition: host_memory_resource.hpp:175