starpu_mpi.c 56 KB

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