random_policy.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <core/sched_ctx.h>
  21. #include <sched_policies/fifo_queues.h>
  22. static int _random_push_task(struct starpu_task *task, unsigned prio)
  23. {
  24. /* find the queue */
  25. unsigned selected = 0;
  26. double alpha_sum = 0.0;
  27. unsigned sched_ctx_id = task->sched_ctx;
  28. struct worker_collection *workers = starpu_get_worker_collection_of_sched_ctx(sched_ctx_id);
  29. int worker;
  30. if(workers->init_cursor)
  31. workers->init_cursor(workers);
  32. while(workers->has_next(workers))
  33. {
  34. worker = workers->get_next(workers);
  35. enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
  36. alpha_sum += starpu_worker_get_relative_speedup(perf_arch);
  37. }
  38. double random = starpu_drand48()*alpha_sum;
  39. // _STARPU_DEBUG("my rand is %e\n", random);
  40. double alpha = 0.0;
  41. while(workers->has_next(workers))
  42. {
  43. worker = workers->get_next(workers);
  44. enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
  45. double worker_alpha = starpu_worker_get_relative_speedup(perf_arch);
  46. if (alpha + worker_alpha > random && starpu_worker_can_execute_task(worker, task, 0))
  47. {
  48. /* we found the worker */
  49. selected = worker;
  50. break;
  51. }
  52. alpha += worker_alpha;
  53. }
  54. if(workers->init_cursor)
  55. workers->deinit_cursor(workers);
  56. /* we should now have the best worker in variable "selected" */
  57. int n = starpu_push_local_task(selected, task, prio);
  58. return n;
  59. }
  60. static int random_push_task(struct starpu_task *task)
  61. {
  62. unsigned sched_ctx_id = task->sched_ctx;
  63. pthread_mutex_t *changing_ctx_mutex = starpu_get_changing_ctx_mutex(sched_ctx_id);
  64. unsigned nworkers;
  65. int ret_val = -1;
  66. _STARPU_PTHREAD_MUTEX_LOCK(changing_ctx_mutex);
  67. nworkers = starpu_get_nworkers_of_sched_ctx(sched_ctx_id);
  68. if(nworkers == 0)
  69. {
  70. _STARPU_PTHREAD_MUTEX_UNLOCK(changing_ctx_mutex);
  71. return ret_val;
  72. }
  73. ret_val = _random_push_task(task, !!task->priority);
  74. _STARPU_PTHREAD_MUTEX_UNLOCK(changing_ctx_mutex);
  75. return ret_val;
  76. }
  77. static void random_add_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  78. {
  79. unsigned i;
  80. int workerid;
  81. for (i = 0; i < nworkers; i++)
  82. {
  83. workerid = workerids[i];
  84. struct _starpu_worker *workerarg = _starpu_get_worker_struct(workerid);
  85. starpu_worker_set_sched_condition(sched_ctx_id, workerid, &workerarg->sched_mutex, &workerarg->sched_cond);
  86. }
  87. }
  88. static void random_remove_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  89. {
  90. unsigned i;
  91. int workerid;
  92. for (i = 0; i < nworkers; i++)
  93. {
  94. workerid = workerids[i];
  95. starpu_worker_set_sched_condition(sched_ctx_id, workerid, NULL, NULL);
  96. }
  97. }
  98. static void initialize_random_policy(unsigned sched_ctx_id)
  99. {
  100. starpu_create_worker_collection_for_sched_ctx(sched_ctx_id, WORKER_LIST);
  101. starpu_srand48(time(NULL));
  102. }
  103. static void deinitialize_random_policy(unsigned sched_ctx_id)
  104. {
  105. starpu_delete_worker_collection_for_sched_ctx(sched_ctx_id);
  106. }
  107. struct starpu_sched_policy _starpu_sched_random_policy =
  108. {
  109. .init_sched = initialize_random_policy,
  110. .add_workers = random_add_workers,
  111. .remove_workers = random_remove_workers,
  112. .deinit_sched = deinitialize_random_policy,
  113. .push_task = random_push_task,
  114. .pop_task = NULL,
  115. .pre_exec_hook = NULL,
  116. .post_exec_hook = NULL,
  117. .pop_every_task = NULL,
  118. .policy_name = "random",
  119. .policy_description = "weighted random"
  120. };