starpu_mpi_insert_task.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <common/hash.h>
  22. #include <util/starpu_insert_task_utils.h>
  23. //#define STARPU_MPI_VERBOSE 1
  24. #include <starpu_mpi_private.h>
  25. /* Whether we are allowed to keep copies of remote data. Does not work
  26. * yet: the sender has to know whether the receiver has it, keeping it
  27. * in an array indexed by node numbers. */
  28. //#define MPI_CACHE
  29. #ifdef MPI_CACHE
  30. static struct starpu_htbl32_node_s **sent_data = NULL;
  31. static struct starpu_htbl32_node_s **received_data = NULL;
  32. static void _starpu_mpi_task_init(int nb_nodes) {
  33. int i;
  34. _STARPU_MPI_DEBUG("Initialising hash table for cache\n");
  35. sent_data = malloc(nb_nodes * sizeof(struct starpu_htbl32_node_s *));
  36. for(i=0 ; i<nb_nodes ; i++) sent_data[i] = NULL;
  37. received_data = malloc(nb_nodes * sizeof(struct starpu_htbl32_node_s *));
  38. for(i=0 ; i<nb_nodes ; i++) received_data[i] = NULL;
  39. }
  40. #endif
  41. int starpu_mpi_insert_task(MPI_Comm comm, starpu_codelet *codelet, ...) {
  42. int arg_type;
  43. va_list varg_list;
  44. int me, do_execute;
  45. size_t arg_buffer_size = 0;
  46. int dest;
  47. _STARPU_MPI_LOG_IN();
  48. MPI_Comm_rank(comm, &me);
  49. #ifdef MPI_CACHE
  50. if (sent_data == NULL) {
  51. int size;
  52. MPI_Comm_size(comm, &size);
  53. _starpu_mpi_task_init(size);
  54. }
  55. #endif
  56. /* Get the number of buffers and the size of the arguments */
  57. va_start(varg_list, codelet);
  58. arg_buffer_size = starpu_insert_task_get_arg_size(varg_list);
  59. /* Find out whether we are to execute the data because we own the data to be written to. */
  60. do_execute = -1;
  61. va_start(varg_list, codelet);
  62. while ((arg_type = va_arg(varg_list, int)) != 0) {
  63. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  64. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  65. if (arg_type & STARPU_W) {
  66. if (!data) {
  67. /* We don't have anything allocated for this.
  68. * The application knows we won't do anything
  69. * about this task */
  70. /* Yes, the app could actually not call
  71. * insert_task at all itself, this is just a
  72. * safeguard. */
  73. _STARPU_MPI_DEBUG("oh oh\n");
  74. _STARPU_MPI_LOG_OUT();
  75. return;
  76. }
  77. int mpi_rank = starpu_data_get_rank(data);
  78. if (mpi_rank == me) {
  79. if (do_execute == 0) {
  80. _STARPU_ERROR("erh? incoherent!\n");
  81. }
  82. else {
  83. do_execute = 1;
  84. }
  85. }
  86. else if (mpi_rank != -1) {
  87. if (do_execute == 1) {
  88. _STARPU_ERROR("erh? incoherent!\n");
  89. }
  90. else {
  91. do_execute = 0;
  92. dest = mpi_rank;
  93. /* That's the rank which needs the data to be sent to */
  94. }
  95. }
  96. }
  97. }
  98. else if (arg_type==STARPU_VALUE) {
  99. va_arg(varg_list, void *);
  100. }
  101. else if (arg_type==STARPU_CALLBACK) {
  102. va_arg(varg_list, void (*)(void *));
  103. }
  104. else if (arg_type==STARPU_CALLBACK_ARG) {
  105. va_arg(varg_list, void *);
  106. }
  107. else if (arg_type==STARPU_PRIORITY) {
  108. va_arg(varg_list, int);
  109. }
  110. }
  111. va_end(varg_list);
  112. assert(do_execute != -1);
  113. /* Send and receive data as requested */
  114. va_start(varg_list, codelet);
  115. while ((arg_type = va_arg(varg_list, int)) != 0) {
  116. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  117. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  118. if (arg_type & STARPU_R) {
  119. int mpi_rank = starpu_data_get_rank(data);
  120. /* The task needs to read this data */
  121. if (do_execute && mpi_rank != me && mpi_rank != -1) {
  122. /* I will have to execute but I don't have the data, receive */
  123. #ifdef MPI_CACHE
  124. uint32_t key = _starpu_crc32_be(data, 0);
  125. void *already_received = _starpu_htbl_search_32(received_data[mpi_rank], key);
  126. if (!already_received) {
  127. _starpu_htbl_insert_32(&received_data[mpi_rank], key, data);
  128. }
  129. else {
  130. _STARPU_MPI_DEBUG("Do not receive data %p from node %d as it is already available\n", data, mpi_rank);
  131. }
  132. if (!already_received)
  133. //if (!starpu_allocated(data))
  134. #endif
  135. {
  136. _STARPU_MPI_DEBUG("Receive data %p from %d\n", data, mpi_rank);
  137. starpu_mpi_irecv_detached(data, mpi_rank, 0, comm, NULL, NULL);
  138. }
  139. }
  140. if (!do_execute && mpi_rank == me) {
  141. /* Somebody else will execute it, and I have the data, send it. */
  142. /* FIXME CACHE: we need to know whether the receiver has it. */
  143. #ifdef MPI_CACHE
  144. uint32_t key = _starpu_crc32_be(data, 0);
  145. void *already_sent = _starpu_htbl_search_32(sent_data[dest], key);
  146. if (!already_sent) {
  147. _starpu_htbl_insert_32(&sent_data[dest], key, data);
  148. }
  149. else {
  150. _STARPU_MPI_DEBUG("Do not sent data %p to node %d as it has already been sent\n", data, dest);
  151. }
  152. if (!already_sent)
  153. #endif
  154. {
  155. _STARPU_MPI_DEBUG("Send data %p to %d\n", data, dest);
  156. starpu_mpi_isend_detached(data, dest, 0, comm, NULL, NULL);
  157. }
  158. }
  159. }
  160. }
  161. else if (arg_type==STARPU_VALUE) {
  162. va_arg(varg_list, void *);
  163. }
  164. else if (arg_type==STARPU_CALLBACK) {
  165. va_arg(varg_list, void (*)(void *));
  166. }
  167. else if (arg_type==STARPU_CALLBACK_ARG) {
  168. va_arg(varg_list, void *);
  169. }
  170. else if (arg_type==STARPU_PRIORITY) {
  171. va_arg(varg_list, int);
  172. }
  173. }
  174. va_end(varg_list);
  175. if (do_execute) {
  176. _STARPU_MPI_DEBUG("Execution of the codelet\n");
  177. va_start(varg_list, codelet);
  178. struct starpu_task *task = starpu_task_create();
  179. int ret = starpu_insert_task_create_and_submit(arg_buffer_size, codelet, &task, varg_list);
  180. _STARPU_MPI_DEBUG("ret: %d\n", ret);
  181. STARPU_ASSERT(ret==0);
  182. }
  183. /* No need to handle W, as we assume (and check) that task
  184. * write in data that they own */
  185. va_start(varg_list, codelet);
  186. while ((arg_type = va_arg(varg_list, int)) != 0) {
  187. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  188. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  189. #ifdef MPI_CACHE
  190. if (arg_type & STARPU_W) {
  191. if (do_execute) {
  192. /* FIXME: I need to note that all
  193. * copies I've sent to neighbours are
  194. * now invalid */
  195. }
  196. else {
  197. int mpi_rank = starpu_data_get_rank(data);
  198. uint32_t key = _starpu_crc32_be(data, 0);
  199. void *already_received = _starpu_htbl_search_32(received_data[mpi_rank], key);
  200. if (already_received) {
  201. /* Somebody else will write to the data, so discard our cached copy if any */
  202. /* TODO: starpu_mpi could just remember itself. */
  203. _STARPU_MPI_DEBUG("Clear cache for data %p\n", data);
  204. // if (starpu_data_allocated(data))
  205. #warning do a submit deallocate
  206. //starpu_data_deallocate(data);
  207. }
  208. }
  209. }
  210. #else
  211. /* We allocated a temporary buffer for the received data, now drop it */
  212. if ((arg_type & STARPU_R) && do_execute) {
  213. int mpi_rank = starpu_data_get_rank(data);
  214. if (mpi_rank != me && mpi_rank != -1) {
  215. //starpu_data_deallocate(data);
  216. }
  217. }
  218. #endif
  219. }
  220. else if (arg_type==STARPU_VALUE) {
  221. va_arg(varg_list, void *);
  222. }
  223. else if (arg_type==STARPU_CALLBACK) {
  224. va_arg(varg_list, void (*)(void *));
  225. }
  226. else if (arg_type==STARPU_CALLBACK_ARG) {
  227. va_arg(varg_list, void *);
  228. }
  229. else if (arg_type==STARPU_PRIORITY) {
  230. va_arg(varg_list, int);
  231. }
  232. }
  233. va_end(varg_list);
  234. _STARPU_MPI_LOG_OUT();
  235. }