dw_mult.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #ifdef HAVE_POSIX_MEMALIGN
  134. posix_memalign((void **)&A, 4096, zdim*ydim*sizeof(float));
  135. posix_memalign((void **)&B, 4096, xdim*zdim*sizeof(float));
  136. posix_memalign((void **)&C, 4096, xdim*ydim*sizeof(float));
  137. #else
  138. A = malloc(zdim*ydim*sizeof(float));
  139. B = malloc(xdim*zdim*sizeof(float));
  140. C = malloc(xdim*ydim*sizeof(float));
  141. #endif
  142. }
  143. /* fill the A and B matrices */
  144. if (norandom) {
  145. for (j=0; j < ydim; j++) {
  146. for (i=0; i < zdim; i++) {
  147. A[j+i*ydim] = (float)(i);
  148. }
  149. }
  150. for (j=0; j < zdim; j++) {
  151. for (i=0; i < xdim; i++) {
  152. B[j+i*zdim] = (float)(j);
  153. }
  154. }
  155. }
  156. else {
  157. #ifdef NORANDOM
  158. srand(2008);
  159. STARPU_ASSERT(0);
  160. #endif
  161. for (j=0; j < ydim; j++) {
  162. for (i=0; i < zdim; i++) {
  163. A[j+i*ydim] = (float)(drand48());
  164. }
  165. }
  166. for (j=0; j < zdim; j++) {
  167. for (i=0; i < xdim; i++) {
  168. B[j+i*zdim] = (float)(drand48());
  169. }
  170. }
  171. }
  172. for (j=0; j < ydim; j++) {
  173. for (i=0; i < xdim; i++) {
  174. C[j+i*ydim] = (float)(0);
  175. }
  176. }
  177. display_memory_consumption();
  178. }
  179. static void partition_mult_data(void)
  180. {
  181. gettimeofday(&start, NULL);
  182. starpu_monitor_blas_data(&A_state, 0, (uintptr_t)A,
  183. ydim, ydim, zdim, sizeof(float));
  184. starpu_monitor_blas_data(&B_state, 0, (uintptr_t)B,
  185. zdim, zdim, xdim, sizeof(float));
  186. starpu_monitor_blas_data(&C_state, 0, (uintptr_t)C,
  187. ydim, ydim, xdim, sizeof(float));
  188. conf.k = zdim;
  189. conf.m = ydim/nslicesy;
  190. conf.n = xdim/nslicesx;
  191. starpu_filter f;
  192. f.filter_func = starpu_vertical_block_filter_func;
  193. f.filter_arg = nslicesx;
  194. starpu_filter f2;
  195. f2.filter_func = starpu_block_filter_func;
  196. f2.filter_arg = nslicesy;
  197. starpu_partition_data(B_state, &f);
  198. starpu_partition_data(A_state, &f2);
  199. starpu_map_filters(C_state, 2, &f, &f2);
  200. }
  201. static void launch_codelets(void)
  202. {
  203. #ifdef USE_FXT
  204. fxt_register_thread(0);
  205. #endif
  206. /* partition the work into slices */
  207. unsigned taskx, tasky;
  208. taskcounter = nslicesx * nslicesy;
  209. srand(time(NULL));
  210. starpu_codelet cl = {
  211. .where = CORE|CUBLAS|GORDON,
  212. .core_func = core_mult,
  213. #ifdef USE_CUDA
  214. .cublas_func = cublas_mult,
  215. #endif
  216. #ifdef USE_GORDON
  217. .gordon_func = SPU_FUNC_SGEMM,
  218. #endif
  219. .nbuffers = 3
  220. };
  221. for (taskx = 0; taskx < nslicesx; taskx++)
  222. {
  223. for (tasky = 0; tasky < nslicesy; tasky++)
  224. {
  225. /* A B[task] = C[task] */
  226. struct starpu_task *task = starpu_task_create();
  227. task->cl = &cl;
  228. task->cl_arg = &conf;
  229. task->cl_arg_size = sizeof(struct block_conf);
  230. task->callback_func = callback_func;
  231. task->callback_arg = &taskcounter;
  232. starpu_tag_t tag =
  233. ((((unsigned long long)(taskx))<<32)
  234. | (unsigned long long)(tasky));
  235. task->use_tag = 1;
  236. task->tag_id = tag;
  237. task->buffers[0].state = get_sub_data(A_state, 1, tasky);
  238. task->buffers[0].mode = R;
  239. task->buffers[1].state = get_sub_data(B_state, 1, taskx);
  240. task->buffers[1].mode = R;
  241. task->buffers[2].state =
  242. get_sub_data(C_state, 2, taskx, tasky);
  243. task->buffers[2].mode = RW;
  244. if (use_common_model)
  245. {
  246. task->cl->model = &sgemm_model_common;
  247. }
  248. else
  249. {
  250. task->cl->model = &sgemm_model;
  251. }
  252. starpu_submit_task(task);
  253. }
  254. }
  255. }
  256. int main(__attribute__ ((unused)) int argc,
  257. __attribute__ ((unused)) char **argv)
  258. {
  259. parse_args(argc, argv);
  260. /* start the runtime */
  261. starpu_init();
  262. sem_init(&sem, 0, 0U);
  263. init_problem_data();
  264. partition_mult_data();
  265. launch_codelets();
  266. sem_wait(&sem);
  267. sem_destroy(&sem);
  268. starpu_shutdown();
  269. return 0;
  270. }