cholesky_implicit.c 9.5 KB

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