user_interactions.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2014 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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, unsigned 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_data_access_mode mode;
  43. int node;
  44. starpu_pthread_cond_t cond;
  45. starpu_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. if (wrapper->post_sync_task)
  67. _starpu_add_post_sync_tasks(wrapper->post_sync_task, handle);
  68. wrapper->callback(wrapper->callback_arg);
  69. free(wrapper);
  70. }
  71. static void _starpu_data_acquire_continuation_non_blocking(void *arg)
  72. {
  73. int ret;
  74. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  75. starpu_data_handle_t handle = wrapper->handle;
  76. STARPU_ASSERT(handle);
  77. if (wrapper->node >= 0)
  78. {
  79. struct _starpu_data_replicate *replicate = &handle->per_node[wrapper->node];
  80. ret = _starpu_fetch_data_on_node(handle, replicate, wrapper->mode, 0, 1,
  81. _starpu_data_acquire_fetch_data_callback, wrapper);
  82. STARPU_ASSERT(!ret);
  83. }
  84. else
  85. _starpu_data_acquire_fetch_data_callback(wrapper);
  86. }
  87. static void starpu_data_acquire_cb_pre_sync_callback(void *arg)
  88. {
  89. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  90. /* we try to get the data, if we do not succeed immediately, we set a
  91. * callback function that will be executed automatically when the data is
  92. * available again, otherwise we fetch the data directly */
  93. if (!_starpu_attempt_to_submit_data_request_from_apps(wrapper->handle, wrapper->mode,
  94. _starpu_data_acquire_continuation_non_blocking, wrapper))
  95. {
  96. /* no one has locked this data yet, so we proceed immediately */
  97. _starpu_data_acquire_continuation_non_blocking(wrapper);
  98. }
  99. }
  100. /* The data must be released by calling starpu_data_release later on */
  101. int starpu_data_acquire_on_node_cb_sequential_consistency(starpu_data_handle_t handle, int node,
  102. enum starpu_data_access_mode mode, void (*callback)(void *), void *arg,
  103. int sequential_consistency)
  104. {
  105. STARPU_ASSERT(handle);
  106. STARPU_ASSERT_MSG(handle->nchildren == 0, "Acquiring a partitioned data (%p) is not possible", handle);
  107. _STARPU_LOG_IN();
  108. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) malloc(sizeof(struct user_interaction_wrapper));
  109. STARPU_ASSERT(wrapper);
  110. wrapper->handle = handle;
  111. wrapper->node = node;
  112. wrapper->mode = mode;
  113. wrapper->callback = callback;
  114. wrapper->callback_arg = arg;
  115. STARPU_PTHREAD_COND_INIT(&wrapper->cond, NULL);
  116. STARPU_PTHREAD_MUTEX_INIT(&wrapper->lock, NULL);
  117. wrapper->finished = 0;
  118. wrapper->pre_sync_task = NULL;
  119. wrapper->post_sync_task = NULL;
  120. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  121. int handle_sequential_consistency = handle->sequential_consistency;
  122. if (handle_sequential_consistency && sequential_consistency)
  123. {
  124. struct starpu_task *new_task;
  125. wrapper->pre_sync_task = starpu_task_create();
  126. wrapper->pre_sync_task->name = "acquire_cb_pre";
  127. wrapper->pre_sync_task->detach = 1;
  128. wrapper->pre_sync_task->callback_func = starpu_data_acquire_cb_pre_sync_callback;
  129. wrapper->pre_sync_task->callback_arg = wrapper;
  130. wrapper->post_sync_task = starpu_task_create();
  131. wrapper->post_sync_task->name = "acquire_cb_post";
  132. wrapper->post_sync_task->detach = 1;
  133. new_task = _starpu_detect_implicit_data_deps_with_handle(wrapper->pre_sync_task, wrapper->post_sync_task, handle, mode);
  134. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  135. if (new_task)
  136. {
  137. int ret = _starpu_task_submit_internally(new_task);
  138. STARPU_ASSERT(!ret);
  139. }
  140. /* TODO detect if this is superflous */
  141. int ret = _starpu_task_submit_internally(wrapper->pre_sync_task);
  142. STARPU_ASSERT(!ret);
  143. }
  144. else
  145. {
  146. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  147. starpu_data_acquire_cb_pre_sync_callback(wrapper);
  148. }
  149. _STARPU_LOG_OUT();
  150. return 0;
  151. }
  152. int starpu_data_acquire_on_node_cb(starpu_data_handle_t handle, int node,
  153. enum starpu_data_access_mode mode, void (*callback)(void *), void *arg)
  154. {
  155. return starpu_data_acquire_on_node_cb_sequential_consistency(handle, node, mode, callback, arg, 1);
  156. }
  157. int starpu_data_acquire_cb(starpu_data_handle_t handle,
  158. enum starpu_data_access_mode mode, void (*callback)(void *), void *arg)
  159. {
  160. return starpu_data_acquire_on_node_cb(handle, STARPU_MAIN_RAM, mode, callback, arg);
  161. }
  162. int starpu_data_acquire_cb_sequential_consistency(starpu_data_handle_t handle,
  163. enum starpu_data_access_mode mode, void (*callback)(void *), void *arg, int sequential_consistency)
  164. {
  165. return starpu_data_acquire_on_node_cb_sequential_consistency(handle, STARPU_MAIN_RAM, mode, callback, arg, sequential_consistency);
  166. }
  167. /*
  168. * Block data request from application
  169. */
  170. static inline void _starpu_data_acquire_continuation(void *arg)
  171. {
  172. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  173. starpu_data_handle_t handle = wrapper->handle;
  174. STARPU_ASSERT(handle);
  175. if (wrapper->node >= 0)
  176. {
  177. int ret;
  178. struct _starpu_data_replicate *replicate = &handle->per_node[wrapper->node];
  179. ret = _starpu_fetch_data_on_node(handle, replicate, wrapper->mode, 0, 0, NULL, NULL);
  180. STARPU_ASSERT(!ret);
  181. }
  182. /* continuation of starpu_data_acquire */
  183. STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  184. wrapper->finished = 1;
  185. STARPU_PTHREAD_COND_SIGNAL(&wrapper->cond);
  186. STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  187. }
  188. /* The data must be released by calling starpu_data_release later on */
  189. int starpu_data_acquire_on_node(starpu_data_handle_t handle, int node, enum starpu_data_access_mode mode)
  190. {
  191. STARPU_ASSERT(handle);
  192. STARPU_ASSERT_MSG(handle->nchildren == 0, "Acquiring a partitioned data is not possible");
  193. _STARPU_LOG_IN();
  194. /* unless asynchronous, it is forbidden to call this function from a callback or a codelet */
  195. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "Acquiring a data synchronously is not possible from a codelet or from a task callback, use starpu_data_acquire_cb instead.");
  196. if (_starpu_data_is_multiformat_handle(handle) &&
  197. _starpu_handle_needs_conversion_task(handle, 0))
  198. {
  199. struct starpu_task *task = _starpu_create_conversion_task(handle, 0);
  200. int ret;
  201. _starpu_spin_lock(&handle->header_lock);
  202. handle->refcnt--;
  203. handle->busy_count--;
  204. handle->mf_node = 0;
  205. _starpu_spin_unlock(&handle->header_lock);
  206. task->synchronous = 1;
  207. ret = _starpu_task_submit_internally(task);
  208. STARPU_ASSERT(!ret);
  209. }
  210. struct user_interaction_wrapper wrapper =
  211. {
  212. .handle = handle,
  213. .mode = mode,
  214. .node = node,
  215. .finished = 0
  216. };
  217. STARPU_PTHREAD_COND_INIT(&wrapper.cond, NULL);
  218. STARPU_PTHREAD_MUTEX_INIT(&wrapper.lock, NULL);
  219. // _STARPU_DEBUG("TAKE sequential_consistency_mutex starpu_data_acquire\n");
  220. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  221. int sequential_consistency = handle->sequential_consistency;
  222. if (sequential_consistency)
  223. {
  224. struct starpu_task *new_task;
  225. wrapper.pre_sync_task = starpu_task_create();
  226. wrapper.pre_sync_task->name = "acquire_pre";
  227. wrapper.pre_sync_task->detach = 0;
  228. wrapper.post_sync_task = starpu_task_create();
  229. wrapper.post_sync_task->name = "acquire_post";
  230. wrapper.post_sync_task->detach = 1;
  231. new_task = _starpu_detect_implicit_data_deps_with_handle(wrapper.pre_sync_task, wrapper.post_sync_task, handle, mode);
  232. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  233. if (new_task)
  234. {
  235. int ret = _starpu_task_submit_internally(new_task);
  236. STARPU_ASSERT(!ret);
  237. }
  238. /* TODO detect if this is superflous */
  239. wrapper.pre_sync_task->synchronous = 1;
  240. int ret = _starpu_task_submit_internally(wrapper.pre_sync_task);
  241. STARPU_ASSERT(!ret);
  242. }
  243. else
  244. {
  245. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  246. }
  247. /* we try to get the data, if we do not succeed immediately, we set a
  248. * callback function that will be executed automatically when the data is
  249. * available again, otherwise we fetch the data directly */
  250. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _starpu_data_acquire_continuation, &wrapper))
  251. {
  252. if (node >= 0)
  253. {
  254. /* no one has locked this data yet, so we proceed immediately */
  255. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  256. int ret = _starpu_fetch_data_on_node(handle, replicate, mode, 0, 0, NULL, NULL);
  257. STARPU_ASSERT(!ret);
  258. }
  259. }
  260. else
  261. {
  262. STARPU_PTHREAD_MUTEX_LOCK(&wrapper.lock);
  263. while (!wrapper.finished)
  264. STARPU_PTHREAD_COND_WAIT(&wrapper.cond, &wrapper.lock);
  265. STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper.lock);
  266. }
  267. STARPU_PTHREAD_COND_DESTROY(&wrapper.cond);
  268. STARPU_PTHREAD_MUTEX_DESTROY(&wrapper.lock);
  269. /* At that moment, the caller holds a reference to the piece of data.
  270. * We enqueue the "post" sync task in the list associated to the handle
  271. * so that it is submitted by the starpu_data_release
  272. * function. */
  273. _starpu_add_post_sync_tasks(wrapper.post_sync_task, handle);
  274. _STARPU_LOG_OUT();
  275. return 0;
  276. }
  277. int starpu_data_acquire(starpu_data_handle_t handle, enum starpu_data_access_mode mode)
  278. {
  279. return starpu_data_acquire_on_node(handle, STARPU_MAIN_RAM, mode);
  280. }
  281. /* This function must be called after starpu_data_acquire so that the
  282. * application release the data */
  283. void starpu_data_release_on_node(starpu_data_handle_t handle, int node)
  284. {
  285. STARPU_ASSERT(handle);
  286. /* In case there are some implicit dependencies, unlock the "post sync" tasks */
  287. _starpu_unlock_post_sync_tasks(handle);
  288. /* The application can now release the rw-lock */
  289. if (node >= 0)
  290. _starpu_release_data_on_node(handle, 0, &handle->per_node[node]);
  291. else
  292. {
  293. _starpu_spin_lock(&handle->header_lock);
  294. if (!_starpu_notify_data_dependencies(handle))
  295. _starpu_spin_unlock(&handle->header_lock);
  296. }
  297. }
  298. void starpu_data_release(starpu_data_handle_t handle)
  299. {
  300. starpu_data_release_on_node(handle, STARPU_MAIN_RAM);
  301. }
  302. static void _prefetch_data_on_node(void *arg)
  303. {
  304. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  305. starpu_data_handle_t handle = wrapper->handle;
  306. int ret;
  307. struct _starpu_data_replicate *replicate = &handle->per_node[wrapper->node];
  308. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, wrapper->async, wrapper->async, NULL, NULL);
  309. STARPU_ASSERT(!ret);
  310. if (wrapper->async)
  311. free(wrapper);
  312. else
  313. {
  314. STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  315. wrapper->finished = 1;
  316. STARPU_PTHREAD_COND_SIGNAL(&wrapper->cond);
  317. STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  318. }
  319. _starpu_spin_lock(&handle->header_lock);
  320. if (!_starpu_notify_data_dependencies(handle))
  321. _starpu_spin_unlock(&handle->header_lock);
  322. }
  323. static
  324. int _starpu_prefetch_data_on_node_with_mode(starpu_data_handle_t handle, unsigned node, unsigned async, enum starpu_data_access_mode mode)
  325. {
  326. STARPU_ASSERT(handle);
  327. /* it is forbidden to call this function from a callback or a codelet */
  328. STARPU_ASSERT_MSG(async || _starpu_worker_may_perform_blocking_calls(), "Synchronous prefetch is not possible from a task or a callback");
  329. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) malloc(sizeof(*wrapper));
  330. wrapper->handle = handle;
  331. wrapper->node = node;
  332. wrapper->async = async;
  333. STARPU_PTHREAD_COND_INIT(&wrapper->cond, NULL);
  334. STARPU_PTHREAD_MUTEX_INIT(&wrapper->lock, NULL);
  335. wrapper->finished = 0;
  336. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _prefetch_data_on_node, wrapper))
  337. {
  338. /* we can immediately proceed */
  339. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  340. STARPU_PTHREAD_COND_DESTROY(&wrapper->cond);
  341. STARPU_PTHREAD_MUTEX_DESTROY(&wrapper->lock);
  342. free(wrapper);
  343. _starpu_fetch_data_on_node(handle, replicate, mode, async, async, NULL, NULL);
  344. /* remove the "lock"/reference */
  345. _starpu_spin_lock(&handle->header_lock);
  346. if (!async)
  347. {
  348. /* Release our refcnt, like _starpu_release_data_on_node would do */
  349. replicate->refcnt--;
  350. STARPU_ASSERT(replicate->refcnt >= 0);
  351. STARPU_ASSERT(handle->busy_count > 0);
  352. handle->busy_count--;
  353. }
  354. /* In case there was a temporary handle (eg. used for reduction), this
  355. * handle may have requested to be destroyed when the data is released
  356. * */
  357. if (!_starpu_notify_data_dependencies(handle))
  358. _starpu_spin_unlock(&handle->header_lock);
  359. }
  360. else if (!async)
  361. {
  362. STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  363. while (!wrapper->finished)
  364. STARPU_PTHREAD_COND_WAIT(&wrapper->cond, &wrapper->lock);
  365. STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  366. STARPU_PTHREAD_COND_DESTROY(&wrapper->cond);
  367. STARPU_PTHREAD_MUTEX_DESTROY(&wrapper->lock);
  368. free(wrapper);
  369. }
  370. return 0;
  371. }
  372. int starpu_data_prefetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async)
  373. {
  374. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R);
  375. }
  376. /*
  377. * It is possible to specify that a piece of data can be discarded without
  378. * impacting the application.
  379. */
  380. int _starpu_has_not_important_data;
  381. void starpu_data_advise_as_important(starpu_data_handle_t handle, unsigned is_important)
  382. {
  383. if (!is_important)
  384. _starpu_has_not_important_data = 1;
  385. _starpu_spin_lock(&handle->header_lock);
  386. /* first take all the children lock (in order !) */
  387. unsigned child;
  388. for (child = 0; child < handle->nchildren; child++)
  389. {
  390. /* make sure the intermediate children is advised as well */
  391. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  392. if (child_handle->nchildren > 0)
  393. starpu_data_advise_as_important(child_handle, is_important);
  394. }
  395. handle->is_not_important = !is_important;
  396. /* now the parent may be used again so we release the lock */
  397. _starpu_spin_unlock(&handle->header_lock);
  398. }
  399. void starpu_data_set_sequential_consistency_flag(starpu_data_handle_t handle, unsigned flag)
  400. {
  401. _starpu_spin_lock(&handle->header_lock);
  402. unsigned child;
  403. for (child = 0; child < handle->nchildren; child++)
  404. {
  405. /* make sure that the flags are applied to the children as well */
  406. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  407. if (child_handle->nchildren > 0)
  408. starpu_data_set_sequential_consistency_flag(child_handle, flag);
  409. }
  410. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  411. handle->sequential_consistency = flag;
  412. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  413. _starpu_spin_unlock(&handle->header_lock);
  414. }
  415. unsigned starpu_data_get_sequential_consistency_flag(starpu_data_handle_t handle)
  416. {
  417. return handle->sequential_consistency;
  418. }
  419. /* By default, sequential consistency is enabled */
  420. static unsigned default_sequential_consistency_flag = 1;
  421. unsigned starpu_data_get_default_sequential_consistency_flag(void)
  422. {
  423. return default_sequential_consistency_flag;
  424. }
  425. void starpu_data_set_default_sequential_consistency_flag(unsigned flag)
  426. {
  427. default_sequential_consistency_flag = flag;
  428. }
  429. /* Query the status of the handle on the specified memory node. */
  430. void starpu_data_query_status(starpu_data_handle_t handle, int memory_node, int *is_allocated, int *is_valid, int *is_requested)
  431. {
  432. // XXX : this is just a hint, so we don't take the lock ...
  433. // _starpu_spin_lock(&handle->header_lock);
  434. if (is_allocated)
  435. *is_allocated = handle->per_node[memory_node].allocated;
  436. if (is_valid)
  437. *is_valid = (handle->per_node[memory_node].state != STARPU_INVALID);
  438. if (is_requested)
  439. {
  440. int requested = 0;
  441. unsigned node;
  442. for (node = 0; node < STARPU_MAXNODES; node++)
  443. {
  444. if (handle->per_node[memory_node].requested & (1UL << node))
  445. {
  446. requested = 1;
  447. break;
  448. }
  449. }
  450. *is_requested = requested;
  451. }
  452. // _starpu_spin_unlock(&handle->header_lock);
  453. }