xgemm.c 11 KB

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