worker_tree.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2017 Inria
  4. * Copyright (C) 2012-2017 CNRS
  5. * Copyright (C) 2013-2014,2016-2017 Université de Bordeaux
  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. #include <starpu.h>
  19. #ifdef STARPU_HAVE_HWLOC
  20. #include <hwloc.h>
  21. #include "core/workers.h"
  22. static unsigned tree_has_next_unblocked_worker(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  23. {
  24. STARPU_ASSERT(it != NULL);
  25. if(workers->nworkers == 0)
  26. return 0;
  27. struct starpu_tree *tree = (struct starpu_tree*)workers->collection_private;
  28. struct starpu_tree *neighbour = starpu_tree_get_neighbour(tree, (struct starpu_tree*)it->value, it->visited, workers->present);
  29. if(!neighbour)
  30. {
  31. starpu_tree_reset_visited(tree, it->visited);
  32. it->value = NULL;
  33. it->possible_value = NULL;
  34. return 0;
  35. }
  36. int id = -1;
  37. int *workerids;
  38. int nworkers = starpu_bindid_get_workerids(neighbour->id, &workerids);
  39. int w;
  40. for(w = 0; w < nworkers; w++)
  41. {
  42. if(!it->visited[workerids[w]] && workers->present[workerids[w]])
  43. {
  44. if(workers->is_unblocked[workerids[w]])
  45. {
  46. id = workerids[w];
  47. it->possible_value = neighbour;
  48. break;
  49. }
  50. else
  51. {
  52. it->visited[workerids[w]] = 1;
  53. it->value = neighbour;
  54. return tree_has_next_unblocked_worker(workers, it);
  55. }
  56. }
  57. }
  58. STARPU_ASSERT_MSG(id != -1, "bind id (%d) for workerid (%d) not correct", neighbour->id, id);
  59. return 1;
  60. }
  61. static int tree_get_next_unblocked_worker(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  62. {
  63. int ret = -1;
  64. struct starpu_tree *tree = (struct starpu_tree *)workers->collection_private;
  65. struct starpu_tree *neighbour = NULL;
  66. if(it->possible_value)
  67. {
  68. neighbour = it->possible_value;
  69. it->possible_value = NULL;
  70. }
  71. else
  72. neighbour = starpu_tree_get_neighbour(tree, (struct starpu_tree*)it->value, it->visited, workers->present);
  73. STARPU_ASSERT_MSG(neighbour, "no element anymore");
  74. int *workerids;
  75. int nworkers = starpu_bindid_get_workerids(neighbour->id, &workerids);
  76. int w;
  77. for(w = 0; w < nworkers; w++)
  78. {
  79. if(!it->visited[workerids[w]] && workers->present[workerids[w]] && workers->is_unblocked[workerids[w]])
  80. {
  81. ret = workerids[w];
  82. it->visited[workerids[w]] = 1;
  83. it->value = neighbour;
  84. break;
  85. }
  86. }
  87. STARPU_ASSERT_MSG(ret != -1, "bind id not correct");
  88. return ret;
  89. }
  90. static unsigned tree_has_next_master(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  91. {
  92. STARPU_ASSERT(it != NULL);
  93. if(workers->nworkers == 0)
  94. return 0;
  95. struct starpu_tree *tree = (struct starpu_tree*)workers->collection_private;
  96. struct starpu_tree *neighbour = starpu_tree_get_neighbour(tree, (struct starpu_tree*)it->value, it->visited, workers->is_master);
  97. if(!neighbour)
  98. {
  99. starpu_tree_reset_visited(tree, it->visited);
  100. it->value = NULL;
  101. it->possible_value = NULL;
  102. return 0;
  103. }
  104. int id = -1;
  105. int *workerids;
  106. int nworkers = starpu_bindid_get_workerids(neighbour->id, &workerids);
  107. int w;
  108. for(w = 0; w < nworkers; w++)
  109. {
  110. if(!it->visited[workerids[w]] && workers->is_master[workerids[w]])
  111. {
  112. id = workerids[w];
  113. it->possible_value = neighbour;
  114. break;
  115. }
  116. }
  117. STARPU_ASSERT_MSG(id != -1, "bind id (%d) for workerid (%d) not correct", neighbour->id, id);
  118. return 1;
  119. }
  120. static int tree_get_next_master(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  121. {
  122. int ret = -1;
  123. struct starpu_tree *tree = (struct starpu_tree *)workers->collection_private;
  124. struct starpu_tree *neighbour = NULL;
  125. if(it->possible_value)
  126. {
  127. neighbour = it->possible_value;
  128. it->possible_value = NULL;
  129. }
  130. else
  131. neighbour = starpu_tree_get_neighbour(tree, (struct starpu_tree*)it->value, it->visited, workers->is_master);
  132. STARPU_ASSERT_MSG(neighbour, "no element anymore");
  133. int *workerids;
  134. int nworkers = starpu_bindid_get_workerids(neighbour->id, &workerids);
  135. int w;
  136. for(w = 0; w < nworkers; w++)
  137. {
  138. if(!it->visited[workerids[w]] && workers->is_master[workerids[w]])
  139. {
  140. ret = workerids[w];
  141. it->visited[workerids[w]] = 1;
  142. it->value = neighbour;
  143. break;
  144. }
  145. }
  146. STARPU_ASSERT_MSG(ret != -1, "bind id not correct");
  147. return ret;
  148. }
  149. static unsigned tree_has_next(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  150. {
  151. if(it->possibly_parallel == 1)
  152. return tree_has_next_master(workers, it);
  153. else if(it->possibly_parallel == 0)
  154. return tree_has_next_unblocked_worker(workers, it);
  155. STARPU_ASSERT(it != NULL);
  156. if(workers->nworkers == 0)
  157. return 0;
  158. struct starpu_tree *tree = (struct starpu_tree*)workers->collection_private;
  159. int *workerids;
  160. int nworkers;
  161. int w;
  162. if (it->value)
  163. {
  164. struct starpu_tree *node = it->value;
  165. /* Are there workers left to be processed in the current node? */
  166. nworkers = starpu_bindid_get_workerids(node->id, &workerids);
  167. for(w = 0; w < nworkers; w++)
  168. {
  169. if(!it->visited[workerids[w]] && workers->present[workerids[w]] )
  170. {
  171. /* Still some! */
  172. it->possible_value = node;
  173. return 1;
  174. }
  175. }
  176. }
  177. struct starpu_tree *neighbour = starpu_tree_get_neighbour(tree, (struct starpu_tree*)it->value, it->visited, workers->present);
  178. if(!neighbour)
  179. {
  180. starpu_tree_reset_visited(tree, it->visited);
  181. it->value = NULL;
  182. it->possible_value = NULL;
  183. return 0;
  184. }
  185. int id = -1;
  186. nworkers = starpu_bindid_get_workerids(neighbour->id, &workerids);
  187. for(w = 0; w < nworkers; w++)
  188. {
  189. if(!it->visited[workerids[w]] && workers->present[workerids[w]])
  190. {
  191. id = workerids[w];
  192. it->possible_value = neighbour;
  193. break;
  194. }
  195. }
  196. STARPU_ASSERT_MSG(id != -1, "bind id (%d) for workerid (%d) not correct", neighbour->id, id);
  197. return 1;
  198. }
  199. static int tree_get_next(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  200. {
  201. if(it->possibly_parallel == 1)
  202. return tree_get_next_master(workers, it);
  203. else if(it->possibly_parallel == 0)
  204. return tree_get_next_unblocked_worker(workers, it);
  205. int ret = -1;
  206. struct starpu_tree *tree = (struct starpu_tree *)workers->collection_private;
  207. struct starpu_tree *neighbour = NULL;
  208. if(it->possible_value)
  209. {
  210. neighbour = it->possible_value;
  211. it->possible_value = NULL;
  212. }
  213. else
  214. neighbour = starpu_tree_get_neighbour(tree, (struct starpu_tree*)it->value, it->visited, workers->present);
  215. STARPU_ASSERT_MSG(neighbour, "no element anymore");
  216. int *workerids;
  217. int nworkers = starpu_bindid_get_workerids(neighbour->id, &workerids);
  218. int w;
  219. for(w = 0; w < nworkers; w++)
  220. {
  221. if(!it->visited[workerids[w]] && workers->present[workerids[w]] )
  222. {
  223. ret = workerids[w];
  224. it->visited[workerids[w]] = 1;
  225. it->value = neighbour;
  226. break;
  227. }
  228. }
  229. STARPU_ASSERT_MSG(ret != -1, "bind id not correct");
  230. return ret;
  231. }
  232. static int tree_add(struct starpu_worker_collection *workers, int worker)
  233. {
  234. if(!workers->present[worker])
  235. {
  236. workers->present[worker] = 1;
  237. workers->workerids[workers->nworkers] = worker;
  238. workers->nworkers++;
  239. return worker;
  240. }
  241. else
  242. return -1;
  243. }
  244. static int tree_remove(struct starpu_worker_collection *workers, int worker)
  245. {
  246. if(workers->present[worker])
  247. {
  248. unsigned i;
  249. for (i = 0; i < workers->nworkers; i++)
  250. if (workers->workerids[i] == worker)
  251. {
  252. memmove(&workers->workerids[i], &workers->workerids[i+1], (workers->nworkers-1-i) * sizeof(workers->workerids[i]));
  253. break;
  254. }
  255. workers->present[worker] = 0;
  256. workers->is_unblocked[worker] = 0;
  257. workers->is_master[worker] = 0;
  258. workers->nworkers--;
  259. return worker;
  260. }
  261. else
  262. return -1;
  263. }
  264. static void tree_init(struct starpu_worker_collection *workers)
  265. {
  266. _STARPU_MALLOC(workers->workerids, (STARPU_NMAXWORKERS+STARPU_NMAX_COMBINEDWORKERS) * sizeof(int));
  267. workers->collection_private = (void*)starpu_workers_get_tree();
  268. workers->nworkers = 0;
  269. int i;
  270. int nworkers = starpu_worker_get_count();
  271. for(i = 0; i < nworkers; i++)
  272. {
  273. workers->workerids[i] = -1;
  274. workers->present[i] = 0;
  275. workers->is_unblocked[i] = 0;
  276. workers->is_master[i] = 0;
  277. }
  278. return;
  279. }
  280. static void tree_deinit(struct starpu_worker_collection *workers)
  281. {
  282. (void) workers;
  283. free(workers->workerids);
  284. }
  285. static void tree_init_iterator(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  286. {
  287. (void) workers;
  288. it->value = NULL;
  289. it->possible_value = NULL;
  290. it->possibly_parallel = -1;
  291. int nworkers = starpu_worker_get_count();
  292. memset(&it->visited, 0, nworkers * sizeof(it->visited[0]));
  293. }
  294. static void tree_init_iterator_for_parallel_tasks(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it, struct starpu_task *task)
  295. {
  296. if (_starpu_get_nsched_ctxs() <= 1)
  297. {
  298. tree_init_iterator(workers, it);
  299. return;
  300. }
  301. tree_init_iterator(workers, it);
  302. it->possibly_parallel = task->possibly_parallel;
  303. int i;
  304. int nworkers = starpu_worker_get_count();
  305. for(i = 0; i < nworkers; i++)
  306. {
  307. workers->is_unblocked[i] = (workers->present[i] && !starpu_worker_is_blocked_in_parallel(i));
  308. if(!it->possibly_parallel) /* don't bother filling the table with masters we won't use it anyway */
  309. continue;
  310. workers->is_master[i] = (workers->present[i] && !starpu_worker_is_blocked_in_parallel(i) && !starpu_worker_is_slave_somewhere(i));
  311. }
  312. }
  313. struct starpu_worker_collection worker_tree =
  314. {
  315. .has_next = tree_has_next,
  316. .get_next = tree_get_next,
  317. .add = tree_add,
  318. .remove = tree_remove,
  319. .init = tree_init,
  320. .deinit = tree_deinit,
  321. .init_iterator = tree_init_iterator,
  322. .init_iterator_for_parallel_tasks = tree_init_iterator_for_parallel_tasks,
  323. .type = STARPU_WORKER_TREE
  324. };
  325. #endif// STARPU_HAVE_HWLOC