user_interactions.c 16 KB

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