xgemm.c 7.4 KB

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