-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
60 lines (49 loc) · 2.04 KB
/
example.cpp
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
#include <chrono>
// include all required gpuMPHF headers
#include <phobic_gpu_mphf.hpp>
using namespace phobicgpu;
int main(int argc, char *argv[]) {
std::cout.precision(3);
std::cout << std::fixed;
// print name of the GPU which will be used
App::getInstance().printDebugInfo();
// number of elements
size_t size = 1e8;
// create some input
std::vector<std::string> keys;
keys.reserve(size);
for (int i = 0; i < size; ++i) {
keys.push_back(std::to_string(i));
}
// initialize the GPU builder using a particular config
// the average bucket size tradeoffs construction time with space consumption
// the partition size should be tuned for the particular hardware
// the builder can be reused
MPHFbuilder builder(MPHFconfig(4.5f, 2500));
// the minimal perfect hash function
// the templates are <pilot_encoding, partition_offset_encoding, initial_hash_function>
MPHF<interleaved_encoder<rice>, diff_partition_encoder<compact>, xxhash> f;
// measure construction time
auto beginConstruction = std::chrono::high_resolution_clock::now();
builder.build(keys, f);
unsigned long constructionDurationMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - beginConstruction).count();
std::cout << "Construction took " << constructionDurationMs << " ms" << std::endl;
std::cout << "Space required " << f.getBitsPerKey() << " bits per key" << std::endl;
// check that the MPHF is valid
std::vector<bool> taken(keys.size(), false);
for (size_t i = 0; i < keys.size(); i++) {
// use f(key) to query the MPHF
size_t hash = f(keys.at(i));
if (hash >= keys.size()) {
std::cerr << "Out of range!" << std::endl;
exit(1);
}
if (taken[hash]) {
std::cerr << "Collision by key " << i << "!" << std::endl;
exit(1);
}
taken[hash] = true;
}
std::cout << "valid for " << keys.size() << std::endl;
}