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-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 
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 #include <type_traits>
27 
28 namespace RMM_NAMESPACE {
29 namespace mr {
49 template <typename T>
51  public:
52  using value_type = T;
58  polymorphic_allocator() = default;
59 
68 
75  template <typename U>
77  : mr_{other.get_upstream_resource()}
78  {
79  }
80 
88  value_type* allocate(std::size_t num, cuda_stream_view stream)
89  {
90  return static_cast<value_type*>(
91  get_upstream_resource().allocate_async(num * sizeof(T), stream));
92  }
93 
104  void deallocate(value_type* ptr, std::size_t num, cuda_stream_view stream)
105  {
106  get_upstream_resource().deallocate_async(ptr, num * sizeof(T), stream);
107  }
108 
113  {
114  return mr_;
115  }
116 
117  private:
120 };
121 
133 template <typename T, typename U>
135 {
136  return lhs.get_upstream_resource() == rhs.get_upstream_resource();
137 }
138 
151 template <typename T, typename U>
153 {
154  return not(lhs == rhs);
155 }
156 
179 template <typename Allocator>
181  public:
182  using value_type =
183  typename std::allocator_traits<Allocator>::value_type;
185 
186  stream_allocator_adaptor() = delete;
187 
197  stream_allocator_adaptor(Allocator const& allocator, cuda_stream_view stream)
198  : alloc_{allocator}, stream_{stream}
199  {
200  }
201 
210  template <typename OtherAllocator>
212  : stream_allocator_adaptor{other.underlying_allocator(), other.stream()}
213  {
214  }
215 
221  template <typename T>
222  struct rebind {
223  using other = stream_allocator_adaptor<typename std::allocator_traits<
224  Allocator>::template rebind_alloc<T>>;
225  };
226 
234  value_type* allocate(std::size_t num) { return alloc_.allocate(num, stream()); }
235 
245  void deallocate(value_type* ptr, std::size_t num) { alloc_.deallocate(ptr, num, stream()); }
246 
250  [[nodiscard]] cuda_stream_view stream() const noexcept { return stream_; }
251 
255  [[nodiscard]] Allocator underlying_allocator() const noexcept { return alloc_; }
256 
257  private:
258  Allocator alloc_;
259  cuda_stream_view stream_;
260 };
261 
273 template <typename A, typename O>
275 {
276  return lhs.underlying_allocator() == rhs.underlying_allocator();
277 }
278 
290 template <typename A, typename O>
292 {
293  return not(lhs == rhs);
294 }
295 
306 template <typename Allocator>
307 [[deprecated(
308  "make_stream_allocator_adaptor is deprecated in RMM 24.10. Use the stream_allocator_adaptor "
309  "constructor "
310  "instead.")]]
311 auto make_stream_allocator_adaptor(Allocator const& allocator, cuda_stream_view stream)
312 {
313  return stream_allocator_adaptor<Allocator>{allocator, stream};
314 } // end of group
316 } // namespace mr
317 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
A stream ordered Allocator using a rmm::mr::device_memory_resource to satisfy (de)allocations.
Definition: polymorphic_allocator.hpp:50
T value_type
T, the value type of objects allocated by this allocator.
Definition: polymorphic_allocator.hpp:52
rmm::device_async_resource_ref get_upstream_resource() const noexcept
rmm::device_async_resource_ref to the upstream resource
Definition: polymorphic_allocator.hpp:112
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:104
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:88
polymorphic_allocator(device_async_resource_ref mr)
Construct a polymorphic_allocator using the provided memory resource.
Definition: polymorphic_allocator.hpp:67
polymorphic_allocator(polymorphic_allocator< U > const &other) noexcept
Construct a polymorphic_allocator using the underlying memory resource of other.
Definition: polymorphic_allocator.hpp:76
Adapts a stream ordered allocator to provide a standard Allocator interface.
Definition: polymorphic_allocator.hpp:180
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:234
typename std::allocator_traits< Allocator >::value_type value_type
Definition: polymorphic_allocator.hpp:184
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:245
Allocator underlying_allocator() const noexcept
The underlying allocator.
Definition: polymorphic_allocator.hpp:255
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:197
stream_allocator_adaptor(stream_allocator_adaptor< OtherAllocator > const &other)
Construct a stream_allocator_adaptor using other.underlying_allocator() and other....
Definition: polymorphic_allocator.hpp:211
cuda_stream_view stream() const noexcept
The stream on which calls to the underlying allocator are made.
Definition: polymorphic_allocator.hpp:250
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:274
auto make_stream_allocator_adaptor(Allocator const &allocator, cuda_stream_view stream)
Factory to construct a stream_allocator_adaptor from an existing stream-ordered allocator.
Definition: polymorphic_allocator.hpp:311
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:291
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:222