gemm_helper.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU 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. * StarPU 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 <common/blas.h>
  17. #include "../../examples/mult/simple.h"
  18. #include "helper.h"
  19. #include "gemm_helper.h"
  20. #define CHECK_TASK_SUBMIT(ret) do { \
  21. if (ret == -ENODEV) \
  22. { \
  23. return -ENODEV; \
  24. } \
  25. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit"); \
  26. } while(0)
  27. unsigned nslices = 4;
  28. #if defined(STARPU_QUICK_CHECK) && !defined(STARPU_SIMGRID)
  29. unsigned matrix_dim = 256;
  30. #else
  31. unsigned matrix_dim = 320 * 4;
  32. #endif
  33. unsigned check = 0;
  34. int comm_thread_cpuid = -1;
  35. static TYPE *A, *B, *C;
  36. static starpu_data_handle_t A_handle, B_handle, C_handle;
  37. static void check_output(void)
  38. {
  39. /* compute C = C - AB */
  40. CPU_GEMM("N", "N", matrix_dim, matrix_dim, matrix_dim, (TYPE)-1.0f, A, matrix_dim, B, matrix_dim, (TYPE)1.0f, C, matrix_dim);
  41. /* make sure C = 0 */
  42. TYPE err;
  43. err = CPU_ASUM(matrix_dim*matrix_dim, C, 1);
  44. if (err < matrix_dim*matrix_dim*0.001)
  45. {
  46. FPRINTF(stderr, "Results are OK\n");
  47. }
  48. else
  49. {
  50. int max;
  51. max = CPU_IAMAX(matrix_dim*matrix_dim, C, 1);
  52. FPRINTF(stderr, "There were errors ... err = %f\n", err);
  53. FPRINTF(stderr, "Max error : %e\n", C[max]);
  54. }
  55. }
  56. static void partition_mult_data(void)
  57. {
  58. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A,
  59. matrix_dim, matrix_dim, matrix_dim, sizeof(TYPE));
  60. starpu_matrix_data_register(&B_handle, STARPU_MAIN_RAM, (uintptr_t)B,
  61. matrix_dim, matrix_dim, matrix_dim, sizeof(TYPE));
  62. starpu_matrix_data_register(&C_handle, STARPU_MAIN_RAM, (uintptr_t)C,
  63. matrix_dim, matrix_dim, matrix_dim, sizeof(TYPE));
  64. struct starpu_data_filter vert;
  65. memset(&vert, 0, sizeof(vert));
  66. vert.filter_func = starpu_matrix_filter_vertical_block;
  67. vert.nchildren = nslices;
  68. struct starpu_data_filter horiz;
  69. memset(&horiz, 0, sizeof(horiz));
  70. horiz.filter_func = starpu_matrix_filter_block;
  71. horiz.nchildren = nslices;
  72. starpu_data_partition(B_handle, &vert);
  73. starpu_data_partition(A_handle, &horiz);
  74. starpu_data_map_filters(C_handle, 2, &vert, &horiz);
  75. }
  76. static void cpu_init_matrix_random(void *descr[], void *arg)
  77. {
  78. (void)arg;
  79. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  80. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  81. unsigned nx = STARPU_MATRIX_GET_NX(descr[0]);
  82. unsigned ny = STARPU_MATRIX_GET_NY(descr[0]);
  83. unsigned i = 0;
  84. for (i = 0; i < nx *ny; i++)
  85. {
  86. subA[i] = (TYPE) (starpu_drand48());
  87. subB[i] = (TYPE) (starpu_drand48());
  88. }
  89. }
  90. static void cpu_init_matrix_zero(void *descr[], void *arg)
  91. {
  92. (void)arg;
  93. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  94. unsigned nx = STARPU_MATRIX_GET_NX(descr[0]);
  95. unsigned ny = STARPU_MATRIX_GET_NY(descr[0]);
  96. unsigned i = 0;
  97. for (i = 0; i < nx *ny; i++)
  98. {
  99. subA[i] = (TYPE) (0);
  100. }
  101. }
  102. static void cpu_mult(void *descr[], void *arg)
  103. {
  104. (void)arg;
  105. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  106. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  107. TYPE *subC = (TYPE *)STARPU_MATRIX_GET_PTR(descr[2]);
  108. unsigned nxC = STARPU_MATRIX_GET_NX(descr[2]);
  109. unsigned nyC = STARPU_MATRIX_GET_NY(descr[2]);
  110. unsigned nyA = STARPU_MATRIX_GET_NY(descr[0]);
  111. unsigned ldA = STARPU_MATRIX_GET_LD(descr[0]);
  112. unsigned ldB = STARPU_MATRIX_GET_LD(descr[1]);
  113. unsigned ldC = STARPU_MATRIX_GET_LD(descr[2]);
  114. int worker_size = starpu_combined_worker_get_size();
  115. if (worker_size == 1)
  116. {
  117. /* Sequential CPU task */
  118. CPU_GEMM("N", "N", nxC, nyC, nyA, (TYPE)1.0, subA, ldA, subB, ldB, (TYPE)0.0, subC, ldC);
  119. }
  120. else
  121. {
  122. /* Parallel CPU task */
  123. unsigned rank = starpu_combined_worker_get_rank();
  124. unsigned block_size = (nyC + worker_size - 1)/worker_size;
  125. unsigned new_nyC = STARPU_MIN(nyC, block_size*(rank+1)) - block_size*rank;
  126. STARPU_ASSERT(nyC == STARPU_MATRIX_GET_NY(descr[1]));
  127. TYPE *new_subB = &subB[block_size*rank];
  128. TYPE *new_subC = &subC[block_size*rank];
  129. CPU_GEMM("N", "N", nxC, new_nyC, nyA, (TYPE)1.0, subA, ldA, new_subB, ldB, (TYPE)0.0, new_subC, ldC);
  130. }
  131. }
  132. static struct starpu_perfmodel starpu_gemm_model =
  133. {
  134. .type = STARPU_HISTORY_BASED,
  135. .symbol = STARPU_GEMM_STR(gemm)
  136. };
  137. static struct starpu_codelet cl =
  138. {
  139. .type = STARPU_SEQ, /* changed to STARPU_SPMD if -spmd is passed */
  140. .max_parallelism = INT_MAX,
  141. .cpu_funcs = {cpu_mult},
  142. .cpu_funcs_name = {"cpu_mult"},
  143. .nbuffers = 3,
  144. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  145. .model = &starpu_gemm_model
  146. };
  147. static struct starpu_codelet cl_init_matrix_random =
  148. {
  149. .max_parallelism = INT_MAX,
  150. .cpu_funcs = {cpu_init_matrix_random},
  151. .cpu_funcs_name = {"cpu_init_matrix_random"},
  152. .nbuffers = 2,
  153. .modes = {STARPU_W, STARPU_W},
  154. .name = "init_matrix_random",
  155. .color = 0xffa500 // orange
  156. };
  157. static struct starpu_codelet cl_init_matrix_zero =
  158. {
  159. .max_parallelism = INT_MAX,
  160. .cpu_funcs = {cpu_init_matrix_zero},
  161. .cpu_funcs_name = {"cpu_init_matrix_zero"},
  162. .nbuffers = 1,
  163. .modes = {STARPU_W},
  164. .name = "init_matrix_zero",
  165. .color = 0x808000 // olive
  166. };
  167. /* Allocate and partition buffers */
  168. void gemm_alloc_data()
  169. {
  170. starpu_malloc_flags((void **)&A, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  171. starpu_malloc_flags((void **)&B, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  172. starpu_malloc_flags((void **)&C, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  173. partition_mult_data();
  174. }
  175. /* Submit tasks to initialize matrices: fill them with zeros or random numbers */
  176. int gemm_init_data()
  177. {
  178. #ifndef STARPU_SIMGRID
  179. int ret;
  180. unsigned x, y;
  181. for (x = 0; x < nslices; x++)
  182. {
  183. struct starpu_task *task = starpu_task_create();
  184. task->cl = &cl_init_matrix_random;
  185. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, x);
  186. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, x);
  187. ret = starpu_task_submit(task);
  188. CHECK_TASK_SUBMIT(ret);
  189. for (y = 0; y < nslices; y++)
  190. {
  191. task = starpu_task_create();
  192. task->cl = &cl_init_matrix_zero;
  193. task->handles[0] = starpu_data_get_sub_data(C_handle, 2, x, y);
  194. ret = starpu_task_submit(task);
  195. CHECK_TASK_SUBMIT(ret);
  196. }
  197. }
  198. #endif
  199. return 0;
  200. }
  201. /* Submit tasks to compute the GEMM */
  202. int gemm_submit_tasks()
  203. {
  204. return gemm_submit_tasks_with_tags(/* by default, disable task tags */ 0);
  205. }
  206. int gemm_submit_tasks_with_tags(int with_tags)
  207. {
  208. int ret;
  209. unsigned x, y;
  210. starpu_tag_t task_tag = 0;
  211. for (x = 0; x < nslices; x++)
  212. for (y = 0; y < nslices; y++)
  213. {
  214. struct starpu_task *task = starpu_task_create();
  215. task->cl = &cl;
  216. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, y);
  217. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, x);
  218. task->handles[2] = starpu_data_get_sub_data(C_handle, 2, x, y);
  219. task->flops = 2ULL * (matrix_dim/nslices) * (matrix_dim/nslices) * matrix_dim;
  220. if (with_tags)
  221. {
  222. task->use_tag = 1;
  223. task->tag_id = ++task_tag;
  224. }
  225. ret = starpu_task_submit(task);
  226. CHECK_TASK_SUBMIT(ret);
  227. starpu_data_wont_use(starpu_data_get_sub_data(C_handle, 2, x, y));
  228. }
  229. return 0;
  230. }
  231. /* Add dependencies between GEMM tasks to see the impact of polling workers which will at the end get a task.
  232. * The new dependency graph has the following shape:
  233. * - the same number of GEMMs as the number of workers are executed in parallel on all workers ("a column of tasks")
  234. * - then a GEMM waits all tasks of the previous column of tasks, and is executed on a worker
  235. * - the next column of tasks waits for the previous GEMM
  236. * - and so on...
  237. *
  238. * worker 0 | 1 | 4 | 5 | 8 | 9 |
  239. * worker 1 | 2 | | 6 | | 10 | ...
  240. * worker 2 | 3 | | 7 | | 11 |
  241. *
  242. * This function has to be called before gemm_submit_tasks_with_tags(1).
  243. */
  244. void gemm_add_polling_dependencies()
  245. {
  246. starpu_tag_t nb_tasks = (starpu_tag_t) nslices * (starpu_tag_t) nslices;
  247. unsigned nb_workers = starpu_worker_get_count();
  248. starpu_tag_t synchro_tag = 0;
  249. starpu_tag_t previous_tag = 0;
  250. starpu_tag_t next_tag = 0;
  251. for (synchro_tag = nb_workers+1; synchro_tag <= nb_tasks; synchro_tag += (nb_workers+1))
  252. {
  253. // this synchro tag depends on tasks of previous column of tasks:
  254. for (previous_tag = synchro_tag - nb_workers; previous_tag < synchro_tag; previous_tag++)
  255. {
  256. starpu_tag_declare_deps(synchro_tag, 1, previous_tag);
  257. }
  258. // tasks of the next column of tasks depend on this synchro tag:
  259. // this actually allows workers to poll for new tasks, while no task is available
  260. for (next_tag = synchro_tag+1; next_tag < (synchro_tag + nb_workers + 1) && next_tag <= nb_tasks; next_tag++)
  261. {
  262. starpu_tag_declare_deps(next_tag, 1, synchro_tag);
  263. }
  264. }
  265. }
  266. void gemm_release()
  267. {
  268. starpu_data_unpartition(C_handle, STARPU_MAIN_RAM);
  269. starpu_data_unpartition(B_handle, STARPU_MAIN_RAM);
  270. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  271. starpu_data_unregister(A_handle);
  272. starpu_data_unregister(B_handle);
  273. starpu_data_unregister(C_handle);
  274. if (check)
  275. check_output();
  276. starpu_free_flags(A, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  277. starpu_free_flags(B, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  278. starpu_free_flags(C, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  279. }