task_deps.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2014 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 <core/dependencies/tags.h>
  22. #include <core/jobs.h>
  23. #include <core/task.h>
  24. #include <core/sched_policy.h>
  25. #include <core/dependencies/data_concurrency.h>
  26. #include <profiling/bound.h>
  27. #include <core/debug.h>
  28. static struct _starpu_cg *create_cg_task(unsigned ntags, struct _starpu_job *j)
  29. {
  30. struct _starpu_cg *cg = (struct _starpu_cg *) malloc(sizeof(struct _starpu_cg));
  31. STARPU_ASSERT(cg);
  32. cg->ntags = ntags;
  33. cg->remaining = ntags;
  34. cg->cg_type = STARPU_CG_TASK;
  35. cg->succ.job = j;
  36. j->job_successors.ndeps++;
  37. return cg;
  38. }
  39. static void _starpu_task_add_succ(struct _starpu_job *j, struct _starpu_cg *cg)
  40. {
  41. STARPU_ASSERT(j);
  42. if (_starpu_add_successor_to_cg_list(&j->job_successors, cg))
  43. /* the task was already completed sooner */
  44. _starpu_notify_cg(cg);
  45. }
  46. void _starpu_notify_task_dependencies(struct _starpu_job *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[], int check)
  52. {
  53. if (ndeps == 0)
  54. return;
  55. struct _starpu_job *job;
  56. job = _starpu_get_job_associated_to_task(task);
  57. STARPU_PTHREAD_MUTEX_LOCK(&job->sync_mutex);
  58. if (check)
  59. STARPU_ASSERT_MSG(
  60. !job->submitted || !task->destroy || task->detach
  61. #ifdef STARPU_OPENMP
  62. || job->continuation
  63. #endif
  64. , "Task dependencies have to be set before submission (submitted %u destroy %d detach %d)", job->submitted, task->destroy, task->detach);
  65. else
  66. STARPU_ASSERT_MSG(job->terminated <= 1, "Task dependencies have to be set before termination (terminated %u)", job->terminated);
  67. struct _starpu_cg *cg = create_cg_task(ndeps, job);
  68. STARPU_PTHREAD_MUTEX_UNLOCK(&job->sync_mutex);
  69. unsigned i;
  70. for (i = 0; i < ndeps; i++)
  71. {
  72. struct starpu_task *dep_task = task_array[i];
  73. struct _starpu_job *dep_job;
  74. dep_job = _starpu_get_job_associated_to_task(dep_task);
  75. STARPU_ASSERT_MSG(dep_job != job, "A task must not depend on itself.");
  76. STARPU_PTHREAD_MUTEX_LOCK(&dep_job->sync_mutex);
  77. if (check)
  78. {
  79. 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");
  80. STARPU_ASSERT_MSG(dep_job->submitted != 2, "For resubmited tasks, dependencies have to be set before first re-submission");
  81. STARPU_ASSERT_MSG(!dep_job->submitted || !dep_job->task->regenerate, "For regenerated tasks, dependencies have to be set before first submission");
  82. } else
  83. STARPU_ASSERT_MSG(dep_job->terminated <= 1, "Task dependencies have to be set before termination (terminated %u)", dep_job->terminated);
  84. STARPU_PTHREAD_MUTEX_UNLOCK(&dep_job->sync_mutex);
  85. _STARPU_TRACE_TASK_DEPS(dep_job, job);
  86. _starpu_bound_task_dep(job, dep_job);
  87. #ifdef HAVE_AYUDAME_H
  88. if (AYU_event && check)
  89. {
  90. uintptr_t AYU_data[3] = {dep_job->job_id, 0, 0};
  91. AYU_event(AYU_ADDDEPENDENCY, job->job_id, AYU_data);
  92. }
  93. #endif
  94. _starpu_task_add_succ(dep_job, cg);
  95. }
  96. }
  97. void starpu_task_declare_deps_array(struct starpu_task *task, unsigned ndeps, struct starpu_task *task_array[])
  98. {
  99. _starpu_task_declare_deps_array(task, ndeps, task_array, 1);
  100. }