gemm_helper.c 9.6 KB

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