xlu_pivot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 "xlu.h"
  17. #include "xlu_kernels.h"
  18. #define TAG11(k) ((starpu_tag_t)( (1ULL<<60) | (unsigned long long)(k)))
  19. #define TAG12(k,i) ((starpu_tag_t)(((2ULL<<60) | (((unsigned long long)(k))<<32) \
  20. | (unsigned long long)(i))))
  21. #define TAG21(k,j) ((starpu_tag_t)(((3ULL<<60) | (((unsigned long long)(k))<<32) \
  22. | (unsigned long long)(j))))
  23. #define TAG22(k,i,j) ((starpu_tag_t)(((4ULL<<60) | ((unsigned long long)(k)<<32) \
  24. | ((unsigned long long)(i)<<16) \
  25. | (unsigned long long)(j))))
  26. #define PIVOT(k,i) ((starpu_tag_t)(((5ULL<<60) | (((unsigned long long)(k))<<32) \
  27. | (unsigned long long)(i))))
  28. static unsigned no_prio = 0;
  29. /*
  30. * Construct the DAG
  31. */
  32. static struct starpu_task *create_task(starpu_tag_t id)
  33. {
  34. struct starpu_task *task = starpu_task_create();
  35. task->cl_arg = NULL;
  36. task->use_tag = 1;
  37. task->tag_id = id;
  38. return task;
  39. }
  40. static struct starpu_perfmodel_t STARPU_LU(model_pivot) = {
  41. .type = STARPU_HISTORY_BASED,
  42. #ifdef ATLAS
  43. .symbol = STARPU_LU_STR(lu_model_pivot_atlas)
  44. #elif defined(GOTO)
  45. .symbol = STARPU_LU_STR(lu_model_pivot_goto)
  46. #else
  47. .symbol = STARPU_LU_STR(lu_model_pivot)
  48. #endif
  49. };
  50. static starpu_codelet cl_pivot = {
  51. .where = STARPU_CPU|STARPU_CUDA,
  52. .cpu_func = STARPU_LU(cpu_pivot),
  53. #ifdef USE_CUDA
  54. .cuda_func = STARPU_LU(cublas_pivot),
  55. #endif
  56. .nbuffers = 1,
  57. .model = &STARPU_LU(model_pivot)
  58. };
  59. static void create_task_pivot(starpu_data_handle *dataAp, unsigned nblocks,
  60. struct piv_s *piv_description,
  61. unsigned k, unsigned i,
  62. starpu_data_handle (* get_block)(starpu_data_handle *, unsigned, unsigned, unsigned))
  63. {
  64. struct starpu_task *task = create_task(PIVOT(k, i));
  65. task->cl = &cl_pivot;
  66. /* which sub-data is manipulated ? */
  67. task->buffers[0].handle = get_block(dataAp, nblocks, k, i);
  68. task->buffers[0].mode = STARPU_RW;
  69. task->cl_arg = &piv_description[k];
  70. /* this is an important task */
  71. if (!no_prio && (i == k+1))
  72. task->priority = STARPU_MAX_PRIO;
  73. /* enforce dependencies ... */
  74. if (k == 0) {
  75. starpu_tag_declare_deps(PIVOT(k, i), 1, TAG11(k));
  76. }
  77. else
  78. {
  79. if (i > k) {
  80. starpu_tag_declare_deps(PIVOT(k, i), 2, TAG11(k), TAG22(k-1, i, k));
  81. }
  82. else {
  83. starpu_tag_t *tags = malloc((nblocks - k)*sizeof(starpu_tag_t));
  84. tags[0] = TAG11(k);
  85. unsigned ind, ind2;
  86. for (ind = k + 1, ind2 = 0; ind < nblocks; ind++, ind2++)
  87. {
  88. tags[1 + ind2] = TAG22(k-1, ind, k);
  89. }
  90. /* perhaps we could do better ... :/ */
  91. starpu_tag_declare_deps_array(PIVOT(k, i), (nblocks-k), tags);
  92. }
  93. }
  94. starpu_submit_task(task);
  95. }
  96. static struct starpu_perfmodel_t STARPU_LU(model_11_pivot) = {
  97. .type = STARPU_HISTORY_BASED,
  98. #ifdef ATLAS
  99. .symbol = STARPU_LU_STR(lu_model_11_pivot_atlas)
  100. #elif defined(GOTO)
  101. .symbol = STARPU_LU_STR(lu_model_11_pivot_goto)
  102. #else
  103. .symbol = STARPU_LU_STR(lu_model_11_pivot)
  104. #endif
  105. };
  106. static starpu_codelet cl11_pivot = {
  107. .where = STARPU_CPU|STARPU_CUDA,
  108. .cpu_func = STARPU_LU(cpu_u11_pivot),
  109. #ifdef USE_CUDA
  110. .cuda_func = STARPU_LU(cublas_u11_pivot),
  111. #endif
  112. .nbuffers = 1,
  113. .model = &STARPU_LU(model_11_pivot)
  114. };
  115. static struct starpu_task *create_task_11_pivot(starpu_data_handle *dataAp, unsigned nblocks,
  116. unsigned k, struct piv_s *piv_description,
  117. starpu_data_handle (* get_block)(starpu_data_handle *, unsigned, unsigned, unsigned))
  118. {
  119. struct starpu_task *task = create_task(TAG11(k));
  120. task->cl = &cl11_pivot;
  121. task->cl_arg = &piv_description[k];
  122. /* which sub-data is manipulated ? */
  123. task->buffers[0].handle = get_block(dataAp, nblocks, k, k);
  124. task->buffers[0].mode = STARPU_RW;
  125. /* this is an important task */
  126. if (!no_prio)
  127. task->priority = STARPU_MAX_PRIO;
  128. /* enforce dependencies ... */
  129. if (k > 0) {
  130. starpu_tag_declare_deps(TAG11(k), 1, TAG22(k-1, k, k));
  131. }
  132. return task;
  133. }
  134. static struct starpu_perfmodel_t STARPU_LU(model_12) = {
  135. .type = STARPU_HISTORY_BASED,
  136. #ifdef ATLAS
  137. .symbol = STARPU_LU_STR(lu_model_12_atlas)
  138. #elif defined(GOTO)
  139. .symbol = STARPU_LU_STR(lu_model_12_goto)
  140. #else
  141. .symbol = STARPU_LU_STR(lu_model_12)
  142. #endif
  143. };
  144. static starpu_codelet cl12 = {
  145. .where = STARPU_CPU|STARPU_CUDA,
  146. .cpu_func = STARPU_LU(cpu_u12),
  147. #ifdef USE_CUDA
  148. .cuda_func = STARPU_LU(cublas_u12),
  149. #endif
  150. .nbuffers = 2,
  151. .model = &STARPU_LU(model_12)
  152. };
  153. static void create_task_12(starpu_data_handle *dataAp, unsigned nblocks, unsigned k, unsigned j,
  154. starpu_data_handle (* get_block)(starpu_data_handle *, unsigned, unsigned, unsigned))
  155. {
  156. // printf("task 12 k,i = %d,%d TAG = %llx\n", k,i, TAG12(k,i));
  157. struct starpu_task *task = create_task(TAG12(k, j));
  158. task->cl = &cl12;
  159. task->cl_arg = (void *)(task->tag_id);
  160. /* which sub-data is manipulated ? */
  161. task->buffers[0].handle = get_block(dataAp, nblocks, k, k);
  162. task->buffers[0].mode = STARPU_R;
  163. task->buffers[1].handle = get_block(dataAp, nblocks, j, k);
  164. task->buffers[1].mode = STARPU_RW;
  165. if (!no_prio && (j == k+1)) {
  166. task->priority = STARPU_MAX_PRIO;
  167. }
  168. /* enforce dependencies ... */
  169. #if 0
  170. starpu_tag_declare_deps(TAG12(k, i), 1, PIVOT(k, i));
  171. #endif
  172. if (k > 0) {
  173. starpu_tag_declare_deps(TAG12(k, j), 2, TAG11(k), TAG22(k-1, k, j));
  174. }
  175. else {
  176. starpu_tag_declare_deps(TAG12(k, j), 1, TAG11(k));
  177. }
  178. starpu_submit_task(task);
  179. }
  180. static struct starpu_perfmodel_t STARPU_LU(model_21) = {
  181. .type = STARPU_HISTORY_BASED,
  182. #ifdef ATLAS
  183. .symbol = STARPU_LU_STR(lu_model_21_atlas)
  184. #elif defined(GOTO)
  185. .symbol = STARPU_LU_STR(lu_model_21_goto)
  186. #else
  187. .symbol = STARPU_LU_STR(lu_model_21)
  188. #endif
  189. };
  190. static starpu_codelet cl21 = {
  191. .where = STARPU_CPU|STARPU_CUDA,
  192. .cpu_func = STARPU_LU(cpu_u21),
  193. #ifdef USE_CUDA
  194. .cuda_func = STARPU_LU(cublas_u21),
  195. #endif
  196. .nbuffers = 2,
  197. .model = &STARPU_LU(model_21)
  198. };
  199. static void create_task_21(starpu_data_handle *dataAp, unsigned nblocks, unsigned k, unsigned i,
  200. starpu_data_handle (* get_block)(starpu_data_handle *, unsigned, unsigned, unsigned))
  201. {
  202. struct starpu_task *task = create_task(TAG21(k, i));
  203. task->cl = &cl21;
  204. /* which sub-data is manipulated ? */
  205. task->buffers[0].handle = get_block(dataAp, nblocks, k, k);
  206. task->buffers[0].mode = STARPU_R;
  207. task->buffers[1].handle = get_block(dataAp, nblocks, k, i);
  208. task->buffers[1].mode = STARPU_RW;
  209. if (!no_prio && (i == k+1)) {
  210. task->priority = STARPU_MAX_PRIO;
  211. }
  212. task->cl_arg = (void *)(task->tag_id);
  213. /* enforce dependencies ... */
  214. starpu_tag_declare_deps(TAG21(k, i), 1, PIVOT(k, i));
  215. #if 0
  216. if (k > 0) {
  217. starpu_tag_declare_deps(TAG21(k, i), 3, TAG11(k), TAG22(k-1, k, i), PIVOT(k, i));
  218. }
  219. else {
  220. starpu_tag_declare_deps(TAG21(k, i), 2, TAG11(k), PIVOT(k, i));
  221. }
  222. #endif
  223. starpu_submit_task(task);
  224. }
  225. static struct starpu_perfmodel_t STARPU_LU(model_22) = {
  226. .type = STARPU_HISTORY_BASED,
  227. #ifdef ATLAS
  228. .symbol = STARPU_LU_STR(lu_model_22_atlas)
  229. #elif defined(GOTO)
  230. .symbol = STARPU_LU_STR(lu_model_22_goto)
  231. #else
  232. .symbol = STARPU_LU_STR(lu_model_22)
  233. #endif
  234. };
  235. static starpu_codelet cl22 = {
  236. .where = STARPU_CPU|STARPU_CUDA,
  237. .cpu_func = STARPU_LU(cpu_u22),
  238. #ifdef USE_CUDA
  239. .cuda_func = STARPU_LU(cublas_u22),
  240. #endif
  241. .nbuffers = 3,
  242. .model = &STARPU_LU(model_22)
  243. };
  244. static void create_task_22(starpu_data_handle *dataAp, unsigned nblocks, unsigned k, unsigned i, unsigned j,
  245. starpu_data_handle (* get_block)(starpu_data_handle *, unsigned, unsigned, unsigned))
  246. {
  247. // printf("task 22 k,i,j = %d,%d,%d TAG = %llx\n", k,i,j, TAG22(k,i,j));
  248. struct starpu_task *task = create_task(TAG22(k, i, j));
  249. task->cl = &cl22;
  250. task->cl_arg = (void *)(task->tag_id);
  251. /* which sub-data is manipulated ? */
  252. task->buffers[0].handle = get_block(dataAp, nblocks, k, i); /* produced by TAG21(k, i) */
  253. task->buffers[0].mode = STARPU_R;
  254. task->buffers[1].handle = get_block(dataAp, nblocks, j, k); /* produced by TAG12(k, j) */
  255. task->buffers[1].mode = STARPU_R;
  256. task->buffers[2].handle = get_block(dataAp, nblocks, j, i); /* produced by TAG22(k-1, i, j) */
  257. task->buffers[2].mode = STARPU_RW;
  258. if (!no_prio && (i == k + 1) && (j == k +1) ) {
  259. task->priority = STARPU_MAX_PRIO;
  260. }
  261. /* enforce dependencies ... */
  262. if (k > 0) {
  263. starpu_tag_declare_deps(TAG22(k, i, j), 3, TAG22(k-1, i, j), TAG12(k, j), TAG21(k, i));
  264. }
  265. else {
  266. starpu_tag_declare_deps(TAG22(k, i, j), 2, TAG12(k, j), TAG21(k, i));
  267. }
  268. starpu_submit_task(task);
  269. }
  270. /*
  271. * code to bootstrap the factorization
  272. */
  273. static double dw_codelet_facto_pivot(starpu_data_handle *dataAp,
  274. struct piv_s *piv_description,
  275. unsigned nblocks,
  276. starpu_data_handle (* get_block)(starpu_data_handle *, unsigned, unsigned, unsigned))
  277. {
  278. struct timeval start;
  279. struct timeval end;
  280. struct starpu_task *entry_task = NULL;
  281. /* create all the DAG nodes */
  282. unsigned i,j,k;
  283. for (k = 0; k < nblocks; k++)
  284. {
  285. struct starpu_task *task = create_task_11_pivot(dataAp, nblocks, k, piv_description, get_block);
  286. /* we defer the launch of the first task */
  287. if (k == 0) {
  288. entry_task = task;
  289. }
  290. else {
  291. starpu_submit_task(task);
  292. }
  293. for (i = 0; i < nblocks; i++)
  294. {
  295. if (i != k)
  296. create_task_pivot(dataAp, nblocks, piv_description, k, i, get_block);
  297. }
  298. for (i = k+1; i<nblocks; i++)
  299. {
  300. create_task_12(dataAp, nblocks, k, i, get_block);
  301. create_task_21(dataAp, nblocks, k, i, get_block);
  302. }
  303. for (i = k+1; i<nblocks; i++)
  304. {
  305. for (j = k+1; j<nblocks; j++)
  306. {
  307. create_task_22(dataAp, nblocks, k, i, j, get_block);
  308. }
  309. }
  310. }
  311. /* we wait the last task (TAG11(nblocks - 1)) and all the pivot tasks */
  312. starpu_tag_t *tags = malloc(nblocks*nblocks*sizeof(starpu_tag_t));
  313. unsigned ndeps = 0;
  314. tags[ndeps++] = TAG11(nblocks - 1);
  315. for (j = 0; j < nblocks; j++)
  316. {
  317. for (i = 0; i < j; i++)
  318. {
  319. tags[ndeps++] = PIVOT(j, i);
  320. }
  321. }
  322. /* schedule the codelet */
  323. gettimeofday(&start, NULL);
  324. int ret = starpu_submit_task(entry_task);
  325. if (STARPU_UNLIKELY(ret == -ENODEV))
  326. {
  327. fprintf(stderr, "No worker may execute this task\n");
  328. exit(-1);
  329. }
  330. /* stall the application until the end of computations */
  331. starpu_tag_wait_array(ndeps, tags);
  332. // starpu_wait_all_tasks();
  333. gettimeofday(&end, NULL);
  334. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  335. return timing;
  336. }
  337. starpu_data_handle get_block_with_striding(starpu_data_handle *dataAp,
  338. unsigned nblocks __attribute__((unused)), unsigned j, unsigned i)
  339. {
  340. /* we use filters */
  341. return starpu_get_sub_data(*dataAp, 2, j, i);
  342. }
  343. void STARPU_LU(lu_decomposition_pivot)(TYPE *matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks)
  344. {
  345. starpu_data_handle dataA;
  346. /* monitor and partition the A matrix into blocks :
  347. * one block is now determined by 2 unsigned (i,j) */
  348. starpu_register_blas_data(&dataA, 0, (uintptr_t)matA, ld, size, size, sizeof(TYPE));
  349. starpu_filter f;
  350. f.filter_func = starpu_vertical_block_filter_func;
  351. f.filter_arg = nblocks;
  352. starpu_filter f2;
  353. f2.filter_func = starpu_block_filter_func;
  354. f2.filter_arg = nblocks;
  355. starpu_map_filters(dataA, 2, &f, &f2);
  356. unsigned i;
  357. for (i = 0; i < size; i++)
  358. ipiv[i] = i;
  359. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  360. unsigned block;
  361. for (block = 0; block < nblocks; block++)
  362. {
  363. piv_description[block].piv = ipiv;
  364. piv_description[block].first = block * (size / nblocks);
  365. piv_description[block].last = (block + 1) * (size / nblocks);
  366. }
  367. #if 0
  368. unsigned j;
  369. for (j = 0; j < nblocks; j++)
  370. for (i = 0; i < nblocks; i++)
  371. {
  372. printf("BLOCK %d %d %p\n", i, j, &matA[i*(size/nblocks) + j * (size/nblocks)*ld]);
  373. }
  374. #endif
  375. double timing;
  376. timing = dw_codelet_facto_pivot(&dataA, piv_description, nblocks, get_block_with_striding);
  377. fprintf(stderr, "Computation took (in ms)\n");
  378. fprintf(stderr, "%2.2f\n", timing/1000);
  379. unsigned n = starpu_get_blas_nx(dataA);
  380. double flop = (2.0f*n*n*n)/3.0f;
  381. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  382. /* gather all the data */
  383. starpu_unpartition_data(dataA, 0);
  384. }
  385. starpu_data_handle get_block_with_no_striding(starpu_data_handle *dataAp, unsigned nblocks, unsigned j, unsigned i)
  386. {
  387. /* dataAp is an array of data handle */
  388. return dataAp[i+j*nblocks];
  389. }
  390. void STARPU_LU(lu_decomposition_pivot_no_stride)(TYPE **matA, unsigned *ipiv, unsigned size, unsigned ld, unsigned nblocks)
  391. {
  392. starpu_data_handle *dataAp = malloc(nblocks*nblocks*sizeof(starpu_data_handle));
  393. /* monitor and partition the A matrix into blocks :
  394. * one block is now determined by 2 unsigned (i,j) */
  395. unsigned bi, bj;
  396. for (bj = 0; bj < nblocks; bj++)
  397. for (bi = 0; bi < nblocks; bi++)
  398. {
  399. starpu_register_blas_data(&dataAp[bi+nblocks*bj], 0,
  400. (uintptr_t)matA[bi+nblocks*bj], size/nblocks,
  401. size/nblocks, size/nblocks, sizeof(TYPE));
  402. }
  403. unsigned i;
  404. for (i = 0; i < size; i++)
  405. ipiv[i] = i;
  406. struct piv_s *piv_description = malloc(nblocks*sizeof(struct piv_s));
  407. unsigned block;
  408. for (block = 0; block < nblocks; block++)
  409. {
  410. piv_description[block].piv = ipiv;
  411. piv_description[block].first = block * (size / nblocks);
  412. piv_description[block].last = (block + 1) * (size / nblocks);
  413. }
  414. double timing;
  415. timing = dw_codelet_facto_pivot(dataAp, piv_description, nblocks, get_block_with_no_striding);
  416. fprintf(stderr, "Computation took (in ms)\n");
  417. fprintf(stderr, "%2.2f\n", timing/1000);
  418. unsigned n = starpu_get_blas_nx(dataAp[0])*nblocks;
  419. double flop = (2.0f*n*n*n)/3.0f;
  420. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  421. for (bj = 0; bj < nblocks; bj++)
  422. for (bi = 0; bi < nblocks; bi++)
  423. {
  424. starpu_delete_data(dataAp[bi+nblocks*bj]);
  425. }
  426. }