darknet  v3
cuda.c
Go to the documentation of this file.
1 int gpu_index = 0;
2 
3 #ifdef GPU
4 
5 #include "cuda.h"
6 #include "utils.h"
7 #include "blas.h"
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <time.h>
11 
12 void cuda_set_device(int n)
13 {
14  gpu_index = n;
15  cudaError_t status = cudaSetDevice(n);
16  check_error(status);
17 }
18 
19 int cuda_get_device()
20 {
21  int n = 0;
22  cudaError_t status = cudaGetDevice(&n);
23  check_error(status);
24  return n;
25 }
26 
27 void check_error(cudaError_t status)
28 {
29  //cudaDeviceSynchronize();
30  cudaError_t status2 = cudaGetLastError();
31  if (status != cudaSuccess)
32  {
33  const char *s = cudaGetErrorString(status);
34  char buffer[256];
35  printf("CUDA Error: %s\n", s);
36  assert(0);
37  snprintf(buffer, 256, "CUDA Error: %s", s);
38  error(buffer);
39  }
40  if (status2 != cudaSuccess)
41  {
42  const char *s = cudaGetErrorString(status);
43  char buffer[256];
44  printf("CUDA Error Prev: %s\n", s);
45  assert(0);
46  snprintf(buffer, 256, "CUDA Error Prev: %s", s);
47  error(buffer);
48  }
49 }
50 
51 dim3 cuda_gridsize(size_t n){
52  size_t k = (n-1) / BLOCK + 1;
53  size_t x = k;
54  size_t y = 1;
55  if(x > 65535){
56  x = ceil(sqrt(k));
57  y = (n-1)/(x*BLOCK) + 1;
58  }
59  dim3 d = {x, y, 1};
60  //printf("%ld %ld %ld %ld\n", n, x, y, x*y*BLOCK);
61  return d;
62 }
63 
64 #ifdef CUDNN
65 cudnnHandle_t cudnn_handle()
66 {
67  static int init[16] = {0};
68  static cudnnHandle_t handle[16];
69  int i = cuda_get_device();
70  if(!init[i]) {
71  cudnnCreate(&handle[i]);
72  init[i] = 1;
73  }
74  return handle[i];
75 }
76 #endif
77 
78 cublasHandle_t blas_handle()
79 {
80  static int init[16] = {0};
81  static cublasHandle_t handle[16];
82  int i = cuda_get_device();
83  if(!init[i]) {
84  cublasCreate(&handle[i]);
85  init[i] = 1;
86  }
87  return handle[i];
88 }
89 
90 float *cuda_make_array(float *x, size_t n)
91 {
92  float *x_gpu;
93  size_t size = sizeof(float)*n;
94  cudaError_t status = cudaMalloc((void **)&x_gpu, size);
95  check_error(status);
96  if(x){
97  status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
98  check_error(status);
99  } else {
100  fill_gpu(n, 0, x_gpu, 1);
101  }
102  if(!x_gpu) error("Cuda malloc failed\n");
103  return x_gpu;
104 }
105 
106 void cuda_random(float *x_gpu, size_t n)
107 {
108  static curandGenerator_t gen[16];
109  static int init[16] = {0};
110  int i = cuda_get_device();
111  if(!init[i]){
112  curandCreateGenerator(&gen[i], CURAND_RNG_PSEUDO_DEFAULT);
113  curandSetPseudoRandomGeneratorSeed(gen[i], time(0));
114  init[i] = 1;
115  }
116  curandGenerateUniform(gen[i], x_gpu, n);
117  check_error(cudaPeekAtLastError());
118 }
119 
120 float cuda_compare(float *x_gpu, float *x, size_t n, char *s)
121 {
122  float *tmp = calloc(n, sizeof(float));
123  cuda_pull_array(x_gpu, tmp, n);
124  //int i;
125  //for(i = 0; i < n; ++i) printf("%f %f\n", tmp[i], x[i]);
126  axpy_cpu(n, -1, x, 1, tmp, 1);
127  float err = dot_cpu(n, tmp, 1, tmp, 1);
128  printf("Error %s: %f\n", s, sqrt(err/n));
129  free(tmp);
130  return err;
131 }
132 
133 int *cuda_make_int_array(int *x, size_t n)
134 {
135  int *x_gpu;
136  size_t size = sizeof(int)*n;
137  cudaError_t status = cudaMalloc((void **)&x_gpu, size);
138  check_error(status);
139  if(x){
140  status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
141  check_error(status);
142  }
143  if(!x_gpu) error("Cuda malloc failed\n");
144  return x_gpu;
145 }
146 
147 void cuda_free(float *x_gpu)
148 {
149  cudaError_t status = cudaFree(x_gpu);
150  check_error(status);
151 }
152 
153 void cuda_push_array(float *x_gpu, float *x, size_t n)
154 {
155  size_t size = sizeof(float)*n;
156  cudaError_t status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
157  check_error(status);
158 }
159 
160 void cuda_pull_array(float *x_gpu, float *x, size_t n)
161 {
162  size_t size = sizeof(float)*n;
163  cudaError_t status = cudaMemcpy(x, x_gpu, size, cudaMemcpyDeviceToHost);
164  check_error(status);
165 }
166 
167 float cuda_mag_array(float *x_gpu, size_t n)
168 {
169  float *temp = calloc(n, sizeof(float));
170  cuda_pull_array(x_gpu, temp, n);
171  float m = mag_array(temp, n);
172  free(temp);
173  return m;
174 }
175 #else
176 void cuda_set_device(int n){}
177 
178 #endif
void fill_gpu(int N, float ALPHA, float *X, int INCX)
void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
Definition: blas.c:178
void cuda_set_device(int n)
Definition: cuda.c:176
float dot_cpu(int N, float *X, int INCX, float *Y, int INCY)
Definition: blas.c:297
int gpu_index
Definition: cuda.c:1
float mag_array(float *a, int n)
Definition: utils.c:574
void error(const char *s)
Definition: utils.c:253