burst.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * This test sends simultaneously many communications, with various configurations.
  18. *
  19. * Global purpose is to watch the behaviour with traces.
  20. */
  21. #include <starpu_mpi.h>
  22. #include "helper.h"
  23. #ifdef STARPU_SIMGRID
  24. #define NB_REQUESTS 10
  25. #else
  26. #define NB_REQUESTS 500
  27. #endif
  28. #define NX_ARRAY (320 * 320)
  29. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  30. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  31. void recv_callback(void* arg)
  32. {
  33. int* received = arg;
  34. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  35. *received = 1;
  36. STARPU_PTHREAD_COND_SIGNAL(&cond);
  37. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  38. }
  39. int main(int argc, char **argv)
  40. {
  41. int ret, rank, size, mpi_init, other_rank;
  42. starpu_data_handle_t recv_handles[NB_REQUESTS];
  43. starpu_data_handle_t send_handles[NB_REQUESTS];
  44. float* recv_buffers[NB_REQUESTS];
  45. float* send_buffers[NB_REQUESTS];
  46. starpu_mpi_req recv_reqs[NB_REQUESTS];
  47. starpu_mpi_req send_reqs[NB_REQUESTS];
  48. MPI_Status status;
  49. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  50. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  51. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  52. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  53. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  54. if (rank > 1)
  55. {
  56. starpu_mpi_barrier(MPI_COMM_WORLD);
  57. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  58. starpu_mpi_barrier(MPI_COMM_WORLD);
  59. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  60. starpu_mpi_barrier(MPI_COMM_WORLD);
  61. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  62. starpu_mpi_barrier(MPI_COMM_WORLD);
  63. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  64. starpu_mpi_shutdown();
  65. if (!mpi_init)
  66. MPI_Finalize();
  67. return 0;
  68. }
  69. other_rank = (rank == 0) ? 1 : 0;
  70. /* Burst simultaneous from both nodes */
  71. if (rank == 0)
  72. {
  73. printf("Simultaneous....\n");
  74. }
  75. for (int i = 0; i < NB_REQUESTS; i++)
  76. {
  77. send_buffers[i] = malloc(NX_ARRAY * sizeof(float));
  78. memset(send_buffers[i], 0, NX_ARRAY * sizeof(float));
  79. starpu_vector_data_register(&send_handles[i], STARPU_MAIN_RAM, (uintptr_t) send_buffers[i], NX_ARRAY, sizeof(float));
  80. recv_buffers[i] = malloc(NX_ARRAY * sizeof(float));
  81. memset(recv_buffers[i], 0, NX_ARRAY * sizeof(float));
  82. starpu_vector_data_register(&recv_handles[i], STARPU_MAIN_RAM, (uintptr_t) recv_buffers[i], NX_ARRAY, sizeof(float));
  83. starpu_mpi_irecv(recv_handles[i], &recv_reqs[i], other_rank, i, MPI_COMM_WORLD);
  84. }
  85. starpu_mpi_barrier(MPI_COMM_WORLD);
  86. for (int i = 0; i < NB_REQUESTS; i++)
  87. {
  88. starpu_mpi_isend_prio(send_handles[i], &send_reqs[i], other_rank, i, i, MPI_COMM_WORLD);
  89. }
  90. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  91. /* Burst from 0 to 1 */
  92. if (rank == 0)
  93. {
  94. printf("Done.\n");
  95. printf("0 -> 1...\n");
  96. }
  97. else
  98. {
  99. for (int i = 0; i < NB_REQUESTS; i++)
  100. {
  101. starpu_mpi_irecv(recv_handles[i], &recv_reqs[i], other_rank, i, MPI_COMM_WORLD);
  102. }
  103. }
  104. starpu_mpi_barrier(MPI_COMM_WORLD);
  105. if (rank == 0)
  106. {
  107. for (int i = 0; i < NB_REQUESTS; i++)
  108. {
  109. starpu_mpi_isend_prio(send_handles[i], &send_reqs[i], other_rank, i, i, MPI_COMM_WORLD);
  110. }
  111. }
  112. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  113. /* Burst from 1 to 0 */
  114. if (rank == 0)
  115. {
  116. printf("Done.\n");
  117. printf("1 -> 0...\n");
  118. for (int i = 0; i < NB_REQUESTS; i++)
  119. {
  120. starpu_mpi_irecv(recv_handles[i], &recv_reqs[i], other_rank, i, MPI_COMM_WORLD);
  121. }
  122. }
  123. starpu_mpi_barrier(MPI_COMM_WORLD);
  124. if (rank == 1)
  125. {
  126. for (int i = 0; i < NB_REQUESTS; i++)
  127. {
  128. starpu_mpi_isend_prio(send_handles[i], &send_reqs[i], other_rank, i, i, MPI_COMM_WORLD);
  129. }
  130. }
  131. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  132. /* Half burst from both nodes, second half burst is triggered after some requests finished. */
  133. if (rank == 0)
  134. {
  135. printf("Done.\n");
  136. printf("Half/half burst...\n");
  137. }
  138. int received = 0;
  139. for (int i = 0; i < NB_REQUESTS; i++)
  140. {
  141. if (i == (NB_REQUESTS / 4))
  142. {
  143. starpu_mpi_irecv_detached(recv_handles[i], other_rank, i, MPI_COMM_WORLD, recv_callback, &received);
  144. }
  145. else
  146. {
  147. starpu_mpi_irecv(recv_handles[i], &recv_reqs[i], other_rank, i, MPI_COMM_WORLD);
  148. }
  149. }
  150. starpu_mpi_barrier(MPI_COMM_WORLD);
  151. for (int i = 0; i < (NB_REQUESTS / 2); i++)
  152. {
  153. starpu_mpi_isend_prio(send_handles[i], &send_reqs[i], other_rank, i, i, MPI_COMM_WORLD);
  154. }
  155. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  156. while (!received)
  157. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  158. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  159. for (int i = (NB_REQUESTS / 2); i < NB_REQUESTS; i++)
  160. {
  161. starpu_mpi_isend_prio(send_handles[i], &send_reqs[i], other_rank, i, i, MPI_COMM_WORLD);
  162. }
  163. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  164. if (rank == 0)
  165. {
  166. printf("Done.\n");
  167. }
  168. for (int i = 0; i < NB_REQUESTS; i++)
  169. {
  170. starpu_data_unregister(send_handles[i]);
  171. free(send_buffers[i]);
  172. starpu_data_unregister(recv_handles[i]);
  173. free(recv_buffers[i]);
  174. }
  175. starpu_mpi_shutdown();
  176. if (!mpi_init)
  177. MPI_Finalize();
  178. return 0;
  179. }