starpu_mpi.c 62 KB

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