xgemm.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_matrix_filter_vertical_block;
  106. vert.nchildren = nslicesx;
  107. struct starpu_data_filter horiz;
  108. memset(&horiz, 0, sizeof(horiz));
  109. horiz.filter_func = starpu_matrix_filter_block;
  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. .type = STARPU_SEQ, /* changed to STARPU_SPMD if -spmd is passed */
  173. .max_parallelism = INT_MAX,
  174. .cpu_funcs = {cpu_mult, NULL},
  175. #ifdef STARPU_USE_CUDA
  176. .cuda_funcs = {cublas_mult, NULL},
  177. #endif
  178. .nbuffers = 3,
  179. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  180. .model = &starpu_gemm_model
  181. };
  182. static void parse_args(int argc, char **argv)
  183. {
  184. int i;
  185. for (i = 1; i < argc; i++)
  186. {
  187. if (strcmp(argv[i], "-nblocks") == 0)
  188. {
  189. char *argptr;
  190. nslicesx = strtol(argv[++i], &argptr, 10);
  191. nslicesy = nslicesx;
  192. }
  193. else if (strcmp(argv[i], "-nblocksx") == 0)
  194. {
  195. char *argptr;
  196. nslicesx = strtol(argv[++i], &argptr, 10);
  197. }
  198. else if (strcmp(argv[i], "-nblocksy") == 0)
  199. {
  200. char *argptr;
  201. nslicesy = strtol(argv[++i], &argptr, 10);
  202. }
  203. else if (strcmp(argv[i], "-x") == 0)
  204. {
  205. char *argptr;
  206. xdim = strtol(argv[++i], &argptr, 10);
  207. }
  208. else if (strcmp(argv[i], "-y") == 0)
  209. {
  210. char *argptr;
  211. ydim = strtol(argv[++i], &argptr, 10);
  212. }
  213. else if (strcmp(argv[i], "-z") == 0)
  214. {
  215. char *argptr;
  216. zdim = strtol(argv[++i], &argptr, 10);
  217. }
  218. else if (strcmp(argv[i], "-iter") == 0)
  219. {
  220. char *argptr;
  221. niter = strtol(argv[++i], &argptr, 10);
  222. }
  223. else if (strcmp(argv[i], "-check") == 0)
  224. {
  225. check = 1;
  226. }
  227. else if (strcmp(argv[i], "-spmd") == 0)
  228. {
  229. cl.type = STARPU_SPMD;
  230. }
  231. else if (strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
  232. {
  233. fprintf(stderr,"Usage: %s [-nblocks n] [-nblocksx x] [-nblocksy y] [-x x] [-y y] [-z z] [-iter iter] [-check] [-spmd]\n", argv[0]);
  234. fprintf(stderr,"Currently selected: %ux%u * %ux%u and %ux%u blocks, %u iterations\n", zdim, ydim, xdim, zdim, nslicesx, nslicesy, niter);
  235. exit(EXIT_SUCCESS);
  236. }
  237. }
  238. }
  239. int main(int argc, char **argv)
  240. {
  241. double start, end;
  242. int ret;
  243. parse_args(argc, argv);
  244. #ifdef STARPU_QUICK_CHECK
  245. niter /= 10;
  246. #endif
  247. ret = starpu_init(NULL);
  248. if (ret == -ENODEV)
  249. return 77;
  250. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  251. starpu_helper_cublas_init();
  252. init_problem_data();
  253. partition_mult_data();
  254. start = starpu_timing_now();
  255. unsigned x, y, iter;
  256. for (iter = 0; iter < niter; iter++)
  257. {
  258. for (x = 0; x < nslicesx; x++)
  259. for (y = 0; y < nslicesy; y++)
  260. {
  261. struct starpu_task *task = starpu_task_create();
  262. task->cl = &cl;
  263. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, y);
  264. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, x);
  265. task->handles[2] = starpu_data_get_sub_data(C_handle, 2, x, y);
  266. ret = starpu_task_submit(task);
  267. if (ret == -ENODEV)
  268. {
  269. ret = 77;
  270. goto enodev;
  271. }
  272. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  273. }
  274. starpu_task_wait_for_all();
  275. }
  276. end = starpu_timing_now();
  277. double timing = end - start;
  278. FPRINTF(stderr, "Time: %2.2f ms\n", timing/1000.0);
  279. double flops = 2.0*((unsigned long)niter)*((unsigned long)xdim)
  280. *((unsigned long)ydim)*((unsigned long)zdim);
  281. FPRINTF(stderr, "GFlop/s: %.2f\n", flops/timing/1000.0);
  282. enodev:
  283. starpu_data_unpartition(C_handle, 0);
  284. starpu_data_unpartition(B_handle, 0);
  285. starpu_data_unpartition(A_handle, 0);
  286. starpu_data_unregister(A_handle);
  287. starpu_data_unregister(B_handle);
  288. starpu_data_unregister(C_handle);
  289. if (check)
  290. check_output();
  291. starpu_free(A);
  292. starpu_free(B);
  293. starpu_free(C);
  294. starpu_helper_cublas_shutdown();
  295. starpu_shutdown();
  296. return ret;
  297. }