user_interactions.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. #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, 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_LOG_IN();
  98. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) malloc(sizeof(struct user_interaction_wrapper));
  99. STARPU_ASSERT(wrapper);
  100. wrapper->handle = handle;
  101. wrapper->mode = mode;
  102. wrapper->callback = callback;
  103. wrapper->callback_arg = arg;
  104. _STARPU_PTHREAD_COND_INIT(&wrapper->cond, NULL);
  105. _STARPU_PTHREAD_MUTEX_INIT(&wrapper->lock, NULL);
  106. wrapper->finished = 0;
  107. #ifdef STARPU_DEVEL
  108. #warning TODO instead of having the is_prefetch argument, _starpu_fetch_data shoud consider two flags: async and detached
  109. #endif
  110. _starpu_spin_lock(&handle->header_lock);
  111. handle->per_node[0].refcnt++;
  112. handle->busy_count++;
  113. _starpu_spin_unlock(&handle->header_lock);
  114. _STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  115. int sequential_consistency = handle->sequential_consistency;
  116. if (sequential_consistency)
  117. {
  118. wrapper->pre_sync_task = starpu_task_create();
  119. wrapper->pre_sync_task->detach = 1;
  120. wrapper->pre_sync_task->callback_func = starpu_data_acquire_cb_pre_sync_callback;
  121. wrapper->pre_sync_task->callback_arg = wrapper;
  122. wrapper->post_sync_task = starpu_task_create();
  123. wrapper->post_sync_task->detach = 1;
  124. #ifdef STARPU_USE_FXT
  125. struct _starpu_job *job = _starpu_get_job_associated_to_task(wrapper->pre_sync_task);
  126. job->model_name = "acquire_cb_pre";
  127. job = _starpu_get_job_associated_to_task(wrapper->post_sync_task);
  128. job->model_name = "acquire_cb_post";
  129. #endif
  130. _starpu_detect_implicit_data_deps_with_handle(wrapper->pre_sync_task, wrapper->post_sync_task, handle, mode);
  131. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  132. /* TODO detect if this is superflous */
  133. int ret = starpu_task_submit(wrapper->pre_sync_task);
  134. STARPU_ASSERT(!ret);
  135. }
  136. else
  137. {
  138. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  139. starpu_data_acquire_cb_pre_sync_callback(wrapper);
  140. }
  141. _STARPU_LOG_OUT();
  142. return 0;
  143. }
  144. /*
  145. * Block data request from application
  146. */
  147. static inline void _starpu_data_acquire_continuation(void *arg)
  148. {
  149. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  150. starpu_data_handle_t handle = wrapper->handle;
  151. STARPU_ASSERT(handle);
  152. struct _starpu_data_replicate *ram_replicate = &handle->per_node[0];
  153. _starpu_fetch_data_on_node(handle, ram_replicate, wrapper->mode, 0, NULL, NULL);
  154. /* continuation of starpu_data_acquire */
  155. _STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  156. wrapper->finished = 1;
  157. _STARPU_PTHREAD_COND_SIGNAL(&wrapper->cond);
  158. _STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  159. }
  160. /* The data must be released by calling starpu_data_release later on */
  161. int starpu_data_acquire(starpu_data_handle_t handle, enum starpu_access_mode mode)
  162. {
  163. STARPU_ASSERT(handle);
  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. enum starpu_data_interface_id id = starpu_get_handle_interface_id(handle);
  172. if (id == STARPU_MULTIFORMAT_INTERFACE_ID &&
  173. _starpu_handle_needs_conversion_task(handle, 0))
  174. {
  175. struct starpu_task *task = _starpu_create_conversion_task(handle, 0);
  176. handle->refcnt--;
  177. handle->busy_count--;
  178. handle->mf_node = 0;
  179. task->synchronous = 1;
  180. starpu_task_submit(task);
  181. }
  182. struct user_interaction_wrapper wrapper =
  183. {
  184. .handle = handle,
  185. .mode = mode,
  186. .node = 0, // unused
  187. .cond = PTHREAD_COND_INITIALIZER,
  188. .lock = PTHREAD_MUTEX_INITIALIZER,
  189. .finished = 0
  190. };
  191. // _STARPU_DEBUG("TAKE sequential_consistency_mutex starpu_data_acquire\n");
  192. _STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  193. int sequential_consistency = handle->sequential_consistency;
  194. if (sequential_consistency)
  195. {
  196. wrapper.pre_sync_task = starpu_task_create();
  197. wrapper.pre_sync_task->detach = 0;
  198. wrapper.post_sync_task = starpu_task_create();
  199. wrapper.post_sync_task->detach = 1;
  200. #ifdef STARPU_USE_FXT
  201. struct _starpu_job *job = _starpu_get_job_associated_to_task(wrapper.pre_sync_task);
  202. job->model_name = "acquire_pre";
  203. job = _starpu_get_job_associated_to_task(wrapper.post_sync_task);
  204. job->model_name = "acquire_post";
  205. #endif
  206. _starpu_detect_implicit_data_deps_with_handle(wrapper.pre_sync_task, wrapper.post_sync_task, handle, mode);
  207. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  208. /* TODO detect if this is superflous */
  209. wrapper.pre_sync_task->synchronous = 1;
  210. int ret = starpu_task_submit(wrapper.pre_sync_task);
  211. STARPU_ASSERT(!ret);
  212. //starpu_task_wait(wrapper.pre_sync_task);
  213. }
  214. else
  215. {
  216. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  217. }
  218. /* we try to get the data, if we do not succeed immediately, we set a
  219. * callback function that will be executed automatically when the data is
  220. * available again, otherwise we fetch the data directly */
  221. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _starpu_data_acquire_continuation, &wrapper))
  222. {
  223. /* no one has locked this data yet, so we proceed immediately */
  224. struct _starpu_data_replicate *ram_replicate = &handle->per_node[0];
  225. int ret = _starpu_fetch_data_on_node(handle, ram_replicate, mode, 0, NULL, NULL);
  226. STARPU_ASSERT(!ret);
  227. }
  228. else
  229. {
  230. _STARPU_PTHREAD_MUTEX_LOCK(&wrapper.lock);
  231. while (!wrapper.finished)
  232. _STARPU_PTHREAD_COND_WAIT(&wrapper.cond, &wrapper.lock);
  233. _STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper.lock);
  234. }
  235. /* At that moment, the caller holds a reference to the piece of data.
  236. * We enqueue the "post" sync task in the list associated to the handle
  237. * so that it is submitted by the starpu_data_release
  238. * function. */
  239. _starpu_add_post_sync_tasks(wrapper.post_sync_task, handle);
  240. _STARPU_LOG_OUT();
  241. return 0;
  242. }
  243. /* This function must be called after starpu_data_acquire so that the
  244. * application release the data */
  245. void starpu_data_release(starpu_data_handle_t handle)
  246. {
  247. STARPU_ASSERT(handle);
  248. /* The application can now release the rw-lock */
  249. _starpu_release_data_on_node(handle, 0, &handle->per_node[0]);
  250. /* In case there are some implicit dependencies, unlock the "post sync" tasks */
  251. _starpu_unlock_post_sync_tasks(handle);
  252. }
  253. static void _prefetch_data_on_node(void *arg)
  254. {
  255. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  256. starpu_data_handle_t handle = wrapper->handle;
  257. int ret;
  258. struct _starpu_data_replicate *replicate = &handle->per_node[wrapper->node];
  259. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, wrapper->async, NULL, NULL);
  260. STARPU_ASSERT(!ret);
  261. if (!wrapper->async)
  262. {
  263. _STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  264. wrapper->finished = 1;
  265. _STARPU_PTHREAD_COND_SIGNAL(&wrapper->cond);
  266. _STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  267. }
  268. _starpu_spin_lock(&handle->header_lock);
  269. _starpu_notify_data_dependencies(handle);
  270. _starpu_spin_unlock(&handle->header_lock);
  271. }
  272. static
  273. int _starpu_prefetch_data_on_node_with_mode(starpu_data_handle_t handle, unsigned node, unsigned async, enum starpu_access_mode mode)
  274. {
  275. STARPU_ASSERT(handle);
  276. /* it is forbidden to call this function from a callback or a codelet */
  277. if (STARPU_UNLIKELY(!async && !_starpu_worker_may_perform_blocking_calls()))
  278. return -EDEADLK;
  279. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) malloc(sizeof(*wrapper));
  280. wrapper->handle = handle;
  281. wrapper->node = node;
  282. wrapper->async = async;
  283. _STARPU_PTHREAD_COND_INIT(&wrapper->cond, NULL);
  284. _STARPU_PTHREAD_MUTEX_INIT(&wrapper->lock, NULL);
  285. wrapper->finished = 0;
  286. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _prefetch_data_on_node, wrapper))
  287. {
  288. /* we can immediately proceed */
  289. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  290. _starpu_fetch_data_on_node(handle, replicate, mode, async, NULL, NULL);
  291. /* remove the "lock"/reference */
  292. _starpu_spin_lock(&handle->header_lock);
  293. if (!async)
  294. {
  295. replicate->refcnt--;
  296. STARPU_ASSERT(replicate->refcnt >= 0);
  297. STARPU_ASSERT(handle->busy_count > 0);
  298. handle->busy_count--;
  299. _starpu_data_check_not_busy(handle);
  300. }
  301. _starpu_notify_data_dependencies(handle);
  302. _starpu_spin_unlock(&handle->header_lock);
  303. }
  304. else if (!async)
  305. {
  306. _STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  307. while (!wrapper->finished)
  308. _STARPU_PTHREAD_COND_WAIT(&wrapper->cond, &wrapper->lock);
  309. _STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  310. }
  311. return 0;
  312. }
  313. int starpu_data_prefetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async)
  314. {
  315. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R);
  316. }
  317. /*
  318. * It is possible to specify that a piece of data can be discarded without
  319. * impacting the application.
  320. */
  321. void starpu_data_advise_as_important(starpu_data_handle_t handle, unsigned is_important)
  322. {
  323. _starpu_spin_lock(&handle->header_lock);
  324. /* first take all the children lock (in order !) */
  325. unsigned child;
  326. for (child = 0; child < handle->nchildren; child++)
  327. {
  328. /* make sure the intermediate children is advised as well */
  329. struct _starpu_data_state *child_handle = &handle->children[child];
  330. if (child_handle->nchildren > 0)
  331. starpu_data_advise_as_important(child_handle, is_important);
  332. }
  333. handle->is_not_important = !is_important;
  334. /* now the parent may be used again so we release the lock */
  335. _starpu_spin_unlock(&handle->header_lock);
  336. }
  337. void starpu_data_set_sequential_consistency_flag(starpu_data_handle_t handle, unsigned flag)
  338. {
  339. _starpu_spin_lock(&handle->header_lock);
  340. unsigned child;
  341. for (child = 0; child < handle->nchildren; child++)
  342. {
  343. /* make sure that the flags are applied to the children as well */
  344. struct _starpu_data_state *child_handle = &handle->children[child];
  345. if (child_handle->nchildren > 0)
  346. starpu_data_set_sequential_consistency_flag(child_handle, flag);
  347. }
  348. _STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  349. handle->sequential_consistency = flag;
  350. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  351. _starpu_spin_unlock(&handle->header_lock);
  352. }
  353. /* By default, sequential consistency is enabled */
  354. static unsigned default_sequential_consistency_flag = 1;
  355. unsigned starpu_data_get_default_sequential_consistency_flag(void)
  356. {
  357. return default_sequential_consistency_flag;
  358. }
  359. void starpu_data_set_default_sequential_consistency_flag(unsigned flag)
  360. {
  361. default_sequential_consistency_flag = flag;
  362. }
  363. /* Query the status of the handle on the specified memory node. */
  364. void starpu_data_query_status(starpu_data_handle_t handle, int memory_node, int *is_allocated, int *is_valid, int *is_requested)
  365. {
  366. #ifdef STARPU_DEVEL
  367. #warning FIXME
  368. #endif
  369. // _starpu_spin_lock(&handle->header_lock);
  370. if (is_allocated)
  371. *is_allocated = handle->per_node[memory_node].allocated;
  372. if (is_valid)
  373. *is_valid = (handle->per_node[memory_node].state != STARPU_INVALID);
  374. if (is_requested)
  375. {
  376. int requested = 0;
  377. unsigned node;
  378. for (node = 0; node < STARPU_MAXNODES; node++)
  379. {
  380. if (handle->per_node[memory_node].requested[node])
  381. {
  382. requested = 1;
  383. break;
  384. }
  385. }
  386. *is_requested = requested;
  387. }
  388. // _starpu_spin_unlock(&handle->header_lock);
  389. }