cholesky_implicit.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. #include "../sched_ctx_utils/sched_ctx_utils.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. int hypervisor_tag = 1;
  66. static void _cholesky(starpu_data_handle dataA, unsigned nblocks)
  67. {
  68. struct timeval start;
  69. struct timeval end;
  70. unsigned i,j,k;
  71. int prio_level = noprio?STARPU_DEFAULT_PRIO:STARPU_MAX_PRIO;
  72. gettimeofday(&start, NULL);
  73. /* create all the DAG nodes */
  74. for (k = 0; k < nblocks; k++)
  75. {
  76. starpu_data_handle sdatakk = starpu_data_get_sub_data(dataA, 2, k, k);
  77. if(k == 0 && with_ctxs)
  78. {
  79. starpu_insert_task(&cl11,
  80. STARPU_PRIORITY, prio_level,
  81. STARPU_RW, sdatakk,
  82. STARPU_CALLBACK, (k == 3*nblocks/4)?callback_turn_spmd_on:NULL,
  83. STARPU_HYPERVISOR_TAG, hypervisor_tag,
  84. 0);
  85. set_hypervisor_conf(START_BENCH, hypervisor_tag++);
  86. }
  87. else
  88. starpu_insert_task(&cl11,
  89. STARPU_PRIORITY, prio_level,
  90. STARPU_RW, sdatakk,
  91. STARPU_CALLBACK, (k == 3*nblocks/4)?callback_turn_spmd_on:NULL,
  92. 0);
  93. for (j = k+1; j<nblocks; j++)
  94. {
  95. starpu_data_handle sdatakj = starpu_data_get_sub_data(dataA, 2, k, j);
  96. starpu_insert_task(&cl21,
  97. STARPU_PRIORITY, (j == k+1)?prio_level:STARPU_DEFAULT_PRIO,
  98. STARPU_R, sdatakk,
  99. STARPU_RW, sdatakj,
  100. 0);
  101. for (i = k+1; i<nblocks; i++)
  102. {
  103. if (i <= j)
  104. {
  105. starpu_data_handle sdataki = starpu_data_get_sub_data(dataA, 2, k, i);
  106. starpu_data_handle sdataij = starpu_data_get_sub_data(dataA, 2, i, j);
  107. if(k == (nblocks-1) && j == (nblocks-1) &&
  108. i == (nblocks-1) && with_ctxs)
  109. {
  110. starpu_insert_task(&cl22,
  111. STARPU_PRIORITY, ((i == k+1) && (j == k+1))?prio_level:STARPU_DEFAULT_PRIO,
  112. STARPU_R, sdataki,
  113. STARPU_R, sdatakj,
  114. STARPU_RW, sdataij,
  115. STARPU_HYPERVISOR_TAG, hypervisor_tag,
  116. 0);
  117. set_hypervisor_conf(END_BENCH, hypervisor_tag++);
  118. }
  119. else
  120. starpu_insert_task(&cl22,
  121. STARPU_PRIORITY, ((i == k+1) && (j == k+1))?prio_level:STARPU_DEFAULT_PRIO,
  122. STARPU_R, sdataki,
  123. STARPU_R, sdatakj,
  124. STARPU_RW, sdataij,
  125. 0);
  126. }
  127. }
  128. }
  129. }
  130. starpu_task_wait_for_all();
  131. starpu_data_unpartition(dataA, 0);
  132. gettimeofday(&end, NULL);
  133. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  134. unsigned long n = starpu_matrix_get_nx(dataA);
  135. double flop = (1.0f*n*n*n)/3.0f;
  136. if(with_ctxs || with_noctxs || chole1 || chole2)
  137. update_sched_ctx_timing_results((flop/timing/1000.0f), (timing/1000000.0f));
  138. else
  139. {
  140. FPRINTF(stderr, "Computation took (in ms)\n");
  141. FPRINTF(stdout, "%2.2f\n", timing/1000);
  142. FPRINTF(stderr, "Synthetic GFlops : %2.2f\n", (flop/timing/1000.0f));
  143. }
  144. }
  145. static void cholesky(float *matA, unsigned size, unsigned ld, unsigned nblocks)
  146. {
  147. starpu_data_handle dataA;
  148. /* monitor and partition the A matrix into blocks :
  149. * one block is now determined by 2 unsigned (i,j) */
  150. starpu_matrix_data_register(&dataA, 0, (uintptr_t)matA, ld, size, size, sizeof(float));
  151. struct starpu_data_filter f = {
  152. .filter_func = starpu_vertical_block_filter_func,
  153. .nchildren = nblocks
  154. };
  155. struct starpu_data_filter f2 = {
  156. .filter_func = starpu_block_filter_func,
  157. .nchildren = nblocks
  158. };
  159. starpu_data_map_filters(dataA, 2, &f, &f2);
  160. _cholesky(dataA, nblocks);
  161. }
  162. static void execute_cholesky(unsigned size, unsigned nblocks)
  163. {
  164. float *mat;
  165. starpu_malloc((void **)&mat, (size_t)size*size*sizeof(float));
  166. unsigned i,j;
  167. for (i = 0; i < size; i++)
  168. {
  169. for (j = 0; j < size; j++)
  170. {
  171. mat[j +i*size] = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  172. /* mat[j +i*size] = ((i == j)?1.0f*size:0.0f); */
  173. }
  174. }
  175. /* #define PRINT_OUTPUT */
  176. #ifdef PRINT_OUTPUT
  177. FPRINTF(stdout, "Input :\n");
  178. for (j = 0; j < size; j++)
  179. {
  180. for (i = 0; i < size; i++)
  181. {
  182. if (i <= j) {
  183. FPRINTF(stdout, "%2.2f\t", mat[j +i*size]);
  184. }
  185. else {
  186. FPRINTF(stdout, ".\t");
  187. }
  188. }
  189. FPRINTF(stdout, "\n");
  190. }
  191. #endif
  192. cholesky(mat, size, size, nblocks);
  193. #ifdef PRINT_OUTPUT
  194. FPRINTF(stdout, "Results :\n");
  195. for (j = 0; j < size; j++)
  196. {
  197. for (i = 0; i < size; i++)
  198. {
  199. if (i <= j) {
  200. FPRINTF(stdout, "%2.2f\t", mat[j +i*size]);
  201. }
  202. else {
  203. FPRINTF(stdout, ".\t");
  204. mat[j+i*size] = 0.0f; /* debug */
  205. }
  206. }
  207. FPRINTF(stdout, "\n");
  208. }
  209. #endif
  210. if (check)
  211. {
  212. FPRINTF(stderr, "compute explicit LLt ...\n");
  213. for (j = 0; j < size; j++)
  214. {
  215. for (i = 0; i < size; i++)
  216. {
  217. if (i > j) {
  218. mat[j+i*size] = 0.0f; /* debug */
  219. }
  220. }
  221. }
  222. float *test_mat = malloc(size*size*sizeof(float));
  223. STARPU_ASSERT(test_mat);
  224. SSYRK("L", "N", size, size, 1.0f,
  225. mat, size, 0.0f, test_mat, size);
  226. FPRINTF(stderr, "comparing results ...\n");
  227. #ifdef PRINT_OUTPUT
  228. for (j = 0; j < size; j++)
  229. {
  230. for (i = 0; i < size; i++)
  231. {
  232. if (i <= j) {
  233. FPRINTF(stdout, "%2.2f\t", test_mat[j +i*size]);
  234. }
  235. else {
  236. FPRINTF(stdout, ".\t");
  237. }
  238. }
  239. FPRINTF(stdout, "\n");
  240. }
  241. #endif
  242. for (j = 0; j < size; j++)
  243. {
  244. for (i = 0; i < size; i++)
  245. {
  246. if (i <= j) {
  247. float orig = (1.0f/(1.0f+i+j)) + ((i == j)?1.0f*size:0.0f);
  248. float err = abs(test_mat[j +i*size] - orig);
  249. if (err > 0.00001) {
  250. FPRINTF(stderr, "Error[%u, %u] --> %2.2f != %2.2f (err %2.2f)\n", i, j, test_mat[j +i*size], orig, err);
  251. assert(0);
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. int main(int argc, char **argv)
  259. {
  260. /* create a simple definite positive symetric matrix example
  261. *
  262. * Hilbert matrix : h(i,j) = 1/(i+j+1)
  263. * */
  264. parse_args(argc, argv);
  265. if(with_ctxs || with_noctxs || chole1 || chole2)
  266. parse_args_ctx(argc, argv);
  267. starpu_init(NULL);
  268. starpu_helper_cublas_init();
  269. if(with_ctxs)
  270. {
  271. construct_contexts(execute_cholesky);
  272. start_2benchs(execute_cholesky);
  273. }
  274. else if(with_noctxs)
  275. start_2benchs(execute_cholesky);
  276. else if(chole1)
  277. start_1stbench(execute_cholesky);
  278. else if(chole2)
  279. start_2ndbench(execute_cholesky);
  280. else
  281. execute_cholesky(size, nblocks);
  282. starpu_helper_cublas_shutdown();
  283. starpu_shutdown();
  284. if(with_ctxs)
  285. end_contexts();
  286. return 0;
  287. }