worker_tree.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013, 2016-2017 Université de Bordeaux
  4. * Copyright (C) 2012-2014, 2016, 2017 CNRS
  5. * Copyright (C) 2011-2013, 2017 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. #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. struct starpu_tree *node = it->value;
  164. /* Are there workers left to be processed in the current node? */
  165. nworkers = starpu_bindid_get_workerids(node->id, &workerids);
  166. for(w = 0; w < nworkers; w++)
  167. {
  168. if(!it->visited[workerids[w]] && workers->present[workerids[w]] )
  169. {
  170. /* Still some! */
  171. it->possible_value = node;
  172. return 1;
  173. }
  174. }
  175. }
  176. struct starpu_tree *neighbour = starpu_tree_get_neighbour(tree, (struct starpu_tree*)it->value, it->visited, workers->present);
  177. if(!neighbour)
  178. {
  179. starpu_tree_reset_visited(tree, it->visited);
  180. it->value = NULL;
  181. it->possible_value = NULL;
  182. return 0;
  183. }
  184. int id = -1;
  185. nworkers = starpu_bindid_get_workerids(neighbour->id, &workerids);
  186. for(w = 0; w < nworkers; w++)
  187. {
  188. if(!it->visited[workerids[w]] && workers->present[workerids[w]])
  189. {
  190. id = workerids[w];
  191. it->possible_value = neighbour;
  192. break;
  193. }
  194. }
  195. STARPU_ASSERT_MSG(id != -1, "bind id (%d) for workerid (%d) not correct", neighbour->id, id);
  196. return 1;
  197. }
  198. static int tree_get_next(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  199. {
  200. if(it->possibly_parallel == 1)
  201. return tree_get_next_master(workers, it);
  202. else if(it->possibly_parallel == 0)
  203. return tree_get_next_unblocked_worker(workers, it);
  204. int ret = -1;
  205. struct starpu_tree *tree = (struct starpu_tree *)workers->collection_private;
  206. struct starpu_tree *neighbour = NULL;
  207. if(it->possible_value)
  208. {
  209. neighbour = it->possible_value;
  210. it->possible_value = NULL;
  211. }
  212. else
  213. neighbour = starpu_tree_get_neighbour(tree, (struct starpu_tree*)it->value, it->visited, workers->present);
  214. STARPU_ASSERT_MSG(neighbour, "no element anymore");
  215. int *workerids;
  216. int nworkers = starpu_bindid_get_workerids(neighbour->id, &workerids);
  217. int w;
  218. for(w = 0; w < nworkers; w++)
  219. {
  220. if(!it->visited[workerids[w]] && workers->present[workerids[w]] )
  221. {
  222. ret = workerids[w];
  223. it->visited[workerids[w]] = 1;
  224. it->value = neighbour;
  225. break;
  226. }
  227. }
  228. STARPU_ASSERT_MSG(ret != -1, "bind id not correct");
  229. return ret;
  230. }
  231. static int tree_add(struct starpu_worker_collection *workers, int worker)
  232. {
  233. if(!workers->present[worker])
  234. {
  235. workers->present[worker] = 1;
  236. workers->workerids[workers->nworkers] = worker;
  237. workers->nworkers++;
  238. return worker;
  239. }
  240. else
  241. return -1;
  242. }
  243. static int tree_remove(struct starpu_worker_collection *workers, int worker)
  244. {
  245. if(workers->present[worker])
  246. {
  247. unsigned i;
  248. for (i = 0; i < workers->nworkers; i++)
  249. if (workers->workerids[i] == worker)
  250. {
  251. memmove(&workers->workerids[i], &workers->workerids[i+1], (workers->nworkers-1-i) * sizeof(workers->workerids[i]));
  252. break;
  253. }
  254. workers->present[worker] = 0;
  255. workers->is_unblocked[worker] = 0;
  256. workers->is_master[worker] = 0;
  257. workers->nworkers--;
  258. return worker;
  259. }
  260. else
  261. return -1;
  262. }
  263. static void tree_init(struct starpu_worker_collection *workers)
  264. {
  265. _STARPU_MALLOC(workers->workerids, (STARPU_NMAXWORKERS+STARPU_NMAX_COMBINEDWORKERS) * sizeof(int));
  266. workers->collection_private = (void*)starpu_workers_get_tree();
  267. workers->nworkers = 0;
  268. int i;
  269. int nworkers = starpu_worker_get_count();
  270. for(i = 0; i < nworkers; i++)
  271. {
  272. workers->workerids[i] = -1;
  273. workers->present[i] = 0;
  274. workers->is_unblocked[i] = 0;
  275. workers->is_master[i] = 0;
  276. }
  277. return;
  278. }
  279. static void tree_deinit(struct starpu_worker_collection *workers)
  280. {
  281. (void) workers;
  282. free(workers->workerids);
  283. }
  284. static void tree_init_iterator(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  285. {
  286. (void) workers;
  287. it->value = NULL;
  288. it->possible_value = NULL;
  289. it->possibly_parallel = -1;
  290. int nworkers = starpu_worker_get_count();
  291. memset(&it->visited, 0, nworkers * sizeof(it->visited[0]));
  292. }
  293. static void tree_init_iterator_for_parallel_tasks(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it, struct starpu_task *task)
  294. {
  295. if (_starpu_get_nsched_ctxs() <= 1)
  296. {
  297. tree_init_iterator(workers, it);
  298. return;
  299. }
  300. tree_init_iterator(workers, it);
  301. it->possibly_parallel = task->possibly_parallel;
  302. int i;
  303. int nworkers = starpu_worker_get_count();
  304. for(i = 0; i < nworkers; i++)
  305. {
  306. workers->is_unblocked[i] = (workers->present[i] && !starpu_worker_is_blocked_in_parallel(i));
  307. if(!it->possibly_parallel) /* don't bother filling the table with masters we won't use it anyway */
  308. continue;
  309. workers->is_master[i] = (workers->present[i] && !starpu_worker_is_blocked_in_parallel(i) && !starpu_worker_is_slave_somewhere(i));
  310. }
  311. }
  312. struct starpu_worker_collection worker_tree =
  313. {
  314. .has_next = tree_has_next,
  315. .get_next = tree_get_next,
  316. .add = tree_add,
  317. .remove = tree_remove,
  318. .init = tree_init,
  319. .deinit = tree_deinit,
  320. .init_iterator = tree_init_iterator,
  321. .init_iterator_for_parallel_tasks = tree_init_iterator_for_parallel_tasks,
  322. .type = STARPU_WORKER_TREE
  323. };
  324. #endif// STARPU_HAVE_HWLOC