starpu_mpi_private.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #ifndef __STARPU_MPI_PRIVATE_H__
  18. #define __STARPU_MPI_PRIVATE_H__
  19. #include <starpu.h>
  20. #include <common/config.h>
  21. #include <common/uthash.h>
  22. #include <starpu_mpi.h>
  23. #include <starpu_mpi_fxt.h>
  24. #include <common/list.h>
  25. #include <common/prio_list.h>
  26. #include <core/simgrid.h>
  27. #ifdef __cplusplus
  28. extern "C"
  29. {
  30. #endif
  31. #ifdef STARPU_SIMGRID
  32. starpu_pthread_wait_t wait;
  33. starpu_pthread_queue_t dontsleep;
  34. struct _starpu_simgrid_mpi_req
  35. {
  36. MPI_Request *request;
  37. MPI_Status *status;
  38. starpu_pthread_queue_t *queue;
  39. unsigned *done;
  40. };
  41. int _starpu_mpi_simgrid_mpi_test(unsigned *done, int *flag);
  42. void _starpu_mpi_simgrid_wait_req(MPI_Request *request, MPI_Status *status, starpu_pthread_queue_t *queue, unsigned *done);
  43. #endif
  44. extern int _starpu_debug_rank;
  45. char *_starpu_mpi_get_mpi_error_code(int code);
  46. extern int _starpu_mpi_comm_debug;
  47. #ifdef STARPU_VERBOSE
  48. extern int _starpu_debug_level_min;
  49. extern int _starpu_debug_level_max;
  50. void _starpu_mpi_set_debug_level_min(int level);
  51. void _starpu_mpi_set_debug_level_max(int level);
  52. #endif
  53. extern int _starpu_mpi_fake_world_size;
  54. extern int _starpu_mpi_fake_world_rank;
  55. #ifdef STARPU_NO_ASSERT
  56. # define STARPU_MPI_ASSERT_MSG(x, msg, ...) do { if (0) { (void) (x); }} while(0)
  57. #else
  58. # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
  59. int _starpu_debug_rank;
  60. # define STARPU_MPI_ASSERT_MSG(x, msg, ...) \
  61. do \
  62. { \
  63. if (STARPU_UNLIKELY(!(x))) \
  64. { \
  65. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  66. fprintf(stderr, "\n[%d][starpu_mpi][%s][assert failure] " msg "\n\n", _starpu_debug_rank, __starpu_func__, ## __VA_ARGS__); *(int*)NULL = 0; \
  67. } \
  68. } while(0)
  69. # else
  70. # define STARPU_MPI_ASSERT_MSG(x, msg, ...) \
  71. do \
  72. { \
  73. if (STARPU_UNLIKELY(!(x))) \
  74. { \
  75. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  76. fprintf(stderr, "\n[%d][starpu_mpi][%s][assert failure] " msg "\n\n", _starpu_debug_rank, __starpu_func__, ## __VA_ARGS__); \
  77. } \
  78. assert(x); \
  79. } while(0)
  80. # endif
  81. #endif
  82. #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)
  83. #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)
  84. #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)
  85. #ifdef STARPU_VERBOSE
  86. # define _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, node, tag, utag, comm, way) \
  87. do \
  88. { \
  89. if (_starpu_mpi_comm_debug) \
  90. { \
  91. int __size; \
  92. char _comm_name[128]; \
  93. int _comm_name_len; \
  94. int _rank; \
  95. starpu_mpi_comm_rank(comm, &_rank); \
  96. MPI_Type_size(datatype, &__size); \
  97. MPI_Comm_get_name(comm, _comm_name, &_comm_name_len); \
  98. fprintf(stderr, "[%d][starpu_mpi] :%d:%s:%d:%d:%d:%s:%p:%ld:%d:%s:%d\n", _rank, _rank, way, node, tag, utag, _comm_name, ptr, count, __size, __starpu_func__ , __LINE__); \
  99. fflush(stderr); \
  100. } \
  101. } while(0);
  102. # define _STARPU_MPI_COMM_TO_DEBUG(ptr, count, datatype, dest, tag, utag, comm) _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, dest, tag, utag, comm, "-->")
  103. # define _STARPU_MPI_COMM_FROM_DEBUG(ptr, count, datatype, source, tag, utag, comm) _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, source, tag, utag, comm, "<--")
  104. # define _STARPU_MPI_DEBUG(level, fmt, ...) \
  105. do \
  106. { \
  107. if (!_starpu_silent && _starpu_debug_level_min <= level && level <= _starpu_debug_level_max) \
  108. { \
  109. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  110. fprintf(stderr, "%*s[%d][starpu_mpi][%s:%d] " fmt , (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__ , __LINE__,## __VA_ARGS__); \
  111. fflush(stderr); \
  112. } \
  113. } while(0);
  114. #else
  115. # define _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, node, tag, utag, comm, way) do { } while(0)
  116. # define _STARPU_MPI_COMM_TO_DEBUG(ptr, count, datatype, dest, tag, utag, comm) do { } while(0)
  117. # define _STARPU_MPI_COMM_FROM_DEBUG(ptr, count, datatype, source, tag, utag, comm) do { } while(0)
  118. # define _STARPU_MPI_DEBUG(level, fmt, ...) do { } while(0)
  119. #endif
  120. #define _STARPU_MPI_DISP(fmt, ...) do { if (!_starpu_silent) { \
  121. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  122. fprintf(stderr, "%*s[%d][starpu_mpi][%s:%d] " fmt , (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__ , __LINE__ ,## __VA_ARGS__); \
  123. fflush(stderr); }} while(0);
  124. #define _STARPU_MPI_MSG(fmt, ...) do { if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  125. fprintf(stderr, "[%d][starpu_mpi][%s:%d] " fmt , _starpu_debug_rank, __starpu_func__ , __LINE__ ,## __VA_ARGS__); \
  126. fflush(stderr); } while(0);
  127. #ifdef STARPU_VERBOSE
  128. # define _STARPU_MPI_LOG_IN() do { if (!_starpu_silent) { \
  129. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  130. fprintf(stderr, "%*s[%d][starpu_mpi][%s:%d] -->\n", (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__ , __LINE__); \
  131. fflush(stderr); }} while(0)
  132. # define _STARPU_MPI_LOG_OUT() do { if (!_starpu_silent) { \
  133. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  134. fprintf(stderr, "%*s[%d][starpu_mpi][%s:%d] <--\n", (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__, __LINE__ ); \
  135. fflush(stderr); }} while(0)
  136. #else
  137. # define _STARPU_MPI_LOG_IN()
  138. # define _STARPU_MPI_LOG_OUT()
  139. #endif
  140. extern int _starpu_mpi_tag;
  141. #define _STARPU_MPI_TAG_ENVELOPE _starpu_mpi_tag
  142. #define _STARPU_MPI_TAG_DATA _starpu_mpi_tag+1
  143. #define _STARPU_MPI_TAG_SYNC_DATA _starpu_mpi_tag+2
  144. enum _starpu_mpi_request_type
  145. {
  146. SEND_REQ=0,
  147. RECV_REQ=1,
  148. WAIT_REQ=2,
  149. TEST_REQ=3,
  150. BARRIER_REQ=4,
  151. PROBE_REQ=5,
  152. UNKNOWN_REQ=6,
  153. };
  154. #define _STARPU_MPI_ENVELOPE_DATA 0
  155. #define _STARPU_MPI_ENVELOPE_SYNC_READY 1
  156. struct _starpu_mpi_envelope
  157. {
  158. int mode;
  159. starpu_ssize_t size;
  160. int data_tag;
  161. unsigned sync;
  162. };
  163. struct _starpu_mpi_req;
  164. struct _starpu_mpi_node_tag
  165. {
  166. MPI_Comm comm;
  167. int rank;
  168. int data_tag;
  169. };
  170. struct _starpu_mpi_data
  171. {
  172. int magic;
  173. struct _starpu_mpi_node_tag node_tag;
  174. int *cache_sent;
  175. int cache_received;
  176. };
  177. LIST_TYPE(_starpu_mpi_req,
  178. /* description of the data at StarPU level */
  179. starpu_data_handle_t data_handle;
  180. int prio;
  181. /* description of the data to be sent/received */
  182. MPI_Datatype datatype;
  183. char *datatype_name;
  184. void *ptr;
  185. starpu_ssize_t count;
  186. int registered_datatype;
  187. /* who are we talking to ? */
  188. struct _starpu_mpi_node_tag node_tag;
  189. void (*func)(struct _starpu_mpi_req *);
  190. MPI_Status *status;
  191. MPI_Request data_request;
  192. int *flag;
  193. unsigned sync;
  194. int ret;
  195. starpu_pthread_mutex_t req_mutex;
  196. starpu_pthread_cond_t req_cond;
  197. starpu_pthread_mutex_t posted_mutex;
  198. starpu_pthread_cond_t posted_cond;
  199. enum _starpu_mpi_request_type request_type; /* 0 send, 1 recv */
  200. unsigned submitted;
  201. unsigned completed;
  202. unsigned posted;
  203. /* In the case of a Wait/Test request, we are going to post a request
  204. * to test the completion of another request */
  205. struct _starpu_mpi_req *other_request;
  206. /* in the case of detached requests */
  207. int detached;
  208. void *callback_arg;
  209. void (*callback)(void *);
  210. /* in the case of user-defined datatypes, we need to send the size of the data */
  211. MPI_Request size_req;
  212. struct _starpu_mpi_envelope* envelope;
  213. unsigned is_internal_req:1;
  214. unsigned to_destroy:1;
  215. struct _starpu_mpi_req *internal_req;
  216. struct _starpu_mpi_early_data_handle *early_data_handle;
  217. int sequential_consistency;
  218. long pre_sync_jobid;
  219. long post_sync_jobid;
  220. UT_hash_handle hh;
  221. #ifdef STARPU_SIMGRID
  222. MPI_Status status_store;
  223. starpu_pthread_queue_t queue;
  224. unsigned done;
  225. #endif
  226. );
  227. PRIO_LIST_TYPE(_starpu_mpi_req, prio)
  228. struct _starpu_mpi_argc_argv
  229. {
  230. int initialize_mpi;
  231. int *argc;
  232. char ***argv;
  233. MPI_Comm comm;
  234. int fargc; // Fortran argc
  235. char **fargv; // Fortran argv
  236. int rank;
  237. int world_size;
  238. };
  239. void _starpu_mpi_progress_shutdown(int *value);
  240. int _starpu_mpi_progress_init(struct _starpu_mpi_argc_argv *argc_argv);
  241. #ifdef STARPU_SIMGRID
  242. void _starpu_mpi_wait_for_initialization();
  243. #endif
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. #endif // __STARPU_MPI_PRIVATE_H__