cholesky_implicit.c 8.7 KB

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