eager_central_priority_policy.c 6.6 KB

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