xgemm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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, 2011, 2012 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 <limits.h>
  19. #include <string.h>
  20. #include <math.h>
  21. #include <sys/types.h>
  22. #include <sys/time.h>
  23. #include <starpu.h>
  24. #include <common/blas.h>
  25. #ifdef STARPU_USE_CUDA
  26. #include <cuda.h>
  27. #include <cublas.h>
  28. #include <starpu_cuda.h>
  29. #endif
  30. static unsigned niter = 100;
  31. static unsigned nslicesx = 4;
  32. static unsigned nslicesy = 4;
  33. static unsigned xdim = 256;
  34. static unsigned ydim = 256;
  35. static unsigned zdim = 64;
  36. static unsigned check = 0;
  37. static TYPE *A, *B, *C;
  38. static starpu_data_handle_t A_handle, B_handle, C_handle;
  39. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  40. static void check_output(void)
  41. {
  42. /* compute C = C - AB */
  43. CPU_GEMM("N", "N", ydim, xdim, zdim, (TYPE)-1.0f, A, ydim, B, zdim, (TYPE)1.0f, C, ydim);
  44. /* make sure C = 0 */
  45. TYPE err;
  46. err = CPU_ASUM(xdim*ydim, C, 1);
  47. if (err < xdim*ydim*0.001)
  48. {
  49. FPRINTF(stderr, "Results are OK\n");
  50. }
  51. else
  52. {
  53. int max;
  54. max = CPU_IAMAX(xdim*ydim, C, 1);
  55. FPRINTF(stderr, "There were errors ... err = %f\n", err);
  56. FPRINTF(stderr, "Max error : %e\n", C[max]);
  57. }
  58. }
  59. static void init_problem_data(void)
  60. {
  61. unsigned i,j;
  62. starpu_malloc((void **)&A, zdim*ydim*sizeof(TYPE));
  63. starpu_malloc((void **)&B, xdim*zdim*sizeof(TYPE));
  64. starpu_malloc((void **)&C, xdim*ydim*sizeof(TYPE));
  65. /* fill the A and B matrices */
  66. for (j=0; j < ydim; j++)
  67. {
  68. for (i=0; i < zdim; i++)
  69. {
  70. A[j+i*ydim] = (TYPE)(starpu_drand48());
  71. }
  72. }
  73. for (j=0; j < zdim; j++)
  74. {
  75. for (i=0; i < xdim; i++)
  76. {
  77. B[j+i*zdim] = (TYPE)(starpu_drand48());
  78. }
  79. }
  80. for (j=0; j < ydim; j++)
  81. {
  82. for (i=0; i < xdim; i++)
  83. {
  84. C[j+i*ydim] = (TYPE)(0);
  85. }
  86. }
  87. }
  88. static void partition_mult_data(void)
  89. {
  90. starpu_matrix_data_register(&A_handle, 0, (uintptr_t)A,
  91. ydim, ydim, zdim, sizeof(TYPE));
  92. starpu_matrix_data_register(&B_handle, 0, (uintptr_t)B,
  93. zdim, zdim, xdim, sizeof(TYPE));
  94. starpu_matrix_data_register(&C_handle, 0, (uintptr_t)C,
  95. ydim, ydim, xdim, sizeof(TYPE));
  96. struct starpu_data_filter vert;
  97. memset(&vert, 0, sizeof(vert));
  98. vert.filter_func = starpu_vertical_block_filter_func;
  99. vert.nchildren = nslicesx;
  100. struct starpu_data_filter horiz;
  101. memset(&horiz, 0, sizeof(horiz));
  102. horiz.filter_func = starpu_block_filter_func;
  103. horiz.nchildren = nslicesy;
  104. starpu_data_partition(B_handle, &vert);
  105. starpu_data_partition(A_handle, &horiz);
  106. starpu_data_map_filters(C_handle, 2, &vert, &horiz);
  107. }
  108. static void mult_kernel_common(void *descr[], int type)
  109. {
  110. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  111. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  112. TYPE *subC = (TYPE *)STARPU_MATRIX_GET_PTR(descr[2]);
  113. unsigned nxC = STARPU_MATRIX_GET_NX(descr[2]);
  114. unsigned nyC = STARPU_MATRIX_GET_NY(descr[2]);
  115. unsigned nyA = STARPU_MATRIX_GET_NY(descr[0]);
  116. unsigned ldA = STARPU_MATRIX_GET_LD(descr[0]);
  117. unsigned ldB = STARPU_MATRIX_GET_LD(descr[1]);
  118. unsigned ldC = STARPU_MATRIX_GET_LD(descr[2]);
  119. if (type == STARPU_CPU)
  120. {
  121. int worker_size = starpu_combined_worker_get_size();
  122. if (worker_size == 1)
  123. {
  124. /* Sequential CPU task */
  125. CPU_GEMM("N", "N", nxC, nyC, nyA, (TYPE)1.0, subA, ldA, subB, ldB, (TYPE)0.0, subC, ldC);
  126. }
  127. else
  128. {
  129. /* Parallel CPU task */
  130. int rank = starpu_combined_worker_get_rank();
  131. int block_size = (nyC + worker_size - 1)/worker_size;
  132. int new_nyC = STARPU_MIN(nyC, block_size*(rank+1)) - block_size*rank;
  133. STARPU_ASSERT(nyC = STARPU_MATRIX_GET_NY(descr[1]));
  134. TYPE *new_subB = &subB[block_size*rank];
  135. TYPE *new_subC = &subC[block_size*rank];
  136. CPU_GEMM("N", "N", nxC, new_nyC, nyA, (TYPE)1.0, subA, ldA, new_subB, ldB, (TYPE)0.0, new_subC, ldC);
  137. }
  138. }
  139. #ifdef STARPU_USE_CUDA
  140. else
  141. {
  142. CUBLAS_GEMM('n', 'n', nxC, nyC, nyA, (TYPE)1.0, subA, ldA, subB, ldB,
  143. (TYPE)0.0, subC, ldC);
  144. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  145. }
  146. #endif
  147. }
  148. #ifdef STARPU_USE_CUDA
  149. static void cublas_mult(void *descr[], __attribute__((unused)) void *arg)
  150. {
  151. mult_kernel_common(descr, STARPU_CUDA);
  152. }
  153. #endif
  154. static void cpu_mult(void *descr[], __attribute__((unused)) void *arg)
  155. {
  156. mult_kernel_common(descr, STARPU_CPU);
  157. }
  158. static struct starpu_perfmodel starpu_gemm_model =
  159. {
  160. .type = STARPU_HISTORY_BASED,
  161. .symbol = STARPU_GEMM_STR(gemm)
  162. };
  163. static struct starpu_codelet cl =
  164. {
  165. .where = STARPU_CPU|STARPU_CUDA,
  166. .type = STARPU_SEQ, /* changed to STARPU_SPMD if -spmd is passed */
  167. .max_parallelism = INT_MAX,
  168. .cpu_funcs = {cpu_mult, NULL},
  169. #ifdef STARPU_USE_CUDA
  170. .cuda_funcs = {cublas_mult, NULL},
  171. #endif
  172. .nbuffers = 3,
  173. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  174. .model = &starpu_gemm_model
  175. };
  176. static void parse_args(int argc, char **argv)
  177. {
  178. int i;
  179. for (i = 1; i < argc; i++)
  180. {
  181. if (strcmp(argv[i], "-nblocks") == 0)
  182. {
  183. char *argptr;
  184. nslicesx = strtol(argv[++i], &argptr, 10);
  185. nslicesy = nslicesx;
  186. }
  187. if (strcmp(argv[i], "-nblocksx") == 0)
  188. {
  189. char *argptr;
  190. nslicesx = strtol(argv[++i], &argptr, 10);
  191. }
  192. if (strcmp(argv[i], "-nblocksy") == 0)
  193. {
  194. char *argptr;
  195. nslicesy = strtol(argv[++i], &argptr, 10);
  196. }
  197. if (strcmp(argv[i], "-x") == 0)
  198. {
  199. char *argptr;
  200. xdim = strtol(argv[++i], &argptr, 10);
  201. }
  202. if (strcmp(argv[i], "-y") == 0)
  203. {
  204. char *argptr;
  205. ydim = strtol(argv[++i], &argptr, 10);
  206. }
  207. if (strcmp(argv[i], "-z") == 0)
  208. {
  209. char *argptr;
  210. zdim = strtol(argv[++i], &argptr, 10);
  211. }
  212. if (strcmp(argv[i], "-iter") == 0)
  213. {
  214. char *argptr;
  215. niter = strtol(argv[++i], &argptr, 10);
  216. }
  217. if (strcmp(argv[i], "-check") == 0)
  218. {
  219. check = 1;
  220. }
  221. if (strcmp(argv[i], "-spmd") == 0)
  222. {
  223. cl.type = STARPU_SPMD;
  224. }
  225. }
  226. }
  227. int main(int argc, char **argv)
  228. {
  229. struct timeval start;
  230. struct timeval end;
  231. int ret;
  232. parse_args(argc, argv);
  233. ret = starpu_init(NULL);
  234. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  235. starpu_helper_cublas_init();
  236. init_problem_data();
  237. partition_mult_data();
  238. gettimeofday(&start, NULL);
  239. unsigned x, y, iter;
  240. for (iter = 0; iter < niter; iter++)
  241. {
  242. for (x = 0; x < nslicesx; x++)
  243. for (y = 0; y < nslicesy; y++)
  244. {
  245. struct starpu_task *task = starpu_task_create();
  246. task->cl = &cl;
  247. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, y);
  248. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, x);
  249. task->handles[2] = starpu_data_get_sub_data(C_handle, 2, x, y);
  250. int ret = starpu_task_submit(task);
  251. STARPU_ASSERT(!ret);
  252. }
  253. starpu_task_wait_for_all();
  254. }
  255. gettimeofday(&end, NULL);
  256. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  257. FPRINTF(stderr, "Time: %2.2f ms\n", timing/1000.0);
  258. double flops = 2.0*((unsigned long)niter)*((unsigned long)xdim)
  259. *((unsigned long)ydim)*((unsigned long)zdim);
  260. FPRINTF(stderr, "GFlop/s: %.2f\n", flops/timing/1000.0);
  261. starpu_data_unpartition(C_handle, 0);
  262. starpu_data_unregister(A_handle);
  263. starpu_data_unregister(B_handle);
  264. starpu_data_unregister(C_handle);
  265. if (check)
  266. check_output();
  267. starpu_helper_cublas_shutdown();
  268. starpu_shutdown();
  269. return 0;
  270. }