host_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2025, 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/detail/cuda_memory_resource.hpp>
19 #include <rmm/detail/export.hpp>
20 #include <rmm/detail/nvtx/ranges.hpp>
21 #include <rmm/resource_ref.hpp>
22 
23 #include <cstddef>
24 
25 namespace RMM_NAMESPACE {
26 namespace mr {
58  public:
59  host_memory_resource() = default;
60  virtual ~host_memory_resource() = default;
64  default;
65  host_memory_resource& operator=(host_memory_resource&&) noexcept =
66  default;
67 
80  void* allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t))
81  {
82  RMM_FUNC_RANGE();
83  return do_allocate(bytes, alignment);
84  }
85 
99  void deallocate(void* ptr,
100  std::size_t bytes,
101  std::size_t alignment = alignof(std::max_align_t)) noexcept
102  {
103  RMM_FUNC_RANGE();
104  do_deallocate(ptr, bytes, alignment);
105  }
106 
119  [[nodiscard]] bool is_equal(host_memory_resource const& other) const noexcept
120  {
121  return do_is_equal(other);
122  }
123 
131  [[nodiscard]] bool operator==(host_memory_resource const& other) const noexcept
132  {
133  return do_is_equal(other);
134  }
135 
143  [[nodiscard]] bool operator!=(host_memory_resource const& other) const noexcept
144  {
145  return !do_is_equal(other);
146  }
147 
153  friend void get_property(host_memory_resource const&, cuda::mr::host_accessible) noexcept {}
154 
155  private:
168  virtual void* do_allocate(std::size_t bytes,
169  std::size_t alignment = alignof(std::max_align_t)) = 0;
170 
184  virtual void do_deallocate(void* ptr,
185  std::size_t bytes,
186  std::size_t alignment = alignof(std::max_align_t)) noexcept = 0;
187 
200  [[nodiscard]] virtual bool do_is_equal(host_memory_resource const& other) const noexcept
201  {
202  return this == &other;
203  }
204 
205 #if CCCL_MAJOR_VERSION > 3 || (CCCL_MAJOR_VERSION == 3 && CCCL_MINOR_VERSION >= 1)
206 
207  public:
220  void* allocate_sync(std::size_t bytes, std::size_t alignment)
221  {
222  return allocate(bytes, alignment);
223  }
224 
238  void deallocate_sync(void* ptr, std::size_t bytes, std::size_t alignment)
239  {
240  return deallocate(ptr, bytes, alignment);
241  }
242 
243 #endif
244 };
245 
246 // static property checks
247 static_assert(
248  rmm::detail::polyfill::resource_with<host_memory_resource, cuda::mr::host_accessible>);
249  // end of group
251 
252 } // namespace mr
253 } // namespace RMM_NAMESPACE
Base class for host memory allocation.
Definition: host_memory_resource.hpp:57
bool is_equal(host_memory_resource const &other) const noexcept
Compare this resource to another.
Definition: host_memory_resource.hpp:119
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:131
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:153
bool operator!=(host_memory_resource const &other) const noexcept
Comparison operator with another host_memory_resource.
Definition: host_memory_resource.hpp:143
void deallocate(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:99