cholesky_implicit.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2014 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012, 2013 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 "cholesky.h"
  19. #include "../sched_ctx_utils/sched_ctx_utils.h"
  20. /*
  21. * Create the codelets
  22. */
  23. struct starpu_perfmodel chol_model_11;
  24. struct starpu_perfmodel chol_model_21;
  25. struct starpu_perfmodel chol_model_22;
  26. static struct starpu_codelet cl11 =
  27. {
  28. .type = STARPU_SEQ,
  29. .cpu_funcs = {chol_cpu_codelet_update_u11, NULL},
  30. #ifdef STARPU_USE_CUDA
  31. .cuda_funcs = {chol_cublas_codelet_update_u11, NULL},
  32. #elif defined(STARPU_SIMGRID)
  33. .cuda_funcs = {(void*)1, NULL},
  34. #endif
  35. .nbuffers = 1,
  36. .modes = {STARPU_RW},
  37. .model = &chol_model_11
  38. };
  39. static struct starpu_codelet cl21 =
  40. {
  41. .type = STARPU_SEQ,
  42. .cpu_funcs = {chol_cpu_codelet_update_u21, NULL},
  43. #ifdef STARPU_USE_CUDA
  44. .cuda_funcs = {chol_cublas_codelet_update_u21, NULL},
  45. #elif defined(STARPU_SIMGRID)
  46. .cuda_funcs = {(void*)1, NULL},
  47. #endif
  48. .nbuffers = 2,
  49. .modes = {STARPU_R, STARPU_RW},
  50. .model = &chol_model_21
  51. };
  52. static struct starpu_codelet cl22 =
  53. {
  54. .type = STARPU_SEQ,
  55. .max_parallelism = INT_MAX,
  56. .cpu_funcs = {chol_cpu_codelet_update_u22, NULL},
  57. #ifdef STARPU_USE_CUDA
  58. .cuda_funcs = {chol_cublas_codelet_update_u22, NULL},
  59. #elif defined(STARPU_SIMGRID)
  60. .cuda_funcs = {(void*)1, NULL},
  61. #endif
  62. .nbuffers = 3,
  63. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  64. .model = &chol_model_22
  65. };
  66. /*
  67. * code to bootstrap the factorization
  68. * and construct the DAG
  69. */
  70. static void callback_turn_spmd_on(void *arg STARPU_ATTRIBUTE_UNUSED)
  71. {
  72. cl22.type = STARPU_SPMD;
  73. }
  74. static int _cholesky(starpu_data_handle_t dataA, unsigned nblocks)
  75. {
  76. int ret;
  77. double start;
  78. double end;
  79. unsigned i,j,k;
  80. unsigned long n = starpu_matrix_get_nx(dataA);
  81. unsigned long nn = n/nblocks;
  82. int prio_level = noprio?STARPU_DEFAULT_PRIO:STARPU_MAX_PRIO;
  83. start = starpu_timing_now();
  84. if (bound || bound_lp || bound_mps)
  85. starpu_bound_start(bound_deps, 0);
  86. /* create all the DAG nodes */
  87. for (k = 0; k < nblocks; k++)
  88. {
  89. starpu_data_handle_t sdatakk = starpu_data_get_sub_data(dataA, 2, k, k);
  90. ret = starpu_task_insert(&cl11,
  91. STARPU_PRIORITY, prio_level,
  92. STARPU_RW, sdatakk,
  93. STARPU_CALLBACK, (k == 3*nblocks/4)?callback_turn_spmd_on:NULL,
  94. STARPU_FLOPS, (double) FLOPS_SPOTRF(nn),
  95. 0);
  96. if (ret == -ENODEV) return 77;
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  98. for (j = k+1; j<nblocks; j++)
  99. {
  100. starpu_data_handle_t sdatakj = starpu_data_get_sub_data(dataA, 2, k, j);
  101. ret = starpu_task_insert(&cl21,
  102. STARPU_PRIORITY, (j == k+1)?prio_level:STARPU_DEFAULT_PRIO,
  103. STARPU_R, sdatakk,
  104. STARPU_RW, sdatakj,
  105. STARPU_FLOPS, (double) FLOPS_STRSM(nn, nn),
  106. 0);
  107. if (ret == -ENODEV) return 77;
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  109. for (i = k+1; i<nblocks; i++)
  110. {
  111. if (i <= j)
  112. {
  113. starpu_data_handle_t sdataki = starpu_data_get_sub_data(dataA, 2, k, i);
  114. starpu_data_handle_t sdataij = starpu_data_get_sub_data(dataA, 2, i, j);
  115. ret = starpu_task_insert(&cl22,
  116. STARPU_PRIORITY, ((i == k+1) && (j == k+1))?prio_level:STARPU_DEFAULT_PRIO,
  117. STARPU_R, sdataki,
  118. STARPU_R, sdatakj,
  119. STARPU_RW, sdataij,
  120. STARPU_FLOPS, (double) FLOPS_SGEMM(nn, nn, nn),
  121. 0);
  122. if (ret == -ENODEV) return 77;
  123. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  124. }
  125. }
  126. }
  127. }
  128. starpu_task_wait_for_all();
  129. if (bound || bound_lp || bound_mps)
  130. starpu_bound_stop();
  131. end = starpu_timing_now();
  132. double timing = end - start;
  133. double flop = FLOPS_SPOTRF(n);
  134. if(with_ctxs || with_noctxs || chole1 || chole2)
  135. update_sched_ctx_timing_results((flop/timing/1000.0f), (timing/1000000.0f));
  136. else
  137. {
  138. FPRINTF(stderr, "Computation took (in ms)\n");
  139. FPRINTF(stdout, "%2.2f\n", timing/1000);
  140. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  141. if (bound_lp)
  142. {
  143. FILE *f = fopen("cholesky.lp", "w");
  144. starpu_bound_print_lp(f);
  145. }
  146. if (bound_mps)
  147. {
  148. FILE *f = fopen("cholesky.mps", "w");
  149. starpu_bound_print_mps(f);
  150. }
  151. if (bound)
  152. {
  153. double res;
  154. starpu_bound_compute(&res, NULL, 0);
  155. FPRINTF(stderr, "Theoretical makespan: %2.2f\n", res);
  156. FPRINTF(stderr, "Theoretical GFlops: %2.2f\n", (flop/res/1000000.0f));
  157. }
  158. }
  159. return 0;
  160. }
  161. static int cholesky(float *matA, unsigned size, unsigned ld, unsigned nblocks)
  162. {
  163. starpu_data_handle_t dataA;
  164. /* monitor and partition the A matrix into blocks :
  165. * one block is now determined by 2 unsigned (i,j) */
  166. starpu_matrix_data_register(&dataA, STARPU_MAIN_RAM, (uintptr_t)matA, ld, size, size, sizeof(float));
  167. struct starpu_data_filter f =
  168. {
  169. .filter_func = starpu_matrix_filter_vertical_block,
  170. .nchildren = nblocks
  171. };
  172. struct starpu_data_filter f2 =
  173. {
  174. .filter_func = starpu_matrix_filter_block,
  175. .nchildren = nblocks
  176. };
  177. starpu_data_map_filters(dataA, 2, &f, &f2);
  178. int ret = _cholesky(dataA, nblocks);
  179. starpu_data_unpartition(dataA, STARPU_MAIN_RAM);
  180. starpu_data_unregister(dataA);
  181. return ret;
  182. }
  183. static void execute_cholesky(unsigned size, unsigned nblocks)
  184. {
  185. int ret;
  186. float *mat = NULL;
  187. unsigned i,j;
  188. #ifndef STARPU_SIMGRID
  189. starpu_malloc((void **)&mat, (size_t)size*size*sizeof(float));
  190. for (i = 0; i < size; i++)
  191. {
  192. for (j = 0; j < size; j++)
  193. {
  194. mat[j +i*size] = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  195. /* mat[j +i*size] = ((i == j)?1.0f*size:0.0f); */
  196. }
  197. }
  198. #endif
  199. /* #define PRINT_OUTPUT */
  200. #ifdef PRINT_OUTPUT
  201. FPRINTF(stdout, "Input :\n");
  202. for (j = 0; j < size; j++)
  203. {
  204. for (i = 0; i < size; i++)
  205. {
  206. if (i <= j)
  207. {
  208. FPRINTF(stdout, "%2.2f\t", mat[j +i*size]);
  209. }
  210. else
  211. {
  212. FPRINTF(stdout, ".\t");
  213. }
  214. }
  215. FPRINTF(stdout, "\n");
  216. }
  217. #endif
  218. ret = cholesky(mat, size, size, nblocks);
  219. #ifdef PRINT_OUTPUT
  220. FPRINTF(stdout, "Results :\n");
  221. for (j = 0; j < size; j++)
  222. {
  223. for (i = 0; i < size; i++)
  224. {
  225. if (i <= j)
  226. {
  227. FPRINTF(stdout, "%2.2f\t", mat[j +i*size]);
  228. }
  229. else
  230. {
  231. FPRINTF(stdout, ".\t");
  232. mat[j+i*size] = 0.0f; /* debug */
  233. }
  234. }
  235. FPRINTF(stdout, "\n");
  236. }
  237. #endif
  238. if (check)
  239. {
  240. FPRINTF(stderr, "compute explicit LLt ...\n");
  241. for (j = 0; j < size; j++)
  242. {
  243. for (i = 0; i < size; i++)
  244. {
  245. if (i > j)
  246. {
  247. mat[j+i*size] = 0.0f; /* debug */
  248. }
  249. }
  250. }
  251. float *test_mat = malloc(size*size*sizeof(float));
  252. STARPU_ASSERT(test_mat);
  253. SSYRK("L", "N", size, size, 1.0f,
  254. mat, size, 0.0f, test_mat, size);
  255. FPRINTF(stderr, "comparing results ...\n");
  256. #ifdef PRINT_OUTPUT
  257. for (j = 0; j < size; j++)
  258. {
  259. for (i = 0; i < size; i++)
  260. {
  261. if (i <= j)
  262. {
  263. FPRINTF(stdout, "%2.2f\t", test_mat[j +i*size]);
  264. }
  265. else
  266. {
  267. FPRINTF(stdout, ".\t");
  268. }
  269. }
  270. FPRINTF(stdout, "\n");
  271. }
  272. #endif
  273. for (j = 0; j < size; j++)
  274. {
  275. for (i = 0; i < size; i++)
  276. {
  277. if (i <= j)
  278. {
  279. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  280. float err = abs(test_mat[j +i*size] - orig);
  281. if (err > 0.00001)
  282. {
  283. FPRINTF(stderr, "Error[%u, %u] --> %2.2f != %2.2f (err %2.2f)\n", i, j, test_mat[j +i*size], orig, err);
  284. assert(0);
  285. }
  286. }
  287. }
  288. }
  289. free(test_mat);
  290. }
  291. starpu_free(mat);
  292. }
  293. int main(int argc, char **argv)
  294. {
  295. /* create a simple definite positive symetric matrix example
  296. *
  297. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  298. * */
  299. parse_args(argc, argv);
  300. if(with_ctxs || with_noctxs || chole1 || chole2)
  301. parse_args_ctx(argc, argv);
  302. int ret;
  303. ret = starpu_init(NULL);
  304. if (ret == -ENODEV)
  305. return 77;
  306. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  307. #ifdef STARPU_USE_CUDA
  308. initialize_chol_model(&chol_model_11,"chol_model_11",cpu_chol_task_11_cost,cuda_chol_task_11_cost);
  309. initialize_chol_model(&chol_model_21,"chol_model_21",cpu_chol_task_21_cost,cuda_chol_task_21_cost);
  310. initialize_chol_model(&chol_model_22,"chol_model_22",cpu_chol_task_22_cost,cuda_chol_task_22_cost);
  311. #else
  312. initialize_chol_model(&chol_model_11,"chol_model_11",cpu_chol_task_11_cost,NULL);
  313. initialize_chol_model(&chol_model_21,"chol_model_21",cpu_chol_task_21_cost,NULL);
  314. initialize_chol_model(&chol_model_22,"chol_model_22",cpu_chol_task_22_cost,NULL);
  315. #endif
  316. starpu_cublas_init();
  317. if(with_ctxs)
  318. {
  319. construct_contexts(execute_cholesky);
  320. start_2benchs(execute_cholesky);
  321. }
  322. else if(with_noctxs)
  323. start_2benchs(execute_cholesky);
  324. else if(chole1)
  325. start_1stbench(execute_cholesky);
  326. else if(chole2)
  327. start_2ndbench(execute_cholesky);
  328. else
  329. execute_cholesky(size, nblocks);
  330. starpu_cublas_shutdown();
  331. starpu_shutdown();
  332. return ret;
  333. }