prio_list.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015-2016 Université de Bordeaux
  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. /*
  17. * This implements list with priorities (as an int), by using two stages:
  18. * - an RB tree stage sorted by priority, whose leafs are...
  19. * - ... double-linked lists sorted by insertion order.
  20. *
  21. * We always keep the 0-priority list allocated, to avoid keeping
  22. * allocating/deallocating it when all priorities are 0.
  23. *
  24. * We maintain an "empty" flag, to allow lockless FOO_prio_list_empty call.
  25. *
  26. * PRIO_LIST_TYPE(FOO, priority field)
  27. * - Declares the following type:
  28. * + priority list: struct FOO_prio_list
  29. * - Declares the following inlines:
  30. * * Initialize a new priority list
  31. * void FOO_prio_list_init(struct FOO_prio_list*)
  32. * * Add a new element at the end of the list of the priority of the element
  33. * void FOO_prio_list_push_back(struct FOO_prio_list*, struct FOO*)
  34. * * Add a new element at the beginning of the list of the priority of the element
  35. * void FOO_prio_list_push_back(struct FOO_prio_list*, struct FOO*)
  36. * * Test that the priority list is empty
  37. * void FOO_prio_list_empty(struct FOO_prio_list*)
  38. * * Erase element from the priority list
  39. * void FOO_prio_list_empty(struct FOO_prio_list*, struct FOO*)
  40. * * Return and erase the first element of the priority list
  41. * void FOO_prio_list_pop_front(struct FOO_prio_list*)
  42. * * Catenate second priority list at ends of the first priority list
  43. * void FOO_prio_list_push_prio_list_back(struct FOO_prio_list*, struct FOO_prio_list*)
  44. * * Test whether element is part of the list
  45. * void FOO_prio_list_ismember(struct FOO_prio_list*, struct FOO*)
  46. *
  47. * PRIO_LIST_TYPE assumes that LIST_TYPE has already been called to create the
  48. * final structure.
  49. */
  50. #ifndef __PRIO_LIST_H__
  51. #define __PRIO_LIST_H__
  52. #include <common/rbtree.h>
  53. #define PRIO_LIST_TYPE(ENAME, PRIOFIELD) \
  54. PRIO_LIST_CREATE_TYPE(ENAME, PRIOFIELD)
  55. #ifndef STARPU_DEBUG
  56. #define PRIO_LIST_CREATE_TYPE(ENAME, PRIOFIELD) \
  57. /* The main type: an RB binary tree */ \
  58. struct ENAME##_prio_list { \
  59. struct starpu_rbtree tree; \
  60. int empty; \
  61. }; \
  62. /* The second stage: a list */ \
  63. struct ENAME##_prio_list_stage { \
  64. struct starpu_rbtree_node node; /* Keep this first so ENAME##_node_to_list_stage can work. */ \
  65. int prio; \
  66. struct ENAME##_list list; \
  67. }; \
  68. static inline struct ENAME##_prio_list_stage *ENAME##_node_to_list_stage(struct starpu_rbtree_node *node) \
  69. { \
  70. /* This assumes node is first member of stage */ \
  71. return (struct ENAME##_prio_list_stage *) node; \
  72. } \
  73. static inline const struct ENAME##_prio_list_stage *ENAME##_node_to_list_stage_const(const struct starpu_rbtree_node *node) \
  74. { \
  75. /* This assumes node is first member of stage */ \
  76. return (struct ENAME##_prio_list_stage *) node; \
  77. } \
  78. static inline void ENAME##_prio_list_init(struct ENAME##_prio_list *priolist) \
  79. { \
  80. starpu_rbtree_init(&priolist->tree); \
  81. priolist->empty = 1; \
  82. } \
  83. static inline void ENAME##_prio_list_deinit(struct ENAME##_prio_list *priolist) \
  84. { \
  85. if (starpu_rbtree_empty(&priolist->tree)) \
  86. return; \
  87. struct starpu_rbtree_node *root = priolist->tree.root; \
  88. struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage(root); \
  89. assert(ENAME##_list_empty(&stage->list)); \
  90. assert(!root->children[0] && !root->children[1]); \
  91. starpu_rbtree_remove(&priolist->tree, root); \
  92. free(stage); \
  93. } \
  94. static inline int ENAME##_prio_list_cmp_fn(int prio, const struct starpu_rbtree_node *node) \
  95. { \
  96. /* Sort by decreasing order */ \
  97. const struct ENAME##_prio_list_stage *e2 = ENAME##_node_to_list_stage_const(node); \
  98. return (e2->PRIOFIELD - prio); \
  99. } \
  100. static inline struct ENAME##_prio_list_stage *ENAME##_prio_list_add(struct ENAME##_prio_list *priolist, int prio) \
  101. { \
  102. unsigned long slot; \
  103. struct starpu_rbtree_node *node; \
  104. struct ENAME##_prio_list_stage *stage; \
  105. node = starpu_rbtree_lookup_slot(&priolist->tree, prio, ENAME##_prio_list_cmp_fn, slot); \
  106. if (node) \
  107. stage = ENAME##_node_to_list_stage(node); \
  108. else { \
  109. _STARPU_MALLOC(stage, sizeof(*stage)); \
  110. starpu_rbtree_node_init(&stage->node); \
  111. stage->prio = prio; \
  112. ENAME##_list_init(&stage->list); \
  113. starpu_rbtree_insert_slot(&priolist->tree, slot, &stage->node); \
  114. } \
  115. return stage; \
  116. } \
  117. static inline void ENAME##_prio_list_push_back(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  118. { \
  119. struct ENAME##_prio_list_stage *stage = ENAME##_prio_list_add(priolist, e->PRIOFIELD); \
  120. ENAME##_list_push_back(&stage->list, e); \
  121. priolist->empty = 0; \
  122. } \
  123. static inline void ENAME##_prio_list_push_front(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  124. { \
  125. struct ENAME##_prio_list_stage *stage = ENAME##_prio_list_add(priolist, e->PRIOFIELD); \
  126. ENAME##_list_push_front(&stage->list, e); \
  127. priolist->empty = 0; \
  128. } \
  129. static inline int ENAME##_prio_list_empty(const struct ENAME##_prio_list *priolist) \
  130. { \
  131. return priolist->empty; \
  132. } \
  133. /* Version of list_empty which does not use the cached empty flag,
  134. * typically used to compute the value of the flag */ \
  135. static inline int ENAME##_prio_list_empty_slow(const struct ENAME##_prio_list *priolist) \
  136. { \
  137. if (starpu_rbtree_empty(&priolist->tree)) \
  138. return 1; \
  139. struct starpu_rbtree_node *root = priolist->tree.root; \
  140. const struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage_const(root); \
  141. if (ENAME##_list_empty(&stage->list) && !root->children[0] && !root->children[1]) \
  142. /* Just one empty list */ \
  143. return 1; \
  144. return 0; \
  145. } \
  146. static inline void ENAME##_prio_list_erase(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  147. { \
  148. struct starpu_rbtree_node *node = starpu_rbtree_lookup(&priolist->tree, e->PRIOFIELD, ENAME##_prio_list_cmp_fn); \
  149. struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage(node); \
  150. ENAME##_list_erase(&stage->list, e); \
  151. if (ENAME##_list_empty(&stage->list)) { \
  152. if (stage->prio != 0) \
  153. { \
  154. /* stage got empty, remove it */ \
  155. starpu_rbtree_remove(&priolist->tree, node); \
  156. free(stage); \
  157. } \
  158. priolist->empty = ENAME##_prio_list_empty_slow(priolist); \
  159. } \
  160. } \
  161. static inline struct ENAME *ENAME##_prio_list_pop_front(struct ENAME##_prio_list *priolist) \
  162. { \
  163. struct starpu_rbtree_node *node; \
  164. struct ENAME##_prio_list_stage *stage; \
  165. struct ENAME *ret; \
  166. node = starpu_rbtree_first(&priolist->tree); \
  167. while(1) { \
  168. struct starpu_rbtree_node *next; \
  169. if (!node) \
  170. /* Tree is empty */ \
  171. return NULL; \
  172. stage = ENAME##_node_to_list_stage(node); \
  173. if (!ENAME##_list_empty(&stage->list)) \
  174. break; \
  175. /* Empty list, skip to next tree entry */ \
  176. next = starpu_rbtree_next(node); \
  177. /* drop it if not 0-prio */ \
  178. if (stage->prio != 0) \
  179. { \
  180. starpu_rbtree_remove(&priolist->tree, node); \
  181. free(stage); \
  182. } \
  183. node = next; \
  184. } \
  185. ret = ENAME##_list_pop_front(&stage->list); \
  186. if (ENAME##_list_empty(&stage->list)) { \
  187. if (stage->prio != 0) \
  188. { \
  189. /* stage got empty, remove it */ \
  190. starpu_rbtree_remove(&priolist->tree, node); \
  191. free(stage); \
  192. } \
  193. priolist->empty = ENAME##_prio_list_empty_slow(priolist); \
  194. } \
  195. return ret; \
  196. } \
  197. static inline void ENAME##_prio_list_push_prio_list_back(struct ENAME##_prio_list *priolist, struct ENAME##_prio_list *priolist_toadd) \
  198. { \
  199. struct starpu_rbtree_node *node_toadd, *tmp; \
  200. starpu_rbtree_for_each_remove(&priolist_toadd->tree, node_toadd, tmp) { \
  201. struct ENAME##_prio_list_stage *stage_toadd = ENAME##_node_to_list_stage(node_toadd); \
  202. unsigned long slot; \
  203. struct starpu_rbtree_node *node = starpu_rbtree_lookup_slot(&priolist->tree, stage_toadd->prio, ENAME##_prio_list_cmp_fn, slot); \
  204. if (node) \
  205. { \
  206. /* Catenate the lists */ \
  207. if (!ENAME##_list_empty(&stage_toadd->list)) { \
  208. struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage(node); \
  209. ENAME##_list_push_list_back(&stage->list, &stage_toadd->list); \
  210. free(node_toadd); \
  211. priolist->empty = 0; \
  212. } \
  213. } \
  214. else \
  215. { \
  216. if (!ENAME##_list_empty(&stage_toadd->list)) { \
  217. /* Just move the node between the trees */ \
  218. starpu_rbtree_insert_slot(&priolist->tree, slot, node_toadd); \
  219. priolist->empty = 0; \
  220. } \
  221. else \
  222. { \
  223. /* Actually empty, don't bother moving the list */ \
  224. free(node_toadd); \
  225. } \
  226. } \
  227. } \
  228. } \
  229. static inline int ENAME##_prio_list_ismember(const struct ENAME##_prio_list *priolist, const struct ENAME *e) \
  230. { \
  231. struct starpu_rbtree_node *node = starpu_rbtree_lookup(&priolist->tree, e->PRIOFIELD, ENAME##_prio_list_cmp_fn); \
  232. if (node) { \
  233. const struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage_const(node); \
  234. return ENAME##_list_ismember(&stage->list, e); \
  235. } \
  236. return 0; \
  237. }
  238. #else
  239. /* gdbinit can't recurse in a tree. Use a mere list in debugging mode. */
  240. #define PRIO_LIST_CREATE_TYPE(ENAME, PRIOFIELD) \
  241. struct ENAME##_prio_list { struct ENAME##_list list; }; \
  242. static inline void ENAME##_prio_list_init(struct ENAME##_prio_list *priolist) \
  243. { ENAME##_list_init(&(priolist)->list); } \
  244. static inline void ENAME##_prio_list_deinit(struct ENAME##_prio_list *priolist) \
  245. { (void) (priolist); /* ENAME##_list_deinit(&(priolist)->list); */ } \
  246. static inline void ENAME##_prio_list_push_back(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  247. { ENAME##_list_push_back(&(priolist)->list, (e)); } \
  248. static inline void ENAME##_prio_list_push_front(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  249. { ENAME##_list_push_front(&(priolist)->list, (e)); } \
  250. static inline int ENAME##_prio_list_empty(const struct ENAME##_prio_list *priolist) \
  251. { return ENAME##_list_empty(&(priolist)->list); } \
  252. static inline void ENAME##_prio_list_erase(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  253. { ENAME##_list_erase(&(priolist)->list, (e)); } \
  254. static inline struct ENAME *ENAME##_prio_list_pop_front(struct ENAME##_prio_list *priolist) \
  255. { return ENAME##_list_pop_front(&(priolist)->list); } \
  256. static inline void ENAME##_prio_list_push_prio_list_back(struct ENAME##_prio_list *priolist, struct ENAME##_prio_list *priolist_toadd) \
  257. { ENAME##_list_push_list_back(&(priolist)->list, &(priolist_toadd)->list); } \
  258. static inline int ENAME##_prio_list_ismember(const struct ENAME##_prio_list *priolist, const struct ENAME *e) \
  259. { return ENAME##_list_ismember(&(priolist)->list, (e)); } \
  260. #endif
  261. #endif // __PRIO_LIST_H__