cholesky_implicit.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "dw_cholesky.h"
  17. #include "dw_cholesky_models.h"
  18. /*
  19. * Create the codelets
  20. */
  21. static starpu_codelet cl11 =
  22. {
  23. .where = STARPU_CPU|STARPU_CUDA,
  24. .cpu_func = chol_cpu_codelet_update_u11,
  25. #ifdef STARPU_USE_CUDA
  26. .cuda_func = chol_cublas_codelet_update_u11,
  27. #endif
  28. .nbuffers = 1,
  29. .model = &chol_model_11
  30. };
  31. static starpu_codelet cl21 =
  32. {
  33. .where = STARPU_CPU|STARPU_CUDA,
  34. .cpu_func = chol_cpu_codelet_update_u21,
  35. #ifdef STARPU_USE_CUDA
  36. .cuda_func = chol_cublas_codelet_update_u21,
  37. #endif
  38. .nbuffers = 2,
  39. .model = &chol_model_21
  40. };
  41. static starpu_codelet cl22 =
  42. {
  43. .where = STARPU_CPU|STARPU_CUDA,
  44. .cpu_func = chol_cpu_codelet_update_u22,
  45. #ifdef STARPU_USE_CUDA
  46. .cuda_func = chol_cublas_codelet_update_u22,
  47. #endif
  48. .nbuffers = 3,
  49. .model = &chol_model_22
  50. };
  51. /*
  52. * code to bootstrap the factorization
  53. * and construct the DAG
  54. */
  55. static void _dw_cholesky(starpu_data_handle dataA, unsigned nblocks)
  56. {
  57. struct timeval start;
  58. struct timeval end;
  59. /* create all the DAG nodes */
  60. unsigned i,j,k;
  61. gettimeofday(&start, NULL);
  62. for (k = 0; k < nblocks; k++)
  63. {
  64. starpu_data_handle subdata = starpu_data_get_sub_data(dataA, 2, k, k);
  65. int prio = STARPU_DEFAULT_PRIO;
  66. if (!noprio) prio = STARPU_MAX_PRIO;
  67. starpu_insert_task(&cl11,
  68. STARPU_PRIORITY, prio,
  69. STARPU_RW, subdata,
  70. 0);
  71. for (j = k+1; j<nblocks; j++)
  72. {
  73. starpu_data_handle sdatakk = starpu_data_get_sub_data(dataA, 2, k, k);
  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 sdatakj = starpu_data_get_sub_data(dataA, 2, k, j);
  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. float *mat;
  152. mat = malloc(size*size*sizeof(float));
  153. initialize_system(&mat, size, pinned);
  154. unsigned i,j;
  155. for (i = 0; i < size; i++)
  156. {
  157. for (j = 0; j < size; j++)
  158. {
  159. mat[j +i*size] = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  160. //mat[j +i*size] = ((i == j)?1.0f*size:0.0f);
  161. }
  162. }
  163. #ifdef CHECK_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. dw_cholesky(mat, size, size, nblocks);
  180. #ifdef CHECK_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. fprintf(stderr, "compute explicit LLt ...\n");
  197. float *test_mat = malloc(size*size*sizeof(float));
  198. STARPU_ASSERT(test_mat);
  199. SSYRK("L", "N", size, size, 1.0f,
  200. mat, size, 0.0f, test_mat, size);
  201. fprintf(stderr, "comparing results ...\n");
  202. for (j = 0; j < size; j++)
  203. {
  204. for (i = 0; i < size; i++)
  205. {
  206. if (i <= j) {
  207. printf("%2.2f\t", test_mat[j +i*size]);
  208. }
  209. else {
  210. printf(".\t");
  211. }
  212. }
  213. printf("\n");
  214. }
  215. #endif
  216. return 0;
  217. }