-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackingSet.h
232 lines (206 loc) · 5.85 KB
/
TrackingSet.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
#pragma once
#include "Utils.h"
#include <unordered_set>
#include <cstdint>
#include <mutex>
#include <cassert>
template<typename TItem> struct Bucket {
mutable std::mutex sync_;
std::unordered_set<TItem> set_;
int64_t prefixSum_;
};
template<typename TItem> struct MulKHashBaseWithSalt {
uint128 operator()(const TItem& item) const {
return item * kHashBase + 37;
}
};
template<typename TItem, typename THasher=MulKHashBaseWithSalt<TItem>> struct TrackingSet {
static constexpr const int64_t cSyncContention = 3;
std::unique_ptr<Bucket<TItem>[]> buckets_ = std::make_unique<Bucket<TItem>[]>(cSyncContention * nSysCpus);
uint128 hash_ = 0;
std::atomic<int64_t> size_ = 0;
TrackingSet() = default;
void UpdateHash(const TItem& item) {
const uint128 h = THasher()(item);
reinterpret_cast<std::atomic<uint64_t>*>(&hash_)[0].fetch_xor(h & (-1LL));
reinterpret_cast<std::atomic<uint64_t>*>(&hash_)[1].fetch_xor(h >> 64);
}
void CopyFrom(const TrackingSet& src) {
hash_ = src.hash_;
size_ = src.Size();
#pragma omp parallel for
for(int64_t i=0; i<cSyncContention * nSysCpus; i++) {
buckets_[i].set_ = src.buckets_[i].set_;
}
}
TrackingSet(const TrackingSet& src) {
CopyFrom(src);
}
TrackingSet& operator=(const TrackingSet& src) {
if(this != &src) {
CopyFrom(src);
}
return *this;
}
Bucket<TItem>& GetBucket(const TItem& item) {
return buckets_[THasher()(item) % (cSyncContention * nSysCpus)];
}
const Bucket<TItem>& GetBucket(const TItem& item) const {
return buckets_[THasher()(item) % (cSyncContention * nSysCpus)];
}
bool Add(const TItem& item) {
Bucket<TItem>& b = GetBucket(item);
bool bAdded = false;
{
std::unique_lock<std::mutex> lock(b.sync_);
auto it = b.set_.find(item);
if(it == b.set_.end()) {
b.set_.emplace(item);
bAdded = true;
}
}
if(bAdded) {
UpdateHash(item);
[[maybe_unused]] const int64_t oldSize = size_.fetch_add(1, std::memory_order_relaxed);
assert(oldSize >= 0);
}
return bAdded;
}
void Remove(const TItem& item) {
Bucket<TItem>& b = GetBucket(item);
bool bRemoved = false;
{
std::unique_lock<std::mutex> lock(b.sync_);
auto it = b.set_.find(item);
if(it != b.set_.end()) {
b.set_.erase(it);
bRemoved = true;
}
}
if(bRemoved) {
UpdateHash(item);
[[maybe_unused]] const int64_t oldSize = size_.fetch_sub(1, std::memory_order_relaxed);
assert(oldSize >= 1);
}
}
void Flip(const TItem& item) {
Bucket<TItem>& b = GetBucket(item);
int sizeMod = 0;
{
std::unique_lock<std::mutex> lock(b.sync_);
auto it = b.set_.find(item);
if(it == b.set_.end()) {
b.set_.emplace(item);
sizeMod = 1;
} else {
b.set_.erase(it);
sizeMod = -1;
}
}
UpdateHash(item);
[[maybe_unused]] const int64_t oldSize = size_.fetch_add(sizeMod, std::memory_order_relaxed);
assert(oldSize + sizeMod >= 0);
}
bool Contains(const TItem& item) const {
const Bucket<TItem>& b = GetBucket(item);
std::unique_lock<std::mutex> lock(b.sync_);
return b.set_.find(item) != b.set_.end();
}
void Clear() {
#pragma omp parallel for
for(int64_t i=0; i<cSyncContention * nSysCpus; i++) {
buckets_[i].set_.clear();
}
hash_ = 0;
size_ = 0;
}
int64_t Size() const {
return size_.load(std::memory_order_relaxed);
}
bool operator==(const TrackingSet& fellow) const {
if(hash_ != fellow.hash_) {
return false;
}
std::atomic<bool> isEqual = true;
#pragma omp parallel for shared(isEqual)
for(int64_t i=0; i<cSyncContention * nSysCpus; i++) {
if(buckets_[i].set_ != fellow.buckets_[i].set_) {
isEqual = false;
#pragma omp cancel for
}
#pragma omp cancellation point for
}
return isEqual;
}
bool operator!=(const TrackingSet& fellow) const {
if(hash_ != fellow.hash_) {
return true;
}
std::atomic<bool> differ = false;
#pragma omp parallel for
for(int64_t i=0; i<cSyncContention * nSysCpus; i++) {
if(buckets_[i].set_ != fellow.buckets_[i].set_) {
differ = true;
#pragma omp cancel for
}
#pragma omp cancellation point for
}
return differ;
}
TrackingSet operator-(const TrackingSet& fellow) const {
TrackingSet ans;
#pragma omp parallel for
for(int64_t i=0; i<cSyncContention * nSysCpus; i++) {
for(const TItem& item : buckets_[i].set_) {
if(!fellow.Contains(item)) {
ans.Add(item);
}
}
}
return ans;
}
TrackingSet operator+(const TrackingSet& fellow) const {
if(Size() <= fellow.Size()) {
TrackingSet ans = fellow;
#pragma omp parallel for
for(int64_t i=0; i<cSyncContention * nSysCpus; i++) {
for(const TItem& item : buckets_[i].set_) {
ans.Add(item);
}
}
return ans;
}
else {
return fellow + *this;
}
}
std::vector<int64_t> ToVector() const {
std::vector<int64_t> ans(Size());
int64_t prefSum = 0;
buckets_[0].prefixSum_ = prefSum;
prefSum += buckets_[0].set_.size();
//TODO: use parallel prefix sum computation?
for(int64_t i=1; i<cSyncContention * nSysCpus; i++) {
buckets_[i].prefixSum_ = prefSum;
prefSum += buckets_[i].set_.size();
}
assert( prefSum == Size() );
#pragma omp parallel for
for(int64_t i=0; i<cSyncContention * nSysCpus; i++) {
int64_t j=buckets_[i].prefixSum_;
for(const TItem& item : buckets_[i].set_) {
ans[j] = item;
j++;
}
}
return ans;
}
};
using VCTrackingSet = TrackingSet<int64_t>;
namespace std {
template<> struct hash<VCTrackingSet> {
std::size_t operator()(const VCTrackingSet& ts) const {
return ts.hash_;
}
};
} // namespace std