starpu_mpi.c 66 KB

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