xgemm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2016 Université de Bordeaux
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012, 2013, 2016 CNRS
  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. /*
  19. * Simple parallel GEMM implementation: partition the output matrix in the two
  20. * dimensions, and the input matrices in the corresponding dimension, and
  21. * perform the output computations in parallel.
  22. */
  23. #ifndef TYPE
  24. #error "Do not compile xgemm.c directly, compile sgemm.c or dgemm.c"
  25. #endif
  26. #include <limits.h>
  27. #include <string.h>
  28. #include <math.h>
  29. #include <sys/types.h>
  30. #include <starpu.h>
  31. #include <starpu_fxt.h>
  32. #include <common/blas.h>
  33. #ifdef STARPU_USE_CUDA
  34. #include <cuda.h>
  35. #include <cublas.h>
  36. #endif
  37. static unsigned niter = 10;
  38. static unsigned nslicesx = 4;
  39. static unsigned nslicesy = 4;
  40. #if defined(STARPU_QUICK_CHECK) && !defined(STARPU_SIMGRID)
  41. static unsigned xdim = 256;
  42. static unsigned ydim = 256;
  43. static unsigned zdim = 64;
  44. #else
  45. static unsigned xdim = 960*4;
  46. static unsigned ydim = 960*4;
  47. static unsigned zdim = 960*4;
  48. #endif
  49. static unsigned check = 0;
  50. static unsigned bound = 0;
  51. static TYPE *A, *B, *C;
  52. static starpu_data_handle_t A_handle, B_handle, C_handle;
  53. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  54. #define PRINTF(fmt, ...) do { if (!getenv("STARPU_SSILENT")) {printf(fmt, ## __VA_ARGS__); }} while(0)
  55. static void check_output(void)
  56. {
  57. /* compute C = C - AB */
  58. CPU_GEMM("N", "N", ydim, xdim, zdim, (TYPE)-1.0f, A, ydim, B, zdim, (TYPE)1.0f, C, ydim);
  59. /* make sure C = 0 */
  60. TYPE err;
  61. err = CPU_ASUM(xdim*ydim, C, 1);
  62. if (err < xdim*ydim*0.001)
  63. {
  64. FPRINTF(stderr, "Results are OK\n");
  65. }
  66. else
  67. {
  68. int max;
  69. max = CPU_IAMAX(xdim*ydim, C, 1);
  70. FPRINTF(stderr, "There were errors ... err = %f\n", err);
  71. FPRINTF(stderr, "Max error : %e\n", C[max]);
  72. }
  73. }
  74. static void init_problem_data(void)
  75. {
  76. #ifndef STARPU_SIMGRID
  77. unsigned i,j;
  78. #endif
  79. starpu_malloc_flags((void **)&A, zdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  80. starpu_malloc_flags((void **)&B, xdim*zdim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  81. starpu_malloc_flags((void **)&C, xdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  82. #ifndef STARPU_SIMGRID
  83. /* fill the A and B matrices */
  84. for (j=0; j < ydim; j++)
  85. {
  86. for (i=0; i < zdim; i++)
  87. {
  88. A[j+i*ydim] = (TYPE)(starpu_drand48());
  89. }
  90. }
  91. for (j=0; j < zdim; j++)
  92. {
  93. for (i=0; i < xdim; i++)
  94. {
  95. B[j+i*zdim] = (TYPE)(starpu_drand48());
  96. }
  97. }
  98. for (j=0; j < ydim; j++)
  99. {
  100. for (i=0; i < xdim; i++)
  101. {
  102. C[j+i*ydim] = (TYPE)(0);
  103. }
  104. }
  105. #endif
  106. }
  107. static void partition_mult_data(void)
  108. {
  109. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A,
  110. ydim, ydim, zdim, sizeof(TYPE));
  111. starpu_matrix_data_register(&B_handle, STARPU_MAIN_RAM, (uintptr_t)B,
  112. zdim, zdim, xdim, sizeof(TYPE));
  113. starpu_matrix_data_register(&C_handle, STARPU_MAIN_RAM, (uintptr_t)C,
  114. ydim, ydim, xdim, sizeof(TYPE));
  115. struct starpu_data_filter vert;
  116. memset(&vert, 0, sizeof(vert));
  117. vert.filter_func = starpu_matrix_filter_vertical_block;
  118. vert.nchildren = nslicesx;
  119. struct starpu_data_filter horiz;
  120. memset(&horiz, 0, sizeof(horiz));
  121. horiz.filter_func = starpu_matrix_filter_block;
  122. horiz.nchildren = nslicesy;
  123. starpu_data_partition(B_handle, &vert);
  124. starpu_data_partition(A_handle, &horiz);
  125. starpu_data_map_filters(C_handle, 2, &vert, &horiz);
  126. }
  127. #ifdef STARPU_USE_CUDA
  128. static void cublas_mult(void *descr[], STARPU_ATTRIBUTE_UNUSED void *arg)
  129. {
  130. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  131. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  132. TYPE *subC = (TYPE *)STARPU_MATRIX_GET_PTR(descr[2]);
  133. unsigned nxC = STARPU_MATRIX_GET_NX(descr[2]);
  134. unsigned nyC = STARPU_MATRIX_GET_NY(descr[2]);
  135. unsigned nyA = STARPU_MATRIX_GET_NY(descr[0]);
  136. unsigned ldA = STARPU_MATRIX_GET_LD(descr[0]);
  137. unsigned ldB = STARPU_MATRIX_GET_LD(descr[1]);
  138. unsigned ldC = STARPU_MATRIX_GET_LD(descr[2]);
  139. CUBLAS_GEMM('n', 'n', nxC, nyC, nyA, (TYPE)1.0, subA, ldA, subB, ldB,
  140. (TYPE)0.0, subC, ldC);
  141. }
  142. #endif
  143. void cpu_mult(void *descr[], STARPU_ATTRIBUTE_UNUSED void *arg)
  144. {
  145. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  146. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  147. TYPE *subC = (TYPE *)STARPU_MATRIX_GET_PTR(descr[2]);
  148. unsigned nxC = STARPU_MATRIX_GET_NX(descr[2]);
  149. unsigned nyC = STARPU_MATRIX_GET_NY(descr[2]);
  150. unsigned nyA = STARPU_MATRIX_GET_NY(descr[0]);
  151. unsigned ldA = STARPU_MATRIX_GET_LD(descr[0]);
  152. unsigned ldB = STARPU_MATRIX_GET_LD(descr[1]);
  153. unsigned ldC = STARPU_MATRIX_GET_LD(descr[2]);
  154. int worker_size = starpu_combined_worker_get_size();
  155. if (worker_size == 1)
  156. {
  157. /* Sequential CPU task */
  158. CPU_GEMM("N", "N", nxC, nyC, nyA, (TYPE)1.0, subA, ldA, subB, ldB, (TYPE)0.0, subC, ldC);
  159. }
  160. else
  161. {
  162. /* Parallel CPU task */
  163. unsigned rank = starpu_combined_worker_get_rank();
  164. unsigned block_size = (nyC + worker_size - 1)/worker_size;
  165. unsigned new_nyC = STARPU_MIN(nyC, block_size*(rank+1)) - block_size*rank;
  166. STARPU_ASSERT(nyC == STARPU_MATRIX_GET_NY(descr[1]));
  167. TYPE *new_subB = &subB[block_size*rank];
  168. TYPE *new_subC = &subC[block_size*rank];
  169. CPU_GEMM("N", "N", nxC, new_nyC, nyA, (TYPE)1.0, subA, ldA, new_subB, ldB, (TYPE)0.0, new_subC, ldC);
  170. }
  171. }
  172. static struct starpu_perfmodel starpu_gemm_model =
  173. {
  174. .type = STARPU_HISTORY_BASED,
  175. .symbol = STARPU_GEMM_STR(gemm)
  176. };
  177. static struct starpu_codelet cl =
  178. {
  179. .type = STARPU_SEQ, /* changed to STARPU_SPMD if -spmd is passed */
  180. .max_parallelism = INT_MAX,
  181. .cpu_funcs = {cpu_mult},
  182. .cpu_funcs_name = {"cpu_mult"},
  183. #ifdef STARPU_USE_CUDA
  184. .cuda_funcs = {cublas_mult},
  185. #elif defined(STARPU_SIMGRID)
  186. .cuda_funcs = {(void*)1},
  187. #endif
  188. .cuda_flags = {STARPU_CUDA_ASYNC},
  189. .nbuffers = 3,
  190. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  191. .model = &starpu_gemm_model
  192. };
  193. static void parse_args(int argc, char **argv)
  194. {
  195. int i;
  196. for (i = 1; i < argc; i++)
  197. {
  198. if (strcmp(argv[i], "-nblocks") == 0)
  199. {
  200. char *argptr;
  201. nslicesx = strtol(argv[++i], &argptr, 10);
  202. nslicesy = nslicesx;
  203. }
  204. else if (strcmp(argv[i], "-nblocksx") == 0)
  205. {
  206. char *argptr;
  207. nslicesx = strtol(argv[++i], &argptr, 10);
  208. }
  209. else if (strcmp(argv[i], "-nblocksy") == 0)
  210. {
  211. char *argptr;
  212. nslicesy = strtol(argv[++i], &argptr, 10);
  213. }
  214. else if (strcmp(argv[i], "-x") == 0)
  215. {
  216. char *argptr;
  217. xdim = strtol(argv[++i], &argptr, 10);
  218. }
  219. else if (strcmp(argv[i], "-y") == 0)
  220. {
  221. char *argptr;
  222. ydim = strtol(argv[++i], &argptr, 10);
  223. }
  224. else if (strcmp(argv[i], "-z") == 0)
  225. {
  226. char *argptr;
  227. zdim = strtol(argv[++i], &argptr, 10);
  228. }
  229. else if (strcmp(argv[i], "-size") == 0)
  230. {
  231. char *argptr;
  232. xdim = ydim = zdim = strtol(argv[++i], &argptr, 10);
  233. }
  234. else if (strcmp(argv[i], "-iter") == 0)
  235. {
  236. char *argptr;
  237. niter = strtol(argv[++i], &argptr, 10);
  238. }
  239. else if (strcmp(argv[i], "-bound") == 0)
  240. {
  241. bound = 1;
  242. }
  243. else if (strcmp(argv[i], "-check") == 0)
  244. {
  245. check = 1;
  246. }
  247. else if (strcmp(argv[i], "-spmd") == 0)
  248. {
  249. cl.type = STARPU_SPMD;
  250. }
  251. else if (strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
  252. {
  253. fprintf(stderr,"Usage: %s [-nblocks n] [-nblocksx x] [-nblocksy y] [-x x] [-y y] [-z z] [-size size] [-iter iter] [-bound] [-check] [-spmd]\n", argv[0]);
  254. fprintf(stderr,"Currently selected: %ux%u * %ux%u and %ux%u blocks, %u iterations\n", zdim, ydim, xdim, zdim, nslicesx, nslicesy, niter);
  255. exit(EXIT_SUCCESS);
  256. }
  257. else
  258. {
  259. fprintf(stderr,"Unrecognized option %s", argv[i]);
  260. exit(EXIT_FAILURE);
  261. }
  262. }
  263. }
  264. int main(int argc, char **argv)
  265. {
  266. double start, end;
  267. int ret;
  268. parse_args(argc, argv);
  269. #ifdef STARPU_QUICK_CHECK
  270. niter /= 10;
  271. #endif
  272. starpu_fxt_autostart_profiling(0);
  273. ret = starpu_init(NULL);
  274. if (ret == -ENODEV)
  275. return 77;
  276. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  277. starpu_cublas_init();
  278. init_problem_data();
  279. partition_mult_data();
  280. if (bound)
  281. starpu_bound_start(0, 0);
  282. starpu_fxt_start_profiling();
  283. start = starpu_timing_now();
  284. unsigned x, y, iter;
  285. for (iter = 0; iter < niter; iter++)
  286. {
  287. for (x = 0; x < nslicesx; x++)
  288. for (y = 0; y < nslicesy; y++)
  289. {
  290. struct starpu_task *task = starpu_task_create();
  291. task->cl = &cl;
  292. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, y);
  293. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, x);
  294. task->handles[2] = starpu_data_get_sub_data(C_handle, 2, x, y);
  295. task->flops = 2ULL * (xdim/nslicesx) * (ydim/nslicesy) * zdim;
  296. ret = starpu_task_submit(task);
  297. if (ret == -ENODEV)
  298. {
  299. ret = 77;
  300. goto enodev;
  301. }
  302. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  303. starpu_data_wont_use(starpu_data_get_sub_data(C_handle, 2, x, y));
  304. }
  305. starpu_task_wait_for_all();
  306. }
  307. end = starpu_timing_now();
  308. starpu_fxt_stop_profiling();
  309. if (bound)
  310. starpu_bound_stop();
  311. double timing = end - start;
  312. double min, min_int;
  313. double flops = 2.0*((unsigned long long)niter)*((unsigned long long)xdim)
  314. *((unsigned long long)ydim)*((unsigned long long)zdim);
  315. if (bound)
  316. starpu_bound_compute(&min, &min_int, 1);
  317. PRINTF("# x\ty\tz\tms\tGFlops");
  318. if (bound)
  319. PRINTF("\tTms\tTGFlops\tTims\tTiGFlops");
  320. PRINTF("\n");
  321. PRINTF("%u\t%u\t%u\t%.0f\t%.1f", xdim, ydim, zdim, timing/niter/1000.0, flops/timing/1000.0);
  322. if (bound)
  323. PRINTF("\t%.0f\t%.1f\t%.0f\t%.1f", min, flops/min/1000000.0, min_int, flops/min_int/1000000.0);
  324. PRINTF("\n");
  325. enodev:
  326. starpu_data_unpartition(C_handle, STARPU_MAIN_RAM);
  327. starpu_data_unpartition(B_handle, STARPU_MAIN_RAM);
  328. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  329. starpu_data_unregister(A_handle);
  330. starpu_data_unregister(B_handle);
  331. starpu_data_unregister(C_handle);
  332. if (check)
  333. check_output();
  334. starpu_free_flags(A, zdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  335. starpu_free_flags(B, xdim*zdim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  336. starpu_free_flags(C, xdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  337. starpu_cublas_shutdown();
  338. starpu_shutdown();
  339. return ret;
  340. }