user_interactions.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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_t handle, uint32_t node)
  27. {
  28. struct _starpu_data_request *r;
  29. STARPU_ASSERT(handle);
  30. r = _starpu_create_data_request(handle, NULL, &handle->per_node[node], node, 0, 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. {
  38. starpu_data_handle_t handle;
  39. enum starpu_access_mode mode;
  40. unsigned node;
  41. pthread_cond_t cond;
  42. pthread_mutex_t lock;
  43. unsigned finished;
  44. unsigned async;
  45. void (*callback)(void *);
  46. void (*callback_fetch_data)(void *); // called after fetch_data
  47. void *callback_arg;
  48. struct starpu_task *pre_sync_task;
  49. struct starpu_task *post_sync_task;
  50. };
  51. /*
  52. * Non Blocking data request from application
  53. */
  54. /* put the current value of the data into RAM */
  55. static void _starpu_data_acquire_fetch_data_callback(void *arg)
  56. {
  57. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  58. starpu_data_handle_t handle = wrapper->handle;
  59. /* At that moment, the caller holds a reference to the piece of data.
  60. * We enqueue the "post" sync task in the list associated to the handle
  61. * so that it is submitted by the starpu_data_release
  62. * function. */
  63. _starpu_add_post_sync_tasks(wrapper->post_sync_task, handle);
  64. wrapper->callback(wrapper->callback_arg);
  65. free(wrapper);
  66. }
  67. static void _starpu_data_acquire_continuation_non_blocking(void *arg)
  68. {
  69. int ret;
  70. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  71. starpu_data_handle_t handle = wrapper->handle;
  72. STARPU_ASSERT(handle);
  73. struct _starpu_data_replicate *ram_replicate = &handle->per_node[0];
  74. ret = _starpu_fetch_data_on_node(handle, ram_replicate, wrapper->mode, 1,
  75. _starpu_data_acquire_fetch_data_callback, wrapper);
  76. STARPU_ASSERT(!ret);
  77. }
  78. static void starpu_data_acquire_cb_pre_sync_callback(void *arg)
  79. {
  80. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  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(wrapper->handle, wrapper->mode,
  85. _starpu_data_acquire_continuation_non_blocking, wrapper))
  86. {
  87. /* no one has locked this data yet, so we proceed immediately */
  88. _starpu_data_acquire_continuation_non_blocking(wrapper);
  89. }
  90. }
  91. /* The data must be released by calling starpu_data_release later on */
  92. int starpu_data_acquire_cb(starpu_data_handle_t handle,
  93. enum starpu_access_mode mode, void (*callback)(void *), void *arg)
  94. {
  95. STARPU_ASSERT(handle);
  96. _STARPU_LOG_IN();
  97. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) malloc(sizeof(struct user_interaction_wrapper));
  98. STARPU_ASSERT(wrapper);
  99. wrapper->handle = handle;
  100. wrapper->mode = mode;
  101. wrapper->callback = callback;
  102. wrapper->callback_arg = arg;
  103. _STARPU_PTHREAD_COND_INIT(&wrapper->cond, NULL);
  104. _STARPU_PTHREAD_MUTEX_INIT(&wrapper->lock, NULL);
  105. wrapper->finished = 0;
  106. #ifdef STARPU_DEVEL
  107. #warning TODO instead of having the is_prefetch argument, _starpu_fetch_data shoud consider two flags: async and detached
  108. #endif
  109. _starpu_spin_lock(&handle->header_lock);
  110. handle->per_node[0].refcnt++;
  111. handle->busy_count++;
  112. _starpu_spin_unlock(&handle->header_lock);
  113. _STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  114. int sequential_consistency = handle->sequential_consistency;
  115. if (sequential_consistency)
  116. {
  117. wrapper->pre_sync_task = starpu_task_create();
  118. wrapper->pre_sync_task->detach = 1;
  119. wrapper->pre_sync_task->callback_func = starpu_data_acquire_cb_pre_sync_callback;
  120. wrapper->pre_sync_task->callback_arg = wrapper;
  121. wrapper->post_sync_task = starpu_task_create();
  122. wrapper->post_sync_task->detach = 1;
  123. #ifdef STARPU_USE_FXT
  124. struct _starpu_job *job = _starpu_get_job_associated_to_task(wrapper->pre_sync_task);
  125. job->model_name = "acquire_cb_pre";
  126. job = _starpu_get_job_associated_to_task(wrapper->post_sync_task);
  127. job->model_name = "acquire_cb_post";
  128. #endif
  129. _starpu_detect_implicit_data_deps_with_handle(wrapper->pre_sync_task, wrapper->post_sync_task, handle, mode);
  130. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  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, 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_LOG_IN();
  164. /* unless asynchronous, it is forbidden to call this function from a callback or a codelet */
  165. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  166. {
  167. _STARPU_LOG_OUT_TAG("EDEADLK");
  168. return -EDEADLK;
  169. }
  170. struct user_interaction_wrapper wrapper =
  171. {
  172. .handle = handle,
  173. .mode = mode,
  174. .node = 0, // unused
  175. .cond = PTHREAD_COND_INITIALIZER,
  176. .lock = PTHREAD_MUTEX_INITIALIZER,
  177. .finished = 0
  178. };
  179. // _STARPU_DEBUG("TAKE sequential_consistency_mutex starpu_data_acquire\n");
  180. _STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  181. int sequential_consistency = handle->sequential_consistency;
  182. if (sequential_consistency)
  183. {
  184. wrapper.pre_sync_task = starpu_task_create();
  185. wrapper.pre_sync_task->detach = 0;
  186. wrapper.post_sync_task = starpu_task_create();
  187. wrapper.post_sync_task->detach = 1;
  188. #ifdef STARPU_USE_FXT
  189. struct _starpu_job *job = _starpu_get_job_associated_to_task(wrapper.pre_sync_task);
  190. job->model_name = "acquire_pre";
  191. job = _starpu_get_job_associated_to_task(wrapper.post_sync_task);
  192. job->model_name = "acquire_post";
  193. #endif
  194. _starpu_detect_implicit_data_deps_with_handle(wrapper.pre_sync_task, wrapper.post_sync_task, handle, mode);
  195. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  196. /* TODO detect if this is superflous */
  197. wrapper.pre_sync_task->synchronous = 1;
  198. int ret = starpu_task_submit(wrapper.pre_sync_task);
  199. STARPU_ASSERT(!ret);
  200. //starpu_task_wait(wrapper.pre_sync_task);
  201. }
  202. else
  203. {
  204. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  205. }
  206. /* we try to get the data, if we do not succeed immediately, we set a
  207. * callback function that will be executed automatically when the data is
  208. * available again, otherwise we fetch the data directly */
  209. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _starpu_data_acquire_continuation, &wrapper))
  210. {
  211. /* no one has locked this data yet, so we proceed immediately */
  212. struct _starpu_data_replicate *ram_replicate = &handle->per_node[0];
  213. int ret = _starpu_fetch_data_on_node(handle, ram_replicate, mode, 0, NULL, NULL);
  214. STARPU_ASSERT(!ret);
  215. }
  216. else
  217. {
  218. _STARPU_PTHREAD_MUTEX_LOCK(&wrapper.lock);
  219. while (!wrapper.finished)
  220. _STARPU_PTHREAD_COND_WAIT(&wrapper.cond, &wrapper.lock);
  221. _STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper.lock);
  222. }
  223. /* At that moment, the caller holds a reference to the piece of data.
  224. * We enqueue the "post" sync task in the list associated to the handle
  225. * so that it is submitted by the starpu_data_release
  226. * function. */
  227. _starpu_add_post_sync_tasks(wrapper.post_sync_task, handle);
  228. _STARPU_LOG_OUT();
  229. return 0;
  230. }
  231. /* This function must be called after starpu_data_acquire so that the
  232. * application release the data */
  233. void starpu_data_release(starpu_data_handle_t handle)
  234. {
  235. STARPU_ASSERT(handle);
  236. /* The application can now release the rw-lock */
  237. _starpu_release_data_on_node(handle, 0, &handle->per_node[0]);
  238. /* In case there are some implicit dependencies, unlock the "post sync" tasks */
  239. _starpu_unlock_post_sync_tasks(handle);
  240. }
  241. static void _prefetch_data_on_node(void *arg)
  242. {
  243. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  244. starpu_data_handle_t handle = wrapper->handle;
  245. int ret;
  246. struct _starpu_data_replicate *replicate = &handle->per_node[wrapper->node];
  247. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, wrapper->async, NULL, NULL);
  248. STARPU_ASSERT(!ret);
  249. if (!wrapper->async)
  250. {
  251. _STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  252. wrapper->finished = 1;
  253. _STARPU_PTHREAD_COND_SIGNAL(&wrapper->cond);
  254. _STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  255. }
  256. _starpu_spin_lock(&handle->header_lock);
  257. _starpu_notify_data_dependencies(handle);
  258. _starpu_spin_unlock(&handle->header_lock);
  259. }
  260. static
  261. int _starpu_prefetch_data_on_node_with_mode(starpu_data_handle_t handle, unsigned node, unsigned async, enum starpu_access_mode mode)
  262. {
  263. STARPU_ASSERT(handle);
  264. /* it is forbidden to call this function from a callback or a codelet */
  265. if (STARPU_UNLIKELY(!async && !_starpu_worker_may_perform_blocking_calls()))
  266. return -EDEADLK;
  267. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) malloc(sizeof(*wrapper));
  268. wrapper->handle = handle;
  269. wrapper->node = node;
  270. wrapper->async = async;
  271. _STARPU_PTHREAD_COND_INIT(&wrapper->cond, NULL);
  272. _STARPU_PTHREAD_MUTEX_INIT(&wrapper->lock, NULL);
  273. wrapper->finished = 0;
  274. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _prefetch_data_on_node, wrapper))
  275. {
  276. /* we can immediately proceed */
  277. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  278. _starpu_fetch_data_on_node(handle, replicate, mode, async, NULL, NULL);
  279. /* remove the "lock"/reference */
  280. _starpu_spin_lock(&handle->header_lock);
  281. if (!async)
  282. {
  283. replicate->refcnt--;
  284. STARPU_ASSERT(replicate->refcnt >= 0);
  285. STARPU_ASSERT(handle->busy_count > 0);
  286. handle->busy_count--;
  287. _starpu_data_check_not_busy(handle);
  288. }
  289. _starpu_notify_data_dependencies(handle);
  290. _starpu_spin_unlock(&handle->header_lock);
  291. }
  292. else if (!async)
  293. {
  294. _STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  295. while (!wrapper->finished)
  296. _STARPU_PTHREAD_COND_WAIT(&wrapper->cond, &wrapper->lock);
  297. _STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  298. }
  299. return 0;
  300. }
  301. int starpu_data_prefetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async)
  302. {
  303. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R);
  304. }
  305. /*
  306. * It is possible to specify that a piece of data can be discarded without
  307. * impacting the application.
  308. */
  309. void starpu_data_advise_as_important(starpu_data_handle_t handle, unsigned is_important)
  310. {
  311. _starpu_spin_lock(&handle->header_lock);
  312. /* first take all the children lock (in order !) */
  313. unsigned child;
  314. for (child = 0; child < handle->nchildren; child++)
  315. {
  316. /* make sure the intermediate children is advised as well */
  317. struct _starpu_data_state *child_handle = &handle->children[child];
  318. if (child_handle->nchildren > 0)
  319. starpu_data_advise_as_important(child_handle, is_important);
  320. }
  321. handle->is_not_important = !is_important;
  322. /* now the parent may be used again so we release the lock */
  323. _starpu_spin_unlock(&handle->header_lock);
  324. }
  325. void starpu_data_set_sequential_consistency_flag(starpu_data_handle_t handle, unsigned flag)
  326. {
  327. _starpu_spin_lock(&handle->header_lock);
  328. unsigned child;
  329. for (child = 0; child < handle->nchildren; child++)
  330. {
  331. /* make sure that the flags are applied to the children as well */
  332. struct _starpu_data_state *child_handle = &handle->children[child];
  333. if (child_handle->nchildren > 0)
  334. starpu_data_set_sequential_consistency_flag(child_handle, flag);
  335. }
  336. _STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  337. handle->sequential_consistency = flag;
  338. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  339. _starpu_spin_unlock(&handle->header_lock);
  340. }
  341. /* By default, sequential consistency is enabled */
  342. static unsigned default_sequential_consistency_flag = 1;
  343. unsigned starpu_data_get_default_sequential_consistency_flag(void)
  344. {
  345. return default_sequential_consistency_flag;
  346. }
  347. void starpu_data_set_default_sequential_consistency_flag(unsigned flag)
  348. {
  349. default_sequential_consistency_flag = flag;
  350. }
  351. /* Query the status of the handle on the specified memory node. */
  352. void starpu_data_query_status(starpu_data_handle_t handle, int memory_node, int *is_allocated, int *is_valid, int *is_requested)
  353. {
  354. #ifdef STARPU_DEVEL
  355. #warning FIXME
  356. #endif
  357. // _starpu_spin_lock(&handle->header_lock);
  358. if (is_allocated)
  359. *is_allocated = handle->per_node[memory_node].allocated;
  360. if (is_valid)
  361. *is_valid = (handle->per_node[memory_node].state != STARPU_INVALID);
  362. if (is_requested)
  363. {
  364. int requested = 0;
  365. unsigned node;
  366. for (node = 0; node < STARPU_MAXNODES; node++)
  367. {
  368. if (handle->per_node[memory_node].requested[node])
  369. {
  370. requested = 1;
  371. break;
  372. }
  373. }
  374. *is_requested = requested;
  375. }
  376. // _starpu_spin_unlock(&handle->header_lock);
  377. }