component_random.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Inria
  4. * Copyright (C) 2014-2015,2017 CNRS
  5. * Copyright (C) 2014-2017 Université de Bordeaux
  6. * Copyright (C) 2013 Simon Archipoff
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu_sched_component.h>
  20. #include <core/workers.h>
  21. #include <core/sched_policy.h>
  22. #include <core/task.h>
  23. static double compute_relative_speedup(struct starpu_sched_component * component)
  24. {
  25. double sum = 0.0;
  26. int id;
  27. for(id = starpu_bitmap_first(component->workers_in_ctx);
  28. id != -1;
  29. id = starpu_bitmap_next(component->workers_in_ctx, id))
  30. {
  31. struct starpu_perfmodel_arch* perf_arch = starpu_worker_get_perf_archtype(id, component->tree->sched_ctx_id);
  32. sum += starpu_worker_get_relative_speedup(perf_arch);
  33. }
  34. STARPU_ASSERT(sum != 0.0);
  35. return sum;
  36. }
  37. static int random_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  38. {
  39. STARPU_ASSERT(component->nchildren > 0);
  40. /* indexes_components and size are used to memoize component that can execute tasks
  41. * during the first phase of algorithm, it contain the size indexes of the components
  42. * that can execute task.
  43. */
  44. int indexes_components[component->nchildren];
  45. unsigned size=0;
  46. /* speedup[i] is revelant only if i is in the size firsts elements of
  47. * indexes_components
  48. */
  49. double speedup[component->nchildren];
  50. double alpha_sum = 0.0;
  51. unsigned i;
  52. for(i = 0; i < component->nchildren ; i++)
  53. {
  54. if(starpu_sched_component_can_execute_task(component->children[i],task))
  55. {
  56. speedup[size] = compute_relative_speedup(component->children[i]);
  57. alpha_sum += speedup[size];
  58. indexes_components[size] = i;
  59. size++;
  60. }
  61. }
  62. if(size == 0)
  63. return -ENODEV;
  64. /* not fully sure that this code is correct
  65. * because of bad properties of double arithmetic
  66. */
  67. double random = starpu_drand48()*alpha_sum;
  68. double alpha = 0.0;
  69. struct starpu_sched_component * select = NULL;
  70. for(i = 0; i < size ; i++)
  71. {
  72. int index = indexes_components[i];
  73. if(alpha + speedup[i] >= random)
  74. {
  75. select = component->children[index];
  76. break;
  77. }
  78. alpha += speedup[i];
  79. }
  80. STARPU_ASSERT(select != NULL);
  81. if(starpu_sched_component_is_worker(select))
  82. {
  83. select->can_pull(select);
  84. return 1;
  85. }
  86. starpu_sched_task_break(task);
  87. int ret_val = starpu_sched_component_push_task(component,select,task);
  88. return ret_val;
  89. }
  90. int starpu_sched_component_is_random(struct starpu_sched_component *component)
  91. {
  92. return component->push_task == random_push_task;
  93. }
  94. struct starpu_sched_component * starpu_sched_component_random_create(struct starpu_sched_tree *tree, void *arg)
  95. {
  96. (void)arg;
  97. struct starpu_sched_component * component = starpu_sched_component_create(tree, "random");
  98. component->push_task = random_push_task;
  99. return component;
  100. }