-
Notifications
You must be signed in to change notification settings - Fork 144
/
common.cpp
358 lines (302 loc) · 12.1 KB
/
common.cpp
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
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <map>
#include <cuda.h>
#include "common.hpp"
namespace gdrcopy {
namespace test {
bool print_dbg_msg = false;
void print_dbg(const char* fmt, ...)
{
if (print_dbg_msg) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
}
CUresult gpu_mem_alloc(gpu_mem_handle_t *handle, const size_t size, bool aligned_mapping, bool set_sync_memops)
{
CUresult ret = CUDA_SUCCESS;
CUdeviceptr ptr, out_ptr;
size_t allocated_size;
if (aligned_mapping)
allocated_size = size + GPU_PAGE_SIZE - 1;
else
allocated_size = size;
ret = cuMemAlloc(&ptr, allocated_size);
if (ret != CUDA_SUCCESS)
return ret;
if (set_sync_memops) {
unsigned int flag = 1;
ret = cuPointerSetAttribute(&flag, CU_POINTER_ATTRIBUTE_SYNC_MEMOPS, ptr);
if (ret != CUDA_SUCCESS) {
cuMemFree(ptr);
return ret;
}
}
if (aligned_mapping)
out_ptr = PAGE_ROUND_UP(ptr, GPU_PAGE_SIZE);
else
out_ptr = ptr;
handle->ptr = out_ptr;
handle->unaligned_ptr = ptr;
handle->size = size;
handle->allocated_size = allocated_size;
return CUDA_SUCCESS;
}
CUresult gpu_mem_free(gpu_mem_handle_t *handle)
{
CUresult ret = CUDA_SUCCESS;
CUdeviceptr ptr;
ret = cuMemFree(handle->unaligned_ptr);
if (ret == CUDA_SUCCESS)
memset(handle, 0, sizeof(gpu_mem_handle_t));
return ret;
}
#if CUDA_VERSION >= 11000
/**
* Allocating GPU memory using VMM API.
* VMM API is available since CUDA 10.2. However, the RDMA support is added in CUDA 11.0.
* Our tests are not useful without RDMA support. So, we enable this VMM allocation from CUDA 11.0.
*/
CUresult gpu_vmm_alloc(gpu_mem_handle_t *handle, const size_t size, bool aligned_mapping, bool set_sync_memops)
{
CUresult ret = CUDA_SUCCESS;
size_t granularity, gran;
CUmemAllocationProp mprop;
CUdevice gpu_dev;
size_t rounded_size;
CUdeviceptr ptr = 0;
CUmemGenericAllocationHandle mem_handle = 0;
bool is_mapped = false;
int RDMASupported = 0;
int version;
ret = cuDriverGetVersion(&version);
if (ret != CUDA_SUCCESS) {
print_dbg("error in cuDriverGetVersion\n");
goto out;
}
if (version < 11000) {
print_dbg("VMM with RDMA is not supported in this CUDA version.\n");
ret = CUDA_ERROR_NOT_SUPPORTED;
goto out;
}
ret = cuCtxGetDevice(&gpu_dev);
if (ret != CUDA_SUCCESS) {
print_dbg("error in cuCtxGetDevice\n");
goto out;
}
ret = cuDeviceGetAttribute(&RDMASupported, CU_DEVICE_ATTRIBUTE_GPU_DIRECT_RDMA_WITH_CUDA_VMM_SUPPORTED, gpu_dev);
if (ret != CUDA_SUCCESS) {
print_dbg("error in cuDeviceGetAttribute\n");
goto out;
}
if (!RDMASupported) {
print_dbg("GPUDirect RDMA is not supported on this GPU.\n");
ret = CUDA_ERROR_NOT_SUPPORTED;
goto out;
}
memset(&mprop, 0, sizeof(CUmemAllocationProp));
mprop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
mprop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
mprop.location.id = gpu_dev;
mprop.allocFlags.gpuDirectRDMACapable = 1;
ret = cuMemGetAllocationGranularity(&gran, &mprop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED);
if (ret != CUDA_SUCCESS) {
print_dbg("error in cuMemGetAllocationGranularity\n");
goto out;
}
// In case gran is smaller than GPU_PAGE_SIZE
granularity = PAGE_ROUND_UP(gran, GPU_PAGE_SIZE);
rounded_size = PAGE_ROUND_UP(size, granularity);
ret = cuMemAddressReserve(&ptr, rounded_size, granularity, 0, 0);
if (ret != CUDA_SUCCESS) {
print_dbg("error in cuMemAddressReserve\n");
goto out;
}
ret = cuMemCreate(&mem_handle, rounded_size, &mprop, 0);
if (ret != CUDA_SUCCESS) {
print_dbg("error in cuMemCreate\n");
goto out;
}
ret = cuMemMap(ptr, rounded_size, 0, mem_handle, 0);
if (ret != CUDA_SUCCESS) {
print_dbg("error in cuMemMap\n");
goto out;
}
is_mapped = true;
CUmemAccessDesc access;
access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
access.location.id = gpu_dev;
access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
ret = cuMemSetAccess(ptr, rounded_size, &access, 1);
if (ret != CUDA_SUCCESS) {
print_dbg("error in cuMemSetAccess\n");
goto out;
}
// cuMemAddressReserve always returns aligned ptr
handle->ptr = ptr;
handle->handle = mem_handle;
handle->size = size;
handle->allocated_size = rounded_size;
out:
if (ret != CUDA_SUCCESS) {
if (is_mapped)
cuMemUnmap(ptr, rounded_size);
if (mem_handle)
cuMemRelease(mem_handle);
if (ptr)
cuMemAddressFree(ptr, rounded_size);
}
return ret;
}
CUresult gpu_vmm_free(gpu_mem_handle_t *handle)
{
CUresult ret;
if (!handle || !handle->ptr)
return CUDA_ERROR_INVALID_VALUE;
ret = cuMemUnmap(handle->ptr, handle->allocated_size);
if (ret != CUDA_SUCCESS) {
print_dbg("error in cuMemUnmap\n");
return ret;
}
ret = cuMemRelease(handle->handle);
if (ret != CUDA_SUCCESS) {
print_dbg("error in cuMemRelease\n");
return ret;
}
ret = cuMemAddressFree(handle->ptr, handle->allocated_size);
if (ret != CUDA_SUCCESS) {
print_dbg("error in cuMemAddressFree\n");
return ret;
}
memset(handle, 0, sizeof(gpu_mem_handle_t));
return CUDA_SUCCESS;
}
#else
/* VMM with RDMA is not available before CUDA 11.0 */
CUresult gpu_vmm_alloc(gpu_mem_handle_t *handle, const size_t size, bool aligned_mapping, bool set_sync_memops)
{
return CUDA_ERROR_NOT_SUPPORTED;
}
CUresult gpu_vmm_free(gpu_mem_handle_t *handle)
{
return CUDA_ERROR_NOT_SUPPORTED;
}
#endif
int compare_buf(uint32_t *ref_buf, uint32_t *buf, size_t size)
{
int diff = 0;
if (size % 4 != 0U) {
print_dbg("warning: buffer size %zu is not dword aligned, ignoring trailing bytes\n", size);
size -= (size % 4);
}
unsigned ndwords = size/sizeof(uint32_t);
for(unsigned w = 0; w < ndwords; ++w) {
if (ref_buf[w] != buf[w]) {
if (!diff) {
printf("%10.10s %8.8s %8.8s\n", "word", "content", "expected");
}
if (diff < 10) {
printf("%10d %08x %08x\n", w, buf[w], ref_buf[w]);
}
++diff;
}
}
if (diff) {
print_dbg("check error: %d different dwords out of %d\n", diff, ndwords);
}
return diff;
}
void init_hbuf_walking_bit(uint32_t *h_buf, size_t size)
{
uint32_t base_value = 0x3F4C5E6A; // 0xa55ad33d;
unsigned w;
ASSERT_NEQ(h_buf, (void*)0);
ASSERT_EQ(size % 4, 0U);
//OUT << "filling mem with walking bit " << endl;
for(w = 0; w<size/sizeof(uint32_t); ++w)
h_buf[w] = base_value ^ (1<< (w%32));
}
void init_hbuf_linear_ramp(uint32_t *h_buf, size_t size)
{
uint32_t base_value = 0x3F4C5E6A; // 0xa55ad33d;
unsigned w;
ASSERT_NEQ(h_buf, (void*)0);
ASSERT_EQ(size % 4, 0U);
//OUT << "filling mem with walking bit " << endl;
for(w = 0; w<size/sizeof(uint32_t); ++w)
h_buf[w] = w;
}
bool check_gdr_support(CUdevice dev)
{
#if CUDA_VERSION >= 11030
int drv_version;
ASSERTDRV(cuDriverGetVersion(&drv_version));
// Starting from CUDA 11.3, CUDA provides an ability to check GPUDirect RDMA support.
if (drv_version >= 11030) {
int gdr_support = 0;
ASSERTDRV(cuDeviceGetAttribute(&gdr_support, CU_DEVICE_ATTRIBUTE_GPU_DIRECT_RDMA_SUPPORTED, dev));
if (!gdr_support)
print_dbg("This GPU does not support GPUDirect RDMA.\n");
return !!gdr_support;
}
#endif
// For older versions, we fall back to detect this support with gdr_pin_buffer.
const size_t size = GPU_PAGE_SIZE;
CUdeviceptr d_A;
gpu_mem_handle_t mhandle;
ASSERTDRV(gpu_mem_alloc(&mhandle, size, true, true));
d_A = mhandle.ptr;
gdr_t g = gdr_open_safe();
gdr_mh_t mh;
int status = gdr_pin_buffer(g, d_A, size, 0, 0, &mh);
if (status != 0) {
print_dbg("error in gdr_pin_buffer with code=%d\n", status);
print_dbg("Your GPU might not support GPUDirect RDMA\n");
}
else
ASSERT_EQ(gdr_unpin_buffer(g, mh), 0);
ASSERT_EQ(gdr_close(g), 0);
ASSERTDRV(gpu_mem_free(&mhandle));
return status == 0;
}
void print_histogram(double *lat_arr, int count, int *bin_arr, int num_bins, double min, double max)
{
int den = (max - min) / num_bins;
den = den > 0 ? den : 1;
for (int j = 0; j < num_bins; j++)
bin_arr[j] = 0;
for (int i = 0; i < count; i++) {
bin_arr[(int) ((lat_arr[i] - min) / den)]++;
}
for (int j = 0; j < num_bins; j++) {
printf("[%lf\t-\t%lf]\t%d\n", (min * (j + 1)), (min * (j + 2)), bin_arr[j]);
}
}
}
}