gemm_helper.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. for (unsigned i = 0; i < nx *ny; i++)
  84. {
  85. subA[i] = (TYPE) (starpu_drand48());
  86. subB[i] = (TYPE) (starpu_drand48());
  87. }
  88. }
  89. static void cpu_init_matrix_zero(void *descr[], void *arg)
  90. {
  91. (void)arg;
  92. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  93. unsigned nx = STARPU_MATRIX_GET_NX(descr[0]);
  94. unsigned ny = STARPU_MATRIX_GET_NY(descr[0]);
  95. for (unsigned i = 0; i < nx *ny; i++)
  96. {
  97. subA[i] = (TYPE) (0);
  98. }
  99. }
  100. static void cpu_mult(void *descr[], void *arg)
  101. {
  102. (void)arg;
  103. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  104. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  105. TYPE *subC = (TYPE *)STARPU_MATRIX_GET_PTR(descr[2]);
  106. unsigned nxC = STARPU_MATRIX_GET_NX(descr[2]);
  107. unsigned nyC = STARPU_MATRIX_GET_NY(descr[2]);
  108. unsigned nyA = STARPU_MATRIX_GET_NY(descr[0]);
  109. unsigned ldA = STARPU_MATRIX_GET_LD(descr[0]);
  110. unsigned ldB = STARPU_MATRIX_GET_LD(descr[1]);
  111. unsigned ldC = STARPU_MATRIX_GET_LD(descr[2]);
  112. int worker_size = starpu_combined_worker_get_size();
  113. if (worker_size == 1)
  114. {
  115. /* Sequential CPU task */
  116. CPU_GEMM("N", "N", nxC, nyC, nyA, (TYPE)1.0, subA, ldA, subB, ldB, (TYPE)0.0, subC, ldC);
  117. }
  118. else
  119. {
  120. /* Parallel CPU task */
  121. unsigned rank = starpu_combined_worker_get_rank();
  122. unsigned block_size = (nyC + worker_size - 1)/worker_size;
  123. unsigned new_nyC = STARPU_MIN(nyC, block_size*(rank+1)) - block_size*rank;
  124. STARPU_ASSERT(nyC == STARPU_MATRIX_GET_NY(descr[1]));
  125. TYPE *new_subB = &subB[block_size*rank];
  126. TYPE *new_subC = &subC[block_size*rank];
  127. CPU_GEMM("N", "N", nxC, new_nyC, nyA, (TYPE)1.0, subA, ldA, new_subB, ldB, (TYPE)0.0, new_subC, ldC);
  128. }
  129. }
  130. static struct starpu_perfmodel starpu_gemm_model =
  131. {
  132. .type = STARPU_HISTORY_BASED,
  133. .symbol = STARPU_GEMM_STR(gemm)
  134. };
  135. static struct starpu_codelet cl =
  136. {
  137. .type = STARPU_SEQ, /* changed to STARPU_SPMD if -spmd is passed */
  138. .max_parallelism = INT_MAX,
  139. .cpu_funcs = {cpu_mult},
  140. .cpu_funcs_name = {"cpu_mult"},
  141. .nbuffers = 3,
  142. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  143. .model = &starpu_gemm_model
  144. };
  145. static struct starpu_codelet cl_init_matrix_random =
  146. {
  147. .max_parallelism = INT_MAX,
  148. .cpu_funcs = {cpu_init_matrix_random},
  149. .cpu_funcs_name = {"cpu_init_matrix_random"},
  150. .nbuffers = 2,
  151. .modes = {STARPU_W, STARPU_W},
  152. .name = "init_matrix_random",
  153. .color = 0xffa500 // orange
  154. };
  155. static struct starpu_codelet cl_init_matrix_zero =
  156. {
  157. .max_parallelism = INT_MAX,
  158. .cpu_funcs = {cpu_init_matrix_zero},
  159. .cpu_funcs_name = {"cpu_init_matrix_zero"},
  160. .nbuffers = 1,
  161. .modes = {STARPU_W},
  162. .name = "init_matrix_zero",
  163. .color = 0x808000 // olive
  164. };
  165. void gemm_alloc_data()
  166. {
  167. starpu_malloc_flags((void **)&A, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  168. starpu_malloc_flags((void **)&B, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  169. starpu_malloc_flags((void **)&C, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  170. partition_mult_data();
  171. }
  172. int gemm_init_data()
  173. {
  174. #ifndef STARPU_SIMGRID
  175. int ret;
  176. unsigned x, y;
  177. // Initialize matrices:
  178. for (x = 0; x < nslices; x++)
  179. {
  180. struct starpu_task *task = starpu_task_create();
  181. task->cl = &cl_init_matrix_random;
  182. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, x);
  183. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, x);
  184. ret = starpu_task_submit(task);
  185. CHECK_TASK_SUBMIT(ret);
  186. for (y = 0; y < nslices; y++)
  187. {
  188. task = starpu_task_create();
  189. task->cl = &cl_init_matrix_zero;
  190. task->handles[0] = starpu_data_get_sub_data(C_handle, 2, x, y);
  191. ret = starpu_task_submit(task);
  192. CHECK_TASK_SUBMIT(ret);
  193. }
  194. }
  195. #endif
  196. return 0;
  197. }
  198. int gemm_submit_tasks()
  199. {
  200. int ret;
  201. unsigned x, y;
  202. for (x = 0; x < nslices; x++)
  203. for (y = 0; y < nslices; y++)
  204. {
  205. struct starpu_task *task = starpu_task_create();
  206. task->cl = &cl;
  207. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, y);
  208. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, x);
  209. task->handles[2] = starpu_data_get_sub_data(C_handle, 2, x, y);
  210. task->flops = 2ULL * (matrix_dim/nslices) * (matrix_dim/nslices) * matrix_dim;
  211. ret = starpu_task_submit(task);
  212. CHECK_TASK_SUBMIT(ret);
  213. starpu_data_wont_use(starpu_data_get_sub_data(C_handle, 2, x, y));
  214. }
  215. return 0;
  216. }
  217. void gemm_release()
  218. {
  219. starpu_data_unpartition(C_handle, STARPU_MAIN_RAM);
  220. starpu_data_unpartition(B_handle, STARPU_MAIN_RAM);
  221. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  222. starpu_data_unregister(A_handle);
  223. starpu_data_unregister(B_handle);
  224. starpu_data_unregister(C_handle);
  225. if (check)
  226. check_output();
  227. starpu_free_flags(A, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  228. starpu_free_flags(B, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  229. starpu_free_flags(C, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  230. }