All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
polymorphic_allocator.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2025, 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 
17 #pragma once
18 
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/detail/export.hpp>
22 #include <rmm/resource_ref.hpp>
23 
24 #include <cstddef>
25 #include <memory>
26 
27 namespace RMM_NAMESPACE {
28 namespace mr {
48 template <typename T>
50  public:
51  using value_type = T;
57  polymorphic_allocator() = default;
58 
67 
74  template <typename U>
76  : mr_{other.get_upstream_resource()}
77  {
78  }
79 
87  value_type* allocate(std::size_t num, cuda_stream_view stream)
88  {
89  return static_cast<value_type*>(
90  get_upstream_resource().allocate_async(num * sizeof(T), stream));
91  }
92 
103  void deallocate(value_type* ptr, std::size_t num, cuda_stream_view stream)
104  {
105  get_upstream_resource().deallocate_async(ptr, num * sizeof(T), stream);
106  }
107 
112  {
113  return mr_;
114  }
115 
116  private:
119 };
120 
132 template <typename T, typename U>
134 {
135  return lhs.get_upstream_resource() == rhs.get_upstream_resource();
136 }
137 
150 template <typename T, typename U>
152 {
153  return not(lhs == rhs);
154 }
155 
178 template <typename Allocator>
180  public:
181  using value_type =
182  typename std::allocator_traits<Allocator>::value_type;
184 
185  stream_allocator_adaptor() = delete;
186 
196  stream_allocator_adaptor(Allocator const& allocator, cuda_stream_view stream)
197  : alloc_{allocator}, stream_{stream}
198  {
199  }
200 
209  template <typename OtherAllocator>
211  : stream_allocator_adaptor{other.underlying_allocator(), other.stream()}
212  {
213  }
214 
220  template <typename T>
221  struct rebind {
222  using other = stream_allocator_adaptor<typename std::allocator_traits<
223  Allocator>::template rebind_alloc<T>>;
224  };
225 
233  value_type* allocate(std::size_t num) { return alloc_.allocate(num, stream()); }
234 
244  void deallocate(value_type* ptr, std::size_t num) { alloc_.deallocate(ptr, num, stream()); }
245 
249  [[nodiscard]] cuda_stream_view stream() const noexcept { return stream_; }
250 
254  [[nodiscard]] Allocator underlying_allocator() const noexcept { return alloc_; }
255 
256  private:
257  Allocator alloc_;
258  cuda_stream_view stream_;
259 };
260 
272 template <typename A, typename O>
274 {
275  return lhs.underlying_allocator() == rhs.underlying_allocator();
276 }
277 
289 template <typename A, typename O>
291 {
292  return not(lhs == rhs);
293 }
294  // end of group
296 } // namespace mr
297 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:39
A stream ordered Allocator using a rmm::mr::device_memory_resource to satisfy (de)allocations.
Definition: polymorphic_allocator.hpp:49
T value_type
T, the value type of objects allocated by this allocator.
Definition: polymorphic_allocator.hpp:51
rmm::device_async_resource_ref get_upstream_resource() const noexcept
rmm::device_async_resource_ref to the upstream resource
Definition: polymorphic_allocator.hpp:111
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)
Deallocates storage pointed to by ptr.
Definition: polymorphic_allocator.hpp:103
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:87
polymorphic_allocator(device_async_resource_ref mr)
Construct a polymorphic_allocator using the provided memory resource.
Definition: polymorphic_allocator.hpp:66
polymorphic_allocator(polymorphic_allocator< U > const &other) noexcept
Construct a polymorphic_allocator using the underlying memory resource of other.
Definition: polymorphic_allocator.hpp:75
Adapts a stream ordered allocator to provide a standard Allocator interface.
Definition: polymorphic_allocator.hpp:179
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:233
typename std::allocator_traits< Allocator >::value_type value_type
Definition: polymorphic_allocator.hpp:183
void deallocate(value_type *ptr, std::size_t num)
Deallocates storage pointed to by ptr using the underlying allocator on stream().
Definition: polymorphic_allocator.hpp:244
Allocator underlying_allocator() const noexcept
The underlying allocator.
Definition: polymorphic_allocator.hpp:254
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:196
stream_allocator_adaptor(stream_allocator_adaptor< OtherAllocator > const &other)
Construct a stream_allocator_adaptor using other.underlying_allocator() and other....
Definition: polymorphic_allocator.hpp:210
cuda_stream_view stream() const noexcept
The stream on which calls to the underlying allocator are made.
Definition: polymorphic_allocator.hpp:249
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:273
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:290
cuda::mr::async_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:41
device_async_resource_ref get_current_device_resource_ref()
Get the device_async_resource_ref for the current device.
Definition: per_device_resource.hpp:411
Management of per-device device_memory_resources.
Rebinds the allocator to the specified type.
Definition: polymorphic_allocator.hpp:221