starpu_mpi.c 22 KB

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