starpu_mpi_private.h 13 KB

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