component_heft.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013,2017 Inria
  4. * Copyright (C) 2014-2017 CNRS
  5. * Copyright (C) 2013-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. /* HEFT variant which tries to schedule a given number of tasks instead of just
  20. * the first of its scheduling window, and actually schedule the task for which
  21. * the most benefit is achieved. */
  22. #include <starpu_sched_component.h>
  23. #include "prio_deque.h"
  24. #include "sched_component.h"
  25. #include <starpu_perfmodel.h>
  26. #include "helper_mct.h"
  27. #include <float.h>
  28. #include <core/sched_policy.h>
  29. #include <core/task.h>
  30. #define NTASKS 5
  31. struct _starpu_heft_data
  32. {
  33. struct _starpu_prio_deque prio;
  34. starpu_pthread_mutex_t mutex;
  35. struct _starpu_mct_data *mct_data;
  36. };
  37. static int heft_progress_one(struct starpu_sched_component *component)
  38. {
  39. struct _starpu_heft_data * data = component->data;
  40. starpu_pthread_mutex_t * mutex = &data->mutex;
  41. struct _starpu_prio_deque * prio = &data->prio;
  42. struct starpu_task * (tasks[NTASKS]);
  43. unsigned ntasks = 0;
  44. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  45. tasks[0] = _starpu_prio_deque_pop_task(prio);
  46. if (tasks[0])
  47. {
  48. int priority = tasks[0]->priority;
  49. /* Try to look at NTASKS from the queue */
  50. for (ntasks = 1; ntasks < NTASKS; ntasks++)
  51. {
  52. tasks[ntasks] = _starpu_prio_deque_highest_task(prio);
  53. if (!tasks[ntasks] || tasks[ntasks]->priority < priority)
  54. break;
  55. _starpu_prio_deque_pop_task(prio);
  56. }
  57. }
  58. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  59. if (!ntasks)
  60. {
  61. return 1;
  62. }
  63. {
  64. struct _starpu_mct_data * d = data->mct_data;
  65. struct starpu_sched_component * best_component = NULL;
  66. unsigned n, i;
  67. /* Estimated task duration for each child */
  68. double estimated_lengths[component->nchildren * ntasks];
  69. /* Estimated transfer duration for each child */
  70. double estimated_transfer_length[component->nchildren * ntasks];
  71. /* Estimated transfer+task termination for each child */
  72. double estimated_ends_with_task[component->nchildren * ntasks];
  73. /* Minimum transfer+task termination on all children */
  74. double min_exp_end_with_task[ntasks];
  75. /* Maximum transfer+task termination on all children */
  76. double max_exp_end_with_task[ntasks];
  77. unsigned suitable_components[component->nchildren * ntasks];
  78. unsigned nsuitable_components[ntasks];
  79. /* Estimate durations */
  80. for (n = 0; n < ntasks; n++)
  81. {
  82. unsigned offset = component->nchildren * n;
  83. min_exp_end_with_task[n] = DBL_MAX;
  84. max_exp_end_with_task[n] = 0.0;
  85. nsuitable_components[n] = starpu_mct_compute_execution_times(component, tasks[n],
  86. estimated_lengths + offset,
  87. estimated_transfer_length + offset,
  88. suitable_components + offset);
  89. starpu_mct_compute_expected_times(component, tasks[n],
  90. estimated_lengths + offset,
  91. estimated_transfer_length + offset,
  92. estimated_ends_with_task + offset,
  93. &min_exp_end_with_task[n], &max_exp_end_with_task[n],
  94. suitable_components + offset, nsuitable_components[n]);
  95. }
  96. int best_task = 0;
  97. double max_benefit = 0;
  98. /* Find the task which provides the most computation time benefit */
  99. for (n = 0; n < ntasks; n++)
  100. {
  101. double benefit = max_exp_end_with_task[n] - min_exp_end_with_task[n];
  102. if (max_benefit < benefit)
  103. {
  104. max_benefit = benefit;
  105. best_task = n;
  106. }
  107. }
  108. double best_fitness = DBL_MAX;
  109. int best_icomponent = -1;
  110. /* Push back the other tasks */
  111. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  112. for (n = ntasks - 1; n < ntasks; n--)
  113. if ((int) n != best_task)
  114. _starpu_prio_deque_push_back_task(prio, tasks[n]);
  115. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  116. /* And now find out which worker suits best for this task,
  117. * including data transfer */
  118. for(i = 0; i < nsuitable_components[best_task]; i++)
  119. {
  120. unsigned offset = component->nchildren * best_task;
  121. unsigned icomponent = suitable_components[offset + i];
  122. #ifdef STARPU_DEVEL
  123. #warning FIXME: take energy consumption into account
  124. #endif
  125. double tmp = starpu_mct_compute_fitness(d,
  126. estimated_ends_with_task[offset + icomponent] - estimated_transfer_length[offset + icomponent],
  127. min_exp_end_with_task[best_task],
  128. max_exp_end_with_task[best_task],
  129. estimated_transfer_length[offset + icomponent],
  130. 0.0);
  131. if(tmp < best_fitness)
  132. {
  133. best_fitness = tmp;
  134. best_icomponent = icomponent;
  135. }
  136. }
  137. STARPU_ASSERT(best_icomponent != -1);
  138. STARPU_ASSERT(best_task >= 0);
  139. best_component = component->children[best_icomponent];
  140. if(starpu_sched_component_is_worker(best_component))
  141. {
  142. best_component->can_pull(best_component);
  143. return 1;
  144. }
  145. _STARPU_TASK_BREAK_ON(tasks[best_task], sched);
  146. int ret = starpu_sched_component_push_task(component, best_component, tasks[best_task]);
  147. if (ret)
  148. {
  149. /* Could not push to child actually, push that one back too */
  150. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  151. _starpu_prio_deque_push_back_task(prio, tasks[best_task]);
  152. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  153. return 1;
  154. }
  155. else
  156. return 0;
  157. }
  158. }
  159. /* Try to push some tasks below */
  160. static void heft_progress(struct starpu_sched_component *component)
  161. {
  162. STARPU_ASSERT(component && starpu_sched_component_is_heft(component));
  163. while (!heft_progress_one(component))
  164. ;
  165. }
  166. static int heft_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  167. {
  168. STARPU_ASSERT(component && task && starpu_sched_component_is_heft(component));
  169. struct _starpu_heft_data * data = component->data;
  170. struct _starpu_prio_deque * prio = &data->prio;
  171. starpu_pthread_mutex_t * mutex = &data->mutex;
  172. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  173. _starpu_prio_deque_push_task(prio,task);
  174. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  175. heft_progress(component);
  176. return 0;
  177. }
  178. static int heft_can_push(struct starpu_sched_component *component)
  179. {
  180. heft_progress(component);
  181. int ret = 0;
  182. unsigned j;
  183. for(j=0; j < component->nparents; j++)
  184. {
  185. if(component->parents[j] == NULL)
  186. continue;
  187. else
  188. {
  189. ret = component->parents[j]->can_push(component->parents[j]);
  190. if(ret)
  191. break;
  192. }
  193. }
  194. return ret;
  195. }
  196. static void heft_component_deinit_data(struct starpu_sched_component * component)
  197. {
  198. STARPU_ASSERT(starpu_sched_component_is_heft(component));
  199. struct _starpu_heft_data * d = component->data;
  200. struct _starpu_mct_data * mct_d = d->mct_data;
  201. _starpu_prio_deque_destroy(&d->prio);
  202. free(mct_d);
  203. free(d);
  204. }
  205. int starpu_sched_component_is_heft(struct starpu_sched_component * component)
  206. {
  207. return component->push_task == heft_push_task;
  208. }
  209. struct starpu_sched_component * starpu_sched_component_heft_create(struct starpu_sched_tree *tree, struct starpu_sched_component_mct_data * params)
  210. {
  211. struct starpu_sched_component * component = starpu_sched_component_create(tree, "heft");
  212. struct _starpu_mct_data *mct_data = starpu_mct_init_parameters(params);
  213. struct _starpu_heft_data *data;
  214. _STARPU_MALLOC(data, sizeof(*data));
  215. _starpu_prio_deque_init(&data->prio);
  216. STARPU_PTHREAD_MUTEX_INIT(&data->mutex,NULL);
  217. data->mct_data = mct_data;
  218. component->data = data;
  219. component->push_task = heft_push_task;
  220. component->can_push = heft_can_push;
  221. component->deinit_data = heft_component_deinit_data;
  222. return component;
  223. }