starpu_mpi.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 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. #include <stdlib.h>
  18. #include <starpu_mpi.h>
  19. #include <starpu_mpi_datatype.h>
  20. #include <starpu_mpi_private.h>
  21. #include <starpu_mpi_cache.h>
  22. #include <starpu_profiling.h>
  23. #include <starpu_mpi_stats.h>
  24. #include <starpu_mpi_cache.h>
  25. #include <starpu_mpi_sync_data.h>
  26. #include <starpu_mpi_early_data.h>
  27. #include <starpu_mpi_early_request.h>
  28. #include <starpu_mpi_select_node.h>
  29. #include <starpu_mpi_tag.h>
  30. #include <starpu_mpi_comm.h>
  31. #include <common/config.h>
  32. #include <common/thread.h>
  33. #include <datawizard/interfaces/data_interface.h>
  34. #include <datawizard/coherency.h>
  35. #include <core/simgrid.h>
  36. static void _starpu_mpi_add_sync_point_in_fxt(void);
  37. static void _starpu_mpi_submit_ready_request(void *arg);
  38. static void _starpu_mpi_handle_ready_request(struct _starpu_mpi_req *req);
  39. static void _starpu_mpi_handle_request_termination(struct _starpu_mpi_req *req);
  40. #ifdef STARPU_VERBOSE
  41. static char *_starpu_mpi_request_type(enum _starpu_mpi_request_type request_type);
  42. #endif
  43. static struct _starpu_mpi_req *_starpu_mpi_isend_common(starpu_data_handle_t data_handle,
  44. int dest, int data_tag, MPI_Comm comm,
  45. unsigned detached, unsigned sync, void (*callback)(void *), void *arg,
  46. int sequential_consistency);
  47. static struct _starpu_mpi_req *_starpu_mpi_irecv_common(starpu_data_handle_t data_handle,
  48. int source, int data_tag, MPI_Comm comm,
  49. unsigned detached, unsigned sync, void (*callback)(void *), void *arg,
  50. int sequential_consistency, int is_internal_req,
  51. starpu_ssize_t count);
  52. static void _starpu_mpi_handle_detached_request(struct _starpu_mpi_req *req);
  53. static void _starpu_mpi_early_data_cb(void* arg);
  54. /* The list of ready requests */
  55. static struct _starpu_mpi_req_list *ready_requests;
  56. /* The list of detached requests that have already been submitted to MPI */
  57. static struct _starpu_mpi_req_list *detached_requests;
  58. static starpu_pthread_mutex_t detached_requests_mutex;
  59. /* Condition to wake up progression thread */
  60. static starpu_pthread_cond_t cond_progression;
  61. /* Condition to wake up waiting for all current MPI requests to finish */
  62. static starpu_pthread_cond_t cond_finished;
  63. static starpu_pthread_mutex_t mutex;
  64. static starpu_pthread_t progress_thread;
  65. static int running = 0;
  66. #ifdef STARPU_SIMGRID
  67. static int _mpi_world_size;
  68. static int _mpi_world_rank;
  69. #endif
  70. /* Count requests posted by the application and not yet submitted to MPI */
  71. static starpu_pthread_mutex_t mutex_posted_requests;
  72. static int posted_requests = 0, newer_requests, barrier_running = 0;
  73. #define _STARPU_MPI_INC_POSTED_REQUESTS(value) { STARPU_PTHREAD_MUTEX_LOCK(&mutex_posted_requests); posted_requests += value; STARPU_PTHREAD_MUTEX_UNLOCK(&mutex_posted_requests); }
  74. #pragma weak smpi_simulated_main_
  75. extern int smpi_simulated_main_(int argc, char *argv[]);
  76. static void _starpu_mpi_request_init(struct _starpu_mpi_req **req)
  77. {
  78. *req = calloc(1, sizeof(struct _starpu_mpi_req));
  79. STARPU_MPI_ASSERT_MSG(*req, "Invalid request");
  80. /* Initialize the request structure */
  81. (*req)->data_handle = NULL;
  82. (*req)->datatype = 0;
  83. (*req)->ptr = NULL;
  84. (*req)->count = -1;
  85. (*req)->registered_datatype = -1;
  86. (*req)->node_tag.rank = -1;
  87. (*req)->node_tag.data_tag = -1;
  88. (*req)->node_tag.comm = NULL;
  89. (*req)->func = NULL;
  90. (*req)->status = NULL;
  91. (*req)->data_request = 0;
  92. (*req)->flag = NULL;
  93. (*req)->ret = -1;
  94. STARPU_PTHREAD_MUTEX_INIT(&((*req)->req_mutex), NULL);
  95. STARPU_PTHREAD_COND_INIT(&((*req)->req_cond), NULL);
  96. STARPU_PTHREAD_MUTEX_INIT(&((*req)->posted_mutex), NULL);
  97. STARPU_PTHREAD_COND_INIT(&((*req)->posted_cond), NULL);
  98. (*req)->request_type = UNKNOWN_REQ;
  99. (*req)->submitted = 0;
  100. (*req)->completed = 0;
  101. (*req)->posted = 0;
  102. (*req)->other_request = NULL;
  103. (*req)->sync = 0;
  104. (*req)->detached = -1;
  105. (*req)->callback = NULL;
  106. (*req)->callback_arg = NULL;
  107. (*req)->size_req = 0;
  108. (*req)->internal_req = NULL;
  109. (*req)->is_internal_req = 0;
  110. (*req)->envelope = NULL;
  111. (*req)->sequential_consistency = 1;
  112. }
  113. /********************************************************/
  114. /* */
  115. /* Send/Receive functionalities */
  116. /* */
  117. /********************************************************/
  118. struct _starpu_mpi_early_data_cb_args
  119. {
  120. starpu_data_handle_t data_handle;
  121. starpu_data_handle_t early_handle;
  122. struct _starpu_mpi_req *req;
  123. void *buffer;
  124. };
  125. static void _starpu_mpi_submit_ready_request(void *arg)
  126. {
  127. _STARPU_MPI_LOG_IN();
  128. struct _starpu_mpi_req *req = arg;
  129. _STARPU_MPI_INC_POSTED_REQUESTS(-1);
  130. _STARPU_MPI_DEBUG(3, "new req %p srcdst %d tag %d and type %s %d\n", req, req->node_tag.rank, req->node_tag.data_tag, _starpu_mpi_request_type(req->request_type), req->is_internal_req);
  131. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  132. if (req->request_type == RECV_REQ)
  133. {
  134. /* Case : the request is the internal receive request submitted
  135. * by StarPU-MPI to receive incoming data without a matching
  136. * early_request from the application. We immediately allocate the
  137. * pointer associated to the data_handle, and push it into the
  138. * ready_requests list, so as the real MPI request can be submitted
  139. * before the next submission of the envelope-catching request. */
  140. if (req->is_internal_req)
  141. {
  142. _starpu_mpi_handle_allocate_datatype(req->data_handle, &req->datatype, &req->registered_datatype);
  143. if (req->registered_datatype == 1)
  144. {
  145. req->count = 1;
  146. req->ptr = starpu_data_get_local_ptr(req->data_handle);
  147. }
  148. else
  149. {
  150. STARPU_ASSERT(req->count);
  151. req->ptr = malloc(req->count);
  152. STARPU_MPI_ASSERT_MSG(req->ptr, "cannot allocate message of size %ld\n", req->count);
  153. }
  154. _STARPU_MPI_DEBUG(3, "Pushing internal starpu_mpi_irecv request %p type %s tag %d src %d data %p ptr %p datatype '%s' count %d registered_datatype %d \n",
  155. req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, req->ptr,
  156. _starpu_mpi_datatype(req->datatype), (int)req->count, req->registered_datatype);
  157. _starpu_mpi_req_list_push_front(ready_requests, req);
  158. /* inform the starpu mpi thread that the request has been pushed in the ready_requests list */
  159. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  160. STARPU_PTHREAD_MUTEX_LOCK(&req->posted_mutex);
  161. req->posted = 1;
  162. STARPU_PTHREAD_COND_BROADCAST(&req->posted_cond);
  163. STARPU_PTHREAD_MUTEX_UNLOCK(&req->posted_mutex);
  164. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  165. }
  166. else
  167. {
  168. /* test whether some data with the given tag and source have already been received by StarPU-MPI*/
  169. struct _starpu_mpi_early_data_handle *early_data_handle = _starpu_mpi_early_data_find(&req->node_tag);
  170. /* Case: a receive request for a data with the given tag and source has already been
  171. * posted by StarPU. Asynchronously requests a Read permission over the temporary handle ,
  172. * so as when the internal receive is completed, the _starpu_mpi_early_data_cb function
  173. * will be called to bring the data back to the original data handle associated to the request.*/
  174. if (early_data_handle)
  175. {
  176. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  177. STARPU_PTHREAD_MUTEX_LOCK(&(early_data_handle->req_mutex));
  178. while (!(early_data_handle->req_ready))
  179. STARPU_PTHREAD_COND_WAIT(&(early_data_handle->req_cond), &(early_data_handle->req_mutex));
  180. STARPU_PTHREAD_MUTEX_UNLOCK(&(early_data_handle->req_mutex));
  181. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  182. _STARPU_MPI_DEBUG(3, "The RECV request %p with tag %d has already been received, copying previously received data into handle's pointer..\n", req, req->node_tag.data_tag);
  183. STARPU_ASSERT(req->data_handle != early_data_handle->handle);
  184. req->internal_req = early_data_handle->req;
  185. struct _starpu_mpi_early_data_cb_args *cb_args = malloc(sizeof(struct _starpu_mpi_early_data_cb_args));
  186. cb_args->data_handle = req->data_handle;
  187. cb_args->early_handle = early_data_handle->handle;
  188. cb_args->buffer = early_data_handle->buffer;
  189. cb_args->req = req;
  190. _STARPU_MPI_DEBUG(3, "Calling data_acquire_cb on starpu_mpi_copy_cb..\n");
  191. starpu_data_acquire_cb(early_data_handle->handle,STARPU_R,_starpu_mpi_early_data_cb,(void*) cb_args);
  192. }
  193. /* Case: no matching data has been received. Store the receive request as an early_request. */
  194. else
  195. {
  196. struct _starpu_mpi_req *sync_req = _starpu_mpi_sync_data_find(req->node_tag.data_tag, req->node_tag.rank, req->node_tag.comm);
  197. _STARPU_MPI_DEBUG(3, "----------> Looking for sync data for tag %d and src %d = %p\n", req->node_tag.data_tag, req->node_tag.rank, sync_req);
  198. if (sync_req)
  199. {
  200. req->sync = 1;
  201. _starpu_mpi_handle_allocate_datatype(req->data_handle, &req->datatype, &req->registered_datatype);
  202. if (req->registered_datatype == 1)
  203. {
  204. req->count = 1;
  205. req->ptr = starpu_data_get_local_ptr(req->data_handle);
  206. }
  207. else
  208. {
  209. req->count = sync_req->count;
  210. STARPU_ASSERT(req->count);
  211. req->ptr = malloc(req->count);
  212. STARPU_MPI_ASSERT_MSG(req->ptr, "cannot allocate message of size %ld\n", req->count);
  213. }
  214. _starpu_mpi_req_list_push_front(ready_requests, req);
  215. free(sync_req);
  216. }
  217. else
  218. {
  219. _STARPU_MPI_DEBUG(3, "Adding the pending receive request %p (srcdst %d tag %d) into the request hashmap\n", req, req->node_tag.rank, req->node_tag.data_tag);
  220. _starpu_mpi_early_request_enqueue(req);
  221. }
  222. }
  223. }
  224. }
  225. else
  226. {
  227. _starpu_mpi_req_list_push_front(ready_requests, req);
  228. _STARPU_MPI_DEBUG(3, "Pushing new request %p type %s tag %d src %d data %p ptr %p datatype '%s' count %d registered_datatype %d \n",
  229. req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, req->ptr,
  230. _starpu_mpi_datatype(req->datatype), (int)req->count, req->registered_datatype);
  231. }
  232. newer_requests = 1;
  233. STARPU_PTHREAD_COND_BROADCAST(&cond_progression);
  234. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  235. _STARPU_MPI_LOG_OUT();
  236. }
  237. static struct _starpu_mpi_req *_starpu_mpi_isend_irecv_common(starpu_data_handle_t data_handle,
  238. int srcdst, int data_tag, MPI_Comm comm,
  239. unsigned detached, unsigned sync, void (*callback)(void *), void *arg,
  240. enum _starpu_mpi_request_type request_type, void (*func)(struct _starpu_mpi_req *),
  241. enum starpu_data_access_mode mode,
  242. int sequential_consistency,
  243. int is_internal_req,
  244. starpu_ssize_t count)
  245. {
  246. struct _starpu_mpi_req *req;
  247. _STARPU_MPI_LOG_IN();
  248. _STARPU_MPI_INC_POSTED_REQUESTS(1);
  249. _starpu_mpi_comm_register(comm);
  250. /* Initialize the request structure */
  251. _starpu_mpi_request_init(&req);
  252. req->request_type = request_type;
  253. req->data_handle = data_handle;
  254. req->node_tag.rank = srcdst;
  255. req->node_tag.data_tag = data_tag;
  256. req->node_tag.comm = comm;
  257. req->detached = detached;
  258. req->sync = sync;
  259. req->callback = callback;
  260. req->callback_arg = arg;
  261. req->func = func;
  262. req->sequential_consistency = sequential_consistency;
  263. req->is_internal_req = is_internal_req;
  264. req->count = count;
  265. /* Asynchronously request StarPU to fetch the data in main memory: when
  266. * it is available in main memory, _starpu_mpi_submit_ready_request(req) is called and
  267. * the request is actually submitted */
  268. starpu_data_acquire_cb_sequential_consistency(data_handle, mode, _starpu_mpi_submit_ready_request, (void *)req, sequential_consistency);
  269. _STARPU_MPI_LOG_OUT();
  270. return req;
  271. }
  272. /********************************************************/
  273. /* */
  274. /* Send functionalities */
  275. /* */
  276. /********************************************************/
  277. static void _starpu_mpi_isend_data_func(struct _starpu_mpi_req *req)
  278. {
  279. _STARPU_MPI_LOG_IN();
  280. _STARPU_MPI_DEBUG(20, "post MPI isend request %p type %s tag %d src %d data %p datasize %ld ptr %p datatype '%s' count %d registered_datatype %d sync %d\n", req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, starpu_data_get_size(req->data_handle), req->ptr, _starpu_mpi_datatype(req->datatype), (int)req->count, req->registered_datatype, req->sync);
  281. _starpu_mpi_comm_amounts_inc(req->node_tag.comm, req->node_tag.rank, req->datatype, req->count);
  282. _STARPU_MPI_TRACE_ISEND_SUBMIT_BEGIN(req->node_tag.rank, req->node_tag.data_tag, 0);
  283. if (req->sync == 0)
  284. {
  285. _STARPU_MPI_COMM_TO_DEBUG(req->count, req->datatype, req->node_tag.rank, _STARPU_MPI_TAG_DATA, req->node_tag.data_tag, req->node_tag.comm);
  286. req->ret = MPI_Isend(req->ptr, req->count, req->datatype, req->node_tag.rank, _STARPU_MPI_TAG_DATA, req->node_tag.comm, &req->data_request);
  287. STARPU_MPI_ASSERT_MSG(req->ret == MPI_SUCCESS, "MPI_Isend returning %s", _starpu_mpi_get_mpi_code(req->ret));
  288. }
  289. else
  290. {
  291. _STARPU_MPI_COMM_TO_DEBUG(req->count, req->datatype, req->node_tag.rank, _STARPU_MPI_TAG_SYNC_DATA, req->node_tag.data_tag, req->node_tag.comm);
  292. req->ret = MPI_Issend(req->ptr, req->count, req->datatype, req->node_tag.rank, _STARPU_MPI_TAG_SYNC_DATA, req->node_tag.comm, &req->data_request);
  293. STARPU_MPI_ASSERT_MSG(req->ret == MPI_SUCCESS, "MPI_Issend returning %s", _starpu_mpi_get_mpi_code(req->ret));
  294. }
  295. _STARPU_MPI_TRACE_ISEND_SUBMIT_END(req->node_tag.rank, req->node_tag.data_tag, 0);
  296. /* somebody is perhaps waiting for the MPI request to be posted */
  297. STARPU_PTHREAD_MUTEX_LOCK(&req->req_mutex);
  298. req->submitted = 1;
  299. STARPU_PTHREAD_COND_BROADCAST(&req->req_cond);
  300. STARPU_PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  301. _starpu_mpi_handle_detached_request(req);
  302. _STARPU_MPI_LOG_OUT();
  303. }
  304. static void _starpu_mpi_isend_size_func(struct _starpu_mpi_req *req)
  305. {
  306. _starpu_mpi_handle_allocate_datatype(req->data_handle, &req->datatype, &req->registered_datatype);
  307. req->envelope = calloc(1,sizeof(struct _starpu_mpi_envelope));
  308. req->envelope->mode = _STARPU_MPI_ENVELOPE_DATA;
  309. req->envelope->data_tag = req->node_tag.data_tag;
  310. req->envelope->sync = req->sync;
  311. if (req->registered_datatype == 1)
  312. {
  313. int size;
  314. req->count = 1;
  315. req->ptr = starpu_data_get_local_ptr(req->data_handle);
  316. MPI_Type_size(req->datatype, &size);
  317. req->envelope->size = (starpu_ssize_t)req->count * size;
  318. _STARPU_MPI_DEBUG(20, "Post MPI isend count (%ld) datatype_size %ld request to %d\n",req->count,starpu_data_get_size(req->data_handle), req->node_tag.rank);
  319. _STARPU_MPI_COMM_TO_DEBUG(sizeof(struct _starpu_mpi_envelope), MPI_BYTE, req->node_tag.rank, _STARPU_MPI_TAG_ENVELOPE, _STARPU_MPI_TAG_ENVELOPE, req->node_tag.comm);
  320. MPI_Isend(req->envelope, sizeof(struct _starpu_mpi_envelope), MPI_BYTE, req->node_tag.rank, _STARPU_MPI_TAG_ENVELOPE, req->node_tag.comm, &req->size_req);
  321. }
  322. else
  323. {
  324. int ret;
  325. // Do not pack the data, just try to find out the size
  326. starpu_data_pack(req->data_handle, NULL, &(req->envelope->size));
  327. if (req->envelope->size != -1)
  328. {
  329. // We already know the size of the data, let's send it to overlap with the packing of the data
  330. _STARPU_MPI_DEBUG(20, "Sending size %ld (%ld %s) to node %d (first call to pack)\n", req->envelope->size, sizeof(req->count), _starpu_mpi_datatype(MPI_BYTE), req->node_tag.rank);
  331. req->count = req->envelope->size;
  332. _STARPU_MPI_COMM_TO_DEBUG(sizeof(struct _starpu_mpi_envelope), MPI_BYTE, req->node_tag.rank, _STARPU_MPI_TAG_ENVELOPE, _STARPU_MPI_TAG_ENVELOPE, req->node_tag.comm);
  333. ret = MPI_Isend(req->envelope, sizeof(struct _starpu_mpi_envelope), MPI_BYTE, req->node_tag.rank, _STARPU_MPI_TAG_ENVELOPE, req->node_tag.comm, &req->size_req);
  334. STARPU_MPI_ASSERT_MSG(ret == MPI_SUCCESS, "when sending size, MPI_Isend returning %s", _starpu_mpi_get_mpi_code(ret));
  335. }
  336. // Pack the data
  337. starpu_data_pack(req->data_handle, &req->ptr, &req->count);
  338. if (req->envelope->size == -1)
  339. {
  340. // We know the size now, let's send it
  341. _STARPU_MPI_DEBUG(20, "Sending size %ld (%ld %s) to node %d (second call to pack)\n", req->envelope->size, sizeof(req->count), _starpu_mpi_datatype(MPI_BYTE), req->node_tag.rank);
  342. _STARPU_MPI_COMM_TO_DEBUG(sizeof(struct _starpu_mpi_envelope), MPI_BYTE, req->node_tag.rank, _STARPU_MPI_TAG_ENVELOPE, _STARPU_MPI_TAG_ENVELOPE, req->node_tag.comm);
  343. ret = MPI_Isend(req->envelope, sizeof(struct _starpu_mpi_envelope), MPI_BYTE, req->node_tag.rank, _STARPU_MPI_TAG_ENVELOPE, req->node_tag.comm, &req->size_req);
  344. STARPU_MPI_ASSERT_MSG(ret == MPI_SUCCESS, "when sending size, MPI_Isend returning %s", _starpu_mpi_get_mpi_code(ret));
  345. }
  346. else
  347. {
  348. // We check the size returned with the 2 calls to pack is the same
  349. STARPU_MPI_ASSERT_MSG(req->count == req->envelope->size, "Calls to pack_data returned different sizes %ld != %ld", req->count, req->envelope->size);
  350. }
  351. // We can send the data now
  352. }
  353. if (req->sync)
  354. {
  355. // If the data is to be sent in synchronous mode, we need to wait for the receiver ready message
  356. _starpu_mpi_sync_data_add(req);
  357. }
  358. else
  359. {
  360. // Otherwise we can send the data
  361. _starpu_mpi_isend_data_func(req);
  362. }
  363. }
  364. static struct _starpu_mpi_req *_starpu_mpi_isend_common(starpu_data_handle_t data_handle,
  365. int dest, int data_tag, MPI_Comm comm,
  366. unsigned detached, unsigned sync, void (*callback)(void *), void *arg,
  367. int sequential_consistency)
  368. {
  369. return _starpu_mpi_isend_irecv_common(data_handle, dest, data_tag, comm, detached, sync, callback, arg, SEND_REQ, _starpu_mpi_isend_size_func, STARPU_R, sequential_consistency, 0, 0);
  370. }
  371. int starpu_mpi_isend(starpu_data_handle_t data_handle, starpu_mpi_req *public_req, int dest, int data_tag, MPI_Comm comm)
  372. {
  373. _STARPU_MPI_LOG_IN();
  374. STARPU_MPI_ASSERT_MSG(public_req, "starpu_mpi_isend needs a valid starpu_mpi_req");
  375. struct _starpu_mpi_req *req;
  376. _STARPU_MPI_TRACE_ISEND_COMPLETE_BEGIN(dest, data_tag, 0);
  377. req = _starpu_mpi_isend_common(data_handle, dest, data_tag, comm, 0, 0, NULL, NULL, 1);
  378. _STARPU_MPI_TRACE_ISEND_COMPLETE_END(dest, data_tag, 0);
  379. STARPU_MPI_ASSERT_MSG(req, "Invalid return for _starpu_mpi_isend_common");
  380. *public_req = req;
  381. _STARPU_MPI_LOG_OUT();
  382. return 0;
  383. }
  384. int starpu_mpi_isend_detached(starpu_data_handle_t data_handle,
  385. int dest, int data_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  386. {
  387. _STARPU_MPI_LOG_IN();
  388. _starpu_mpi_isend_common(data_handle, dest, data_tag, comm, 1, 0, callback, arg, 1);
  389. _STARPU_MPI_LOG_OUT();
  390. return 0;
  391. }
  392. int starpu_mpi_send(starpu_data_handle_t data_handle, int dest, int data_tag, MPI_Comm comm)
  393. {
  394. starpu_mpi_req req;
  395. MPI_Status status;
  396. _STARPU_MPI_LOG_IN();
  397. memset(&status, 0, sizeof(MPI_Status));
  398. starpu_mpi_isend(data_handle, &req, dest, data_tag, comm);
  399. starpu_mpi_wait(&req, &status);
  400. _STARPU_MPI_LOG_OUT();
  401. return 0;
  402. }
  403. int starpu_mpi_issend(starpu_data_handle_t data_handle, starpu_mpi_req *public_req, int dest, int data_tag, MPI_Comm comm)
  404. {
  405. _STARPU_MPI_LOG_IN();
  406. STARPU_MPI_ASSERT_MSG(public_req, "starpu_mpi_issend needs a valid starpu_mpi_req");
  407. struct _starpu_mpi_req *req;
  408. req = _starpu_mpi_isend_common(data_handle, dest, data_tag, comm, 0, 1, NULL, NULL, 1);
  409. STARPU_MPI_ASSERT_MSG(req, "Invalid return for _starpu_mpi_isend_common");
  410. *public_req = req;
  411. _STARPU_MPI_LOG_OUT();
  412. return 0;
  413. }
  414. int starpu_mpi_issend_detached(starpu_data_handle_t data_handle, int dest, int data_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  415. {
  416. _STARPU_MPI_LOG_IN();
  417. _starpu_mpi_isend_common(data_handle, dest, data_tag, comm, 1, 1, callback, arg, 1);
  418. _STARPU_MPI_LOG_OUT();
  419. return 0;
  420. }
  421. /********************************************************/
  422. /* */
  423. /* receive functionalities */
  424. /* */
  425. /********************************************************/
  426. static void _starpu_mpi_irecv_data_func(struct _starpu_mpi_req *req)
  427. {
  428. _STARPU_MPI_LOG_IN();
  429. _STARPU_MPI_DEBUG(20, "post MPI irecv request %p type %s tag %d src %d data %p ptr %p datatype '%s' count %d registered_datatype %d \n", req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, req->ptr, _starpu_mpi_datatype(req->datatype), (int)req->count, req->registered_datatype);
  430. _STARPU_MPI_TRACE_IRECV_SUBMIT_BEGIN(req->node_tag.rank, req->node_tag.data_tag);
  431. if (req->sync)
  432. {
  433. struct _starpu_mpi_envelope *_envelope = calloc(1,sizeof(struct _starpu_mpi_envelope));
  434. _envelope->mode = _STARPU_MPI_ENVELOPE_SYNC_READY;
  435. _envelope->data_tag = req->node_tag.data_tag;
  436. _STARPU_MPI_DEBUG(20, "Telling node %d it can send the data and waiting for the data back ...\n", req->node_tag.rank);
  437. _STARPU_MPI_COMM_TO_DEBUG(sizeof(struct _starpu_mpi_envelope), MPI_BYTE, req->node_tag.rank, _STARPU_MPI_TAG_ENVELOPE, _STARPU_MPI_TAG_ENVELOPE, req->node_tag.comm);
  438. req->ret = MPI_Send(_envelope, sizeof(struct _starpu_mpi_envelope), MPI_BYTE, req->node_tag.rank, _STARPU_MPI_TAG_ENVELOPE, req->node_tag.comm);
  439. STARPU_MPI_ASSERT_MSG(req->ret == MPI_SUCCESS, "MPI_Send returning %s", _starpu_mpi_get_mpi_code(req->ret));
  440. free(_envelope);
  441. }
  442. if (req->sync)
  443. {
  444. _STARPU_MPI_COMM_FROM_DEBUG(req->count, req->datatype, req->node_tag.rank, _STARPU_MPI_TAG_SYNC_DATA, req->node_tag.data_tag, req->node_tag.comm);
  445. req->ret = MPI_Irecv(req->ptr, req->count, req->datatype, req->node_tag.rank, _STARPU_MPI_TAG_SYNC_DATA, req->node_tag.comm, &req->data_request);
  446. }
  447. else
  448. {
  449. _STARPU_MPI_COMM_FROM_DEBUG(req->count, req->datatype, req->node_tag.rank, _STARPU_MPI_TAG_DATA, req->node_tag.data_tag, req->node_tag.comm);
  450. req->ret = MPI_Irecv(req->ptr, req->count, req->datatype, req->node_tag.rank, _STARPU_MPI_TAG_DATA, req->node_tag.comm, &req->data_request);
  451. }
  452. STARPU_MPI_ASSERT_MSG(req->ret == MPI_SUCCESS, "MPI_IRecv returning %s", _starpu_mpi_get_mpi_code(req->ret));
  453. _STARPU_MPI_TRACE_IRECV_SUBMIT_END(req->node_tag.rank, req->node_tag.data_tag);
  454. /* somebody is perhaps waiting for the MPI request to be posted */
  455. STARPU_PTHREAD_MUTEX_LOCK(&req->req_mutex);
  456. req->submitted = 1;
  457. STARPU_PTHREAD_COND_BROADCAST(&req->req_cond);
  458. STARPU_PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  459. _starpu_mpi_handle_detached_request(req);
  460. _STARPU_MPI_LOG_OUT();
  461. }
  462. static struct _starpu_mpi_req *_starpu_mpi_irecv_common(starpu_data_handle_t data_handle, int source, int data_tag, MPI_Comm comm, unsigned detached, unsigned sync, void (*callback)(void *), void *arg, int sequential_consistency, int is_internal_req, starpu_ssize_t count)
  463. {
  464. return _starpu_mpi_isend_irecv_common(data_handle, source, data_tag, comm, detached, sync, callback, arg, RECV_REQ, _starpu_mpi_irecv_data_func, STARPU_W, sequential_consistency, is_internal_req, count);
  465. }
  466. int starpu_mpi_irecv(starpu_data_handle_t data_handle, starpu_mpi_req *public_req, int source, int data_tag, MPI_Comm comm)
  467. {
  468. _STARPU_MPI_LOG_IN();
  469. STARPU_MPI_ASSERT_MSG(public_req, "starpu_mpi_irecv needs a valid starpu_mpi_req");
  470. // // We check if a tag is defined for the data handle, if not,
  471. // // we define the one given for the communication.
  472. // // A tag is necessary for the internal mpi engine.
  473. // int tag = starpu_data_get_tag(data_handle);
  474. // if (tag == -1)
  475. // starpu_data_set_tag(data_handle, data_tag);
  476. struct _starpu_mpi_req *req;
  477. _STARPU_MPI_TRACE_IRECV_COMPLETE_BEGIN(source, data_tag);
  478. req = _starpu_mpi_irecv_common(data_handle, source, data_tag, comm, 0, 0, NULL, NULL, 1, 0, 0);
  479. _STARPU_MPI_TRACE_IRECV_COMPLETE_END(source, data_tag);
  480. STARPU_MPI_ASSERT_MSG(req, "Invalid return for _starpu_mpi_irecv_common");
  481. *public_req = req;
  482. _STARPU_MPI_LOG_OUT();
  483. return 0;
  484. }
  485. int starpu_mpi_irecv_detached(starpu_data_handle_t data_handle, int source, int data_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  486. {
  487. _STARPU_MPI_LOG_IN();
  488. // // We check if a tag is defined for the data handle, if not,
  489. // // we define the one given for the communication.
  490. // // A tag is necessary for the internal mpi engine.
  491. // int tag = starpu_data_get_tag(data_handle);
  492. // if (tag == -1)
  493. // starpu_data_set_tag(data_handle, data_tag);
  494. _starpu_mpi_irecv_common(data_handle, source, data_tag, comm, 1, 0, callback, arg, 1, 0, 0);
  495. _STARPU_MPI_LOG_OUT();
  496. return 0;
  497. }
  498. int starpu_mpi_irecv_detached_sequential_consistency(starpu_data_handle_t data_handle, int source, int data_tag, MPI_Comm comm, void (*callback)(void *), void *arg, int sequential_consistency)
  499. {
  500. _STARPU_MPI_LOG_IN();
  501. // // We check if a tag is defined for the data handle, if not,
  502. // // we define the one given for the communication.
  503. // // A tag is necessary for the internal mpi engine.
  504. // int tag = starpu_data_get_tag(data_handle);
  505. // if (tag == -1)
  506. // starpu_data_set_tag(data_handle, data_tag);
  507. _starpu_mpi_irecv_common(data_handle, source, data_tag, comm, 1, 0, callback, arg, sequential_consistency, 0, 0);
  508. _STARPU_MPI_LOG_OUT();
  509. return 0;
  510. }
  511. int starpu_mpi_recv(starpu_data_handle_t data_handle, int source, int data_tag, MPI_Comm comm, MPI_Status *status)
  512. {
  513. starpu_mpi_req req;
  514. _STARPU_MPI_LOG_IN();
  515. // // We check if a tag is defined for the data handle, if not,
  516. // // we define the one given for the communication.
  517. // // A tag is necessary for the internal mpi engine.
  518. // int tag = starpu_data_get_tag(data_handle);
  519. // if (tag == -1)
  520. // starpu_data_set_tag(data_handle, data_tag);
  521. starpu_mpi_irecv(data_handle, &req, source, data_tag, comm);
  522. starpu_mpi_wait(&req, status);
  523. _STARPU_MPI_LOG_OUT();
  524. return 0;
  525. }
  526. /********************************************************/
  527. /* */
  528. /* Wait functionalities */
  529. /* */
  530. /********************************************************/
  531. static void _starpu_mpi_wait_func(struct _starpu_mpi_req *waiting_req)
  532. {
  533. _STARPU_MPI_LOG_IN();
  534. /* Which is the mpi request we are waiting for ? */
  535. struct _starpu_mpi_req *req = waiting_req->other_request;
  536. _STARPU_MPI_TRACE_UWAIT_BEGIN(req->node_tag.rank, req->node_tag.data_tag);
  537. req->ret = MPI_Wait(&req->data_request, waiting_req->status);
  538. STARPU_MPI_ASSERT_MSG(req->ret == MPI_SUCCESS, "MPI_Wait returning %s", _starpu_mpi_get_mpi_code(req->ret));
  539. _STARPU_MPI_TRACE_UWAIT_END(req->node_tag.rank, req->node_tag.data_tag);
  540. _starpu_mpi_handle_request_termination(req);
  541. _STARPU_MPI_LOG_OUT();
  542. }
  543. int starpu_mpi_wait(starpu_mpi_req *public_req, MPI_Status *status)
  544. {
  545. int ret;
  546. struct _starpu_mpi_req *req = *public_req;
  547. struct _starpu_mpi_req *waiting_req;
  548. _STARPU_MPI_LOG_IN();
  549. _STARPU_MPI_INC_POSTED_REQUESTS(1);
  550. /* We cannot try to complete a MPI request that was not actually posted
  551. * to MPI yet. */
  552. STARPU_PTHREAD_MUTEX_LOCK(&(req->req_mutex));
  553. while (!(req->submitted))
  554. STARPU_PTHREAD_COND_WAIT(&(req->req_cond), &(req->req_mutex));
  555. STARPU_PTHREAD_MUTEX_UNLOCK(&(req->req_mutex));
  556. /* Initialize the request structure */
  557. _starpu_mpi_request_init(&waiting_req);
  558. waiting_req->status = status;
  559. waiting_req->other_request = req;
  560. waiting_req->func = _starpu_mpi_wait_func;
  561. waiting_req->request_type = WAIT_REQ;
  562. _starpu_mpi_submit_ready_request(waiting_req);
  563. /* We wait for the MPI request to finish */
  564. STARPU_PTHREAD_MUTEX_LOCK(&req->req_mutex);
  565. while (!req->completed)
  566. STARPU_PTHREAD_COND_WAIT(&req->req_cond, &req->req_mutex);
  567. STARPU_PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  568. ret = req->ret;
  569. /* The internal request structure was automatically allocated */
  570. *public_req = NULL;
  571. if (req->internal_req)
  572. {
  573. free(req->internal_req); req->internal_req = NULL;
  574. }
  575. free(req);
  576. free(waiting_req);
  577. _STARPU_MPI_LOG_OUT();
  578. return ret;
  579. }
  580. /********************************************************/
  581. /* */
  582. /* Test functionalities */
  583. /* */
  584. /********************************************************/
  585. static void _starpu_mpi_test_func(struct _starpu_mpi_req *testing_req)
  586. {
  587. _STARPU_MPI_LOG_IN();
  588. /* Which is the mpi request we are testing for ? */
  589. struct _starpu_mpi_req *req = testing_req->other_request;
  590. _STARPU_MPI_DEBUG(2, "Test request %p type %s tag %d src %d data %p ptr %p datatype '%s' count %d registered_datatype %d \n",
  591. req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, req->ptr, _starpu_mpi_datatype(req->datatype), (int)req->count, req->registered_datatype);
  592. _STARPU_MPI_TRACE_UTESTING_BEGIN(req->node_tag.rank, req->node_tag.data_tag);
  593. req->ret = MPI_Test(&req->data_request, testing_req->flag, testing_req->status);
  594. STARPU_MPI_ASSERT_MSG(req->ret == MPI_SUCCESS, "MPI_Test returning %s", _starpu_mpi_get_mpi_code(req->ret));
  595. _STARPU_MPI_TRACE_UTESTING_END(req->node_tag.rank, req->node_tag.data_tag);
  596. if (*testing_req->flag)
  597. {
  598. testing_req->ret = req->ret;
  599. _starpu_mpi_handle_request_termination(req);
  600. }
  601. STARPU_PTHREAD_MUTEX_LOCK(&testing_req->req_mutex);
  602. testing_req->completed = 1;
  603. STARPU_PTHREAD_COND_SIGNAL(&testing_req->req_cond);
  604. STARPU_PTHREAD_MUTEX_UNLOCK(&testing_req->req_mutex);
  605. _STARPU_MPI_LOG_OUT();
  606. }
  607. int starpu_mpi_test(starpu_mpi_req *public_req, int *flag, MPI_Status *status)
  608. {
  609. _STARPU_MPI_LOG_IN();
  610. int ret = 0;
  611. STARPU_MPI_ASSERT_MSG(public_req, "starpu_mpi_test needs a valid starpu_mpi_req");
  612. struct _starpu_mpi_req *req = *public_req;
  613. STARPU_MPI_ASSERT_MSG(!req->detached, "MPI_Test cannot be called on a detached request");
  614. STARPU_PTHREAD_MUTEX_LOCK(&req->req_mutex);
  615. unsigned submitted = req->submitted;
  616. STARPU_PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  617. if (submitted)
  618. {
  619. struct _starpu_mpi_req *testing_req;
  620. _starpu_mpi_request_init(&testing_req);
  621. /* Initialize the request structure */
  622. STARPU_PTHREAD_MUTEX_INIT(&(testing_req->req_mutex), NULL);
  623. STARPU_PTHREAD_COND_INIT(&(testing_req->req_cond), NULL);
  624. testing_req->flag = flag;
  625. testing_req->status = status;
  626. testing_req->other_request = req;
  627. testing_req->func = _starpu_mpi_test_func;
  628. testing_req->completed = 0;
  629. testing_req->request_type = TEST_REQ;
  630. _STARPU_MPI_INC_POSTED_REQUESTS(1);
  631. _starpu_mpi_submit_ready_request(testing_req);
  632. /* We wait for the test request to finish */
  633. STARPU_PTHREAD_MUTEX_LOCK(&(testing_req->req_mutex));
  634. while (!(testing_req->completed))
  635. STARPU_PTHREAD_COND_WAIT(&(testing_req->req_cond), &(testing_req->req_mutex));
  636. STARPU_PTHREAD_MUTEX_UNLOCK(&(testing_req->req_mutex));
  637. ret = testing_req->ret;
  638. if (*(testing_req->flag))
  639. {
  640. /* The request was completed so we free the internal
  641. * request structure which was automatically allocated
  642. * */
  643. *public_req = NULL;
  644. if (req->internal_req)
  645. {
  646. free(req->internal_req); req->internal_req = NULL;
  647. }
  648. free(req);
  649. }
  650. free(testing_req);
  651. }
  652. else
  653. {
  654. *flag = 0;
  655. }
  656. _STARPU_MPI_LOG_OUT();
  657. return ret;
  658. }
  659. /********************************************************/
  660. /* */
  661. /* Barrier functionalities */
  662. /* */
  663. /********************************************************/
  664. static void _starpu_mpi_barrier_func(struct _starpu_mpi_req *barrier_req)
  665. {
  666. _STARPU_MPI_LOG_IN();
  667. barrier_req->ret = MPI_Barrier(barrier_req->node_tag.comm);
  668. STARPU_MPI_ASSERT_MSG(barrier_req->ret == MPI_SUCCESS, "MPI_Barrier returning %s", _starpu_mpi_get_mpi_code(barrier_req->ret));
  669. _starpu_mpi_handle_request_termination(barrier_req);
  670. _STARPU_MPI_LOG_OUT();
  671. }
  672. int starpu_mpi_barrier(MPI_Comm comm)
  673. {
  674. int ret;
  675. struct _starpu_mpi_req *barrier_req;
  676. _STARPU_MPI_LOG_IN();
  677. _starpu_mpi_request_init(&barrier_req);
  678. /* First wait for *both* all tasks and MPI requests to finish, in case
  679. * some tasks generate MPI requests, MPI requests generate tasks, etc.
  680. */
  681. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  682. STARPU_MPI_ASSERT_MSG(!barrier_running, "Concurrent starpu_mpi_barrier is not implemented, even on different communicators");
  683. barrier_running = 1;
  684. do
  685. {
  686. while (posted_requests)
  687. /* Wait for all current MPI requests to finish */
  688. STARPU_PTHREAD_COND_WAIT(&cond_finished, &mutex);
  689. /* No current request, clear flag */
  690. newer_requests = 0;
  691. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  692. /* Now wait for all tasks */
  693. starpu_task_wait_for_all();
  694. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  695. /* Check newer_requests again, in case some MPI requests
  696. * triggered by tasks completed and triggered tasks between
  697. * wait_for_all finished and we take the lock */
  698. } while (posted_requests || newer_requests);
  699. barrier_running = 0;
  700. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  701. /* Initialize the request structure */
  702. STARPU_PTHREAD_MUTEX_INIT(&(barrier_req->req_mutex), NULL);
  703. STARPU_PTHREAD_COND_INIT(&(barrier_req->req_cond), NULL);
  704. barrier_req->func = _starpu_mpi_barrier_func;
  705. barrier_req->request_type = BARRIER_REQ;
  706. barrier_req->node_tag.comm = comm;
  707. _STARPU_MPI_INC_POSTED_REQUESTS(1);
  708. _starpu_mpi_submit_ready_request(barrier_req);
  709. /* We wait for the MPI request to finish */
  710. STARPU_PTHREAD_MUTEX_LOCK(&barrier_req->req_mutex);
  711. while (!barrier_req->completed)
  712. STARPU_PTHREAD_COND_WAIT(&barrier_req->req_cond, &barrier_req->req_mutex);
  713. STARPU_PTHREAD_MUTEX_UNLOCK(&barrier_req->req_mutex);
  714. ret = barrier_req->ret;
  715. free(barrier_req);
  716. _STARPU_MPI_LOG_OUT();
  717. return ret;
  718. }
  719. /********************************************************/
  720. /* */
  721. /* Progression */
  722. /* */
  723. /********************************************************/
  724. #ifdef STARPU_VERBOSE
  725. static char *_starpu_mpi_request_type(enum _starpu_mpi_request_type request_type)
  726. {
  727. switch (request_type)
  728. {
  729. case SEND_REQ: return "SEND_REQ";
  730. case RECV_REQ: return "RECV_REQ";
  731. case WAIT_REQ: return "WAIT_REQ";
  732. case TEST_REQ: return "TEST_REQ";
  733. case BARRIER_REQ: return "BARRIER_REQ";
  734. case UNKNOWN_REQ: return "UNSET_REQ";
  735. default: return "unknown request type";
  736. }
  737. }
  738. #endif
  739. static void _starpu_mpi_handle_request_termination(struct _starpu_mpi_req *req)
  740. {
  741. _STARPU_MPI_LOG_IN();
  742. _STARPU_MPI_DEBUG(2, "complete MPI request %p type %s tag %d src %d data %p ptr %p datatype '%s' count %d registered_datatype %d internal_req %p\n",
  743. req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, req->ptr,
  744. _starpu_mpi_datatype(req->datatype), (int)req->count, req->registered_datatype, req->internal_req);
  745. if (req->internal_req)
  746. {
  747. struct _starpu_mpi_early_data_handle *early_data_handle = _starpu_mpi_early_data_find(&req->node_tag);
  748. STARPU_MPI_ASSERT_MSG(early_data_handle, "Could not find a copy data handle with the tag %d and the node %d\n", req->node_tag.data_tag, req->node_tag.rank);
  749. _STARPU_MPI_DEBUG(3, "Handling deleting of early_data structure from the hashmap..\n");
  750. _starpu_mpi_early_data_delete(early_data_handle);
  751. free(early_data_handle);
  752. }
  753. else
  754. {
  755. if (req->request_type == RECV_REQ || req->request_type == SEND_REQ)
  756. {
  757. if (req->registered_datatype == 0)
  758. {
  759. if (req->request_type == SEND_REQ)
  760. {
  761. // We need to make sure the communication for sending the size
  762. // has completed, as MPI can re-order messages, let's call
  763. // MPI_Wait to make sure data have been sent
  764. int ret;
  765. ret = MPI_Wait(&req->size_req, MPI_STATUS_IGNORE);
  766. STARPU_MPI_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Wait returning %s", _starpu_mpi_get_mpi_code(ret));
  767. free(req->ptr);
  768. }
  769. else if (req->request_type == RECV_REQ)
  770. {
  771. // req->ptr is freed by starpu_data_unpack
  772. starpu_data_unpack(req->data_handle, req->ptr, req->count);
  773. }
  774. }
  775. else
  776. {
  777. _starpu_mpi_handle_free_datatype(req->data_handle, &req->datatype);
  778. }
  779. }
  780. }
  781. if (req->data_handle)
  782. starpu_data_release(req->data_handle);
  783. if (req->envelope)
  784. {
  785. free(req->envelope);
  786. req->envelope = NULL;
  787. }
  788. /* Execute the specified callback, if any */
  789. if (req->callback)
  790. req->callback(req->callback_arg);
  791. /* tell anyone potentially waiting on the request that it is
  792. * terminated now */
  793. STARPU_PTHREAD_MUTEX_LOCK(&req->req_mutex);
  794. req->completed = 1;
  795. STARPU_PTHREAD_COND_BROADCAST(&req->req_cond);
  796. STARPU_PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  797. _STARPU_MPI_LOG_OUT();
  798. }
  799. static void _starpu_mpi_early_data_cb(void* arg)
  800. {
  801. struct _starpu_mpi_early_data_cb_args *args = arg;
  802. // We store in the application request the internal MPI
  803. // request so that it can be used by starpu_mpi_wait
  804. args->req->data_request = args->req->internal_req->data_request;
  805. args->req->submitted = 1;
  806. if (args->buffer)
  807. {
  808. /* Data has been received as a raw memory, it has to be unpacked */
  809. struct starpu_data_interface_ops *itf_src = starpu_data_get_interface_ops(args->early_handle);
  810. struct starpu_data_interface_ops *itf_dst = starpu_data_get_interface_ops(args->data_handle);
  811. STARPU_MPI_ASSERT_MSG(itf_dst->unpack_data, "The data interface does not define an unpack function\n");
  812. itf_dst->unpack_data(args->data_handle, STARPU_MAIN_RAM, args->buffer, itf_src->get_size(args->early_handle));
  813. free(args->buffer);
  814. }
  815. else
  816. {
  817. struct starpu_data_interface_ops *itf = starpu_data_get_interface_ops(args->early_handle);
  818. void* itf_src = starpu_data_get_interface_on_node(args->early_handle, STARPU_MAIN_RAM);
  819. void* itf_dst = starpu_data_get_interface_on_node(args->data_handle, STARPU_MAIN_RAM);
  820. if (!itf->copy_methods->ram_to_ram)
  821. {
  822. _STARPU_MPI_DEBUG(3, "Initiating any_to_any copy..\n");
  823. itf->copy_methods->any_to_any(itf_src, STARPU_MAIN_RAM, itf_dst, STARPU_MAIN_RAM, NULL);
  824. }
  825. else
  826. {
  827. _STARPU_MPI_DEBUG(3, "Initiating ram_to_ram copy..\n");
  828. itf->copy_methods->ram_to_ram(itf_src, STARPU_MAIN_RAM, itf_dst, STARPU_MAIN_RAM);
  829. }
  830. }
  831. _STARPU_MPI_DEBUG(3, "Done, handling release of early_handle..\n");
  832. starpu_data_release(args->early_handle);
  833. _STARPU_MPI_DEBUG(3, "Done, handling unregister of early_handle..\n");
  834. starpu_data_unregister_submit(args->early_handle);
  835. _STARPU_MPI_DEBUG(3, "Done, handling request %p termination of the already received request\n",args->req);
  836. // If the request is detached, we need to call _starpu_mpi_handle_request_termination
  837. // as it will not be called automatically as the request is not in the list detached_requests
  838. if (args->req->detached)
  839. _starpu_mpi_handle_request_termination(args->req);
  840. // else: If the request is not detached its termination will
  841. // be handled when calling starpu_mpi_wait
  842. free(args);
  843. }
  844. #ifdef STARPU_MPI_ACTIVITY
  845. static unsigned _starpu_mpi_progression_hook_func(void *arg STARPU_ATTRIBUTE_UNUSED)
  846. {
  847. unsigned may_block = 1;
  848. STARPU_PTHREAD_MUTEX_LOCK(&detached_requests_mutex);
  849. if (!_starpu_mpi_req_list_empty(detached_requests))
  850. {
  851. STARPU_PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  852. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  853. STARPU_PTHREAD_COND_SIGNAL(&cond_progression);
  854. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  855. may_block = 0;
  856. }
  857. else
  858. STARPU_PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  859. return may_block;
  860. }
  861. #endif /* STARPU_MPI_ACTIVITY */
  862. static void _starpu_mpi_test_detached_requests(void)
  863. {
  864. _STARPU_MPI_LOG_IN();
  865. int flag;
  866. MPI_Status status;
  867. struct _starpu_mpi_req *req, *next_req;
  868. STARPU_PTHREAD_MUTEX_LOCK(&detached_requests_mutex);
  869. for (req = _starpu_mpi_req_list_begin(detached_requests);
  870. req != _starpu_mpi_req_list_end(detached_requests);
  871. req = next_req)
  872. {
  873. next_req = _starpu_mpi_req_list_next(req);
  874. STARPU_PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  875. //_STARPU_MPI_DEBUG(3, "Test detached request %p - mpitag %d - TYPE %s %d\n", &req->data_request, req->node_tag.data_tag, _starpu_mpi_request_type(req->request_type), req->node_tag.rank);
  876. req->ret = MPI_Test(&req->data_request, &flag, &status);
  877. STARPU_MPI_ASSERT_MSG(req->ret == MPI_SUCCESS, "MPI_Test returning %s", _starpu_mpi_get_mpi_code(req->ret));
  878. if (flag)
  879. {
  880. if (req->request_type == RECV_REQ)
  881. {
  882. _STARPU_MPI_TRACE_IRECV_COMPLETE_BEGIN(req->node_tag.rank, req->node_tag.data_tag);
  883. }
  884. else if (req->request_type == SEND_REQ)
  885. {
  886. _STARPU_MPI_TRACE_ISEND_COMPLETE_BEGIN(req->node_tag.rank, req->node_tag.data_tag, 0);
  887. }
  888. _starpu_mpi_handle_request_termination(req);
  889. if (req->request_type == RECV_REQ)
  890. {
  891. _STARPU_MPI_TRACE_IRECV_COMPLETE_END(req->node_tag.rank, req->node_tag.data_tag);
  892. }
  893. else if (req->request_type == SEND_REQ)
  894. {
  895. _STARPU_MPI_TRACE_ISEND_COMPLETE_END(req->node_tag.rank, req->node_tag.data_tag, 0);
  896. }
  897. }
  898. STARPU_PTHREAD_MUTEX_LOCK(&detached_requests_mutex);
  899. if (flag)
  900. {
  901. _starpu_mpi_req_list_erase(detached_requests, req);
  902. #ifdef STARPU_DEVEL
  903. #warning FIXME: when do we free internal requests
  904. #endif
  905. if (!req->is_internal_req)
  906. free(req);
  907. }
  908. }
  909. STARPU_PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  910. _STARPU_MPI_LOG_OUT();
  911. }
  912. static void _starpu_mpi_handle_detached_request(struct _starpu_mpi_req *req)
  913. {
  914. if (req->detached)
  915. {
  916. /* put the submitted request into the list of pending requests
  917. * so that it can be handled by the progression mechanisms */
  918. STARPU_PTHREAD_MUTEX_LOCK(&detached_requests_mutex);
  919. _starpu_mpi_req_list_push_front(detached_requests, req);
  920. STARPU_PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  921. starpu_wake_all_blocked_workers();
  922. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  923. STARPU_PTHREAD_COND_SIGNAL(&cond_progression);
  924. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  925. }
  926. }
  927. static void _starpu_mpi_handle_ready_request(struct _starpu_mpi_req *req)
  928. {
  929. _STARPU_MPI_LOG_IN();
  930. STARPU_MPI_ASSERT_MSG(req, "Invalid request");
  931. /* submit the request to MPI */
  932. _STARPU_MPI_DEBUG(2, "Handling new request %p type %s tag %d src %d data %p ptr %p datatype '%s' count %d registered_datatype %d \n",
  933. req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, req->ptr, _starpu_mpi_datatype(req->datatype), (int)req->count, req->registered_datatype);
  934. req->func(req);
  935. _STARPU_MPI_LOG_OUT();
  936. }
  937. struct _starpu_mpi_argc_argv
  938. {
  939. int initialize_mpi;
  940. int *argc;
  941. char ***argv;
  942. MPI_Comm comm;
  943. };
  944. static void _starpu_mpi_print_thread_level_support(int thread_level, char *msg)
  945. {
  946. switch (thread_level)
  947. {
  948. case MPI_THREAD_SERIALIZED:
  949. {
  950. _STARPU_DISP("MPI%s MPI_THREAD_SERIALIZED; Multiple threads may make MPI calls, but only one at a time.\n", msg);
  951. break;
  952. }
  953. case MPI_THREAD_FUNNELED:
  954. {
  955. _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);
  956. break;
  957. }
  958. case MPI_THREAD_SINGLE:
  959. {
  960. _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);
  961. break;
  962. }
  963. }
  964. }
  965. static void _starpu_mpi_receive_early_data(struct _starpu_mpi_envelope *envelope, MPI_Status status, MPI_Comm comm)
  966. {
  967. _STARPU_MPI_DEBUG(20, "Request with tag %d and source %d not found, creating a early_handle to receive incoming data..\n", envelope->data_tag, status.MPI_SOURCE);
  968. _STARPU_MPI_DEBUG(20, "Request sync %d\n", envelope->sync);
  969. struct _starpu_mpi_early_data_handle* early_data_handle = _starpu_mpi_early_data_create(envelope, status.MPI_SOURCE, comm);
  970. starpu_data_handle_t data_handle = NULL;
  971. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  972. data_handle = _starpu_mpi_data_get_data_handle_from_tag(envelope->data_tag);
  973. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  974. if (data_handle && starpu_data_get_interface_id(data_handle) < STARPU_MAX_INTERFACE_ID)
  975. {
  976. /* We know which data will receive it and we won't have to unpack, use just the same kind of data. */
  977. early_data_handle->buffer = NULL;
  978. starpu_data_register_same(&early_data_handle->handle, data_handle);
  979. _starpu_mpi_early_data_add(early_data_handle);
  980. }
  981. else
  982. {
  983. /* The application has not registered yet a data with the tag,
  984. * we are going to receive the data as a raw memory, and give it
  985. * to the application when it post a receive for this tag
  986. */
  987. _STARPU_MPI_DEBUG(3, "Posting a receive for a data of size %d which has not yet been registered\n", (int)early_data_handle->env->size);
  988. early_data_handle->buffer = malloc(early_data_handle->env->size);
  989. starpu_variable_data_register(&early_data_handle->handle, STARPU_MAIN_RAM, (uintptr_t) early_data_handle->buffer, early_data_handle->env->size);
  990. _starpu_mpi_early_data_add(early_data_handle);
  991. }
  992. _STARPU_MPI_DEBUG(20, "Posting internal detached irecv on early_handle with tag %d from comm %p src %d ..\n", early_data_handle->node_tag.data_tag, comm, status.MPI_SOURCE);
  993. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  994. early_data_handle->req = _starpu_mpi_irecv_common(early_data_handle->handle, status.MPI_SOURCE,
  995. early_data_handle->node_tag.data_tag, comm, 1, 0,
  996. NULL, NULL, 1, 1, envelope->size);
  997. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  998. // We wait until the request is pushed in the
  999. // ready_request list, that ensures that the next loop
  1000. // will call _starpu_mpi_handle_ready_request
  1001. // on the request and post the corresponding mpi_irecv,
  1002. // otherwise, it may lead to read data as envelop
  1003. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  1004. STARPU_PTHREAD_MUTEX_LOCK(&(early_data_handle->req->posted_mutex));
  1005. while (!(early_data_handle->req->posted))
  1006. STARPU_PTHREAD_COND_WAIT(&(early_data_handle->req->posted_cond), &(early_data_handle->req->posted_mutex));
  1007. STARPU_PTHREAD_MUTEX_UNLOCK(&(early_data_handle->req->posted_mutex));
  1008. STARPU_PTHREAD_MUTEX_LOCK(&early_data_handle->req_mutex);
  1009. early_data_handle->req_ready = 1;
  1010. STARPU_PTHREAD_COND_BROADCAST(&early_data_handle->req_cond);
  1011. STARPU_PTHREAD_MUTEX_UNLOCK(&early_data_handle->req_mutex);
  1012. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  1013. }
  1014. static void *_starpu_mpi_progress_thread_func(void *arg)
  1015. {
  1016. struct _starpu_mpi_argc_argv *argc_argv = (struct _starpu_mpi_argc_argv *) arg;
  1017. int rank, worldsize;
  1018. if (argc_argv->initialize_mpi)
  1019. {
  1020. int thread_support;
  1021. _STARPU_DEBUG("Calling MPI_Init_thread\n");
  1022. if (MPI_Init_thread(argc_argv->argc, argc_argv->argv, MPI_THREAD_SERIALIZED, &thread_support) != MPI_SUCCESS)
  1023. {
  1024. _STARPU_ERROR("MPI_Init_thread failed\n");
  1025. }
  1026. _starpu_mpi_print_thread_level_support(thread_support, "_Init_thread level =");
  1027. }
  1028. else
  1029. {
  1030. int provided;
  1031. MPI_Query_thread(&provided);
  1032. _starpu_mpi_print_thread_level_support(provided, " has been initialized with");
  1033. }
  1034. MPI_Comm_rank(argc_argv->comm, &rank);
  1035. MPI_Comm_size(argc_argv->comm, &worldsize);
  1036. MPI_Comm_set_errhandler(argc_argv->comm, MPI_ERRORS_RETURN);
  1037. {
  1038. _STARPU_MPI_TRACE_START(rank, worldsize);
  1039. #ifdef STARPU_USE_FXT
  1040. starpu_profiling_set_id(rank);
  1041. #endif //STARPU_USE_FXT
  1042. }
  1043. _starpu_mpi_add_sync_point_in_fxt();
  1044. _starpu_mpi_comm_amounts_init(argc_argv->comm);
  1045. _starpu_mpi_cache_init(argc_argv->comm);
  1046. _starpu_mpi_select_node_init();
  1047. _starpu_mpi_tag_init();
  1048. _starpu_mpi_comm_init(argc_argv->comm);
  1049. _starpu_mpi_early_request_init();
  1050. _starpu_mpi_early_data_init();
  1051. _starpu_mpi_sync_data_init();
  1052. /* notify the main thread that the progression thread is ready */
  1053. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  1054. running = 1;
  1055. STARPU_PTHREAD_COND_SIGNAL(&cond_progression);
  1056. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  1057. #ifdef STARPU_SIMGRID
  1058. _mpi_world_size = worldsize;
  1059. _mpi_world_rank = rank;
  1060. /* Now that MPI is set up, let the rest of simgrid get initialized */
  1061. MSG_process_create_with_arguments("main", smpi_simulated_main_, NULL, _starpu_simgrid_get_host_by_name("MAIN"), *(argc_argv->argc), *(argc_argv->argv));
  1062. #endif
  1063. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  1064. int envelope_request_submitted = 0;
  1065. while (running || posted_requests || !(_starpu_mpi_req_list_empty(ready_requests)) || !(_starpu_mpi_req_list_empty(detached_requests)))// || !(_starpu_mpi_early_request_count()) || !(_starpu_mpi_sync_data_count()))
  1066. {
  1067. /* shall we block ? */
  1068. unsigned block = _starpu_mpi_req_list_empty(ready_requests) && _starpu_mpi_early_request_count() == 0 && _starpu_mpi_sync_data_count() == 0;
  1069. #ifndef STARPU_MPI_ACTIVITY
  1070. STARPU_PTHREAD_MUTEX_LOCK(&detached_requests_mutex);
  1071. block = block && _starpu_mpi_req_list_empty(detached_requests);
  1072. STARPU_PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  1073. #endif /* STARPU_MPI_ACTIVITY */
  1074. if (block)
  1075. {
  1076. _STARPU_MPI_DEBUG(3, "NO MORE REQUESTS TO HANDLE\n");
  1077. _STARPU_MPI_TRACE_SLEEP_BEGIN();
  1078. if (barrier_running)
  1079. /* Tell mpi_barrier */
  1080. STARPU_PTHREAD_COND_SIGNAL(&cond_finished);
  1081. STARPU_PTHREAD_COND_WAIT(&cond_progression, &mutex);
  1082. _STARPU_MPI_TRACE_SLEEP_END();
  1083. }
  1084. /* get one request */
  1085. struct _starpu_mpi_req *req;
  1086. while (!_starpu_mpi_req_list_empty(ready_requests))
  1087. {
  1088. req = _starpu_mpi_req_list_pop_back(ready_requests);
  1089. /* handling a request is likely to block for a while
  1090. * (on a sync_data_with_mem call), we want to let the
  1091. * application submit requests in the meantime, so we
  1092. * release the lock. */
  1093. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  1094. _starpu_mpi_handle_ready_request(req);
  1095. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  1096. }
  1097. /* If there is no currently submitted envelope_request submitted to
  1098. * catch envelopes from senders, and there is some pending
  1099. * receive requests on our side, we resubmit a header request. */
  1100. if (((_starpu_mpi_early_request_count() > 0) || (_starpu_mpi_sync_data_count() > 0)) && (envelope_request_submitted == 0))// && (HASH_COUNT(_starpu_mpi_early_data_handle_hashmap) == 0))
  1101. {
  1102. _starpu_mpi_comm_post_recv();
  1103. envelope_request_submitted = 1;
  1104. }
  1105. /* test whether there are some terminated "detached request" */
  1106. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  1107. _starpu_mpi_test_detached_requests();
  1108. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  1109. if (envelope_request_submitted == 1)
  1110. {
  1111. int flag;
  1112. struct _starpu_mpi_envelope *envelope;
  1113. MPI_Status envelope_status;
  1114. MPI_Comm envelope_comm;
  1115. /* test whether an envelope has arrived. */
  1116. flag = _starpu_mpi_comm_test_recv(&envelope_status, &envelope, &envelope_comm);
  1117. if (flag)
  1118. {
  1119. _STARPU_MPI_DEBUG(4, "Envelope received with mode %d\n", envelope->mode);
  1120. if (envelope->mode == _STARPU_MPI_ENVELOPE_SYNC_READY)
  1121. {
  1122. struct _starpu_mpi_req *_sync_req = _starpu_mpi_sync_data_find(envelope->data_tag, envelope_status.MPI_SOURCE, envelope_comm);
  1123. _STARPU_MPI_DEBUG(20, "Sending data with tag %d to node %d\n", _sync_req->node_tag.data_tag, envelope_status.MPI_SOURCE);
  1124. STARPU_MPI_ASSERT_MSG(envelope->data_tag == _sync_req->node_tag.data_tag, "Tag mismatch (envelope %d != req %d)\n", envelope->data_tag, _sync_req->node_tag.data_tag);
  1125. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  1126. _starpu_mpi_isend_data_func(_sync_req);
  1127. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  1128. }
  1129. else
  1130. {
  1131. _STARPU_MPI_DEBUG(3, "Searching for application request with tag %d and source %d (size %ld)\n", envelope->data_tag, envelope_status.MPI_SOURCE, envelope->size);
  1132. struct _starpu_mpi_req *early_request = _starpu_mpi_early_request_dequeue(envelope->data_tag, envelope_status.MPI_SOURCE, envelope_comm);
  1133. /* Case: a data will arrive before a matching receive is
  1134. * posted by the application. Create a temporary handle to
  1135. * store the incoming data, submit a starpu_mpi_irecv_detached
  1136. * on this handle, and store it as an early_data
  1137. */
  1138. if (early_request == NULL)
  1139. {
  1140. if (envelope->sync)
  1141. {
  1142. _STARPU_MPI_DEBUG(2000, "-------------------------> adding request for tag %d\n", envelope->data_tag);
  1143. struct _starpu_mpi_req *new_req;
  1144. #ifdef STARPU_DEVEL
  1145. #warning creating a request is not really useful.
  1146. #endif
  1147. /* Initialize the request structure */
  1148. _starpu_mpi_request_init(&new_req);
  1149. new_req->request_type = RECV_REQ;
  1150. new_req->data_handle = NULL;
  1151. new_req->node_tag.rank = envelope_status.MPI_SOURCE;
  1152. new_req->node_tag.data_tag = envelope->data_tag;
  1153. new_req->node_tag.comm = envelope_comm;
  1154. new_req->detached = 1;
  1155. new_req->sync = 1;
  1156. new_req->callback = NULL;
  1157. new_req->callback_arg = NULL;
  1158. new_req->func = _starpu_mpi_irecv_data_func;
  1159. new_req->sequential_consistency = 1;
  1160. new_req->is_internal_req = 0; // ????
  1161. new_req->count = envelope->size;
  1162. _starpu_mpi_sync_data_add(new_req);
  1163. }
  1164. else
  1165. {
  1166. _starpu_mpi_receive_early_data(envelope, envelope_status, envelope_comm);
  1167. }
  1168. }
  1169. /* Case: a matching application request has been found for
  1170. * the incoming data, we handle the correct allocation
  1171. * of the pointer associated to the data handle, then
  1172. * submit the corresponding receive with
  1173. * _starpu_mpi_handle_ready_request. */
  1174. else
  1175. {
  1176. _STARPU_MPI_DEBUG(2000, "A matching application request has been found for the incoming data with tag %d\n", envelope->data_tag);
  1177. _STARPU_MPI_DEBUG(2000, "Request sync %d\n", envelope->sync);
  1178. early_request->sync = envelope->sync;
  1179. _starpu_mpi_handle_allocate_datatype(early_request->data_handle, &early_request->datatype, &early_request->registered_datatype);
  1180. if (early_request->registered_datatype == 1)
  1181. {
  1182. early_request->count = 1;
  1183. early_request->ptr = starpu_data_get_local_ptr(early_request->data_handle);
  1184. }
  1185. else
  1186. {
  1187. early_request->count = envelope->size;
  1188. early_request->ptr = malloc(early_request->count);
  1189. STARPU_MPI_ASSERT_MSG(early_request->ptr, "cannot allocate message of size %ld\n", early_request->count);
  1190. }
  1191. _STARPU_MPI_DEBUG(3, "Handling new request... \n");
  1192. /* handling a request is likely to block for a while
  1193. * (on a sync_data_with_mem call), we want to let the
  1194. * application submit requests in the meantime, so we
  1195. * release the lock. */
  1196. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  1197. _starpu_mpi_handle_ready_request(early_request);
  1198. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  1199. }
  1200. }
  1201. envelope_request_submitted = 0;
  1202. }
  1203. else
  1204. {
  1205. //_STARPU_MPI_DEBUG(4, "Nothing received, continue ..\n");
  1206. }
  1207. }
  1208. }
  1209. if (envelope_request_submitted)
  1210. {
  1211. _starpu_mpi_comm_cancel_recv();
  1212. envelope_request_submitted = 0;
  1213. }
  1214. STARPU_MPI_ASSERT_MSG(_starpu_mpi_req_list_empty(detached_requests), "List of detached requests not empty");
  1215. STARPU_MPI_ASSERT_MSG(_starpu_mpi_req_list_empty(ready_requests), "List of ready requests not empty");
  1216. STARPU_MPI_ASSERT_MSG(posted_requests == 0, "Number of posted request is not zero");
  1217. _starpu_mpi_early_request_check_termination();
  1218. _starpu_mpi_early_data_check_termination();
  1219. _starpu_mpi_sync_data_check_termination();
  1220. if (argc_argv->initialize_mpi)
  1221. {
  1222. _STARPU_MPI_DEBUG(3, "Calling MPI_Finalize()\n");
  1223. MPI_Finalize();
  1224. }
  1225. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  1226. _starpu_mpi_sync_data_free();
  1227. _starpu_mpi_early_data_free();
  1228. _starpu_mpi_early_request_free();
  1229. free(argc_argv);
  1230. return NULL;
  1231. }
  1232. /********************************************************/
  1233. /* */
  1234. /* (De)Initialization methods */
  1235. /* */
  1236. /********************************************************/
  1237. #ifdef STARPU_MPI_ACTIVITY
  1238. static int hookid = - 1;
  1239. #endif /* STARPU_MPI_ACTIVITY */
  1240. static void _starpu_mpi_add_sync_point_in_fxt(void)
  1241. {
  1242. #ifdef STARPU_USE_FXT
  1243. int rank;
  1244. int worldsize;
  1245. int ret;
  1246. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  1247. starpu_mpi_comm_size(MPI_COMM_WORLD, &worldsize);
  1248. ret = MPI_Barrier(MPI_COMM_WORLD);
  1249. STARPU_MPI_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Barrier returning %s", _starpu_mpi_get_mpi_code(ret));
  1250. /* We generate a "unique" key so that we can make sure that different
  1251. * FxT traces come from the same MPI run. */
  1252. int random_number;
  1253. /* XXX perhaps we don't want to generate a new seed if the application
  1254. * specified some reproductible behaviour ? */
  1255. if (rank == 0)
  1256. {
  1257. srand(time(NULL));
  1258. random_number = rand();
  1259. }
  1260. ret = MPI_Bcast(&random_number, 1, MPI_INT, 0, MPI_COMM_WORLD);
  1261. STARPU_MPI_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Bcast returning %s", _starpu_mpi_get_mpi_code(ret));
  1262. _STARPU_MPI_TRACE_BARRIER(rank, worldsize, random_number);
  1263. _STARPU_MPI_DEBUG(3, "unique key %x\n", random_number);
  1264. #endif
  1265. }
  1266. static
  1267. int _starpu_mpi_initialize(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm)
  1268. {
  1269. STARPU_PTHREAD_MUTEX_INIT(&mutex, NULL);
  1270. STARPU_PTHREAD_COND_INIT(&cond_progression, NULL);
  1271. STARPU_PTHREAD_COND_INIT(&cond_finished, NULL);
  1272. ready_requests = _starpu_mpi_req_list_new();
  1273. STARPU_PTHREAD_MUTEX_INIT(&detached_requests_mutex, NULL);
  1274. detached_requests = _starpu_mpi_req_list_new();
  1275. STARPU_PTHREAD_MUTEX_INIT(&mutex_posted_requests, NULL);
  1276. struct _starpu_mpi_argc_argv *argc_argv = malloc(sizeof(struct _starpu_mpi_argc_argv));
  1277. argc_argv->initialize_mpi = initialize_mpi;
  1278. argc_argv->argc = argc;
  1279. argc_argv->argv = argv;
  1280. argc_argv->comm = comm;
  1281. #ifdef STARPU_MPI_ACTIVITY
  1282. hookid = starpu_progression_hook_register(_starpu_mpi_progression_hook_func, NULL);
  1283. STARPU_MPI_ASSERT_MSG(hookid >= 0, "starpu_progression_hook_register failed");
  1284. #endif /* STARPU_MPI_ACTIVITY */
  1285. #ifdef STARPU_SIMGRID
  1286. _starpu_mpi_progress_thread_func(argc_argv);
  1287. return 0;
  1288. #else
  1289. STARPU_PTHREAD_CREATE(&progress_thread, NULL, _starpu_mpi_progress_thread_func, argc_argv);
  1290. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  1291. while (!running)
  1292. STARPU_PTHREAD_COND_WAIT(&cond_progression, &mutex);
  1293. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  1294. return 0;
  1295. #endif
  1296. }
  1297. #ifdef STARPU_SIMGRID
  1298. /* This is called before application's main, to initialize SMPI before we can
  1299. * create MSG processes to run application's main */
  1300. int _starpu_mpi_simgrid_init(int argc, char *argv[])
  1301. {
  1302. return _starpu_mpi_initialize(&argc, &argv, 1, MPI_COMM_WORLD);
  1303. }
  1304. #endif
  1305. int starpu_mpi_init_comm(int *argc, char ***argv, int initialize_mpi, MPI_Comm comm)
  1306. {
  1307. #ifdef STARPU_SIMGRID
  1308. STARPU_MPI_ASSERT_MSG(initialize_mpi, "application has to let StarPU initialize MPI");
  1309. return 0;
  1310. #else
  1311. return _starpu_mpi_initialize(argc, argv, initialize_mpi, comm);
  1312. #endif
  1313. }
  1314. int starpu_mpi_init(int *argc, char ***argv, int initialize_mpi)
  1315. {
  1316. return starpu_mpi_init_comm(argc, argv, initialize_mpi, MPI_COMM_WORLD);
  1317. }
  1318. int starpu_mpi_initialize(void)
  1319. {
  1320. #ifdef STARPU_SIMGRID
  1321. STARPU_MPI_ASSERT_MSG(0, "application has to let StarPU initialize MPI");
  1322. return 0;
  1323. #else
  1324. return _starpu_mpi_initialize(NULL, NULL, 0, MPI_COMM_WORLD);
  1325. #endif
  1326. }
  1327. int starpu_mpi_initialize_extended(int *rank, int *world_size)
  1328. {
  1329. #ifdef STARPU_SIMGRID
  1330. *world_size = _mpi_world_size;
  1331. *rank = _mpi_world_rank;
  1332. return 0;
  1333. #else
  1334. int ret;
  1335. ret = _starpu_mpi_initialize(NULL, NULL, 1, MPI_COMM_WORLD);
  1336. if (ret == 0)
  1337. {
  1338. _STARPU_DEBUG("Calling MPI_Comm_rank\n");
  1339. MPI_Comm_rank(MPI_COMM_WORLD, rank);
  1340. MPI_Comm_size(MPI_COMM_WORLD, world_size);
  1341. }
  1342. return ret;
  1343. #endif
  1344. }
  1345. int starpu_mpi_shutdown(void)
  1346. {
  1347. void *value;
  1348. int rank, world_size;
  1349. /* We need to get the rank before calling MPI_Finalize to pass to _starpu_mpi_comm_amounts_display() */
  1350. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  1351. starpu_mpi_comm_size(MPI_COMM_WORLD, &world_size);
  1352. /* kill the progression thread */
  1353. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  1354. running = 0;
  1355. STARPU_PTHREAD_COND_BROADCAST(&cond_progression);
  1356. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  1357. starpu_pthread_join(progress_thread, &value);
  1358. #ifdef STARPU_MPI_ACTIVITY
  1359. starpu_progression_hook_deregister(hookid);
  1360. #endif /* STARPU_MPI_ACTIVITY */
  1361. _STARPU_MPI_TRACE_STOP(rank, world_size);
  1362. /* free the request queues */
  1363. _starpu_mpi_req_list_delete(detached_requests);
  1364. _starpu_mpi_req_list_delete(ready_requests);
  1365. _starpu_mpi_comm_amounts_display(rank);
  1366. _starpu_mpi_comm_amounts_free();
  1367. _starpu_mpi_cache_free(world_size);
  1368. _starpu_mpi_tag_free();
  1369. _starpu_mpi_comm_free();
  1370. return 0;
  1371. }
  1372. void _starpu_mpi_clear_cache(starpu_data_handle_t data_handle)
  1373. {
  1374. _starpu_mpi_data_release_tag(data_handle);
  1375. struct _starpu_mpi_node_tag *mpi_data = data_handle->mpi_data;
  1376. _starpu_mpi_cache_flush(mpi_data->comm, data_handle);
  1377. free(data_handle->mpi_data);
  1378. }
  1379. void starpu_mpi_data_register_comm(starpu_data_handle_t data_handle, int tag, int rank, MPI_Comm comm)
  1380. {
  1381. struct _starpu_mpi_node_tag *mpi_data;
  1382. if (data_handle->mpi_data)
  1383. {
  1384. mpi_data = data_handle->mpi_data;
  1385. }
  1386. else
  1387. {
  1388. mpi_data = calloc(1, sizeof(struct _starpu_mpi_node_tag));
  1389. data_handle->mpi_data = mpi_data;
  1390. _starpu_mpi_data_register_tag(data_handle, tag);
  1391. _starpu_data_set_unregister_hook(data_handle, _starpu_mpi_clear_cache);
  1392. }
  1393. if (tag != -1)
  1394. {
  1395. mpi_data->data_tag = tag;
  1396. }
  1397. if (rank != -1)
  1398. {
  1399. mpi_data->rank = rank;
  1400. mpi_data->comm = comm;
  1401. _starpu_mpi_comm_register(comm);
  1402. }
  1403. }
  1404. void starpu_mpi_data_set_rank_comm(starpu_data_handle_t handle, int rank, MPI_Comm comm)
  1405. {
  1406. starpu_mpi_data_register_comm(handle, -1, rank, comm);
  1407. }
  1408. void starpu_mpi_data_set_tag(starpu_data_handle_t handle, int tag)
  1409. {
  1410. starpu_mpi_data_register_comm(handle, tag, -1, MPI_COMM_WORLD);
  1411. }
  1412. int starpu_mpi_data_get_rank(starpu_data_handle_t data)
  1413. {
  1414. STARPU_ASSERT_MSG(data->mpi_data, "starpu_mpi_data_register MUST be called for data %p\n", data);
  1415. return ((struct _starpu_mpi_node_tag *)(data->mpi_data))->rank;
  1416. }
  1417. int starpu_mpi_data_get_tag(starpu_data_handle_t data)
  1418. {
  1419. STARPU_ASSERT_MSG(data->mpi_data, "starpu_mpi_data_register MUST be called for data %p\n", data);
  1420. return ((struct _starpu_mpi_node_tag *)(data->mpi_data))->data_tag;
  1421. }
  1422. int starpu_mpi_comm_size(MPI_Comm comm, int *size)
  1423. {
  1424. #ifdef STARPU_SIMGRID
  1425. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  1426. *size = _mpi_world_size;
  1427. return 0;
  1428. #else
  1429. return MPI_Comm_size(comm, size);
  1430. #endif
  1431. }
  1432. int starpu_mpi_comm_rank(MPI_Comm comm, int *rank)
  1433. {
  1434. #ifdef STARPU_SIMGRID
  1435. STARPU_MPI_ASSERT_MSG(comm == MPI_COMM_WORLD, "StarPU-SMPI only works with MPI_COMM_WORLD for now");
  1436. *rank = _mpi_world_rank;
  1437. return 0;
  1438. #else
  1439. return MPI_Comm_rank(comm, rank);
  1440. #endif
  1441. }
  1442. int starpu_mpi_world_rank(void)
  1443. {
  1444. int rank;
  1445. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  1446. return rank;
  1447. }