This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
762 lines (587 loc) · 18.1 KB
/
main.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*
* benchmark of two mphf libraries:
* - phf
* - emphf
*
*/
#include <unistd.h>
#include <cstdlib> // stdtoul
#include <random>
#include <err.h> /* for warnx() */
#include "phf/phf.cc" /* phf library */
#include "libcmph/include/cmph.h" /* cmph library */
#include "gatb_system.hpp"
// parameters:
// switch between emphf algorithms
#define EMPHF_HEM
//#define EMPHF_SCAN
//#define EMPHF_SEQ
//** from this point, no need to look unless developer*/
// comment this line to switch to GATB-core's emphf
#define VANILLA_EMPHF
// number of elements in the MPHF. determined at compile-time, so change here.
unsigned long n = 100000000; // [phf]: 4 GB mem , ~2 minutes construction
//unsigned long n = 10000000; // [phf]: 7 seconds construction
bool bench_lookup = false;
bool from_disk = false;
bool only_emphf = false;
bool only_phf = false;
bool only_chd = false;
u_int64_t nb_in_bench_file;
#ifdef VANILLA_EMPHF
// vanilla emphf
#include "emphf/common.hpp"
#include "emphf/base_hash.hpp"
#include "emphf/hypergraph.hpp"
#include "emphf/mmap_memory_model.hpp"
#ifdef EMPHF_HEM
#include "emphf/mphf_hem.hpp" // for hem
#include "emphf/hypergraph_sorter_seq.hpp"
#else
#include "emphf/mphf.hpp"
#ifdef EMPHF_SCAN
#include "emphf/hypergraph_sorter_scan.hpp"
#else
#include "emphf/hypergraph_sorter_seq.hpp"
#endif
#endif
#else
// emphf from gatb-core
#include <gatb/gatb_core.hpp>
#include <emphf/common.hpp>
#include <emphf/mphf.hpp>
#include <emphf/base_hash.hpp>
#include <emphf/mmap_memory_model.hpp>
#include <emphf/hypergraph_sorter_scan.hpp>
#endif
using namespace std;
//taken from emphf for consistent bench
struct stats_accumulator {
stats_accumulator()
: m_n(0)
, m_mean(0)
, m_m2(0)
{}
void add(double x)
{
m_n += 1;
auto delta = x - m_mean;
m_mean += delta / m_n;
m_m2 += delta * (x - m_mean);
}
double mean() const
{
return m_mean;
}
double variance() const
{
return m_m2 / (m_n - 1);
}
double relative_stddev() const
{
return std::sqrt(variance()) / mean() * 100;
}
private:
double m_n;
double m_mean;
double m_m2;
};
u_int64_t _previousMem = 0;
unsigned long memory_usage(string message="", string mphftype="")
{
// from Progress.cpp of gatb-core
/** We get the memory used by the current process. */
u_int64_t mem = getMemorySelfUsed() / 1024;
u_int64_t memMaxProcess = getMemorySelfMaxUsed() / 1024;
/** We format the string to be displayed. */
char tmp[128];
snprintf (tmp, sizeof(tmp), " memory [current, maximum (maxRSS)]: [%4llu, %4llu] MB ",
mem, memMaxProcess
);
std::cout << message << " " << tmp << std::endl;
if (mphftype != "")
{
float nbitsMPHF = ((mem - _previousMem ) * (1024.0 * 1024.0 * 8.0)) / n;
std::cout << "Very rough estimation of memory used by the MPHF constructed by " << mphftype << " : " << mem - _previousMem << " MB (" << nbitsMPHF << " bits per elt)" << std::endl; // based on peak RSS, so not very accurate unless a huge amount of elements
}
_previousMem = mem;
return mem;
}
// iterator from disk file of u_int64_t with buffered read, todo template
class bfile_iterator : public std::iterator<std::forward_iterator_tag, u_int64_t>{
public:
bfile_iterator()
: _is(nullptr)
, _pos(0) ,_inbuff (0), _cptread(0)
{
_buffsize = 10000;
_buffer = (u_int64_t *) malloc(_buffsize*sizeof(u_int64_t));
}
bfile_iterator(const bfile_iterator& cr)
{
_buffsize = cr._buffsize;
_pos = cr._pos;
_is = cr._is;
_buffer = (u_int64_t *) malloc(_buffsize*sizeof(u_int64_t));
memcpy(_buffer,cr._buffer,_buffsize*sizeof(u_int64_t) );
_inbuff = cr._inbuff;
_cptread = cr._cptread;
_elem = cr._elem;
}
bfile_iterator(FILE* is): _is(is) , _pos(0) ,_inbuff (0), _cptread(0)
{
_buffsize = 10000;
_buffer = (u_int64_t *) malloc(_buffsize*sizeof(u_int64_t));
int reso = fseek(_is,0,SEEK_SET);
advance();
}
~bfile_iterator()
{
if(_buffer!=NULL)
free(_buffer);
}
u_int64_t const& operator*() { return _elem; }
bfile_iterator& operator++()
{
advance();
return *this;
}
friend bool operator==(bfile_iterator const& lhs, bfile_iterator const& rhs)
{
if (!lhs._is || !rhs._is) { if (!lhs._is && !rhs._is) { return true; } else { return false; } }
assert(lhs._is == rhs._is);
return rhs._pos == lhs._pos;
}
friend bool operator!=(bfile_iterator const& lhs, bfile_iterator const& rhs) { return !(lhs == rhs); }
private:
void advance()
{
_pos++;
if(_cptread >= _inbuff)
{
int res = fread(_buffer,sizeof(u_int64_t),_buffsize,_is);
_inbuff = res; _cptread = 0;
if(res == 0)
{
_is = nullptr;
_pos = 0;
return;
}
}
_elem = _buffer[_cptread];
_cptread ++;
}
u_int64_t _elem;
FILE * _is;
unsigned long _pos;
u_int64_t * _buffer; // for buffered read
int _inbuff, _cptread;
int _buffsize;
};
class file_binary{
public:
file_binary(const char* filename)
{
_is = fopen(filename, "rb");
if (!_is) {
throw std::invalid_argument("Error opening " + std::string(filename));
}
}
~file_binary()
{
fclose(_is);
}
bfile_iterator begin() const
{
return bfile_iterator(_is);
}
bfile_iterator end() const {return bfile_iterator(); }
size_t size () const { return 0; }//todo ?
private:
FILE * _is;
};
u_int64_t *data;
std::vector<u_int64_t> data_bench;
struct phf phf; // let's put it as global variable, so that phf object isn't destroyed after end of function
void do_phf()
{
clock_t begin, end;
begin = clock();
size_t lambda = 4, alpha = 80, seed = std::time(0); // default phf values
const bool nodiv = true;
PHF::init<u_int64_t, nodiv>(&phf, data, n, lambda, alpha, seed);
end = clock();
warnx("[phf] found perfect hash for %zu keys in %fs", n, (double)(end - begin) / CLOCKS_PER_SEC);
begin = clock();
PHF::compact(&phf);
end = clock();
warnx("compacted displacement map in %fs", (double)(end - begin) / CLOCKS_PER_SEC);
int d_bits = ffsl((long)phf_powerup(phf.d_max));
double k_bits = ((double)phf.r * d_bits) / n;
double g_load = (double)n / phf.r;
warnx("r:%zu m:%zu d_max:%zu d_bits:%d k_bits:%.2f g_load:%.2f", phf.r, phf.m, phf.d_max, d_bits, k_bits, g_load);
if(bench_lookup)
{
//bench procedure taken from emphf
stats_accumulator stats;
double tick = emphf::get_time_usecs();
size_t lookups = 0;
static const size_t lookups_per_sample = 1 << 16;
u_int64_t dumb=0;
double elapsed;
size_t runs = 10;
u_int64_t mphf_value;
for (size_t run = 0; run < runs; ++run) {
for (size_t ii = 0; ii < data_bench.size(); ++ii) {
mphf_value = PHF::hash<u_int64_t>(&phf,data_bench[ii]);
//do some silly work
dumb+= mphf_value;
if (++lookups == lookups_per_sample) {
elapsed = emphf::get_time_usecs() - tick;
stats.add(elapsed / (double)lookups);
tick = emphf::get_time_usecs();
lookups = 0;
}
}
}
printf("PHF bench lookups average %.2f ns +- stddev %.2f %% (fingerprint %llu) \n", 1000.0*stats.mean(),stats.relative_stddev(),dumb);
}
// if(bench_lookup)
// {
// u_int64_t dumb=0;
// u_int64_t mphf_value;
// begin = clock();
// for (u_int64_t i = 0; i < n; i++)
// {
// mphf_value = PHF::hash<u_int64_t>(&phf,data[i]);
// //do some silly work
// dumb+= mphf_value;
// }
//
// end = clock();
// printf("PHF %lu lookups in %.2fs, approx %.2f ns per lookup (fingerprint %llu) \n", n, (double)(end - begin) / CLOCKS_PER_SEC, ((double)(end - begin) / CLOCKS_PER_SEC)*1000000000/n,dumb);
// }
}
struct uint64_adaptor
{
emphf::byte_range_t operator()(u_int64_t const& s) const
{
const uint8_t* buf = reinterpret_cast<uint8_t const*>(&s);
const uint8_t* end = buf + sizeof(u_int64_t); // add the null terminator
return emphf::byte_range_t(buf, end);
}
};
// code from stackoverflow, thank you stack overflow.
// http://stackoverflow.com/questions/15904896/range-based-for-loop-on-a-dynamic-array
template <typename T>
struct wrapped_array {
wrapped_array(T* first, T* last) : begin_ {first}, end_ {last} {}
wrapped_array(T* first, std::ptrdiff_t size)
: wrapped_array {first, first + size} {}
T* begin() const noexcept { return begin_; }
T* end() const noexcept { return end_; }
T* begin_;
T* end_;
};
template <typename T>
wrapped_array<T> wrap_array(T* first, std::ptrdiff_t size) noexcept
{ return {first, size}; }
#ifdef EMPHF_HEM
typedef emphf::mphf_hem<emphf::jenkins64_hasher> mphf_t;
#else
#ifdef EMPHF_SCAN
typedef emphf::hypergraph_sorter_scan<uint32_t, emphf::mmap_memory_model> HypergraphSorter32; // follows compute_mphf_scan_mmap
//typedef emphf::hypergraph_sorter_scan<uint32_t, emphf::internal_memory_model> HypergraphSorter32; // follows compute_mphf_scan
#else
typedef emphf::hypergraph_sorter_seq<emphf::hypergraph<uint32_t>> HypergraphSorter32; // follows compute_mphf_seq
#endif
typedef emphf::jenkins64_hasher BaseHasher;
typedef emphf::mphf<BaseHasher> mphf_t;
#endif
mphf_t mphf; // let's put it as global variable so that mphf isn't destroyed after end of function
void do_emphf()
{
clock_t begin, end;
begin = clock();
#ifndef EMPHF_HEM
HypergraphSorter32 sorter;
#endif
uint64_adaptor adaptor;
//auto data_iterator = emphf::range(static_cast<const u_int64_t*>(data), static_cast<const u_int64_t*>(data+n));
//auto data_iterator = wrap_array(static_cast<const u_int64_t*>(data),n); // would also work, just to show off that i now know two solutions to wrap an iterator for emphf
#ifdef VANILLA_EMPHF
#ifdef EMPHF_HEM
emphf::mmap_memory_model mm;
auto data_iterator = emphf::range(static_cast<const u_int64_t*>(data), static_cast<const u_int64_t*>(data+n));
//from_disk not implemented
mphf_t(mm, n, data_iterator, adaptor).swap(mphf); // TODO: ajouter gamma, log2_expected_bucket (controle nb buckets)
string emphf_type = "vanilla emphf HEM";
#else
#ifdef EMPHF_SCAN
string emphf_type = "vanilla emphf scan";
#else
#ifdef EMPHF_SEQ
string emphf_type = "vanilla emphf seq";
#endif
#endif
if(from_disk)
{
auto data_iterator = file_binary("keyfile");
mphf_t(sorter, n, data_iterator, adaptor).swap(mphf);
}
else
{
auto data_iterator = emphf::range(static_cast<const u_int64_t*>(data), static_cast<const u_int64_t*>(data+n));
mphf_t(sorter, n, data_iterator, adaptor).swap(mphf);
}
#endif
#else
gatb::core::tools::dp::IteratorListener* progress = nullptr; // added by gatb in emphf
mphf_t(sorter, n, data_iterator, adaptor, progress).swap(mphf);
string emphf_type = "gatb-core emphf";
#endif
end = clock();
warnx("[%s] constructed perfect hash for %zu keys in %fs", emphf_type.c_str(), n, (double)(end - begin) / CLOCKS_PER_SEC);
if(bench_lookup && !from_disk)
{
u_int64_t dumb=0;
u_int64_t mphf_value;
begin = clock();
for (u_int64_t i = 0; i < n; i++)
{
mphf_value = mphf.lookup(data[i],adaptor);
//do some silly work
dumb+= mphf_value;
}
end = clock();
printf("emphf %lu lookups in %.2fs, approx %.2f ns per lookup (fingerprint %llu) \n", n, (double)(end - begin) / CLOCKS_PER_SEC, ((double)(end - begin) / CLOCKS_PER_SEC)*1000000000/n,dumb);
}
if (bench_lookup && from_disk)
{
auto input_range = file_binary("benchfile");
vector<u_int64_t> sample;
u_int64_t mphf_value;
//copy sample in ram
for (auto const& key: input_range) {
sample.push_back(key);
}
//bench procedure taken from emphf
stats_accumulator stats;
double tick = emphf::get_time_usecs();
size_t lookups = 0;
static const size_t lookups_per_sample = 1 << 16;
u_int64_t dumb=0;
double elapsed;
size_t runs = 10;
for (size_t run = 0; run < runs; ++run) {
for (size_t ii = 0; ii < sample.size(); ++ii) {
mphf_value = mphf.lookup(sample[ii],adaptor);
//do some silly work
dumb+= mphf_value;
if (++lookups == lookups_per_sample) {
elapsed = emphf::get_time_usecs() - tick;
stats.add(elapsed / (double)lookups);
tick = emphf::get_time_usecs();
lookups = 0;
}
}
}
printf("EMPHF bench lookups average %.2f ns +- stddev %.2f %% (fingerprint %llu) \n", 1000.0*stats.mean(),stats.relative_stddev(),dumb);
}
// get true mphf size
// a bit of a dirty hack, could be made cleaner
std::ofstream os("dummy_emphf", std::ios::binary);
mphf.save(os);
std::ifstream is("dummy_emphf", std::ios::binary);
mphf.load(is);
size_t file_size = (size_t)is.tellg();
double bits_per_key = 8.0 * (double)file_size / (double)mphf.size();
std::cout << "EMPHF bits_per_key: " << bits_per_key << std::endl;
}
void do_chd()
{
clock_t begin, end;
begin = clock();
//Open file with newline separated list of keys
FILE * keys_fd = fopen("keyfile_chd.txt", "r");
cmph_t *hash = NULL;
if (keys_fd == NULL)
{
fprintf(stderr, "File \"keys_chd.txt\" not found\n");
exit(1);
}
// Source of keys
cmph_io_adapter_t *source = cmph_io_nlfile_adapter(keys_fd);
cmph_config_t *config = cmph_config_new(source);
cmph_config_set_algo(config, CMPH_CHD);
hash = cmph_new(config);
cmph_config_destroy(config);
end = clock();
warnx("[%s] constructed perfect hash for %zu keys in %fs", "CMPH_CHD", n, (double)(end - begin) / CLOCKS_PER_SEC);
if (bench_lookup && from_disk)
{
vector<std::string> sample;
u_int64_t mphf_value;
FILE * keyb = fopen ("benchfile_chd.txt","r");
//copy sample in ram
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
while ((linelen = getline(&line, &linecap, keyb)) > 0)
{
sample.push_back(line);
//printf("%s\n",sample[sample.size()-1].c_str());
}
fclose(keyb);
printf("sample size %lu \n",sample.size());
//bench procedure taken from emphf
stats_accumulator stats;
double tick = emphf::get_time_usecs();
size_t lookups = 0;
static const size_t lookups_per_sample = 1 << 16;
u_int64_t dumb=0;
double elapsed;
size_t runs = 10;
for (size_t run = 0; run < runs; ++run) {
for (size_t ii = 0; ii < sample.size(); ++ii) {
mphf_value = cmph_search(hash, sample[ii].c_str(), (cmph_uint32)sample[ii].size());
//do some silly work
dumb+= mphf_value;
if (++lookups == lookups_per_sample) {
elapsed = emphf::get_time_usecs() - tick;
stats.add(elapsed / (double)lookups);
tick = emphf::get_time_usecs();
lookups = 0;
}
}
}
printf("CMPH_CHD bench lookups average %.2f ns +- stddev %.2f %% (fingerprint %llu) \n", 1000.0*stats.mean(),stats.relative_stddev(),dumb);
}
FILE * mphf_fd = fopen("chd_file", "w");
cmph_dump(hash, mphf_fd);
fclose(mphf_fd);
FILE * f = fopen("chd_file", "r");
fseek(f, 0, SEEK_END);
unsigned long len = (unsigned long)ftell(f);
fclose(f);
printf("CHD bits_per_key %.3f \n",len*8/(double)n);
//Destroy hash
cmph_destroy(hash);
cmph_io_nlfile_adapter_destroy(source);
fclose(keys_fd);
}
int main (int argc, char* argv[])
{
if ( argc < 2 )
cout << "Constructing a MPHF with (default) n=" << n << " elements" << std::endl;
else
{
n = strtoul(argv[1], NULL,0);
cout << "Constructing a MPHF with n=" << n << " elements" << std::endl;
}
for (int ii=2; ii<argc; ii++)
{
if(!strcmp("-bench",argv[ii])) bench_lookup= true;
if(!strcmp("-fromdisk",argv[ii])) from_disk= true;
if(!strcmp("-emphf",argv[ii])) only_emphf= true;
if(!strcmp("-phf",argv[ii])) only_phf= true;
if(!strcmp("-chd",argv[ii])) only_chd= true;
}
FILE * key_file = NULL;
FILE * key_file_chd = NULL;
FILE * bench_file = NULL;
FILE * bench_file_chd = NULL;
if(from_disk)
{
key_file = fopen("keyfile","w+");
key_file_chd = fopen("keyfile_chd.txt","w+");
//simple mehtod to ensure all elements are unique, but not random
u_int64_t step = ULLONG_MAX / n;
u_int64_t current = 0;
fwrite(¤t, sizeof(u_int64_t), 1, key_file);
for (u_int64_t i = 1; i < n; i++)
{
current = current + step;
fwrite(¤t, sizeof(u_int64_t), 1, key_file);
fprintf(key_file_chd,"%llx\n",current);
}
fclose(key_file);
fclose(key_file_chd);
printf("key file generated \n");
if(bench_lookup)
{
bench_file = fopen("benchfile","w+");
bench_file_chd = fopen("benchfile_chd.txt","w+");
//create a test file
//if n < 10 M take all elements, otherwise regular sample to have 10 M elements
u_int64_t stepb = n / 10000000;
if(stepb==0) stepb=1;
auto data_iterator = file_binary("keyfile");
u_int64_t cpt = 0;
nb_in_bench_file = 0;
for (auto const& key: data_iterator) {
if( (cpt % stepb) == 0)
{
fwrite(&key, sizeof(u_int64_t), 1, bench_file);
fprintf(bench_file_chd,"%llx\n",key);
nb_in_bench_file ++;
}
cpt++;
}
fclose(bench_file);
fclose(bench_file_chd);
}
}
else
{
// create a bunch of sorted 64-bits integers (it doesnt matter if they were sorted actually)
// adapted from http://stackoverflow.com/questions/14009637/c11-random-numbers
static std::mt19937_64 rng;
rng.seed(std::mt19937_64::default_seed);
data = new u_int64_t[n];
for (u_int64_t i = 1; i < n; i++)
data[i] = rng();
if(bench_lookup)
{
u_int64_t stepb = n / 10000000;
if(stepb==0) stepb=1;
for (int ii=0; ii<n; ii++) {
if( (ii % stepb) == 0)
{
data_bench.push_back(data[ii]);
}
}
}
}
memory_usage("initial data allocation");
if ((!only_phf) && (!only_chd))
{
cout << endl << "Construction with 'emphf' library.. " << endl;
do_emphf();
memory_usage("after emphf construction", "emphf");
if (only_emphf)
return 0;
}
if ((!only_emphf) && (!only_phf))
{
if(from_disk)
{
cout << endl << "Construction with 'chd' library.. " << endl;
do_chd();
memory_usage("after chd construction", "chd");
}
if (only_chd)
return 0;
}
if ((!only_emphf) && (!only_chd))
{
if(!from_disk)
{
cout << endl << "Construction with 'phf' library.. " << endl;
do_phf();
memory_usage("after phf construction","phf");
if (only_phf)
return 0;
}
}
}