implicit_data_deps.c 14 KB

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