starpu_mpi.c 50 KB

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