starpu_mpi.c 56 KB

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