xgemm.c 7.9 KB

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