component_fifo.c 9.0 KB

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