eager_central_priority_policy.c 4.9 KB

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