implicit_data_deps.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2013 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. #include <core/debug.h>
  23. #if 0
  24. # define _STARPU_DEP_DEBUG(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__);
  25. #else
  26. # define _STARPU_DEP_DEBUG(fmt, ...)
  27. #endif
  28. static void _starpu_add_ghost_dependency(starpu_data_handle_t handle STARPU_ATTRIBUTE_UNUSED, unsigned long previous STARPU_ATTRIBUTE_UNUSED, struct starpu_task *next STARPU_ATTRIBUTE_UNUSED)
  29. {
  30. struct _starpu_job *next_job = _starpu_get_job_associated_to_task(next);
  31. _starpu_bound_job_id_dep(handle, next_job, previous);
  32. #ifdef HAVE_AYUDAME_H
  33. if (AYU_event)
  34. {
  35. uintptr_t AYU_data[3] = { previous, (uintptr_t) handle, (uintptr_t) handle };
  36. AYU_event(AYU_ADDDEPENDENCY, next_job->job_id, AYU_data);
  37. }
  38. #endif
  39. }
  40. static void _starpu_add_dependency(starpu_data_handle_t handle STARPU_ATTRIBUTE_UNUSED, struct starpu_task *previous STARPU_ATTRIBUTE_UNUSED, struct starpu_task *next STARPU_ATTRIBUTE_UNUSED)
  41. {
  42. _starpu_add_ghost_dependency(handle, _starpu_get_job_associated_to_task(previous)->job_id, next);
  43. }
  44. /* Add pre_sync_task as new accessor among the existing ones, making it depend on the last synchronization task if any. */
  45. static void _starpu_add_accessor(starpu_data_handle_t handle, struct starpu_task *pre_sync_task, struct starpu_task *post_sync_task)
  46. {
  47. /* Add this task to the list of readers */
  48. struct _starpu_task_wrapper_list *link = (struct _starpu_task_wrapper_list *) malloc(sizeof(struct _starpu_task_wrapper_list));
  49. link->task = post_sync_task;
  50. link->next = handle->last_submitted_accessors;
  51. handle->last_submitted_accessors = link;
  52. /* This task depends on the previous synchronization task if any */
  53. if (handle->last_sync_task && handle->last_sync_task != post_sync_task)
  54. {
  55. struct starpu_task *task_array[1] = {handle->last_sync_task};
  56. _starpu_task_declare_deps_array(pre_sync_task, 1, task_array, 0);
  57. _starpu_add_dependency(handle, handle->last_sync_task, pre_sync_task);
  58. _STARPU_DEP_DEBUG("dep %p -> %p\n", handle->last_sync_task, pre_sync_task);
  59. }
  60. else
  61. {
  62. _STARPU_DEP_DEBUG("No dep\n");
  63. }
  64. /* There was perhaps no last submitted writer but a
  65. * ghost one, we should report that here, and keep the
  66. * ghost writer valid */
  67. if (
  68. (
  69. #ifdef STARPU_USE_FXT
  70. 1
  71. #else
  72. _starpu_bound_recording
  73. #endif
  74. #ifdef HAVE_AYUDAME_H
  75. || AYU_event
  76. #endif
  77. ) && handle->last_submitted_ghost_sync_id_is_valid)
  78. {
  79. _STARPU_TRACE_GHOST_TASK_DEPS(handle->last_submitted_ghost_sync_id,
  80. _starpu_get_job_associated_to_task(pre_sync_task)->job_id);
  81. _starpu_add_ghost_dependency(handle, handle->last_submitted_ghost_sync_id, pre_sync_task);
  82. _STARPU_DEP_DEBUG("dep ID%lu -> %p\n", handle->last_submitted_ghost_sync_id, pre_sync_task);
  83. }
  84. if (!pre_sync_task->cl) {
  85. /* Add a reference to be released in _starpu_handle_job_termination */
  86. _starpu_spin_lock(&handle->header_lock);
  87. handle->busy_count++;
  88. _starpu_spin_unlock(&handle->header_lock);
  89. _starpu_get_job_associated_to_task(pre_sync_task)->implicit_dep_handle = handle;
  90. }
  91. }
  92. /* This adds a new synchronization task which depends on all the previous accessors */
  93. static void _starpu_add_sync_task(starpu_data_handle_t handle, struct starpu_task *pre_sync_task, struct starpu_task *post_sync_task)
  94. {
  95. /* Count the existing accessors */
  96. unsigned naccessors = 0;
  97. struct _starpu_task_wrapper_list *l;
  98. l = handle->last_submitted_accessors;
  99. while (l)
  100. {
  101. if (l->task != post_sync_task)
  102. naccessors++;
  103. l = l->next;
  104. }
  105. _STARPU_DEP_DEBUG("%d accessors\n", naccessors);
  106. if (naccessors > 0)
  107. {
  108. /* Put all tasks in the list into task_array */
  109. struct starpu_task *task_array[naccessors];
  110. unsigned i = 0;
  111. l = handle->last_submitted_accessors;
  112. while (l)
  113. {
  114. STARPU_ASSERT(l->task);
  115. if (l->task != post_sync_task)
  116. {
  117. task_array[i++] = l->task;
  118. _starpu_add_dependency(handle, l->task, pre_sync_task);
  119. _STARPU_DEP_DEBUG("dep %p -> %p\n", l->task, pre_sync_task);
  120. }
  121. struct _starpu_task_wrapper_list *prev = l;
  122. l = l->next;
  123. free(prev);
  124. }
  125. _starpu_task_declare_deps_array(pre_sync_task, naccessors, task_array, 0);
  126. }
  127. #ifndef STARPU_USE_FXT
  128. if (_starpu_bound_recording)
  129. #endif
  130. {
  131. /* Declare all dependencies with ghost accessors */
  132. struct _starpu_jobid_list *ghost_accessors_id = handle->last_submitted_ghost_accessors_id;
  133. while (ghost_accessors_id)
  134. {
  135. unsigned long id = ghost_accessors_id->id;
  136. _STARPU_TRACE_GHOST_TASK_DEPS(id,
  137. _starpu_get_job_associated_to_task(pre_sync_task)->job_id);
  138. _starpu_add_ghost_dependency(handle, id, pre_sync_task);
  139. _STARPU_DEP_DEBUG("dep ID%lu -> %p\n", id, pre_sync_task);
  140. struct _starpu_jobid_list *prev = ghost_accessors_id;
  141. ghost_accessors_id = ghost_accessors_id->next;
  142. free(prev);
  143. }
  144. handle->last_submitted_ghost_accessors_id = NULL;
  145. }
  146. handle->last_submitted_accessors = NULL;
  147. handle->last_sync_task = post_sync_task;
  148. if (!post_sync_task->cl) {
  149. /* Add a reference to be released in _starpu_handle_job_termination */
  150. _starpu_spin_lock(&handle->header_lock);
  151. handle->busy_count++;
  152. _starpu_spin_unlock(&handle->header_lock);
  153. _starpu_get_job_associated_to_task(post_sync_task)->implicit_dep_handle = handle;
  154. }
  155. }
  156. /* This function adds the implicit task dependencies introduced by data
  157. * sequential consistency. Two tasks are provided: pre_sync and post_sync which
  158. * respectively indicates which task is going to depend on the previous deps
  159. * and on which task future deps should wait. In the case of a dependency
  160. * introduced by a task submission, both tasks are just the submitted task, but
  161. * in the case of user interactions with the DSM, these may be different tasks.
  162. * */
  163. /* NB : handle->sequential_consistency_mutex must be hold by the caller;
  164. * returns a task, to be submitted after releasing that mutex. */
  165. struct starpu_task *_starpu_detect_implicit_data_deps_with_handle(struct starpu_task *pre_sync_task, struct starpu_task *post_sync_task,
  166. starpu_data_handle_t handle, enum starpu_data_access_mode mode)
  167. {
  168. struct starpu_task *task = NULL;
  169. STARPU_ASSERT(!(mode & STARPU_SCRATCH));
  170. _STARPU_LOG_IN();
  171. if (handle->sequential_consistency)
  172. {
  173. struct _starpu_job *pre_sync_job = _starpu_get_job_associated_to_task(pre_sync_task);
  174. struct _starpu_job *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 NULL;
  179. /* In case we are generating the DAG, we add an implicit
  180. * dependency between the pre and the post sync tasks in case
  181. * they are not the same. */
  182. if (pre_sync_task != post_sync_task
  183. #ifndef STARPU_USE_FXT
  184. && _starpu_bound_recording
  185. #endif
  186. )
  187. {
  188. _STARPU_TRACE_GHOST_TASK_DEPS(pre_sync_job->job_id, post_sync_job->job_id);
  189. _starpu_bound_task_dep(post_sync_job, pre_sync_job);
  190. }
  191. enum starpu_data_access_mode previous_mode = handle->last_submitted_mode;
  192. _STARPU_DEP_DEBUG("Handle %p Tasks %p %p %x->%x\n", handle, pre_sync_task, post_sync_task, previous_mode, mode);
  193. /*
  194. * Tasks can access the data concurrently only if they have the
  195. * same access mode, which can only be either:
  196. * - write with STARPU_COMMUTE
  197. * - read
  198. * - redux
  199. *
  200. * In other cases, the tasks have to depend on each other.
  201. */
  202. if ((mode & STARPU_W && mode & STARPU_COMMUTE && previous_mode & STARPU_W && previous_mode && STARPU_COMMUTE)
  203. || (mode == STARPU_R && previous_mode == STARPU_R)
  204. || (mode == STARPU_REDUX && previous_mode == STARPU_REDUX))
  205. {
  206. _STARPU_DEP_DEBUG("concurrently\n");
  207. /* Can access concurrently with current tasks */
  208. _starpu_add_accessor(handle, pre_sync_task, post_sync_task);
  209. }
  210. else
  211. {
  212. /* Can not access concurrently, have to wait for existing accessors */
  213. struct _starpu_task_wrapper_list *l = handle->last_submitted_accessors;
  214. _STARPU_DEP_DEBUG("dependency\n");
  215. if (l && l->next)
  216. {
  217. /* Several previous accessors */
  218. if (mode == STARPU_W)
  219. {
  220. /* Optimization: this task can not
  221. * combine with others anyway, use it
  222. * as synchronization task by making it
  223. * wait for the previous ones. */
  224. _starpu_add_sync_task(handle, pre_sync_task, post_sync_task);
  225. } else {
  226. _STARPU_DEP_DEBUG("several predecessors, adding sync task\n");
  227. /* insert an empty synchronization task
  228. * which waits for the whole set,
  229. * instead of creating a quadratic
  230. * number of dependencies. */
  231. struct starpu_task *sync_task = starpu_task_create();
  232. STARPU_ASSERT(sync_task);
  233. sync_task->cl = NULL;
  234. #ifdef STARPU_USE_FXT
  235. _starpu_get_job_associated_to_task(sync_task)->model_name = "sync_task_redux";
  236. #endif
  237. /* Make this task wait for the previous ones */
  238. _starpu_add_sync_task(handle, sync_task, sync_task);
  239. /* And the requested task wait for this one */
  240. _starpu_add_accessor(handle, pre_sync_task, post_sync_task);
  241. task = sync_task;
  242. }
  243. }
  244. else
  245. {
  246. if (l)
  247. {
  248. /* One previous accessor, make it the sync
  249. * task, and start depending on it. */
  250. handle->last_sync_task = l->task;
  251. handle->last_submitted_accessors = NULL;
  252. free(l);
  253. }
  254. _starpu_add_accessor(handle, pre_sync_task, post_sync_task);
  255. }
  256. }
  257. handle->last_submitted_mode = mode;
  258. }
  259. _STARPU_LOG_OUT();
  260. return task;
  261. }
  262. /* Create the implicit dependencies for a newly submitted task */
  263. void _starpu_detect_implicit_data_deps(struct starpu_task *task)
  264. {
  265. STARPU_ASSERT(task->cl);
  266. _STARPU_LOG_IN();
  267. if (!task->sequential_consistency)
  268. return;
  269. /* We don't want to enforce a sequential consistency for tasks that are
  270. * not visible to the application. */
  271. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  272. if (j->reduction_task)
  273. return;
  274. unsigned nbuffers = task->cl->nbuffers;
  275. unsigned buffer;
  276. for (buffer = 0; buffer < nbuffers; buffer++)
  277. {
  278. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, buffer);
  279. enum starpu_data_access_mode mode = STARPU_CODELET_GET_MODE(task->cl, buffer);
  280. struct starpu_task *new_task;
  281. /* Scratch memory does not introduce any deps */
  282. if (mode & STARPU_SCRATCH)
  283. continue;
  284. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  285. new_task = _starpu_detect_implicit_data_deps_with_handle(task, task, handle, mode);
  286. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  287. if (new_task)
  288. {
  289. int ret = _starpu_task_submit_internally(new_task);
  290. STARPU_ASSERT(!ret);
  291. }
  292. }
  293. _STARPU_LOG_OUT();
  294. }
  295. /* This function is called when a task has been executed so that we don't
  296. * create dependencies to task that do not exist anymore. */
  297. /* NB: We maintain a list of "ghost deps" in case FXT is enabled. Ghost
  298. * dependencies are the dependencies that are implicitely enforced by StarPU
  299. * even if they do not imply a real dependency. For instance in the following
  300. * sequence, f(Ar) g(Ar) h(Aw), we expect to have h depend on both f and g, but
  301. * if h is submitted after the termination of f or g, StarPU will not create a
  302. * dependency as this is not needed anymore. */
  303. /* the sequential_consistency_mutex of the handle has to be already held */
  304. void _starpu_release_data_enforce_sequential_consistency(struct starpu_task *task, starpu_data_handle_t handle)
  305. {
  306. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  307. if (handle->sequential_consistency)
  308. {
  309. /* If this is the last writer, there is no point in adding
  310. * extra deps to that tasks that does not exists anymore */
  311. if (task == handle->last_sync_task)
  312. {
  313. handle->last_sync_task = NULL;
  314. #ifndef STARPU_USE_FXT
  315. if (_starpu_bound_recording)
  316. #endif
  317. {
  318. /* Save the previous writer as the ghost last writer */
  319. handle->last_submitted_ghost_sync_id_is_valid = 1;
  320. struct _starpu_job *ghost_job = _starpu_get_job_associated_to_task(task);
  321. handle->last_submitted_ghost_sync_id = ghost_job->job_id;
  322. }
  323. }
  324. /* XXX can a task be both the last writer associated to a data
  325. * and be in its list of readers ? If not, we should not go
  326. * through the entire list once we have detected it was the
  327. * last writer. */
  328. /* Same if this is one of the readers: we go through the list
  329. * of readers and remove the task if it is found. */
  330. struct _starpu_task_wrapper_list *l;
  331. l = handle->last_submitted_accessors;
  332. struct _starpu_task_wrapper_list *prev = NULL;
  333. #ifdef STARPU_DEVEL
  334. #warning TODO: use double-linked list to make finding ourself fast
  335. #endif
  336. while (l)
  337. {
  338. struct _starpu_task_wrapper_list *next = l->next;
  339. if (l->task == task)
  340. {
  341. /* If we found the task in the reader list */
  342. free(l);
  343. #ifndef STARPU_USE_FXT
  344. if (_starpu_bound_recording)
  345. #endif
  346. {
  347. /* Save the job id of the reader task in the ghost reader linked list list */
  348. struct _starpu_job *ghost_reader_job = _starpu_get_job_associated_to_task(task);
  349. struct _starpu_jobid_list *link = (struct _starpu_jobid_list *) malloc(sizeof(struct _starpu_jobid_list));
  350. STARPU_ASSERT(link);
  351. link->next = handle->last_submitted_ghost_accessors_id;
  352. link->id = ghost_reader_job->job_id;
  353. handle->last_submitted_ghost_accessors_id = link;
  354. }
  355. if (prev)
  356. {
  357. prev->next = next;
  358. }
  359. else
  360. {
  361. /* This is the first element of the list */
  362. handle->last_submitted_accessors = next;
  363. }
  364. /* XXX can we really find the same task again
  365. * once we have found it ? Otherwise, we should
  366. * avoid going through the entire list and stop
  367. * as soon as we find the task. TODO: check how
  368. * duplicate dependencies are treated. */
  369. }
  370. else
  371. {
  372. prev = l;
  373. }
  374. l = next;
  375. }
  376. }
  377. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  378. }
  379. /* This is the same as _starpu_release_data_enforce_sequential_consistency, but
  380. * for all data of a task */
  381. void _starpu_release_task_enforce_sequential_consistency(struct _starpu_job *j)
  382. {
  383. struct starpu_task *task = j->task;
  384. struct starpu_data_descr *descrs = _STARPU_JOB_GET_ORDERED_BUFFERS(j);
  385. if (!task->cl)
  386. return;
  387. unsigned nbuffers = task->cl->nbuffers;
  388. unsigned index;
  389. for (index = 0; index < nbuffers; index++)
  390. {
  391. starpu_data_handle_t handle = descrs[index].handle;
  392. if (index && descrs[index-1].handle == descrs[index].handle)
  393. /* We have already released this data, skip it. This
  394. * depends on ordering putting writes before reads, see
  395. * _starpu_compar_handles */
  396. continue;
  397. _starpu_release_data_enforce_sequential_consistency(task, handle);
  398. /* Release the reference acquired in _starpu_push_task_output */
  399. _starpu_spin_lock(&handle->header_lock);
  400. STARPU_ASSERT(handle->busy_count > 0);
  401. handle->busy_count--;
  402. if (!_starpu_data_check_not_busy(handle))
  403. _starpu_spin_unlock(&handle->header_lock);
  404. }
  405. }
  406. void _starpu_add_post_sync_tasks(struct starpu_task *post_sync_task, starpu_data_handle_t handle)
  407. {
  408. _STARPU_LOG_IN();
  409. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  410. if (handle->sequential_consistency)
  411. {
  412. handle->post_sync_tasks_cnt++;
  413. struct _starpu_task_wrapper_list *link = (struct _starpu_task_wrapper_list *) malloc(sizeof(struct _starpu_task_wrapper_list));
  414. link->task = post_sync_task;
  415. link->next = handle->post_sync_tasks;
  416. handle->post_sync_tasks = link;
  417. }
  418. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  419. _STARPU_LOG_OUT();
  420. }
  421. void _starpu_unlock_post_sync_tasks(starpu_data_handle_t handle)
  422. {
  423. struct _starpu_task_wrapper_list *post_sync_tasks = NULL;
  424. unsigned do_submit_tasks = 0;
  425. if (handle->post_sync_tasks_cnt > 0)
  426. {
  427. if (--handle->post_sync_tasks_cnt == 0)
  428. {
  429. /* unlock all tasks : we need not hold the lock while unlocking all these tasks */
  430. do_submit_tasks = 1;
  431. post_sync_tasks = handle->post_sync_tasks;
  432. handle->post_sync_tasks = NULL;
  433. }
  434. }
  435. if (do_submit_tasks)
  436. {
  437. struct _starpu_task_wrapper_list *link = post_sync_tasks;
  438. while (link)
  439. {
  440. /* There is no need to depend on that task now, since it was already unlocked */
  441. _starpu_release_data_enforce_sequential_consistency(link->task, handle);
  442. int ret = _starpu_task_submit_internally(link->task);
  443. STARPU_ASSERT(!ret);
  444. struct _starpu_task_wrapper_list *tmp = link;
  445. link = link->next;
  446. free(tmp);
  447. }
  448. }
  449. }
  450. /* If sequential consistency mode is enabled, this function blocks until the
  451. * handle is available in the requested access mode. */
  452. int _starpu_data_wait_until_available(starpu_data_handle_t handle, enum starpu_data_access_mode mode)
  453. {
  454. /* If sequential consistency is enabled, wait until data is available */
  455. STARPU_PTHREAD_MUTEX_LOCK(&handle->sequential_consistency_mutex);
  456. int sequential_consistency = handle->sequential_consistency;
  457. if (sequential_consistency)
  458. {
  459. struct starpu_task *sync_task, *new_task;
  460. sync_task = starpu_task_create();
  461. sync_task->detach = 0;
  462. sync_task->destroy = 1;
  463. #ifdef STARPU_USE_FXT
  464. _starpu_get_job_associated_to_task(sync_task)->model_name = "sync_task";
  465. #endif
  466. /* It is not really a RW access, but we want to make sure that
  467. * all previous accesses are done */
  468. new_task = _starpu_detect_implicit_data_deps_with_handle(sync_task, sync_task, handle, mode);
  469. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  470. if (new_task)
  471. {
  472. int ret = _starpu_task_submit_internally(new_task);
  473. STARPU_ASSERT(!ret);
  474. }
  475. /* TODO detect if this is superflous */
  476. int ret = _starpu_task_submit_internally(sync_task);
  477. STARPU_ASSERT(!ret);
  478. ret = starpu_task_wait(sync_task);
  479. STARPU_ASSERT(ret == 0);
  480. }
  481. else
  482. {
  483. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->sequential_consistency_mutex);
  484. }
  485. return 0;
  486. }