starpu_mpi_init.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016,2017,2020 Inria
  4. * Copyright (C) 2010-2019 CNRS
  5. * Copyright (C) 2009-2018 Université de Bordeaux
  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. #ifdef STARPU_SIMGRID
  34. static int _mpi_world_size;
  35. static int _mpi_world_rank;
  36. #endif
  37. static int _mpi_initialized_starpu;
  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. case MPI_THREAD_MULTIPLE:
  58. /* no problem */
  59. break;
  60. }
  61. }
  62. void _starpu_mpi_do_initialize(struct _starpu_mpi_argc_argv *argc_argv)
  63. {
  64. if (argc_argv->initialize_mpi)
  65. {
  66. STARPU_ASSERT_MSG(argc_argv->comm == MPI_COMM_WORLD, "It does not make sense to ask StarPU-MPI to initialize MPI while a non-world communicator was given");
  67. int thread_support;
  68. _STARPU_DEBUG("Calling MPI_Init_thread\n");
  69. if (MPI_Init_thread(argc_argv->argc, argc_argv->argv, MPI_THREAD_SERIALIZED, &thread_support) != MPI_SUCCESS)
  70. {
  71. _STARPU_ERROR("MPI_Init_thread failed\n");
  72. }
  73. _starpu_mpi_print_thread_level_support(thread_support, "_Init_thread level =");
  74. }
  75. else
  76. {
  77. int provided;
  78. MPI_Query_thread(&provided);
  79. _starpu_mpi_print_thread_level_support(provided, " has been initialized with");
  80. }
  81. MPI_Comm_rank(argc_argv->comm, &argc_argv->rank);
  82. MPI_Comm_size(argc_argv->comm, &argc_argv->world_size);
  83. MPI_Comm_set_errhandler(argc_argv->comm, MPI_ERRORS_RETURN);
  84. #ifdef STARPU_SIMGRID
  85. _mpi_world_size = argc_argv->world_size;
  86. _mpi_world_rank = argc_argv->rank;
  87. #endif
  88. }
  89. static
  90. int _starpu_mpi_initialize(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm)
  91. {
  92. struct _starpu_mpi_argc_argv *argc_argv;
  93. _STARPU_MALLOC(argc_argv, sizeof(struct _starpu_mpi_argc_argv));
  94. argc_argv->initialize_mpi = initialize_mpi;
  95. argc_argv->argc = argc;
  96. argc_argv->argv = argv;
  97. argc_argv->comm = comm;
  98. _starpu_implicit_data_deps_write_hook(_starpu_mpi_data_flush);
  99. #ifdef STARPU_SIMGRID
  100. /* Call MPI_Init_thread as early as possible, to initialize simgrid
  101. * before working with mutexes etc. */
  102. _starpu_mpi_do_initialize(argc_argv);
  103. #endif
  104. return _starpu_mpi_progress_init(argc_argv);
  105. }
  106. #ifdef STARPU_SIMGRID
  107. /* This is called before application's main, to initialize SMPI before we can
  108. * create MSG processes to run application's main */
  109. int _starpu_mpi_simgrid_init(int argc, char *argv[])
  110. {
  111. return _starpu_mpi_initialize(&argc, &argv, 1, MPI_COMM_WORLD);
  112. }
  113. #endif
  114. int starpu_mpi_init_comm(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm)
  115. {
  116. #ifdef STARPU_SIMGRID
  117. (void)argc;
  118. (void)argv;
  119. (void)initialize_mpi;
  120. (void)comm;
  121. _starpu_mpi_wait_for_initialization();
  122. return 0;
  123. #else
  124. return _starpu_mpi_initialize(argc, argv, initialize_mpi, comm);
  125. #endif
  126. }
  127. int starpu_mpi_init(int *argc, char ***argv, int initialize_mpi)
  128. {
  129. return starpu_mpi_init_comm(argc, argv, initialize_mpi, MPI_COMM_WORLD);
  130. }
  131. int starpu_mpi_initialize(void)
  132. {
  133. #ifdef STARPU_SIMGRID
  134. return 0;
  135. #else
  136. return _starpu_mpi_initialize(NULL, NULL, 0, MPI_COMM_WORLD);
  137. #endif
  138. }
  139. int starpu_mpi_initialize_extended(int *rank, int *world_size)
  140. {
  141. #ifdef STARPU_SIMGRID
  142. *world_size = _mpi_world_size;
  143. *rank = _mpi_world_rank;
  144. return 0;
  145. #else
  146. int ret;
  147. ret = _starpu_mpi_initialize(NULL, NULL, 1, MPI_COMM_WORLD);
  148. if (ret == 0)
  149. {
  150. _STARPU_DEBUG("Calling MPI_Comm_rank\n");
  151. MPI_Comm_rank(MPI_COMM_WORLD, rank);
  152. MPI_Comm_size(MPI_COMM_WORLD, world_size);
  153. }
  154. return ret;
  155. #endif
  156. }
  157. int starpu_mpi_init_conf(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm, struct starpu_conf *conf)
  158. {
  159. struct starpu_conf localconf;
  160. if (!conf)
  161. {
  162. starpu_conf_init(&localconf);
  163. conf = &localconf;
  164. }
  165. _mpi_backend._starpu_mpi_backend_init(conf);
  166. /* Reserve a core only if required by the backend and if STARPU_NCPU isn't provided */
  167. if (_mpi_backend._starpu_mpi_backend_reserve_core() && conf->ncpus == -1)
  168. {
  169. /* Reserve a core for our progression thread */
  170. if (conf->reserve_ncpus == -1)
  171. conf->reserve_ncpus = 1;
  172. else
  173. conf->reserve_ncpus++;
  174. }
  175. int ret = starpu_init(conf);
  176. if (ret < 0)
  177. return ret;
  178. _mpi_initialized_starpu = 1;
  179. return starpu_mpi_init_comm(argc, argv, initialize_mpi, comm);
  180. }
  181. int starpu_mpi_shutdown(void)
  182. {
  183. void *value;
  184. int rank, world_size;
  185. /* We need to get the rank before calling MPI_Finalize to pass to _starpu_mpi_comm_amounts_display() */
  186. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  187. starpu_mpi_comm_size(MPI_COMM_WORLD, &world_size);
  188. /* kill the progression thread */
  189. _starpu_mpi_progress_shutdown(&value);
  190. #ifdef STARPU_USE_FXT
  191. if (starpu_fxt_is_enabled())
  192. {
  193. _STARPU_MPI_TRACE_STOP(rank, world_size);
  194. }
  195. #endif // STARPU_USE_FXT
  196. _starpu_mpi_comm_amounts_display(stderr, rank);
  197. _starpu_mpi_comm_amounts_shutdown();
  198. _starpu_mpi_cache_shutdown(world_size);
  199. if (_mpi_initialized_starpu)
  200. starpu_shutdown();
  201. return 0;
  202. }
  203. int starpu_mpi_comm_size(MPI_Comm comm, int *size)
  204. {
  205. if (_starpu_mpi_fake_world_size != -1)
  206. {
  207. *size = _starpu_mpi_fake_world_size;
  208. return 0;
  209. }
  210. #ifdef STARPU_SIMGRID
  211. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  212. *size = _mpi_world_size;
  213. return 0;
  214. #else
  215. return MPI_Comm_size(comm, size);
  216. #endif
  217. }
  218. int starpu_mpi_comm_rank(MPI_Comm comm, int *rank)
  219. {
  220. if (_starpu_mpi_fake_world_rank != -1)
  221. {
  222. *rank = _starpu_mpi_fake_world_rank;
  223. return 0;
  224. }
  225. #ifdef STARPU_SIMGRID
  226. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  227. *rank = _mpi_world_rank;
  228. return 0;
  229. #else
  230. return MPI_Comm_rank(comm, rank);
  231. #endif
  232. }
  233. int starpu_mpi_world_size(void)
  234. {
  235. int size;
  236. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  237. return size;
  238. }
  239. int starpu_mpi_world_rank(void)
  240. {
  241. int rank;
  242. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  243. return rank;
  244. }