starpu_bitmap.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Simon Archipoff
  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. #ifndef __STARPU_BITMAP_H__
  18. #define __STARPU_BITMAP_H__
  19. #include <starpu_util.h>
  20. #ifdef __cplusplus
  21. extern "C"
  22. {
  23. #endif
  24. /**
  25. @defgroup API_Bitmap Bitmap
  26. @brief This is the interface for the bitmap utilities provided by StarPU.
  27. @{
  28. */
  29. #ifndef LONG_BIT
  30. #define LONG_BIT (sizeof(unsigned long) * 8)
  31. #endif
  32. #define BITMAP_SIZE ((STARPU_NMAXWORKERS - 1)/LONG_BIT) + 1
  33. /** create a empty starpu_bitmap */
  34. static inline struct starpu_bitmap *starpu_bitmap_create(void) STARPU_ATTRIBUTE_MALLOC;
  35. /** free \p b */
  36. static inline void starpu_bitmap_destroy(struct starpu_bitmap *b);
  37. /** set bit \p e in \p b */
  38. static inline void starpu_bitmap_set(struct starpu_bitmap *b, int e);
  39. /** unset bit \p e in \p b */
  40. static inline void starpu_bitmap_unset(struct starpu_bitmap *b, int e);
  41. /** unset all bits in \p b */
  42. static inline void starpu_bitmap_unset_all(struct starpu_bitmap *b);
  43. /** return true iff bit \p e is set in \p b */
  44. static inline int starpu_bitmap_get(struct starpu_bitmap *b, int e);
  45. /** Basically compute \c starpu_bitmap_unset_all(\p a) ; \p a = \p b & \p c; */
  46. static inline void starpu_bitmap_unset_and(struct starpu_bitmap *a, struct starpu_bitmap *b, struct starpu_bitmap *c);
  47. /** Basically compute \p a |= \p b */
  48. static inline void starpu_bitmap_or(struct starpu_bitmap *a, struct starpu_bitmap *b);
  49. /** return 1 iff \p e is set in \p b1 AND \p e is set in \p b2 */
  50. static inline int starpu_bitmap_and_get(struct starpu_bitmap *b1, struct starpu_bitmap *b2, int e);
  51. /** return the number of set bits in \p b */
  52. static inline int starpu_bitmap_cardinal(struct starpu_bitmap *b);
  53. /** return the index of the first set bit of \p b, -1 if none */
  54. static inline int starpu_bitmap_first(struct starpu_bitmap *b);
  55. /** return the position of the last set bit of \p b, -1 if none */
  56. static inline int starpu_bitmap_last(struct starpu_bitmap *b);
  57. /** return the position of set bit right after \p e in \p b, -1 if none */
  58. static inline int starpu_bitmap_next(struct starpu_bitmap *b, int e);
  59. /** todo */
  60. static inline int starpu_bitmap_has_next(struct starpu_bitmap *b, int e);
  61. /** @} */
  62. struct starpu_bitmap
  63. {
  64. unsigned long bits[BITMAP_SIZE];
  65. int cardinal;
  66. };
  67. #ifdef DEBUG_BITMAP
  68. static int check_bitmap(struct starpu_bitmap *b)
  69. {
  70. int card = b->cardinal;
  71. int i = starpu_bitmap_first(b);
  72. int j;
  73. for(j = 0; j < card; j++)
  74. {
  75. if(i == -1)
  76. return 0;
  77. int tmp = starpu_bitmap_next(b,i);
  78. if(tmp == i)
  79. return 0;
  80. i = tmp;
  81. }
  82. if(i != -1)
  83. return 0;
  84. return 1;
  85. }
  86. #else
  87. #define check_bitmap(b) 1
  88. #endif
  89. static int _count_bit_static(unsigned long e)
  90. {
  91. #if (__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__) >= 4)
  92. return __builtin_popcountl(e);
  93. #else
  94. int c = 0;
  95. while(e)
  96. {
  97. c += e&1;
  98. e >>= 1;
  99. }
  100. return c;
  101. #endif
  102. }
  103. static inline struct starpu_bitmap *starpu_bitmap_create()
  104. {
  105. return (struct starpu_bitmap *) calloc(1, sizeof(struct starpu_bitmap));
  106. }
  107. static inline void starpu_bitmap_destroy(struct starpu_bitmap * b)
  108. {
  109. if(b)
  110. {
  111. free(b);
  112. }
  113. }
  114. static inline void starpu_bitmap_set(struct starpu_bitmap * b, int e)
  115. {
  116. if(!starpu_bitmap_get(b, e))
  117. b->cardinal++;
  118. else
  119. return;
  120. STARPU_ASSERT(e/LONG_BIT < BITMAP_SIZE);
  121. b->bits[e/LONG_BIT] |= (1ul << (e%LONG_BIT));
  122. STARPU_ASSERT(check_bitmap(b));
  123. }
  124. static inline void starpu_bitmap_unset(struct starpu_bitmap *b, int e)
  125. {
  126. if(starpu_bitmap_get(b, e))
  127. b->cardinal--;
  128. else
  129. return;
  130. STARPU_ASSERT(e/LONG_BIT < BITMAP_SIZE);
  131. if(e / LONG_BIT > BITMAP_SIZE)
  132. return;
  133. b->bits[e/LONG_BIT] &= ~(1ul << (e%LONG_BIT));
  134. STARPU_ASSERT(check_bitmap(b));
  135. }
  136. static inline void starpu_bitmap_unset_all(struct starpu_bitmap * b)
  137. {
  138. memset(b->bits, 0, BITMAP_SIZE * sizeof(unsigned long));
  139. }
  140. static inline void starpu_bitmap_unset_and(struct starpu_bitmap * a, struct starpu_bitmap * b, struct starpu_bitmap * c)
  141. {
  142. a->cardinal = 0;
  143. int i;
  144. for(i = 0; i < BITMAP_SIZE; i++)
  145. {
  146. a->bits[i] = b->bits[i] & c->bits[i];
  147. a->cardinal += _count_bit_static(a->bits[i]);
  148. }
  149. }
  150. static inline int starpu_bitmap_get(struct starpu_bitmap * b, int e)
  151. {
  152. STARPU_ASSERT(e / LONG_BIT < BITMAP_SIZE);
  153. if(e / LONG_BIT >= BITMAP_SIZE)
  154. return 0;
  155. return (b->bits[e/LONG_BIT] & (1ul << (e%LONG_BIT))) ?
  156. 1:
  157. 0;
  158. }
  159. static inline void starpu_bitmap_or(struct starpu_bitmap * a, struct starpu_bitmap * b)
  160. {
  161. int i;
  162. a->cardinal = 0;
  163. for(i = 0; i < BITMAP_SIZE; i++)
  164. {
  165. a->bits[i] |= b->bits[i];
  166. a->cardinal += _count_bit_static(a->bits[i]);
  167. }
  168. }
  169. int starpu_bitmap_and_get(struct starpu_bitmap * b1, struct starpu_bitmap * b2, int e)
  170. {
  171. return starpu_bitmap_get(b1,e) && starpu_bitmap_get(b2,e);
  172. }
  173. static inline int starpu_bitmap_cardinal(struct starpu_bitmap * b)
  174. {
  175. return b->cardinal;
  176. }
  177. static inline int get_first_bit_rank(unsigned long ms)
  178. {
  179. STARPU_ASSERT(ms != 0);
  180. #if (__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))
  181. return __builtin_ffsl(ms) - 1;
  182. #else
  183. unsigned long m = 1ul;
  184. int i = 0;
  185. while(!(m&ms))
  186. i++,m<<=1;
  187. return i;
  188. #endif
  189. }
  190. static inline int get_last_bit_rank(unsigned long l)
  191. {
  192. STARPU_ASSERT(l != 0);
  193. #if (__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))
  194. return 8*sizeof(l) - __builtin_clzl(l);
  195. #else
  196. int ibit = LONG_BIT - 1;
  197. while((!(1ul << ibit)) & l)
  198. ibit--;
  199. STARPU_ASSERT(ibit >= 0);
  200. return ibit;
  201. #endif
  202. }
  203. static inline int starpu_bitmap_first(struct starpu_bitmap * b)
  204. {
  205. int i = 0;
  206. while(i < BITMAP_SIZE && !b->bits[i])
  207. i++;
  208. if( i == BITMAP_SIZE)
  209. return -1;
  210. int nb_long = i;
  211. unsigned long ms = b->bits[i];
  212. return (nb_long * LONG_BIT) + get_first_bit_rank(ms);
  213. }
  214. static inline int starpu_bitmap_has_next(struct starpu_bitmap * b, int e)
  215. {
  216. int nb_long = (e+1) / LONG_BIT;
  217. int nb_bit = (e+1) % LONG_BIT;
  218. unsigned long mask = (~0ul) << nb_bit;
  219. if(b->bits[nb_long] & mask)
  220. return 1;
  221. for(nb_long++; nb_long < BITMAP_SIZE; nb_long++)
  222. if(b->bits[nb_long])
  223. return 1;
  224. return 0;
  225. }
  226. static inline int starpu_bitmap_last(struct starpu_bitmap * b)
  227. {
  228. if(b->cardinal == 0)
  229. return -1;
  230. int ilong;
  231. for(ilong = BITMAP_SIZE - 1; ilong >= 0; ilong--)
  232. {
  233. if(b->bits[ilong])
  234. break;
  235. }
  236. STARPU_ASSERT(ilong >= 0);
  237. unsigned long l = b->bits[ilong];
  238. return ilong * LONG_BIT + get_last_bit_rank(l);
  239. }
  240. static inline int starpu_bitmap_next(struct starpu_bitmap *b, int e)
  241. {
  242. int nb_long = e / LONG_BIT;
  243. int nb_bit = e % LONG_BIT;
  244. unsigned long rest = nb_bit == LONG_BIT - 1 ? 0 : (~0ul << (nb_bit + 1)) & b->bits[nb_long];
  245. if(nb_bit != (LONG_BIT - 1) && rest)
  246. {
  247. int i = get_first_bit_rank(rest);
  248. STARPU_ASSERT(i >= 0 && i < LONG_BIT);
  249. return (nb_long * LONG_BIT) + i;
  250. }
  251. for(nb_long++;nb_long < BITMAP_SIZE; nb_long++)
  252. if(b->bits[nb_long])
  253. return nb_long * LONG_BIT + get_first_bit_rank(b->bits[nb_long]);
  254. return -1;
  255. }
  256. #ifdef __cplusplus
  257. }
  258. #endif
  259. #endif /* __STARPU_BITMAP_H__ */