user_interactions.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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. #include <core/sched_policy.h>
  25. /* Explicitly ask StarPU to allocate room for a piece of data on the specified
  26. * memory node. */
  27. int starpu_data_request_allocation(starpu_data_handle_t handle, uint32_t node)
  28. {
  29. struct _starpu_data_request *r;
  30. STARPU_ASSERT(handle);
  31. r = _starpu_create_data_request(handle, NULL, &handle->per_node[node], node, STARPU_NONE, 0, 0);
  32. /* we do not increase the refcnt associated to the request since we are
  33. * not waiting for its termination */
  34. _starpu_post_data_request(r, node);
  35. return 0;
  36. }
  37. struct user_interaction_wrapper
  38. {
  39. starpu_data_handle_t handle;
  40. enum starpu_access_mode mode;
  41. unsigned node;
  42. pthread_cond_t cond;
  43. pthread_mutex_t lock;
  44. unsigned finished;
  45. unsigned async;
  46. void (*callback)(void *);
  47. void (*callback_fetch_data)(void *); // called after fetch_data
  48. void *callback_arg;
  49. struct starpu_task *pre_sync_task;
  50. struct starpu_task *post_sync_task;
  51. };
  52. /*
  53. * Non Blocking data request from application
  54. */
  55. /* put the current value of the data into RAM */
  56. static void _starpu_data_acquire_fetch_data_callback(void *arg)
  57. {
  58. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  59. starpu_data_handle_t handle = wrapper->handle;
  60. /* At that moment, the caller holds a reference to the piece of data.
  61. * We enqueue the "post" sync task in the list associated to the handle
  62. * so that it is submitted by the starpu_data_release
  63. * function. */
  64. _starpu_add_post_sync_tasks(wrapper->post_sync_task, handle);
  65. wrapper->callback(wrapper->callback_arg);
  66. free(wrapper);
  67. }
  68. static void _starpu_data_acquire_continuation_non_blocking(void *arg)
  69. {
  70. int ret;
  71. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  72. starpu_data_handle_t handle = wrapper->handle;
  73. STARPU_ASSERT(handle);
  74. struct _starpu_data_replicate *ram_replicate = &handle->per_node[0];
  75. ret = _starpu_fetch_data_on_node(handle, ram_replicate, wrapper->mode, 0, 1,
  76. _starpu_data_acquire_fetch_data_callback, wrapper);
  77. STARPU_ASSERT(!ret);
  78. }
  79. static void starpu_data_acquire_cb_pre_sync_callback(void *arg)
  80. {
  81. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  82. /* we try to get the data, if we do not succeed immediately, we set a
  83. * callback function that will be executed automatically when the data is
  84. * available again, otherwise we fetch the data directly */
  85. if (!_starpu_attempt_to_submit_data_request_from_apps(wrapper->handle, wrapper->mode,
  86. _starpu_data_acquire_continuation_non_blocking, wrapper))
  87. {
  88. /* no one has locked this data yet, so we proceed immediately */
  89. _starpu_data_acquire_continuation_non_blocking(wrapper);
  90. }
  91. }
  92. /* The data must be released by calling starpu_data_release later on */
  93. int starpu_data_acquire_cb(starpu_data_handle_t handle,
  94. enum starpu_access_mode mode, void (*callback)(void *), void *arg)
  95. {
  96. STARPU_ASSERT(handle);
  97. STARPU_ASSERT_MSG(handle->nchildren == 0, "Acquiring a partitioned data is not possible");
  98. _STARPU_LOG_IN();
  99. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) malloc(sizeof(struct user_interaction_wrapper));
  100. STARPU_ASSERT(wrapper);
  101. wrapper->handle = handle;
  102. wrapper->mode = mode;
  103. wrapper->callback = callback;
  104. wrapper->callback_arg = arg;
  105. _STARPU_PTHREAD_COND_INIT(&wrapper->cond, NULL);
  106. _STARPU_PTHREAD_MUTEX_INIT(&wrapper->lock, NULL);
  107. wrapper->finished = 0;
  108. _STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  109. int sequential_consistency = handle->sequential_consistency;
  110. if (sequential_consistency)
  111. {
  112. struct starpu_task *new_task;
  113. wrapper->pre_sync_task = starpu_task_create();
  114. wrapper->pre_sync_task->detach = 1;
  115. wrapper->pre_sync_task->callback_func = starpu_data_acquire_cb_pre_sync_callback;
  116. wrapper->pre_sync_task->callback_arg = wrapper;
  117. wrapper->post_sync_task = starpu_task_create();
  118. wrapper->post_sync_task->detach = 1;
  119. #ifdef STARPU_USE_FXT
  120. struct _starpu_job *job = _starpu_get_job_associated_to_task(wrapper->pre_sync_task);
  121. job->model_name = "acquire_cb_pre";
  122. job = _starpu_get_job_associated_to_task(wrapper->post_sync_task);
  123. job->model_name = "acquire_cb_post";
  124. #endif
  125. new_task = _starpu_detect_implicit_data_deps_with_handle(wrapper->pre_sync_task, wrapper->post_sync_task, handle, mode);
  126. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  127. if (new_task) {
  128. int ret = starpu_task_submit(new_task);
  129. STARPU_ASSERT(!ret);
  130. }
  131. /* TODO detect if this is superflous */
  132. int ret = starpu_task_submit(wrapper->pre_sync_task);
  133. STARPU_ASSERT(!ret);
  134. }
  135. else
  136. {
  137. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  138. starpu_data_acquire_cb_pre_sync_callback(wrapper);
  139. }
  140. _STARPU_LOG_OUT();
  141. return 0;
  142. }
  143. /*
  144. * Block data request from application
  145. */
  146. static inline void _starpu_data_acquire_continuation(void *arg)
  147. {
  148. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  149. starpu_data_handle_t handle = wrapper->handle;
  150. STARPU_ASSERT(handle);
  151. struct _starpu_data_replicate *ram_replicate = &handle->per_node[0];
  152. _starpu_fetch_data_on_node(handle, ram_replicate, wrapper->mode, 0, 0, NULL, NULL);
  153. /* continuation of starpu_data_acquire */
  154. _STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  155. wrapper->finished = 1;
  156. _STARPU_PTHREAD_COND_SIGNAL(&wrapper->cond);
  157. _STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  158. }
  159. /* The data must be released by calling starpu_data_release later on */
  160. int starpu_data_acquire(starpu_data_handle_t handle, enum starpu_access_mode mode)
  161. {
  162. STARPU_ASSERT(handle);
  163. STARPU_ASSERT_MSG(handle->nchildren == 0, "Acquiring a partitioned data is not possible");
  164. _STARPU_LOG_IN();
  165. /* unless asynchronous, it is forbidden to call this function from a callback or a codelet */
  166. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  167. {
  168. _STARPU_LOG_OUT_TAG("EDEADLK");
  169. return -EDEADLK;
  170. }
  171. if (_starpu_data_is_multiformat_handle(handle) &&
  172. _starpu_handle_needs_conversion_task(handle, 0))
  173. {
  174. struct starpu_task *task = _starpu_create_conversion_task(handle, 0);
  175. int ret;
  176. handle->refcnt--;
  177. handle->busy_count--;
  178. handle->mf_node = 0;
  179. task->synchronous = 1;
  180. ret = starpu_task_submit(task);
  181. STARPU_ASSERT(!ret);
  182. }
  183. struct user_interaction_wrapper wrapper =
  184. {
  185. .handle = handle,
  186. .mode = mode,
  187. .node = 0, // unused
  188. .cond = PTHREAD_COND_INITIALIZER,
  189. .lock = PTHREAD_MUTEX_INITIALIZER,
  190. .finished = 0
  191. };
  192. // _STARPU_DEBUG("TAKE sequential_consistency_mutex starpu_data_acquire\n");
  193. _STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  194. int sequential_consistency = handle->sequential_consistency;
  195. if (sequential_consistency)
  196. {
  197. struct starpu_task *new_task;
  198. wrapper.pre_sync_task = starpu_task_create();
  199. wrapper.pre_sync_task->detach = 0;
  200. wrapper.post_sync_task = starpu_task_create();
  201. wrapper.post_sync_task->detach = 1;
  202. #ifdef STARPU_USE_FXT
  203. struct _starpu_job *job = _starpu_get_job_associated_to_task(wrapper.pre_sync_task);
  204. job->model_name = "acquire_pre";
  205. job = _starpu_get_job_associated_to_task(wrapper.post_sync_task);
  206. job->model_name = "acquire_post";
  207. #endif
  208. new_task = _starpu_detect_implicit_data_deps_with_handle(wrapper.pre_sync_task, wrapper.post_sync_task, handle, mode);
  209. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  210. if (new_task) {
  211. int ret = starpu_task_submit(new_task);
  212. STARPU_ASSERT(!ret);
  213. }
  214. /* TODO detect if this is superflous */
  215. wrapper.pre_sync_task->synchronous = 1;
  216. int ret = starpu_task_submit(wrapper.pre_sync_task);
  217. STARPU_ASSERT(!ret);
  218. }
  219. else
  220. {
  221. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  222. }
  223. /* we try to get the data, if we do not succeed immediately, we set a
  224. * callback function that will be executed automatically when the data is
  225. * available again, otherwise we fetch the data directly */
  226. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _starpu_data_acquire_continuation, &wrapper))
  227. {
  228. /* no one has locked this data yet, so we proceed immediately */
  229. struct _starpu_data_replicate *ram_replicate = &handle->per_node[0];
  230. int ret = _starpu_fetch_data_on_node(handle, ram_replicate, mode, 0, 0, NULL, NULL);
  231. STARPU_ASSERT(!ret);
  232. }
  233. else
  234. {
  235. _STARPU_PTHREAD_MUTEX_LOCK(&wrapper.lock);
  236. while (!wrapper.finished)
  237. _STARPU_PTHREAD_COND_WAIT(&wrapper.cond, &wrapper.lock);
  238. _STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper.lock);
  239. _STARPU_PTHREAD_MUTEX_DESTROY(&wrapper.lock);
  240. }
  241. /* At that moment, the caller holds a reference to the piece of data.
  242. * We enqueue the "post" sync task in the list associated to the handle
  243. * so that it is submitted by the starpu_data_release
  244. * function. */
  245. _starpu_add_post_sync_tasks(wrapper.post_sync_task, handle);
  246. _STARPU_LOG_OUT();
  247. return 0;
  248. }
  249. /* This function must be called after starpu_data_acquire so that the
  250. * application release the data */
  251. void starpu_data_release(starpu_data_handle_t handle)
  252. {
  253. STARPU_ASSERT(handle);
  254. /* The application can now release the rw-lock */
  255. _starpu_release_data_on_node(handle, 0, &handle->per_node[0]);
  256. /* In case there are some implicit dependencies, unlock the "post sync" tasks */
  257. _starpu_unlock_post_sync_tasks(handle);
  258. }
  259. static void _prefetch_data_on_node(void *arg)
  260. {
  261. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  262. starpu_data_handle_t handle = wrapper->handle;
  263. int ret;
  264. struct _starpu_data_replicate *replicate = &handle->per_node[wrapper->node];
  265. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, wrapper->async, wrapper->async, NULL, NULL);
  266. STARPU_ASSERT(!ret);
  267. if (wrapper->async)
  268. free(wrapper);
  269. else {
  270. _STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  271. wrapper->finished = 1;
  272. _STARPU_PTHREAD_COND_SIGNAL(&wrapper->cond);
  273. _STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  274. }
  275. _starpu_spin_lock(&handle->header_lock);
  276. _starpu_notify_data_dependencies(handle);
  277. _starpu_spin_unlock(&handle->header_lock);
  278. }
  279. static
  280. int _starpu_prefetch_data_on_node_with_mode(starpu_data_handle_t handle, unsigned node, unsigned async, enum starpu_access_mode mode)
  281. {
  282. STARPU_ASSERT(handle);
  283. /* it is forbidden to call this function from a callback or a codelet */
  284. if (STARPU_UNLIKELY(!async && !_starpu_worker_may_perform_blocking_calls()))
  285. return -EDEADLK;
  286. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) malloc(sizeof(*wrapper));
  287. wrapper->handle = handle;
  288. wrapper->node = node;
  289. wrapper->async = async;
  290. _STARPU_PTHREAD_COND_INIT(&wrapper->cond, NULL);
  291. _STARPU_PTHREAD_MUTEX_INIT(&wrapper->lock, NULL);
  292. wrapper->finished = 0;
  293. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _prefetch_data_on_node, wrapper))
  294. {
  295. /* we can immediately proceed */
  296. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  297. free(wrapper);
  298. _starpu_fetch_data_on_node(handle, replicate, mode, async, async, NULL, NULL);
  299. /* remove the "lock"/reference */
  300. _starpu_spin_lock(&handle->header_lock);
  301. if (!async)
  302. {
  303. /* Release our refcnt, like _starpu_release_data_on_node would do */
  304. replicate->refcnt--;
  305. STARPU_ASSERT(replicate->refcnt >= 0);
  306. STARPU_ASSERT(handle->busy_count > 0);
  307. handle->busy_count--;
  308. _starpu_data_check_not_busy(handle);
  309. }
  310. /* In case there was a temporary handle (eg. used for reduction), this
  311. * handle may have requested to be destroyed when the data is released
  312. * */
  313. unsigned handle_was_destroyed = handle->lazy_unregister;
  314. _starpu_notify_data_dependencies(handle);
  315. if (!handle_was_destroyed)
  316. _starpu_spin_unlock(&handle->header_lock);
  317. }
  318. else if (!async)
  319. {
  320. _STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  321. while (!wrapper->finished)
  322. _STARPU_PTHREAD_COND_WAIT(&wrapper->cond, &wrapper->lock);
  323. _STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  324. free(wrapper);
  325. }
  326. return 0;
  327. }
  328. int starpu_data_prefetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async)
  329. {
  330. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R);
  331. }
  332. /*
  333. * It is possible to specify that a piece of data can be discarded without
  334. * impacting the application.
  335. */
  336. void starpu_data_advise_as_important(starpu_data_handle_t handle, unsigned is_important)
  337. {
  338. _starpu_spin_lock(&handle->header_lock);
  339. /* first take all the children lock (in order !) */
  340. unsigned child;
  341. for (child = 0; child < handle->nchildren; child++)
  342. {
  343. /* make sure the intermediate children is advised as well */
  344. struct _starpu_data_state *child_handle = &handle->children[child];
  345. if (child_handle->nchildren > 0)
  346. starpu_data_advise_as_important(child_handle, is_important);
  347. }
  348. handle->is_not_important = !is_important;
  349. /* now the parent may be used again so we release the lock */
  350. _starpu_spin_unlock(&handle->header_lock);
  351. }
  352. void starpu_data_set_sequential_consistency_flag(starpu_data_handle_t handle, unsigned flag)
  353. {
  354. _starpu_spin_lock(&handle->header_lock);
  355. unsigned child;
  356. for (child = 0; child < handle->nchildren; child++)
  357. {
  358. /* make sure that the flags are applied to the children as well */
  359. struct _starpu_data_state *child_handle = &handle->children[child];
  360. if (child_handle->nchildren > 0)
  361. starpu_data_set_sequential_consistency_flag(child_handle, flag);
  362. }
  363. _STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  364. handle->sequential_consistency = flag;
  365. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  366. _starpu_spin_unlock(&handle->header_lock);
  367. }
  368. /* By default, sequential consistency is enabled */
  369. static unsigned default_sequential_consistency_flag = 1;
  370. unsigned starpu_data_get_default_sequential_consistency_flag(void)
  371. {
  372. return default_sequential_consistency_flag;
  373. }
  374. void starpu_data_set_default_sequential_consistency_flag(unsigned flag)
  375. {
  376. default_sequential_consistency_flag = flag;
  377. }
  378. /* Query the status of the handle on the specified memory node. */
  379. void starpu_data_query_status(starpu_data_handle_t handle, int memory_node, int *is_allocated, int *is_valid, int *is_requested)
  380. {
  381. #ifdef STARPU_DEVEL
  382. #warning FIXME
  383. #endif
  384. // _starpu_spin_lock(&handle->header_lock);
  385. if (is_allocated)
  386. *is_allocated = handle->per_node[memory_node].allocated;
  387. if (is_valid)
  388. *is_valid = (handle->per_node[memory_node].state != STARPU_INVALID);
  389. if (is_requested)
  390. {
  391. int requested = 0;
  392. unsigned node;
  393. for (node = 0; node < STARPU_MAXNODES; node++)
  394. {
  395. if (handle->per_node[memory_node].requested[node])
  396. {
  397. requested = 1;
  398. break;
  399. }
  400. }
  401. *is_requested = requested;
  402. }
  403. // _starpu_spin_unlock(&handle->header_lock);
  404. }