user_interactions.c 20 KB

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