component_prio.c 9.0 KB

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