owning_wrapper.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/detail/export.hpp>
9 
10 #include <memory>
11 #include <utility>
12 
13 namespace RMM_NAMESPACE {
14 namespace mr {
15 namespace detail {
31 template <typename Resource, typename UpstreamTuple, std::size_t... Indices, typename... Args>
32 auto make_resource_impl(UpstreamTuple const& upstreams,
33  std::index_sequence<Indices...>,
34  Args&&... args)
35 {
36  return std::make_unique<Resource>(std::get<Indices>(upstreams).get()...,
37  std::forward<Args>(args)...);
38 }
39 
52 template <typename Resource, typename... Upstreams, typename... Args>
53 auto make_resource(std::tuple<std::shared_ptr<Upstreams>...> const& upstreams, Args&&... args)
54 {
55  return make_resource_impl<Resource>(
56  upstreams, std::index_sequence_for<Upstreams...>{}, std::forward<Args>(args)...);
57 }
58 } // namespace detail
59 
93 template <typename Resource, typename... Upstreams>
95  public:
97  std::tuple<std::shared_ptr<Upstreams>...>;
98 
132  template <typename... Args>
133  owning_wrapper(upstream_tuple upstreams, Args&&... args)
134  : upstreams_{std::move(upstreams)},
135  wrapped_{detail::make_resource<Resource>(upstreams_, std::forward<Args>(args)...)}
136  {
137  }
138 
142  [[nodiscard]] Resource const& wrapped() const noexcept { return *wrapped_; }
143 
147  [[nodiscard]] Resource& wrapped() noexcept { return *wrapped_; }
148 
149  private:
160  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
161  {
162  return wrapped().allocate(stream, bytes);
163  }
164 
174  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
175  {
176  wrapped().deallocate(stream, ptr, bytes);
177  }
178 
188  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
189  {
190  if (this == &other) { return true; }
191  auto casted = dynamic_cast<owning_wrapper<Resource, Upstreams...> const*>(&other);
192  if (nullptr != casted) { return wrapped().is_equal(casted->wrapped()); }
193  return wrapped().is_equal(other);
194  }
195 
196  upstream_tuple upstreams_;
197  std::unique_ptr<Resource> wrapped_;
198 };
199 
232 template <template <typename...> class Resource, typename... Upstreams, typename... Args>
233 auto make_owning_wrapper(std::tuple<std::shared_ptr<Upstreams>...> upstreams, Args&&... args)
234 {
235  return std::make_shared<owning_wrapper<Resource<Upstreams...>, Upstreams...>>(
236  std::move(upstreams), std::forward<Args>(args)...);
237 }
238 
254 template <template <typename> class Resource, typename Upstream, typename... Args>
255 auto make_owning_wrapper(std::shared_ptr<Upstream> upstream, Args&&... args)
256 {
257  return make_owning_wrapper<Resource>(std::make_tuple(std::move(upstream)),
258  std::forward<Args>(args)...);
259 }
260  // end of group
262 } // namespace mr
263 } // 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 adaptor that maintains the lifetime of upstream resources.
Definition: owning_wrapper.hpp:94
Resource const & wrapped() const noexcept
A constant reference to the wrapped resource.
Definition: owning_wrapper.hpp:142
std::tuple< std::shared_ptr< Upstreams >... > upstream_tuple
Tuple of upstream memory resources.
Definition: owning_wrapper.hpp:97
Resource & wrapped() noexcept
A reference to the wrapped resource.
Definition: owning_wrapper.hpp:147
owning_wrapper(upstream_tuple upstreams, Args &&... args)
Constructs the wrapped resource using the provided upstreams and any additional arguments forwarded t...
Definition: owning_wrapper.hpp:133
auto make_owning_wrapper(std::shared_ptr< Upstream > upstream, Args &&... args)
Additional convenience factory for owning_wrapper when Resource has only a single upstream resource.
Definition: owning_wrapper.hpp:255
auto make_resource(std::tuple< std::shared_ptr< Upstreams >... > const &upstreams, Args &&... args)
Create a std::unique_ptr to a Resource with the given upstreams and arguments.
Definition: owning_wrapper.hpp:53
auto make_resource_impl(UpstreamTuple const &upstreams, std::index_sequence< Indices... >, Args &&... args)
Converts a tuple into a parameter pack.
Definition: owning_wrapper.hpp:32