xgemm.c 8.4 KB

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