implicit_data_deps.c 24 KB

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