thrust_allocator_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <rmm/cuda_device.hpp>
9 #include <rmm/detail/export.hpp>
10 #include <rmm/detail/thrust_namespace.h>
12 #include <rmm/resource_ref.hpp>
13 
14 #include <thrust/device_malloc_allocator.h>
15 #include <thrust/device_ptr.h>
16 #include <thrust/memory.h>
17 
18 namespace RMM_NAMESPACE {
19 namespace mr {
38 template <typename T>
39 class thrust_allocator : public thrust::device_malloc_allocator<T> {
40  public:
41  using Base = thrust::device_malloc_allocator<T>;
42  using pointer = typename Base::pointer;
43  using size_type = typename Base::size_type;
44 
51  template <typename U>
52  struct rebind {
54  };
55 
60  thrust_allocator() = default;
61 
68  explicit thrust_allocator(cuda_stream_view stream) : _stream{stream} {}
69 
78  : _stream{stream}, _mr(mr)
79  {
80  }
81 
87  template <typename U>
89  : _mr(other.resource()), _stream{other.stream()}, _device{other._device}
90  {
91  }
92 
100  {
101  cuda_set_device_raii dev{_device};
102  return thrust::device_pointer_cast(static_cast<T*>(_mr.allocate(_stream, num * sizeof(T))));
103  }
104 
112  void deallocate(pointer ptr, size_type num) noexcept
113  {
114  cuda_set_device_raii dev{_device};
115  return _mr.deallocate(_stream, thrust::raw_pointer_cast(ptr), num * sizeof(T));
116  }
117 
122  {
123  return _mr;
124  }
125 
129  [[nodiscard]] cuda_stream_view stream() const noexcept { return _stream; }
130 
136  friend void get_property(thrust_allocator const&, cuda::mr::device_accessible) noexcept {}
137 
138  private:
139  cuda_stream_view _stream{};
141  cuda_device_id _device{get_current_cuda_device()};
142 }; // end of group
144 } // namespace mr
145 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
An allocator compatible with Thrust containers and algorithms using a device_async_resource_ref for m...
Definition: thrust_allocator_adaptor.hpp:39
thrust_allocator(cuda_stream_view stream, rmm::device_async_resource_ref mr)
Constructs a thrust_allocator using a device memory resource and stream.
Definition: thrust_allocator_adaptor.hpp:77
typename Base::pointer pointer
The pointer type.
Definition: thrust_allocator_adaptor.hpp:42
thrust_allocator(thrust_allocator< U > const &other)
Copy constructor. Copies the resource pointer and stream.
Definition: thrust_allocator_adaptor.hpp:88
void deallocate(pointer ptr, size_type num) noexcept
Deallocates objects of type T
Definition: thrust_allocator_adaptor.hpp:112
pointer allocate(size_type num)
Allocate objects of type T
Definition: thrust_allocator_adaptor.hpp:99
cuda_stream_view stream() const noexcept
The stream used by this allocator.
Definition: thrust_allocator_adaptor.hpp:129
rmm::device_async_resource_ref get_upstream_resource() const noexcept
rmm::device_async_resource_ref to the upstream resource
Definition: thrust_allocator_adaptor.hpp:121
thrust_allocator()=default
Default constructor creates an allocator using the default memory resource and default stream.
typename Base::size_type size_type
The size type.
Definition: thrust_allocator_adaptor.hpp:43
thrust::device_malloc_allocator< T > Base
The base type of this allocator.
Definition: thrust_allocator_adaptor.hpp:41
friend void get_property(thrust_allocator const &, cuda::mr::device_accessible) noexcept
Enables the cuda::mr::device_accessible property.
Definition: thrust_allocator_adaptor.hpp:136
thrust_allocator(cuda_stream_view stream)
Constructs a thrust_allocator using the default device memory resource and specified stream.
Definition: thrust_allocator_adaptor.hpp:68
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:406
detail::cccl_async_resource_ref< cuda::mr::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:32
Management of per-device 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:52