implicit_data_deps.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. #if 0
  22. # define _STARPU_DEP_DEBUG(fmt, args ...) fprintf(stderr, fmt, ##args);
  23. #else
  24. # define _STARPU_DEP_DEBUG(fmt, args ...)
  25. #endif
  26. /* This function adds the implicit task dependencies introduced by data
  27. * sequential consistency. Two tasks are provided: pre_sync and post_sync which
  28. * respectively indicates which task is going to depend on the previous deps
  29. * and on which task future deps should wait. In the case of a dependency
  30. * introduced by a task submission, both tasks are just the submitted task, but
  31. * in the case of user interactions with the DSM, these may be different tasks.
  32. * */
  33. /* NB : handle->sequential_consistency_mutex must be hold by the caller */
  34. void _starpu_detect_implicit_data_deps_with_handle(struct starpu_task *pre_sync_task, struct starpu_task *post_sync_task,
  35. starpu_data_handle handle, starpu_access_mode mode)
  36. {
  37. STARPU_ASSERT(!(mode & STARPU_SCRATCH));
  38. _STARPU_LOG_IN();
  39. if (handle->sequential_consistency)
  40. {
  41. /* Skip tasks that are associated to a reduction phase so that
  42. * they do not interfere with the application. */
  43. {
  44. starpu_job_t pre_sync_job = _starpu_get_job_associated_to_task(pre_sync_task);
  45. starpu_job_t post_sync_job = _starpu_get_job_associated_to_task(post_sync_task);
  46. if (pre_sync_job->reduction_task || post_sync_job->reduction_task)
  47. return;
  48. }
  49. _STARPU_DEP_DEBUG("Tasks %p %p\n", pre_sync_task, post_sync_task);
  50. /* In case we are generating the DAG, we add an implicit
  51. * dependency between the pre and the post sync tasks in case
  52. * they are not the same. */
  53. if (pre_sync_task != post_sync_task
  54. #ifndef STARPU_USE_FXT
  55. && _starpu_bound_recording
  56. #endif
  57. )
  58. {
  59. starpu_job_t pre_sync_job = _starpu_get_job_associated_to_task(pre_sync_task);
  60. starpu_job_t post_sync_job = _starpu_get_job_associated_to_task(post_sync_task);
  61. STARPU_TRACE_GHOST_TASK_DEPS(pre_sync_job->job_id, post_sync_job->job_id);
  62. _starpu_bound_task_dep(post_sync_job, pre_sync_job);
  63. }
  64. starpu_access_mode previous_mode = handle->last_submitted_mode;
  65. if (mode & STARPU_W)
  66. {
  67. _STARPU_DEP_DEBUG("W %p\n", handle);
  68. if (previous_mode & STARPU_W)
  69. {
  70. _STARPU_DEP_DEBUG("WAW %p\n", handle);
  71. /* (Read) Write */
  72. /* This task depends on the previous writer */
  73. if (handle->last_submitted_writer)
  74. {
  75. struct starpu_task *task_array[1] = {handle->last_submitted_writer};
  76. starpu_task_declare_deps_array(pre_sync_task, 1, task_array);
  77. _STARPU_DEP_DEBUG("dep %p -> %p\n", handle->last_submitted_writer, pre_sync_task);
  78. }
  79. else
  80. {
  81. _STARPU_DEP_DEBUG("No dep\n");
  82. }
  83. /* If there is a ghost writer instead, we
  84. * should declare a ghost dependency here, and
  85. * invalidate the ghost value. */
  86. #ifndef STARPU_USE_FXT
  87. if (_starpu_bound_recording)
  88. #endif
  89. {
  90. if (handle->last_submitted_ghost_writer_id_is_valid)
  91. {
  92. starpu_job_t pre_sync_job = _starpu_get_job_associated_to_task(pre_sync_task);
  93. STARPU_TRACE_GHOST_TASK_DEPS(handle->last_submitted_ghost_writer_id, pre_sync_job->job_id);
  94. _starpu_bound_job_id_dep(pre_sync_job, handle->last_submitted_ghost_writer_id);
  95. _STARPU_DEP_DEBUG("dep ID%lu -> %p\n", handle->last_submitted_ghost_writer_id, pre_sync_task);
  96. handle->last_submitted_ghost_writer_id_is_valid = 0;
  97. }
  98. else
  99. {
  100. _STARPU_DEP_DEBUG("No dep ID\n");
  101. }
  102. }
  103. handle->last_submitted_writer = post_sync_task;
  104. }
  105. else {
  106. /* The task submitted previously were in read-only
  107. * mode: this task must depend on all those read-only
  108. * tasks and we get rid of the list of readers */
  109. _STARPU_DEP_DEBUG("WAR %p\n", handle);
  110. /* Count the readers */
  111. unsigned nreaders = 0;
  112. struct starpu_task_wrapper_list *l;
  113. l = handle->last_submitted_readers;
  114. while (l)
  115. {
  116. nreaders++;
  117. l = l->next;
  118. }
  119. _STARPU_DEP_DEBUG("%d readers\n", nreaders);
  120. struct starpu_task *task_array[nreaders];
  121. unsigned i = 0;
  122. l = handle->last_submitted_readers;
  123. while (l)
  124. {
  125. STARPU_ASSERT(l->task);
  126. task_array[i++] = l->task;
  127. _STARPU_DEP_DEBUG("dep %p -> %p\n", l->task, pre_sync_task);
  128. struct starpu_task_wrapper_list *prev = l;
  129. l = l->next;
  130. free(prev);
  131. }
  132. #ifndef STARPU_USE_FXT
  133. if (_starpu_bound_recording)
  134. #endif
  135. {
  136. /* Declare all dependencies with ghost readers */
  137. starpu_job_t pre_sync_job = _starpu_get_job_associated_to_task(pre_sync_task);
  138. struct starpu_jobid_list *ghost_readers_id = handle->last_submitted_ghost_readers_id;
  139. while (ghost_readers_id)
  140. {
  141. unsigned long id = ghost_readers_id->id;
  142. STARPU_TRACE_GHOST_TASK_DEPS(id, pre_sync_job->job_id);
  143. _starpu_bound_job_id_dep(pre_sync_job, id);
  144. _STARPU_DEP_DEBUG("dep ID%lu -> %p\n", id, pre_sync_task);
  145. struct starpu_jobid_list *prev = ghost_readers_id;
  146. ghost_readers_id = ghost_readers_id->next;
  147. free(prev);
  148. }
  149. handle->last_submitted_ghost_readers_id = NULL;
  150. }
  151. handle->last_submitted_readers = NULL;
  152. handle->last_submitted_writer = post_sync_task;
  153. starpu_task_declare_deps_array(pre_sync_task, nreaders, task_array);
  154. }
  155. }
  156. else {
  157. _STARPU_DEP_DEBUG("R %p\n", handle);
  158. /* Add a reader */
  159. STARPU_ASSERT(pre_sync_task);
  160. STARPU_ASSERT(post_sync_task);
  161. /* Add this task to the list of readers */
  162. struct starpu_task_wrapper_list *link = malloc(sizeof(struct starpu_task_wrapper_list));
  163. link->task = post_sync_task;
  164. link->next = handle->last_submitted_readers;
  165. handle->last_submitted_readers = link;
  166. /* This task depends on the previous writer if any */
  167. if (handle->last_submitted_writer)
  168. {
  169. _STARPU_DEP_DEBUG("RAW %p\n", handle);
  170. struct starpu_task *task_array[1] = {handle->last_submitted_writer};
  171. _STARPU_DEP_DEBUG("dep %p -> %p\n", handle->last_submitted_writer, pre_sync_task);
  172. starpu_task_declare_deps_array(pre_sync_task, 1, task_array);
  173. }
  174. else
  175. {
  176. _STARPU_DEP_DEBUG("No dep\n");
  177. }
  178. /* There was perhaps no last submitted writer but a
  179. * ghost one, we should report that here, and keep the
  180. * ghost writer valid */
  181. if (
  182. #ifndef STARPU_USE_FXT
  183. _starpu_bound_recording &&
  184. #endif
  185. handle->last_submitted_ghost_writer_id_is_valid)
  186. {
  187. starpu_job_t pre_sync_job = _starpu_get_job_associated_to_task(pre_sync_task);
  188. STARPU_TRACE_GHOST_TASK_DEPS(handle->last_submitted_ghost_writer_id, pre_sync_job->job_id);
  189. _starpu_bound_job_id_dep(pre_sync_job, handle->last_submitted_ghost_writer_id);
  190. _STARPU_DEP_DEBUG("dep ID%lu -> %p\n", handle->last_submitted_ghost_writer_id, pre_sync_task);
  191. }
  192. }
  193. handle->last_submitted_mode = mode;
  194. }
  195. _STARPU_LOG_OUT();
  196. }
  197. /* Create the implicit dependencies for a newly submitted task */
  198. void _starpu_detect_implicit_data_deps(struct starpu_task *task)
  199. {
  200. STARPU_ASSERT(task->cl);
  201. _STARPU_LOG_IN();
  202. /* We don't want to enforce a sequential consistency for tasks that are
  203. * not visible to the application. */
  204. starpu_job_t j = _starpu_get_job_associated_to_task(task);
  205. if (j->reduction_task)
  206. return;
  207. unsigned nbuffers = task->cl->nbuffers;
  208. unsigned buffer;
  209. for (buffer = 0; buffer < nbuffers; buffer++)
  210. {
  211. starpu_data_handle handle = task->buffers[buffer].handle;
  212. starpu_access_mode mode = task->buffers[buffer].mode;
  213. /* Scratch memory does not introduce any deps */
  214. if (mode & STARPU_SCRATCH)
  215. continue;
  216. PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  217. _starpu_detect_implicit_data_deps_with_handle(task, task, handle, mode);
  218. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  219. }
  220. _STARPU_LOG_OUT();
  221. }
  222. /* This function is called when a task has been executed so that we don't
  223. * create dependencies to task that do not exist anymore. */
  224. /* NB: We maintain a list of "ghost deps" in case FXT is enabled. Ghost
  225. * dependencies are the dependencies that are implicitely enforced by StarPU
  226. * even if they do not imply a real dependency. For instance in the following
  227. * sequence, f(Ar) g(Ar) h(Aw), we expect to have h depend on both f and g, but
  228. * if h is submitted after the termination of f or g, StarPU will not create a
  229. * dependency as this is not needed anymore. */
  230. void _starpu_release_data_enforce_sequential_consistency(struct starpu_task *task, starpu_data_handle handle)
  231. {
  232. PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  233. if (handle->sequential_consistency)
  234. {
  235. /* If this is the last writer, there is no point in adding
  236. * extra deps to that tasks that does not exists anymore */
  237. if (task == handle->last_submitted_writer)
  238. {
  239. handle->last_submitted_writer = NULL;
  240. #ifndef STARPU_USE_FXT
  241. if (_starpu_bound_recording)
  242. #endif
  243. {
  244. /* Save the previous writer as the ghost last writer */
  245. handle->last_submitted_ghost_writer_id_is_valid = 1;
  246. starpu_job_t ghost_job = _starpu_get_job_associated_to_task(task);
  247. handle->last_submitted_ghost_writer_id = ghost_job->job_id;
  248. }
  249. }
  250. /* XXX can a task be both the last writer associated to a data
  251. * and be in its list of readers ? If not, we should not go
  252. * through the entire list once we have detected it was the
  253. * last writer. */
  254. /* Same if this is one of the readers: we go through the list
  255. * of readers and remove the task if it is found. */
  256. struct starpu_task_wrapper_list *l;
  257. l = handle->last_submitted_readers;
  258. struct starpu_task_wrapper_list *prev = NULL;
  259. while (l)
  260. {
  261. struct starpu_task_wrapper_list *next = l->next;
  262. if (l->task == task)
  263. {
  264. /* If we found the task in the reader list */
  265. free(l);
  266. #ifndef STARPU_USE_FXT
  267. if (_starpu_bound_recording)
  268. #endif
  269. {
  270. /* Save the job id of the reader task in the ghost reader linked list list */
  271. starpu_job_t ghost_reader_job = _starpu_get_job_associated_to_task(task);
  272. struct starpu_jobid_list *link = malloc(sizeof(struct starpu_jobid_list));
  273. STARPU_ASSERT(link);
  274. link->next = handle->last_submitted_ghost_readers_id;
  275. link->id = ghost_reader_job->job_id;
  276. handle->last_submitted_ghost_readers_id = link;
  277. }
  278. if (prev)
  279. {
  280. prev->next = next;
  281. }
  282. else {
  283. /* This is the first element of the list */
  284. handle->last_submitted_readers = next;
  285. }
  286. /* XXX can we really find the same task again
  287. * once we have found it ? Otherwise, we should
  288. * avoid going through the entire list and stop
  289. * as soon as we find the task. TODO: check how
  290. * duplicate dependencies are treated. */
  291. }
  292. else {
  293. prev = l;
  294. }
  295. l = next;
  296. }
  297. }
  298. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  299. }
  300. void _starpu_add_post_sync_tasks(struct starpu_task *post_sync_task, starpu_data_handle handle)
  301. {
  302. _STARPU_LOG_IN();
  303. PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  304. if (handle->sequential_consistency)
  305. {
  306. handle->post_sync_tasks_cnt++;
  307. struct starpu_task_wrapper_list *link = malloc(sizeof(struct starpu_task_wrapper_list));
  308. link->task = post_sync_task;
  309. link->next = handle->post_sync_tasks;
  310. handle->post_sync_tasks = link;
  311. }
  312. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  313. _STARPU_LOG_OUT();
  314. }
  315. void _starpu_unlock_post_sync_tasks(starpu_data_handle handle)
  316. {
  317. struct starpu_task_wrapper_list *post_sync_tasks = NULL;
  318. unsigned do_submit_tasks = 0;
  319. PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  320. if (handle->sequential_consistency)
  321. {
  322. STARPU_ASSERT(handle->post_sync_tasks_cnt > 0);
  323. if (--handle->post_sync_tasks_cnt == 0)
  324. {
  325. /* unlock all tasks : we need not hold the lock while unlocking all these tasks */
  326. do_submit_tasks = 1;
  327. post_sync_tasks = handle->post_sync_tasks;
  328. handle->post_sync_tasks = NULL;
  329. }
  330. }
  331. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  332. if (do_submit_tasks)
  333. {
  334. struct starpu_task_wrapper_list *link = post_sync_tasks;
  335. while (link) {
  336. /* There is no need to depend on that task now, since it was already unlocked */
  337. _starpu_release_data_enforce_sequential_consistency(link->task, handle);
  338. int ret = starpu_task_submit(link->task);
  339. STARPU_ASSERT(!ret);
  340. link = link->next;
  341. }
  342. }
  343. }
  344. /* If sequential consistency mode is enabled, this function blocks until the
  345. * handle is available in the requested access mode. */
  346. int _starpu_data_wait_until_available(starpu_data_handle handle, starpu_access_mode mode)
  347. {
  348. /* If sequential consistency is enabled, wait until data is available */
  349. PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  350. int sequential_consistency = handle->sequential_consistency;
  351. if (sequential_consistency)
  352. {
  353. struct starpu_task *sync_task;
  354. sync_task = starpu_task_create();
  355. sync_task->detach = 0;
  356. sync_task->destroy = 1;
  357. /* It is not really a RW access, but we want to make sure that
  358. * all previous accesses are done */
  359. _starpu_detect_implicit_data_deps_with_handle(sync_task, sync_task, handle, mode);
  360. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  361. /* TODO detect if this is superflous */
  362. int ret = starpu_task_submit(sync_task);
  363. STARPU_ASSERT(!ret);
  364. starpu_task_wait(sync_task);
  365. }
  366. else {
  367. PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  368. }
  369. return 0;
  370. }