component_work_stealing.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 <starpu.h>
  20. #include <float.h>
  21. #include "prio_deque.h"
  22. struct _starpu_work_stealing_data
  23. {
  24. /* keep track of the work performed from the beginning of the algorithm to make
  25. * better decisions about which queue to child when stealing or deferring work
  26. */
  27. unsigned performed_total, last_pop_child, last_push_child;
  28. struct _starpu_prio_deque ** fifos;
  29. starpu_pthread_mutex_t ** mutexes;
  30. int size;
  31. };
  32. /**
  33. * steal a task in a round robin way
  34. * return NULL if none available
  35. */
  36. static struct starpu_task * steal_task_round_robin(struct starpu_sched_component *component, int workerid)
  37. {
  38. struct _starpu_work_stealing_data *wsd = component->data;
  39. unsigned i = wsd->last_pop_child;
  40. wsd->last_pop_child = (wsd->last_pop_child + 1) % component->nchildren;
  41. /* If the worker's queue have no suitable tasks, let's try
  42. * the next ones */
  43. struct starpu_task * task = NULL;
  44. while (1)
  45. {
  46. struct _starpu_prio_deque * fifo = wsd->fifos[i];
  47. STARPU_PTHREAD_MUTEX_LOCK(wsd->mutexes[i]);
  48. task = _starpu_prio_deque_deque_task_for_worker(fifo, workerid);
  49. if(task && !isnan(task->predicted))
  50. {
  51. fifo->exp_len -= task->predicted;
  52. fifo->nprocessed--;
  53. }
  54. STARPU_PTHREAD_MUTEX_UNLOCK(wsd->mutexes[i]);
  55. if(task)
  56. break;
  57. if (i == wsd->last_pop_child)
  58. {
  59. /* We got back to the first worker,
  60. * don't go in infinite loop */
  61. return NULL;
  62. }
  63. i = (i + 1) % component->nchildren;
  64. }
  65. return task;
  66. }
  67. /**
  68. * Return a worker to whom add a task.
  69. * Selecting a worker is done in a round-robin fashion.
  70. */
  71. static unsigned select_worker_round_robin(struct starpu_sched_component * component)
  72. {
  73. struct _starpu_work_stealing_data *ws = (struct _starpu_work_stealing_data*)component->data;
  74. unsigned i = (ws->last_push_child + 1) % component->nchildren ;
  75. ws->last_push_child = i;
  76. return i;
  77. }
  78. /**
  79. * Return a worker from which a task can be stolen.
  80. * This is a phony function used to call the right
  81. * function depending on the value of USE_OVERLOAD.
  82. */
  83. static inline struct starpu_task * steal_task(struct starpu_sched_component * component, int workerid)
  84. {
  85. return steal_task_round_robin(component, workerid);
  86. }
  87. /**
  88. * Return a worker from which a task can be stolen.
  89. * This is a phony function used to call the right
  90. * function depending on the value of USE_OVERLOAD.
  91. */
  92. static inline unsigned select_worker(struct starpu_sched_component * component)
  93. {
  94. return select_worker_round_robin(component);
  95. }
  96. static int is_worker_of_component(struct starpu_sched_component * component, int workerid)
  97. {
  98. return starpu_bitmap_get(component->workers, workerid);
  99. }
  100. static struct starpu_task * pull_task(struct starpu_sched_component * component)
  101. {
  102. int workerid = starpu_worker_get_id();
  103. int i;
  104. for(i = 0; i < component->nchildren; i++)
  105. {
  106. if(is_worker_of_component(component->children[i], workerid))
  107. break;
  108. }
  109. STARPU_ASSERT(i < component->nchildren);
  110. struct _starpu_work_stealing_data * wsd = component->data;
  111. STARPU_PTHREAD_MUTEX_LOCK(wsd->mutexes[i]);
  112. struct starpu_task * task = _starpu_prio_deque_pop_task(wsd->fifos[i]);
  113. if(task)
  114. {
  115. if(!isnan(task->predicted))
  116. {
  117. wsd->fifos[i]->exp_len -= task->predicted;
  118. wsd->fifos[i]->exp_start = starpu_timing_now() + task->predicted;
  119. }
  120. }
  121. else
  122. wsd->fifos[i]->exp_len = 0.0;
  123. STARPU_PTHREAD_MUTEX_UNLOCK(wsd->mutexes[i]);
  124. if(task)
  125. {
  126. return task;
  127. }
  128. task = steal_task(component, workerid);
  129. if(task)
  130. {
  131. STARPU_PTHREAD_MUTEX_LOCK(wsd->mutexes[i]);
  132. wsd->fifos[i]->nprocessed++;
  133. STARPU_PTHREAD_MUTEX_UNLOCK(wsd->mutexes[i]);
  134. return task;
  135. }
  136. for(i=0; i < component->nparents; i++)
  137. {
  138. if(component->parents[i] == NULL)
  139. continue;
  140. else
  141. {
  142. task = component->parents[i]->pull_task(component->parents[i]);
  143. if(task)
  144. break;
  145. }
  146. }
  147. if(task)
  148. return task;
  149. else
  150. return NULL;
  151. }
  152. double _ws_estimated_end(struct starpu_sched_component * component)
  153. {
  154. STARPU_ASSERT(starpu_sched_component_is_work_stealing(component));
  155. struct _starpu_work_stealing_data * wsd = component->data;
  156. double sum_len = 0.0;
  157. double sum_start = 0.0;
  158. int i;
  159. for(i = 0; i < component->nchildren; i++)
  160. {
  161. STARPU_PTHREAD_MUTEX_LOCK(wsd->mutexes[i]);
  162. sum_len += wsd->fifos[i]->exp_len;
  163. wsd->fifos[i]->exp_start = STARPU_MAX(starpu_timing_now(), wsd->fifos[i]->exp_start);
  164. sum_start += wsd->fifos[i]->exp_start;
  165. STARPU_PTHREAD_MUTEX_UNLOCK(wsd->mutexes[i]);
  166. }
  167. int nb_workers = starpu_bitmap_cardinal(component->workers_in_ctx);
  168. return (sum_start + sum_len) / nb_workers;
  169. }
  170. double _ws_estimated_load(struct starpu_sched_component * component)
  171. {
  172. STARPU_ASSERT(starpu_sched_component_is_work_stealing(component));
  173. struct _starpu_work_stealing_data * wsd = component->data;
  174. int ntasks = 0;
  175. int i;
  176. for(i = 0; i < component->nchildren; i++)
  177. {
  178. STARPU_PTHREAD_MUTEX_LOCK(wsd->mutexes[i]);
  179. ntasks += wsd->fifos[i]->ntasks;
  180. STARPU_PTHREAD_MUTEX_UNLOCK(wsd->mutexes[i]);
  181. }
  182. double speedup = 0.0;
  183. int workerid;
  184. for(workerid = starpu_bitmap_first(component->workers_in_ctx);
  185. -1 != workerid;
  186. workerid = starpu_bitmap_next(component->workers_in_ctx, workerid))
  187. {
  188. speedup += starpu_worker_get_relative_speedup(starpu_worker_get_perf_archtype(workerid));
  189. }
  190. return ntasks / speedup;
  191. }
  192. static int push_task(struct starpu_sched_component * component, struct starpu_task * task)
  193. {
  194. struct _starpu_work_stealing_data * wsd = component->data;
  195. int ret = -1;
  196. int i = wsd->last_push_child;
  197. i = (i+1)%component->nchildren;
  198. STARPU_PTHREAD_MUTEX_LOCK(wsd->mutexes[i]);
  199. ret = _starpu_prio_deque_push_task(wsd->fifos[i], task);
  200. STARPU_PTHREAD_MUTEX_UNLOCK(wsd->mutexes[i]);
  201. wsd->last_push_child = i;
  202. component->can_pull(component);
  203. return ret;
  204. }
  205. //this function is special, when a worker call it, we want to push the task in his fifo
  206. int starpu_sched_tree_work_stealing_push_task(struct starpu_task *task)
  207. {
  208. int workerid = starpu_worker_get_id();
  209. if(workerid == -1)
  210. return starpu_sched_tree_push_task(task);
  211. unsigned sched_ctx_id = task->sched_ctx;
  212. struct starpu_sched_component * component =starpu_sched_component_worker_get(workerid);
  213. while(component->parents[sched_ctx_id] != NULL)
  214. {
  215. component = component->parents[sched_ctx_id];
  216. if(starpu_sched_component_is_work_stealing(component))
  217. {
  218. if(!starpu_sched_component_can_execute_task(component, task))
  219. return starpu_sched_tree_push_task(task);
  220. int i;
  221. for(i = 0; i < component->nchildren; i++)
  222. if(is_worker_of_component(component->children[i], workerid))
  223. break;
  224. STARPU_ASSERT(i < component->nchildren);
  225. struct _starpu_work_stealing_data * wsd = component->data;
  226. STARPU_PTHREAD_MUTEX_LOCK(wsd->mutexes[i]);
  227. int ret = _starpu_prio_deque_push_task(wsd->fifos[i] , task);
  228. if(ret == 0 && !isnan(task->predicted))
  229. wsd->fifos[i]->exp_len += task->predicted;
  230. STARPU_PTHREAD_MUTEX_UNLOCK(wsd->mutexes[i]);
  231. //we need to wake all workers
  232. component->can_pull(component);
  233. return ret;
  234. }
  235. }
  236. /* this should not be reached */
  237. return starpu_sched_tree_push_task(task);
  238. }
  239. void _ws_add_child(struct starpu_sched_component * component, struct starpu_sched_component * child)
  240. {
  241. struct _starpu_work_stealing_data * wsd = component->data;
  242. component->add_child(component, child);
  243. if(wsd->size < component->nchildren)
  244. {
  245. STARPU_ASSERT(wsd->size == component->nchildren - 1);
  246. wsd->fifos = realloc(wsd->fifos, component->nchildren * sizeof(*wsd->fifos));
  247. wsd->mutexes = realloc(wsd->mutexes, component->nchildren * sizeof(*wsd->mutexes));
  248. wsd->size = component->nchildren;
  249. }
  250. struct _starpu_prio_deque * fifo = malloc(sizeof(*fifo));
  251. _starpu_prio_deque_init(fifo);
  252. wsd->fifos[component->nchildren - 1] = fifo;
  253. starpu_pthread_mutex_t * mutex = malloc(sizeof(*mutex));
  254. STARPU_PTHREAD_MUTEX_INIT(mutex,NULL);
  255. wsd->mutexes[component->nchildren - 1] = mutex;
  256. }
  257. void _ws_remove_child(struct starpu_sched_component * component, struct starpu_sched_component * child)
  258. {
  259. struct _starpu_work_stealing_data * wsd = component->data;
  260. STARPU_PTHREAD_MUTEX_DESTROY(wsd->mutexes[component->nchildren - 1]);
  261. free(wsd->mutexes[component->nchildren - 1]);
  262. int i_component;
  263. for(i_component = 0; i_component < component->nchildren; i_component++)
  264. {
  265. if(component->children[i_component] == child)
  266. break;
  267. }
  268. STARPU_ASSERT(i_component != component->nchildren);
  269. struct _starpu_prio_deque * tmp_fifo = wsd->fifos[i_component];
  270. wsd->fifos[i_component] = wsd->fifos[component->nchildren - 1];
  271. component->children[i_component] = component->children[component->nchildren - 1];
  272. component->nchildren--;
  273. struct starpu_task * task;
  274. while((task = _starpu_prio_deque_pop_task(tmp_fifo)))
  275. {
  276. component->push_task(component, task);
  277. }
  278. _starpu_prio_deque_destroy(tmp_fifo);
  279. free(tmp_fifo);
  280. }
  281. void _work_stealing_component_deinit_data(struct starpu_sched_component * component)
  282. {
  283. free(component->data);
  284. }
  285. int starpu_sched_component_is_work_stealing(struct starpu_sched_component * component)
  286. {
  287. return component->push_task == push_task;
  288. }
  289. struct starpu_sched_component * starpu_sched_component_work_stealing_create(void * arg STARPU_ATTRIBUTE_UNUSED)
  290. {
  291. struct starpu_sched_component * component = starpu_sched_component_create();
  292. struct _starpu_work_stealing_data * wsd = malloc(sizeof(*wsd));
  293. memset(wsd, 0, sizeof(*wsd));
  294. component->pull_task = pull_task;
  295. component->push_task = push_task;
  296. component->add_child = _ws_add_child;
  297. component->remove_child = _ws_remove_child;
  298. component->estimated_end = _ws_estimated_end;
  299. component->estimated_load = _ws_estimated_load;
  300. component->deinit_data = _work_stealing_component_deinit_data;
  301. component->data = wsd;
  302. return component;
  303. }