sendrecv_bench.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2019-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. * Basic send receive benchmark.
  18. * Inspired a lot from NewMadeleine examples/benchmarks/nm_bench_sendrecv.c
  19. */
  20. #include <math.h>
  21. #include <starpu_mpi.h>
  22. #include "helper.h"
  23. #define NX_MAX (512 * 1024 * 1024) // kB
  24. #define NX_MIN 0
  25. #ifdef STARPU_QUICK_CHECK
  26. #define MULT_DEFAULT 4
  27. #else
  28. #define MULT_DEFAULT 2
  29. #endif
  30. #define INCR_DEFAULT 0
  31. #define NX_STEP 1.4 // multiplication
  32. #ifdef STARPU_QUICK_CHECK
  33. #define LOOPS_DEFAULT 100
  34. #else
  35. #define LOOPS_DEFAULT 10000
  36. #endif
  37. int times_nb_nodes;
  38. int times_size;
  39. int worldsize;
  40. static int comp_double(const void*_a, const void*_b)
  41. {
  42. const double* a = _a;
  43. const double* b = _b;
  44. if(*a < *b)
  45. return -1;
  46. else if(*a > *b)
  47. return 1;
  48. else
  49. return 0;
  50. }
  51. static inline uint64_t _next(uint64_t len, double multiplier, uint64_t increment)
  52. {
  53. uint64_t next = len * multiplier + increment;
  54. if(next <= len)
  55. next++;
  56. return next;
  57. }
  58. static inline uint64_t _iterations(int iterations, uint64_t len)
  59. {
  60. const uint64_t max_data = 512 * 1024 * 1024;
  61. if(len <= 0)
  62. len = 1;
  63. uint64_t data_size = ((uint64_t)iterations * (uint64_t)len);
  64. if(data_size > max_data)
  65. {
  66. iterations = (max_data / (uint64_t)len);
  67. if(iterations < 2)
  68. iterations = 2;
  69. }
  70. return iterations;
  71. }
  72. int main(int argc, char **argv)
  73. {
  74. int ret, rank;
  75. starpu_data_handle_t handle_send, handle_recv;
  76. int mpi_init;
  77. float* vector_send = NULL;
  78. float* vector_recv = NULL;
  79. double t1, t2;
  80. double* lats = malloc(sizeof(double) * LOOPS_DEFAULT);
  81. uint64_t iterations = LOOPS_DEFAULT;
  82. double multiplier = MULT_DEFAULT;
  83. uint64_t increment = INCR_DEFAULT;
  84. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  85. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  87. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  88. starpu_mpi_comm_size(MPI_COMM_WORLD, &worldsize);
  89. if (worldsize < 2)
  90. {
  91. if (rank == 0)
  92. FPRINTF(stderr, "We need 2 processes.\n");
  93. starpu_mpi_shutdown();
  94. if (!mpi_init)
  95. MPI_Finalize();
  96. return STARPU_TEST_SKIPPED;
  97. }
  98. if (rank >= 2)
  99. {
  100. starpu_pause();
  101. for (uint64_t s = NX_MIN; s <= NX_MAX; s = _next(s, multiplier, increment))
  102. {
  103. iterations = _iterations(iterations, s);
  104. starpu_mpi_barrier(MPI_COMM_WORLD);
  105. for (uint64_t j = 0; j < iterations; j++)
  106. {
  107. starpu_mpi_barrier(MPI_COMM_WORLD);
  108. }
  109. }
  110. starpu_resume();
  111. starpu_mpi_shutdown();
  112. if (!mpi_init)
  113. MPI_Finalize();
  114. return 0;
  115. }
  116. if (rank == 0)
  117. {
  118. printf("Times in us\n");
  119. printf("# size (Bytes)\t| latency \t| 10^6 B/s \t| MB/s \t| d1 \t|median \t| avg \t| d9 \t| max\n");
  120. }
  121. int array_size = 0;
  122. for (uint64_t s = NX_MIN; s <= NX_MAX; s = _next(s, multiplier, increment))
  123. {
  124. vector_send = malloc(s);
  125. vector_recv = malloc(s);
  126. memset(vector_send, 0, s);
  127. memset(vector_recv, 0, s);
  128. starpu_vector_data_register(&handle_send, STARPU_MAIN_RAM, (uintptr_t) vector_send, s, 1);
  129. starpu_vector_data_register(&handle_recv, STARPU_MAIN_RAM, (uintptr_t) vector_recv, s, 1);
  130. iterations = _iterations(iterations, s);
  131. starpu_mpi_barrier(MPI_COMM_WORLD);
  132. for (uint64_t j = 0; j < iterations; j++)
  133. {
  134. if (rank == 0)
  135. {
  136. t1 = starpu_timing_now();
  137. starpu_mpi_send(handle_send, 1, 0, MPI_COMM_WORLD);
  138. starpu_mpi_recv(handle_recv, 1, 1, MPI_COMM_WORLD, NULL);
  139. t2 = starpu_timing_now();
  140. const double delay = t2 - t1;
  141. const double t = delay / 2;
  142. lats[j] = t;
  143. }
  144. else
  145. {
  146. starpu_mpi_recv(handle_recv, 0, 0, MPI_COMM_WORLD, NULL);
  147. starpu_mpi_send(handle_send, 0, 1, MPI_COMM_WORLD);
  148. }
  149. starpu_mpi_barrier(MPI_COMM_WORLD);
  150. }
  151. if (rank == 0)
  152. {
  153. qsort(lats, iterations, sizeof(double), &comp_double);
  154. const double min_lat = lats[0];
  155. const double max_lat = lats[iterations - 1];
  156. const double med_lat = lats[(iterations - 1) / 2];
  157. const double d1_lat = lats[(iterations - 1) / 10];
  158. const double d9_lat = lats[9 * (iterations - 1) / 10];
  159. double avg_lat = 0.0;
  160. for(uint64_t k = 0; k < iterations; k++)
  161. {
  162. avg_lat += lats[k];
  163. }
  164. avg_lat /= iterations;
  165. const double bw_million_byte = s / min_lat;
  166. const double bw_mbyte = bw_million_byte / 1.048576;
  167. printf("%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",
  168. (long long)s, min_lat, bw_million_byte, bw_mbyte, d1_lat, med_lat, avg_lat, d9_lat, max_lat);
  169. fflush(stdout);
  170. }
  171. starpu_data_unregister(handle_recv);
  172. starpu_data_unregister(handle_send);
  173. free(vector_send);
  174. free(vector_recv);
  175. }
  176. starpu_mpi_shutdown();
  177. if (!mpi_init)
  178. MPI_Finalize();
  179. return 0;
  180. }