prefetch_resource_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2025, 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 
14 namespace RMM_NAMESPACE {
15 namespace mr {
27 template <typename Upstream>
29  public:
36  prefetch_resource_adaptor(device_async_resource_ref upstream) : upstream_{upstream} {}
37 
46  prefetch_resource_adaptor(Upstream* upstream)
47  : upstream_{to_device_async_resource_ref_checked(upstream)}
48  {
49  }
50 
51  prefetch_resource_adaptor() = delete;
52  ~prefetch_resource_adaptor() override = default;
54  prefetch_resource_adaptor& operator=(prefetch_resource_adaptor const&) = delete;
56  default;
58  default;
59 
63  [[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
64  {
65  return upstream_;
66  }
67 
68  private:
82  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
83  {
84  void* ptr = get_upstream_resource().allocate(stream, bytes);
85  rmm::prefetch(ptr, bytes, rmm::get_current_cuda_device(), stream);
86  return ptr;
87  }
88 
96  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
97  {
98  get_upstream_resource().deallocate(stream, ptr, bytes);
99  }
100 
108  bool do_is_equal(device_memory_resource const& other) const noexcept override
109  {
110  if (this == &other) { return true; }
111  auto cast = dynamic_cast<prefetch_resource_adaptor<Upstream> const*>(&other);
112  if (cast == nullptr) { return false; }
113  return get_upstream_resource() == cast->get_upstream_resource();
114  }
115 
116  // the upstream resource used for satisfying allocation requests
117  device_async_resource_ref upstream_;
118 };
119  // end of group
121 } // namespace mr
122 } // 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
void * allocate(cuda_stream_view stream, std::size_t bytes, std::size_t alignment=rmm::CUDA_ALLOCATION_ALIGNMENT)
Allocates memory of size at least bytes on the specified stream.
Definition: device_memory_resource.hpp:322
Resource that prefetches all memory allocations.
Definition: prefetch_resource_adaptor.hpp:28
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:46
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:36
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.