starpu_mpi_nmad.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2014, 2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 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 <limits.h>
  19. #include <starpu_mpi.h>
  20. #include <starpu_mpi_datatype.h>
  21. #include <starpu_mpi_private.h>
  22. #include <starpu_mpi_cache.h>
  23. #include <starpu_profiling.h>
  24. #include <starpu_mpi_stats.h>
  25. #include <starpu_mpi_cache.h>
  26. #include <starpu_mpi_select_node.h>
  27. #include <starpu_mpi_init.h>
  28. #include <common/config.h>
  29. #include <common/thread.h>
  30. #include <datawizard/coherency.h>
  31. #include <core/task.h>
  32. #include <core/topology.h>
  33. #ifdef STARPU_USE_MPI_NMAD
  34. #include <nm_sendrecv_interface.h>
  35. #include <nm_mpi_nmad.h>
  36. static void _starpu_mpi_handle_request_termination(struct _starpu_mpi_req *req,nm_sr_event_t event);
  37. #ifdef STARPU_VERBOSE
  38. static char *_starpu_mpi_request_type(enum _starpu_mpi_request_type request_type);
  39. #endif
  40. static void _starpu_mpi_handle_new_request(void *arg);
  41. static void _starpu_mpi_handle_pending_request(struct _starpu_mpi_req *req);
  42. static void _starpu_mpi_add_sync_point_in_fxt(void);
  43. static int mpi_thread_cpuid = -1;
  44. int _starpu_mpi_fake_world_size = -1;
  45. int _starpu_mpi_fake_world_rank = -1;
  46. /* Condition to wake up waiting for all current MPI requests to finish */
  47. static starpu_pthread_t progress_thread;
  48. static starpu_pthread_cond_t progress_cond;
  49. static starpu_pthread_mutex_t progress_mutex;
  50. static volatile int running = 0;
  51. extern struct _starpu_mpi_req *_starpu_mpi_irecv_common(starpu_data_handle_t data_handle, int source, int data_tag, MPI_Comm comm, unsigned detached, unsigned sync, void (*callback)(void *), void *arg, int sequential_consistency, int is_internal_req, starpu_ssize_t count);
  52. /* Count requests posted by the application and not yet submitted to MPI, i.e pushed into the new_requests list */
  53. static volatile int pending_request = 0;
  54. #define REQ_FINALIZED 0x1
  55. PUK_LFSTACK_TYPE(callback, struct _starpu_mpi_req *req;);
  56. static callback_lfstack_t callback_stack = NULL;
  57. static starpu_sem_t callback_sem;
  58. void _starpu_mpi_request_init(struct _starpu_mpi_req **req)
  59. {
  60. _STARPU_MPI_CALLOC(*req, 1, sizeof(struct _starpu_mpi_req));
  61. /* Initialize the request structure */
  62. (*req)->data_handle = NULL;
  63. (*req)->prio = 0;
  64. (*req)->completed = 0;
  65. (*req)->datatype = 0;
  66. (*req)->datatype_name = NULL;
  67. (*req)->ptr = NULL;
  68. (*req)->count = -1;
  69. (*req)->registered_datatype = -1;
  70. (*req)->node_tag.rank = -1;
  71. (*req)->node_tag.data_tag = -1;
  72. (*req)->node_tag.comm = 0;
  73. (*req)->func = NULL;
  74. (*req)->status = NULL;
  75. // (*req)->data_request = 0;
  76. (*req)->flag = NULL;
  77. (*req)->ret = -1;
  78. piom_cond_init(&((*req)->req_cond), 0);
  79. //STARPU_PTHREAD_MUTEX_INIT(&((*req)->req_mutex), NULL);
  80. //STARPU_PTHREAD_COND_INIT(&((*req)->req_cond), NULL);
  81. // STARPU_PTHREAD_MUTEX_INIT(&((*req)->posted_mutex), NULL);
  82. //STARPU_PTHREAD_COND_INIT(&((*req)->posted_cond), NULL);
  83. (*req)->request_type = UNKNOWN_REQ;
  84. (*req)->submitted = 0;
  85. (*req)->completed = 0;
  86. (*req)->posted = 0;
  87. //(*req)->other_request = NULL;
  88. (*req)->sync = 0;
  89. (*req)->detached = -1;
  90. (*req)->callback = NULL;
  91. (*req)->callback_arg = NULL;
  92. // (*req)->size_req = 0;
  93. //(*req)->internal_req = NULL;
  94. //(*req)->is_internal_req = 0;
  95. //(*req)->to_destroy = 1;
  96. //(*req)->early_data_handle = NULL;
  97. //(*req)->envelope = NULL;
  98. (*req)->sequential_consistency = 1;
  99. (*req)->pre_sync_jobid = -1;
  100. (*req)->post_sync_jobid = -1;
  101. #ifdef STARPU_SIMGRID
  102. starpu_pthread_queue_init(&((*req)->queue));
  103. starpu_pthread_queue_register(&wait, &((*req)->queue));
  104. (*req)->done = 0;
  105. #endif
  106. }
  107. void _starpu_mpi_request_destroy(struct _starpu_mpi_req *req)
  108. {
  109. piom_cond_destroy(&(req->req_cond));
  110. free(req);
  111. }
  112. /********************************************************/
  113. /* */
  114. /* Send/Receive functionalities */
  115. /* */
  116. /********************************************************/
  117. static void nop_acquire_cb(void *arg)
  118. {
  119. starpu_data_release(arg);
  120. }
  121. struct _starpu_mpi_req *_starpu_mpi_isend_irecv_common(starpu_data_handle_t data_handle,
  122. int srcdst, int data_tag, MPI_Comm comm,
  123. unsigned detached, unsigned sync, int prio, void (*callback)(void *), void *arg,
  124. enum _starpu_mpi_request_type request_type, void (*func)(struct _starpu_mpi_req *),
  125. enum starpu_data_access_mode mode,
  126. int sequential_consistency,
  127. int is_internal_req,
  128. starpu_ssize_t count)
  129. {
  130. struct _starpu_mpi_req *req;
  131. if (_starpu_mpi_fake_world_size != -1)
  132. {
  133. /* Don't actually do the communication */
  134. starpu_data_acquire_on_node_cb_sequential_consistency(data_handle, STARPU_MAIN_RAM, mode, nop_acquire_cb, data_handle, sequential_consistency);
  135. return NULL;
  136. }
  137. _STARPU_MPI_LOG_IN();
  138. STARPU_ATOMIC_ADD( &pending_request, 1);
  139. /* Initialize the request structure */
  140. _starpu_mpi_request_init(&req);
  141. req->request_type = request_type;
  142. /* prio_list is sorted by increasing values */
  143. req->prio = prio;
  144. req->data_handle = data_handle;
  145. req->node_tag.rank = srcdst;
  146. req->node_tag.data_tag = data_tag;
  147. req->node_tag.comm = comm;
  148. req->detached = detached;
  149. req->sync = sync;
  150. req->callback = callback;
  151. req->callback_arg = arg;
  152. req->func = func;
  153. req->sequential_consistency = sequential_consistency;
  154. nm_mpi_nmad_dest(&req->session, &req->gate, comm, req->node_tag.rank);
  155. /* Asynchronously request StarPU to fetch the data in main memory: when
  156. * it is available in main memory, _starpu_mpi_submit_new_mpi_request(req) is called and
  157. * the request is actually submitted */
  158. starpu_data_acquire_on_node_cb_sequential_consistency_sync_jobids(data_handle, STARPU_MAIN_RAM, mode, _starpu_mpi_handle_new_request, (void *)req, sequential_consistency, &req->pre_sync_jobid, &req->post_sync_jobid);
  159. _STARPU_MPI_LOG_OUT();
  160. return req;
  161. }
  162. /********************************************************/
  163. /* */
  164. /* Send functionalities */
  165. /* */
  166. /********************************************************/
  167. static void _starpu_mpi_isend_data_func(struct _starpu_mpi_req *req)
  168. {
  169. _STARPU_MPI_LOG_IN();
  170. _STARPU_MPI_DEBUG(30, "post NM isend request %p type %s tag %d src %d data %p datasize %ld ptr %p datatype '%s' count %d registered_datatype %d sync %d\n", req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, starpu_data_get_size(req->data_handle), req->ptr, req->datatype_name, (int)req->count, req->registered_datatype, req->sync);
  171. _starpu_mpi_comm_amounts_inc(req->node_tag.comm, req->node_tag.rank, req->datatype, req->count);
  172. _STARPU_MPI_TRACE_ISEND_SUBMIT_BEGIN(req->node_tag.rank, req->node_tag.data_tag, 0);
  173. struct nm_data_s data;
  174. nm_mpi_nmad_data_get(&data, (void*)req->ptr, req->datatype, req->count);
  175. nm_sr_send_init(req->session, &(req->data_request));
  176. nm_sr_send_pack_data(req->session, &(req->data_request), &data);
  177. nm_sr_send_set_priority(req->session, &req->data_request, req->prio);
  178. if (req->sync == 0)
  179. {
  180. req->ret = nm_sr_send_isend(req->session, &(req->data_request), req->gate, req->node_tag.data_tag);
  181. STARPU_ASSERT_MSG(req->ret == NM_ESUCCESS, "MPI_Isend returning %d", req->ret);
  182. }
  183. else
  184. {
  185. req->ret = nm_sr_send_issend(req->session, &(req->data_request), req->gate, req->node_tag.data_tag);
  186. STARPU_ASSERT_MSG(req->ret == NM_ESUCCESS, "MPI_Issend returning %d", req->ret);
  187. }
  188. _STARPU_MPI_TRACE_ISEND_SUBMIT_END(req->node_tag.rank, req->node_tag.data_tag, starpu_data_get_size(req->data_handle), req->pre_sync_jobid);
  189. _starpu_mpi_handle_pending_request(req);
  190. _STARPU_MPI_LOG_OUT();
  191. }
  192. void _starpu_mpi_isend_size_func(struct _starpu_mpi_req *req)
  193. {
  194. _starpu_mpi_datatype_allocate(req->data_handle, req);
  195. if (req->registered_datatype == 1)
  196. {
  197. req->waited = 1;
  198. req->count = 1;
  199. req->ptr = starpu_data_handle_to_pointer(req->data_handle, STARPU_MAIN_RAM);
  200. }
  201. else
  202. {
  203. starpu_ssize_t psize = -1;
  204. int ret;
  205. req->waited =2;
  206. // Do not pack the data, just try to find out the size
  207. starpu_data_pack(req->data_handle, NULL, &psize);
  208. if (psize != -1)
  209. {
  210. // We already know the size of the data, let's send it to overlap with the packing of the data
  211. _STARPU_MPI_DEBUG(20, "Sending size %ld (%ld %s) to node %d (first call to pack)\n", psize, sizeof(req->count), "MPI_BYTE", req->node_tag.rank);
  212. req->count = psize;
  213. //ret = nm_sr_isend(nm_mpi_communicator_get_session(p_req->p_comm),nm_mpi_communicator_get_gate(p_comm,req->srcdst), req->mpi_tag,&req->count, sizeof(req->count), &req->size_req);
  214. ret = nm_sr_isend(req->session,req->gate, req->node_tag.data_tag,&req->count, sizeof(req->count), &req->size_req);
  215. // ret = MPI_Isend(&req->count, sizeof(req->count), MPI_BYTE, req->srcdst, req->mpi_tag, req->comm, &req->size_req);
  216. STARPU_ASSERT_MSG(ret == NM_ESUCCESS, "when sending size, nm_sr_isend returning %d", ret);
  217. }
  218. // Pack the data
  219. starpu_data_pack(req->data_handle, &req->ptr, &req->count);
  220. if (psize == -1)
  221. {
  222. // We know the size now, let's send it
  223. _STARPU_MPI_DEBUG(1, "Sending size %ld (%ld %s) with tag %d to node %d (second call to pack)\n", req->count, sizeof(req->count), "MPI_BYTE", req->node_tag.data_tag, req->node_tag.rank);
  224. ret = nm_sr_isend(req->session,req->gate, req->node_tag.data_tag,&req->count, sizeof(req->count), &req->size_req);
  225. STARPU_ASSERT_MSG(ret == NM_ESUCCESS, "when sending size, nm_sr_isend returning %d", ret);
  226. }
  227. else
  228. {
  229. // We check the size returned with the 2 calls to pack is the same
  230. STARPU_ASSERT_MSG(req->count == psize, "Calls to pack_data returned different sizes %ld != %ld", req->count, psize);
  231. }
  232. // We can send the data now
  233. }
  234. _starpu_mpi_isend_data_func(req);
  235. }
  236. /********************************************************/
  237. /* */
  238. /* Receive functionalities */
  239. /* */
  240. /********************************************************/
  241. static void _starpu_mpi_irecv_data_func(struct _starpu_mpi_req *req)
  242. {
  243. _STARPU_MPI_LOG_IN();
  244. _STARPU_MPI_DEBUG(20, "post NM irecv request %p type %s tag %d src %d data %p ptr %p datatype '%s' count %d registered_datatype %d \n", req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, req->ptr, req->datatype_name, (int)req->count, req->registered_datatype);
  245. _STARPU_MPI_TRACE_IRECV_SUBMIT_BEGIN(req->node_tag.rank, req->node_tag.data_tag);
  246. //req->ret = MPI_Irecv(req->ptr, req->count, req->datatype, req->srcdst, req->mpi_tag, req->comm, &req->request);
  247. struct nm_data_s data;
  248. nm_mpi_nmad_data_get(&data, (void*)req->ptr, req->datatype, req->count);
  249. nm_sr_recv_init(req->session, &(req->data_request));
  250. nm_sr_recv_unpack_data(req->session, &(req->data_request), &data);
  251. nm_sr_recv_irecv(req->session, &(req->data_request), req->gate, req->node_tag.data_tag, NM_TAG_MASK_FULL);
  252. _STARPU_MPI_TRACE_IRECV_SUBMIT_END(req->node_tag.rank, req->node_tag.data_tag);
  253. _starpu_mpi_handle_pending_request(req);
  254. _STARPU_MPI_LOG_OUT();
  255. }
  256. struct _starpu_mpi_irecv_size_callback
  257. {
  258. starpu_data_handle_t handle;
  259. struct _starpu_mpi_req *req;
  260. };
  261. static void _starpu_mpi_irecv_size_callback(void *arg)
  262. {
  263. struct _starpu_mpi_irecv_size_callback *callback = (struct _starpu_mpi_irecv_size_callback *)arg;
  264. starpu_data_unregister(callback->handle);
  265. callback->req->ptr = malloc(callback->req->count);
  266. STARPU_ASSERT_MSG(callback->req->ptr, "cannot allocate message of size %ld", callback->req->count);
  267. _starpu_mpi_irecv_data_func(callback->req);
  268. free(callback);
  269. }
  270. void _starpu_mpi_irecv_size_func(struct _starpu_mpi_req *req)
  271. {
  272. _STARPU_MPI_LOG_IN();
  273. _starpu_mpi_datatype_allocate(req->data_handle, req);
  274. if (req->registered_datatype == 1)
  275. {
  276. req->count = 1;
  277. req->ptr = starpu_data_handle_to_pointer(req->data_handle, STARPU_MAIN_RAM);
  278. _starpu_mpi_irecv_data_func(req);
  279. }
  280. else
  281. {
  282. struct _starpu_mpi_irecv_size_callback *callback = malloc(sizeof(struct _starpu_mpi_irecv_size_callback));
  283. callback->req = req;
  284. starpu_variable_data_register(&callback->handle, 0, (uintptr_t)&(callback->req->count), sizeof(callback->req->count));
  285. _STARPU_MPI_DEBUG(4, "Receiving size with tag %d from node %d\n", req->node_tag.data_tag, req->node_tag.rank);
  286. _starpu_mpi_irecv_common(callback->handle, req->node_tag.rank, req->node_tag.data_tag, req->node_tag.comm, 1, 0, _starpu_mpi_irecv_size_callback, callback,1,0,0);
  287. }
  288. }
  289. /********************************************************/
  290. /* */
  291. /* Wait functionalities */
  292. /* */
  293. /********************************************************/
  294. #define _starpu_mpi_req_status(PUBLIC_REQ,STATUS) do { \
  295. STATUS->MPI_SOURCE=PUBLIC_REQ->node_tag.rank; /**< field name mandatory by spec */ \
  296. STATUS->MPI_TAG=PUBLIC_REQ->node_tag.data_tag; /**< field name mandatory by spec */ \
  297. STATUS->MPI_ERROR=PUBLIC_REQ->ret; /**< field name mandatory by spec */ \
  298. STATUS->size=PUBLIC_REQ->count; /**< size of data received */ \
  299. STATUS->cancelled=0; /**< whether request was cancelled */ \
  300. } while(0)
  301. int _starpu_mpi_wait(starpu_mpi_req *public_req, MPI_Status *status)
  302. {
  303. _STARPU_MPI_LOG_IN();
  304. STARPU_MPI_ASSERT_MSG(public_req, "starpu_mpi_wait needs a valid starpu_mpi_req");
  305. struct _starpu_mpi_req *req = *public_req;
  306. STARPU_MPI_ASSERT_MSG(!req->detached, "MPI_Wait cannot be called on a detached request");
  307. /* we must do a test_locked to avoid race condition :
  308. * without req_cond could still be used and couldn't be freed)*/
  309. while (!req->completed || ! piom_cond_test_locked(&(req->req_cond),REQ_FINALIZED))
  310. {
  311. piom_cond_wait(&(req->req_cond),REQ_FINALIZED);
  312. }
  313. if (status!=MPI_STATUS_IGNORE)
  314. _starpu_mpi_req_status(req,status);
  315. _starpu_mpi_request_destroy(req);
  316. *public_req = NULL;
  317. _STARPU_MPI_LOG_OUT();
  318. return MPI_SUCCESS;
  319. }
  320. /********************************************************/
  321. /* */
  322. /* Test functionalities */
  323. /* */
  324. /********************************************************/
  325. int _starpu_mpi_test(starpu_mpi_req *public_req, int *flag, MPI_Status *status)
  326. {
  327. _STARPU_MPI_LOG_IN();
  328. STARPU_MPI_ASSERT_MSG(public_req, "starpu_mpi_test needs a valid starpu_mpi_req");
  329. struct _starpu_mpi_req *req = *public_req;
  330. STARPU_MPI_ASSERT_MSG(!req->detached, "MPI_Test cannot be called on a detached request");
  331. _STARPU_MPI_DEBUG(2, "Test request %p type %s tag %d src %d data %p ptr %p datatype '%s' count %d registered_datatype %d \n",
  332. req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, req->ptr, req->datatype_name, (int)req->count, req->registered_datatype);
  333. _STARPU_MPI_TRACE_UTESTING_BEGIN(req->node_tag.rank, req->node_tag.data_tag);
  334. /* we must do a test_locked to avoid race condition :
  335. * without req_cond could still be used and couldn't be freed)*/
  336. *flag = req->completed && piom_cond_test_locked(&(req->req_cond),REQ_FINALIZED);
  337. if (*flag && status!=MPI_STATUS_IGNORE)
  338. _starpu_mpi_req_status(req,status);
  339. _STARPU_MPI_TRACE_UTESTING_END(req->node_tag.rank, req->node_tag.data_tag);
  340. if(*flag)
  341. {
  342. _starpu_mpi_request_destroy(req);
  343. *public_req = NULL;
  344. }
  345. _STARPU_MPI_LOG_OUT();
  346. return MPI_SUCCESS;
  347. }
  348. /********************************************************/
  349. /* */
  350. /* Barrier functionalities */
  351. /* */
  352. /********************************************************/
  353. int _starpu_mpi_barrier(MPI_Comm comm)
  354. {
  355. _STARPU_MPI_LOG_IN();
  356. int ret;
  357. // STARPU_ASSERT_MSG(!barrier_running, "Concurrent starpu_mpi_barrier is not implemented, even on different communicators");
  358. ret = MPI_Barrier(comm);
  359. STARPU_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Barrier returning %d", ret);
  360. _STARPU_MPI_LOG_OUT();
  361. return ret;
  362. }
  363. /********************************************************/
  364. /* */
  365. /* Progression */
  366. /* */
  367. /********************************************************/
  368. #ifdef STARPU_VERBOSE
  369. static char *_starpu_mpi_request_type(enum _starpu_mpi_request_type request_type)
  370. {
  371. switch (request_type)
  372. {
  373. case SEND_REQ: return "SEND_REQ";
  374. case RECV_REQ: return "RECV_REQ";
  375. case WAIT_REQ: return "WAIT_REQ";
  376. case TEST_REQ: return "TEST_REQ";
  377. case BARRIER_REQ: return "BARRIER_REQ";
  378. default: return "unknown request type";
  379. }
  380. }
  381. #endif
  382. static void _starpu_mpi_handle_request_termination(struct _starpu_mpi_req *req,nm_sr_event_t event)
  383. {
  384. _STARPU_MPI_LOG_IN();
  385. _STARPU_MPI_DEBUG(2, "complete MPI request %p type %s tag %d src %d data %p ptr %p datatype '%s' count %d registered_datatype %d \n",
  386. req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, req->ptr, req->datatype_name, (int)req->count, req->registered_datatype);
  387. if (req->request_type == RECV_REQ || req->request_type == SEND_REQ)
  388. {
  389. if (req->registered_datatype == 0)
  390. {
  391. if(req->waited == 1)
  392. nm_mpi_nmad_data_release(req->datatype);
  393. if (req->request_type == SEND_REQ)
  394. {
  395. req->waited--;
  396. // We need to make sure the communication for sending the size
  397. // has completed, as MPI can re-order messages, let's count
  398. // recerived message.
  399. // FIXME concurent access.
  400. STARPU_ASSERT_MSG(event == NM_SR_EVENT_FINALIZED, "Callback with event %d", event);
  401. if(req->waited>0)
  402. return;
  403. }
  404. if (req->request_type == RECV_REQ)
  405. // req->ptr is freed by starpu_data_unpack
  406. starpu_data_unpack(req->data_handle, req->ptr, req->count);
  407. else
  408. free(req->ptr);
  409. }
  410. else
  411. {
  412. nm_mpi_nmad_data_release(req->datatype);
  413. _starpu_mpi_datatype_free(req->data_handle, &req->datatype);
  414. }
  415. starpu_data_release(req->data_handle);
  416. }
  417. /* Execute the specified callback, if any */
  418. if (req->callback)
  419. {
  420. struct callback_lfstack_cell_s* c = padico_malloc(sizeof(struct callback_lfstack_cell_s));
  421. c->req = req;
  422. /* The main thread can exit without waiting
  423. * the end of the detached request. Callback thread
  424. * must then be kept alive if they have a callback.*/
  425. callback_lfstack_push(&callback_stack, c);
  426. starpu_sem_post(&callback_sem);
  427. }
  428. else
  429. {
  430. if(req->detached)
  431. {
  432. _starpu_mpi_request_destroy(req);
  433. // a detached request wont be wait/test (and freed inside).
  434. }
  435. else
  436. {
  437. /* tell anyone potentially waiting on the request that it is
  438. * terminated now (should be done after the callback)*/
  439. req->completed = 1;
  440. piom_cond_signal(&req->req_cond, REQ_FINALIZED);
  441. }
  442. int pending_remaining = STARPU_ATOMIC_ADD(&pending_request, -1);
  443. if (!running && !pending_remaining)
  444. starpu_sem_post(&callback_sem);
  445. }
  446. _STARPU_MPI_LOG_OUT();
  447. }
  448. void _starpu_mpi_handle_request_termination_callback(nm_sr_event_t event, const nm_sr_event_info_t*event_info, void*ref)
  449. {
  450. _starpu_mpi_handle_request_termination(ref,event);
  451. }
  452. static void _starpu_mpi_handle_pending_request(struct _starpu_mpi_req *req)
  453. {
  454. if(req->request_type == SEND_REQ && req->waited>1)
  455. {
  456. nm_sr_request_set_ref(&(req->size_req), req);
  457. nm_sr_request_monitor(req->session, &(req->size_req), NM_SR_EVENT_FINALIZED,_starpu_mpi_handle_request_termination_callback);
  458. }
  459. /* the if must be before, because the first callback can directly free
  460. * a detached request (the second callback free if req->waited>1). */
  461. nm_sr_request_set_ref(&(req->data_request), req);
  462. nm_sr_request_monitor(req->session, &(req->data_request), NM_SR_EVENT_FINALIZED,_starpu_mpi_handle_request_termination_callback);
  463. }
  464. static void _starpu_mpi_handle_new_request(void *arg)
  465. {
  466. _STARPU_MPI_LOG_IN();
  467. struct _starpu_mpi_req *req = arg;
  468. STARPU_ASSERT_MSG(req, "Invalid request");
  469. /* submit the request to MPI */
  470. _STARPU_MPI_DEBUG(2, "Handling new request %p type %s tag %d src %d data %p ptr %p datatype '%s' count %d registered_datatype %d \n",
  471. req, _starpu_mpi_request_type(req->request_type), req->node_tag.data_tag, req->node_tag.rank, req->data_handle, req->ptr, req->datatype_name, (int)req->count, req->registered_datatype);
  472. req->func(req);
  473. _STARPU_MPI_LOG_OUT();
  474. }
  475. static void *_starpu_mpi_progress_thread_func(void *arg)
  476. {
  477. struct _starpu_mpi_argc_argv *argc_argv = (struct _starpu_mpi_argc_argv *) arg;
  478. starpu_pthread_setname("MPI");
  479. #ifndef STARPU_SIMGRID
  480. if (mpi_thread_cpuid >= 0)
  481. _starpu_bind_thread_on_cpu(mpi_thread_cpuid, STARPU_NOWORKERID);
  482. _starpu_mpi_do_initialize(argc_argv);
  483. if (mpi_thread_cpuid >= 0)
  484. /* In case MPI changed the binding */
  485. _starpu_bind_thread_on_cpu(mpi_thread_cpuid, STARPU_NOWORKERID);
  486. #endif
  487. _starpu_mpi_fake_world_size = starpu_get_env_number("STARPU_MPI_FAKE_SIZE");
  488. _starpu_mpi_fake_world_rank = starpu_get_env_number("STARPU_MPI_FAKE_RANK");
  489. #ifdef STARPU_SIMGRID
  490. /* Now that MPI is set up, let the rest of simgrid get initialized */
  491. char **argv_cpy;
  492. _STARPU_MPI_MALLOC(argv_cpy, *(argc_argv->argc) * sizeof(char*));
  493. int i;
  494. for (i = 0; i < *(argc_argv->argc); i++)
  495. argv_cpy[i] = strdup((*(argc_argv->argv))[i]);
  496. MSG_process_create_with_arguments("main", smpi_simulated_main_, NULL, _starpu_simgrid_get_host_by_name("MAIN"), *(argc_argv->argc), argv_cpy);
  497. /* And set TSD for us */
  498. void **tsd;
  499. _STARPU_CALLOC(tsd, MAX_TSD + 1, sizeof(void*));
  500. if (!smpi_process_set_user_data)
  501. {
  502. _STARPU_ERROR("Your version of simgrid does not provide smpi_process_set_user_data, we can not continue without it\n");
  503. }
  504. smpi_process_set_user_data(tsd);
  505. #endif
  506. #ifdef STARPU_USE_FXT
  507. _starpu_fxt_wait_initialisation();
  508. #endif //STARPU_USE_FXT
  509. {
  510. _STARPU_MPI_TRACE_START(argc_argv->rank, argc_argv->world_size);
  511. #ifdef STARPU_USE_FXT
  512. starpu_profiling_set_id(argc_argv->rank);
  513. #endif //STARPU_USE_FXT
  514. }
  515. _starpu_mpi_add_sync_point_in_fxt();
  516. _starpu_mpi_comm_amounts_init(argc_argv->comm);
  517. _starpu_mpi_cache_init(argc_argv->comm);
  518. _starpu_mpi_select_node_init();
  519. _starpu_mpi_datatype_init();
  520. /* notify the main thread that the progression thread is ready */
  521. STARPU_PTHREAD_MUTEX_LOCK(&progress_mutex);
  522. running = 1;
  523. STARPU_PTHREAD_COND_SIGNAL(&progress_cond);
  524. STARPU_PTHREAD_MUTEX_UNLOCK(&progress_mutex);
  525. while (1)
  526. {
  527. struct callback_lfstack_cell_s* c = callback_lfstack_pop(&callback_stack);
  528. int err=0;
  529. if(running || pending_request>0)
  530. {/* shall we block ? */
  531. err = starpu_sem_wait(&callback_sem);
  532. //running pending_request can change while waiting
  533. }
  534. if(c==NULL)
  535. {
  536. c = callback_lfstack_pop(&callback_stack);
  537. if (c == NULL)
  538. {
  539. if(running && pending_request>0)
  540. {
  541. STARPU_ASSERT_MSG(c!=NULL, "Callback thread awakened without callback ready with error %d.",err);
  542. }
  543. else
  544. {
  545. if (pending_request==0)
  546. break;
  547. }
  548. continue;
  549. }
  550. }
  551. c->req->callback(c->req->callback_arg);
  552. if (c->req->detached)
  553. {
  554. _starpu_mpi_request_destroy(c->req);
  555. }
  556. else
  557. {
  558. c->req->completed=1;
  559. piom_cond_signal(&(c->req->req_cond), REQ_FINALIZED);
  560. }
  561. STARPU_ATOMIC_ADD( &pending_request, -1);
  562. /* we signal that the request is completed.*/
  563. free(c);
  564. }
  565. STARPU_ASSERT_MSG(callback_lfstack_pop(&callback_stack)==NULL, "List of callback not empty.");
  566. STARPU_ASSERT_MSG(pending_request==0, "Request still pending.");
  567. if (argc_argv->initialize_mpi)
  568. {
  569. _STARPU_MPI_DEBUG(3, "Calling MPI_Finalize()\n");
  570. MPI_Finalize();
  571. }
  572. starpu_sem_destroy(&callback_sem);
  573. free(argc_argv);
  574. return NULL;
  575. }
  576. /********************************************************/
  577. /* */
  578. /* (De)Initialization methods */
  579. /* */
  580. /********************************************************/
  581. // #ifdef STARPU_MPI_ACTIVITY
  582. // static int hookid = - 1;
  583. // #endif /* STARPU_MPI_ACTIVITY */
  584. static void _starpu_mpi_add_sync_point_in_fxt(void)
  585. {
  586. #ifdef STARPU_USE_FXT
  587. int rank;
  588. int worldsize;
  589. int ret;
  590. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  591. starpu_mpi_comm_size(MPI_COMM_WORLD, &worldsize);
  592. ret = MPI_Barrier(MPI_COMM_WORLD);
  593. STARPU_MPI_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Barrier returning %s", _starpu_mpi_get_mpi_error_code(ret));
  594. /* We generate a "unique" key so that we can make sure that different
  595. * FxT traces come from the same MPI run. */
  596. int random_number;
  597. /* XXX perhaps we don't want to generate a new seed if the application
  598. * specified some reproductible behaviour ? */
  599. if (rank == 0)
  600. {
  601. srand(time(NULL));
  602. random_number = rand();
  603. }
  604. ret = MPI_Bcast(&random_number, 1, MPI_INT, 0, MPI_COMM_WORLD);
  605. STARPU_MPI_ASSERT_MSG(ret == MPI_SUCCESS, "MPI_Bcast returning %s", _starpu_mpi_get_mpi_error_code(ret));
  606. _STARPU_MPI_TRACE_BARRIER(rank, worldsize, random_number);
  607. _STARPU_MPI_DEBUG(3, "unique key %x\n", random_number);
  608. #endif
  609. }
  610. int _starpu_mpi_progress_init(struct _starpu_mpi_argc_argv *argc_argv)
  611. {
  612. STARPU_PTHREAD_MUTEX_INIT(&progress_mutex, NULL);
  613. STARPU_PTHREAD_COND_INIT(&progress_cond, NULL);
  614. starpu_sem_init(&callback_sem, 0, 0);
  615. running = 0;
  616. mpi_thread_cpuid = starpu_get_env_number_default("STARPU_MPI_THREAD_CPUID", -1);
  617. STARPU_PTHREAD_CREATE(&progress_thread, NULL, _starpu_mpi_progress_thread_func, argc_argv);
  618. STARPU_PTHREAD_MUTEX_LOCK(&progress_mutex);
  619. while (!running)
  620. STARPU_PTHREAD_COND_WAIT(&progress_cond, &progress_mutex);
  621. STARPU_PTHREAD_MUTEX_UNLOCK(&progress_mutex);
  622. return 0;
  623. }
  624. void _starpu_mpi_progress_shutdown(int *value)
  625. {
  626. /* kill the progression thread */
  627. STARPU_PTHREAD_MUTEX_LOCK(&progress_mutex);
  628. running = 0;
  629. STARPU_PTHREAD_COND_BROADCAST(&progress_cond);
  630. STARPU_PTHREAD_MUTEX_UNLOCK(&progress_mutex);
  631. starpu_sem_post(&callback_sem);
  632. starpu_pthread_join(progress_thread, &value);
  633. STARPU_PTHREAD_MUTEX_DESTROY(&progress_mutex);
  634. STARPU_PTHREAD_COND_DESTROY(&progress_cond);
  635. }
  636. #endif /* STARPU_USE_MPI_NMAD*/