starpu_mpi_init.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdlib.h>
  17. #include <starpu_mpi.h>
  18. #include <starpu_mpi_datatype.h>
  19. #include <starpu_mpi_private.h>
  20. #include <starpu_mpi_cache.h>
  21. #include <starpu_profiling.h>
  22. #include <starpu_mpi_stats.h>
  23. #include <starpu_mpi_cache.h>
  24. #include <starpu_mpi_select_node.h>
  25. #include <common/config.h>
  26. #include <common/thread.h>
  27. #include <datawizard/interfaces/data_interface.h>
  28. #include <datawizard/coherency.h>
  29. #include <core/simgrid.h>
  30. #include <core/task.h>
  31. #ifdef STARPU_SIMGRID
  32. static int _mpi_world_size;
  33. static int _mpi_world_rank;
  34. #endif
  35. static int _mpi_initialized_starpu;
  36. static void _starpu_mpi_print_thread_level_support(int thread_level, char *msg)
  37. {
  38. switch (thread_level)
  39. {
  40. case MPI_THREAD_SERIALIZED:
  41. {
  42. _STARPU_DISP("MPI%s MPI_THREAD_SERIALIZED; Multiple threads may make MPI calls, but only one at a time.\n", msg);
  43. break;
  44. }
  45. case MPI_THREAD_FUNNELED:
  46. {
  47. _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);
  48. break;
  49. }
  50. case MPI_THREAD_SINGLE:
  51. {
  52. _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);
  53. break;
  54. }
  55. case MPI_THREAD_MULTIPLE:
  56. /* no problem */
  57. break;
  58. }
  59. }
  60. void _starpu_mpi_do_initialize(struct _starpu_mpi_argc_argv *argc_argv)
  61. {
  62. if (argc_argv->initialize_mpi)
  63. {
  64. 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");
  65. int thread_support;
  66. _STARPU_DEBUG("Calling MPI_Init_thread\n");
  67. if (MPI_Init_thread(argc_argv->argc, argc_argv->argv, MPI_THREAD_SERIALIZED, &thread_support) != MPI_SUCCESS)
  68. {
  69. _STARPU_ERROR("MPI_Init_thread failed\n");
  70. }
  71. _starpu_mpi_print_thread_level_support(thread_support, "_Init_thread level =");
  72. }
  73. else
  74. {
  75. int provided;
  76. MPI_Query_thread(&provided);
  77. _starpu_mpi_print_thread_level_support(provided, " has been initialized with");
  78. }
  79. MPI_Comm_rank(argc_argv->comm, &argc_argv->rank);
  80. MPI_Comm_size(argc_argv->comm, &argc_argv->world_size);
  81. MPI_Comm_set_errhandler(argc_argv->comm, MPI_ERRORS_RETURN);
  82. #ifdef STARPU_SIMGRID
  83. _mpi_world_size = argc_argv->world_size;
  84. _mpi_world_rank = argc_argv->rank;
  85. #endif
  86. }
  87. static
  88. void _starpu_mpi_backend_check()
  89. {
  90. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_init != NULL);
  91. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_shutdown != NULL);
  92. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_reserve_core != NULL);
  93. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_request_init != NULL);
  94. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_request_fill != NULL);
  95. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_request_destroy != NULL);
  96. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_data_clear != NULL);
  97. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_data_register != NULL);
  98. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_comm_register != NULL);
  99. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_progress_init != NULL);
  100. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_progress_shutdown != NULL);
  101. #ifdef STARPU_SIMGRID
  102. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_wait_for_initialization != NULL);
  103. #endif
  104. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_barrier != NULL);
  105. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_wait_for_all != NULL);
  106. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_wait != NULL);
  107. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_test != NULL);
  108. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_isend_size_func != NULL);
  109. STARPU_ASSERT(_mpi_backend._starpu_mpi_backend_irecv_size_func != NULL);
  110. }
  111. static
  112. int _starpu_mpi_initialize(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm)
  113. {
  114. struct _starpu_mpi_argc_argv *argc_argv;
  115. _STARPU_MALLOC(argc_argv, sizeof(struct _starpu_mpi_argc_argv));
  116. argc_argv->initialize_mpi = initialize_mpi;
  117. argc_argv->argc = argc;
  118. argc_argv->argv = argv;
  119. argc_argv->comm = comm;
  120. _starpu_implicit_data_deps_write_hook(_starpu_mpi_data_flush);
  121. _starpu_mpi_backend_check();
  122. #ifdef STARPU_SIMGRID
  123. /* Call MPI_Init_thread as early as possible, to initialize simgrid
  124. * before working with mutexes etc. */
  125. _starpu_mpi_do_initialize(argc_argv);
  126. #endif
  127. int ret = _mpi_backend._starpu_mpi_backend_progress_init(argc_argv);
  128. if (starpu_get_env_number_default("STARPU_DISPLAY_BINDINGS", 0))
  129. {
  130. int rank, size, i;
  131. char hostname[65];
  132. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  133. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  134. gethostname(hostname, sizeof(hostname));
  135. /* We make a barrier between each node calling hwloc-ps, to avoid mixing
  136. * outputs in stdout. */
  137. for (i = 0; i < size; i++)
  138. {
  139. starpu_mpi_barrier(MPI_COMM_WORLD);
  140. if (rank == i)
  141. {
  142. fprintf(stdout, "== Binding for rank %d on node %s ==\n", rank, hostname);
  143. starpu_display_bindings();
  144. fflush(stdout);
  145. }
  146. }
  147. starpu_mpi_barrier(MPI_COMM_WORLD);
  148. if (rank == 0)
  149. {
  150. fprintf(stdout, "== End of bindings ==\n");
  151. fflush(stdout);
  152. }
  153. }
  154. return ret;
  155. }
  156. #ifdef STARPU_SIMGRID
  157. /* This is called before application's main, to initialize SMPI before we can
  158. * create MSG processes to run application's main */
  159. int _starpu_mpi_simgrid_init(int argc, char *argv[])
  160. {
  161. return _starpu_mpi_initialize(&argc, &argv, 1, MPI_COMM_WORLD);
  162. }
  163. #endif
  164. int starpu_mpi_init_comm(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm)
  165. {
  166. #ifdef STARPU_SIMGRID
  167. (void)argc;
  168. (void)argv;
  169. (void)initialize_mpi;
  170. (void)comm;
  171. _mpi_backend._starpu_mpi_backend_wait_for_initialization();
  172. return 0;
  173. #else
  174. return _starpu_mpi_initialize(argc, argv, initialize_mpi, comm);
  175. #endif
  176. }
  177. int starpu_mpi_init(int *argc, char ***argv, int initialize_mpi)
  178. {
  179. return starpu_mpi_init_comm(argc, argv, initialize_mpi, MPI_COMM_WORLD);
  180. }
  181. int starpu_mpi_initialize(void)
  182. {
  183. #ifdef STARPU_SIMGRID
  184. return 0;
  185. #else
  186. return _starpu_mpi_initialize(NULL, NULL, 0, MPI_COMM_WORLD);
  187. #endif
  188. }
  189. int starpu_mpi_initialize_extended(int *rank, int *world_size)
  190. {
  191. #ifdef STARPU_SIMGRID
  192. *world_size = _mpi_world_size;
  193. *rank = _mpi_world_rank;
  194. return 0;
  195. #else
  196. int ret;
  197. ret = _starpu_mpi_initialize(NULL, NULL, 1, MPI_COMM_WORLD);
  198. if (ret == 0)
  199. {
  200. _STARPU_DEBUG("Calling MPI_Comm_rank\n");
  201. MPI_Comm_rank(MPI_COMM_WORLD, rank);
  202. MPI_Comm_size(MPI_COMM_WORLD, world_size);
  203. }
  204. return ret;
  205. #endif
  206. }
  207. int starpu_mpi_init_conf(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm, struct starpu_conf *conf)
  208. {
  209. struct starpu_conf localconf;
  210. if (!conf)
  211. {
  212. starpu_conf_init(&localconf);
  213. conf = &localconf;
  214. }
  215. _mpi_backend._starpu_mpi_backend_init(conf);
  216. /* Reserve a core only if required by the backend and if STARPU_NCPU isn't provided */
  217. if (_mpi_backend._starpu_mpi_backend_reserve_core() && conf->ncpus == -1)
  218. {
  219. /* Reserve a core for our progression thread */
  220. if (conf->reserve_ncpus == -1)
  221. conf->reserve_ncpus = 1;
  222. else
  223. conf->reserve_ncpus++;
  224. }
  225. conf->will_use_mpi = 1;
  226. int ret = starpu_init(conf);
  227. if (ret < 0)
  228. return ret;
  229. _mpi_initialized_starpu = 1;
  230. return starpu_mpi_init_comm(argc, argv, initialize_mpi, comm);
  231. }
  232. int starpu_mpi_shutdown(void)
  233. {
  234. void *value;
  235. int rank, world_size;
  236. /* Make sure we do not have MPI communications pending in the task graph
  237. * before shutting down MPI */
  238. starpu_mpi_wait_for_all(MPI_COMM_WORLD);
  239. /* We need to get the rank before calling MPI_Finalize to pass to _starpu_mpi_comm_amounts_display() */
  240. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  241. starpu_mpi_comm_size(MPI_COMM_WORLD, &world_size);
  242. /* kill the progression thread */
  243. _mpi_backend._starpu_mpi_backend_progress_shutdown(&value);
  244. #ifdef STARPU_USE_FXT
  245. if (starpu_fxt_is_enabled())
  246. {
  247. _STARPU_MPI_TRACE_STOP(rank, world_size);
  248. }
  249. #endif // STARPU_USE_FXT
  250. _starpu_mpi_comm_amounts_display(stderr, rank);
  251. _starpu_mpi_comm_amounts_shutdown();
  252. _starpu_mpi_cache_shutdown(world_size);
  253. _mpi_backend._starpu_mpi_backend_shutdown();
  254. if (_mpi_initialized_starpu)
  255. starpu_shutdown();
  256. return 0;
  257. }
  258. int starpu_mpi_comm_size(MPI_Comm comm, int *size)
  259. {
  260. if (_starpu_mpi_fake_world_size != -1)
  261. {
  262. *size = _starpu_mpi_fake_world_size;
  263. return 0;
  264. }
  265. #ifdef STARPU_SIMGRID
  266. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  267. *size = _mpi_world_size;
  268. return 0;
  269. #else
  270. return MPI_Comm_size(comm, size);
  271. #endif
  272. }
  273. int starpu_mpi_comm_rank(MPI_Comm comm, int *rank)
  274. {
  275. if (_starpu_mpi_fake_world_rank != -1)
  276. {
  277. *rank = _starpu_mpi_fake_world_rank;
  278. return 0;
  279. }
  280. #ifdef STARPU_SIMGRID
  281. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  282. *rank = _mpi_world_rank;
  283. return 0;
  284. #else
  285. return MPI_Comm_rank(comm, rank);
  286. #endif
  287. }
  288. int starpu_mpi_world_size(void)
  289. {
  290. int size;
  291. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  292. return size;
  293. }
  294. int starpu_mpi_world_rank(void)
  295. {
  296. int rank;
  297. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  298. return rank;
  299. }
  300. int starpu_mpi_get_thread_cpuid(void)
  301. {
  302. return _starpu_mpi_thread_cpuid;
  303. }