starpu_mpi_insert_task.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (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 <stdarg.h>
  17. #include <mpi.h>
  18. #include <starpu.h>
  19. #include <starpu_data.h>
  20. #include <common/utils.h>
  21. #include <util/starpu_insert_task_utils.h>
  22. #define STARPU_MPI_VERBOSE 1
  23. #include <starpu_mpi_private.h>
  24. /* Whether we are allowed to keep copies of remote data. Does not work
  25. * yet: the sender has to know whether the receiver has it, keeping it
  26. * in an array indexed by node numbers. */
  27. //#define MPI_CACHE
  28. int starpu_mpi_insert_task(MPI_Comm comm, starpu_codelet *codelet, ...) {
  29. int arg_type;
  30. va_list varg_list;
  31. int me, do_execute;
  32. size_t arg_buffer_size = 0;
  33. int nb_buffers;
  34. int dest;
  35. MPI_Comm_rank(comm, &me);
  36. /* Get the number of buffers and the size of the arguments */
  37. va_start(varg_list, codelet);
  38. starpu_insert_task_get_sizes(&arg_buffer_size, &nb_buffers, varg_list);
  39. /* Find out whether we are to execute the data because we own the data to be written to. */
  40. do_execute = -1;
  41. va_start(varg_list, codelet);
  42. while ((arg_type = va_arg(varg_list, int)) != 0) {
  43. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  44. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  45. if (arg_type & STARPU_W) {
  46. if (!data) {
  47. /* We don't have anything allocated for this.
  48. * The application knows we won't do anything
  49. * about this task */
  50. /* Yes, the app could actually not call
  51. * insert_task at all itself, this is just a
  52. * safeguard. */
  53. _STARPU_MPI_DEBUG("oh oh\n");
  54. return;
  55. }
  56. int mpi_rank = starpu_data_get_rank(data);
  57. if (mpi_rank == me) {
  58. if (do_execute == 0) {
  59. _STARPU_ERROR("erh? incoherent!\n");
  60. }
  61. else {
  62. do_execute = 1;
  63. }
  64. }
  65. else {
  66. if (do_execute == 1) {
  67. _STARPU_ERROR("erh? incoherent!\n");
  68. }
  69. else {
  70. do_execute = 0;
  71. dest = mpi_rank;
  72. /* That's the rank which needs the data to be sent to */
  73. }
  74. }
  75. }
  76. }
  77. }
  78. va_end(varg_list);
  79. assert(do_execute != -1);
  80. starpu_mpi_req *req = malloc(nb_buffers * sizeof(starpu_mpi_req));
  81. int nb_req=0;
  82. /* Send and receive data as requested */
  83. va_start(varg_list, codelet);
  84. while ((arg_type = va_arg(varg_list, int)) != 0) {
  85. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  86. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  87. if (arg_type & STARPU_R) {
  88. int mpi_rank = starpu_data_get_rank(data);
  89. /* The task needs to read this data */
  90. if (do_execute && mpi_rank != me) {
  91. _STARPU_MPI_DEBUG("Receive data from %d\n", mpi_rank);
  92. /* I will have to execute but I don't have the data, receive */
  93. #ifdef MPI_CACHE
  94. if (!starpu_allocated(data))
  95. #endif
  96. {
  97. starpu_mpi_irecv(data, &req[nb_req], mpi_rank, 0, comm);
  98. nb_req++;
  99. }
  100. }
  101. if (!do_execute && mpi_rank == me) {
  102. /* Somebody else will execute it, and I have the data, send it. */
  103. /* FIXME: we need to know whether the receiver has it. */
  104. _STARPU_MPI_DEBUG("Send data to %d\n", dest);
  105. starpu_mpi_isend(data, &req[nb_req], dest, 0, comm);
  106. nb_req++;
  107. }
  108. }
  109. }
  110. }
  111. va_end(varg_list);
  112. /* If some MPI communications have been posted, wait until they are finished */
  113. _STARPU_MPI_DEBUG("Waiting for %d request(s)\n", nb_req);
  114. int nb_waiting_requests=nb_req;
  115. while(nb_waiting_requests) {
  116. //_STARPU_MPI_DEBUG("Testing %d request(s)\n", nb_waiting_requests);
  117. int r=0;
  118. for(r=0 ; r<nb_req ; r++) {
  119. if (req[r]) {
  120. int finished = 0;
  121. MPI_Status status;
  122. //_STARPU_MPI_DEBUG("Testing request %d\n", r);
  123. starpu_mpi_test(&req[r], &finished, &status);
  124. STARPU_ASSERT(finished != -1);
  125. if(finished) {
  126. req[r] = NULL;
  127. nb_waiting_requests--;
  128. }
  129. }
  130. }
  131. }
  132. _STARPU_MPI_DEBUG("All requests processed\n", nb_req);
  133. free(req);
  134. if (do_execute) {
  135. _STARPU_MPI_DEBUG("Execution of the codelet\n");
  136. va_start(varg_list, codelet);
  137. struct starpu_task *task = starpu_task_create();
  138. task->synchronous = 1;
  139. int ret = starpu_insert_task_create_and_submit(arg_buffer_size, codelet, &task, varg_list);
  140. _STARPU_MPI_DEBUG("ret: %d\n", ret);
  141. STARPU_ASSERT(ret==0);
  142. // ret = starpu_task_wait(task);
  143. // _STARPU_MPI_DEBUG("ret: %d\n", ret);
  144. // STARPU_ASSERT(ret==0);
  145. }
  146. /* No need to handle W, as we assume (and check) that task
  147. * write in data that they own */
  148. va_start(varg_list, codelet);
  149. while ((arg_type = va_arg(varg_list, int)) != 0) {
  150. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  151. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  152. #ifdef MPI_CACHE
  153. if (arg_type & STARPU_W) {
  154. if (do_execute) {
  155. /* FIXME: I need to note that all
  156. * copies I've sent to neighbours are
  157. * now invalid */
  158. }
  159. else {
  160. /* Somebody else will write to the data, so discard our cached copy if any */
  161. /* TODO: starpu_mpi could just remember itself. */
  162. if (starpu_allocated(data))
  163. starpu_deallocate(data);
  164. }
  165. }
  166. #else
  167. /* We allocated a temporary buffer for the received data, now drop it */
  168. if ((arg_type & STARPU_R) && do_execute) {
  169. int mpi_rank = starpu_data_get_rank(data);
  170. if (mpi_rank != me) {
  171. // starpu_deallocate(data);
  172. }
  173. }
  174. #endif
  175. }
  176. }
  177. va_end(varg_list);
  178. }