pingpong.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011,2014,2015,2017,2018,2020 Université de Bordeaux
  4. * Copyright (C) 2013 Inria
  5. * Copyright (C) 2010-2013,2015-2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu_mpi.h>
  19. #include <unistd.h>
  20. #include "helper.h"
  21. #ifdef STARPU_QUICK_CHECK
  22. # define DEFAULT_NITER 16
  23. #else
  24. # define DEFAULT_NITER 2048
  25. #endif
  26. #define DEFAULT_DATA_SIZE 16
  27. #define DEFAULT_SLEEP_TIME 0
  28. #define DEFAULT_METHOD 0 // ping pongs
  29. void usage()
  30. {
  31. fprintf(stderr, "-n [number of iteration] (default: %d)\n", DEFAULT_NITER);
  32. fprintf(stderr, "-s [number of floats to exchange] (default: %d)\n", DEFAULT_DATA_SIZE);
  33. fprintf(stderr, "-S [time in millisecond of sleep between exchange, less than 1 second] (default: %d)\n", DEFAULT_SLEEP_TIME);
  34. fprintf(stderr, "-b : broadcasts instead of simple pair-wise ping-pongs (default: %s)\n", DEFAULT_METHOD ? "broadcast" : "ping pongs");
  35. }
  36. float *tab;
  37. starpu_data_handle_t tab_handle;
  38. int main(int argc, char **argv)
  39. {
  40. int ret, rank, size;
  41. int mpi_init;
  42. int niter = DEFAULT_NITER;
  43. int data_size = DEFAULT_DATA_SIZE;
  44. int sleep_time = DEFAULT_SLEEP_TIME;
  45. int method = DEFAULT_METHOD;
  46. for (int i = 1; i < argc; i++)
  47. {
  48. if (strcmp(argv[i], "-n") == 0)
  49. {
  50. niter = atoi(argv[i+1]);
  51. if (niter <= 0)
  52. {
  53. fprintf(stderr, "%s: illegal argument %s\n", argv[0], argv[i]);
  54. usage();
  55. exit(0);
  56. }
  57. i++;
  58. }
  59. else if (strcmp(argv[i], "-s") == 0)
  60. {
  61. data_size = atoi(argv[i+1]);
  62. if (data_size <= 0)
  63. {
  64. fprintf(stderr, "%s: illegal argument %s\n", argv[0], argv[i]);
  65. usage();
  66. exit(0);
  67. }
  68. i++;
  69. }
  70. else if(strcmp(argv[i], "-S") == 0)
  71. {
  72. sleep_time = atoi(argv[i+1]);
  73. if (sleep_time <= 0 || sleep_time >= 1000)
  74. {
  75. fprintf(stderr, "%s: illegal argument %s\n", argv[0], argv[i]);
  76. usage();
  77. exit(0);
  78. }
  79. i++;
  80. }
  81. else if(strcmp(argv[i], "-b") == 0)
  82. {
  83. method = 1; // broadcasts
  84. }
  85. else
  86. {
  87. fprintf(stderr, "%s: illegal argument %s\n", argv[0], argv[i]);
  88. usage();
  89. exit(0);
  90. }
  91. }
  92. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  93. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  95. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  96. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  97. if (size%2 != 0)
  98. {
  99. if (rank == 0)
  100. FPRINTF(stderr, "We need a even number of processes.\n");
  101. starpu_mpi_shutdown();
  102. if (!mpi_init)
  103. MPI_Finalize();
  104. return STARPU_TEST_SKIPPED;
  105. }
  106. if (rank == 0)
  107. {
  108. FPRINTF(stdout, "Number of iterations: %d\n", niter);
  109. FPRINTF(stdout, "Number of floats to exchange: %d\n", data_size);
  110. FPRINTF(stdout, "Sleep time between exchanges: %d milliseconds\n", sleep_time);
  111. if (method == 0)
  112. FPRINTF(stdout, "Method: ping pongs\n");
  113. else
  114. FPRINTF(stdout, "Method: broadcasts\n");
  115. }
  116. tab = calloc(data_size, sizeof(float));
  117. starpu_vector_data_register(&tab_handle, STARPU_MAIN_RAM, (uintptr_t)tab, data_size, sizeof(float));
  118. int loop;
  119. int other_rank = rank%2 == 0 ? rank+1 : rank-1;
  120. int sender;
  121. if (method == 0) // ping pongs
  122. {
  123. for (loop = 0; loop < niter; loop++)
  124. {
  125. if ((loop % 2) == (rank%2))
  126. {
  127. //FPRINTF_MPI(stderr, "Sending to %d\n", other_rank);
  128. starpu_mpi_send(tab_handle, other_rank, loop, MPI_COMM_WORLD);
  129. }
  130. else
  131. {
  132. MPI_Status status;
  133. //FPRINTF_MPI(stderr, "Receiving from %d\n", other_rank);
  134. starpu_mpi_recv(tab_handle, other_rank, loop, MPI_COMM_WORLD, &status);
  135. }
  136. starpu_sleep(sleep_time / 1000);
  137. }
  138. }
  139. else // broadcasts
  140. {
  141. for (loop = 0; loop < niter; loop++)
  142. {
  143. sender = loop % size;
  144. if (sender == rank)
  145. {
  146. for (int r = 0; r < size; r++)
  147. {
  148. if (r != rank)
  149. {
  150. starpu_mpi_send(tab_handle, r, (r * niter) + loop, MPI_COMM_WORLD);
  151. starpu_sleep(sleep_time / 1000);
  152. }
  153. }
  154. }
  155. else
  156. {
  157. MPI_Status status;
  158. starpu_mpi_recv(tab_handle, sender, (rank * niter) + loop, MPI_COMM_WORLD, &status);
  159. for (int r = 0; r < (size-1); r++)
  160. starpu_sleep(sleep_time / 1000);
  161. }
  162. }
  163. }
  164. starpu_data_unregister(tab_handle);
  165. free(tab);
  166. starpu_mpi_shutdown();
  167. if (!mpi_init)
  168. MPI_Finalize();
  169. return 0;
  170. }