starpu_mpi.c 14 KB

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