xgemm.c 7.6 KB

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