worker_tree.c 9.8 KB

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