dw_factolu.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include "dw_factolu.h"
  19. #include <sys/time.h>
  20. #if 0
  21. #define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
  22. #else
  23. #define debug(fmt, ...)
  24. #endif
  25. unsigned *advance_11; /* size nblocks, whether the 11 task is done */
  26. unsigned *advance_12_21; /* size nblocks*nblocks */
  27. unsigned *advance_22; /* array of nblocks *nblocks*nblocks */
  28. struct timeval start;
  29. struct timeval end;
  30. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  31. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  32. static unsigned finished = 0;
  33. static unsigned no_prio = 0;
  34. static struct starpu_codelet cl11 =
  35. {
  36. .where = STARPU_CPU|STARPU_CUDA,
  37. .cpu_funcs = {dw_cpu_codelet_update_u11, NULL},
  38. #ifdef STARPU_USE_CUDA
  39. .cuda_funcs = {dw_cublas_codelet_update_u11, NULL},
  40. #endif
  41. .nbuffers = 1,
  42. .modes = {STARPU_RW},
  43. .model = &model_11
  44. };
  45. static struct starpu_codelet cl12 =
  46. {
  47. .where = STARPU_CPU|STARPU_CUDA,
  48. .cpu_funcs = {dw_cpu_codelet_update_u12, NULL},
  49. #ifdef STARPU_USE_CUDA
  50. .cuda_funcs = {dw_cublas_codelet_update_u12, NULL},
  51. #endif
  52. .nbuffers = 2,
  53. .modes = {STARPU_R, STARPU_RW},
  54. .model = &model_12
  55. };
  56. static struct starpu_codelet cl21 =
  57. {
  58. .where = STARPU_CPU|STARPU_CUDA,
  59. .cpu_funcs = {dw_cpu_codelet_update_u21, NULL},
  60. #ifdef STARPU_USE_CUDA
  61. .cuda_funcs = {dw_cublas_codelet_update_u21, NULL},
  62. #endif
  63. .nbuffers = 2,
  64. .modes = {STARPU_R, STARPU_RW},
  65. .model = &model_21
  66. };
  67. static struct starpu_codelet cl22 =
  68. {
  69. .where = STARPU_CPU|STARPU_CUDA,
  70. .cpu_funcs = {dw_cpu_codelet_update_u22, NULL},
  71. #ifdef STARPU_USE_CUDA
  72. .cuda_funcs = {dw_cublas_codelet_update_u22, NULL},
  73. #endif
  74. .nbuffers = 3,
  75. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  76. .model = &model_22
  77. };
  78. #define STARTED 0x01
  79. #define DONE 0x11
  80. /*
  81. * Upgraded Callbacks : break the pipeline design !
  82. */
  83. void dw_callback_v2_codelet_update_u22(void *argcb)
  84. {
  85. int ret;
  86. cl_args *args = argcb;
  87. unsigned k = args->k;
  88. unsigned i = args->i;
  89. unsigned j = args->j;
  90. unsigned nblocks = args->nblocks;
  91. debug("u22 %d %d %d\n", k, i, j);
  92. /* we did task 22k,i,j */
  93. advance_22[k*nblocks*nblocks + i + j*nblocks] = DONE;
  94. if ( (i == j) && (i == k+1))
  95. {
  96. /* we now reduce the LU22 part (recursion appears there) */
  97. cl_args *u11arg = malloc(sizeof(cl_args));
  98. struct starpu_task *task = starpu_task_create();
  99. task->callback_func = dw_callback_v2_codelet_update_u11;
  100. task->callback_arg = u11arg;
  101. task->cl = &cl11;
  102. task->cl_arg = u11arg;
  103. task->handles[0] = starpu_data_get_sub_data(args->dataA, 2, k+1, k+1);
  104. u11arg->dataA = args->dataA;
  105. u11arg->i = k + 1;
  106. u11arg->nblocks = args->nblocks;
  107. /* schedule the codelet */
  108. if (!no_prio)
  109. task->priority = STARPU_MAX_PRIO;
  110. debug( "u22 %d %d %d start u11 %d\n", k, i, j, k + 1);
  111. ret = starpu_task_submit(task);
  112. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  113. }
  114. /* 11k+1 + 22k,k+1,j => 21 k+1,j */
  115. if ( i == k + 1 && j > k + 1)
  116. {
  117. uint8_t dep;
  118. /* 11 k+1*/
  119. dep = advance_11[(k+1)];
  120. if (dep & DONE)
  121. {
  122. /* try to push the task */
  123. uint8_t u = STARPU_ATOMIC_OR(&advance_12_21[(k+1) + j*nblocks], STARTED);
  124. if ((u & STARTED) == 0)
  125. {
  126. /* we are the only one that should launch that task */
  127. cl_args *u21a = malloc(sizeof(cl_args));
  128. struct starpu_task *task21 = starpu_task_create();
  129. task21->callback_func = dw_callback_v2_codelet_update_u21;
  130. task21->callback_arg = u21a;
  131. task21->cl = &cl21;
  132. task21->cl_arg = u21a;
  133. u21a->i = k+1;
  134. u21a->k = j;
  135. u21a->nblocks = args->nblocks;
  136. u21a->dataA = args->dataA;
  137. task21->handles[0] = starpu_data_get_sub_data(args->dataA, 2, u21a->i, u21a->i);
  138. task21->handles[1] = starpu_data_get_sub_data(args->dataA, 2, u21a->i, u21a->k);
  139. debug( "u22 %d %d %d start u21 %d %d\n", k, i, j, k+1, j);
  140. ret = starpu_task_submit(task21);
  141. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  142. }
  143. }
  144. }
  145. /* 11k + 22k-1,i,k => 12 k,i */
  146. if (j == k + 1 && i > k + 1)
  147. {
  148. uint8_t dep;
  149. /* 11 k+1*/
  150. dep = advance_11[(k+1)];
  151. if (dep & DONE)
  152. {
  153. /* try to push the task */
  154. uint8_t u = STARPU_ATOMIC_OR(&advance_12_21[(k+1)*nblocks + i], STARTED);
  155. if ((u & STARTED) == 0)
  156. {
  157. /* we are the only one that should launch that task */
  158. cl_args *u12a = malloc(sizeof(cl_args));
  159. struct starpu_task *task12 = starpu_task_create();
  160. task12->callback_func = dw_callback_v2_codelet_update_u12;
  161. task12->callback_arg = u12a;
  162. task12->cl = &cl12;
  163. task12->cl_arg = u12a;
  164. u12a->i = k+1;
  165. u12a->k = i;
  166. u12a->nblocks = args->nblocks;
  167. u12a->dataA = args->dataA;
  168. task12->handles[0] = starpu_data_get_sub_data(args->dataA, 2, u12a->i, u12a->i);
  169. task12->handles[1] = starpu_data_get_sub_data(args->dataA, 2, u12a->k, u12a->i);
  170. debug( "u22 %d %d %d start u12 %d %d\n", k, i, j, k+1, i);
  171. ret = starpu_task_submit(task12);
  172. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  173. }
  174. }
  175. }
  176. free(args);
  177. }
  178. void dw_callback_v2_codelet_update_u12(void *argcb)
  179. {
  180. int ret;
  181. cl_args *args = argcb;
  182. /* now launch the update of LU22 */
  183. unsigned i = args->i;
  184. unsigned k = args->k;
  185. unsigned nblocks = args->nblocks;
  186. debug( "u12 %d %d\n", i, k);
  187. /* we did task 21i,k */
  188. advance_12_21[i*nblocks + k] = DONE;
  189. unsigned slicey;
  190. for (slicey = i+1; slicey < nblocks; slicey++)
  191. {
  192. /* can we launch 22 i,args->k,slicey ? */
  193. /* deps : 21 args->k, slicey */
  194. uint8_t dep;
  195. dep = advance_12_21[i + slicey*nblocks];
  196. if (dep & DONE)
  197. {
  198. /* perhaps we may schedule the 22 i,args->k,slicey task */
  199. uint8_t u = STARPU_ATOMIC_OR(&advance_22[i*nblocks*nblocks + slicey*nblocks + k], STARTED);
  200. if ((u & STARTED) == 0)
  201. {
  202. /* update that square matrix */
  203. cl_args *u22a = malloc(sizeof(cl_args));
  204. struct starpu_task *task22 = starpu_task_create();
  205. task22->callback_func = dw_callback_v2_codelet_update_u22;
  206. task22->callback_arg = u22a;
  207. task22->cl = &cl22;
  208. task22->cl_arg = u22a;
  209. u22a->k = i;
  210. u22a->i = k;
  211. u22a->j = slicey;
  212. u22a->dataA = args->dataA;
  213. u22a->nblocks = nblocks;
  214. task22->handles[0] = starpu_data_get_sub_data(args->dataA, 2, u22a->i, u22a->k);
  215. task22->handles[1] = starpu_data_get_sub_data(args->dataA, 2, u22a->k, u22a->j);
  216. task22->handles[2] = starpu_data_get_sub_data(args->dataA, 2, u22a->i, u22a->j);
  217. /* schedule that codelet */
  218. if (!no_prio && (slicey == i+1))
  219. task22->priority = STARPU_MAX_PRIO;
  220. debug( "u12 %d %d start u22 %d %d %d\n", i, k, i, k, slicey);
  221. ret = starpu_task_submit(task22);
  222. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  223. }
  224. }
  225. }
  226. free(argcb);
  227. }
  228. void dw_callback_v2_codelet_update_u21(void *argcb)
  229. {
  230. int ret;
  231. cl_args *args = argcb;
  232. /* now launch the update of LU22 */
  233. unsigned i = args->i;
  234. unsigned k = args->k;
  235. unsigned nblocks = args->nblocks;
  236. /* we did task 21i,k */
  237. advance_12_21[i + k*nblocks] = DONE;
  238. debug("u21 %d %d\n", i, k);
  239. unsigned slicex;
  240. for (slicex = i+1; slicex < nblocks; slicex++)
  241. {
  242. /* can we launch 22 i,slicex,k ? */
  243. /* deps : 12 slicex k */
  244. uint8_t dep;
  245. dep = advance_12_21[i*nblocks + slicex];
  246. if (dep & DONE)
  247. {
  248. /* perhaps we may schedule the 22 i,args->k,slicey task */
  249. uint8_t u = STARPU_ATOMIC_OR(&advance_22[i*nblocks*nblocks + k*nblocks + slicex], STARTED);
  250. if ((u & STARTED) == 0)
  251. {
  252. /* update that square matrix */
  253. cl_args *u22a = malloc(sizeof(cl_args));
  254. struct starpu_task *task22 = starpu_task_create();
  255. task22->callback_func = dw_callback_v2_codelet_update_u22;
  256. task22->callback_arg = u22a;
  257. task22->cl = &cl22;
  258. task22->cl_arg = u22a;
  259. u22a->k = i;
  260. u22a->i = slicex;
  261. u22a->j = k;
  262. u22a->dataA = args->dataA;
  263. u22a->nblocks = nblocks;
  264. task22->handles[0] = starpu_data_get_sub_data(args->dataA, 2, u22a->i, u22a->k);
  265. task22->handles[1] = starpu_data_get_sub_data(args->dataA, 2, u22a->k, u22a->j);
  266. task22->handles[2] = starpu_data_get_sub_data(args->dataA, 2, u22a->i, u22a->j);
  267. /* schedule that codelet */
  268. if (!no_prio && (slicex == i+1))
  269. task22->priority = STARPU_MAX_PRIO;
  270. debug( "u21 %d %d start u22 %d %d %d\n", i, k, i, slicex, k);
  271. ret = starpu_task_submit(task22);
  272. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  273. }
  274. }
  275. }
  276. free(argcb);
  277. }
  278. void dw_callback_v2_codelet_update_u11(void *argcb)
  279. {
  280. int ret;
  281. /* in case there remains work, go on */
  282. cl_args *args = argcb;
  283. unsigned nblocks = args->nblocks;
  284. unsigned i = args->i;
  285. debug("u11 %d\n", i);
  286. /* we did task 11k */
  287. advance_11[i] = DONE;
  288. if (i == nblocks - 1)
  289. {
  290. /* we are done : wake the application up */
  291. pthread_mutex_lock(&mutex);
  292. finished = 1;
  293. pthread_cond_signal(&cond);
  294. pthread_mutex_unlock(&mutex);
  295. free(argcb);
  296. return;
  297. }
  298. else
  299. {
  300. /* put new tasks */
  301. unsigned slice;
  302. for (slice = i + 1; slice < nblocks; slice++)
  303. {
  304. /* can we launch 12i,slice ? */
  305. uint8_t deps12;
  306. if (i == 0)
  307. {
  308. deps12 = DONE;
  309. }
  310. else
  311. {
  312. deps12 = advance_22[(i-1)*nblocks*nblocks + slice + i*nblocks];
  313. }
  314. if (deps12 & DONE)
  315. {
  316. /* we may perhaps launch the task 12i,slice */
  317. uint8_t u = STARPU_ATOMIC_OR(&advance_12_21[i*nblocks + slice], STARTED);
  318. if ((u & STARTED) == 0)
  319. {
  320. /* we are the only one that should launch that task */
  321. cl_args *u12a = malloc(sizeof(cl_args));
  322. struct starpu_task *task12 = starpu_task_create();
  323. task12->callback_func = dw_callback_v2_codelet_update_u12;
  324. task12->callback_arg = u12a;
  325. task12->cl = &cl12;
  326. task12->cl_arg = u12a;
  327. u12a->i = i;
  328. u12a->k = slice;
  329. u12a->nblocks = args->nblocks;
  330. u12a->dataA = args->dataA;
  331. task12->handles[0] = starpu_data_get_sub_data(args->dataA, 2, u12a->i, u12a->i);
  332. task12->handles[1] = starpu_data_get_sub_data(args->dataA, 2, u12a->k, u12a->i);
  333. if (!no_prio && (slice == i +1))
  334. task12->priority = STARPU_MAX_PRIO;
  335. debug( "u11 %d start u12 %d %d\n", i, i, slice);
  336. ret = starpu_task_submit(task12);
  337. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  338. }
  339. }
  340. /* can we launch 21i,slice ? */
  341. if (i == 0)
  342. {
  343. deps12 = DONE;
  344. }
  345. else
  346. {
  347. deps12 = advance_22[(i-1)*nblocks*nblocks + slice*nblocks + i];
  348. }
  349. if (deps12 & DONE)
  350. {
  351. /* we may perhaps launch the task 12i,slice */
  352. uint8_t u = STARPU_ATOMIC_OR(&advance_12_21[i + slice*nblocks], STARTED);
  353. if ((u & STARTED) == 0)
  354. {
  355. /* we are the only one that should launch that task */
  356. cl_args *u21a = malloc(sizeof(cl_args));
  357. struct starpu_task *task21 = starpu_task_create();
  358. task21->callback_func = dw_callback_v2_codelet_update_u21;
  359. task21->callback_arg = u21a;
  360. task21->cl = &cl21;
  361. task21->cl_arg = u21a;
  362. u21a->i = i;
  363. u21a->k = slice;
  364. u21a->nblocks = args->nblocks;
  365. u21a->dataA = args->dataA;
  366. task21->handles[0] = starpu_data_get_sub_data(args->dataA, 2, u21a->i, u21a->i);
  367. task21->handles[1] = starpu_data_get_sub_data(args->dataA, 2, u21a->i, u21a->k);
  368. if (!no_prio && (slice == i +1))
  369. task21->priority = STARPU_MAX_PRIO;
  370. debug( "u11 %d start u21 %d %d\n", i, i, slice);
  371. ret = starpu_task_submit(task21);
  372. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  373. }
  374. }
  375. }
  376. }
  377. free(argcb);
  378. }
  379. /*
  380. * Callbacks
  381. */
  382. void dw_callback_codelet_update_u11(void *argcb)
  383. {
  384. int ret;
  385. /* in case there remains work, go on */
  386. cl_args *args = argcb;
  387. if (args->i == args->nblocks - 1)
  388. {
  389. /* we are done : wake the application up */
  390. pthread_mutex_lock(&mutex);
  391. finished = 1;
  392. pthread_cond_signal(&cond);
  393. pthread_mutex_unlock(&mutex);
  394. free(argcb);
  395. return;
  396. }
  397. else
  398. {
  399. /* put new tasks */
  400. unsigned nslices;
  401. nslices = args->nblocks - 1 - args->i;
  402. unsigned *remaining = malloc(sizeof(unsigned));
  403. *remaining = 2*nslices;
  404. unsigned slice;
  405. for (slice = args->i + 1; slice < args->nblocks; slice++)
  406. {
  407. /* update slice from u12 */
  408. cl_args *u12a = malloc(sizeof(cl_args));
  409. /* update slice from u21 */
  410. cl_args *u21a = malloc(sizeof(cl_args));
  411. struct starpu_task *task12 = starpu_task_create();
  412. task12->callback_func = dw_callback_codelet_update_u12_21;
  413. task12->callback_arg = u12a;
  414. task12->cl = &cl12;
  415. task12->cl_arg = u12a;
  416. struct starpu_task *task21 = starpu_task_create();
  417. task21->callback_func = dw_callback_codelet_update_u12_21;
  418. task21->callback_arg = u21a;
  419. task21->cl = &cl21;
  420. task21->cl_arg = u21a;
  421. u12a->i = args->i;
  422. u12a->k = slice;
  423. u12a->nblocks = args->nblocks;
  424. u12a->dataA = args->dataA;
  425. u12a->remaining = remaining;
  426. u21a->i = args->i;
  427. u21a->k = slice;
  428. u21a->nblocks = args->nblocks;
  429. u21a->dataA = args->dataA;
  430. u21a->remaining = remaining;
  431. task12->handles[0] = starpu_data_get_sub_data(args->dataA, 2, u12a->i, u12a->i);
  432. task12->handles[1] = starpu_data_get_sub_data(args->dataA, 2, u12a->k, u12a->i);
  433. task21->handles[0] = starpu_data_get_sub_data(args->dataA, 2, u21a->i, u21a->i);
  434. task21->handles[1] = starpu_data_get_sub_data(args->dataA, 2, u21a->i, u21a->k);
  435. ret = starpu_task_submit(task12);
  436. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  437. ret = starpu_task_submit(task21);
  438. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  439. }
  440. }
  441. }
  442. void dw_callback_codelet_update_u22(void *argcb)
  443. {
  444. int ret;
  445. cl_args *args = argcb;
  446. if (STARPU_ATOMIC_ADD(args->remaining, (-1)) == 0)
  447. {
  448. /* all worker already used the counter */
  449. free(args->remaining);
  450. /* we now reduce the LU22 part (recursion appears there) */
  451. cl_args *u11arg = malloc(sizeof(cl_args));
  452. struct starpu_task *task = starpu_task_create();
  453. task->callback_func = dw_callback_codelet_update_u11;
  454. task->callback_arg = u11arg;
  455. task->cl = &cl11;
  456. task->cl_arg = u11arg;
  457. task->handles[0] = starpu_data_get_sub_data(args->dataA, 2, args->k + 1, args->k + 1);
  458. u11arg->dataA = args->dataA;
  459. u11arg->i = args->k + 1;
  460. u11arg->nblocks = args->nblocks;
  461. /* schedule the codelet */
  462. ret = starpu_task_submit(task);
  463. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  464. }
  465. free(args);
  466. }
  467. void dw_callback_codelet_update_u12_21(void *argcb)
  468. {
  469. int ret;
  470. cl_args *args = argcb;
  471. if (STARPU_ATOMIC_ADD(args->remaining, -1) == 0)
  472. {
  473. /* now launch the update of LU22 */
  474. unsigned i = args->i;
  475. unsigned nblocks = args->nblocks;
  476. /* the number of tasks to be done */
  477. unsigned *remaining = malloc(sizeof(unsigned));
  478. *remaining = (nblocks - 1 - i)*(nblocks - 1 - i);
  479. unsigned slicey, slicex;
  480. for (slicey = i+1; slicey < nblocks; slicey++)
  481. {
  482. for (slicex = i+1; slicex < nblocks; slicex++)
  483. {
  484. /* update that square matrix */
  485. cl_args *u22a = malloc(sizeof(cl_args));
  486. struct starpu_task *task22 = starpu_task_create();
  487. task22->callback_func = dw_callback_codelet_update_u22;
  488. task22->callback_arg = u22a;
  489. task22->cl = &cl22;
  490. task22->cl_arg = u22a;
  491. u22a->k = i;
  492. u22a->i = slicex;
  493. u22a->j = slicey;
  494. u22a->dataA = args->dataA;
  495. u22a->nblocks = nblocks;
  496. u22a->remaining = remaining;
  497. task22->handles[0] = starpu_data_get_sub_data(args->dataA, 2, u22a->i, u22a->k);
  498. task22->handles[1] = starpu_data_get_sub_data(args->dataA, 2, u22a->k, u22a->j);
  499. task22->handles[2] = starpu_data_get_sub_data(args->dataA, 2, u22a->i, u22a->j);
  500. /* schedule that codelet */
  501. ret = starpu_task_submit(task22);
  502. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  503. }
  504. }
  505. }
  506. }
  507. /*
  508. * code to bootstrap the factorization
  509. */
  510. void dw_codelet_facto(starpu_data_handle_t dataA, unsigned nblocks)
  511. {
  512. int ret;
  513. cl_args *args = malloc(sizeof(cl_args));
  514. args->i = 0;
  515. args->nblocks = nblocks;
  516. args->dataA = dataA;
  517. gettimeofday(&start, NULL);
  518. /* inject a new task with this codelet into the system */
  519. struct starpu_task *task = starpu_task_create();
  520. task->callback_func = dw_callback_codelet_update_u11;
  521. task->callback_arg = args;
  522. task->cl = &cl11;
  523. task->cl_arg = args;
  524. task->handles[0] = starpu_data_get_sub_data(dataA, 2, 0, 0);
  525. /* schedule the codelet */
  526. ret = starpu_task_submit(task);
  527. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  528. /* stall the application until the end of computations */
  529. pthread_mutex_lock(&mutex);
  530. if (!finished)
  531. pthread_cond_wait(&cond, &mutex);
  532. pthread_mutex_unlock(&mutex);
  533. gettimeofday(&end, NULL);
  534. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  535. FPRINTF(stderr, "Computation took (in ms)\n");
  536. FPRINTF(stdout, "%2.2f\n", timing/1000);
  537. unsigned n = starpu_matrix_get_nx(dataA);
  538. double flop = (2.0f*n*n*n)/3.0f;
  539. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  540. }
  541. void dw_codelet_facto_v2(starpu_data_handle_t dataA, unsigned nblocks)
  542. {
  543. advance_11 = calloc(nblocks, sizeof(*advance_11));
  544. STARPU_ASSERT(advance_11);
  545. advance_12_21 = calloc(nblocks*nblocks, sizeof(*advance_12_21));
  546. STARPU_ASSERT(advance_12_21);
  547. advance_22 = calloc(nblocks*nblocks*nblocks, sizeof(*advance_22));
  548. STARPU_ASSERT(advance_22);
  549. cl_args *args = malloc(sizeof(cl_args));
  550. args->i = 0;
  551. args->nblocks = nblocks;
  552. args->dataA = dataA;
  553. gettimeofday(&start, NULL);
  554. /* inject a new task with this codelet into the system */
  555. struct starpu_task *task = starpu_task_create();
  556. task->callback_func = dw_callback_v2_codelet_update_u11;
  557. task->callback_arg = args;
  558. task->cl = &cl11;
  559. task->cl_arg = args;
  560. task->handles[0] = starpu_data_get_sub_data(dataA, 2, 0, 0);
  561. /* schedule the codelet */
  562. int ret = starpu_task_submit(task);
  563. if (STARPU_UNLIKELY(ret == -ENODEV))
  564. {
  565. FPRINTF(stderr, "No worker may execute this task\n");
  566. exit(0);
  567. }
  568. /* stall the application until the end of computations */
  569. pthread_mutex_lock(&mutex);
  570. if (!finished)
  571. pthread_cond_wait(&cond, &mutex);
  572. pthread_mutex_unlock(&mutex);
  573. gettimeofday(&end, NULL);
  574. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  575. FPRINTF(stderr, "Computation took (in ms)\n");
  576. FPRINTF(stdout, "%2.2f\n", timing/1000);
  577. unsigned n = starpu_matrix_get_nx(dataA);
  578. double flop = (2.0f*n*n*n)/3.0f;
  579. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  580. free(advance_11);
  581. free(advance_12_21);
  582. free(advance_22);
  583. }
  584. void initialize_system(float **A, float **B, unsigned dim, unsigned pinned)
  585. {
  586. int ret;
  587. ret = starpu_init(NULL);
  588. if (ret == -ENODEV)
  589. exit(77);
  590. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  591. starpu_helper_cublas_init();
  592. if (pinned)
  593. {
  594. starpu_malloc((void **)A, (size_t)dim*dim*sizeof(float));
  595. starpu_malloc((void **)B, (size_t)dim*sizeof(float));
  596. }
  597. else
  598. {
  599. *A = malloc((size_t)dim*dim*sizeof(float));
  600. STARPU_ASSERT(*A);
  601. *B = malloc((size_t)dim*sizeof(float));
  602. STARPU_ASSERT(*B);
  603. }
  604. }
  605. void free_system(float *A, float *B, unsigned dim, unsigned pinned)
  606. {
  607. if (pinned)
  608. {
  609. starpu_free(A);
  610. starpu_free(B);
  611. }
  612. else
  613. {
  614. free(A);
  615. free(B);
  616. }
  617. }
  618. void dw_factoLU(float *matA, unsigned size,
  619. unsigned ld, unsigned nblocks,
  620. unsigned version, unsigned _no_prio)
  621. {
  622. #ifdef CHECK_RESULTS
  623. FPRINTF(stderr, "Checking results ...\n");
  624. float *Asaved;
  625. Asaved = malloc((size_t)ld*ld*sizeof(float));
  626. memcpy(Asaved, matA, (size_t)ld*ld*sizeof(float));
  627. #endif
  628. no_prio = _no_prio;
  629. starpu_data_handle_t dataA;
  630. /* monitor and partition the A matrix into blocks :
  631. * one block is now determined by 2 unsigned (i,j) */
  632. starpu_matrix_data_register(&dataA, 0, (uintptr_t)matA, ld,
  633. size, size, sizeof(float));
  634. struct starpu_data_filter f =
  635. {
  636. .filter_func = starpu_vertical_block_filter_func,
  637. .nchildren = nblocks
  638. };
  639. struct starpu_data_filter f2 =
  640. {
  641. .filter_func = starpu_block_filter_func,
  642. .nchildren = nblocks
  643. };
  644. starpu_data_map_filters(dataA, 2, &f, &f2);
  645. switch (version)
  646. {
  647. case 1:
  648. dw_codelet_facto(dataA, nblocks);
  649. break;
  650. default:
  651. case 2:
  652. dw_codelet_facto_v2(dataA, nblocks);
  653. break;
  654. }
  655. /* gather all the data */
  656. starpu_data_unpartition(dataA, 0);
  657. starpu_data_unregister(dataA);
  658. #ifdef CHECK_RESULTS
  659. compare_A_LU(Asaved, matA, size, ld);
  660. #endif
  661. }