implicit_data_deps.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2013, 2015 CNRS
  5. * Copyright (C) 2016 Inria
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <common/config.h>
  20. #include <core/task.h>
  21. #include <datawizard/datawizard.h>
  22. #include <profiling/bound.h>
  23. #include <core/debug.h>
  24. #if 0
  25. # define _STARPU_DEP_DEBUG(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__);
  26. #else
  27. # define _STARPU_DEP_DEBUG(fmt, ...)
  28. #endif
  29. static void _starpu_add_ghost_dependency(starpu_data_handle_t handle STARPU_ATTRIBUTE_UNUSED, unsigned long previous STARPU_ATTRIBUTE_UNUSED, struct starpu_task *next STARPU_ATTRIBUTE_UNUSED)
  30. {
  31. struct _starpu_job *next_job = _starpu_get_job_associated_to_task(next);
  32. _starpu_bound_job_id_dep(handle, next_job, previous);
  33. STARPU_AYU_ADDDEPENDENCY(previous, handle, next_job->job_id);
  34. }
  35. static void _starpu_add_dependency(starpu_data_handle_t handle STARPU_ATTRIBUTE_UNUSED, struct starpu_task *previous STARPU_ATTRIBUTE_UNUSED, struct starpu_task *next STARPU_ATTRIBUTE_UNUSED)
  36. {
  37. _starpu_add_ghost_dependency(handle, _starpu_get_job_associated_to_task(previous)->job_id, next);
  38. }
  39. /* Add pre_sync_task as new accessor among the existing ones, making it depend on the last synchronization task if any. */
  40. static void _starpu_add_accessor(starpu_data_handle_t handle, struct starpu_task *pre_sync_task, struct starpu_task *post_sync_task, struct _starpu_task_wrapper_dlist *post_sync_task_dependency_slot)
  41. {
  42. /* Add this task to the list of readers */
  43. STARPU_ASSERT(!post_sync_task_dependency_slot->prev);
  44. STARPU_ASSERT(!post_sync_task_dependency_slot->next);
  45. post_sync_task_dependency_slot->task = post_sync_task;
  46. post_sync_task_dependency_slot->next = handle->last_submitted_accessors.next;
  47. post_sync_task_dependency_slot->prev = &handle->last_submitted_accessors;
  48. post_sync_task_dependency_slot->next->prev = post_sync_task_dependency_slot;
  49. handle->last_submitted_accessors.next = post_sync_task_dependency_slot;
  50. /* This task depends on the previous synchronization task if any */
  51. if (handle->last_sync_task && handle->last_sync_task != post_sync_task)
  52. {
  53. struct starpu_task *task_array[1] = {handle->last_sync_task};
  54. _starpu_task_declare_deps_array(pre_sync_task, 1, task_array, 0);
  55. _starpu_add_dependency(handle, handle->last_sync_task, pre_sync_task);
  56. _STARPU_DEP_DEBUG("dep %p -> %p\n", handle->last_sync_task, pre_sync_task);
  57. }
  58. else
  59. {
  60. _STARPU_DEP_DEBUG("No dep\n");
  61. }
  62. /* There was perhaps no last submitted writer but a
  63. * ghost one, we should report that here, and keep the
  64. * ghost writer valid */
  65. if (
  66. (
  67. #ifdef STARPU_USE_FXT
  68. 1
  69. #else
  70. _starpu_bound_recording
  71. #endif
  72. || STARPU_AYU_EVENT
  73. ) && handle->last_submitted_ghost_sync_id_is_valid)
  74. {
  75. _STARPU_TRACE_GHOST_TASK_DEPS(handle->last_submitted_ghost_sync_id,
  76. _starpu_get_job_associated_to_task(pre_sync_task)->job_id);
  77. _starpu_add_ghost_dependency(handle, handle->last_submitted_ghost_sync_id, pre_sync_task);
  78. _STARPU_DEP_DEBUG("dep ID%lu -> %p\n", handle->last_submitted_ghost_sync_id, pre_sync_task);
  79. }
  80. if (!pre_sync_task->cl)
  81. {
  82. /* Add a reference to be released in _starpu_handle_job_termination */
  83. _starpu_spin_lock(&handle->header_lock);
  84. handle->busy_count++;
  85. _starpu_spin_unlock(&handle->header_lock);
  86. _starpu_get_job_associated_to_task(pre_sync_task)->implicit_dep_handle = handle;
  87. }
  88. }
  89. /* This adds a new synchronization task which depends on all the previous accessors */
  90. static void _starpu_add_sync_task(starpu_data_handle_t handle, struct starpu_task *pre_sync_task, struct starpu_task *post_sync_task, struct starpu_task *ignored_task)
  91. {
  92. /* Count the existing accessors */
  93. unsigned naccessors = 0;
  94. struct _starpu_task_wrapper_dlist *l;
  95. l = handle->last_submitted_accessors.next;
  96. while (l != &handle->last_submitted_accessors)
  97. {
  98. if (l->task == ignored_task)
  99. {
  100. /* Don't make pre_sync_task depend on post_sync_task!
  101. * but still drop from the list.
  102. * This happens notably when a task accesses several
  103. * times to the same data.
  104. */
  105. struct _starpu_task_wrapper_dlist *next;
  106. l->prev->next = l->next;
  107. l->next->prev = l->prev;
  108. l->task = NULL;
  109. l->prev = NULL;
  110. next = l->next;
  111. l->next = NULL;
  112. l = next;
  113. }
  114. else
  115. {
  116. naccessors++;
  117. l = l->next;
  118. }
  119. }
  120. _STARPU_DEP_DEBUG("%d accessors\n", naccessors);
  121. if (naccessors > 0)
  122. {
  123. /* Put all tasks in the list into task_array */
  124. struct starpu_task *task_array[naccessors];
  125. unsigned i = 0;
  126. l = handle->last_submitted_accessors.next;
  127. while (l != &handle->last_submitted_accessors)
  128. {
  129. STARPU_ASSERT(l->task);
  130. STARPU_ASSERT(l->task != ignored_task);
  131. task_array[i++] = l->task;
  132. _starpu_add_dependency(handle, l->task, pre_sync_task);
  133. _STARPU_DEP_DEBUG("dep %p -> %p\n", l->task, pre_sync_task);
  134. struct _starpu_task_wrapper_dlist *prev = l;
  135. l = l->next;
  136. prev->task = NULL;
  137. prev->next = NULL;
  138. prev->prev = NULL;
  139. }
  140. _starpu_task_declare_deps_array(pre_sync_task, naccessors, task_array, 0);
  141. }
  142. #ifndef STARPU_USE_FXT
  143. if (_starpu_bound_recording)
  144. #endif
  145. {
  146. /* Declare all dependencies with ghost accessors */
  147. struct _starpu_jobid_list *ghost_accessors_id = handle->last_submitted_ghost_accessors_id;
  148. while (ghost_accessors_id)
  149. {
  150. unsigned long id = ghost_accessors_id->id;
  151. _STARPU_TRACE_GHOST_TASK_DEPS(id,
  152. _starpu_get_job_associated_to_task(pre_sync_task)->job_id);
  153. _starpu_add_ghost_dependency(handle, id, pre_sync_task);
  154. _STARPU_DEP_DEBUG("dep ID%lu -> %p\n", id, pre_sync_task);
  155. struct _starpu_jobid_list *prev = ghost_accessors_id;
  156. ghost_accessors_id = ghost_accessors_id->next;
  157. free(prev);
  158. }
  159. handle->last_submitted_ghost_accessors_id = NULL;
  160. }
  161. handle->last_submitted_accessors.next = &handle->last_submitted_accessors;
  162. handle->last_submitted_accessors.prev = &handle->last_submitted_accessors;
  163. handle->last_sync_task = post_sync_task;
  164. if (!post_sync_task->cl)
  165. {
  166. /* Add a reference to be released in _starpu_handle_job_termination */
  167. _starpu_spin_lock(&handle->header_lock);
  168. handle->busy_count++;
  169. _starpu_spin_unlock(&handle->header_lock);
  170. _starpu_get_job_associated_to_task(post_sync_task)->implicit_dep_handle = handle;
  171. }
  172. }
  173. /* This function adds the implicit task dependencies introduced by data
  174. * sequential consistency. Two tasks are provided: pre_sync and post_sync which
  175. * respectively indicates which task is going to depend on the previous deps
  176. * and on which task future deps should wait. In the case of a dependency
  177. * introduced by a task submission, both tasks are just the submitted task, but
  178. * in the case of user interactions with the DSM, these may be different tasks.
  179. * */
  180. /* NB : handle->sequential_consistency_mutex must be hold by the caller;
  181. * returns a task, to be submitted after releasing that mutex. */
  182. struct starpu_task *_starpu_detect_implicit_data_deps_with_handle(struct starpu_task *pre_sync_task, struct starpu_task *post_sync_task, struct _starpu_task_wrapper_dlist *post_sync_task_dependency_slot,
  183. starpu_data_handle_t handle, enum starpu_data_access_mode mode)
  184. {
  185. struct starpu_task *task = NULL;
  186. /* Do not care about some flags */
  187. mode &= ~ STARPU_SSEND;
  188. mode &= ~ STARPU_LOCALITY;
  189. STARPU_ASSERT(!(mode & STARPU_SCRATCH));
  190. _STARPU_LOG_IN();
  191. if (handle->sequential_consistency)
  192. {
  193. struct _starpu_job *pre_sync_job = _starpu_get_job_associated_to_task(pre_sync_task);
  194. struct _starpu_job *post_sync_job = _starpu_get_job_associated_to_task(post_sync_task);
  195. if (mode & STARPU_W || mode == STARPU_REDUX)
  196. handle->initialized = 1;
  197. /* Skip tasks that are associated to a reduction phase so that
  198. * they do not interfere with the application. */
  199. if (pre_sync_job->reduction_task || post_sync_job->reduction_task)
  200. return NULL;
  201. /* In case we are generating the DAG, we add an implicit
  202. * dependency between the pre and the post sync tasks in case
  203. * they are not the same. */
  204. if (pre_sync_task != post_sync_task
  205. #ifndef STARPU_USE_FXT
  206. && _starpu_bound_recording
  207. #endif
  208. )
  209. {
  210. _STARPU_TRACE_GHOST_TASK_DEPS(pre_sync_job->job_id, post_sync_job->job_id);
  211. _starpu_bound_task_dep(post_sync_job, pre_sync_job);
  212. }
  213. enum starpu_data_access_mode previous_mode = handle->last_submitted_mode;
  214. _STARPU_DEP_DEBUG("Handle %p Tasks %p %p %x->%x\n", handle, pre_sync_task, post_sync_task, previous_mode, mode);
  215. /*
  216. * Tasks can access the data concurrently only if they have the
  217. * same access mode, which can only be either:
  218. * - write with STARPU_COMMUTE
  219. * - read
  220. * - redux
  221. *
  222. * In other cases, the tasks have to depend on each other.
  223. */
  224. if ((mode & STARPU_W && mode & STARPU_COMMUTE && previous_mode & STARPU_W && previous_mode & STARPU_COMMUTE)
  225. || (mode == STARPU_R && previous_mode == STARPU_R)
  226. || (mode == STARPU_REDUX && previous_mode == STARPU_REDUX))
  227. {
  228. _STARPU_DEP_DEBUG("concurrently\n");
  229. /* Can access concurrently with current tasks */
  230. _starpu_add_accessor(handle, pre_sync_task, post_sync_task, post_sync_task_dependency_slot);
  231. }
  232. else
  233. {
  234. /* Can not access concurrently, have to wait for existing accessors */
  235. struct _starpu_task_wrapper_dlist *l = handle->last_submitted_accessors.next;
  236. _STARPU_DEP_DEBUG("dependency\n");
  237. if ((l != &handle->last_submitted_accessors && l->next != &handle->last_submitted_accessors)
  238. || (handle->last_submitted_ghost_accessors_id && handle->last_submitted_ghost_accessors_id->next))
  239. {
  240. /* Several previous accessors */
  241. if (mode == STARPU_W)
  242. {
  243. _STARPU_DEP_DEBUG("several predecessors, and this is a W-only task, thus can serve directly as a synchronization task.\n");
  244. /* Optimization: this task can not
  245. * combine with others anyway, use it
  246. * as synchronization task by making it
  247. * wait for the previous ones. */
  248. _starpu_add_sync_task(handle, pre_sync_task, post_sync_task, post_sync_task);
  249. }
  250. else
  251. {
  252. _STARPU_DEP_DEBUG("several predecessors, adding sync task\n");
  253. /* insert an empty synchronization task
  254. * which waits for the whole set,
  255. * instead of creating a quadratic
  256. * number of dependencies. */
  257. struct starpu_task *sync_task = starpu_task_create();
  258. STARPU_ASSERT(sync_task);
  259. if (previous_mode == STARPU_REDUX)
  260. sync_task->name = "sync_task_redux";
  261. else if (mode == STARPU_COMMUTE || previous_mode == STARPU_COMMUTE)
  262. sync_task->name = "sync_task_commute";
  263. else
  264. sync_task->name = "sync_task";
  265. sync_task->cl = NULL;
  266. /* Make this task wait for the previous ones */
  267. _starpu_add_sync_task(handle, sync_task, sync_task, post_sync_task);
  268. /* And the requested task wait for this one */
  269. _starpu_add_accessor(handle, pre_sync_task, post_sync_task, post_sync_task_dependency_slot);
  270. task = sync_task;
  271. }
  272. }
  273. else
  274. {
  275. /* One previous accessor, make it the sync
  276. * task, and start depending on it. */
  277. if (l != &handle->last_submitted_accessors)
  278. {
  279. _STARPU_DEP_DEBUG("One previous accessor, depending on it\n");
  280. handle->last_sync_task = l->task;
  281. l->next = NULL;
  282. l->prev = NULL;
  283. handle->last_submitted_accessors.next = &handle->last_submitted_accessors;
  284. handle->last_submitted_accessors.prev = &handle->last_submitted_accessors;
  285. }
  286. else if (handle->last_submitted_ghost_accessors_id)
  287. {
  288. _STARPU_DEP_DEBUG("No more currently running accessor, but a ghost id, taking it.\n");
  289. handle->last_submitted_ghost_sync_id = handle->last_submitted_ghost_accessors_id->id;
  290. handle->last_submitted_ghost_sync_id_is_valid = 1;
  291. free(handle->last_submitted_ghost_accessors_id);
  292. handle->last_submitted_ghost_accessors_id = NULL;
  293. }
  294. else
  295. {
  296. _STARPU_DEP_DEBUG("No previous accessor, no dependency\n");
  297. }
  298. _starpu_add_accessor(handle, pre_sync_task, post_sync_task, post_sync_task_dependency_slot);
  299. }
  300. }
  301. handle->last_submitted_mode = mode;
  302. }
  303. _STARPU_LOG_OUT();
  304. return task;
  305. }
  306. /* Create the implicit dependencies for a newly submitted task */
  307. void _starpu_detect_implicit_data_deps(struct starpu_task *task)
  308. {
  309. STARPU_ASSERT(task->cl);
  310. _STARPU_LOG_IN();
  311. if (!task->sequential_consistency)
  312. return;
  313. /* We don't want to enforce a sequential consistency for tasks that are
  314. * not visible to the application. */
  315. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  316. if (j->reduction_task)
  317. return;
  318. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  319. struct _starpu_task_wrapper_dlist *dep_slots = _STARPU_JOB_GET_DEP_SLOTS(j);
  320. unsigned buffer;
  321. for (buffer = 0; buffer < nbuffers; buffer++)
  322. {
  323. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, buffer);
  324. enum starpu_data_access_mode mode = STARPU_TASK_GET_MODE(task, buffer);
  325. struct starpu_task *new_task;
  326. /* Scratch memory does not introduce any deps */
  327. if (mode & STARPU_SCRATCH)
  328. continue;
  329. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  330. new_task = _starpu_detect_implicit_data_deps_with_handle(task, task, &dep_slots[buffer], handle, mode);
  331. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  332. if (new_task)
  333. {
  334. int ret = _starpu_task_submit_internally(new_task);
  335. STARPU_ASSERT(!ret);
  336. }
  337. }
  338. _STARPU_LOG_OUT();
  339. }
  340. /* This function is called when a task has been executed so that we don't
  341. * create dependencies to task that do not exist anymore. */
  342. /* NB: We maintain a list of "ghost deps" in case FXT is enabled. Ghost
  343. * dependencies are the dependencies that are implicitely enforced by StarPU
  344. * even if they do not imply a real dependency. For instance in the following
  345. * sequence, f(Ar) g(Ar) h(Aw), we expect to have h depend on both f and g, but
  346. * if h is submitted after the termination of f or g, StarPU will not create a
  347. * dependency as this is not needed anymore. */
  348. /* the sequential_consistency_mutex of the handle has to be already held */
  349. void _starpu_release_data_enforce_sequential_consistency(struct starpu_task *task, struct _starpu_task_wrapper_dlist *task_dependency_slot, starpu_data_handle_t handle)
  350. {
  351. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  352. if (handle->sequential_consistency)
  353. {
  354. /* If this is the last writer, there is no point in adding
  355. * extra deps to that tasks that does not exists anymore */
  356. if (task == handle->last_sync_task)
  357. {
  358. handle->last_sync_task = NULL;
  359. #ifndef STARPU_USE_FXT
  360. if (_starpu_bound_recording)
  361. #endif
  362. {
  363. /* Save the previous writer as the ghost last writer */
  364. handle->last_submitted_ghost_sync_id_is_valid = 1;
  365. struct _starpu_job *ghost_job = _starpu_get_job_associated_to_task(task);
  366. handle->last_submitted_ghost_sync_id = ghost_job->job_id;
  367. }
  368. }
  369. /* Same if this is one of the readers: we go through the list
  370. * of readers and remove the task if it is found. */
  371. if (task_dependency_slot && task_dependency_slot->next)
  372. {
  373. #ifdef STARPU_DEBUG
  374. /* Make sure we are removing ourself from the proper handle */
  375. struct _starpu_task_wrapper_dlist *l;
  376. for (l = task_dependency_slot->prev; l->task; l = l->prev)
  377. ;
  378. STARPU_ASSERT(l == &handle->last_submitted_accessors);
  379. for (l = task_dependency_slot->next; l->task; l = l->next)
  380. ;
  381. STARPU_ASSERT(l == &handle->last_submitted_accessors);
  382. #endif
  383. STARPU_ASSERT(task_dependency_slot->task == task);
  384. task_dependency_slot->next->prev = task_dependency_slot->prev;
  385. task_dependency_slot->prev->next = task_dependency_slot->next;
  386. task_dependency_slot->task = NULL;
  387. task_dependency_slot->next = NULL;
  388. task_dependency_slot->prev = NULL;
  389. #ifndef STARPU_USE_FXT
  390. if (_starpu_bound_recording)
  391. #endif
  392. {
  393. /* Save the job id of the reader task in the ghost reader linked list list */
  394. struct _starpu_job *ghost_reader_job = _starpu_get_job_associated_to_task(task);
  395. struct _starpu_jobid_list *link = (struct _starpu_jobid_list *) malloc(sizeof(struct _starpu_jobid_list));
  396. STARPU_ASSERT(link);
  397. link->next = handle->last_submitted_ghost_accessors_id;
  398. link->id = ghost_reader_job->job_id;
  399. handle->last_submitted_ghost_accessors_id = link;
  400. }
  401. }
  402. }
  403. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  404. }
  405. /* This is the same as _starpu_release_data_enforce_sequential_consistency, but
  406. * for all data of a task */
  407. void _starpu_release_task_enforce_sequential_consistency(struct _starpu_job *j)
  408. {
  409. struct starpu_task *task = j->task;
  410. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  411. struct _starpu_task_wrapper_dlist *slots = _STARPU_JOB_GET_DEP_SLOTS(j);
  412. if (!task->cl)
  413. return;
  414. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  415. unsigned index;
  416. /* Release all implicit dependencies */
  417. for (index = 0; index < nbuffers; index++)
  418. {
  419. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, index);
  420. _starpu_release_data_enforce_sequential_consistency(task, &slots[index], handle);
  421. }
  422. for (index = 0; index < nbuffers; index++)
  423. {
  424. starpu_data_handle_t handle = descrs[index].handle;
  425. if (index && descrs[index-1].handle == descrs[index].handle)
  426. /* We have already released this data, skip it. This
  427. * depends on ordering putting writes before reads, see
  428. * _starpu_compar_handles */
  429. continue;
  430. /* Release the reference acquired in _starpu_push_task_output */
  431. _starpu_spin_lock(&handle->header_lock);
  432. STARPU_ASSERT(handle->busy_count > 0);
  433. handle->busy_count--;
  434. if (!_starpu_data_check_not_busy(handle))
  435. _starpu_spin_unlock(&handle->header_lock);
  436. }
  437. }
  438. void _starpu_add_post_sync_tasks(struct starpu_task *post_sync_task, starpu_data_handle_t handle)
  439. {
  440. _STARPU_LOG_IN();
  441. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  442. if (handle->sequential_consistency)
  443. {
  444. handle->post_sync_tasks_cnt++;
  445. struct _starpu_task_wrapper_list *link = (struct _starpu_task_wrapper_list *) malloc(sizeof(struct _starpu_task_wrapper_list));
  446. link->task = post_sync_task;
  447. link->next = handle->post_sync_tasks;
  448. handle->post_sync_tasks = link;
  449. }
  450. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  451. _STARPU_LOG_OUT();
  452. }
  453. void _starpu_unlock_post_sync_tasks(starpu_data_handle_t handle)
  454. {
  455. struct _starpu_task_wrapper_list *post_sync_tasks = NULL;
  456. unsigned do_submit_tasks = 0;
  457. /* Here helgrind would shout that this is an unprotected access, but
  458. * count can only be zero if we don't have to care about
  459. * post_sync_tasks_cnt at all. */
  460. if (STARPU_RUNNING_ON_VALGRIND || handle->post_sync_tasks_cnt)
  461. {
  462. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  463. if (--handle->post_sync_tasks_cnt == 0)
  464. {
  465. /* unlock all tasks : we need not hold the lock while unlocking all these tasks */
  466. do_submit_tasks = 1;
  467. post_sync_tasks = handle->post_sync_tasks;
  468. handle->post_sync_tasks = NULL;
  469. }
  470. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  471. }
  472. if (do_submit_tasks)
  473. {
  474. struct _starpu_task_wrapper_list *link = post_sync_tasks;
  475. while (link)
  476. {
  477. /* There is no need to depend on that task now, since it was already unlocked */
  478. _starpu_release_data_enforce_sequential_consistency(link->task, &_starpu_get_job_associated_to_task(link->task)->implicit_dep_slot, handle);
  479. int ret = _starpu_task_submit_internally(link->task);
  480. STARPU_ASSERT(!ret);
  481. struct _starpu_task_wrapper_list *tmp = link;
  482. link = link->next;
  483. free(tmp);
  484. }
  485. }
  486. }
  487. /* If sequential consistency mode is enabled, this function blocks until the
  488. * handle is available in the requested access mode. */
  489. int _starpu_data_wait_until_available(starpu_data_handle_t handle, enum starpu_data_access_mode mode, const char *sync_name)
  490. {
  491. /* If sequential consistency is enabled, wait until data is available */
  492. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  493. int sequential_consistency = handle->sequential_consistency;
  494. if (sequential_consistency)
  495. {
  496. struct starpu_task *sync_task, *new_task;
  497. sync_task = starpu_task_create();
  498. sync_task->name = sync_name;
  499. sync_task->detach = 0;
  500. sync_task->destroy = 1;
  501. /* It is not really a RW access, but we want to make sure that
  502. * all previous accesses are done */
  503. new_task = _starpu_detect_implicit_data_deps_with_handle(sync_task, sync_task, &_starpu_get_job_associated_to_task(sync_task)->implicit_dep_slot, handle, mode);
  504. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  505. if (new_task)
  506. {
  507. int ret = _starpu_task_submit_internally(new_task);
  508. STARPU_ASSERT(!ret);
  509. }
  510. /* TODO detect if this is superflous */
  511. int ret = _starpu_task_submit_internally(sync_task);
  512. STARPU_ASSERT(!ret);
  513. ret = starpu_task_wait(sync_task);
  514. STARPU_ASSERT(ret == 0);
  515. }
  516. else
  517. {
  518. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  519. }
  520. return 0;
  521. }
  522. /* This data is about to be freed, clean our stuff */
  523. void _starpu_data_clear_implicit(starpu_data_handle_t handle)
  524. {
  525. struct _starpu_jobid_list *list;
  526. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  527. list = handle->last_submitted_ghost_accessors_id;
  528. while (list)
  529. {
  530. struct _starpu_jobid_list *next = list->next;
  531. free(list);
  532. list = next;
  533. }
  534. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  535. }