cholesky_implicit.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011 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 "dw_cholesky.h"
  19. #include "dw_cholesky_models.h"
  20. /*
  21. * Create the codelets
  22. */
  23. static starpu_codelet cl11 =
  24. {
  25. .where = STARPU_CPU|STARPU_CUDA,
  26. .cpu_func = chol_cpu_codelet_update_u11,
  27. #ifdef STARPU_USE_CUDA
  28. .cuda_func = chol_cublas_codelet_update_u11,
  29. #endif
  30. .nbuffers = 1,
  31. .model = &chol_model_11
  32. };
  33. static starpu_codelet cl21 =
  34. {
  35. .where = STARPU_CPU|STARPU_CUDA,
  36. .cpu_func = chol_cpu_codelet_update_u21,
  37. #ifdef STARPU_USE_CUDA
  38. .cuda_func = chol_cublas_codelet_update_u21,
  39. #endif
  40. .nbuffers = 2,
  41. .model = &chol_model_21
  42. };
  43. static starpu_codelet cl22 =
  44. {
  45. .where = STARPU_CPU|STARPU_CUDA,
  46. .cpu_func = chol_cpu_codelet_update_u22,
  47. #ifdef STARPU_USE_CUDA
  48. .cuda_func = chol_cublas_codelet_update_u22,
  49. #endif
  50. .nbuffers = 3,
  51. .model = &chol_model_22
  52. };
  53. /*
  54. * code to bootstrap the factorization
  55. * and construct the DAG
  56. */
  57. static void _dw_cholesky(starpu_data_handle dataA, unsigned nblocks)
  58. {
  59. struct timeval start;
  60. struct timeval end;
  61. /* create all the DAG nodes */
  62. unsigned i,j,k;
  63. gettimeofday(&start, NULL);
  64. for (k = 0; k < nblocks; k++)
  65. {
  66. starpu_data_handle sdatakk = starpu_data_get_sub_data(dataA, 2, k, k);
  67. int prio = STARPU_DEFAULT_PRIO;
  68. if (!noprio) prio = STARPU_MAX_PRIO;
  69. starpu_insert_task(&cl11,
  70. STARPU_PRIORITY, prio,
  71. STARPU_RW, sdatakk,
  72. 0);
  73. for (j = k+1; j<nblocks; j++)
  74. {
  75. starpu_data_handle sdatakj = starpu_data_get_sub_data(dataA, 2, k, j);
  76. prio = STARPU_DEFAULT_PRIO;
  77. if (!noprio && (j == k+1)) prio = STARPU_MAX_PRIO;
  78. starpu_insert_task(&cl21,
  79. STARPU_PRIORITY, prio,
  80. STARPU_R, sdatakk,
  81. STARPU_RW, sdatakj,
  82. 0);
  83. for (i = k+1; i<nblocks; i++)
  84. {
  85. if (i <= j)
  86. {
  87. starpu_data_handle sdataki = starpu_data_get_sub_data(dataA, 2, k, i);
  88. starpu_data_handle sdataij = starpu_data_get_sub_data(dataA, 2, i, j);
  89. prio = STARPU_DEFAULT_PRIO;
  90. if (!noprio && (i == k + 1) && (j == k +1) ) prio = STARPU_MAX_PRIO;
  91. starpu_insert_task(&cl22,
  92. STARPU_PRIORITY, prio,
  93. STARPU_R, sdataki,
  94. STARPU_R, sdatakj,
  95. STARPU_RW, sdataij,
  96. 0);
  97. }
  98. }
  99. }
  100. }
  101. starpu_task_wait_for_all();
  102. starpu_data_unpartition(dataA, 0);
  103. gettimeofday(&end, NULL);
  104. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  105. fprintf(stderr, "Computation took (in ms)\n");
  106. printf("%2.2f\n", timing/1000);
  107. unsigned n = starpu_matrix_get_nx(dataA);
  108. double flop = (1.0f*n*n*n)/3.0f;
  109. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  110. }
  111. void initialize_system(float **A, unsigned dim, unsigned pinned)
  112. {
  113. starpu_init(NULL);
  114. starpu_helper_cublas_init();
  115. if (pinned)
  116. {
  117. starpu_data_malloc_pinned_if_possible((void **)A, (size_t)dim*dim*sizeof(float));
  118. }
  119. else {
  120. *A = malloc(dim*dim*sizeof(float));
  121. }
  122. }
  123. void dw_cholesky(float *matA, unsigned size, unsigned ld, unsigned nblocks)
  124. {
  125. starpu_data_handle dataA;
  126. /* monitor and partition the A matrix into blocks :
  127. * one block is now determined by 2 unsigned (i,j) */
  128. starpu_matrix_data_register(&dataA, 0, (uintptr_t)matA, ld, size, size, sizeof(float));
  129. struct starpu_data_filter f;
  130. f.filter_func = starpu_vertical_block_filter_func;
  131. f.nchildren = nblocks;
  132. f.get_nchildren = NULL;
  133. f.get_child_ops = NULL;
  134. struct starpu_data_filter f2;
  135. f2.filter_func = starpu_block_filter_func;
  136. f2.nchildren = nblocks;
  137. f2.get_nchildren = NULL;
  138. f2.get_child_ops = NULL;
  139. starpu_data_map_filters(dataA, 2, &f, &f2);
  140. _dw_cholesky(dataA, nblocks);
  141. starpu_helper_cublas_shutdown();
  142. starpu_shutdown();
  143. }
  144. int main(int argc, char **argv)
  145. {
  146. /* create a simple definite positive symetric matrix example
  147. *
  148. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  149. * */
  150. parse_args(argc, argv);
  151. #define CHECK_OUTPUT
  152. float *mat;
  153. mat = malloc(size*size*sizeof(float));
  154. initialize_system(&mat, size, pinned);
  155. unsigned i,j;
  156. for (i = 0; i < size; i++)
  157. {
  158. for (j = 0; j < size; j++)
  159. {
  160. mat[j +i*size] = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  161. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  162. }
  163. }
  164. #ifdef PRINT_OUTPUT
  165. printf("Input :\n");
  166. for (j = 0; j < size; j++)
  167. {
  168. for (i = 0; i < size; i++)
  169. {
  170. if (i <= j) {
  171. printf("%2.2f\t", mat[j +i*size]);
  172. }
  173. else {
  174. printf(".\t");
  175. }
  176. }
  177. printf("\n");
  178. }
  179. #endif
  180. dw_cholesky(mat, size, size, nblocks);
  181. #ifdef PRINT_OUTPUT
  182. printf("Results :\n");
  183. for (j = 0; j < size; j++)
  184. {
  185. for (i = 0; i < size; i++)
  186. {
  187. if (i <= j) {
  188. printf("%2.2f\t", mat[j +i*size]);
  189. }
  190. else {
  191. printf(".\t");
  192. mat[j+i*size] = 0.0f; // debug
  193. }
  194. }
  195. printf("\n");
  196. }
  197. #endif
  198. fprintf(stderr, "compute explicit LLt ...\n");
  199. for (j = 0; j < size; j++)
  200. {
  201. for (i = 0; i < size; i++)
  202. {
  203. if (i > j) {
  204. mat[j+i*size] = 0.0f; // debug
  205. }
  206. }
  207. }
  208. float *test_mat = malloc(size*size*sizeof(float));
  209. STARPU_ASSERT(test_mat);
  210. SSYRK("L", "N", size, size, 1.0f,
  211. mat, size, 0.0f, test_mat, size);
  212. fprintf(stderr, "comparing results ...\n");
  213. #ifdef PRINT_OUTPUT
  214. for (j = 0; j < size; j++)
  215. {
  216. for (i = 0; i < size; i++)
  217. {
  218. if (i <= j) {
  219. printf("%2.2f\t", test_mat[j +i*size]);
  220. }
  221. else {
  222. printf(".\t");
  223. }
  224. }
  225. printf("\n");
  226. }
  227. #endif
  228. for (j = 0; j < size; j++)
  229. {
  230. for (i = 0; i < size; i++)
  231. {
  232. if (i <= j) {
  233. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  234. float err = abs(test_mat[j +i*size] - orig);
  235. if (err > 0.00001) {
  236. fprintf(stderr, "Error[%d, %d] --> %2.2f != %2.2f (err %2.2f)\n", i, j, test_mat[j +i*size], orig, err);
  237. assert(0);
  238. }
  239. }
  240. }
  241. }
  242. return 0;
  243. }