work_stealing_policy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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. #include <core/debug.h>
  23. #ifdef HAVE_AYUDAME_H
  24. #include <Ayudame.h>
  25. #endif
  26. struct _starpu_work_stealing_data
  27. {
  28. struct _starpu_deque_jobq **queue_array;
  29. unsigned rr_worker;
  30. /* keep track of the work performed from the beginning of the algorithm to make
  31. * better decisions about which queue to select when stealing or deferring work
  32. */
  33. unsigned performed_total;
  34. unsigned last_pop_worker;
  35. unsigned last_push_worker;
  36. };
  37. #ifdef USE_OVERLOAD
  38. /**
  39. * Minimum number of task we wait for being processed before we start assuming
  40. * on which worker the computation would be faster.
  41. */
  42. static int calibration_value = 0;
  43. #endif /* USE_OVERLOAD */
  44. /**
  45. * Return a worker from which a task can be stolen.
  46. * Selecting a worker is done in a round-robin fashion, unless
  47. * the worker previously selected doesn't own any task,
  48. * then we return the first non-empty worker.
  49. */
  50. static unsigned select_victim_round_robin(unsigned sched_ctx_id)
  51. {
  52. struct _starpu_work_stealing_data *ws = (struct _starpu_work_stealing_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  53. unsigned worker = ws->last_pop_worker;
  54. unsigned nworkers = starpu_sched_ctx_get_nworkers(sched_ctx_id);
  55. starpu_pthread_mutex_t *victim_sched_mutex;
  56. starpu_pthread_cond_t *victim_sched_cond;
  57. /* If the worker's queue is empty, let's try
  58. * the next ones */
  59. while (1)
  60. {
  61. unsigned njobs;
  62. starpu_worker_get_sched_condition(worker, &victim_sched_mutex, &victim_sched_cond);
  63. /* Here helgrind would shout that this is unprotected, but we
  64. * are fine with getting outdated values, this is just an
  65. * estimation */
  66. njobs = ws->queue_array[worker]->njobs;
  67. if (njobs)
  68. break;
  69. worker = (worker + 1) % nworkers;
  70. if (worker == ws->last_pop_worker)
  71. {
  72. /* We got back to the first worker,
  73. * don't go in infinite loop */
  74. break;
  75. }
  76. }
  77. ws->last_pop_worker = (worker + 1) % nworkers;
  78. return worker;
  79. }
  80. /**
  81. * Return a worker to whom add a task.
  82. * Selecting a worker is done in a round-robin fashion.
  83. */
  84. static unsigned select_worker_round_robin(unsigned sched_ctx_id)
  85. {
  86. struct _starpu_work_stealing_data *ws = (struct _starpu_work_stealing_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  87. unsigned worker = ws->last_push_worker;
  88. unsigned nworkers = starpu_sched_ctx_get_nworkers(sched_ctx_id);
  89. ws->last_push_worker = (ws->last_push_worker + 1) % nworkers;
  90. return worker;
  91. }
  92. #ifdef USE_OVERLOAD
  93. /**
  94. * Return a ratio helpful to determine whether a worker is suitable to steal
  95. * tasks from or to put some tasks in its queue.
  96. *
  97. * \return a ratio with a positive or negative value, describing the current state of the worker :
  98. * a smaller value implies a faster worker with an relatively emptier queue : more suitable to put tasks in
  99. * a bigger value implies a slower worker with an reletively more replete queue : more suitable to steal tasks from
  100. */
  101. static float overload_metric(unsigned sched_ctx_id, unsigned id)
  102. {
  103. struct _starpu_work_stealing_data *ws = (struct _starpu_work_stealing_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  104. float execution_ratio = 0.0f;
  105. float current_ratio = 0.0f;
  106. int nprocessed = _starpu_get_deque_nprocessed(ws->queue_array[id]);
  107. unsigned njobs = _starpu_get_deque_njobs(ws->queue_array[id]);
  108. /* Did we get enough information ? */
  109. if (performed_total > 0 && nprocessed > 0)
  110. {
  111. /* How fast or slow is the worker compared to the other workers */
  112. execution_ratio = (float) nprocessed / performed_total;
  113. /* How replete is its queue */
  114. current_ratio = (float) njobs / nprocessed;
  115. }
  116. else
  117. {
  118. return 0.0f;
  119. }
  120. return (current_ratio - execution_ratio);
  121. }
  122. /**
  123. * Return the most suitable worker from which a task can be stolen.
  124. * The number of previously processed tasks, total and local,
  125. * and the number of tasks currently awaiting to be processed
  126. * by the tasks are taken into account to select the most suitable
  127. * worker to steal task from.
  128. */
  129. static unsigned select_victim_overload(unsigned sched_ctx_id)
  130. {
  131. unsigned worker;
  132. float worker_ratio;
  133. unsigned best_worker = 0;
  134. float best_ratio = FLT_MIN;
  135. /* Don't try to play smart until we get
  136. * enough informations. */
  137. if (performed_total < calibration_value)
  138. return select_victim_round_robin(sched_ctx_id);
  139. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  140. struct starpu_sched_ctx_iterator it;
  141. if(workers->init_iterator)
  142. workers->init_iterator(workers, &it);
  143. while(workers->has_next(workers, &it))
  144. {
  145. worker = workers->get_next(workers, &it);
  146. worker_ratio = overload_metric(sched_ctx_id, worker);
  147. if (worker_ratio > best_ratio)
  148. {
  149. best_worker = worker;
  150. best_ratio = worker_ratio;
  151. }
  152. }
  153. return best_worker;
  154. }
  155. /**
  156. * Return the most suitable worker to whom add a task.
  157. * The number of previously processed tasks, total and local,
  158. * and the number of tasks currently awaiting to be processed
  159. * by the tasks are taken into account to select the most suitable
  160. * worker to add a task to.
  161. */
  162. static unsigned select_worker_overload(unsigned sched_ctx_id)
  163. {
  164. unsigned worker;
  165. float worker_ratio;
  166. unsigned best_worker = 0;
  167. float best_ratio = FLT_MAX;
  168. /* Don't try to play smart until we get
  169. * enough informations. */
  170. if (performed_total < calibration_value)
  171. return select_worker_round_robin(sched_ctx_id);
  172. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  173. struct starpu_sched_ctx_iterator it;
  174. if(workers->init_iterator)
  175. workers->init_iterator(workers, &it);
  176. while(workers->has_next(workers, &it))
  177. {
  178. worker = workers->get_next(workers, &it);
  179. worker_ratio = overload_metric(sched_ctx_id, worker);
  180. if (worker_ratio < best_ratio)
  181. {
  182. best_worker = worker;
  183. best_ratio = worker_ratio;
  184. }
  185. }
  186. return best_worker;
  187. }
  188. #endif /* USE_OVERLOAD */
  189. /**
  190. * Return a worker from which a task can be stolen.
  191. * This is a phony function used to call the right
  192. * function depending on the value of USE_OVERLOAD.
  193. */
  194. static inline unsigned select_victim(unsigned sched_ctx_id)
  195. {
  196. #ifdef USE_OVERLOAD
  197. return select_victim_overload(sched_ctx_id);
  198. #else
  199. return select_victim_round_robin(sched_ctx_id);
  200. #endif /* USE_OVERLOAD */
  201. }
  202. /**
  203. * Return a worker from which a task can be stolen.
  204. * This is a phony function used to call the right
  205. * function depending on the value of USE_OVERLOAD.
  206. */
  207. static inline unsigned select_worker(unsigned sched_ctx_id)
  208. {
  209. #ifdef USE_OVERLOAD
  210. return select_worker_overload(sched_ctx_id);
  211. #else
  212. return select_worker_round_robin(sched_ctx_id);
  213. #endif /* USE_OVERLOAD */
  214. }
  215. #ifdef STARPU_DEVEL
  216. #warning TODO rewrite ... this will not scale at all now
  217. #endif
  218. static struct starpu_task *ws_pop_task(unsigned sched_ctx_id)
  219. {
  220. struct _starpu_work_stealing_data *ws = (struct _starpu_work_stealing_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  221. struct starpu_task *task;
  222. struct _starpu_deque_jobq *q;
  223. int workerid = starpu_worker_get_id();
  224. STARPU_ASSERT(workerid != -1);
  225. q = ws->queue_array[workerid];
  226. task = _starpu_deque_pop_task(q, workerid);
  227. if (task)
  228. {
  229. /* there was a local task */
  230. ws->performed_total++;
  231. q->nprocessed++;
  232. q->njobs--;
  233. return task;
  234. }
  235. starpu_pthread_mutex_t *worker_sched_mutex;
  236. starpu_pthread_cond_t *worker_sched_cond;
  237. starpu_worker_get_sched_condition(workerid, &worker_sched_mutex, &worker_sched_cond);
  238. /* Note: Releasing this mutex before taking the victim mutex, to avoid interlock*/
  239. STARPU_PTHREAD_MUTEX_UNLOCK(worker_sched_mutex);
  240. /* we need to steal someone's job */
  241. unsigned victim = select_victim(sched_ctx_id);
  242. starpu_pthread_mutex_t *victim_sched_mutex;
  243. starpu_pthread_cond_t *victim_sched_cond;
  244. starpu_worker_get_sched_condition(victim, &victim_sched_mutex, &victim_sched_cond);
  245. STARPU_PTHREAD_MUTEX_LOCK(victim_sched_mutex);
  246. struct _starpu_deque_jobq *victimq = ws->queue_array[victim];
  247. task = _starpu_deque_pop_task(victimq, workerid);
  248. if (task)
  249. {
  250. _STARPU_TRACE_WORK_STEALING(q, workerid);
  251. ws->performed_total++;
  252. /* Beware : we have to increase the number of processed tasks of
  253. * the stealer, not the victim ! */
  254. q->nprocessed++;
  255. victimq->njobs--;
  256. }
  257. STARPU_PTHREAD_MUTEX_UNLOCK(victim_sched_mutex);
  258. STARPU_PTHREAD_MUTEX_LOCK(worker_sched_mutex);
  259. if(!task)
  260. {
  261. task = _starpu_deque_pop_task(q, workerid);
  262. if (task)
  263. {
  264. /* there was a local task */
  265. ws->performed_total++;
  266. q->nprocessed++;
  267. q->njobs--;
  268. return task;
  269. }
  270. }
  271. return task;
  272. }
  273. int ws_push_task(struct starpu_task *task)
  274. {
  275. unsigned sched_ctx_id = task->sched_ctx;
  276. struct _starpu_work_stealing_data *ws = (struct _starpu_work_stealing_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  277. struct _starpu_deque_jobq *deque_queue;
  278. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  279. int workerid = starpu_worker_get_id();
  280. unsigned worker = 0;
  281. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  282. struct starpu_sched_ctx_iterator it;
  283. if(workers->init_iterator)
  284. workers->init_iterator(workers, &it);
  285. while(workers->has_next(workers, &it))
  286. {
  287. worker = workers->get_next(workers, &it);
  288. starpu_pthread_mutex_t *sched_mutex;
  289. starpu_pthread_cond_t *sched_cond;
  290. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  291. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  292. }
  293. /* If the current thread is not a worker but
  294. * the main thread (-1), we find the better one to
  295. * put task on its queue */
  296. if (workerid == -1)
  297. workerid = select_worker(sched_ctx_id);
  298. deque_queue = ws->queue_array[workerid];
  299. #ifdef HAVE_AYUDAME_H
  300. if (AYU_event)
  301. {
  302. intptr_t id = workerid;
  303. AYU_event(AYU_ADDTASKTOQUEUE, j->job_id, &id);
  304. }
  305. #endif
  306. _starpu_job_list_push_back(deque_queue->jobq, j);
  307. deque_queue->njobs++;
  308. starpu_push_task_end(task);
  309. while(workers->has_next(workers, &it))
  310. {
  311. worker = workers->get_next(workers, &it);
  312. starpu_pthread_mutex_t *sched_mutex;
  313. starpu_pthread_cond_t *sched_cond;
  314. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  315. STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  316. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  317. }
  318. return 0;
  319. }
  320. static void ws_add_workers(unsigned sched_ctx_id, int *workerids,unsigned nworkers)
  321. {
  322. struct _starpu_work_stealing_data *ws = (struct _starpu_work_stealing_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  323. unsigned i;
  324. int workerid;
  325. for (i = 0; i < nworkers; i++)
  326. {
  327. workerid = workerids[i];
  328. starpu_sched_ctx_worker_shares_tasks_lists(workerid, sched_ctx_id);
  329. ws->queue_array[workerid] = _starpu_create_deque();
  330. /* Tell helgrid that we are fine with getting outdated values,
  331. * this is just an estimation */
  332. STARPU_HG_DISABLE_CHECKING(ws->queue_array[workerid]->njobs);
  333. /**
  334. * The first WS_POP_TASK will increase NPROCESSED though no task was actually performed yet,
  335. * we need to initialize it at -1.
  336. */
  337. ws->queue_array[workerid]->nprocessed = -1;
  338. ws->queue_array[workerid]->njobs = 0;
  339. }
  340. }
  341. static void ws_remove_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  342. {
  343. struct _starpu_work_stealing_data *ws = (struct _starpu_work_stealing_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  344. unsigned i;
  345. int workerid;
  346. for (i = 0; i < nworkers; i++)
  347. {
  348. workerid = workerids[i];
  349. _starpu_destroy_deque(ws->queue_array[workerid]);
  350. }
  351. }
  352. static void initialize_ws_policy(unsigned sched_ctx_id)
  353. {
  354. starpu_sched_ctx_create_worker_collection(sched_ctx_id, STARPU_WORKER_LIST);
  355. struct _starpu_work_stealing_data *ws = (struct _starpu_work_stealing_data*)malloc(sizeof(struct _starpu_work_stealing_data));
  356. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)ws);
  357. ws->last_pop_worker = 0;
  358. ws->last_push_worker = 0;
  359. /**
  360. * The first WS_POP_TASK will increase PERFORMED_TOTAL though no task was actually performed yet,
  361. * we need to initialize it at -1.
  362. */
  363. ws->performed_total = -1;
  364. ws->queue_array = (struct _starpu_deque_jobq**)malloc(STARPU_NMAXWORKERS*sizeof(struct _starpu_deque_jobq*));
  365. }
  366. static void deinit_ws_policy(unsigned sched_ctx_id)
  367. {
  368. struct _starpu_work_stealing_data *ws = (struct _starpu_work_stealing_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  369. free(ws->queue_array);
  370. free(ws);
  371. starpu_sched_ctx_delete_worker_collection(sched_ctx_id);
  372. }
  373. struct starpu_sched_policy _starpu_sched_ws_policy =
  374. {
  375. .init_sched = initialize_ws_policy,
  376. .deinit_sched = deinit_ws_policy,
  377. .add_workers = ws_add_workers,
  378. .remove_workers = ws_remove_workers,
  379. .push_task = ws_push_task,
  380. .pop_task = ws_pop_task,
  381. .pre_exec_hook = NULL,
  382. .post_exec_hook = NULL,
  383. .pop_every_task = NULL,
  384. .policy_name = "ws",
  385. .policy_description = "work stealing"
  386. };