forked from starpu-runtime/starpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarpu_bitmap.h
299 lines (265 loc) · 8.07 KB
/
starpu_bitmap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* StarPU --- Runtime system for heterogeneous multicore architectures.
*
* Copyright (C) 2013-2023 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
* Copyright (C) 2013 Simon Archipoff
*
* StarPU is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* StarPU is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU Lesser General Public License in COPYING.LGPL for more details.
*/
#ifndef __STARPU_BITMAP_H__
#define __STARPU_BITMAP_H__
#include <starpu_util.h>
#include <starpu_config.h>
#include <string.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
@defgroup API_Bitmap Bitmap
@brief This is the interface for the bitmap utilities provided by StarPU.
@{
*/
#ifndef _STARPU_LONG_BIT
#define _STARPU_LONG_BIT ((int)(sizeof(unsigned long) * 8))
#endif
#define _STARPU_BITMAP_SIZE ((STARPU_NMAXWORKERS - 1) / _STARPU_LONG_BIT) + 1
/** create a empty starpu_bitmap */
static inline struct starpu_bitmap *starpu_bitmap_create(void) STARPU_ATTRIBUTE_MALLOC;
/** zero a starpu_bitmap */
static inline void starpu_bitmap_init(struct starpu_bitmap *b);
/** free \p b */
static inline void starpu_bitmap_destroy(struct starpu_bitmap *b);
/** set bit \p e in \p b */
static inline void starpu_bitmap_set(struct starpu_bitmap *b, int e);
/** unset bit \p e in \p b */
static inline void starpu_bitmap_unset(struct starpu_bitmap *b, int e);
/** unset all bits in \p b */
static inline void starpu_bitmap_unset_all(struct starpu_bitmap *b);
/** return true iff bit \p e is set in \p b */
static inline int starpu_bitmap_get(struct starpu_bitmap *b, int e);
/** Basically compute \c starpu_bitmap_unset_all(\p a) ; \p a = \p b & \p c; */
static inline void starpu_bitmap_unset_and(struct starpu_bitmap *a, struct starpu_bitmap *b, struct starpu_bitmap *c);
/** Basically compute \p a |= \p b */
static inline void starpu_bitmap_or(struct starpu_bitmap *a, struct starpu_bitmap *b);
/** return 1 iff \p e is set in \p b1 AND \p e is set in \p b2 */
static inline int starpu_bitmap_and_get(struct starpu_bitmap *b1, struct starpu_bitmap *b2, int e);
/** return the number of set bits in \p b */
static inline int starpu_bitmap_cardinal(struct starpu_bitmap *b);
/** return the index of the first set bit of \p b, -1 if none */
static inline int starpu_bitmap_first(struct starpu_bitmap *b);
/** return the position of the last set bit of \p b, -1 if none */
static inline int starpu_bitmap_last(struct starpu_bitmap *b);
/** return the position of set bit right after \p e in \p b, -1 if none */
static inline int starpu_bitmap_next(struct starpu_bitmap *b, int e);
/** todo */
static inline int starpu_bitmap_has_next(struct starpu_bitmap *b, int e);
/** @} */
/**
todo
*/
struct starpu_bitmap
{
unsigned long bits[_STARPU_BITMAP_SIZE];
int cardinal;
};
#ifdef _STARPU_DEBUG_BITMAP
static int _starpu_check_bitmap(struct starpu_bitmap *b)
{
int card = b->cardinal;
int i = starpu_bitmap_first(b);
int j;
for (j = 0; j < card; j++)
{
if (i == -1)
return 0;
int tmp = starpu_bitmap_next(b, i);
if (tmp == i)
return 0;
i = tmp;
}
if (i != -1)
return 0;
return 1;
}
#else
#define _starpu_check_bitmap(b) 1
#endif
static int _starpu_count_bit_static(unsigned long e)
{
#if (__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__) >= 4)
return __builtin_popcountl(e);
#else
int c = 0;
while (e)
{
c += e & 1;
e >>= 1;
}
return c;
#endif
}
static inline struct starpu_bitmap *starpu_bitmap_create(void)
{
return (struct starpu_bitmap *)calloc(1, sizeof(struct starpu_bitmap));
}
static inline void starpu_bitmap_init(struct starpu_bitmap *b)
{
memset(b, 0, sizeof(*b));
}
static inline void starpu_bitmap_destroy(struct starpu_bitmap *b)
{
free(b);
}
static inline void starpu_bitmap_set(struct starpu_bitmap *b, int e)
{
if (!starpu_bitmap_get(b, e))
b->cardinal++;
else
return;
STARPU_ASSERT(e / _STARPU_LONG_BIT < _STARPU_BITMAP_SIZE);
b->bits[e / _STARPU_LONG_BIT] |= (1ul << (e % _STARPU_LONG_BIT));
STARPU_ASSERT(_starpu_check_bitmap(b));
}
static inline void starpu_bitmap_unset(struct starpu_bitmap *b, int e)
{
if (starpu_bitmap_get(b, e))
b->cardinal--;
else
return;
STARPU_ASSERT(e / _STARPU_LONG_BIT < _STARPU_BITMAP_SIZE);
if (e / _STARPU_LONG_BIT > _STARPU_BITMAP_SIZE)
return;
b->bits[e / _STARPU_LONG_BIT] &= ~(1ul << (e % _STARPU_LONG_BIT));
STARPU_ASSERT(_starpu_check_bitmap(b));
}
static inline void starpu_bitmap_unset_all(struct starpu_bitmap *b)
{
memset(b->bits, 0, _STARPU_BITMAP_SIZE * sizeof(unsigned long));
}
static inline void starpu_bitmap_unset_and(struct starpu_bitmap *a, struct starpu_bitmap *b, struct starpu_bitmap *c)
{
a->cardinal = 0;
int i;
for (i = 0; i < _STARPU_BITMAP_SIZE; i++)
{
a->bits[i] = b->bits[i] & c->bits[i];
a->cardinal += _starpu_count_bit_static(a->bits[i]);
}
}
static inline int starpu_bitmap_get(struct starpu_bitmap *b, int e)
{
STARPU_ASSERT(e / _STARPU_LONG_BIT < _STARPU_BITMAP_SIZE);
if (e / _STARPU_LONG_BIT >= _STARPU_BITMAP_SIZE)
return 0;
return (b->bits[e / _STARPU_LONG_BIT] & (1ul << (e % _STARPU_LONG_BIT))) ? 1 : 0;
}
static inline void starpu_bitmap_or(struct starpu_bitmap *a, struct starpu_bitmap *b)
{
int i;
a->cardinal = 0;
for (i = 0; i < _STARPU_BITMAP_SIZE; i++)
{
a->bits[i] |= b->bits[i];
a->cardinal += _starpu_count_bit_static(a->bits[i]);
}
}
static inline int starpu_bitmap_and_get(struct starpu_bitmap *b1, struct starpu_bitmap *b2, int e)
{
return starpu_bitmap_get(b1, e) && starpu_bitmap_get(b2, e);
}
static inline int starpu_bitmap_cardinal(struct starpu_bitmap *b)
{
return b->cardinal;
}
static inline int _starpu_get_first_bit_rank(unsigned long ms)
{
STARPU_ASSERT(ms != 0);
#if (__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))
return __builtin_ffsl(ms) - 1;
#else
unsigned long m = 1ul;
int i = 0;
while (!(m & ms))
i++, m <<= 1;
return i;
#endif
}
static inline int _starpu_get_last_bit_rank(unsigned long l)
{
STARPU_ASSERT(l != 0);
#if (__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))
return 8 * sizeof(l) - __builtin_clzl(l);
#else
int ibit = _STARPU_LONG_BIT - 1;
while ((!(1ul << ibit)) & l)
ibit--;
STARPU_ASSERT(ibit >= 0);
return ibit;
#endif
}
static inline int starpu_bitmap_first(struct starpu_bitmap *b)
{
int i = 0;
while (i < _STARPU_BITMAP_SIZE && !b->bits[i])
i++;
if (i == _STARPU_BITMAP_SIZE)
return -1;
int nb_long = i;
unsigned long ms = b->bits[i];
return (nb_long * _STARPU_LONG_BIT) + _starpu_get_first_bit_rank(ms);
}
static inline int starpu_bitmap_has_next(struct starpu_bitmap *b, int e)
{
int nb_long = (e + 1) / _STARPU_LONG_BIT;
int nb_bit = (e + 1) % _STARPU_LONG_BIT;
unsigned long mask = (~0ul) << nb_bit;
if (b->bits[nb_long] & mask)
return 1;
for (nb_long++; nb_long < _STARPU_BITMAP_SIZE; nb_long++)
if (b->bits[nb_long])
return 1;
return 0;
}
static inline int starpu_bitmap_last(struct starpu_bitmap *b)
{
if (b->cardinal == 0)
return -1;
int ilong;
for (ilong = _STARPU_BITMAP_SIZE - 1; ilong >= 0; ilong--)
{
if (b->bits[ilong])
break;
}
STARPU_ASSERT(ilong >= 0);
unsigned long l = b->bits[ilong];
return ilong * _STARPU_LONG_BIT + _starpu_get_last_bit_rank(l);
}
static inline int starpu_bitmap_next(struct starpu_bitmap *b, int e)
{
int nb_long = e / _STARPU_LONG_BIT;
int nb_bit = e % _STARPU_LONG_BIT;
unsigned long rest = nb_bit == _STARPU_LONG_BIT - 1 ? 0 : (~0ul << (nb_bit + 1)) & b->bits[nb_long];
if (nb_bit != (_STARPU_LONG_BIT - 1) && rest)
{
int i = _starpu_get_first_bit_rank(rest);
STARPU_ASSERT(i >= 0 && i < _STARPU_LONG_BIT);
return (nb_long * _STARPU_LONG_BIT) + i;
}
for (nb_long++; nb_long < _STARPU_BITMAP_SIZE; nb_long++)
if (b->bits[nb_long])
return nb_long * _STARPU_LONG_BIT + _starpu_get_first_bit_rank(b->bits[nb_long]);
return -1;
}
#ifdef __cplusplus
}
#endif
#endif /* __STARPU_BITMAP_H__ */