sendrecv_parallel_tasks_bench.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /*
  17. * sendrecv benchmark from different tasks, executed simultaneously on serveral
  18. * workers.
  19. * Inspired a lot from NewMadeleine examples/piom/nm_piom_pingpong.c
  20. *
  21. * The goal is to measure impact of calls to starpu_mpi_* from different threads.
  22. *
  23. * Use STARPU_NCPU to set the number of parallel ping pongs
  24. *
  25. *
  26. * Note: This currently can not work with the MPI backend with more than 1 CPU,
  27. * since with big sizes, the MPI_Wait call in the MPI thread may block waiting
  28. * for the peer to call MPI_Recv+Wait, and there is no guarantee that the peer
  29. * will call MPI_Recv+Wait for the same data since tasks can proceed in any
  30. * order.
  31. */
  32. #include <starpu_mpi.h>
  33. #include "helper.h"
  34. #include "bench_helper.h"
  35. #include "abstract_sendrecv_bench.h"
  36. #define NB_WARMUP_PINGPONGS 10
  37. /* We reduce NX_MAX, since some NICs don't support exchanging simultaneously such amount of memory */
  38. #undef NX_MAX
  39. #ifdef STARPU_QUICK_CHECK
  40. #define NX_MAX (1024)
  41. #else
  42. #define NX_MAX (64 * 1024 * 1024)
  43. #endif
  44. void cpu_task(void* descr[], void* args)
  45. {
  46. int mpi_rank;
  47. uint64_t iterations = LOOPS_DEFAULT / 100;
  48. uint64_t s;
  49. starpu_data_handle_t handle_send, handle_recv;
  50. double t1, t2;
  51. int asked_worker;
  52. int current_worker = starpu_worker_get_id();
  53. starpu_codelet_unpack_args(args, &mpi_rank, &asked_worker, &s, &handle_send, &handle_recv);
  54. STARPU_ASSERT(asked_worker == current_worker);
  55. iterations = bench_nb_iterations(iterations, s);
  56. double* lats = malloc(sizeof(double) * iterations);
  57. for (uint64_t j = 0; j < NB_WARMUP_PINGPONGS; j++)
  58. {
  59. if (mpi_rank == 0)
  60. {
  61. starpu_mpi_send(handle_send, 1, 0, MPI_COMM_WORLD);
  62. starpu_mpi_recv(handle_recv, 1, 1, MPI_COMM_WORLD, NULL);
  63. }
  64. else
  65. {
  66. starpu_mpi_recv(handle_recv, 0, 0, MPI_COMM_WORLD, NULL);
  67. starpu_mpi_send(handle_send, 0, 1, MPI_COMM_WORLD);
  68. }
  69. }
  70. for (uint64_t j = 0; j < iterations; j++)
  71. {
  72. if (mpi_rank == 0)
  73. {
  74. t1 = starpu_timing_now();
  75. starpu_mpi_send(handle_send, 1, 0, MPI_COMM_WORLD);
  76. starpu_mpi_recv(handle_recv, 1, 1, MPI_COMM_WORLD, NULL);
  77. t2 = starpu_timing_now();
  78. lats[j] = (t2 - t1) / 2;
  79. }
  80. else
  81. {
  82. starpu_mpi_recv(handle_recv, 0, 0, MPI_COMM_WORLD, NULL);
  83. starpu_mpi_send(handle_send, 0, 1, MPI_COMM_WORLD);
  84. }
  85. }
  86. if (mpi_rank == 0)
  87. {
  88. qsort(lats, iterations, sizeof(double), &comp_double);
  89. const double min_lat = lats[0];
  90. const double max_lat = lats[iterations - 1];
  91. const double med_lat = lats[(iterations - 1) / 2];
  92. const double d1_lat = lats[(iterations - 1) / 10];
  93. const double d9_lat = lats[9 * (iterations - 1) / 10];
  94. double avg_lat = 0.0;
  95. for(uint64_t k = 0; k < iterations; k++)
  96. {
  97. avg_lat += lats[k];
  98. }
  99. avg_lat /= iterations;
  100. const double bw_million_byte = s / min_lat;
  101. const double bw_mbyte = bw_million_byte / 1.048576;
  102. printf("%2d\t\t%9lld\t%9.3lf\t%9.3f\t%9.3f\t%9.3lf\t%9.3lf\t%9.3lf\t%9.3lf\t%9.3lf\n",
  103. current_worker, (long long) s, min_lat, bw_million_byte, bw_mbyte, d1_lat, med_lat, avg_lat, d9_lat, max_lat);
  104. fflush(stdout);
  105. }
  106. free(lats);
  107. }
  108. static struct starpu_codelet cl =
  109. {
  110. .cpu_funcs = { cpu_task },
  111. .cpu_funcs_name = { "cpu_task" },
  112. .nbuffers = 0
  113. };
  114. int main(int argc, char **argv)
  115. {
  116. int ret, rank, worldsize;
  117. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  118. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  119. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  120. starpu_mpi_comm_size(MPI_COMM_WORLD, &worldsize);
  121. if (worldsize < 2)
  122. {
  123. if (rank == 0)
  124. FPRINTF(stderr, "We need 2 processes.\n");
  125. starpu_mpi_shutdown();
  126. return STARPU_TEST_SKIPPED;
  127. }
  128. if (rank == 0)
  129. {
  130. printf("Times in us\n");
  131. printf("# worker | size (Bytes)\t| latency \t| 10^6 B/s \t| MB/s \t| d1 \t|median \t| avg \t| d9 \t| max\n");
  132. }
  133. else if (rank >= 2)
  134. {
  135. starpu_mpi_shutdown();
  136. return 0;
  137. }
  138. unsigned cpu_count = starpu_cpu_worker_get_count();
  139. unsigned* mpi_tags = malloc(cpu_count * sizeof(unsigned));
  140. unsigned tag = 0;
  141. int* workers = malloc(cpu_count * sizeof(int));
  142. float** vectors_send = malloc(cpu_count * sizeof(float*));
  143. float** vectors_recv = malloc(cpu_count * sizeof(float*));
  144. starpu_data_handle_t* handles_send = malloc(cpu_count * sizeof(starpu_data_handle_t));
  145. starpu_data_handle_t* handles_recv = malloc(cpu_count * sizeof(starpu_data_handle_t));
  146. for (uint64_t s = NX_MIN; s <= NX_MAX; s = bench_next_size(s))
  147. {
  148. starpu_pause();
  149. for (unsigned i = 0; i < cpu_count; i++)
  150. {
  151. workers[i] = i;
  152. vectors_send[i] = malloc(s);
  153. vectors_recv[i] = malloc(s);
  154. memset(vectors_send[i], 0, s);
  155. memset(vectors_recv[i], 0, s);
  156. starpu_vector_data_register(&handles_send[i], STARPU_MAIN_RAM, (uintptr_t) vectors_send[i], s, 1);
  157. starpu_vector_data_register(&handles_recv[i], STARPU_MAIN_RAM, (uintptr_t) vectors_recv[i], s, 1);
  158. starpu_task_insert(&cl,
  159. STARPU_EXECUTE_ON_WORKER, workers[i],
  160. STARPU_VALUE, &rank, sizeof(int),
  161. STARPU_VALUE, workers + i, sizeof(int),
  162. STARPU_VALUE, &s, sizeof(uint64_t),
  163. STARPU_VALUE, &handles_send[i], sizeof(starpu_data_handle_t),
  164. STARPU_VALUE, &handles_recv[i], sizeof(starpu_data_handle_t), 0);
  165. }
  166. starpu_resume();
  167. starpu_task_wait_for_all();
  168. for (unsigned i = 0; i < cpu_count; i++)
  169. {
  170. starpu_data_unregister(handles_send[i]);
  171. starpu_data_unregister(handles_recv[i]);
  172. free(vectors_send[i]);
  173. free(vectors_recv[i]);
  174. }
  175. }
  176. free(workers);
  177. free(vectors_send);
  178. free(vectors_recv);
  179. free(handles_send);
  180. free(handles_recv);
  181. free(mpi_tags);
  182. starpu_mpi_shutdown();
  183. return 0;
  184. }