sched_ctx_list.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013 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->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 = (struct _starpu_sched_ctx_elt*)malloc(sizeof(struct _starpu_sched_ctx_elt));
  62. _starpu_sched_ctx_elt_init(elt, sched_ctx);
  63. elt->parent = list;
  64. head = list->head;
  65. if (head != NULL)
  66. {
  67. next = head->next;
  68. head->next = elt;
  69. elt->prev = head;
  70. /** We know next != NULL since it is at least head **/
  71. elt->next = next;
  72. next->prev = elt;
  73. }
  74. else
  75. {
  76. elt->next = elt;
  77. elt->prev = elt;
  78. list->head = elt;
  79. }
  80. return elt;
  81. }
  82. /* Adds a new element before the head of the given list. */
  83. struct _starpu_sched_ctx_elt* _starpu_sched_ctx_elt_add_before(struct _starpu_sched_ctx_list *list,
  84. unsigned sched_ctx)
  85. {
  86. struct _starpu_sched_ctx_elt *head, *prev;
  87. struct _starpu_sched_ctx_elt *elt = (struct _starpu_sched_ctx_elt*)malloc(sizeof(struct _starpu_sched_ctx_elt));
  88. _starpu_sched_ctx_elt_init(elt, sched_ctx);
  89. elt->parent = list;
  90. head = list->head;
  91. if (head != NULL)
  92. {
  93. prev = head->prev;
  94. head->prev = elt;
  95. elt->next = head;
  96. elt->prev = prev;
  97. prev->next = elt;
  98. }
  99. else
  100. {
  101. elt->next = elt;
  102. elt->prev = elt;
  103. list->head = elt;
  104. }
  105. return elt;
  106. }
  107. struct _starpu_sched_ctx_elt* _starpu_sched_ctx_elt_add(struct _starpu_sched_ctx_list *list,
  108. unsigned sched_ctx)
  109. {
  110. return _starpu_sched_ctx_elt_add_after(list, sched_ctx);
  111. }
  112. /* Remove elt from list */
  113. void _starpu_sched_ctx_elt_remove(struct _starpu_sched_ctx_list *list,
  114. struct _starpu_sched_ctx_elt **elt)
  115. {
  116. (*elt)->prev->next = (*elt)->next;
  117. (*elt)->next->prev = (*elt)->prev;
  118. if ((*elt)->next == (*elt)) //singleton
  119. list->head = NULL;
  120. else if ((*elt)->next != (*elt) && list->head == (*elt))
  121. list->head = (*elt)->next;
  122. free(*elt);
  123. *elt = NULL;
  124. return;
  125. }
  126. int _starpu_sched_ctx_elt_exists(struct _starpu_sched_ctx_list *list,
  127. unsigned sched_ctx)
  128. {
  129. struct _starpu_sched_ctx_elt *e = NULL;
  130. e = _starpu_sched_ctx_elt_find(list, sched_ctx);
  131. return (e == NULL) ? 0 : 1;
  132. }
  133. int _starpu_sched_ctx_elt_get_priority(struct _starpu_sched_ctx_list *list,
  134. unsigned sched_ctx)
  135. {
  136. struct _starpu_sched_ctx_elt *e = NULL;
  137. e = _starpu_sched_ctx_elt_find(list, sched_ctx);
  138. return (e == NULL) ? 0 : e->parent->priority;
  139. }
  140. struct _starpu_sched_ctx_list* _starpu_sched_ctx_list_find(struct _starpu_sched_ctx_list *list,
  141. unsigned prio)
  142. {
  143. struct _starpu_sched_ctx_list *l = NULL;
  144. for (l = list; l != NULL ; l=l->next)
  145. {
  146. if (l->priority == prio)
  147. break;
  148. }
  149. return l;
  150. }
  151. /* Adds sched_ctx in a priority list. We consider that we don't add two times
  152. * the same sched_ctx. Returns head of list. */
  153. struct _starpu_sched_ctx_elt* _starpu_sched_ctx_list_add_prio(struct _starpu_sched_ctx_list **list,
  154. unsigned prio, unsigned sched_ctx)
  155. {
  156. struct _starpu_sched_ctx_list *parent_list = NULL, *prev = NULL, *last = NULL;
  157. struct _starpu_sched_ctx_list *l = *list;
  158. for (l = *list; l != NULL; l=l->next)
  159. {
  160. if (l->priority <= prio)
  161. break;
  162. last = l;
  163. }
  164. if (l != NULL && l->priority == prio)
  165. {
  166. parent_list = l;
  167. }
  168. else //l's priority is inferior or inexistant, add before
  169. {
  170. parent_list = (struct _starpu_sched_ctx_list*)malloc(sizeof(struct _starpu_sched_ctx_list));
  171. parent_list->priority = prio;
  172. parent_list->next = l;
  173. parent_list->head = NULL;
  174. parent_list->prev = NULL;
  175. if (l != NULL)
  176. {
  177. prev = l->prev;
  178. l->prev = parent_list;
  179. if (prev != NULL)
  180. {
  181. prev->next = parent_list;
  182. parent_list->prev = prev;
  183. }
  184. else
  185. {
  186. *list = parent_list;
  187. }
  188. }
  189. else
  190. {
  191. if (last == NULL)
  192. {
  193. *list = parent_list;
  194. }
  195. else
  196. {
  197. last->next = parent_list;
  198. parent_list->prev = last;
  199. }
  200. }
  201. }
  202. return _starpu_sched_ctx_elt_add(parent_list, sched_ctx);
  203. }
  204. int _starpu_sched_ctx_list_add(struct _starpu_sched_ctx_list **list,
  205. unsigned sched_ctx)
  206. {
  207. return _starpu_sched_ctx_list_add_prio(list, 0, sched_ctx) != NULL ? 0 : 1;
  208. }
  209. void _starpu_sched_ctx_list_remove_elt(struct _starpu_sched_ctx_list **list,
  210. struct _starpu_sched_ctx_elt *rm)
  211. {
  212. struct _starpu_sched_ctx_list *parent;
  213. parent = rm->parent;
  214. _starpu_sched_ctx_elt_remove(parent, &rm);
  215. /* Automatically clean up useless prio list */
  216. if (parent->head == NULL)
  217. {
  218. if (parent->prev == NULL)
  219. {
  220. *list = parent->next;
  221. parent->next->prev = NULL;
  222. }
  223. else
  224. {
  225. parent->prev->next = parent->next;
  226. parent->next->prev = parent->prev;
  227. }
  228. free(parent);
  229. parent = NULL;
  230. }
  231. return;
  232. }
  233. /* Searches for a context and remove it */
  234. int _starpu_sched_ctx_list_remove(struct _starpu_sched_ctx_list **list,
  235. unsigned sched_ctx)
  236. {
  237. struct _starpu_sched_ctx_elt *rm;
  238. rm = _starpu_sched_ctx_elt_find(*list, sched_ctx);
  239. if (rm == NULL)
  240. return -1;
  241. _starpu_sched_ctx_list_remove_elt(list, rm);
  242. return 0;
  243. }
  244. int _starpu_sched_ctx_list_move(struct _starpu_sched_ctx_list **list,
  245. unsigned sched_ctx, unsigned prio_to)
  246. {
  247. struct _starpu_sched_ctx_elt *elt = _starpu_sched_ctx_elt_find(*list, sched_ctx);
  248. long task_number = 0;
  249. if (elt == NULL)
  250. return -1;
  251. task_number = elt->task_number;
  252. _starpu_sched_ctx_list_remove_elt(list, elt);
  253. elt = _starpu_sched_ctx_list_add_prio(list, prio_to, sched_ctx);
  254. elt->task_number = task_number;
  255. return 0;
  256. }
  257. int _starpu_sched_ctx_list_exists(struct _starpu_sched_ctx_list *list,
  258. unsigned prio)
  259. {
  260. struct _starpu_sched_ctx_list *l = NULL;
  261. l = _starpu_sched_ctx_list_find(list, prio);
  262. return ((l == NULL && list->priority == prio) || l != NULL) ? 1 : 0;
  263. }
  264. void _starpu_sched_ctx_list_remove_all(struct _starpu_sched_ctx_list *list)
  265. {
  266. struct _starpu_sched_ctx_elt *next = NULL;
  267. while (list->head != NULL)
  268. _starpu_sched_ctx_elt_remove(list, &list->head);
  269. free(list);
  270. }
  271. void _starpu_sched_ctx_list_delete(struct _starpu_sched_ctx_list **list)
  272. {
  273. while(*list)
  274. {
  275. struct _starpu_sched_ctx_list *next = (*list)->next;
  276. _starpu_sched_ctx_list_remove_all(*list);
  277. *list = NULL;
  278. if(next)
  279. *list = next;
  280. }
  281. }
  282. int _starpu_sched_ctx_list_iterator_init(struct _starpu_sched_ctx_list *list,
  283. struct _starpu_sched_ctx_list_iterator *it)
  284. {
  285. it->list_head = list;
  286. it->cursor = NULL;
  287. return 0;
  288. }
  289. int _starpu_sched_ctx_list_iterator_has_next(struct _starpu_sched_ctx_list_iterator *it)
  290. {
  291. struct _starpu_sched_ctx_list *parent;
  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. 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 = parent->next->head;
  321. } //else ret = NULL (end)
  322. }
  323. else
  324. {
  325. it->cursor = current->next;
  326. ret = it->cursor;
  327. }
  328. }
  329. else
  330. {
  331. it->cursor = it->list_head->head;
  332. ret = it->cursor;
  333. }
  334. return ret;
  335. }
  336. int _starpu_sched_ctx_list_push_event(struct _starpu_sched_ctx_list *list, unsigned sched_ctx)
  337. {
  338. struct _starpu_sched_ctx_elt *elt = _starpu_sched_ctx_elt_find(list, sched_ctx);
  339. if (elt == NULL)
  340. return -1;
  341. elt->task_number++;
  342. return 0;
  343. }
  344. int _starpu_sched_ctx_list_pop_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. /** Balance circular lists **/
  351. elt->parent->head = elt->next;
  352. return 0;
  353. }
  354. int _starpu_sched_ctx_list_pop_all_event(struct _starpu_sched_ctx_list *list, unsigned sched_ctx)
  355. {
  356. struct _starpu_sched_ctx_elt *elt = _starpu_sched_ctx_elt_find(list, sched_ctx);
  357. if (elt == NULL)
  358. return -1;
  359. elt->task_number = 0;
  360. /** Balance circular lists **/
  361. elt->parent->head = elt->next;
  362. return 0;
  363. }