starpu_mpi_private.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013,2016,2017 Inria
  4. * Copyright (C) 2010-2017, 2019 CNRS
  5. * Copyright (C) 2010-2019 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. #ifndef __STARPU_MPI_PRIVATE_H__
  19. #define __STARPU_MPI_PRIVATE_H__
  20. #include <starpu.h>
  21. #include <common/config.h>
  22. #include <common/uthash.h>
  23. #include <starpu_mpi.h>
  24. #include <starpu_mpi_fxt.h>
  25. #include <common/list.h>
  26. #include <common/prio_list.h>
  27. #include <common/starpu_spinlock.h>
  28. #include <core/simgrid.h>
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #endif
  33. #ifdef STARPU_SIMGRID
  34. extern starpu_pthread_wait_t _starpu_mpi_thread_wait;
  35. extern starpu_pthread_queue_t _starpu_mpi_thread_dontsleep;
  36. struct _starpu_simgrid_mpi_req
  37. {
  38. MPI_Request *request;
  39. MPI_Status *status;
  40. starpu_pthread_queue_t *queue;
  41. unsigned *done;
  42. };
  43. int _starpu_mpi_simgrid_mpi_test(unsigned *done, int *flag);
  44. void _starpu_mpi_simgrid_wait_req(MPI_Request *request, MPI_Status *status, starpu_pthread_queue_t *queue, unsigned *done);
  45. #endif
  46. extern int _starpu_debug_rank;
  47. char *_starpu_mpi_get_mpi_error_code(int code);
  48. extern int _starpu_mpi_comm_debug;
  49. #ifdef STARPU_MPI_VERBOSE
  50. extern int _starpu_debug_level_min;
  51. extern int _starpu_debug_level_max;
  52. void _starpu_mpi_set_debug_level_min(int level);
  53. void _starpu_mpi_set_debug_level_max(int level);
  54. #endif
  55. extern int _starpu_mpi_fake_world_size;
  56. extern int _starpu_mpi_fake_world_rank;
  57. extern int _starpu_mpi_use_prio;
  58. extern int _starpu_mpi_thread_cpuid;
  59. extern int _starpu_mpi_use_coop_sends;
  60. void _starpu_mpi_env_init(void);
  61. #ifdef STARPU_NO_ASSERT
  62. # define STARPU_MPI_ASSERT_MSG(x, msg, ...) do { if (0) { (void) (x); }} while(0)
  63. #else
  64. # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
  65. int _starpu_debug_rank;
  66. # define STARPU_MPI_ASSERT_MSG(x, msg, ...) \
  67. do \
  68. { \
  69. if (STARPU_UNLIKELY(!(x))) \
  70. { \
  71. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  72. fprintf(stderr, "\n[%d][starpu_mpi][%s][assert failure] " msg "\n\n", _starpu_debug_rank, __starpu_func__, ## __VA_ARGS__); *(int*)NULL = 0; \
  73. } \
  74. } while(0)
  75. # else
  76. # define STARPU_MPI_ASSERT_MSG(x, msg, ...) \
  77. do \
  78. { \
  79. if (STARPU_UNLIKELY(!(x))) \
  80. { \
  81. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  82. fprintf(stderr, "\n[%d][starpu_mpi][%s][assert failure] " msg "\n\n", _starpu_debug_rank, __starpu_func__, ## __VA_ARGS__); \
  83. } \
  84. assert(x); \
  85. } while(0)
  86. # endif
  87. #endif
  88. #define _STARPU_MPI_MALLOC(ptr, size) do { ptr = malloc(size); STARPU_MPI_ASSERT_MSG(ptr != NULL, "Cannot allocate %ld bytes\n", (long) (size)); } while (0)
  89. #define _STARPU_MPI_CALLOC(ptr, nmemb, size) do { ptr = calloc(nmemb, size); STARPU_MPI_ASSERT_MSG(ptr != NULL, "Cannot allocate %ld bytes\n", (long) (nmemb*size)); } while (0)
  90. #define _STARPU_MPI_REALLOC(ptr, size) do { void *_new_ptr = realloc(ptr, size); STARPU_MPI_ASSERT_MSG(_new_ptr != NULL, "Cannot reallocate %ld bytes\n", (long) (size)); ptr = _new_ptr; } while (0)
  91. #ifdef STARPU_MPI_VERBOSE
  92. # define _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, node, tag, utag, comm, way) \
  93. do \
  94. { \
  95. if (_starpu_mpi_comm_debug) \
  96. { \
  97. int __size; \
  98. char _comm_name[128]; \
  99. int _comm_name_len; \
  100. int _rank; \
  101. starpu_mpi_comm_rank(comm, &_rank); \
  102. MPI_Type_size(datatype, &__size); \
  103. MPI_Comm_get_name(comm, _comm_name, &_comm_name_len); \
  104. fprintf(stderr, "[%d][starpu_mpi] :%d:%s:%d:%d:%ld:%s:%p:%ld:%d:%s:%d\n", _rank, _rank, way, node, tag, utag, _comm_name, ptr, count, __size, __starpu_func__ , __LINE__); \
  105. fflush(stderr); \
  106. } \
  107. } while(0);
  108. # define _STARPU_MPI_COMM_TO_DEBUG(ptr, count, datatype, dest, tag, utag, comm) _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, dest, tag, utag, comm, "-->")
  109. # define _STARPU_MPI_COMM_FROM_DEBUG(ptr, count, datatype, source, tag, utag, comm) _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, source, tag, utag, comm, "<--")
  110. # define _STARPU_MPI_DEBUG(level, fmt, ...) \
  111. do \
  112. { \
  113. if (!_starpu_silent && _starpu_debug_level_min <= level && level <= _starpu_debug_level_max) \
  114. { \
  115. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  116. fprintf(stderr, "%*s[%d][starpu_mpi][%s:%d] " fmt , (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__ , __LINE__,## __VA_ARGS__); \
  117. fflush(stderr); \
  118. } \
  119. } while(0);
  120. #else
  121. # define _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, node, tag, utag, comm, way) do { } while(0)
  122. # define _STARPU_MPI_COMM_TO_DEBUG(ptr, count, datatype, dest, tag, utag, comm) do { } while(0)
  123. # define _STARPU_MPI_COMM_FROM_DEBUG(ptr, count, datatype, source, tag, utag, comm) do { } while(0)
  124. # define _STARPU_MPI_DEBUG(level, fmt, ...) do { } while(0)
  125. #endif
  126. #define _STARPU_MPI_DISP(fmt, ...) do { if (!_starpu_silent) { \
  127. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  128. fprintf(stderr, "%*s[%d][starpu_mpi][%s:%d] " fmt , (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__ , __LINE__ ,## __VA_ARGS__); \
  129. fflush(stderr); }} while(0);
  130. #define _STARPU_MPI_MSG(fmt, ...) do { if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  131. fprintf(stderr, "[%d][starpu_mpi][%s:%d] " fmt , _starpu_debug_rank, __starpu_func__ , __LINE__ ,## __VA_ARGS__); \
  132. fflush(stderr); } while(0);
  133. #ifdef STARPU_MPI_EXTRA_VERBOSE
  134. # define _STARPU_MPI_LOG_IN() do { if (!_starpu_silent) { \
  135. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  136. fprintf(stderr, "%*s[%d][starpu_mpi][%s:%d] -->\n", (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__ , __LINE__); \
  137. fflush(stderr); }} while(0)
  138. # define _STARPU_MPI_LOG_OUT() do { if (!_starpu_silent) { \
  139. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  140. fprintf(stderr, "%*s[%d][starpu_mpi][%s:%d] <--\n", (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__, __LINE__ ); \
  141. fflush(stderr); }} while(0)
  142. #else
  143. # define _STARPU_MPI_LOG_IN()
  144. # define _STARPU_MPI_LOG_OUT()
  145. #endif
  146. enum _starpu_mpi_request_type
  147. {
  148. SEND_REQ=0,
  149. RECV_REQ=1,
  150. WAIT_REQ=2,
  151. TEST_REQ=3,
  152. BARRIER_REQ=4,
  153. PROBE_REQ=5,
  154. UNKNOWN_REQ=6,
  155. };
  156. struct _starpu_mpi_node
  157. {
  158. MPI_Comm comm;
  159. int rank;
  160. };
  161. struct _starpu_mpi_node_tag
  162. {
  163. struct _starpu_mpi_node node;
  164. starpu_mpi_tag_t data_tag;
  165. };
  166. MULTILIST_CREATE_TYPE(_starpu_mpi_req, coop_sends)
  167. /* One bag of cooperative sends */
  168. struct _starpu_mpi_coop_sends
  169. {
  170. /* List of send requests */
  171. struct _starpu_mpi_req_multilist_coop_sends reqs;
  172. struct _starpu_mpi_data *mpi_data;
  173. /* Array of send requests, after sorting out */
  174. struct _starpu_spinlock lock;
  175. struct _starpu_mpi_req **reqs_array;
  176. unsigned n;
  177. unsigned redirects_sent;
  178. };
  179. /* Initialized in starpu_mpi_data_register_comm */
  180. struct _starpu_mpi_data
  181. {
  182. int magic;
  183. struct _starpu_mpi_node_tag node_tag;
  184. int *cache_sent;
  185. int cache_received;
  186. /* Rendez-vous data for opportunistic cooperative sends */
  187. struct _starpu_spinlock coop_lock; /* Needed to synchronize between submit thread and workers */
  188. struct _starpu_mpi_coop_sends *coop_sends; /* Current cooperative send bag */
  189. };
  190. struct _starpu_mpi_data *_starpu_mpi_data_get(starpu_data_handle_t data_handle);
  191. struct _starpu_mpi_req_backend;
  192. struct _starpu_mpi_req;
  193. LIST_TYPE(_starpu_mpi_req,
  194. /* description of the data at StarPU level */
  195. starpu_data_handle_t data_handle;
  196. int prio;
  197. /* description of the data to be sent/received */
  198. MPI_Datatype datatype;
  199. char *datatype_name;
  200. void *ptr;
  201. starpu_ssize_t count;
  202. int registered_datatype;
  203. struct _starpu_mpi_req_backend *backend;
  204. /* who are we talking to ? */
  205. struct _starpu_mpi_node_tag node_tag;
  206. void (*func)(struct _starpu_mpi_req *);
  207. MPI_Status *status;
  208. struct _starpu_mpi_req_multilist_coop_sends coop_sends;
  209. struct _starpu_mpi_coop_sends *coop_sends_head;
  210. int *flag;
  211. unsigned sync;
  212. int ret;
  213. enum _starpu_mpi_request_type request_type; /* 0 send, 1 recv */
  214. unsigned submitted;
  215. unsigned completed;
  216. unsigned posted;
  217. /* in the case of detached requests */
  218. int detached;
  219. void *callback_arg;
  220. void (*callback)(void *);
  221. /* in the case of user-defined datatypes, we need to send the size of the data */
  222. int sequential_consistency;
  223. long pre_sync_jobid;
  224. long post_sync_jobid;
  225. #ifdef STARPU_SIMGRID
  226. MPI_Status status_store;
  227. starpu_pthread_queue_t queue;
  228. unsigned done;
  229. #endif
  230. );
  231. PRIO_LIST_TYPE(_starpu_mpi_req, prio)
  232. MULTILIST_CREATE_INLINES(struct _starpu_mpi_req, _starpu_mpi_req, coop_sends)
  233. /* To be called before actually queueing a request, so the communication layer knows it has something to look at */
  234. void _starpu_mpi_req_willpost(struct _starpu_mpi_req *req);
  235. /* To be called to actually submit the request */
  236. void _starpu_mpi_submit_ready_request(void *arg);
  237. /* To be called when request is completed */
  238. void _starpu_mpi_release_req_data(struct _starpu_mpi_req *req);
  239. /* Build a communication tree. Called before _starpu_mpi_coop_send is ever called. coop_sends->lock is held. */
  240. void _starpu_mpi_coop_sends_build_tree(struct _starpu_mpi_coop_sends *coop_sends);
  241. /* Try to merge with send request with other send requests */
  242. void _starpu_mpi_coop_send(starpu_data_handle_t data_handle, struct _starpu_mpi_req *req, enum starpu_data_access_mode mode, int sequential_consistency);
  243. /* Actually submit the coop_sends bag to MPI.
  244. * At least one of submit_control or submit_data is true.
  245. * _starpu_mpi_submit_coop_sends may be called either
  246. * - just once with both parameters being true,
  247. * - or once with submit_control being true (data is not available yet, but we
  248. * can send control messages), and a second time with submit_data being true. Or
  249. * the converse, possibly on different threads, etc.
  250. */
  251. void _starpu_mpi_submit_coop_sends(struct _starpu_mpi_coop_sends *coop_sends, int submit_control, int submit_data);
  252. void _starpu_mpi_submit_ready_request_inc(struct _starpu_mpi_req *req);
  253. void _starpu_mpi_request_init(struct _starpu_mpi_req **req);
  254. struct _starpu_mpi_req * _starpu_mpi_request_fill(starpu_data_handle_t data_handle,
  255. int srcdst, starpu_mpi_tag_t data_tag, MPI_Comm comm,
  256. unsigned detached, unsigned sync, int prio, void (*callback)(void *), void *arg,
  257. enum _starpu_mpi_request_type request_type, void (*func)(struct _starpu_mpi_req *),
  258. int sequential_consistency,
  259. int is_internal_req,
  260. starpu_ssize_t count);
  261. void _starpu_mpi_request_destroy(struct _starpu_mpi_req *req);
  262. void _starpu_mpi_isend_size_func(struct _starpu_mpi_req *req);
  263. void _starpu_mpi_irecv_size_func(struct _starpu_mpi_req *req);
  264. int _starpu_mpi_wait(starpu_mpi_req *public_req, MPI_Status *status);
  265. int _starpu_mpi_test(starpu_mpi_req *public_req, int *flag, MPI_Status *status);
  266. int _starpu_mpi_barrier(MPI_Comm comm);
  267. struct _starpu_mpi_argc_argv
  268. {
  269. int initialize_mpi;
  270. int *argc;
  271. char ***argv;
  272. MPI_Comm comm;
  273. int fargc; // Fortran argc
  274. char **fargv; // Fortran argv
  275. int rank;
  276. int world_size;
  277. };
  278. void _starpu_mpi_progress_shutdown(void **value);
  279. int _starpu_mpi_progress_init(struct _starpu_mpi_argc_argv *argc_argv);
  280. #ifdef STARPU_SIMGRID
  281. void _starpu_mpi_wait_for_initialization();
  282. #endif
  283. void _starpu_mpi_data_flush(starpu_data_handle_t data_handle);
  284. /*
  285. * Specific functions to backend implementation
  286. */
  287. struct _starpu_mpi_backend
  288. {
  289. void (*_starpu_mpi_backend_init)(struct starpu_conf *conf);
  290. void (*_starpu_mpi_backend_shutdown)(void);
  291. int (*_starpu_mpi_backend_reserve_core)(void);
  292. void (*_starpu_mpi_backend_request_init)(struct _starpu_mpi_req *req);
  293. void (*_starpu_mpi_backend_request_fill)(struct _starpu_mpi_req *req, MPI_Comm comm, int is_internal_req);
  294. void (*_starpu_mpi_backend_request_destroy)(struct _starpu_mpi_req *req);
  295. void (*_starpu_mpi_backend_data_clear)(starpu_data_handle_t data_handle);
  296. void (*_starpu_mpi_backend_data_register)(starpu_data_handle_t data_handle, starpu_mpi_tag_t data_tag);
  297. void (*_starpu_mpi_backend_comm_register)(MPI_Comm comm);
  298. };
  299. extern struct _starpu_mpi_backend _mpi_backend;
  300. #ifdef __cplusplus
  301. }
  302. #endif
  303. #endif // __STARPU_MPI_PRIVATE_H__