starpu_mpi.c 6.7 KB

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