starpu_mpi.c 15 KB

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