task_deps.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2015, 2016 CNRS
  5. * Copyright (C) 2014, 2016 INRIA
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <common/config.h>
  20. #include <common/utils.h>
  21. #include <common/graph.h>
  22. #include <core/dependencies/tags.h>
  23. #include <core/jobs.h>
  24. #include <core/task.h>
  25. #include <core/sched_policy.h>
  26. #include <core/dependencies/data_concurrency.h>
  27. #include <profiling/bound.h>
  28. #include <core/debug.h>
  29. static struct _starpu_cg *create_cg_task(unsigned ntags, struct _starpu_job *j)
  30. {
  31. struct _starpu_cg *cg;
  32. _STARPU_MALLOC(cg, sizeof(struct _starpu_cg));
  33. cg->ntags = ntags;
  34. cg->remaining = ntags;
  35. cg->cg_type = STARPU_CG_TASK;
  36. cg->succ.job = j;
  37. j->job_successors.ndeps++;
  38. return cg;
  39. }
  40. static void _starpu_task_add_succ(struct _starpu_job *j, struct _starpu_cg *cg)
  41. {
  42. STARPU_ASSERT(j);
  43. if (_starpu_add_successor_to_cg_list(&j->job_successors, cg))
  44. /* the task was already completed sooner */
  45. _starpu_notify_cg(cg);
  46. }
  47. void _starpu_notify_task_dependencies(struct _starpu_job *j)
  48. {
  49. _starpu_notify_cg_list(&j->job_successors);
  50. }
  51. /* task depends on the tasks in task array */
  52. void _starpu_task_declare_deps_array(struct starpu_task *task, unsigned ndeps, struct starpu_task *task_array[], int check)
  53. {
  54. if (ndeps == 0)
  55. return;
  56. struct _starpu_job *job;
  57. job = _starpu_get_job_associated_to_task(task);
  58. STARPU_PTHREAD_MUTEX_LOCK(&job->sync_mutex);
  59. if (check)
  60. STARPU_ASSERT_MSG(
  61. !job->submitted || !task->destroy || task->detach
  62. #ifdef STARPU_OPENMP
  63. || job->continuation
  64. #endif
  65. , "Task dependencies have to be set before submission (submitted %u destroy %u detach %u)", job->submitted, task->destroy, task->detach);
  66. else
  67. STARPU_ASSERT_MSG(job->terminated <= 1, "Task dependencies have to be set before termination (terminated %u)", job->terminated);
  68. struct _starpu_cg *cg = create_cg_task(ndeps, job);
  69. STARPU_PTHREAD_MUTEX_UNLOCK(&job->sync_mutex);
  70. unsigned i;
  71. for (i = 0; i < ndeps; i++)
  72. {
  73. struct starpu_task *dep_task = task_array[i];
  74. struct _starpu_job *dep_job;
  75. struct _starpu_cg *back_cg = NULL;
  76. dep_job = _starpu_get_job_associated_to_task(dep_task);
  77. STARPU_ASSERT_MSG(dep_job != job, "A task must not depend on itself.");
  78. STARPU_PTHREAD_MUTEX_LOCK(&dep_job->sync_mutex);
  79. if (check)
  80. {
  81. STARPU_ASSERT_MSG(!dep_job->submitted || !dep_job->task->destroy || dep_job->task->detach, "Unless it is not to be destroyed automatically, a task dependencies have to be set before submission");
  82. STARPU_ASSERT_MSG(dep_job->submitted != 2, "For resubmited tasks, dependencies have to be set before first re-submission");
  83. STARPU_ASSERT_MSG(!dep_job->submitted || !dep_job->task->regenerate, "For regenerated tasks, dependencies have to be set before first submission");
  84. }
  85. else
  86. STARPU_ASSERT_MSG(dep_job->terminated <= 1, "Task dependencies have to be set before termination (terminated %u)", dep_job->terminated);
  87. if (dep_job->task->regenerate)
  88. {
  89. /* Make sure we don't regenerate the dependency before this task is finished */
  90. back_cg = create_cg_task(1, dep_job);
  91. /* Just do not take that dependency into account for the first submission */
  92. dep_job->job_successors.ndeps_completed++;
  93. }
  94. STARPU_PTHREAD_MUTEX_UNLOCK(&dep_job->sync_mutex);
  95. _STARPU_TRACE_TASK_DEPS(dep_job, job);
  96. _starpu_bound_task_dep(job, dep_job);
  97. if (check)
  98. {
  99. STARPU_AYU_ADDDEPENDENCY(dep_job->job_id, 0, job->job_id);
  100. }
  101. if (_starpu_graph_record)
  102. _starpu_graph_add_job_dep(job, dep_job);
  103. _starpu_task_add_succ(dep_job, cg);
  104. if (dep_job->task->regenerate)
  105. _starpu_task_add_succ(job, back_cg);
  106. }
  107. }
  108. void starpu_task_declare_deps_array(struct starpu_task *task, unsigned ndeps, struct starpu_task *task_array[])
  109. {
  110. _starpu_task_declare_deps_array(task, ndeps, task_array, 1);
  111. }
  112. int starpu_task_get_task_succs(struct starpu_task *task, unsigned ndeps, struct starpu_task *task_array[])
  113. {
  114. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  115. return _starpu_list_task_successors_in_cg_list(&j->job_successors, ndeps, task_array);
  116. }
  117. int starpu_task_get_task_scheduled_succs(struct starpu_task *task, unsigned ndeps, struct starpu_task *task_array[])
  118. {
  119. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  120. return _starpu_list_task_scheduled_successors_in_cg_list(&j->job_successors, ndeps, task_array);
  121. }