Loading...
Searching...
No Matches
polymorphic_allocator.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6#pragma once
7
8#include <rmm/aligned.hpp>
9#include <rmm/detail/export.hpp>
11#include <rmm/resource_ref.hpp>
12
13#include <cuda/memory_resource>
14#include <cuda/stream>
15
16#include <cstddef>
17#include <memory>
18
19RMM_NAMESPACE_BEGIN
20namespace mr {
40template <typename T>
42 public:
43 using value_type = T;
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_ref 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_ref stream) noexcept
98 {
99 mr_.deallocate(stream, ptr, num * sizeof(T), rmm::CUDA_ALLOCATION_ALIGNMENT);
100 }
101
106 {
108 }
109
110 private:
111 mutable cuda::mr::any_resource<cuda::mr::device_accessible> mr_{
113};
114
126template <typename T, typename U>
128{
129 return lhs.get_upstream_resource() == rhs.get_upstream_resource();
130}
131
144template <typename T, typename U>
146{
147 return not(lhs == rhs);
148}
149
172template <typename Allocator>
173class stream_allocator_adaptor {
174 public:
176 typename std::allocator_traits<Allocator>::value_type;
178
179 stream_allocator_adaptor() = delete;
180
190 stream_allocator_adaptor(Allocator const& allocator, cuda::stream_ref stream)
191 : alloc_{allocator}, stream_{stream}
192 {
193 }
194
203 template <typename OtherAllocator>
204 stream_allocator_adaptor(stream_allocator_adaptor<OtherAllocator> const& other)
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_ref stream() const noexcept { return stream_; }
247
251 [[nodiscard]] Allocator underlying_allocator() const noexcept { return alloc_; }
252
253 private:
254 Allocator alloc_;
255 cuda::stream_ref stream_{
256 cuda::invalid_stream_t{}};
257};
258
270template <typename A, typename O>
275
287template <typename A, typename O>
289{
290 return not(lhs == rhs);
291}
292 // end of group
294} // namespace mr
295RMM_NAMESPACE_END
A stream ordered Allocator using a device_async_resource_ref to satisfy (de)allocations.
Definition polymorphic_allocator.hpp:41
void deallocate(value_type *ptr, std::size_t num, cuda::stream_ref stream) noexcept
Deallocates storage pointed to by ptr.
Definition polymorphic_allocator.hpp:97
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
value_type * allocate(std::size_t num, cuda::stream_ref stream)
Allocates storage for num objects of type T using the underlying memory resource.
Definition polymorphic_allocator.hpp:81
polymorphic_allocator()=default
Construct a polymorphic_allocator using the return value of rmm::mr::get_current_device_resource_ref(...
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
stream_allocator_adaptor(Allocator const &allocator, cuda::stream_ref stream)
Construct a stream_allocator_adaptor using a as the underlying allocator.
Definition polymorphic_allocator.hpp:190
Allocator underlying_allocator() const noexcept
Definition polymorphic_allocator.hpp:251
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
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_ref stream() const noexcept
Definition polymorphic_allocator.hpp:246
typename std::allocator_traits< Allocator >::value_type value_type
Definition polymorphic_allocator.hpp:175
bool operator==(polymorphic_allocator< T > const &lhs, polymorphic_allocator< U > const &rhs)
Compare two polymorphic_allocators for equality.
Definition polymorphic_allocator.hpp:127
device_async_resource_ref get_current_device_resource_ref()
Get the device_async_resource_ref for the current device.
Definition per_device_resource.hpp:187
bool operator!=(polymorphic_allocator< T > const &lhs, polymorphic_allocator< U > const &rhs)
Compare two polymorphic_allocators for inequality.
Definition polymorphic_allocator.hpp:145
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
Memory Resource classes and adaptors.
Management of per-device memory resources.
Rebinds the allocator to the specified type.
Definition polymorphic_allocator.hpp:215
stream_allocator_adaptor< typename std::allocator_traits< Allocator >::template rebind_alloc< T > > other
The type to bind to.
Definition polymorphic_allocator.hpp:216