xgemm.c 9.7 KB

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