20
20
#include < rmm/detail/error.hpp>
21
21
#include < rmm/detail/export.hpp>
22
22
#include < rmm/mr/device/device_memory_resource.hpp>
23
+ #include < rmm/mr/device/per_device_resource.hpp>
23
24
#include < rmm/resource_ref.hpp>
24
25
25
26
#include < cstddef>
@@ -59,20 +60,40 @@ class aligned_resource_adaptor final : public device_memory_resource {
59
60
/* *
60
61
* @brief Construct an aligned resource adaptor using `upstream` to satisfy allocation requests.
61
62
*
62
- * @throws rmm::logic_error if `upstream == nullptr`
63
63
* @throws rmm::logic_error if `allocation_alignment` is not a power of 2
64
64
*
65
65
* @param upstream The resource used for allocating/deallocating device memory.
66
66
* @param alignment The size used for allocation alignment.
67
67
* @param alignment_threshold Only allocations with a size larger than or equal to this threshold
68
68
* are aligned.
69
69
*/
70
- explicit aligned_resource_adaptor (Upstream* upstream,
70
+ explicit aligned_resource_adaptor (device_async_resource_ref upstream,
71
71
std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT,
72
72
std::size_t alignment_threshold = default_alignment_threshold)
73
73
: upstream_{upstream}, alignment_{alignment}, alignment_threshold_{alignment_threshold}
74
74
{
75
- RMM_EXPECTS (nullptr != upstream, " Unexpected null upstream resource pointer." );
75
+ RMM_EXPECTS (rmm::is_supported_alignment (alignment),
76
+ " Allocation alignment is not a power of 2." );
77
+ }
78
+
79
+ /* *
80
+ * @brief Construct an aligned resource adaptor using `upstream` to satisfy allocation requests.
81
+ *
82
+ * @throws rmm::logic_error if `upstream == nullptr`
83
+ * @throws rmm::logic_error if `alignment` is not a power of 2
84
+ *
85
+ * @param upstream The resource used for allocating/deallocating device memory.
86
+ * @param alignment The size used for allocation alignment.
87
+ * @param alignment_threshold Only allocations with a size larger than or equal to this threshold
88
+ * are aligned.
89
+ */
90
+ explicit aligned_resource_adaptor (Upstream* upstream,
91
+ std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT,
92
+ std::size_t alignment_threshold = default_alignment_threshold)
93
+ : upstream_{to_device_async_resource_ref_checked (upstream)},
94
+ alignment_{alignment},
95
+ alignment_threshold_{alignment_threshold}
96
+ {
76
97
RMM_EXPECTS (rmm::is_supported_alignment (alignment),
77
98
" Allocation alignment is not a power of 2." );
78
99
}
@@ -92,11 +113,6 @@ class aligned_resource_adaptor final : public device_memory_resource {
92
113
return upstream_;
93
114
}
94
115
95
- /* *
96
- * @briefreturn{Upstream* to the upstream memory resource}
97
- */
98
- [[nodiscard]] Upstream* get_upstream () const noexcept { return upstream_; }
99
-
100
116
/* *
101
117
* @brief The default alignment used by the adaptor.
102
118
*/
@@ -106,8 +122,8 @@ class aligned_resource_adaptor final : public device_memory_resource {
106
122
using lock_guard = std::lock_guard<std::mutex>;
107
123
108
124
/* *
109
- * @brief Allocates memory of size at least `bytes` using the upstream resource with the specified
110
- * alignment.
125
+ * @brief Allocates memory of size at least `bytes` using the upstream resource with the
126
+ * specified alignment.
111
127
*
112
128
* @throws rmm::bad_alloc if the requested allocation could not be fulfilled
113
129
* by the upstream resource.
@@ -119,10 +135,10 @@ class aligned_resource_adaptor final : public device_memory_resource {
119
135
void * do_allocate (std::size_t bytes, cuda_stream_view stream) override
120
136
{
121
137
if (alignment_ == rmm::CUDA_ALLOCATION_ALIGNMENT || bytes < alignment_threshold_) {
122
- return upstream_-> allocate ( bytes, stream);
138
+ return get_upstream_resource (). allocate_async ( bytes, 1 , stream);
123
139
}
124
140
auto const size = upstream_allocation_size (bytes);
125
- void * pointer = upstream_-> allocate ( size, stream);
141
+ void * pointer = get_upstream_resource (). allocate_async ( size, 1 , stream);
126
142
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
127
143
auto const address = reinterpret_cast <std::size_t >(pointer);
128
144
auto const aligned_address = rmm::align_up (address, alignment_);
@@ -145,7 +161,7 @@ class aligned_resource_adaptor final : public device_memory_resource {
145
161
void do_deallocate (void * ptr, std::size_t bytes, cuda_stream_view stream) override
146
162
{
147
163
if (alignment_ == rmm::CUDA_ALLOCATION_ALIGNMENT || bytes < alignment_threshold_) {
148
- upstream_-> deallocate ( ptr, bytes, stream);
164
+ get_upstream_resource (). deallocate_async ( ptr, bytes, 1 , stream);
149
165
} else {
150
166
{
151
167
lock_guard lock (mtx_);
@@ -155,7 +171,7 @@ class aligned_resource_adaptor final : public device_memory_resource {
155
171
pointers_.erase (iter);
156
172
}
157
173
}
158
- upstream_-> deallocate ( ptr, upstream_allocation_size (bytes), stream);
174
+ get_upstream_resource (). deallocate_async ( ptr, upstream_allocation_size (bytes), 1 , stream);
159
175
}
160
176
}
161
177
@@ -176,8 +192,8 @@ class aligned_resource_adaptor final : public device_memory_resource {
176
192
}
177
193
178
194
/* *
179
- * @brief Calculate the allocation size needed from upstream to account for alignments of both the
180
- * size and the base pointer.
195
+ * @brief Calculate the allocation size needed from upstream to account for alignments of both
196
+ * the size and the base pointer.
181
197
*
182
198
* @param bytes The requested allocation size.
183
199
* @return Allocation size needed from upstream to align both the size and the base pointer.
@@ -188,7 +204,8 @@ class aligned_resource_adaptor final : public device_memory_resource {
188
204
return aligned_size + alignment_ - rmm::CUDA_ALLOCATION_ALIGNMENT;
189
205
}
190
206
191
- Upstream* upstream_; // /< The upstream resource used for satisfying allocation requests
207
+ // / The upstream resource used for satisfying allocation requests
208
+ device_async_resource_ref upstream_;
192
209
std::unordered_map<void *, void *> pointers_; // /< Map of aligned pointers to upstream pointers.
193
210
std::size_t alignment_; // /< The size used for allocation alignment
194
211
std::size_t alignment_threshold_; // /< The size above which allocations should be aligned
0 commit comments