prefetch_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/detail/export.hpp>
9 #include <rmm/prefetch.hpp>
10 #include <rmm/resource_ref.hpp>
11 
12 #include <cstddef>
13 #include <memory>
14 
15 namespace RMM_NAMESPACE {
16 namespace mr {
28 template <typename Upstream>
30  public:
37  prefetch_resource_adaptor(device_async_resource_ref upstream) : upstream_{upstream} {}
38 
47  prefetch_resource_adaptor(Upstream* upstream)
48  : upstream_{to_device_async_resource_ref_checked(upstream)}
49  {
50  }
51 
52  prefetch_resource_adaptor() = delete;
53  ~prefetch_resource_adaptor() override = default;
55  prefetch_resource_adaptor& operator=(prefetch_resource_adaptor const&) = delete;
57  default;
59  default;
60 
64  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
65  {
66  return upstream_;
67  }
68 
69  private:
83  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
84  {
85  void* ptr = get_upstream_resource().allocate(stream, bytes);
86  rmm::prefetch(ptr, bytes, rmm::get_current_cuda_device(), stream);
87  return ptr;
88  }
89 
97  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
98  {
99  get_upstream_resource().deallocate(stream, ptr, bytes);
100  }
101 
109  bool do_is_equal(device_memory_resource const& other) const noexcept override
110  {
111  if (this == std::addressof(other)) { return true; }
112  auto cast = dynamic_cast<prefetch_resource_adaptor<Upstream> const*>(&other);
113  if (cast == nullptr) { return false; }
114  return get_upstream_resource() == cast->get_upstream_resource();
115  }
116 
117  // the upstream resource used for satisfying allocation requests
118  device_async_resource_ref upstream_;
119 };
120  // end of group
122 } // namespace mr
123 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:83
Resource that prefetches all memory allocations.
Definition: prefetch_resource_adaptor.hpp:29
prefetch_resource_adaptor(prefetch_resource_adaptor &&) noexcept=default
Default move constructor.
prefetch_resource_adaptor(Upstream *upstream)
Construct a new prefetch resource adaptor using upstream to satisfy allocation requests.
Definition: prefetch_resource_adaptor.hpp:47
prefetch_resource_adaptor(device_async_resource_ref upstream)
Construct a new prefetch resource adaptor using upstream to satisfy allocation requests.
Definition: prefetch_resource_adaptor.hpp:37
cuda_device_id get_current_cuda_device()
Returns a cuda_device_id for the current device.
device_async_resource_ref to_device_async_resource_ref_checked(Resource *res)
Convert pointer to memory resource into device_async_resource_ref, checking for nullptr
Definition: resource_ref.hpp:72
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::device_accessible > > device_async_resource_ref
Alias for a cuda::mr::async_resource_ref with the property cuda::mr::device_accessible.
Definition: resource_ref.hpp:32
void prefetch(void const *ptr, std::size_t size, rmm::cuda_device_id device, rmm::cuda_stream_view stream)
Prefetch memory to the specified device on the specified stream.
RAPIDS Memory Manager - The top-level namespace for all RMM functionality.