cholesky_implicit.c 7.2 KB

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