dw_mult_no_stride.c 12 KB

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