starpu_mpi.c 63 KB

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