starpu_mpi_insert_task.c 7.4 KB

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