component_fifo.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 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_PTHREAD_MUTEX_LOCK(mutex);
  62. load += fifo->ntasks / relative_speedup;
  63. STARPU_PTHREAD_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_PTHREAD_MUTEX_LOCK(mutex);
  76. load += fifo->ntasks / relative_speedup;
  77. STARPU_PTHREAD_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_PTHREAD_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_PTHREAD_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. if(!is_pushback)
  126. component->can_pull(component);
  127. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  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. STARPU_PTHREAD_MUTEX_LOCK(mutex);
  142. struct starpu_task * task = _starpu_fifo_pop_task(fifo, starpu_worker_get_id_check());
  143. if(task)
  144. {
  145. if(!isnan(task->predicted))
  146. {
  147. fifo->exp_start = starpu_timing_now() + task->predicted;
  148. fifo->exp_len -= task->predicted;
  149. }
  150. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  151. if(fifo->ntasks == 0)
  152. fifo->exp_len = 0.0;
  153. }
  154. STARPU_ASSERT(!isnan(fifo->exp_end));
  155. STARPU_ASSERT(!isnan(fifo->exp_len));
  156. STARPU_ASSERT(!isnan(fifo->exp_start));
  157. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  158. // When a pop is called, a can_push is called for pushing tasks onto
  159. // the empty place of the queue left by the popped task.
  160. int i,ret;
  161. for(i=0; i < component->nparents; i++)
  162. {
  163. if(component->parents[i] == NULL)
  164. continue;
  165. else
  166. {
  167. ret = component->parents[i]->can_push(component->parents[i]);
  168. if(ret)
  169. break;
  170. }
  171. }
  172. if(task)
  173. return task;
  174. return NULL;
  175. }
  176. /* When a can_push is caught by this function, we try to pop and push
  177. * tasks from our local queue as much as possible, until a
  178. * push fails, which means that the worker fifo_components are
  179. * currently "full".
  180. */
  181. static int fifo_can_push(struct starpu_sched_component * component)
  182. {
  183. STARPU_ASSERT(component && starpu_sched_component_is_fifo(component));
  184. int ret = 0;
  185. int res = 0;
  186. STARPU_ASSERT(component->nchildren == 1);
  187. struct starpu_sched_component * child = component->children[0];
  188. struct starpu_task * task = NULL;
  189. while (1)
  190. {
  191. task = starpu_sched_component_pull_task(component,component);
  192. if (!task)
  193. break;
  194. ret = starpu_sched_component_push_task(component,child,task);
  195. if (ret)
  196. break;
  197. res = 1;
  198. }
  199. if(task && ret)
  200. fifo_push_local_task(component,task,1);
  201. return res;
  202. }
  203. int starpu_sched_component_is_fifo(struct starpu_sched_component * component)
  204. {
  205. return component->push_task == fifo_push_task;
  206. }
  207. struct starpu_sched_component * starpu_sched_component_fifo_create(struct starpu_sched_tree *tree, struct starpu_sched_component_fifo_data * params)
  208. {
  209. struct starpu_sched_component *component = starpu_sched_component_create(tree, "fifo");
  210. struct _starpu_fifo_data *data;
  211. _STARPU_MALLOC(data, sizeof(*data));
  212. data->fifo = _starpu_create_fifo();
  213. STARPU_PTHREAD_MUTEX_INIT(&data->mutex,NULL);
  214. component->data = data;
  215. component->estimated_end = fifo_estimated_end;
  216. component->estimated_load = fifo_estimated_load;
  217. component->push_task = fifo_push_task;
  218. component->pull_task = fifo_pull_task;
  219. component->can_push = fifo_can_push;
  220. component->deinit_data = fifo_component_deinit_data;
  221. if(params)
  222. {
  223. data->ntasks_threshold=params->ntasks_threshold;
  224. data->exp_len_threshold=params->exp_len_threshold;
  225. }
  226. else
  227. {
  228. data->ntasks_threshold=0;
  229. data->exp_len_threshold=0.0;
  230. }
  231. return component;
  232. }