starpu_mpi_insert_task.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2011 Université de Bordeaux 1
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdarg.h>
  18. #include <mpi.h>
  19. #include <starpu.h>
  20. #include <starpu_data.h>
  21. #include <common/utils.h>
  22. #include <common/hash.h>
  23. #include <common/htable32.h>
  24. #include <util/starpu_insert_task_utils.h>
  25. //#define STARPU_MPI_VERBOSE 1
  26. #include <starpu_mpi_private.h>
  27. /* Whether we are allowed to keep copies of remote data. Does not work
  28. * yet: the sender has to know whether the receiver has it, keeping it
  29. * in an array indexed by node numbers. */
  30. #define MPI_CACHE
  31. #ifdef MPI_CACHE
  32. static struct starpu_htbl32_node_s **sent_data = NULL;
  33. static struct starpu_htbl32_node_s **received_data = NULL;
  34. static void _starpu_mpi_task_init(int nb_nodes)
  35. {
  36. int i;
  37. _STARPU_MPI_DEBUG("Initialising hash table for cache\n");
  38. sent_data = malloc(nb_nodes * sizeof(struct starpu_htbl32_node_s *));
  39. for(i=0 ; i<nb_nodes ; i++) sent_data[i] = NULL;
  40. received_data = malloc(nb_nodes * sizeof(struct starpu_htbl32_node_s *));
  41. for(i=0 ; i<nb_nodes ; i++) received_data[i] = NULL;
  42. }
  43. typedef struct _starpu_mpi_clear_cache_s {
  44. starpu_data_handle data;
  45. int rank;
  46. int mode;
  47. } _starpu_mpi_clear_cache_t;
  48. #define _STARPU_MPI_CLEAR_SENT_DATA 0
  49. #define _STARPU_MPI_CLEAR_RECEIVED_DATA 1
  50. void _starpu_mpi_clear_cache_callback(void *callback_arg)
  51. {
  52. _starpu_mpi_clear_cache_t *clear_cache = (_starpu_mpi_clear_cache_t *)callback_arg;
  53. uint32_t key = _starpu_crc32_be((uintptr_t)clear_cache->data, 0);
  54. if (clear_cache->mode == _STARPU_MPI_CLEAR_SENT_DATA) {
  55. _STARPU_MPI_DEBUG("Clearing sent cache for data %p and rank %d\n", clear_cache->data, clear_cache->rank);
  56. _starpu_htbl_insert_32(&sent_data[clear_cache->rank], key, NULL);
  57. }
  58. else if (clear_cache->mode == _STARPU_MPI_CLEAR_RECEIVED_DATA) {
  59. _STARPU_MPI_DEBUG("Clearing received cache for data %p and rank %d\n", clear_cache->data, clear_cache->rank);
  60. _starpu_htbl_insert_32(&received_data[clear_cache->rank], key, NULL);
  61. }
  62. free(clear_cache);
  63. }
  64. void _starpu_mpi_clear_cache_request(starpu_data_handle data_handle, int rank, int mode)
  65. {
  66. struct starpu_task *task = starpu_task_create();
  67. task->cl = NULL;
  68. task->buffers[0].handle = data_handle;
  69. task->buffers[0].mode = STARPU_RW;
  70. _starpu_mpi_clear_cache_t *clear_cache = malloc(sizeof(_starpu_mpi_clear_cache_t));
  71. clear_cache->data = data_handle;
  72. clear_cache->rank = rank;
  73. clear_cache->mode = mode;
  74. task->callback_func = _starpu_mpi_clear_cache_callback;
  75. task->callback_arg = clear_cache;
  76. starpu_task_submit(task);
  77. }
  78. #endif
  79. void _starpu_data_deallocate(starpu_data_handle data_handle)
  80. {
  81. #ifdef STARPU_DEVEL
  82. #warning _starpu_data_deallocate not implemented yet
  83. #endif
  84. }
  85. int starpu_mpi_insert_task(MPI_Comm comm, starpu_codelet *codelet, ...)
  86. {
  87. int arg_type;
  88. va_list varg_list;
  89. int me, do_execute;
  90. size_t arg_buffer_size = 0;
  91. char *arg_buffer;
  92. int dest=0, execute, inconsistent_execute;
  93. int mpi_tag = 100;
  94. _STARPU_MPI_LOG_IN();
  95. MPI_Comm_rank(comm, &me);
  96. #ifdef MPI_CACHE
  97. if (sent_data == NULL) {
  98. int size;
  99. MPI_Comm_size(comm, &size);
  100. _starpu_mpi_task_init(size);
  101. }
  102. #endif
  103. /* Get the number of buffers and the size of the arguments */
  104. va_start(varg_list, codelet);
  105. arg_buffer_size = _starpu_insert_task_get_arg_size(varg_list);
  106. va_start(varg_list, codelet);
  107. _starpu_pack_cl_args(arg_buffer_size, &arg_buffer, varg_list);
  108. /* Finds out if the property STARPU_EXECUTE is specified */
  109. execute = -1;
  110. va_start(varg_list, codelet);
  111. while ((arg_type = va_arg(varg_list, int)) != 0) {
  112. if (arg_type==STARPU_EXECUTE) {
  113. execute = va_arg(varg_list, int);
  114. }
  115. else if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  116. va_arg(varg_list, starpu_data_handle);
  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. /* Find out whether we are to execute the data because we own the data to be written to. */
  133. inconsistent_execute = 0;
  134. do_execute = -1;
  135. va_start(varg_list, codelet);
  136. while ((arg_type = va_arg(varg_list, int)) != 0) {
  137. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  138. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  139. if (arg_type & STARPU_W) {
  140. if (!data) {
  141. /* We don't have anything allocated for this.
  142. * The application knows we won't do anything
  143. * about this task */
  144. /* Yes, the app could actually not call
  145. * insert_task at all itself, this is just a
  146. * safeguard. */
  147. _STARPU_MPI_DEBUG("oh oh\n");
  148. _STARPU_MPI_LOG_OUT();
  149. return -EINVAL;
  150. }
  151. int mpi_rank = starpu_data_get_rank(data);
  152. if (mpi_rank == me) {
  153. if (do_execute == 0) {
  154. inconsistent_execute = 1;
  155. }
  156. else {
  157. do_execute = 1;
  158. }
  159. }
  160. else if (mpi_rank != -1) {
  161. if (do_execute == 1) {
  162. inconsistent_execute = 1;
  163. }
  164. else {
  165. do_execute = 0;
  166. dest = mpi_rank;
  167. /* That's the rank which needs the data to be sent to */
  168. }
  169. }
  170. else {
  171. _STARPU_ERROR("rank invalid\n");
  172. }
  173. }
  174. }
  175. else if (arg_type==STARPU_VALUE) {
  176. va_arg(varg_list, void *);
  177. }
  178. else if (arg_type==STARPU_CALLBACK) {
  179. va_arg(varg_list, void (*)(void *));
  180. }
  181. else if (arg_type==STARPU_CALLBACK_ARG) {
  182. va_arg(varg_list, void *);
  183. }
  184. else if (arg_type==STARPU_PRIORITY) {
  185. va_arg(varg_list, int);
  186. }
  187. else if (arg_type==STARPU_EXECUTE) {
  188. va_arg(varg_list, int);
  189. }
  190. }
  191. va_end(varg_list);
  192. assert(do_execute != -1);
  193. if (inconsistent_execute == 1) {
  194. if (execute == -1) {
  195. _STARPU_MPI_DEBUG("Different tasks are owning W data. Needs to specify which one is to execute the codelet\n");
  196. return -EINVAL;
  197. }
  198. else {
  199. do_execute = (me == execute);
  200. dest = execute;
  201. }
  202. }
  203. else if (execute != -1) {
  204. _STARPU_MPI_DEBUG("Property STARPU_EXECUTE ignored as W data are all owned by the same task\n");
  205. }
  206. /* Send and receive data as requested */
  207. va_start(varg_list, codelet);
  208. while ((arg_type = va_arg(varg_list, int)) != 0) {
  209. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  210. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  211. if (data && arg_type & STARPU_R) {
  212. int mpi_rank = starpu_data_get_rank(data);
  213. /* The task needs to read this data */
  214. if (do_execute && mpi_rank != me && mpi_rank != -1) {
  215. /* I will have to execute but I don't have the data, receive */
  216. #ifdef MPI_CACHE
  217. uint32_t key = _starpu_crc32_be((uintptr_t)data, 0);
  218. void *already_received = _starpu_htbl_search_32(received_data[mpi_rank], key);
  219. if (!already_received) {
  220. _starpu_htbl_insert_32(&received_data[mpi_rank], key, data);
  221. }
  222. else {
  223. _STARPU_MPI_DEBUG("Do not receive data %p from node %d as it is already available\n", data, mpi_rank);
  224. }
  225. if (!already_received)
  226. #endif
  227. {
  228. _STARPU_MPI_DEBUG("Receive data %p from %d\n", data, mpi_rank);
  229. starpu_mpi_irecv_detached(data, mpi_rank, mpi_tag, comm, NULL, NULL);
  230. }
  231. }
  232. if (!do_execute && mpi_rank == me) {
  233. /* Somebody else will execute it, and I have the data, send it. */
  234. #ifdef MPI_CACHE
  235. uint32_t key = _starpu_crc32_be((uintptr_t)data, 0);
  236. void *already_sent = _starpu_htbl_search_32(sent_data[dest], key);
  237. if (!already_sent) {
  238. _starpu_htbl_insert_32(&sent_data[dest], key, data);
  239. }
  240. else {
  241. _STARPU_MPI_DEBUG("Do not sent data %p to node %d as it has already been sent\n", data, dest);
  242. }
  243. if (!already_sent)
  244. #endif
  245. {
  246. _STARPU_MPI_DEBUG("Send data %p to %d\n", data, dest);
  247. starpu_mpi_isend_detached(data, dest, mpi_tag, comm, NULL, NULL);
  248. }
  249. }
  250. mpi_tag++;
  251. }
  252. }
  253. else if (arg_type==STARPU_VALUE) {
  254. va_arg(varg_list, void *);
  255. }
  256. else if (arg_type==STARPU_CALLBACK) {
  257. va_arg(varg_list, void (*)(void *));
  258. }
  259. else if (arg_type==STARPU_CALLBACK_ARG) {
  260. va_arg(varg_list, void *);
  261. }
  262. else if (arg_type==STARPU_PRIORITY) {
  263. va_arg(varg_list, int);
  264. }
  265. else if (arg_type==STARPU_EXECUTE) {
  266. va_arg(varg_list, int);
  267. }
  268. }
  269. va_end(varg_list);
  270. if (do_execute) {
  271. _STARPU_MPI_DEBUG("Execution of the codelet %p\n", codelet);
  272. va_start(varg_list, codelet);
  273. struct starpu_task *task = starpu_task_create();
  274. int ret = _starpu_insert_task_create_and_submit(arg_buffer, codelet, &task, varg_list);
  275. _STARPU_MPI_DEBUG("ret: %d\n", ret);
  276. STARPU_ASSERT(ret==0);
  277. }
  278. if (inconsistent_execute) {
  279. va_start(varg_list, codelet);
  280. while ((arg_type = va_arg(varg_list, int)) != 0) {
  281. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  282. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  283. if (arg_type & STARPU_W) {
  284. int mpi_rank = starpu_data_get_rank(data);
  285. if (mpi_rank == me) {
  286. if (execute != -1 && me != execute) {
  287. _STARPU_MPI_DEBUG("Receive data %p back from the task %d which executed the codelet ...\n", data, dest);
  288. starpu_mpi_irecv_detached(data, dest, mpi_tag, comm, NULL, NULL);
  289. }
  290. }
  291. else if (do_execute) {
  292. _STARPU_MPI_DEBUG("Send data %p back to its owner %d...\n", data, mpi_rank);
  293. starpu_mpi_isend_detached(data, mpi_rank, mpi_tag, comm, NULL, NULL);
  294. }
  295. mpi_tag ++;
  296. }
  297. }
  298. else if (arg_type==STARPU_VALUE) {
  299. va_arg(varg_list, void *);
  300. }
  301. else if (arg_type==STARPU_CALLBACK) {
  302. va_arg(varg_list, void (*)(void *));
  303. }
  304. else if (arg_type==STARPU_CALLBACK_ARG) {
  305. va_arg(varg_list, void *);
  306. }
  307. else if (arg_type==STARPU_PRIORITY) {
  308. va_arg(varg_list, int);
  309. }
  310. else if (arg_type==STARPU_EXECUTE) {
  311. va_arg(varg_list, int);
  312. }
  313. }
  314. va_end(varg_list);
  315. }
  316. va_start(varg_list, codelet);
  317. while ((arg_type = va_arg(varg_list, int)) != 0) {
  318. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  319. starpu_data_handle data = va_arg(varg_list, starpu_data_handle);
  320. #ifdef MPI_CACHE
  321. if (arg_type & STARPU_W) {
  322. uint32_t key = _starpu_crc32_be((uintptr_t)data, 0);
  323. if (do_execute) {
  324. /* Note that all copies I've sent to neighbours are now invalid */
  325. int n, size;
  326. MPI_Comm_size(comm, &size);
  327. for(n=0 ; n<size ; n++) {
  328. void *already_sent = _starpu_htbl_search_32(sent_data[n], key);
  329. if (already_sent) {
  330. _STARPU_MPI_DEBUG("Posting request to clear send cache for data %p\n", data);
  331. _starpu_mpi_clear_cache_request(data, n, _STARPU_MPI_CLEAR_SENT_DATA);
  332. }
  333. }
  334. }
  335. else {
  336. int mpi_rank = starpu_data_get_rank(data);
  337. void *already_received = _starpu_htbl_search_32(received_data[mpi_rank], key);
  338. if (already_received) {
  339. /* Somebody else will write to the data, so discard our cached copy if any */
  340. /* TODO: starpu_mpi could just remember itself. */
  341. _STARPU_MPI_DEBUG("Posting request to clear receive cache for data %p\n", data);
  342. _starpu_mpi_clear_cache_request(data, mpi_rank, _STARPU_MPI_CLEAR_RECEIVED_DATA);
  343. _starpu_data_deallocate(data);
  344. }
  345. }
  346. }
  347. #else
  348. /* We allocated a temporary buffer for the received data, now drop it */
  349. if ((arg_type & STARPU_R) && do_execute) {
  350. int mpi_rank = starpu_data_get_rank(data);
  351. if (mpi_rank != me && mpi_rank != -1) {
  352. _starpu_data_deallocate(data);
  353. }
  354. }
  355. #endif
  356. }
  357. else if (arg_type==STARPU_VALUE) {
  358. va_arg(varg_list, void *);
  359. }
  360. else if (arg_type==STARPU_CALLBACK) {
  361. va_arg(varg_list, void (*)(void *));
  362. }
  363. else if (arg_type==STARPU_CALLBACK_ARG) {
  364. va_arg(varg_list, void *);
  365. }
  366. else if (arg_type==STARPU_PRIORITY) {
  367. va_arg(varg_list, int);
  368. }
  369. else if (arg_type==STARPU_EXECUTE) {
  370. va_arg(varg_list, int);
  371. }
  372. }
  373. va_end(varg_list);
  374. _STARPU_MPI_LOG_OUT();
  375. return 0;
  376. }
  377. void starpu_mpi_get_data_on_node(MPI_Comm comm, starpu_data_handle data_handle, int node)
  378. {
  379. int me, rank;
  380. rank = starpu_data_get_rank(data_handle);
  381. MPI_Comm_rank(comm, &me);
  382. if (node == rank) return;
  383. if (me == node)
  384. {
  385. starpu_mpi_irecv_detached(data_handle, rank, 42, comm, NULL, NULL);
  386. }
  387. else if (me == rank)
  388. {
  389. starpu_mpi_isend_detached(data_handle, node, 42, comm, NULL, NULL);
  390. }
  391. starpu_task_wait_for_all();
  392. }