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
|
#include <vector>
#include <string>
#include <iostream>
#include "../../nerv/lib/matrix/cuda_helper.h"
#include <dlfcn.h>
extern "C" {
#include "Device.h"
#include "stdlib.h"
#include "string.h"
struct GPUInfo
{
int gpuid;
float d2h_bandwidth;
float h2d_bandwidth;
size_t mem_free;
size_t mem_total;
float mem_ratio;
bool used;
};
struct MPIGPUInfo
{
char hostname[STRLEN];
int gpuid;
int myid;
};
struct Device
{
std::vector<GPUInfo> gpuinfo_;
int refcount;
};
///////////////////////////////////////////
extern cublasHandle_t* nerv_get_cublas_handle();
CuContext* CuContext_new()
{
CuContext *context = new CuContext;
cublasCreate(&context->cublas_handle);
cudaEventCreate(&context->profile_start);
cudaEventCreate(&context->profile_stop);
context->profile = hashmap_create(PROFILE_HASHMAP_SIZE, bkdr_hash, strcmp);
context->refcount = 1;
context->pid = pthread_self();
return context;
}
CuContext* CuContext_newWithId(long id)
{
CuContext *context = (CuContext*)id;
__sync_fetch_and_add(&context->refcount, 1);
return context;
}
long CuContext_id(CuContext *context)
{
return (long)context;
}
void CuContext_destroy(CuContext *context)
{
if (NULL != context && __sync_fetch_and_add(&context->refcount, -1) == 1)
{
cublasDestroy(context->cublas_handle);
hashmap_clear(context->profile);
delete context;
context = NULL;
}
}
Device* Device_new()
{
Device* device = new Device;
device->refcount = 1;
return device;
}
Device* Device_newWithId(long id)
{
Device *device = (Device*)id;
__sync_fetch_and_add(&device->refcount, 1);
return device;
}
long Device_id(Device *device)
{
return (long)device;
}
void Device_destroy(Device *device)
{
if (NULL != device && __sync_fetch_and_add(&device->refcount, -1) == 1)
{
delete device;
device = NULL;
}
}
void GetFreeMemory(size_t* free, size_t* total, Status *status)
{
// WARNING! the CUDA API is inconsistent accross versions!
#if (CUDA_VERSION >= 3020)
//define the function signature type
size_t mem_free, mem_total;
#else
unsigned int mem_free, mem_total;
#endif
{
//we will load the cuMemGetInfo dynamically from libcuda.so
//cuMemGetInfo(&mem_free, &mem_total);
//pre-fill ``safe'' values that will not cause problems
mem_free = 1; mem_total = 1;
//open libcuda.so
void* libcuda = dlopen("libcuda.so",RTLD_LAZY);
if(NULL == libcuda)
{
NERV_EXIT_STATUS(status, MAT_CUDA_ERR, "Cannot open libcuda.so");
}
else
{
//define the function signature type
//and get the symbol
#if (CUDA_VERSION >= 3020)
typedef CUresult (*cu_fun_ptr)(size_t*, size_t*);
cu_fun_ptr dl_cuMemGetInfo = (cu_fun_ptr)dlsym(libcuda,"cuMemGetInfo_v2");
#else
typedef CUresult (*cu_fun_ptr)(int*, int*);
cu_fun_ptr dl_cuMemGetInfo = (cu_fun_ptr)dlsym(libcuda,"cuMemGetInfo");
#endif
if(NULL == dl_cuMemGetInfo)
{
NERV_EXIT_STATUS(status, MAT_CUDA_ERR, "Cannot load cuMemGetInfo from libcuda.so");
}
else
{
//call the function
dl_cuMemGetInfo(&mem_free, &mem_total);
}
//close the library
dlclose(libcuda);
}
}
// copy the output values outside
if(NULL != free) *free = mem_free;
if(NULL != total) *total = mem_total;
}
void DeviceGetName(char* name, int len, int dev, Status *status)
{
//prefill with something reasonable
strncpy(name,"Unknown GPU",len);
//open libcuda.so
void* libcuda = dlopen("libcuda.so",RTLD_LAZY);
if(NULL == libcuda)
{
NERV_EXIT_STATUS(status, MAT_CUDA_ERR, "Cannot open libcuda.so");
}
else
{
//define the function signature type
typedef CUresult (*cu_fun_ptr)(char*,int,CUdevice);
//get the symbol
cu_fun_ptr cuDeviceGetName_ptr = (cu_fun_ptr)dlsym(libcuda,"cuDeviceGetName");
if(NULL == cuDeviceGetName_ptr) {
NERV_EXIT_STATUS(status, MAT_CUDA_ERR, "Cannot load cuDeviceGetName from libcuda.so");
}
else {
//call the function
cuDeviceGetName_ptr(name, len, dev);
}
//close the library
dlclose(libcuda);
}
}
void GetBandwidth(int gpu_idx, float *d2h, float *h2d, Status *status)
{
int idx = gpu_idx;
int memSize = 64*1024*1024;
float elapsedTimeInMs = 0.0f;
float bandwidthInMBs = 0.0f;
unsigned char *h_idata = NULL;
unsigned char *h_odata = NULL;
cudaEvent_t start, stop;
bool PINNED = true;
int MEMCOPY_ITERATIONS = 5;
CUDA_SAFE_SYNC_CALL(cudaEventCreate(&start), status);
CUDA_SAFE_SYNC_CALL(cudaEventCreate(&stop), status);
//allocate host memory
if (PINNED)
{
#if CUDART_VERSION >= 2020
CUDA_SAFE_SYNC_CALL(cudaHostAlloc((void **)&h_idata, memSize, cudaHostAllocPortable), status);
CUDA_SAFE_SYNC_CALL(cudaHostAlloc((void **)&h_odata, memSize, cudaHostAllocPortable), status);
#else
CUDA_SAFE_SYNC_CALL(cudaMallocHost((void **)&h_idata, memSize), status);
CUDA_SAFE_SYNC_CALL(cudaMallocHost((void **)&h_odata, memSize), status);
#endif
}
else
{
//pageable memory mode - use malloc
h_odata = (unsigned char *)malloc(memSize);
if (h_odata == 0)
{
NERV_EXIT_STATUS(status, MAT_CUDA_ERR, "Not enough memory available on host to run test!");
}
}
//initialize the memory
for (unsigned int i = 0; i < memSize/sizeof(unsigned char); i++)
{
h_idata[i] = (unsigned char)(i & 0xff);
}
// allocate device memory
unsigned char *d_idata;
CUDA_SAFE_SYNC_CALL(cudaMalloc((void **) &d_idata, memSize), status);
//initialize the device memory
CUDA_SAFE_SYNC_CALL(cudaMemcpy(d_idata, h_idata, memSize,cudaMemcpyHostToDevice), status);
//copy data from GPU to Host
CUDA_SAFE_SYNC_CALL(cudaEventRecord(start, 0), status);
if (PINNED)
{
for (unsigned int i = 0; i < MEMCOPY_ITERATIONS; i++)
{
CUDA_SAFE_SYNC_CALL(cudaMemcpyAsync(h_odata, d_idata, memSize,cudaMemcpyDeviceToHost, 0), status);
}
}
else
{
for (unsigned int i = 0; i < MEMCOPY_ITERATIONS; i++)
{
CUDA_SAFE_SYNC_CALL(cudaMemcpy(h_odata, d_idata, memSize,cudaMemcpyDeviceToHost), status);
}
}
CUDA_SAFE_SYNC_CALL(cudaEventRecord(stop, 0), status);
// make sure GPU has finished copying
CUDA_SAFE_SYNC_CALL(cudaDeviceSynchronize(), status);
//get the the total elapsed time in ms
CUDA_SAFE_SYNC_CALL(cudaEventElapsedTime(&elapsedTimeInMs, start, stop), status);
//calculate bandwidth in MB/s
bandwidthInMBs = (1e3f * memSize * (float)MEMCOPY_ITERATIONS) / (elapsedTimeInMs * (float)(1 << 20));
*d2h = bandwidthInMBs;
/////////////////////////////////////////////////////
//copy data from Host to GPU
CUDA_SAFE_SYNC_CALL(cudaEventRecord(start, 0), status);
if (PINNED)
{
for (unsigned int i = 0; i < MEMCOPY_ITERATIONS; i++)
{
CUDA_SAFE_SYNC_CALL(cudaMemcpyAsync(d_idata, h_odata, memSize,cudaMemcpyHostToDevice, 0), status);
}
}
else
{
for (unsigned int i = 0; i < MEMCOPY_ITERATIONS; i++)
{
CUDA_SAFE_SYNC_CALL(cudaMemcpy(d_idata, h_odata, memSize,cudaMemcpyHostToDevice), status);
}
}
CUDA_SAFE_SYNC_CALL(cudaEventRecord(stop, 0), status);
// make sure GPU has finished copying
CUDA_SAFE_SYNC_CALL(cudaDeviceSynchronize(), status);
//get the the total elapsed time in ms
CUDA_SAFE_SYNC_CALL(cudaEventElapsedTime(&elapsedTimeInMs, start, stop), status);
//calculate bandwidth in MB/s
bandwidthInMBs = (1e3f * memSize * (float)MEMCOPY_ITERATIONS) / (elapsedTimeInMs * (float)(1 << 20));
*h2d = bandwidthInMBs;
//clean up memory
CUDA_SAFE_SYNC_CALL(cudaEventDestroy(stop), status);
CUDA_SAFE_SYNC_CALL(cudaEventDestroy(start), status);
if (PINNED)
{
CUDA_SAFE_SYNC_CALL(cudaFreeHost(h_idata), status);
CUDA_SAFE_SYNC_CALL(cudaFreeHost(h_odata), status);
}
else
{
free(h_idata);
free(h_odata);
}
CUDA_SAFE_SYNC_CALL(cudaFree(d_idata), status);
}
int AutoSelectGPU(Device *device, Status *status)
{
//GPU selection is based on largest proportion of free memory.
int max_id = 0, n_gpu = 0;
std::vector<GPUInfo> &gpuinfo_ = device->gpuinfo_;
cudaGetDeviceCount(&n_gpu);
if(n_gpu == 0)
{
NERV_SET_STATUS(status, MAT_CUDA_ERR, "No CUDA devices found");
return -1;
}
if (n_gpu > 0)
{
for (int i = 0; i < gpuinfo_.size(); i++)
{
if (!gpuinfo_[i].used)
{
max_id = i;
break;
}
}
//find GPU with max free memory
for (int n = 1; n < gpuinfo_.size(); n++)
{
if (!gpuinfo_[n].used && gpuinfo_[n].mem_ratio > gpuinfo_[max_id].mem_ratio)
max_id = n;
}
std::cerr << "Selected device: " << max_id << " (automatically)" << std::endl;
std::cerr << "free: " << gpuinfo_[max_id].mem_free/1024/1024 << "M, "
<< "total: "<< gpuinfo_[max_id].mem_total/1024/1024 << "M, "
<< "ratio: "<< gpuinfo_[max_id].mem_ratio << " "
<< "d2h bandwidth: " << gpuinfo_[max_id].d2h_bandwidth << "MB/s, "
<< "h2d bandwidth: "<< gpuinfo_[max_id].h2d_bandwidth << "MB/s" << std::endl;
cudaSetDevice(max_id);
//initialize the CUBLAS
//cublasInit();
//cublasHandle_t *cublas_handle = nerv_get_cublas_handle();
//std::cerr << "cublasHandle_t: " << cublas_handle << std::endl;
//cublasCreate(cublas_handle);
//create the context
cudaError_t e;
e = cudaThreadSynchronize(); //deprecated, but for legacy not cudaDeviceSynchronize
if(e != cudaSuccess) {
std::cerr << "Failed to create CUDA context on a GPU." << std::endl;
}
}
gpuinfo_[max_id].used = true;
NERV_SET_STATUS(status, NERV_NORMAL, 0);
return max_id;
}
void SelectGPU(Device *device, int gpu_id, Status *status)
{
std::vector<GPUInfo> &gpuinfo_ = device->gpuinfo_;
int n_gpu = 0;
cudaGetDeviceCount(&n_gpu);
if(gpu_id >= n_gpu)
{
NERV_EXIT_STATUS(status, MAT_CUDA_ERR, "Cannot select GPU CUDA capable cards!");
std::cerr << "Cannot select GPU " << gpu_id
<< ", detected " << n_gpu << " CUDA capable cards!" << std::endl;
}
std::cerr << "Selected device: " << gpu_id << " (manually)";
std::cerr << "free: " << gpuinfo_[gpu_id].mem_free/1024/1024 << "M, "
<< "total: "<< gpuinfo_[gpu_id].mem_total/1024/1024 << "M, "
<< "ratio: "<< gpuinfo_[gpu_id].mem_ratio << " "
<< "d2h bandwidth: " << gpuinfo_[gpu_id].d2h_bandwidth << "MB/s, "
<< "h2d bandwidth: "<< gpuinfo_[gpu_id].h2d_bandwidth << "MB/s" << std::endl;
CUDA_SAFE_SYNC_CALL(cudaSetDevice(gpu_id), status);
//initialize the CUBLAS
//CUBLAS_SAFE_SYNC_CALL(cublasInit(), status);
//cublasHandle_t *cublas_handle = nerv_get_cublas_handle();
//cublasCreate(cublas_handle);
//create the context
cudaError_t e;
e = cudaThreadSynchronize(); //deprecated, but for legacy not cudaDeviceSynchronize
if(e != cudaSuccess) {
NERV_EXIT_STATUS(status, MAT_CUDA_ERR, "Failed to create CUDA context on a GPU.");
std::cerr << "Failed to create CUDA context on a GPU.";
}
gpuinfo_[gpu_id].used = true;
NERV_SET_STATUS(status, NERV_NORMAL, 0);
}
void Initialize(Device *device, Status *status)
{
// Check that we have at least one gpu
int n_gpu = 0;
cudaGetDeviceCount(&n_gpu);
if(n_gpu == 0)
NERV_EXIT_STATUS(status, MAT_CUDA_ERR, "No CUDA devices found");
std::vector<GPUInfo> &gpuinfo_ = device->gpuinfo_;
gpuinfo_.resize(n_gpu);
std::cerr << "gpu information ..." << std::endl;
// Get ratios of memory use, if possible
for(int n = 0; n < n_gpu; n++)
{
int ret = cudaSetDevice(n);
switch(ret)
{
case cudaSuccess : {
//create the CUDA context for the thread
//cudaThreadSynchronize(); //deprecated, but for legacy not cudaDeviceSynchronize
cudaDeviceSynchronize();
//get GPU name
char name[STRLEN];
DeviceGetName(name,STRLEN,n, status);
//get GPU memory stats
size_t free, total;
float d2h, h2d;
GetFreeMemory(&free, &total, status);
GetBandwidth(n, &d2h, &h2d, status);
gpuinfo_[n].gpuid = n;
gpuinfo_[n].d2h_bandwidth = d2h;
gpuinfo_[n].h2d_bandwidth = h2d;
gpuinfo_[n].mem_free = free;
gpuinfo_[n].mem_total = total;
gpuinfo_[n].mem_ratio = free/(float)total;
gpuinfo_[n].used = false;
std::cerr << "gpu: " << n << " ==> "
<< "free: " << free/1024/1024 << "M, "
<< "total: "<< total/1024/1024 << "M, "
<< "ratio: "<< free/(float)total << " "
<< "d2h bandwidth: " << d2h << "MB/s, "
<< "h2d bandwidth: "<< h2d << "MB/s" << std::endl;
//destroy the CUDA context for the thread
//cudaThreadExit(); //deprecated, but for legacy reason not cudaDeviceReset
} break;
#if (CUDA_VERSION > 3020)
case cudaErrorDeviceAlreadyInUse :
std::cerr << "cudaSetDevice(" << n << "): "
<< "Device cannot be accessed, used EXCLUSIVE-THREAD mode..." << std::endl;
break;
#endif
case cudaErrorInvalidDevice :
std::cerr << "cudaSetDevice(" << n << "): "
<< "Device cannot be accessed, not a VALID CUDA device!" << std::endl;
break;
default :
std::cerr << "cudaSetDevice(" << n << "): "
<< "returned " << ret << ", "
<< cudaGetErrorString((cudaError_t)ret) << std::endl;
}
}
cudaDeviceReset();
printf("Result = PASS\n");
NERV_SET_STATUS(status, NERV_NORMAL, 0);
}
} // extern
|