stack_queues.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2013 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. /* Stack queues, ready for use by schedulers */
  18. #include <starpu.h>
  19. #include <starpu_scheduler.h>
  20. #include <sched_policies/stack_queues.h>
  21. #include <errno.h>
  22. #include <common/utils.h>
  23. /* keep track of the total number of jobs to be scheduled to avoid infinite
  24. * polling when there are really few jobs in the overall queue */
  25. static unsigned total_number_of_jobs;
  26. void _starpu_init_stack_queues_mechanisms(void)
  27. {
  28. total_number_of_jobs = 0;
  29. }
  30. struct _starpu_stack_jobq *_starpu_create_stack(void)
  31. {
  32. struct _starpu_stack_jobq *stack;
  33. stack = (struct _starpu_stack_jobq *) malloc(sizeof(struct _starpu_stack_jobq));
  34. stack->jobq = _starpu_job_list_new();
  35. stack->njobs = 0;
  36. stack->nprocessed = 0;
  37. stack->exp_start = starpu_timing_now();
  38. stack->exp_len = 0.0;
  39. stack->exp_end = stack->exp_start;
  40. return stack;
  41. }
  42. unsigned _starpu_get_stack_njobs(struct _starpu_stack_jobq *stack_queue)
  43. {
  44. return stack_queue->njobs;
  45. }
  46. unsigned _starpu_get_stack_nprocessed(struct _starpu_stack_jobq *stack_queue)
  47. {
  48. return stack_queue->nprocessed;
  49. }
  50. void _starpu_stack_push_task(struct _starpu_stack_jobq *stack_queue, starpu_pthread_mutex_t *sched_mutex, starpu_pthread_cond_t *sched_cond, struct _starpu_job *task)
  51. {
  52. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  53. total_number_of_jobs++;
  54. if (task->task->priority)
  55. _starpu_job_list_push_back(stack_queue->jobq, task);
  56. else
  57. _starpu_job_list_push_front(stack_queue->jobq, task);
  58. stack_queue->njobs++;
  59. stack_queue->nprocessed++;
  60. STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  61. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  62. }
  63. struct _starpu_job *_starpu_stack_pop_task(struct _starpu_stack_jobq *stack_queue, starpu_pthread_mutex_t *sched_mutex, int workerid __attribute__ ((unused)))
  64. {
  65. struct _starpu_job *j = NULL;
  66. if (stack_queue->njobs == 0)
  67. return NULL;
  68. /* TODO find a task that suits workerid */
  69. if (stack_queue->njobs > 0)
  70. {
  71. /* there is a task */
  72. j = _starpu_job_list_pop_back(stack_queue->jobq);
  73. STARPU_ASSERT(j);
  74. stack_queue->njobs--;
  75. _STARPU_TRACE_JOB_POP(j, 0);
  76. /* we are sure that we got it now, so at worst, some people thought
  77. * there remained some work and will soon discover it is not true */
  78. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  79. total_number_of_jobs--;
  80. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  81. }
  82. return j;
  83. }