thrust_allocator_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-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/cuda_device.hpp>
10 #include <rmm/detail/exec_check_disable.hpp>
11 #include <rmm/detail/export.hpp>
12 #include <rmm/detail/thrust_namespace.h>
14 #include <rmm/resource_ref.hpp>
15 
16 #include <cuda/memory_resource>
17 #include <cuda/stream>
18 #include <thrust/device_malloc_allocator.h>
19 #include <thrust/device_ptr.h>
20 #include <thrust/memory.h>
21 
22 RMM_NAMESPACE_BEGIN
23 namespace mr {
42 template <typename T>
43 class thrust_allocator : public thrust::device_malloc_allocator<T> {
44  public:
45  using Base = thrust::device_malloc_allocator<T>;
46  using pointer = typename Base::pointer;
47  using size_type = typename Base::size_type;
48 
55  template <typename U>
56  struct rebind {
58  };
59 
64  RMM_EXEC_CHECK_DISABLE
66 
73  RMM_EXEC_CHECK_DISABLE
74  explicit thrust_allocator(cuda::stream_ref stream) : _stream{stream} {}
75 
83  RMM_EXEC_CHECK_DISABLE
84  thrust_allocator(cuda::stream_ref stream, cuda::mr::any_resource<cuda::mr::device_accessible> mr)
85  : _stream{stream}, _mr(std::move(mr))
86  {
87  }
88 
94  RMM_EXEC_CHECK_DISABLE
96  : Base(other), _stream{other._stream}, _mr(other._mr), _device{other._device}
97  {
98  }
99 
105  RMM_EXEC_CHECK_DISABLE
107  : Base(std::move(other)),
108  _stream{other._stream},
109  _mr(std::move(other._mr)),
110  _device{other._device}
111  {
112  }
113 
118 
125  RMM_EXEC_CHECK_DISABLE
126  template <typename U>
128  : _mr(other.resource()), _stream{other.stream()}, _device{other._device}
129  {
130  }
131 
139  {
140  cuda_set_device_raii dev{_device};
141  return thrust::device_pointer_cast(
142  static_cast<T*>(_mr.allocate(_stream, num * sizeof(T), rmm::CUDA_ALLOCATION_ALIGNMENT)));
143  }
144 
152  void deallocate(pointer ptr, size_type num) noexcept
153  {
154  cuda_set_device_raii dev{_device};
155  return _mr.deallocate(
156  _stream, thrust::raw_pointer_cast(ptr), num * sizeof(T), rmm::CUDA_ALLOCATION_ALIGNMENT);
157  }
158 
163  {
164  return rmm::device_async_resource_ref{_mr};
165  }
166 
170  [[nodiscard]] cuda::stream_ref stream() const noexcept { return _stream; }
171 
177  RMM_CONSTEXPR_FRIEND void get_property(thrust_allocator const&,
178  cuda::mr::device_accessible) noexcept
179  {
180  }
181 
182  private:
183  cuda::stream_ref _stream{cuda::stream_ref{cudaStream_t{cudaStreamDefault}}};
184  mutable cuda::mr::any_resource<cuda::mr::device_accessible> _mr{
186  cuda_device_id _device{get_current_cuda_device()};
187 }; // end of group
189 } // namespace mr
190 RMM_NAMESPACE_END
An allocator compatible with Thrust containers and algorithms using a device_async_resource_ref for m...
Definition: thrust_allocator_adaptor.hpp:43
typename Base::pointer pointer
The pointer type.
Definition: thrust_allocator_adaptor.hpp:46
void deallocate(pointer ptr, size_type num) noexcept
Deallocates objects of type T
Definition: thrust_allocator_adaptor.hpp:152
thrust_allocator(thrust_allocator const &other)
Copy constructor. Copies the resource pointer and stream.
Definition: thrust_allocator_adaptor.hpp:95
thrust_allocator(thrust_allocator &&other) noexcept
Move constructor. Moves the resource pointer and stream.
Definition: thrust_allocator_adaptor.hpp:106
pointer allocate(size_type num)
Allocate objects of type T
Definition: thrust_allocator_adaptor.hpp:138
rmm::device_async_resource_ref get_upstream_resource() const noexcept
rmm::device_async_resource_ref to the upstream resource
Definition: thrust_allocator_adaptor.hpp:162
thrust_allocator()
Default constructor creates an allocator using the default memory resource and default stream.
Definition: thrust_allocator_adaptor.hpp:65
thrust_allocator & operator=(thrust_allocator const &)=default
Default copy assignment operator.
typename Base::size_type size_type
The size type.
Definition: thrust_allocator_adaptor.hpp:47
cuda::stream_ref stream() const noexcept
The stream used by this allocator.
Definition: thrust_allocator_adaptor.hpp:170
thrust::device_malloc_allocator< T > Base
The base type of this allocator.
Definition: thrust_allocator_adaptor.hpp:45
thrust_allocator(cuda::stream_ref stream)
Constructs a thrust_allocator using the default device memory resource and specified stream.
Definition: thrust_allocator_adaptor.hpp:74
friend void get_property(thrust_allocator const &, cuda::mr::device_accessible) noexcept
Enables the cuda::mr::device_accessible property.
Definition: thrust_allocator_adaptor.hpp:177
thrust_allocator & operator=(thrust_allocator &&) noexcept=default
Default move assignment operator.
thrust_allocator(cuda::stream_ref stream, cuda::mr::any_resource< cuda::mr::device_accessible > mr)
Constructs a thrust_allocator using a device memory resource and stream.
Definition: thrust_allocator_adaptor.hpp:84
cuda_device_id get_current_cuda_device()
Returns a cuda_device_id for the current device.
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
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.
RAII class that sets the current CUDA device to the specified device on construction and restores the...
Definition: cuda_device.hpp:115
Provides the type of a thrust_allocator instantiated with another type.
Definition: thrust_allocator_adaptor.hpp:56