sendrecv_gemm_bench.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-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. /*
  17. * Simple *not distributed* parallel GEMM implementation and sendrecv bench at the same time.
  18. *
  19. * This bench is a merge of mpi/tests/sendrecv_bench and examples/mult/sgemm
  20. *
  21. * A *non-distributed* GEMM is computed on each node, while a sendrecv bench is running,
  22. * completely independently. The goal is to measure the impact of worker computations on
  23. * communications.
  24. *
  25. * Use the -nblocks parameter to define the matrix size (matrix size = nblocks * 320), such as
  26. * the GEMM finishes after the sendrecv bench.
  27. */
  28. #include <limits.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <starpu_mpi.h>
  33. #include <starpu_fxt.h>
  34. #include <common/blas.h>
  35. #include "helper.h"
  36. #include "abstract_sendrecv_bench.h"
  37. #include "../../examples/mult/simple.h"
  38. #define CHECK_TASK_SUBMIT(ret) do { \
  39. if (ret == -ENODEV) \
  40. { \
  41. ret = 77; \
  42. goto enodev; \
  43. } \
  44. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit"); \
  45. } while(0)
  46. static int mpi_rank;
  47. static int comm_thread_cpuid = -1;
  48. static unsigned nslices = 4;
  49. #if defined(STARPU_QUICK_CHECK) && !defined(STARPU_SIMGRID)
  50. static unsigned matrix_dim = 256;
  51. #else
  52. static unsigned matrix_dim = 320 * 4;
  53. #endif
  54. static unsigned check = 0;
  55. static TYPE *A, *B, *C;
  56. static starpu_data_handle_t A_handle, B_handle, C_handle;
  57. static starpu_pthread_barrier_t thread_barrier;
  58. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  59. #define PRINTF(fmt, ...) do { if (!getenv("STARPU_SSILENT")) {printf(fmt, ## __VA_ARGS__); fflush(stdout); }} while(0)
  60. static void check_output(void)
  61. {
  62. /* compute C = C - AB */
  63. 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);
  64. /* make sure C = 0 */
  65. TYPE err;
  66. err = CPU_ASUM(matrix_dim*matrix_dim, C, 1);
  67. if (err < matrix_dim*matrix_dim*0.001)
  68. {
  69. FPRINTF(stderr, "Results are OK\n");
  70. }
  71. else
  72. {
  73. int max;
  74. max = CPU_IAMAX(matrix_dim*matrix_dim, C, 1);
  75. FPRINTF(stderr, "There were errors ... err = %f\n", err);
  76. FPRINTF(stderr, "Max error : %e\n", C[max]);
  77. }
  78. }
  79. static void init_problem_data(void)
  80. {
  81. #ifndef STARPU_SIMGRID
  82. unsigned i,j;
  83. #endif
  84. starpu_malloc_flags((void **)&A, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  85. starpu_malloc_flags((void **)&B, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  86. starpu_malloc_flags((void **)&C, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  87. #ifndef STARPU_SIMGRID
  88. /* fill the matrices */
  89. for (j=0; j < matrix_dim; j++)
  90. {
  91. for (i=0; i < matrix_dim; i++)
  92. {
  93. A[j+i*matrix_dim] = (TYPE)(starpu_drand48());
  94. B[j+i*matrix_dim] = (TYPE)(starpu_drand48());
  95. C[j+i*matrix_dim] = (TYPE)(0);
  96. }
  97. }
  98. #endif
  99. }
  100. static void partition_mult_data(void)
  101. {
  102. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A,
  103. matrix_dim, matrix_dim, matrix_dim, sizeof(TYPE));
  104. starpu_matrix_data_register(&B_handle, STARPU_MAIN_RAM, (uintptr_t)B,
  105. matrix_dim, matrix_dim, matrix_dim, sizeof(TYPE));
  106. starpu_matrix_data_register(&C_handle, STARPU_MAIN_RAM, (uintptr_t)C,
  107. matrix_dim, matrix_dim, matrix_dim, sizeof(TYPE));
  108. struct starpu_data_filter vert;
  109. memset(&vert, 0, sizeof(vert));
  110. vert.filter_func = starpu_matrix_filter_vertical_block;
  111. vert.nchildren = nslices;
  112. struct starpu_data_filter horiz;
  113. memset(&horiz, 0, sizeof(horiz));
  114. horiz.filter_func = starpu_matrix_filter_block;
  115. horiz.nchildren = nslices;
  116. starpu_data_partition(B_handle, &vert);
  117. starpu_data_partition(A_handle, &horiz);
  118. starpu_data_map_filters(C_handle, 2, &vert, &horiz);
  119. }
  120. void cpu_init_matrix_random(void *descr[], void *arg)
  121. {
  122. (void)arg;
  123. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  124. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  125. unsigned nx = STARPU_MATRIX_GET_NX(descr[0]);
  126. unsigned ny = STARPU_MATRIX_GET_NY(descr[0]);
  127. for (unsigned i = 0; i < nx *ny; i++)
  128. {
  129. subA[i] = (TYPE) (starpu_drand48());
  130. subB[i] = (TYPE) (starpu_drand48());
  131. }
  132. }
  133. void cpu_init_matrix_zero(void *descr[], void *arg)
  134. {
  135. (void)arg;
  136. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  137. unsigned nx = STARPU_MATRIX_GET_NX(descr[0]);
  138. unsigned ny = STARPU_MATRIX_GET_NY(descr[0]);
  139. for (unsigned i = 0; i < nx *ny; i++)
  140. {
  141. subA[i] = (TYPE) (0);
  142. }
  143. }
  144. void cpu_mult(void *descr[], void *arg)
  145. {
  146. (void)arg;
  147. TYPE *subA = (TYPE *)STARPU_MATRIX_GET_PTR(descr[0]);
  148. TYPE *subB = (TYPE *)STARPU_MATRIX_GET_PTR(descr[1]);
  149. TYPE *subC = (TYPE *)STARPU_MATRIX_GET_PTR(descr[2]);
  150. unsigned nxC = STARPU_MATRIX_GET_NX(descr[2]);
  151. unsigned nyC = STARPU_MATRIX_GET_NY(descr[2]);
  152. unsigned nyA = STARPU_MATRIX_GET_NY(descr[0]);
  153. unsigned ldA = STARPU_MATRIX_GET_LD(descr[0]);
  154. unsigned ldB = STARPU_MATRIX_GET_LD(descr[1]);
  155. unsigned ldC = STARPU_MATRIX_GET_LD(descr[2]);
  156. int worker_size = starpu_combined_worker_get_size();
  157. if (worker_size == 1)
  158. {
  159. /* Sequential CPU task */
  160. CPU_GEMM("N", "N", nxC, nyC, nyA, (TYPE)1.0, subA, ldA, subB, ldB, (TYPE)0.0, subC, ldC);
  161. }
  162. else
  163. {
  164. /* Parallel CPU task */
  165. unsigned rank = starpu_combined_worker_get_rank();
  166. unsigned block_size = (nyC + worker_size - 1)/worker_size;
  167. unsigned new_nyC = STARPU_MIN(nyC, block_size*(rank+1)) - block_size*rank;
  168. STARPU_ASSERT(nyC == STARPU_MATRIX_GET_NY(descr[1]));
  169. TYPE *new_subB = &subB[block_size*rank];
  170. TYPE *new_subC = &subC[block_size*rank];
  171. CPU_GEMM("N", "N", nxC, new_nyC, nyA, (TYPE)1.0, subA, ldA, new_subB, ldB, (TYPE)0.0, new_subC, ldC);
  172. }
  173. }
  174. static struct starpu_perfmodel starpu_gemm_model =
  175. {
  176. .type = STARPU_HISTORY_BASED,
  177. .symbol = STARPU_GEMM_STR(gemm)
  178. };
  179. static struct starpu_codelet cl =
  180. {
  181. .type = STARPU_SEQ, /* changed to STARPU_SPMD if -spmd is passed */
  182. .max_parallelism = INT_MAX,
  183. .cpu_funcs = {cpu_mult},
  184. .cpu_funcs_name = {"cpu_mult"},
  185. .nbuffers = 3,
  186. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  187. .model = &starpu_gemm_model
  188. };
  189. static struct starpu_codelet cl_init_matrix_random =
  190. {
  191. .max_parallelism = INT_MAX,
  192. .cpu_funcs = {cpu_init_matrix_random},
  193. .cpu_funcs_name = {"cpu_init_matrix_random"},
  194. .nbuffers = 2,
  195. .modes = {STARPU_W, STARPU_W}
  196. };
  197. static struct starpu_codelet cl_init_matrix_zero =
  198. {
  199. .max_parallelism = INT_MAX,
  200. .cpu_funcs = {cpu_init_matrix_zero},
  201. .cpu_funcs_name = {"cpu_init_matrix_zero"},
  202. .nbuffers = 1,
  203. .modes = {STARPU_W}
  204. };
  205. static void parse_args(int argc, char **argv)
  206. {
  207. int i;
  208. for (i = 1; i < argc; i++)
  209. {
  210. if (strcmp(argv[i], "-nblocks") == 0)
  211. {
  212. char *argptr;
  213. nslices = strtol(argv[++i], &argptr, 10);
  214. matrix_dim = 320 * nslices;
  215. }
  216. else if (strcmp(argv[i], "-size") == 0)
  217. {
  218. char *argptr;
  219. unsigned matrix_dim_tmp = strtol(argv[++i], &argptr, 10);
  220. if (matrix_dim_tmp % 320 != 0)
  221. {
  222. fprintf(stderr, "Matrix size has to be a multiple of 320\n");
  223. }
  224. else
  225. {
  226. matrix_dim = matrix_dim_tmp;
  227. nslices = matrix_dim / 320;
  228. }
  229. }
  230. else if (strcmp(argv[i], "-check") == 0)
  231. {
  232. check = 1;
  233. }
  234. else if (strcmp(argv[i], "-spmd") == 0)
  235. {
  236. cl.type = STARPU_SPMD;
  237. }
  238. else if (strcmp(argv[i], "-comm-thread-cpuid") == 0)
  239. {
  240. comm_thread_cpuid = atoi(argv[++i]);
  241. }
  242. else if (strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
  243. {
  244. fprintf(stderr,"Usage: %s [-nblocks n] [-size size] [-check] [-spmd] [-comm-thread-cpuid cpuid]\n", argv[0]);
  245. fprintf(stderr,"Currently selected: matrix size: %u - %u blocks\n", matrix_dim, nslices);
  246. fprintf(stderr, "Use -comm-thread-cpuid to specifiy where to bind the comm benchmarking thread\n");
  247. exit(EXIT_SUCCESS);
  248. }
  249. else
  250. {
  251. fprintf(stderr,"Unrecognized option %s\n", argv[i]);
  252. exit(EXIT_FAILURE);
  253. }
  254. }
  255. }
  256. static void* comm_thread_func(void* arg)
  257. {
  258. if (comm_thread_cpuid < 0)
  259. {
  260. comm_thread_cpuid = starpu_get_next_bindid(STARPU_THREAD_ACTIVE, NULL, 0);
  261. }
  262. if (starpu_bind_thread_on(comm_thread_cpuid, 0, "Comm") < 0)
  263. {
  264. char hostname[65];
  265. gethostname(hostname, sizeof(hostname));
  266. _STARPU_DISP("[%s] No core was available for the comm thread. You should increase STARPU_RESERVE_NCPU or decrease STARPU_NCPU\n", hostname);
  267. }
  268. sendrecv_bench(mpi_rank, &thread_barrier);
  269. return NULL;
  270. }
  271. int main(int argc, char **argv)
  272. {
  273. double start, end;
  274. int ret, mpi_init, worldsize;
  275. starpu_pthread_t comm_thread;
  276. char hostname[255];
  277. gethostname(hostname, 255);
  278. parse_args(argc, argv);
  279. starpu_fxt_autostart_profiling(0);
  280. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  281. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  282. if (ret == -ENODEV)
  283. return 77;
  284. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  285. starpu_mpi_comm_rank(MPI_COMM_WORLD, &mpi_rank);
  286. starpu_mpi_comm_size(MPI_COMM_WORLD, &worldsize);
  287. if (worldsize < 2)
  288. {
  289. if (mpi_rank == 0)
  290. FPRINTF(stderr, "We need 2 processes.\n");
  291. starpu_mpi_shutdown();
  292. if (!mpi_init)
  293. MPI_Finalize();
  294. return STARPU_TEST_SKIPPED;
  295. }
  296. STARPU_PTHREAD_BARRIER_INIT(&thread_barrier, NULL, 2);
  297. // Start comm thread, benchmarking sendrecv:
  298. STARPU_PTHREAD_CREATE(&comm_thread, NULL, comm_thread_func, NULL);
  299. // Main thread will submit GEMM tasks:
  300. starpu_malloc_flags((void **)&A, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  301. starpu_malloc_flags((void **)&B, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  302. starpu_malloc_flags((void **)&C, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  303. partition_mult_data();
  304. if (mpi_rank == 0)
  305. {
  306. PRINTF("# node\tx\ty\tz\tms\tGFlops\n");
  307. }
  308. starpu_pause();
  309. unsigned x, y;
  310. #ifndef STARPU_SIMGRID
  311. // Initialize matrices:
  312. for (x = 0; x < nslices; x++)
  313. {
  314. struct starpu_task *task = starpu_task_create();
  315. task->cl = &cl_init_matrix_random;
  316. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, x);
  317. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, x);
  318. ret = starpu_task_submit(task);
  319. CHECK_TASK_SUBMIT(ret);
  320. for (y = 0; y < nslices; y++)
  321. {
  322. task = starpu_task_create();
  323. task->cl = &cl_init_matrix_zero;
  324. task->handles[0] = starpu_data_get_sub_data(C_handle, 2, x, y);
  325. ret = starpu_task_submit(task);
  326. CHECK_TASK_SUBMIT(ret);
  327. }
  328. }
  329. #endif
  330. for (x = 0; x < nslices; x++)
  331. for (y = 0; y < nslices; y++)
  332. {
  333. struct starpu_task *task = starpu_task_create();
  334. task->cl = &cl;
  335. task->handles[0] = starpu_data_get_sub_data(A_handle, 1, y);
  336. task->handles[1] = starpu_data_get_sub_data(B_handle, 1, x);
  337. task->handles[2] = starpu_data_get_sub_data(C_handle, 2, x, y);
  338. task->flops = 2ULL * (matrix_dim/nslices) * (matrix_dim/nslices) * matrix_dim;
  339. ret = starpu_task_submit(task);
  340. CHECK_TASK_SUBMIT(ret);
  341. starpu_data_wont_use(starpu_data_get_sub_data(C_handle, 2, x, y));
  342. }
  343. starpu_mpi_barrier(MPI_COMM_WORLD);
  344. starpu_fxt_start_profiling();
  345. STARPU_PTHREAD_BARRIER_WAIT(&thread_barrier);
  346. start = starpu_timing_now();
  347. starpu_resume();
  348. starpu_task_wait_for_all();
  349. end = starpu_timing_now();
  350. starpu_pause(); // Pause not to disturb comm thread if it isn't done
  351. double timing = end - start;
  352. double flops = 2.0*((unsigned long long)matrix_dim) * ((unsigned long long)matrix_dim)*((unsigned long long)matrix_dim);
  353. PRINTF("%s\t%u\t%u\t%u\t%.0f\t%.1f\n", hostname, matrix_dim, matrix_dim, matrix_dim, timing/1000.0, flops/timing/1000.0);
  354. enodev:
  355. starpu_data_unpartition(C_handle, STARPU_MAIN_RAM);
  356. starpu_data_unpartition(B_handle, STARPU_MAIN_RAM);
  357. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  358. starpu_data_unregister(A_handle);
  359. starpu_data_unregister(B_handle);
  360. starpu_data_unregister(C_handle);
  361. if (check)
  362. check_output();
  363. starpu_free_flags(A, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  364. starpu_free_flags(B, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  365. starpu_free_flags(C, matrix_dim*matrix_dim*sizeof(TYPE), STARPU_MALLOC_PINNED|STARPU_MALLOC_SIMULATION_FOLDED);
  366. // Wait comm thread:
  367. STARPU_PTHREAD_JOIN(comm_thread, NULL);
  368. STARPU_PTHREAD_BARRIER_DESTROY(&thread_barrier);
  369. starpu_fxt_stop_profiling();
  370. starpu_resume();
  371. starpu_mpi_shutdown();
  372. if (!mpi_init)
  373. MPI_Finalize();
  374. return ret;
  375. }