user_interactions.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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 <common/config.h>
  17. #include <common/utils.h>
  18. #include <datawizard/coherency.h>
  19. #include <datawizard/copy_driver.h>
  20. #include <datawizard/write_back.h>
  21. #include <core/dependencies/data_concurrency.h>
  22. /* Explicitly ask StarPU to allocate room for a piece of data on the specified
  23. * memory node. */
  24. int starpu_data_request_allocation(starpu_data_handle handle, uint32_t node)
  25. {
  26. starpu_data_request_t r;
  27. STARPU_ASSERT(handle);
  28. r = _starpu_create_data_request(handle, 0, node, node, 0, 0, 1);
  29. /* we do not increase the refcnt associated to the request since we are
  30. * not waiting for its termination */
  31. _starpu_post_data_request(r, node);
  32. return 0;
  33. }
  34. struct state_and_node {
  35. starpu_data_handle state;
  36. starpu_access_mode mode;
  37. unsigned node;
  38. pthread_cond_t cond;
  39. pthread_mutex_t lock;
  40. unsigned finished;
  41. unsigned async;
  42. void (*callback)(void *);
  43. void *callback_arg;
  44. struct starpu_task *pre_sync_task;
  45. struct starpu_task *post_sync_task;
  46. };
  47. /*
  48. * Non Blocking data request from application
  49. */
  50. /* put the current value of the data into RAM */
  51. static inline void _starpu_sync_data_with_mem_continuation_non_blocking(void *arg)
  52. {
  53. int ret;
  54. struct state_and_node *statenode = arg;
  55. starpu_data_handle handle = statenode->state;
  56. STARPU_ASSERT(handle);
  57. unsigned r = (statenode->mode & STARPU_R);
  58. unsigned w = (statenode->mode & STARPU_W);
  59. ret = _starpu_fetch_data_on_node(handle, 0, r, w, 0);
  60. STARPU_ASSERT(!ret);
  61. /* continuation of starpu_data_sync_with_mem_non_blocking: we
  62. * execute the callback if any */
  63. if (statenode->callback)
  64. statenode->callback(statenode->callback_arg);
  65. free(statenode);
  66. }
  67. /* The data must be released by calling starpu_data_release_from_mem later on */
  68. int starpu_data_sync_with_mem_non_blocking(starpu_data_handle handle,
  69. starpu_access_mode mode, void (*callback)(void *), void *arg)
  70. {
  71. STARPU_ASSERT(handle);
  72. struct state_and_node *statenode = malloc(sizeof(struct state_and_node));
  73. STARPU_ASSERT(statenode);
  74. statenode->state = handle;
  75. statenode->mode = mode;
  76. statenode->callback = callback;
  77. statenode->callback_arg = arg;
  78. PTHREAD_COND_INIT(&statenode->cond, NULL);
  79. PTHREAD_MUTEX_INIT(&statenode->lock, NULL);
  80. statenode->finished = 0;
  81. /* we try to get the data, if we do not succeed immediately, we set a
  82. * callback function that will be executed automatically when the data is
  83. * available again, otherwise we fetch the data directly */
  84. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode,
  85. _starpu_sync_data_with_mem_continuation_non_blocking, statenode))
  86. {
  87. /* no one has locked this data yet, so we proceed immediately */
  88. _starpu_sync_data_with_mem_continuation_non_blocking(statenode);
  89. }
  90. #warning TODO fix sequential consistency !
  91. /* XXX this is a temporary hack to have the starpu_sync_data_with_mem
  92. * function working properly. It should be fixed later on. */
  93. PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  94. if (handle->sequential_consistency)
  95. {
  96. handle->post_sync_tasks_cnt++;
  97. }
  98. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  99. return 0;
  100. }
  101. /*
  102. * Block data request from application
  103. */
  104. static inline void _starpu_sync_data_with_mem_continuation(void *arg)
  105. {
  106. int ret;
  107. struct state_and_node *statenode = arg;
  108. starpu_data_handle handle = statenode->state;
  109. STARPU_ASSERT(handle);
  110. unsigned r = (statenode->mode & STARPU_R);
  111. unsigned w = (statenode->mode & STARPU_W);
  112. ret = _starpu_fetch_data_on_node(handle, 0, r, w, 0);
  113. STARPU_ASSERT(!ret);
  114. /* continuation of starpu_data_sync_with_mem */
  115. PTHREAD_MUTEX_LOCK(&statenode->lock);
  116. statenode->finished = 1;
  117. PTHREAD_COND_SIGNAL(&statenode->cond);
  118. PTHREAD_MUTEX_UNLOCK(&statenode->lock);
  119. }
  120. /* The data must be released by calling starpu_data_release_from_mem later on */
  121. int starpu_data_sync_with_mem(starpu_data_handle handle, starpu_access_mode mode)
  122. {
  123. STARPU_ASSERT(handle);
  124. /* it is forbidden to call this function from a callback or a codelet */
  125. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  126. return -EDEADLK;
  127. struct state_and_node statenode =
  128. {
  129. .state = handle,
  130. .mode = mode,
  131. .node = 0, // unused
  132. .cond = PTHREAD_COND_INITIALIZER,
  133. .lock = PTHREAD_MUTEX_INITIALIZER,
  134. .finished = 0
  135. };
  136. // fprintf(stderr, "TAKE sequential_consistency_mutex starpu_data_sync_with_mem\n");
  137. PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  138. int sequential_consistency = handle->sequential_consistency;
  139. if (sequential_consistency)
  140. {
  141. statenode.pre_sync_task = starpu_task_create();
  142. statenode.pre_sync_task->detach = 0;
  143. statenode.post_sync_task = starpu_task_create();
  144. statenode.post_sync_task->detach = 1;
  145. _starpu_detect_implicit_data_deps_with_handle(statenode.pre_sync_task, statenode.post_sync_task, handle, mode);
  146. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  147. /* TODO detect if this is superflous */
  148. statenode.pre_sync_task->synchronous = 1;
  149. int ret = starpu_task_submit(statenode.pre_sync_task);
  150. STARPU_ASSERT(!ret);
  151. //starpu_task_wait(statenode.pre_sync_task);
  152. }
  153. else {
  154. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  155. }
  156. /* we try to get the data, if we do not succeed immediately, we set a
  157. * callback function that will be executed automatically when the data is
  158. * available again, otherwise we fetch the data directly */
  159. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode,
  160. _starpu_sync_data_with_mem_continuation, &statenode))
  161. {
  162. /* no one has locked this data yet, so we proceed immediately */
  163. unsigned r = (mode & STARPU_R);
  164. unsigned w = (mode & STARPU_W);
  165. int ret = _starpu_fetch_data_on_node(handle, 0, r, w, 0);
  166. STARPU_ASSERT(!ret);
  167. }
  168. else {
  169. PTHREAD_MUTEX_LOCK(&statenode.lock);
  170. while (!statenode.finished)
  171. PTHREAD_COND_WAIT(&statenode.cond, &statenode.lock);
  172. PTHREAD_MUTEX_UNLOCK(&statenode.lock);
  173. }
  174. /* At that moment, the caller holds a reference to the piece of data.
  175. * We enqueue the "post" sync task in the list associated to the handle
  176. * so that it is submitted by the starpu_data_release_from_mem
  177. * function. */
  178. _starpu_add_post_sync_tasks(statenode.post_sync_task, handle);
  179. return 0;
  180. }
  181. /* This function must be called after starpu_data_sync_with_mem so that the
  182. * application release the data */
  183. void starpu_data_release_from_mem(starpu_data_handle handle)
  184. {
  185. STARPU_ASSERT(handle);
  186. /* The application can now release the rw-lock */
  187. _starpu_release_data_on_node(handle, 0, 0);
  188. /* In case there are some implicit dependencies, unlock the "post sync" tasks */
  189. _starpu_unlock_post_sync_tasks(handle);
  190. }
  191. static void _prefetch_data_on_node(void *arg)
  192. {
  193. struct state_and_node *statenode = arg;
  194. _starpu_fetch_data_on_node(statenode->state, statenode->node, 1, 0, statenode->async);
  195. PTHREAD_MUTEX_LOCK(&statenode->lock);
  196. statenode->finished = 1;
  197. PTHREAD_COND_SIGNAL(&statenode->cond);
  198. PTHREAD_MUTEX_UNLOCK(&statenode->lock);
  199. if (!statenode->async)
  200. {
  201. _starpu_spin_lock(&statenode->state->header_lock);
  202. _starpu_notify_data_dependencies(statenode->state);
  203. _starpu_spin_unlock(&statenode->state->header_lock);
  204. }
  205. }
  206. int _starpu_prefetch_data_on_node_with_mode(starpu_data_handle handle, unsigned node, unsigned async, starpu_access_mode mode)
  207. {
  208. STARPU_ASSERT(handle);
  209. /* it is forbidden to call this function from a callback or a codelet */
  210. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  211. return -EDEADLK;
  212. struct state_and_node statenode =
  213. {
  214. .state = handle,
  215. .node = node,
  216. .async = async,
  217. .cond = PTHREAD_COND_INITIALIZER,
  218. .lock = PTHREAD_MUTEX_INITIALIZER,
  219. .finished = 0
  220. };
  221. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _prefetch_data_on_node, &statenode))
  222. {
  223. /* we can immediately proceed */
  224. uint8_t read = (mode & STARPU_R);
  225. uint8_t write = (mode & STARPU_W);
  226. _starpu_fetch_data_on_node(handle, node, read, write, async);
  227. /* remove the "lock"/reference */
  228. if (!async)
  229. {
  230. _starpu_spin_lock(&handle->header_lock);
  231. _starpu_notify_data_dependencies(handle);
  232. _starpu_spin_unlock(&handle->header_lock);
  233. }
  234. }
  235. else {
  236. PTHREAD_MUTEX_LOCK(&statenode.lock);
  237. while (!statenode.finished)
  238. PTHREAD_COND_WAIT(&statenode.cond, &statenode.lock);
  239. PTHREAD_MUTEX_UNLOCK(&statenode.lock);
  240. }
  241. return 0;
  242. }
  243. int starpu_data_prefetch_on_node(starpu_data_handle handle, unsigned node, unsigned async)
  244. {
  245. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R);
  246. }
  247. /*
  248. * It is possible to specify that a piece of data can be discarded without
  249. * impacting the application.
  250. */
  251. void starpu_data_advise_as_important(starpu_data_handle handle, unsigned is_important)
  252. {
  253. _starpu_spin_lock(&handle->header_lock);
  254. /* first take all the children lock (in order !) */
  255. unsigned child;
  256. for (child = 0; child < handle->nchildren; child++)
  257. {
  258. /* make sure the intermediate children is advised as well */
  259. struct starpu_data_state_t *child_handle = &handle->children[child];
  260. if (child_handle->nchildren > 0)
  261. starpu_data_advise_as_important(child_handle, is_important);
  262. }
  263. handle->is_not_important = !is_important;
  264. /* now the parent may be used again so we release the lock */
  265. _starpu_spin_unlock(&handle->header_lock);
  266. }
  267. void starpu_data_set_sequential_consistency_flag(starpu_data_handle handle, unsigned flag)
  268. {
  269. _starpu_spin_lock(&handle->header_lock);
  270. unsigned child;
  271. for (child = 0; child < handle->nchildren; child++)
  272. {
  273. /* make sure that the flags are applied to the children as well */
  274. struct starpu_data_state_t *child_handle = &handle->children[child];
  275. if (child_handle->nchildren > 0)
  276. starpu_data_set_sequential_consistency_flag(child_handle, flag);
  277. }
  278. PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  279. handle->sequential_consistency = flag;
  280. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  281. _starpu_spin_unlock(&handle->header_lock);
  282. }