starpu_mpi_init.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_select_node.h>
  27. #include <common/config.h>
  28. #include <common/thread.h>
  29. #include <datawizard/interfaces/data_interface.h>
  30. #include <datawizard/coherency.h>
  31. #include <core/simgrid.h>
  32. #include <core/task.h>
  33. #if defined(STARPU_USE_MPI_MPI)
  34. #include <mpi/starpu_mpi_comm.h>
  35. #include <mpi/starpu_mpi_tag.h>
  36. #endif
  37. #ifdef STARPU_SIMGRID
  38. static int _mpi_world_size;
  39. static int _mpi_world_rank;
  40. #endif
  41. static void _starpu_mpi_print_thread_level_support(int thread_level, char *msg)
  42. {
  43. switch (thread_level)
  44. {
  45. case MPI_THREAD_SERIALIZED:
  46. {
  47. _STARPU_DISP("MPI%s MPI_THREAD_SERIALIZED; Multiple threads may make MPI calls, but only one at a time.\n", msg);
  48. break;
  49. }
  50. case MPI_THREAD_FUNNELED:
  51. {
  52. _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);
  53. break;
  54. }
  55. case MPI_THREAD_SINGLE:
  56. {
  57. _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);
  58. break;
  59. }
  60. }
  61. }
  62. void _starpu_mpi_do_initialize(struct _starpu_mpi_argc_argv *argc_argv)
  63. {
  64. if (argc_argv->initialize_mpi)
  65. {
  66. int thread_support;
  67. #ifdef STARPU_USE_MPI_NMAD
  68. /* strat_prio is preferred for StarPU instead of default strat_aggreg */
  69. setenv("NMAD_STRATEGY", "prio", 0 /* do not overwrite user-supplied value, if set */);
  70. #endif /* STARPU_USE_MPI_NMAD */
  71. _STARPU_DEBUG("Calling MPI_Init_thread\n");
  72. if (MPI_Init_thread(argc_argv->argc, argc_argv->argv, MPI_THREAD_SERIALIZED, &thread_support) != MPI_SUCCESS)
  73. {
  74. _STARPU_ERROR("MPI_Init_thread failed\n");
  75. }
  76. _starpu_mpi_print_thread_level_support(thread_support, "_Init_thread level =");
  77. }
  78. else
  79. {
  80. int provided;
  81. MPI_Query_thread(&provided);
  82. _starpu_mpi_print_thread_level_support(provided, " has been initialized with");
  83. }
  84. MPI_Comm_rank(argc_argv->comm, &argc_argv->rank);
  85. MPI_Comm_size(argc_argv->comm, &argc_argv->world_size);
  86. MPI_Comm_set_errhandler(argc_argv->comm, MPI_ERRORS_RETURN);
  87. #ifdef STARPU_SIMGRID
  88. _mpi_world_size = argc_argv->world_size;
  89. _mpi_world_rank = argc_argv->rank;
  90. #endif
  91. }
  92. static
  93. int _starpu_mpi_initialize(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm)
  94. {
  95. struct _starpu_mpi_argc_argv *argc_argv;
  96. _STARPU_MALLOC(argc_argv, sizeof(struct _starpu_mpi_argc_argv));
  97. argc_argv->initialize_mpi = initialize_mpi;
  98. argc_argv->argc = argc;
  99. argc_argv->argv = argv;
  100. argc_argv->comm = comm;
  101. #ifdef STARPU_SIMGRID
  102. /* Call MPI_Init_thread as early as possible, to initialize simgrid
  103. * before working with mutexes etc. */
  104. _starpu_mpi_do_initialize(argc_argv);
  105. #endif
  106. return _starpu_mpi_progress_init(argc_argv);
  107. }
  108. #ifdef STARPU_SIMGRID
  109. /* This is called before application's main, to initialize SMPI before we can
  110. * create MSG processes to run application's main */
  111. int _starpu_mpi_simgrid_init(int argc, char *argv[])
  112. {
  113. return _starpu_mpi_initialize(&argc, &argv, 1, MPI_COMM_WORLD);
  114. }
  115. #endif
  116. int starpu_mpi_init_comm(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm)
  117. {
  118. #ifdef STARPU_SIMGRID
  119. (void)argc;
  120. (void)argv;
  121. (void)initialize_mpi;
  122. (void)comm;
  123. _starpu_mpi_wait_for_initialization();
  124. return 0;
  125. #else
  126. return _starpu_mpi_initialize(argc, argv, initialize_mpi, comm);
  127. #endif
  128. }
  129. int starpu_mpi_init(int *argc, char ***argv, int initialize_mpi)
  130. {
  131. return starpu_mpi_init_comm(argc, argv, initialize_mpi, MPI_COMM_WORLD);
  132. }
  133. int starpu_mpi_initialize(void)
  134. {
  135. #ifdef STARPU_SIMGRID
  136. return 0;
  137. #else
  138. return _starpu_mpi_initialize(NULL, NULL, 0, MPI_COMM_WORLD);
  139. #endif
  140. }
  141. int starpu_mpi_initialize_extended(int *rank, int *world_size)
  142. {
  143. #ifdef STARPU_SIMGRID
  144. *world_size = _mpi_world_size;
  145. *rank = _mpi_world_rank;
  146. return 0;
  147. #else
  148. int ret;
  149. ret = _starpu_mpi_initialize(NULL, NULL, 1, MPI_COMM_WORLD);
  150. if (ret == 0)
  151. {
  152. _STARPU_DEBUG("Calling MPI_Comm_rank\n");
  153. MPI_Comm_rank(MPI_COMM_WORLD, rank);
  154. MPI_Comm_size(MPI_COMM_WORLD, world_size);
  155. }
  156. return ret;
  157. #endif
  158. }
  159. int starpu_mpi_shutdown(void)
  160. {
  161. int value;
  162. int rank, world_size;
  163. /* We need to get the rank before calling MPI_Finalize to pass to _starpu_mpi_comm_amounts_display() */
  164. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  165. starpu_mpi_comm_size(MPI_COMM_WORLD, &world_size);
  166. /* kill the progression thread */
  167. _starpu_mpi_progress_shutdown(&value);
  168. _STARPU_MPI_TRACE_STOP(rank, world_size);
  169. _starpu_mpi_comm_amounts_display(stderr, rank);
  170. _starpu_mpi_comm_amounts_shutdown();
  171. _starpu_mpi_cache_shutdown(world_size);
  172. #if defined(STARPU_USE_MPI_MPI)
  173. _starpu_mpi_tag_shutdown();
  174. _starpu_mpi_comm_shutdown();
  175. #endif
  176. return 0;
  177. }
  178. int starpu_mpi_comm_size(MPI_Comm comm, int *size)
  179. {
  180. if (_starpu_mpi_fake_world_size != -1)
  181. {
  182. *size = _starpu_mpi_fake_world_size;
  183. return 0;
  184. }
  185. #ifdef STARPU_SIMGRID
  186. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  187. *size = _mpi_world_size;
  188. return 0;
  189. #else
  190. return MPI_Comm_size(comm, size);
  191. #endif
  192. }
  193. int starpu_mpi_comm_rank(MPI_Comm comm, int *rank)
  194. {
  195. if (_starpu_mpi_fake_world_rank != -1)
  196. {
  197. *rank = _starpu_mpi_fake_world_rank;
  198. return 0;
  199. }
  200. #ifdef STARPU_SIMGRID
  201. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  202. *rank = _mpi_world_rank;
  203. return 0;
  204. #else
  205. return MPI_Comm_rank(comm, rank);
  206. #endif
  207. }
  208. int starpu_mpi_world_size(void)
  209. {
  210. int size;
  211. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  212. return size;
  213. }
  214. int starpu_mpi_world_rank(void)
  215. {
  216. int rank;
  217. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  218. return rank;
  219. }