component_fifo.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013, 2017 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. #include "sched_component.h"
  22. struct _starpu_fifo_data
  23. {
  24. struct _starpu_fifo_taskq * fifo;
  25. starpu_pthread_mutex_t mutex;
  26. unsigned ntasks_threshold;
  27. double exp_len_threshold;
  28. };
  29. static void fifo_component_deinit_data(struct starpu_sched_component * component)
  30. {
  31. STARPU_ASSERT(component && component->data);
  32. struct _starpu_fifo_data * f = component->data;
  33. _starpu_destroy_fifo(f->fifo);
  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 * fifo = data->fifo;
  42. int card = starpu_bitmap_cardinal(component->workers_in_ctx);
  43. STARPU_ASSERT(card != 0);
  44. double estimated_end = starpu_sched_component_estimated_end_min(component);
  45. estimated_end += fifo->exp_len / card;
  46. return estimated_end;
  47. }
  48. static double fifo_estimated_load(struct starpu_sched_component * component)
  49. {
  50. STARPU_ASSERT(component && component->data);
  51. STARPU_ASSERT(starpu_bitmap_cardinal(component->workers_in_ctx) != 0);
  52. struct _starpu_fifo_data * data = component->data;
  53. struct _starpu_fifo_taskq * fifo = data->fifo;
  54. starpu_pthread_mutex_t * mutex = &data->mutex;
  55. double relative_speedup = 0.0;
  56. double load = starpu_sched_component_estimated_load(component);
  57. if(STARPU_SCHED_COMPONENT_IS_HOMOGENEOUS(component))
  58. {
  59. int first_worker = starpu_bitmap_first(component->workers_in_ctx);
  60. relative_speedup = starpu_worker_get_relative_speedup(starpu_worker_get_perf_archtype(first_worker, component->tree->sched_ctx_id));
  61. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  62. load += fifo->ntasks / relative_speedup;
  63. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  64. return load;
  65. }
  66. else
  67. {
  68. int i;
  69. for(i = starpu_bitmap_first(component->workers_in_ctx);
  70. i != -1;
  71. i = starpu_bitmap_next(component->workers_in_ctx, i))
  72. relative_speedup += starpu_worker_get_relative_speedup(starpu_worker_get_perf_archtype(i, component->tree->sched_ctx_id));
  73. relative_speedup /= starpu_bitmap_cardinal(component->workers_in_ctx);
  74. STARPU_ASSERT(!_STARPU_IS_ZERO(relative_speedup));
  75. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  76. load += fifo->ntasks / relative_speedup;
  77. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  78. }
  79. return load;
  80. }
  81. static int fifo_push_local_task(struct starpu_sched_component * component, struct starpu_task * task, unsigned is_pushback)
  82. {
  83. STARPU_ASSERT(component && component->data && task);
  84. STARPU_ASSERT(starpu_sched_component_can_execute_task(component,task));
  85. struct _starpu_fifo_data * data = component->data;
  86. struct _starpu_fifo_taskq * fifo = data->fifo;
  87. starpu_pthread_mutex_t * mutex = &data->mutex;
  88. int ret = 0;
  89. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  90. double exp_len;
  91. if(!isnan(task->predicted))
  92. exp_len = fifo->exp_len + task->predicted;
  93. else
  94. exp_len = fifo->exp_len;
  95. if((data->ntasks_threshold != 0) && (data->exp_len_threshold != 0.0) &&
  96. ((fifo->ntasks >= data->ntasks_threshold) || (exp_len >= data->exp_len_threshold)))
  97. {
  98. static int warned;
  99. if(task->predicted > data->exp_len_threshold && !warned)
  100. {
  101. _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);
  102. warned = 1;
  103. }
  104. STARPU_ASSERT(!is_pushback);
  105. ret = 1;
  106. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  107. }
  108. else
  109. {
  110. if(is_pushback)
  111. ret = _starpu_fifo_push_back_task(fifo,task);
  112. else
  113. {
  114. ret = _starpu_fifo_push_task(fifo,task);
  115. starpu_sched_component_prefetch_on_node(component, task);
  116. }
  117. if(!isnan(task->predicted))
  118. {
  119. fifo->exp_len = exp_len;
  120. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  121. }
  122. STARPU_ASSERT(!isnan(fifo->exp_end));
  123. STARPU_ASSERT(!isnan(fifo->exp_len));
  124. STARPU_ASSERT(!isnan(fifo->exp_start));
  125. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  126. if(!is_pushback)
  127. component->can_pull(component);
  128. }
  129. return ret;
  130. }
  131. static int fifo_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  132. {
  133. return fifo_push_local_task(component, task, 0);
  134. }
  135. static struct starpu_task * fifo_pull_task(struct starpu_sched_component * component)
  136. {
  137. STARPU_ASSERT(component && component->data);
  138. struct _starpu_fifo_data * data = component->data;
  139. struct _starpu_fifo_taskq * fifo = data->fifo;
  140. starpu_pthread_mutex_t * mutex = &data->mutex;
  141. const double now = starpu_timing_now();
  142. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  143. struct starpu_task * task = _starpu_fifo_pop_task(fifo, starpu_worker_get_id_check());
  144. if(task)
  145. {
  146. if(!isnan(task->predicted))
  147. {
  148. fifo->exp_start = now + task->predicted;
  149. fifo->exp_len -= task->predicted;
  150. }
  151. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  152. if(fifo->ntasks == 0)
  153. fifo->exp_len = 0.0;
  154. }
  155. STARPU_ASSERT(!isnan(fifo->exp_end));
  156. STARPU_ASSERT(!isnan(fifo->exp_len));
  157. STARPU_ASSERT(!isnan(fifo->exp_start));
  158. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  159. // When a pop is called, a can_push is called for pushing tasks onto
  160. // the empty place of the queue left by the popped task.
  161. starpu_sched_component_send_can_push_to_parents(component);
  162. if(task)
  163. return task;
  164. return NULL;
  165. }
  166. /* When a can_push is caught by this function, we try to pop and push
  167. * tasks from our local queue as much as possible, until a
  168. * push fails, which means that the worker fifo_components are
  169. * currently "full".
  170. */
  171. static int fifo_can_push(struct starpu_sched_component * component)
  172. {
  173. STARPU_ASSERT(component && starpu_sched_component_is_fifo(component));
  174. int res = 0;
  175. struct starpu_task * task;
  176. task = starpu_sched_component_pump_downstream(component, &res);
  177. if(task)
  178. {
  179. int ret = fifo_push_local_task(component,task,1);
  180. STARPU_ASSERT(!ret);
  181. }
  182. return res;
  183. }
  184. int starpu_sched_component_is_fifo(struct starpu_sched_component * component)
  185. {
  186. return component->push_task == fifo_push_task;
  187. }
  188. struct starpu_sched_component * starpu_sched_component_fifo_create(struct starpu_sched_tree *tree, struct starpu_sched_component_fifo_data * params)
  189. {
  190. struct starpu_sched_component *component = starpu_sched_component_create(tree, "fifo");
  191. struct _starpu_fifo_data *data;
  192. _STARPU_MALLOC(data, sizeof(*data));
  193. data->fifo = _starpu_create_fifo();
  194. STARPU_PTHREAD_MUTEX_INIT(&data->mutex,NULL);
  195. component->data = data;
  196. component->estimated_end = fifo_estimated_end;
  197. component->estimated_load = fifo_estimated_load;
  198. component->push_task = fifo_push_task;
  199. component->pull_task = fifo_pull_task;
  200. component->can_push = fifo_can_push;
  201. component->deinit_data = fifo_component_deinit_data;
  202. if(params)
  203. {
  204. data->ntasks_threshold=params->ntasks_threshold;
  205. data->exp_len_threshold=params->exp_len_threshold;
  206. }
  207. else
  208. {
  209. data->ntasks_threshold=0;
  210. data->exp_len_threshold=0.0;
  211. }
  212. return component;
  213. }