dw_mult_no_stride.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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[MAXSLICESY][MAXSLICESZ];
  18. float *B[MAXSLICESZ][MAXSLICESX];
  19. float *C[MAXSLICESY][MAXSLICESX];
  20. starpu_data_handle A_state[MAXSLICESY][MAXSLICESZ];
  21. starpu_data_handle B_state[MAXSLICESZ][MAXSLICESX];
  22. starpu_data_handle C_state[MAXSLICESY][MAXSLICESX];
  23. /* fortran ordering ... */
  24. #define FULLA(i,j) \
  25. (A[(i)/BLOCKSIZEY][(j)/BLOCKSIZEZ][(i)%BLOCKSIZEY + ((j)%BLOCKSIZEZ)*BLOCKSIZEY])
  26. #define FULLB(i,j) \
  27. (B[(i)/BLOCKSIZEZ][(j)/BLOCKSIZEX][(i)%BLOCKSIZEZ + ((j)%BLOCKSIZEX)*BLOCKSIZEZ])
  28. #define FULLC(i,j) \
  29. (C[(i)/BLOCKSIZEY][(j)/BLOCKSIZEX][(i)%BLOCKSIZEY + ((j)%BLOCKSIZEX)*BLOCKSIZEY])
  30. #define TAG(x,y,z,iter) \
  31. ((z) + (iter)*nslicesz + (x)*(nslicesz*niter) + (y)*(nslicesx*nslicesz*niter))
  32. static void submit_new_iter(unsigned x, unsigned y, unsigned iter);
  33. /*
  34. * That program should compute C = A * B
  35. *
  36. * A of size (z,y)
  37. * B of size (x,z)
  38. * C of size (x,y)
  39. |---------------|
  40. z | B |
  41. |---------------|
  42. z x
  43. |----| |---------------|
  44. | | | |
  45. | | | |
  46. | A | y | C |
  47. | | | |
  48. | | | |
  49. |----| |---------------|
  50. */
  51. static void terminate(void)
  52. {
  53. gettimeofday(&end, NULL);
  54. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  55. uint64_t total_flop = BLAS3_FLOP(ydim, xdim, zdim)*niter;
  56. fprintf(stderr, "Computation took (ms):\n");
  57. printf("%2.2f\n", timing/1000);
  58. 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);
  59. fprintf(stderr, " GFlop/s : %2.2f\n", (double)total_flop / (double)timing/1000);
  60. sem_post(&sem);
  61. }
  62. #define COMMON_CODE \
  63. uint32_t nxC, nyC, nyA; \
  64. uint32_t ldA, ldB, ldC; \
  65. \
  66. float *subA; \
  67. float *subB; \
  68. float *subC; \
  69. \
  70. subA = (float *)descr[0].blas.ptr; \
  71. subB = (float *)descr[1].blas.ptr; \
  72. subC = (float *)descr[2].blas.ptr; \
  73. \
  74. nxC = descr[2].blas.nx; \
  75. nyC = descr[2].blas.ny; \
  76. nyA = descr[0].blas.ny; \
  77. \
  78. ldA = descr[0].blas.ld; \
  79. ldB = descr[1].blas.ld; \
  80. ldC = descr[2].blas.ld;
  81. #ifdef USE_CUDA
  82. static void cublas_mult(starpu_data_interface_t *descr, __attribute__((unused)) void *arg)
  83. {
  84. COMMON_CODE
  85. cublasSgemm('n', 'n', nxC, nyC, nyA, 1.0f, subA, ldA, subB, ldB,
  86. 1.0f, subC, ldC);
  87. cublasStatus st;
  88. st = cublasGetError();
  89. if (st != CUBLAS_STATUS_SUCCESS)
  90. STARPU_ASSERT(0);
  91. uint64_t flopcnt = BLAS3_FLOP(nyC, nxC, nyA);
  92. flop_cublas += flopcnt;
  93. ls_cublas += BLAS3_LS(nyC, nxC, nyA);
  94. }
  95. #endif
  96. static void core_mult(starpu_data_interface_t *descr, __attribute__((unused)) void *arg)
  97. {
  98. COMMON_CODE
  99. // fprintf(stderr, "Call SGEMM : nxC %d nyC %d nyA %d subA %p ldA %d subB %p ldB %d subC %p ldC %d\n",
  100. // nxC, nyC, nyA, subA, ldA, subB, ldB, subC, ldC);
  101. SGEMM("N", "N", nxC, nyC, nyA, 1.0f, subA, ldA, subB, ldB, 1.0f, subC, ldC);
  102. flop_atlas += BLAS3_FLOP(nxC, nyC, nyA);
  103. ls_atlas += BLAS3_LS(nxC, nyC, nyA);
  104. }
  105. #define MEM_ALIGNMENT 16
  106. static void init_problem_data(void)
  107. {
  108. unsigned i,j;
  109. /* debug ... */
  110. memset(A, 0, MAXSLICESY*MAXSLICESZ*sizeof(float *));
  111. memset(B, 0, MAXSLICESZ*MAXSLICESZ*sizeof(float *));
  112. memset(C, 0, MAXSLICESY*MAXSLICESX*sizeof(float *));
  113. memset(&A_state, 0, MAXSLICESY*MAXSLICESZ*sizeof(starpu_data_handle));
  114. memset(&B_state, 0, MAXSLICESZ*MAXSLICESZ*sizeof(starpu_data_handle));
  115. memset(&C_state, 0, MAXSLICESY*MAXSLICESX*sizeof(starpu_data_handle));
  116. /* Allocate grids of buffer */
  117. /* TODO pin ... */
  118. unsigned z, y, x;
  119. for (y = 0; y < nslicesy; y++)
  120. {
  121. for (z = 0; z < nslicesz; z++)
  122. {
  123. posix_memalign((void **)&A[y][z], MEM_ALIGNMENT, BLOCKSIZEZ*BLOCKSIZEY*sizeof(float));
  124. assert(A[y][z]);
  125. }
  126. }
  127. for (z = 0; z < nslicesz; z++)
  128. {
  129. for (x = 0; x < nslicesx; x++)
  130. {
  131. posix_memalign((void **)&B[z][x], MEM_ALIGNMENT, BLOCKSIZEX*BLOCKSIZEZ*sizeof(float));
  132. assert(B[z][x]);
  133. }
  134. }
  135. for (y = 0; y < nslicesy; y++)
  136. {
  137. for (x = 0; x < nslicesx; x++)
  138. {
  139. posix_memalign((void **)&C[y][x], MEM_ALIGNMENT, BLOCKSIZEX*BLOCKSIZEY*sizeof(float));
  140. assert(C[y][x]);
  141. }
  142. }
  143. /* fill the A and B matrices */
  144. unsigned blockx, blocky, blockz;
  145. if (norandom) {
  146. for (blocky = 0; blocky < nslicesy; blocky++)
  147. for (blockz = 0; blockz < nslicesz; blockz++)
  148. for (j = 0; j < BLOCKSIZEY; j++)
  149. for (i = 0; i < BLOCKSIZEZ; i++)
  150. {
  151. A[blocky][blockz][i*BLOCKSIZEY + j] = (float)(1 + blockz + blocky*nslicesz);
  152. }
  153. for (blockz = 0; blockz < nslicesz; blockz++)
  154. for (blockx = 0; blockx < nslicesx; blockx++)
  155. for (j = 0; j < BLOCKSIZEZ; j++)
  156. for (i = 0; i < BLOCKSIZEX; i++)
  157. {
  158. B[blockz][blockx][i*BLOCKSIZEZ + j] = (float)(1 + blockx + blockz*nslicesx);
  159. }
  160. }
  161. else {
  162. for (blocky = 0; blocky < nslicesy; blocky++)
  163. for (blockz = 0; blockz < nslicesz; blockz++)
  164. for (j = 0; j < BLOCKSIZEY; j++)
  165. for (i = 0; i < BLOCKSIZEZ; i++)
  166. {
  167. A[blocky][blockz][i*BLOCKSIZEY + j] = (float)(drand48());
  168. }
  169. for (blockz = 0; blockz < nslicesz; blockz++)
  170. for (blockx = 0; blockx < nslicesx; blockx++)
  171. for (j = 0; j < BLOCKSIZEZ; j++)
  172. for (i = 0; i < BLOCKSIZEX; i++)
  173. {
  174. B[blockz][blockx][i*BLOCKSIZEZ + j] = (float)(drand48());
  175. }
  176. }
  177. for (blocky = 0; blocky < nslicesy; blocky++)
  178. for (blockx = 0; blockx < nslicesx; blockx++)
  179. for (j = 0; j < BLOCKSIZEY; j++)
  180. for (i = 0; i < BLOCKSIZEX; i++)
  181. {
  182. C[blocky][blockx][i*BLOCKSIZEY + j] = (float)(blockx + blocky*nslicesx + 1);
  183. }
  184. /* declare the StarPU data to monitor */
  185. for (y = 0; y < nslicesy; y++)
  186. {
  187. for (z = 0; z < nslicesz; z++)
  188. {
  189. starpu_monitor_blas_data(&A_state[y][z], 0, (uintptr_t)A[y][z],
  190. BLOCKSIZEY, BLOCKSIZEY, BLOCKSIZEZ, sizeof(float));
  191. }
  192. }
  193. for (z = 0; z < nslicesz; z++)
  194. {
  195. for (x = 0; x < nslicesx; x++)
  196. {
  197. starpu_monitor_blas_data(&B_state[z][x], 0, (uintptr_t)B[z][x],
  198. BLOCKSIZEZ, BLOCKSIZEZ, BLOCKSIZEX, sizeof(float));
  199. }
  200. }
  201. for (y = 0; y < nslicesy; y++)
  202. {
  203. for (x = 0; x < nslicesx; x++)
  204. {
  205. starpu_monitor_blas_data(&C_state[y][x], 0, (uintptr_t)C[y][x],
  206. BLOCKSIZEY, BLOCKSIZEY, BLOCKSIZEX, sizeof(float));
  207. }
  208. }
  209. conf.k = BLOCKSIZEZ;
  210. conf.m = BLOCKSIZEY;
  211. conf.n = BLOCKSIZEX;
  212. display_memory_consumption();
  213. }
  214. static void cleanup_problem(void)
  215. {
  216. unsigned z, y, x;
  217. for (y = 0; y < nslicesy; y++)
  218. {
  219. for (z = 0; z < nslicesz; z++)
  220. {
  221. // free(A[y][z]);
  222. }
  223. }
  224. for (z = 0; z < nslicesz; z++)
  225. {
  226. for (x = 0; x < nslicesx; x++)
  227. {
  228. // free(B[z][x]);
  229. }
  230. }
  231. for (y = 0; y < nslicesy; y++)
  232. {
  233. for (x = 0; x < nslicesx; x++)
  234. {
  235. // free(C[y][x]);
  236. starpu_tag_remove(TAG(nslicesz - 1, y, x, niter - 1));
  237. }
  238. }
  239. }
  240. int xycounter;
  241. struct cb2_s {
  242. unsigned blockx;
  243. unsigned blocky;
  244. unsigned iter;
  245. int *xycounter;
  246. };
  247. static starpu_codelet cl = {
  248. .core_func = core_mult,
  249. #ifdef USE_CUDA
  250. .cublas_func = cublas_mult,
  251. #endif
  252. #ifdef USE_GORDON
  253. .gordon_func = SPU_FUNC_SGEMM,
  254. #endif
  255. .where = CORE|CUBLAS|GORDON,
  256. .nbuffers = 3
  257. };
  258. static struct starpu_task *construct_task(unsigned x, unsigned y, unsigned z, unsigned iter)
  259. {
  260. struct starpu_task *task = starpu_task_create();
  261. task->cl = &cl;
  262. task->cl_arg = &conf;
  263. task->cl_arg_size = sizeof(struct block_conf);
  264. task->use_tag = 1;
  265. task->tag_id = TAG(z, y, x, iter);
  266. task->buffers[0].state = A_state[y][z];
  267. task->buffers[0].mode = R;
  268. task->buffers[1].state = B_state[z][x];
  269. task->buffers[1].mode = R;
  270. task->buffers[2].state = C_state[y][x];
  271. task->buffers[2].mode = RW;
  272. return task;
  273. }
  274. static void callback_func(void *arg)
  275. {
  276. /* the argument is a pointer to a counter of the remaining tasks */
  277. int *counter = arg;
  278. int newvalue = STARPU_ATOMIC_ADD(counter, -1);
  279. if (newvalue == 0)
  280. {
  281. /* we are done */
  282. fprintf(stderr, "done ...\n");
  283. terminate();
  284. }
  285. return;
  286. }
  287. static void callback_func_2(void *arg)
  288. {
  289. /* the argument is a pointer to a counter of the remaining tasks */
  290. struct cb2_s *cb2 = arg;
  291. unsigned x,y,z,iter;
  292. iter = cb2->iter;
  293. x = cb2->blockx;
  294. y = cb2->blocky;
  295. free(cb2);
  296. // fprintf(stderr, "func 2 for x %d y %d iter %d\n", x, y, iter);
  297. /* TAG(nslicesz - 1, y, x, iter) remains ... */
  298. for (z = 0; z < nslicesz - 1; z++)
  299. {
  300. starpu_tag_remove(TAG(z, y, x, iter));
  301. }
  302. if (iter > 0)
  303. {
  304. starpu_tag_remove(TAG(nslicesz - 1, y, x, iter-1));
  305. }
  306. if (iter == niter - 1) {
  307. callback_func(&xycounter);
  308. }
  309. else {
  310. submit_new_iter(x, y, iter+1);
  311. }
  312. }
  313. static void submit_new_iter(unsigned x, unsigned y, unsigned iter)
  314. {
  315. unsigned z;
  316. for (z = 0; z < nslicesz; z++)
  317. {
  318. struct starpu_task *task;
  319. task = construct_task(x, y, z, iter);
  320. if (z != 0) {
  321. starpu_tag_declare_deps(TAG(z, y, x, iter), 1, TAG(z-1, y, x, iter));
  322. }
  323. if (z == nslicesz - 1) {
  324. struct cb2_s *cb2 = malloc(sizeof(struct cb2_s));
  325. cb2->blockx = x;
  326. cb2->blocky = y;
  327. cb2->iter = iter;
  328. cb2->xycounter = &xycounter;
  329. task->callback_func = callback_func_2;
  330. task->callback_arg = cb2;
  331. }
  332. starpu_submit_task(task);
  333. }
  334. }
  335. static void launch_codelets(void)
  336. {
  337. #ifdef USE_FXT
  338. fxt_register_thread(0);
  339. #endif
  340. /* partition the work into slices */
  341. unsigned taskx, tasky;
  342. /* only a callback per (nslicesz * niter) task given deps */
  343. xycounter = nslicesx * nslicesy;
  344. srand(time(NULL));
  345. gettimeofday(&start, NULL);
  346. for (taskx = 0; taskx < nslicesx; taskx++)
  347. for (tasky = 0; tasky < nslicesy; tasky++)
  348. {
  349. submit_new_iter(taskx, tasky, 0);
  350. }
  351. }
  352. int main(__attribute__ ((unused)) int argc,
  353. __attribute__ ((unused)) char **argv)
  354. {
  355. parse_args(argc, argv);
  356. /* start the runtime */
  357. starpu_init();
  358. sem_init(&sem, 0, 0U);
  359. init_problem_data();
  360. launch_codelets();
  361. sem_wait(&sem);
  362. sem_destroy(&sem);
  363. cleanup_problem();
  364. exit(-1);
  365. starpu_shutdown();
  366. return 0;
  367. }