implicit_data_deps.c 16 KB

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