starpu_mpi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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. //#define STARPU_MPI_VERBOSE 1
  21. #include <starpu_mpi_private.h>
  22. /* TODO find a better way to select the polling method (perhaps during the
  23. * configuration) */
  24. //#define USE_STARPU_ACTIVITY 1
  25. static void submit_mpi_req(void *arg);
  26. static void handle_request_termination(struct starpu_mpi_req_s *req);
  27. /* The list of requests that have been newly submitted by the application */
  28. static starpu_mpi_req_list_t new_requests;
  29. /* The list of detached requests that have already been submitted to MPI */
  30. static starpu_mpi_req_list_t detached_requests;
  31. static pthread_mutex_t detached_requests_mutex;
  32. static pthread_cond_t cond;
  33. static pthread_mutex_t mutex;
  34. static pthread_t progress_thread;
  35. static int running = 0;
  36. /* Count requests posted by the application and not yet submitted to MPI, i.e pushed into the new_requests list */
  37. static pthread_mutex_t mutex_posted_requests;
  38. static int posted_requests = 0;
  39. #define INC_POSTED_REQUESTS(value) { PTHREAD_MUTEX_LOCK(&mutex_posted_requests); posted_requests += value; PTHREAD_MUTEX_UNLOCK(&mutex_posted_requests); }
  40. #if 0
  41. void starpu_mpi_debug(FILE *stream, const char *format, ...) {
  42. int rank;
  43. va_list args;
  44. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  45. fprintf(stream, "[%d] ", rank);
  46. va_start(args, format);
  47. vfprintf(stream, format, args);
  48. va_end(args);
  49. fflush(stream);
  50. }
  51. #endif
  52. /*
  53. * Isend
  54. */
  55. static void starpu_mpi_isend_func(struct starpu_mpi_req_s *req)
  56. {
  57. _STARPU_MPI_LOG_IN();
  58. void *ptr = starpu_mpi_handle_to_ptr(req->data_handle);
  59. _STARPU_MPI_DEBUG("post MPI isend tag %x dst %d ptr %p req %p\n", req->mpi_tag, req->srcdst, ptr, &req->request);
  60. starpu_mpi_handle_to_datatype(req->data_handle, &req->datatype);
  61. req->ret = MPI_Isend(ptr, 1, req->datatype, req->srcdst, req->mpi_tag, req->comm, &req->request);
  62. STARPU_ASSERT(req->ret == MPI_SUCCESS);
  63. TRACE_MPI_ISEND(req->srcdst, req->mpi_tag, 0);
  64. /* somebody is perhaps waiting for the MPI request to be posted */
  65. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  66. req->submitted = 1;
  67. PTHREAD_COND_BROADCAST(&req->req_cond);
  68. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  69. _STARPU_MPI_LOG_OUT();
  70. }
  71. static struct starpu_mpi_req_s *_starpu_mpi_isend_common(starpu_data_handle data_handle,
  72. int dest, int mpi_tag, MPI_Comm comm,
  73. unsigned detached, void (*callback)(void *), void *arg)
  74. {
  75. struct starpu_mpi_req_s *req = calloc(1, sizeof(struct starpu_mpi_req_s));
  76. STARPU_ASSERT(req);
  77. _STARPU_MPI_LOG_IN();
  78. INC_POSTED_REQUESTS(1);
  79. /* Initialize the request structure */
  80. req->submitted = 0;
  81. req->completed = 0;
  82. PTHREAD_MUTEX_INIT(&req->req_mutex, NULL);
  83. PTHREAD_COND_INIT(&req->req_cond, NULL);
  84. req->request_type = SEND_REQ;
  85. req->data_handle = data_handle;
  86. req->srcdst = dest;
  87. req->mpi_tag = mpi_tag;
  88. req->comm = comm;
  89. req->func = starpu_mpi_isend_func;
  90. req->detached = detached;
  91. req->callback = callback;
  92. req->callback_arg = arg;
  93. /* Asynchronously request StarPU to fetch the data in main memory: when
  94. * it is available in main memory, submit_mpi_req(req) is called and
  95. * the request is actually submitted */
  96. starpu_data_acquire_cb(data_handle, STARPU_R, submit_mpi_req, (void *)req);
  97. _STARPU_MPI_LOG_OUT();
  98. return req;
  99. }
  100. int starpu_mpi_isend(starpu_data_handle data_handle, starpu_mpi_req *public_req, int dest, int mpi_tag, MPI_Comm comm)
  101. {
  102. _STARPU_MPI_LOG_IN();
  103. STARPU_ASSERT(public_req);
  104. struct starpu_mpi_req_s *req;
  105. req = _starpu_mpi_isend_common(data_handle, dest, mpi_tag, comm, 0, NULL, NULL);
  106. STARPU_ASSERT(req);
  107. *public_req = req;
  108. _STARPU_MPI_LOG_OUT();
  109. return 0;
  110. }
  111. /*
  112. * Isend (detached)
  113. */
  114. int starpu_mpi_isend_detached(starpu_data_handle data_handle,
  115. int dest, int mpi_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  116. {
  117. _STARPU_MPI_LOG_IN();
  118. _starpu_mpi_isend_common(data_handle, dest, mpi_tag, comm, 1, callback, arg);
  119. _STARPU_MPI_LOG_OUT();
  120. return 0;
  121. }
  122. /*
  123. * Irecv
  124. */
  125. static void starpu_mpi_irecv_func(struct starpu_mpi_req_s *req)
  126. {
  127. _STARPU_MPI_LOG_IN();
  128. void *ptr = starpu_mpi_handle_to_ptr(req->data_handle);
  129. STARPU_ASSERT(ptr);
  130. starpu_mpi_handle_to_datatype(req->data_handle, &req->datatype);
  131. _STARPU_MPI_DEBUG("post MPI irecv tag %x src %d ptr %p req %p datatype %d\n", req->mpi_tag, req->srcdst, ptr, &req->request, req->datatype);
  132. req->ret = MPI_Irecv(ptr, 1, req->datatype, req->srcdst, req->mpi_tag, req->comm, &req->request);
  133. STARPU_ASSERT(req->ret == MPI_SUCCESS);
  134. /* somebody is perhaps waiting for the MPI request to be posted */
  135. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  136. req->submitted = 1;
  137. PTHREAD_COND_BROADCAST(&req->req_cond);
  138. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  139. _STARPU_MPI_LOG_OUT();
  140. }
  141. static struct starpu_mpi_req_s *_starpu_mpi_irecv_common(starpu_data_handle data_handle, int source, int mpi_tag, MPI_Comm comm, unsigned detached, void (*callback)(void *), void *arg)
  142. {
  143. _STARPU_MPI_LOG_IN();
  144. struct starpu_mpi_req_s *req = calloc(1, sizeof(struct starpu_mpi_req_s));
  145. STARPU_ASSERT(req);
  146. INC_POSTED_REQUESTS(1);
  147. /* Initialize the request structure */
  148. req->submitted = 0;
  149. PTHREAD_MUTEX_INIT(&req->req_mutex, NULL);
  150. PTHREAD_COND_INIT(&req->req_cond, NULL);
  151. req->request_type = RECV_REQ;
  152. req->data_handle = data_handle;
  153. req->srcdst = source;
  154. req->mpi_tag = mpi_tag;
  155. req->comm = comm;
  156. req->detached = detached;
  157. req->callback = callback;
  158. req->callback_arg = arg;
  159. req->func = starpu_mpi_irecv_func;
  160. /* Asynchronously request StarPU to fetch the data in main memory: when
  161. * it is available in main memory, submit_mpi_req(req) is called and
  162. * the request is actually submitted */
  163. starpu_data_acquire_cb(data_handle, STARPU_W, submit_mpi_req, (void *)req);
  164. _STARPU_MPI_LOG_OUT();
  165. return req;
  166. }
  167. int starpu_mpi_irecv(starpu_data_handle data_handle, starpu_mpi_req *public_req, int source, int mpi_tag, MPI_Comm comm)
  168. {
  169. _STARPU_MPI_LOG_IN();
  170. STARPU_ASSERT(public_req);
  171. struct starpu_mpi_req_s *req;
  172. req = _starpu_mpi_irecv_common(data_handle, source, mpi_tag, comm, 0, NULL, NULL);
  173. STARPU_ASSERT(req);
  174. *public_req = req;
  175. _STARPU_MPI_LOG_OUT();
  176. return 0;
  177. }
  178. /*
  179. * Irecv (detached)
  180. */
  181. int starpu_mpi_irecv_detached(starpu_data_handle data_handle, int source, int mpi_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  182. {
  183. _STARPU_MPI_LOG_IN();
  184. _starpu_mpi_irecv_common(data_handle, source, mpi_tag, comm, 1, callback, arg);
  185. _STARPU_MPI_LOG_OUT();
  186. return 0;
  187. }
  188. /*
  189. * Recv
  190. */
  191. int starpu_mpi_recv(starpu_data_handle data_handle, int source, int mpi_tag, MPI_Comm comm, MPI_Status *status)
  192. {
  193. starpu_mpi_req req;
  194. _STARPU_MPI_LOG_IN();
  195. starpu_mpi_irecv(data_handle, &req, source, mpi_tag, comm);
  196. starpu_mpi_wait(&req, status);
  197. _STARPU_MPI_LOG_OUT();
  198. return 0;
  199. }
  200. /*
  201. * Send
  202. */
  203. int starpu_mpi_send(starpu_data_handle data_handle, int dest, int mpi_tag, MPI_Comm comm)
  204. {
  205. starpu_mpi_req req;
  206. MPI_Status status;
  207. _STARPU_MPI_LOG_IN();
  208. memset(&status, 0, sizeof(MPI_Status));
  209. starpu_mpi_isend(data_handle, &req, dest, mpi_tag, comm);
  210. starpu_mpi_wait(&req, &status);
  211. _STARPU_MPI_LOG_OUT();
  212. return 0;
  213. }
  214. /*
  215. * Wait
  216. */
  217. static void starpu_mpi_wait_func(struct starpu_mpi_req_s *waiting_req)
  218. {
  219. _STARPU_MPI_LOG_IN();
  220. /* Which is the mpi request we are waiting for ? */
  221. struct starpu_mpi_req_s *req = waiting_req->other_request;
  222. req->ret = MPI_Wait(&req->request, waiting_req->status);
  223. STARPU_ASSERT(req->ret == MPI_SUCCESS);
  224. handle_request_termination(req);
  225. _STARPU_MPI_LOG_OUT();
  226. }
  227. int starpu_mpi_wait(starpu_mpi_req *public_req, MPI_Status *status)
  228. {
  229. _STARPU_MPI_LOG_IN();
  230. int ret;
  231. struct starpu_mpi_req_s *waiting_req = calloc(1, sizeof(struct starpu_mpi_req_s));
  232. STARPU_ASSERT(waiting_req);
  233. struct starpu_mpi_req_s *req = *public_req;
  234. INC_POSTED_REQUESTS(1);
  235. /* We cannot try to complete a MPI request that was not actually posted
  236. * to MPI yet. */
  237. PTHREAD_MUTEX_LOCK(&(req->req_mutex));
  238. while (!(req->submitted))
  239. PTHREAD_COND_WAIT(&(req->req_cond), &(req->req_mutex));
  240. PTHREAD_MUTEX_UNLOCK(&(req->req_mutex));
  241. /* Initialize the request structure */
  242. PTHREAD_MUTEX_INIT(&(waiting_req->req_mutex), NULL);
  243. PTHREAD_COND_INIT(&(waiting_req->req_cond), NULL);
  244. waiting_req->status = status;
  245. waiting_req->other_request = req;
  246. waiting_req->func = starpu_mpi_wait_func;
  247. waiting_req->request_type = WAIT_REQ;
  248. submit_mpi_req(waiting_req);
  249. /* We wait for the MPI request to finish */
  250. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  251. while (!req->completed)
  252. PTHREAD_COND_WAIT(&req->req_cond, &req->req_mutex);
  253. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  254. ret = req->ret;
  255. /* The internal request structure was automatically allocated */
  256. *public_req = NULL;
  257. free(req);
  258. //free(waiting_req);
  259. _STARPU_MPI_LOG_OUT();
  260. return ret;
  261. }
  262. /*
  263. * Test
  264. */
  265. static void starpu_mpi_test_func(struct starpu_mpi_req_s *testing_req)
  266. {
  267. _STARPU_MPI_LOG_IN();
  268. /* Which is the mpi request we are testing for ? */
  269. struct starpu_mpi_req_s *req = testing_req->other_request;
  270. _STARPU_MPI_DEBUG("Test request %p - mpitag %x - TYPE %s %d\n", &req->request, req->mpi_tag, (req->request_type == RECV_REQ)?"recv : source":"send : dest", req->srcdst);
  271. req->ret = MPI_Test(&req->request, testing_req->flag, testing_req->status);
  272. STARPU_ASSERT(req->ret == MPI_SUCCESS);
  273. if (*testing_req->flag)
  274. {
  275. testing_req->ret = req->ret;
  276. handle_request_termination(req);
  277. }
  278. PTHREAD_MUTEX_LOCK(&testing_req->req_mutex);
  279. testing_req->completed = 1;
  280. PTHREAD_COND_SIGNAL(&testing_req->req_cond);
  281. PTHREAD_MUTEX_UNLOCK(&testing_req->req_mutex);
  282. _STARPU_MPI_LOG_OUT();
  283. }
  284. int starpu_mpi_test(starpu_mpi_req *public_req, int *flag, MPI_Status *status)
  285. {
  286. _STARPU_MPI_LOG_IN();
  287. int ret = 0;
  288. STARPU_ASSERT(public_req);
  289. struct starpu_mpi_req_s *req = *public_req;
  290. STARPU_ASSERT(!req->detached);
  291. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  292. unsigned submitted = req->submitted;
  293. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  294. if (submitted)
  295. {
  296. struct starpu_mpi_req_s *testing_req = calloc(1, sizeof(struct starpu_mpi_req_s));
  297. STARPU_ASSERT(testing_req);
  298. // memset(testing_req, 0, sizeof(struct starpu_mpi_req_s));
  299. /* Initialize the request structure */
  300. PTHREAD_MUTEX_INIT(&(testing_req->req_mutex), NULL);
  301. PTHREAD_COND_INIT(&(testing_req->req_cond), NULL);
  302. testing_req->flag = flag;
  303. testing_req->status = status;
  304. testing_req->other_request = req;
  305. testing_req->func = starpu_mpi_test_func;
  306. testing_req->completed = 0;
  307. testing_req->request_type = TEST_REQ;
  308. INC_POSTED_REQUESTS(1);
  309. submit_mpi_req(testing_req);
  310. /* We wait for the test request to finish */
  311. PTHREAD_MUTEX_LOCK(&(testing_req->req_mutex));
  312. while (!(testing_req->completed))
  313. PTHREAD_COND_WAIT(&(testing_req->req_cond), &(testing_req->req_mutex));
  314. PTHREAD_MUTEX_UNLOCK(&(testing_req->req_mutex));
  315. ret = testing_req->ret;
  316. if (*(testing_req->flag))
  317. {
  318. /* The request was completed so we free the internal
  319. * request structure which was automatically allocated
  320. * */
  321. *public_req = NULL;
  322. free(req);
  323. }
  324. }
  325. else {
  326. *flag = 0;
  327. }
  328. _STARPU_MPI_LOG_OUT();
  329. return ret;
  330. }
  331. /*
  332. * Barrier
  333. */
  334. static void starpu_mpi_barrier_func(struct starpu_mpi_req_s *barrier_req)
  335. {
  336. _STARPU_MPI_LOG_IN();
  337. barrier_req->ret = MPI_Barrier(barrier_req->comm);
  338. STARPU_ASSERT(barrier_req->ret == MPI_SUCCESS);
  339. handle_request_termination(barrier_req);
  340. _STARPU_MPI_LOG_OUT();
  341. }
  342. int starpu_mpi_barrier(MPI_Comm comm)
  343. {
  344. _STARPU_MPI_LOG_IN();
  345. int ret;
  346. struct starpu_mpi_req_s *barrier_req = calloc(1, sizeof(struct starpu_mpi_req_s));
  347. STARPU_ASSERT(barrier_req);
  348. /* Initialize the request structure */
  349. PTHREAD_MUTEX_INIT(&(barrier_req->req_mutex), NULL);
  350. PTHREAD_COND_INIT(&(barrier_req->req_cond), NULL);
  351. barrier_req->func = starpu_mpi_barrier_func;
  352. barrier_req->request_type = BARRIER_REQ;
  353. barrier_req->comm = comm;
  354. INC_POSTED_REQUESTS(1);
  355. submit_mpi_req(barrier_req);
  356. /* We wait for the MPI request to finish */
  357. PTHREAD_MUTEX_LOCK(&barrier_req->req_mutex);
  358. while (!barrier_req->completed)
  359. PTHREAD_COND_WAIT(&barrier_req->req_cond, &barrier_req->req_mutex);
  360. PTHREAD_MUTEX_UNLOCK(&barrier_req->req_mutex);
  361. ret = barrier_req->ret;
  362. //free(waiting_req);
  363. _STARPU_MPI_LOG_OUT();
  364. return ret;
  365. }
  366. /*
  367. * Requests
  368. */
  369. static char *starpu_mpi_request_type(unsigned request_type)
  370. {
  371. switch (request_type)
  372. {
  373. case SEND_REQ: return "send";
  374. case RECV_REQ: return "recv";
  375. case WAIT_REQ: return "wait";
  376. case TEST_REQ: return "test";
  377. case BARRIER_REQ: return "barrier";
  378. default: return "unknown request type";
  379. }
  380. }
  381. static void handle_request_termination(struct starpu_mpi_req_s *req)
  382. {
  383. _STARPU_MPI_LOG_IN();
  384. _STARPU_MPI_DEBUG("complete MPI (%s %d) req %p - tag %x\n", starpu_mpi_request_type(req->request_type), req->srcdst, &req->request, req->mpi_tag);
  385. if (req->request_type != BARRIER_REQ) {
  386. MPI_Type_free(&req->datatype);
  387. starpu_data_release(req->data_handle);
  388. }
  389. if (req->request_type == RECV_REQ)
  390. {
  391. TRACE_MPI_IRECV_END(req->srcdst, req->mpi_tag);
  392. }
  393. /* Execute the specified callback, if any */
  394. if (req->callback)
  395. req->callback(req->callback_arg);
  396. /* tell anyone potentiallly waiting on the request that it is
  397. * terminated now */
  398. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  399. req->completed = 1;
  400. PTHREAD_COND_BROADCAST(&req->req_cond);
  401. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  402. _STARPU_MPI_LOG_OUT();
  403. }
  404. static void submit_mpi_req(void *arg)
  405. {
  406. _STARPU_MPI_LOG_IN();
  407. struct starpu_mpi_req_s *req = arg;
  408. INC_POSTED_REQUESTS(-1);
  409. PTHREAD_MUTEX_LOCK(&mutex);
  410. starpu_mpi_req_list_push_front(new_requests, req);
  411. _STARPU_MPI_DEBUG("Pushing new request type %d\n", req->request_type);
  412. PTHREAD_COND_BROADCAST(&cond);
  413. PTHREAD_MUTEX_UNLOCK(&mutex);
  414. _STARPU_MPI_LOG_OUT();
  415. }
  416. /*
  417. * Scheduler hook
  418. */
  419. #ifdef USE_STARPU_ACTIVITY
  420. static unsigned progression_hook_func(void *arg __attribute__((unused)))
  421. {
  422. unsigned may_block = 1;
  423. PTHREAD_MUTEX_LOCK(&mutex);
  424. if (!starpu_mpi_req_list_empty(detached_requests))
  425. {
  426. PTHREAD_COND_SIGNAL(&cond);
  427. may_block = 0;
  428. }
  429. PTHREAD_MUTEX_UNLOCK(&mutex);
  430. return may_block;
  431. }
  432. #endif
  433. /*
  434. * Progression loop
  435. */
  436. static void test_detached_requests(void)
  437. {
  438. _STARPU_MPI_LOG_IN();
  439. int flag;
  440. MPI_Status status;
  441. struct starpu_mpi_req_s *req, *next_req;
  442. PTHREAD_MUTEX_LOCK(&detached_requests_mutex);
  443. for (req = starpu_mpi_req_list_begin(detached_requests);
  444. req != starpu_mpi_req_list_end(detached_requests);
  445. req = next_req)
  446. {
  447. next_req = starpu_mpi_req_list_next(req);
  448. PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  449. _STARPU_MPI_DEBUG("Test detached request %p - mpitag %x - TYPE %s %d\n", &req->request, req->mpi_tag, (req->request_type == RECV_REQ)?"recv : source":"send : dest", req->srcdst);
  450. req->ret = MPI_Test(&req->request, &flag, &status);
  451. STARPU_ASSERT(req->ret == MPI_SUCCESS);
  452. if (flag)
  453. {
  454. handle_request_termination(req);
  455. }
  456. PTHREAD_MUTEX_LOCK(&detached_requests_mutex);
  457. if (flag)
  458. starpu_mpi_req_list_erase(detached_requests, req);
  459. #warning TODO fix memleak
  460. /* Detached requests are automatically allocated by the lib */
  461. //if (req->detached)
  462. // free(req);
  463. }
  464. PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  465. _STARPU_MPI_LOG_OUT();
  466. }
  467. static void handle_new_request(struct starpu_mpi_req_s *req)
  468. {
  469. _STARPU_MPI_LOG_IN();
  470. STARPU_ASSERT(req);
  471. /* submit the request to MPI */
  472. _STARPU_MPI_DEBUG("Handling new request type %d\n", req->request_type);
  473. req->func(req);
  474. if (req->detached)
  475. {
  476. PTHREAD_MUTEX_LOCK(&mutex);
  477. starpu_mpi_req_list_push_front(detached_requests, req);
  478. PTHREAD_MUTEX_UNLOCK(&mutex);
  479. starpu_wake_all_blocked_workers();
  480. /* put the submitted request into the list of pending requests
  481. * so that it can be handled by the progression mechanisms */
  482. PTHREAD_MUTEX_LOCK(&mutex);
  483. PTHREAD_COND_SIGNAL(&cond);
  484. PTHREAD_MUTEX_UNLOCK(&mutex);
  485. }
  486. _STARPU_MPI_LOG_OUT();
  487. }
  488. static void *progress_thread_func(void *arg)
  489. {
  490. int initialize_mpi = *((int *) arg);
  491. _STARPU_DEBUG("Initialize mpi: %d\n", initialize_mpi);
  492. if (initialize_mpi) {
  493. #warning get real argc and argv from the application
  494. int argc = 0;
  495. char **argv = NULL;
  496. int thread_support;
  497. _STARPU_DEBUG("Calling MPI_Init_thread\n");
  498. if (MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED, &thread_support) != MPI_SUCCESS) {
  499. fprintf(stderr,"MPI_Init_thread failed\n");
  500. exit(1);
  501. }
  502. if (thread_support == MPI_THREAD_FUNNELED)
  503. fprintf(stderr,"Warning: MPI only has funneled thread support, not serialized, hoping this will work\n");
  504. if (thread_support < MPI_THREAD_FUNNELED)
  505. fprintf(stderr,"Warning: MPI does not have thread support!\n");
  506. }
  507. /* notify the main thread that the progression thread is ready */
  508. PTHREAD_MUTEX_LOCK(&mutex);
  509. running = 1;
  510. PTHREAD_COND_SIGNAL(&cond);
  511. PTHREAD_MUTEX_UNLOCK(&mutex);
  512. PTHREAD_MUTEX_LOCK(&mutex);
  513. while (running || posted_requests || !(starpu_mpi_req_list_empty(new_requests)) || !(starpu_mpi_req_list_empty(detached_requests))) {
  514. /* shall we block ? */
  515. unsigned block = starpu_mpi_req_list_empty(new_requests);
  516. #ifndef USE_STARPU_ACTIVITY
  517. block = block && starpu_mpi_req_list_empty(detached_requests);
  518. #endif
  519. if (block)
  520. {
  521. _STARPU_MPI_DEBUG("NO MORE REQUESTS TO HANDLE\n");
  522. PTHREAD_COND_WAIT(&cond, &mutex);
  523. }
  524. /* test whether there are some terminated "detached request" */
  525. PTHREAD_MUTEX_UNLOCK(&mutex);
  526. test_detached_requests();
  527. PTHREAD_MUTEX_LOCK(&mutex);
  528. /* get one request */
  529. struct starpu_mpi_req_s *req;
  530. while (!starpu_mpi_req_list_empty(new_requests))
  531. {
  532. req = starpu_mpi_req_list_pop_back(new_requests);
  533. /* handling a request is likely to block for a while
  534. * (on a sync_data_with_mem call), we want to let the
  535. * application submit requests in the meantime, so we
  536. * release the lock. */
  537. PTHREAD_MUTEX_UNLOCK(&mutex);
  538. handle_new_request(req);
  539. PTHREAD_MUTEX_LOCK(&mutex);
  540. }
  541. }
  542. STARPU_ASSERT(starpu_mpi_req_list_empty(detached_requests));
  543. STARPU_ASSERT(starpu_mpi_req_list_empty(new_requests));
  544. STARPU_ASSERT(posted_requests == 0);
  545. if (initialize_mpi) {
  546. _STARPU_MPI_DEBUG("Calling MPI_Finalize()\n");
  547. MPI_Finalize();
  548. }
  549. PTHREAD_MUTEX_UNLOCK(&mutex);
  550. return NULL;
  551. }
  552. /*
  553. * (De)Initialization methods
  554. */
  555. #ifdef USE_STARPU_ACTIVITY
  556. static int hookid = - 1;
  557. #endif
  558. static void _starpu_mpi_add_sync_point_in_fxt(void)
  559. {
  560. #ifdef STARPU_USE_FXT
  561. int rank;
  562. int worldsize;
  563. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  564. MPI_Comm_size(MPI_COMM_WORLD, &worldsize);
  565. int barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  566. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  567. /* We generate a "unique" key so that we can make sure that different
  568. * FxT traces come from the same MPI run. */
  569. int random_number;
  570. /* XXX perhaps we don't want to generate a new seed if the application
  571. * specified some reproductible behaviour ? */
  572. if (rank == 0)
  573. {
  574. srand(time(NULL));
  575. random_number = rand();
  576. }
  577. MPI_Bcast(&random_number, 1, MPI_INT, 0, MPI_COMM_WORLD);
  578. TRACE_MPI_BARRIER(rank, worldsize, random_number);
  579. _STARPU_MPI_DEBUG("unique key %x\n", random_number);
  580. #endif
  581. }
  582. int starpu_mpi_initialize(void)
  583. {
  584. return starpu_mpi_initialize_extended(0, NULL, NULL);
  585. }
  586. int starpu_mpi_initialize_extended(int initialize_mpi, int *rank, int *world_size)
  587. {
  588. PTHREAD_MUTEX_INIT(&mutex, NULL);
  589. PTHREAD_COND_INIT(&cond, NULL);
  590. new_requests = starpu_mpi_req_list_new();
  591. PTHREAD_MUTEX_INIT(&detached_requests_mutex, NULL);
  592. detached_requests = starpu_mpi_req_list_new();
  593. PTHREAD_MUTEX_INIT(&mutex_posted_requests, NULL);
  594. int ret = pthread_create(&progress_thread, NULL, progress_thread_func, (void *)&initialize_mpi);
  595. PTHREAD_MUTEX_LOCK(&mutex);
  596. while (!running)
  597. PTHREAD_COND_WAIT(&cond, &mutex);
  598. PTHREAD_MUTEX_UNLOCK(&mutex);
  599. if (initialize_mpi) {
  600. _STARPU_DEBUG("Calling MPI_Comm_rank\n");
  601. MPI_Comm_rank(MPI_COMM_WORLD, rank);
  602. MPI_Comm_size(MPI_COMM_WORLD, world_size);
  603. }
  604. #ifdef USE_STARPU_ACTIVITY
  605. hookid = starpu_progression_hook_register(progression_hook_func, NULL);
  606. STARPU_ASSERT(hookid >= 0);
  607. #endif
  608. _starpu_mpi_add_sync_point_in_fxt();
  609. return 0;
  610. }
  611. int starpu_mpi_shutdown(void)
  612. {
  613. void *value;
  614. /* kill the progression thread */
  615. PTHREAD_MUTEX_LOCK(&mutex);
  616. running = 0;
  617. PTHREAD_COND_BROADCAST(&cond);
  618. PTHREAD_MUTEX_UNLOCK(&mutex);
  619. pthread_join(progress_thread, &value);
  620. #ifdef USE_STARPU_ACTIVITY
  621. starpu_progression_hook_deregister(hookid);
  622. #endif
  623. /* free the request queues */
  624. starpu_mpi_req_list_delete(detached_requests);
  625. starpu_mpi_req_list_delete(new_requests);
  626. return 0;
  627. }