starpu_mpi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. /* TODO find a better way to select the polling method (perhaps during the
  29. * configuration) */
  30. //#define USE_STARPU_ACTIVITY 1
  31. static void submit_mpi_req(void *arg);
  32. static void handle_request_termination(struct starpu_mpi_req_s *req);
  33. /* The list of requests that have been newly submitted by the application */
  34. static starpu_mpi_req_list_t new_requests;
  35. /* The list of detached requests that have already been submitted to MPI */
  36. static starpu_mpi_req_list_t detached_requests;
  37. static pthread_mutex_t detached_requests_mutex;
  38. static pthread_cond_t cond;
  39. static pthread_mutex_t mutex;
  40. static pthread_t progress_thread;
  41. static int running = 0;
  42. /*
  43. * Isend
  44. */
  45. static void starpu_mpi_isend_func(struct starpu_mpi_req_s *req)
  46. {
  47. void *ptr = starpu_mpi_handle_to_ptr(req->data_handle);
  48. _STARPU_MPI_DEBUG("post MPI isend tag %x dst %d ptr %p req %p\n", req->mpi_tag, req->srcdst, ptr, &req->request);
  49. starpu_mpi_handle_to_datatype(req->data_handle, &req->datatype);
  50. //MPI_Isend(ptr, 1, req->datatype, req->srcdst, req->mpi_tag, req->comm, &req->request);
  51. MPI_Isend(ptr, 1, req->datatype, req->srcdst, req->mpi_tag, req->comm, &req->request);
  52. TRACE_MPI_ISEND(req->srcdst, req->mpi_tag, 0);
  53. /* somebody is perhaps waiting for the MPI request to be posted */
  54. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  55. req->submitted = 1;
  56. PTHREAD_COND_BROADCAST(&req->req_cond);
  57. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  58. }
  59. static struct starpu_mpi_req_s *_starpu_mpi_isend_common(starpu_data_handle data_handle,
  60. int dest, int mpi_tag, MPI_Comm comm,
  61. unsigned detached, void (*callback)(void *), void *arg)
  62. {
  63. struct starpu_mpi_req_s *req = calloc(1, sizeof(struct starpu_mpi_req_s));
  64. STARPU_ASSERT(req);
  65. /* Initialize the request structure */
  66. req->submitted = 0;
  67. req->completed = 0;
  68. PTHREAD_MUTEX_INIT(&req->req_mutex, NULL);
  69. PTHREAD_COND_INIT(&req->req_cond, NULL);
  70. req->request_type = SEND_REQ;
  71. req->data_handle = data_handle;
  72. req->srcdst = dest;
  73. req->mpi_tag = mpi_tag;
  74. req->comm = comm;
  75. req->func = starpu_mpi_isend_func;
  76. req->detached = detached;
  77. req->callback = callback;
  78. req->callback_arg = arg;
  79. /* Asynchronously request StarPU to fetch the data in main memory: when
  80. * it is available in main memory, submit_mpi_req(req) is called and
  81. * the request is actually submitted */
  82. starpu_data_acquire_cb(data_handle, STARPU_R,
  83. submit_mpi_req, (void *)req);
  84. return req;
  85. }
  86. int starpu_mpi_isend(starpu_data_handle data_handle, starpu_mpi_req *public_req, int dest, int mpi_tag, MPI_Comm comm)
  87. {
  88. STARPU_ASSERT(public_req);
  89. struct starpu_mpi_req_s *req;
  90. req = _starpu_mpi_isend_common(data_handle, dest, mpi_tag, comm, 0, NULL, NULL);
  91. STARPU_ASSERT(req);
  92. *public_req = req;
  93. return 0;
  94. }
  95. /*
  96. * Isend (detached)
  97. */
  98. int starpu_mpi_isend_detached(starpu_data_handle data_handle,
  99. int dest, int mpi_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  100. {
  101. _starpu_mpi_isend_common(data_handle, dest, mpi_tag, comm, 1, callback, arg);
  102. return 0;
  103. }
  104. /*
  105. * Irecv
  106. */
  107. static void starpu_mpi_irecv_func(struct starpu_mpi_req_s *req)
  108. {
  109. void *ptr = starpu_mpi_handle_to_ptr(req->data_handle);
  110. STARPU_ASSERT(ptr);
  111. starpu_mpi_handle_to_datatype(req->data_handle, &req->datatype);
  112. _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);
  113. MPI_Irecv(ptr, 1, req->datatype, req->srcdst, req->mpi_tag, req->comm, &req->request);
  114. /* somebody is perhaps waiting for the MPI request to be posted */
  115. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  116. req->submitted = 1;
  117. PTHREAD_COND_BROADCAST(&req->req_cond);
  118. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  119. }
  120. 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)
  121. {
  122. struct starpu_mpi_req_s *req = calloc(1, sizeof(struct starpu_mpi_req_s));
  123. STARPU_ASSERT(req);
  124. /* Initialize the request structure */
  125. req->submitted = 0;
  126. PTHREAD_MUTEX_INIT(&req->req_mutex, NULL);
  127. PTHREAD_COND_INIT(&req->req_cond, NULL);
  128. req->request_type = RECV_REQ;
  129. req->data_handle = data_handle;
  130. req->srcdst = source;
  131. req->mpi_tag = mpi_tag;
  132. req->comm = comm;
  133. req->detached = detached;
  134. req->callback = callback;
  135. req->callback_arg = arg;
  136. req->func = starpu_mpi_irecv_func;
  137. /* Asynchronously request StarPU to fetch the data in main memory: when
  138. * it is available in main memory, submit_mpi_req(req) is called and
  139. * the request is actually submitted */
  140. starpu_data_acquire_cb(data_handle, STARPU_W,
  141. submit_mpi_req, (void *)req);
  142. return req;
  143. }
  144. int starpu_mpi_irecv(starpu_data_handle data_handle, starpu_mpi_req *public_req, int source, int mpi_tag, MPI_Comm comm)
  145. {
  146. STARPU_ASSERT(public_req);
  147. struct starpu_mpi_req_s *req;
  148. req = _starpu_mpi_irecv_common(data_handle, source, mpi_tag, comm, 0, NULL, NULL);
  149. STARPU_ASSERT(req);
  150. *public_req = req;
  151. return 0;
  152. }
  153. /*
  154. * Irecv (detached)
  155. */
  156. int starpu_mpi_irecv_detached(starpu_data_handle data_handle, int source, int mpi_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  157. {
  158. _starpu_mpi_irecv_common(data_handle, source, mpi_tag, comm, 1, callback, arg);
  159. return 0;
  160. }
  161. /*
  162. * Recv
  163. */
  164. int starpu_mpi_recv(starpu_data_handle data_handle,
  165. int source, int mpi_tag, MPI_Comm comm, MPI_Status *status)
  166. {
  167. starpu_mpi_req req;
  168. starpu_mpi_irecv(data_handle, &req, source, mpi_tag, comm);
  169. starpu_mpi_wait(&req, status);
  170. return 0;
  171. }
  172. /*
  173. * Send
  174. */
  175. int starpu_mpi_send(starpu_data_handle data_handle,
  176. int dest, int mpi_tag, MPI_Comm comm)
  177. {
  178. starpu_mpi_req req;
  179. MPI_Status status;
  180. memset(&status, 0, sizeof(MPI_Status));
  181. starpu_mpi_isend(data_handle, &req, dest, mpi_tag, comm);
  182. starpu_mpi_wait(&req, &status);
  183. return 0;
  184. }
  185. /*
  186. * Wait
  187. */
  188. static void starpu_mpi_wait_func(struct starpu_mpi_req_s *waiting_req)
  189. {
  190. /* Which is the mpi request we are waiting for ? */
  191. struct starpu_mpi_req_s *req = waiting_req->other_request;
  192. req->ret = MPI_Wait(&req->request, waiting_req->status);
  193. handle_request_termination(req);
  194. }
  195. int starpu_mpi_wait(starpu_mpi_req *public_req, MPI_Status *status)
  196. {
  197. int ret;
  198. struct starpu_mpi_req_s waiting_req;
  199. memset(&waiting_req, 0, sizeof(struct starpu_mpi_req_s));
  200. struct starpu_mpi_req_s *req = *public_req;
  201. /* We cannot try to complete a MPI request that was not actually posted
  202. * to MPI yet. */
  203. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  204. while (!req->submitted)
  205. PTHREAD_COND_WAIT(&req->req_cond, &req->req_mutex);
  206. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  207. /* Initialize the request structure */
  208. PTHREAD_MUTEX_INIT(&waiting_req.req_mutex, NULL);
  209. PTHREAD_COND_INIT(&waiting_req.req_cond, NULL);
  210. waiting_req.status = status;
  211. waiting_req.other_request = req;
  212. waiting_req.func = starpu_mpi_wait_func;
  213. submit_mpi_req(&waiting_req);
  214. /* We wait for the MPI request to finish */
  215. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  216. while (!req->completed)
  217. PTHREAD_COND_WAIT(&req->req_cond, &req->req_mutex);
  218. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  219. ret = req->ret;
  220. /* The internal request structure was automatically allocated */
  221. *public_req = NULL;
  222. free(req);
  223. return ret;
  224. }
  225. /*
  226. * Test
  227. */
  228. static void starpu_mpi_test_func(struct starpu_mpi_req_s *testing_req)
  229. {
  230. /* Which is the mpi request we are testing for ? */
  231. struct starpu_mpi_req_s *req = testing_req->other_request;
  232. _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);
  233. int ret = MPI_Test(&req->request, testing_req->flag, testing_req->status);
  234. if (*testing_req->flag)
  235. {
  236. testing_req->ret = ret;
  237. handle_request_termination(req);
  238. }
  239. PTHREAD_MUTEX_LOCK(&testing_req->req_mutex);
  240. testing_req->completed = 1;
  241. pthread_cond_signal(&testing_req->req_cond);
  242. PTHREAD_MUTEX_UNLOCK(&testing_req->req_mutex);
  243. }
  244. int starpu_mpi_test(starpu_mpi_req *public_req, int *flag, MPI_Status *status)
  245. {
  246. int ret = 0;
  247. STARPU_ASSERT(public_req);
  248. struct starpu_mpi_req_s *req = *public_req;
  249. STARPU_ASSERT(!req->detached);
  250. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  251. unsigned submitted = req->submitted;
  252. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  253. if (submitted)
  254. {
  255. struct starpu_mpi_req_s testing_req;
  256. memset(&testing_req, 0, sizeof(struct starpu_mpi_req_s));
  257. /* Initialize the request structure */
  258. PTHREAD_MUTEX_INIT(&testing_req.req_mutex, NULL);
  259. PTHREAD_COND_INIT(&testing_req.req_cond, NULL);
  260. testing_req.flag = flag;
  261. testing_req.status = status;
  262. testing_req.other_request = req;
  263. testing_req.func = starpu_mpi_test_func;
  264. testing_req.completed = 0;
  265. submit_mpi_req(&testing_req);
  266. /* We wait for the test request to finish */
  267. PTHREAD_MUTEX_LOCK(&testing_req.req_mutex);
  268. while (!testing_req.completed)
  269. PTHREAD_COND_WAIT(&testing_req.req_cond, &testing_req.req_mutex);
  270. PTHREAD_MUTEX_UNLOCK(&testing_req.req_mutex);
  271. ret = testing_req.ret;
  272. if (*testing_req.flag)
  273. {
  274. /* The request was completed so we free the internal
  275. * request structure which was automatically allocated
  276. * */
  277. *public_req = NULL;
  278. free(req);
  279. }
  280. }
  281. else {
  282. *flag = 0;
  283. }
  284. return ret;
  285. }
  286. /*
  287. * Requests
  288. */
  289. static void handle_request_termination(struct starpu_mpi_req_s *req)
  290. {
  291. MPI_Type_free(&req->datatype);
  292. starpu_data_release(req->data_handle);
  293. _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);
  294. if (req->request_type == RECV_REQ)
  295. {
  296. TRACE_MPI_IRECV_END(req->srcdst, req->mpi_tag);
  297. }
  298. /* Execute the specified callback, if any */
  299. if (req->callback)
  300. req->callback(req->callback_arg);
  301. /* tell anyone potentiallly waiting on the request that it is
  302. * terminated now */
  303. PTHREAD_MUTEX_LOCK(&req->req_mutex);
  304. req->completed = 1;
  305. PTHREAD_COND_BROADCAST(&req->req_cond);
  306. PTHREAD_MUTEX_UNLOCK(&req->req_mutex);
  307. }
  308. static void submit_mpi_req(void *arg)
  309. {
  310. struct starpu_mpi_req_s *req = arg;
  311. PTHREAD_MUTEX_LOCK(&mutex);
  312. starpu_mpi_req_list_push_front(new_requests, req);
  313. PTHREAD_COND_BROADCAST(&cond);
  314. PTHREAD_MUTEX_UNLOCK(&mutex);
  315. }
  316. /*
  317. * Scheduler hook
  318. */
  319. static unsigned progression_hook_func(void *arg __attribute__((unused)))
  320. {
  321. unsigned may_block = 1;
  322. PTHREAD_MUTEX_LOCK(&mutex);
  323. if (!starpu_mpi_req_list_empty(detached_requests))
  324. {
  325. pthread_cond_signal(&cond);
  326. may_block = 0;
  327. }
  328. PTHREAD_MUTEX_UNLOCK(&mutex);
  329. return may_block;
  330. }
  331. /*
  332. * Progression loop
  333. */
  334. static void test_detached_requests(void)
  335. {
  336. int flag;
  337. MPI_Status status;
  338. struct starpu_mpi_req_s *req, *next_req;
  339. PTHREAD_MUTEX_LOCK(&detached_requests_mutex);
  340. for (req = starpu_mpi_req_list_begin(detached_requests);
  341. req != starpu_mpi_req_list_end(detached_requests);
  342. req = next_req)
  343. {
  344. next_req = starpu_mpi_req_list_next(req);
  345. PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  346. int ret = MPI_Test(&req->request, &flag, &status);
  347. STARPU_ASSERT(ret == MPI_SUCCESS);
  348. _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);
  349. if (flag)
  350. {
  351. handle_request_termination(req);
  352. }
  353. PTHREAD_MUTEX_LOCK(&detached_requests_mutex);
  354. if (flag)
  355. starpu_mpi_req_list_erase(detached_requests, req);
  356. #warning TODO fix memleak
  357. /* Detached requests are automatically allocated by the lib */
  358. //if (req->detached)
  359. // free(req);
  360. }
  361. PTHREAD_MUTEX_UNLOCK(&detached_requests_mutex);
  362. }
  363. static void handle_new_request(struct starpu_mpi_req_s *req)
  364. {
  365. STARPU_ASSERT(req);
  366. /* submit the request to MPI */
  367. req->func(req);
  368. if (req->detached)
  369. {
  370. PTHREAD_MUTEX_LOCK(&mutex);
  371. starpu_mpi_req_list_push_front(detached_requests, req);
  372. PTHREAD_MUTEX_UNLOCK(&mutex);
  373. starpu_wake_all_blocked_workers();
  374. /* put the submitted request into the list of pending requests
  375. * so that it can be handled by the progression mechanisms */
  376. PTHREAD_MUTEX_LOCK(&mutex);
  377. pthread_cond_signal(&cond);
  378. PTHREAD_MUTEX_UNLOCK(&mutex);
  379. }
  380. }
  381. static void *progress_thread_func(void *arg __attribute__((unused)))
  382. {
  383. /* notify the main thread that the progression thread is ready */
  384. PTHREAD_MUTEX_LOCK(&mutex);
  385. running = 1;
  386. pthread_cond_signal(&cond);
  387. PTHREAD_MUTEX_UNLOCK(&mutex);
  388. PTHREAD_MUTEX_LOCK(&mutex);
  389. while (running) {
  390. /* shall we block ? */
  391. unsigned block = starpu_mpi_req_list_empty(new_requests);
  392. #ifndef USE_STARPU_ACTIVITY
  393. block = block && starpu_mpi_req_list_empty(detached_requests);
  394. #endif
  395. if (block)
  396. {
  397. _STARPU_MPI_DEBUG("NO MORE REQUESTS TO HANDLE\n");
  398. PTHREAD_COND_WAIT(&cond, &mutex);
  399. }
  400. if (!running)
  401. break;
  402. /* test whether there are some terminated "detached request" */
  403. PTHREAD_MUTEX_UNLOCK(&mutex);
  404. test_detached_requests();
  405. PTHREAD_MUTEX_LOCK(&mutex);
  406. /* get one request */
  407. struct starpu_mpi_req_s *req;
  408. while (!starpu_mpi_req_list_empty(new_requests))
  409. {
  410. req = starpu_mpi_req_list_pop_back(new_requests);
  411. /* handling a request is likely to block for a while
  412. * (on a sync_data_with_mem call), we want to let the
  413. * application submit requests in the meantime, so we
  414. * release the lock. */
  415. PTHREAD_MUTEX_UNLOCK(&mutex);
  416. handle_new_request(req);
  417. PTHREAD_MUTEX_LOCK(&mutex);
  418. }
  419. }
  420. PTHREAD_MUTEX_UNLOCK(&mutex);
  421. return NULL;
  422. }
  423. /*
  424. * (De)Initialization methods
  425. */
  426. #ifdef USE_STARPU_ACTIVITY
  427. static int hookid = - 1;
  428. #endif
  429. static void _starpu_mpi_add_sync_point_in_fxt(void)
  430. {
  431. #ifdef STARPU_USE_FXT
  432. int rank;
  433. int worldsize;
  434. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  435. MPI_Comm_size(MPI_COMM_WORLD, &worldsize);
  436. int barrier_ret = MPI_Barrier(MPI_COMM_WORLD);
  437. STARPU_ASSERT(barrier_ret == MPI_SUCCESS);
  438. /* We generate a "unique" key so that we can make sure that different
  439. * FxT traces come from the same MPI run. */
  440. int random_number;
  441. /* XXX perhaps we don't want to generate a new seed if the application
  442. * specified some reproductible behaviour ? */
  443. if (rank == 0)
  444. {
  445. srand(time(NULL));
  446. random_number = rand();
  447. }
  448. MPI_Bcast(&random_number, 1, MPI_INT, 0, MPI_COMM_WORLD);
  449. TRACE_MPI_BARRIER(rank, worldsize, random_number);
  450. _STARPU_MPI_DEBUG("unique key %x\n", random_number);
  451. #endif
  452. }
  453. int starpu_mpi_initialize(void)
  454. {
  455. PTHREAD_MUTEX_INIT(&mutex, NULL);
  456. PTHREAD_COND_INIT(&cond, NULL);
  457. new_requests = starpu_mpi_req_list_new();
  458. PTHREAD_MUTEX_INIT(&detached_requests_mutex, NULL);
  459. detached_requests = starpu_mpi_req_list_new();
  460. int ret = pthread_create(&progress_thread, NULL, progress_thread_func, NULL);
  461. PTHREAD_MUTEX_LOCK(&mutex);
  462. while (!running)
  463. PTHREAD_COND_WAIT(&cond, &mutex);
  464. PTHREAD_MUTEX_UNLOCK(&mutex);
  465. #ifdef USE_STARPU_ACTIVITY
  466. hookid = starpu_progression_hook_register(progression_hook_func, NULL);
  467. STARPU_ASSERT(hookid >= 0);
  468. #endif
  469. _starpu_mpi_add_sync_point_in_fxt();
  470. return 0;
  471. }
  472. int starpu_mpi_shutdown(void)
  473. {
  474. void *value;
  475. /* kill the progression thread */
  476. PTHREAD_MUTEX_LOCK(&mutex);
  477. running = 0;
  478. PTHREAD_COND_BROADCAST(&cond);
  479. PTHREAD_MUTEX_UNLOCK(&mutex);
  480. pthread_join(progress_thread, &value);
  481. #ifdef USE_STARPU_ACTIVITY
  482. starpu_progression_hook_deregister(hookid);
  483. #endif
  484. /* free the request queues */
  485. starpu_mpi_req_list_delete(detached_requests);
  486. starpu_mpi_req_list_delete(new_requests);
  487. return 0;
  488. }