dw_mult_no_stride.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. conf.k = BLOCKSIZEZ;
  229. conf.m = BLOCKSIZEY;
  230. conf.n = BLOCKSIZEX;
  231. display_memory_consumption();
  232. }
  233. static void cleanup_problem(void)
  234. {
  235. unsigned z, y, x;
  236. for (y = 0; y < nslicesy; y++)
  237. {
  238. for (z = 0; z < nslicesz; z++)
  239. {
  240. // free(A[y][z]);
  241. }
  242. }
  243. for (z = 0; z < nslicesz; z++)
  244. {
  245. for (x = 0; x < nslicesx; x++)
  246. {
  247. // free(B[z][x]);
  248. }
  249. }
  250. for (y = 0; y < nslicesy; y++)
  251. {
  252. for (x = 0; x < nslicesx; x++)
  253. {
  254. // free(C[y][x]);
  255. starpu_tag_remove(TAG(nslicesz - 1, y, x, niter - 1));
  256. }
  257. }
  258. }
  259. int xycounter;
  260. struct cb2_s {
  261. unsigned blockx;
  262. unsigned blocky;
  263. unsigned iter;
  264. int *xycounter;
  265. };
  266. static starpu_codelet cl = {
  267. .core_func = core_mult,
  268. #ifdef USE_CUDA
  269. .cublas_func = cublas_mult,
  270. #endif
  271. #ifdef USE_GORDON
  272. /* .gordon_func will be set by load_elf_sgemm */
  273. #endif
  274. .model = &sgemm_model,
  275. .where = CORE|CUBLAS|GORDON,
  276. .nbuffers = 3
  277. };
  278. #ifdef USE_GORDON
  279. static const char *spu_func_sgemm_elf_file = "./gordon/func_sgemm_ibm.spuelf";
  280. static unsigned spu_func_sgemm_elf_id;
  281. static unsigned spu_func_sgemm_ibm_id;
  282. static void load_elf_sgemm(void)
  283. {
  284. spu_func_sgemm_elf_id =
  285. gordon_register_elf_plugin(spu_func_sgemm_elf_file);
  286. spu_func_sgemm_ibm_id = gordon_register_kernel(spu_func_sgemm_elf_id, "func_sgemm_ibm");
  287. gordon_load_plugin_on_all_spu(spu_func_sgemm_elf_id);
  288. gordon_load_kernel_on_all_spu(spu_func_sgemm_ibm_id);
  289. cl.gordon_func = spu_func_sgemm_ibm_id;
  290. }
  291. #endif // USE_GORDON
  292. static struct starpu_task *construct_task(unsigned x, unsigned y, unsigned z, unsigned iter)
  293. {
  294. struct starpu_task *task = starpu_task_create();
  295. task->cl = &cl;
  296. task->cl_arg = &conf;
  297. task->cl_arg_size = sizeof(struct ibm_sgemm_block_conf);
  298. task->use_tag = 1;
  299. task->tag_id = TAG(z, y, x, iter);
  300. task->buffers[0].state = A_state[y][z];
  301. task->buffers[0].mode = R;
  302. task->buffers[1].state = B_state[z][x];
  303. task->buffers[1].mode = R;
  304. task->buffers[2].state = C_state[y][x];
  305. task->buffers[2].mode = RW;
  306. return task;
  307. }
  308. static void callback_func(void *arg)
  309. {
  310. /* the argument is a pointer to a counter of the remaining tasks */
  311. int *counter = arg;
  312. int newvalue = STARPU_ATOMIC_ADD(counter, -1);
  313. if (newvalue == 0)
  314. {
  315. /* we are done */
  316. fprintf(stderr, "done ...\n");
  317. terminate();
  318. }
  319. return;
  320. }
  321. static void callback_func_2(void *arg)
  322. {
  323. /* the argument is a pointer to a counter of the remaining tasks */
  324. struct cb2_s *cb2 = arg;
  325. unsigned x,y,z,iter;
  326. iter = cb2->iter;
  327. x = cb2->blockx;
  328. y = cb2->blocky;
  329. free(cb2);
  330. // fprintf(stderr, "func 2 for x %d y %d iter %d\n", x, y, iter);
  331. /* TAG(nslicesz - 1, y, x, iter) remains ... */
  332. for (z = 0; z < nslicesz - 1; z++)
  333. {
  334. starpu_tag_remove(TAG(z, y, x, iter));
  335. }
  336. if (iter > 0)
  337. {
  338. starpu_tag_remove(TAG(nslicesz - 1, y, x, iter-1));
  339. }
  340. if (iter == niter - 1) {
  341. callback_func(&xycounter);
  342. }
  343. else {
  344. submit_new_iter(x, y, iter+1);
  345. }
  346. }
  347. static void submit_new_iter(unsigned x, unsigned y, unsigned iter)
  348. {
  349. unsigned z;
  350. for (z = 0; z < nslicesz; z++)
  351. {
  352. struct starpu_task *task;
  353. task = construct_task(x, y, z, iter);
  354. if (z != 0) {
  355. starpu_tag_declare_deps(TAG(z, y, x, iter), 1, TAG(z-1, y, x, iter));
  356. }
  357. if (z == nslicesz - 1) {
  358. struct cb2_s *cb2 = malloc(sizeof(struct cb2_s));
  359. cb2->blockx = x;
  360. cb2->blocky = y;
  361. cb2->iter = iter;
  362. cb2->xycounter = &xycounter;
  363. task->callback_func = callback_func_2;
  364. task->callback_arg = cb2;
  365. }
  366. starpu_submit_task(task);
  367. }
  368. }
  369. static void launch_codelets(void)
  370. {
  371. #ifdef USE_FXT
  372. fxt_register_thread(0);
  373. #endif
  374. /* partition the work into slices */
  375. unsigned taskx, tasky;
  376. /* only a callback per (nslicesz * niter) task given deps */
  377. xycounter = nslicesx * nslicesy;
  378. srand(time(NULL));
  379. gettimeofday(&start, NULL);
  380. for (taskx = 0; taskx < nslicesx; taskx++)
  381. for (tasky = 0; tasky < nslicesy; tasky++)
  382. {
  383. submit_new_iter(taskx, tasky, 0);
  384. }
  385. }
  386. int main(__attribute__ ((unused)) int argc,
  387. __attribute__ ((unused)) char **argv)
  388. {
  389. parse_args(argc, argv);
  390. /* start the runtime */
  391. starpu_init(NULL);
  392. #ifdef USE_GORDON
  393. load_elf_sgemm();
  394. #endif
  395. pthread_mutex_init(&mutex, NULL);
  396. pthread_cond_init(&cond, NULL);
  397. init_problem_data();
  398. launch_codelets();
  399. pthread_mutex_lock(&mutex);
  400. pthread_cond_wait(&cond, &mutex);
  401. pthread_mutex_unlock(&mutex);
  402. cleanup_problem();
  403. exit(-1);
  404. starpu_shutdown();
  405. return 0;
  406. }