-
Notifications
You must be signed in to change notification settings - Fork 3
/
bitmap.h
201 lines (184 loc) · 6.02 KB
/
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
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yichao Cheng
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* bitmap class declaration, GPU supported
*
* Authors:
* Yichao Cheng ([email protected])
* Ye Li ([email protected])
*
* Created on 2014-11-29
* Last modified on 2014-11-30
*/
#ifndef BITMAP_H
#define BITMAP_H
#include <algorithm>
#include "common.h"
class Bitmap : public Managed {
private:
Word *words;
int numWords;
public:
Bitmap() : words(NULL), numWords(0) {}
/**
* Allocate a bitmap initalize with zeros on unified memory
*
* @param numBits [number of bits in the bitmap]
*/
explicit Bitmap(int numBits) {
numWords = ((numBits - 1) >> 6) + 1;
cudaMallocManaged(&words, numWords * sizeof(Word));
memset(words, 0, numWords * sizeof(Word));
}
~Bitmap() {
cudaFree(words);
}
/**
* Sets the bit at the specified index to 1. Both host and device side
*
* @param index [the bit index]
*/
inline __host__ __device__
void set(int index) {
Word bitmask = static_cast<Word>(1) << (index & 0x3f);
#ifdef __CUDA_ARCH__
atomicOr(
reinterpret_cast<unsigned long long *>(&words[index >> 6]),
static_cast<unsigned long long>(bitmask) );
#else
words[index >> 6] |= bitmask;
#endif
}
/**
* Sets the bit at the specified index to 0. Both host and device side
*
* @param index [the bit index]
*/
inline __host__ __device__
void unset(int index) {
Word bitmask = static_cast<Word>(1) << (index & 0x3f);
#ifdef __CUDA_ARCH__
atomicAnd(
reinterpret_cast<unsigned long long *>(&words[index >> 6]),
static_cast<unsigned long long>(~bitmask) );
#else
words[index >> 6] &= ~bitmask;
#endif
}
/**
* Get the bit with the specified index.
*
* @param index The bit index
* @return [True if the bit is currently set]
*/
inline __host__ __device__
bool get(int index) const {
Word bitmask = static_cast<Word>(1) << (index & 0x3f);
return (words[index >> 6] & bitmask) != 0;
}
/**
* Get the capacity (number of bits) contained in this bitmap
* @return [the capacity]
*/
inline __host__ __device__
int capacity(void) const {
return numWords << 6;
}
/**
* Deep copy a bitmap to myself
*
* @param other The other bitmap
* @return Result bitmap
*/
Bitmap &operator= (const Bitmap &other) {
Word *temp = new Word[other.numWords];
memcpy(temp, other.words, other.numWords * sizeof(Word));
if (words != NULL) {
delete[] words;
}
words = temp;
numWords = other.numWords;
return * this;
}
/**
* Compute the bit-wise AND of two bitmaps and return it as result.
*
* @param other The other bitmap
* @return Result bitmap
*/
Bitmap operator& (const Bitmap &other) {
int numBitsBigger = std::max(capacity(), other.capacity());
int numWordsSmaller = std::min(numWords, other.numWords);
Bitmap newBitmap(numBitsBigger);
for (int i = 0; i < numWordsSmaller; i++) {
newBitmap.words[i] = words[i] & other.words[i];
}
return newBitmap;
}
/**
* Compute the bit-wise OR of two bitmaps and return it as result.
*
* @param other The other bitmap
* @return Result bitmap
*/
Bitmap operator| (const Bitmap &other) {
int numBitsBigger = std::max(capacity(), other.capacity());
int numWordsSmaller = std::min(numWords, other.numWords);
Bitmap newBitmap(numBitsBigger);
for (int i = 0; i < numWordsSmaller; i++) {
newBitmap.words[i] = words[i] | other.words[i];
}
// The bigger one lasts here and copys the rest of itself
for (int i = numWordsSmaller; i < numWords; i++) {
newBitmap.words[i] = words[i];
}
for (int i = numWordsSmaller; i < other.numWords; i++) {
newBitmap.words[i] = other.words[i];
}
return newBitmap;
}
/**
* Compute the bit-wise XOR of two bitmaps and return it as result.
*
* @param other The other bitmap
* @return Result bitmap
*/
Bitmap operator^ (const Bitmap &other) {
int numBitsBigger = std::max(capacity(), other.capacity());
int numWordsSmaller = std::min(numWords, other.numWords);
Bitmap newBitmap(numBitsBigger);
for (int i = 0; i < numWordsSmaller; i++) {
newBitmap.words[i] = words[i] ^ other.words[i];
}
// The bigger one lasts here and copys the rest of itself
for (int i = numWordsSmaller; i < numWords; i++) {
newBitmap.words[i] = words[i];
}
for (int i = numWordsSmaller; i < other.numWords; i++) {
newBitmap.words[i] = other.words[i];
}
return newBitmap;
}
};
#endif // BITMAP_H