prio_list.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 void ENAME##_prio_list_init(struct ENAME##_prio_list *priolist) \
  74. { \
  75. starpu_rbtree_init(&priolist->tree); \
  76. priolist->empty = 1; \
  77. } \
  78. static inline void ENAME##_prio_list_deinit(struct ENAME##_prio_list *priolist) \
  79. { \
  80. if (starpu_rbtree_empty(&priolist->tree)) \
  81. return; \
  82. struct starpu_rbtree_node *root = priolist->tree.root; \
  83. struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage(root); \
  84. assert(ENAME##_list_empty(&stage->list)); \
  85. assert(!root->children[0] && !root->children[1]); \
  86. starpu_rbtree_remove(&priolist->tree, root); \
  87. free(stage); \
  88. } \
  89. static inline int ENAME##_prio_list_cmp_fn(int prio, struct starpu_rbtree_node *node) \
  90. { \
  91. /* Sort by decreasing order */ \
  92. struct ENAME##_prio_list_stage *e2 = ENAME##_node_to_list_stage(node); \
  93. return (e2->PRIOFIELD - prio); \
  94. } \
  95. static inline struct ENAME##_prio_list_stage *ENAME##_prio_list_add(struct ENAME##_prio_list *priolist, int prio) \
  96. { \
  97. unsigned long slot; \
  98. struct starpu_rbtree_node *node; \
  99. struct ENAME##_prio_list_stage *stage; \
  100. node = starpu_rbtree_lookup_slot(&priolist->tree, prio, ENAME##_prio_list_cmp_fn, slot); \
  101. if (node) \
  102. stage = ENAME##_node_to_list_stage(node); \
  103. else { \
  104. stage = malloc(sizeof(*stage)); \
  105. starpu_rbtree_node_init(&stage->node); \
  106. stage->prio = prio; \
  107. _starpu_data_request_list_init(&stage->list); \
  108. starpu_rbtree_insert_slot(&priolist->tree, slot, &stage->node); \
  109. } \
  110. return stage; \
  111. } \
  112. static inline void ENAME##_prio_list_push_back(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  113. { \
  114. struct ENAME##_prio_list_stage *stage = ENAME##_prio_list_add(priolist, e->PRIOFIELD); \
  115. ENAME##_list_push_back(&stage->list, e); \
  116. priolist->empty = 0; \
  117. } \
  118. static inline void ENAME##_prio_list_push_front(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  119. { \
  120. struct ENAME##_prio_list_stage *stage = ENAME##_prio_list_add(priolist, e->PRIOFIELD); \
  121. ENAME##_list_push_front(&stage->list, e); \
  122. priolist->empty = 0; \
  123. } \
  124. static inline int ENAME##_prio_list_empty(struct ENAME##_prio_list *priolist) \
  125. { \
  126. return priolist->empty; \
  127. } \
  128. /* Version of list_empty which does not use the cached empty flag,
  129. * typically used to compute the value of the flag */ \
  130. static inline int ENAME##_prio_list_empty_slow(struct ENAME##_prio_list *priolist) \
  131. { \
  132. if (starpu_rbtree_empty(&priolist->tree)) \
  133. return 1; \
  134. struct starpu_rbtree_node *root = priolist->tree.root; \
  135. struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage(root); \
  136. if (ENAME##_list_empty(&stage->list) && !root->children[0] && !root->children[1]) \
  137. /* Just one empty list */ \
  138. return 1; \
  139. return 0; \
  140. } \
  141. static inline void ENAME##_prio_list_erase(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  142. { \
  143. struct starpu_rbtree_node *node = starpu_rbtree_lookup(&priolist->tree, e->PRIOFIELD, ENAME##_prio_list_cmp_fn); \
  144. struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage(node); \
  145. ENAME##_list_erase(&stage->list, e); \
  146. if (ENAME##_list_empty(&stage->list)) { \
  147. if (stage->prio != 0) \
  148. { \
  149. /* stage got empty, remove it */ \
  150. starpu_rbtree_remove(&priolist->tree, node); \
  151. free(stage); \
  152. } \
  153. priolist->empty = ENAME##_prio_list_empty_slow(priolist); \
  154. } \
  155. } \
  156. static inline struct ENAME *ENAME##_prio_list_pop_front(struct ENAME##_prio_list *priolist) \
  157. { \
  158. struct starpu_rbtree_node *node; \
  159. struct ENAME##_prio_list_stage *stage; \
  160. struct ENAME *ret; \
  161. node = starpu_rbtree_first(&priolist->tree); \
  162. while(1) { \
  163. struct starpu_rbtree_node *next; \
  164. if (!node) \
  165. /* Tree is empty */ \
  166. return NULL; \
  167. stage = ENAME##_node_to_list_stage(node); \
  168. if (!ENAME##_list_empty(&stage->list)) \
  169. break; \
  170. /* Empty list, skip to next tree entry */ \
  171. next = starpu_rbtree_next(node); \
  172. /* drop it if not 0-prio */ \
  173. if (stage->prio != 0) \
  174. { \
  175. starpu_rbtree_remove(&priolist->tree, node); \
  176. free(stage); \
  177. } \
  178. node = next; \
  179. } \
  180. ret = ENAME##_list_pop_front(&stage->list); \
  181. if (ENAME##_list_empty(&stage->list)) { \
  182. if (stage->prio != 0) \
  183. { \
  184. /* stage got empty, remove it */ \
  185. starpu_rbtree_remove(&priolist->tree, node); \
  186. free(stage); \
  187. } \
  188. priolist->empty = ENAME##_prio_list_empty_slow(priolist); \
  189. } \
  190. return ret; \
  191. } \
  192. static inline void ENAME##_prio_list_push_prio_list_back(struct ENAME##_prio_list *priolist, struct ENAME##_prio_list *priolist_toadd) \
  193. { \
  194. struct starpu_rbtree_node *node_toadd, *tmp; \
  195. starpu_rbtree_for_each_remove(&priolist_toadd->tree, node_toadd, tmp) { \
  196. struct ENAME##_prio_list_stage *stage_toadd = ENAME##_node_to_list_stage(node_toadd); \
  197. unsigned long slot; \
  198. struct starpu_rbtree_node *node = starpu_rbtree_lookup_slot(&priolist->tree, stage_toadd->prio, ENAME##_prio_list_cmp_fn, slot); \
  199. if (node) \
  200. { \
  201. /* Catenate the lists */ \
  202. if (!ENAME##_list_empty(&stage_toadd->list)) { \
  203. struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage(node); \
  204. ENAME##_list_push_list_back(&stage->list, &stage_toadd->list); \
  205. free(node_toadd); \
  206. priolist->empty = 0; \
  207. } \
  208. } \
  209. else \
  210. { \
  211. if (!ENAME##_list_empty(&stage_toadd->list)) { \
  212. /* Just move the node between the trees */ \
  213. starpu_rbtree_insert_slot(&priolist->tree, slot, node_toadd); \
  214. priolist->empty = 0; \
  215. } \
  216. else \
  217. { \
  218. /* Actually empty, don't bother moving the list */ \
  219. free(node_toadd); \
  220. } \
  221. } \
  222. } \
  223. } \
  224. static inline int ENAME##_prio_list_ismember(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  225. { \
  226. struct starpu_rbtree_node *node = starpu_rbtree_lookup(&priolist->tree, e->PRIOFIELD, ENAME##_prio_list_cmp_fn); \
  227. if (node) { \
  228. struct ENAME##_prio_list_stage *stage = ENAME##_node_to_list_stage(node); \
  229. return ENAME##_list_ismember(&stage->list, e); \
  230. } \
  231. return 0; \
  232. }
  233. #else
  234. /* gdbinit can't recurse in a tree. Use a mere list in debugging mode. */
  235. #define PRIO_LIST_CREATE_TYPE(ENAME, PRIOFIELD) \
  236. struct ENAME##_prio_list { struct ENAME##_list list; }; \
  237. static inline void ENAME##_prio_list_init(struct ENAME##_prio_list *priolist) \
  238. { ENAME##_list_init(&(priolist)->list); } \
  239. static inline void ENAME##_prio_list_deinit(struct ENAME##_prio_list *priolist) \
  240. { (void) (priolist); /* ENAME##_list_deinit(&(priolist)->list); */ } \
  241. static inline void ENAME##_prio_list_push_back(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  242. { ENAME##_list_push_back(&(priolist)->list, (e)); } \
  243. static inline void ENAME##_prio_list_push_front(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  244. { ENAME##_list_push_front(&(priolist)->list, (e)); } \
  245. static inline int ENAME##_prio_list_empty(struct ENAME##_prio_list *priolist) \
  246. { return ENAME##_list_empty(&(priolist)->list); } \
  247. static inline void ENAME##_prio_list_erase(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  248. { ENAME##_list_erase(&(priolist)->list, (e)); } \
  249. static inline struct ENAME *ENAME##_prio_list_pop_front(struct ENAME##_prio_list *priolist) \
  250. { return ENAME##_list_pop_front(&(priolist)->list); } \
  251. static inline void ENAME##_prio_list_push_prio_list_back(struct ENAME##_prio_list *priolist, struct ENAME##_prio_list *priolist_toadd) \
  252. { ENAME##_list_push_list_back(&(priolist)->list, &(priolist_toadd)->list); } \
  253. static inline int ENAME##_prio_list_ismember(struct ENAME##_prio_list *priolist, struct ENAME *e) \
  254. { return ENAME##_list_ismember(&(priolist)->list, (e)); } \
  255. #endif
  256. #endif // __PRIO_LIST_H__