xgemm.c 11 KB

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