-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathli_wavelet_test.cpp
More file actions
440 lines (395 loc) · 17.6 KB
/
Copy pathli_wavelet_test.cpp
File metadata and controls
440 lines (395 loc) · 17.6 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include <spdlog/spdlog.h>
#include <chrono>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <vector>
#include <iomanip> //CSV formatting
#include <learned_hashing.hpp>
// #include "src/ArityFlatMultiary.h"
#include "src/FlatMultiaryWaveletTree.h"
// #include "src/FlatMultiaryWaveletTree_RankX.h"
// #include "src/FlatMultiaryWaveletTree_HRLE.h"
#include "src/WaveletTree.h"
#include "src/permute_roaring.h"
#include "src/utils.h"
#include "rs/builder.h"
#include "rs/radix_spline.h"
using namespace std;
const size_t max_error = 16;
using RSHash = learned_hashing::RadixSplineHash<uint64_t, 18,max_error>; //18 is fanout and 16 is max error bits
// template <typename F>
// unsigned long long time_function(F f) {
// auto start = std::chrono::high_resolution_clock::now();
// f();
// auto stop = std::chrono::high_resolution_clock::now();
// return std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count();
// }
template <typename T>
bool validate_access_function(T &wt,
const std::vector<uint64_t> &original_permutation) {
spdlog::info("validating");
for (int i = 0; i < original_permutation.size(); i++) {
int result = access(wt, i);
if (result != original_permutation[i]) {
spdlog::error("FAIL! at index {}: Expected {}, Got {}", i,
original_permutation[i], result);
return false;
}
}
spdlog::info("Success!");
return true;
}
template <typename T>
int access(WaveletTree<T> &wt, int x) {
return wt.access(x);
}
int access(FlatMultiaryWaveletTree &wt, int x) { return wt.access(x); }
int access(vector<int> p, int x) { return p.at(x); }
int access(int *p, int x) { return p[x]; }
template <typename T>
int binary_search(int start, int end, int val, const vector<uint64_t> &input_stream, T &wt,
long long &duration, long &num_access_calls,
bool regenerate_random = false) {
int midpt_pos_in_sorted = start + (end - start) / 2;
// start timer for calculating access time
auto start_time = std::chrono::high_resolution_clock::now();
int midpt_pos_in_input = access(wt, midpt_pos_in_sorted);
// stop timer
auto stop_time = std::chrono::high_resolution_clock::now();
auto d = std::chrono::duration_cast<std::chrono::nanoseconds>(stop_time -
start_time)
.count();
duration += d;
#ifdef PRINT_ACCESS_DURATION
std::cout << "duration = " << duration << ", time = " << d << std::endl;
#endif
num_access_calls++;
uint64_t midpt_val = input_stream[midpt_pos_in_input];
if (val < midpt_val) {
return binary_search(start, midpt_pos_in_sorted, val, input_stream, wt,
duration, num_access_calls);
} else if (val > midpt_val) {
return binary_search(midpt_pos_in_sorted, end, val, input_stream, wt,
duration, num_access_calls);
} else {
return midpt_pos_in_input;
}
}
int binary_search(int start, int end, int val, int *input_stream,
int input_stream_size, int *p,
bool regenerate_random = false) {
int midpt_pos_in_sorted = start + (end - start) / 2;
int midpt_pos_in_input = access(p, midpt_pos_in_sorted);
int midpt_val = input_stream[midpt_pos_in_input];
if (val < midpt_val) {
return binary_search(start, midpt_pos_in_sorted, val, input_stream,
input_stream_size, p);
} else if (val > midpt_val) {
return binary_search(midpt_pos_in_sorted, end, val, input_stream,
input_stream_size, p);
} else {
return midpt_pos_in_input;
}
}
int scan(int val, vector<int> input_stream, vector<int> p) {
for (int i = 0; i < p.size(); i++) {
int x = access(p, i);
if (input_stream[x] == val) {
return x;
}
}
return -1;
}
std::vector<uint64_t> generateRandomNumbers(int n, bool shuffle = true,
int seed = -1) {
std::vector<uint64_t> numbers;
for (uint64_t i = 0; i < n; i++) {
numbers.push_back(i);
}
if (shuffle) {
spdlog::info("shuffling input");
if (seed >= 0) {
std::mt19937 g(seed);
std::shuffle(numbers.begin(), numbers.end(), g);
} else {
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(numbers.begin(), numbers.end(), g);
}
}
return numbers;
}
std::vector<uint64_t> generateRandomLookups(int n, int num_lookups, int seed) {
std::vector<uint64_t> lookups;
std::mt19937 rng(seed); // Random number generator with seed
std::uniform_int_distribution<uint64_t> dist(0,
n - 1); // Distribution from 1 to n
std::cout << "Random numbers: ";
for (int i = 0; i < num_lookups; ++i) {
lookups.push_back(dist(rng));
}
return lookups;
}
template <typename T, typename Index>
std::pair<unsigned long long, unsigned long long>
do_experiment(WaveletTree<T> &wt,
const std::vector<uint64_t> &input_stream,
const std::vector<uint64_t> &lookups,
Index &rsh,
const std::string &results_file,
int n,
int num_trials,
long long &duration_wt,
long &num_access_calls) {
spdlog::info("Executing compare_binsearch_wt on with n = {}, num_lookups = {}, num_trials = {}",
n, lookups.size(), num_trials);
unsigned long long time_taken_wt = 0;
unsigned long long total_access_time = 0;
unsigned long long total_search_bound_time = 0;
duration_wt = 0;
num_access_calls = 0;
for (int t = 0; t < num_trials; ++t) {
unsigned long long time_taken_for_trial = 0;
for (auto q : lookups) {
// 1) search bound
// total_search_ns += time_function([&](){
auto search_bound_start = std::chrono::high_resolution_clock::now();
auto pred = rsh(q);
auto search_bound_stop = std::chrono::high_resolution_clock::now();
auto search_bound_duration = std::chrono::duration_cast<std::chrono::nanoseconds>(
search_bound_stop - search_bound_start).count();
total_search_bound_time += search_bound_duration;
auto begin = std::max(static_cast<long>(pred - max_error), 0L);
auto end = std::min(static_cast<long>(pred + max_error + 1), static_cast<long>(input_stream.size()));
// 2) full binary search + wavelet access timing
auto start = std::chrono::high_resolution_clock::now();
time_function([&]() {
int x = binary_search(begin, end, q,
input_stream, wt,
duration_wt, num_access_calls);
x+=1;//dummy
});
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count();
time_taken_for_trial += duration;
}
total_access_time += (time_taken_for_trial / lookups.size());
}
total_access_time /= static_cast<double>(num_trials);
double avg_wt_access_time = static_cast<double>(duration_wt) / num_access_calls;
double avg_total_access_time = static_cast<double>(total_access_time);
return {static_cast<unsigned long long>(avg_wt_access_time),
static_cast<unsigned long long>(avg_total_access_time)};
spdlog::info("Avg. Time taken per wavelet access call: {} nanoseconds", avg_wt_access_time);
spdlog::info("Avg. Total time per access (wavelet + search bound): {} nanoseconds", avg_total_access_time);
return {avg_wt_access_time, avg_total_access_time};
}
template<typename Index>
std::pair<unsigned long long, unsigned long long> do_experiment(
FlatMultiaryWaveletTree &wt,
const std::vector<uint64_t> &input_stream,
const std::vector<uint64_t> &lookups,
Index &rsh,
const std::string &results_file,
int n,
int num_trials,
long long &duration_wt,
long &num_access_calls) {
spdlog::info(
"Executing compare_binsearch_wt on with n = {}, num_lookups = {}, "
"and num_trials = {}",
n, lookups.size(), num_trials);
unsigned long long time_taken_wt = 0; // Time for wavelet tree access
unsigned long long total_access_time = 0; // Total time (wavelet access + search bound)
unsigned long long total_search_bound_time = 0;
duration_wt = 0;
num_access_calls = 0;
for (int i = 0; i < num_trials; i++) {
unsigned long long time_taken_for_trial = 0;
for (int j = 0; j < lookups.size(); j++) {
auto search_bound_start = std::chrono::high_resolution_clock::now();
auto pred = rsh(lookups[j]);
auto search_bound_stop = std::chrono::high_resolution_clock::now();
auto search_bound_duration = std::chrono::duration_cast<std::chrono::nanoseconds>(
search_bound_stop - search_bound_start).count();
total_search_bound_time += search_bound_duration;
auto begin = std::max(static_cast<long>(pred - max_error), 0L);
auto end = std::min(static_cast<long>(pred + max_error + 1), static_cast<long>(input_stream.size()));
auto start = std::chrono::high_resolution_clock::now();
time_function([&]() {
int x = binary_search(begin, end, lookups[j],
input_stream,
wt,
duration_wt,
num_access_calls);
x+=1;
});
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count();
time_taken_for_trial += duration;
}
total_access_time += (time_taken_for_trial / lookups.size());
}
total_access_time /= static_cast<double>(num_trials);
double avg_wt_access_time = static_cast<double>(duration_wt) / num_access_calls;
double avg_total_access_time = static_cast<double>(total_access_time);
return {static_cast<unsigned long long>(avg_wt_access_time),
static_cast<unsigned long long>(avg_total_access_time)};
spdlog::info("Avg. Time taken per wavelet access call: {} nanoseconds", avg_wt_access_time);
spdlog::info("Avg. Total time per access (wavelet + search bound): {} nanoseconds", avg_total_access_time);
return {avg_wt_access_time, avg_total_access_time};
}
std::vector<uint64_t> read_numbers_from_file(const std::string& filename) {
std::vector<uint64_t> numbers;
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << filename << std::endl;
exit(1);
}
std::string line;
uint64_t value;
while (std::getline(file, line)) {
try {
value = std::stoull(line);
numbers.push_back(value);
} catch (const std::exception& e) {
std::cerr << "Could not parse line '" << line << "' from file " << filename << "Error = " << e.what() << std::endl;
}
}
file.close();
spdlog::info("Done reading from file{}!", filename);
return numbers;
}
std::vector<uint64_t> read_numbers_from_bin_file(const std::string &filename) {
std::vector<uint64_t> numbers;
std::ifstream file(filename, std::ios::binary | std::ios::in);
if (!file.is_open()) {
std::cerr << "Error: Could not open binary file " << filename << std::endl;
exit(1);
}
// Read the file as `int` values (4 bytes per element)
int value;
while (file.read(reinterpret_cast<char *>(&value), sizeof(int))) {
// Convert `int` to `uint64_t` and store in the vector
numbers.push_back(static_cast<uint64_t>(value));
}
file.close();
spdlog::info("Done reading from binary file: {}!", filename);
return numbers;
}
int main(int argc, char **argv) {
int n_actual;
int n = atoi(argv[1]);
cout << n;
int num_trials = atoi(argv[2]);
string results_file = argv[3];
int num_lookups = atoi(argv[4]);
int seed = atoi(argv[5]);
bool random_input = atoi(argv[6]);
int arity = atoi(argv[7]);
string input_file_path = "";
if (argc > 8) {
input_file_path = argv[8];
}
spdlog::info("Writing results to file: {}", results_file);
try {
vector<uint64_t> input_stream;
if (!input_file_path.empty()) {
spdlog::info("Reading input from file: {}", input_file_path);
input_stream = read_numbers_from_bin_file(input_file_path);
n_actual = input_stream.size();
if (n_actual == 0) {
std::cerr << "Input file is empty or could not be read\n";
return 1;
}
} else {
spdlog::info("Generating random input numbers with size: {}", n);
input_stream = generateRandomNumbers(n, random_input, seed);
n_actual = input_stream.size();
}
vector<uint64_t> p = sorted_indices(input_stream);
vector<int> p_int(p.begin(), p.end());
vector<uint64_t> lookups = generateRandomLookups(n_actual, num_lookups, seed);
auto start_construction = std::chrono::high_resolution_clock::now();
// FlatMultiaryWaveletTree wt3(p.data(), n, arity);
// FlatMultiaryWaveletTree wt3(p_int.data(), n);
WaveletTree<permute::PermuteRoaring> wt3(p_int, n);
auto end_construction = std::chrono::high_resolution_clock::now();
auto construction_time = std::chrono::duration_cast<std::chrono::nanoseconds>(end_construction - start_construction).count();
spdlog::info("Wavelet‐tree build time = {} ns", construction_time);
std::vector<uint64_t> sorted_p = p;
std::sort(sorted_p.begin(), sorted_p.end());
// rs::Builder<uint64_t> rsb(sorted_p.front(), sorted_p.back());
RSHash rsh;
rsh.train(sorted_p.begin(), sorted_p.end(), sorted_p.size());
// for (const auto &val : sorted_p) rsb.AddKey(val);
// rs::RadixSpline<uint64_t> rs = rsb.Finalize();
long long duration_wt = 0;
long num_access_calls = 0;
#ifdef PROFILE
cout << "enter something to continue" << endl;
cin >> c;
#endif
auto [time_wavelet_access, time_total_access] = do_experiment(wt3, input_stream, lookups, rsh,
results_file, n, num_trials,
duration_wt, num_access_calls);
uint64_t size = wt3.size();
#ifdef VALIDATE
// if (!validate_access_function(wt3, p)) {
// std::cerr
// << "Validation multiary failed! Check the implementation of "
// "access or the wavelet tree."
// << std::endl;
// return 1;
// }
#endif
std::ofstream csv_file(results_file, std::ios::app);
if (!csv_file.is_open()) {
std::cerr << "Error: Could not open run.csv for writing.\n";
return 1;
}
std::ifstream check_file(results_file);
check_file.seekg(0, std::ios::end); // go to end of file
if (check_file.tellg() == 0) { // If the file is empty
csv_file << "filename,num_elements,arity,num_lookups,num_trials,size_bytes,time_wavelet_access_ns,time_total_access_ns,k,l\n";
}
if (check_file.tellg() == 0) { // If the file is empty
csv_file << "filename,num_elements,arity,num_lookups,num_trials,size_bytes,time_wavelet_access_ns,time_total_access_ns,k,l\n";
}
check_file.close();
//getting the k and l values from the input file name
std::string filename = input_file_path.substr(input_file_path.find_last_of("/") + 1);
size_t k_pos = filename.find("k");
size_t l_pos = filename.find("l");
std::string k_value = "NA"; // Default value if k is not found
std::string l_value = "NA"; // Default value if l is not found
if (k_pos != std::string::npos && l_pos != std::string::npos) {
k_value = filename.substr(k_pos + 1, l_pos - k_pos - 1); // Extract value between 'k' and 'l'
size_t dot_pos = filename.find(".", l_pos);
if (dot_pos != std::string::npos) {
l_value = filename.substr(l_pos + 1, dot_pos - l_pos - 1); // Extract value after 'l' till '.'
}
}
csv_file << std::fixed << std::setprecision(2); // Format floating-point numbers
csv_file << input_file_path << "," // File name
<< input_stream.size() << "," // Number of elements
<< arity << "," // Arity
<< num_lookups << "," // Number of lookups
<< num_trials << "," // Number of trials
<< size << "," // Size of wavelet tree (bytes)
<< time_wavelet_access << "," // Time for wavelet tree access (ns)
<< time_total_access << "," // Total time per access (ns)
<< construction_time << "," // Construction time (ns)
<< k_value << "," // Value of k
<< l_value << "\n";
csv_file.close();
spdlog::info("Results written to csv file");
} catch (exception &e) {
std::cout << "Exception caught: ";
cout << e.what() << endl;
return 1;
}
return 0;
};