work_stealing_policy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011, 2012 INRIA
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /* Work stealing policy */
  19. #include <float.h>
  20. #include <core/workers.h>
  21. #include <sched_policies/deque_queues.h>
  22. typedef struct{
  23. struct _starpu_deque_jobq **queue_array;
  24. unsigned rr_worker;
  25. /* keep track of the work performed from the beginning of the algorithm to make
  26. * better decisions about which queue to select when stealing or deferring work
  27. */
  28. unsigned performed_total;
  29. pthread_mutex_t sched_mutex;
  30. pthread_cond_t sched_cond;
  31. unsigned last_pop_worker;
  32. unsigned last_push_worker;
  33. } work_stealing_data;
  34. #ifdef USE_OVERLOAD
  35. /**
  36. * Minimum number of task we wait for being processed before we start assuming
  37. * on which worker the computation would be faster.
  38. */
  39. static int calibration_value = 0;
  40. #endif /* USE_OVERLOAD */
  41. /**
  42. * Return a worker from which a task can be stolen.
  43. * Selecting a worker is done in a round-robin fashion, unless
  44. * the worker previously selected doesn't own any task,
  45. * then we return the first non-empty worker.
  46. */
  47. static unsigned select_victim_round_robin(unsigned sched_ctx_id)
  48. {
  49. work_stealing_data *ws = (work_stealing_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  50. unsigned worker = ws->last_pop_worker;
  51. unsigned nworkers = starpu_get_nworkers_of_sched_ctx(sched_ctx_id);
  52. /* If the worker's queue is empty, let's try
  53. * the next ones */
  54. while (!ws->queue_array[worker]->njobs)
  55. {
  56. worker = (worker + 1) % nworkers;
  57. if (worker == ws->last_pop_worker)
  58. {
  59. /* We got back to the first worker,
  60. * don't go in infinite loop */
  61. break;
  62. }
  63. }
  64. ws->last_pop_worker = (worker + 1) % nworkers;
  65. return worker;
  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(unsigned sched_ctx_id)
  72. {
  73. work_stealing_data *ws = (work_stealing_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  74. unsigned worker = ws->last_push_worker;
  75. unsigned nworkers = starpu_get_nworkers_of_sched_ctx(sched_ctx_id);
  76. ws->last_push_worker = (ws->last_push_worker + 1) % nworkers;
  77. return worker;
  78. }
  79. #ifdef USE_OVERLOAD
  80. /**
  81. * Return a ratio helpful to determine whether a worker is suitable to steal
  82. * tasks from or to put some tasks in its queue.
  83. *
  84. * \return a ratio with a positive or negative value, describing the current state of the worker :
  85. * a smaller value implies a faster worker with an relatively emptier queue : more suitable to put tasks in
  86. * a bigger value implies a slower worker with an reletively more replete queue : more suitable to steal tasks from
  87. */
  88. static float overload_metric(unsigned sched_ctx_id, unsigned id)
  89. {
  90. work_stealing_data *ws = (work_stealing_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  91. float execution_ratio = 0.0f;
  92. float current_ratio = 0.0f;
  93. int nprocessed = _starpu_get_deque_nprocessed(ws->queue_array[id]);
  94. unsigned njobs = _starpu_get_deque_njobs(ws->queue_array[id]);
  95. /* Did we get enough information ? */
  96. if (performed_total > 0 && nprocessed > 0)
  97. {
  98. /* How fast or slow is the worker compared to the other workers */
  99. execution_ratio = (float) nprocessed / performed_total;
  100. /* How replete is its queue */
  101. current_ratio = (float) njobs / nprocessed;
  102. }
  103. else
  104. {
  105. return 0.0f;
  106. }
  107. return (current_ratio - execution_ratio);
  108. }
  109. /**
  110. * Return the most suitable worker from which a task can be stolen.
  111. * The number of previously processed tasks, total and local,
  112. * and the number of tasks currently awaiting to be processed
  113. * by the tasks are taken into account to select the most suitable
  114. * worker to steal task from.
  115. */
  116. static unsigned select_victim_overload(unsigned sched_ctx_id)
  117. {
  118. unsigned worker;
  119. float worker_ratio;
  120. unsigned best_worker = 0;
  121. float best_ratio = FLT_MIN;
  122. /* Don't try to play smart until we get
  123. * enough informations. */
  124. if (performed_total < calibration_value)
  125. return select_victim_round_robin(sched_ctx_id);
  126. struct worker_collection *workers = starpu_get_worker_collection_of_sched_ctx(sched_ctx_id);
  127. if(workers->init_cursor)
  128. workers->init_cursor(workers);
  129. while(workers->has_next(workers))
  130. {
  131. worker = workers->get_next(workers);
  132. worker_ratio = overload_metric(sched_ctx_id, worker);
  133. if (worker_ratio > best_ratio)
  134. {
  135. best_worker = worker;
  136. best_ratio = worker_ratio;
  137. }
  138. }
  139. if(workers->init_cursor)
  140. workers->deinit_cursor(workers);
  141. return best_worker;
  142. }
  143. /**
  144. * Return the most suitable worker to whom add a task.
  145. * The number of previously processed tasks, total and local,
  146. * and the number of tasks currently awaiting to be processed
  147. * by the tasks are taken into account to select the most suitable
  148. * worker to add a task to.
  149. */
  150. static unsigned select_worker_overload(unsigned sched_ctx_id)
  151. {
  152. unsigned worker;
  153. float worker_ratio;
  154. unsigned best_worker = 0;
  155. float best_ratio = FLT_MAX;
  156. /* Don't try to play smart until we get
  157. * enough informations. */
  158. if (performed_total < calibration_value)
  159. return select_worker_round_robin(sched_ctx_id);
  160. struct worker_collection *workers = starpu_get_worker_collection_of_sched_ctx(sched_ctx_id);
  161. if(workers->init_cursor)
  162. workers->init_cursor(workers);
  163. while(workers->has_next(workers))
  164. {
  165. worker = workers->get_next(workers);
  166. worker_ratio = overload_metric(sched_ctx_id, worker);
  167. if (worker_ratio < best_ratio)
  168. {
  169. best_worker = worker;
  170. best_ratio = worker_ratio;
  171. }
  172. }
  173. if(workers->init_cursor)
  174. workers->deinit_cursor(workers);
  175. return best_worker;
  176. }
  177. #endif /* USE_OVERLOAD */
  178. /**
  179. * Return a worker from which a task can be stolen.
  180. * This is a phony function used to call the right
  181. * function depending on the value of USE_OVERLOAD.
  182. */
  183. static inline unsigned select_victim(unsigned sched_ctx_id)
  184. {
  185. #ifdef USE_OVERLOAD
  186. return select_victim_overload(sched_ctx_id);
  187. #else
  188. return select_victim_round_robin(sched_ctx_id);
  189. #endif /* USE_OVERLOAD */
  190. }
  191. /**
  192. * Return a worker from which a task can be stolen.
  193. * This is a phony function used to call the right
  194. * function depending on the value of USE_OVERLOAD.
  195. */
  196. static inline unsigned select_worker(unsigned sched_ctx_id)
  197. {
  198. #ifdef USE_OVERLOAD
  199. return select_worker_overload(sched_ctx_id);
  200. #else
  201. return select_worker_round_robin(sched_ctx_id);
  202. #endif /* USE_OVERLOAD */
  203. }
  204. #ifdef STARPU_DEVEL
  205. #warning TODO rewrite ... this will not scale at all now
  206. #endif
  207. static struct starpu_task *ws_pop_task(unsigned sched_ctx_id)
  208. {
  209. work_stealing_data *ws = (work_stealing_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  210. struct starpu_task *task;
  211. struct _starpu_deque_jobq *q;
  212. int workerid = starpu_worker_get_id();
  213. STARPU_ASSERT(workerid != -1);
  214. q = ws->queue_array[workerid];
  215. task = _starpu_deque_pop_task(q, workerid);
  216. if (task)
  217. {
  218. /* there was a local task */
  219. ws->performed_total++;
  220. q->nprocessed++;
  221. q->njobs--;
  222. return task;
  223. }
  224. /* we need to steal someone's job */
  225. unsigned victim = select_victim(sched_ctx_id);
  226. struct _starpu_deque_jobq *victimq = ws->queue_array[victim];
  227. task = _starpu_deque_pop_task(victimq, workerid);
  228. if (task)
  229. {
  230. _STARPU_TRACE_WORK_STEALING(q, workerid);
  231. ws->performed_total++;
  232. /* Beware : we have to increase the number of processed tasks of
  233. * the stealer, not the victim ! */
  234. q->nprocessed++;
  235. victimq->njobs--;
  236. }
  237. return task;
  238. }
  239. int ws_push_task(struct starpu_task *task)
  240. {
  241. unsigned sched_ctx_id = task->sched_ctx;
  242. work_stealing_data *ws = (work_stealing_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  243. struct _starpu_deque_jobq *deque_queue;
  244. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  245. int workerid = starpu_worker_get_id();
  246. pthread_mutex_t *changing_ctx_mutex = starpu_get_changing_ctx_mutex(sched_ctx_id);
  247. unsigned nworkers;
  248. int ret_val = -1;
  249. /* if the context has no workers return */
  250. _STARPU_PTHREAD_MUTEX_LOCK(changing_ctx_mutex);
  251. nworkers = starpu_get_nworkers_of_sched_ctx(sched_ctx_id);
  252. if(nworkers == 0)
  253. {
  254. _STARPU_PTHREAD_MUTEX_UNLOCK(changing_ctx_mutex);
  255. return ret_val;
  256. }
  257. _STARPU_PTHREAD_MUTEX_LOCK(&ws->sched_mutex);
  258. /* If the current thread is not a worker but
  259. * the main thread (-1), we find the better one to
  260. * put task on its queue */
  261. if (workerid == -1)
  262. workerid = select_worker(sched_ctx_id);
  263. deque_queue = ws->queue_array[workerid];
  264. _STARPU_TRACE_JOB_PUSH(task, 0);
  265. _starpu_job_list_push_back(deque_queue->jobq, j);
  266. deque_queue->njobs++;
  267. _STARPU_PTHREAD_COND_SIGNAL(&ws->sched_cond);
  268. _STARPU_PTHREAD_MUTEX_UNLOCK(&ws->sched_mutex);
  269. _STARPU_PTHREAD_MUTEX_UNLOCK(changing_ctx_mutex);
  270. return 0;
  271. }
  272. static void ws_add_workers(unsigned sched_ctx_id, int *workerids,unsigned nworkers)
  273. {
  274. work_stealing_data *ws = (work_stealing_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  275. unsigned i;
  276. int workerid;
  277. for (i = 0; i < nworkers; i++)
  278. {
  279. workerid = workerids[i];
  280. ws->queue_array[workerid] = _starpu_create_deque();
  281. /**
  282. * The first WS_POP_TASK will increase NPROCESSED though no task was actually performed yet,
  283. * we need to initialize it at -1.
  284. */
  285. ws->queue_array[workerid]->nprocessed = -1;
  286. ws->queue_array[workerid]->njobs = 0;
  287. starpu_worker_set_sched_condition(sched_ctx_id, workerid, &ws->sched_mutex, &ws->sched_cond);
  288. }
  289. }
  290. static void ws_remove_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  291. {
  292. work_stealing_data *ws = (work_stealing_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  293. unsigned i;
  294. int workerid;
  295. for (i = 0; i < nworkers; i++)
  296. {
  297. workerid = workerids[i];
  298. _starpu_destroy_deque(ws->queue_array[workerid]);
  299. starpu_worker_set_sched_condition(sched_ctx_id, workerid, NULL, NULL);
  300. }
  301. }
  302. static void initialize_ws_policy(unsigned sched_ctx_id)
  303. {
  304. starpu_create_worker_collection_for_sched_ctx(sched_ctx_id, WORKER_LIST);
  305. work_stealing_data *ws = (work_stealing_data*)malloc(sizeof(work_stealing_data));
  306. starpu_set_sched_ctx_policy_data(sched_ctx_id, (void*)ws);
  307. ws->last_pop_worker = 0;
  308. ws->last_push_worker = 0;
  309. /**
  310. * The first WS_POP_TASK will increase PERFORMED_TOTAL though no task was actually performed yet,
  311. * we need to initialize it at -1.
  312. */
  313. ws->performed_total = -1;
  314. ws->queue_array = (struct _starpu_deque_jobq**)malloc(STARPU_NMAXWORKERS*sizeof(struct _starpu_deque_jobq*));
  315. _STARPU_PTHREAD_MUTEX_INIT(&ws->sched_mutex, NULL);
  316. _STARPU_PTHREAD_COND_INIT(&ws->sched_cond, NULL);
  317. }
  318. static void deinit_ws_policy(unsigned sched_ctx_id)
  319. {
  320. work_stealing_data *ws = (work_stealing_data*)starpu_get_sched_ctx_policy_data(sched_ctx_id);
  321. free(ws->queue_array);
  322. _STARPU_PTHREAD_MUTEX_DESTROY(&ws->sched_mutex);
  323. _STARPU_PTHREAD_COND_DESTROY(&ws->sched_cond);
  324. free(ws);
  325. starpu_delete_worker_collection_for_sched_ctx(sched_ctx_id);
  326. }
  327. struct starpu_sched_policy _starpu_sched_ws_policy =
  328. {
  329. .init_sched = initialize_ws_policy,
  330. .deinit_sched = deinit_ws_policy,
  331. .add_workers = ws_add_workers,
  332. .remove_workers = ws_remove_workers,
  333. .push_task = ws_push_task,
  334. .pop_task = ws_pop_task,
  335. .pre_exec_hook = NULL,
  336. .post_exec_hook = NULL,
  337. .pop_every_task = NULL,
  338. .policy_name = "ws",
  339. .policy_description = "work stealing"
  340. };