starpu_mpi_cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  4. * Copyright (C) 2011-2017 Université de Bordeaux
  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 <datawizard/coherency.h>
  21. #include <starpu_mpi_cache.h>
  22. #include <starpu_mpi_cache_stats.h>
  23. #include <starpu_mpi_private.h>
  24. /* Whether we are allowed to keep copies of remote data. */
  25. struct _starpu_data_entry
  26. {
  27. UT_hash_handle hh;
  28. starpu_data_handle_t data_handle;
  29. };
  30. static starpu_pthread_mutex_t _cache_mutex;
  31. static struct _starpu_data_entry *_cache_data = NULL;
  32. int _starpu_cache_enabled=1;
  33. static MPI_Comm _starpu_cache_comm;
  34. static int _starpu_cache_comm_size;
  35. static void _starpu_mpi_cache_flush_nolock(starpu_data_handle_t data_handle);
  36. int starpu_mpi_cache_is_enabled()
  37. {
  38. return _starpu_cache_enabled==1;
  39. }
  40. int starpu_mpi_cache_set(int enabled)
  41. {
  42. if (enabled == 1)
  43. {
  44. _starpu_cache_enabled = 1;
  45. }
  46. else
  47. {
  48. if (_starpu_cache_enabled)
  49. {
  50. // We need to clean the cache
  51. starpu_mpi_cache_flush_all_data(_starpu_cache_comm);
  52. _starpu_mpi_cache_shutdown();
  53. }
  54. _starpu_cache_enabled = 0;
  55. }
  56. return 0;
  57. }
  58. void _starpu_mpi_cache_init(MPI_Comm comm)
  59. {
  60. _starpu_cache_enabled = starpu_get_env_number("STARPU_MPI_CACHE");
  61. if (_starpu_cache_enabled == -1)
  62. {
  63. _starpu_cache_enabled = 1;
  64. }
  65. if (_starpu_cache_enabled == 0)
  66. {
  67. _STARPU_DISP("Warning: StarPU MPI Communication cache is disabled\n");
  68. return;
  69. }
  70. _starpu_cache_comm = comm;
  71. starpu_mpi_comm_size(comm, &_starpu_cache_comm_size);
  72. _starpu_mpi_cache_stats_init();
  73. STARPU_PTHREAD_MUTEX_INIT(&_cache_mutex, NULL);
  74. }
  75. void _starpu_mpi_cache_shutdown()
  76. {
  77. if (_starpu_cache_enabled == 0) return;
  78. struct _starpu_data_entry *entry, *tmp;
  79. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  80. HASH_ITER(hh, _cache_data, entry, tmp)
  81. {
  82. HASH_DEL(_cache_data, entry);
  83. free(entry);
  84. }
  85. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  86. STARPU_PTHREAD_MUTEX_DESTROY(&_cache_mutex);
  87. free(_cache_data);
  88. _starpu_mpi_cache_stats_shutdown();
  89. }
  90. void _starpu_mpi_cache_data_clear(starpu_data_handle_t data_handle)
  91. {
  92. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  93. struct _starpu_data_entry *entry;
  94. if (_starpu_cache_enabled == 0) return;
  95. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  96. _starpu_mpi_cache_flush_nolock(data_handle);
  97. free(mpi_data->cache_sent);
  98. HASH_FIND_PTR(_cache_data, &data_handle, entry);
  99. if (entry != NULL)
  100. {
  101. HASH_DEL(_cache_data, entry);
  102. free(entry);
  103. }
  104. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  105. }
  106. void _starpu_mpi_cache_data_init(starpu_data_handle_t data_handle)
  107. {
  108. int i;
  109. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  110. if (_starpu_cache_enabled == 0) return;
  111. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  112. mpi_data->cache_received = 0;
  113. _STARPU_MALLOC(mpi_data->cache_sent, _starpu_cache_comm_size*sizeof(mpi_data->cache_sent[0]));
  114. for(i=0 ; i<_starpu_cache_comm_size ; i++)
  115. {
  116. mpi_data->cache_sent[i] = 0;
  117. }
  118. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  119. }
  120. static void _starpu_mpi_cache_data_add_nolock(starpu_data_handle_t data_handle)
  121. {
  122. struct _starpu_data_entry *entry;
  123. if (_starpu_cache_enabled == 0) return;
  124. HASH_FIND_PTR(_cache_data, &data_handle, entry);
  125. if (entry == NULL)
  126. {
  127. _STARPU_MPI_MALLOC(entry, sizeof(*entry));
  128. entry->data_handle = data_handle;
  129. HASH_ADD_PTR(_cache_data, data_handle, entry);
  130. }
  131. }
  132. static void _starpu_mpi_cache_data_remove_nolock(starpu_data_handle_t data_handle)
  133. {
  134. struct _starpu_data_entry *entry;
  135. if (_starpu_cache_enabled == 0) return;
  136. HASH_FIND_PTR(_cache_data, &data_handle, entry);
  137. if (entry)
  138. {
  139. HASH_DEL(_cache_data, entry);
  140. free(entry);
  141. }
  142. }
  143. /**************************************
  144. * Received cache
  145. **************************************/
  146. void _starpu_mpi_cache_received_data_clear(starpu_data_handle_t data_handle)
  147. {
  148. int mpi_rank = starpu_mpi_data_get_rank(data_handle);
  149. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  150. if (_starpu_cache_enabled == 0) return;
  151. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  152. STARPU_ASSERT(mpi_data->magic == 42);
  153. STARPU_MPI_ASSERT_MSG(mpi_rank < _starpu_cache_comm_size, "Node %d invalid. Max node is %d\n", mpi_rank, _starpu_cache_comm_size);
  154. if (mpi_data->cache_received == 1)
  155. {
  156. #ifdef STARPU_DEVEL
  157. # warning TODO: Somebody else will write to the data, so discard our cached copy if any. starpu_mpi could just remember itself.
  158. #endif
  159. _STARPU_MPI_DEBUG(2, "Clearing receive cache for data %p\n", data_handle);
  160. mpi_data->cache_received = 0;
  161. starpu_data_invalidate_submit(data_handle);
  162. _starpu_mpi_cache_data_remove_nolock(data_handle);
  163. _starpu_mpi_cache_stats_dec(mpi_rank, data_handle);
  164. }
  165. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  166. }
  167. int _starpu_mpi_cache_received_data_set(starpu_data_handle_t data_handle)
  168. {
  169. int mpi_rank = starpu_mpi_data_get_rank(data_handle);
  170. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  171. if (_starpu_cache_enabled == 0) return 0;
  172. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  173. STARPU_ASSERT(mpi_data->magic == 42);
  174. STARPU_MPI_ASSERT_MSG(mpi_rank < _starpu_cache_comm_size, "Node %d invalid. Max node is %d\n", mpi_rank, _starpu_cache_comm_size);
  175. int already_received = mpi_data->cache_received;
  176. if (already_received == 0)
  177. {
  178. _STARPU_MPI_DEBUG(2, "Noting that data %p has already been received by %d\n", data_handle, mpi_rank);
  179. mpi_data->cache_received = 1;
  180. _starpu_mpi_cache_data_add_nolock(data_handle);
  181. _starpu_mpi_cache_stats_inc(mpi_rank, data_handle);
  182. }
  183. else
  184. {
  185. _STARPU_MPI_DEBUG(2, "Do not receive data %p from node %d as it is already available\n", data_handle, mpi_rank);
  186. }
  187. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  188. return already_received;
  189. }
  190. int _starpu_mpi_cache_received_data_get(starpu_data_handle_t data_handle)
  191. {
  192. int already_received;
  193. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  194. if (_starpu_cache_enabled == 0) return 0;
  195. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  196. STARPU_ASSERT(mpi_data->magic == 42);
  197. already_received = mpi_data->cache_received;
  198. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  199. return already_received;
  200. }
  201. int starpu_mpi_cached_receive(starpu_data_handle_t data_handle)
  202. {
  203. return _starpu_mpi_cache_received_data_get(data_handle);
  204. }
  205. /**************************************
  206. * Send cache
  207. **************************************/
  208. void _starpu_mpi_cache_sent_data_clear(starpu_data_handle_t data_handle)
  209. {
  210. int n, size;
  211. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  212. if (_starpu_cache_enabled == 0) return;
  213. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  214. starpu_mpi_comm_size(mpi_data->node_tag.comm, &size);
  215. for(n=0 ; n<size ; n++)
  216. {
  217. if (mpi_data->cache_sent[n] == 1)
  218. {
  219. _STARPU_MPI_DEBUG(2, "Clearing send cache for data %p\n", data_handle);
  220. mpi_data->cache_sent[n] = 0;
  221. _starpu_mpi_cache_data_remove_nolock(data_handle);
  222. }
  223. }
  224. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  225. }
  226. int _starpu_mpi_cache_sent_data_set(starpu_data_handle_t data_handle, int dest)
  227. {
  228. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  229. if (_starpu_cache_enabled == 0) return 0;
  230. STARPU_MPI_ASSERT_MSG(dest < _starpu_cache_comm_size, "Node %d invalid. Max node is %d\n", dest, _starpu_cache_comm_size);
  231. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  232. int already_sent = mpi_data->cache_sent[dest];
  233. if (mpi_data->cache_sent[dest] == 0)
  234. {
  235. mpi_data->cache_sent[dest] = 1;
  236. _starpu_mpi_cache_data_add_nolock(data_handle);
  237. _STARPU_MPI_DEBUG(2, "Noting that data %p has already been sent to %d\n", data_handle, dest);
  238. }
  239. else
  240. {
  241. _STARPU_MPI_DEBUG(2, "Do not send data %p to node %d as it has already been sent\n", data_handle, dest);
  242. }
  243. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  244. return already_sent;
  245. }
  246. int _starpu_mpi_cache_sent_data_get(starpu_data_handle_t data_handle, int dest)
  247. {
  248. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  249. int already_sent;
  250. if (_starpu_cache_enabled == 0) return 0;
  251. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  252. STARPU_MPI_ASSERT_MSG(dest < _starpu_cache_comm_size, "Node %d invalid. Max node is %d\n", dest, _starpu_cache_comm_size);
  253. already_sent = mpi_data->cache_sent[dest];
  254. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  255. return already_sent;
  256. }
  257. int starpu_mpi_cached_send(starpu_data_handle_t data_handle, int dest)
  258. {
  259. return _starpu_mpi_cache_sent_data_get(data_handle, dest);
  260. }
  261. static void _starpu_mpi_cache_flush_nolock(starpu_data_handle_t data_handle)
  262. {
  263. struct _starpu_mpi_data *mpi_data = data_handle->mpi_data;
  264. int i, nb_nodes;
  265. if (_starpu_cache_enabled == 0) return;
  266. starpu_mpi_comm_size(mpi_data->node_tag.comm, &nb_nodes);
  267. for(i=0 ; i<nb_nodes ; i++)
  268. {
  269. if (mpi_data->cache_sent[i] == 1)
  270. {
  271. _STARPU_MPI_DEBUG(2, "Clearing send cache for data %p\n", data_handle);
  272. mpi_data->cache_sent[i] = 0;
  273. _starpu_mpi_cache_stats_dec(i, data_handle);
  274. }
  275. }
  276. if (mpi_data->cache_received == 1)
  277. {
  278. int mpi_rank = starpu_mpi_data_get_rank(data_handle);
  279. _STARPU_MPI_DEBUG(2, "Clearing received cache for data %p\n", data_handle);
  280. mpi_data->cache_received = 0;
  281. _starpu_mpi_cache_stats_dec(mpi_rank, data_handle);
  282. }
  283. }
  284. void _starpu_mpi_cache_flush(starpu_data_handle_t data_handle)
  285. {
  286. if (_starpu_cache_enabled == 0) return;
  287. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  288. _starpu_mpi_cache_flush_nolock(data_handle);
  289. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  290. }
  291. static void _starpu_mpi_cache_flush_and_invalidate_nolock(MPI_Comm comm, starpu_data_handle_t data_handle)
  292. {
  293. int my_rank, mpi_rank;
  294. _starpu_mpi_cache_flush_nolock(data_handle);
  295. starpu_mpi_comm_rank(comm, &my_rank);
  296. mpi_rank = starpu_mpi_data_get_rank(data_handle);
  297. if (mpi_rank != my_rank && mpi_rank != -1)
  298. starpu_data_invalidate_submit(data_handle);
  299. }
  300. void starpu_mpi_cache_flush(MPI_Comm comm, starpu_data_handle_t data_handle)
  301. {
  302. if (_starpu_cache_enabled == 0) return;
  303. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  304. _starpu_mpi_cache_flush_and_invalidate_nolock(comm, data_handle);
  305. _starpu_mpi_cache_data_remove_nolock(data_handle);
  306. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  307. }
  308. void starpu_mpi_cache_flush_all_data(MPI_Comm comm)
  309. {
  310. struct _starpu_data_entry *entry, *tmp;
  311. if (_starpu_cache_enabled == 0) return;
  312. STARPU_PTHREAD_MUTEX_LOCK(&_cache_mutex);
  313. HASH_ITER(hh, _cache_data, entry, tmp)
  314. {
  315. _starpu_mpi_cache_flush_and_invalidate_nolock(comm, entry->data_handle);
  316. HASH_DEL(_cache_data, entry);
  317. free(entry);
  318. }
  319. STARPU_PTHREAD_MUTEX_UNLOCK(&_cache_mutex);
  320. }