dw_mult.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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. float *A, *B, *C;
  18. starpu_data_handle A_state, B_state, C_state;
  19. /*
  20. * That program should compute 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. void terminate(void)
  38. {
  39. fprintf(stderr, "unpartition !!\n");
  40. starpu_unpartition_data(C_state, 0);
  41. starpu_delete_data(C_state);
  42. gettimeofday(&end, NULL);
  43. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  44. uint64_t total_flop = BLAS3_FLOP(ydim, xdim, zdim);
  45. uint64_t total_ls = ls_cublas + ls_atlas;
  46. fprintf(stderr, "Computation took (ms):\n");
  47. printf("%2.2f\n", timing/1000);
  48. fprintf(stderr, " GFlop : total (%2.2f) cublas (%2.2f) atlas (%2.2f)\n", (double)total_flop/1000000000.0f, (double)flop_cublas/1000000000.0f, (double)flop_atlas/1000000000.0f);
  49. fprintf(stderr, " GFlop/s : %2.2f\n", (double)total_flop / (double)timing/1000);
  50. fprintf(stderr, " GB : total (%2.2f) cublas (%2.2f) atlas (%2.2f)\n", (double)total_ls/1000000000.0f, (double)ls_cublas/1000000000.0f, (double)ls_atlas/1000000000.0f);
  51. fprintf(stderr, " GB/s : %2.2f\n", (double)total_ls / (double)timing/1000);
  52. #ifdef CHECK_OUTPUT
  53. /* check results */
  54. /* compute C = C - AB */
  55. SGEMM("N", "N", ydim, xdim, zdim, -1.0f, A, ydim, B, zdim, 1.0f, C, ydim);
  56. /* make sure C = 0 */
  57. float err;
  58. err = SASUM(xdim*ydim, C, 1);
  59. if (err < xdim*ydim*0.001) {
  60. fprintf(stderr, "Results are OK\n");
  61. }
  62. else {
  63. fprintf(stderr, "There were errors ... err = %f\n", err);
  64. }
  65. #endif // CHECK_OUTPUT
  66. sem_post(&sem);
  67. }
  68. void callback_func(void *arg)
  69. {
  70. /* the argument is a pointer to a counter of the remaining tasks */
  71. int *counterptr = arg;
  72. int counter = STARPU_ATOMIC_ADD(counterptr, -1);
  73. if (counter == 0)
  74. {
  75. /* we are done */
  76. fprintf(stderr, "done ...\n");
  77. terminate();
  78. }
  79. return;
  80. }
  81. #define COMMON_CODE \
  82. uint32_t nxC, nyC, nyA; \
  83. uint32_t ldA, ldB, ldC; \
  84. \
  85. float *subA; \
  86. float *subB; \
  87. float *subC; \
  88. \
  89. subA = (float *)descr[0].blas.ptr; \
  90. subB = (float *)descr[1].blas.ptr; \
  91. subC = (float *)descr[2].blas.ptr; \
  92. \
  93. nxC = descr[2].blas.nx; \
  94. nyC = descr[2].blas.ny; \
  95. nyA = descr[0].blas.ny; \
  96. \
  97. ldA = descr[0].blas.ld; \
  98. ldB = descr[1].blas.ld; \
  99. ldC = descr[2].blas.ld;
  100. #ifdef USE_CUDA
  101. void cublas_mult(starpu_data_interface_t *descr, __attribute__((unused)) void *arg)
  102. {
  103. COMMON_CODE
  104. cublasSgemm('n', 'n', nxC, nyC, nyA, 1.0f, subA, ldA, subB, ldB,
  105. 0.0f, subC, ldC);
  106. cublasStatus st;
  107. st = cublasGetError();
  108. if (st != CUBLAS_STATUS_SUCCESS)
  109. STARPU_ASSERT(0);
  110. uint64_t flopcnt = BLAS3_FLOP(nyC, nxC, nyA);
  111. flop_cublas += flopcnt;
  112. ls_cublas += BLAS3_LS(nyC, nxC, nyA);
  113. }
  114. #endif
  115. void core_mult(starpu_data_interface_t *descr, __attribute__((unused)) void *arg)
  116. {
  117. COMMON_CODE
  118. SGEMM("N", "N", nxC, nyC, nyA, 1.0f, subA, ldA, subB, ldB, 0.0f, subC, ldC);
  119. flop_atlas += BLAS3_FLOP(nxC, nyC, nyA);
  120. ls_atlas += BLAS3_LS(nxC, nyC, nyA);
  121. }
  122. static void init_problem_data(void)
  123. {
  124. unsigned i,j;
  125. #ifdef USE_CUDA
  126. if (pin) {
  127. starpu_malloc_pinned_if_possible(&A, zdim*ydim*sizeof(float));
  128. starpu_malloc_pinned_if_possible(&B, xdim*zdim*sizeof(float));
  129. starpu_malloc_pinned_if_possible(&C, xdim*ydim*sizeof(float));
  130. } else
  131. #endif
  132. {
  133. posix_memalign((void **)&A, 4096, zdim*ydim*sizeof(float));
  134. posix_memalign((void **)&B, 4096, xdim*zdim*sizeof(float));
  135. posix_memalign((void **)&C, 4096, xdim*ydim*sizeof(float));
  136. }
  137. /* fill the A and B matrices */
  138. if (norandom) {
  139. for (j=0; j < ydim; j++) {
  140. for (i=0; i < zdim; i++) {
  141. A[j+i*ydim] = (float)(i);
  142. }
  143. }
  144. for (j=0; j < zdim; j++) {
  145. for (i=0; i < xdim; i++) {
  146. B[j+i*zdim] = (float)(j);
  147. }
  148. }
  149. }
  150. else {
  151. #ifdef NORANDOM
  152. srand(2008);
  153. STARPU_ASSERT(0);
  154. #endif
  155. for (j=0; j < ydim; j++) {
  156. for (i=0; i < zdim; i++) {
  157. A[j+i*ydim] = (float)(drand48());
  158. }
  159. }
  160. for (j=0; j < zdim; j++) {
  161. for (i=0; i < xdim; i++) {
  162. B[j+i*zdim] = (float)(drand48());
  163. }
  164. }
  165. }
  166. for (j=0; j < ydim; j++) {
  167. for (i=0; i < xdim; i++) {
  168. C[j+i*ydim] = (float)(0);
  169. }
  170. }
  171. display_memory_consumption();
  172. }
  173. static void partition_mult_data(void)
  174. {
  175. gettimeofday(&start, NULL);
  176. starpu_monitor_blas_data(&A_state, 0, (uintptr_t)A,
  177. ydim, ydim, zdim, sizeof(float));
  178. starpu_monitor_blas_data(&B_state, 0, (uintptr_t)B,
  179. zdim, zdim, xdim, sizeof(float));
  180. starpu_monitor_blas_data(&C_state, 0, (uintptr_t)C,
  181. ydim, ydim, xdim, sizeof(float));
  182. conf.k = zdim;
  183. conf.m = ydim/nslicesy;
  184. conf.n = xdim/nslicesx;
  185. starpu_filter f;
  186. f.filter_func = starpu_vertical_block_filter_func;
  187. f.filter_arg = nslicesx;
  188. starpu_filter f2;
  189. f2.filter_func = starpu_block_filter_func;
  190. f2.filter_arg = nslicesy;
  191. starpu_partition_data(B_state, &f);
  192. starpu_partition_data(A_state, &f2);
  193. starpu_map_filters(C_state, 2, &f, &f2);
  194. }
  195. static void launch_codelets(void)
  196. {
  197. #ifdef USE_FXT
  198. fxt_register_thread(0);
  199. #endif
  200. /* partition the work into slices */
  201. unsigned taskx, tasky;
  202. taskcounter = nslicesx * nslicesy;
  203. srand(time(NULL));
  204. starpu_codelet cl = {
  205. .where = CORE|CUBLAS|GORDON,
  206. .core_func = core_mult,
  207. #ifdef USE_CUDA
  208. .cublas_func = cublas_mult,
  209. #endif
  210. #ifdef USE_GORDON
  211. .gordon_func = SPU_FUNC_SGEMM,
  212. #endif
  213. .nbuffers = 3
  214. };
  215. for (taskx = 0; taskx < nslicesx; taskx++)
  216. {
  217. for (tasky = 0; tasky < nslicesy; tasky++)
  218. {
  219. /* A B[task] = C[task] */
  220. struct starpu_task *task = starpu_task_create();
  221. task->cl = &cl;
  222. task->cl_arg = &conf;
  223. task->cl_arg_size = sizeof(struct block_conf);
  224. task->callback_func = callback_func;
  225. task->callback_arg = &taskcounter;
  226. starpu_tag_t tag =
  227. ((((unsigned long long)(taskx))<<32)
  228. | (unsigned long long)(tasky));
  229. task->use_tag = 1;
  230. task->tag_id = tag;
  231. task->buffers[0].state = get_sub_data(A_state, 1, tasky);
  232. task->buffers[0].mode = R;
  233. task->buffers[1].state = get_sub_data(B_state, 1, taskx);
  234. task->buffers[1].mode = R;
  235. task->buffers[2].state =
  236. get_sub_data(C_state, 2, taskx, tasky);
  237. task->buffers[2].mode = RW;
  238. if (use_common_model)
  239. {
  240. task->cl->model = &sgemm_model_common;
  241. }
  242. else
  243. {
  244. task->cl->model = &sgemm_model;
  245. }
  246. starpu_submit_task(task);
  247. }
  248. }
  249. }
  250. int main(__attribute__ ((unused)) int argc,
  251. __attribute__ ((unused)) char **argv)
  252. {
  253. parse_args(argc, argv);
  254. /* start the runtime */
  255. starpu_init();
  256. sem_init(&sem, 0, 0U);
  257. init_problem_data();
  258. partition_mult_data();
  259. launch_codelets();
  260. sem_wait(&sem);
  261. sem_destroy(&sem);
  262. starpu_shutdown();
  263. return 0;
  264. }