component_prio.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013,2017 Inria
  4. * Copyright (C) 2014-2017 CNRS
  5. * Copyright (C) 2014-2015,2017-2018 Université de Bordeaux
  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. #include <starpu_sched_component.h>
  19. #include <starpu_scheduler.h>
  20. #include <common/fxt.h>
  21. #include <core/workers.h>
  22. #include "prio_deque.h"
  23. #ifdef STARPU_USE_FXT
  24. #define STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(component,ntasks,exp_len) do { \
  25. int workerid = STARPU_NMAXWORKERS + 1; \
  26. if((component->nchildren == 1) && starpu_sched_component_is_worker(component->children[0])) \
  27. workerid = starpu_sched_component_worker_get_workerid(component->children[0]); \
  28. _STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(workerid, ntasks, exp_len); \
  29. } while (0)
  30. #define STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(component,ntasks,exp_len) do { \
  31. int workerid = STARPU_NMAXWORKERS + 1; \
  32. if((component->nchildren == 1) && starpu_sched_component_is_worker(component->children[0])) \
  33. workerid = starpu_sched_component_worker_get_workerid(component->children[0]); \
  34. _STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(workerid, ntasks, exp_len); \
  35. } while (0)
  36. #else
  37. #define STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(component,ntasks,exp_len) do { } while (0)
  38. #define STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(component,ntasks,exp_len) do { } while (0)
  39. #endif
  40. struct _starpu_prio_data
  41. {
  42. struct _starpu_prio_deque prio;
  43. starpu_pthread_mutex_t mutex;
  44. unsigned ntasks_threshold;
  45. double exp_len_threshold;
  46. };
  47. static void prio_component_deinit_data(struct starpu_sched_component * component)
  48. {
  49. STARPU_ASSERT(component && component->data);
  50. struct _starpu_prio_data * f = component->data;
  51. _starpu_prio_deque_destroy(&f->prio);
  52. STARPU_PTHREAD_MUTEX_DESTROY(&f->mutex);
  53. free(f);
  54. }
  55. static double prio_estimated_end(struct starpu_sched_component * component)
  56. {
  57. STARPU_ASSERT(component && component->data);
  58. struct _starpu_prio_data * data = component->data;
  59. struct _starpu_prio_deque * prio = &data->prio;
  60. return starpu_sched_component_estimated_end_min_add(component, prio->exp_len);
  61. }
  62. static double prio_estimated_load(struct starpu_sched_component * component)
  63. {
  64. STARPU_ASSERT(component && component->data);
  65. STARPU_ASSERT(starpu_bitmap_cardinal(component->workers_in_ctx) != 0);
  66. struct _starpu_prio_data * data = component->data;
  67. struct _starpu_prio_deque * prio = &data->prio;
  68. starpu_pthread_mutex_t * mutex = &data->mutex;
  69. double relative_speedup = 0.0;
  70. double load = starpu_sched_component_estimated_load(component);
  71. if(STARPU_SCHED_COMPONENT_IS_HOMOGENEOUS(component))
  72. {
  73. int first_worker = starpu_bitmap_first(component->workers_in_ctx);
  74. relative_speedup = starpu_worker_get_relative_speedup(starpu_worker_get_perf_archtype(first_worker, component->tree->sched_ctx_id));
  75. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  76. load += prio->ntasks / relative_speedup;
  77. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  78. return load;
  79. }
  80. else
  81. {
  82. int i;
  83. for(i = starpu_bitmap_first(component->workers_in_ctx);
  84. i != -1;
  85. i = starpu_bitmap_next(component->workers_in_ctx, i))
  86. relative_speedup += starpu_worker_get_relative_speedup(starpu_worker_get_perf_archtype(i, component->tree->sched_ctx_id));
  87. relative_speedup /= starpu_bitmap_cardinal(component->workers_in_ctx);
  88. STARPU_ASSERT(!_STARPU_IS_ZERO(relative_speedup));
  89. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  90. load += prio->ntasks / relative_speedup;
  91. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  92. }
  93. return load;
  94. }
  95. static int prio_push_local_task(struct starpu_sched_component * component, struct starpu_task * task, unsigned is_pushback)
  96. {
  97. STARPU_ASSERT(component && component->data && task);
  98. STARPU_ASSERT(starpu_sched_component_can_execute_task(component,task));
  99. struct _starpu_prio_data * data = component->data;
  100. struct _starpu_prio_deque * prio = &data->prio;
  101. starpu_pthread_mutex_t * mutex = &data->mutex;
  102. int ret;
  103. const double now = starpu_timing_now();
  104. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  105. double exp_len;
  106. if(!isnan(task->predicted))
  107. exp_len = prio->exp_len + task->predicted;
  108. else
  109. exp_len = prio->exp_len;
  110. if((data->ntasks_threshold != 0) && (data->exp_len_threshold != 0.0) &&
  111. ((prio->ntasks >= data->ntasks_threshold) || (exp_len >= data->exp_len_threshold)))
  112. {
  113. static int warned;
  114. if(task->predicted > data->exp_len_threshold && !warned)
  115. {
  116. _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);
  117. warned = 1;
  118. }
  119. ret = 1;
  120. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  121. }
  122. else
  123. {
  124. if(is_pushback)
  125. ret = _starpu_prio_deque_push_front_task(prio,task);
  126. else
  127. {
  128. ret = _starpu_prio_deque_push_back_task(prio,task);
  129. starpu_sched_component_prefetch_on_node(component, task);
  130. STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(component, prio->ntasks, exp_len);
  131. }
  132. if(!isnan(task->predicted_transfer))
  133. {
  134. double end = prio_estimated_end(component);
  135. double tfer_end = now + task->predicted_transfer;
  136. if(tfer_end < end)
  137. task->predicted_transfer = 0.0;
  138. else
  139. task->predicted_transfer = tfer_end - end;
  140. exp_len += task->predicted_transfer;
  141. }
  142. if(!isnan(task->predicted))
  143. {
  144. prio->exp_len = exp_len;
  145. prio->exp_end = prio->exp_start + prio->exp_len;
  146. }
  147. STARPU_ASSERT(!isnan(prio->exp_end));
  148. STARPU_ASSERT(!isnan(prio->exp_len));
  149. STARPU_ASSERT(!isnan(prio->exp_start));
  150. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  151. if(!is_pushback)
  152. component->can_pull(component);
  153. }
  154. return ret;
  155. }
  156. static int prio_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  157. {
  158. int ret = prio_push_local_task(component, task, 0);
  159. return ret;
  160. }
  161. static struct starpu_task * prio_pull_task(struct starpu_sched_component * component, struct starpu_sched_component * to STARPU_ATTRIBUTE_UNUSED)
  162. {
  163. STARPU_ASSERT(component && component->data);
  164. struct _starpu_prio_data * data = component->data;
  165. struct _starpu_prio_deque * prio = &data->prio;
  166. starpu_pthread_mutex_t * mutex = &data->mutex;
  167. const double now = starpu_timing_now();
  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. prio->exp_start = now + task->predicted;
  176. if (exp_len >= 0.0)
  177. {
  178. prio->exp_len = exp_len;
  179. }
  180. else
  181. {
  182. /* exp_len can become negative due to rounding errors */
  183. prio->exp_len = 0.0;
  184. }
  185. }
  186. STARPU_ASSERT_MSG(prio->exp_len>=0, "prio->exp_len=%lf\n",prio->exp_len);
  187. if(!isnan(task->predicted_transfer))
  188. {
  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, struct starpu_sched_component * to STARPU_ATTRIBUTE_UNUSED)
  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. }