user_interactions.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. if (mode == STARPU_RW)
  413. /* They are equivalent here, and current_mode is never STARPU_RW */
  414. mode = STARPU_W;
  415. STARPU_ASSERT_MSG(mode == STARPU_NONE ||
  416. mode == handle->current_mode ||
  417. (mode == STARPU_R &&
  418. handle->current_mode == STARPU_W),
  419. "We only support releasing from W to R");
  420. /* In case there are some implicit dependencies, unlock the "post sync" tasks */
  421. _starpu_unlock_post_sync_tasks(handle, mode);
  422. /* The application can now release the rw-lock */
  423. if (node >= 0)
  424. _starpu_release_data_on_node(handle, 0, mode, &handle->per_node[node]);
  425. else
  426. {
  427. _starpu_spin_lock(&handle->header_lock);
  428. if (node == STARPU_ACQUIRE_NO_NODE_LOCK_ALL)
  429. {
  430. int i;
  431. for (i = 0; i < STARPU_MAXNODES; i++)
  432. handle->per_node[i].refcnt--;
  433. }
  434. handle->busy_count--;
  435. if (!_starpu_notify_data_dependencies(handle, mode))
  436. _starpu_spin_unlock(&handle->header_lock);
  437. }
  438. }
  439. void starpu_data_release_on_node(starpu_data_handle_t handle, int node)
  440. {
  441. starpu_data_release_to_on_node(handle, STARPU_NONE, node);
  442. }
  443. void starpu_data_release_to(starpu_data_handle_t handle, enum starpu_data_access_mode mode)
  444. {
  445. int home_node = handle->home_node;
  446. if (home_node < 0)
  447. home_node = STARPU_MAIN_RAM;
  448. starpu_data_release_to_on_node(handle, mode, home_node);
  449. }
  450. void starpu_data_release(starpu_data_handle_t handle)
  451. {
  452. starpu_data_release_to(handle, STARPU_NONE);
  453. }
  454. static void _prefetch_data_on_node(void *arg)
  455. {
  456. struct user_interaction_wrapper *wrapper = (struct user_interaction_wrapper *) arg;
  457. starpu_data_handle_t handle = wrapper->handle;
  458. _starpu_data_acquire_launch_fetch(wrapper, wrapper->async, NULL, NULL);
  459. if (wrapper->async)
  460. free(wrapper);
  461. else
  462. _starpu_data_acquire_wrapper_finished(wrapper);
  463. _starpu_spin_lock(&handle->header_lock);
  464. if (!_starpu_notify_data_dependencies(handle, STARPU_NONE))
  465. _starpu_spin_unlock(&handle->header_lock);
  466. }
  467. static
  468. 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)
  469. {
  470. STARPU_ASSERT(handle);
  471. /* it is forbidden to call this function from a callback or a codelet */
  472. STARPU_ASSERT_MSG(async || _starpu_worker_may_perform_blocking_calls(), "Synchronous prefetch is not possible from a task or a callback");
  473. /* Check that previous tasks have set a value if needed */
  474. _starpu_data_check_initialized(handle, mode);
  475. struct user_interaction_wrapper *wrapper;
  476. _STARPU_MALLOC(wrapper, sizeof(*wrapper));
  477. _starpu_data_acquire_wrapper_init(wrapper, handle, node, STARPU_R);
  478. wrapper->detached = async;
  479. wrapper->prefetch = prefetch;
  480. wrapper->async = async;
  481. wrapper->prio = prio;
  482. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, mode, _prefetch_data_on_node, wrapper))
  483. {
  484. /* we can immediately proceed */
  485. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  486. _starpu_data_acquire_launch_fetch(wrapper, async, NULL, NULL);
  487. _starpu_data_acquire_wrapper_fini(wrapper);
  488. free(wrapper);
  489. /* remove the "lock"/reference */
  490. _starpu_spin_lock(&handle->header_lock);
  491. if (!async)
  492. {
  493. /* Release our refcnt, like _starpu_release_data_on_node would do */
  494. replicate->refcnt--;
  495. STARPU_ASSERT(replicate->refcnt >= 0);
  496. STARPU_ASSERT(handle->busy_count > 0);
  497. handle->busy_count--;
  498. }
  499. /* In case there was a temporary handle (eg. used for reduction), this
  500. * handle may have requested to be destroyed when the data is released
  501. * */
  502. if (!_starpu_notify_data_dependencies(handle, STARPU_NONE))
  503. _starpu_spin_unlock(&handle->header_lock);
  504. }
  505. else if (!async)
  506. {
  507. _starpu_data_acquire_wrapper_wait(wrapper);
  508. _starpu_data_acquire_wrapper_fini(wrapper);
  509. free(wrapper);
  510. }
  511. return 0;
  512. }
  513. int starpu_data_fetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async)
  514. {
  515. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R, STARPU_FETCH, 0);
  516. }
  517. int starpu_data_prefetch_on_node_prio(starpu_data_handle_t handle, unsigned node, unsigned async, int prio)
  518. {
  519. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R, STARPU_PREFETCH, prio);
  520. }
  521. int starpu_data_prefetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async)
  522. {
  523. return starpu_data_prefetch_on_node_prio(handle, node, async, 0);
  524. }
  525. int starpu_data_idle_prefetch_on_node_prio(starpu_data_handle_t handle, unsigned node, unsigned async, int prio)
  526. {
  527. return _starpu_prefetch_data_on_node_with_mode(handle, node, async, STARPU_R, STARPU_IDLEFETCH, prio);
  528. }
  529. int starpu_data_idle_prefetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async)
  530. {
  531. return starpu_data_idle_prefetch_on_node_prio(handle, node, async, 0);
  532. }
  533. static void _starpu_data_wont_use(void *data)
  534. {
  535. unsigned node;
  536. starpu_data_handle_t handle = data;
  537. _STARPU_TRACE_DATA_DOING_WONT_USE(handle);
  538. _starpu_spin_lock(&handle->header_lock);
  539. for (node = 0; node < STARPU_MAXNODES; node++)
  540. {
  541. struct _starpu_data_replicate *local = &handle->per_node[node];
  542. if (local->allocated && local->automatically_allocated)
  543. _starpu_memchunk_wont_use(local->mc, node);
  544. }
  545. if (handle->per_worker)
  546. {
  547. unsigned nworkers = starpu_worker_get_count();
  548. unsigned worker;
  549. for (worker = 0; worker < nworkers; worker++)
  550. {
  551. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  552. if (local->allocated && local->automatically_allocated)
  553. _starpu_memchunk_wont_use(local->mc, starpu_worker_get_memory_node(worker));
  554. }
  555. }
  556. _starpu_spin_unlock(&handle->header_lock);
  557. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  558. if (handle->home_node != -1)
  559. starpu_data_idle_prefetch_on_node(handle, handle->home_node, 1);
  560. else
  561. {
  562. if (handle->ooc)
  563. {
  564. /* Try to push it to some disk */
  565. unsigned i;
  566. unsigned nnodes = starpu_memory_nodes_get_count();
  567. for (i = 0; i < nnodes; i++)
  568. {
  569. if (starpu_node_get_kind(i) == STARPU_DISK_RAM)
  570. starpu_data_idle_prefetch_on_node(handle, i, 1);
  571. }
  572. }
  573. }
  574. }
  575. void starpu_data_wont_use(starpu_data_handle_t handle)
  576. {
  577. if (!handle->initialized)
  578. /* No value atm actually */
  579. return;
  580. if (starpu_data_get_nb_children(handle) != 0)
  581. {
  582. int i;
  583. for(i=0 ; i<starpu_data_get_nb_children(handle) ; i++)
  584. starpu_data_wont_use(starpu_data_get_child(handle, i));
  585. return;
  586. }
  587. if (handle->partitioned != 0)
  588. {
  589. unsigned i;
  590. for(i=0 ; i<handle->partitioned; i++)
  591. {
  592. unsigned j;
  593. for(j=0 ; j<handle->active_readonly_nchildren[i] ; j++)
  594. starpu_data_wont_use(handle->active_readonly_children[i][j]);
  595. }
  596. }
  597. if (handle->active_nchildren != 0)
  598. {
  599. unsigned j;
  600. for(j=0 ; j<handle->active_nchildren ; j++)
  601. starpu_data_wont_use(handle->active_children[j]);
  602. return;
  603. }
  604. _STARPU_TRACE_DATA_WONT_USE(handle);
  605. 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);
  606. }
  607. /*
  608. * It is possible to specify that a piece of data can be discarded without
  609. * impacting the application.
  610. */
  611. int _starpu_has_not_important_data;
  612. void starpu_data_advise_as_important(starpu_data_handle_t handle, unsigned is_important)
  613. {
  614. if (!is_important)
  615. _starpu_has_not_important_data = 1;
  616. _starpu_spin_lock(&handle->header_lock);
  617. /* first take all the children lock (in order !) */
  618. unsigned child;
  619. for (child = 0; child < handle->nchildren; child++)
  620. {
  621. /* make sure the intermediate children is advised as well */
  622. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  623. if (child_handle->nchildren > 0)
  624. starpu_data_advise_as_important(child_handle, is_important);
  625. }
  626. handle->is_not_important = !is_important;
  627. /* now the parent may be used again so we release the lock */
  628. _starpu_spin_unlock(&handle->header_lock);
  629. }
  630. void starpu_data_set_sequential_consistency_flag(starpu_data_handle_t handle, unsigned flag)
  631. {
  632. _starpu_spin_lock(&handle->header_lock);
  633. unsigned child;
  634. for (child = 0; child < handle->nchildren; child++)
  635. {
  636. /* make sure that the flags are applied to the children as well */
  637. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  638. if (child_handle->nchildren > 0)
  639. starpu_data_set_sequential_consistency_flag(child_handle, flag);
  640. }
  641. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  642. handle->sequential_consistency = flag;
  643. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  644. _starpu_spin_unlock(&handle->header_lock);
  645. }
  646. unsigned starpu_data_get_sequential_consistency_flag(starpu_data_handle_t handle)
  647. {
  648. return handle->sequential_consistency;
  649. }
  650. void starpu_data_set_ooc_flag(starpu_data_handle_t handle, unsigned flag)
  651. {
  652. handle->ooc = flag;
  653. }
  654. unsigned starpu_data_get_ooc_flag(starpu_data_handle_t handle)
  655. {
  656. return handle->ooc;
  657. }
  658. /* By default, sequential consistency is enabled */
  659. static unsigned default_sequential_consistency_flag = 1;
  660. unsigned starpu_data_get_default_sequential_consistency_flag(void)
  661. {
  662. return default_sequential_consistency_flag;
  663. }
  664. void starpu_data_set_default_sequential_consistency_flag(unsigned flag)
  665. {
  666. default_sequential_consistency_flag = flag;
  667. }
  668. /* Query the status of the handle on the specified memory node. */
  669. void starpu_data_query_status(starpu_data_handle_t handle, int memory_node, int *is_allocated, int *is_valid, int *is_requested)
  670. {
  671. // XXX : this is just a hint, so we don't take the lock ...
  672. // _starpu_spin_lock(&handle->header_lock);
  673. if (is_allocated)
  674. *is_allocated = handle->per_node[memory_node].allocated;
  675. if (is_valid)
  676. *is_valid = (handle->per_node[memory_node].state != STARPU_INVALID);
  677. if (is_requested)
  678. {
  679. int requested = 0;
  680. unsigned node;
  681. for (node = 0; node < STARPU_MAXNODES; node++)
  682. {
  683. if (handle->per_node[memory_node].requested & (1UL << node))
  684. {
  685. requested = 1;
  686. break;
  687. }
  688. }
  689. *is_requested = requested;
  690. }
  691. // _starpu_spin_unlock(&handle->header_lock);
  692. }