starpu_mpi.c 22 KB

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