starpu_mpi.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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(struct starpu_mpi_req_s *req);
  19. void handle_request_termination(struct starpu_mpi_req_s *req);
  20. static starpu_mpi_req_list_t new_requests;
  21. static starpu_mpi_req_list_t pending_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. static void _handle_new_mpi_isend(struct starpu_mpi_req_s *req)
  27. {
  28. //int rank;
  29. //MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  30. //fprintf(stdout, "Rank %d _handle_new_mpi_isend\n", rank);
  31. //fflush(stdout);
  32. void *ptr = starpu_mpi_handle_to_ptr(req->data_handle);
  33. starpu_mpi_handle_to_datatype(req->data_handle, &req->datatype);
  34. MPI_Isend(ptr, 1, req->datatype, req->dst, req->mpi_tag, req->comm, &req->request);
  35. }
  36. int starpu_mpi_isend(starpu_data_handle data_handle, struct starpu_mpi_req_s *req,
  37. int dest, int mpi_tag, MPI_Comm comm,
  38. void (*callback)(void *))
  39. {
  40. req->submitted = 0;
  41. pthread_mutex_init(&req->req_mutex, NULL);
  42. pthread_cond_init(&req->req_cond, NULL);
  43. req->data_handle = data_handle;
  44. req->dst = dest;
  45. req->mpi_tag = mpi_tag;
  46. req->comm = comm;
  47. req->mode = STARPU_R;
  48. req->handle_new = _handle_new_mpi_isend;
  49. submit_mpi_req(req);
  50. return 0;
  51. }
  52. static void _handle_new_mpi_irecv(struct starpu_mpi_req_s *req)
  53. {
  54. void *ptr = starpu_mpi_handle_to_ptr(req->data_handle);
  55. starpu_mpi_handle_to_datatype(req->data_handle, &req->datatype);
  56. MPI_Irecv(ptr, 1, req->datatype, req->src, req->mpi_tag, req->comm, &req->request);
  57. }
  58. /* NB: there is no status field here as we (may) return before the request is
  59. * actually transmitted to MPI. */
  60. int starpu_mpi_irecv(starpu_data_handle data_handle, struct starpu_mpi_req_s *req,
  61. int source, int mpi_tag, MPI_Comm comm,
  62. void (*callback)(void *))
  63. {
  64. req->submitted = 0;
  65. pthread_mutex_init(&req->req_mutex, NULL);
  66. pthread_cond_init(&req->req_cond, NULL);
  67. req->data_handle = data_handle;
  68. req->mode = STARPU_W;
  69. req->src = source;
  70. req->mpi_tag = mpi_tag;
  71. req->comm = comm;
  72. req->handle_new = _handle_new_mpi_irecv;
  73. submit_mpi_req(req);
  74. return 0;
  75. }
  76. int starpu_mpi_recv(starpu_data_handle data_handle,
  77. int source, int mpi_tag, MPI_Comm comm, MPI_Status *status)
  78. {
  79. /* test if we are blocking in a callback .. */
  80. int ret = starpu_sync_data_with_mem(data_handle, STARPU_W);
  81. if (ret)
  82. return ret;
  83. void *ptr = starpu_mpi_handle_to_ptr(data_handle);
  84. MPI_Datatype datatype;
  85. starpu_mpi_handle_to_datatype(data_handle, &datatype);
  86. MPI_Recv(ptr, 1, datatype, source, mpi_tag, comm, status);
  87. starpu_release_data_from_mem(data_handle);
  88. return 0;
  89. }
  90. int starpu_mpi_send(starpu_data_handle data_handle,
  91. int dest, int mpi_tag, MPI_Comm comm)
  92. {
  93. /* test if we are blocking in a callback .. */
  94. int ret = starpu_sync_data_with_mem(data_handle, STARPU_R);
  95. if (ret)
  96. return ret;
  97. void *ptr = starpu_mpi_handle_to_ptr(data_handle);
  98. MPI_Status status;
  99. MPI_Datatype datatype;
  100. starpu_mpi_handle_to_datatype(data_handle, &datatype);
  101. MPI_Send(ptr, 1, datatype, dest, mpi_tag, comm);
  102. starpu_release_data_from_mem(data_handle);
  103. return 0;
  104. }
  105. int starpu_mpi_wait(struct starpu_mpi_req_s *req, MPI_Status *status)
  106. {
  107. int ret;
  108. pthread_mutex_lock(&req->req_mutex);
  109. while (!req->submitted)
  110. pthread_cond_wait(&req->req_cond, &req->req_mutex);
  111. ret = MPI_Wait(&req->request, status);
  112. handle_request_termination(req);
  113. pthread_mutex_unlock(&req->req_mutex);
  114. return ret;
  115. }
  116. int starpu_mpi_test(struct starpu_mpi_req_s *req, int *flag, MPI_Status *status)
  117. {
  118. int ret = 0;
  119. pthread_mutex_lock(&req->req_mutex);
  120. if (req->submitted)
  121. {
  122. ret = MPI_Test(&req->request, flag, status);
  123. if (*flag)
  124. handle_request_termination(req);
  125. }
  126. else {
  127. *flag = 0;
  128. }
  129. pthread_mutex_unlock(&req->req_mutex);
  130. return ret;
  131. }
  132. /*
  133. * Requests
  134. */
  135. void handle_request_termination(struct starpu_mpi_req_s *req)
  136. {
  137. MPI_Type_free(&req->datatype);
  138. starpu_release_data_from_mem(req->data_handle);
  139. }
  140. void handle_request(struct starpu_mpi_req_s *req)
  141. {
  142. STARPU_ASSERT(req);
  143. pthread_mutex_lock(&req->req_mutex);
  144. starpu_sync_data_with_mem(req->data_handle, req->mode);
  145. /* submit the request to MPI */
  146. req->handle_new(req);
  147. /* perhaps somebody is waiting or trying to test */
  148. req->submitted = 1;
  149. pthread_cond_broadcast(&req->req_cond);
  150. pthread_mutex_unlock(&req->req_mutex);
  151. }
  152. static void submit_mpi_req(struct starpu_mpi_req_s *req)
  153. {
  154. pthread_mutex_lock(&mutex);
  155. pthread_mutex_lock(&req->req_mutex);
  156. starpu_mpi_req_list_push_front(new_requests, req);
  157. pthread_cond_broadcast(&cond);
  158. pthread_cond_broadcast(&req->req_cond);
  159. pthread_mutex_unlock(&req->req_mutex);
  160. pthread_mutex_unlock(&mutex);
  161. }
  162. /*
  163. * Progression loop
  164. */
  165. void *progress_thread_func(void *arg __attribute__((unused)))
  166. {
  167. /* notify the main thread that the progression thread is ready */
  168. pthread_mutex_lock(&mutex);
  169. running = 1;
  170. pthread_cond_signal(&cond);
  171. pthread_mutex_unlock(&mutex);
  172. pthread_mutex_lock(&mutex);
  173. while (running) {
  174. pthread_cond_wait(&cond, &mutex);
  175. if (!running)
  176. break;
  177. while (!starpu_mpi_req_list_empty(new_requests))
  178. {
  179. /* get one request */
  180. struct starpu_mpi_req_s *req;
  181. req = starpu_mpi_req_list_pop_back(new_requests);
  182. /* handling a request is likely to block for a while
  183. * (on a sync_data_with_mem call), we want to let the
  184. * application submit requests in the meantime, so we
  185. * release the lock. */
  186. pthread_mutex_unlock(&mutex);
  187. handle_request(req);
  188. pthread_mutex_lock(&mutex);
  189. }
  190. pthread_mutex_unlock(&mutex);
  191. }
  192. pthread_mutex_unlock(&mutex);
  193. return NULL;
  194. }
  195. /*
  196. * (De)Initialization methods
  197. */
  198. int starpu_mpi_initialize(void)
  199. {
  200. pthread_mutex_init(&mutex, NULL);
  201. pthread_cond_init(&cond, NULL);
  202. /* requests that have not be submitted to MPI yet */
  203. new_requests = starpu_mpi_req_list_new();
  204. /* requests that are already submitted and which are not completed yet */
  205. pending_requests = starpu_mpi_req_list_new();
  206. int ret = pthread_create(&progress_thread, NULL, progress_thread_func, NULL);
  207. pthread_mutex_lock(&mutex);
  208. if (!running)
  209. pthread_cond_wait(&cond, &mutex);
  210. pthread_mutex_unlock(&mutex);
  211. return 0;
  212. }
  213. int starpu_mpi_shutdown(void)
  214. {
  215. /* kill the progression thread */
  216. pthread_mutex_lock(&mutex);
  217. running = 0;
  218. pthread_cond_signal(&cond);
  219. pthread_mutex_unlock(&mutex);
  220. void *value;
  221. pthread_join(progress_thread, &value);
  222. return 0;
  223. }