stack_queues.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. *
  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_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. 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. PTHREAD_COND_SIGNAL(sched_cond);
  61. PTHREAD_MUTEX_UNLOCK(sched_mutex);
  62. }
  63. starpu_job_t _starpu_stack_pop_task(struct starpu_stack_jobq_s *stack_queue, pthread_mutex_t *sched_mutex, int workerid __attribute__ ((unused)))
  64. {
  65. starpu_job_t 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. PTHREAD_MUTEX_LOCK(sched_mutex);
  79. total_number_of_jobs--;
  80. PTHREAD_MUTEX_UNLOCK(sched_mutex);
  81. }
  82. return j;
  83. }