starpu_mpi_private.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 <core/simgrid.h>
  26. #ifdef __cplusplus
  27. extern "C"
  28. {
  29. #endif
  30. #ifdef STARPU_SIMGRID
  31. starpu_pthread_wait_t wait;
  32. starpu_pthread_queue_t dontsleep;
  33. struct _starpu_simgrid_mpi_req
  34. {
  35. MPI_Request *request;
  36. MPI_Status *status;
  37. starpu_pthread_queue_t *queue;
  38. unsigned *done;
  39. };
  40. int _starpu_mpi_simgrid_mpi_test(unsigned *done, int *flag);
  41. void _starpu_mpi_simgrid_wait_req(MPI_Request *request, MPI_Status *status, starpu_pthread_queue_t *queue, unsigned *done);
  42. #endif
  43. extern int _starpu_debug_rank;
  44. char *_starpu_mpi_get_mpi_error_code(int code);
  45. extern int _starpu_mpi_comm_debug;
  46. #ifdef STARPU_VERBOSE
  47. extern int _starpu_debug_level_min;
  48. extern int _starpu_debug_level_max;
  49. void _starpu_mpi_set_debug_level_min(int level);
  50. void _starpu_mpi_set_debug_level_max(int level);
  51. #endif
  52. extern int _starpu_mpi_fake_world_size;
  53. extern int _starpu_mpi_fake_world_rank;
  54. #ifdef STARPU_NO_ASSERT
  55. # define STARPU_MPI_ASSERT_MSG(x, msg, ...) do { if (0) { (void) (x); }} while(0)
  56. #else
  57. # if defined(__CUDACC__) && defined(STARPU_HAVE_WINDOWS)
  58. int _starpu_debug_rank;
  59. # define STARPU_MPI_ASSERT_MSG(x, msg, ...) \
  60. do \
  61. { \
  62. if (STARPU_UNLIKELY(!(x))) \
  63. { \
  64. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  65. fprintf(stderr, "\n[%d][starpu_mpi][%s][assert failure] " msg "\n\n", _starpu_debug_rank, __starpu_func__, ## __VA_ARGS__); *(int*)NULL = 0; \
  66. } \
  67. } while(0)
  68. # else
  69. # define STARPU_MPI_ASSERT_MSG(x, msg, ...) \
  70. do \
  71. { \
  72. if (STARPU_UNLIKELY(!(x))) \
  73. { \
  74. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  75. fprintf(stderr, "\n[%d][starpu_mpi][%s][assert failure] " msg "\n\n", _starpu_debug_rank, __starpu_func__, ## __VA_ARGS__); \
  76. } \
  77. assert(x); \
  78. } while(0)
  79. # endif
  80. #endif
  81. #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)
  82. #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)
  83. #define _STARPU_MPI_REALLOC(ptr, size) do { ptr = realloc(ptr, size); STARPU_MPI_ASSERT_MSG(ptr != NULL, "Cannot reallocate %ld bytes\n", (long) size); } while (0)
  84. #ifdef STARPU_VERBOSE
  85. # define _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, node, tag, utag, comm, way) \
  86. do \
  87. { \
  88. if (_starpu_mpi_comm_debug) \
  89. { \
  90. int __size; \
  91. char _comm_name[128]; \
  92. int _comm_name_len; \
  93. int _rank; \
  94. starpu_mpi_comm_rank(comm, &_rank); \
  95. MPI_Type_size(datatype, &__size); \
  96. MPI_Comm_get_name(comm, _comm_name, &_comm_name_len); \
  97. 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__); \
  98. fflush(stderr); \
  99. } \
  100. } while(0);
  101. # define _STARPU_MPI_COMM_TO_DEBUG(ptr, count, datatype, dest, tag, utag, comm) _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, dest, tag, utag, comm, "-->")
  102. # define _STARPU_MPI_COMM_FROM_DEBUG(ptr, count, datatype, source, tag, utag, comm) _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, source, tag, utag, comm, "<--")
  103. # define _STARPU_MPI_DEBUG(level, fmt, ...) \
  104. do \
  105. { \
  106. if (!_starpu_silent && _starpu_debug_level_min <= level && level <= _starpu_debug_level_max) \
  107. { \
  108. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  109. fprintf(stderr, "%*s[%d][starpu_mpi][%s:%d] " fmt , (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__ , __LINE__,## __VA_ARGS__); \
  110. fflush(stderr); \
  111. } \
  112. } while(0);
  113. #else
  114. # define _STARPU_MPI_COMM_DEBUG(ptr, count, datatype, node, tag, utag, comm, way) do { } while(0)
  115. # define _STARPU_MPI_COMM_TO_DEBUG(ptr, count, datatype, dest, tag, utag, comm) do { } while(0)
  116. # define _STARPU_MPI_COMM_FROM_DEBUG(ptr, count, datatype, source, tag, utag, comm) do { } while(0)
  117. # define _STARPU_MPI_DEBUG(level, fmt, ...) do { } while(0)
  118. #endif
  119. #define _STARPU_MPI_DISP(fmt, ...) do { if (!_starpu_silent) { \
  120. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  121. fprintf(stderr, "%*s[%d][starpu_mpi][%s:%d] " fmt , (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__ , __LINE__ ,## __VA_ARGS__); \
  122. fflush(stderr); }} while(0);
  123. #define _STARPU_MPI_MSG(fmt, ...) do { if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  124. fprintf(stderr, "[%d][starpu_mpi][%s:%d] " fmt , _starpu_debug_rank, __starpu_func__ , __LINE__ ,## __VA_ARGS__); \
  125. fflush(stderr); } while(0);
  126. #ifdef STARPU_VERBOSE
  127. # define _STARPU_MPI_LOG_IN() 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] -->\n", (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__ , __LINE__); \
  130. fflush(stderr); }} while(0)
  131. # define _STARPU_MPI_LOG_OUT() do { if (!_starpu_silent) { \
  132. if (_starpu_debug_rank == -1) starpu_mpi_comm_rank(MPI_COMM_WORLD, &_starpu_debug_rank); \
  133. fprintf(stderr, "%*s[%d][starpu_mpi][%s:%d] <--\n", (_starpu_debug_rank+1)*4, "", _starpu_debug_rank, __starpu_func__, __LINE__ ); \
  134. fflush(stderr); }} while(0)
  135. #else
  136. # define _STARPU_MPI_LOG_IN()
  137. # define _STARPU_MPI_LOG_OUT()
  138. #endif
  139. extern int _starpu_mpi_tag;
  140. #define _STARPU_MPI_TAG_ENVELOPE _starpu_mpi_tag
  141. #define _STARPU_MPI_TAG_DATA _starpu_mpi_tag+1
  142. #define _STARPU_MPI_TAG_SYNC_DATA _starpu_mpi_tag+2
  143. enum _starpu_mpi_request_type
  144. {
  145. SEND_REQ=0,
  146. RECV_REQ=1,
  147. WAIT_REQ=2,
  148. TEST_REQ=3,
  149. BARRIER_REQ=4,
  150. PROBE_REQ=5,
  151. UNKNOWN_REQ=6,
  152. };
  153. #define _STARPU_MPI_ENVELOPE_DATA 0
  154. #define _STARPU_MPI_ENVELOPE_SYNC_READY 1
  155. struct _starpu_mpi_envelope
  156. {
  157. int mode;
  158. starpu_ssize_t size;
  159. int data_tag;
  160. unsigned sync;
  161. };
  162. struct _starpu_mpi_req;
  163. struct _starpu_mpi_node_tag
  164. {
  165. MPI_Comm comm;
  166. int rank;
  167. int data_tag;
  168. };
  169. struct _starpu_mpi_data
  170. {
  171. int magic;
  172. struct _starpu_mpi_node_tag node_tag;
  173. int *cache_sent;
  174. int cache_received;
  175. };
  176. LIST_TYPE(_starpu_mpi_req,
  177. /* description of the data at StarPU level */
  178. starpu_data_handle_t data_handle;
  179. /* description of the data to be sent/received */
  180. MPI_Datatype datatype;
  181. char *datatype_name;
  182. void *ptr;
  183. starpu_ssize_t count;
  184. int registered_datatype;
  185. /* who are we talking to ? */
  186. struct _starpu_mpi_node_tag node_tag;
  187. void (*func)(struct _starpu_mpi_req *);
  188. MPI_Status *status;
  189. MPI_Request data_request;
  190. int *flag;
  191. unsigned sync;
  192. int ret;
  193. starpu_pthread_mutex_t req_mutex;
  194. starpu_pthread_cond_t req_cond;
  195. starpu_pthread_mutex_t posted_mutex;
  196. starpu_pthread_cond_t posted_cond;
  197. enum _starpu_mpi_request_type request_type; /* 0 send, 1 recv */
  198. unsigned submitted;
  199. unsigned completed;
  200. unsigned posted;
  201. /* In the case of a Wait/Test request, we are going to post a request
  202. * to test the completion of another request */
  203. struct _starpu_mpi_req *other_request;
  204. /* in the case of detached requests */
  205. int detached;
  206. void *callback_arg;
  207. void (*callback)(void *);
  208. /* in the case of user-defined datatypes, we need to send the size of the data */
  209. MPI_Request size_req;
  210. struct _starpu_mpi_envelope* envelope;
  211. int is_internal_req;
  212. struct _starpu_mpi_req *internal_req;
  213. struct _starpu_mpi_early_data_handle *early_data_handle;
  214. int sequential_consistency;
  215. long pre_sync_jobid;
  216. long post_sync_jobid;
  217. UT_hash_handle hh;
  218. #ifdef STARPU_SIMGRID
  219. MPI_Status status_store;
  220. starpu_pthread_queue_t queue;
  221. unsigned done;
  222. #endif
  223. );
  224. struct _starpu_mpi_argc_argv
  225. {
  226. int initialize_mpi;
  227. int *argc;
  228. char ***argv;
  229. MPI_Comm comm;
  230. int fargc; // Fortran argc
  231. char **fargv; // Fortran argv
  232. int rank;
  233. int world_size;
  234. };
  235. void _starpu_mpi_progress_shutdown(int *value);
  236. int _starpu_mpi_progress_init(struct _starpu_mpi_argc_argv *argc_argv);
  237. #ifdef STARPU_SIMGRID
  238. void _starpu_mpi_wait_for_initialization();
  239. #endif
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243. #endif // __STARPU_MPI_PRIVATE_H__