prio_list.h 8.9 KB

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