xgemm.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010 Centre National de la Recherche Scientifique
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <string.h>
  19. #include <math.h>
  20. #include <sys/types.h>
  21. #include <sys/time.h>
  22. #include <starpu.h>
  23. #include <common/blas.h>
  24. #ifdef STARPU_USE_CUDA
  25. #include <cuda.h>
  26. #include <cublas.h>
  27. #endif
  28. static unsigned niter = 100;
  29. static unsigned nslicesx = 4;
  30. static unsigned nslicesy = 4;
  31. static unsigned xdim = 256;
  32. static unsigned ydim = 256;
  33. static unsigned zdim = 64;
  34. static unsigned check = 0;
  35. static TYPE *A, *B, *C;
  36. static starpu_data_handle A_handle, B_handle, C_handle;
  37. static void parse_args(int argc, char **argv)
  38. {
  39. int i;
  40. for (i = 1; i < argc; i++) {
  41. if (strcmp(argv[i], "-nblocks") == 0) {
  42. char *argptr;
  43. nslicesx = strtol(argv[++i], &argptr, 10);
  44. nslicesy = nslicesx;
  45. }
  46. if (strcmp(argv[i], "-nblocksx") == 0) {
  47. char *argptr;
  48. nslicesx = strtol(argv[++i], &argptr, 10);
  49. }
  50. if (strcmp(argv[i], "-nblocksy") == 0) {
  51. char *argptr;
  52. nslicesy = strtol(argv[++i], &argptr, 10);
  53. }
  54. if (strcmp(argv[i], "-x") == 0) {
  55. char *argptr;
  56. xdim = strtol(argv[++i], &argptr, 10);
  57. }
  58. if (strcmp(argv[i], "-y") == 0) {
  59. char *argptr;
  60. ydim = strtol(argv[++i], &argptr, 10);
  61. }
  62. if (strcmp(argv[i], "-z") == 0) {
  63. char *argptr;
  64. zdim = strtol(argv[++i], &argptr, 10);
  65. }
  66. if (strcmp(argv[i], "-iter") == 0) {
  67. char *argptr;
  68. niter = strtol(argv[++i], &argptr, 10);
  69. }
  70. if (strcmp(argv[i], "-check") == 0) {
  71. check = 1;
  72. }
  73. }
  74. }
  75. static void check_output(void)
  76. {
  77. /* compute C = C - AB */
  78. CPU_GEMM("N", "N", ydim, xdim, zdim, (TYPE)-1.0f, A, ydim, B, zdim, (TYPE)1.0f, C, ydim);
  79. /* make sure C = 0 */
  80. TYPE err;
  81. err = CPU_ASUM(xdim*ydim, C, 1);
  82. if (err < xdim*ydim*0.001) {
  83. fprintf(stderr, "Results are OK\n");
  84. }
  85. else {
  86. int max;
  87. max = CPU_IAMAX(xdim*ydim, C, 1);
  88. fprintf(stderr, "There were errors ... err = %f\n", err);
  89. fprintf(stderr, "Max error : %e\n", C[max]);
  90. }
  91. }
  92. static void init_problem_data(void)
  93. {
  94. unsigned i,j;
  95. starpu_data_malloc_pinned_if_possible((void **)&A, zdim*ydim*sizeof(TYPE));
  96. starpu_data_malloc_pinned_if_possible((void **)&B, xdim*zdim*sizeof(TYPE));
  97. starpu_data_malloc_pinned_if_possible((void **)&C, xdim*ydim*sizeof(TYPE));
  98. /* fill the A and B matrices */
  99. for (j=0; j < ydim; j++) {
  100. for (i=0; i < zdim; i++) {
  101. A[j+i*ydim] = (TYPE)(starpu_drand48());
  102. }
  103. }
  104. for (j=0; j < zdim; j++) {
  105. for (i=0; i < xdim; i++) {
  106. B[j+i*zdim] = (TYPE)(starpu_drand48());
  107. }
  108. }
  109. for (j=0; j < ydim; j++) {
  110. for (i=0; i < xdim; i++) {
  111. C[j+i*ydim] = (TYPE)(0);
  112. }
  113. }
  114. }
  115. static void partition_mult_data(void)
  116. {
  117. starpu_matrix_data_register(&A_handle, 0, (uintptr_t)A,
  118. ydim, ydim, zdim, sizeof(TYPE));
  119. starpu_matrix_data_register(&B_handle, 0, (uintptr_t)B,
  120. zdim, zdim, xdim, sizeof(TYPE));
  121. starpu_matrix_data_register(&C_handle, 0, (uintptr_t)C,
  122. ydim, ydim, xdim, sizeof(TYPE));
  123. starpu_data_set_wt_mask(C_handle, 1<<0);
  124. struct starpu_data_filter f;
  125. memset(&f, 0, sizeof(f));
  126. f.filter_func = starpu_vertical_block_filter_func;
  127. f.nchildren = nslicesx;
  128. struct starpu_data_filter f2;
  129. memset(&f2, 0, sizeof(f2));
  130. f2.filter_func = starpu_block_filter_func;
  131. f2.nchildren = nslicesy;
  132. starpu_data_partition(B_handle, &f);
  133. starpu_data_partition(A_handle, &f2);
  134. starpu_data_map_filters(C_handle, 2, &f, &f2);
  135. }
  136. static void mult_kernel_common(void *descr[], int type)
  137. {
  138. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  139. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  140. TYPE *subC = (TYPE *)STARPU_MATRIX_GET_PTR(descr[2]);
  141. unsigned nxC = STARPU_MATRIX_GET_NX(descr[2]);
  142. unsigned nyC = STARPU_MATRIX_GET_NY(descr[2]);
  143. unsigned nyA = STARPU_MATRIX_GET_NY(descr[0]);
  144. unsigned ldA = STARPU_MATRIX_GET_LD(descr[0]);
  145. unsigned ldB = STARPU_MATRIX_GET_LD(descr[1]);
  146. unsigned ldC = STARPU_MATRIX_GET_LD(descr[2]);
  147. if (type == STARPU_CPU) {
  148. CPU_GEMM("N", "N", nxC, nyC, nyA, (TYPE)1.0, subA, ldA, subB, ldB, (TYPE)0.0, subC, ldC);
  149. }
  150. #ifdef STARPU_USE_CUDA
  151. else {
  152. CUBLAS_GEMM('n', 'n', nxC, nyC, nyA, (TYPE)1.0, subA, ldA, subB, ldB,
  153. (TYPE)0.0, subC, ldC);
  154. cudaThreadSynchronize();
  155. }
  156. #endif
  157. }
  158. #ifdef STARPU_USE_CUDA
  159. static void cublas_mult(void *descr[], __attribute__((unused)) void *arg)
  160. {
  161. mult_kernel_common(descr, STARPU_CUDA);
  162. }
  163. #endif
  164. static void cpu_mult(void *descr[], __attribute__((unused)) void *arg)
  165. {
  166. mult_kernel_common(descr, STARPU_CPU);
  167. }
  168. static struct starpu_perfmodel_t starpu_gemm_model = {
  169. .type = STARPU_HISTORY_BASED,
  170. .symbol = STARPU_GEMM_STR(gemm)
  171. };
  172. static starpu_codelet cl = {
  173. .where = STARPU_CPU|STARPU_CUDA,
  174. .cpu_func = cpu_mult,
  175. #ifdef STARPU_USE_CUDA
  176. .cuda_func = cublas_mult,
  177. #endif
  178. .nbuffers = 3,
  179. .model = &starpu_gemm_model
  180. };
  181. int main(int argc, char **argv)
  182. {
  183. struct timeval start;
  184. struct timeval end;
  185. parse_args(argc, argv);
  186. starpu_init(NULL);
  187. starpu_helper_cublas_init();
  188. init_problem_data();
  189. partition_mult_data();
  190. gettimeofday(&start, NULL);
  191. unsigned x, y, iter;
  192. for (iter = 0; iter < niter; iter++)
  193. {
  194. for (x = 0; x < nslicesx; x++)
  195. for (y = 0; y < nslicesy; y++)
  196. {
  197. struct starpu_task *task = starpu_task_create();
  198. task->cl = &cl;
  199. task->buffers[0].handle = starpu_data_get_sub_data(A_handle, 1, y);
  200. task->buffers[0].mode = STARPU_R;
  201. task->buffers[1].handle = starpu_data_get_sub_data(B_handle, 1, x);
  202. task->buffers[1].mode = STARPU_R;
  203. task->buffers[2].handle = starpu_data_get_sub_data(C_handle, 2, x, y);
  204. task->buffers[2].mode = STARPU_RW;
  205. int ret = starpu_task_submit(task);
  206. STARPU_ASSERT(!ret);
  207. }
  208. starpu_task_wait_for_all();
  209. }
  210. gettimeofday(&end, NULL);
  211. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  212. fprintf(stderr, "Time: %2.2f ms\n", timing/1000.0);
  213. double flops = 2.0*((unsigned long)niter)*((unsigned long)xdim)
  214. *((unsigned long)ydim)*((unsigned long)zdim);
  215. fprintf(stderr, "GFlop/s: %.2f\n", flops/timing/1000.0);
  216. starpu_data_unpartition(C_handle, 0);
  217. starpu_data_unregister(C_handle);
  218. if (check)
  219. check_output();
  220. starpu_helper_cublas_shutdown();
  221. starpu_shutdown();
  222. return 0;
  223. }