eager_central_priority_policy.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /*
  37. * Centralized queue with priorities
  38. */
  39. static struct starpu_priority_taskq_s *_starpu_create_priority_taskq(void)
  40. {
  41. struct starpu_priority_taskq_s *central_queue;
  42. central_queue = (struct starpu_priority_taskq_s *) malloc(sizeof(struct starpu_priority_taskq_s));
  43. central_queue->total_ntasks = 0;
  44. unsigned prio;
  45. for (prio = 0; prio < NPRIO_LEVELS; prio++)
  46. {
  47. starpu_task_list_init(&central_queue->taskq[prio]);
  48. central_queue->ntasks[prio] = 0;
  49. }
  50. return central_queue;
  51. }
  52. static void _starpu_destroy_priority_taskq(struct starpu_priority_taskq_s *priority_queue)
  53. {
  54. free(priority_queue);
  55. }
  56. static void initialize_eager_center_priority_policy_for_workers(unsigned sched_ctx_id, unsigned nnew_workers)
  57. {
  58. struct starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx(sched_ctx_id);
  59. unsigned nworkers_ctx = sched_ctx->nworkers_in_ctx;
  60. struct starpu_machine_config_s *config = (struct starpu_machine_config_s *)_starpu_get_machine_config();
  61. unsigned ntotal_workers = config->topology.nworkers;
  62. unsigned all_workers = nnew_workers == ntotal_workers ? ntotal_workers : nworkers_ctx + nnew_workers;
  63. unsigned workerid_ctx;
  64. for (workerid_ctx = nworkers_ctx; workerid_ctx < all_workers; workerid_ctx++)
  65. {
  66. sched_ctx->sched_mutex[workerid_ctx] = sched_ctx->sched_mutex[0];
  67. sched_ctx->sched_cond[workerid_ctx] = sched_ctx->sched_cond[0];
  68. }
  69. /* take into account the new number of threads at the next push */
  70. PTHREAD_MUTEX_LOCK(&sched_ctx->changing_ctx_mutex);
  71. sched_ctx->temp_nworkers_in_ctx = all_workers;
  72. PTHREAD_MUTEX_UNLOCK(&sched_ctx->changing_ctx_mutex);
  73. }
  74. static void initialize_eager_center_priority_policy(unsigned sched_ctx_id)
  75. {
  76. struct starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx(sched_ctx_id);
  77. /* In this policy, we support more than two levels of priority. */
  78. starpu_sched_set_min_priority(MIN_LEVEL);
  79. starpu_sched_set_max_priority(MAX_LEVEL);
  80. /* only a single queue (even though there are several internaly) */
  81. struct starpu_priority_taskq_s *taskq = _starpu_create_priority_taskq();
  82. sched_ctx->policy_data = (void*) taskq;
  83. pthread_cond_t *global_sched_cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t));
  84. pthread_mutex_t *global_sched_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
  85. PTHREAD_MUTEX_INIT(global_sched_mutex, NULL);
  86. PTHREAD_COND_INIT(global_sched_cond, NULL);
  87. int nworkers = sched_ctx->nworkers_in_ctx;
  88. int workerid_ctx;
  89. for (workerid_ctx = 0; workerid_ctx < nworkers; workerid_ctx++)
  90. {
  91. sched_ctx->sched_mutex[workerid_ctx] = global_sched_mutex;
  92. sched_ctx->sched_cond[workerid_ctx] = global_sched_cond;
  93. }
  94. }
  95. static void deinitialize_eager_center_priority_policy(unsigned sched_ctx_id)
  96. {
  97. /* TODO check that there is no task left in the queue */
  98. struct starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx(sched_ctx_id);
  99. struct starpu_priority_taskq_s *taskq = (struct starpu_priority_taskq_s*)sched_ctx->policy_data;
  100. /* deallocate the task queue */
  101. _starpu_destroy_priority_taskq(taskq);
  102. PTHREAD_MUTEX_DESTROY(sched_ctx->sched_mutex[0]);
  103. PTHREAD_COND_DESTROY(sched_ctx->sched_cond[0]);
  104. free(sched_ctx->sched_mutex[0]);
  105. free(sched_ctx->sched_cond[0]);
  106. free(taskq);
  107. }
  108. static int _starpu_priority_push_task(struct starpu_task *task, unsigned sched_ctx_id)
  109. {
  110. struct starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx(sched_ctx_id);
  111. struct starpu_priority_taskq_s *taskq = (struct starpu_priority_taskq_s*)sched_ctx->policy_data;
  112. /* wake people waiting for a task */
  113. PTHREAD_MUTEX_LOCK(sched_ctx->sched_mutex[0]);
  114. STARPU_TRACE_JOB_PUSH(task, 1);
  115. unsigned priolevel = task->priority - STARPU_MIN_PRIO;
  116. starpu_task_list_push_front(&taskq->taskq[priolevel], task);
  117. taskq->ntasks[priolevel]++;
  118. taskq->total_ntasks++;
  119. PTHREAD_COND_SIGNAL(sched_ctx->sched_cond[0]);
  120. PTHREAD_MUTEX_UNLOCK(sched_ctx->sched_mutex[0]);
  121. return 0;
  122. }
  123. static struct starpu_task *_starpu_priority_pop_task(unsigned sched_ctx_id)
  124. {
  125. struct starpu_task *task = NULL;
  126. struct starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx(sched_ctx_id);
  127. struct starpu_priority_taskq_s *taskq = (struct starpu_priority_taskq_s*)sched_ctx->policy_data;
  128. /* block until some event happens */
  129. PTHREAD_MUTEX_LOCK(sched_ctx->sched_mutex[0]);
  130. if ((taskq->total_ntasks == 0) && _starpu_machine_is_running())
  131. {
  132. #ifdef STARPU_NON_BLOCKING_DRIVERS
  133. PTHREAD_MUTEX_UNLOCK(sched_ctx->sched_mutex[0]);
  134. return NULL;
  135. #else
  136. PTHREAD_COND_WAIT(sched_ctx->sched_cond[0], sched_ctx->sched_mutex[0]);
  137. #endif
  138. }
  139. if (taskq->total_ntasks > 0)
  140. {
  141. unsigned priolevel = NPRIO_LEVELS - 1;
  142. do {
  143. if (taskq->ntasks[priolevel] > 0) {
  144. /* there is some task that we can grab */
  145. task = starpu_task_list_pop_back(&taskq->taskq[priolevel]);
  146. taskq->ntasks[priolevel]--;
  147. taskq->total_ntasks--;
  148. STARPU_TRACE_JOB_POP(task, 0);
  149. }
  150. } while (!task && priolevel-- > 0);
  151. }
  152. PTHREAD_MUTEX_UNLOCK(sched_ctx->sched_mutex[0]);
  153. return task;
  154. }
  155. struct starpu_sched_policy_s _starpu_sched_prio_policy = {
  156. .init_sched = initialize_eager_center_priority_policy,
  157. .init_sched_for_workers = initialize_eager_center_priority_policy_for_workers,
  158. .deinit_sched = deinitialize_eager_center_priority_policy,
  159. /* we always use priorities in that policy */
  160. .push_task = _starpu_priority_push_task,
  161. .pop_task = _starpu_priority_pop_task,
  162. .post_exec_hook = NULL,
  163. .pop_every_task = NULL,
  164. .policy_name = "prio",
  165. .policy_description = "eager (with priorities)"
  166. };