data_interface.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 <datawizard/datawizard.h>
  17. #include <core/dependencies/data_concurrency.h>
  18. /*
  19. * Start monitoring a piece of data
  20. */
  21. static void _starpu_register_new_data(starpu_data_handle handle,
  22. uint32_t home_node, uint32_t wt_mask)
  23. {
  24. STARPU_ASSERT(handle);
  25. /* initialize the new lock */
  26. handle->req_list = starpu_data_requester_list_new();
  27. handle->refcnt = 0;
  28. _starpu_spin_init(&handle->header_lock);
  29. /* first take care to properly lock the data */
  30. _starpu_spin_lock(&handle->header_lock);
  31. /* there is no hierarchy yet */
  32. handle->nchildren = 0;
  33. handle->root_handle = handle;
  34. handle->father_handle = NULL;
  35. handle->sibling_index = 0; /* could be anything for the root */
  36. handle->depth = 1; /* the tree is just a node yet */
  37. handle->is_not_important = 0;
  38. handle->sequential_consistency =
  39. starpu_data_get_default_sequential_consistency_flag();
  40. PTHREAD_MUTEX_INIT(&handle->sequential_consistency_mutex, NULL);
  41. handle->last_submitted_mode = STARPU_R;
  42. handle->last_submitted_writer = NULL;
  43. handle->last_submitted_readers = NULL;
  44. handle->post_sync_tasks = NULL;
  45. handle->post_sync_tasks_cnt = 0;
  46. /* By default, there are no methods available to perform a reduction */
  47. handle->redux_cl = NULL;
  48. handle->init_cl = NULL;
  49. handle->reduction_refcnt = 0;
  50. handle->reduction_req_list = starpu_data_requester_list_new();
  51. #ifdef STARPU_USE_FXT
  52. handle->last_submitted_ghost_writer_id_is_valid = 0;
  53. handle->last_submitted_ghost_writer_id = 0;
  54. handle->last_submitted_ghost_readers_id = NULL;
  55. #endif
  56. handle->wt_mask = wt_mask;
  57. /* Store some values directly in the handle not to recompute them all
  58. * the time. */
  59. handle->data_size = handle->ops->get_size(handle);
  60. handle->footprint = _starpu_compute_data_footprint(handle);
  61. handle->home_node = home_node;
  62. /* that new data is invalid from all nodes perpective except for the
  63. * home node */
  64. unsigned node;
  65. for (node = 0; node < STARPU_MAXNODES; node++)
  66. {
  67. struct starpu_data_replicate_s *replicate;
  68. replicate = &handle->per_node[node];
  69. replicate->memory_node = node;
  70. replicate->relaxed_coherency = 0;
  71. replicate->refcnt = 0;
  72. if (node == home_node) {
  73. /* this is the home node with the only valid copy */
  74. replicate->state = STARPU_OWNER;
  75. replicate->allocated = 1;
  76. replicate->automatically_allocated = 0;
  77. }
  78. else {
  79. /* the value is not available here yet */
  80. replicate->state = STARPU_INVALID;
  81. replicate->allocated = 0;
  82. }
  83. }
  84. unsigned worker;
  85. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  86. {
  87. struct starpu_data_replicate_s *replicate;
  88. replicate = &handle->per_worker[worker];
  89. replicate->allocated = 0;
  90. replicate->automatically_allocated = 0;
  91. replicate->state = STARPU_INVALID;
  92. replicate->refcnt = 0;
  93. replicate->handle = handle;
  94. replicate->requested = 0;
  95. replicate->request = NULL;
  96. replicate->relaxed_coherency = 1;
  97. replicate->initialized = 0;
  98. replicate->memory_node = starpu_worker_get_memory_node(worker);
  99. /* duplicate the content of the interface on node 0 */
  100. memcpy(replicate->interface, handle->per_node[0].interface, handle->ops->interface_size);
  101. }
  102. /* now the data is available ! */
  103. _starpu_spin_unlock(&handle->header_lock);
  104. }
  105. static starpu_data_handle _starpu_data_handle_allocate(struct starpu_data_interface_ops_t *interface_ops)
  106. {
  107. starpu_data_handle handle =
  108. calloc(1, sizeof(struct starpu_data_state_t));
  109. STARPU_ASSERT(handle);
  110. handle->ops = interface_ops;
  111. size_t interfacesize = interface_ops->interface_size;
  112. unsigned node;
  113. for (node = 0; node < STARPU_MAXNODES; node++)
  114. {
  115. struct starpu_data_replicate_s *replicate;
  116. replicate = &handle->per_node[node];
  117. /* relaxed_coherency = 0 */
  118. replicate->handle = handle;
  119. replicate->interface = calloc(1, interfacesize);
  120. STARPU_ASSERT(replicate->interface);
  121. }
  122. unsigned worker;
  123. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  124. {
  125. struct starpu_data_replicate_s *replicate;
  126. replicate = &handle->per_worker[worker];
  127. replicate->handle = handle;
  128. replicate->interface = calloc(1, interfacesize);
  129. STARPU_ASSERT(replicate->interface);
  130. }
  131. return handle;
  132. }
  133. void starpu_data_register(starpu_data_handle *handleptr, uint32_t home_node,
  134. void *interface,
  135. struct starpu_data_interface_ops_t *ops)
  136. {
  137. starpu_data_handle handle =
  138. _starpu_data_handle_allocate(ops);
  139. STARPU_ASSERT(handleptr);
  140. *handleptr = handle;
  141. /* fill the interface fields with the appropriate method */
  142. ops->register_data_handle(handle, home_node, interface);
  143. _starpu_register_new_data(handle, home_node, 0);
  144. }
  145. /*
  146. * Stop monitoring a piece of data
  147. */
  148. void _starpu_data_free_interfaces(starpu_data_handle handle)
  149. {
  150. unsigned node;
  151. for (node = 0; node < STARPU_MAXNODES; node++)
  152. free(handle->per_node[node].interface);
  153. }
  154. struct unregister_callback_arg {
  155. unsigned memory_node;
  156. starpu_data_handle handle;
  157. unsigned terminated;
  158. pthread_mutex_t mutex;
  159. pthread_cond_t cond;
  160. };
  161. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  162. {
  163. int ret;
  164. struct unregister_callback_arg *arg = _arg;
  165. starpu_data_handle handle = arg->handle;
  166. STARPU_ASSERT(handle);
  167. struct starpu_data_replicate_s *replicate = &handle->per_node[arg->memory_node];
  168. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, 0, NULL, NULL);
  169. STARPU_ASSERT(!ret);
  170. /* unlock the caller */
  171. PTHREAD_MUTEX_LOCK(&arg->mutex);
  172. arg->terminated = 1;
  173. PTHREAD_COND_SIGNAL(&arg->cond);
  174. PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  175. }
  176. /* Unregister the data handle, perhaps we don't need to update the home_node
  177. * (in that case coherent is set to 0) */
  178. static void _starpu_data_unregister(starpu_data_handle handle, unsigned coherent)
  179. {
  180. STARPU_ASSERT(handle);
  181. if (coherent)
  182. {
  183. /* If sequential consistency is enabled, wait until data is available */
  184. _starpu_data_wait_until_available(handle, STARPU_RW);
  185. /* Fetch data in the home of the data to ensure we have a valid copy
  186. * where we registered it */
  187. int home_node = handle->home_node;
  188. if (home_node >= 0)
  189. {
  190. struct unregister_callback_arg arg;
  191. arg.handle = handle;
  192. arg.memory_node = (unsigned)home_node;
  193. arg.terminated = 0;
  194. PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  195. PTHREAD_COND_INIT(&arg.cond, NULL);
  196. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  197. _starpu_data_unregister_fetch_data_callback, &arg))
  198. {
  199. /* no one has locked this data yet, so we proceed immediately */
  200. struct starpu_data_replicate_s *home_replicate = &handle->per_node[home_node];
  201. int ret = _starpu_fetch_data_on_node(handle, home_replicate, STARPU_R, 0, NULL, NULL);
  202. STARPU_ASSERT(!ret);
  203. }
  204. else {
  205. PTHREAD_MUTEX_LOCK(&arg.mutex);
  206. while (!arg.terminated)
  207. PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  208. PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  209. }
  210. }
  211. }
  212. else {
  213. /* Should we postpone the unregister operation ? */
  214. if ((handle->refcnt > 0) && handle->lazy_unregister)
  215. return;
  216. }
  217. _starpu_data_free_interfaces(handle);
  218. /* Destroy the data now */
  219. unsigned node;
  220. for (node = 0; node < STARPU_MAXNODES; node++)
  221. {
  222. struct starpu_data_replicate_s *local = &handle->per_node[node];
  223. if (local->allocated && local->automatically_allocated){
  224. /* free the data copy in a lazy fashion */
  225. _starpu_request_mem_chunk_removal(handle, node);
  226. }
  227. }
  228. starpu_data_requester_list_delete(handle->req_list);
  229. free(handle);
  230. }
  231. void starpu_data_unregister(starpu_data_handle handle)
  232. {
  233. _starpu_data_unregister(handle, 1);
  234. }
  235. void starpu_data_unregister_no_coherency(starpu_data_handle handle)
  236. {
  237. _starpu_data_unregister(handle, 0);
  238. }
  239. void starpu_data_invalidate(starpu_data_handle handle)
  240. {
  241. STARPU_ASSERT(handle);
  242. starpu_data_acquire(handle, STARPU_W);
  243. _starpu_spin_lock(&handle->header_lock);
  244. unsigned node;
  245. for (node = 0; node < STARPU_MAXNODES; node++)
  246. {
  247. struct starpu_data_replicate_s *local = &handle->per_node[node];
  248. if (local->allocated && local->automatically_allocated){
  249. /* free the data copy in a lazy fashion */
  250. _starpu_request_mem_chunk_removal(handle, node);
  251. }
  252. local->state = STARPU_INVALID;
  253. }
  254. _starpu_spin_unlock(&handle->header_lock);
  255. starpu_data_release(handle);
  256. }
  257. unsigned starpu_get_handle_interface_id(starpu_data_handle handle)
  258. {
  259. return handle->ops->interfaceid;
  260. }
  261. void *starpu_data_get_interface_on_node(starpu_data_handle handle, unsigned memory_node)
  262. {
  263. return handle->per_node[memory_node].interface;
  264. }