starpu_mpi_insert_task.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 if (mpi_rank != -1) {
  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. else if (arg_type==STARPU_VALUE) {
  78. va_arg(varg_list, void *);
  79. }
  80. else if (arg_type==STARPU_CALLBACK) {
  81. va_arg(varg_list, void (*)(void *));
  82. }
  83. else if (arg_type==STARPU_CALLBACK_ARG) {
  84. va_arg(varg_list, void *);
  85. }
  86. else if (arg_type==STARPU_PRIORITY) {
  87. va_arg(varg_list, int);
  88. }
  89. }
  90. va_end(varg_list);
  91. assert(do_execute != -1);
  92. /* Send and receive data as requested */
  93. va_start(varg_list, codelet);
  94. while ((arg_type = va_arg(varg_list, int)) != 0) {
  95. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  96. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  97. if (arg_type & STARPU_R) {
  98. int mpi_rank = starpu_data_get_rank(data);
  99. /* The task needs to read this data */
  100. if (do_execute && mpi_rank != me && mpi_rank != -1) {
  101. _STARPU_MPI_DEBUG("Receive data from %d\n", mpi_rank);
  102. /* I will have to execute but I don't have the data, receive */
  103. #ifdef MPI_CACHE
  104. if (!starpu_allocated(data))
  105. #endif
  106. {
  107. starpu_mpi_irecv_detached(data, mpi_rank, 0, comm, NULL, NULL);
  108. }
  109. }
  110. if (!do_execute && mpi_rank == me) {
  111. /* Somebody else will execute it, and I have the data, send it. */
  112. /* FIXME CACHE: we need to know whether the receiver has it. */
  113. _STARPU_MPI_DEBUG("Send data to %d\n", dest);
  114. starpu_mpi_isend_detached(data, dest, 0, comm, NULL, NULL);
  115. }
  116. }
  117. }
  118. else if (arg_type==STARPU_VALUE) {
  119. va_arg(varg_list, void *);
  120. }
  121. else if (arg_type==STARPU_CALLBACK) {
  122. va_arg(varg_list, void (*)(void *));
  123. }
  124. else if (arg_type==STARPU_CALLBACK_ARG) {
  125. va_arg(varg_list, void *);
  126. }
  127. else if (arg_type==STARPU_PRIORITY) {
  128. va_arg(varg_list, int);
  129. }
  130. }
  131. va_end(varg_list);
  132. if (do_execute) {
  133. _STARPU_MPI_DEBUG("Execution of the codelet\n");
  134. va_start(varg_list, codelet);
  135. struct starpu_task *task = starpu_task_create();
  136. int ret = starpu_insert_task_create_and_submit(arg_buffer_size, codelet, &task, varg_list);
  137. _STARPU_MPI_DEBUG("ret: %d\n", ret);
  138. STARPU_ASSERT(ret==0);
  139. }
  140. /* No need to handle W, as we assume (and check) that task
  141. * write in data that they own */
  142. va_start(varg_list, codelet);
  143. while ((arg_type = va_arg(varg_list, int)) != 0) {
  144. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  145. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  146. #ifdef MPI_CACHE
  147. if (arg_type & STARPU_W) {
  148. if (do_execute) {
  149. /* FIXME: I need to note that all
  150. * copies I've sent to neighbours are
  151. * now invalid */
  152. }
  153. else {
  154. /* Somebody else will write to the data, so discard our cached copy if any */
  155. /* TODO: starpu_mpi could just remember itself. */
  156. if (starpu_allocated(data))
  157. starpu_deallocate(data);
  158. }
  159. }
  160. #else
  161. /* We allocated a temporary buffer for the received data, now drop it */
  162. if ((arg_type & STARPU_R) && do_execute) {
  163. int mpi_rank = starpu_data_get_rank(data);
  164. if (mpi_rank != me && mpi_rank != -1) {
  165. // starpu_deallocate(data);
  166. }
  167. }
  168. #endif
  169. }
  170. else if (arg_type==STARPU_VALUE) {
  171. va_arg(varg_list, void *);
  172. }
  173. else if (arg_type==STARPU_CALLBACK) {
  174. va_arg(varg_list, void (*)(void *));
  175. }
  176. else if (arg_type==STARPU_CALLBACK_ARG) {
  177. va_arg(varg_list, void *);
  178. }
  179. else if (arg_type==STARPU_PRIORITY) {
  180. va_arg(varg_list, int);
  181. }
  182. }
  183. va_end(varg_list);
  184. }