dw_mult.c 7.8 KB

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