component_prio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 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 "prio_deque.h"
  19. #include "sched_component.h"
  20. #define STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(component,ntasks,exp_len) do { \
  21. int workerid = STARPU_NMAXWORKERS + 1; \
  22. if((component->nchildren == 1) && starpu_sched_component_is_worker(component->children[0])) \
  23. workerid = starpu_sched_component_worker_get_workerid(component->children[0]); \
  24. _STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(workerid, ntasks, exp_len); \
  25. } while (0)
  26. #define STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(component,ntasks,exp_len) do { \
  27. int workerid = STARPU_NMAXWORKERS + 1; \
  28. if((component->nchildren == 1) && starpu_sched_component_is_worker(component->children[0])) \
  29. workerid = starpu_sched_component_worker_get_workerid(component->children[0]); \
  30. _STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(workerid, ntasks, exp_len); \
  31. } while (0)
  32. struct _starpu_prio_data
  33. {
  34. struct _starpu_prio_deque prio;
  35. starpu_pthread_mutex_t mutex;
  36. unsigned ntasks_threshold;
  37. double exp_len_threshold;
  38. };
  39. static void prio_component_deinit_data(struct starpu_sched_component * component)
  40. {
  41. STARPU_ASSERT(component && component->data);
  42. struct _starpu_prio_data * f = component->data;
  43. _starpu_prio_deque_destroy(&f->prio);
  44. STARPU_PTHREAD_MUTEX_DESTROY(&f->mutex);
  45. free(f);
  46. }
  47. static double prio_estimated_end(struct starpu_sched_component * component)
  48. {
  49. STARPU_ASSERT(component && component->data);
  50. struct _starpu_prio_data * data = component->data;
  51. struct _starpu_prio_deque * prio = &data->prio;
  52. starpu_pthread_mutex_t * mutex = &data->mutex;
  53. int card = starpu_bitmap_cardinal(component->workers_in_ctx);
  54. STARPU_ASSERT(card != 0);
  55. STARPU_PTHREAD_MUTEX_LOCK(mutex);
  56. prio->exp_start = STARPU_MAX(prio->exp_start, starpu_timing_now());
  57. double estimated_end = prio->exp_start + prio->exp_len / card;
  58. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  59. return estimated_end;
  60. }
  61. static double prio_estimated_load(struct starpu_sched_component * component)
  62. {
  63. STARPU_ASSERT(component && component->data);
  64. STARPU_ASSERT(starpu_bitmap_cardinal(component->workers_in_ctx) != 0);
  65. struct _starpu_prio_data * data = component->data;
  66. struct _starpu_prio_deque * prio = &data->prio;
  67. starpu_pthread_mutex_t * mutex = &data->mutex;
  68. double relative_speedup = 0.0;
  69. double load;
  70. if(STARPU_SCHED_COMPONENT_IS_HOMOGENEOUS(component))
  71. {
  72. int first_worker = starpu_bitmap_first(component->workers_in_ctx);
  73. relative_speedup = starpu_worker_get_relative_speedup(starpu_worker_get_perf_archtype(first_worker, component->tree->sched_ctx_id));
  74. STARPU_PTHREAD_MUTEX_LOCK(mutex);
  75. load = prio->ntasks / relative_speedup;
  76. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  77. return load;
  78. }
  79. else
  80. {
  81. int i;
  82. for(i = starpu_bitmap_first(component->workers_in_ctx);
  83. i != -1;
  84. i = starpu_bitmap_next(component->workers_in_ctx, i))
  85. relative_speedup += starpu_worker_get_relative_speedup(starpu_worker_get_perf_archtype(i, component->tree->sched_ctx_id));
  86. relative_speedup /= starpu_bitmap_cardinal(component->workers_in_ctx);
  87. STARPU_ASSERT(!_STARPU_IS_ZERO(relative_speedup));
  88. STARPU_PTHREAD_MUTEX_LOCK(mutex);
  89. load = prio->ntasks / relative_speedup;
  90. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  91. }
  92. int i;
  93. for(i = 0; i < component->nchildren; i++)
  94. {
  95. struct starpu_sched_component * c = component->children[i];
  96. load += c->estimated_load(c);
  97. }
  98. return load;
  99. }
  100. static int prio_push_local_task(struct starpu_sched_component * component, struct starpu_task * task, unsigned is_pushback)
  101. {
  102. STARPU_ASSERT(component && component->data && task);
  103. STARPU_ASSERT(starpu_sched_component_can_execute_task(component,task));
  104. struct _starpu_prio_data * data = component->data;
  105. struct _starpu_prio_deque * prio = &data->prio;
  106. starpu_pthread_mutex_t * mutex = &data->mutex;
  107. int ret;
  108. STARPU_PTHREAD_MUTEX_LOCK(mutex);
  109. double exp_len;
  110. if(!isnan(task->predicted))
  111. exp_len = prio->exp_len + task->predicted;
  112. else
  113. exp_len = prio->exp_len;
  114. if((data->ntasks_threshold != 0) && (data->exp_len_threshold != 0.0) &&
  115. ((prio->ntasks >= data->ntasks_threshold) || (exp_len >= data->exp_len_threshold)))
  116. {
  117. static int warned;
  118. if(task->predicted > data->exp_len_threshold && !warned)
  119. {
  120. _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);
  121. warned = 1;
  122. }
  123. ret = 1;
  124. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  125. }
  126. else
  127. {
  128. if(is_pushback)
  129. ret = _starpu_prio_deque_push_back_task(prio,task);
  130. else
  131. {
  132. ret = _starpu_prio_deque_push_task(prio,task);
  133. starpu_sched_component_prefetch_on_node(component, task);
  134. STARPU_TRACE_SCHED_COMPONENT_PUSH_PRIO(component, prio->ntasks, exp_len);
  135. }
  136. if(!isnan(task->predicted))
  137. {
  138. prio->exp_len = exp_len;
  139. prio->exp_end = prio->exp_start + prio->exp_len;
  140. }
  141. STARPU_ASSERT(!isnan(prio->exp_end));
  142. STARPU_ASSERT(!isnan(prio->exp_len));
  143. STARPU_ASSERT(!isnan(prio->exp_start));
  144. if(!is_pushback)
  145. component->can_pull(component);
  146. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  147. }
  148. return ret;
  149. }
  150. static int prio_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  151. {
  152. int ret = prio_push_local_task(component, task, 0);
  153. return ret;
  154. }
  155. static struct starpu_task * prio_pull_task(struct starpu_sched_component * component)
  156. {
  157. STARPU_ASSERT(component && component->data);
  158. struct _starpu_prio_data * data = component->data;
  159. struct _starpu_prio_deque * prio = &data->prio;
  160. starpu_pthread_mutex_t * mutex = &data->mutex;
  161. STARPU_PTHREAD_MUTEX_LOCK(mutex);
  162. struct starpu_task * task = _starpu_prio_deque_pop_task(prio);
  163. if(task)
  164. {
  165. if(!isnan(task->predicted))
  166. {
  167. prio->exp_start = starpu_timing_now() + task->predicted;
  168. prio->exp_len -= task->predicted;
  169. }
  170. prio->exp_end = prio->exp_start + prio->exp_len;
  171. if(prio->ntasks == 0)
  172. prio->exp_len = 0.0;
  173. STARPU_TRACE_SCHED_COMPONENT_POP_PRIO(component, prio->ntasks, prio->exp_len);
  174. }
  175. STARPU_ASSERT(!isnan(prio->exp_end));
  176. STARPU_ASSERT(!isnan(prio->exp_len));
  177. STARPU_ASSERT(!isnan(prio->exp_start));
  178. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  179. // When a pop is called, a can_push is called for pushing tasks onto
  180. // the empty place of the queue left by the popped task.
  181. int i,ret;
  182. for(i=0; i < component->nparents; i++)
  183. {
  184. if(component->parents[i] == NULL)
  185. continue;
  186. else
  187. {
  188. ret = component->parents[i]->can_push(component->parents[i]);
  189. if(ret)
  190. break;
  191. }
  192. }
  193. if(task)
  194. return task;
  195. return NULL;
  196. }
  197. /* When a can_push is caught by this function, we try to pop and push
  198. * tasks from our local queue as much as possible, until a
  199. * push fails, which means that the worker prio_components are
  200. * currently "full".
  201. */
  202. static int prio_can_push(struct starpu_sched_component * component)
  203. {
  204. STARPU_ASSERT(component && starpu_sched_component_is_prio(component));
  205. int ret = 0;
  206. int res = 0;
  207. STARPU_ASSERT(component->nchildren == 1);
  208. struct starpu_sched_component * child = component->children[0];
  209. struct starpu_task * task = component->pull_task(component);
  210. if(task)
  211. ret = child->push_task(child,task);
  212. while(task && !ret)
  213. {
  214. if(!res)
  215. res = 1;
  216. task = component->pull_task(component);
  217. if(task)
  218. ret = child->push_task(child,task);
  219. }
  220. if(task && ret)
  221. prio_push_local_task(component,task,1);
  222. return res;
  223. }
  224. int starpu_sched_component_is_prio(struct starpu_sched_component * component)
  225. {
  226. return component->push_task == prio_push_task;
  227. }
  228. struct starpu_sched_component * starpu_sched_component_prio_create(struct starpu_sched_tree *tree, struct starpu_prio_data * params)
  229. {
  230. struct starpu_sched_component * component = starpu_sched_component_create(tree);
  231. struct _starpu_prio_data * data = malloc(sizeof(*data));
  232. _starpu_prio_deque_init(&data->prio);
  233. STARPU_PTHREAD_MUTEX_INIT(&data->mutex,NULL);
  234. component->data = data;
  235. component->estimated_end = prio_estimated_end;
  236. component->estimated_load = prio_estimated_load;
  237. component->push_task = prio_push_task;
  238. component->pull_task = prio_pull_task;
  239. component->can_push = prio_can_push;
  240. component->deinit_data = prio_component_deinit_data;
  241. if(params)
  242. {
  243. data->ntasks_threshold=params->ntasks_threshold;
  244. data->exp_len_threshold=params->exp_len_threshold;
  245. }
  246. else
  247. {
  248. data->ntasks_threshold=0;
  249. data->exp_len_threshold=0.0;
  250. }
  251. return component;
  252. }