random_policy.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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. /* Policy attributing tasks randomly to workers */
  18. #include <starpu_rand.h>
  19. #include <core/workers.h>
  20. #include <sched_policies/fifo_queues.h>
  21. static unsigned nworkers;
  22. static pthread_cond_t sched_cond[STARPU_NMAXWORKERS];
  23. static pthread_mutex_t sched_mutex[STARPU_NMAXWORKERS];
  24. static int _random_push_task(struct starpu_task *task, unsigned prio)
  25. {
  26. /* find the queue */
  27. unsigned worker;
  28. unsigned selected = 0;
  29. double alpha_sum = 0.0;
  30. for (worker = 0; worker < nworkers; worker++)
  31. {
  32. enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
  33. alpha_sum += starpu_worker_get_relative_speedup(perf_arch);
  34. }
  35. double random = starpu_drand48()*alpha_sum;
  36. // _STARPU_DEBUG("my rand is %e\n", random);
  37. double alpha = 0.0;
  38. for (worker = 0; worker < nworkers; worker++)
  39. {
  40. enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
  41. double worker_alpha = starpu_worker_get_relative_speedup(perf_arch);
  42. if (alpha + worker_alpha > random && starpu_worker_can_execute_task(worker, task, 0))
  43. {
  44. /* we found the worker */
  45. selected = worker;
  46. break;
  47. }
  48. alpha += worker_alpha;
  49. }
  50. /* we should now have the best worker in variable "selected" */
  51. return starpu_push_local_task(selected, task, prio);
  52. }
  53. static int random_push_task(struct starpu_task *task)
  54. {
  55. return _random_push_task(task, !!task->priority);
  56. }
  57. static void initialize_random_policy(struct starpu_machine_topology *topology,
  58. __attribute__ ((unused)) struct starpu_sched_policy *_policy)
  59. {
  60. starpu_srand48(time(NULL));
  61. nworkers = topology->nworkers;
  62. unsigned workerid;
  63. for (workerid = 0; workerid < nworkers; workerid++)
  64. {
  65. _STARPU_PTHREAD_MUTEX_INIT(&sched_mutex[workerid], NULL);
  66. _STARPU_PTHREAD_COND_INIT(&sched_cond[workerid], NULL);
  67. starpu_worker_set_sched_condition(workerid, &sched_cond[workerid], &sched_mutex[workerid]);
  68. }
  69. }
  70. struct starpu_sched_policy _starpu_sched_random_policy =
  71. {
  72. .init_sched = initialize_random_policy,
  73. .deinit_sched = NULL,
  74. .push_task = random_push_task,
  75. .pop_task = NULL,
  76. .pre_exec_hook = NULL,
  77. .post_exec_hook = NULL,
  78. .pop_every_task = NULL,
  79. .policy_name = "random",
  80. .policy_description = "weighted random"
  81. };