starpu_mpi.c 13 KB

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