starpu_mpi.c 23 KB

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