starpu_mpi_init.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_sync_data.h>
  27. #include <starpu_mpi_early_data.h>
  28. #include <starpu_mpi_early_request.h>
  29. #include <starpu_mpi_select_node.h>
  30. #include <starpu_mpi_tag.h>
  31. #include <starpu_mpi_comm.h>
  32. #include <common/config.h>
  33. #include <common/thread.h>
  34. #include <datawizard/interfaces/data_interface.h>
  35. #include <datawizard/coherency.h>
  36. #include <core/simgrid.h>
  37. #include <core/task.h>
  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. }
  62. }
  63. void _starpu_mpi_do_initialize(struct _starpu_mpi_argc_argv *argc_argv)
  64. {
  65. if (argc_argv->initialize_mpi)
  66. {
  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. #ifdef STARPU_SIMGRID
  99. /* Call MPI_Init_thread as early as possible, to initialize simgrid
  100. * before working with mutexes etc. */
  101. _starpu_mpi_do_initialize(argc_argv);
  102. #endif
  103. return _starpu_mpi_progress_init(argc_argv);
  104. }
  105. #ifdef STARPU_SIMGRID
  106. /* This is called before application's main, to initialize SMPI before we can
  107. * create MSG processes to run application's main */
  108. int _starpu_mpi_simgrid_init(int argc, char *argv[])
  109. {
  110. return _starpu_mpi_initialize(&argc, &argv, 1, MPI_COMM_WORLD);
  111. }
  112. #endif
  113. int starpu_mpi_init_comm(int *argc STARPU_ATTRIBUTE_UNUSED, char ***argv STARPU_ATTRIBUTE_UNUSED, int initialize_mpi STARPU_ATTRIBUTE_UNUSED, MPI_Comm comm STARPU_ATTRIBUTE_UNUSED)
  114. {
  115. #ifdef STARPU_SIMGRID
  116. _starpu_mpi_wait_for_initialization();
  117. return 0;
  118. #else
  119. return _starpu_mpi_initialize(argc, argv, initialize_mpi, comm);
  120. #endif
  121. }
  122. int starpu_mpi_init(int *argc, char ***argv, int initialize_mpi)
  123. {
  124. return starpu_mpi_init_comm(argc, argv, initialize_mpi, MPI_COMM_WORLD);
  125. }
  126. int starpu_mpi_initialize(void)
  127. {
  128. #ifdef STARPU_SIMGRID
  129. return 0;
  130. #else
  131. return _starpu_mpi_initialize(NULL, NULL, 0, MPI_COMM_WORLD);
  132. #endif
  133. }
  134. int starpu_mpi_initialize_extended(int *rank, int *world_size)
  135. {
  136. #ifdef STARPU_SIMGRID
  137. *world_size = _mpi_world_size;
  138. *rank = _mpi_world_rank;
  139. return 0;
  140. #else
  141. int ret;
  142. ret = _starpu_mpi_initialize(NULL, NULL, 1, MPI_COMM_WORLD);
  143. if (ret == 0)
  144. {
  145. _STARPU_DEBUG("Calling MPI_Comm_rank\n");
  146. MPI_Comm_rank(MPI_COMM_WORLD, rank);
  147. MPI_Comm_size(MPI_COMM_WORLD, world_size);
  148. }
  149. return ret;
  150. #endif
  151. }
  152. int starpu_mpi_shutdown(void)
  153. {
  154. int value;
  155. int rank, world_size;
  156. /* We need to get the rank before calling MPI_Finalize to pass to _starpu_mpi_comm_amounts_display() */
  157. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  158. starpu_mpi_comm_size(MPI_COMM_WORLD, &world_size);
  159. /* kill the progression thread */
  160. _starpu_mpi_progress_shutdown(&value);
  161. _STARPU_MPI_TRACE_STOP(rank, world_size);
  162. _starpu_mpi_comm_amounts_display(stderr, rank);
  163. _starpu_mpi_comm_amounts_shutdown();
  164. _starpu_mpi_cache_shutdown(world_size);
  165. _starpu_mpi_tag_shutdown();
  166. _starpu_mpi_comm_shutdown();
  167. return 0;
  168. }
  169. int starpu_mpi_comm_size(MPI_Comm comm, int *size)
  170. {
  171. if (_starpu_mpi_fake_world_size != -1)
  172. {
  173. *size = _starpu_mpi_fake_world_size;
  174. return 0;
  175. }
  176. #ifdef STARPU_SIMGRID
  177. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  178. *size = _mpi_world_size;
  179. return 0;
  180. #else
  181. return MPI_Comm_size(comm, size);
  182. #endif
  183. }
  184. int starpu_mpi_comm_rank(MPI_Comm comm, int *rank)
  185. {
  186. if (_starpu_mpi_fake_world_rank != -1)
  187. {
  188. *rank = _starpu_mpi_fake_world_rank;
  189. return 0;
  190. }
  191. #ifdef STARPU_SIMGRID
  192. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  193. *rank = _mpi_world_rank;
  194. return 0;
  195. #else
  196. return MPI_Comm_rank(comm, rank);
  197. #endif
  198. }
  199. int starpu_mpi_world_size(void)
  200. {
  201. int size;
  202. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  203. return size;
  204. }
  205. int starpu_mpi_world_rank(void)
  206. {
  207. int rank;
  208. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  209. return rank;
  210. }