random_policy.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2016 CNRS
  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. double alpha_sum = 0.0;
  29. unsigned sched_ctx_id = task->sched_ctx;
  30. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  31. int worker;
  32. int worker_arr[STARPU_NMAXWORKERS];
  33. double speedup_arr[STARPU_NMAXWORKERS];
  34. int size = 0;
  35. struct starpu_sched_ctx_iterator it;
  36. workers->init_iterator(workers, &it);
  37. while(workers->has_next(workers, &it))
  38. {
  39. worker = workers->get_next(workers, &it);
  40. unsigned impl;
  41. if(starpu_worker_can_execute_task_first_impl(worker, task, &impl))
  42. {
  43. struct starpu_perfmodel_arch* perf_arch = starpu_worker_get_perf_archtype(worker, sched_ctx_id);
  44. double speedup = starpu_worker_get_relative_speedup(perf_arch);
  45. alpha_sum += speedup;
  46. speedup_arr[size] = speedup;
  47. worker_arr[size++] = worker;
  48. }
  49. }
  50. double random = starpu_drand48()*alpha_sum;
  51. //printf("my rand is %e over %e\n", random, alpha_sum);
  52. if(size == 0)
  53. return -ENODEV;
  54. unsigned selected = worker_arr[size - 1];
  55. double alpha = 0.0;
  56. int i;
  57. for(i = 0; i < size; i++)
  58. {
  59. worker = worker_arr[i];
  60. double worker_alpha = speedup_arr[i];
  61. if (alpha + worker_alpha >= random)
  62. {
  63. /* we found the worker */
  64. selected = worker;
  65. break;
  66. }
  67. alpha += worker_alpha;
  68. }
  69. #ifdef HAVE_AYUDAME_H
  70. if (AYU_event)
  71. {
  72. intptr_t id = selected;
  73. AYU_event(AYU_ADDTASKTOQUEUE, _starpu_get_job_associated_to_task(task)->job_id, &id);
  74. }
  75. #endif
  76. return starpu_push_local_task(selected, task, prio);
  77. }
  78. static int random_push_task(struct starpu_task *task)
  79. {
  80. return _random_push_task(task, !!task->priority);
  81. }
  82. static void initialize_random_policy(unsigned sched_ctx_id)
  83. {
  84. (void) sched_ctx_id;
  85. starpu_srand48(time(NULL));
  86. }
  87. static void deinitialize_random_policy(unsigned sched_ctx_id)
  88. {
  89. (void) sched_ctx_id;
  90. }
  91. struct starpu_sched_policy _starpu_sched_random_policy =
  92. {
  93. .init_sched = initialize_random_policy,
  94. .add_workers = NULL,
  95. .remove_workers = NULL,
  96. .deinit_sched = deinitialize_random_policy,
  97. .push_task = random_push_task,
  98. .pop_task = NULL,
  99. .pre_exec_hook = NULL,
  100. .post_exec_hook = NULL,
  101. .pop_every_task = NULL,
  102. .policy_name = "random",
  103. .policy_description = "weighted random based on worker overall performance",
  104. .worker_type = STARPU_WORKER_LIST,
  105. };