polymorphic_allocator.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 
6 #pragma once
7 
9 #include <rmm/detail/export.hpp>
11 #include <rmm/resource_ref.hpp>
12 
13 #include <cstddef>
14 #include <memory>
15 
16 namespace RMM_NAMESPACE {
17 namespace mr {
37 template <typename T>
39  public:
40  using value_type = T;
46  polymorphic_allocator() = default;
47 
56 
63  template <typename U>
65  : mr_{other.get_upstream_resource()}
66  {
67  }
68 
76  value_type* allocate(std::size_t num, cuda_stream_view stream)
77  {
78  return static_cast<value_type*>(get_upstream_resource().allocate(stream, num * sizeof(T)));
79  }
80 
91  void deallocate(value_type* ptr, std::size_t num, cuda_stream_view stream) noexcept
92  {
93  get_upstream_resource().deallocate(stream, ptr, num * sizeof(T));
94  }
95 
100  {
101  return mr_;
102  }
103 
104  private:
107 };
108 
120 template <typename T, typename U>
122 {
123  return lhs.get_upstream_resource() == rhs.get_upstream_resource();
124 }
125 
138 template <typename T, typename U>
140 {
141  return not(lhs == rhs);
142 }
143 
166 template <typename Allocator>
168  public:
169  using value_type =
170  typename std::allocator_traits<Allocator>::value_type;
172 
173  stream_allocator_adaptor() = delete;
174 
184  stream_allocator_adaptor(Allocator const& allocator, cuda_stream_view stream)
185  : alloc_{allocator}, stream_{stream}
186  {
187  }
188 
197  template <typename OtherAllocator>
199  : stream_allocator_adaptor{other.underlying_allocator(), other.stream()}
200  {
201  }
202 
208  template <typename T>
209  struct rebind {
210  using other = stream_allocator_adaptor<typename std::allocator_traits<
211  Allocator>::template rebind_alloc<T>>;
212  };
213 
221  value_type* allocate(std::size_t num) { return alloc_.allocate(num, stream()); }
222 
232  void deallocate(value_type* ptr, std::size_t num) noexcept
233  {
234  alloc_.deallocate(ptr, num, stream());
235  }
236 
240  [[nodiscard]] cuda_stream_view stream() const noexcept { return stream_; }
241 
245  [[nodiscard]] Allocator underlying_allocator() const noexcept { return alloc_; }
246 
247  private:
248  Allocator alloc_;
249  cuda_stream_view stream_;
250 };
251 
263 template <typename A, typename O>
265 {
266  return lhs.underlying_allocator() == rhs.underlying_allocator();
267 }
268 
280 template <typename A, typename O>
282 {
283  return not(lhs == rhs);
284 }
285  // end of group
287 } // namespace mr
288 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
A stream ordered Allocator using a rmm::mr::device_memory_resource to satisfy (de)allocations.
Definition: polymorphic_allocator.hpp:38
T value_type
T, the value type of objects allocated by this allocator.
Definition: polymorphic_allocator.hpp:40
rmm::device_async_resource_ref get_upstream_resource() const noexcept
rmm::device_async_resource_ref to the upstream resource
Definition: polymorphic_allocator.hpp:99
polymorphic_allocator()=default
Construct a polymorphic_allocator using the return value of rmm::mr::get_current_device_resource_ref(...
void deallocate(value_type *ptr, std::size_t num, cuda_stream_view stream) noexcept
Deallocates storage pointed to by ptr.
Definition: polymorphic_allocator.hpp:91
value_type * allocate(std::size_t num, cuda_stream_view stream)
Allocates storage for num objects of type T using the underlying memory resource.
Definition: polymorphic_allocator.hpp:76
polymorphic_allocator(device_async_resource_ref mr)
Construct a polymorphic_allocator using the provided memory resource.
Definition: polymorphic_allocator.hpp:55
polymorphic_allocator(polymorphic_allocator< U > const &other) noexcept
Construct a polymorphic_allocator using the underlying memory resource of other.
Definition: polymorphic_allocator.hpp:64
Adapts a stream ordered allocator to provide a standard Allocator interface.
Definition: polymorphic_allocator.hpp:167
value_type * allocate(std::size_t num)
Allocates storage for num objects of type T using the underlying allocator on stream().
Definition: polymorphic_allocator.hpp:221
typename std::allocator_traits< Allocator >::value_type value_type
Definition: polymorphic_allocator.hpp:171
Allocator underlying_allocator() const noexcept
The underlying allocator.
Definition: polymorphic_allocator.hpp:245
stream_allocator_adaptor(Allocator const &allocator, cuda_stream_view stream)
Construct a stream_allocator_adaptor using a as the underlying allocator.
Definition: polymorphic_allocator.hpp:184
stream_allocator_adaptor(stream_allocator_adaptor< OtherAllocator > const &other)
Construct a stream_allocator_adaptor using other.underlying_allocator() and other....
Definition: polymorphic_allocator.hpp:198
void deallocate(value_type *ptr, std::size_t num) noexcept
Deallocates storage pointed to by ptr using the underlying allocator on stream().
Definition: polymorphic_allocator.hpp:232
cuda_stream_view stream() const noexcept
The stream on which calls to the underlying allocator are made.
Definition: polymorphic_allocator.hpp:240
bool operator==(stream_allocator_adaptor< A > const &lhs, stream_allocator_adaptor< O > const &rhs)
Compare two stream_allocator_adaptors for equality.
Definition: polymorphic_allocator.hpp:264
bool operator!=(stream_allocator_adaptor< A > const &lhs, stream_allocator_adaptor< O > const &rhs)
Compare two stream_allocator_adaptors for inequality.
Definition: polymorphic_allocator.hpp:281
device_async_resource_ref get_current_device_resource_ref()
Get the device_async_resource_ref for the current device.
Definition: per_device_resource.hpp:400
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
Management of per-device device_memory_resources.
Rebinds the allocator to the specified type.
Definition: polymorphic_allocator.hpp:209