cuda.c
Go to the documentation of this file.
00001 int gpu_index = 0;
00002 
00003 #ifdef GPU
00004 
00005 #include "cuda.h"
00006 #include "utils.h"
00007 #include "blas.h"
00008 #include "assert.h"
00009 #include <stdlib.h>
00010 #include <time.h>
00011 
00012 void cuda_set_device(int n)
00013 {
00014     gpu_index = n;
00015     cudaError_t status = cudaSetDevice(n);
00016     check_error(status);
00017 }
00018 
00019 int cuda_get_device()
00020 {
00021     int n = 0;
00022     cudaError_t status = cudaGetDevice(&n);
00023     check_error(status);
00024     return n;
00025 }
00026 
00027 void check_error(cudaError_t status)
00028 {
00029     //cudaDeviceSynchronize();
00030     cudaError_t status2 = cudaGetLastError();
00031     if (status != cudaSuccess)
00032     {   
00033         const char *s = cudaGetErrorString(status);
00034         char buffer[256];
00035         printf("CUDA Error: %s\n", s);
00036         assert(0);
00037         snprintf(buffer, 256, "CUDA Error: %s", s);
00038         error(buffer);
00039     } 
00040     if (status2 != cudaSuccess)
00041     {   
00042         const char *s = cudaGetErrorString(status);
00043         char buffer[256];
00044         printf("CUDA Error Prev: %s\n", s);
00045         assert(0);
00046         snprintf(buffer, 256, "CUDA Error Prev: %s", s);
00047         error(buffer);
00048     } 
00049 }
00050 
00051 dim3 cuda_gridsize(size_t n){
00052     size_t k = (n-1) / BLOCK + 1;
00053     size_t x = k;
00054     size_t y = 1;
00055     if(x > 65535){
00056         x = ceil(sqrt(k));
00057         y = (n-1)/(x*BLOCK) + 1;
00058     }
00059     dim3 d = {x, y, 1};
00060     //printf("%ld %ld %ld %ld\n", n, x, y, x*y*BLOCK);
00061     return d;
00062 }
00063 
00064 #ifdef CUDNN
00065 cudnnHandle_t cudnn_handle()
00066 {
00067     static int init[16] = {0};
00068     static cudnnHandle_t handle[16];
00069     int i = cuda_get_device();
00070     if(!init[i]) {
00071         cudnnCreate(&handle[i]);
00072         init[i] = 1;
00073     }
00074     return handle[i];
00075 }
00076 #endif
00077 
00078 cublasHandle_t blas_handle()
00079 {
00080     static int init[16] = {0};
00081     static cublasHandle_t handle[16];
00082     int i = cuda_get_device();
00083     if(!init[i]) {
00084         cublasCreate(&handle[i]);
00085         init[i] = 1;
00086     }
00087     return handle[i];
00088 }
00089 
00090 float *cuda_make_array(float *x, size_t n)
00091 {
00092     float *x_gpu;
00093     size_t size = sizeof(float)*n;
00094     cudaError_t status = cudaMalloc((void **)&x_gpu, size);
00095     check_error(status);
00096     if(x){
00097         status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
00098         check_error(status);
00099     }
00100     if(!x_gpu) error("Cuda malloc failed\n");
00101     return x_gpu;
00102 }
00103 
00104 void cuda_random(float *x_gpu, size_t n)
00105 {
00106     static curandGenerator_t gen[16];
00107     static int init[16] = {0};
00108     int i = cuda_get_device();
00109     if(!init[i]){
00110         curandCreateGenerator(&gen[i], CURAND_RNG_PSEUDO_DEFAULT);
00111         curandSetPseudoRandomGeneratorSeed(gen[i], time(0));
00112         init[i] = 1;
00113     }
00114     curandGenerateUniform(gen[i], x_gpu, n);
00115     check_error(cudaPeekAtLastError());
00116 }
00117 
00118 float cuda_compare(float *x_gpu, float *x, size_t n, char *s)
00119 {
00120     float *tmp = calloc(n, sizeof(float));
00121     cuda_pull_array(x_gpu, tmp, n);
00122     //int i;
00123     //for(i = 0; i < n; ++i) printf("%f %f\n", tmp[i], x[i]);
00124     axpy_cpu(n, -1, x, 1, tmp, 1);
00125     float err = dot_cpu(n, tmp, 1, tmp, 1);
00126     printf("Error %s: %f\n", s, sqrt(err/n));
00127     free(tmp);
00128     return err;
00129 }
00130 
00131 int *cuda_make_int_array(size_t n)
00132 {
00133     int *x_gpu;
00134     size_t size = sizeof(int)*n;
00135     cudaError_t status = cudaMalloc((void **)&x_gpu, size);
00136     check_error(status);
00137     return x_gpu;
00138 }
00139 
00140 void cuda_free(float *x_gpu)
00141 {
00142     cudaError_t status = cudaFree(x_gpu);
00143     check_error(status);
00144 }
00145 
00146 void cuda_push_array(float *x_gpu, float *x, size_t n)
00147 {
00148     size_t size = sizeof(float)*n;
00149     cudaError_t status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
00150     check_error(status);
00151 }
00152 
00153 void cuda_pull_array(float *x_gpu, float *x, size_t n)
00154 {
00155     size_t size = sizeof(float)*n;
00156     cudaError_t status = cudaMemcpy(x, x_gpu, size, cudaMemcpyDeviceToHost);
00157     check_error(status);
00158 }
00159 
00160 #endif


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:29