cholesky_implicit.c 9.4 KB

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