stack_queues.c 3.2 KB

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