user_interactions.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <common/config.h>
  17. #include <common/utils.h>
  18. #include <core/task.h>
  19. #include <datawizard/coherency.h>
  20. #include <datawizard/copy_driver.h>
  21. #include <datawizard/write_back.h>
  22. #include <core/dependencies/data_concurrency.h>
  23. /* Explicitly ask StarPU to allocate room for a piece of data on the specified
  24. * memory node. */
  25. int starpu_data_request_allocation(starpu_data_handle handle, uint32_t node)
  26. {
  27. starpu_data_request_t r;
  28. STARPU_ASSERT(handle);
  29. r = _starpu_create_data_request(handle, 0, node, node, 0, 1);
  30. /* we do not increase the refcnt associated to the request since we are
  31. * not waiting for its termination */
  32. _starpu_post_data_request(r, node);
  33. return 0;
  34. }
  35. struct user_interaction_wrapper {
  36. starpu_data_handle handle;
  37. starpu_access_mode mode;
  38. unsigned node;
  39. pthread_cond_t cond;
  40. pthread_mutex_t lock;
  41. unsigned finished;
  42. unsigned async;
  43. void (*callback)(void *);
  44. void (*callback_fetch_data)(void *); // called after fetch_data
  45. void *callback_arg;
  46. struct starpu_task *pre_sync_task;
  47. struct starpu_task *post_sync_task;
  48. };
  49. /*
  50. * Non Blocking data request from application
  51. */
  52. /* put the current value of the data into RAM */
  53. static void _starpu_data_acquire_fetch_data_callback(void *arg)
  54. {
  55. struct user_interaction_wrapper *wrapper = arg;
  56. starpu_data_handle handle = wrapper->handle;
  57. /* At that moment, the caller holds a reference to the piece of data.
  58. * We enqueue the "post" sync task in the list associated to the handle
  59. * so that it is submitted by the starpu_data_release
  60. * function. */
  61. _starpu_add_post_sync_tasks(wrapper->post_sync_task, handle);
  62. wrapper->callback(wrapper->callback_arg);
  63. }
  64. static void _starpu_data_acquire_continuation_non_blocking(void *arg)
  65. {
  66. int ret;
  67. struct user_interaction_wrapper *wrapper = arg;
  68. starpu_data_handle handle = wrapper->handle;
  69. STARPU_ASSERT(handle);
  70. ret = _starpu_fetch_data_on_node(handle, 0, wrapper->mode, 1,
  71. _starpu_data_acquire_fetch_data_callback, wrapper);
  72. STARPU_ASSERT(!ret);
  73. }
  74. static void starpu_data_acquire_cb_pre_sync_callback(void *arg)
  75. {
  76. struct user_interaction_wrapper *wrapper = arg;
  77. /* we try to get the data, if we do not succeed immediately, we set a
  78. * callback function that will be executed automatically when the data is
  79. * available again, otherwise we fetch the data directly */
  80. if (!_starpu_attempt_to_submit_data_request_from_apps(wrapper->handle, wrapper->mode,
  81. _starpu_data_acquire_continuation_non_blocking, wrapper))
  82. {
  83. /* no one has locked this data yet, so we proceed immediately */
  84. _starpu_data_acquire_continuation_non_blocking(wrapper);
  85. }
  86. }
  87. /* The data must be released by calling starpu_data_release later on */
  88. int starpu_data_acquire_cb(starpu_data_handle handle,
  89. starpu_access_mode mode, void (*callback)(void *), void *arg)
  90. {
  91. STARPU_ASSERT(handle);
  92. struct user_interaction_wrapper *wrapper = malloc(sizeof(struct user_interaction_wrapper));
  93. STARPU_ASSERT(wrapper);
  94. wrapper->handle = handle;
  95. wrapper->mode = mode;
  96. wrapper->callback = callback;
  97. wrapper->callback_arg = arg;
  98. PTHREAD_COND_INIT(&wrapper->cond, NULL);
  99. PTHREAD_MUTEX_INIT(&wrapper->lock, NULL);
  100. wrapper->finished = 0;
  101. #warning TODO instead of having the is_prefetch argument, _starpu_fetch_data shoud consider two flags: async and detached
  102. _starpu_spin_lock(&handle->header_lock);
  103. handle->per_node[0].refcnt++;
  104. _starpu_spin_unlock(&handle->header_lock);
  105. PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  106. int sequential_consistency = handle->sequential_consistency;
  107. if (sequential_consistency)
  108. {
  109. wrapper->pre_sync_task = starpu_task_create();
  110. wrapper->pre_sync_task->detach = 1;
  111. wrapper->pre_sync_task->callback_func = starpu_data_acquire_cb_pre_sync_callback;
  112. wrapper->pre_sync_task->callback_arg = wrapper;
  113. wrapper->post_sync_task = starpu_task_create();
  114. wrapper->post_sync_task->detach = 1;
  115. #ifdef STARPU_USE_FXT
  116. starpu_job_t job = _starpu_get_job_associated_to_task(wrapper->pre_sync_task);
  117. job->model_name = "acquire_cb_pre";
  118. job = _starpu_get_job_associated_to_task(wrapper->post_sync_task);
  119. job->model_name = "acquire_cb_post";
  120. #endif
  121. _starpu_detect_implicit_data_deps_with_handle(wrapper->pre_sync_task, wrapper->post_sync_task, handle, mode);
  122. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  123. /* TODO detect if this is superflous */
  124. int ret = starpu_task_submit(wrapper->pre_sync_task);
  125. STARPU_ASSERT(!ret);
  126. }
  127. else {
  128. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  129. starpu_data_acquire_cb_pre_sync_callback(wrapper);
  130. }
  131. return 0;
  132. }
  133. /*
  134. * Block data request from application
  135. */
  136. static inline void _starpu_data_acquire_continuation(void *arg)
  137. {
  138. struct user_interaction_wrapper *wrapper = arg;
  139. starpu_data_handle handle = wrapper->handle;
  140. STARPU_ASSERT(handle);
  141. _starpu_fetch_data_on_node(handle, 0, wrapper->mode, 0, NULL, NULL);
  142. /* continuation of starpu_data_acquire */
  143. PTHREAD_MUTEX_LOCK(&wrapper->lock);
  144. wrapper->finished = 1;
  145. PTHREAD_COND_SIGNAL(&wrapper->cond);
  146. PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  147. }
  148. /* The data must be released by calling starpu_data_release later on */
  149. int starpu_data_acquire(starpu_data_handle handle, starpu_access_mode mode)
  150. {
  151. STARPU_ASSERT(handle);
  152. /* it is forbidden to call this function from a callback or a codelet */
  153. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  154. return -EDEADLK;
  155. struct user_interaction_wrapper wrapper =
  156. {
  157. .handle = handle,
  158. .mode = mode,
  159. .node = 0, // unused
  160. .cond = PTHREAD_COND_INITIALIZER,
  161. .lock = PTHREAD_MUTEX_INITIALIZER,
  162. .finished = 0
  163. };
  164. // fprintf(stderr, "TAKE sequential_consistency_mutex starpu_data_acquire\n");
  165. PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  166. int sequential_consistency = handle->sequential_consistency;
  167. if (sequential_consistency)
  168. {
  169. wrapper.pre_sync_task = starpu_task_create();
  170. wrapper.pre_sync_task->detach = 0;
  171. wrapper.post_sync_task = starpu_task_create();
  172. wrapper.post_sync_task->detach = 1;
  173. #ifdef STARPU_USE_FXT
  174. starpu_job_t job = _starpu_get_job_associated_to_task(wrapper.pre_sync_task);
  175. job->model_name = "acquire_pre";
  176. job = _starpu_get_job_associated_to_task(wrapper.post_sync_task);
  177. job->model_name = "acquire_post";
  178. #endif
  179. _starpu_detect_implicit_data_deps_with_handle(wrapper.pre_sync_task, wrapper.post_sync_task, handle, mode);
  180. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  181. /* TODO detect if this is superflous */
  182. wrapper.pre_sync_task->synchronous = 1;
  183. int ret = starpu_task_submit(wrapper.pre_sync_task);
  184. STARPU_ASSERT(!ret);
  185. //starpu_task_wait(wrapper.pre_sync_task);
  186. }
  187. else {
  188. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  189. }
  190. /* we try to get the data, if we do not succeed immediately, we set a
  191. * callback function that will be executed automatically when the data is
  192. * available again, otherwise we fetch the data directly */
  193. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode,
  194. _starpu_data_acquire_continuation, &wrapper))
  195. {
  196. /* no one has locked this data yet, so we proceed immediately */
  197. int ret = _starpu_fetch_data_on_node(handle, 0, mode, 0, NULL, NULL);
  198. STARPU_ASSERT(!ret);
  199. }
  200. else {
  201. PTHREAD_MUTEX_LOCK(&wrapper.lock);
  202. while (!wrapper.finished)
  203. PTHREAD_COND_WAIT(&wrapper.cond, &wrapper.lock);
  204. PTHREAD_MUTEX_UNLOCK(&wrapper.lock);
  205. }
  206. /* At that moment, the caller holds a reference to the piece of data.
  207. * We enqueue the "post" sync task in the list associated to the handle
  208. * so that it is submitted by the starpu_data_release
  209. * function. */
  210. _starpu_add_post_sync_tasks(wrapper.post_sync_task, handle);
  211. return 0;
  212. }
  213. /* This function must be called after starpu_data_acquire so that the
  214. * application release the data */
  215. void starpu_data_release(starpu_data_handle handle)
  216. {
  217. STARPU_ASSERT(handle);
  218. /* The application can now release the rw-lock */
  219. _starpu_release_data_on_node(handle, 0, 0);
  220. /* In case there are some implicit dependencies, unlock the "post sync" tasks */
  221. _starpu_unlock_post_sync_tasks(handle);
  222. }
  223. static void _prefetch_data_on_node(void *arg)
  224. {
  225. struct user_interaction_wrapper *wrapper = arg;
  226. int ret;
  227. ret = _starpu_fetch_data_on_node(wrapper->handle, wrapper->node, STARPU_R, wrapper->async, NULL, NULL);
  228. STARPU_ASSERT(!ret);
  229. PTHREAD_MUTEX_LOCK(&wrapper->lock);
  230. wrapper->finished = 1;
  231. PTHREAD_COND_SIGNAL(&wrapper->cond);
  232. PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  233. if (!wrapper->async)
  234. {
  235. _starpu_spin_lock(&wrapper->handle->header_lock);
  236. _starpu_notify_data_dependencies(wrapper->handle);
  237. _starpu_spin_unlock(&wrapper->handle->header_lock);
  238. }
  239. }
  240. int _starpu_prefetch_data_on_node_with_mode(starpu_data_handle handle, unsigned node, unsigned async, starpu_access_mode mode)
  241. {
  242. STARPU_ASSERT(handle);
  243. /* it is forbidden to call this function from a callback or a codelet */
  244. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  245. return -EDEADLK;
  246. struct user_interaction_wrapper wrapper =
  247. {
  248. .handle = handle,
  249. .node = node,
  250. .async = async,
  251. .cond = PTHREAD_COND_INITIALIZER,
  252. .lock = PTHREAD_MUTEX_INITIALIZER,
  253. .finished = 0
  254. };
  255. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _prefetch_data_on_node, &wrapper))
  256. {
  257. /* we can immediately proceed */
  258. _starpu_fetch_data_on_node(handle, node, mode, async, NULL, NULL);
  259. /* remove the "lock"/reference */
  260. if (!async)
  261. {
  262. _starpu_spin_lock(&handle->header_lock);
  263. _starpu_notify_data_dependencies(handle);
  264. _starpu_spin_unlock(&handle->header_lock);
  265. }
  266. }
  267. else {
  268. PTHREAD_MUTEX_LOCK(&wrapper.lock);
  269. while (!wrapper.finished)
  270. PTHREAD_COND_WAIT(&wrapper.cond, &wrapper.lock);
  271. PTHREAD_MUTEX_UNLOCK(&wrapper.lock);
  272. }
  273. return 0;
  274. }
  275. int starpu_data_prefetch_on_node(starpu_data_handle handle, unsigned node, unsigned async)
  276. {
  277. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R);
  278. }
  279. /*
  280. * It is possible to specify that a piece of data can be discarded without
  281. * impacting the application.
  282. */
  283. void starpu_data_advise_as_important(starpu_data_handle handle, unsigned is_important)
  284. {
  285. _starpu_spin_lock(&handle->header_lock);
  286. /* first take all the children lock (in order !) */
  287. unsigned child;
  288. for (child = 0; child < handle->nchildren; child++)
  289. {
  290. /* make sure the intermediate children is advised as well */
  291. struct starpu_data_state_t *child_handle = &handle->children[child];
  292. if (child_handle->nchildren > 0)
  293. starpu_data_advise_as_important(child_handle, is_important);
  294. }
  295. handle->is_not_important = !is_important;
  296. /* now the parent may be used again so we release the lock */
  297. _starpu_spin_unlock(&handle->header_lock);
  298. }
  299. void starpu_data_set_sequential_consistency_flag(starpu_data_handle handle, unsigned flag)
  300. {
  301. _starpu_spin_lock(&handle->header_lock);
  302. unsigned child;
  303. for (child = 0; child < handle->nchildren; child++)
  304. {
  305. /* make sure that the flags are applied to the children as well */
  306. struct starpu_data_state_t *child_handle = &handle->children[child];
  307. if (child_handle->nchildren > 0)
  308. starpu_data_set_sequential_consistency_flag(child_handle, flag);
  309. }
  310. PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  311. handle->sequential_consistency = flag;
  312. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  313. _starpu_spin_unlock(&handle->header_lock);
  314. }
  315. /* By default, sequential consistency is enabled */
  316. static unsigned default_sequential_consistency_flag = 1;
  317. unsigned starpu_data_get_default_sequential_consistency_flag(void)
  318. {
  319. return default_sequential_consistency_flag;
  320. }
  321. void starpu_data_set_default_sequential_consistency_flag(unsigned flag)
  322. {
  323. default_sequential_consistency_flag = flag;
  324. }
  325. /* Query the status of the handle on the specified memory node. */
  326. void starpu_data_query_status(starpu_data_handle handle, int memory_node, int *is_allocated, int *is_valid, int *is_requested)
  327. {
  328. #warning FIXME
  329. // _starpu_spin_lock(&handle->header_lock);
  330. if (is_allocated)
  331. *is_allocated = handle->per_node[memory_node].allocated;
  332. if (is_valid)
  333. *is_valid = (handle->per_node[memory_node].state != STARPU_INVALID);
  334. if (is_requested)
  335. *is_requested = handle->per_node[memory_node].requested;
  336. // _starpu_spin_unlock(&handle->header_lock);
  337. }