user_interactions.c 14 KB

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