sched_ctx_list.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 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. #include "sched_ctx_list.h"
  18. struct _starpu_sched_ctx_elt* _starpu_sched_ctx_elt_find(struct _starpu_sched_ctx_list *list,
  19. unsigned sched_ctx)
  20. {
  21. struct _starpu_sched_ctx_list *l = NULL;
  22. struct _starpu_sched_ctx_elt *e = NULL;
  23. unsigned found = 0;
  24. for (l = list; l && !found; l=l->next)
  25. {
  26. e=l->head; //Go in a circle once before stopping
  27. do
  28. {
  29. if (e->sched_ctx == sched_ctx)
  30. {
  31. found = 1;
  32. break;
  33. }
  34. e = e->next;
  35. }
  36. while (e != l->head);
  37. }
  38. return found ? e : NULL;
  39. }
  40. void _starpu_sched_ctx_elt_init(struct _starpu_sched_ctx_elt *elt, unsigned sched_ctx)
  41. {
  42. elt->sched_ctx = sched_ctx;
  43. elt->task_number = 0;
  44. elt->last_poped = 0;
  45. elt->parent = NULL;
  46. elt->next = NULL;
  47. elt->prev = NULL;
  48. }
  49. void _starpu_sched_ctx_elt_ensure_consistency(struct _starpu_sched_ctx_list *list,
  50. unsigned sched_ctx)
  51. {
  52. struct _starpu_sched_ctx_elt *elt = _starpu_sched_ctx_elt_find(list, sched_ctx);
  53. if (elt && elt->task_number>0)
  54. elt->task_number = 0;
  55. }
  56. /* Adds a new element after the head of the given list. */
  57. struct _starpu_sched_ctx_elt* _starpu_sched_ctx_elt_add_after(struct _starpu_sched_ctx_list *list,
  58. unsigned sched_ctx)
  59. {
  60. struct _starpu_sched_ctx_elt *head, *next;
  61. struct _starpu_sched_ctx_elt *elt;
  62. _STARPU_MALLOC(elt, sizeof(struct _starpu_sched_ctx_elt));
  63. _starpu_sched_ctx_elt_init(elt, sched_ctx);
  64. elt->parent = list;
  65. head = list->head;
  66. if (head != NULL)
  67. {
  68. next = head->next;
  69. head->next = elt;
  70. elt->prev = head;
  71. /** We know next != NULL since it is at least head **/
  72. elt->next = next;
  73. next->prev = elt;
  74. }
  75. else
  76. {
  77. elt->next = elt;
  78. elt->prev = elt;
  79. list->head = elt;
  80. }
  81. return elt;
  82. }
  83. /* Adds a new element before the head of the given list. */
  84. struct _starpu_sched_ctx_elt* _starpu_sched_ctx_elt_add_before(struct _starpu_sched_ctx_list *list,
  85. unsigned sched_ctx)
  86. {
  87. struct _starpu_sched_ctx_elt *head, *prev;
  88. struct _starpu_sched_ctx_elt *elt;
  89. _STARPU_MALLOC(elt, sizeof(struct _starpu_sched_ctx_elt));
  90. _starpu_sched_ctx_elt_init(elt, sched_ctx);
  91. elt->parent = list;
  92. head = list->head;
  93. if (head != NULL)
  94. {
  95. prev = head->prev;
  96. head->prev = elt;
  97. elt->next = head;
  98. elt->prev = prev;
  99. prev->next = elt;
  100. }
  101. else
  102. {
  103. elt->next = elt;
  104. elt->prev = elt;
  105. list->head = elt;
  106. }
  107. return elt;
  108. }
  109. struct _starpu_sched_ctx_elt* _starpu_sched_ctx_elt_add(struct _starpu_sched_ctx_list *list,
  110. unsigned sched_ctx)
  111. {
  112. return _starpu_sched_ctx_elt_add_after(list, sched_ctx);
  113. }
  114. /* Remove elt from list */
  115. void _starpu_sched_ctx_elt_remove(struct _starpu_sched_ctx_list *list,
  116. struct _starpu_sched_ctx_elt *elt)
  117. {
  118. elt->prev->next = elt->next;
  119. elt->next->prev = elt->prev;
  120. if (elt->next == elt) //singleton
  121. list->head = NULL;
  122. else if (elt->next != elt && list->head == elt)
  123. list->head = elt->next;
  124. free(elt);
  125. return;
  126. }
  127. int _starpu_sched_ctx_elt_exists(struct _starpu_sched_ctx_list *list,
  128. unsigned sched_ctx)
  129. {
  130. struct _starpu_sched_ctx_elt *e;
  131. e = _starpu_sched_ctx_elt_find(list, sched_ctx);
  132. return (e == NULL) ? 0 : 1;
  133. }
  134. int _starpu_sched_ctx_elt_get_priority(struct _starpu_sched_ctx_list *list,
  135. unsigned sched_ctx)
  136. {
  137. struct _starpu_sched_ctx_elt *e;
  138. e = _starpu_sched_ctx_elt_find(list, sched_ctx);
  139. return (e == NULL) ? 0 : e->parent->priority;
  140. }
  141. struct _starpu_sched_ctx_list* _starpu_sched_ctx_list_find(struct _starpu_sched_ctx_list *list,
  142. unsigned prio)
  143. {
  144. struct _starpu_sched_ctx_list *l = NULL;
  145. for (l = list; l != NULL ; l=l->next)
  146. {
  147. if (l->priority == prio)
  148. break;
  149. }
  150. return l;
  151. }
  152. /* Adds sched_ctx in a priority list. We consider that we don't add two times
  153. * the same sched_ctx. Returns head of list. */
  154. struct _starpu_sched_ctx_elt* _starpu_sched_ctx_list_add_prio(struct _starpu_sched_ctx_list **list,
  155. unsigned prio, unsigned sched_ctx)
  156. {
  157. struct _starpu_sched_ctx_list *parent_list = NULL, *prev = NULL, *last = NULL;
  158. struct _starpu_sched_ctx_list *l;
  159. for (l = *list; l != NULL; l=l->next)
  160. {
  161. if (l->priority <= prio)
  162. break;
  163. last = l;
  164. }
  165. if (l != NULL && l->priority == prio)
  166. {
  167. parent_list = l;
  168. }
  169. else //l's priority is inferior or inexistant, add before
  170. {
  171. _STARPU_MALLOC(parent_list, sizeof(struct _starpu_sched_ctx_list));
  172. parent_list->priority = prio;
  173. parent_list->next = l;
  174. parent_list->head = NULL;
  175. parent_list->prev = NULL;
  176. if (l != NULL)
  177. {
  178. prev = l->prev;
  179. l->prev = parent_list;
  180. if (prev != NULL)
  181. {
  182. prev->next = parent_list;
  183. parent_list->prev = prev;
  184. }
  185. else
  186. {
  187. *list = parent_list;
  188. }
  189. }
  190. else
  191. {
  192. if (last == NULL)
  193. {
  194. *list = parent_list;
  195. }
  196. else
  197. {
  198. last->next = parent_list;
  199. parent_list->prev = last;
  200. }
  201. }
  202. }
  203. return _starpu_sched_ctx_elt_add(parent_list, sched_ctx);
  204. }
  205. int _starpu_sched_ctx_list_add(struct _starpu_sched_ctx_list **list,
  206. unsigned sched_ctx)
  207. {
  208. return _starpu_sched_ctx_list_add_prio(list, 0, sched_ctx) != NULL ? 0 : -1;
  209. }
  210. void _starpu_sched_ctx_list_remove_elt(struct _starpu_sched_ctx_list **list,
  211. struct _starpu_sched_ctx_elt *rm)
  212. {
  213. struct _starpu_sched_ctx_list *parent;
  214. parent = rm->parent;
  215. _starpu_sched_ctx_elt_remove(parent, rm);
  216. /* Automatically clean up useless prio list */
  217. if (parent->head == NULL)
  218. {
  219. if (parent->prev == NULL)
  220. {
  221. *list = parent->next;
  222. if (parent->next != NULL)
  223. parent->next->prev = NULL;
  224. }
  225. else
  226. {
  227. parent->prev->next = parent->next;
  228. parent->next->prev = parent->prev;
  229. }
  230. free(parent);
  231. parent = NULL;
  232. }
  233. return;
  234. }
  235. /* Searches for a context and remove it */
  236. int _starpu_sched_ctx_list_remove(struct _starpu_sched_ctx_list **list,
  237. unsigned sched_ctx)
  238. {
  239. struct _starpu_sched_ctx_elt *rm;
  240. rm = _starpu_sched_ctx_elt_find(*list, sched_ctx);
  241. if (rm == NULL)
  242. return -1;
  243. _starpu_sched_ctx_list_remove_elt(list, rm);
  244. return 0;
  245. }
  246. int _starpu_sched_ctx_list_move(struct _starpu_sched_ctx_list **list,
  247. unsigned sched_ctx, unsigned prio_to)
  248. {
  249. struct _starpu_sched_ctx_elt *elt = _starpu_sched_ctx_elt_find(*list, sched_ctx);
  250. long task_number = 0;
  251. if (elt == NULL)
  252. return -1;
  253. task_number = elt->task_number;
  254. _starpu_sched_ctx_list_remove_elt(list, elt);
  255. elt = _starpu_sched_ctx_list_add_prio(list, prio_to, sched_ctx);
  256. elt->task_number = task_number;
  257. return 0;
  258. }
  259. int _starpu_sched_ctx_list_exists(struct _starpu_sched_ctx_list *list,
  260. unsigned prio)
  261. {
  262. struct _starpu_sched_ctx_list *l;
  263. l = _starpu_sched_ctx_list_find(list, prio);
  264. return ((l == NULL && list->priority == prio) || l != NULL) ? 1 : 0;
  265. }
  266. void _starpu_sched_ctx_list_remove_all(struct _starpu_sched_ctx_list *list)
  267. {
  268. while (list->head != NULL)
  269. _starpu_sched_ctx_elt_remove(list, list->head);
  270. free(list);
  271. }
  272. void _starpu_sched_ctx_list_delete(struct _starpu_sched_ctx_list **list)
  273. {
  274. while(*list)
  275. {
  276. struct _starpu_sched_ctx_list *next = (*list)->next;
  277. _starpu_sched_ctx_list_remove_all(*list);
  278. *list = NULL;
  279. if(next)
  280. *list = next;
  281. }
  282. }
  283. int _starpu_sched_ctx_list_iterator_init(struct _starpu_sched_ctx_list *list,
  284. struct _starpu_sched_ctx_list_iterator *it)
  285. {
  286. it->list_head = list;
  287. it->cursor = NULL;
  288. return 0;
  289. }
  290. int _starpu_sched_ctx_list_iterator_has_next(struct _starpu_sched_ctx_list_iterator *it)
  291. {
  292. if (it->cursor == NULL)
  293. {
  294. if (it->list_head != NULL)
  295. return it->list_head->head != NULL;
  296. else
  297. return 0;
  298. }
  299. else
  300. {
  301. struct _starpu_sched_ctx_list *parent = it->cursor->parent;
  302. if (it->cursor->next == parent->head)
  303. return parent->next != NULL;
  304. }
  305. return 1;
  306. }
  307. struct _starpu_sched_ctx_elt* _starpu_sched_ctx_list_iterator_get_next(struct _starpu_sched_ctx_list_iterator *it)
  308. {
  309. struct _starpu_sched_ctx_elt *ret=NULL, *current;
  310. struct _starpu_sched_ctx_list *parent;
  311. current = it->cursor;
  312. if (current != NULL)
  313. {
  314. parent = it->cursor->parent;
  315. if (current->next == parent->head)
  316. {
  317. if (parent->next != NULL)
  318. {
  319. it->cursor = parent->next->head;
  320. ret = it->cursor;
  321. }
  322. else
  323. {
  324. /* if everything fails (e.g. worker removed from ctx since related has_next call)
  325. just return head, it'll save us a synchro */
  326. it->cursor = NULL;
  327. ret = it->list_head->head;
  328. }
  329. }
  330. else
  331. {
  332. it->cursor = current->next;
  333. ret = it->cursor;
  334. }
  335. }
  336. else
  337. {
  338. it->cursor = it->list_head->head;
  339. ret = it->cursor;
  340. }
  341. return ret;
  342. }
  343. int _starpu_sched_ctx_list_push_event(struct _starpu_sched_ctx_list *list, unsigned sched_ctx)
  344. {
  345. struct _starpu_sched_ctx_elt *elt = _starpu_sched_ctx_elt_find(list, sched_ctx);
  346. if (elt == NULL)
  347. return -1;
  348. elt->task_number++;
  349. return 0;
  350. }
  351. int _starpu_sched_ctx_list_pop_event(struct _starpu_sched_ctx_list *list, unsigned sched_ctx)
  352. {
  353. struct _starpu_sched_ctx_elt *elt = _starpu_sched_ctx_elt_find(list, sched_ctx);
  354. if (elt == NULL)
  355. return -1;
  356. elt->task_number--;
  357. /** Balance circular lists **/
  358. elt->parent->head = elt->next;
  359. return 0;
  360. }
  361. int _starpu_sched_ctx_list_pop_all_event(struct _starpu_sched_ctx_list *list, unsigned sched_ctx)
  362. {
  363. struct _starpu_sched_ctx_elt *elt = _starpu_sched_ctx_elt_find(list, sched_ctx);
  364. if (elt == NULL)
  365. return -1;
  366. elt->task_number = 0;
  367. /** Balance circular lists **/
  368. elt->parent->head = elt->next;
  369. return 0;
  370. }