implicit_data_deps.c 21 KB

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