Skip to content

Commit 38fd734

Browse files
committed
aaa
Signed-off-by: zhenshan.cao <zhenshan.cao@zilliz.com>
1 parent cead9ba commit 38fd734

5 files changed

Lines changed: 382 additions & 14 deletions

File tree

knowhere/utils/Bitset.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ ConcurrentBitset::operator&=(const BitsetView& view) {
6767

6868
std::shared_ptr<ConcurrentBitset>
6969
ConcurrentBitset::operator&(const ConcurrentBitset& bitset) const {
70-
auto result_bitset = std::make_shared<ConcurrentBitset>(bitset.count());
70+
auto result_bitset = std::make_shared<ConcurrentBitset>(bitset.size());
7171

7272
auto result_8 = result_bitset->mutable_data();
7373
auto result_64 = reinterpret_cast<uint64_t*>(result_8);
@@ -97,7 +97,7 @@ return result_bitset;
9797

9898
std::shared_ptr<ConcurrentBitset>
9999
ConcurrentBitset::operator&(const BitsetView& view) const {
100-
auto result_bitset = std::make_shared<ConcurrentBitset>(view.count());
100+
auto result_bitset = std::make_shared<ConcurrentBitset>(view.size());
101101

102102
auto result_8 = result_bitset->mutable_data();
103103
auto result_64 = reinterpret_cast<uint64_t*>(result_8);
@@ -177,7 +177,7 @@ return *this;
177177

178178
std::shared_ptr<ConcurrentBitset>
179179
ConcurrentBitset::operator|(const ConcurrentBitset& bitset) const {
180-
auto result_bitset = std::make_shared<ConcurrentBitset>(bitset.count());
180+
auto result_bitset = std::make_shared<ConcurrentBitset>(bitset.size());
181181
std::cout<<"1111111"<<std::endl;
182182

183183
auto result_8 = result_bitset->mutable_data();
@@ -213,7 +213,7 @@ return result_bitset;
213213

214214
std::shared_ptr<ConcurrentBitset>
215215
ConcurrentBitset::operator|(const BitsetView& view) const {
216-
auto result_bitset = std::make_shared<ConcurrentBitset>(view.count());
216+
auto result_bitset = std::make_shared<ConcurrentBitset>(view.size());
217217

218218
auto result_8 = result_bitset->mutable_data();
219219
auto result_64 = reinterpret_cast<uint64_t*>(result_8);

knowhere/utils/Bitset.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,20 @@ class ConcurrentBitset {
7676

7777
inline bool
7878
test(id_type_t id) const {
79-
unsigned char mask = (unsigned char)(0x01) << (id & 0x07);
80-
return (bitset_[id >> 3] & mask);
79+
uint8_t mask = (uint8_t)(0x01) << (id & 0x07);
80+
return bitset_[id >> 3] & mask;
8181
}
8282

8383
inline void
8484
set(id_type_t id) {
85-
uint8_t mask = ~uint8_t(0);
86-
// unsigned char mask = (unsigned char)(0x01) << (id & 0x07);
87-
//bitset_[id >> 3].fetch_or(mask);
88-
bitset_[id >> 3] = mask;
85+
uint8_t mask = (uint8_t)(0x01) << (id & 0x07);
86+
bitset_[id >> 3] |= mask;
8987
}
9088

9189
inline void
9290
clear(id_type_t id) {
93-
// uint8_t mask = ~uint8_t(0);
94-
//unsigned char mask = (unsigned char)(0x01) << (id & 0x07);
95-
// bitset_[id >> 3].fetch_and(~mask);
96-
bitset_[id >> 3] = 0;
91+
uint8_t mask = (uint8_t)(0x01) << (id & 0x07);
92+
bitset_[id >> 3] &= ~mask;
9793
}
9894

9995
size_t

knowhere/utils/Bitset2.cpp

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
4+
// with the License. You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software distributed under the License
9+
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10+
// or implied. See the License for the specific language governing permissions and limitations under the License.
11+
12+
#include <cstring>
13+
#include <string>
14+
#include <memory>
15+
#include "Bitset2.h"
16+
#include "BitsetView.h"
17+
18+
namespace faiss {
19+
20+
ConcurrentBitset2&
21+
ConcurrentBitset2::operator&=(const ConcurrentBitset2& bitset) {
22+
auto u8_1 = mutable_data();
23+
auto u8_2 = bitset.data();
24+
auto u64_1 = reinterpret_cast<uint64_t*>(u8_1);
25+
auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2);
26+
27+
size_t n64 = bitset_.size();
28+
// size_t n64 = n8 / 8;
29+
30+
for (size_t i = 0; i < n64; i++) {
31+
u64_1[i] &= u64_2[i];
32+
}
33+
34+
return *this;
35+
}
36+
37+
ConcurrentBitset2&
38+
ConcurrentBitset2::operator&=(const BitsetView& view) {
39+
auto u8_1 = mutable_data();
40+
auto u8_2 = view.data();
41+
auto u64_1 = reinterpret_cast<uint64_t*>(u8_1);
42+
auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2);
43+
44+
size_t n64 = bitset_.size();
45+
46+
for (size_t i = 0; i < n64; i++) {
47+
u64_1[i] &= u64_2[i];
48+
}
49+
50+
return *this;
51+
}
52+
53+
std::shared_ptr<ConcurrentBitset2>
54+
ConcurrentBitset2::operator&(const ConcurrentBitset2& bitset) const {
55+
auto result_bitset = std::make_shared<ConcurrentBitset2>(bitset.size());
56+
57+
//auto result_8 =
58+
auto result_64 = reinterpret_cast<uint64_t*>(result_bitset->mutable_data());
59+
60+
auto u64_1 = reinterpret_cast<const uint64_t*>(data());
61+
auto u64_2 = reinterpret_cast<const uint64_t*>(bitset.data());
62+
63+
size_t n64 = bitset_.size();
64+
65+
for (size_t i = 0; i < n64; i++) {
66+
result_64[i] = u64_1[i] & u64_2[i];
67+
}
68+
69+
return result_bitset;
70+
}
71+
72+
std::shared_ptr<ConcurrentBitset2>
73+
ConcurrentBitset2::operator&(const BitsetView& view) const {
74+
auto result_bitset = std::make_shared<ConcurrentBitset2>(view.size());
75+
76+
auto result_64 = reinterpret_cast<uint64_t*>(result_bitset->mutable_data());
77+
78+
auto u64_1 = reinterpret_cast<const uint64_t*>(data());
79+
auto u64_2 = reinterpret_cast<const uint64_t*>(view.data());
80+
81+
size_t n64 = bitset_.size();
82+
83+
for (size_t i = 0; i < n64; i++) {
84+
result_64[i] = u64_1[i] & u64_2[i];
85+
}
86+
87+
return result_bitset;
88+
}
89+
90+
91+
ConcurrentBitset2&
92+
ConcurrentBitset2::operator|=(const ConcurrentBitset2& bitset) {
93+
auto u64_1 = reinterpret_cast<uint64_t*>(mutable_data());
94+
auto u64_2 = reinterpret_cast<const uint64_t*>(bitset.data());
95+
96+
size_t n64 = bitset_.size();
97+
98+
for (size_t i = 0; i < n64; i++) {
99+
u64_1[i] |= u64_2[i];
100+
}
101+
102+
return *this;
103+
}
104+
105+
ConcurrentBitset2&
106+
ConcurrentBitset2::operator|=(const BitsetView& view) {
107+
auto u64_1 = reinterpret_cast<uint64_t*>(mutable_data());
108+
auto u64_2 = reinterpret_cast<const uint64_t*>(view.data());
109+
110+
size_t n64 = bitset_.size();
111+
112+
for (size_t i = 0; i < n64; i++) {
113+
u64_1[i] |= u64_2[i];
114+
}
115+
116+
return *this;
117+
}
118+
119+
120+
std::shared_ptr<ConcurrentBitset2>
121+
ConcurrentBitset2::operator|(const ConcurrentBitset2& bitset) const {
122+
auto result_bitset = std::make_shared<ConcurrentBitset2>(bitset.size());
123+
124+
auto result_64 = reinterpret_cast<uint64_t*>(result_bitset->mutable_data());
125+
126+
auto u64_1 = reinterpret_cast<const uint64_t*>(data());
127+
auto u64_2 = reinterpret_cast<const uint64_t*>(bitset.data());
128+
129+
size_t n64 = bitset_.size();
130+
131+
for (size_t i = 0; i < n64; i++) {
132+
result_64[i] = u64_1[i] | u64_2[i];
133+
}
134+
135+
return result_bitset;
136+
}
137+
138+
std::shared_ptr<ConcurrentBitset2>
139+
ConcurrentBitset2::operator|(const BitsetView& view) const {
140+
auto result_bitset = std::make_shared<ConcurrentBitset2>(view.size());
141+
142+
auto result_64 = reinterpret_cast<uint64_t*>(result_bitset->mutable_data());
143+
144+
auto u64_1 = reinterpret_cast<const uint64_t*>(data());
145+
auto u64_2 = reinterpret_cast<const uint64_t*>(view.data());
146+
147+
size_t n64 = bitset_.size();
148+
149+
for (size_t i = 0; i < n64; i++) {
150+
result_64[i] = u64_1[i] | u64_2[i];
151+
}
152+
153+
return result_bitset;
154+
}
155+
156+
157+
ConcurrentBitset2&
158+
ConcurrentBitset2::negate() {
159+
auto u64_1 = reinterpret_cast<uint64_t*>(mutable_data());
160+
161+
size_t n64 = bitset_.size();
162+
163+
for (size_t i = 0; i < n64; i++) {
164+
u64_1[i] = ~u64_1[i];
165+
}
166+
167+
return *this;
168+
}
169+
170+
size_t
171+
ConcurrentBitset2::count() const {
172+
size_t ret = 0;
173+
auto p_data = reinterpret_cast<const uint64_t *>(data());
174+
175+
auto len = size() >> 3;
176+
auto popcount8 = [&](uint8_t x) -> int{
177+
x = (x & 0x55) + ((x >> 1) & 0x55);
178+
x = (x & 0x33) + ((x >> 2) & 0x33);
179+
x = (x & 0x0F) + ((x >> 4) & 0x0F);
180+
return x;
181+
};
182+
183+
for (size_t i = 0; i < len; ++i) {
184+
ret += __builtin_popcountl(*p_data);
185+
p_data++;
186+
}
187+
188+
auto p_byte = data() + (len << 3);
189+
for (auto i = (len << 3); i < size(); ++i) {
190+
ret += popcount8(*p_byte);
191+
p_byte++;
192+
}
193+
return ret;
194+
}
195+
196+
ConcurrentBitset2::operator std::string() const {
197+
const char one = '1';
198+
const char zero = '0';
199+
const size_t len = size();
200+
std::string s;
201+
s.assign (len, zero);
202+
203+
for (size_t i = 0; i < len; ++i) {
204+
if (test(id_type_t(i)))
205+
s.assign(len - 1 - i, one);
206+
}
207+
return s;
208+
}
209+
210+
bool operator==(const ConcurrentBitset2& lhs, const ConcurrentBitset2& rhs) {
211+
if (std::addressof(lhs) == std::addressof(rhs)){
212+
return true;
213+
}
214+
215+
if (lhs.size() != rhs.size()){
216+
return false;
217+
}
218+
219+
if (lhs.byte_size() != rhs.byte_size()){
220+
return false;
221+
}
222+
223+
224+
auto ret = std::memcmp(lhs.data(), rhs.data(), lhs.byte_size());
225+
return ret == 0;
226+
}
227+
228+
bool operator!=(const ConcurrentBitset2& lhs, const ConcurrentBitset2& rhs){
229+
return !(lhs == rhs);
230+
}
231+
232+
std::ostream& operator<<(std::ostream& os, const ConcurrentBitset2& bitset)
233+
{
234+
os << std::string(bitset);
235+
return os;
236+
}
237+
238+
239+
} // namespace faiss

0 commit comments

Comments
 (0)