dw_mult_no_stride.c 11 KB

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