starpu_mpi_init.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  5. * Copyright (C) 2016 Inria
  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 <stdlib.h>
  19. #include <starpu_mpi.h>
  20. #include <starpu_mpi_datatype.h>
  21. #include <starpu_mpi_private.h>
  22. #include <starpu_mpi_cache.h>
  23. #include <starpu_profiling.h>
  24. #include <starpu_mpi_stats.h>
  25. #include <starpu_mpi_cache.h>
  26. #include <starpu_mpi_sync_data.h>
  27. #include <starpu_mpi_early_data.h>
  28. #include <starpu_mpi_early_request.h>
  29. #include <starpu_mpi_select_node.h>
  30. #include <starpu_mpi_tag.h>
  31. #include <starpu_mpi_comm.h>
  32. #include <common/config.h>
  33. #include <common/thread.h>
  34. #include <datawizard/interfaces/data_interface.h>
  35. #include <datawizard/coherency.h>
  36. #include <core/simgrid.h>
  37. #include <core/task.h>
  38. static void _starpu_mpi_print_thread_level_support(int thread_level, char *msg)
  39. {
  40. switch (thread_level)
  41. {
  42. case MPI_THREAD_SERIALIZED:
  43. {
  44. _STARPU_DISP("MPI%s MPI_THREAD_SERIALIZED; Multiple threads may make MPI calls, but only one at a time.\n", msg);
  45. break;
  46. }
  47. case MPI_THREAD_FUNNELED:
  48. {
  49. _STARPU_DISP("MPI%s MPI_THREAD_FUNNELED; The application can safely make calls to StarPU-MPI functions, but should not call directly MPI communication functions.\n", msg);
  50. break;
  51. }
  52. case MPI_THREAD_SINGLE:
  53. {
  54. _STARPU_DISP("MPI%s MPI_THREAD_SINGLE; MPI does not have multi-thread support, this might cause problems. The application can make calls to StarPU-MPI functions, but not call directly MPI Communication functions.\n", msg);
  55. break;
  56. }
  57. }
  58. }
  59. void _starpu_mpi_do_initialize(struct _starpu_mpi_argc_argv *argc_argv)
  60. {
  61. if (argc_argv->initialize_mpi)
  62. {
  63. int thread_support;
  64. _STARPU_DEBUG("Calling MPI_Init_thread\n");
  65. if (MPI_Init_thread(argc_argv->argc, argc_argv->argv, MPI_THREAD_SERIALIZED, &thread_support) != MPI_SUCCESS)
  66. {
  67. _STARPU_ERROR("MPI_Init_thread failed\n");
  68. }
  69. _starpu_mpi_print_thread_level_support(thread_support, "_Init_thread level =");
  70. }
  71. else
  72. {
  73. int provided;
  74. MPI_Query_thread(&provided);
  75. _starpu_mpi_print_thread_level_support(provided, " has been initialized with");
  76. }
  77. }
  78. static
  79. int _starpu_mpi_initialize(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm)
  80. {
  81. struct _starpu_mpi_argc_argv *argc_argv;
  82. _STARPU_MALLOC(argc_argv, sizeof(struct _starpu_mpi_argc_argv));
  83. argc_argv->initialize_mpi = initialize_mpi;
  84. argc_argv->argc = argc;
  85. argc_argv->argv = argv;
  86. argc_argv->comm = comm;
  87. #ifdef STARPU_SIMGRID
  88. /* Call MPI_Init_thread as early as possible, to initialize simgrid
  89. * before working with mutexes etc. */
  90. _starpu_mpi_do_initialize(argc_argv);
  91. #endif
  92. return _starpu_mpi_progress_init(argc_argv);
  93. }
  94. #ifdef STARPU_SIMGRID
  95. /* This is called before application's main, to initialize SMPI before we can
  96. * create MSG processes to run application's main */
  97. int _starpu_mpi_simgrid_init(int argc, char *argv[])
  98. {
  99. return _starpu_mpi_initialize(&argc, &argv, 1, MPI_COMM_WORLD);
  100. }
  101. #endif
  102. int starpu_mpi_init_comm(int *argc STARPU_ATTRIBUTE_UNUSED, char ***argv STARPU_ATTRIBUTE_UNUSED, int initialize_mpi STARPU_ATTRIBUTE_UNUSED, MPI_Comm comm STARPU_ATTRIBUTE_UNUSED)
  103. {
  104. #ifdef STARPU_SIMGRID
  105. _starpu_mpi_wait_for_initialization();
  106. return 0;
  107. #else
  108. return _starpu_mpi_initialize(argc, argv, initialize_mpi, comm);
  109. #endif
  110. }
  111. int starpu_mpi_init(int *argc, char ***argv, int initialize_mpi)
  112. {
  113. return starpu_mpi_init_comm(argc, argv, initialize_mpi, MPI_COMM_WORLD);
  114. }
  115. int starpu_mpi_initialize(void)
  116. {
  117. #ifdef STARPU_SIMGRID
  118. return 0;
  119. #else
  120. return _starpu_mpi_initialize(NULL, NULL, 0, MPI_COMM_WORLD);
  121. #endif
  122. }
  123. int starpu_mpi_initialize_extended(int *rank, int *world_size)
  124. {
  125. #ifdef STARPU_SIMGRID
  126. *world_size = _simgrid_mpi_world_size;
  127. *rank = _simgrid_mpi_world_rank;
  128. return 0;
  129. #else
  130. int ret;
  131. ret = _starpu_mpi_initialize(NULL, NULL, 1, MPI_COMM_WORLD);
  132. if (ret == 0)
  133. {
  134. _STARPU_DEBUG("Calling MPI_Comm_rank\n");
  135. MPI_Comm_rank(MPI_COMM_WORLD, rank);
  136. MPI_Comm_size(MPI_COMM_WORLD, world_size);
  137. }
  138. return ret;
  139. #endif
  140. }
  141. int starpu_mpi_shutdown(void)
  142. {
  143. int value;
  144. int rank, world_size;
  145. /* We need to get the rank before calling MPI_Finalize to pass to _starpu_mpi_comm_amounts_display() */
  146. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  147. starpu_mpi_comm_size(MPI_COMM_WORLD, &world_size);
  148. /* kill the progression thread */
  149. _starpu_mpi_progress_shutdown(&value);
  150. _STARPU_MPI_TRACE_STOP(rank, world_size);
  151. _starpu_mpi_comm_amounts_display(stderr, rank);
  152. _starpu_mpi_comm_amounts_free();
  153. _starpu_mpi_cache_free(world_size);
  154. _starpu_mpi_tag_free();
  155. _starpu_mpi_comm_free();
  156. return 0;
  157. }
  158. int starpu_mpi_comm_size(MPI_Comm comm, int *size)
  159. {
  160. if (_starpu_mpi_fake_world_size != -1)
  161. {
  162. *size = _starpu_mpi_fake_world_size;
  163. return 0;
  164. }
  165. #ifdef STARPU_SIMGRID
  166. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  167. *size = _simgrid_mpi_world_size;
  168. return 0;
  169. #else
  170. return MPI_Comm_size(comm, size);
  171. #endif
  172. }
  173. int starpu_mpi_comm_rank(MPI_Comm comm, int *rank)
  174. {
  175. if (_starpu_mpi_fake_world_rank != -1)
  176. {
  177. *rank = _starpu_mpi_fake_world_rank;
  178. return 0;
  179. }
  180. #ifdef STARPU_SIMGRID
  181. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  182. *rank = _simgrid_mpi_world_rank;
  183. return 0;
  184. #else
  185. return MPI_Comm_rank(comm, rank);
  186. #endif
  187. }
  188. int starpu_mpi_world_size(void)
  189. {
  190. int size;
  191. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  192. return size;
  193. }
  194. int starpu_mpi_world_rank(void)
  195. {
  196. int rank;
  197. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  198. return rank;
  199. }