component_random.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 INRIA
  4. * Copyright (C) 2013 Simon Archipoff
  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. #include <starpu_sched_component.h>
  18. #include <core/workers.h>
  19. #include <core/sched_policy.h>
  20. #include <core/task.h>
  21. static double compute_relative_speedup(struct starpu_sched_component * component)
  22. {
  23. double sum = 0.0;
  24. int id;
  25. for(id = starpu_bitmap_first(component->workers_in_ctx);
  26. id != -1;
  27. id = starpu_bitmap_next(component->workers_in_ctx, id))
  28. {
  29. struct starpu_perfmodel_arch* perf_arch = starpu_worker_get_perf_archtype(id, component->tree->sched_ctx_id);
  30. sum += starpu_worker_get_relative_speedup(perf_arch);
  31. }
  32. STARPU_ASSERT(sum != 0.0);
  33. return sum;
  34. }
  35. static int random_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  36. {
  37. STARPU_ASSERT(component->nchildren > 0);
  38. /* indexes_components and size are used to memoize component that can execute tasks
  39. * during the first phase of algorithm, it contain the size indexes of the components
  40. * that can execute task.
  41. */
  42. int indexes_components[component->nchildren];
  43. unsigned size=0;
  44. /* speedup[i] is revelant only if i is in the size firsts elements of
  45. * indexes_components
  46. */
  47. double speedup[component->nchildren];
  48. double alpha_sum = 0.0;
  49. unsigned i;
  50. for(i = 0; i < component->nchildren ; i++)
  51. {
  52. if(starpu_sched_component_can_execute_task(component->children[i],task))
  53. {
  54. speedup[size] = compute_relative_speedup(component->children[i]);
  55. alpha_sum += speedup[size];
  56. indexes_components[size] = i;
  57. size++;
  58. }
  59. }
  60. if(size == 0)
  61. return -ENODEV;
  62. /* not fully sure that this code is correct
  63. * because of bad properties of double arithmetic
  64. */
  65. double random = starpu_drand48()*alpha_sum;
  66. double alpha = 0.0;
  67. struct starpu_sched_component * select = NULL;
  68. for(i = 0; i < size ; i++)
  69. {
  70. int index = indexes_components[i];
  71. if(alpha + speedup[i] >= random)
  72. {
  73. select = component->children[index];
  74. break;
  75. }
  76. alpha += speedup[i];
  77. }
  78. STARPU_ASSERT(select != NULL);
  79. if(starpu_sched_component_is_worker(select))
  80. {
  81. select->can_pull(select);
  82. return 1;
  83. }
  84. _STARPU_TASK_BREAK_ON(task, sched);
  85. int ret_val = starpu_sched_component_push_task(component,select,task);
  86. return ret_val;
  87. }
  88. int starpu_sched_component_is_random(struct starpu_sched_component *component)
  89. {
  90. return component->push_task == random_push_task;
  91. }
  92. struct starpu_sched_component * starpu_sched_component_random_create(struct starpu_sched_tree *tree, void * arg STARPU_ATTRIBUTE_UNUSED)
  93. {
  94. struct starpu_sched_component * component = starpu_sched_component_create(tree, "random");
  95. component->push_task = random_push_task;
  96. return component;
  97. }