cholesky_implicit.c 8.1 KB

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