eager_central_priority_policy.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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
  31. {
  32. /* the actual lists
  33. * taskq[p] is for priority [p - STARPU_MIN_PRIO] */
  34. struct starpu_task_list taskq[NPRIO_LEVELS];
  35. unsigned ntasks[NPRIO_LEVELS];
  36. unsigned total_ntasks;
  37. };
  38. struct _starpu_eager_central_prio_data
  39. {
  40. struct _starpu_priority_taskq *taskq;
  41. _starpu_pthread_mutex_t policy_mutex;
  42. };
  43. /*
  44. * Centralized queue with priorities
  45. */
  46. static struct _starpu_priority_taskq *_starpu_create_priority_taskq(void)
  47. {
  48. struct _starpu_priority_taskq *central_queue;
  49. central_queue = (struct _starpu_priority_taskq *) malloc(sizeof(struct _starpu_priority_taskq));
  50. central_queue->total_ntasks = 0;
  51. unsigned prio;
  52. for (prio = 0; prio < NPRIO_LEVELS; prio++)
  53. {
  54. starpu_task_list_init(&central_queue->taskq[prio]);
  55. central_queue->ntasks[prio] = 0;
  56. }
  57. return central_queue;
  58. }
  59. static void _starpu_destroy_priority_taskq(struct _starpu_priority_taskq *priority_queue)
  60. {
  61. free(priority_queue);
  62. }
  63. static void initialize_eager_center_priority_policy(unsigned sched_ctx_id)
  64. {
  65. starpu_sched_ctx_create_worker_collection(sched_ctx_id, STARPU_WORKER_LIST);
  66. struct _starpu_eager_central_prio_data *data = (struct _starpu_eager_central_prio_data*)malloc(sizeof(struct _starpu_eager_central_prio_data));
  67. /* In this policy, we support more than two levels of priority. */
  68. starpu_sched_set_min_priority(MIN_LEVEL);
  69. starpu_sched_set_max_priority(MAX_LEVEL);
  70. /* only a single queue (even though there are several internaly) */
  71. data->taskq = _starpu_create_priority_taskq();
  72. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)data);
  73. _STARPU_PTHREAD_MUTEX_INIT(&data->policy_mutex, NULL);
  74. }
  75. static void deinitialize_eager_center_priority_policy(unsigned sched_ctx_id)
  76. {
  77. /* TODO check that there is no task left in the queue */
  78. struct _starpu_eager_central_prio_data *data = (struct _starpu_eager_central_prio_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  79. /* deallocate the task queue */
  80. _starpu_destroy_priority_taskq(data->taskq);
  81. starpu_sched_ctx_delete_worker_collection(sched_ctx_id);
  82. _STARPU_PTHREAD_MUTEX_DESTROY(&data->policy_mutex);
  83. free(data);
  84. }
  85. static int _starpu_priority_push_task(struct starpu_task *task)
  86. {
  87. unsigned sched_ctx_id = task->sched_ctx;
  88. struct _starpu_eager_central_prio_data *data = (struct _starpu_eager_central_prio_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  89. struct _starpu_priority_taskq *taskq = data->taskq;
  90. /* if the context has no workers return */
  91. _starpu_pthread_mutex_t *changing_ctx_mutex = starpu_get_changing_ctx_mutex(sched_ctx_id);
  92. unsigned nworkers;
  93. int ret_val = -1;
  94. _STARPU_PTHREAD_MUTEX_LOCK(changing_ctx_mutex);
  95. nworkers = starpu_sched_ctx_get_nworkers(sched_ctx_id);
  96. if(nworkers == 0)
  97. {
  98. _STARPU_PTHREAD_MUTEX_UNLOCK(changing_ctx_mutex);
  99. return ret_val;
  100. }
  101. /*if there are no tasks block */
  102. /* wake people waiting for a task */
  103. unsigned worker = 0;
  104. struct starpu_sched_ctx_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  105. struct starpu_iterator it;
  106. if(workers->init_iterator)
  107. workers->init_iterator(workers, &it);
  108. while(workers->has_next(workers,&it))
  109. {
  110. worker = workers->get_next(workers, &it);
  111. _starpu_pthread_mutex_t *sched_mutex;
  112. _starpu_pthread_cond_t *sched_cond;
  113. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  114. _STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  115. }
  116. unsigned priolevel = task->priority - STARPU_MIN_PRIO;
  117. starpu_task_list_push_back(&taskq->taskq[priolevel], task);
  118. taskq->ntasks[priolevel]++;
  119. taskq->total_ntasks++;
  120. _starpu_push_task_end(task);
  121. while(workers->has_next(workers, &it))
  122. {
  123. worker = workers->get_next(workers, &it);
  124. _starpu_pthread_mutex_t *sched_mutex;
  125. _starpu_pthread_cond_t *sched_cond;
  126. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  127. _STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  128. _STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  129. }
  130. _STARPU_PTHREAD_MUTEX_UNLOCK(changing_ctx_mutex);
  131. return 0;
  132. }
  133. static struct starpu_task *_starpu_priority_pop_task(unsigned sched_ctx_id)
  134. {
  135. struct starpu_task *chosen_task = NULL, *task;
  136. unsigned workerid = starpu_worker_get_id();
  137. int skipped = 0;
  138. struct _starpu_eager_central_prio_data *data = (struct _starpu_eager_central_prio_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  139. struct _starpu_priority_taskq *taskq = data->taskq;
  140. /* block until some event happens */
  141. if (taskq->total_ntasks == 0)
  142. return NULL;
  143. _STARPU_PTHREAD_MUTEX_LOCK(&data->policy_mutex);
  144. unsigned priolevel = NPRIO_LEVELS - 1;
  145. do
  146. {
  147. if (taskq->ntasks[priolevel] > 0)
  148. {
  149. for (task = starpu_task_list_begin(&taskq->taskq[priolevel]);
  150. task != starpu_task_list_end(&taskq->taskq[priolevel]);
  151. task = starpu_task_list_next(task)) {
  152. unsigned nimpl;
  153. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  154. {
  155. if (starpu_worker_can_execute_task(workerid, task, nimpl))
  156. {
  157. /* there is some task that we can grab */
  158. _starpu_get_job_associated_to_task(task)->nimpl = nimpl;
  159. starpu_task_list_erase(&taskq->taskq[priolevel], task);
  160. chosen_task = task;
  161. taskq->ntasks[priolevel]--;
  162. taskq->total_ntasks--;
  163. _STARPU_TRACE_JOB_POP(task, 0);
  164. } else skipped = 1;
  165. }
  166. }
  167. }
  168. }
  169. while (!chosen_task && priolevel-- > 0);
  170. _STARPU_PTHREAD_MUTEX_UNLOCK(&data->policy_mutex);
  171. if (!chosen_task && skipped)
  172. {
  173. /* Notify another worker to do that task */
  174. unsigned worker = 0;
  175. struct starpu_sched_ctx_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  176. struct starpu_iterator it;
  177. if(workers->init_iterator)
  178. workers->init_iterator(workers, &it);
  179. while(workers->has_next(workers, &it))
  180. {
  181. worker = workers->get_next(workers, &it);
  182. if(worker != workerid)
  183. {
  184. _starpu_pthread_mutex_t *sched_mutex;
  185. _starpu_pthread_cond_t *sched_cond;
  186. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  187. _STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  188. _STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  189. _STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  190. }
  191. }
  192. }
  193. return chosen_task;
  194. }
  195. struct starpu_sched_policy _starpu_sched_prio_policy =
  196. {
  197. .init_sched = initialize_eager_center_priority_policy,
  198. .deinit_sched = deinitialize_eager_center_priority_policy,
  199. /* we always use priorities in that policy */
  200. .push_task = _starpu_priority_push_task,
  201. .pop_task = _starpu_priority_pop_task,
  202. .pre_exec_hook = NULL,
  203. .post_exec_hook = NULL,
  204. .pop_every_task = NULL,
  205. .policy_name = "prio",
  206. .policy_description = "eager (with priorities)"
  207. };