cholesky_implicit.c 8.3 KB

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