user_interactions.c 15 KB

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