cholesky_implicit.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. * Copyright (C) 2011 INRIA
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include "cholesky.h"
  20. /*
  21. * Create the codelets
  22. */
  23. static starpu_codelet cl11 =
  24. {
  25. .where = STARPU_CPU|STARPU_CUDA,
  26. .type = STARPU_SEQ,
  27. .cpu_func = chol_cpu_codelet_update_u11,
  28. #ifdef STARPU_USE_CUDA
  29. .cuda_func = chol_cublas_codelet_update_u11,
  30. #endif
  31. .nbuffers = 1,
  32. .model = &chol_model_11
  33. };
  34. static starpu_codelet cl21 =
  35. {
  36. .where = STARPU_CPU|STARPU_CUDA,
  37. .type = STARPU_SEQ,
  38. .cpu_func = chol_cpu_codelet_update_u21,
  39. #ifdef STARPU_USE_CUDA
  40. .cuda_func = chol_cublas_codelet_update_u21,
  41. #endif
  42. .nbuffers = 2,
  43. .model = &chol_model_21
  44. };
  45. static starpu_codelet cl22 =
  46. {
  47. .where = STARPU_CPU|STARPU_CUDA,
  48. .type = STARPU_SEQ,
  49. .max_parallelism = INT_MAX,
  50. .cpu_func = chol_cpu_codelet_update_u22,
  51. #ifdef STARPU_USE_CUDA
  52. .cuda_func = chol_cublas_codelet_update_u22,
  53. #endif
  54. .nbuffers = 3,
  55. .model = &chol_model_22
  56. };
  57. /*
  58. * code to bootstrap the factorization
  59. * and construct the DAG
  60. */
  61. static void callback_turn_spmd_on(void *arg __attribute__ ((unused)))
  62. {
  63. cl22.type = STARPU_SPMD;
  64. }
  65. static double _cholesky(starpu_data_handle dataA, unsigned nblocks, unsigned sched_ctx, double *timing)
  66. {
  67. struct timeval start;
  68. struct timeval end;
  69. unsigned i,j,k;
  70. int prio_level = noprio?STARPU_DEFAULT_PRIO:STARPU_MAX_PRIO;
  71. gettimeofday(&start, NULL);
  72. /* create all the DAG nodes */
  73. for (k = 0; k < nblocks; k++)
  74. {
  75. starpu_data_handle sdatakk = starpu_data_get_sub_data(dataA, 2, k, k);
  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. for (j = k+1; j<nblocks; j++)
  83. {
  84. starpu_data_handle sdatakj = starpu_data_get_sub_data(dataA, 2, k, j);
  85. starpu_insert_task(&cl21,
  86. STARPU_PRIORITY, (j == k+1)?prio_level:STARPU_DEFAULT_PRIO,
  87. STARPU_R, sdatakk,
  88. STARPU_RW, sdatakj,
  89. STARPU_CTX, sched_ctx,
  90. 0);
  91. for (i = k+1; i<nblocks; i++)
  92. {
  93. if (i <= j)
  94. {
  95. starpu_data_handle sdataki = starpu_data_get_sub_data(dataA, 2, k, i);
  96. starpu_data_handle sdataij = starpu_data_get_sub_data(dataA, 2, i, j);
  97. starpu_insert_task(&cl22,
  98. STARPU_PRIORITY, ((i == k+1) && (j == k+1))?prio_level:STARPU_DEFAULT_PRIO,
  99. STARPU_R, sdataki,
  100. STARPU_R, sdatakj,
  101. STARPU_RW, sdataij,
  102. STARPU_CTX, sched_ctx,
  103. 0);
  104. }
  105. }
  106. }
  107. }
  108. if(sched_ctx != 0)
  109. starpu_wait_for_all_tasks_of_sched_ctx(sched_ctx);
  110. else
  111. starpu_task_wait_for_all();
  112. starpu_data_unpartition(dataA, 0);
  113. gettimeofday(&end, NULL);
  114. (*timing) = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  115. unsigned long n = starpu_matrix_get_nx(dataA);
  116. double flop = (1.0f*n*n*n)/3.0f;
  117. double gflops = (flop/(*timing)/1000.0f);
  118. (*timing) /= 1000000.0f; //sec
  119. // (*timing) /= 60.0f; //min
  120. return gflops;
  121. }
  122. static double cholesky(float *matA, unsigned size, unsigned ld, unsigned nblocks, unsigned sched_ctx, double *timing)
  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. .filter_func = starpu_vertical_block_filter_func,
  130. .nchildren = nblocks
  131. };
  132. struct starpu_data_filter f2 = {
  133. .filter_func = starpu_block_filter_func,
  134. .nchildren = nblocks
  135. };
  136. starpu_data_map_filters(dataA, 2, &f, &f2);
  137. double gflops = _cholesky(dataA, nblocks, sched_ctx, timing);
  138. starpu_data_unregister(dataA);
  139. return gflops;
  140. }
  141. double run_cholesky_implicit(unsigned sched_ctx, int start, int argc, char **argv, double *timing, pthread_barrier_t *barrier)
  142. {
  143. /* create a simple definite positive symetric matrix example
  144. *
  145. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  146. * */
  147. unsigned size = 4 * 1024;
  148. unsigned nblocks = 16;
  149. parse_args_ctx(start, argc, argv, &size, &nblocks);
  150. // starpu_init(NULL);
  151. // starpu_helper_cublas_init();
  152. float *mat;
  153. starpu_malloc((void **)&mat, (size_t)size*size*sizeof(float));
  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. //#define PRINT_OUTPUT
  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. // if(barrier != NULL)
  181. // pthread_barrier_wait(barrier);
  182. double gflops = cholesky(mat, size, size, nblocks, sched_ctx, timing);
  183. #ifdef PRINT_OUTPUT
  184. printf("Results :\n");
  185. for (j = 0; j < size; j++)
  186. {
  187. for (i = 0; i < size; i++)
  188. {
  189. if (i <= j) {
  190. printf("%2.2f\t", mat[j +i*size]);
  191. }
  192. else {
  193. printf(".\t");
  194. mat[j+i*size] = 0.0f; // debug
  195. }
  196. }
  197. printf("\n");
  198. }
  199. #endif
  200. if (check)
  201. {
  202. fprintf(stderr, "compute explicit LLt ...\n");
  203. for (j = 0; j < size; j++)
  204. {
  205. for (i = 0; i < size; i++)
  206. {
  207. if (i > j) {
  208. mat[j+i*size] = 0.0f; // debug
  209. }
  210. }
  211. }
  212. float *test_mat = malloc(size*size*sizeof(float));
  213. STARPU_ASSERT(test_mat);
  214. SSYRK("L", "N", size, size, 1.0f,
  215. mat, size, 0.0f, test_mat, size);
  216. fprintf(stderr, "comparing results ...\n");
  217. #ifdef PRINT_OUTPUT
  218. for (j = 0; j < size; j++)
  219. {
  220. for (i = 0; i < size; i++)
  221. {
  222. if (i <= j) {
  223. printf("%2.2f\t", test_mat[j +i*size]);
  224. }
  225. else {
  226. printf(".\t");
  227. }
  228. }
  229. printf("\n");
  230. }
  231. #endif
  232. for (j = 0; j < size; j++)
  233. {
  234. for (i = 0; i < size; i++)
  235. {
  236. if (i <= j) {
  237. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  238. float err = abs(test_mat[j +i*size] - orig);
  239. if (err > 0.00001) {
  240. fprintf(stderr, "Error[%d, %d] --> %2.2f != %2.2f (err %2.2f)\n", i, j, test_mat[j +i*size], orig, err);
  241. assert(0);
  242. }
  243. }
  244. }
  245. }
  246. }
  247. starpu_free((void *)mat);
  248. // starpu_helper_cublas_shutdown();
  249. // starpu_shutdown();
  250. return gflops;
  251. }