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.

349 lines
8.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "gemm.h"
  2. #include "../base/basicUtils.h"
  3. #include "../cuda/cuda.h"
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <math.h>
  7. #include <math.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. void gemm_bin(int M, int N, int K, float ALPHA,
  13. char *A, int lda,
  14. float *B, int ldb,
  15. float *C, int ldc)
  16. {
  17. int i,j,k;
  18. for(i = 0; i < M; ++i){
  19. for(k = 0; k < K; ++k){
  20. char A_PART = A[i*lda+k];
  21. if(A_PART){
  22. for(j = 0; j < N; ++j){
  23. C[i*ldc+j] += B[k*ldb+j];
  24. }
  25. } else {
  26. for(j = 0; j < N; ++j){
  27. C[i*ldc+j] -= B[k*ldb+j];
  28. }
  29. }
  30. }
  31. }
  32. }
  33. float *random_matrix(int rows, int cols)
  34. {
  35. int i;
  36. float *m = (float*)calloc(rows*cols, sizeof(float));
  37. for(i = 0; i < rows*cols; ++i){
  38. m[i] = (float)rand()/RAND_MAX;
  39. }
  40. return m;
  41. }
  42. void time_random_matrix(int TA, int TB, int m, int k, int n)
  43. {
  44. float *a;
  45. if(!TA) a = random_matrix(m,k);
  46. else a = random_matrix(k,m);
  47. int lda = (!TA)?k:m;
  48. float *b;
  49. if(!TB) b = random_matrix(k,n);
  50. else b = random_matrix(n,k);
  51. int ldb = (!TB)?n:k;
  52. float *c = random_matrix(m,n);
  53. int i;
  54. clock_t start = clock(), end;
  55. for(i = 0; i<10; ++i){
  56. gemm_cpu(TA,TB,m,n,k,1,a,lda,b,ldb,1,c,n);
  57. }
  58. end = clock();
  59. printf("Matrix Multiplication %dx%d * %dx%d, TA=%d, TB=%d: %lf ms\n",m,k,k,n, TA, TB, (float)(end-start)/CLOCKS_PER_SEC);
  60. free(a);
  61. free(b);
  62. free(c);
  63. }
  64. void gemm(int TA, int TB, int M, int N, int K, float ALPHA,
  65. float *A, int lda,
  66. float *B, int ldb,
  67. float BETA,
  68. float *C, int ldc)
  69. {
  70. gemm_cpu( TA, TB, M, N, K, ALPHA,A,lda, B, ldb,BETA,C,ldc);
  71. }
  72. void gemm_nn(int M, int N, int K, float ALPHA,
  73. float *A, int lda,
  74. float *B, int ldb,
  75. float *C, int ldc)
  76. {
  77. int i,j,k;
  78. for(i = 0; i < M; ++i){
  79. for(k = 0; k < K; ++k){
  80. register float A_PART = ALPHA*A[i*lda+k];
  81. for(j = 0; j < N; ++j){
  82. C[i*ldc+j] += A_PART*B[k*ldb+j];
  83. }
  84. }
  85. }
  86. }
  87. void gemm_nt(int M, int N, int K, float ALPHA,
  88. float *A, int lda,
  89. float *B, int ldb,
  90. float *C, int ldc)
  91. {
  92. int i,j,k;
  93. for(i = 0; i < M; ++i){
  94. for(j = 0; j < N; ++j){
  95. register float sum = 0;
  96. for(k = 0; k < K; ++k){
  97. sum += ALPHA*A[i*lda+k]*B[j*ldb + k];
  98. }
  99. C[i*ldc+j] += sum;
  100. }
  101. }
  102. }
  103. void gemm_tn(int M, int N, int K, float ALPHA,
  104. float *A, int lda,
  105. float *B, int ldb,
  106. float *C, int ldc)
  107. {
  108. int i,j,k;
  109. for(i = 0; i < M; ++i){
  110. for(k = 0; k < K; ++k){
  111. register float A_PART = ALPHA*A[k*lda+i];
  112. for(j = 0; j < N; ++j){
  113. C[i*ldc+j] += A_PART*B[k*ldb+j];
  114. }
  115. }
  116. }
  117. }
  118. void gemm_tt(int M, int N, int K, float ALPHA,
  119. float *A, int lda,
  120. float *B, int ldb,
  121. float *C, int ldc)
  122. {
  123. int i,j,k;
  124. for(i = 0; i < M; ++i){
  125. for(j = 0; j < N; ++j){
  126. register float sum = 0;
  127. for(k = 0; k < K; ++k){
  128. sum += ALPHA*A[i+k*lda]*B[k+j*ldb];
  129. }
  130. C[i*ldc+j] += sum;
  131. }
  132. }
  133. }
  134. void gemm_cpu(int TA, int TB, int M, int N, int K, float ALPHA,
  135. float *A, int lda,
  136. float *B, int ldb,
  137. float BETA,
  138. float *C, int ldc)
  139. {
  140. //printf("cpu: %d %d %d %d %d %f %d %d %f %d\n",TA, TB, M, N, K, ALPHA, lda, ldb, BETA, ldc);
  141. int i, j;
  142. for(i = 0; i < M; ++i){
  143. for(j = 0; j < N; ++j){
  144. C[i*ldc + j] *= BETA;
  145. }
  146. }
  147. int t;
  148. #pragma omp parallel for
  149. for (t = 0; t < M; ++t) {
  150. if (!TA && !TB)
  151. gemm_nn(1, N, K, ALPHA, A + t*lda, lda, B, ldb, C + t*ldc, ldc);
  152. else if (TA && !TB)
  153. gemm_tn(1, N, K, ALPHA, A + t, lda, B, ldb, C + t*ldc, ldc);
  154. else if (!TA && TB)
  155. gemm_nt(1, N, K, ALPHA, A + t*lda, lda, B, ldb, C + t*ldc, ldc);
  156. else
  157. gemm_tt(1, N, K, ALPHA, A + t, lda, B, ldb, C + t*ldc, ldc);
  158. }
  159. }
  160. #ifdef GPU
  161. void gpuCUDA::gemm_ongpu(int TA, int TB, int M, int N, int K, float ALPHA,
  162. float *A_gpu, int lda,
  163. float *B_gpu, int ldb,
  164. float BETA,
  165. float *C_gpu, int ldc){
  166. cublasHandle_t handle = getBlasHandle();
  167. if (handle != NULL){
  168. printf("gemm on BLAS [%p]\n", handle );
  169. cublasStatus_t bla_st;
  170. cudaError_t da_st;
  171. bla_st = cublasSgemm( handle, (TB ? CUBLAS_OP_T : CUBLAS_OP_N),
  172. (TA ? CUBLAS_OP_T : CUBLAS_OP_N), N, M, K, &ALPHA, B_gpu, ldb, A_gpu, lda, &BETA, C_gpu, ldc);
  173. da_st = cudaGetLastError();
  174. printf("CUBLA Status: %d\n", bla_st );
  175. printf("CUDA Status: %d\n", da_st);
  176. checkError();
  177. } else {
  178. printf("blas handle error\n");
  179. };
  180. };
  181. void gpuCUDA::gemm(int TA, int TB, int M, int N, int K, float ALPHA,
  182. float *A, int lda,
  183. float *B, int ldb,
  184. float BETA,
  185. float *C, int ldc){
  186. float *A_gpu = makeFloatArray((TA ? lda*K : lda*M), A);
  187. float *B_gpu = makeFloatArray((TB ? ldb*N : ldb*K), B);
  188. float *C_gpu = makeFloatArray(ldc*M, C);
  189. gemm_ongpu(TA, TB, M, N, K, ALPHA, A_gpu, lda, B_gpu, ldb, BETA, C_gpu, ldc);
  190. pullFloatArray(C_gpu, C, ldc*M);
  191. freeMem(A_gpu);
  192. freeMem(B_gpu);
  193. freeMem(C_gpu);
  194. };
  195. void gpuCUDA::time_gpu_random_matrix(int TA, int TB, int m, int k, int n){
  196. float *a;
  197. if (!TA){
  198. a = random_matrix(m, k);
  199. }else{
  200. a = random_matrix(k, m);
  201. };
  202. int lda = (!TA)?k:m;
  203. float *b;
  204. if (!TB){
  205. b = random_matrix(k, n);
  206. }else{
  207. b = random_matrix(n, k);
  208. };
  209. int ldb = (!TB)?n:k;
  210. float *c = random_matrix(m,n);
  211. int i;
  212. clock_t start = clock(), end;
  213. for(i = 0; i<32; ++i){
  214. gemm_gpu(TA,TB,m,n,k,1,a,lda,b,ldb,1,c,n);
  215. };
  216. end = clock();
  217. printf("Matrix Multiplication %dx%d * %dx%d, TA=%d, TB=%d: %lf s\n",m,k,k,n, TA, TB, (float)(end-start)/CLOCKS_PER_SEC);
  218. free(a);
  219. free(b);
  220. free(c);
  221. };
  222. void gpuCUDA::time_ongpu(int TA, int TB, int m, int k, int n){
  223. int iter = 10;
  224. float *a = random_matrix(m,k);
  225. float *b = random_matrix(k,n);
  226. int lda = (!TA)?k:m;
  227. int ldb = (!TB)?n:k;
  228. float *c = random_matrix(m,n);
  229. float *a_cl = makeFloatArray(m*k, a);
  230. float *b_cl = makeFloatArray(k*n, b);
  231. float *c_cl = makeFloatArray(m*n, c);
  232. int i;
  233. clock_t start = clock(), end;
  234. for(i = 0; i<iter; ++i){
  235. gemm_ongpu(TA,TB,m,n,k,1,a_cl,lda,b_cl,ldb,1,c_cl,n);
  236. cudaThreadSynchronize();
  237. }
  238. double flop = ((double)m)*n*(2.*k + 2.)*iter;
  239. double gflop = flop/pow(10., 9);
  240. end = clock();
  241. double seconds = sec(end-start);
  242. printf("Matrix Multiplication %dx%d * %dx%d, TA=%d, TB=%d: %lf s, %lf GFLOPS\n",m,k,k,n, TA, TB, seconds, gflop/seconds);
  243. freeMem(a_cl);
  244. freeMem(b_cl);
  245. freeMem(c_cl);
  246. free(a);
  247. free(b);
  248. free(c);
  249. }
  250. void gpuCUDA::test_gpu_accuracy(int TA, int TB, int m, int k, int n){
  251. srand(0);
  252. float *a;
  253. if(!TA) a = random_matrix(m,k);
  254. else a = random_matrix(k,m);
  255. int lda = (!TA)?k:m;
  256. float *b;
  257. if(!TB) b = random_matrix(k,n);
  258. else b = random_matrix(n,k);
  259. int ldb = (!TB)?n:k;
  260. float *c = random_matrix(m,n);
  261. float *c_gpu = random_matrix(m,n);
  262. memset(c, 0, m*n*sizeof(float));
  263. memset(c_gpu, 0, m*n*sizeof(float));
  264. int i;
  265. //pm(m,k,b);
  266. gemm_gpu(TA,TB,m,n,k,1,a,lda,b,ldb,1,c_gpu,n);
  267. //printf("GPU\n");
  268. //pm(m, n, c_gpu);
  269. gemm_cpu(TA,TB,m,n,k,1,a,lda,b,ldb,1,c,n);
  270. //printf("\n\nCPU\n");
  271. //pm(m, n, c);
  272. double sse = 0;
  273. for(i = 0; i < m*n; ++i) {
  274. //printf("%f %f\n", c[i], c_gpu[i]);
  275. sse += pow(c[i]-c_gpu[i], 2);
  276. }
  277. printf("Matrix Multiplication %dx%d * %dx%d, TA=%d, TB=%d: %g SSE\n",m,k,k,n, TA, TB, sse/(m*n));
  278. free(a);
  279. free(b);
  280. free(c);
  281. free(c_gpu);
  282. }
  283. int gpuCUDA::test_gpu_blas(){
  284. /*
  285. test_gpu_accuracy(0,0,10,576,75);
  286. test_gpu_accuracy(0,0,17,10,10);
  287. test_gpu_accuracy(1,0,17,10,10);
  288. test_gpu_accuracy(0,1,17,10,10);
  289. test_gpu_accuracy(1,1,17,10,10);
  290. test_gpu_accuracy(0,0,1000,10,100);
  291. test_gpu_accuracy(1,0,1000,10,100);
  292. test_gpu_accuracy(0,1,1000,10,100);
  293. test_gpu_accuracy(1,1,1000,10,100);
  294. test_gpu_accuracy(0,0,10,10,10);
  295. time_ongpu(0,0,64,2916,363);
  296. time_ongpu(0,0,64,2916,363);
  297. time_ongpu(0,0,64,2916,363);
  298. time_ongpu(0,0,192,729,1600);
  299. time_ongpu(0,0,384,196,1728);
  300. time_ongpu(0,0,256,196,3456);
  301. time_ongpu(0,0,256,196,2304);
  302. time_ongpu(0,0,128,4096,12544);
  303. time_ongpu(0,0,128,4096,4096);
  304. */
  305. time_ongpu(0,0,64,75,12544);
  306. time_ongpu(0,0,64,75,12544);
  307. time_ongpu(0,0,64,75,12544);
  308. time_ongpu(0,0,64,576,12544);
  309. time_ongpu(0,0,256,2304,784);
  310. time_ongpu(1,1,2304,256,784);
  311. time_ongpu(0,0,512,4608,196);
  312. time_ongpu(1,1,4608,512,196);
  313. return 0;
  314. };
  315. #endif