component_heft.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2017 Université de Bordeaux
  4. * Copyright (C) 2013, 2017 INRIA
  5. * Copyright (C) 2013 Simon Archipoff
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /* HEFT variant which tries to schedule a given number of tasks instead of just
  19. * the first of its scheduling window, and actually schedule the task for which
  20. * the most benefit is achieved. */
  21. #include <starpu_sched_component.h>
  22. #include "prio_deque.h"
  23. #include "sched_component.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;
  43. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  44. /* Try to look at NTASKS from the queue */
  45. for (ntasks = 0; ntasks < NTASKS; ntasks++)
  46. {
  47. tasks[ntasks] = _starpu_prio_deque_pop_task(prio);
  48. if (!tasks[ntasks])
  49. break;
  50. }
  51. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  52. if (!ntasks)
  53. {
  54. return 1;
  55. }
  56. {
  57. struct _starpu_mct_data * d = data->mct_data;
  58. struct starpu_sched_component * best_component = NULL;
  59. unsigned n, i;
  60. /* Estimated task duration for each child */
  61. double estimated_lengths[component->nchildren * ntasks];
  62. /* Estimated transfer duration for each child */
  63. double estimated_transfer_length[component->nchildren * ntasks];
  64. /* Estimated transfer+task termination for each child */
  65. double estimated_ends_with_task[component->nchildren * ntasks];
  66. /* Minimum transfer+task termination on all children */
  67. double min_exp_end_with_task[ntasks];
  68. /* Maximum transfer+task termination on all children */
  69. double max_exp_end_with_task[ntasks];
  70. int suitable_components[component->nchildren * ntasks];
  71. unsigned nsuitable_components[ntasks];
  72. /* Estimate durations */
  73. for (n = 0; n < ntasks; n++)
  74. {
  75. int offset = component->nchildren * n;
  76. min_exp_end_with_task[n] = DBL_MAX;
  77. max_exp_end_with_task[n] = 0.0;
  78. nsuitable_components[n] = starpu_mct_compute_execution_times(component, tasks[n],
  79. estimated_lengths + offset,
  80. estimated_transfer_length + offset,
  81. suitable_components + offset);
  82. starpu_mct_compute_expected_times(component, tasks[n],
  83. estimated_lengths + offset,
  84. estimated_transfer_length + offset,
  85. estimated_ends_with_task + offset,
  86. &min_exp_end_with_task[n], &max_exp_end_with_task[n],
  87. suitable_components + offset, nsuitable_components[n]);
  88. }
  89. int best_task = 0;
  90. double max_benefit = 0;
  91. /* Find the task which provides the most computation time benefit */
  92. for (n = 1; n < ntasks; n++)
  93. {
  94. double benefit = max_exp_end_with_task[n] - min_exp_end_with_task[n];
  95. if (max_benefit < benefit)
  96. {
  97. max_benefit = benefit;
  98. best_task = n;
  99. }
  100. }
  101. double best_fitness = DBL_MAX;
  102. int best_icomponent = -1;
  103. /* Push back the other tasks */
  104. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  105. for (n = ntasks - 1; n < ntasks; n--)
  106. if ((int) n != best_task)
  107. _starpu_prio_deque_push_back_task(prio, tasks[n]);
  108. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  109. /* And now find out which worker suits best for this task,
  110. * including data transfer */
  111. for(i = 0; i < nsuitable_components[best_task]; i++)
  112. {
  113. int offset = component->nchildren * best_task;
  114. int icomponent = suitable_components[offset + i];
  115. #ifdef STARPU_DEVEL
  116. #warning FIXME: take energy consumption into account
  117. #endif
  118. double tmp = starpu_mct_compute_fitness(d,
  119. estimated_ends_with_task[offset + icomponent] - estimated_transfer_length[offset + icomponent],
  120. min_exp_end_with_task[best_task],
  121. max_exp_end_with_task[best_task],
  122. estimated_transfer_length[offset + icomponent],
  123. 0.0);
  124. if(tmp < best_fitness)
  125. {
  126. best_fitness = tmp;
  127. best_icomponent = icomponent;
  128. }
  129. }
  130. STARPU_ASSERT(best_icomponent != -1);
  131. STARPU_ASSERT(best_task >= 0);
  132. best_component = component->children[best_icomponent];
  133. if(starpu_sched_component_is_worker(best_component))
  134. {
  135. best_component->can_pull(best_component);
  136. return 1;
  137. }
  138. _STARPU_TASK_BREAK_ON(tasks[best_task], sched);
  139. int ret = starpu_sched_component_push_task(component, best_component, tasks[best_task]);
  140. if (ret)
  141. {
  142. /* Could not push to child actually, push that one back too */
  143. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  144. _starpu_prio_deque_push_back_task(prio, tasks[best_task]);
  145. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  146. return 1;
  147. }
  148. else
  149. return 0;
  150. }
  151. }
  152. /* Try to push some tasks below */
  153. static void heft_progress(struct starpu_sched_component *component)
  154. {
  155. STARPU_ASSERT(component && starpu_sched_component_is_heft(component));
  156. while (!heft_progress_one(component))
  157. ;
  158. }
  159. static int heft_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  160. {
  161. STARPU_ASSERT(component && task && starpu_sched_component_is_heft(component));
  162. struct _starpu_heft_data * data = component->data;
  163. struct _starpu_prio_deque * prio = &data->prio;
  164. starpu_pthread_mutex_t * mutex = &data->mutex;
  165. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  166. _starpu_prio_deque_push_task(prio,task);
  167. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  168. heft_progress(component);
  169. return 0;
  170. }
  171. static int heft_can_push(struct starpu_sched_component *component)
  172. {
  173. heft_progress(component);
  174. int ret = 0, j;
  175. for(j=0; j < component->nparents; j++)
  176. {
  177. if(component->parents[j] == NULL)
  178. continue;
  179. else
  180. {
  181. ret = component->parents[j]->can_push(component->parents[j]);
  182. if(ret)
  183. break;
  184. }
  185. }
  186. return ret;
  187. }
  188. static void heft_component_deinit_data(struct starpu_sched_component * component)
  189. {
  190. STARPU_ASSERT(starpu_sched_component_is_heft(component));
  191. struct _starpu_heft_data * d = component->data;
  192. struct _starpu_mct_data * mct_d = d->mct_data;
  193. _starpu_prio_deque_destroy(&d->prio);
  194. free(mct_d);
  195. free(d);
  196. }
  197. int starpu_sched_component_is_heft(struct starpu_sched_component * component)
  198. {
  199. return component->push_task == heft_push_task;
  200. }
  201. struct starpu_sched_component * starpu_sched_component_heft_create(struct starpu_sched_tree *tree, struct starpu_sched_component_mct_data * params)
  202. {
  203. struct starpu_sched_component * component = starpu_sched_component_create(tree, "heft");
  204. struct _starpu_mct_data *mct_data = starpu_mct_init_parameters(params);
  205. struct _starpu_heft_data *data;
  206. _STARPU_MALLOC(data, sizeof(*data));
  207. _starpu_prio_deque_init(&data->prio);
  208. STARPU_PTHREAD_MUTEX_INIT(&data->mutex,NULL);
  209. data->mct_data = mct_data;
  210. component->data = data;
  211. component->push_task = heft_push_task;
  212. component->can_push = heft_can_push;
  213. component->deinit_data = heft_component_deinit_data;
  214. return component;
  215. }