dw_mult_no_stride.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #ifdef HAVE_POSIX_MEMALIGN
  124. posix_memalign((void **)&A[y][z], MEM_ALIGNMENT, BLOCKSIZEZ*BLOCKSIZEY*sizeof(float));
  125. #else
  126. A[y][z] = malloc(BLOCKSIZEZ*BLOCKSIZEY*sizeof(float));
  127. #endif
  128. assert(A[y][z]);
  129. }
  130. }
  131. for (z = 0; z < nslicesz; z++)
  132. {
  133. for (x = 0; x < nslicesx; x++)
  134. {
  135. #ifdef HAVE_POSIX_MEMALIGN
  136. posix_memalign((void **)&B[z][x], MEM_ALIGNMENT, BLOCKSIZEX*BLOCKSIZEZ*sizeof(float));
  137. #else
  138. B[z][x] = malloc(BLOCKSIZEX*BLOCKSIZEZ*sizeof(float));
  139. #endif
  140. assert(B[z][x]);
  141. }
  142. }
  143. for (y = 0; y < nslicesy; y++)
  144. {
  145. for (x = 0; x < nslicesx; x++)
  146. {
  147. #ifdef HAVE_POSIX_MEMALIGN
  148. posix_memalign((void **)&C[y][x], MEM_ALIGNMENT, BLOCKSIZEX*BLOCKSIZEY*sizeof(float));
  149. #else
  150. C[y][x] = malloc(BLOCKSIZEX*BLOCKSIZEY*sizeof(float));
  151. #endif
  152. assert(C[y][x]);
  153. }
  154. }
  155. /* fill the A and B matrices */
  156. unsigned blockx, blocky, blockz;
  157. if (norandom) {
  158. for (blocky = 0; blocky < nslicesy; blocky++)
  159. for (blockz = 0; blockz < nslicesz; blockz++)
  160. for (j = 0; j < BLOCKSIZEY; j++)
  161. for (i = 0; i < BLOCKSIZEZ; i++)
  162. {
  163. A[blocky][blockz][i*BLOCKSIZEY + j] = (float)(1 + blockz + blocky*nslicesz);
  164. }
  165. for (blockz = 0; blockz < nslicesz; blockz++)
  166. for (blockx = 0; blockx < nslicesx; blockx++)
  167. for (j = 0; j < BLOCKSIZEZ; j++)
  168. for (i = 0; i < BLOCKSIZEX; i++)
  169. {
  170. B[blockz][blockx][i*BLOCKSIZEZ + j] = (float)(1 + blockx + blockz*nslicesx);
  171. }
  172. }
  173. else {
  174. for (blocky = 0; blocky < nslicesy; blocky++)
  175. for (blockz = 0; blockz < nslicesz; blockz++)
  176. for (j = 0; j < BLOCKSIZEY; j++)
  177. for (i = 0; i < BLOCKSIZEZ; i++)
  178. {
  179. A[blocky][blockz][i*BLOCKSIZEY + j] = (float)(drand48());
  180. }
  181. for (blockz = 0; blockz < nslicesz; blockz++)
  182. for (blockx = 0; blockx < nslicesx; blockx++)
  183. for (j = 0; j < BLOCKSIZEZ; j++)
  184. for (i = 0; i < BLOCKSIZEX; i++)
  185. {
  186. B[blockz][blockx][i*BLOCKSIZEZ + j] = (float)(drand48());
  187. }
  188. }
  189. for (blocky = 0; blocky < nslicesy; blocky++)
  190. for (blockx = 0; blockx < nslicesx; blockx++)
  191. for (j = 0; j < BLOCKSIZEY; j++)
  192. for (i = 0; i < BLOCKSIZEX; i++)
  193. {
  194. C[blocky][blockx][i*BLOCKSIZEY + j] = (float)(blockx + blocky*nslicesx + 1);
  195. }
  196. /* declare the StarPU data to monitor */
  197. for (y = 0; y < nslicesy; y++)
  198. {
  199. for (z = 0; z < nslicesz; z++)
  200. {
  201. starpu_monitor_blas_data(&A_state[y][z], 0, (uintptr_t)A[y][z],
  202. BLOCKSIZEY, BLOCKSIZEY, BLOCKSIZEZ, sizeof(float));
  203. }
  204. }
  205. for (z = 0; z < nslicesz; z++)
  206. {
  207. for (x = 0; x < nslicesx; x++)
  208. {
  209. starpu_monitor_blas_data(&B_state[z][x], 0, (uintptr_t)B[z][x],
  210. BLOCKSIZEZ, BLOCKSIZEZ, BLOCKSIZEX, sizeof(float));
  211. }
  212. }
  213. for (y = 0; y < nslicesy; y++)
  214. {
  215. for (x = 0; x < nslicesx; x++)
  216. {
  217. starpu_monitor_blas_data(&C_state[y][x], 0, (uintptr_t)C[y][x],
  218. BLOCKSIZEY, BLOCKSIZEY, BLOCKSIZEX, sizeof(float));
  219. }
  220. }
  221. conf.k = BLOCKSIZEZ;
  222. conf.m = BLOCKSIZEY;
  223. conf.n = BLOCKSIZEX;
  224. display_memory_consumption();
  225. }
  226. static void cleanup_problem(void)
  227. {
  228. unsigned z, y, x;
  229. for (y = 0; y < nslicesy; y++)
  230. {
  231. for (z = 0; z < nslicesz; z++)
  232. {
  233. // free(A[y][z]);
  234. }
  235. }
  236. for (z = 0; z < nslicesz; z++)
  237. {
  238. for (x = 0; x < nslicesx; x++)
  239. {
  240. // free(B[z][x]);
  241. }
  242. }
  243. for (y = 0; y < nslicesy; y++)
  244. {
  245. for (x = 0; x < nslicesx; x++)
  246. {
  247. // free(C[y][x]);
  248. starpu_tag_remove(TAG(nslicesz - 1, y, x, niter - 1));
  249. }
  250. }
  251. }
  252. int xycounter;
  253. struct cb2_s {
  254. unsigned blockx;
  255. unsigned blocky;
  256. unsigned iter;
  257. int *xycounter;
  258. };
  259. static starpu_codelet cl = {
  260. .core_func = core_mult,
  261. #ifdef USE_CUDA
  262. .cublas_func = cublas_mult,
  263. #endif
  264. #ifdef USE_GORDON
  265. .gordon_func = SPU_FUNC_SGEMM,
  266. #endif
  267. .where = CORE|CUBLAS|GORDON,
  268. .nbuffers = 3
  269. };
  270. static struct starpu_task *construct_task(unsigned x, unsigned y, unsigned z, unsigned iter)
  271. {
  272. struct starpu_task *task = starpu_task_create();
  273. task->cl = &cl;
  274. task->cl_arg = &conf;
  275. task->cl_arg_size = sizeof(struct block_conf);
  276. task->use_tag = 1;
  277. task->tag_id = TAG(z, y, x, iter);
  278. task->buffers[0].state = A_state[y][z];
  279. task->buffers[0].mode = R;
  280. task->buffers[1].state = B_state[z][x];
  281. task->buffers[1].mode = R;
  282. task->buffers[2].state = C_state[y][x];
  283. task->buffers[2].mode = RW;
  284. return task;
  285. }
  286. static void callback_func(void *arg)
  287. {
  288. /* the argument is a pointer to a counter of the remaining tasks */
  289. int *counter = arg;
  290. int newvalue = STARPU_ATOMIC_ADD(counter, -1);
  291. if (newvalue == 0)
  292. {
  293. /* we are done */
  294. fprintf(stderr, "done ...\n");
  295. terminate();
  296. }
  297. return;
  298. }
  299. static void callback_func_2(void *arg)
  300. {
  301. /* the argument is a pointer to a counter of the remaining tasks */
  302. struct cb2_s *cb2 = arg;
  303. unsigned x,y,z,iter;
  304. iter = cb2->iter;
  305. x = cb2->blockx;
  306. y = cb2->blocky;
  307. free(cb2);
  308. // fprintf(stderr, "func 2 for x %d y %d iter %d\n", x, y, iter);
  309. /* TAG(nslicesz - 1, y, x, iter) remains ... */
  310. for (z = 0; z < nslicesz - 1; z++)
  311. {
  312. starpu_tag_remove(TAG(z, y, x, iter));
  313. }
  314. if (iter > 0)
  315. {
  316. starpu_tag_remove(TAG(nslicesz - 1, y, x, iter-1));
  317. }
  318. if (iter == niter - 1) {
  319. callback_func(&xycounter);
  320. }
  321. else {
  322. submit_new_iter(x, y, iter+1);
  323. }
  324. }
  325. static void submit_new_iter(unsigned x, unsigned y, unsigned iter)
  326. {
  327. unsigned z;
  328. for (z = 0; z < nslicesz; z++)
  329. {
  330. struct starpu_task *task;
  331. task = construct_task(x, y, z, iter);
  332. if (z != 0) {
  333. starpu_tag_declare_deps(TAG(z, y, x, iter), 1, TAG(z-1, y, x, iter));
  334. }
  335. if (z == nslicesz - 1) {
  336. struct cb2_s *cb2 = malloc(sizeof(struct cb2_s));
  337. cb2->blockx = x;
  338. cb2->blocky = y;
  339. cb2->iter = iter;
  340. cb2->xycounter = &xycounter;
  341. task->callback_func = callback_func_2;
  342. task->callback_arg = cb2;
  343. }
  344. starpu_submit_task(task);
  345. }
  346. }
  347. static void launch_codelets(void)
  348. {
  349. #ifdef USE_FXT
  350. fxt_register_thread(0);
  351. #endif
  352. /* partition the work into slices */
  353. unsigned taskx, tasky;
  354. /* only a callback per (nslicesz * niter) task given deps */
  355. xycounter = nslicesx * nslicesy;
  356. srand(time(NULL));
  357. gettimeofday(&start, NULL);
  358. for (taskx = 0; taskx < nslicesx; taskx++)
  359. for (tasky = 0; tasky < nslicesy; tasky++)
  360. {
  361. submit_new_iter(taskx, tasky, 0);
  362. }
  363. }
  364. int main(__attribute__ ((unused)) int argc,
  365. __attribute__ ((unused)) char **argv)
  366. {
  367. parse_args(argc, argv);
  368. /* start the runtime */
  369. starpu_init();
  370. sem_init(&sem, 0, 0U);
  371. init_problem_data();
  372. launch_codelets();
  373. sem_wait(&sem);
  374. sem_destroy(&sem);
  375. cleanup_problem();
  376. exit(-1);
  377. starpu_shutdown();
  378. return 0;
  379. }