sendrecv_bench.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #define MULT_DEFAULT 2
  26. #define INCR_DEFAULT 0
  27. #define NX_STEP 1.4 // multiplication
  28. #define LOOPS_DEFAULT 10000
  29. int times_nb_nodes;
  30. int times_size;
  31. int worldsize;
  32. static int comp_double(const void*_a, const void*_b)
  33. {
  34. const double* a = _a;
  35. const double* b = _b;
  36. if(*a < *b)
  37. return -1;
  38. else if(*a > *b)
  39. return 1;
  40. else
  41. return 0;
  42. }
  43. static inline uint64_t _next(uint64_t len, double multiplier, uint64_t increment)
  44. {
  45. uint64_t next = len * multiplier + increment;
  46. if(next <= len)
  47. next++;
  48. return next;
  49. }
  50. static inline uint64_t _iterations(int iterations, uint64_t len)
  51. {
  52. const uint64_t max_data = 512 * 1024 * 1024;
  53. if(len <= 0)
  54. len = 1;
  55. uint64_t data_size = ((uint64_t)iterations * (uint64_t)len);
  56. if(data_size > max_data)
  57. {
  58. iterations = (max_data / (uint64_t)len);
  59. if(iterations < 2)
  60. iterations = 2;
  61. }
  62. return iterations;
  63. }
  64. int main(int argc, char **argv)
  65. {
  66. int ret, rank;
  67. starpu_data_handle_t handle_send, handle_recv;
  68. int mpi_init;
  69. float* vector_send = NULL;
  70. float* vector_recv = NULL;
  71. double t1, t2;
  72. double* lats = malloc(sizeof(double) * LOOPS_DEFAULT);
  73. uint64_t iterations = LOOPS_DEFAULT;
  74. double multiplier = MULT_DEFAULT;
  75. uint64_t increment = INCR_DEFAULT;
  76. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  77. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  78. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  79. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  80. starpu_mpi_comm_size(MPI_COMM_WORLD, &worldsize);
  81. if (worldsize != 2)
  82. {
  83. if (rank == 0)
  84. FPRINTF(stderr, "We need 2 processes.\n");
  85. starpu_mpi_shutdown();
  86. if (!mpi_init)
  87. MPI_Finalize();
  88. return STARPU_TEST_SKIPPED;
  89. }
  90. if (rank == 0)
  91. {
  92. printf("Times in us\n");
  93. printf("# size (Bytes)\t| latency \t| 10^6 B/s \t| MB/s \t| d1 \t|median \t| avg \t| d9 \t| max\n");
  94. }
  95. int array_size = 0;
  96. for (uint64_t s = NX_MIN; s <= NX_MAX; s = _next(s, multiplier, increment))
  97. {
  98. vector_send = malloc(s);
  99. vector_recv = malloc(s);
  100. memset(vector_send, 0, s);
  101. memset(vector_recv, 0, s);
  102. starpu_vector_data_register(&handle_send, STARPU_MAIN_RAM, (uintptr_t) vector_send, s, 1);
  103. starpu_vector_data_register(&handle_recv, STARPU_MAIN_RAM, (uintptr_t) vector_recv, s, 1);
  104. iterations = _iterations(iterations, s);
  105. starpu_mpi_barrier(MPI_COMM_WORLD);
  106. for (uint64_t j = 0; j < iterations; j++)
  107. {
  108. if (rank == 0)
  109. {
  110. t1 = starpu_timing_now();
  111. starpu_mpi_send(handle_send, 1, 0, MPI_COMM_WORLD);
  112. starpu_mpi_recv(handle_recv, 1, 1, MPI_COMM_WORLD, NULL);
  113. t2 = starpu_timing_now();
  114. const double delay = t2 - t1;
  115. const double t = delay / 2;
  116. lats[j] = t;
  117. }
  118. else
  119. {
  120. starpu_mpi_recv(handle_recv, 0, 0, MPI_COMM_WORLD, NULL);
  121. starpu_mpi_send(handle_send, 0, 1, MPI_COMM_WORLD);
  122. }
  123. starpu_mpi_barrier(MPI_COMM_WORLD);
  124. }
  125. if (rank == 0)
  126. {
  127. qsort(lats, iterations, sizeof(double), &comp_double);
  128. const double min_lat = lats[0];
  129. const double max_lat = lats[iterations - 1];
  130. const double med_lat = lats[(iterations - 1) / 2];
  131. const double d1_lat = lats[(iterations - 1) / 10];
  132. const double d9_lat = lats[9 * (iterations - 1) / 10];
  133. double avg_lat = 0.0;
  134. for(uint64_t k = 0; k < iterations; k++)
  135. {
  136. avg_lat += lats[k];
  137. }
  138. avg_lat /= iterations;
  139. const double bw_million_byte = s / min_lat;
  140. const double bw_mbyte = bw_million_byte / 1.048576;
  141. 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",
  142. (long long)s, min_lat, bw_million_byte, bw_mbyte, d1_lat, med_lat, avg_lat, d9_lat, max_lat);
  143. fflush(stdout);
  144. }
  145. starpu_data_unregister(handle_recv);
  146. starpu_data_unregister(handle_send);
  147. free(vector_send);
  148. free(vector_recv);
  149. }
  150. starpu_mpi_shutdown();
  151. return 0;
  152. }