xgemm.c 8.5 KB

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