implicit_data_deps.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-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 <starpu.h>
  17. #include <common/config.h>
  18. #include <core/task.h>
  19. #include <datawizard/datawizard.h>
  20. #include <profiling/bound.h>
  21. #include <core/debug.h>
  22. #if 0
  23. # define _STARPU_DEP_DEBUG(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__);
  24. #else
  25. # define _STARPU_DEP_DEBUG(fmt, ...)
  26. #endif
  27. static void (*write_hook)(starpu_data_handle_t);
  28. void _starpu_implicit_data_deps_write_hook(void (*func)(starpu_data_handle_t))
  29. {
  30. STARPU_ASSERT_MSG(!write_hook || write_hook == func, "only one implicit data deps hook at a time\n");
  31. write_hook = func;
  32. }
  33. static void _starpu_add_ghost_dependency(starpu_data_handle_t handle, unsigned long previous, struct starpu_task *next)
  34. {
  35. struct _starpu_job *next_job = _starpu_get_job_associated_to_task(next);
  36. _starpu_bound_job_id_dep(handle, next_job, previous);
  37. STARPU_AYU_ADDDEPENDENCY(previous, handle, next_job->job_id);
  38. }
  39. static void _starpu_add_dependency(starpu_data_handle_t handle, struct starpu_task *previous, struct starpu_task *next)
  40. {
  41. _starpu_add_ghost_dependency(handle, _starpu_get_job_associated_to_task(previous)->job_id, next);
  42. }
  43. /* Add post_sync_task as new accessor among the existing ones, making pre_sync_task depend on the last synchronization task if any. */
  44. static void _starpu_add_accessor(starpu_data_handle_t handle, struct starpu_task *pre_sync_task, int *submit_pre_sync, struct starpu_task *post_sync_task, struct _starpu_task_wrapper_dlist *post_sync_task_dependency_slot)
  45. {
  46. /* Add this task to the list of readers */
  47. STARPU_ASSERT(!post_sync_task_dependency_slot->prev);
  48. STARPU_ASSERT(!post_sync_task_dependency_slot->next);
  49. post_sync_task_dependency_slot->task = post_sync_task;
  50. post_sync_task_dependency_slot->next = handle->last_submitted_accessors.next;
  51. post_sync_task_dependency_slot->prev = &handle->last_submitted_accessors;
  52. post_sync_task_dependency_slot->next->prev = post_sync_task_dependency_slot;
  53. handle->last_submitted_accessors.next = post_sync_task_dependency_slot;
  54. /* This task depends on the previous synchronization task if any */
  55. if (handle->last_sync_task && handle->last_sync_task != post_sync_task)
  56. {
  57. *submit_pre_sync= 1;
  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. || STARPU_AYU_EVENT
  78. ) && handle->last_submitted_ghost_sync_id_is_valid)
  79. {
  80. _STARPU_TRACE_GHOST_TASK_DEPS(handle->last_submitted_ghost_sync_id,
  81. _starpu_get_job_associated_to_task(pre_sync_task));
  82. _starpu_add_ghost_dependency(handle, handle->last_submitted_ghost_sync_id, pre_sync_task);
  83. _STARPU_DEP_DEBUG("dep ID%lu -> %p\n", handle->last_submitted_ghost_sync_id, pre_sync_task);
  84. }
  85. if (*submit_pre_sync && !pre_sync_task->cl)
  86. {
  87. /* Add a reference to be released in _starpu_handle_job_termination */
  88. _starpu_spin_lock(&handle->header_lock);
  89. handle->busy_count++;
  90. _starpu_spin_unlock(&handle->header_lock);
  91. _starpu_get_job_associated_to_task(pre_sync_task)->implicit_dep_handle = handle;
  92. }
  93. }
  94. /* This adds a new synchronization task which depends on all the previous accessors */
  95. 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)
  96. {
  97. /* Count the existing accessors */
  98. unsigned naccessors = 0;
  99. struct _starpu_task_wrapper_dlist *l;
  100. l = handle->last_submitted_accessors.next;
  101. while (l != &handle->last_submitted_accessors)
  102. {
  103. if (l->task == ignored_task)
  104. {
  105. /* Don't make pre_sync_task depend on post_sync_task!
  106. * but still drop from the list.
  107. * This happens notably when a task accesses several
  108. * times to the same data.
  109. */
  110. struct _starpu_task_wrapper_dlist *next;
  111. l->prev->next = l->next;
  112. l->next->prev = l->prev;
  113. l->task = NULL;
  114. l->prev = NULL;
  115. next = l->next;
  116. l->next = NULL;
  117. l = next;
  118. }
  119. else
  120. {
  121. naccessors++;
  122. l = l->next;
  123. }
  124. }
  125. _STARPU_DEP_DEBUG("%d accessors\n", naccessors);
  126. if (naccessors > 0)
  127. {
  128. /* Put all tasks in the list into task_array */
  129. struct starpu_task *task_array[naccessors];
  130. unsigned i = 0;
  131. l = handle->last_submitted_accessors.next;
  132. while (l != &handle->last_submitted_accessors)
  133. {
  134. STARPU_ASSERT(l->task);
  135. STARPU_ASSERT(l->task != ignored_task);
  136. task_array[i++] = l->task;
  137. _starpu_add_dependency(handle, l->task, pre_sync_task);
  138. _STARPU_DEP_DEBUG("dep %p -> %p\n", l->task, pre_sync_task);
  139. struct _starpu_task_wrapper_dlist *prev = l;
  140. l = l->next;
  141. prev->task = NULL;
  142. prev->next = NULL;
  143. prev->prev = NULL;
  144. }
  145. _starpu_task_declare_deps_array(pre_sync_task, naccessors, task_array, 0);
  146. }
  147. #ifndef STARPU_USE_FXT
  148. if (_starpu_bound_recording)
  149. #endif
  150. {
  151. /* Declare all dependencies with ghost accessors */
  152. struct _starpu_jobid_list *ghost_accessors_id = handle->last_submitted_ghost_accessors_id;
  153. while (ghost_accessors_id)
  154. {
  155. unsigned long id = ghost_accessors_id->id;
  156. _STARPU_TRACE_GHOST_TASK_DEPS(id,
  157. _starpu_get_job_associated_to_task(pre_sync_task));
  158. _starpu_add_ghost_dependency(handle, id, pre_sync_task);
  159. _STARPU_DEP_DEBUG("dep ID%lu -> %p\n", id, pre_sync_task);
  160. struct _starpu_jobid_list *prev = ghost_accessors_id;
  161. ghost_accessors_id = ghost_accessors_id->next;
  162. free(prev);
  163. }
  164. handle->last_submitted_ghost_accessors_id = NULL;
  165. }
  166. handle->last_submitted_accessors.next = &handle->last_submitted_accessors;
  167. handle->last_submitted_accessors.prev = &handle->last_submitted_accessors;
  168. handle->last_sync_task = post_sync_task;
  169. if (!post_sync_task->cl)
  170. {
  171. /* Add a reference to be released in _starpu_handle_job_termination */
  172. _starpu_spin_lock(&handle->header_lock);
  173. handle->busy_count++;
  174. _starpu_spin_unlock(&handle->header_lock);
  175. _starpu_get_job_associated_to_task(post_sync_task)->implicit_dep_handle = handle;
  176. }
  177. }
  178. /* This function adds the implicit task dependencies introduced by data
  179. * sequential consistency. Two tasks are provided: pre_sync and post_sync which
  180. * respectively indicates which task is going to depend on the previous deps
  181. * and on which task future deps should wait. In the case of a dependency
  182. * introduced by a task submission, both tasks are just the submitted task, but
  183. * in the case of user interactions with the DSM, these may be different tasks.
  184. * */
  185. /* NB : handle->sequential_consistency_mutex must be hold by the caller;
  186. * returns a task, to be submitted after releasing that mutex. */
  187. /* *submit_pre_sync is whether the pre_sync_task will be submitted or not. The
  188. * caller should set it to 1 if it intends to submit it anyway, or to 0
  189. * if it may not submit it (because it has no other use for the task than
  190. * synchronization). In the latter case,
  191. * _starpu_detect_implicit_data_deps_with_handle will set it to 1 in case the
  192. * task really needs to be submitted, or leave it to 0 if there is nothing to be
  193. * waited for anyway. */
  194. struct starpu_task *_starpu_detect_implicit_data_deps_with_handle(struct starpu_task *pre_sync_task, int *submit_pre_sync, struct starpu_task *post_sync_task, struct _starpu_task_wrapper_dlist *post_sync_task_dependency_slot,
  195. starpu_data_handle_t handle, enum starpu_data_access_mode mode, unsigned task_handle_sequential_consistency)
  196. {
  197. struct starpu_task *task = NULL;
  198. /* Do not care about some flags */
  199. mode &= ~ STARPU_SSEND;
  200. mode &= ~ STARPU_LOCALITY;
  201. STARPU_ASSERT(!(mode & STARPU_SCRATCH));
  202. _STARPU_LOG_IN();
  203. if (handle->sequential_consistency && task_handle_sequential_consistency)
  204. {
  205. struct _starpu_job *pre_sync_job = _starpu_get_job_associated_to_task(pre_sync_task);
  206. struct _starpu_job *post_sync_job = _starpu_get_job_associated_to_task(post_sync_task);
  207. if (mode & STARPU_R)
  208. STARPU_ASSERT_MSG(handle->initialized || handle->init_cl, "Handle %p is not initialized, it cannot be read", handle);
  209. if (mode & STARPU_W || mode == STARPU_REDUX)
  210. {
  211. STARPU_ASSERT_MSG(!handle->readonly, "Read-only handle %p can not be written to", handle);
  212. handle->initialized = 1;
  213. /* We will change our value, disconnect from our readonly duplicates */
  214. if (handle->readonly_dup)
  215. {
  216. STARPU_ASSERT(handle->readonly_dup->readonly_dup_of == handle);
  217. handle->readonly_dup->readonly_dup_of = NULL;
  218. handle->readonly_dup = NULL;
  219. }
  220. if (write_hook)
  221. write_hook(handle);
  222. }
  223. /* Skip tasks that are associated to a reduction phase so that
  224. * they do not interfere with the application. */
  225. if (pre_sync_job->reduction_task)
  226. {
  227. *submit_pre_sync = 1;
  228. return NULL;
  229. }
  230. if (post_sync_job->reduction_task)
  231. {
  232. *submit_pre_sync = 0;
  233. return NULL;
  234. }
  235. /* In case we are generating the DAG, we add an implicit
  236. * dependency between the pre and the post sync tasks in case
  237. * they are not the same. */
  238. if (pre_sync_task != post_sync_task
  239. #ifndef STARPU_USE_FXT
  240. && _starpu_bound_recording
  241. #endif
  242. )
  243. {
  244. _STARPU_TRACE_GHOST_TASK_DEPS(pre_sync_job->job_id, post_sync_job);
  245. _starpu_bound_task_dep(post_sync_job, pre_sync_job);
  246. }
  247. enum starpu_data_access_mode previous_mode = handle->last_submitted_mode;
  248. _STARPU_DEP_DEBUG("Handle %p Tasks %p %p %x->%x\n", handle, pre_sync_task, post_sync_task, previous_mode, mode);
  249. /*
  250. * Tasks can access the data concurrently only if they have the
  251. * same access mode, which can only be either:
  252. * - write with STARPU_COMMUTE
  253. * - read
  254. * - redux
  255. *
  256. * In other cases, the tasks have to depend on each other.
  257. */
  258. if ((mode & STARPU_W && mode & STARPU_COMMUTE && previous_mode & STARPU_W && previous_mode & STARPU_COMMUTE)
  259. || (mode == STARPU_R && previous_mode == STARPU_R)
  260. || (mode == STARPU_REDUX && previous_mode == STARPU_REDUX))
  261. {
  262. _STARPU_DEP_DEBUG("concurrently\n");
  263. /* Can access concurrently with current tasks */
  264. if (handle->last_sync_task != NULL)
  265. *submit_pre_sync = 1;
  266. _starpu_add_accessor(handle, pre_sync_task, submit_pre_sync, post_sync_task, post_sync_task_dependency_slot);
  267. }
  268. else
  269. {
  270. /* Can not access concurrently, have to wait for existing accessors */
  271. struct _starpu_task_wrapper_dlist *l = handle->last_submitted_accessors.next;
  272. _STARPU_DEP_DEBUG("dependency\n");
  273. if ((l != &handle->last_submitted_accessors && l->next != &handle->last_submitted_accessors)
  274. || (handle->last_submitted_ghost_accessors_id && handle->last_submitted_ghost_accessors_id->next)
  275. || (l != &handle->last_submitted_accessors && handle->last_submitted_ghost_accessors_id))
  276. {
  277. /* Several previous accessors */
  278. *submit_pre_sync = 1;
  279. if (mode == STARPU_W)
  280. {
  281. _STARPU_DEP_DEBUG("several predecessors, and this is a W-only task, thus can serve directly as a synchronization task.\n");
  282. /* Optimization: this task can not
  283. * combine with others anyway, use it
  284. * as synchronization task by making it
  285. * wait for the previous ones. */
  286. _starpu_add_sync_task(handle, pre_sync_task, post_sync_task, post_sync_task);
  287. }
  288. else
  289. {
  290. _STARPU_DEP_DEBUG("several predecessors, adding sync task\n");
  291. /* insert an empty synchronization task
  292. * which waits for the whole set,
  293. * instead of creating a quadratic
  294. * number of dependencies. */
  295. struct starpu_task *sync_task = starpu_task_create();
  296. STARPU_ASSERT(sync_task);
  297. if (previous_mode == STARPU_REDUX)
  298. sync_task->name = "_starpu_sync_task_redux";
  299. else if (mode == STARPU_COMMUTE || previous_mode == STARPU_COMMUTE)
  300. sync_task->name = "_starpu_sync_task_commute";
  301. else
  302. sync_task->name = "_starpu_sync_task";
  303. sync_task->cl = NULL;
  304. sync_task->type = post_sync_task->type;
  305. /* Make this task wait for the previous ones */
  306. _starpu_add_sync_task(handle, sync_task, sync_task, post_sync_task);
  307. /* And the requested task wait for this one */
  308. _starpu_add_accessor(handle, pre_sync_task, submit_pre_sync, post_sync_task, post_sync_task_dependency_slot);
  309. task = sync_task;
  310. }
  311. }
  312. else
  313. {
  314. struct _starpu_jobid_list *ghost_accessors_id = handle->last_submitted_ghost_accessors_id;
  315. /* At most one previous accessor or one ghost */
  316. if (l != &handle->last_submitted_accessors)
  317. {
  318. /* One accessor, make it the sync task,
  319. * and start depending on it. */
  320. *submit_pre_sync = 1;
  321. _STARPU_DEP_DEBUG("One previous accessor, depending on it\n");
  322. handle->last_sync_task = l->task;
  323. l->next = NULL;
  324. l->prev = NULL;
  325. handle->last_submitted_accessors.next = &handle->last_submitted_accessors;
  326. handle->last_submitted_accessors.prev = &handle->last_submitted_accessors;
  327. handle->last_submitted_ghost_sync_id_is_valid = 0;
  328. }
  329. else if (ghost_accessors_id)
  330. {
  331. /* One ghost, just remember its id */
  332. _STARPU_DEP_DEBUG("No more currently running accessor, but a ghost id, taking it.\n");
  333. handle->last_submitted_ghost_sync_id = ghost_accessors_id->id;
  334. handle->last_submitted_ghost_sync_id_is_valid = 1;
  335. STARPU_ASSERT(!ghost_accessors_id->next);
  336. handle->last_submitted_ghost_accessors_id = NULL;
  337. free(ghost_accessors_id);
  338. }
  339. else
  340. {
  341. _STARPU_DEP_DEBUG("No previous accessor, no dependency\n");
  342. }
  343. _starpu_add_accessor(handle, pre_sync_task, submit_pre_sync, post_sync_task, post_sync_task_dependency_slot);
  344. }
  345. }
  346. handle->last_submitted_mode = mode;
  347. } else {
  348. *submit_pre_sync = 0;
  349. }
  350. _STARPU_LOG_OUT();
  351. return task;
  352. }
  353. int _starpu_test_implicit_data_deps_with_handle(starpu_data_handle_t handle, enum starpu_data_access_mode mode)
  354. {
  355. /* Do not care about some flags */
  356. mode &= ~ STARPU_SSEND;
  357. mode &= ~ STARPU_LOCALITY;
  358. STARPU_ASSERT(!(mode & STARPU_SCRATCH));
  359. if (handle->sequential_consistency)
  360. {
  361. if (handle->last_sync_task)
  362. return -EAGAIN;
  363. if (handle->last_submitted_accessors.next != &handle->last_submitted_accessors)
  364. return -EAGAIN;
  365. if (mode & STARPU_W || mode == STARPU_REDUX)
  366. handle->initialized = 1;
  367. handle->last_submitted_mode = mode;
  368. }
  369. return 0;
  370. }
  371. /* Create the implicit dependencies for a newly submitted task */
  372. void _starpu_detect_implicit_data_deps(struct starpu_task *task)
  373. {
  374. STARPU_ASSERT(task->cl);
  375. _STARPU_LOG_IN();
  376. if (!task->sequential_consistency)
  377. return;
  378. /* We don't want to enforce a sequential consistency for tasks that are
  379. * not visible to the application. */
  380. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  381. if (j->reduction_task)
  382. return;
  383. j->sequential_consistency = 1;
  384. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  385. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  386. struct _starpu_task_wrapper_dlist *dep_slots = _STARPU_JOB_GET_DEP_SLOTS(j);
  387. unsigned buffer;
  388. for (buffer = 0; buffer < nbuffers; buffer++)
  389. {
  390. starpu_data_handle_t handle = descrs[buffer].handle;
  391. enum starpu_data_access_mode mode = descrs[buffer].mode;
  392. struct starpu_task *new_task;
  393. /* Scratch memory does not introduce any deps */
  394. if (mode & STARPU_SCRATCH)
  395. continue;
  396. if (buffer)
  397. {
  398. starpu_data_handle_t handle_m1 = descrs[buffer-1].handle;
  399. enum starpu_data_access_mode mode_m1 = descrs[buffer-1].mode;
  400. if (handle_m1 == handle && mode_m1 == mode)
  401. /* We have already added dependencies for this
  402. * data, skip it. This reduces the number of
  403. * dependencies, and allows notify_soon to work
  404. * when a task uses the same data several times
  405. * (otherwise it will not be able to find out that the two
  406. * dependencies will be over at the same time) */
  407. continue;
  408. }
  409. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  410. unsigned index = descrs[buffer].index;
  411. unsigned task_handle_sequential_consistency = task->handles_sequential_consistency ? task->handles_sequential_consistency[index] : handle->sequential_consistency;
  412. int submit_pre_sync = 1;
  413. if (!task_handle_sequential_consistency)
  414. j->sequential_consistency = 0;
  415. new_task = _starpu_detect_implicit_data_deps_with_handle(task, &submit_pre_sync, task, &dep_slots[buffer], handle, mode, task_handle_sequential_consistency);
  416. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  417. if (new_task)
  418. {
  419. int ret = _starpu_task_submit_internally(new_task);
  420. STARPU_ASSERT(!ret);
  421. }
  422. }
  423. _STARPU_LOG_OUT();
  424. }
  425. /* This function is called when a task has been executed so that we don't
  426. * create dependencies to task that do not exist anymore. */
  427. /* NB: We maintain a list of "ghost deps" in case FXT is enabled. Ghost
  428. * dependencies are the dependencies that are implicitely enforced by StarPU
  429. * even if they do not imply a real dependency. For instance in the following
  430. * sequence, f(Ar) g(Ar) h(Aw), we expect to have h depend on both f and g, but
  431. * if h is submitted after the termination of f or g, StarPU will not create a
  432. * dependency as this is not needed anymore. */
  433. /* the sequential_consistency_mutex of the handle has to be already held */
  434. void _starpu_release_data_enforce_sequential_consistency(struct starpu_task *task, struct _starpu_task_wrapper_dlist *task_dependency_slot, starpu_data_handle_t handle)
  435. {
  436. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  437. if (handle->sequential_consistency)
  438. {
  439. /* If this is the last writer, there is no point in adding
  440. * extra deps to that tasks that does not exists anymore */
  441. if (task == handle->last_sync_task)
  442. {
  443. handle->last_sync_task = NULL;
  444. #ifndef STARPU_USE_FXT
  445. if (_starpu_bound_recording)
  446. #endif
  447. {
  448. /* Save the previous writer as the ghost last writer */
  449. handle->last_submitted_ghost_sync_id_is_valid = 1;
  450. struct _starpu_job *ghost_job = _starpu_get_job_associated_to_task(task);
  451. handle->last_submitted_ghost_sync_id = ghost_job->job_id;
  452. }
  453. }
  454. /* Same if this is one of the readers: we go through the list
  455. * of readers and remove the task if it is found. */
  456. if (task_dependency_slot && task_dependency_slot->next)
  457. {
  458. #ifdef STARPU_DEBUG
  459. /* Make sure we are removing ourself from the proper handle */
  460. struct _starpu_task_wrapper_dlist *l;
  461. for (l = task_dependency_slot->prev; l->task; l = l->prev)
  462. ;
  463. STARPU_ASSERT(l == &handle->last_submitted_accessors);
  464. for (l = task_dependency_slot->next; l->task; l = l->next)
  465. ;
  466. STARPU_ASSERT(l == &handle->last_submitted_accessors);
  467. #endif
  468. STARPU_ASSERT(task_dependency_slot->task == task);
  469. task_dependency_slot->next->prev = task_dependency_slot->prev;
  470. task_dependency_slot->prev->next = task_dependency_slot->next;
  471. task_dependency_slot->task = NULL;
  472. task_dependency_slot->next = NULL;
  473. task_dependency_slot->prev = NULL;
  474. #ifndef STARPU_USE_FXT
  475. if (_starpu_bound_recording)
  476. #endif
  477. {
  478. /* Save the job id of the reader task in the ghost reader linked list list */
  479. struct _starpu_job *ghost_reader_job = _starpu_get_job_associated_to_task(task);
  480. struct _starpu_jobid_list *link;
  481. _STARPU_MALLOC(link, sizeof(struct _starpu_jobid_list));
  482. link->next = handle->last_submitted_ghost_accessors_id;
  483. link->id = ghost_reader_job->job_id;
  484. handle->last_submitted_ghost_accessors_id = link;
  485. }
  486. }
  487. }
  488. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  489. }
  490. /* This is the same as _starpu_release_data_enforce_sequential_consistency, but
  491. * for all data of a task */
  492. void _starpu_release_task_enforce_sequential_consistency(struct _starpu_job *j)
  493. {
  494. struct starpu_task *task = j->task;
  495. if (!task->cl)
  496. return;
  497. struct _starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  498. struct _starpu_task_wrapper_dlist *slots = _STARPU_JOB_GET_DEP_SLOTS(j);
  499. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  500. unsigned index;
  501. /* Release all implicit dependencies */
  502. for (index = 0; index < nbuffers; index++)
  503. {
  504. starpu_data_handle_t handle = descrs[index].handle;
  505. enum starpu_data_access_mode mode = descrs[index].mode;
  506. if (index)
  507. {
  508. starpu_data_handle_t handle_m1 = descrs[index-1].handle;
  509. enum starpu_data_access_mode mode_m1 = descrs[index-1].mode;
  510. if (handle_m1 == handle && mode_m1 == mode)
  511. /* See _starpu_detect_implicit_data_deps */
  512. continue;
  513. }
  514. _starpu_release_data_enforce_sequential_consistency(task, &slots[index], handle);
  515. }
  516. for (index = 0; index < nbuffers; index++)
  517. {
  518. starpu_data_handle_t handle = descrs[index].handle;
  519. if (index && descrs[index-1].handle == descrs[index].handle)
  520. /* We have already released this data, skip it. This
  521. * depends on ordering putting writes before reads, see
  522. * _starpu_compar_handles */
  523. continue;
  524. /* Release the reference acquired in _starpu_push_task_output */
  525. _starpu_spin_lock(&handle->header_lock);
  526. STARPU_ASSERT(handle->busy_count > 0);
  527. handle->busy_count--;
  528. if (!_starpu_data_check_not_busy(handle))
  529. _starpu_spin_unlock(&handle->header_lock);
  530. }
  531. }
  532. void _starpu_add_post_sync_tasks(struct starpu_task *post_sync_task, starpu_data_handle_t handle)
  533. {
  534. _STARPU_LOG_IN();
  535. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  536. if (handle->sequential_consistency)
  537. {
  538. handle->post_sync_tasks_cnt++;
  539. struct _starpu_task_wrapper_list *link;
  540. _STARPU_MALLOC(link, sizeof(struct _starpu_task_wrapper_list));
  541. link->task = post_sync_task;
  542. link->next = handle->post_sync_tasks;
  543. handle->post_sync_tasks = link;
  544. }
  545. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  546. _STARPU_LOG_OUT();
  547. }
  548. void _starpu_unlock_post_sync_tasks(starpu_data_handle_t handle)
  549. {
  550. struct _starpu_task_wrapper_list *post_sync_tasks = NULL;
  551. unsigned do_submit_tasks = 0;
  552. /* Here helgrind would shout that this is an unprotected access, but
  553. * count can only be zero if we don't have to care about
  554. * post_sync_tasks_cnt at all. */
  555. if (STARPU_RUNNING_ON_VALGRIND || handle->post_sync_tasks_cnt)
  556. {
  557. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  558. if (--handle->post_sync_tasks_cnt == 0)
  559. {
  560. /* unlock all tasks : we need not hold the lock while unlocking all these tasks */
  561. do_submit_tasks = 1;
  562. post_sync_tasks = handle->post_sync_tasks;
  563. handle->post_sync_tasks = NULL;
  564. }
  565. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  566. }
  567. if (do_submit_tasks)
  568. {
  569. struct _starpu_task_wrapper_list *link = post_sync_tasks;
  570. while (link)
  571. {
  572. /* There is no need to depend on that task now, since it was already unlocked */
  573. _starpu_release_data_enforce_sequential_consistency(link->task, &_starpu_get_job_associated_to_task(link->task)->implicit_dep_slot, handle);
  574. int ret = _starpu_task_submit_internally(link->task);
  575. STARPU_ASSERT(!ret);
  576. struct _starpu_task_wrapper_list *tmp = link;
  577. link = link->next;
  578. free(tmp);
  579. }
  580. }
  581. }
  582. /* If sequential consistency mode is enabled, this function blocks until the
  583. * handle is available in the requested access mode. */
  584. int _starpu_data_wait_until_available(starpu_data_handle_t handle, enum starpu_data_access_mode mode, const char *sync_name)
  585. {
  586. /* If sequential consistency is enabled, wait until data is available */
  587. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  588. int sequential_consistency = handle->sequential_consistency;
  589. if (sequential_consistency)
  590. {
  591. struct starpu_task *sync_task, *new_task;
  592. int submit_pre_sync = 0;
  593. sync_task = starpu_task_create();
  594. sync_task->name = sync_name;
  595. sync_task->detach = 0;
  596. sync_task->destroy = 1;
  597. sync_task->type = STARPU_TASK_TYPE_INTERNAL;
  598. /* It is not really a RW access, but we want to make sure that
  599. * all previous accesses are done */
  600. new_task = _starpu_detect_implicit_data_deps_with_handle(sync_task, &submit_pre_sync, sync_task, &_starpu_get_job_associated_to_task(sync_task)->implicit_dep_slot, handle, mode, sequential_consistency);
  601. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  602. if (new_task)
  603. {
  604. int ret = _starpu_task_submit_internally(new_task);
  605. STARPU_ASSERT(!ret);
  606. }
  607. if (submit_pre_sync)
  608. {
  609. int ret = _starpu_task_submit_internally(sync_task);
  610. STARPU_ASSERT(!ret);
  611. ret = starpu_task_wait(sync_task);
  612. STARPU_ASSERT(ret == 0);
  613. }
  614. else
  615. {
  616. starpu_task_destroy(sync_task);
  617. }
  618. }
  619. else
  620. {
  621. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  622. }
  623. return 0;
  624. }
  625. /* This data is about to be freed, clean our stuff */
  626. void _starpu_data_clear_implicit(starpu_data_handle_t handle)
  627. {
  628. struct _starpu_jobid_list *list;
  629. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  630. list = handle->last_submitted_ghost_accessors_id;
  631. while (list)
  632. {
  633. struct _starpu_jobid_list *next = list->next;
  634. free(list);
  635. list = next;
  636. }
  637. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  638. }