starpu_mpi_insert_task.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 dest;
  34. MPI_Comm_rank(comm, &me);
  35. /* Get the number of buffers and the size of the arguments */
  36. va_start(varg_list, codelet);
  37. arg_buffer_size = starpu_insert_task_get_arg_size(varg_list);
  38. /* Find out whether we are to execute the data because we own the data to be written to. */
  39. do_execute = -1;
  40. va_start(varg_list, codelet);
  41. while ((arg_type = va_arg(varg_list, int)) != 0) {
  42. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  43. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  44. if (arg_type & STARPU_W) {
  45. if (!data) {
  46. /* We don't have anything allocated for this.
  47. * The application knows we won't do anything
  48. * about this task */
  49. /* Yes, the app could actually not call
  50. * insert_task at all itself, this is just a
  51. * safeguard. */
  52. _STARPU_MPI_DEBUG("oh oh\n");
  53. return;
  54. }
  55. int mpi_rank = starpu_data_get_rank(data);
  56. if (mpi_rank == me) {
  57. if (do_execute == 0) {
  58. _STARPU_ERROR("erh? incoherent!\n");
  59. }
  60. else {
  61. do_execute = 1;
  62. }
  63. }
  64. else if (mpi_rank != -1) {
  65. if (do_execute == 1) {
  66. _STARPU_ERROR("erh? incoherent!\n");
  67. }
  68. else {
  69. do_execute = 0;
  70. dest = mpi_rank;
  71. /* That's the rank which needs the data to be sent to */
  72. }
  73. }
  74. }
  75. }
  76. else if (arg_type==STARPU_VALUE) {
  77. va_arg(varg_list, void *);
  78. }
  79. else if (arg_type==STARPU_CALLBACK) {
  80. va_arg(varg_list, void (*)(void *));
  81. }
  82. else if (arg_type==STARPU_CALLBACK_ARG) {
  83. va_arg(varg_list, void *);
  84. }
  85. else if (arg_type==STARPU_PRIORITY) {
  86. va_arg(varg_list, int);
  87. }
  88. }
  89. va_end(varg_list);
  90. assert(do_execute != -1);
  91. /* Send and receive data as requested */
  92. va_start(varg_list, codelet);
  93. while ((arg_type = va_arg(varg_list, int)) != 0) {
  94. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  95. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  96. if (arg_type & STARPU_R) {
  97. int mpi_rank = starpu_data_get_rank(data);
  98. /* The task needs to read this data */
  99. if (do_execute && mpi_rank != me && mpi_rank != -1) {
  100. _STARPU_MPI_DEBUG("Receive data from %d\n", mpi_rank);
  101. /* I will have to execute but I don't have the data, receive */
  102. #ifdef MPI_CACHE
  103. if (!starpu_allocated(data))
  104. #endif
  105. {
  106. starpu_mpi_irecv_detached(data, mpi_rank, 0, comm, NULL, NULL);
  107. }
  108. }
  109. if (!do_execute && mpi_rank == me) {
  110. /* Somebody else will execute it, and I have the data, send it. */
  111. /* FIXME CACHE: we need to know whether the receiver has it. */
  112. _STARPU_MPI_DEBUG("Send data to %d\n", dest);
  113. starpu_mpi_isend_detached(data, dest, 0, comm, NULL, NULL);
  114. }
  115. }
  116. }
  117. else if (arg_type==STARPU_VALUE) {
  118. va_arg(varg_list, void *);
  119. }
  120. else if (arg_type==STARPU_CALLBACK) {
  121. va_arg(varg_list, void (*)(void *));
  122. }
  123. else if (arg_type==STARPU_CALLBACK_ARG) {
  124. va_arg(varg_list, void *);
  125. }
  126. else if (arg_type==STARPU_PRIORITY) {
  127. va_arg(varg_list, int);
  128. }
  129. }
  130. va_end(varg_list);
  131. if (do_execute) {
  132. _STARPU_MPI_DEBUG("Execution of the codelet\n");
  133. va_start(varg_list, codelet);
  134. struct starpu_task *task = starpu_task_create();
  135. int ret = starpu_insert_task_create_and_submit(arg_buffer_size, codelet, &task, varg_list);
  136. _STARPU_MPI_DEBUG("ret: %d\n", ret);
  137. STARPU_ASSERT(ret==0);
  138. }
  139. /* No need to handle W, as we assume (and check) that task
  140. * write in data that they own */
  141. va_start(varg_list, codelet);
  142. while ((arg_type = va_arg(varg_list, int)) != 0) {
  143. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  144. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  145. #ifdef MPI_CACHE
  146. if (arg_type & STARPU_W) {
  147. if (do_execute) {
  148. /* FIXME: I need to note that all
  149. * copies I've sent to neighbours are
  150. * now invalid */
  151. }
  152. else {
  153. /* Somebody else will write to the data, so discard our cached copy if any */
  154. /* TODO: starpu_mpi could just remember itself. */
  155. if (starpu_allocated(data))
  156. starpu_deallocate(data);
  157. }
  158. }
  159. #else
  160. /* We allocated a temporary buffer for the received data, now drop it */
  161. if ((arg_type & STARPU_R) && do_execute) {
  162. int mpi_rank = starpu_data_get_rank(data);
  163. if (mpi_rank != me && mpi_rank != -1) {
  164. // starpu_deallocate(data);
  165. }
  166. }
  167. #endif
  168. }
  169. else if (arg_type==STARPU_VALUE) {
  170. va_arg(varg_list, void *);
  171. }
  172. else if (arg_type==STARPU_CALLBACK) {
  173. va_arg(varg_list, void (*)(void *));
  174. }
  175. else if (arg_type==STARPU_CALLBACK_ARG) {
  176. va_arg(varg_list, void *);
  177. }
  178. else if (arg_type==STARPU_PRIORITY) {
  179. va_arg(varg_list, int);
  180. }
  181. }
  182. va_end(varg_list);
  183. }