sendrecv_parallel_tasks_bench.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #include <starpu_mpi.h>
  26. #include "helper.h"
  27. #include "bench_helper.h"
  28. #include "abstract_sendrecv_bench.h"
  29. #define NB_WARMUP_PINGPONGS 10
  30. /* We reduce NX_MAX, since some NICs don't support exchanging simultaneously such amount of memory */
  31. #undef NX_MAX
  32. #define NX_MAX (64 * 1024 * 1024)
  33. void cpu_task(void* descr[], void* args)
  34. {
  35. int mpi_rank;
  36. uint64_t iterations = LOOPS_DEFAULT / 100;
  37. uint64_t s;
  38. starpu_data_handle_t handle_send, handle_recv;
  39. double t1, t2;
  40. unsigned worker;
  41. starpu_codelet_unpack_args(args, &mpi_rank, &worker, &s, &handle_send, &handle_recv);
  42. iterations = bench_nb_iterations(iterations, s);
  43. double* lats = malloc(sizeof(double) * iterations);
  44. for (uint64_t j = 0; j < NB_WARMUP_PINGPONGS; j++)
  45. {
  46. if (mpi_rank == 0)
  47. {
  48. starpu_mpi_send(handle_send, 1, 0, MPI_COMM_WORLD);
  49. starpu_mpi_recv(handle_recv, 1, 1, MPI_COMM_WORLD, NULL);
  50. }
  51. else
  52. {
  53. starpu_mpi_recv(handle_recv, 0, 0, MPI_COMM_WORLD, NULL);
  54. starpu_mpi_send(handle_send, 0, 1, MPI_COMM_WORLD);
  55. }
  56. }
  57. for (uint64_t j = 0; j < iterations; j++)
  58. {
  59. if (mpi_rank == 0)
  60. {
  61. t1 = starpu_timing_now();
  62. starpu_mpi_send(handle_send, 1, 0, MPI_COMM_WORLD);
  63. starpu_mpi_recv(handle_recv, 1, 1, MPI_COMM_WORLD, NULL);
  64. t2 = starpu_timing_now();
  65. lats[j] = (t2 - t1) / 2;
  66. }
  67. else
  68. {
  69. starpu_mpi_recv(handle_recv, 0, 0, MPI_COMM_WORLD, NULL);
  70. starpu_mpi_send(handle_send, 0, 1, MPI_COMM_WORLD);
  71. }
  72. }
  73. if (mpi_rank == 0)
  74. {
  75. qsort(lats, iterations, sizeof(double), &comp_double);
  76. const double min_lat = lats[0];
  77. const double max_lat = lats[iterations - 1];
  78. const double med_lat = lats[(iterations - 1) / 2];
  79. const double d1_lat = lats[(iterations - 1) / 10];
  80. const double d9_lat = lats[9 * (iterations - 1) / 10];
  81. double avg_lat = 0.0;
  82. for(uint64_t k = 0; k < iterations; k++)
  83. {
  84. avg_lat += lats[k];
  85. }
  86. avg_lat /= iterations;
  87. const double bw_million_byte = s / min_lat;
  88. const double bw_mbyte = bw_million_byte / 1.048576;
  89. printf("%2u\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",
  90. worker, (long long) s, min_lat, bw_million_byte, bw_mbyte, d1_lat, med_lat, avg_lat, d9_lat, max_lat);
  91. fflush(stdout);
  92. }
  93. }
  94. static struct starpu_codelet cl =
  95. {
  96. .cpu_funcs = { cpu_task },
  97. .cpu_funcs_name = { "cpu_task" },
  98. .nbuffers = 0
  99. };
  100. int main(int argc, char **argv)
  101. {
  102. int ret, rank, worldsize;
  103. int mpi_init;
  104. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  105. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  107. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  108. starpu_mpi_comm_size(MPI_COMM_WORLD, &worldsize);
  109. if (worldsize < 2)
  110. {
  111. if (rank == 0)
  112. FPRINTF(stderr, "We need 2 processes.\n");
  113. starpu_mpi_shutdown();
  114. if (!mpi_init)
  115. MPI_Finalize();
  116. return STARPU_TEST_SKIPPED;
  117. }
  118. if (rank == 0)
  119. {
  120. printf("Times in us\n");
  121. 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");
  122. }
  123. else if (rank >= 2)
  124. {
  125. starpu_mpi_shutdown();
  126. if (!mpi_init)
  127. MPI_Finalize();
  128. return 0;
  129. }
  130. unsigned cpu_count = starpu_cpu_worker_get_count();
  131. unsigned* mpi_tags = malloc(cpu_count * sizeof(unsigned));
  132. unsigned tag = 0;
  133. unsigned* workers = malloc(cpu_count * sizeof(unsigned));
  134. float** vectors_send = malloc(cpu_count * sizeof(float*));
  135. float** vectors_recv = malloc(cpu_count * sizeof(float*));
  136. starpu_data_handle_t* handles_send = malloc(cpu_count * sizeof(starpu_data_handle_t));
  137. starpu_data_handle_t* handles_recv = malloc(cpu_count * sizeof(starpu_data_handle_t));
  138. for (uint64_t s = NX_MIN; s <= NX_MAX; s = bench_next_size(s))
  139. {
  140. starpu_pause();
  141. for (unsigned i = 0; i < cpu_count; i++)
  142. {
  143. workers[i] = i;
  144. vectors_send[i] = malloc(s);
  145. vectors_recv[i] = malloc(s);
  146. memset(vectors_send[i], 0, s);
  147. memset(vectors_recv[i], 0, s);
  148. starpu_vector_data_register(&handles_send[i], STARPU_MAIN_RAM, (uintptr_t) vectors_send[i], s, 1);
  149. starpu_vector_data_register(&handles_recv[i], STARPU_MAIN_RAM, (uintptr_t) vectors_recv[i], s, 1);
  150. starpu_task_insert(&cl,
  151. STARPU_VALUE, &rank, sizeof(int),
  152. STARPU_VALUE, workers + i, sizeof(unsigned),
  153. STARPU_VALUE, &s, sizeof(uint64_t),
  154. STARPU_VALUE, &handles_send[i], sizeof(starpu_data_handle_t),
  155. STARPU_VALUE, &handles_recv[i], sizeof(starpu_data_handle_t), 0);
  156. }
  157. starpu_resume();
  158. starpu_task_wait_for_all();
  159. for (unsigned i = 0; i < cpu_count; i++)
  160. {
  161. starpu_data_unregister(handles_send[i]);
  162. starpu_data_unregister(handles_recv[i]);
  163. free(vectors_send[i]);
  164. free(vectors_recv[i]);
  165. }
  166. }
  167. free(workers);
  168. free(vectors_send);
  169. free(vectors_recv);
  170. free(handles_send);
  171. free(handles_recv);
  172. free(mpi_tags);
  173. starpu_mpi_shutdown();
  174. if (!mpi_init)
  175. MPI_Finalize();
  176. return 0;
  177. }