host_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2021, 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 <cuda/memory_resource>
19 
20 #include <cstddef>
21 #include <utility>
22 
23 namespace rmm::mr {
55  public:
56  host_memory_resource() = default;
57  virtual ~host_memory_resource() = default;
61  default;
62  host_memory_resource& operator=(host_memory_resource&&) noexcept =
63  default;
64 
77  void* allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t))
78  {
79  return do_allocate(bytes, alignment);
80  }
81 
95  void deallocate(void* ptr, std::size_t bytes, std::size_t alignment = alignof(std::max_align_t))
96  {
97  do_deallocate(ptr, bytes, alignment);
98  }
99 
112  [[nodiscard]] bool is_equal(host_memory_resource const& other) const noexcept
113  {
114  return do_is_equal(other);
115  }
116 
124  [[nodiscard]] bool operator==(host_memory_resource const& other) const noexcept
125  {
126  return do_is_equal(other);
127  }
128 
136  [[nodiscard]] bool operator!=(host_memory_resource const& other) const noexcept
137  {
138  return !do_is_equal(other);
139  }
140 
146  friend void get_property(host_memory_resource const&, cuda::mr::host_accessible) noexcept {}
147 
148  private:
161  virtual void* do_allocate(std::size_t bytes,
162  std::size_t alignment = alignof(std::max_align_t)) = 0;
163 
177  virtual void do_deallocate(void* ptr,
178  std::size_t bytes,
179  std::size_t alignment = alignof(std::max_align_t)) = 0;
180 
193  [[nodiscard]] virtual bool do_is_equal(host_memory_resource const& other) const noexcept
194  {
195  return this == &other;
196  }
197 };
198 static_assert(cuda::mr::resource_with<host_memory_resource, cuda::mr::host_accessible>); // end of group
200 
201 } // namespace rmm::mr
Base class for host memory allocation.
Definition: host_memory_resource.hpp:54
bool is_equal(host_memory_resource const &other) const noexcept
Compare this resource to another.
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 device_memory_resource.
Definition: host_memory_resource.hpp:124
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:146
void deallocate(void *ptr, std::size_t bytes, std::size_t alignment=alignof(std::max_align_t))
Deallocate memory pointed to by ptr.
Definition: host_memory_resource.hpp:95
void * allocate(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:77
bool operator!=(host_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: host_memory_resource.hpp:136