starpu_mpi.c 67 KB

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