cholesky_implicit.c 9.5 KB

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