-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsketch.cpp
More file actions
227 lines (187 loc) · 7.24 KB
/
Copy pathsketch.cpp
File metadata and controls
227 lines (187 loc) · 7.24 KB
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
#include "sketch.h"
#include <cstring>
#include <iostream>
#include <vector>
#include <cassert>
Sketch::Sketch(vec_t vector_len, uint64_t seed, size_t _samples, size_t _cols) : seed(seed) {
num_samples = _samples;
cols_per_sample = _cols;
num_columns = num_samples * cols_per_sample;
bkt_per_col = calc_bkt_per_col(vector_len);
num_buckets = num_columns * bkt_per_col + 1; // plus 1 for deterministic bucket
buckets = new Bucket[num_buckets];
// initialize bucket values
for (size_t i = 0; i < num_buckets; ++i) {
buckets[i].alpha = 0;
buckets[i].gamma = 0;
}
}
Sketch::Sketch(vec_t vector_len, uint64_t seed, std::istream &binary_in, size_t _samples,
size_t _cols)
: seed(seed) {
num_samples = _samples;
cols_per_sample = _cols;
num_columns = num_samples * cols_per_sample;
bkt_per_col = calc_bkt_per_col(vector_len);
num_buckets = num_columns * bkt_per_col + 1; // plus 1 for deterministic bucket
buckets = new Bucket[num_buckets];
// Read the serialized Sketch contents
binary_in.read((char *)buckets, bucket_array_bytes());
}
Sketch::Sketch(const Sketch &s) : seed(s.seed) {
num_samples = s.num_samples;
cols_per_sample = s.cols_per_sample;
num_columns = s.num_columns;
bkt_per_col = s.bkt_per_col;
num_buckets = s.num_buckets;
buckets = new Bucket[num_buckets];
std::memcpy(buckets, s.buckets, bucket_array_bytes());
}
Sketch::~Sketch() { delete[] buckets; }
#ifdef L0_SAMPLING
void Sketch::update(const vec_t update_idx) {
vec_hash_t checksum = Bucket_Boruvka::get_index_hash(update_idx, checksum_seed());
// Update depth 0 bucket
Bucket_Boruvka::update(buckets[num_buckets - 1], update_idx, checksum);
// Update higher depth buckets
for (unsigned i = 0; i < num_columns; ++i) {
col_hash_t depth = Bucket_Boruvka::get_index_depth(update_idx, column_seed(i), bkt_per_col);
likely_if(depth < bkt_per_col) {
for (col_hash_t j = 0; j <= depth; ++j) {
size_t bucket_id = i * bkt_per_col + j;
Bucket_Boruvka::update(buckets[bucket_id], update_idx, checksum);
}
}
}
}
#else // Use support finding algorithm instead. Faster but no guarantee of uniform sample.
void Sketch::update(const vec_t update_idx) {
vec_hash_t checksum = Bucket_Boruvka::get_index_hash(update_idx, checksum_seed());
// Update depth 0 bucket
Bucket_Boruvka::update(buckets[num_buckets - 1], update_idx, checksum);
// Update higher depth buckets
for (unsigned i = 0; i < num_columns; ++i) {
col_hash_t depth = Bucket_Boruvka::get_index_depth(update_idx, column_seed(i), bkt_per_col);
size_t bucket_id = i * bkt_per_col + depth;
likely_if(depth < bkt_per_col) {
Bucket_Boruvka::update(buckets[bucket_id], update_idx, checksum);
}
}
}
#endif
void Sketch::zero_contents() {
for (size_t i = 0; i < num_buckets; i++) {
buckets[i].alpha = 0;
buckets[i].gamma = 0;
}
reset_sample_state();
}
SketchSample Sketch::sample() {
if (sample_idx >= num_samples) {
throw OutOfSamplesException(seed, num_samples, sample_idx);
}
size_t idx = sample_idx++;
size_t first_column = idx * cols_per_sample;
if (buckets[num_buckets - 1].alpha == 0 && buckets[num_buckets - 1].gamma == 0)
return {0, ZERO}; // the "first" bucket is deterministic so if all zero then no edges to return
if (Bucket_Boruvka::is_good(buckets[num_buckets - 1], checksum_seed()))
return {buckets[num_buckets - 1].alpha, GOOD};
for (size_t i = 0; i < cols_per_sample; ++i) {
for (size_t j = 0; j < bkt_per_col; ++j) {
size_t bucket_id = (i + first_column) * bkt_per_col + j;
if (Bucket_Boruvka::is_good(buckets[bucket_id], checksum_seed()))
return {buckets[bucket_id].alpha, GOOD};
}
}
return {0, FAIL};
}
ExhaustiveSketchSample Sketch::exhaustive_sample() {
if (sample_idx >= num_samples) {
throw OutOfSamplesException(seed, num_samples, sample_idx);
}
std::unordered_set<vec_t> ret;
size_t idx = sample_idx++;
size_t first_column = idx * cols_per_sample;
unlikely_if (buckets[num_buckets - 1].alpha == 0 && buckets[num_buckets - 1].gamma == 0)
return {ret, ZERO}; // the "first" bucket is deterministic so if zero then no edges to return
unlikely_if (Bucket_Boruvka::is_good(buckets[num_buckets - 1], checksum_seed())) {
ret.insert(buckets[num_buckets - 1].alpha);
return {ret, GOOD};
}
for (size_t i = 0; i < cols_per_sample; ++i) {
for (size_t j = 0; j < bkt_per_col; ++j) {
size_t bucket_id = (i + first_column) * bkt_per_col + j;
unlikely_if (Bucket_Boruvka::is_good(buckets[bucket_id], checksum_seed())) {
ret.insert(buckets[bucket_id].alpha);
}
}
}
unlikely_if (ret.size() == 0)
return {ret, FAIL};
return {ret, GOOD};
}
void Sketch::merge(const Sketch &other) {
for (size_t i = 0; i < num_buckets; ++i) {
buckets[i].alpha ^= other.buckets[i].alpha;
buckets[i].gamma ^= other.buckets[i].gamma;
}
}
void Sketch::range_merge(const Sketch &other, size_t start_sample, size_t n_samples) {
if (start_sample + n_samples > num_samples) {
assert(false);
sample_idx = num_samples; // sketch is in a fail state!
return;
}
// update sample idx to point at beginning of this range if before it
sample_idx = std::max(sample_idx, start_sample);
// merge deterministic buffer
buckets[num_buckets - 1].alpha ^= other.buckets[num_buckets - 1].alpha;
buckets[num_buckets - 1].gamma ^= other.buckets[num_buckets - 1].gamma;
// merge other buckets
size_t start_bucket_id = start_sample * cols_per_sample * bkt_per_col;
size_t n_buckets = n_samples * cols_per_sample * bkt_per_col;
for (size_t i = 0; i < n_buckets; i++) {
size_t bucket_id = start_bucket_id + i;
buckets[bucket_id].alpha ^= other.buckets[bucket_id].alpha;
buckets[bucket_id].gamma ^= other.buckets[bucket_id].gamma;
}
}
void Sketch::merge_raw_bucket_buffer(const Bucket *raw_buckets) {
for (size_t i = 0; i < num_buckets; i++) {
buckets[i].alpha ^= raw_buckets[i].alpha;
buckets[i].gamma ^= raw_buckets[i].gamma;
}
}
void Sketch::serialize(std::ostream &binary_out) const {
binary_out.write((char*) buckets, bucket_array_bytes());
}
bool operator==(const Sketch &sketch1, const Sketch &sketch2) {
if (sketch1.num_buckets != sketch2.num_buckets || sketch1.seed != sketch2.seed)
return false;
for (size_t i = 0; i < sketch1.num_buckets; ++i) {
if (sketch1.buckets[i].alpha != sketch2.buckets[i].alpha ||
sketch1.buckets[i].gamma != sketch2.buckets[i].gamma) {
return false;
}
}
return true;
}
std::ostream &operator<<(std::ostream &os, const Sketch &sketch) {
Bucket bkt = sketch.buckets[sketch.num_buckets - 1];
bool good = Bucket_Boruvka::is_good(bkt, sketch.checksum_seed());
vec_t a = bkt.alpha;
vec_hash_t c = bkt.gamma;
os << " a:" << a << " c:" << c << (good ? " good" : " bad") << std::endl;
for (unsigned i = 0; i < sketch.num_columns; ++i) {
for (unsigned j = 0; j < sketch.bkt_per_col; ++j) {
unsigned bucket_id = i * sketch.bkt_per_col + j;
Bucket bkt = sketch.buckets[bucket_id];
vec_t a = bkt.alpha;
vec_hash_t c = bkt.gamma;
bool good = Bucket_Boruvka::is_good(bkt, sketch.checksum_seed());
os << " a:" << a << " c:" << c << (good ? " good" : " bad") << std::endl;
}
os << std::endl;
}
return os;
}