starpu_mpi.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. static pthread_cond_t cond;
  23. static pthread_mutex_t mutex;
  24. static pthread_t progress_thread;
  25. static int running = 0;
  26. /*
  27. * Isend
  28. */
  29. static void starpu_mpi_isend_func(struct starpu_mpi_req_s *req)
  30. {
  31. void *ptr = starpu_mpi_handle_to_ptr(req->data_handle);
  32. starpu_mpi_handle_to_datatype(req->data_handle, &req->datatype);
  33. MPI_Isend(ptr, 1, req->datatype, req->srcdst, req->mpi_tag, req->comm, &req->request);
  34. /* somebody is perhaps waiting for the MPI request to be posted */
  35. pthread_mutex_lock(&req->req_mutex);
  36. req->submitted = 1;
  37. pthread_cond_broadcast(&req->req_cond);
  38. pthread_mutex_unlock(&req->req_mutex);
  39. }
  40. int starpu_mpi_isend(starpu_data_handle data_handle, struct starpu_mpi_req_s *req, int dest, int mpi_tag, MPI_Comm comm)
  41. {
  42. STARPU_ASSERT(req);
  43. memset(req, 0, sizeof(struct starpu_mpi_req_s));
  44. /* Initialize the request structure */
  45. req->submitted = 0;
  46. req->completed = 0;
  47. pthread_mutex_init(&req->req_mutex, NULL);
  48. pthread_cond_init(&req->req_cond, NULL);
  49. req->data_handle = data_handle;
  50. req->srcdst = dest;
  51. req->mpi_tag = mpi_tag;
  52. req->comm = comm;
  53. req->detached = 0;
  54. req->func = starpu_mpi_isend_func;
  55. /* Asynchronously request StarPU to fetch the data in main memory: when
  56. * it is available in main memory, submit_mpi_req(req) is called and
  57. * the request is actually submitted */
  58. starpu_sync_data_with_mem_non_blocking(data_handle, STARPU_R,
  59. submit_mpi_req, (void *)req);
  60. return 0;
  61. }
  62. /*
  63. * Isend (detached)
  64. */
  65. int starpu_mpi_isend_detached(starpu_data_handle data_handle, struct starpu_mpi_req_s *req, int dest, int mpi_tag, MPI_Comm comm, void (*callback)(void *), void *arg)
  66. {
  67. /* TODO */
  68. return 0;
  69. }
  70. /*
  71. * Irecv
  72. */
  73. static void starpu_mpi_irecv_func(struct starpu_mpi_req_s *req)
  74. {
  75. void *ptr = starpu_mpi_handle_to_ptr(req->data_handle);
  76. starpu_mpi_handle_to_datatype(req->data_handle, &req->datatype);
  77. MPI_Irecv(ptr, 1, req->datatype, req->srcdst, req->mpi_tag, req->comm, &req->request);
  78. /* somebody is perhaps waiting for the MPI request to be posted */
  79. pthread_mutex_lock(&req->req_mutex);
  80. req->submitted = 1;
  81. pthread_cond_broadcast(&req->req_cond);
  82. pthread_mutex_unlock(&req->req_mutex);
  83. }
  84. int starpu_mpi_irecv(starpu_data_handle data_handle, struct starpu_mpi_req_s *req, int source, int mpi_tag, MPI_Comm comm)
  85. {
  86. STARPU_ASSERT(req);
  87. memset(req, 0, sizeof(struct starpu_mpi_req_s));
  88. /* Initialize the request structure */
  89. req->submitted = 0;
  90. pthread_mutex_init(&req->req_mutex, NULL);
  91. pthread_cond_init(&req->req_cond, NULL);
  92. req->data_handle = data_handle;
  93. req->srcdst = source;
  94. req->mpi_tag = mpi_tag;
  95. req->comm = comm;
  96. req->detached = 0;
  97. req->func = starpu_mpi_irecv_func;
  98. /* Asynchronously request StarPU to fetch the data in main memory: when
  99. * it is available in main memory, submit_mpi_req(req) is called and
  100. * the request is actually submitted */
  101. starpu_sync_data_with_mem_non_blocking(data_handle, STARPU_W,
  102. submit_mpi_req, (void *)req);
  103. return 0;
  104. }
  105. /*
  106. * Recv
  107. */
  108. int starpu_mpi_recv(starpu_data_handle data_handle,
  109. int source, int mpi_tag, MPI_Comm comm, MPI_Status *status)
  110. {
  111. struct starpu_mpi_req_s req;
  112. memset(&req, 0, sizeof(struct starpu_mpi_req_s));
  113. starpu_mpi_irecv(data_handle, &req, source, mpi_tag, comm);
  114. starpu_mpi_wait(&req, status);
  115. return 0;
  116. }
  117. /*
  118. * Send
  119. */
  120. int starpu_mpi_send(starpu_data_handle data_handle,
  121. int dest, int mpi_tag, MPI_Comm comm)
  122. {
  123. struct starpu_mpi_req_s req;
  124. MPI_Status status;
  125. memset(&req, 0, sizeof(struct starpu_mpi_req_s));
  126. memset(&status, 0, sizeof(MPI_Status));
  127. starpu_mpi_isend(data_handle, &req, dest, mpi_tag, comm);
  128. starpu_mpi_wait(&req, &status);
  129. return 0;
  130. }
  131. /*
  132. * Wait
  133. */
  134. static void starpu_mpi_wait_func(struct starpu_mpi_req_s *waiting_req)
  135. {
  136. /* Which is the mpi request we are waiting for ? */
  137. struct starpu_mpi_req_s *req = waiting_req->other_request;
  138. req->ret = MPI_Wait(&req->request, waiting_req->status);
  139. handle_request_termination(req);
  140. }
  141. int starpu_mpi_wait(struct starpu_mpi_req_s *req, MPI_Status *status)
  142. {
  143. int ret;
  144. struct starpu_mpi_req_s waiting_req;
  145. memset(&waiting_req, 0, sizeof(struct starpu_mpi_req_s));
  146. /* We cannot try to complete a MPI request that was not actually posted
  147. * to MPI yet. */
  148. pthread_mutex_lock(&req->req_mutex);
  149. while (!req->submitted)
  150. pthread_cond_wait(&req->req_cond, &req->req_mutex);
  151. pthread_mutex_unlock(&req->req_mutex);
  152. /* Initialize the request structure */
  153. pthread_mutex_init(&waiting_req.req_mutex, NULL);
  154. pthread_cond_init(&waiting_req.req_cond, NULL);
  155. waiting_req.status = status;
  156. waiting_req.other_request = req;
  157. waiting_req.func = starpu_mpi_wait_func;
  158. submit_mpi_req(&waiting_req);
  159. /* We wait for the MPI request to finish */
  160. pthread_mutex_lock(&req->req_mutex);
  161. while (!req->completed)
  162. pthread_cond_wait(&req->req_cond, &req->req_mutex);
  163. pthread_mutex_unlock(&req->req_mutex);
  164. ret = req->ret;
  165. return ret;
  166. }
  167. /*
  168. * Test
  169. */
  170. static void starpu_mpi_test_func(struct starpu_mpi_req_s *testing_req)
  171. {
  172. /* Which is the mpi request we are testing for ? */
  173. struct starpu_mpi_req_s *req = testing_req->other_request;
  174. int ret = MPI_Test(&req->request, testing_req->flag, testing_req->status);
  175. if (*testing_req->flag)
  176. {
  177. testing_req->ret = ret;
  178. handle_request_termination(req);
  179. }
  180. pthread_mutex_lock(&testing_req->req_mutex);
  181. testing_req->completed = 1;
  182. pthread_cond_signal(&testing_req->req_cond);
  183. pthread_mutex_unlock(&testing_req->req_mutex);
  184. }
  185. int starpu_mpi_test(struct starpu_mpi_req_s *req, int *flag, MPI_Status *status)
  186. {
  187. int ret = 0;
  188. STARPU_ASSERT(!req->detached);
  189. pthread_mutex_lock(&req->req_mutex);
  190. unsigned submitted = req->submitted;
  191. pthread_mutex_unlock(&req->req_mutex);
  192. if (submitted)
  193. {
  194. struct starpu_mpi_req_s testing_req;
  195. memset(&testing_req, 0, sizeof(struct starpu_mpi_req_s));
  196. /* Initialize the request structure */
  197. pthread_mutex_init(&testing_req.req_mutex, NULL);
  198. pthread_cond_init(&testing_req.req_cond, NULL);
  199. testing_req.flag = flag;
  200. testing_req.status = status;
  201. testing_req.other_request = req;
  202. testing_req.func = starpu_mpi_test_func;
  203. testing_req.completed = 0;
  204. submit_mpi_req(&testing_req);
  205. /* We wait for the test request to finish */
  206. pthread_mutex_lock(&testing_req.req_mutex);
  207. while (!testing_req.completed)
  208. pthread_cond_wait(&testing_req.req_cond, &testing_req.req_mutex);
  209. pthread_mutex_unlock(&testing_req.req_mutex);
  210. ret = testing_req.ret;
  211. }
  212. else {
  213. *flag = 0;
  214. }
  215. return ret;
  216. }
  217. /*
  218. * Requests
  219. */
  220. void handle_request_termination(struct starpu_mpi_req_s *req)
  221. {
  222. MPI_Type_free(&req->datatype);
  223. starpu_release_data_from_mem(req->data_handle);
  224. /* tell anyone potentiallly waiting on the request that it is
  225. * terminated now */
  226. pthread_mutex_lock(&req->req_mutex);
  227. req->completed = 1;
  228. pthread_cond_broadcast(&req->req_cond);
  229. pthread_mutex_unlock(&req->req_mutex);
  230. }
  231. void submit_mpi_req(void *arg)
  232. {
  233. struct starpu_mpi_req_s *req = arg;
  234. pthread_mutex_lock(&mutex);
  235. starpu_mpi_req_list_push_front(new_requests, req);
  236. pthread_cond_broadcast(&cond);
  237. pthread_mutex_unlock(&mutex);
  238. }
  239. /*
  240. * Progression loop
  241. */
  242. void handle_new_request(struct starpu_mpi_req_s *req)
  243. {
  244. STARPU_ASSERT(req);
  245. /* submit the request to MPI */
  246. req->func(req);
  247. }
  248. void *progress_thread_func(void *arg __attribute__((unused)))
  249. {
  250. /* notify the main thread that the progression thread is ready */
  251. pthread_mutex_lock(&mutex);
  252. running = 1;
  253. pthread_cond_signal(&cond);
  254. pthread_mutex_unlock(&mutex);
  255. pthread_mutex_lock(&mutex);
  256. while (running) {
  257. /* TODO test if there is some "detached request" and progress if this is the case */
  258. pthread_cond_wait(&cond, &mutex);
  259. if (!running)
  260. break;
  261. /* get one request */
  262. struct starpu_mpi_req_s *req;
  263. while (!starpu_mpi_req_list_empty(new_requests))
  264. {
  265. req = starpu_mpi_req_list_pop_back(new_requests);
  266. /* handling a request is likely to block for a while
  267. * (on a sync_data_with_mem call), we want to let the
  268. * application submit requests in the meantime, so we
  269. * release the lock. */
  270. pthread_mutex_unlock(&mutex);
  271. handle_new_request(req);
  272. pthread_mutex_lock(&mutex);
  273. }
  274. }
  275. pthread_mutex_unlock(&mutex);
  276. return NULL;
  277. }
  278. /*
  279. * (De)Initialization methods
  280. */
  281. int starpu_mpi_initialize(void)
  282. {
  283. pthread_mutex_init(&mutex, NULL);
  284. pthread_cond_init(&cond, NULL);
  285. new_requests = starpu_mpi_req_list_new();
  286. int ret = pthread_create(&progress_thread, NULL, progress_thread_func, NULL);
  287. pthread_mutex_lock(&mutex);
  288. if (!running)
  289. pthread_cond_wait(&cond, &mutex);
  290. pthread_mutex_unlock(&mutex);
  291. return 0;
  292. }
  293. int starpu_mpi_shutdown(void)
  294. {
  295. /* kill the progression thread */
  296. pthread_mutex_lock(&mutex);
  297. running = 0;
  298. pthread_cond_signal(&cond);
  299. pthread_mutex_unlock(&mutex);
  300. void *value;
  301. pthread_join(progress_thread, &value);
  302. /* liberate the request queues */
  303. starpu_mpi_req_list_delete(new_requests);
  304. return 0;
  305. }