eager_central_priority_policy.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /*
  17. * This is policy where every worker use the same JOB QUEUE, but taking
  18. * task priorities into account
  19. */
  20. #include <starpu.h>
  21. #include <starpu_scheduler.h>
  22. #include <common/config.h>
  23. #include <core/workers.h>
  24. #include <common/utils.h>
  25. #define MIN_LEVEL (-5)
  26. #define MAX_LEVEL (+5)
  27. #define NPRIO_LEVELS (MAX_LEVEL - MIN_LEVEL + 1)
  28. struct starpu_priority_taskq_s {
  29. /* the actual lists
  30. * taskq[p] is for priority [p - STARPU_MIN_PRIO] */
  31. struct starpu_task_list taskq[NPRIO_LEVELS];
  32. unsigned ntasks[NPRIO_LEVELS];
  33. unsigned total_ntasks;
  34. };
  35. /* the former is the actual queue, the latter some container */
  36. static struct starpu_priority_taskq_s *taskq;
  37. /* keep track of the total number of tasks to be scheduled to avoid infinite
  38. * polling when there are really few tasks in the overall queue */
  39. static pthread_cond_t global_sched_cond;
  40. static pthread_mutex_t global_sched_mutex;
  41. /*
  42. * Centralized queue with priorities
  43. */
  44. static struct starpu_priority_taskq_s *_starpu_create_priority_taskq(void)
  45. {
  46. struct starpu_priority_taskq_s *central_queue;
  47. central_queue = malloc(sizeof(struct starpu_priority_taskq_s));
  48. central_queue->total_ntasks = 0;
  49. unsigned prio;
  50. for (prio = 0; prio < NPRIO_LEVELS; prio++)
  51. {
  52. starpu_task_list_init(&central_queue->taskq[prio]);
  53. central_queue->ntasks[prio] = 0;
  54. }
  55. return central_queue;
  56. }
  57. static void _starpu_destroy_priority_taskq(struct starpu_priority_taskq_s *priority_queue)
  58. {
  59. free(priority_queue);
  60. }
  61. static void initialize_eager_center_priority_policy(struct starpu_machine_topology_s *topology,
  62. __attribute__ ((unused)) struct starpu_sched_policy_s *_policy)
  63. {
  64. /* In this policy, we support more than two levels of priority. */
  65. starpu_sched_set_min_priority(MIN_LEVEL);
  66. starpu_sched_set_max_priority(MAX_LEVEL);
  67. /* only a single queue (even though there are several internaly) */
  68. taskq = _starpu_create_priority_taskq();
  69. PTHREAD_MUTEX_INIT(&global_sched_mutex, NULL);
  70. PTHREAD_COND_INIT(&global_sched_cond, NULL);
  71. unsigned workerid;
  72. for (workerid = 0; workerid < topology->nworkers; workerid++)
  73. starpu_worker_set_sched_condition(workerid, &global_sched_cond, &global_sched_mutex);
  74. }
  75. static void deinitialize_eager_center_priority_policy(struct starpu_machine_topology_s *topology,
  76. __attribute__ ((unused)) struct starpu_sched_policy_s *_policy)
  77. {
  78. /* TODO check that there is no task left in the queue */
  79. /* deallocate the task queue */
  80. _starpu_destroy_priority_taskq(taskq);
  81. }
  82. static int _starpu_priority_push_task(struct starpu_task *task)
  83. {
  84. /* wake people waiting for a task */
  85. PTHREAD_MUTEX_LOCK(&global_sched_mutex);
  86. STARPU_TRACE_JOB_PUSH(task, 1);
  87. unsigned priolevel = task->priority - STARPU_MIN_PRIO;
  88. starpu_task_list_push_front(&taskq->taskq[priolevel], task);
  89. taskq->ntasks[priolevel]++;
  90. taskq->total_ntasks++;
  91. PTHREAD_COND_SIGNAL(&global_sched_cond);
  92. PTHREAD_MUTEX_UNLOCK(&global_sched_mutex);
  93. return 0;
  94. }
  95. static struct starpu_task *_starpu_priority_pop_task(void)
  96. {
  97. struct starpu_task *task = NULL;
  98. /* block until some event happens */
  99. PTHREAD_MUTEX_LOCK(&global_sched_mutex);
  100. if ((taskq->total_ntasks == 0) && _starpu_machine_is_running())
  101. {
  102. #ifdef STARPU_NON_BLOCKING_DRIVERS
  103. PTHREAD_MUTEX_UNLOCK(&global_sched_mutex);
  104. return NULL;
  105. #else
  106. PTHREAD_COND_WAIT(&global_sched_cond, &global_sched_mutex);
  107. #endif
  108. }
  109. if (taskq->total_ntasks > 0)
  110. {
  111. unsigned priolevel = NPRIO_LEVELS - 1;
  112. do {
  113. if (taskq->ntasks[priolevel] > 0) {
  114. /* there is some task that we can grab */
  115. task = starpu_task_list_pop_back(&taskq->taskq[priolevel]);
  116. taskq->ntasks[priolevel]--;
  117. taskq->total_ntasks--;
  118. STARPU_TRACE_JOB_POP(task, 0);
  119. }
  120. } while (!task && priolevel-- > 0);
  121. }
  122. PTHREAD_MUTEX_UNLOCK(&global_sched_mutex);
  123. return task;
  124. }
  125. struct starpu_sched_policy_s _starpu_sched_prio_policy = {
  126. .init_sched = initialize_eager_center_priority_policy,
  127. .deinit_sched = deinitialize_eager_center_priority_policy,
  128. /* we always use priorities in that policy */
  129. .push_task = _starpu_priority_push_task,
  130. .push_prio_task = _starpu_priority_push_task,
  131. .pop_task = _starpu_priority_pop_task,
  132. .post_exec_hook = NULL,
  133. .pop_every_task = NULL,
  134. .policy_name = "prio",
  135. .policy_description = "eager (with priorities)"
  136. };