starpu_mpi_init.c 7.0 KB

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