xgemm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2010 Mehdi Juhoor
  5. * Copyright (C) 2017 Erwan Leria
  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 <unistd.h>
  29. #include <math.h>
  30. #include <sys/types.h>
  31. #include <starpu.h>
  32. #include <starpu_fxt.h>
  33. #include <common/blas.h>
  34. #ifdef STARPU_USE_CUDA
  35. #include <cuda.h>
  36. #include <starpu_cublas_v2.h>
  37. static const TYPE p1 = 1.0;
  38. static const TYPE m1 = -1.0;
  39. static const TYPE v0 = 0.0;
  40. #endif
  41. static unsigned niter = 10;
  42. static unsigned nsleeps = 1;
  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 unsigned print_hostname = 0;
  57. static TYPE *A, *B, *C;
  58. static starpu_data_handle_t A_handle, B_handle, C_handle;
  59. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  60. #define PRINTF(fmt, ...) do { if (!getenv("STARPU_SSILENT")) {printf(fmt, ## __VA_ARGS__); fflush(stdout); }} while(0)
  61. static void check_output(void)
  62. {
  63. /* compute C = C - AB */
  64. CPU_GEMM("N", "N", ydim, xdim, zdim, (TYPE)-1.0f, A, ydim, B, zdim, (TYPE)1.0f, C, ydim);
  65. /* make sure C = 0 */
  66. TYPE err;
  67. err = CPU_ASUM(xdim*ydim, C, 1);
  68. if (err < xdim*ydim*0.001)
  69. {
  70. FPRINTF(stderr, "Results are OK\n");
  71. }
  72. else
  73. {
  74. int max;
  75. max = CPU_IAMAX(xdim*ydim, C, 1);
  76. FPRINTF(stderr, "There were errors ... err = %f\n", err);
  77. FPRINTF(stderr, "Max error : %e\n", C[max]);
  78. }
  79. }
  80. static void init_problem_data(void)
  81. {
  82. #ifndef STARPU_SIMGRID
  83. unsigned i,j;
  84. #endif
  85. starpu_malloc_flags((void **)&A, zdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  86. starpu_malloc_flags((void **)&B, xdim*zdim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  87. starpu_malloc_flags((void **)&C, xdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  88. #ifndef STARPU_SIMGRID
  89. /* fill the A and B matrices */
  90. for (j=0; j < ydim; j++)
  91. {
  92. for (i=0; i < zdim; i++)
  93. {
  94. A[j+i*ydim] = (TYPE)(starpu_drand48());
  95. }
  96. }
  97. for (j=0; j < zdim; j++)
  98. {
  99. for (i=0; i < xdim; i++)
  100. {
  101. B[j+i*zdim] = (TYPE)(starpu_drand48());
  102. }
  103. }
  104. for (j=0; j < ydim; j++)
  105. {
  106. for (i=0; i < xdim; i++)
  107. {
  108. C[j+i*ydim] = (TYPE)(0);
  109. }
  110. }
  111. #endif
  112. }
  113. static void partition_mult_data(void)
  114. {
  115. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A,
  116. ydim, ydim, zdim, sizeof(TYPE));
  117. starpu_matrix_data_register(&B_handle, STARPU_MAIN_RAM, (uintptr_t)B,
  118. zdim, zdim, xdim, sizeof(TYPE));
  119. starpu_matrix_data_register(&C_handle, STARPU_MAIN_RAM, (uintptr_t)C,
  120. ydim, ydim, xdim, sizeof(TYPE));
  121. struct starpu_data_filter vert;
  122. memset(&vert, 0, sizeof(vert));
  123. vert.filter_func = starpu_matrix_filter_vertical_block;
  124. vert.nchildren = nslicesx;
  125. struct starpu_data_filter horiz;
  126. memset(&horiz, 0, sizeof(horiz));
  127. horiz.filter_func = starpu_matrix_filter_block;
  128. horiz.nchildren = nslicesy;
  129. starpu_data_partition(B_handle, &vert);
  130. starpu_data_partition(A_handle, &horiz);
  131. starpu_data_map_filters(C_handle, 2, &vert, &horiz);
  132. unsigned x, y;
  133. for (x = 0; x < nslicesx; x++)
  134. for (y = 0; y < nslicesy; y++)
  135. starpu_data_set_coordinates(starpu_data_get_sub_data(C_handle, 2, x, y), 2, x, y);
  136. }
  137. #ifdef STARPU_USE_CUDA
  138. static void cublas_mult(void *descr[], void *arg)
  139. {
  140. (void)arg;
  141. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  142. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  143. TYPE *subC = (TYPE *)STARPU_MATRIX_GET_PTR(descr[2]);
  144. unsigned nxC = STARPU_MATRIX_GET_NX(descr[2]);
  145. unsigned nyC = STARPU_MATRIX_GET_NY(descr[2]);
  146. unsigned nyA = STARPU_MATRIX_GET_NY(descr[0]);
  147. unsigned ldA = STARPU_MATRIX_GET_LD(descr[0]);
  148. unsigned ldB = STARPU_MATRIX_GET_LD(descr[1]);
  149. unsigned ldC = STARPU_MATRIX_GET_LD(descr[2]);
  150. cudaStream_t stream = starpu_cuda_get_local_stream();
  151. if (nxC == ldC)
  152. cudaMemsetAsync(subC, 0, sizeof(*subC) * nxC * nyC, stream);
  153. else
  154. {
  155. unsigned i;
  156. for (i = 0; i < nyC; i++)
  157. cudaMemsetAsync(subC + i*ldC, 0, sizeof(*subC) * nxC, stream);
  158. }
  159. cublasStatus_t status = CUBLAS_GEMM(starpu_cublas_get_local_handle(),
  160. CUBLAS_OP_N, CUBLAS_OP_N,
  161. nxC, nyC, nyA,
  162. &p1, subA, ldA, subB, ldB,
  163. &v0, subC, ldC);
  164. if (status != CUBLAS_STATUS_SUCCESS)
  165. STARPU_CUBLAS_REPORT_ERROR(status);
  166. }
  167. #endif
  168. void cpu_mult(void *descr[], void *arg)
  169. {
  170. (void)arg;
  171. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  172. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  173. TYPE *subC = (TYPE *)STARPU_MATRIX_GET_PTR(descr[2]);
  174. unsigned nxC = STARPU_MATRIX_GET_NX(descr[2]);
  175. unsigned nyC = STARPU_MATRIX_GET_NY(descr[2]);
  176. unsigned nyA = STARPU_MATRIX_GET_NY(descr[0]);
  177. unsigned ldA = STARPU_MATRIX_GET_LD(descr[0]);
  178. unsigned ldB = STARPU_MATRIX_GET_LD(descr[1]);
  179. unsigned ldC = STARPU_MATRIX_GET_LD(descr[2]);
  180. int worker_size = starpu_combined_worker_get_size();
  181. if (nxC == ldC)
  182. memset(subC, 0, sizeof(*subC) * nxC * nyC);
  183. else
  184. {
  185. unsigned i;
  186. for (i = 0; i < nyC; i++)
  187. memset(subC + i*ldC, 0, sizeof(*subC) * nxC);
  188. }
  189. if (worker_size == 1)
  190. {
  191. /* Sequential CPU task */
  192. CPU_GEMM("N", "N", nxC, nyC, nyA, (TYPE)1.0, subA, ldA, subB, ldB, (TYPE)0.0, subC, ldC);
  193. }
  194. else
  195. {
  196. /* Parallel CPU task */
  197. unsigned rank = starpu_combined_worker_get_rank();
  198. unsigned block_size = (nyC + worker_size - 1)/worker_size;
  199. unsigned new_nyC = STARPU_MIN(nyC, block_size*(rank+1)) - block_size*rank;
  200. STARPU_ASSERT(nyC == STARPU_MATRIX_GET_NY(descr[1]));
  201. TYPE *new_subB = &subB[block_size*rank];
  202. TYPE *new_subC = &subC[block_size*rank];
  203. CPU_GEMM("N", "N", nxC, new_nyC, nyA, (TYPE)1.0, subA, ldA, new_subB, ldB, (TYPE)0.0, new_subC, ldC);
  204. }
  205. }
  206. static struct starpu_perfmodel starpu_gemm_model =
  207. {
  208. .type = STARPU_HISTORY_BASED,
  209. .symbol = STARPU_GEMM_STR(gemm)
  210. };
  211. static struct starpu_codelet cl =
  212. {
  213. .type = STARPU_SEQ, /* changed to STARPU_SPMD if -spmd is passed */
  214. .max_parallelism = INT_MAX,
  215. .cpu_funcs = {cpu_mult},
  216. .cpu_funcs_name = {"cpu_mult"},
  217. #ifdef STARPU_USE_CUDA
  218. .cuda_funcs = {cublas_mult},
  219. #elif defined(STARPU_SIMGRID)
  220. .cuda_funcs = {(void*)1},
  221. #endif
  222. .cuda_flags = {STARPU_CUDA_ASYNC},
  223. .nbuffers = 3,
  224. .modes = {STARPU_R, STARPU_R, STARPU_W},
  225. .model = &starpu_gemm_model
  226. };
  227. static void parse_args(int argc, char **argv)
  228. {
  229. int i;
  230. for (i = 1; i < argc; i++)
  231. {
  232. if (strcmp(argv[i], "-nblocks") == 0)
  233. {
  234. char *argptr;
  235. nslicesx = strtol(argv[++i], &argptr, 10);
  236. nslicesy = nslicesx;
  237. }
  238. else if (strcmp(argv[i], "-nblocksx") == 0)
  239. {
  240. char *argptr;
  241. nslicesx = strtol(argv[++i], &argptr, 10);
  242. }
  243. else if (strcmp(argv[i], "-nblocksy") == 0)
  244. {
  245. char *argptr;
  246. nslicesy = strtol(argv[++i], &argptr, 10);
  247. }
  248. else if (strcmp(argv[i], "-x") == 0)
  249. {
  250. char *argptr;
  251. xdim = strtol(argv[++i], &argptr, 10);
  252. }
  253. else if (strcmp(argv[i], "-xy") == 0)
  254. {
  255. char *argptr;
  256. xdim = ydim = strtol(argv[++i], &argptr, 10);
  257. }
  258. else if (strcmp(argv[i], "-y") == 0)
  259. {
  260. char *argptr;
  261. ydim = strtol(argv[++i], &argptr, 10);
  262. }
  263. else if (strcmp(argv[i], "-z") == 0)
  264. {
  265. char *argptr;
  266. zdim = strtol(argv[++i], &argptr, 10);
  267. }
  268. else if (strcmp(argv[i], "-size") == 0)
  269. {
  270. char *argptr;
  271. xdim = ydim = zdim = strtol(argv[++i], &argptr, 10);
  272. }
  273. else if (strcmp(argv[i], "-iter") == 0)
  274. {
  275. char *argptr;
  276. niter = strtol(argv[++i], &argptr, 10);
  277. }
  278. else if (strcmp(argv[i], "-nsleeps") == 0)
  279. {
  280. char *argptr;
  281. nsleeps = strtol(argv[++i], &argptr, 10);
  282. }
  283. else if (strcmp(argv[i], "-bound") == 0)
  284. {
  285. bound = 1;
  286. }
  287. else if (strcmp(argv[i], "-hostname") == 0)
  288. {
  289. print_hostname = 1;
  290. }
  291. else if (strcmp(argv[i], "-check") == 0)
  292. {
  293. check = 1;
  294. }
  295. else if (strcmp(argv[i], "-spmd") == 0)
  296. {
  297. cl.type = STARPU_SPMD;
  298. }
  299. else if (strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
  300. {
  301. 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] [-hostname] [-nsleeps nsleeps]\n", argv[0]);
  302. fprintf(stderr,"Currently selected: %ux%u * %ux%u and %ux%u blocks, %u iterations, %u sleeps\n", zdim, ydim, xdim, zdim, nslicesx, nslicesy, niter, nsleeps);
  303. exit(EXIT_SUCCESS);
  304. }
  305. else
  306. {
  307. fprintf(stderr,"Unrecognized option %s\n", argv[i]);
  308. exit(EXIT_FAILURE);
  309. }
  310. }
  311. }
  312. int main(int argc, char **argv)
  313. {
  314. double start, end;
  315. int ret;
  316. parse_args(argc, argv);
  317. #ifdef STARPU_QUICK_CHECK
  318. niter /= 10;
  319. #endif
  320. starpu_fxt_autostart_profiling(0);
  321. ret = starpu_init(NULL);
  322. if (ret == -ENODEV)
  323. return 77;
  324. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  325. starpu_cublas_init();
  326. init_problem_data();
  327. partition_mult_data();
  328. PRINTF("# ");
  329. if (print_hostname)
  330. PRINTF("node\t");
  331. PRINTF("x\ty\tz\tms\tGFlops");
  332. if (bound)
  333. PRINTF("\tTms\tTGFlops\tTims\tTiGFlops");
  334. PRINTF("\n");
  335. unsigned sleeps;
  336. for (sleeps = 0; sleeps < nsleeps; sleeps++)
  337. {
  338. if (bound)
  339. starpu_bound_start(0, 0);
  340. starpu_fxt_start_profiling();
  341. start = starpu_timing_now();
  342. unsigned x, y, iter;
  343. for (iter = 0; iter < niter; iter++)
  344. {
  345. for (x = 0; x < nslicesx; x++)
  346. for (y = 0; y < nslicesy; y++)
  347. {
  348. struct starpu_task *task = starpu_task_create();
  349. task->cl = &cl;
  350. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, y);
  351. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, x);
  352. task->handles[2] = starpu_data_get_sub_data(C_handle, 2, x, y);
  353. task->flops = 2ULL * (xdim/nslicesx) * (ydim/nslicesy) * zdim;
  354. ret = starpu_task_submit(task);
  355. if (ret == -ENODEV)
  356. {
  357. ret = 77;
  358. goto enodev;
  359. }
  360. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  361. starpu_data_wont_use(starpu_data_get_sub_data(C_handle, 2, x, y));
  362. }
  363. starpu_task_wait_for_all();
  364. }
  365. end = starpu_timing_now();
  366. starpu_fxt_stop_profiling();
  367. if (bound)
  368. starpu_bound_stop();
  369. double timing = end - start;
  370. double min, min_int;
  371. double flops = 2.0*((unsigned long long)niter)*((unsigned long long)xdim)
  372. *((unsigned long long)ydim)*((unsigned long long)zdim);
  373. if (bound)
  374. starpu_bound_compute(&min, &min_int, 1);
  375. if (print_hostname)
  376. {
  377. char hostname[255];
  378. gethostname(hostname, 255);
  379. PRINTF("%s\t", hostname);
  380. }
  381. PRINTF("%u\t%u\t%u\t%.0f\t%.1f", xdim, ydim, zdim, timing/niter/1000.0, flops/timing/1000.0);
  382. if (bound)
  383. PRINTF("\t%.0f\t%.1f\t%.0f\t%.1f", min, flops/min/1000000.0, min_int, flops/min_int/1000000.0);
  384. PRINTF("\n");
  385. if (sleeps < nsleeps-1)
  386. {
  387. sleep(10);
  388. }
  389. }
  390. enodev:
  391. starpu_data_unpartition(C_handle, STARPU_MAIN_RAM);
  392. starpu_data_unpartition(B_handle, STARPU_MAIN_RAM);
  393. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  394. starpu_data_unregister(A_handle);
  395. starpu_data_unregister(B_handle);
  396. starpu_data_unregister(C_handle);
  397. if (check)
  398. check_output();
  399. starpu_free_flags(A, zdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  400. starpu_free_flags(B, xdim*zdim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  401. starpu_free_flags(C, xdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  402. starpu_cublas_shutdown();
  403. starpu_shutdown();
  404. return ret;
  405. }