-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcuda_utils.cu
626 lines (525 loc) · 18.6 KB
/
cuda_utils.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#include <iostream>
#include <fstream>
#include <algorithm>
#include <utility>
#include <cuda.h>
#include <nvToolsExtCuda.h>
#include "cuda_utils.h"
//----------------------------------------------------------------------------------------
//
// Deallocate unified (managed) memory
// pp = memory pointer
//
void deallocate_unified_T(void **pp) {
if (*pp != NULL) {
cudaCheck(cudaFree((void *)(*pp)));
*pp = NULL;
}
}
//----------------------------------------------------------------------------------------
//
// Allocate unified (managed) memory
// pp = memory pointer
// len = length of the array
//
void allocate_unified_T(void **pp, const int len, const size_t sizeofT) {
cudaCheck(cudaMallocManaged(pp, sizeofT*len, cudaMemAttachGlobal));
}
//----------------------------------------------------------------------------------------
//
// Allocate & re-allocate unified (managed) memory
// pp = memory pointer
// curlen = current length of the array
// newlen = new required length of the array
// fac = extra space allocation factor: in case of re-allocation new length will be fac*newlen
//
void reallocate_unified_T(void **pp, int *curlen, const int newlen, const float fac, const size_t sizeofT) {
if (*pp != NULL && *curlen < newlen) {
cudaCheck(cudaFree((void *)(*pp)));
*pp = NULL;
}
if (*pp == NULL) {
if (fac > 1.0f) {
*curlen = (int)(((double)(newlen))*(double)fac);
} else {
*curlen = newlen;
}
allocate_unified_T(pp, *curlen, sizeofT);
}
}
//----------------------------------------------------------------------------------------
//
// Allocate & re-allocate unified (managed) memory, preserves content
//
void resize_unified_T(void **pp, int *curlen, const int cur_size, const int new_size,
const float fac, const size_t sizeofT) {
char *old = NULL;
if (*pp != NULL && *curlen < new_size) {
old = new char[cur_size*sizeofT];
memcpy(old, *pp, cur_size*sizeofT);
cudaCheck(cudaFree((void *)(*pp)));
*pp = NULL;
}
if (*pp == NULL) {
if (fac > 1.0f) {
*curlen = (int)(((double)(new_size))*(double)fac);
} else {
*curlen = new_size;
}
allocate_unified_T(pp, *curlen, sizeofT);
if (old != NULL) {
memcpy(*pp, old, cur_size*sizeofT);
delete [] old;
}
}
}
//----------------------------------------------------------------------------------------
//
// Deallocate page-locked host memory
// pp = memory pointer
//
void deallocate_host_T(void **pp) {
if (*pp != NULL) {
cudaCheck(cudaFreeHost((void *)(*pp)));
*pp = NULL;
}
}
//----------------------------------------------------------------------------------------
//
// Allocate page-locked host memory
// pp = memory pointer
// len = length of the array
//
void allocate_host_T(void **pp, const int len, const size_t sizeofT) {
cudaCheck(cudaMallocHost(pp, sizeofT*len));
}
//----------------------------------------------------------------------------------------
//
// Allocate & re-allocate page-locked host memory
// pp = memory pointer
// curlen = current length of the array
// newlen = new required length of the array
// fac = extra space allocation factor: in case of re-allocation new length will be fac*newlen
//
void reallocate_host_T(void **pp, int *curlen, const int newlen, const float fac, const size_t sizeofT) {
if (*pp != NULL && *curlen < newlen) {
cudaCheck(cudaFreeHost((void *)(*pp)));
*pp = NULL;
}
if (*pp == NULL) {
if (fac > 1.0f) {
*curlen = (int)(((double)(newlen))*(double)fac);
} else {
*curlen = newlen;
}
allocate_host_T(pp, *curlen, sizeofT);
}
}
//----------------------------------------------------------------------------------------
//
// Allocate & re-allocate page-locked host memory, preserves content
//
void resize_host_T(void **pp, int *curlen, const int cur_size, const int new_size,
const float fac, const size_t sizeofT) {
char *old = NULL;
if (*pp != NULL && *curlen < new_size) {
old = new char[cur_size*sizeofT];
memcpy(old, *pp, cur_size*sizeofT);
cudaCheck(cudaFreeHost((void *)(*pp)));
*pp = NULL;
}
if (*pp == NULL) {
if (fac > 1.0f) {
*curlen = (int)(((double)(new_size))*(double)fac);
} else {
*curlen = new_size;
}
allocate_host_T(pp, *curlen, sizeofT);
if (old != NULL) {
memcpy(*pp, old, cur_size*sizeofT);
delete [] old;
}
}
}
//----------------------------------------------------------------------------------------
//
// Deallocate gpu memory
// pp = memory pointer
//
void deallocate_T(void **pp) {
if (*pp != NULL) {
cudaCheck(cudaFree((void *)(*pp)));
*pp = NULL;
}
}
//----------------------------------------------------------------------------------------
//
// Allocate gpu memory
// pp = memory pointer
// len = length of the array
//
void allocate_T(void **pp, const int len, const size_t sizeofT) {
cudaCheck(cudaMalloc(pp, sizeofT*len));
}
//----------------------------------------------------------------------------------------
//
// Allocate & re-allocate gpu memory
// pp = memory pointer
// curlen = current length of the array
// newlen = new required length of the array
// fac = extra space allocation factor: in case of re-allocation new length will be fac*newlen
//
void reallocate_T(void **pp, int *curlen, const int newlen, const float fac, const size_t sizeofT) {
if (*pp != NULL && *curlen < newlen) {
cudaCheck(cudaFree((void *)(*pp)));
*pp = NULL;
}
if (*pp == NULL) {
if (fac > 1.0f) {
*curlen = (int)(((double)(newlen))*(double)fac);
} else {
*curlen = newlen;
}
allocate_T(pp, *curlen, sizeofT);
}
}
//----------------------------------------------------------------------------------------
//
// Allocate & re-allocate page-locked host memory, preserves content
//
void resize_T(void **pp, int *curlen, const int cur_size, const int new_size,
const float fac, const size_t sizeofT) {
void *old = NULL;
if (*pp != NULL && *curlen < new_size) {
allocate_T(&old, cur_size, sizeofT);
copy_DtoD_T(*pp, old, cur_size, sizeofT);
cudaCheck(cudaDeviceSynchronize()); //Make sure D-D copy is done
cudaCheck(cudaFree((void *)(*pp)));
*pp = NULL;
}
if (*pp == NULL) {
if (fac > 1.0f) {
*curlen = (int)(((double)(new_size))*(double)fac);
} else {
*curlen = new_size;
}
allocate_T(pp, *curlen, sizeofT);
if (old != NULL) {
copy_DtoD_T(old, *pp, cur_size, sizeofT);
cudaCheck(cudaDeviceSynchronize()); //Make sure D-D copy is done
deallocate_T(&old);
}
}
}
//----------------------------------------------------------------------------------------
//
// Copies memory Host -> Device
//
void copy_HtoD_async_T(const void *h_array, void *d_array, int array_len, cudaStream_t stream,
const size_t sizeofT) {
cudaCheck(cudaMemcpyAsync(d_array, h_array, sizeofT*array_len,
cudaMemcpyHostToDevice, stream));
}
void copy_HtoD_T(const void *h_array, void *d_array, int array_len,
const size_t sizeofT) {
cudaCheck(cudaMemcpy(d_array, h_array, sizeofT*array_len,
cudaMemcpyHostToDevice));
}
//----------------------------------------------------------------------------------------
//
// Copies memory Device -> Host
//
void copy_DtoH_async_T(const void *d_array, void *h_array, const int array_len, cudaStream_t stream,
const size_t sizeofT) {
cudaCheck(cudaMemcpyAsync(h_array, d_array, sizeofT*array_len, cudaMemcpyDeviceToHost, stream));
}
void copy_DtoH_T(const void *d_array, void *h_array, const int array_len, const size_t sizeofT) {
cudaCheck(cudaMemcpy(h_array, d_array, sizeofT*array_len, cudaMemcpyDeviceToHost));
}
//----------------------------------------------------------------------------------------
//
// Copies memory Device -> Device
//
void copy_DtoD_async_T(const void *d_src, void *d_dst, const int array_len, cudaStream_t stream,
const size_t sizeofT) {
cudaCheck(cudaMemcpyAsync(d_dst, d_src, sizeofT*array_len, cudaMemcpyDeviceToDevice, stream));
}
void copy_DtoD_T(const void *d_src, void *d_dst, const int array_len, const size_t sizeofT) {
cudaCheck(cudaMemcpy(d_dst, d_src, sizeofT*array_len, cudaMemcpyDeviceToDevice));
}
//----------------------------------------------------------------------------------------
void clear_gpu_array_async_T(void *data, const int ndata, cudaStream_t stream, const size_t sizeofT) {
cudaCheck(cudaMemsetAsync(data, 0, sizeofT*ndata, stream));
}
void clear_gpu_array_T(void *data, const int ndata, const size_t sizeofT) {
cudaCheck(cudaMemset(data, 0, sizeofT*ndata));
}
//----------------------------------------------------------------------------------------
void set_gpu_array_async_T(void *data, const int ndata, const int value,
cudaStream_t stream, const size_t sizeofT) {
cudaCheck(cudaMemsetAsync(data, value, sizeofT*ndata, stream));
}
void set_gpu_array_T(void *data, const int ndata, const int value, const size_t sizeofT) {
cudaCheck(cudaMemset(data, value, sizeofT*ndata));
}
//----------------------------------------------------------------------------------------
void copy3D_HtoD_T(void* src_data, void* dst_data,
int src_x0, int src_y0, int src_z0,
size_t src_xsize, size_t src_ysize,
int dst_x0, int dst_y0, int dst_z0,
size_t width, size_t height, size_t depth,
size_t dst_xsize, size_t dst_ysize,
size_t sizeofT) {
cudaMemcpy3DParms parms = {0};
parms.srcPos = make_cudaPos(sizeofT*src_x0, src_y0, src_z0);
parms.srcPtr = make_cudaPitchedPtr(src_data, sizeofT*src_xsize, src_xsize, src_ysize);
parms.dstPos = make_cudaPos(sizeofT*dst_x0, dst_y0, dst_z0);
parms.dstPtr = make_cudaPitchedPtr(dst_data, sizeofT*dst_xsize, dst_xsize, dst_ysize);
parms.extent = make_cudaExtent(sizeofT*width, height, depth);
parms.kind = cudaMemcpyHostToDevice;
// cudaCheck(cudaMemcpy3D(&parms));
if (cudaMemcpy3D(&parms) != cudaSuccess) {
std::cerr << "copy3D_HtoD_T" << std::endl;
std::cerr << "source: " << std::endl;
std::cerr << parms.srcPos.x << " " << parms.srcPos.y << " " << parms.srcPos.z << std::endl;
std::cerr << parms.srcPtr.pitch << " " << parms.srcPtr.xsize << " "<< parms.srcPtr.ysize << std::endl;
std::cerr << "destination: " << std::endl;
std::cerr << parms.dstPos.x << " " << parms.dstPos.y << " " << parms.dstPos.z << std::endl;
std::cerr << parms.dstPtr.pitch << " " << parms.dstPtr.xsize << " "<< parms.dstPtr.ysize << std::endl;
std::cerr << "extent: " << std::endl;
std::cerr << parms.extent.width << " "<< parms.extent.height << " "<< parms.extent.depth << std::endl;
exit(1);
}
}
//----------------------------------------------------------------------------------------
void copy3D_DtoH_T(void* src_data, void* dst_data,
int src_x0, int src_y0, int src_z0,
size_t src_xsize, size_t src_ysize,
int dst_x0, int dst_y0, int dst_z0,
size_t width, size_t height, size_t depth,
size_t dst_xsize, size_t dst_ysize,
size_t sizeofT) {
cudaMemcpy3DParms parms = {0};
parms.srcPos = make_cudaPos(sizeofT*src_x0, src_y0, src_z0);
parms.srcPtr = make_cudaPitchedPtr(src_data, sizeofT*src_xsize, src_xsize, src_ysize);
parms.dstPos = make_cudaPos(sizeofT*dst_x0, dst_y0, dst_z0);
parms.dstPtr = make_cudaPitchedPtr(dst_data, sizeofT*dst_xsize, dst_xsize, dst_ysize);
parms.extent = make_cudaExtent(sizeofT*width, height, depth);
parms.kind = cudaMemcpyDeviceToHost;
// cudaCheck(cudaMemcpy3D(&parms));
if (cudaMemcpy3D(&parms) != cudaSuccess) {
std::cerr << "copy3D_DtoH_T" << std::endl;
std::cerr << "source: " << std::endl;
std::cerr << parms.srcPos.x << " " << parms.srcPos.y << " " << parms.srcPos.z << std::endl;
std::cerr << parms.srcPtr.pitch << " " << parms.srcPtr.xsize << " "<< parms.srcPtr.ysize << std::endl;
std::cerr << "destination: " << std::endl;
std::cerr << parms.dstPos.x << " " << parms.dstPos.y << " " << parms.dstPos.z << std::endl;
std::cerr << parms.dstPtr.pitch << " " << parms.dstPtr.xsize << " "<< parms.dstPtr.ysize << std::endl;
std::cerr << "extent: " << std::endl;
std::cerr << parms.extent.width << " "<< parms.extent.height << " "<< parms.extent.depth << std::endl;
exit(1);
}
}
//----------------------------------------------------------------------------------------
void gpu_range_start(const char *range_name) {
static int color_id=0;
nvtxEventAttributes_t att;
att.version = NVTX_VERSION;
att.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
att.colorType = NVTX_COLOR_ARGB;
if (color_id == 0) {
att.color = 0xFFFF0000;
} else if (color_id == 1) {
att.color = 0xFF00FF00;
} else if (color_id == 2) {
att.color = 0xFF0000FF;
} else if (color_id == 3) {
att.color = 0xFFFF00FF;
}
color_id++;
if (color_id > 3) color_id = 0;
att.messageType = NVTX_MESSAGE_TYPE_ASCII;
att.message.ascii = range_name;
nvtxRangePushEx(&att);
}
void gpu_range_stop() {
nvtxRangePop();
}
//----------------------------------------------------------------------------------------
__global__ void read_CUDA_ARCH_kernel(int *cuda_arch) {
if (threadIdx.x == 0) {
#if __CUDA_ARCH__ == 100
*cuda_arch = 100;
#elif __CUDA_ARCH__ == 110
*cuda_arch = 110;
#elif __CUDA_ARCH__ == 120
*cuda_arch = 120;
#elif __CUDA_ARCH__ == 130
*cuda_arch = 130;
#elif __CUDA_ARCH__ == 200
*cuda_arch = 200;
#elif __CUDA_ARCH__ == 210
*cuda_arch = 210;
#elif __CUDA_ARCH__ == 300
*cuda_arch = 300;
#elif __CUDA_ARCH__ == 350
*cuda_arch = 350;
#elif __CUDA_ARCH__ == 500
*cuda_arch = 500;
#else
*cuda_arch = 500;
#endif
}
}
//
// Reads the value of __CUDA_ARCH__ from device code
//
int read_CUDA_ARCH() {
int *d_cuda_arch;
int h_cuda_arch;
allocate<int>(&d_cuda_arch, 1);
read_CUDA_ARCH_kernel <<< 1, 1 >>> (d_cuda_arch);
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
std::cout << "Error executing CUDA kernel read_CUDA_ARCH_kernel in file " << __FILE__ << std::endl;
std::cout << "Error string: " << cudaGetErrorString(err) << std::endl;
std::cout << "Possible cause: Device compute capability is less than the compute capability the code was compiled for." << std::endl;
exit(1);
}
cudaCheck(cudaDeviceSynchronize());
copy_DtoH_sync<int>(d_cuda_arch, &h_cuda_arch, 1);
deallocate<int>(&d_cuda_arch);
return h_cuda_arch;
}
//----------------------------------------------------------------------------------------
static int gpu_ind = -1;
static cudaDeviceProp gpu_prop;
static int cuda_arch;
static int high_priority;
static int low_priority;
bool gpuDevCompare (std::pair<int, cudaDeviceProp> dev1, std::pair<int, cudaDeviceProp> dev2) {
int dev1cc = dev1.second.major*100+dev1.second.minor;
int dev2cc = dev2.second.major*100+dev2.second.minor;
if (dev1cc == dev2cc) {
// Same compute capacity, choose by the number of SMs
int dev1sm = dev1.second.multiProcessorCount;
int dev2sm = dev2.second.multiProcessorCount;
return (dev1sm >= dev2sm);
} else {
return (dev1cc >= dev2cc);
}
}
void start_gpu(int numnode, int mynode, std::vector<int>& devices) {
int deviceNum;
cudaCheck(cudaGetDeviceCount(&deviceNum));
if (deviceNum < 1) {
std::cout << "No CUDA device found" << std::endl;
exit(1);
}
if (devices.size() != numnode) {
//if (mynode == 0) std::cout << "Selecting GPUs most powerful first" << std::endl;
// Get all device properties and sort "most powerful first"
std::vector<int> deviceID(deviceNum);
std::vector<std::pair<int, cudaDeviceProp> > gpuList(deviceNum);
for (int i=0;i < deviceNum;i++) {
gpuList.at(i).first = i;
cudaCheck(cudaGetDeviceProperties(&gpuList.at(i).second, i));
}
std::sort(gpuList.begin(), gpuList.end(), gpuDevCompare);
gpu_ind = gpuList.at(mynode % deviceNum).first;
} else {
//if (mynode == 0) std::cout << "Selecting GPUs using device list" << std::endl;
// Use devices in order determined by "devices"
gpu_ind = devices.at(mynode % deviceNum);
}
cudaCheck(cudaSetDevice(gpu_ind));
cudaCheck(cudaDeviceSynchronize());
cudaCheck(cudaGetDeviceProperties(&gpu_prop, gpu_ind));
if (has_stream_priorities()) {
cudaCheck(cudaDeviceGetStreamPriorityRange(&low_priority, &high_priority));
}
if (gpu_prop.major < 2) {
std::cout << "CUDA device(s) must have compute capability 2.0 or higher" << std::endl;
exit(1);
}
int cuda_driver_version;
cudaCheck(cudaDriverGetVersion(&cuda_driver_version));
int cuda_rt_version;
cudaCheck(cudaRuntimeGetVersion(&cuda_rt_version));
cuda_arch = read_CUDA_ARCH();
if (cuda_arch < 200) {
std::cout << "Code must be compiled with compute capability 2.0 or higher" << std::endl;
exit(1);
}
#ifdef USE_TEXTURE_OBJECTS
if (cuda_arch < 300 || cuda_rt_version < 5000 || cuda_driver_version < 5000) {
std::cout << "When compiled with USE_TEXTURE_OBJECTS, CUDA 5.0 and compute capability 3.0 (Kepler) or greater required" << std::endl;
exit(1);
}
#endif
if (mynode == 0) {
std::cout << "Number of CUDA devices found " << deviceNum << std::endl;
std::cout << "Using CUDA driver version " << cuda_driver_version << std::endl;
std::cout << "Using CUDA runtime version " << cuda_rt_version << std::endl;
//std::cout << "Compiled using CUDA_ARCH " << cuda_arch << std::endl;
}
std::cout << "Node " << mynode << " uses CUDA device " << gpu_ind
<< " " << gpu_prop.name << " with CUDA_ARCH " << cuda_arch << std::endl;
}
void stop_gpu() {
cudaCheck(cudaDeviceReset());
gpu_ind = -1;
}
int3 get_max_nblock() {
int3 max_nblock;
max_nblock.x = gpu_prop.maxGridSize[0];
max_nblock.y = gpu_prop.maxGridSize[1];
max_nblock.z = gpu_prop.maxGridSize[2];
if (cuda_arch <= 200) {
max_nblock.x = min(65535, max_nblock.x);
max_nblock.y = min(65535, max_nblock.y);
max_nblock.z = min(65535, max_nblock.z);
}
return max_nblock;
}
bool has_stream_priorities() {
return (bool)gpu_prop.streamPrioritiesSupported;
}
int low_stream_priority() {
return low_priority;
}
int high_stream_priority() {
return high_priority;
}
int get_max_nthread() {
return gpu_prop.maxThreadsPerBlock;
}
int get_max_shmem_size() {
return gpu_prop.sharedMemPerBlock;
}
int get_major() {
return gpu_prop.major;
}
int get_gpu_ind() {
return gpu_ind;
}
int get_cuda_arch() {
return cuda_arch;
}
//
// Save float3 device buffer on disk
//
void save_float3(const int n, const float3* buf, const char* filename) {
std::ofstream file(filename);
if (file.is_open()) {
float3 *h_buf = new float3[n];
copy_DtoH_sync<float3>(buf, h_buf, n);
for (int i=0;i < n;i++) {
file << h_buf[i].x << " " << h_buf[i].y << " " << h_buf[i].z << std::endl;
}
delete [] h_buf;
} else {
std::cerr<<"Error opening file "<<filename<<std::endl;
exit(1);
}
}