starpu_bitmap.h 8.0 KB

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