cholesky_implicit.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 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 void _cholesky(starpu_data_handle_t dataA, unsigned nblocks)
  68. {
  69. struct timeval start;
  70. struct timeval end;
  71. unsigned i,j,k;
  72. int prio_level = noprio?STARPU_DEFAULT_PRIO:STARPU_MAX_PRIO;
  73. gettimeofday(&start, NULL);
  74. if (bound)
  75. starpu_bound_start(0, 0);
  76. /* create all the DAG nodes */
  77. for (k = 0; k < nblocks; k++)
  78. {
  79. starpu_data_handle_t sdatakk = starpu_data_get_sub_data(dataA, 2, k, k);
  80. starpu_insert_task(&cl11,
  81. STARPU_PRIORITY, prio_level,
  82. STARPU_RW, sdatakk,
  83. STARPU_CALLBACK, (k == 3*nblocks/4)?callback_turn_spmd_on:NULL,
  84. 0);
  85. for (j = k+1; j<nblocks; j++)
  86. {
  87. starpu_data_handle_t sdatakj = starpu_data_get_sub_data(dataA, 2, k, j);
  88. starpu_insert_task(&cl21,
  89. STARPU_PRIORITY, (j == k+1)?prio_level:STARPU_DEFAULT_PRIO,
  90. STARPU_R, sdatakk,
  91. STARPU_RW, sdatakj,
  92. 0);
  93. for (i = k+1; i<nblocks; i++)
  94. {
  95. if (i <= j)
  96. {
  97. starpu_data_handle_t sdataki = starpu_data_get_sub_data(dataA, 2, k, i);
  98. starpu_data_handle_t sdataij = starpu_data_get_sub_data(dataA, 2, i, j);
  99. starpu_insert_task(&cl22,
  100. STARPU_PRIORITY, ((i == k+1) && (j == k+1))?prio_level:STARPU_DEFAULT_PRIO,
  101. STARPU_R, sdataki,
  102. STARPU_R, sdatakj,
  103. STARPU_RW, sdataij,
  104. 0);
  105. }
  106. }
  107. }
  108. }
  109. starpu_task_wait_for_all();
  110. if (bound)
  111. starpu_bound_stop();
  112. starpu_data_unpartition(dataA, 0);
  113. gettimeofday(&end, NULL);
  114. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  115. FPRINTF(stderr, "Computation took (in ms)\n");
  116. FPRINTF(stdout, "%2.2f\n", timing/1000);
  117. unsigned long n = starpu_matrix_get_nx(dataA);
  118. double flop = (1.0f*n*n*n)/3.0f;
  119. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  120. if (bound)
  121. {
  122. double res;
  123. starpu_bound_compute(&res, NULL, 0);
  124. FPRINTF(stderr, "Theoretical GFlops: %2.2f\n", (flop/res/1000000.0f));
  125. }
  126. }
  127. static void cholesky(float *matA, unsigned size, unsigned ld, unsigned nblocks)
  128. {
  129. starpu_data_handle_t dataA;
  130. /* monitor and partition the A matrix into blocks :
  131. * one block is now determined by 2 unsigned (i,j) */
  132. starpu_matrix_data_register(&dataA, 0, (uintptr_t)matA, ld, size, size, sizeof(float));
  133. struct starpu_data_filter f =
  134. {
  135. .filter_func = starpu_vertical_block_filter_func,
  136. .nchildren = nblocks
  137. };
  138. struct starpu_data_filter f2 =
  139. {
  140. .filter_func = starpu_block_filter_func,
  141. .nchildren = nblocks
  142. };
  143. starpu_data_map_filters(dataA, 2, &f, &f2);
  144. _cholesky(dataA, nblocks);
  145. starpu_data_unregister(dataA);
  146. }
  147. int main(int argc, char **argv)
  148. {
  149. /* create a simple definite positive symetric matrix example
  150. *
  151. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  152. * */
  153. parse_args(argc, argv);
  154. starpu_init(NULL);
  155. starpu_helper_cublas_init();
  156. float *mat;
  157. starpu_malloc((void **)&mat, (size_t)size*size*sizeof(float));
  158. unsigned i,j;
  159. for (i = 0; i < size; i++)
  160. {
  161. for (j = 0; j < size; j++)
  162. {
  163. mat[j +i*size] = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  164. /* mat[j +i*size] = ((i == j)?1.0f*size:0.0f); */
  165. }
  166. }
  167. /* #define PRINT_OUTPUT */
  168. #ifdef PRINT_OUTPUT
  169. FPRINTF(stdout, "Input :\n");
  170. for (j = 0; j < size; j++)
  171. {
  172. for (i = 0; i < size; i++)
  173. {
  174. if (i <= j)
  175. {
  176. FPRINTF(stdout, "%2.2f\t", mat[j +i*size]);
  177. }
  178. else
  179. {
  180. FPRINTF(stdout, ".\t");
  181. }
  182. }
  183. FPRINTF(stdout, "\n");
  184. }
  185. #endif
  186. cholesky(mat, size, size, nblocks);
  187. #ifdef PRINT_OUTPUT
  188. FPRINTF(stdout, "Results :\n");
  189. for (j = 0; j < size; j++)
  190. {
  191. for (i = 0; i < size; i++)
  192. {
  193. if (i <= j)
  194. {
  195. FPRINTF(stdout, "%2.2f\t", mat[j +i*size]);
  196. }
  197. else
  198. {
  199. FPRINTF(stdout, ".\t");
  200. mat[j+i*size] = 0.0f; /* debug */
  201. }
  202. }
  203. FPRINTF(stdout, "\n");
  204. }
  205. #endif
  206. if (check)
  207. {
  208. FPRINTF(stderr, "compute explicit LLt ...\n");
  209. for (j = 0; j < size; j++)
  210. {
  211. for (i = 0; i < size; i++)
  212. {
  213. if (i > j)
  214. {
  215. mat[j+i*size] = 0.0f; /* debug */
  216. }
  217. }
  218. }
  219. float *test_mat = malloc(size*size*sizeof(float));
  220. STARPU_ASSERT(test_mat);
  221. SSYRK("L", "N", size, size, 1.0f,
  222. mat, size, 0.0f, test_mat, size);
  223. FPRINTF(stderr, "comparing results ...\n");
  224. #ifdef PRINT_OUTPUT
  225. for (j = 0; j < size; j++)
  226. {
  227. for (i = 0; i < size; i++)
  228. {
  229. if (i <= j)
  230. {
  231. FPRINTF(stdout, "%2.2f\t", test_mat[j +i*size]);
  232. }
  233. else
  234. {
  235. FPRINTF(stdout, ".\t");
  236. }
  237. }
  238. FPRINTF(stdout, "\n");
  239. }
  240. #endif
  241. for (j = 0; j < size; j++)
  242. {
  243. for (i = 0; i < size; i++)
  244. {
  245. if (i <= j)
  246. {
  247. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  248. float err = abs(test_mat[j +i*size] - orig);
  249. if (err > 0.00001)
  250. {
  251. FPRINTF(stderr, "Error[%u, %u] --> %2.2f != %2.2f (err %2.2f)\n", i, j, test_mat[j +i*size], orig, err);
  252. assert(0);
  253. }
  254. }
  255. }
  256. }
  257. }
  258. starpu_helper_cublas_shutdown();
  259. starpu_shutdown();
  260. return 0;
  261. }