You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
3.8 KiB

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