component_prio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), 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. #ifdef STARPU_USE_FXT
  22. #define STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(component,ntasks,exp_len) do { \
  23. int workerid = STARPU_NMAXWORKERS + 1; \
  24. if((component->nchildren == 1) && starpu_sched_component_is_worker(component->children[0])) \
  25. workerid = starpu_sched_component_worker_get_workerid(component->children[0]); \
  26. _STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(workerid, ntasks, exp_len); \
  27. } while (0)
  28. #define STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(component,ntasks,exp_len) do { \
  29. int workerid = STARPU_NMAXWORKERS + 1; \
  30. if((component->nchildren == 1) && starpu_sched_component_is_worker(component->children[0])) \
  31. workerid = starpu_sched_component_worker_get_workerid(component->children[0]); \
  32. _STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(workerid, ntasks, exp_len); \
  33. } while (0)
  34. #else
  35. #define STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(component,ntasks,exp_len) do { } while (0)
  36. #define STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(component,ntasks,exp_len) do { } while (0)
  37. #endif
  38. struct _starpu_prio_data
  39. {
  40. struct _starpu_prio_deque prio;
  41. starpu_pthread_mutex_t mutex;
  42. unsigned ntasks_threshold;
  43. double exp_len_threshold;
  44. int ready;
  45. int exp;
  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 * queue = &data->prio;
  60. return starpu_sched_component_estimated_end_min_add(component, queue->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 * queue = &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 += queue->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 += queue->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 * queue = &data->prio;
  101. starpu_pthread_mutex_t * mutex = &data->mutex;
  102. int ret = 0;
  103. const double now = starpu_timing_now();
  104. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  105. double exp_len = NAN;
  106. if (data->ntasks_threshold != 0 && queue->ntasks >= data->ntasks_threshold)
  107. {
  108. STARPU_ASSERT(!is_pushback);
  109. ret = 1;
  110. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  111. }
  112. else if(data->exp)
  113. {
  114. if(!isnan(task->predicted))
  115. exp_len = queue->exp_len + task->predicted;
  116. else
  117. exp_len = queue->exp_len;
  118. if (data->exp_len_threshold != 0.0 && exp_len >= data->exp_len_threshold)
  119. {
  120. static int warned;
  121. if(data->exp_len_threshold != 0.0 && task->predicted > data->exp_len_threshold && !warned)
  122. {
  123. _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);
  124. warned = 1;
  125. }
  126. STARPU_ASSERT(!is_pushback);
  127. ret = 1;
  128. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  129. }
  130. else
  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. queue->exp_len = exp_len;
  145. queue->exp_end = queue->exp_start + queue->exp_len;
  146. }
  147. STARPU_ASSERT(!isnan(queue->exp_end));
  148. STARPU_ASSERT(!isnan(queue->exp_len));
  149. STARPU_ASSERT(!isnan(queue->exp_start));
  150. }
  151. }
  152. if(!ret)
  153. {
  154. if(is_pushback)
  155. ret = _starpu_prio_deque_push_front_task(queue,task);
  156. else
  157. {
  158. ret = _starpu_prio_deque_push_back_task(queue,task);
  159. starpu_sched_component_prefetch_on_node(component, task);
  160. STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(component, queue->ntasks, exp_len);
  161. }
  162. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  163. if(!is_pushback)
  164. component->can_pull(component);
  165. }
  166. return ret;
  167. }
  168. static int prio_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  169. {
  170. int ret = prio_push_local_task(component, task, 0);
  171. return ret;
  172. }
  173. static struct starpu_task * prio_pull_task(struct starpu_sched_component * component, struct starpu_sched_component * to)
  174. {
  175. STARPU_ASSERT(component && component->data);
  176. struct _starpu_prio_data * data = component->data;
  177. struct _starpu_prio_deque * queue = &data->prio;
  178. starpu_pthread_mutex_t * mutex = &data->mutex;
  179. const double now = starpu_timing_now();
  180. if (!STARPU_RUNNING_ON_VALGRIND && _starpu_prio_deque_is_empty(queue))
  181. {
  182. starpu_sched_component_send_can_push_to_parents(component);
  183. return NULL;
  184. }
  185. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  186. struct starpu_task * task;
  187. if (data->ready && to->properties & STARPU_SCHED_COMPONENT_SINGLE_MEMORY_NODE)
  188. task = _starpu_prio_deque_deque_first_ready_task(queue, starpu_bitmap_first(&to->workers_in_ctx));
  189. else
  190. task = _starpu_prio_deque_pop_task(queue);
  191. if(task && data->exp)
  192. {
  193. if(!isnan(task->predicted))
  194. {
  195. const double exp_len = queue->exp_len - task->predicted;
  196. queue->exp_start = now + task->predicted;
  197. if (exp_len >= 0.0)
  198. {
  199. queue->exp_len = exp_len;
  200. }
  201. else
  202. {
  203. /* exp_len can become negative due to rounding errors */
  204. queue->exp_len = 0.0;
  205. }
  206. }
  207. STARPU_ASSERT_MSG(queue->exp_len>=0, "prio->exp_len=%lf\n",queue->exp_len);
  208. if(!isnan(task->predicted_transfer))
  209. {
  210. if (queue->exp_len > task->predicted_transfer)
  211. {
  212. queue->exp_start += task->predicted_transfer;
  213. queue->exp_len -= task->predicted_transfer;
  214. }
  215. else
  216. {
  217. queue->exp_start += queue->exp_len;
  218. queue->exp_len = 0;
  219. }
  220. }
  221. queue->exp_end = queue->exp_start + queue->exp_len;
  222. if(queue->ntasks == 0)
  223. queue->exp_len = 0.0;
  224. }
  225. if(task)
  226. STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(component, queue->ntasks, queue->exp_len);
  227. STARPU_ASSERT(!isnan(queue->exp_end));
  228. STARPU_ASSERT(!isnan(queue->exp_len));
  229. STARPU_ASSERT(!isnan(queue->exp_start));
  230. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  231. // When a pop is called, a can_push is called for pushing tasks onto
  232. // the empty place of the queue left by the popped task.
  233. starpu_sched_component_send_can_push_to_parents(component);
  234. if(task)
  235. return task;
  236. return NULL;
  237. }
  238. /* When a can_push is caught by this function, we try to pop and push
  239. * tasks from our local queue as much as possible, until a
  240. * push fails, which means that the worker prio_components are
  241. * currently "full".
  242. */
  243. static int prio_can_push(struct starpu_sched_component * component, struct starpu_sched_component * to STARPU_ATTRIBUTE_UNUSED)
  244. {
  245. STARPU_ASSERT(component && starpu_sched_component_is_prio(component));
  246. int res = 0;
  247. struct starpu_task * task;
  248. task = starpu_sched_component_pump_downstream(component, &res);
  249. if(task)
  250. {
  251. int ret = prio_push_local_task(component,task,1);
  252. STARPU_ASSERT(!ret);
  253. }
  254. return res;
  255. }
  256. int starpu_sched_component_is_prio(struct starpu_sched_component * component)
  257. {
  258. return component->push_task == prio_push_task;
  259. }
  260. struct starpu_sched_component * starpu_sched_component_prio_create(struct starpu_sched_tree *tree, struct starpu_sched_component_prio_data * params)
  261. {
  262. struct starpu_sched_component * component = starpu_sched_component_create(tree, "prio");
  263. struct _starpu_prio_data *data;
  264. _STARPU_MALLOC(data, sizeof(*data));
  265. _starpu_prio_deque_init(&data->prio);
  266. STARPU_PTHREAD_MUTEX_INIT(&data->mutex,NULL);
  267. component->data = data;
  268. component->estimated_end = prio_estimated_end;
  269. component->estimated_load = prio_estimated_load;
  270. component->push_task = prio_push_task;
  271. component->pull_task = prio_pull_task;
  272. component->can_push = prio_can_push;
  273. component->deinit_data = prio_component_deinit_data;
  274. if(params)
  275. {
  276. data->ntasks_threshold=params->ntasks_threshold;
  277. data->exp_len_threshold=params->exp_len_threshold;
  278. data->ready=params->ready;
  279. data->exp=params->exp;
  280. }
  281. else
  282. {
  283. data->ntasks_threshold=0;
  284. data->exp_len_threshold=0.0;
  285. data->ready=0;
  286. data->exp=0;
  287. }
  288. return component;
  289. }