starpu_mpi.c 6.4 KB

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