worker_tree.c 9.5 KB

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