File: opal_bitmap.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (396 lines) | stat: -rw-r--r-- 9,718 bytes parent folder | download | duplicates (4)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2014 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2007      Cisco Systems, Inc.  All rights reserved.
 * Copyright (c) 2010-2012 Oak Ridge National Labs.  All rights reserved.
 * Copyright (c) 2014      Intel, Inc. All rights reserved.
 * Copyright (c) 2015-2017 Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "opal_config.h"

#include <limits.h>
#include <stdio.h>

#include "opal/class/opal_bitmap.h"
#include "opal/constants.h"

/* The number of bits in the underlying type of the bitmap field
 * in the opal_bitmap_t struct
 */
#define SIZE_OF_BASE_TYPE 64

static void opal_bitmap_construct(opal_bitmap_t *bm);
static void opal_bitmap_destruct(opal_bitmap_t *bm);

OBJ_CLASS_INSTANCE(opal_bitmap_t, opal_object_t, opal_bitmap_construct, opal_bitmap_destruct);

static void opal_bitmap_construct(opal_bitmap_t *bm)
{
    bm->bitmap = NULL;
    bm->array_size = 0;
    bm->max_size = INT_MAX;
}

static void opal_bitmap_destruct(opal_bitmap_t *bm)
{
    if (NULL != bm->bitmap) {
        free(bm->bitmap);
        bm->bitmap = NULL;
    }
}

int opal_bitmap_set_max_size(opal_bitmap_t *bm, int max_size)
{
    if (NULL == bm) {
        return OPAL_ERR_BAD_PARAM;
    }

    /*
     * Only if the caller wants to set the maximum size,
     * we set it (in numbers of bits!), otherwise it is
     * set to INT_MAX in the constructor.
     */
    bm->max_size = (int) (((size_t) max_size + SIZE_OF_BASE_TYPE - 1) / SIZE_OF_BASE_TYPE);

    return OPAL_SUCCESS;
}

int opal_bitmap_init(opal_bitmap_t *bm, int size)
{
    /*
     * Only if the caller set the maximum size before initializing,
     * we test here (in numbers of bits!)
     * By default, the max size is INT_MAX, set in the constructor.
     */
    if ((size <= 0) || (NULL == bm) || (size > bm->max_size)) {
        return OPAL_ERR_BAD_PARAM;
    }

    bm->array_size = (int) (((size_t) size + SIZE_OF_BASE_TYPE - 1) / SIZE_OF_BASE_TYPE);
    if (NULL != bm->bitmap) {
        free(bm->bitmap);
        if (bm->max_size < bm->array_size) {
            bm->max_size = bm->array_size;
        }
    }
    bm->bitmap = (uint64_t *) malloc(bm->array_size * sizeof(uint64_t));
    if (NULL == bm->bitmap) {
        return OPAL_ERR_OUT_OF_RESOURCE;
    }

    opal_bitmap_clear_all_bits(bm);
    return OPAL_SUCCESS;
}

int opal_bitmap_set_bit(opal_bitmap_t *bm, int bit)
{
    int index, offset, new_size;

    if ((bit < 0) || (NULL == bm) || (bit > bm->max_size)) {
        return OPAL_ERR_BAD_PARAM;
    }

    index = bit / SIZE_OF_BASE_TYPE;
    offset = bit % SIZE_OF_BASE_TYPE;

    if (index >= bm->array_size) {

        /* We need to allocate more space for the bitmap, since we are
         out of range. We don't throw any error here, because this is
         valid and we simply expand the bitmap */

        new_size = index + 1;
        if (new_size > bm->max_size) {
            new_size = bm->max_size;
        }

        /* New size is just a multiple of the original size to fit in
         the index. */
        bm->bitmap = (uint64_t *) realloc(bm->bitmap, new_size * sizeof(uint64_t));
        if (NULL == bm->bitmap) {
            return OPAL_ERR_OUT_OF_RESOURCE;
        }

        /* zero out the new elements */
        memset(&bm->bitmap[bm->array_size], 0, (new_size - bm->array_size) * sizeof(uint64_t));

        /* Update the array_size */
        bm->array_size = new_size;
    }

    /* Now set the bit */
    bm->bitmap[index] |= (1UL << offset);

    return OPAL_SUCCESS;
}

int opal_bitmap_clear_bit(opal_bitmap_t *bm, int bit)
{
    int index, offset;

    if ((bit < 0) || NULL == bm || (bit >= (bm->array_size * SIZE_OF_BASE_TYPE))) {
        return OPAL_ERR_BAD_PARAM;
    }

    index = bit / SIZE_OF_BASE_TYPE;
    offset = bit % SIZE_OF_BASE_TYPE;

    bm->bitmap[index] &= ~(1UL << offset);
    return OPAL_SUCCESS;
}

bool opal_bitmap_is_set_bit(opal_bitmap_t *bm, int bit)
{
    int index, offset;

    if ((bit < 0) || NULL == bm || (bit >= (bm->array_size * SIZE_OF_BASE_TYPE))) {
        return false;
    }

    index = bit / SIZE_OF_BASE_TYPE;
    offset = bit % SIZE_OF_BASE_TYPE;

    if (0 != (bm->bitmap[index] & (1UL << offset))) {
        return true;
    }

    return false;
}

int opal_bitmap_clear_all_bits(opal_bitmap_t *bm)
{
    if (NULL == bm) {
        return OPAL_ERR_BAD_PARAM;
    }

    memset(bm->bitmap, 0, bm->array_size * sizeof(uint64_t));
    return OPAL_SUCCESS;
}

int opal_bitmap_set_all_bits(opal_bitmap_t *bm)
{
    if (NULL == bm) {
        return OPAL_ERR_BAD_PARAM;
    }

    memset(bm->bitmap, 0xff, bm->array_size * sizeof(uint64_t));

    return OPAL_SUCCESS;
}

int opal_bitmap_find_and_set_first_unset_bit(opal_bitmap_t *bm, int *position)
{
    int i = 0;
    uint64_t temp, all_ones = 0xffffffffffffffffUL;

    if (NULL == bm) {
        return OPAL_ERR_BAD_PARAM;
    }

    /* Neglect all which don't have an unset bit */
    *position = 0;
    while ((i < bm->array_size) && (bm->bitmap[i] == all_ones)) {
        ++i;
    }

    if (i == bm->array_size) {
        /* increase the bitmap size then */
        *position = bm->array_size * SIZE_OF_BASE_TYPE;
        return opal_bitmap_set_bit(bm, *position);
    }

    /* This one has an unset bit, find its bit number */

    temp = bm->bitmap[i];
    bm->bitmap[i] |= (bm->bitmap[i] + 1); /* Set the first zero bit */
    temp ^= bm->bitmap[i]; /* Compute the change: the first unset bit in the original number */
    while (!(temp & 0x1)) {
        ++(*position);
        temp >>= 1;
    }

    (*position) += i * SIZE_OF_BASE_TYPE;
    return OPAL_SUCCESS;
}

int opal_bitmap_bitwise_and_inplace(opal_bitmap_t *dest, opal_bitmap_t *right)
{
    int i;

    /*
     * Sanity check
     */
    if (NULL == dest || NULL == right) {
        return OPAL_ERR_BAD_PARAM;
    }
    if (dest->array_size != right->array_size) {
        return OPAL_ERR_BAD_PARAM;
    }

    /*
     * Bitwise AND
     */
    for (i = 0; i < dest->array_size; ++i) {
        dest->bitmap[i] &= right->bitmap[i];
    }

    return OPAL_SUCCESS;
}

int opal_bitmap_bitwise_or_inplace(opal_bitmap_t *dest, opal_bitmap_t *right)
{
    int i;

    /*
     * Sanity check
     */
    if (NULL == dest || NULL == right) {
        return OPAL_ERR_BAD_PARAM;
    }
    if (dest->array_size != right->array_size) {
        return OPAL_ERR_BAD_PARAM;
    }

    /*
     * Bitwise OR
     */
    for (i = 0; i < dest->array_size; ++i) {
        dest->bitmap[i] |= right->bitmap[i];
    }

    return OPAL_SUCCESS;
}

int opal_bitmap_bitwise_xor_inplace(opal_bitmap_t *dest, opal_bitmap_t *right)
{
    int i;

    /*
     * Sanity check
     */
    if (NULL == dest || NULL == right) {
        return OPAL_ERR_BAD_PARAM;
    }
    if (dest->array_size != right->array_size) {
        return OPAL_ERR_BAD_PARAM;
    }

    /*
     * Bitwise XOR
     */
    for (i = 0; i < dest->array_size; ++i) {
        dest->bitmap[i] ^= right->bitmap[i];
    }

    return OPAL_SUCCESS;
}

bool opal_bitmap_are_different(opal_bitmap_t *left, opal_bitmap_t *right)
{
    int i;

    /*
     * Sanity check
     */
    if (NULL == left || NULL == right) {
        return OPAL_ERR_BAD_PARAM;
    }

    if (opal_bitmap_size(left) != opal_bitmap_size(right)) {
        return true;
    }

    /*
     * Direct comparison
     */
    for (i = 0; i < left->array_size; ++i) {
        if (left->bitmap[i] != right->bitmap[i]) {
            return true;
        }
    }

    return false;
}

char *opal_bitmap_get_string(opal_bitmap_t *bitmap)
{
    int i;
    char *bitmap_str = NULL;

    if (NULL == bitmap) {
        return NULL;
    }

    bitmap_str = malloc(bitmap->array_size * SIZE_OF_BASE_TYPE + 1);
    if (NULL == bitmap_str) {
        return NULL;
    }
    bitmap_str[bitmap->array_size * SIZE_OF_BASE_TYPE] = '\0';

    for (i = 0; i < (bitmap->array_size * SIZE_OF_BASE_TYPE); ++i) {
        if (opal_bitmap_is_set_bit(bitmap, i)) {
            bitmap_str[i] = 'X';
        } else {
            bitmap_str[i] = '_';
        }
    }

    return bitmap_str;
}

int opal_bitmap_num_unset_bits(opal_bitmap_t *bm, int len)
{
    return (len - opal_bitmap_num_set_bits(bm, len));
}

int opal_bitmap_num_set_bits(opal_bitmap_t *bm, int len)
{
    int i, cnt = 0;
    uint64_t val;

#if OPAL_ENABLE_DEBUG
    if ((len < 0) || NULL == bm || (len >= (bm->array_size * SIZE_OF_BASE_TYPE))) {
        return 0;
    }
#endif

    for (i = 0; i < len; ++i) {
        if (0 == (val = bm->bitmap[i])) {
            continue;
        }
        /*  Peter Wegner in CACM 3 (1960), 322. This method goes through as many
         *  iterations as there are set bits. */
        for (; val; cnt++) {
            val &= val - 1; /* clear the least significant bit set */
        }
    }

    return cnt;
}

bool opal_bitmap_is_clear(opal_bitmap_t *bm)
{
    int i;

    for (i = 0; i < bm->array_size; ++i) {
        if (0 != bm->bitmap[i]) {
            return false;
        }
    }
    return true;
}