forked from Sunyouteng/darknet_prune_yoloV3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdark_cuda.c
647 lines (575 loc) · 20.3 KB
/
dark_cuda.c
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#ifdef __cplusplus
extern "C" {
#endif
int cuda_debug_sync = 0;
int gpu_index = 0;
#ifdef __cplusplus
}
#endif // __cplusplus
#ifdef GPU
#include "dark_cuda.h"
#include "utils.h"
#include "blas.h"
#include "assert.h"
#include <stdlib.h>
#include <time.h>
#include <cuda.h>
#include <stdio.h>
#pragma comment(lib, "cuda.lib")
#ifdef CUDNN
#ifndef USE_CMAKE_LIBS
#pragma comment(lib, "cudnn.lib")
#endif // USE_CMAKE_LIBS
#endif // CUDNN
#if defined(CUDNN_HALF) && !defined(CUDNN)
#error "If you set CUDNN_HALF=1 then you must set CUDNN=1"
#endif
void cuda_set_device(int n)
{
gpu_index = n;
cudaError_t status = cudaSetDevice(n);
if(status != cudaSuccess) CHECK_CUDA(status);
}
int cuda_get_device()
{
int n = 0;
cudaError_t status = cudaGetDevice(&n);
CHECK_CUDA(status);
return n;
}
void *cuda_get_context()
{
CUcontext pctx;
CUresult status = cuCtxGetCurrent(&pctx);
if(status != CUDA_SUCCESS) fprintf(stderr, " Error: cuCtxGetCurrent() is failed \n");
return (void *)pctx;
}
void check_error(cudaError_t status)
{
cudaError_t status2 = cudaGetLastError();
if (status != cudaSuccess)
{
const char *s = cudaGetErrorString(status);
char buffer[256];
printf("\n CUDA Error: %s\n", s);
snprintf(buffer, 256, "CUDA Error: %s", s);
#ifdef WIN32
getchar();
#endif
error(buffer);
}
if (status2 != cudaSuccess)
{
const char *s = cudaGetErrorString(status2);
char buffer[256];
printf("\n CUDA Error Prev: %s\n", s);
snprintf(buffer, 256, "CUDA Error Prev: %s", s);
#ifdef WIN32
getchar();
#endif
error(buffer);
}
}
void check_error_extended(cudaError_t status, const char *file, int line, const char *date_time)
{
if (status != cudaSuccess) {
printf("CUDA status Error: file: %s() : line: %d : build time: %s \n", file, line, date_time);
check_error(status);
}
#if defined(DEBUG) || defined(CUDA_DEBUG)
cuda_debug_sync = 1;
#endif
if (cuda_debug_sync) {
status = cudaDeviceSynchronize();
if (status != cudaSuccess)
printf("CUDA status = cudaDeviceSynchronize() Error: file: %s() : line: %d : build time: %s \n", file, line, date_time);
}
check_error(status);
}
dim3 cuda_gridsize(size_t n){
size_t k = (n-1) / BLOCK + 1;
size_t x = k;
size_t y = 1;
if(x > 65535){
x = ceil(sqrt(k));
y = (n-1)/(x*BLOCK) + 1;
}
//dim3 d = { (unsigned int)x, (unsigned int)y, 1 };
dim3 d;
d.x = x;
d.y = y;
d.z = 1;
//printf("%ld %ld %ld %ld\n", n, x, y, x*y*BLOCK);
return d;
}
static cudaStream_t streamsArray[16]; // cudaStreamSynchronize( get_cuda_stream() );
static int streamInit[16] = { 0 };
cudaStream_t get_cuda_stream() {
int i = cuda_get_device();
if (!streamInit[i]) {
printf("Create CUDA-stream - %d \n", i);
#ifdef CUDNN
cudaError_t status = cudaStreamCreateWithFlags(&streamsArray[i], cudaStreamNonBlocking);
#else
cudaError_t status = cudaStreamCreate(&streamsArray[i]);
#endif
if (status != cudaSuccess) {
printf(" cudaStreamCreate error: %d \n", status);
const char *s = cudaGetErrorString(status);
printf("CUDA Error: %s\n", s);
status = cudaStreamCreateWithFlags(&streamsArray[i], cudaStreamNonBlocking); // cudaStreamDefault
CHECK_CUDA(status);
}
streamInit[i] = 1;
}
return streamsArray[i];
}
/*
static cudaStream_t streamsArray2[16]; // cudaStreamSynchronize( get_cuda_memcpy_stream() );
static int streamInit2[16] = { 0 };
cudaStream_t get_cuda_memcpy_stream() {
int i = cuda_get_device();
if (!streamInit2[i]) {
printf(" Create COPY stream %d \n", i);
//cudaError_t status = cudaStreamCreate(&streamsArray2[i], cudaStreamNonBlocking);
cudaError_t status = cudaStreamCreateWithFlags(&streamsArray2[i], cudaStreamNonBlocking);
if (status != cudaSuccess) {
printf(" cudaStreamCreate-Memcpy error: %d \n", status);
const char *s = cudaGetErrorString(status);
printf("CUDA Error: %s\n", s);
status = cudaStreamCreateWithFlags(&streamsArray2[i], cudaStreamNonBlocking);
CHECK_CUDA(status);
}
streamInit2[i] = 1;
}
return streamsArray2[i];
}
*/
#ifdef CUDNN
static int cudnnInit[16] = { 0 };
static cudnnHandle_t cudnnHandle[16];
cudnnHandle_t cudnn_handle()
{
int i = cuda_get_device();
if(!cudnnInit[i]) {
cudnnCreate(&cudnnHandle[i]);
cudnnInit[i] = 1;
cudnnStatus_t status = cudnnSetStream(cudnnHandle[i], get_cuda_stream());
CHECK_CUDNN(status);
printf(" Create cudnn-handle %d \n", i);
}
return cudnnHandle[i];
}
void cudnn_check_error(cudnnStatus_t status)
{
#if defined(DEBUG) || defined(CUDA_DEBUG)
cudaDeviceSynchronize();
#endif
if (cuda_debug_sync) {
cudaDeviceSynchronize();
}
cudnnStatus_t status2 = CUDNN_STATUS_SUCCESS;
#ifdef CUDNN_ERRQUERY_RAWCODE
cudnnStatus_t status_tmp = cudnnQueryRuntimeError(cudnn_handle(), &status2, CUDNN_ERRQUERY_RAWCODE, NULL);
#endif
if (status != CUDNN_STATUS_SUCCESS)
{
const char *s = cudnnGetErrorString(status);
char buffer[256];
printf("\n cuDNN Error: %s\n", s);
snprintf(buffer, 256, "cuDNN Error: %s", s);
#ifdef WIN32
getchar();
#endif
error(buffer);
}
if (status2 != CUDNN_STATUS_SUCCESS)
{
const char *s = cudnnGetErrorString(status2);
char buffer[256];
printf("\n cuDNN Error Prev: %s\n", s);
snprintf(buffer, 256, "cuDNN Error Prev: %s", s);
#ifdef WIN32
getchar();
#endif
error(buffer);
}
}
void cudnn_check_error_extended(cudnnStatus_t status, const char *file, int line, const char *date_time)
{
if (status != CUDNN_STATUS_SUCCESS) {
printf("\n cuDNN status Error in: file: %s() : line: %d : build time: %s \n", file, line, date_time);
cudnn_check_error(status);
}
#if defined(DEBUG) || defined(CUDA_DEBUG)
cuda_debug_sync = 1;
#endif
if (cuda_debug_sync) {
cudaError_t status = cudaDeviceSynchronize();
if (status != CUDNN_STATUS_SUCCESS)
printf("\n cudaError_t status = cudaDeviceSynchronize() Error in: file: %s() : line: %d : build time: %s \n", file, line, date_time);
}
cudnn_check_error(status);
}
static cudnnHandle_t switchCudnnHandle[16];
static int switchCudnnInit[16];
#endif
void cublas_check_error(cublasStatus_t status)
{
#if defined(DEBUG) || defined(CUDA_DEBUG)
cudaDeviceSynchronize();
#endif
if (cuda_debug_sync) {
cudaDeviceSynchronize();
}
if (status != CUBLAS_STATUS_SUCCESS) {
printf("cuBLAS Error\n");
}
}
void cublas_check_error_extended(cublasStatus_t status, const char *file, int line, const char *date_time)
{
if (status != CUBLAS_STATUS_SUCCESS) {
printf("\n cuBLAS status Error in: file: %s() : line: %d : build time: %s \n", file, line, date_time);
}
#if defined(DEBUG) || defined(CUDA_DEBUG)
cuda_debug_sync = 1;
#endif
if (cuda_debug_sync) {
cudaError_t status = cudaDeviceSynchronize();
if (status != CUDA_SUCCESS)
printf("\n cudaError_t status = cudaDeviceSynchronize() Error in: file: %s() : line: %d : build time: %s \n", file, line, date_time);
}
cublas_check_error(status);
}
static int blasInit[16] = { 0 };
static cublasHandle_t blasHandle[16];
cublasHandle_t blas_handle()
{
int i = cuda_get_device();
if (!blasInit[i]) {
CHECK_CUBLAS(cublasCreate(&blasHandle[i]));
cublasStatus_t status = cublasSetStream(blasHandle[i], get_cuda_stream());
CHECK_CUBLAS(status);
blasInit[i] = 1;
}
return blasHandle[i];
}
static int switchBlasInit[16] = { 0 };
static cublasHandle_t switchBlasHandle[16];
static cudaStream_t switchStreamsArray[16];
static int switchStreamInit[16] = { 0 };
cudaStream_t switch_stream(int i) {
int dev_id = cuda_get_device();
//printf(" switch_stream = %d \n", i);
if (!switchStreamInit[i]) {
CHECK_CUDA(cudaStreamCreateWithFlags(&switchStreamsArray[i], cudaStreamNonBlocking));
switchStreamInit[i] = 1;
printf(" Create stream %d \n", i);
}
//cudaStreamQuery(streamsArray[0]); // Flush previous stream queue
streamsArray[dev_id] = switchStreamsArray[i];
streamInit[dev_id] = switchStreamInit[i];
//printf("switch_stream %d - get_cuda_stream() = %d \n", i, get_cuda_stream());
/*
if (!switchBlasInit[i]) {
CHECK_CUDA( cublasCreate(&switchBlasHandle[i]) );
switchBlasInit[i] = 1;
CHECK_CUDA( cublasSetStream(switchBlasHandle[i], switchStreamsArray[i]) );
printf(" Create blas-handle %d \n", i);
}
blasHandle[dev_id] = switchBlasHandle[i];
blasInit[dev_id] = switchBlasInit[i];
*/
#ifdef CUDNN
if (!switchCudnnInit[i]) {
CHECK_CUDNN( cudnnCreate(&switchCudnnHandle[i]) );
switchCudnnInit[i] = 1;
CHECK_CUDNN(cudnnSetStream(switchCudnnHandle[i], switchStreamsArray[i]));
printf(" Create cudnn-handle %d \n", i);
}
cudnnHandle[dev_id] = switchCudnnHandle[i];
cudnnInit[dev_id] = switchCudnnInit[i];
#endif
return switchStreamsArray[i];
}
#ifndef cudaEventWaitDefault
#define cudaEventWaitDefault 0x00
#endif // cudaEventWaitDefault
static const int max_events = 1024;
static cudaEvent_t switchEventsArray[1024];
static volatile int event_counter = 0;
void wait_stream(int i) {
int dev_id = cuda_get_device();
if (event_counter >= max_events) error("CUDA max_events exceeded \n");
CHECK_CUDA( cudaEventCreateWithFlags(&switchEventsArray[event_counter], cudaEventDisableTiming) );
//printf(" create event = %d (wait for stream = %d) \n", event_counter, i);
//CHECK_CUDA(cudaEventRecordWithFlags(switchEventsArray[i], switchStreamsArray[i], cudaEventRecordExternal) );
CHECK_CUDA( cudaEventRecord(switchEventsArray[event_counter], switchStreamsArray[i]) );
CHECK_CUDA( cudaStreamWaitEvent(streamsArray[dev_id], switchEventsArray[event_counter], cudaEventWaitDefault) );
//cudaStreamWaitEvent(streamsArray[dev_id], switchEventsArray[i], cudaEventWaitExternal);
event_counter++;
}
void reset_wait_stream_events() {
int i;
for (i = 0; i < event_counter; ++i) {
CHECK_CUDA(cudaEventDestroy(switchEventsArray[i]));
}
event_counter = 0;
}
static float **pinned_ptr = NULL;
static size_t pinned_num_of_blocks = 0;
static size_t pinned_index = 0;
static size_t pinned_block_id = 0;
static const size_t pinned_block_size = (size_t)1024 * 1024 * 1024 * 1; // 1 GB block size
static pthread_mutex_t mutex_pinned = PTHREAD_MUTEX_INITIALIZER;
// free CPU-pinned memory
void free_pinned_memory()
{
if (pinned_ptr) {
int k;
for (k = 0; k < pinned_num_of_blocks; ++k) {
cuda_free_host(pinned_ptr[k]);
}
free(pinned_ptr);
pinned_ptr = NULL;
}
}
// custom CPU-pinned memory allocation
void pre_allocate_pinned_memory(const size_t size)
{
const size_t num_of_blocks = size / pinned_block_size + ((size % pinned_block_size) ? 1 : 0);
printf("pre_allocate... pinned_ptr = %p \n", pinned_ptr);
pthread_mutex_lock(&mutex_pinned);
if (!pinned_ptr) {
pinned_ptr = (float **)calloc(num_of_blocks, sizeof(float *));
if(!pinned_ptr) error("calloc failed in pre_allocate() \n");
printf("pre_allocate: size = %Iu MB, num_of_blocks = %Iu, block_size = %Iu MB \n",
size / (1024*1024), num_of_blocks, pinned_block_size / (1024 * 1024));
int k;
for (k = 0; k < num_of_blocks; ++k) {
cudaError_t status = cudaHostAlloc((void **)&pinned_ptr[k], pinned_block_size, cudaHostRegisterMapped);
if (status != cudaSuccess) fprintf(stderr, " Can't pre-allocate CUDA-pinned buffer on CPU-RAM \n");
CHECK_CUDA(status);
if (!pinned_ptr[k]) error("cudaHostAlloc failed\n");
else {
printf(" Allocated %d pinned block \n", pinned_block_size);
}
}
pinned_num_of_blocks = num_of_blocks;
}
pthread_mutex_unlock(&mutex_pinned);
}
// simple - get pre-allocated pinned memory
float *cuda_make_array_pinned_preallocated(float *x, size_t n)
{
pthread_mutex_lock(&mutex_pinned);
float *x_cpu = NULL;
const size_t memory_step = 512;// 4096;
const size_t size = sizeof(float)*n;
const size_t allocation_size = ((size / memory_step) + 1) * memory_step;
if (pinned_ptr && pinned_block_id < pinned_num_of_blocks && (allocation_size < pinned_block_size/2))
{
if ((allocation_size + pinned_index) > pinned_block_size) {
const float filled = (float)100 * pinned_index / pinned_block_size;
printf("\n Pinned block_id = %d, filled = %f %% \n", pinned_block_id, filled);
pinned_block_id++;
pinned_index = 0;
}
if ((allocation_size + pinned_index) < pinned_block_size && pinned_block_id < pinned_num_of_blocks) {
x_cpu = (float *)((char *)pinned_ptr[pinned_block_id] + pinned_index);
pinned_index += allocation_size;
}
else {
//printf("Pre-allocated pinned memory is over! \n");
}
}
if(!x_cpu) {
if (allocation_size > pinned_block_size / 2) {
printf("Try to allocate new pinned memory, size = %d MB \n", size / (1024 * 1024));
cudaError_t status = cudaHostAlloc((void **)&x_cpu, size, cudaHostRegisterMapped);
if (status != cudaSuccess) fprintf(stderr, " Can't allocate CUDA-pinned memory on CPU-RAM (pre-allocated memory is over too) \n");
CHECK_CUDA(status);
}
else {
printf("Try to allocate new pinned BLOCK, size = %d MB \n", size / (1024 * 1024));
pinned_num_of_blocks++;
pinned_block_id = pinned_num_of_blocks - 1;
pinned_index = 0;
pinned_ptr = (float **)realloc(pinned_ptr, pinned_num_of_blocks * sizeof(float *));
cudaError_t status = cudaHostAlloc((void **)&pinned_ptr[pinned_block_id], pinned_block_size, cudaHostRegisterMapped);
if (status != cudaSuccess) fprintf(stderr, " Can't pre-allocate CUDA-pinned buffer on CPU-RAM \n");
CHECK_CUDA(status);
x_cpu = pinned_ptr[pinned_block_id];
}
}
if (x) {
cudaError_t status = cudaMemcpyAsync(x_cpu, x, size, cudaMemcpyDefault, get_cuda_stream());
CHECK_CUDA(status);
}
pthread_mutex_unlock(&mutex_pinned);
return x_cpu;
}
float *cuda_make_array_pinned(float *x, size_t n)
{
float *x_gpu;
size_t size = sizeof(float)*n;
//cudaError_t status = cudaMalloc((void **)&x_gpu, size);
cudaError_t status = cudaHostAlloc((void **)&x_gpu, size, cudaHostRegisterMapped);
if (status != cudaSuccess) fprintf(stderr, " Can't allocate CUDA-pinned memory on CPU-RAM \n");
CHECK_CUDA(status);
if (x) {
status = cudaMemcpyAsync(x_gpu, x, size, cudaMemcpyDefault, get_cuda_stream());
CHECK_CUDA(status);
}
if (!x_gpu) error("cudaHostAlloc failed\n");
return x_gpu;
}
float *cuda_make_array(float *x, size_t n)
{
float *x_gpu;
size_t size = sizeof(float)*n;
cudaError_t status = cudaMalloc((void **)&x_gpu, size);
//cudaError_t status = cudaMallocManaged((void **)&x_gpu, size, cudaMemAttachGlobal);
//status = cudaMemAdvise(x_gpu, size, cudaMemAdviseSetPreferredLocation, cudaCpuDeviceId);
if (status != cudaSuccess) fprintf(stderr, " Try to set subdivisions=64 in your cfg-file. \n");
CHECK_CUDA(status);
if(x){
//status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
status = cudaMemcpyAsync(x_gpu, x, size, cudaMemcpyDefault, get_cuda_stream());
CHECK_CUDA(status);
}
if(!x_gpu) error("Cuda malloc failed\n");
return x_gpu;
}
void **cuda_make_array_pointers(void **x, size_t n)
{
void **x_gpu;
size_t size = sizeof(void*) * n;
cudaError_t status = cudaMalloc((void **)&x_gpu, size);
if (status != cudaSuccess) fprintf(stderr, " Try to set subdivisions=64 in your cfg-file. \n");
CHECK_CUDA(status);
if (x) {
status = cudaMemcpyAsync(x_gpu, x, size, cudaMemcpyDefault, get_cuda_stream());
CHECK_CUDA(status);
}
if (!x_gpu) error("Cuda malloc failed\n");
return x_gpu;
}
void cuda_random(float *x_gpu, size_t n)
{
static curandGenerator_t gen[16];
static int init[16] = {0};
int i = cuda_get_device();
if(!init[i]){
curandCreateGenerator(&gen[i], CURAND_RNG_PSEUDO_DEFAULT);
curandSetPseudoRandomGeneratorSeed(gen[i], time(0));
init[i] = 1;
}
curandGenerateUniform(gen[i], x_gpu, n);
CHECK_CUDA(cudaPeekAtLastError());
}
float cuda_compare(float *x_gpu, float *x, size_t n, char *s)
{
float* tmp = (float*)xcalloc(n, sizeof(float));
cuda_pull_array(x_gpu, tmp, n);
//int i;
//for(i = 0; i < n; ++i) printf("%f %f\n", tmp[i], x[i]);
axpy_cpu(n, -1, x, 1, tmp, 1);
float err = dot_cpu(n, tmp, 1, tmp, 1);
printf("Error %s: %f\n", s, sqrt(err/n));
free(tmp);
return err;
}
int *cuda_make_int_array(size_t n)
{
int *x_gpu;
size_t size = sizeof(int)*n;
cudaError_t status = cudaMalloc((void **)&x_gpu, size);
if(status != cudaSuccess) fprintf(stderr, " Try to set subdivisions=64 in your cfg-file. \n");
CHECK_CUDA(status);
return x_gpu;
}
int *cuda_make_int_array_new_api(int *x, size_t n)
{
int *x_gpu;
size_t size = sizeof(int)*n;
cudaError_t status = cudaMalloc((void **)&x_gpu, size);
CHECK_CUDA(status);
if (x) {
//status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
cudaError_t status = cudaMemcpyAsync(x_gpu, x, size, cudaMemcpyHostToDevice, get_cuda_stream());
CHECK_CUDA(status);
}
if (!x_gpu) error("Cuda malloc failed\n");
return x_gpu;
}
void cuda_free(float *x_gpu)
{
//cudaStreamSynchronize(get_cuda_stream());
cudaError_t status = cudaFree(x_gpu);
CHECK_CUDA(status);
}
void cuda_free_host(float *x_cpu)
{
//cudaStreamSynchronize(get_cuda_stream());
cudaError_t status = cudaFreeHost(x_cpu);
CHECK_CUDA(status);
}
void cuda_push_array(float *x_gpu, float *x, size_t n)
{
size_t size = sizeof(float)*n;
//cudaError_t status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
cudaError_t status = cudaMemcpyAsync(x_gpu, x, size, cudaMemcpyHostToDevice, get_cuda_stream());
CHECK_CUDA(status);
}
void cuda_pull_array(float *x_gpu, float *x, size_t n)
{
size_t size = sizeof(float)*n;
//cudaError_t status = cudaMemcpy(x, x_gpu, size, cudaMemcpyDeviceToHost);
//printf("cuda_pull_array - get_cuda_stream() = %d \n", get_cuda_stream());
cudaError_t status = cudaMemcpyAsync(x, x_gpu, size, cudaMemcpyDeviceToHost, get_cuda_stream());
CHECK_CUDA(status);
cudaStreamSynchronize(get_cuda_stream());
}
void cuda_pull_array_async(float *x_gpu, float *x, size_t n)
{
size_t size = sizeof(float)*n;
cudaError_t status = cudaMemcpyAsync(x, x_gpu, size, cudaMemcpyDefault, get_cuda_stream());
check_error(status);
//cudaStreamSynchronize(get_cuda_stream());
}
int get_number_of_blocks(int array_size, int block_size)
{
return array_size / block_size + ((array_size % block_size > 0) ? 1 : 0);
}
int get_gpu_compute_capability(int i, char *device_name)
{
typedef struct cudaDeviceProp cudaDeviceProp;
cudaDeviceProp prop;
cudaError_t status = cudaGetDeviceProperties(&prop, i);
CHECK_CUDA(status);
if (device_name) strcpy(device_name, prop.name);
int cc = prop.major * 100 + prop.minor * 10; // __CUDA_ARCH__ format
return cc;
}
void show_cuda_cudnn_info()
{
int cuda_version = 0, cuda_driver_version = 0, device_count = 0;
CHECK_CUDA(cudaRuntimeGetVersion(&cuda_version));
CHECK_CUDA(cudaDriverGetVersion(&cuda_driver_version));
fprintf(stderr, " CUDA-version: %d (%d)", cuda_version, cuda_driver_version);
if(cuda_version > cuda_driver_version) fprintf(stderr, "\n Warning: CUDA-version is higher than Driver-version! \n");
#ifdef CUDNN
fprintf(stderr, ", cuDNN: %d.%d.%d", CUDNN_MAJOR, CUDNN_MINOR, CUDNN_PATCHLEVEL);
#endif // CUDNN
#ifdef CUDNN_HALF
fprintf(stderr, ", CUDNN_HALF=1");
#endif // CUDNN_HALF
CHECK_CUDA(cudaGetDeviceCount(&device_count));
fprintf(stderr, ", GPU count: %d ", device_count);
fprintf(stderr, " \n");
}
#else // GPU
#include "darknet.h"
void cuda_set_device(int n) {}
#endif // GPU