starpu_mpi.c 45 KB

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