dw_mult.c 7.7 KB

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