component_prio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013, 2017 INRIA
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu_sched_component.h>
  17. #include <starpu_scheduler.h>
  18. #include <common/fxt.h>
  19. #include <core/workers.h>
  20. #include "prio_deque.h"
  21. #include "sched_component.h"
  22. #ifdef STARPU_USE_FXT
  23. #define STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(component,ntasks,exp_len) do { \
  24. int workerid = STARPU_NMAXWORKERS + 1; \
  25. if((component->nchildren == 1) && starpu_sched_component_is_worker(component->children[0])) \
  26. workerid = starpu_sched_component_worker_get_workerid(component->children[0]); \
  27. _STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(workerid, ntasks, exp_len); \
  28. } while (0)
  29. #define STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(component,ntasks,exp_len) do { \
  30. int workerid = STARPU_NMAXWORKERS + 1; \
  31. if((component->nchildren == 1) && starpu_sched_component_is_worker(component->children[0])) \
  32. workerid = starpu_sched_component_worker_get_workerid(component->children[0]); \
  33. _STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(workerid, ntasks, exp_len); \
  34. } while (0)
  35. #else
  36. #define STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(component,ntasks,exp_len) do { } while (0)
  37. #define STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(component,ntasks,exp_len) do { } while (0)
  38. #endif
  39. struct _starpu_prio_data
  40. {
  41. struct _starpu_prio_deque prio;
  42. starpu_pthread_mutex_t mutex;
  43. unsigned ntasks_threshold;
  44. double exp_len_threshold;
  45. };
  46. static void prio_component_deinit_data(struct starpu_sched_component * component)
  47. {
  48. STARPU_ASSERT(component && component->data);
  49. struct _starpu_prio_data * f = component->data;
  50. _starpu_prio_deque_destroy(&f->prio);
  51. STARPU_PTHREAD_MUTEX_DESTROY(&f->mutex);
  52. free(f);
  53. }
  54. static double prio_estimated_end(struct starpu_sched_component * component)
  55. {
  56. STARPU_ASSERT(component && component->data);
  57. struct _starpu_prio_data * data = component->data;
  58. struct _starpu_prio_deque * prio = &data->prio;
  59. int card = starpu_bitmap_cardinal(component->workers_in_ctx);
  60. STARPU_ASSERT(card != 0);
  61. double estimated_end = starpu_sched_component_estimated_end_min(component);
  62. estimated_end += prio->exp_len / card;
  63. return estimated_end;
  64. }
  65. static double prio_estimated_load(struct starpu_sched_component * component)
  66. {
  67. STARPU_ASSERT(component && component->data);
  68. STARPU_ASSERT(starpu_bitmap_cardinal(component->workers_in_ctx) != 0);
  69. struct _starpu_prio_data * data = component->data;
  70. struct _starpu_prio_deque * prio = &data->prio;
  71. starpu_pthread_mutex_t * mutex = &data->mutex;
  72. double relative_speedup = 0.0;
  73. double load = starpu_sched_component_estimated_load(component);
  74. if(STARPU_SCHED_COMPONENT_IS_HOMOGENEOUS(component))
  75. {
  76. int first_worker = starpu_bitmap_first(component->workers_in_ctx);
  77. relative_speedup = starpu_worker_get_relative_speedup(starpu_worker_get_perf_archtype(first_worker, component->tree->sched_ctx_id));
  78. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  79. load += prio->ntasks / relative_speedup;
  80. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  81. return load;
  82. }
  83. else
  84. {
  85. int i;
  86. for(i = starpu_bitmap_first(component->workers_in_ctx);
  87. i != -1;
  88. i = starpu_bitmap_next(component->workers_in_ctx, i))
  89. relative_speedup += starpu_worker_get_relative_speedup(starpu_worker_get_perf_archtype(i, component->tree->sched_ctx_id));
  90. relative_speedup /= starpu_bitmap_cardinal(component->workers_in_ctx);
  91. STARPU_ASSERT(!_STARPU_IS_ZERO(relative_speedup));
  92. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  93. load += prio->ntasks / relative_speedup;
  94. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  95. }
  96. return load;
  97. }
  98. static int prio_push_local_task(struct starpu_sched_component * component, struct starpu_task * task, unsigned is_pushback)
  99. {
  100. STARPU_ASSERT(component && component->data && task);
  101. STARPU_ASSERT(starpu_sched_component_can_execute_task(component,task));
  102. struct _starpu_prio_data * data = component->data;
  103. struct _starpu_prio_deque * prio = &data->prio;
  104. starpu_pthread_mutex_t * mutex = &data->mutex;
  105. int ret;
  106. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  107. double exp_len;
  108. if(!isnan(task->predicted))
  109. exp_len = prio->exp_len + task->predicted;
  110. else
  111. exp_len = prio->exp_len;
  112. if((data->ntasks_threshold != 0) && (data->exp_len_threshold != 0.0) &&
  113. ((prio->ntasks >= data->ntasks_threshold) || (exp_len >= data->exp_len_threshold)))
  114. {
  115. static int warned;
  116. if(task->predicted > data->exp_len_threshold && !warned)
  117. {
  118. _STARPU_DISP("Warning : a predicted task length (%lf) exceeds the expected length threshold (%lf) of a prio component queue, you should reconsider the value of this threshold. This message will not be printed again for further thresholds exceeding.\n",task->predicted,data->exp_len_threshold);
  119. warned = 1;
  120. }
  121. ret = 1;
  122. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  123. }
  124. else
  125. {
  126. if(is_pushback)
  127. ret = _starpu_prio_deque_push_back_task(prio,task);
  128. else
  129. {
  130. ret = _starpu_prio_deque_push_task(prio,task);
  131. starpu_sched_component_prefetch_on_node(component, task);
  132. STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(component, prio->ntasks, exp_len);
  133. }
  134. if(!isnan(task->predicted_transfer)) {
  135. double end = prio_estimated_end(component);
  136. double tfer_end = starpu_timing_now() + task->predicted_transfer;
  137. if(tfer_end < end)
  138. task->predicted_transfer = 0.0;
  139. else
  140. task->predicted_transfer = tfer_end - end;
  141. exp_len += task->predicted_transfer;
  142. }
  143. if(!isnan(task->predicted))
  144. {
  145. prio->exp_len = exp_len;
  146. prio->exp_end = prio->exp_start + prio->exp_len;
  147. }
  148. STARPU_ASSERT(!isnan(prio->exp_end));
  149. STARPU_ASSERT(!isnan(prio->exp_len));
  150. STARPU_ASSERT(!isnan(prio->exp_start));
  151. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  152. if(!is_pushback)
  153. component->can_pull(component);
  154. }
  155. return ret;
  156. }
  157. static int prio_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  158. {
  159. int ret = prio_push_local_task(component, task, 0);
  160. return ret;
  161. }
  162. static struct starpu_task * prio_pull_task(struct starpu_sched_component * component)
  163. {
  164. STARPU_ASSERT(component && component->data);
  165. struct _starpu_prio_data * data = component->data;
  166. struct _starpu_prio_deque * prio = &data->prio;
  167. starpu_pthread_mutex_t * mutex = &data->mutex;
  168. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  169. struct starpu_task * task = _starpu_prio_deque_pop_task(prio);
  170. if(task)
  171. {
  172. if(!isnan(task->predicted))
  173. {
  174. const double exp_len = prio->exp_len - task->predicted;
  175. const double now = starpu_timing_now();
  176. prio->exp_start = now + task->predicted;
  177. if (exp_len >= 0.0)
  178. {
  179. prio->exp_len = exp_len;
  180. }
  181. else
  182. {
  183. /* exp_len can become negative due to rounding errors */
  184. prio->exp_len = starpu_timing_now()-now;
  185. }
  186. }
  187. STARPU_ASSERT_MSG(prio->exp_len>=0, "prio->exp_len=%lf\n",prio->exp_len);
  188. if(!isnan(task->predicted_transfer)){
  189. if (prio->exp_len > task->predicted_transfer)
  190. {
  191. prio->exp_start += task->predicted_transfer;
  192. prio->exp_len -= task->predicted_transfer;
  193. }
  194. else
  195. {
  196. prio->exp_start += prio->exp_len;
  197. prio->exp_len = 0;
  198. }
  199. }
  200. prio->exp_end = prio->exp_start + prio->exp_len;
  201. if(prio->ntasks == 0)
  202. prio->exp_len = 0.0;
  203. STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(component, prio->ntasks, prio->exp_len);
  204. }
  205. STARPU_ASSERT(!isnan(prio->exp_end));
  206. STARPU_ASSERT(!isnan(prio->exp_len));
  207. STARPU_ASSERT(!isnan(prio->exp_start));
  208. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  209. // When a pop is called, a can_push is called for pushing tasks onto
  210. // the empty place of the queue left by the popped task.
  211. starpu_sched_component_send_can_push_to_parents(component);
  212. if(task)
  213. return task;
  214. return NULL;
  215. }
  216. /* When a can_push is caught by this function, we try to pop and push
  217. * tasks from our local queue as much as possible, until a
  218. * push fails, which means that the worker prio_components are
  219. * currently "full".
  220. */
  221. static int prio_can_push(struct starpu_sched_component * component)
  222. {
  223. STARPU_ASSERT(component && starpu_sched_component_is_prio(component));
  224. int res = 0;
  225. struct starpu_task * task;
  226. task = starpu_sched_component_pump_downstream(component, &res);
  227. if(task)
  228. {
  229. int ret = prio_push_local_task(component,task,1);
  230. STARPU_ASSERT(!ret);
  231. }
  232. return res;
  233. }
  234. int starpu_sched_component_is_prio(struct starpu_sched_component * component)
  235. {
  236. return component->push_task == prio_push_task;
  237. }
  238. struct starpu_sched_component * starpu_sched_component_prio_create(struct starpu_sched_tree *tree, struct starpu_sched_component_prio_data * params)
  239. {
  240. struct starpu_sched_component * component = starpu_sched_component_create(tree, "prio");
  241. struct _starpu_prio_data *data;
  242. _STARPU_MALLOC(data, sizeof(*data));
  243. _starpu_prio_deque_init(&data->prio);
  244. STARPU_PTHREAD_MUTEX_INIT(&data->mutex,NULL);
  245. component->data = data;
  246. component->estimated_end = prio_estimated_end;
  247. component->estimated_load = prio_estimated_load;
  248. component->push_task = prio_push_task;
  249. component->pull_task = prio_pull_task;
  250. component->can_push = prio_can_push;
  251. component->deinit_data = prio_component_deinit_data;
  252. if(params)
  253. {
  254. data->ntasks_threshold=params->ntasks_threshold;
  255. data->exp_len_threshold=params->exp_len_threshold;
  256. }
  257. else
  258. {
  259. data->ntasks_threshold=0;
  260. data->exp_len_threshold=0.0;
  261. }
  262. return component;
  263. }