thrust_allocator_adaptor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021, 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 
21 
22 #include <rmm/detail/thrust_namespace.h>
23 #include <thrust/device_malloc_allocator.h>
24 #include <thrust/device_ptr.h>
25 #include <thrust/memory.h>
26 
27 #include <cuda/memory_resource>
28 
29 namespace rmm::mr {
45 template <typename T>
46 class thrust_allocator : public thrust::device_malloc_allocator<T> {
47  using async_resource_ref = cuda::mr::async_resource_ref<cuda::mr::device_accessible>;
48 
49  public:
50  using Base = thrust::device_malloc_allocator<T>;
51  using pointer = typename Base::pointer;
52  using size_type = typename Base::size_type;
53 
60  template <typename U>
61  struct rebind {
63  };
64 
69  thrust_allocator() = default;
70 
78 
86  thrust_allocator(cuda_stream_view stream, async_resource_ref mr) : _stream{stream}, _mr(mr) {}
87 
93  template <typename U>
95  : _mr(other.resource()), _stream{other.stream()}
96  {
97  }
98 
106  {
107  return thrust::device_pointer_cast(
108  static_cast<T*>(_mr.allocate_async(num * sizeof(T), _stream)));
109  }
110 
118  void deallocate(pointer ptr, size_type num)
119  {
120  return _mr.deallocate_async(thrust::raw_pointer_cast(ptr), num * sizeof(T), _stream);
121  }
122 
126  [[nodiscard]] async_resource_ref memory_resource() const noexcept { return _mr; }
127 
131  [[nodiscard]] cuda_stream_view stream() const noexcept { return _stream; }
132 
138  friend void get_property(thrust_allocator const&, cuda::mr::device_accessible) noexcept {}
139 
140  private:
141  cuda_stream_view _stream{};
142  async_resource_ref _mr{rmm::mr::get_current_device_resource()};
143 }; // end of group
145 } // namespace rmm::mr
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
An allocator compatible with Thrust containers and algorithms using a device_memory_resource for memo...
Definition: thrust_allocator_adaptor.hpp:46
thrust_allocator(cuda_stream_view stream, async_resource_ref mr)
Constructs a thrust_allocator using a device memory resource and stream.
Definition: thrust_allocator_adaptor.hpp:86
typename Base::pointer pointer
The pointer type.
Definition: thrust_allocator_adaptor.hpp:51
thrust_allocator(thrust_allocator< U > const &other)
Copy constructor. Copies the resource pointer and stream.
Definition: thrust_allocator_adaptor.hpp:94
pointer allocate(size_type num)
Allocate objects of type T
Definition: thrust_allocator_adaptor.hpp:105
cuda_stream_view stream() const noexcept
The stream used by this allocator.
Definition: thrust_allocator_adaptor.hpp:131
thrust_allocator()=default
Default constructor creates an allocator using the default memory resource and default stream.
async_resource_ref memory_resource() const noexcept
The async_resource_ref used to allocate and deallocate.
Definition: thrust_allocator_adaptor.hpp:126
typename Base::size_type size_type
The size type.
Definition: thrust_allocator_adaptor.hpp:52
thrust::device_malloc_allocator< T > Base
The base type of this allocator.
Definition: thrust_allocator_adaptor.hpp:50
friend void get_property(thrust_allocator const &, cuda::mr::device_accessible) noexcept
Enables the cuda::mr::device_accessible property.
Definition: thrust_allocator_adaptor.hpp:138
void deallocate(pointer ptr, size_type num)
Deallocates objects of type T
Definition: thrust_allocator_adaptor.hpp:118
thrust_allocator(cuda_stream_view stream)
Constructs a thrust_allocator using the default device memory resource and specified stream.
Definition: thrust_allocator_adaptor.hpp:77
device_memory_resource * get_current_device_resource()
Get the memory resource for the current device.
Definition: per_device_resource.hpp:207
Management of per-device device_memory_resources.
Provides the type of a thrust_allocator instantiated with another type.
Definition: thrust_allocator_adaptor.hpp:61