cholesky_implicit.c 7.6 KB

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