stack_queues.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /* Stack queues, ready for use by schedulers */
  17. #include <starpu.h>
  18. #include <sched_policies/stack_queues.h>
  19. #include <errno.h>
  20. #include <common/utils.h>
  21. /* keep track of the total number of jobs to be scheduled to avoid infinite
  22. * polling when there are really few jobs in the overall queue */
  23. static unsigned total_number_of_jobs;
  24. void _starpu_init_stack_queues_mechanisms(void)
  25. {
  26. total_number_of_jobs = 0;
  27. }
  28. struct starpu_stack_jobq_s *_starpu_create_stack(void)
  29. {
  30. struct starpu_stack_jobq_s *stack;
  31. stack = malloc(sizeof(struct starpu_stack_jobq_s));
  32. stack->jobq = starpu_job_list_new();
  33. stack->njobs = 0;
  34. stack->nprocessed = 0;
  35. stack->exp_start = _starpu_timing_now();
  36. stack->exp_len = 0.0;
  37. stack->exp_end = stack->exp_start;
  38. return stack;
  39. }
  40. unsigned get_total_njobs_stacks(void)
  41. {
  42. return total_number_of_jobs;
  43. }
  44. unsigned _starpu_get_stack_njobs(struct starpu_stack_jobq_s *stack_queue)
  45. {
  46. return stack_queue->njobs;
  47. }
  48. unsigned _starpu_get_stack_nprocessed(struct starpu_stack_jobq_s *stack_queue)
  49. {
  50. return stack_queue->nprocessed;
  51. }
  52. void _starpu_stack_push_prio_task(struct starpu_stack_jobq_s *stack_queue, pthread_mutex_t *sched_mutex, pthread_cond_t *sched_cond, starpu_job_t task)
  53. {
  54. PTHREAD_MUTEX_LOCK(sched_mutex);
  55. total_number_of_jobs++;
  56. STARPU_TRACE_JOB_PUSH(task, 0);
  57. starpu_job_list_push_back(stack_queue->jobq, task);
  58. stack_queue->njobs++;
  59. stack_queue->nprocessed++;
  60. PTHREAD_COND_SIGNAL(sched_cond);
  61. PTHREAD_MUTEX_UNLOCK(sched_mutex);
  62. }
  63. void _starpu_stack_push_task(struct starpu_stack_jobq_s *stack_queue, pthread_mutex_t *sched_mutex, pthread_cond_t *sched_cond, starpu_job_t task)
  64. {
  65. PTHREAD_MUTEX_LOCK(sched_mutex);
  66. total_number_of_jobs++;
  67. STARPU_TRACE_JOB_PUSH(task, 0);
  68. starpu_job_list_push_front(stack_queue->jobq, task);
  69. stack_queue->njobs++;
  70. stack_queue->nprocessed++;
  71. PTHREAD_COND_SIGNAL(sched_cond);
  72. PTHREAD_MUTEX_UNLOCK(sched_mutex);
  73. }
  74. starpu_job_t _starpu_stack_pop_task(struct starpu_stack_jobq_s *stack_queue, pthread_mutex_t *sched_mutex)
  75. {
  76. starpu_job_t j = NULL;
  77. if (stack_queue->njobs == 0)
  78. return NULL;
  79. if (stack_queue->njobs > 0)
  80. {
  81. /* there is a task */
  82. j = starpu_job_list_pop_back(stack_queue->jobq);
  83. STARPU_ASSERT(j);
  84. stack_queue->njobs--;
  85. STARPU_TRACE_JOB_POP(j, 0);
  86. /* we are sure that we got it now, so at worst, some people thought
  87. * there remained some work and will soon discover it is not true */
  88. PTHREAD_MUTEX_LOCK(sched_mutex);
  89. total_number_of_jobs--;
  90. PTHREAD_MUTEX_UNLOCK(sched_mutex);
  91. }
  92. return j;
  93. }