host_memory_resource.hpp
1 
5 #pragma once
6 
7 #include <cstddef>
8 #include <cstdlib>
9 #include <cstring>
10 
11 #include <rmm/aligned.hpp>
12 #include <rmm/cuda_stream_view.hpp>
13 #include <rmm/resource_ref.hpp>
14 
15 #include <rapidsmpf/error.hpp>
16 
17 namespace rapidsmpf {
18 
33  public:
35  HostMemoryResource() = default;
36 
38  virtual ~HostMemoryResource() = default;
39 
42 
46 
50 
60  void* allocate_sync(std::size_t, std::size_t) {
61  RAPIDSMPF_FAIL(
62  "only async stream-ordered allocation must be used in RapidsMPF",
63  std::invalid_argument
64  );
65  }
66 
72  void deallocate_sync(void*, std::size_t, std::size_t) {
73  RAPIDSMPF_FAIL(
74  "only async stream-ordered allocation must be used in RapidsMPF",
75  std::invalid_argument
76  );
77  }
78 
92  virtual void* allocate(
93  rmm::cuda_stream_view stream,
94  std::size_t size,
95  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT
96  );
97 
112  virtual void deallocate(
113  rmm::cuda_stream_view stream,
114  void* ptr,
115  std::size_t size,
116  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT
117  ) noexcept;
118 
136  [[nodiscard]] virtual bool is_equal(
137  [[maybe_unused]] HostMemoryResource const& other
138  ) const noexcept {
139  return true;
140  }
141 
143  [[nodiscard]] bool operator==(HostMemoryResource const& other) const noexcept {
144  return is_equal(other);
145  }
146 
148  [[nodiscard]] bool operator!=(HostMemoryResource const& other) const noexcept {
149  return !is_equal(other);
150  }
151 
157  friend void get_property(
158  HostMemoryResource const&, cuda::mr::host_accessible
159  ) noexcept {}
160 };
161 
162 static_assert(cuda::mr::resource<HostMemoryResource>);
163 static_assert(cuda::mr::resource_with<HostMemoryResource, cuda::mr::host_accessible>);
164 static_assert(!cuda::mr::resource_with<HostMemoryResource, cuda::mr::device_accessible>);
165 
166 } // namespace rapidsmpf
Host memory resource using standard CPU allocation.
virtual void deallocate(rmm::cuda_stream_view stream, void *ptr, std::size_t size, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
Deallocates host memory associated with a CUDA stream.
virtual ~HostMemoryResource()=default
Virtual destructor to allow polymorphic use.
HostMemoryResource & operator=(HostMemoryResource const &)=default
Copy assignment.
void * allocate_sync(std::size_t, std::size_t)
Synchronously allocates host memory is disabled.
HostMemoryResource & operator=(HostMemoryResource &&)=default
Move assignment.
void deallocate_sync(void *, std::size_t, std::size_t)
Synchronously deallocates host memory is disabled.
bool operator==(HostMemoryResource const &other) const noexcept
Compares this resource to another resource.
HostMemoryResource(HostMemoryResource &&)=default
Movable.
virtual bool is_equal([[maybe_unused]] HostMemoryResource const &other) const noexcept
Compares this resource to another resource.
virtual void * allocate(rmm::cuda_stream_view stream, std::size_t size, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT)
Allocates host memory associated with a CUDA stream.
friend void get_property(HostMemoryResource const &, cuda::mr::host_accessible) noexcept
Enables the cuda::mr::host_accessible property.
HostMemoryResource()=default
Default constructor.
bool operator!=(HostMemoryResource const &other) const noexcept
Compares this resource to another resource.
HostMemoryResource(HostMemoryResource const &)=default
Copyable.
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:13