starpu_mpi.c 56 KB

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