starpu_mpi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. /*
  54. * Isend
  55. */
  56. static void starpu_mpi_isend_func(struct starpu_mpi_req_s *req)
  57. {
  58. _STARPU_MPI_LOG_IN();
  59. void *ptr = starpu_mpi_handle_to_ptr(req->data_handle);
  60. _STARPU_MPI_DEBUG("post MPI isend tag %x dst %d ptr %p req %p\n", req->mpi_tag, req->srcdst, ptr, &req->request);
  61. starpu_mpi_handle_to_datatype(req->data_handle, &req->datatype);
  62. req->ret = MPI_Isend(ptr, 1, req->datatype, req->srcdst, req->mpi_tag, req->comm, &req->request);
  63. STARPU_ASSERT(req->ret == MPI_SUCCESS);
  64. TRACE_MPI_ISEND(req->srcdst, req->mpi_tag, 0);
  65. /* somebody is perhaps waiting for the MPI request to be posted */
  66. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  67. req->submitted = 1;
  68. PTHREAD_COND_BROADCAST(&req->req_cond);
  69. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  70. _STARPU_MPI_LOG_OUT();
  71. }
  72. static struct starpu_mpi_req_s *_starpu_mpi_isend_common(starpu_data_handle data_handle,
  73. int dest, int mpi_tag, MPI_Comm comm,
  74. unsigned detached, void (*callback)(void *), void *arg)
  75. {
  76. struct starpu_mpi_req_s *req = calloc(1, sizeof(struct starpu_mpi_req_s));
  77. STARPU_ASSERT(req);
  78. _STARPU_MPI_LOG_IN();
  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. /* 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. /* We cannot try to complete a MPI request that was not actually posted
  234. * to MPI yet. */
  235. PTHREAD_MUTEX_LOCK(&(req->req_mutex));
  236. while (!(req->submitted))
  237. PTHREAD_COND_WAIT(&(req->req_cond), &(req->req_mutex));
  238. PTHREAD_MUTEX_UNLOCK(&(req->req_mutex));
  239. /* Initialize the request structure */
  240. PTHREAD_MUTEX_INIT(&(waiting_req->req_mutex), NULL);
  241. PTHREAD_COND_INIT(&(waiting_req->req_cond), NULL);
  242. waiting_req->status = status;
  243. waiting_req->other_request = req;
  244. waiting_req->func = starpu_mpi_wait_func;
  245. waiting_req->request_type = WAIT_REQ;
  246. submit_mpi_req(waiting_req);
  247. /* We wait for the MPI request to finish */
  248. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  249. while (!req->completed)
  250. PTHREAD_COND_WAIT(&req->req_cond, &req->req_mutex);
  251. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  252. ret = req->ret;
  253. /* The internal request structure was automatically allocated */
  254. *public_req = NULL;
  255. free(req);
  256. //free(waiting_req);
  257. _STARPU_MPI_LOG_OUT();
  258. return ret;
  259. }
  260. /*
  261. * Test
  262. */
  263. static void starpu_mpi_test_func(struct starpu_mpi_req_s *testing_req)
  264. {
  265. _STARPU_MPI_LOG_IN();
  266. /* Which is the mpi request we are testing for ? */
  267. struct starpu_mpi_req_s *req = testing_req->other_request;
  268. _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);
  269. req->ret = MPI_Test(&req->request, testing_req->flag, testing_req->status);
  270. STARPU_ASSERT(req->ret == MPI_SUCCESS);
  271. if (*testing_req->flag)
  272. {
  273. testing_req->ret = req->ret;
  274. handle_request_termination(req);
  275. }
  276. PTHREAD_MUTEX_LOCK(&testing_req->req_mutex);
  277. testing_req->completed = 1;
  278. pthread_cond_signal(&testing_req->req_cond);
  279. PTHREAD_MUTEX_UNLOCK(&testing_req->req_mutex);
  280. _STARPU_MPI_LOG_OUT();
  281. }
  282. int starpu_mpi_test(starpu_mpi_req *public_req, int *flag, MPI_Status *status)
  283. {
  284. _STARPU_MPI_LOG_IN();
  285. int ret = 0;
  286. STARPU_ASSERT(public_req);
  287. struct starpu_mpi_req_s *req = *public_req;
  288. STARPU_ASSERT(!req->detached);
  289. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  290. unsigned submitted = req->submitted;
  291. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  292. if (submitted)
  293. {
  294. struct starpu_mpi_req_s *testing_req = calloc(1, sizeof(struct starpu_mpi_req_s));
  295. STARPU_ASSERT(testing_req);
  296. // memset(testing_req, 0, sizeof(struct starpu_mpi_req_s));
  297. /* Initialize the request structure */
  298. PTHREAD_MUTEX_INIT(&(testing_req->req_mutex), NULL);
  299. PTHREAD_COND_INIT(&(testing_req->req_cond), NULL);
  300. testing_req->flag = flag;
  301. testing_req->status = status;
  302. testing_req->other_request = req;
  303. testing_req->func = starpu_mpi_test_func;
  304. testing_req->completed = 0;
  305. testing_req->request_type = TEST_REQ;
  306. submit_mpi_req(testing_req);
  307. /* We wait for the test request to finish */
  308. PTHREAD_MUTEX_LOCK(&(testing_req->req_mutex));
  309. while (!(testing_req->completed))
  310. PTHREAD_COND_WAIT(&(testing_req->req_cond), &(testing_req->req_mutex));
  311. PTHREAD_MUTEX_UNLOCK(&(testing_req->req_mutex));
  312. ret = testing_req->ret;
  313. if (*(testing_req->flag))
  314. {
  315. /* The request was completed so we free the internal
  316. * request structure which was automatically allocated
  317. * */
  318. *public_req = NULL;
  319. free(req);
  320. }
  321. }
  322. else {
  323. *flag = 0;
  324. }
  325. _STARPU_MPI_LOG_OUT();
  326. return ret;
  327. }
  328. /*
  329. * Requests
  330. */
  331. static void handle_request_termination(struct starpu_mpi_req_s *req)
  332. {
  333. _STARPU_MPI_LOG_IN();
  334. MPI_Type_free(&req->datatype);
  335. starpu_data_release(req->data_handle);
  336. _STARPU_MPI_DEBUG("complete MPI (%s %d) req %p - tag %x\n", (req->request_type == RECV_REQ)?"recv : source":"send : dest", req->srcdst, &req->request, req->mpi_tag);
  337. if (req->request_type == RECV_REQ)
  338. {
  339. TRACE_MPI_IRECV_END(req->srcdst, req->mpi_tag);
  340. }
  341. /* Execute the specified callback, if any */
  342. if (req->callback)
  343. req->callback(req->callback_arg);
  344. /* tell anyone potentiallly waiting on the request that it is
  345. * terminated now */
  346. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  347. req->completed = 1;
  348. PTHREAD_COND_BROADCAST(&req->req_cond);
  349. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  350. _STARPU_MPI_LOG_OUT();
  351. }
  352. static void submit_mpi_req(void *arg)
  353. {
  354. _STARPU_MPI_LOG_IN();
  355. struct starpu_mpi_req_s *req = arg;
  356. PTHREAD_MUTEX_LOCK(&mutex);
  357. starpu_mpi_req_list_push_front(new_requests, req);
  358. PTHREAD_COND_BROADCAST(&cond);
  359. PTHREAD_MUTEX_UNLOCK(&mutex);
  360. _STARPU_MPI_LOG_OUT();
  361. }
  362. /*
  363. * Scheduler hook
  364. */
  365. #ifdef USE_STARPU_ACTIVITY
  366. static unsigned progression_hook_func(void *arg __attribute__((unused)))
  367. {
  368. unsigned may_block = 1;
  369. PTHREAD_MUTEX_LOCK(&mutex);
  370. if (!starpu_mpi_req_list_empty(detached_requests))
  371. {
  372. pthread_cond_signal(&cond);
  373. may_block = 0;
  374. }
  375. PTHREAD_MUTEX_UNLOCK(&mutex);
  376. return may_block;
  377. }
  378. #endif
  379. /*
  380. * Progression loop
  381. */
  382. static void test_detached_requests(void)
  383. {
  384. _STARPU_MPI_LOG_IN();
  385. int flag;
  386. MPI_Status status;
  387. struct starpu_mpi_req_s *req, *next_req;
  388. PTHREAD_MUTEX_LOCK(&detached_requests_mutex);
  389. for (req = starpu_mpi_req_list_begin(detached_requests);
  390. req != starpu_mpi_req_list_end(detached_requests);
  391. req = next_req)
  392. {
  393. next_req = starpu_mpi_req_list_next(req);
  394. PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  395. _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);
  396. req->ret = MPI_Test(&req->request, &flag, &status);
  397. STARPU_ASSERT(req->ret == MPI_SUCCESS);
  398. if (flag)
  399. {
  400. handle_request_termination(req);
  401. }
  402. PTHREAD_MUTEX_LOCK(&detached_requests_mutex);
  403. if (flag)
  404. starpu_mpi_req_list_erase(detached_requests, req);
  405. #warning TODO fix memleak
  406. /* Detached requests are automatically allocated by the lib */
  407. //if (req->detached)
  408. // free(req);
  409. }
  410. PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  411. _STARPU_MPI_LOG_OUT();
  412. }
  413. static void handle_new_request(struct starpu_mpi_req_s *req)
  414. {
  415. _STARPU_MPI_LOG_IN();
  416. STARPU_ASSERT(req);
  417. /* submit the request to MPI */
  418. req->func(req);
  419. if (req->detached)
  420. {
  421. PTHREAD_MUTEX_LOCK(&mutex);
  422. starpu_mpi_req_list_push_front(detached_requests, req);
  423. PTHREAD_MUTEX_UNLOCK(&mutex);
  424. starpu_wake_all_blocked_workers();
  425. /* put the submitted request into the list of pending requests
  426. * so that it can be handled by the progression mechanisms */
  427. PTHREAD_MUTEX_LOCK(&mutex);
  428. pthread_cond_signal(&cond);
  429. PTHREAD_MUTEX_UNLOCK(&mutex);
  430. }
  431. _STARPU_MPI_LOG_OUT();
  432. }
  433. static void *progress_thread_func(void *arg __attribute__((unused)))
  434. {
  435. /* notify the main thread that the progression thread is ready */
  436. PTHREAD_MUTEX_LOCK(&mutex);
  437. running = 1;
  438. pthread_cond_signal(&cond);
  439. PTHREAD_MUTEX_UNLOCK(&mutex);
  440. PTHREAD_MUTEX_LOCK(&mutex);
  441. while (running || !(starpu_mpi_req_list_empty(new_requests)) || !(starpu_mpi_req_list_empty(detached_requests))) {
  442. /* shall we block ? */
  443. unsigned block = starpu_mpi_req_list_empty(new_requests);
  444. #ifndef USE_STARPU_ACTIVITY
  445. block = block && starpu_mpi_req_list_empty(detached_requests);
  446. #endif
  447. if (block)
  448. {
  449. _STARPU_MPI_DEBUG("NO MORE REQUESTS TO HANDLE\n");
  450. PTHREAD_COND_WAIT(&cond, &mutex);
  451. }
  452. /* test whether there are some terminated "detached request" */
  453. PTHREAD_MUTEX_UNLOCK(&mutex);
  454. test_detached_requests();
  455. PTHREAD_MUTEX_LOCK(&mutex);
  456. /* get one request */
  457. struct starpu_mpi_req_s *req;
  458. while (!starpu_mpi_req_list_empty(new_requests))
  459. {
  460. req = starpu_mpi_req_list_pop_back(new_requests);
  461. /* handling a request is likely to block for a while
  462. * (on a sync_data_with_mem call), we want to let the
  463. * application submit requests in the meantime, so we
  464. * release the lock. */
  465. PTHREAD_MUTEX_UNLOCK(&mutex);
  466. handle_new_request(req);
  467. PTHREAD_MUTEX_LOCK(&mutex);
  468. }
  469. }
  470. STARPU_ASSERT(starpu_mpi_req_list_empty(detached_requests));
  471. STARPU_ASSERT(starpu_mpi_req_list_empty(new_requests));
  472. PTHREAD_MUTEX_UNLOCK(&mutex);
  473. return NULL;
  474. }
  475. /*
  476. * (De)Initialization methods
  477. */
  478. #ifdef USE_STARPU_ACTIVITY
  479. static int hookid = - 1;
  480. #endif
  481. static void _starpu_mpi_add_sync_point_in_fxt(void)
  482. {
  483. #ifdef STARPU_USE_FXT
  484. int rank;
  485. int worldsize;
  486. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  487. MPI_Comm_size(MPI_COMM_WORLD, &worldsize);
  488. int barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  489. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  490. /* We generate a "unique" key so that we can make sure that different
  491. * FxT traces come from the same MPI run. */
  492. int random_number;
  493. /* XXX perhaps we don't want to generate a new seed if the application
  494. * specified some reproductible behaviour ? */
  495. if (rank == 0)
  496. {
  497. srand(time(NULL));
  498. random_number = rand();
  499. }
  500. MPI_Bcast(&random_number, 1, MPI_INT, 0, MPI_COMM_WORLD);
  501. TRACE_MPI_BARRIER(rank, worldsize, random_number);
  502. _STARPU_MPI_DEBUG("unique key %x\n", random_number);
  503. #endif
  504. }
  505. int starpu_mpi_initialize(void)
  506. {
  507. PTHREAD_MUTEX_INIT(&mutex, NULL);
  508. PTHREAD_COND_INIT(&cond, NULL);
  509. new_requests = starpu_mpi_req_list_new();
  510. PTHREAD_MUTEX_INIT(&detached_requests_mutex, NULL);
  511. detached_requests = starpu_mpi_req_list_new();
  512. int ret = pthread_create(&progress_thread, NULL, progress_thread_func, NULL);
  513. PTHREAD_MUTEX_LOCK(&mutex);
  514. while (!running)
  515. PTHREAD_COND_WAIT(&cond, &mutex);
  516. PTHREAD_MUTEX_UNLOCK(&mutex);
  517. #ifdef USE_STARPU_ACTIVITY
  518. hookid = starpu_progression_hook_register(progression_hook_func, NULL);
  519. STARPU_ASSERT(hookid >= 0);
  520. #endif
  521. _starpu_mpi_add_sync_point_in_fxt();
  522. return 0;
  523. }
  524. int starpu_mpi_shutdown(void)
  525. {
  526. void *value;
  527. /* kill the progression thread */
  528. PTHREAD_MUTEX_LOCK(&mutex);
  529. running = 0;
  530. PTHREAD_COND_BROADCAST(&cond);
  531. PTHREAD_MUTEX_UNLOCK(&mutex);
  532. pthread_join(progress_thread, &value);
  533. #ifdef USE_STARPU_ACTIVITY
  534. starpu_progression_hook_deregister(hookid);
  535. #endif
  536. /* free the request queues */
  537. starpu_mpi_req_list_delete(detached_requests);
  538. starpu_mpi_req_list_delete(new_requests);
  539. return 0;
  540. }