xgemm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 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. static unsigned nslicesz = 4;
  46. #if defined(STARPU_QUICK_CHECK) && !defined(STARPU_SIMGRID)
  47. static unsigned xdim = 256;
  48. static unsigned ydim = 256;
  49. static unsigned zdim = 64;
  50. #else
  51. static unsigned xdim = 960*4;
  52. static unsigned ydim = 960*4;
  53. static unsigned zdim = 960*4;
  54. #endif
  55. static unsigned check = 0;
  56. static unsigned bound = 0;
  57. static unsigned print_hostname = 0;
  58. static unsigned tiled = 0;
  59. static TYPE *A, *B, *C;
  60. static starpu_data_handle_t A_handle, B_handle, C_handle;
  61. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  62. #define PRINTF(fmt, ...) do { if (!getenv("STARPU_SSILENT")) {printf(fmt, ## __VA_ARGS__); fflush(stdout); }} while(0)
  63. static int check_output(void)
  64. {
  65. /* compute C = C - AB */
  66. CPU_GEMM("N", "N", ydim, xdim, zdim, (TYPE)-1.0f, A, ydim, B, zdim, (TYPE)1.0f, C, ydim);
  67. /* make sure C = 0 */
  68. TYPE err;
  69. err = CPU_ASUM(xdim*ydim, C, 1);
  70. if (err < EPSILON*xdim*ydim*zdim)
  71. {
  72. FPRINTF(stderr, "Results are OK\n");
  73. return 0;
  74. }
  75. else
  76. {
  77. int max;
  78. max = CPU_IAMAX(xdim*ydim, C, 1);
  79. FPRINTF(stderr, "There were errors ... err = %f\n", err);
  80. FPRINTF(stderr, "Max error : %e\n", C[max]);
  81. return 1;
  82. }
  83. }
  84. static void init_problem_data(void)
  85. {
  86. #ifndef STARPU_SIMGRID
  87. unsigned i,j;
  88. #endif
  89. starpu_malloc_flags((void **)&A, zdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  90. starpu_malloc_flags((void **)&B, xdim*zdim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  91. starpu_malloc_flags((void **)&C, xdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  92. #ifndef STARPU_SIMGRID
  93. /* fill the A and B matrices */
  94. for (j=0; j < ydim; j++)
  95. {
  96. for (i=0; i < zdim; i++)
  97. {
  98. A[j+i*ydim] = (TYPE)(starpu_drand48());
  99. }
  100. }
  101. for (j=0; j < zdim; j++)
  102. {
  103. for (i=0; i < xdim; i++)
  104. {
  105. B[j+i*zdim] = (TYPE)(starpu_drand48());
  106. }
  107. }
  108. for (j=0; j < ydim; j++)
  109. {
  110. for (i=0; i < xdim; i++)
  111. {
  112. C[j+i*ydim] = (TYPE)(0);
  113. }
  114. }
  115. #endif
  116. }
  117. static void partition_mult_data(void)
  118. {
  119. unsigned x, y, z;
  120. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A,
  121. ydim, ydim, zdim, sizeof(TYPE));
  122. starpu_matrix_data_register(&B_handle, STARPU_MAIN_RAM, (uintptr_t)B,
  123. zdim, zdim, xdim, sizeof(TYPE));
  124. starpu_matrix_data_register(&C_handle, STARPU_MAIN_RAM, (uintptr_t)C,
  125. ydim, ydim, xdim, sizeof(TYPE));
  126. struct starpu_data_filter vert;
  127. memset(&vert, 0, sizeof(vert));
  128. vert.filter_func = starpu_matrix_filter_vertical_block;
  129. vert.nchildren = nslicesx;
  130. struct starpu_data_filter horiz;
  131. memset(&horiz, 0, sizeof(horiz));
  132. horiz.filter_func = starpu_matrix_filter_block;
  133. horiz.nchildren = nslicesy;
  134. if (tiled)
  135. {
  136. struct starpu_data_filter vertA;
  137. memset(&vertA, 0, sizeof(vertA));
  138. vertA.filter_func = starpu_matrix_filter_vertical_block;
  139. vertA.nchildren = nslicesz;
  140. struct starpu_data_filter horizB;
  141. memset(&horizB, 0, sizeof(horizB));
  142. horizB.filter_func = starpu_matrix_filter_block;
  143. horizB.nchildren = nslicesz;
  144. starpu_data_map_filters(A_handle, 2, &vertA, &horiz);
  145. starpu_data_map_filters(B_handle, 2, &vert, &horizB);
  146. starpu_data_map_filters(C_handle, 2, &vert, &horiz);
  147. for (y = 0; y < nslicesy; y++)
  148. for (z = 0; z < nslicesz; z++)
  149. starpu_data_set_coordinates(starpu_data_get_sub_data(A_handle, 2, z, y), 2, z, y);
  150. for (x = 0; x < nslicesx; x++)
  151. for (z = 0; z < nslicesz; z++)
  152. starpu_data_set_coordinates(starpu_data_get_sub_data(B_handle, 2, x, z), 2, x, z);
  153. }
  154. else
  155. {
  156. starpu_data_partition(B_handle, &vert);
  157. starpu_data_partition(A_handle, &horiz);
  158. starpu_data_map_filters(C_handle, 2, &vert, &horiz);
  159. }
  160. for (x = 0; x < nslicesx; x++)
  161. for (y = 0; y < nslicesy; y++)
  162. starpu_data_set_coordinates(starpu_data_get_sub_data(C_handle, 2, x, y), 2, x, y);
  163. }
  164. #ifdef STARPU_USE_CUDA
  165. static void cublas_mult(void *descr[], void *arg, const TYPE *beta)
  166. {
  167. (void)arg;
  168. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  169. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  170. TYPE *subC = (TYPE *)STARPU_MATRIX_GET_PTR(descr[2]);
  171. unsigned nxC = STARPU_MATRIX_GET_NX(descr[2]);
  172. unsigned nyC = STARPU_MATRIX_GET_NY(descr[2]);
  173. unsigned nyA = STARPU_MATRIX_GET_NY(descr[0]);
  174. unsigned ldA = STARPU_MATRIX_GET_LD(descr[0]);
  175. unsigned ldB = STARPU_MATRIX_GET_LD(descr[1]);
  176. unsigned ldC = STARPU_MATRIX_GET_LD(descr[2]);
  177. cublasStatus_t status = CUBLAS_GEMM(starpu_cublas_get_local_handle(),
  178. CUBLAS_OP_N, CUBLAS_OP_N,
  179. nxC, nyC, nyA,
  180. &p1, subA, ldA, subB, ldB,
  181. beta, subC, ldC);
  182. if (status != CUBLAS_STATUS_SUCCESS)
  183. STARPU_CUBLAS_REPORT_ERROR(status);
  184. }
  185. static void cublas_gemm0(void *descr[], void *arg)
  186. {
  187. cublas_mult(descr, arg, &v0);
  188. }
  189. static void cublas_gemm(void *descr[], void *arg)
  190. {
  191. cublas_mult(descr, arg, &p1);
  192. }
  193. #endif
  194. void cpu_mult(void *descr[], void *arg, TYPE beta)
  195. {
  196. (void)arg;
  197. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  198. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  199. TYPE *subC = (TYPE *)STARPU_MATRIX_GET_PTR(descr[2]);
  200. unsigned nxC = STARPU_MATRIX_GET_NX(descr[2]);
  201. unsigned nyC = STARPU_MATRIX_GET_NY(descr[2]);
  202. unsigned nyA = STARPU_MATRIX_GET_NY(descr[0]);
  203. unsigned ldA = STARPU_MATRIX_GET_LD(descr[0]);
  204. unsigned ldB = STARPU_MATRIX_GET_LD(descr[1]);
  205. unsigned ldC = STARPU_MATRIX_GET_LD(descr[2]);
  206. int worker_size = starpu_combined_worker_get_size();
  207. if (worker_size == 1)
  208. {
  209. /* Sequential CPU task */
  210. CPU_GEMM("N", "N", nxC, nyC, nyA, (TYPE)1.0, subA, ldA, subB, ldB, beta, subC, ldC);
  211. }
  212. else
  213. {
  214. /* Parallel CPU task */
  215. unsigned rank = starpu_combined_worker_get_rank();
  216. unsigned block_size = (nyC + worker_size - 1)/worker_size;
  217. unsigned new_nyC = STARPU_MIN(nyC, block_size*(rank+1)) - block_size*rank;
  218. STARPU_ASSERT(nyC == STARPU_MATRIX_GET_NY(descr[1]));
  219. TYPE *new_subB = &subB[block_size*rank];
  220. TYPE *new_subC = &subC[block_size*rank];
  221. CPU_GEMM("N", "N", nxC, new_nyC, nyA, (TYPE)1.0, subA, ldA, new_subB, ldB, beta, new_subC, ldC);
  222. }
  223. }
  224. void cpu_gemm0(void *descr[], void *arg)
  225. {
  226. cpu_mult(descr, arg, 0.);
  227. }
  228. void cpu_gemm(void *descr[], void *arg)
  229. {
  230. cpu_mult(descr, arg, 1.);
  231. }
  232. static struct starpu_perfmodel starpu_gemm_model =
  233. {
  234. .type = STARPU_HISTORY_BASED,
  235. .symbol = STARPU_GEMM_STR(gemm)
  236. };
  237. static struct starpu_codelet cl_gemm0 =
  238. {
  239. .type = STARPU_SEQ, /* changed to STARPU_SPMD if -spmd is passed */
  240. .max_parallelism = INT_MAX,
  241. .cpu_funcs = {cpu_gemm0},
  242. .cpu_funcs_name = {"cpu_gemm0"},
  243. #ifdef STARPU_USE_CUDA
  244. .cuda_funcs = {cublas_gemm0},
  245. #elif defined(STARPU_SIMGRID)
  246. .cuda_funcs = {(void*)1},
  247. #endif
  248. .cuda_flags = {STARPU_CUDA_ASYNC},
  249. .nbuffers = 3,
  250. .modes = {STARPU_R, STARPU_R, STARPU_W},
  251. .model = &starpu_gemm_model
  252. };
  253. static struct starpu_codelet cl_gemm =
  254. {
  255. .type = STARPU_SEQ, /* changed to STARPU_SPMD if -spmd is passed */
  256. .max_parallelism = INT_MAX,
  257. .cpu_funcs = {cpu_gemm},
  258. .cpu_funcs_name = {"cpu_gemm"},
  259. #ifdef STARPU_USE_CUDA
  260. .cuda_funcs = {cublas_gemm},
  261. #elif defined(STARPU_SIMGRID)
  262. .cuda_funcs = {(void*)1},
  263. #endif
  264. .cuda_flags = {STARPU_CUDA_ASYNC},
  265. .nbuffers = 3,
  266. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  267. .model = &starpu_gemm_model
  268. };
  269. static void parse_args(int argc, char **argv)
  270. {
  271. int i;
  272. for (i = 1; i < argc; i++)
  273. {
  274. if (strcmp(argv[i], "-3d") == 0)
  275. {
  276. tiled = 1;
  277. }
  278. else if (strcmp(argv[i], "-nblocks") == 0)
  279. {
  280. char *argptr;
  281. nslicesx = strtol(argv[++i], &argptr, 10);
  282. nslicesy = nslicesx;
  283. nslicesz = nslicesx;
  284. }
  285. else if (strcmp(argv[i], "-nblocksx") == 0)
  286. {
  287. char *argptr;
  288. nslicesx = strtol(argv[++i], &argptr, 10);
  289. }
  290. else if (strcmp(argv[i], "-nblocksy") == 0)
  291. {
  292. char *argptr;
  293. nslicesy = strtol(argv[++i], &argptr, 10);
  294. }
  295. else if (strcmp(argv[i], "-nblocksz") == 0)
  296. {
  297. char *argptr;
  298. nslicesz = strtol(argv[++i], &argptr, 10);
  299. }
  300. else if (strcmp(argv[i], "-x") == 0)
  301. {
  302. char *argptr;
  303. xdim = strtol(argv[++i], &argptr, 10);
  304. }
  305. else if (strcmp(argv[i], "-xy") == 0)
  306. {
  307. char *argptr;
  308. xdim = ydim = strtol(argv[++i], &argptr, 10);
  309. }
  310. else if (strcmp(argv[i], "-y") == 0)
  311. {
  312. char *argptr;
  313. ydim = strtol(argv[++i], &argptr, 10);
  314. }
  315. else if (strcmp(argv[i], "-z") == 0)
  316. {
  317. char *argptr;
  318. zdim = strtol(argv[++i], &argptr, 10);
  319. }
  320. else if (strcmp(argv[i], "-size") == 0)
  321. {
  322. char *argptr;
  323. xdim = ydim = zdim = strtol(argv[++i], &argptr, 10);
  324. }
  325. else if (strcmp(argv[i], "-iter") == 0)
  326. {
  327. char *argptr;
  328. niter = strtol(argv[++i], &argptr, 10);
  329. }
  330. else if (strcmp(argv[i], "-nsleeps") == 0)
  331. {
  332. char *argptr;
  333. nsleeps = strtol(argv[++i], &argptr, 10);
  334. }
  335. else if (strcmp(argv[i], "-bound") == 0)
  336. {
  337. bound = 1;
  338. }
  339. else if (strcmp(argv[i], "-hostname") == 0)
  340. {
  341. print_hostname = 1;
  342. }
  343. else if (strcmp(argv[i], "-check") == 0)
  344. {
  345. check = 1;
  346. }
  347. else if (strcmp(argv[i], "-spmd") == 0)
  348. {
  349. cl_gemm0.type = STARPU_SPMD;
  350. }
  351. else if (strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
  352. {
  353. fprintf(stderr,"Usage: %s [-3d] [-nblocks n] [-nblocksx x] [-nblocksy y] [-nblocksz z] [-x x] [-y y] [-xy n] [-z z] [-size size] [-iter iter] [-bound] [-check] [-spmd] [-hostname] [-nsleeps nsleeps]\n", argv[0]);
  354. if (tiled)
  355. fprintf(stderr,"Currently selected: %ux%u * %ux%u and %ux%ux%u blocks, %u iterations, %u sleeps\n", zdim, ydim, xdim, zdim, nslicesx, nslicesy, nslicesz, niter, nsleeps);
  356. else
  357. 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);
  358. exit(EXIT_SUCCESS);
  359. }
  360. else
  361. {
  362. fprintf(stderr,"Unrecognized option %s\n", argv[i]);
  363. exit(EXIT_FAILURE);
  364. }
  365. }
  366. }
  367. int main(int argc, char **argv)
  368. {
  369. double start, end;
  370. int ret;
  371. parse_args(argc, argv);
  372. #ifdef STARPU_QUICK_CHECK
  373. niter /= 10;
  374. #endif
  375. starpu_fxt_autostart_profiling(0);
  376. ret = starpu_init(NULL);
  377. if (ret == -ENODEV)
  378. return 77;
  379. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  380. starpu_cublas_init();
  381. init_problem_data();
  382. partition_mult_data();
  383. PRINTF("# ");
  384. if (print_hostname)
  385. PRINTF("node\t");
  386. PRINTF("x\ty\tz\tms\tGFlops");
  387. if (bound)
  388. PRINTF("\tTms\tTGFlops\tTims\tTiGFlops");
  389. PRINTF("\n");
  390. unsigned sleeps;
  391. for (sleeps = 0; sleeps < nsleeps; sleeps++)
  392. {
  393. if (bound)
  394. starpu_bound_start(0, 0);
  395. starpu_fxt_start_profiling();
  396. start = starpu_timing_now();
  397. unsigned x, y, z, iter;
  398. for (iter = 0; iter < niter; iter++)
  399. {
  400. if (tiled)
  401. {
  402. for (x = 0; x < nslicesx; x++)
  403. for (y = 0; y < nslicesy; y++)
  404. {
  405. starpu_data_handle_t Ctile = starpu_data_get_sub_data(C_handle, 2, x, y);
  406. for (z = 0; z < nslicesz; z++)
  407. {
  408. struct starpu_task *task = starpu_task_create();
  409. if (z == 0)
  410. task->cl = &cl_gemm0;
  411. else
  412. task->cl = &cl_gemm;
  413. task->handles[0] = starpu_data_get_sub_data(A_handle, 2, z, y);
  414. task->handles[1] = starpu_data_get_sub_data(B_handle, 2, x, z);
  415. task->handles[2] = Ctile;
  416. task->flops = 2ULL * (xdim/nslicesx) * (ydim/nslicesy) * (zdim/nslicesz);
  417. ret = starpu_task_submit(task);
  418. if (ret == -ENODEV)
  419. {
  420. check = 0;
  421. ret = 77;
  422. goto enodev;
  423. }
  424. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  425. }
  426. starpu_data_wont_use(Ctile);
  427. }
  428. }
  429. else
  430. {
  431. for (x = 0; x < nslicesx; x++)
  432. for (y = 0; y < nslicesy; y++)
  433. {
  434. struct starpu_task *task = starpu_task_create();
  435. task->cl = &cl_gemm0;
  436. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, y);
  437. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, x);
  438. task->handles[2] = starpu_data_get_sub_data(C_handle, 2, x, y);
  439. task->flops = 2ULL * (xdim/nslicesx) * (ydim/nslicesy) * zdim;
  440. ret = starpu_task_submit(task);
  441. if (ret == -ENODEV)
  442. {
  443. check = 0;
  444. ret = 77;
  445. goto enodev;
  446. }
  447. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  448. starpu_data_wont_use(starpu_data_get_sub_data(C_handle, 2, x, y));
  449. }
  450. }
  451. starpu_task_wait_for_all();
  452. }
  453. end = starpu_timing_now();
  454. starpu_fxt_stop_profiling();
  455. if (bound)
  456. starpu_bound_stop();
  457. double timing = end - start;
  458. double min, min_int;
  459. double flops = 2.0*((unsigned long long)niter)*((unsigned long long)xdim)
  460. *((unsigned long long)ydim)*((unsigned long long)zdim);
  461. if (bound)
  462. starpu_bound_compute(&min, &min_int, 1);
  463. if (print_hostname)
  464. {
  465. char hostname[255];
  466. gethostname(hostname, 255);
  467. PRINTF("%s\t", hostname);
  468. }
  469. PRINTF("%u\t%u\t%u\t%.0f\t%.1f", xdim, ydim, zdim, timing/niter/1000.0, flops/timing/1000.0);
  470. if (bound)
  471. PRINTF("\t%.0f\t%.1f\t%.0f\t%.1f", min, flops/min/1000000.0, min_int, flops/min_int/1000000.0);
  472. PRINTF("\n");
  473. if (sleeps < nsleeps-1)
  474. {
  475. sleep(10);
  476. }
  477. }
  478. enodev:
  479. starpu_data_unpartition(C_handle, STARPU_MAIN_RAM);
  480. starpu_data_unpartition(B_handle, STARPU_MAIN_RAM);
  481. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  482. starpu_data_unregister(A_handle);
  483. starpu_data_unregister(B_handle);
  484. starpu_data_unregister(C_handle);
  485. #ifndef STARPU_SIMGRID
  486. if (check)
  487. ret = check_output();
  488. #endif
  489. starpu_free_flags(A, zdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  490. starpu_free_flags(B, xdim*zdim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  491. starpu_free_flags(C, xdim*ydim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  492. starpu_cublas_shutdown();
  493. starpu_shutdown();
  494. return ret;
  495. }