starpu_mpi_init.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2017 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. case MPI_THREAD_MULTIPLE:
  61. /* no problem */
  62. break;
  63. }
  64. }
  65. void _starpu_mpi_do_initialize(struct _starpu_mpi_argc_argv *argc_argv)
  66. {
  67. if (argc_argv->initialize_mpi)
  68. {
  69. int thread_support;
  70. #ifdef STARPU_USE_MPI_NMAD
  71. /* strat_prio is preferred for StarPU instead of default strat_aggreg */
  72. setenv("NMAD_STRATEGY", "prio", 0 /* do not overwrite user-supplied value, if set */);
  73. #endif /* STARPU_USE_MPI_NMAD */
  74. _STARPU_DEBUG("Calling MPI_Init_thread\n");
  75. if (MPI_Init_thread(argc_argv->argc, argc_argv->argv, MPI_THREAD_SERIALIZED, &thread_support) != MPI_SUCCESS)
  76. {
  77. _STARPU_ERROR("MPI_Init_thread failed\n");
  78. }
  79. _starpu_mpi_print_thread_level_support(thread_support, "_Init_thread level =");
  80. }
  81. else
  82. {
  83. int provided;
  84. MPI_Query_thread(&provided);
  85. _starpu_mpi_print_thread_level_support(provided, " has been initialized with");
  86. }
  87. MPI_Comm_rank(argc_argv->comm, &argc_argv->rank);
  88. MPI_Comm_size(argc_argv->comm, &argc_argv->world_size);
  89. MPI_Comm_set_errhandler(argc_argv->comm, MPI_ERRORS_RETURN);
  90. #ifdef STARPU_SIMGRID
  91. _mpi_world_size = argc_argv->world_size;
  92. _mpi_world_rank = argc_argv->rank;
  93. #endif
  94. }
  95. static
  96. int _starpu_mpi_initialize(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm)
  97. {
  98. struct _starpu_mpi_argc_argv *argc_argv;
  99. _STARPU_MALLOC(argc_argv, sizeof(struct _starpu_mpi_argc_argv));
  100. argc_argv->initialize_mpi = initialize_mpi;
  101. argc_argv->argc = argc;
  102. argc_argv->argv = argv;
  103. argc_argv->comm = comm;
  104. #ifdef STARPU_SIMGRID
  105. /* Call MPI_Init_thread as early as possible, to initialize simgrid
  106. * before working with mutexes etc. */
  107. _starpu_mpi_do_initialize(argc_argv);
  108. #endif
  109. return _starpu_mpi_progress_init(argc_argv);
  110. }
  111. #ifdef STARPU_SIMGRID
  112. /* This is called before application's main, to initialize SMPI before we can
  113. * create MSG processes to run application's main */
  114. int _starpu_mpi_simgrid_init(int argc, char *argv[])
  115. {
  116. return _starpu_mpi_initialize(&argc, &argv, 1, MPI_COMM_WORLD);
  117. }
  118. #endif
  119. int starpu_mpi_init_comm(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm)
  120. {
  121. #ifdef STARPU_SIMGRID
  122. (void)argc;
  123. (void)argv;
  124. (void)initialize_mpi;
  125. (void)comm;
  126. _starpu_mpi_wait_for_initialization();
  127. return 0;
  128. #else
  129. return _starpu_mpi_initialize(argc, argv, initialize_mpi, comm);
  130. #endif
  131. }
  132. int starpu_mpi_init(int *argc, char ***argv, int initialize_mpi)
  133. {
  134. return starpu_mpi_init_comm(argc, argv, initialize_mpi, MPI_COMM_WORLD);
  135. }
  136. int starpu_mpi_initialize(void)
  137. {
  138. #ifdef STARPU_SIMGRID
  139. return 0;
  140. #else
  141. return _starpu_mpi_initialize(NULL, NULL, 0, MPI_COMM_WORLD);
  142. #endif
  143. }
  144. int starpu_mpi_initialize_extended(int *rank, int *world_size)
  145. {
  146. #ifdef STARPU_SIMGRID
  147. *world_size = _mpi_world_size;
  148. *rank = _mpi_world_rank;
  149. return 0;
  150. #else
  151. int ret;
  152. ret = _starpu_mpi_initialize(NULL, NULL, 1, MPI_COMM_WORLD);
  153. if (ret == 0)
  154. {
  155. _STARPU_DEBUG("Calling MPI_Comm_rank\n");
  156. MPI_Comm_rank(MPI_COMM_WORLD, rank);
  157. MPI_Comm_size(MPI_COMM_WORLD, world_size);
  158. }
  159. return ret;
  160. #endif
  161. }
  162. int starpu_mpi_shutdown(void)
  163. {
  164. int value;
  165. int rank, world_size;
  166. /* We need to get the rank before calling MPI_Finalize to pass to _starpu_mpi_comm_amounts_display() */
  167. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  168. starpu_mpi_comm_size(MPI_COMM_WORLD, &world_size);
  169. /* kill the progression thread */
  170. _starpu_mpi_progress_shutdown((uintptr_t)&value);
  171. _STARPU_MPI_TRACE_STOP(rank, world_size);
  172. _starpu_mpi_comm_amounts_display(stderr, rank);
  173. _starpu_mpi_comm_amounts_shutdown();
  174. _starpu_mpi_cache_shutdown(world_size);
  175. #if defined(STARPU_USE_MPI_MPI)
  176. _starpu_mpi_tag_shutdown();
  177. _starpu_mpi_comm_shutdown();
  178. #endif
  179. return 0;
  180. }
  181. int starpu_mpi_comm_size(MPI_Comm comm, int *size)
  182. {
  183. if (_starpu_mpi_fake_world_size != -1)
  184. {
  185. *size = _starpu_mpi_fake_world_size;
  186. return 0;
  187. }
  188. #ifdef STARPU_SIMGRID
  189. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  190. *size = _mpi_world_size;
  191. return 0;
  192. #else
  193. return MPI_Comm_size(comm, size);
  194. #endif
  195. }
  196. int starpu_mpi_comm_rank(MPI_Comm comm, int *rank)
  197. {
  198. if (_starpu_mpi_fake_world_rank != -1)
  199. {
  200. *rank = _starpu_mpi_fake_world_rank;
  201. return 0;
  202. }
  203. #ifdef STARPU_SIMGRID
  204. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  205. *rank = _mpi_world_rank;
  206. return 0;
  207. #else
  208. return MPI_Comm_rank(comm, rank);
  209. #endif
  210. }
  211. int starpu_mpi_world_size(void)
  212. {
  213. int size;
  214. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  215. return size;
  216. }
  217. int starpu_mpi_world_rank(void)
  218. {
  219. int rank;
  220. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  221. return rank;
  222. }