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 #include <rapidsmpf/memory/back_ref_mixin.hpp>
17 
18 namespace rapidsmpf {
19 
20 class BufferResource;
21 
35 class HostMemoryResource : public BackRefMixin<BufferResource> {
36  public:
37  ~HostMemoryResource() = default;
38 
41 
45 
49 
59  void* allocate_sync(std::size_t, std::size_t) {
60  RAPIDSMPF_FAIL(
61  "only async stream-ordered allocation must be used in RapidsMPF",
62  std::invalid_argument
63  );
64  }
65 
71  void deallocate_sync(void*, std::size_t, std::size_t) {
72  RAPIDSMPF_FAIL(
73  "only async stream-ordered allocation must be used in RapidsMPF",
74  std::invalid_argument
75  );
76  }
77 
89  void* allocate(
90  rmm::cuda_stream_view stream,
91  std::size_t size,
92  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT
93  );
94 
107  rmm::cuda_stream_view stream,
108  void* ptr,
109  std::size_t size,
110  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT
111  ) noexcept;
112 
122  [[nodiscard]] bool operator==(
123  [[maybe_unused]] HostMemoryResource const& other
124  ) const noexcept {
125  return true;
126  }
127 
129  [[nodiscard]] bool operator!=(
130  [[maybe_unused]] HostMemoryResource const& other
131  ) const noexcept {
132  return false;
133  }
134 
140  friend void get_property(
141  HostMemoryResource const&, cuda::mr::host_accessible
142  ) noexcept {}
143 
144  private:
146  HostMemoryResource() = default;
147 
148  friend class BufferResource;
149 };
150 
151 static_assert(cuda::mr::resource<HostMemoryResource>);
152 static_assert(cuda::mr::resource_with<HostMemoryResource, cuda::mr::host_accessible>);
153 static_assert(!cuda::mr::resource_with<HostMemoryResource, cuda::mr::device_accessible>);
154 
155 } // namespace rapidsmpf
Mixin that lets copies of the this object keep an external object reference of type BackRef alive.
Class managing buffer resources.
Host memory resource using standard CPU allocation.
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.
bool operator!=([[maybe_unused]] HostMemoryResource const &other) const noexcept
Compares this resource to another resource.
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.
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.
HostMemoryResource(HostMemoryResource &&)=default
Movable.
bool operator==([[maybe_unused]] HostMemoryResource const &other) const noexcept
Compares this resource to another resource.
friend void get_property(HostMemoryResource const &, cuda::mr::host_accessible) noexcept
Enables the cuda::mr::host_accessible property.
HostMemoryResource(HostMemoryResource const &)=default
Copyable.
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:14