task_deps.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <common/utils.h>
  19. #include <core/dependencies/tags.h>
  20. #include <core/dependencies/htable.h>
  21. #include <core/jobs.h>
  22. #include <core/task.h>
  23. #include <core/sched_policy.h>
  24. #include <core/dependencies/data_concurrency.h>
  25. static starpu_cg_t *create_cg_task(unsigned ntags, starpu_job_t j)
  26. {
  27. starpu_cg_t *cg = malloc(sizeof(starpu_cg_t));
  28. STARPU_ASSERT(cg);
  29. cg->ntags = ntags;
  30. cg->remaining = ntags;
  31. cg->cg_type = STARPU_CG_TASK;
  32. cg->succ.job = j;
  33. j->job_successors.ndeps++;
  34. return cg;
  35. }
  36. /* the job lock must be taken */
  37. static void _starpu_task_add_succ(starpu_job_t j, starpu_cg_t *cg)
  38. {
  39. STARPU_ASSERT(j);
  40. _starpu_add_successor_to_cg_list(&j->job_successors, cg);
  41. if (j->terminated) {
  42. /* the task was already completed sooner */
  43. _starpu_notify_cg(cg);
  44. }
  45. }
  46. void _starpu_notify_task_dependencies(starpu_job_t j)
  47. {
  48. _starpu_notify_cg_list(&j->job_successors);
  49. }
  50. /* task depends on the tasks in task array */
  51. void starpu_task_declare_deps_array(struct starpu_task *task, unsigned ndeps, struct starpu_task *task_array[])
  52. {
  53. if (ndeps == 0)
  54. return;
  55. starpu_job_t job;
  56. job = _starpu_get_job_associated_to_task(task);
  57. PTHREAD_MUTEX_LOCK(&job->sync_mutex);
  58. starpu_cg_t *cg = create_cg_task(ndeps, job);
  59. unsigned i;
  60. for (i = 0; i < ndeps; i++)
  61. {
  62. struct starpu_task *dep_task = task_array[i];
  63. starpu_job_t dep_job;
  64. dep_job = _starpu_get_job_associated_to_task(dep_task);
  65. STARPU_ASSERT(dep_job != job);
  66. STARPU_TRACE_TASK_DEPS(dep_job, job);
  67. _starpu_bound_task_dep(job, dep_job);
  68. PTHREAD_MUTEX_LOCK(&dep_job->sync_mutex);
  69. _starpu_task_add_succ(dep_job, cg);
  70. PTHREAD_MUTEX_UNLOCK(&dep_job->sync_mutex);
  71. }
  72. PTHREAD_MUTEX_UNLOCK(&job->sync_mutex);
  73. }