cholesky_implicit.c 7.5 KB

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