starpu_mpi.c 14 KB

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