user_interactions.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <common/config.h>
  17. #include <common/utils.h>
  18. #include <core/task.h>
  19. #include <datawizard/coherency.h>
  20. #include <datawizard/copy_driver.h>
  21. #include <datawizard/write_back.h>
  22. #include <core/dependencies/data_concurrency.h>
  23. #include <core/sched_policy.h>
  24. #include <datawizard/memory_nodes.h>
  25. static void _starpu_data_check_initialized(starpu_data_handle_t handle, enum starpu_data_access_mode mode)
  26. {
  27. if (!(mode & STARPU_R))
  28. return;
  29. if (!handle->initialized && handle->init_cl)
  30. {
  31. int ret = starpu_task_insert(handle->init_cl, STARPU_W, handle, 0);
  32. STARPU_ASSERT(ret == 0);
  33. }
  34. STARPU_ASSERT_MSG(handle->initialized, "handle %p is not initialized while trying to read it\n", handle);
  35. }
  36. /* Explicitly ask StarPU to allocate room for a piece of data on the specified
  37. * memory node. */
  38. int starpu_data_request_allocation(starpu_data_handle_t handle, unsigned node)
  39. {
  40. struct _starpu_data_request *r;
  41. STARPU_ASSERT(handle);
  42. _starpu_spin_lock(&handle->header_lock);
  43. r = _starpu_create_data_request(handle, NULL, &handle->per_node[node], node, STARPU_NONE, 0, STARPU_PREFETCH, 0, 0, "starpu_data_request_allocation");
  44. /* we do not increase the refcnt associated to the request since we are
  45. * not waiting for its termination */
  46. _starpu_post_data_request(r);
  47. _starpu_spin_unlock(&handle->header_lock);
  48. return 0;
  49. }
  50. struct user_interaction_wrapper
  51. {
  52. starpu_data_handle_t handle;
  53. enum starpu_data_access_mode mode;
  54. int node;
  55. starpu_pthread_cond_t cond;
  56. starpu_pthread_mutex_t lock;
  57. unsigned finished;
  58. unsigned detached;
  59. enum _starpu_is_prefetch prefetch;
  60. unsigned async;
  61. int prio;
  62. void (*callback)(void *);
  63. void (*callback_fetch_data)(void *); // called after fetch_data
  64. void *callback_arg;
  65. struct starpu_task *pre_sync_task;
  66. struct starpu_task *post_sync_task;
  67. };
  68. static inline void _starpu_data_acquire_wrapper_init(struct user_interaction_wrapper *wrapper, starpu_data_handle_t handle, int node, enum starpu_data_access_mode mode)
  69. {
  70. memset(wrapper, 0, sizeof(*wrapper));
  71. wrapper->handle = handle;
  72. wrapper->node = node;
  73. wrapper->mode = mode;
  74. //wrapper->finished = 0;
  75. STARPU_PTHREAD_COND_INIT0(&wrapper->cond, NULL);
  76. STARPU_PTHREAD_MUTEX_INIT0(&wrapper->lock, NULL);
  77. }
  78. /* Called to signal completion of asynchronous data acquisition */
  79. static inline void _starpu_data_acquire_wrapper_finished(struct user_interaction_wrapper *wrapper)
  80. {
  81. STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  82. wrapper->finished = 1;
  83. STARPU_PTHREAD_COND_SIGNAL(&wrapper->cond);
  84. STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  85. }
  86. /* Called to wait for completion of asynchronous data acquisition */
  87. static inline void _starpu_data_acquire_wrapper_wait(struct user_interaction_wrapper *wrapper)
  88. {
  89. STARPU_PTHREAD_MUTEX_LOCK(&wrapper->lock);
  90. while (!wrapper->finished)
  91. STARPU_PTHREAD_COND_WAIT(&wrapper->cond, &wrapper->lock);
  92. STARPU_PTHREAD_MUTEX_UNLOCK(&wrapper->lock);
  93. }
  94. static inline void _starpu_data_acquire_wrapper_fini(struct user_interaction_wrapper *wrapper)
  95. {
  96. STARPU_PTHREAD_COND_DESTROY(&wrapper->cond);
  97. STARPU_PTHREAD_MUTEX_DESTROY(&wrapper->lock);
  98. }
  99. /* Called when the fetch into target memory is done, we're done! */
  100. static inline void _starpu_data_acquire_fetch_done(struct user_interaction_wrapper *wrapper)
  101. {
  102. if (wrapper->node >= 0)
  103. {
  104. struct _starpu_data_replicate *replicate = &wrapper->handle->per_node[wrapper->node];
  105. if (replicate->mc)
  106. replicate->mc->diduse = 1;
  107. }
  108. }
  109. /* Called when the data acquisition is done, to launch the fetch into target memory */
  110. static inline void _starpu_data_acquire_launch_fetch(struct user_interaction_wrapper *wrapper, int async, void (*callback)(void *), void *callback_arg)
  111. {
  112. int node = wrapper->node;
  113. starpu_data_handle_t handle = wrapper->handle;
  114. struct _starpu_data_replicate *replicate = node >= 0 ? &handle->per_node[node] : NULL;
  115. int ret = _starpu_fetch_data_on_node(handle, node, replicate, wrapper->mode, wrapper->detached, wrapper->prefetch, async, callback, callback_arg, wrapper->prio, "_starpu_data_acquire_launch_fetch");
  116. STARPU_ASSERT(!ret);
  117. }
  118. /*
  119. * Non Blocking data request from application
  120. */
  121. /* Called when fetch is done, call the callback */
  122. static void _starpu_data_acquire_fetch_data_callback(void *arg)
  123. {
  124. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  125. starpu_data_handle_t handle = wrapper->handle;
  126. /* At that moment, the caller holds a reference to the piece of data.
  127. * We enqueue the "post" sync task in the list associated to the handle
  128. * so that it is submitted by the starpu_data_release
  129. * function. */
  130. if (wrapper->post_sync_task)
  131. _starpu_add_post_sync_tasks(wrapper->post_sync_task, handle);
  132. _starpu_data_acquire_fetch_done(wrapper);
  133. wrapper->callback(wrapper->callback_arg);
  134. _starpu_data_acquire_wrapper_fini(wrapper);
  135. free(wrapper);
  136. }
  137. /* Called when the data acquisition is done, launch the fetch into target memory */
  138. static void _starpu_data_acquire_continuation_non_blocking(void *arg)
  139. {
  140. _starpu_data_acquire_launch_fetch(arg, 1, _starpu_data_acquire_fetch_data_callback, arg);
  141. }
  142. /* Called when the implicit data dependencies are done, launch the data acquisition */
  143. static void starpu_data_acquire_cb_pre_sync_callback(void *arg)
  144. {
  145. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  146. /* we try to get the data, if we do not succeed immediately, we set a
  147. * callback function that will be executed automatically when the data is
  148. * available again, otherwise we fetch the data directly */
  149. if (!_starpu_attempt_to_submit_data_request_from_apps(wrapper->handle, wrapper->mode,
  150. _starpu_data_acquire_continuation_non_blocking, wrapper))
  151. {
  152. /* no one has locked this data yet, so we proceed immediately */
  153. _starpu_data_acquire_continuation_non_blocking(wrapper);
  154. }
  155. }
  156. /* The data must be released by calling starpu_data_release later on */
  157. int starpu_data_acquire_on_node_cb_sequential_consistency_sync_jobids(starpu_data_handle_t handle, int node,
  158. enum starpu_data_access_mode mode, void (*callback)(void *), void *arg,
  159. int sequential_consistency, int quick,
  160. long *pre_sync_jobid, long *post_sync_jobid)
  161. {
  162. STARPU_ASSERT(handle);
  163. STARPU_ASSERT_MSG(handle->nchildren == 0, "Acquiring a partitioned data (%p) is not possible", handle);
  164. _STARPU_LOG_IN();
  165. /* Check that previous tasks have set a value if needed */
  166. _starpu_data_check_initialized(handle, mode);
  167. struct user_interaction_wrapper *wrapper;
  168. _STARPU_MALLOC(wrapper, sizeof(struct user_interaction_wrapper));
  169. _starpu_data_acquire_wrapper_init(wrapper, handle, node, mode);
  170. wrapper->async = 1;
  171. wrapper->callback = callback;
  172. wrapper->callback_arg = arg;
  173. wrapper->pre_sync_task = NULL;
  174. wrapper->post_sync_task = NULL;
  175. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  176. int handle_sequential_consistency = handle->sequential_consistency;
  177. if (handle_sequential_consistency && sequential_consistency)
  178. {
  179. struct starpu_task *new_task;
  180. struct _starpu_job *pre_sync_job, *post_sync_job;
  181. int submit_pre_sync = 0;
  182. wrapper->pre_sync_task = starpu_task_create();
  183. wrapper->pre_sync_task->name = "_starpu_data_acquire_cb_pre";
  184. wrapper->pre_sync_task->detach = 1;
  185. wrapper->pre_sync_task->callback_func = starpu_data_acquire_cb_pre_sync_callback;
  186. wrapper->pre_sync_task->callback_arg = wrapper;
  187. wrapper->pre_sync_task->type = STARPU_TASK_TYPE_DATA_ACQUIRE;
  188. pre_sync_job = _starpu_get_job_associated_to_task(wrapper->pre_sync_task);
  189. if (pre_sync_jobid)
  190. *pre_sync_jobid = pre_sync_job->job_id;
  191. wrapper->post_sync_task = starpu_task_create();
  192. wrapper->post_sync_task->name = "_starpu_data_acquire_cb_release";
  193. wrapper->post_sync_task->detach = 1;
  194. wrapper->post_sync_task->type = STARPU_TASK_TYPE_DATA_ACQUIRE;
  195. post_sync_job = _starpu_get_job_associated_to_task(wrapper->post_sync_task);
  196. if (post_sync_jobid)
  197. *post_sync_jobid = post_sync_job->job_id;
  198. if (quick)
  199. pre_sync_job->quick_next = post_sync_job;
  200. new_task = _starpu_detect_implicit_data_deps_with_handle(wrapper->pre_sync_task, &submit_pre_sync, wrapper->post_sync_task, &_starpu_get_job_associated_to_task(wrapper->post_sync_task)->implicit_dep_slot, handle, mode, sequential_consistency);
  201. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  202. if (STARPU_UNLIKELY(new_task))
  203. {
  204. int ret = _starpu_task_submit_internally(new_task);
  205. STARPU_ASSERT(!ret);
  206. }
  207. if (submit_pre_sync)
  208. {
  209. int ret = _starpu_task_submit_internally(wrapper->pre_sync_task);
  210. STARPU_ASSERT(!ret);
  211. }
  212. else
  213. {
  214. wrapper->pre_sync_task->detach = 0;
  215. starpu_task_destroy(wrapper->pre_sync_task);
  216. starpu_data_acquire_cb_pre_sync_callback(wrapper);
  217. }
  218. }
  219. else
  220. {
  221. if (pre_sync_jobid)
  222. *pre_sync_jobid = -1;
  223. if (post_sync_jobid)
  224. *post_sync_jobid = -1;
  225. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  226. starpu_data_acquire_cb_pre_sync_callback(wrapper);
  227. }
  228. _STARPU_LOG_OUT();
  229. return 0;
  230. }
  231. int starpu_data_acquire_on_node_cb_sequential_consistency_quick(starpu_data_handle_t handle, int node,
  232. enum starpu_data_access_mode mode, void (*callback)(void *), void *arg,
  233. int sequential_consistency, int quick)
  234. {
  235. return starpu_data_acquire_on_node_cb_sequential_consistency_sync_jobids(handle, node, mode, callback, arg, sequential_consistency, quick, NULL, NULL);
  236. }
  237. int starpu_data_acquire_on_node_cb_sequential_consistency(starpu_data_handle_t handle, int node,
  238. enum starpu_data_access_mode mode, void (*callback)(void *), void *arg,
  239. int sequential_consistency)
  240. {
  241. return starpu_data_acquire_on_node_cb_sequential_consistency_quick(handle, node, mode, callback, arg, sequential_consistency, 0);
  242. }
  243. int starpu_data_acquire_on_node_cb(starpu_data_handle_t handle, int node,
  244. enum starpu_data_access_mode mode, void (*callback)(void *), void *arg)
  245. {
  246. return starpu_data_acquire_on_node_cb_sequential_consistency(handle, node, mode, callback, arg, 1);
  247. }
  248. int starpu_data_acquire_cb(starpu_data_handle_t handle,
  249. enum starpu_data_access_mode mode, void (*callback)(void *), void *arg)
  250. {
  251. int home_node = handle->home_node;
  252. if (home_node < 0)
  253. home_node = STARPU_MAIN_RAM;
  254. return starpu_data_acquire_on_node_cb(handle, home_node, mode, callback, arg);
  255. }
  256. int starpu_data_acquire_cb_sequential_consistency(starpu_data_handle_t handle,
  257. enum starpu_data_access_mode mode, void (*callback)(void *), void *arg, int sequential_consistency)
  258. {
  259. int home_node = handle->home_node;
  260. if (home_node < 0)
  261. home_node = STARPU_MAIN_RAM;
  262. return starpu_data_acquire_on_node_cb_sequential_consistency(handle, home_node, mode, callback, arg, sequential_consistency);
  263. }
  264. /*
  265. * Blocking data request from application
  266. */
  267. static inline void _starpu_data_acquire_continuation(void *arg)
  268. {
  269. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  270. starpu_data_handle_t handle = wrapper->handle;
  271. STARPU_ASSERT(handle);
  272. _starpu_data_acquire_launch_fetch(wrapper, 0, NULL, NULL);
  273. _starpu_data_acquire_fetch_done(wrapper);
  274. _starpu_data_acquire_wrapper_finished(wrapper);
  275. }
  276. /* The data must be released by calling starpu_data_release later on */
  277. int starpu_data_acquire_on_node(starpu_data_handle_t handle, int node, enum starpu_data_access_mode mode)
  278. {
  279. STARPU_ASSERT(handle);
  280. STARPU_ASSERT_MSG(handle->nchildren == 0, "Acquiring a partitioned data is not possible");
  281. _STARPU_LOG_IN();
  282. /* unless asynchronous, it is forbidden to call this function from a callback or a codelet */
  283. 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.");
  284. /* Check that previous tasks have set a value if needed */
  285. _starpu_data_check_initialized(handle, mode);
  286. if (node >= 0 && _starpu_data_is_multiformat_handle(handle) &&
  287. _starpu_handle_needs_conversion_task(handle, node))
  288. {
  289. struct starpu_task *task = _starpu_create_conversion_task(handle, node);
  290. int ret;
  291. _starpu_spin_lock(&handle->header_lock);
  292. handle->refcnt--;
  293. handle->busy_count--;
  294. handle->mf_node = node;
  295. _starpu_spin_unlock(&handle->header_lock);
  296. task->synchronous = 1;
  297. ret = _starpu_task_submit_internally(task);
  298. STARPU_ASSERT(!ret);
  299. }
  300. struct user_interaction_wrapper wrapper;
  301. _starpu_data_acquire_wrapper_init(&wrapper, handle, node, mode);
  302. // _STARPU_DEBUG("TAKE sequential_consistency_mutex starpu_data_acquire\n");
  303. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  304. int sequential_consistency = handle->sequential_consistency;
  305. if (sequential_consistency)
  306. {
  307. struct starpu_task *new_task;
  308. int submit_pre_sync = 0;
  309. wrapper.pre_sync_task = starpu_task_create();
  310. wrapper.pre_sync_task->name = "_starpu_data_acquire_pre";
  311. wrapper.pre_sync_task->detach = 0;
  312. wrapper.pre_sync_task->type = STARPU_TASK_TYPE_DATA_ACQUIRE;
  313. wrapper.post_sync_task = starpu_task_create();
  314. wrapper.post_sync_task->name = "_starpu_data_acquire_post";
  315. wrapper.post_sync_task->detach = 1;
  316. wrapper.post_sync_task->type = STARPU_TASK_TYPE_DATA_ACQUIRE;
  317. new_task = _starpu_detect_implicit_data_deps_with_handle(wrapper.pre_sync_task, &submit_pre_sync, wrapper.post_sync_task, &_starpu_get_job_associated_to_task(wrapper.post_sync_task)->implicit_dep_slot, handle, mode, sequential_consistency);
  318. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  319. if (STARPU_UNLIKELY(new_task))
  320. {
  321. int ret = _starpu_task_submit_internally(new_task);
  322. STARPU_ASSERT(!ret);
  323. }
  324. if (submit_pre_sync)
  325. {
  326. wrapper.pre_sync_task->synchronous = 1;
  327. int ret = _starpu_task_submit_internally(wrapper.pre_sync_task);
  328. STARPU_ASSERT(!ret);
  329. }
  330. else
  331. {
  332. wrapper.pre_sync_task->detach = 0;
  333. starpu_task_destroy(wrapper.pre_sync_task);
  334. }
  335. }
  336. else
  337. {
  338. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  339. }
  340. /* we try to get the data, if we do not succeed immediately, we set a
  341. * callback function that will be executed automatically when the data is
  342. * available again, otherwise we fetch the data directly */
  343. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _starpu_data_acquire_continuation, &wrapper))
  344. {
  345. /* no one has locked this data yet, so we proceed immediately */
  346. _starpu_data_acquire_launch_fetch(&wrapper, 0, NULL, NULL);
  347. _starpu_data_acquire_fetch_done(&wrapper);
  348. }
  349. else
  350. {
  351. _starpu_data_acquire_wrapper_wait(&wrapper);
  352. }
  353. _starpu_data_acquire_wrapper_fini(&wrapper);
  354. /* At that moment, the caller holds a reference to the piece of data.
  355. * We enqueue the "post" sync task in the list associated to the handle
  356. * so that it is submitted by the starpu_data_release
  357. * function. */
  358. if (sequential_consistency)
  359. _starpu_add_post_sync_tasks(wrapper.post_sync_task, handle);
  360. _STARPU_LOG_OUT();
  361. return 0;
  362. }
  363. int starpu_data_acquire(starpu_data_handle_t handle, enum starpu_data_access_mode mode)
  364. {
  365. int home_node = handle->home_node;
  366. if (home_node < 0)
  367. home_node = STARPU_MAIN_RAM;
  368. return starpu_data_acquire_on_node(handle, home_node, mode);
  369. }
  370. int starpu_data_acquire_on_node_try(starpu_data_handle_t handle, int node, enum starpu_data_access_mode mode)
  371. {
  372. STARPU_ASSERT(handle);
  373. STARPU_ASSERT_MSG(handle->nchildren == 0, "Acquiring a partitioned data is not possible");
  374. /* it is forbidden to call this function from a callback or a codelet */
  375. 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.");
  376. /* Check that previous tasks have set a value if needed */
  377. _starpu_data_check_initialized(handle, mode);
  378. int ret;
  379. STARPU_ASSERT_MSG(!_starpu_data_is_multiformat_handle(handle), "not supported yet");
  380. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  381. ret = _starpu_test_implicit_data_deps_with_handle(handle, mode);
  382. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  383. if (ret)
  384. return ret;
  385. struct user_interaction_wrapper wrapper;
  386. _starpu_data_acquire_wrapper_init(&wrapper, handle, node, mode);
  387. /* we try to get the data, if we do not succeed immediately, we set a
  388. * callback function that will be executed automatically when the data is
  389. * available again, otherwise we fetch the data directly */
  390. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _starpu_data_acquire_continuation, &wrapper))
  391. {
  392. /* no one has locked this data yet, so we proceed immediately */
  393. _starpu_data_acquire_launch_fetch(&wrapper, 0, NULL, NULL);
  394. _starpu_data_acquire_fetch_done(&wrapper);
  395. }
  396. else
  397. {
  398. _starpu_data_acquire_wrapper_wait(&wrapper);
  399. }
  400. _starpu_data_acquire_wrapper_fini(&wrapper);
  401. return 0;
  402. }
  403. int starpu_data_acquire_try(starpu_data_handle_t handle, enum starpu_data_access_mode mode)
  404. {
  405. return starpu_data_acquire_on_node_try(handle, STARPU_MAIN_RAM, mode);
  406. }
  407. /* This function must be called after starpu_data_acquire so that the
  408. * application release the data */
  409. void starpu_data_release_to_on_node(starpu_data_handle_t handle, enum starpu_data_access_mode mode, int node)
  410. {
  411. STARPU_ASSERT(handle);
  412. STARPU_ASSERT_MSG(mode == STARPU_NONE ||
  413. (mode == STARPU_R &&
  414. (handle->current_mode == STARPU_RW ||
  415. handle->current_mode == STARPU_W)),
  416. "We only support releasing from W or RW to R");
  417. /* In case there are some implicit dependencies, unlock the "post sync" tasks */
  418. _starpu_unlock_post_sync_tasks(handle);
  419. /* The application can now release the rw-lock */
  420. if (node >= 0)
  421. _starpu_release_data_on_node(handle, 0, mode, &handle->per_node[node]);
  422. else
  423. {
  424. _starpu_spin_lock(&handle->header_lock);
  425. if (node == STARPU_ACQUIRE_NO_NODE_LOCK_ALL)
  426. {
  427. int i;
  428. for (i = 0; i < STARPU_MAXNODES; i++)
  429. handle->per_node[i].refcnt--;
  430. }
  431. handle->busy_count--;
  432. if (!_starpu_notify_data_dependencies(handle, mode))
  433. _starpu_spin_unlock(&handle->header_lock);
  434. }
  435. }
  436. void starpu_data_release_on_node(starpu_data_handle_t handle, int node)
  437. {
  438. starpu_data_release_to_on_node(handle, STARPU_NONE, node);
  439. }
  440. void starpu_data_release_to(starpu_data_handle_t handle, enum starpu_data_access_mode mode)
  441. {
  442. int home_node = handle->home_node;
  443. if (home_node < 0)
  444. home_node = STARPU_MAIN_RAM;
  445. starpu_data_release_to_on_node(handle, mode, home_node);
  446. }
  447. void starpu_data_release(starpu_data_handle_t handle)
  448. {
  449. starpu_data_release_to(handle, STARPU_NONE);
  450. }
  451. static void _prefetch_data_on_node(void *arg)
  452. {
  453. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  454. starpu_data_handle_t handle = wrapper->handle;
  455. _starpu_data_acquire_launch_fetch(wrapper, wrapper->async, NULL, NULL);
  456. if (wrapper->async)
  457. free(wrapper);
  458. else
  459. _starpu_data_acquire_wrapper_finished(wrapper);
  460. _starpu_spin_lock(&handle->header_lock);
  461. if (!_starpu_notify_data_dependencies(handle, STARPU_NONE))
  462. _starpu_spin_unlock(&handle->header_lock);
  463. }
  464. static
  465. int _starpu_prefetch_data_on_node_with_mode(starpu_data_handle_t handle, unsigned node, unsigned async, enum starpu_data_access_mode mode, enum _starpu_is_prefetch prefetch, int prio)
  466. {
  467. STARPU_ASSERT(handle);
  468. /* it is forbidden to call this function from a callback or a codelet */
  469. STARPU_ASSERT_MSG(async || _starpu_worker_may_perform_blocking_calls(), "Synchronous prefetch is not possible from a task or a callback");
  470. /* Check that previous tasks have set a value if needed */
  471. _starpu_data_check_initialized(handle, mode);
  472. struct user_interaction_wrapper *wrapper;
  473. _STARPU_MALLOC(wrapper, sizeof(*wrapper));
  474. _starpu_data_acquire_wrapper_init(wrapper, handle, node, STARPU_R);
  475. wrapper->detached = async;
  476. wrapper->prefetch = prefetch;
  477. wrapper->async = async;
  478. wrapper->prio = prio;
  479. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _prefetch_data_on_node, wrapper))
  480. {
  481. /* we can immediately proceed */
  482. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  483. _starpu_data_acquire_launch_fetch(wrapper, async, NULL, NULL);
  484. _starpu_data_acquire_wrapper_fini(wrapper);
  485. free(wrapper);
  486. /* remove the "lock"/reference */
  487. _starpu_spin_lock(&handle->header_lock);
  488. if (!async)
  489. {
  490. /* Release our refcnt, like _starpu_release_data_on_node would do */
  491. replicate->refcnt--;
  492. STARPU_ASSERT(replicate->refcnt >= 0);
  493. STARPU_ASSERT(handle->busy_count > 0);
  494. handle->busy_count--;
  495. }
  496. /* In case there was a temporary handle (eg. used for reduction), this
  497. * handle may have requested to be destroyed when the data is released
  498. * */
  499. if (!_starpu_notify_data_dependencies(handle, STARPU_NONE))
  500. _starpu_spin_unlock(&handle->header_lock);
  501. }
  502. else if (!async)
  503. {
  504. _starpu_data_acquire_wrapper_wait(wrapper);
  505. _starpu_data_acquire_wrapper_fini(wrapper);
  506. free(wrapper);
  507. }
  508. return 0;
  509. }
  510. int starpu_data_fetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async)
  511. {
  512. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R, STARPU_FETCH, 0);
  513. }
  514. int starpu_data_prefetch_on_node_prio(starpu_data_handle_t handle, unsigned node, unsigned async, int prio)
  515. {
  516. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R, STARPU_PREFETCH, prio);
  517. }
  518. int starpu_data_prefetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async)
  519. {
  520. return starpu_data_prefetch_on_node_prio(handle, node, async, 0);
  521. }
  522. int starpu_data_idle_prefetch_on_node_prio(starpu_data_handle_t handle, unsigned node, unsigned async, int prio)
  523. {
  524. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R, STARPU_IDLEFETCH, prio);
  525. }
  526. int starpu_data_idle_prefetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async)
  527. {
  528. return starpu_data_idle_prefetch_on_node_prio(handle, node, async, 0);
  529. }
  530. static void _starpu_data_wont_use(void *data)
  531. {
  532. unsigned node;
  533. starpu_data_handle_t handle = data;
  534. _STARPU_TRACE_DATA_DOING_WONT_USE(handle);
  535. _starpu_spin_lock(&handle->header_lock);
  536. for (node = 0; node < STARPU_MAXNODES; node++)
  537. {
  538. struct _starpu_data_replicate *local = &handle->per_node[node];
  539. if (local->allocated && local->automatically_allocated)
  540. _starpu_memchunk_wont_use(local->mc, node);
  541. }
  542. if (handle->per_worker)
  543. {
  544. unsigned nworkers = starpu_worker_get_count();
  545. unsigned worker;
  546. for (worker = 0; worker < nworkers; worker++)
  547. {
  548. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  549. if (local->allocated && local->automatically_allocated)
  550. _starpu_memchunk_wont_use(local->mc, starpu_worker_get_memory_node(worker));
  551. }
  552. }
  553. _starpu_spin_unlock(&handle->header_lock);
  554. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  555. if (handle->home_node != -1)
  556. starpu_data_idle_prefetch_on_node(handle, handle->home_node, 1);
  557. else
  558. {
  559. if (handle->ooc)
  560. {
  561. /* Try to push it to some disk */
  562. unsigned i;
  563. unsigned nnodes = starpu_memory_nodes_get_count();
  564. for (i = 0; i < nnodes; i++)
  565. {
  566. if (starpu_node_get_kind(i) == STARPU_DISK_RAM)
  567. starpu_data_idle_prefetch_on_node(handle, i, 1);
  568. }
  569. }
  570. }
  571. }
  572. void starpu_data_wont_use(starpu_data_handle_t handle)
  573. {
  574. _STARPU_TRACE_DATA_WONT_USE(handle);
  575. starpu_data_acquire_on_node_cb_sequential_consistency_quick(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_R, _starpu_data_wont_use, handle, 1, 1);
  576. }
  577. /*
  578. * It is possible to specify that a piece of data can be discarded without
  579. * impacting the application.
  580. */
  581. int _starpu_has_not_important_data;
  582. void starpu_data_advise_as_important(starpu_data_handle_t handle, unsigned is_important)
  583. {
  584. if (!is_important)
  585. _starpu_has_not_important_data = 1;
  586. _starpu_spin_lock(&handle->header_lock);
  587. /* first take all the children lock (in order !) */
  588. unsigned child;
  589. for (child = 0; child < handle->nchildren; child++)
  590. {
  591. /* make sure the intermediate children is advised as well */
  592. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  593. if (child_handle->nchildren > 0)
  594. starpu_data_advise_as_important(child_handle, is_important);
  595. }
  596. handle->is_not_important = !is_important;
  597. /* now the parent may be used again so we release the lock */
  598. _starpu_spin_unlock(&handle->header_lock);
  599. }
  600. void starpu_data_set_sequential_consistency_flag(starpu_data_handle_t handle, unsigned flag)
  601. {
  602. _starpu_spin_lock(&handle->header_lock);
  603. unsigned child;
  604. for (child = 0; child < handle->nchildren; child++)
  605. {
  606. /* make sure that the flags are applied to the children as well */
  607. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  608. if (child_handle->nchildren > 0)
  609. starpu_data_set_sequential_consistency_flag(child_handle, flag);
  610. }
  611. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  612. handle->sequential_consistency = flag;
  613. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  614. _starpu_spin_unlock(&handle->header_lock);
  615. }
  616. unsigned starpu_data_get_sequential_consistency_flag(starpu_data_handle_t handle)
  617. {
  618. return handle->sequential_consistency;
  619. }
  620. void starpu_data_set_ooc_flag(starpu_data_handle_t handle, unsigned flag)
  621. {
  622. handle->ooc = flag;
  623. }
  624. unsigned starpu_data_get_ooc_flag(starpu_data_handle_t handle)
  625. {
  626. return handle->ooc;
  627. }
  628. /* By default, sequential consistency is enabled */
  629. static unsigned default_sequential_consistency_flag = 1;
  630. unsigned starpu_data_get_default_sequential_consistency_flag(void)
  631. {
  632. return default_sequential_consistency_flag;
  633. }
  634. void starpu_data_set_default_sequential_consistency_flag(unsigned flag)
  635. {
  636. default_sequential_consistency_flag = flag;
  637. }
  638. /* Query the status of the handle on the specified memory node. */
  639. void starpu_data_query_status(starpu_data_handle_t handle, int memory_node, int *is_allocated, int *is_valid, int *is_requested)
  640. {
  641. // XXX : this is just a hint, so we don't take the lock ...
  642. // _starpu_spin_lock(&handle->header_lock);
  643. if (is_allocated)
  644. *is_allocated = handle->per_node[memory_node].allocated;
  645. if (is_valid)
  646. *is_valid = (handle->per_node[memory_node].state != STARPU_INVALID);
  647. if (is_requested)
  648. {
  649. int requested = 0;
  650. unsigned node;
  651. for (node = 0; node < STARPU_MAXNODES; node++)
  652. {
  653. if (handle->per_node[memory_node].requested & (1UL << node))
  654. {
  655. requested = 1;
  656. break;
  657. }
  658. }
  659. *is_requested = requested;
  660. }
  661. // _starpu_spin_unlock(&handle->header_lock);
  662. }