starpu_mpi_cache.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012, 2013, 2014 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2011-2014 Université de Bordeaux 1
  5. * Copyright (C) 2014 INRIA
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <common/uthash.h>
  20. #include <starpu_mpi_cache.h>
  21. #include <starpu_mpi_cache_stats.h>
  22. #include <starpu_mpi_private.h>
  23. /* Whether we are allowed to keep copies of remote data. */
  24. struct _starpu_data_entry
  25. {
  26. UT_hash_handle hh;
  27. starpu_data_handle_t data;
  28. };
  29. static struct _starpu_data_entry **_cache_sent_data = NULL;
  30. static struct _starpu_data_entry **_cache_received_data = NULL;
  31. int _cache_enabled=1;
  32. void _starpu_mpi_cache_init(MPI_Comm comm)
  33. {
  34. int nb_nodes;
  35. int i;
  36. _cache_enabled = starpu_get_env_number("STARPU_MPI_CACHE");
  37. if (_cache_enabled == -1)
  38. {
  39. _cache_enabled = 1;
  40. }
  41. if (_cache_enabled == 0)
  42. {
  43. if (!getenv("STARPU_SILENT")) fprintf(stderr,"Warning: StarPU MPI Communication cache is disabled\n");
  44. return;
  45. }
  46. MPI_Comm_size(comm, &nb_nodes);
  47. _STARPU_MPI_DEBUG(2, "Initialising htable for cache\n");
  48. _cache_sent_data = malloc(nb_nodes * sizeof(struct _starpu_data_entry *));
  49. for(i=0 ; i<nb_nodes ; i++) _cache_sent_data[i] = NULL;
  50. _cache_received_data = malloc(nb_nodes * sizeof(struct _starpu_data_entry *));
  51. for(i=0 ; i<nb_nodes ; i++) _cache_received_data[i] = NULL;
  52. _starpu_mpi_cache_stats_init(comm);
  53. }
  54. static
  55. void _starpu_mpi_cache_empty_tables(int world_size)
  56. {
  57. int i;
  58. if (_cache_enabled == 0) return;
  59. _STARPU_MPI_DEBUG(2, "Clearing htable for cache\n");
  60. for(i=0 ; i<world_size ; i++)
  61. {
  62. struct _starpu_data_entry *entry, *tmp;
  63. HASH_ITER(hh, _cache_sent_data[i], entry, tmp)
  64. {
  65. HASH_DEL(_cache_sent_data[i], entry);
  66. free(entry);
  67. }
  68. HASH_ITER(hh, _cache_received_data[i], entry, tmp)
  69. {
  70. HASH_DEL(_cache_received_data[i], entry);
  71. _starpu_mpi_cache_stats_dec(-1, i, entry->data);
  72. free(entry);
  73. }
  74. }
  75. }
  76. void _starpu_mpi_cache_free(int world_size)
  77. {
  78. if (_cache_enabled == 0) return;
  79. _starpu_mpi_cache_empty_tables(world_size);
  80. free(_cache_sent_data);
  81. free(_cache_received_data);
  82. _starpu_mpi_cache_stats_free();
  83. }
  84. void _starpu_mpi_cache_flush_sent(MPI_Comm comm, starpu_data_handle_t data)
  85. {
  86. int n, size;
  87. MPI_Comm_size(comm, &size);
  88. for(n=0 ; n<size ; n++)
  89. {
  90. struct _starpu_data_entry *already_sent;
  91. HASH_FIND_PTR(_cache_sent_data[n], &data, already_sent);
  92. if (already_sent)
  93. {
  94. _STARPU_MPI_DEBUG(2, "Clearing send cache for data %p\n", data);
  95. HASH_DEL(_cache_sent_data[n], already_sent);
  96. free(already_sent);
  97. }
  98. }
  99. }
  100. void _starpu_mpi_cache_flush_recv(starpu_data_handle_t data, int me)
  101. {
  102. int mpi_rank = starpu_data_get_rank(data);
  103. struct _starpu_data_entry *already_received;
  104. HASH_FIND_PTR(_cache_received_data[mpi_rank], &data, already_received);
  105. if (already_received)
  106. {
  107. #ifdef STARPU_DEVEL
  108. # warning TODO: Somebody else will write to the data, so discard our cached copy if any. starpu_mpi could just remember itself.
  109. #endif
  110. _STARPU_MPI_DEBUG(2, "Clearing receive cache for data %p\n", data);
  111. HASH_DEL(_cache_received_data[mpi_rank], already_received);
  112. _starpu_mpi_cache_stats_dec(me, mpi_rank, data);
  113. free(already_received);
  114. starpu_data_invalidate_submit(data);
  115. }
  116. }
  117. void starpu_mpi_cache_flush_all_data(MPI_Comm comm)
  118. {
  119. int nb_nodes, i;
  120. int mpi_rank, my_rank;
  121. if (_cache_enabled == 0) return;
  122. MPI_Comm_size(comm, &nb_nodes);
  123. MPI_Comm_rank(comm, &my_rank);
  124. for(i=0 ; i<nb_nodes ; i++)
  125. {
  126. struct _starpu_data_entry *entry, *tmp;
  127. HASH_ITER(hh, _cache_sent_data[i], entry, tmp)
  128. {
  129. mpi_rank = starpu_data_get_rank(entry->data);
  130. if (mpi_rank != my_rank && mpi_rank != -1)
  131. starpu_data_invalidate_submit(entry->data);
  132. HASH_DEL(_cache_sent_data[i], entry);
  133. free(entry);
  134. }
  135. HASH_ITER(hh, _cache_received_data[i], entry, tmp)
  136. {
  137. mpi_rank = starpu_data_get_rank(entry->data);
  138. if (mpi_rank != my_rank && mpi_rank != -1)
  139. starpu_data_invalidate_submit(entry->data);
  140. HASH_DEL(_cache_received_data[i], entry);
  141. _starpu_mpi_cache_stats_dec(my_rank, i, entry->data);
  142. free(entry);
  143. }
  144. }
  145. }
  146. void starpu_mpi_cache_flush(MPI_Comm comm, starpu_data_handle_t data_handle)
  147. {
  148. struct _starpu_data_entry *avail;
  149. int i, my_rank, nb_nodes;
  150. int mpi_rank;
  151. if (_cache_enabled == 0) return;
  152. MPI_Comm_size(comm, &nb_nodes);
  153. MPI_Comm_rank(comm, &my_rank);
  154. mpi_rank = starpu_data_get_rank(data_handle);
  155. for(i=0 ; i<nb_nodes ; i++)
  156. {
  157. HASH_FIND_PTR(_cache_sent_data[i], &data_handle, avail);
  158. if (avail)
  159. {
  160. _STARPU_MPI_DEBUG(2, "Clearing send cache for data %p\n", data_handle);
  161. HASH_DEL(_cache_sent_data[i], avail);
  162. free(avail);
  163. }
  164. HASH_FIND_PTR(_cache_received_data[i], &data_handle, avail);
  165. if (avail)
  166. {
  167. _STARPU_MPI_DEBUG(2, "Clearing send cache for data %p\n", data_handle);
  168. HASH_DEL(_cache_received_data[i], avail);
  169. _starpu_mpi_cache_stats_dec(my_rank, i, data_handle);
  170. free(avail);
  171. }
  172. }
  173. if (mpi_rank != my_rank && mpi_rank != -1)
  174. starpu_data_invalidate_submit(data_handle);
  175. }
  176. void *_starpu_mpi_already_received(int src, starpu_data_handle_t data, int mpi_rank)
  177. {
  178. if (_cache_enabled == 0) return NULL;
  179. struct _starpu_data_entry *already_received;
  180. HASH_FIND_PTR(_cache_received_data[mpi_rank], &data, already_received);
  181. if (already_received == NULL)
  182. {
  183. struct _starpu_data_entry *entry = (struct _starpu_data_entry *)malloc(sizeof(*entry));
  184. entry->data = data;
  185. HASH_ADD_PTR(_cache_received_data[mpi_rank], data, entry);
  186. _starpu_mpi_cache_stats_inc(src, mpi_rank, data);
  187. }
  188. else
  189. {
  190. _STARPU_MPI_DEBUG(2, "Do not receive data %p from node %d as it is already available\n", data, mpi_rank);
  191. }
  192. return already_received;
  193. }
  194. void *_starpu_mpi_already_sent(starpu_data_handle_t data, int dest)
  195. {
  196. if (_cache_enabled == 0) return NULL;
  197. struct _starpu_data_entry *already_sent;
  198. HASH_FIND_PTR(_cache_sent_data[dest], &data, already_sent);
  199. if (already_sent == NULL)
  200. {
  201. struct _starpu_data_entry *entry = (struct _starpu_data_entry *)malloc(sizeof(*entry));
  202. entry->data = data;
  203. HASH_ADD_PTR(_cache_sent_data[dest], data, entry);
  204. _STARPU_MPI_DEBUG(2, "Noting that data %p has already been sent to %d\n", data, dest);
  205. }
  206. else
  207. {
  208. _STARPU_MPI_DEBUG(2, "Do not send data %p to node %d as it has already been sent\n", data, dest);
  209. }
  210. return already_sent;
  211. }