xgemm.c 9.5 KB

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