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.

540 lines
14 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. int gpu_index = 0;
  2. #ifdef GPU
  3. #include "cuda.h"
  4. #include "../base/basicUtils.h"
  5. #include "../base/basicCortex.h"
  6. #include "../base/basicLayer.h"
  7. #include "../cpu/blas.h"
  8. #define LOG( X ) m_pLog->Log( "CUDA", this, X )
  9. #define LOG_ERROR( X ) m_pLog->Log( "CUDA", this, _LOG_ERROR, X )
  10. gpuCUDA* gpuCUDA::_instance = NULL;
  11. bool gpuCUDA::m_aCublasHandleBusy[CUDA_KERNEL_NODES] = { false };
  12. cublasHandle_t gpuCUDA::m_aCublasHandle[CUDA_KERNEL_NODES] = { NULL };
  13. bool gpuCUDA::m_aCuRandHandleBusy[CUDA_KERNEL_NODES] = { false };
  14. curandGenerator_t gpuCUDA::m_aCuRandHandle[CUDA_KERNEL_NODES] = { NULL };
  15. /**
  16. * Returns the single instance of the object.
  17. */
  18. gpuCUDA *gpuCUDA::getInstance(void) {
  19. // check if the instance has been created yet
  20. if( NULL == _instance ){
  21. // if not, then create it
  22. _instance = new gpuCUDA();
  23. }
  24. // return the single instance
  25. return _instance;
  26. };
  27. gpuCUDA::gpuCUDA(){
  28. m_pLog = basicLogCollection::getInstance();
  29. m_nDeviceIndex = 0;
  30. m_sStatus = cudaGetDeviceCount(&m_nDeviceCount);
  31. if( m_sStatus == cudaSuccess ){
  32. cudaDeviceProp prop;// = NULL;
  33. cudaError_t ret = cudaGetDeviceProperties( &prop, 0 );
  34. if( ret == cudaSuccess ){
  35. char tBuff[1256];
  36. float mSize;
  37. mSize = ((float)prop.totalGlobalMem) / 1073741824.f;
  38. sprintf(tBuff, "%s [%0.1fGb] C:%dT:%dK:%d", prop.name, mSize, prop.multiProcessorCount, prop.maxThreadsPerMultiProcessor,prop.concurrentKernels );
  39. LOG(tBuff);
  40. };
  41. #ifdef _DEBUG
  42. char tBuff[256];
  43. sprintf( tBuff, "Detected %d CUDA GPU Core(s)", m_nDeviceCount );
  44. LOG( tBuff );
  45. #endif
  46. } else
  47. //if( m_sStatus != cudaSuccess )
  48. {
  49. m_nDeviceCount = -1;
  50. LOG_ERROR( "GPU Device Not FOUND!" );
  51. };
  52. };
  53. gpuCUDA::~gpuCUDA(){
  54. };
  55. bool gpuCUDA::setDevice(uint32_t n){
  56. #ifdef _DEBUG
  57. char buffer[256];
  58. snprintf(buffer, 256, "Set CUDA device[%d] ... ", n);
  59. LOG(buffer);
  60. #endif
  61. m_nDeviceIndex = n;
  62. m_sStatus = cudaSetDevice(m_nDeviceIndex);
  63. #ifdef _DEBUG
  64. if (m_sStatus == cudaSuccess){
  65. snprintf(buffer, 256, "OK");
  66. LOG(buffer);
  67. };
  68. #endif
  69. return checkError();
  70. };
  71. int gpuCUDA::getDevice(void){
  72. int n = 0;
  73. LOG("Get CUDA device ... ");
  74. m_sStatus = cudaGetDevice(&n);
  75. if( m_sStatus == cudaSuccess ){
  76. char tBuff[128];
  77. sprintf( tBuff,"Device[%d] - OK", n);
  78. LOG(tBuff);
  79. };
  80. if( checkError() ) return n;
  81. return -1;
  82. };
  83. bool gpuCUDA::checkError(void){
  84. cudaDeviceSynchronize();
  85. cudaError_t tStatus = cudaGetLastError();
  86. if (m_sStatus != cudaSuccess){
  87. #ifdef _DEBUG
  88. const char *s = cudaGetErrorString(m_sStatus);
  89. char buffer[256];
  90. snprintf(buffer, 256, "CUDA Error: %s", s);
  91. LOG_ERROR(buffer);
  92. //assert(0);
  93. #endif
  94. return false;
  95. };
  96. if (tStatus != cudaSuccess){
  97. #ifdef _DEBUG
  98. const char *s = cudaGetErrorString(m_sStatus);
  99. char buffer[256];
  100. snprintf(buffer, 256, "CUDA Error Prev: %s", s);
  101. LOG_ERROR(buffer);
  102. //assert(0);
  103. #endif
  104. return false;
  105. };
  106. return true;
  107. };
  108. dim3 gpuCUDA::gridSize( unsigned int n ){
  109. unsigned int k = (n - 1) / BLOCK + 1;
  110. unsigned int x = k;
  111. unsigned int y = 1;
  112. if (x > 65535){
  113. x = (unsigned int)ceil(sqrt((double)k));
  114. y = (unsigned int)(n - 1) / (x*BLOCK) + 1;
  115. }
  116. dim3 d;// = { x, y, 1 };
  117. d.x = x; d.y = y, d.z = 1;
  118. printf("%ld %ld %ld %ld\n", n, x, y, x*y*BLOCK);
  119. return d;
  120. };
  121. #ifdef CUDNN
  122. cudnnHandle_t gpuCUDA::cudnn_handle(){
  123. static int init[16] = { 0 };
  124. static cudnnHandle_t handle[16];
  125. int i = cuda_get_device();
  126. if (!init[i]) {
  127. cudnnCreate(&handle[i]);
  128. init[i] = 1;
  129. }
  130. return handle[i];
  131. };
  132. #endif
  133. cublasHandle_t gpuCUDA::getBlasHandle(void){
  134. int i = getDevice();
  135. if( i < 0 ) return NULL;
  136. #ifdef _DEBUG
  137. char buffer[256];
  138. snprintf(buffer, 256, "CUDA device ID: %d", i);
  139. LOG(buffer);
  140. #endif
  141. if( !m_aCublasHandleBusy[i] ){
  142. #ifdef _DEBUG
  143. LOG("Create new BLAS handle ... ");
  144. #endif
  145. cublasCreate(&m_aCublasHandle[i]);
  146. if( m_aCublasHandle[i] ){
  147. m_aCublasHandleBusy[i] = true;
  148. #ifdef _DEBUG
  149. snprintf(buffer, 256, "blas[%p] - OK", m_aCublasHandle[i]);
  150. LOG(buffer);
  151. #endif
  152. } else {
  153. LOG_ERROR("Create new BLAS handle FAIL!");
  154. return NULL;
  155. };
  156. };
  157. return m_aCublasHandle[i];
  158. };
  159. float *gpuCUDA::makeFloatArray( size_t n, float *hostMem ){
  160. float *x_gpu = NULL;
  161. size_t size = sizeof(float) * n;
  162. m_sStatus = cudaMalloc((void **)&x_gpu, size);
  163. if( !checkError() || x_gpu == NULL ){
  164. LOG_ERROR("cudaMalloc FAILED!");
  165. return NULL;
  166. };
  167. if (hostMem){
  168. m_sStatus = cudaMemcpy(x_gpu, hostMem, size, cudaMemcpyHostToDevice);
  169. if( !checkError() ){
  170. LOG_ERROR("cudaMemcpy FAILED!");
  171. cudaFree( x_gpu );
  172. return NULL;
  173. };
  174. };
  175. return x_gpu;
  176. };
  177. bool gpuCUDA::makeRandom(float *gpuMem, size_t n){
  178. if (gpuMem == NULL) return false;
  179. int i = getDevice();
  180. if (i < 0) return false;
  181. if (!m_aCuRandHandleBusy[i]){
  182. curandCreateGenerator(&m_aCuRandHandle[i], CURAND_RNG_PSEUDO_DEFAULT);
  183. curandSetPseudoRandomGeneratorSeed(m_aCuRandHandle[i], time(0));
  184. m_aCuRandHandleBusy[i] = true;
  185. }
  186. curandGenerateUniform(m_aCuRandHandle[i], gpuMem, n );
  187. m_sStatus = cudaPeekAtLastError();
  188. return checkError();
  189. };
  190. float gpuCUDA::compare(float *gpuMem, float *x, size_t n, char *s){
  191. float *tmp = (float*)calloc(n, sizeof(float));
  192. pullFloatArray( gpuMem, tmp, n );
  193. size_t i;
  194. for( i = 0; i < n; ++i ) printf("%f %f\n", tmp[i], x[i]);
  195. axpy_cpu((int)n, -1, x, 1, tmp, 1);
  196. float err = dot_cpu((int)n, tmp, 1, tmp, 1);
  197. printf("Error %s: %f\n", s, sqrt(err / n));
  198. free(tmp);
  199. return err;
  200. };
  201. uint32_t *gpuCUDA::makeIntArray(uint32_t n, uint32_t *hostMem){
  202. uint32_t *x_gpu = NULL;
  203. size_t size = sizeof(int)* n;
  204. m_sStatus = cudaMalloc((void **)&x_gpu, size);
  205. if (!checkError() || x_gpu == NULL){
  206. LOG_ERROR("cudaMalloc FAILED!");
  207. return NULL;
  208. };
  209. if (hostMem){
  210. m_sStatus = cudaMemcpy(x_gpu, hostMem, size, cudaMemcpyHostToDevice);
  211. if (!checkError()){
  212. LOG_ERROR("cudaMemcpy FAILED!");
  213. cudaFree(x_gpu);
  214. return NULL;
  215. };
  216. };
  217. return x_gpu;
  218. };
  219. bool gpuCUDA::freeMem(void *gpuMem){
  220. m_sStatus = cudaFree(gpuMem);
  221. return checkError();
  222. };
  223. bool gpuCUDA::pushFloatArray(float *gpuMem, float *hostMem, size_t n){
  224. size_t size = (unsigned int)sizeof(float)*n;
  225. m_sStatus = cudaMemcpy(gpuMem, hostMem, size, cudaMemcpyHostToDevice);
  226. return checkError();
  227. };
  228. bool gpuCUDA::pullFloatArray(float *gpuMem, float *hostMem, size_t n){
  229. size_t size = (unsigned int) sizeof(float)*n;
  230. m_sStatus = cudaMemcpy(hostMem, gpuMem, size, cudaMemcpyDeviceToHost);
  231. return checkError();
  232. };
  233. bool gpuCUDA::pushIntArray(uint32_t *gpuMem, uint32_t *hostMem, size_t n){
  234. size_t size = (unsigned int)sizeof(int)*n;
  235. m_sStatus = cudaMemcpy(gpuMem, hostMem, size, cudaMemcpyHostToDevice);
  236. return checkError();
  237. };
  238. bool gpuCUDA::pullIntArray(uint32_t *gpuMem, uint32_t *hostMem, size_t n){
  239. size_t size = (unsigned int) sizeof(int)*n;
  240. m_sStatus = cudaMemcpy(hostMem, gpuMem, size, cudaMemcpyDeviceToHost);
  241. return checkError();
  242. };
  243. bool gpuCUDA::gemm_ongpu(uint32_t TA, uint32_t TB, uint32_t M, uint32_t N, uint32_t K, float ALPHA,
  244. float *A_gpu, uint32_t lda,
  245. float *B_gpu, uint32_t ldb,
  246. float BETA,
  247. float *C_gpu, uint32_t ldc){
  248. cublasHandle_t handle = getBlasHandle();// blas_handle();
  249. if( handle != NULL ){
  250. char tBuffer[128];
  251. sprintf( tBuffer, "GEMM on BLAS [%p]", handle );
  252. LOG(tBuffer);
  253. cublasStatus_t bla_st;
  254. bla_st = cublasSgemm( handle, (TB ? CUBLAS_OP_T : CUBLAS_OP_N), (TA ? CUBLAS_OP_T : CUBLAS_OP_N), N, M, K, &ALPHA, B_gpu, ldb, A_gpu, lda, &BETA, C_gpu, ldc);
  255. return checkError();
  256. } else {
  257. LOG_ERROR("Blas handle error");
  258. };
  259. return false;
  260. };
  261. void gpuCUDA::gemm(uint32_t TA, uint32_t TB, uint32_t M, uint32_t N, uint32_t K, float ALPHA,
  262. float *A, uint32_t lda,
  263. float *B, uint32_t ldb,
  264. float BETA,
  265. float *C, uint32_t ldc){
  266. float *A_gpu = makeFloatArray((TA ? lda*K : lda*M), A);
  267. float *B_gpu = makeFloatArray((TB ? ldb*N : ldb*K), B);
  268. float *C_gpu = makeFloatArray(ldc*M, C);
  269. gemm_ongpu(TA, TB, M, N, K, ALPHA, A_gpu, lda, B_gpu, ldb, BETA, C_gpu, ldc);
  270. pullFloatArray(C_gpu, C, ldc*M);
  271. freeMem(A_gpu);
  272. freeMem(B_gpu);
  273. freeMem(C_gpu);
  274. };
  275. bool gpuCUDA::makeTestAccuracy(uint32_t TA, uint32_t TB, uint32_t m, uint32_t k, uint32_t n){
  276. return false;
  277. };
  278. bool gpuCUDA::makeTestBlas(void){
  279. return false;
  280. };
  281. bool gpuCUDA::calcScaleBias(float *output, float *biases, uint32_t batch, uint32_t n, uint32_t size){
  282. return false;
  283. };
  284. bool gpuCUDA::calcTimeRandomMatrix(uint32_t TA, uint32_t TB, uint32_t m, uint32_t k, uint32_t n){
  285. return false;
  286. };
  287. bool gpuCUDA::calcTime(uint32_t TA, uint32_t TB, uint32_t m, uint32_t k, uint32_t n){
  288. return false;
  289. };
  290. /*
  291. Defined in CU source
  292. float gpuCUDA::trainNetworkDatum(basicCortex *net, float *X, float *Y){
  293. cortexSTATE tState;
  294. memset(&tState, 0, sizeof(cortexSTATE));
  295. {
  296. uint64_t tVal = net->getSeen();
  297. tVal += net->getBatch();
  298. net->setSeen(tVal);
  299. }
  300. tState.index = 0;
  301. tState.net = net;
  302. uint16_t x_size = net->getInputSize()*net->getBatch();
  303. uint16_t y_size;
  304. if( net->getOutputLayer()->getTruthSize() > 0 ){ //ToDo: Check if the Output Layer is the Last one in the Network
  305. y_size = net->getOutputLayer()->getTruthSize() * net->getBatch();
  306. } else {
  307. y_size = net->getOtputSize()*net->getBatch();
  308. };
  309. float *tPtr;
  310. tPtr = net->getGpuInput();
  311. if ( tPtr == NULL ){
  312. net->setGpuInput(makeFloatArray(x_size, X));//cuda_make_array(X, x_size);
  313. net->setGpuTruth(makeFloatArray(y_size, Y));//cuda_make_array( Y, y_size);
  314. } else {
  315. pushFloatArray(net->getGpuInput(), X, x_size);//cuda_push_array(*net.input_gpu, X, x_size);
  316. pushFloatArray(net->getGpuTruth(), Y, y_size);//cuda_push_array(*net.truth_gpu, Y, y_size);
  317. };
  318. tState.input = net->getGpuInput();
  319. tState.delta = NULL;
  320. tState.truth = net->getGpuTruth();
  321. tState.train = true;
  322. forwardNetwork( net, tState);
  323. backwardNetwork( net, tState);
  324. float error = net->getCost();
  325. if (((net->getSeen()/net->getBatch()) % net->getSubdivisions()) == 0) updateNetwork(net);
  326. return error;
  327. }; */
  328. /********************************
  329. Original DN module
  330. *******************************/
  331. /*
  332. void cuda_set_device(int n){
  333. gpu_index = n;
  334. printf("Set CUDA device[%d] ... ", n);
  335. cudaError_t status = cudaSetDevice(n);
  336. if (status == 0) printf("OK\n");
  337. check_error(status);
  338. }
  339. int cuda_get_device(){
  340. int n = 0;
  341. printf("Ged CUDA device ... ");
  342. cudaError_t status = cudaGetDevice(&n);
  343. if (status == 0)printf("device[%d] - OK\n", n );
  344. check_error(status);
  345. return n;
  346. }
  347. void check_error(cudaError_t status){
  348. cudaDeviceSynchronize();
  349. cudaError_t status2 = cudaGetLastError();
  350. if (status != cudaSuccess)
  351. {
  352. const char *s = cudaGetErrorString(status);
  353. char buffer[256];
  354. printf("CUDA Error: %s\n", s);
  355. assert(0);
  356. snprintf(buffer, 256, "CUDA Error: %s", s);
  357. error(buffer);
  358. }
  359. if (status2 != cudaSuccess)
  360. {
  361. const char *s = cudaGetErrorString(status);
  362. char buffer[256];
  363. printf("CUDA Error Prev: %s\n", s);
  364. assert(0);
  365. snprintf(buffer, 256, "CUDA Error Prev: %s", s);
  366. error(buffer);
  367. }
  368. }
  369. dim3 cuda_gridsize(unsigned int n){
  370. unsigned int k = (n-1) / BLOCK + 1;
  371. unsigned int x = k;
  372. unsigned int y = 1;
  373. if(x > 65535){
  374. x = (unsigned int)ceil(sqrt((double)k));
  375. y = (unsigned int)(n - 1) / (x*BLOCK) + 1;
  376. }
  377. dim3 d = {x, y, 1};
  378. printf("%ld %ld %ld %ld\n", n, x, y, x*y*BLOCK);
  379. return d;
  380. }
  381. #ifdef CUDNN
  382. cudnnHandle_t cudnn_handle()
  383. {
  384. static int init[16] = {0};
  385. static cudnnHandle_t handle[16];
  386. int i = cuda_get_device();
  387. if(!init[i]) {
  388. cudnnCreate(&handle[i]);
  389. init[i] = 1;
  390. }
  391. return handle[i];
  392. }
  393. #endif
  394. cublasHandle_t blas_handle()
  395. {
  396. static int init[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  397. static cublasHandle_t handle[16] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  398. int i = cuda_get_device();
  399. printf("CUDA device ID: %d\n",i );
  400. if(!init[i]) {
  401. printf("Create new BLAS handle ... ");
  402. cublasCreate(&handle[i]);
  403. if(handle[i]){
  404. init[i] = 1;
  405. printf("blas[%p] - OK\n", handle[i] );
  406. } else {
  407. printf("FAIL\n");
  408. };
  409. };
  410. return handle[i];
  411. };
  412. float *cuda_make_array(float *x, unsigned int n)
  413. {
  414. float *x_gpu;
  415. unsigned int size = (unsigned int)sizeof(float)*n;
  416. cudaError_t status = cudaMalloc((void **)&x_gpu, size);
  417. check_error(status);
  418. if(x){
  419. status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
  420. check_error(status);
  421. }
  422. if(!x_gpu) error("Cuda malloc failed\n");
  423. return x_gpu;
  424. }
  425. void cuda_random(float *x_gpu, unsigned int n)
  426. {
  427. static curandGenerator_t gen[16];
  428. static int init[16] = {0};
  429. int i = cuda_get_device();
  430. if(!init[i]){
  431. curandCreateGenerator(&gen[i], CURAND_RNG_PSEUDO_DEFAULT);
  432. curandSetPseudoRandomGeneratorSeed(gen[i], time(0));
  433. init[i] = 1;
  434. }
  435. curandGenerateUniform(gen[i], x_gpu, n);
  436. check_error(cudaPeekAtLastError());
  437. }
  438. float cuda_compare(float *x_gpu, float *x, unsigned int n, char *s)
  439. {
  440. float *tmp = (float*) calloc(n, sizeof(float));
  441. cuda_pull_array(x_gpu, tmp, n);
  442. unsigned int i;
  443. for(i = 0; i < n; ++i) printf("%f %f\n", tmp[i], x[i]);
  444. axpy_cpu(n, -1, x, 1, tmp, 1);
  445. float err = dot_cpu(n, tmp, 1, tmp, 1);
  446. printf("Error %s: %f\n", s, sqrt(err/n));
  447. free(tmp);
  448. return err;
  449. }
  450. int *cuda_make_int_array( unsigned int n)
  451. {
  452. int *x_gpu;
  453. size_t size = sizeof(int)*n;
  454. cudaError_t status = cudaMalloc((void **)&x_gpu, size);
  455. check_error(status);
  456. return x_gpu;
  457. }
  458. void cuda_free(float *x_gpu)
  459. {
  460. cudaError_t status = cudaFree(x_gpu);
  461. check_error(status);
  462. }
  463. void cuda_push_array(float *x_gpu, float *x, unsigned int n)
  464. {
  465. unsigned int size = (unsigned int)sizeof(float)*n;
  466. cudaError_t status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
  467. check_error(status);
  468. }
  469. void cuda_pull_array(float *x_gpu, float *x, unsigned int n)
  470. {
  471. unsigned int size = (unsigned int) sizeof(float)*n;
  472. cudaError_t status = cudaMemcpy(x, x_gpu, size, cudaMemcpyDeviceToHost);
  473. check_error(status);
  474. }
  475. */
  476. #undef LOG
  477. #undef LOG_ERROR
  478. #endif