component_fifo.c 7.7 KB

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