All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
owning_wrapper.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <rmm/detail/export.hpp>
20 
21 #include <functional>
22 #include <iostream>
23 #include <memory>
24 #include <utility>
25 
26 namespace RMM_NAMESPACE {
27 namespace mr {
28 namespace detail {
44 template <typename Resource, typename UpstreamTuple, std::size_t... Indices, typename... Args>
45 auto make_resource_impl(UpstreamTuple const& upstreams,
46  std::index_sequence<Indices...>,
47  Args&&... args)
48 {
49  return std::make_unique<Resource>(std::get<Indices>(upstreams).get()...,
50  std::forward<Args>(args)...);
51 }
52 
65 template <typename Resource, typename... Upstreams, typename... Args>
66 auto make_resource(std::tuple<std::shared_ptr<Upstreams>...> const& upstreams, Args&&... args)
67 {
68  return make_resource_impl<Resource>(
69  upstreams, std::index_sequence_for<Upstreams...>{}, std::forward<Args>(args)...);
70 }
71 } // namespace detail
72 
106 template <typename Resource, typename... Upstreams>
108  public:
110  std::tuple<std::shared_ptr<Upstreams>...>;
111 
145  template <typename... Args>
146  owning_wrapper(upstream_tuple upstreams, Args&&... args)
147  : upstreams_{std::move(upstreams)},
148  wrapped_{detail::make_resource<Resource>(upstreams_, std::forward<Args>(args)...)}
149  {
150  }
151 
155  [[nodiscard]] Resource const& wrapped() const noexcept { return *wrapped_; }
156 
160  [[nodiscard]] Resource& wrapped() noexcept { return *wrapped_; }
161 
162  private:
173  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
174  {
175  return wrapped().allocate(bytes, stream);
176  }
177 
187  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
188  {
189  wrapped().deallocate(ptr, bytes, stream);
190  }
191 
201  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
202  {
203  if (this == &other) { return true; }
204  auto casted = dynamic_cast<owning_wrapper<Resource, Upstreams...> const*>(&other);
205  if (nullptr != casted) { return wrapped().is_equal(casted->wrapped()); }
206  return wrapped().is_equal(other);
207  }
208 
209  upstream_tuple upstreams_;
210  std::unique_ptr<Resource> wrapped_;
211 };
212 
245 template <template <typename...> class Resource, typename... Upstreams, typename... Args>
246 auto make_owning_wrapper(std::tuple<std::shared_ptr<Upstreams>...> upstreams, Args&&... args)
247 {
248  return std::make_shared<owning_wrapper<Resource<Upstreams...>, Upstreams...>>(
249  std::move(upstreams), std::forward<Args>(args)...);
250 }
251 
267 template <template <typename> class Resource, typename Upstream, typename... Args>
268 auto make_owning_wrapper(std::shared_ptr<Upstream> upstream, Args&&... args)
269 {
270  return make_owning_wrapper<Resource>(std::make_tuple(std::move(upstream)),
271  std::forward<Args>(args)...);
272 }
273  // end of group
275 } // namespace mr
276 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:94
Resource adaptor that maintains the lifetime of upstream resources.
Definition: owning_wrapper.hpp:107
Resource const & wrapped() const noexcept
A constant reference to the wrapped resource.
Definition: owning_wrapper.hpp:155
std::tuple< std::shared_ptr< Upstreams >... > upstream_tuple
Tuple of upstream memory resources.
Definition: owning_wrapper.hpp:110
Resource & wrapped() noexcept
A reference to the wrapped resource.
Definition: owning_wrapper.hpp:160
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:146
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:268
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:66
auto make_resource_impl(UpstreamTuple const &upstreams, std::index_sequence< Indices... >, Args &&... args)
Converts a tuple into a parameter pack.
Definition: owning_wrapper.hpp:45