random_policy.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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. #include <core/policies/random_policy.h>
  17. /* XXX 32 is set randomly */
  18. static unsigned nworkers;
  19. static struct starpu_jobq_s *queue_array[32];
  20. static starpu_job_t random_pop_task(struct starpu_jobq_s *q)
  21. {
  22. struct starpu_job_s *j;
  23. j = _starpu_fifo_pop_task(q);
  24. return j;
  25. }
  26. static int _random_push_task(struct starpu_jobq_s *q __attribute__ ((unused)), starpu_job_t task, unsigned prio)
  27. {
  28. /* find the queue */
  29. struct starpu_fifo_jobq_s *fifo;
  30. unsigned worker;
  31. unsigned selected = 0;
  32. double alpha_sum = 0.0;
  33. for (worker = 0; worker < nworkers; worker++)
  34. {
  35. alpha_sum += queue_array[worker]->alpha;
  36. }
  37. double random = starpu_drand48()*alpha_sum;
  38. // fprintf(stderr, "my rand is %e\n", random);
  39. double alpha = 0.0;
  40. for (worker = 0; worker < nworkers; worker++)
  41. {
  42. if (alpha + queue_array[worker]->alpha > random) {
  43. /* we found the worker */
  44. selected = worker;
  45. break;
  46. }
  47. alpha += queue_array[worker]->alpha;
  48. }
  49. /* we should now have the best worker in variable "best" */
  50. fifo = queue_array[selected]->queue;
  51. if (prio) {
  52. return _starpu_fifo_push_prio_task(queue_array[selected], task);
  53. } else {
  54. return _starpu_fifo_push_task(queue_array[selected], task);
  55. }
  56. }
  57. static int random_push_prio_task(struct starpu_jobq_s *q, starpu_job_t task)
  58. {
  59. return _random_push_task(q, task, 1);
  60. }
  61. static int random_push_task(struct starpu_jobq_s *q, starpu_job_t task)
  62. {
  63. return _random_push_task(q, task, 0);
  64. }
  65. static struct starpu_jobq_s *init_random_fifo(void)
  66. {
  67. struct starpu_jobq_s *q;
  68. q = _starpu_create_fifo();
  69. q->push_task = random_push_task;
  70. q->push_prio_task = random_push_prio_task;
  71. q->pop_task = random_pop_task;
  72. q->who = 0;
  73. queue_array[nworkers++] = q;
  74. return q;
  75. }
  76. static void initialize_random_policy(struct starpu_machine_config_s *config,
  77. __attribute__ ((unused)) struct starpu_sched_policy_s *_policy)
  78. {
  79. nworkers = 0;
  80. starpu_srand48(time(NULL));
  81. _starpu_setup_queues(_starpu_init_fifo_queues_mechanisms, init_random_fifo, config);
  82. }
  83. static struct starpu_jobq_s *get_local_queue_random(struct starpu_sched_policy_s *policy __attribute__ ((unused)))
  84. {
  85. struct starpu_jobq_s *queue;
  86. queue = pthread_getspecific(policy->local_queue_key);
  87. if (!queue)
  88. {
  89. /* take one randomly as this *must* be for a push anyway XXX */
  90. queue = queue_array[0];
  91. }
  92. return queue;
  93. }
  94. struct starpu_sched_policy_s _starpu_sched_random_policy = {
  95. .init_sched = initialize_random_policy,
  96. .deinit_sched = NULL,
  97. .starpu_get_local_queue = get_local_queue_random,
  98. .policy_name = "random",
  99. .policy_description = "weighted random"
  100. };