xgemm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "dw_mult.h"
  17. TYPE *A, *B, *C;
  18. starpu_data_handle A_handle, B_handle, C_handle;
  19. /*
  20. * This program computes C = A * B
  21. *
  22. * A of size (z,y)
  23. * B of size (x,z)
  24. * C of size (x,y)
  25. |---------------|
  26. z | B |
  27. |---------------|
  28. z x
  29. |----| |---------------|
  30. | | | |
  31. | | | |
  32. | A | y | C |
  33. | | | |
  34. | | | |
  35. |----| |---------------|
  36. */
  37. static void check_output(void)
  38. {
  39. /* check results */
  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. void callback_func(void *arg)
  56. {
  57. /* do some accounting */
  58. int id = starpu_worker_get_id();
  59. flop_per_worker[id] += BLAS3_FLOP(conf.m, conf.n, conf.k);
  60. ls_per_worker[id] += BLAS3_LS(conf.m, conf.n, conf.k);
  61. }
  62. static void init_problem_data(void)
  63. {
  64. unsigned i,j;
  65. #ifdef STARPU_USE_CUDA
  66. if (pin) {
  67. starpu_data_malloc_pinned_if_possible((void **)&A, zdim*ydim*sizeof(TYPE));
  68. starpu_data_malloc_pinned_if_possible((void **)&B, xdim*zdim*sizeof(TYPE));
  69. starpu_data_malloc_pinned_if_possible((void **)&C, xdim*ydim*sizeof(TYPE));
  70. } else
  71. #endif
  72. {
  73. #ifdef STARPU_HAVE_POSIX_MEMALIGN
  74. posix_memalign((void **)&A, 4096, zdim*ydim*sizeof(TYPE));
  75. posix_memalign((void **)&B, 4096, xdim*zdim*sizeof(TYPE));
  76. posix_memalign((void **)&C, 4096, xdim*ydim*sizeof(TYPE));
  77. #else
  78. A = malloc(zdim*ydim*sizeof(TYPE));
  79. B = malloc(xdim*zdim*sizeof(TYPE));
  80. C = malloc(xdim*ydim*sizeof(TYPE));
  81. #endif
  82. }
  83. /* fill the A and B matrices */
  84. if (norandom) {
  85. for (j=0; j < ydim; j++) {
  86. for (i=0; i < zdim; i++) {
  87. A[j+i*ydim] = (TYPE)(i);
  88. }
  89. }
  90. for (j=0; j < zdim; j++) {
  91. for (i=0; i < xdim; i++) {
  92. B[j+i*zdim] = (TYPE)(j);
  93. }
  94. }
  95. }
  96. else {
  97. #ifdef NORANDOM
  98. srand(2008);
  99. STARPU_ABORT();
  100. #endif
  101. for (j=0; j < ydim; j++) {
  102. for (i=0; i < zdim; i++) {
  103. A[j+i*ydim] = (TYPE)(starpu_drand48());
  104. }
  105. }
  106. for (j=0; j < zdim; j++) {
  107. for (i=0; i < xdim; i++) {
  108. B[j+i*zdim] = (TYPE)(starpu_drand48());
  109. }
  110. }
  111. }
  112. for (j=0; j < ydim; j++) {
  113. for (i=0; i < xdim; i++) {
  114. C[j+i*ydim] = (TYPE)(0);
  115. }
  116. }
  117. display_memory_consumption();
  118. }
  119. static void partition_mult_data(void)
  120. {
  121. starpu_matrix_data_register(&A_handle, 0, (uintptr_t)A,
  122. ydim, ydim, zdim, sizeof(TYPE));
  123. starpu_matrix_data_register(&B_handle, 0, (uintptr_t)B,
  124. zdim, zdim, xdim, sizeof(TYPE));
  125. starpu_matrix_data_register(&C_handle, 0, (uintptr_t)C,
  126. ydim, ydim, xdim, sizeof(TYPE));
  127. starpu_data_set_wt_mask(C_handle, 1<<0);
  128. conf.k = zdim;
  129. conf.m = ydim/nslicesy;
  130. conf.n = xdim/nslicesx;
  131. struct starpu_data_filter f;
  132. f.filter_func = starpu_vertical_block_filter_func;
  133. f.nchildren = nslicesx;
  134. f.get_nchildren = NULL;
  135. f.get_child_ops = NULL;
  136. struct starpu_data_filter f2;
  137. f2.filter_func = starpu_block_filter_func;
  138. f2.nchildren = nslicesy;
  139. f2.get_nchildren = NULL;
  140. f2.get_child_ops = NULL;
  141. starpu_data_partition(B_handle, &f);
  142. starpu_data_partition(A_handle, &f2);
  143. starpu_data_map_filters(C_handle, 2, &f, &f2);
  144. }
  145. static void unpartition_mult_data(void)
  146. {
  147. fprintf(stderr, "unpartition !!\n");
  148. starpu_data_unpartition(C_handle, 0);
  149. starpu_data_unregister(C_handle);
  150. }
  151. static starpu_codelet cl = {
  152. .where = STARPU_CPU|STARPU_CUDA
  153. #ifdef SPU_FUNC_SGEMM
  154. |STARPU_GORDON
  155. #endif
  156. ,
  157. .cpu_func = STARPU_GEMM(cpu_mult),
  158. #ifdef STARPU_USE_CUDA
  159. .cuda_func = STARPU_GEMM(cublas_mult),
  160. #endif
  161. #ifdef STARPU_USE_GORDON
  162. #ifdef SPU_FUNC_SGEMM
  163. .gordon_func = SPU_FUNC_SGEMM,
  164. #else
  165. #warning SPU_FUNC_SGEMM is not available
  166. #endif
  167. #endif
  168. .nbuffers = 3
  169. };
  170. static struct starpu_task *construct_task(unsigned x, unsigned y, unsigned z, unsigned iter)
  171. {
  172. /* A B[task] = C[task] */
  173. struct starpu_task *task = starpu_task_create();
  174. task->cl = &cl;
  175. /* we have a callback to do some accounting */
  176. task->callback_func = callback_func;
  177. task->callback_arg = NULL;
  178. task->buffers[0].handle = starpu_data_get_sub_data(A_handle, 1, y);
  179. task->buffers[0].mode = STARPU_R;
  180. task->buffers[1].handle = starpu_data_get_sub_data(B_handle, 1, x);
  181. task->buffers[1].mode = STARPU_R;
  182. task->buffers[2].handle = starpu_data_get_sub_data(C_handle, 2, x, y);
  183. task->buffers[2].mode = STARPU_RW;
  184. task->cl_arg = &conf;
  185. task->cl_arg_size = sizeof(struct block_conf);
  186. return task;
  187. }
  188. static void submit_new_iter(unsigned x, unsigned y, unsigned iter)
  189. {
  190. unsigned z;
  191. z = 0;
  192. {
  193. struct starpu_task *task;
  194. task = construct_task(x, y, z, iter);
  195. starpu_task_submit(task);
  196. }
  197. }
  198. static void launch_codelets(void)
  199. {
  200. #ifdef STARPU_USE_FXT
  201. _starpu_fxt_register_thread(0);
  202. #endif
  203. /* partition the work into slices */
  204. unsigned taskx, tasky;
  205. srand(time(NULL));
  206. /* should we use a single performance model for all archs and use an
  207. * acceleration factor ? */
  208. if (use_common_model) {
  209. cl.model = &STARPU_GEMM(model_common);
  210. }
  211. else {
  212. cl.model = &STARPU_GEMM(model);
  213. }
  214. for (taskx = 0; taskx < nslicesx; taskx++)
  215. {
  216. for (tasky = 0; tasky < nslicesy; tasky++)
  217. {
  218. submit_new_iter(taskx, tasky, 0);
  219. }
  220. }
  221. }
  222. int main(__attribute__ ((unused)) int argc,
  223. __attribute__ ((unused)) char **argv)
  224. {
  225. parse_args(argc, argv);
  226. /* start the runtime */
  227. starpu_init(NULL);
  228. starpu_helper_cublas_init();
  229. init_problem_data();
  230. gettimeofday(&start, NULL);
  231. partition_mult_data();
  232. launch_codelets();
  233. starpu_task_wait_for_all();
  234. gettimeofday(&end, NULL);
  235. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  236. display_stats(timing);
  237. unpartition_mult_data();
  238. if (check)
  239. check_output();
  240. starpu_helper_cublas_shutdown();
  241. starpu_shutdown();
  242. return 0;
  243. }