component_heft.c 7.7 KB

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