random_policy.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 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 <core/sched_ctx.h>
  21. #include <sched_policies/fifo_queues.h>
  22. #ifdef HAVE_AYUDAME_H
  23. #include <Ayudame.h>
  24. #endif
  25. static int _random_push_task(struct starpu_task *task, unsigned prio)
  26. {
  27. /* find the queue */
  28. unsigned selected = 0;
  29. double alpha_sum = 0.0;
  30. unsigned sched_ctx_id = task->sched_ctx;
  31. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  32. int worker;
  33. struct starpu_sched_ctx_iterator it;
  34. if(workers->init_iterator)
  35. workers->init_iterator(workers, &it);
  36. while(workers->has_next(workers, &it))
  37. {
  38. worker = workers->get_next(workers, &it);
  39. enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
  40. alpha_sum += starpu_worker_get_relative_speedup(perf_arch);
  41. }
  42. double random = starpu_drand48()*alpha_sum;
  43. // _STARPU_DEBUG("my rand is %e\n", random);
  44. double alpha = 0.0;
  45. while(workers->has_next(workers, &it))
  46. {
  47. worker = workers->get_next(workers, &it);
  48. enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
  49. double worker_alpha = starpu_worker_get_relative_speedup(perf_arch);
  50. if (alpha + worker_alpha > random && starpu_worker_can_execute_task(worker, task, 0))
  51. {
  52. /* we found the worker */
  53. selected = worker;
  54. break;
  55. }
  56. alpha += worker_alpha;
  57. }
  58. #ifdef HAVE_AYUDAME_H
  59. if (AYU_event)
  60. {
  61. int id = selected;
  62. AYU_event(AYU_ADDTASKTOQUEUE, _starpu_get_job_associated_to_task(task)->job_id, &id);
  63. }
  64. #endif
  65. /* we should now have the best worker in variable "selected" */
  66. return starpu_push_local_task(selected, task, prio);
  67. }
  68. static int random_push_task(struct starpu_task *task)
  69. {
  70. unsigned sched_ctx_id = task->sched_ctx;
  71. _starpu_pthread_mutex_t *changing_ctx_mutex = starpu_sched_ctx_get_changing_ctx_mutex(sched_ctx_id);
  72. unsigned nworkers;
  73. int ret_val = -1;
  74. _STARPU_PTHREAD_MUTEX_LOCK(changing_ctx_mutex);
  75. nworkers = starpu_sched_ctx_get_nworkers(sched_ctx_id);
  76. if(nworkers == 0)
  77. {
  78. _STARPU_PTHREAD_MUTEX_UNLOCK(changing_ctx_mutex);
  79. return ret_val;
  80. }
  81. ret_val = _random_push_task(task, !!task->priority);
  82. _STARPU_PTHREAD_MUTEX_UNLOCK(changing_ctx_mutex);
  83. return ret_val;
  84. }
  85. static void initialize_random_policy(unsigned sched_ctx_id)
  86. {
  87. starpu_sched_ctx_create_worker_collection(sched_ctx_id, STARPU_WORKER_LIST);
  88. starpu_srand48(time(NULL));
  89. }
  90. static void deinitialize_random_policy(unsigned sched_ctx_id)
  91. {
  92. starpu_sched_ctx_delete_worker_collection(sched_ctx_id);
  93. }
  94. struct starpu_sched_policy _starpu_sched_random_policy =
  95. {
  96. .init_sched = initialize_random_policy,
  97. .add_workers = NULL,
  98. .remove_workers = NULL,
  99. .deinit_sched = deinitialize_random_policy,
  100. .push_task = random_push_task,
  101. .pop_task = NULL,
  102. .pre_exec_hook = NULL,
  103. .post_exec_hook = NULL,
  104. .pop_every_task = NULL,
  105. .policy_name = "random",
  106. .policy_description = "weighted random based on worker overall performance"
  107. };