cholesky_implicit.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 subdata = 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, subdata,
  72. 0);
  73. for (j = k+1; j<nblocks; j++)
  74. {
  75. starpu_data_handle sdatakk = starpu_data_get_sub_data(dataA, 2, k, k);
  76. starpu_data_handle sdatakj = starpu_data_get_sub_data(dataA, 2, k, j);
  77. prio = STARPU_DEFAULT_PRIO;
  78. if (!noprio&& (j == k+1)) prio = STARPU_MAX_PRIO;
  79. starpu_insert_task(&cl21,
  80. STARPU_PRIORITY, prio,
  81. STARPU_R, sdatakk,
  82. STARPU_RW, sdatakj,
  83. 0);
  84. for (i = k+1; i<nblocks; i++)
  85. {
  86. if (i <= j)
  87. {
  88. starpu_data_handle sdataki = starpu_data_get_sub_data(dataA, 2, k, i);
  89. starpu_data_handle sdatakj = starpu_data_get_sub_data(dataA, 2, k, j);
  90. starpu_data_handle sdataij = starpu_data_get_sub_data(dataA, 2, i, j);
  91. prio = STARPU_DEFAULT_PRIO;
  92. if (!noprio && (i == k + 1) && (j == k +1) ) prio = STARPU_MAX_PRIO;
  93. starpu_insert_task(&cl22,
  94. STARPU_PRIORITY, prio,
  95. STARPU_R, sdataki,
  96. STARPU_R, sdatakj,
  97. STARPU_RW, sdataij,
  98. 0);
  99. }
  100. }
  101. }
  102. }
  103. //starpu_task_wait_for_all();
  104. starpu_data_unpartition(dataA, 0);
  105. gettimeofday(&end, NULL);
  106. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  107. fprintf(stderr, "Computation took (in ms)\n");
  108. printf("%2.2f\n", timing/1000);
  109. unsigned n = starpu_matrix_get_nx(dataA);
  110. double flop = (1.0f*n*n*n)/3.0f;
  111. fprintf(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  112. }
  113. void initialize_system(float **A, unsigned dim, unsigned pinned)
  114. {
  115. starpu_init(NULL);
  116. starpu_helper_cublas_init();
  117. if (pinned)
  118. {
  119. starpu_data_malloc_pinned_if_possible((void **)A, (size_t)dim*dim*sizeof(float));
  120. }
  121. else {
  122. *A = malloc(dim*dim*sizeof(float));
  123. }
  124. }
  125. void dw_cholesky(float *matA, unsigned size, unsigned ld, unsigned nblocks)
  126. {
  127. starpu_data_handle dataA;
  128. /* monitor and partition the A matrix into blocks :
  129. * one block is now determined by 2 unsigned (i,j) */
  130. starpu_matrix_data_register(&dataA, 0, (uintptr_t)matA, ld, size, size, sizeof(float));
  131. struct starpu_data_filter f;
  132. f.filter_func = starpu_vertical_block_filter_func;
  133. f.nchildren = nblocks;
  134. f.get_nchildren = NULL;
  135. f.get_child_ops = NULL;
  136. struct starpu_data_filter f2;
  137. f2.filter_func = starpu_block_filter_func;
  138. f2.nchildren = nblocks;
  139. f2.get_nchildren = NULL;
  140. f2.get_child_ops = NULL;
  141. starpu_data_map_filters(dataA, 2, &f, &f2);
  142. _dw_cholesky(dataA, nblocks);
  143. starpu_helper_cublas_shutdown();
  144. starpu_shutdown();
  145. }
  146. int main(int argc, char **argv)
  147. {
  148. /* create a simple definite positive symetric matrix example
  149. *
  150. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  151. * */
  152. parse_args(argc, argv);
  153. float *mat;
  154. mat = malloc(size*size*sizeof(float));
  155. initialize_system(&mat, size, pinned);
  156. unsigned i,j;
  157. for (i = 0; i < size; i++)
  158. {
  159. for (j = 0; j < size; j++)
  160. {
  161. mat[j +i*size] = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  162. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  163. }
  164. }
  165. #ifdef CHECK_OUTPUT
  166. printf("Input :\n");
  167. for (j = 0; j < size; j++)
  168. {
  169. for (i = 0; i < size; i++)
  170. {
  171. if (i <= j) {
  172. printf("%2.2f\t", mat[j +i*size]);
  173. }
  174. else {
  175. printf(".\t");
  176. }
  177. }
  178. printf("\n");
  179. }
  180. #endif
  181. dw_cholesky(mat, size, size, nblocks);
  182. #ifdef CHECK_OUTPUT
  183. printf("Results :\n");
  184. for (j = 0; j < size; j++)
  185. {
  186. for (i = 0; i < size; i++)
  187. {
  188. if (i <= j) {
  189. printf("%2.2f\t", mat[j +i*size]);
  190. }
  191. else {
  192. printf(".\t");
  193. mat[j+i*size] = 0.0f; // debug
  194. }
  195. }
  196. printf("\n");
  197. }
  198. fprintf(stderr, "compute explicit LLt ...\n");
  199. float *test_mat = malloc(size*size*sizeof(float));
  200. STARPU_ASSERT(test_mat);
  201. SSYRK("L", "N", size, size, 1.0f,
  202. mat, size, 0.0f, test_mat, size);
  203. fprintf(stderr, "comparing results ...\n");
  204. for (j = 0; j < size; j++)
  205. {
  206. for (i = 0; i < size; i++)
  207. {
  208. if (i <= j) {
  209. printf("%2.2f\t", test_mat[j +i*size]);
  210. }
  211. else {
  212. printf(".\t");
  213. }
  214. }
  215. printf("\n");
  216. }
  217. #endif
  218. return 0;
  219. }