-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmm_examples.cu
362 lines (289 loc) · 11.7 KB
/
rmm_examples.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (c) 2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either ex ess or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <catch2/catch.hpp>
#include <rmm/device_buffer.hpp>
#include <rmm/device_uvector.hpp>
#include <rmm/device_vector.hpp>
#include <rmm/mr/device/cuda_async_memory_resource.hpp>
#include <rmm/mr/device/per_device_resource.hpp>
#include <rmm/mr/device/thrust_allocator_adaptor.hpp>
#include <thrust/host_vector.h>
#include <memory>
namespace {
__global__ void kernel(int* input, int* output, int n, int iterations = 1)
{
auto idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx < n) {
for (int i = 0; i < iterations; i++) { // so we can control runtime
output[idx] = input[idx];
}
}
}
} // namespace
TEST_CASE("rmm::device_buffer use-after-free", "[example_3a]")
{
auto async_mr = rmm::mr::cuda_async_memory_resource{15UL << 30};
rmm::mr::set_current_device_resource(&async_mr);
int n{1 << 20};
int block_sz = 256;
int num_blocks{(n + block_sz - 1) / block_sz};
std::size_t bytes{n * sizeof(int)};
cudaStream_t stream_a{};
cudaStream_t stream_b{};
cudaStreamCreate(&stream_a);
cudaStreamCreate(&stream_b);
SECTION("Unsafe: rmm::device_buffer freed while still being written")
{
std::vector<int> h_reference(n, 0xCCCCCCCC);
rmm::device_buffer output(bytes, stream_a);
{
rmm::device_buffer buffer(bytes, stream_a, &async_mr);
cudaMemsetAsync(buffer.data(), 0xCC, bytes, stream_a);
cudaStreamSynchronize(stream_a);
kernel<<<num_blocks, block_sz, 0, stream_b>>>(
static_cast<int*>(buffer.data()), static_cast<int*>(output.data()), n);
}
// buffer is out of scope and therefore its memory could be reused on stream_a
// meanwhile kernel may still be reading from it on stream_b...
{
// This exercises the use-after-free. It is not guaranteed to reproduce on all systems.
// However, on CUDA 11.5 with a Quadro GV100 (16GB) the memory allocated overlaps foo and
// the allocation and memset are fast enough to overlap the `cudaMemcpyAsync` on `stream_b`
// above
rmm::device_buffer racer(100 * bytes, stream_a);
cudaMemsetAsync(racer.data(), 0xff, 100 * bytes, stream_a);
}
cudaStreamSynchronize(stream_b);
std::vector<int> h_output(n, 0);
cudaMemcpy(h_output.data(), output.data(), bytes, cudaMemcpyDefault);
REQUIRE(h_output == h_reference); // technically this could fail
}
SECTION("Safe: synchronize streams before and after cross-stream use.")
{
std::vector<int> h_reference(n, 0xCCCCCCCC);
rmm::device_buffer output(bytes, stream_a);
{
rmm::device_buffer buffer(bytes, stream_a, &async_mr);
cudaMemsetAsync(buffer.data(), 0xCC, bytes, stream_a);
cudaStreamSynchronize(stream_a);
kernel<<<num_blocks, block_sz, 0, stream_b>>>(
static_cast<int*>(buffer.data()), static_cast<int*>(output.data()), n);
cudaStreamSynchronize(stream_b);
}
// buffer is out of scope, but only after the kernel finished writing to output.
{
// Since there is no use-after-free, this code cannot overwrite the contents of `output` as in
// the `UseAfterFree` test.
rmm::device_buffer racer(100 * bytes, stream_a);
cudaMemsetAsync(racer.data(), 0xff, 100 * bytes, stream_a);
}
cudaStreamSynchronize(stream_b);
std::vector<int> h_output(n, 0);
cudaMemcpy(h_output.data(), output.data(), bytes, cudaMemcpyDefault);
REQUIRE(h_output == h_reference);
}
SECTION("Safe: RAII rmm::device_buffer used on same stream as it is freed")
{
std::vector<int> h_reference(n, 0xCCCCCCCC);
rmm::device_buffer output(bytes, stream_a);
{
rmm::device_buffer buffer(bytes, stream_a, &async_mr);
cudaMemsetAsync(buffer.data(), 0xCC, bytes, stream_a);
kernel<<<num_blocks, block_sz, 0, stream_a>>>(
static_cast<int*>(buffer.data()), static_cast<int*>(output.data()), n);
}
// buffer is out of scope, but kernel and memcpy ran on the same stream so no synchronization
// necessary
{
// Since there is no use-after-free, this code cannot overwrite the contents of `output` as in
// the `UseAfterFree` test.
rmm::device_buffer racer(100 * bytes, stream_a);
cudaMemsetAsync(racer.data(), 0xff, 100 * bytes, stream_a);
}
std::vector<int> h_output(n, 0);
cudaMemcpy(h_output.data(), output.data(), bytes, cudaMemcpyDefault);
REQUIRE(h_output == h_reference);
}
cudaStreamDestroy(stream_a);
cudaStreamDestroy(stream_b);
rmm::mr::set_current_device_resource(nullptr);
}
TEST_CASE("Stream-ordered device_vector use-after-free a", "[example_4]")
{
auto async_mr = rmm::mr::cuda_async_memory_resource{15UL << 30};
rmm::mr::set_current_device_resource(&async_mr);
int n{1 << 20};
int block_sz = 256;
int num_blocks{(n + block_sz - 1) / block_sz};
std::size_t bytes{n * sizeof(int)};
cudaStream_t stream_a{};
cudaStreamCreate(&stream_a);
SECTION("Unsafe: rmm::device_vector freed while still being written")
{
std::vector<int> h_reference(n, 0xCCCCCCCC);
rmm::device_buffer output(bytes, stream_a);
{
// rmm::device_vector uses the a custom allocator that uses the current RMM memory
// resource. This is equivalent to the `stream_device_vector` used in the
// "Stream Safety First" presentation
rmm::device_vector<int> v{h_reference};
kernel<<<num_blocks, block_sz, 0, stream_a>>>(
v.data().get(), static_cast<int*>(output.data()), n);
}
cudaStreamSynchronize(stream_a);
std::vector<int> h_output(n, 0);
cudaMemcpy(h_output.data(), output.data(), bytes, cudaMemcpyDefault);
REQUIRE(h_output == h_reference); // technically this could fail
}
SECTION("Safe: rmm::device_vector memory freed on same stream")
{
thrust::host_vector<int> h_reference(n, 0xCCCCCCCC);
rmm::device_buffer output(bytes, stream_a);
{
// Using an explicit allocator allows us to specify a stream for the allocations / frees
rmm::mr::thrust_allocator<int> allocator{stream_a};
rmm::device_vector<int> v{h_reference, allocator};
kernel<<<num_blocks, block_sz, 0, stream_a>>>(
v.data().get(), static_cast<int*>(output.data()), n);
}
cudaStreamSynchronize(stream_a);
std::vector<int> h_output(n, 0);
cudaMemcpy(h_output.data(), output.data(), bytes, cudaMemcpyDefault);
REQUIRE(h_output == h_reference);
}
cudaStreamDestroy(stream_a);
rmm::mr::set_current_device_resource(nullptr);
}
TEST_CASE("Host vector use-after-free a", "[example_5]")
{
auto async_mr = rmm::mr::cuda_async_memory_resource{15UL << 30};
rmm::mr::set_current_device_resource(&async_mr);
int n{1 << 20};
int block_sz = 256;
int num_blocks{(n + block_sz - 1) / block_sz};
std::size_t bytes{n * sizeof(int)};
cudaStream_t stream_a{};
cudaStreamCreate(&stream_a);
SECTION("Unsafe: std::vector freed while still being copied")
{
std::vector<int> h_reference(n, 0xCCCCCCCC);
rmm::device_buffer output(bytes, stream_a);
{
std::vector<int> v{h_reference};
rmm::device_uvector<int> d_v{v.size(), stream_a};
cudaMemcpyAsync(d_v.data(), v.data(), v.size() * sizeof(int), cudaMemcpyDefault, stream_a);
kernel<<<num_blocks, block_sz, 0, stream_a>>>(
d_v.data(), static_cast<int*>(output.data()), n);
}
cudaStreamSynchronize(stream_a);
std::vector<int> h_output(n, 0);
cudaMemcpy(h_output.data(), output.data(), bytes, cudaMemcpyDefault);
REQUIRE(h_output == h_reference); // technically this could fail
}
SECTION("Safe: rmm::device_vector freed while still being written")
{
std::vector<int> h_reference(n, 0xCCCCCCCC);
rmm::device_buffer output(bytes, stream_a);
{
std::vector<int> v{h_reference};
rmm::device_uvector<int> d_v{v.size(), stream_a};
cudaMemcpyAsync(d_v.data(), v.data(), v.size() * sizeof(int), cudaMemcpyDefault, stream_a);
kernel<<<num_blocks, block_sz, 0, stream_a>>>(
d_v.data(), static_cast<int*>(output.data()), n);
cudaStreamSynchronize(stream_a);
}
cudaStreamSynchronize(stream_a);
std::vector<int> h_output(n, 0);
cudaMemcpy(h_output.data(), output.data(), bytes, cudaMemcpyDefault);
REQUIRE(h_output == h_reference);
}
cudaStreamDestroy(stream_a);
rmm::mr::set_current_device_resource(nullptr);
}
class widget {
public:
widget(rmm::device_vector<int> const& v, cudaStream_t stream) : _v(v.size(), stream)
{
cudaMemcpyAsync(_v.data(), v.data().get(), v.size() * sizeof(int), cudaMemcpyDefault, stream);
}
widget(rmm::device_uvector<int> const& v, cudaStream_t stream) : _v(v.size(), stream)
{
cudaMemcpyAsync(_v.data(), v.data(), v.size() * sizeof(int), cudaMemcpyDefault, stream);
}
int* data() { return _v.data(); }
private:
rmm::device_uvector<int> _v;
};
// Create a widget from a host vector
std::unique_ptr<widget> make_widget_unsafe_sync(std::vector<int> const& input, cudaStream_t stream)
{
rmm::device_vector<int> d_temp{input};
return std::make_unique<widget>(d_temp, stream);
}
std::unique_ptr<widget> make_widget_unsafe_async(std::vector<int> const& input, cudaStream_t stream)
{
rmm::device_uvector<int> d_temp{input.size(), stream};
cudaMemcpyAsync(
d_temp.data(), input.data(), input.size() * sizeof(int), cudaMemcpyDefault, stream);
return std::make_unique<widget>(d_temp, stream);
}
std::unique_ptr<widget> make_widget_safe_sync(std::vector<int> const& input, cudaStream_t stream)
{
rmm::device_uvector<int> d_temp{input.size(), stream};
cudaMemcpyAsync(
d_temp.data(), input.data(), input.size() * sizeof(int), cudaMemcpyDefault, stream);
auto w = std::make_unique<widget>(d_temp, stream);
cudaStreamSynchronize(stream);
return w;
}
TEST_CASE("Unsafe, asynchronous and slow", "[example_6]")
{
auto async_mr = rmm::mr::cuda_async_memory_resource{15UL << 30};
rmm::mr::set_current_device_resource(&async_mr);
int n{1 << 20};
std::size_t bytes{n * sizeof(int)};
cudaStream_t stream_a{};
cudaStreamCreate(&stream_a);
SECTION("Unsafe: make_widget's device_vector freed while still being copied")
{
std::vector<int> h_reference(n, 0xCCCCCCCC);
rmm::device_buffer output(bytes, stream_a);
auto w = make_widget_unsafe_sync(h_reference, stream_a);
std::vector<int> h_output(n, 0);
cudaMemcpy(h_output.data(), w->data(), bytes, cudaMemcpyDefault);
REQUIRE(h_output == h_reference); // technically this could fail
}
SECTION("Unsafe: make_widget's device_uvector freed while still being copied")
{
std::vector<int> h_reference(n, 0xCCCCCCCC);
rmm::device_buffer output(bytes, stream_a);
auto w = make_widget_unsafe_async(h_reference, stream_a);
std::vector<int> h_output(n, 0);
cudaMemcpy(h_output.data(), w->data(), bytes, cudaMemcpyDefault);
REQUIRE(h_output == h_reference); // technically this could fail
}
SECTION("Safe but synchronous")
{
std::vector<int> h_reference(n, 0xCCCCCCCC);
rmm::device_buffer output(bytes, stream_a);
auto w = make_widget_safe_sync(h_reference, stream_a);
std::vector<int> h_output(n, 0);
cudaMemcpy(h_output.data(), w->data(), bytes, cudaMemcpyDefault);
REQUIRE(h_output == h_reference);
}
cudaStreamDestroy(stream_a);
rmm::mr::set_current_device_resource(nullptr);
}