-
Notifications
You must be signed in to change notification settings - Fork 294
/
onDiskMQF.cpp
3180 lines (2853 loc) · 106 KB
/
onDiskMQF.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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#if 0
# include <assert.h>
#else
# define assert(x)
#endif
#include <string.h>
#include <inttypes.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fstream>
#include <algorithm>
#include <stdexcept>
#include "gqf.h"
#include "onDiskMQF.h"
#include <iostream>
#include <map>
#include "utils.h"
#include <type_traits>
namespace onDiskMQF_Namespace{
/******************************************************************
* Code for managing the metadata bits and slots w/o interpreting *
* the content of the slots.
******************************************************************/
#define LOG2(X) ((unsigned) (8*sizeof (unsigned long long) - __builtin_clzll((X)) - 1))
/* Must be >= 6. 6 seems fastest. */
#define BLOCK_OFFSET_BITS (6)
//#define SLOTS_PER_BLOCK (1ULL << BLOCK_OFFSET_BITS)
//#define METADATA_WORDS_PER_BLOCK ((SLOTS_PER_BLOCK + 63) / 64)
#define NUM_SLOTS_TO_LOCK (1ULL<<16)
#define CLUSTER_SIZE (1ULL<<14)
#define METADATA_WORD(field,slot_index) (get_block((slot_index) / \
SLOTS_PER_BLOCK)->field[((slot_index) % SLOTS_PER_BLOCK) / 64])
uint64_t bitmaskLookup[]={0,1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575,
2097151, 4194303, 8388607, 16777215, 33554431, 67108863, 134217727, 268435455, 536870911, 1073741823,
2147483647, 4294967295, 8589934591, 17179869183, 34359738367, 68719476735, 137438953471, 274877906943, 549755813887, 1099511627775,
2199023255551, 4398046511103, 8796093022207, 17592186044415, 35184372088831, 70368744177663, 140737488355327, 281474976710655, 562949953421311, 1125899906842623,
2251799813685247, 4503599627370495, 9007199254740991, 18014398509481983, 36028797018963967, 72057594037927935, 144115188075855871, 288230376151711743, 576460752303423487, 1152921504606846975,
2305843009213693951, 4611686018427387903, 9223372036854775807, 18446744073709551615};
//#define BITMASK(nbits) ((nbits) == 64 ? 0xffffffffffffffff : (1ULL << (nbits)) \
- 1ULL)
#define BITMASK(nbits) bitmaskLookup[nbits]
#define MAX_VALUE(nbits) ((1ULL << (nbits)) - 1)
#define BILLION 1000000000L
typedef struct __attribute__ ((__packed__)) qfblock {
/* Code works with uint16_t, uint32_t, etc, but uint8_t seems just as fast as
* anything else */
uint8_t offset;
uint64_t occupieds[METADATA_WORDS_PER_BLOCK];
uint64_t runends[METADATA_WORDS_PER_BLOCK];
#if BITS_PER_SLOT == 8
uint8_t slots[SLOTS_PER_BLOCK];
#elif BITS_PER_SLOT == 16
uint16_t slots[SLOTS_PER_BLOCK];
#elif BITS_PER_SLOT == 32
uint32_t slots[SLOTS_PER_BLOCK];
#elif BITS_PER_SLOT == 64
uint64_t slots[SLOTS_PER_BLOCK];
#elif BITS_PER_SLOT != 0
uint8_t slots[SLOTS_PER_BLOCK * BITS_PER_SLOT / 8];
#else
uint8_t slots[];
#endif
} qfblock;
static __inline__ unsigned long long rdtsc(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
/**
* Try to acquire a lock once and return even if the lock is busy.
* If spin flag is set, then spin until the lock is available.
*/
static inline bool onDiskMQF_spin_lock(volatile int *lock, bool flag_spin)
{
if (!flag_spin) {
return !__sync_lock_test_and_set(lock, 1);
} else {
while (__sync_lock_test_and_set(lock, 1))
while (*lock);
return true;
}
return false;
}
static inline void onDiskMQF_spin_unlock(volatile int *lock)
{
__sync_lock_release(lock);
return;
}
template<uint64_t bitsPerSlot>
bool _onDiskMQF<bitsPerSlot>::spin_lock(uint64_t hash_bucket_index, bool spin, bool flag)
{
_onDiskMQF<bitsPerSlot> *cf=this;
uint64_t hash_bucket_lock_offset = hash_bucket_index % NUM_SLOTS_TO_LOCK;
if (flag) {
if (!onDiskMQF_spin_lock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK], spin))
return false;
if (NUM_SLOTS_TO_LOCK - hash_bucket_lock_offset <= CLUSTER_SIZE) {
if (!onDiskMQF_spin_lock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK+1],
spin)) {
onDiskMQF_spin_unlock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK]);
return false;
}
}
} else {
/* take the lock for two lock-blocks; the lock-block in which the
* hash_bucket_index falls and the next lock-block */
if (hash_bucket_index >= NUM_SLOTS_TO_LOCK && hash_bucket_lock_offset <=
CLUSTER_SIZE) {
if (!onDiskMQF_spin_lock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK-1], spin))
return false;
}
if (!onDiskMQF_spin_lock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK], spin)) {
if (hash_bucket_index >= NUM_SLOTS_TO_LOCK && hash_bucket_lock_offset <=
CLUSTER_SIZE)
onDiskMQF_spin_unlock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK-1]);
return false;
}
if (!onDiskMQF_spin_lock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK+1],
spin)) {
onDiskMQF_spin_unlock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK]);
if (hash_bucket_index >= NUM_SLOTS_TO_LOCK && hash_bucket_lock_offset <=
CLUSTER_SIZE)
onDiskMQF_spin_unlock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK-1]);
return false;
}
}
return true;
}
template<uint64_t bitsPerSlot>
void _onDiskMQF<bitsPerSlot>::unlock(uint64_t hash_bucket_index, bool flag)
{
_onDiskMQF<bitsPerSlot>* cf=this;
uint64_t hash_bucket_lock_offset = hash_bucket_index % NUM_SLOTS_TO_LOCK;
if (flag) {
if (NUM_SLOTS_TO_LOCK - hash_bucket_lock_offset <= CLUSTER_SIZE) {
onDiskMQF_spin_unlock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK+1]);
}
onDiskMQF_spin_unlock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK]);
} else {
onDiskMQF_spin_unlock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK+1]);
onDiskMQF_spin_unlock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK]);
if (hash_bucket_index >= NUM_SLOTS_TO_LOCK && hash_bucket_lock_offset <=
CLUSTER_SIZE)
onDiskMQF_spin_unlock(&cf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK-1]);
}
}
template<uint64_t bitsPerSlot>
void _onDiskMQF<bitsPerSlot>::modify_metadata(uint64_t *metadata, int cnt)
{
_onDiskMQF<bitsPerSlot> *cf=this;
onDiskMQF_spin_lock(&cf->mem->metadata_lock, true);
*metadata = *metadata + cnt;
onDiskMQF_spin_unlock(&cf->mem->metadata_lock);
return;
}
static inline int popcnt(uint64_t val)
{
asm("popcnt %[val], %[val]"
: [val] "+r" (val)
:
: "cc");
return val;
}
static inline int64_t bitscanreverse(uint64_t val)
{
if (val == 0) {
return -1;
} else {
asm("bsr %[val], %[val]"
: [val] "+r" (val)
:
: "cc");
return val;
}
}
static inline int popcntv(const uint64_t val, int ignore)
{
if (ignore % 64)
return popcnt (val & ~BITMASK(ignore % 64));
else
return popcnt(val);
}
// Returns the number of 1s up to (and including) the pos'th bit
// Bits are numbered from 0
static inline int bitrank(uint64_t val, int pos) {
val = val & ((2ULL << pos) - 1);
asm("popcnt %[val], %[val]"
: [val] "+r" (val)
:
: "cc");
return val;
}
/**
* Returns the position of the k-th 1 in the 64-bit word x.
* k is 0-based, so k=0 returns the position of the first 1.
*
* Uses the broadword selection algorithm by Vigna [1], improved by Gog
* and Petri [2] and Vigna [3].
*
* [1] Sebastiano Vigna. Broadword Implementation of Rank/Select
* Queries. WEA, 2008
*
* [2] Simon Gog, Matthias Petri. Optimized succinct data
* structures for massive data. Softw. Pract. Exper., 2014
*
* [3] Sebastiano Vigna. MG4J 5.2.1. http://mg4j.di.unimi.it/
* The following code is taken from
* https://github.com/facebook/folly/blob/b28186247104f8b90cfbe094d289c91f9e413317/folly/experimental/Select64.h
*/
const uint8_t kSelectInByte[2048] = {
8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0,
1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0,
2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0,
1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0,
3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 7, 0,
1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0,
2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0,
1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0,
1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 8, 8, 8, 1,
8, 2, 2, 1, 8, 3, 3, 1, 3, 2, 2, 1, 8, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2,
2, 1, 8, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1, 5, 4, 4, 1, 4, 2, 2, 1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8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7
};
static inline uint64_t _select64(uint64_t x, int k)
{
if (k >= popcnt(x)) { return 64; }
const uint64_t kOnesStep4 = 0x1111111111111111ULL;
const uint64_t kOnesStep8 = 0x0101010101010101ULL;
const uint64_t kMSBsStep8 = 0x80ULL * kOnesStep8;
uint64_t s = x;
s = s - ((s & 0xA * kOnesStep4) >> 1);
s = (s & 0x3 * kOnesStep4) + ((s >> 2) & 0x3 * kOnesStep4);
s = (s + (s >> 4)) & 0xF * kOnesStep8;
uint64_t byteSums = s * kOnesStep8;
uint64_t kStep8 = k * kOnesStep8;
uint64_t geqKStep8 = (((kStep8 | kMSBsStep8) - byteSums) & kMSBsStep8);
uint64_t place = popcnt(geqKStep8) * 8;
uint64_t byteRank = k - (((byteSums << 8) >> place) & (uint64_t)(0xFF));
return place + kSelectInByte[((x >> place) & 0xFF) | (byteRank << 8)];
}
// Returns the position of the rank'th 1. (rank = 0 returns the 1st 1)
// Returns 64 if there are fewer than rank+1 1s.
static inline uint64_t bitselect(uint64_t val, int rank) {
#ifdef __SSE4_2_
uint64_t i = 1ULL << rank;
asm("pdep %[val], %[mask], %[val]"
: [val] "+r" (val)
: [mask] "r" (i));
asm("tzcnt %[bit], %[index]"
: [index] "=r" (i)
: [bit] "g" (val)
: "cc");
return i;
#endif
return _select64(val, rank);
}
static inline uint64_t bitselectv(const uint64_t val, int ignore, int rank)
{
return bitselect(val & ~BITMASK(ignore % 64), rank);
}
inline void writeAndFreeBlocks(uint64_t memoryBufferIndex)
{
// if(qf->blocksFilePos[memoryBufferIndex]!=0)
// return;
//
// diskMQFStream.seek(qf->blocksFilePos[memoryBufferIndex]);
// diskMQFStream.write((char*)blocks[memoryBufferIndex],(sizeof(qfblock) +
// diskParams->nBlocksPerPointer*(8 * bits_per_slot )));
// for(uint64_t i=0;i<diskParams->nBlocksPerPointer;i++){
// qf->blocksFilePos[memoryBufferIndex]=0;
// }
// qf->blocksPointers[qf->reverseBlocksPointer[memoryBufferIndex]]=NULL;
}
// template<uint64_t bitsPerSlot>
// inline typename stxxl::vector<onDisk_qfblock<bitsPerSlot> >::iterator get_block(onDiskMQF *qf, uint64_t block_index)
// {
// // printf("block = %p\n",(void*)(((char *)qf->blocks) + block_index * (sizeof(qfblock) +
// // qf->metadata->bits_per_slot * 8 +
// // 8*qf->metadata->fixed_counter_size +
// // 8*qf->metadata->tag_bits
// // )) );
// //printf("blocks start=%p\n",qf->blocks );
// // uint64_t BasepointerIndex= block_index / qf->diskParams->nBlocksPerPointer;
// // char* basePointer=(char*)qf->blocksPointers[BasepointerIndex];
// // if(basePointer==NULL)
// // {
// // //load
// // for(uint64_t i=0;i<qf->diskParams->nBlocksPerIOBatch;i+=qf->diskParams->nBlocksPerPointer)
// // {
// // writeAndFreeBlocks(qf->diskParams->memoryBufferPos+i);
// // }
// // }
// // uint64_t pointerShift= (block_index % qf->diskParams->nBlocksPerPointer)
// // *(sizeof(qfblock) + (8 * qf->metadata->bits_per_slot ));
// // return (qfblock*)(basePointer+pointerShift);
// //return (qfblock *)(((char *)qf->blocks) + block_index * (sizeof(qfblock) +
// // qf->metadata->bits_per_slot * 8
// // ));
//
// typename stxxl::vector<onDisk_qfblock<bitsPerSlot> >::iterator it = begin(qf->blocks);
//
// //return NULL;
//
// it+=block_index;
// //return (qfblock*)&(*it);
// return it;
// }
// template<uint64_t bitsPerSlot>
// inline typename stxxl::vector<onDisk_qfblock<bitsPerSlot> >::const_iterator get_block_const(onDiskMQF *qf, uint64_t block_index)
// {
// typename stxxl::vector<onDisk_qfblock<bitsPerSlot> >::const_iterator it = begin(qf->blocks);
// it+=block_index;
// return it;
// }
template<uint64_t bitsPerSlot>
inline int _onDiskMQF<bitsPerSlot>::is_runend(uint64_t index)
{
return (METADATA_WORD( runends, index) >> ((index % SLOTS_PER_BLOCK) %
64)) & 1ULL;
}
template<uint64_t bitsPerSlot>
inline int _onDiskMQF<bitsPerSlot>::is_occupied(uint64_t index)
{
return (METADATA_WORD( occupieds, index) >> ((index % SLOTS_PER_BLOCK) %
64)) & 1ULL;
}
/* Little-endian code .... Big-endian is TODO */
template<uint64_t bitsPerSlot>
inline uint64_t _onDiskMQF<bitsPerSlot>::_get_slot(uint64_t index)
{
_onDiskMQF<bitsPerSlot> *qf=this;
assert(index < qf->metadata->xnslots);
/* Should use __uint128_t to support up to 64-bit remainders, but gcc seems
* to generate buggy code. :/ */
uint64_t *p = (uint64_t *)&qf->get_block_const( index /
SLOTS_PER_BLOCK)->slots[(index %
SLOTS_PER_BLOCK)
* qf->metadata->bits_per_slot / 8];
return (uint64_t)(((*p) >> (((index % SLOTS_PER_BLOCK) *
qf->metadata->bits_per_slot) % 8)) &
BITMASK(qf->metadata->bits_per_slot));
}
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::_set_slot(uint64_t index, uint64_t value)
{
_onDiskMQF<bitsPerSlot> *qf=this;
/* Should use __uint128_t to support up to 64-bit remainders, but gcc seems
* to generate buggy code. :/ */
//printf("ss %d\n",(index %SLOTS_PER_BLOCK)* qf->metadata->bits_per_slot / 8 );
uint64_t *p = (uint64_t *)&qf->get_block( index /
SLOTS_PER_BLOCK)->slots[(index %
SLOTS_PER_BLOCK)
* qf->metadata->bits_per_slot / 8];
int shift = ((index % SLOTS_PER_BLOCK) * qf->metadata->bits_per_slot) % 8;
uint64_t mask = BITMASK(qf->metadata->bits_per_slot)<< shift;
value <<= shift;
*p=*p ^ ((*p ^ value) & mask);
}
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::set_slot(uint64_t index, uint64_t value){
_onDiskMQF<bitsPerSlot> *qf=this;
uint64_t original_value=_get_slot(index);
value<<=qf->metadata->fixed_counter_size;
uint64_t mask=BITMASK(qf->metadata->fixed_counter_size);
original_value&=mask;
value|=original_value;
_set_slot(index,value);
}
template<uint64_t bitsPerSlot>
inline uint64_t _onDiskMQF<bitsPerSlot>::get_slot(uint64_t index)
{
_onDiskMQF<bitsPerSlot> *qf=this;
uint64_t mask=BITMASK(qf->metadata->key_remainder_bits);
uint64_t t=_get_slot(index);
t >>= qf->metadata->fixed_counter_size;
return t&mask;
}
template<uint64_t bitsPerSlot>
inline uint64_t _onDiskMQF<bitsPerSlot>::get_fixed_counter(uint64_t index)
{
_onDiskMQF<bitsPerSlot> *qf=this;
uint64_t t=_get_slot(index);
uint64_t mask=BITMASK(qf->metadata->fixed_counter_size);
return t&mask;
// uint64_t res=0;
// uint64_t base=1;
// uint64_t* p=(uint64_t*)((uint8_t*)qf->get_block( index /SLOTS_PER_BLOCK)->slots+
// (SLOTS_PER_BLOCK * qf->metadata->bits_per_slot )/ 8);
//
// for(int i=qf->metadata->fixed_counter_size-1;i>=0;i--){
// res+= base*(( p[i] >> ((index % SLOTS_PER_BLOCK) %64)) & 1ULL);
// base*=2;
// }
//
// return res;
}
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::set_fixed_counter(uint64_t index,uint64_t value)
{
_onDiskMQF<bitsPerSlot> *qf=this;
uint64_t mask=BITMASK(qf->metadata->fixed_counter_size);
uint64_t tvalue=_get_slot(index);
tvalue=tvalue ^ ((tvalue ^ value) & mask);
_set_slot(index,tvalue);
}
template<uint64_t bitsPerSlot>
inline uint64_t _onDiskMQF<bitsPerSlot>::_get_tag(uint64_t index)
{
_onDiskMQF<bitsPerSlot> *qf=this;
uint64_t mask=BITMASK(qf->metadata->tag_bits);
uint64_t t=_get_slot(index);
t >>= (qf->metadata->fixed_counter_size+qf->metadata->key_remainder_bits);
return t&mask;
}
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::set_tag(uint64_t index,uint64_t value)
{
_onDiskMQF<bitsPerSlot> *qf=this;
uint64_t original_value=_get_slot(index);
value<<=(qf->metadata->fixed_counter_size+qf->metadata->key_remainder_bits);
uint64_t mask=BITMASK(qf->metadata->fixed_counter_size+qf->metadata->key_remainder_bits);
original_value&=mask;
value|=original_value;
_set_slot(index,value);
}
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::super_get(uint64_t index,uint64_t* slot,uint64_t *fcounter)
{
_onDiskMQF<bitsPerSlot> *qf=this;
uint64_t t=_get_slot(index);
*fcounter=t & BITMASK(qf->metadata->fixed_counter_size);
*slot= t >> qf->metadata->fixed_counter_size & BITMASK(qf->metadata->key_remainder_bits) ;
}
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::super_set(uint64_t index,uint64_t slot,uint64_t fcounter)
{
_onDiskMQF<bitsPerSlot> *qf=this;
uint64_t mask=BITMASK(qf->metadata->fixed_counter_size);
slot<<=qf->metadata->fixed_counter_size;
uint64_t newValue= slot ^ ((slot ^ fcounter) & mask);
//cout<<"slot= "<<slot<<" fcounter= "<<fcounter<<" newValue= "<<newValue<<endl;
uint64_t original_value=_get_slot(index);
mask=BITMASK(qf->metadata->fixed_counter_size+
qf->metadata->key_remainder_bits);
newValue= original_value ^ ((original_value ^ newValue) & mask);
_set_slot(index,newValue);
}
template<uint64_t bitsPerSlot>
inline uint64_t _onDiskMQF<bitsPerSlot>::block_offset(uint64_t blockidx)
{
_onDiskMQF<bitsPerSlot> *qf=this;
/* If we have extended counters and a 16-bit (or larger) offset
field, then we can safely ignore the possibility of overflowing
that field. */
if (sizeof(qf->get_block(0)->offset) > 1 ||
qf->get_block( blockidx)->offset < BITMASK(8*sizeof(qf->get_block(0)->offset)))
return qf->get_block( blockidx)->offset;
return run_end( SLOTS_PER_BLOCK * blockidx - 1) - SLOTS_PER_BLOCK *
blockidx + 1;
}
template<uint64_t bitsPerSlot>
inline uint64_t _onDiskMQF<bitsPerSlot>::run_end(uint64_t hash_bucket_index)
{
_onDiskMQF<bitsPerSlot> *qf=this;
uint64_t bucket_block_index = hash_bucket_index / SLOTS_PER_BLOCK;
uint64_t bucket_intrablock_offset = hash_bucket_index % SLOTS_PER_BLOCK;
uint64_t bucket_blocks_offset = block_offset( bucket_block_index);
uint64_t bucket_intrablock_rank = bitrank(qf->get_block(
bucket_block_index)->occupieds[0],
bucket_intrablock_offset);
if (bucket_intrablock_rank == 0) {
if (bucket_blocks_offset <= bucket_intrablock_offset)
return hash_bucket_index;
else
return SLOTS_PER_BLOCK * bucket_block_index + bucket_blocks_offset - 1;
}
uint64_t runend_block_index = bucket_block_index + bucket_blocks_offset /
SLOTS_PER_BLOCK;
uint64_t runend_ignore_bits = bucket_blocks_offset % SLOTS_PER_BLOCK;
uint64_t runend_rank = bucket_intrablock_rank - 1;
uint64_t runend_block_offset = bitselectv(qf->get_block(
runend_block_index)->runends[0],
runend_ignore_bits, runend_rank);
if (runend_block_offset == SLOTS_PER_BLOCK) {
if (bucket_blocks_offset == 0 && bucket_intrablock_rank == 0) {
/* The block begins in empty space, and this bucket is in that region of
* empty space */
return hash_bucket_index;
} else {
do {
runend_rank -= popcntv(qf->get_block(
runend_block_index)->runends[0],
runend_ignore_bits);
runend_block_index++;
runend_ignore_bits = 0;
runend_block_offset = bitselectv(qf->get_block(
runend_block_index)->runends[0],
runend_ignore_bits, runend_rank);
} while (runend_block_offset == SLOTS_PER_BLOCK);
}
}
uint64_t runend_index = SLOTS_PER_BLOCK * runend_block_index +
runend_block_offset;
if (runend_index < hash_bucket_index)
return hash_bucket_index;
else
return runend_index;
}
// inline uint64_t run_end2(onDiskMQF *qf, uint64_t hash_bucket_index)
// {
// uint8_t offset=hash_bucket_index % SLOTS_PER_BLOCK;
// uint64_t t= METADATA_WORD( runends, hash_bucket_index)
// &BITMASK(offset);
// uint64_t tt=hash_bucket_index / SLOTS_PER_BLOCK;
// hash_bucket_index+=SLOTS_PER_BLOCK-offset;
// while(t==0){
// t= METADATA_WORD( runends, hash_bucket_index);
// hash_bucket_index+=64;
// tt+=1;
// }
// uint64_t res=(tt*64)+SLOTS_PER_BLOCK-LOG2(t)-1;
// return res;
// }
template<uint64_t bitsPerSlot>
inline int _onDiskMQF<bitsPerSlot>::offset_lower_bound(uint64_t slot_index)
{
_onDiskMQF<bitsPerSlot> *qf=this;
typename stxxl::vector<onDisk_qfblock<bitsPerSlot> >::const_iterator b = qf->get_block_const( slot_index / SLOTS_PER_BLOCK);
const uint64_t slot_offset = slot_index % SLOTS_PER_BLOCK;
const uint64_t boffset = b->offset;
const uint64_t occupieds = b->occupieds[0] & BITMASK(slot_offset+1);
assert(SLOTS_PER_BLOCK == 64);
if (boffset <= slot_offset) {
const uint64_t runends = (b->runends[0] & BITMASK(slot_offset)) >> boffset;
return popcnt(occupieds) - popcnt(runends);
}
return boffset - slot_offset + popcnt(occupieds);
}
template<uint64_t bitsPerSlot>
inline int _onDiskMQF<bitsPerSlot>::is_empty2(uint64_t slot_index)
{
return offset_lower_bound( slot_index) == 0;
}
template<uint64_t bitsPerSlot>
inline int _onDiskMQF<bitsPerSlot>::might_be_empty(uint64_t slot_index)
{
return !is_occupied( slot_index)
&& !is_runend( slot_index);
}
template<uint64_t bitsPerSlot>
inline int _onDiskMQF<bitsPerSlot>::probably_is_empty(uint64_t slot_index)
{
_onDiskMQF<bitsPerSlot> *qf=this;
return get_slot( slot_index) == 0
&& !is_occupied( slot_index)
&& !is_runend( slot_index);
}
template<uint64_t bitsPerSlot>
inline uint64_t _onDiskMQF<bitsPerSlot>::find_first_empty_slot(uint64_t from)
{
do {
int t = offset_lower_bound( from);
if(t<0)
{
cout<<"here"<<endl;
}
assert(t>=0);
if (t == 0)
break;
from = from + t;
} while(1);
return from;
}
static inline uint64_t shift_into_b(const uint64_t a, const uint64_t b,
const int bstart, const int bend,
const int amount)
{
const uint64_t a_component = bstart == 0 ? (a >> (64 - amount)) : 0;
const uint64_t b_shifted_mask = BITMASK(bend - bstart) << bstart;
const uint64_t b_shifted = ((b_shifted_mask & b) << amount) & b_shifted_mask;
const uint64_t b_mask = ~b_shifted_mask;
return a_component | b_shifted | (b & b_mask);
}
#define REMAINDER_WORD(i) ((uint64_t *)&(get_block( (i)/metadata->bits_per_slot)->slots[8 * ((i) % metadata->bits_per_slot)]))
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::shift_remainders(const uint64_t start_index, const
uint64_t empty_index)
{
_onDiskMQF<bitsPerSlot> *qf=this;
uint64_t last_word = (empty_index + 1) * qf->metadata->bits_per_slot / 64;
const uint64_t first_word = start_index * qf->metadata->bits_per_slot / 64;
int bend = ((empty_index + 1) * qf->metadata->bits_per_slot) % 64;
const int bstart = (start_index * qf->metadata->bits_per_slot) % 64;
while (last_word != first_word) {
*REMAINDER_WORD( last_word) = shift_into_b(*REMAINDER_WORD( last_word-1),
*REMAINDER_WORD( last_word),
0, bend, qf->metadata->bits_per_slot);
last_word--;
bend = 64;
}
*REMAINDER_WORD( last_word) = shift_into_b(0, *REMAINDER_WORD(
last_word),
bstart, bend,
qf->metadata->bits_per_slot);
}
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::dump_block(uint64_t i)
{
_onDiskMQF<bitsPerSlot>* qf=this;
uint64_t j;
printf("#Block %lu \n",i );
printf("%-192d", qf->get_block( i)->offset);
printf("\n");
for (j = 0; j < SLOTS_PER_BLOCK; j++)
printf("%02lx ", j);
printf("\n");
for (j = 0; j < SLOTS_PER_BLOCK; j++)
printf(" %d ", (qf->get_block( i)->occupieds[j/64] & (1ULL << (j%64))) ? 1 : 0);
printf("\n");
for (j = 0; j < SLOTS_PER_BLOCK; j++)
printf(" %d ", (qf->get_block( i)->runends[j/64] & (1ULL << (j%64))) ? 1 : 0);
printf("\n");
#if BITS_PER_SLOT == 8 || BITS_PER_SLOT == 16 || BITS_PER_SLOT == 32
for (j = 0; j < SLOTS_PER_BLOCK; j++)
printf("%02x ", qf->get_block( i)->slots[j]);
#elif BITS_PER_SLOT == 64
for (j = 0; j < SLOTS_PER_BLOCK; j++)
printf("%02lx ", qf->get_block( i)->slots[j]);
#else
//for (j = 0; j < SLOTS_PER_BLOCK * qf->metadata->bits_per_slot / 8; j++)
// printf("%02x ", qf->get_block( i)->slots[j]);
#endif
for(int j=0;j<64 && (j+64*i)<qf->metadata->xnslots;j++)
{
printf("%lu ", get_slot(j+64*i));
}
printf("\n fixed counter \n");
for(int j=0;j<64&& (j+64*i)<qf->metadata->xnslots;j++)
{
printf("%lu ", get_fixed_counter(j+64*i));
}
printf("\n tags \n");
for(int j=0;j<64&& (j+64*i)<qf->metadata->xnslots;j++)
{
printf("%lu ", _get_tag(j+64*i));
}
printf("\n");
printf("\n");
}
template<uint64_t bitsPerSlot>
void _onDiskMQF<bitsPerSlot>::dump()
{
_onDiskMQF<bitsPerSlot>* qf=this;
uint64_t i;
printf("%lu %lu %lu\n",
qf->metadata->nblocks,
qf->metadata->ndistinct_elts,
qf->metadata->nelts);
for (i = 0; i < qf->metadata->nblocks; i++) {
if(i==540){
cout<<"last bloc"<<endl;
}
dump_block(i);
}
printf("End\n");
}
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::find_next_n_empty_slots(uint64_t from, uint64_t n,
uint64_t *indices)
{
while (n) {
indices[--n] = find_first_empty_slot( from);
from = indices[n] + 1;
}
}
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::shift_slots(int64_t first, uint64_t last, uint64_t
distance)
{
int64_t i;
if (distance == 1)
shift_remainders( first, last+1);
else
for (i = last; i >= first; i--)
_set_slot( i + distance, _get_slot( i));
}
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::shift_runends(int64_t first, uint64_t last,
uint64_t distance)
{
assert(last < metadata->xnslots && distance < 64);
uint64_t first_word = first / 64;
uint64_t bstart = first % 64;
uint64_t last_word = (last + distance + 1) / 64;
uint64_t bend = (last + distance + 1) % 64;
if (last_word != first_word) {
METADATA_WORD( runends, 64*last_word) = shift_into_b(METADATA_WORD( runends, 64*(last_word-1)),
METADATA_WORD( runends, 64*last_word),
0, bend, distance);
bend = 64;
last_word--;
while (last_word != first_word) {
METADATA_WORD( runends, 64*last_word) = shift_into_b(METADATA_WORD( runends, 64*(last_word-1)),
METADATA_WORD( runends, 64*last_word),
0, bend, distance);
last_word--;
}
}
METADATA_WORD( runends, 64*last_word) = shift_into_b(0, METADATA_WORD(
runends,
64*last_word),
bstart, bend, distance);
}
// inline void shift_fixed_counters(onDiskMQF *qf, int64_t first, uint64_t last,
// uint64_t distance)
// {
// assert(last < qf->metadata->xnslots && distance < 64);
// uint64_t first_word = first / 64;
// uint64_t bstart = first % 64;
// uint64_t last_word = (last + distance + 1) / 64;
// uint64_t bend = (last + distance + 1) % 64;
// uint64_t* curr, *prev;
// uint64_t tmp =last_word, tmp_bend=bend;
// for(int i=0;i<qf->metadata->fixed_counter_size;i++){
// last_word=tmp;
// bend=tmp_bend;
// if (last_word != first_word) {
// curr=(uint64_t*)((uint8_t*)qf->get_block( last_word)->slots+
// (SLOTS_PER_BLOCK * qf->metadata->bits_per_slot) / 8);
// prev=(uint64_t*)((uint8_t*)qf->get_block( last_word-1)->slots+
// (SLOTS_PER_BLOCK * qf->metadata->bits_per_slot) / 8);
// curr[i] = shift_into_b(prev[i],curr[i],0, bend, distance);
// bend = 64;
// last_word--;
// while (last_word != first_word) {
// curr=(uint64_t*)((uint8_t*)qf->get_block( last_word)->slots+(SLOTS_PER_BLOCK * qf->metadata->bits_per_slot / 8));
// prev=(uint64_t*)((uint8_t*)qf->get_block( last_word-1)->slots+(SLOTS_PER_BLOCK * qf->metadata->bits_per_slot / 8));
// curr[i] = shift_into_b(prev[i],curr[i],0, bend, distance);
// last_word--;
// }
// }
// curr=(uint64_t*)((uint8_t*)qf->get_block( last_word)->slots+(SLOTS_PER_BLOCK * qf->metadata->bits_per_slot / 8));
// curr[i] = shift_into_b(0,curr[i], bstart, bend, distance);
// }
//
// }
// inline void shift_tags(QF *qf, int64_t first, uint64_t last,
// uint64_t distance)
// {
// assert(last < qf->metadata->xnslots && distance < 64);
// uint64_t first_word = first / 64;
// uint64_t bstart = first % 64;
// uint64_t last_word = (last + distance + 1) / 64;
// uint64_t bend = (last + distance + 1) % 64;
// uint64_t* curr, *prev;
// uint64_t tmp =last_word, tmp_bend=bend;
// for(int i=0;i<qf->metadata->tag_bits;i++){
// last_word=tmp;
// bend=tmp_bend;
// if (last_word != first_word) {
// curr=(uint64_t*)((uint8_t*)qf->get_block( last_word)->slots+
// (8 *(qf->metadata->bits_per_slot + qf->metadata->fixed_counter_size )));
// prev=(uint64_t*)((uint8_t*)qf->get_block( last_word-1)->slots+
// (8 *(qf->metadata->bits_per_slot + qf->metadata->fixed_counter_size )));
// curr[i] = shift_into_b(prev[i],curr[i],0, bend, distance);
// bend = 64;
// last_word--;
// while (last_word != first_word) {
// curr=(uint64_t*)((uint8_t*)qf->get_block( last_word)->slots+
// (8 *(qf->metadata->bits_per_slot + qf->metadata->fixed_counter_size )));
// prev=(uint64_t*)((uint8_t*)qf->get_block( last_word-1)->slots+
// (8 *(qf->metadata->bits_per_slot + qf->metadata->fixed_counter_size )));
// curr[i] = shift_into_b(prev[i],curr[i],0, bend, distance);
// last_word--;
// }
// }
// curr=(uint64_t*)((uint8_t*)qf->get_block( last_word)->slots+
// (8 *(qf->metadata->bits_per_slot + qf->metadata->fixed_counter_size )));
// curr[i] = shift_into_b(0,curr[i], bstart, bend, distance);
// }
//
// }
template<uint64_t bitsPerSlot>
inline void _onDiskMQF<bitsPerSlot>::insert_replace_slots_and_shift_remainders_and_runends_and_offsets(int operation,
uint64_t bucket_index,
uint64_t overwrite_index,
const uint64_t *remainders,
const uint64_t *fixed_size_counters,
uint64_t total_remainders,
uint64_t noverwrites)
{
_onDiskMQF<bitsPerSlot> *qf=this;
uint64_t empties[67];
uint64_t i;
int64_t ninserts = total_remainders - noverwrites;
uint64_t insert_index = overwrite_index + noverwrites;
if(qf->metadata->noccupied_slots+ninserts > qf->metadata->maximum_occupied_slots )
{
throw std::overflow_error("QF is 95% full, cannot insert more items.");
}
//printf("remainder =%lu ,overwrite_index = %lu , insert_index=%lu , operation=%d, noverwites=%lu total_remainders=%lu nnserts=%lu \n", remainders[0],overwrite_index,insert_index,operation,noverwrites,total_remainders,ninserts);
if (ninserts > 0) {
/* First, shift things to create n empty spaces where we need them. */
//printf("shift %lu, ninserts=%lu\n",insert_index,ninserts );
find_next_n_empty_slots( insert_index, ninserts, empties);
for (i = 0; i < ninserts - 1; i++){
shift_slots( empties[i+1] + 1, empties[i] - 1, i + 1);
}
shift_slots( insert_index, empties[ninserts - 1] - 1, ninserts);