stack_queues.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 *_starpu_create_stack(void)
  30. {
  31. struct _starpu_stack_jobq *stack;
  32. stack = (struct _starpu_stack_jobq *) malloc(sizeof(struct _starpu_stack_jobq));
  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 *stack_queue)
  42. {
  43. return stack_queue->njobs;
  44. }
  45. unsigned _starpu_get_stack_nprocessed(struct _starpu_stack_jobq *stack_queue)
  46. {
  47. return stack_queue->nprocessed;
  48. }
  49. 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)
  50. {
  51. _STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  52. total_number_of_jobs++;
  53. if (task->task->priority)
  54. _starpu_job_list_push_back(stack_queue->jobq, task);
  55. else
  56. _starpu_job_list_push_front(stack_queue->jobq, task);
  57. stack_queue->njobs++;
  58. stack_queue->nprocessed++;
  59. _STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  60. _STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  61. }
  62. struct _starpu_job *_starpu_stack_pop_task(struct _starpu_stack_jobq *stack_queue, _starpu_pthread_mutex_t *sched_mutex, int workerid __attribute__ ((unused)))
  63. {
  64. struct _starpu_job *j = NULL;
  65. if (stack_queue->njobs == 0)
  66. return NULL;
  67. /* TODO find a task that suits workerid */
  68. if (stack_queue->njobs > 0)
  69. {
  70. /* there is a task */
  71. j = _starpu_job_list_pop_back(stack_queue->jobq);
  72. STARPU_ASSERT(j);
  73. stack_queue->njobs--;
  74. _STARPU_TRACE_JOB_POP(j, 0);
  75. /* we are sure that we got it now, so at worst, some people thought
  76. * there remained some work and will soon discover it is not true */
  77. _STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  78. total_number_of_jobs--;
  79. _STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  80. }
  81. return j;
  82. }