sched_ctx_list.c 9.7 KB

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