locality_work_stealing_policy.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 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/fifo_queues.h>
  22. #include <core/debug.h>
  23. #include <starpu_bitmap.h>
  24. struct _starpu_lws_data
  25. {
  26. struct _starpu_fifo_taskq **queue_array;
  27. int **proxlist;
  28. unsigned last_pop_worker;
  29. unsigned last_push_worker;
  30. };
  31. #ifdef STARPU_HAVE_HWLOC
  32. /* Return a worker to steal a task from. The worker is selected
  33. * according to the proximity list built using the info on te
  34. * architecture provided by hwloc */
  35. static unsigned select_victim_neighborhood(unsigned sched_ctx_id, int workerid)
  36. {
  37. struct _starpu_lws_data *ws = (struct _starpu_lws_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  38. int nworkers = starpu_sched_ctx_get_nworkers(sched_ctx_id);
  39. int i;
  40. int neighbor;
  41. for(i=0; i<nworkers; i++){
  42. neighbor = ws->proxlist[workerid][i];
  43. int ntasks = ws->queue_array[neighbor]->ntasks;
  44. if (ntasks)
  45. return neighbor;
  46. }
  47. return workerid;
  48. }
  49. #else
  50. /* Return a worker to steal a task from. The worker is selected
  51. * in a round-robin fashion */
  52. static unsigned select_victim_round_robin(unsigned sched_ctx_id)
  53. {
  54. struct _starpu_lws_data *ws = (struct _starpu_lws_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  55. unsigned worker = ws->last_pop_worker;
  56. unsigned nworkers = starpu_sched_ctx_get_nworkers(sched_ctx_id);
  57. starpu_pthread_mutex_t *victim_sched_mutex;
  58. starpu_pthread_cond_t *victim_sched_cond;
  59. /* If the worker's queue is empty, let's try
  60. * the next ones */
  61. while (1)
  62. {
  63. unsigned ntasks;
  64. starpu_worker_get_sched_condition(worker, &victim_sched_mutex, &victim_sched_cond);
  65. ntasks = ws->queue_array[worker]->ntasks;
  66. if (ntasks)
  67. break;
  68. worker = (worker + 1) % nworkers;
  69. if (worker == ws->last_pop_worker)
  70. {
  71. /* We got back to the first worker,
  72. * don't go in infinite loop */
  73. break;
  74. }
  75. }
  76. ws->last_pop_worker = (worker + 1) % nworkers;
  77. return worker;
  78. }
  79. #endif
  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_lws_data *ws = (struct _starpu_lws_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. /* TODO: use an atomic update operation for this */
  90. ws->last_push_worker = (ws->last_push_worker + 1) % nworkers;
  91. return worker;
  92. }
  93. /**
  94. * Return a worker from which a task can be stolen.
  95. */
  96. static inline unsigned select_victim(unsigned sched_ctx_id, int workerid)
  97. {
  98. #ifdef STARPU_HAVE_HWLOC
  99. return select_victim_neighborhood(sched_ctx_id, workerid);
  100. #else
  101. return select_victim_round_robin(sched_ctx_id);
  102. #endif
  103. }
  104. /**
  105. * Return a worker on whose queue a task can be pushed. This is only
  106. * needed when the push is done by the master
  107. */
  108. static inline unsigned select_worker(unsigned sched_ctx_id)
  109. {
  110. return select_worker_round_robin(sched_ctx_id);
  111. }
  112. static struct starpu_task *lws_pop_task(unsigned sched_ctx_id)
  113. {
  114. struct _starpu_lws_data *ws = (struct _starpu_lws_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  115. struct starpu_task *task = NULL;
  116. int workerid = starpu_worker_get_id();
  117. STARPU_ASSERT(workerid != -1);
  118. task = _starpu_fifo_pop_task(ws->queue_array[workerid], workerid);
  119. if (task)
  120. {
  121. /* there was a local task */
  122. /* printf("Own task!%d\n",workerid); */
  123. return task;
  124. }
  125. starpu_pthread_mutex_t *worker_sched_mutex;
  126. starpu_pthread_cond_t *worker_sched_cond;
  127. starpu_worker_get_sched_condition(workerid, &worker_sched_mutex, &worker_sched_cond);
  128. /* Note: Releasing this mutex before taking the victim mutex, to avoid interlock*/
  129. STARPU_PTHREAD_MUTEX_UNLOCK(worker_sched_mutex);
  130. /* we need to steal someone's job */
  131. unsigned victim = select_victim(sched_ctx_id, workerid);
  132. starpu_pthread_mutex_t *victim_sched_mutex;
  133. starpu_pthread_cond_t *victim_sched_cond;
  134. starpu_worker_get_sched_condition(victim, &victim_sched_mutex, &victim_sched_cond);
  135. STARPU_PTHREAD_MUTEX_LOCK(victim_sched_mutex);
  136. task = _starpu_fifo_pop_task(ws->queue_array[victim], workerid);
  137. if (task)
  138. {
  139. _STARPU_TRACE_WORK_STEALING(workerid, victim);
  140. }
  141. STARPU_PTHREAD_MUTEX_UNLOCK(victim_sched_mutex);
  142. STARPU_PTHREAD_MUTEX_LOCK(worker_sched_mutex);
  143. if(!task)
  144. {
  145. task = _starpu_fifo_pop_task(ws->queue_array[workerid], workerid);
  146. if (task)
  147. {
  148. /* there was a local task */
  149. return task;
  150. }
  151. }
  152. return task;
  153. }
  154. static int lws_push_task(struct starpu_task *task)
  155. {
  156. unsigned sched_ctx_id = task->sched_ctx;
  157. struct _starpu_lws_data *ws = (struct _starpu_lws_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  158. int workerid = starpu_worker_get_id();
  159. /* If the current thread is not a worker but
  160. * the main thread (-1), we find the better one to
  161. * put task on its queue */
  162. if (workerid == -1)
  163. workerid = select_worker(sched_ctx_id);
  164. /* int workerid = starpu_worker_get_id(); */
  165. /* print_neighborhood(sched_ctx_id, 0); */
  166. starpu_pthread_mutex_t *sched_mutex;
  167. starpu_pthread_cond_t *sched_cond;
  168. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  169. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  170. _starpu_fifo_push_task(ws->queue_array[workerid], task);
  171. starpu_push_task_end(task);
  172. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  173. #ifndef STARPU_NON_BLOCKING_DRIVERS
  174. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  175. struct starpu_sched_ctx_iterator it;
  176. unsigned worker;
  177. if(workers->init_iterator)
  178. workers->init_iterator(workers, &it);
  179. while(workers->has_next(workers, &it))
  180. {
  181. worker = workers->get_next(workers, &it);
  182. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  183. STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  184. }
  185. #endif
  186. return 0;
  187. }
  188. static void lws_add_workers(unsigned sched_ctx_id, int *workerids,unsigned nworkers)
  189. {
  190. struct _starpu_lws_data *ws = (struct _starpu_lws_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  191. unsigned i;
  192. int workerid;
  193. for (i = 0; i < nworkers; i++)
  194. {
  195. workerid = workerids[i];
  196. starpu_sched_ctx_worker_shares_tasks_lists(workerid, sched_ctx_id);
  197. ws->queue_array[workerid] = _starpu_create_fifo();
  198. /* Tell helgrid that we are fine with getting outdated values,
  199. * this is just an estimation */
  200. STARPU_HG_DISABLE_CHECKING(ws->queue_array[workerid]->ntasks);
  201. ws->queue_array[workerid]->nprocessed = 0;
  202. ws->queue_array[workerid]->ntasks = 0;
  203. }
  204. #ifdef STARPU_HAVE_HWLOC
  205. /* Build a proximity list for every worker. It is cheaper to
  206. * build this once and then use it for popping tasks rather
  207. * than traversing the hwloc tree every time a task must be
  208. * stolen */
  209. ws->proxlist = (int**)malloc(nworkers*sizeof(int*));
  210. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  211. struct starpu_tree *tree = (struct starpu_tree*)workers->workerids;
  212. for (i = 0; i < nworkers; i++)
  213. {
  214. workerid = workerids[i];
  215. ws->proxlist[workerid] = (int*)malloc(nworkers*sizeof(int));
  216. int bindid;
  217. struct starpu_tree *neighbour = NULL;
  218. struct starpu_sched_ctx_iterator it;
  219. if(workers->init_iterator)
  220. workers->init_iterator(workers, &it);
  221. bindid = starpu_worker_get_bindid(workerid);
  222. it.value = starpu_tree_get(tree, bindid);
  223. int cnt = 0;
  224. for(;;)
  225. {
  226. neighbour = (struct starpu_tree*)it.value;
  227. int neigh_workerids[STARPU_NMAXWORKERS];
  228. int neigh_nworkers = _starpu_worker_get_workerids(neighbour->id, neigh_workerids);
  229. int w;
  230. for(w = 0; w < neigh_nworkers; w++)
  231. {
  232. if(!it.visited[neigh_workerids[w]] && workers->present[neigh_workerids[w]])
  233. {
  234. ws->proxlist[workerid][cnt++] = neigh_workerids[w];
  235. it.visited[neigh_workerids[w]] = 1;
  236. }
  237. }
  238. if(!workers->has_next(workers, &it))
  239. break;
  240. it.value = it.possible_value;
  241. it.possible_value = NULL;
  242. }
  243. }
  244. #endif
  245. }
  246. static void lws_remove_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  247. {
  248. struct _starpu_lws_data *ws = (struct _starpu_lws_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  249. unsigned i;
  250. int workerid;
  251. for (i = 0; i < nworkers; i++)
  252. {
  253. workerid = workerids[i];
  254. _starpu_destroy_fifo(ws->queue_array[workerid]);
  255. #ifdef STARPU_HAVE_HWLOC
  256. free(ws->proxlist[workerid]);
  257. #endif
  258. }
  259. }
  260. static void lws_initialize_policy(unsigned sched_ctx_id)
  261. {
  262. #ifdef STARPU_HAVE_HWLOC
  263. starpu_sched_ctx_create_worker_collection(sched_ctx_id, STARPU_WORKER_TREE);
  264. #else
  265. starpu_sched_ctx_create_worker_collection(sched_ctx_id, STARPU_WORKER_LIST);
  266. #endif
  267. struct _starpu_lws_data *ws = (struct _starpu_lws_data*)malloc(sizeof(struct _starpu_lws_data));
  268. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)ws);
  269. ws->last_pop_worker = 0;
  270. ws->last_push_worker = 0;
  271. /* unsigned nw = starpu_sched_ctx_get_nworkers(sched_ctx_id); */
  272. unsigned nw = starpu_worker_get_count();
  273. ws->queue_array = (struct _starpu_fifo_taskq**)malloc(nw*sizeof(struct _starpu_fifo_taskq*));
  274. }
  275. static void lws_deinit_policy(unsigned sched_ctx_id)
  276. {
  277. struct _starpu_lws_data *ws = (struct _starpu_lws_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  278. free(ws->queue_array);
  279. #ifdef STARPU_HAVE_HWLOC
  280. free(ws->proxlist);
  281. #endif
  282. free(ws);
  283. starpu_sched_ctx_delete_worker_collection(sched_ctx_id);
  284. }
  285. struct starpu_sched_policy _starpu_sched_lws_policy =
  286. {
  287. .init_sched = lws_initialize_policy,
  288. .deinit_sched = lws_deinit_policy,
  289. .add_workers = lws_add_workers,
  290. .remove_workers = lws_remove_workers,
  291. .push_task = lws_push_task,
  292. .pop_task = lws_pop_task,
  293. .pre_exec_hook = NULL,
  294. .post_exec_hook = NULL,
  295. .pop_every_task = NULL,
  296. .policy_name = "nws",
  297. .policy_description = "new work stealing"
  298. };