polymorphic_allocator.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2026, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <rmm/aligned.hpp>
10 #include <rmm/detail/export.hpp>
12 #include <rmm/resource_ref.hpp>
13 
14 #include <cuda/memory_resource>
15 
16 #include <cstddef>
17 #include <memory>
18 
19 namespace RMM_NAMESPACE {
20 namespace mr {
40 template <typename T>
42  public:
43  using value_type = T;
49  polymorphic_allocator() = default;
50 
58  polymorphic_allocator(cuda::mr::any_resource<cuda::mr::device_accessible> mr) : mr_(std::move(mr))
59  {
60  }
61 
68  template <typename U>
70  : mr_(other.get_upstream_resource())
71  {
72  }
73 
81  value_type* allocate(std::size_t num, cuda_stream_view stream)
82  {
83  return static_cast<value_type*>(
84  mr_.allocate(stream, num * sizeof(T), rmm::CUDA_ALLOCATION_ALIGNMENT));
85  }
86 
97  void deallocate(value_type* ptr, std::size_t num, cuda_stream_view stream) noexcept
98  {
99  mr_.deallocate(stream, ptr, num * sizeof(T), rmm::CUDA_ALLOCATION_ALIGNMENT);
100  }
101 
106  {
107  return rmm::device_async_resource_ref{mr_};
108  }
109 
110  private:
111  mutable cuda::mr::any_resource<cuda::mr::device_accessible> mr_{
113 };
114 
126 template <typename T, typename U>
128 {
129  return lhs.get_upstream_resource() == rhs.get_upstream_resource();
130 }
131 
144 template <typename T, typename U>
146 {
147  return not(lhs == rhs);
148 }
149 
172 template <typename Allocator>
174  public:
175  using value_type =
176  typename std::allocator_traits<Allocator>::value_type;
178 
179  stream_allocator_adaptor() = delete;
180 
190  stream_allocator_adaptor(Allocator const& allocator, cuda_stream_view stream)
191  : alloc_{allocator}, stream_{stream}
192  {
193  }
194 
203  template <typename OtherAllocator>
205  : stream_allocator_adaptor{other.underlying_allocator(), other.stream()}
206  {
207  }
208 
214  template <typename T>
215  struct rebind {
216  using other = stream_allocator_adaptor<typename std::allocator_traits<
217  Allocator>::template rebind_alloc<T>>;
218  };
219 
227  value_type* allocate(std::size_t num) { return alloc_.allocate(num, stream()); }
228 
238  void deallocate(value_type* ptr, std::size_t num) noexcept
239  {
240  alloc_.deallocate(ptr, num, stream());
241  }
242 
246  [[nodiscard]] cuda_stream_view stream() const noexcept { return stream_; }
247 
251  [[nodiscard]] Allocator underlying_allocator() const noexcept { return alloc_; }
252 
253  private:
254  Allocator alloc_;
255  cuda_stream_view stream_;
256 };
257 
269 template <typename A, typename O>
271 {
272  return lhs.underlying_allocator() == rhs.underlying_allocator();
273 }
274 
286 template <typename A, typename O>
288 {
289  return not(lhs == rhs);
290 }
291  // end of group
293 } // namespace mr
294 } // 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 device_async_resource_ref to satisfy (de)allocations.
Definition: polymorphic_allocator.hpp:41
T value_type
T, the value type of objects allocated by this allocator.
Definition: polymorphic_allocator.hpp:43
rmm::device_async_resource_ref get_upstream_resource() const noexcept
rmm::device_async_resource_ref to the upstream resource
Definition: polymorphic_allocator.hpp:105
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:97
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:81
polymorphic_allocator(cuda::mr::any_resource< cuda::mr::device_accessible > mr)
Construct a polymorphic_allocator using the provided memory resource.
Definition: polymorphic_allocator.hpp:58
polymorphic_allocator(polymorphic_allocator< U > const &other) noexcept
Construct a polymorphic_allocator using the underlying memory resource of other.
Definition: polymorphic_allocator.hpp:69
Adapts a stream ordered allocator to provide a standard Allocator interface.
Definition: polymorphic_allocator.hpp:173
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:227
typename std::allocator_traits< Allocator >::value_type value_type
Definition: polymorphic_allocator.hpp:177
Allocator underlying_allocator() const noexcept
The underlying allocator.
Definition: polymorphic_allocator.hpp:251
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:190
stream_allocator_adaptor(stream_allocator_adaptor< OtherAllocator > const &other)
Construct a stream_allocator_adaptor using other.underlying_allocator() and other....
Definition: polymorphic_allocator.hpp:204
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:238
cuda_stream_view stream() const noexcept
The stream on which calls to the underlying allocator are made.
Definition: polymorphic_allocator.hpp:246
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:270
device_async_resource_ref get_current_device_resource_ref()
Get the device_async_resource_ref for the current device.
Definition: per_device_resource.hpp:223
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:287
cuda::mr::resource_ref< cuda::mr::device_accessible > device_async_resource_ref
Alias for a cuda::mr::resource_ref with the property cuda::mr::device_accessible.
Definition: resource_ref.hpp:30
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:25
Management of per-device memory resources.
Rebinds the allocator to the specified type.
Definition: polymorphic_allocator.hpp:215