10
10
#include < vector>
11
11
#include < cstring>
12
12
#include < sstream>
13
- #include " nvm_alloc.h"
14
-
15
13
16
14
typedef uint8_t bitmap_t ;
17
15
#define BITMAP_WIDTH (sizeof (bitmap_t ) * 8 )
@@ -91,7 +89,6 @@ class LIPP
91
89
: BUILD_LR_REMAIN(BUILD_LR_REMAIN), QUIET(QUIET)
92
90
{
93
91
{
94
-
95
92
std::vector<Node *> nodes;
96
93
for (int _ = 0 ; _ < 1e7 ; _++)
97
94
{
@@ -333,7 +330,7 @@ class LIPP
333
330
printf (" \t time_build_tree_bulk = %lf\n " , stats.time_build_tree_bulk );
334
331
#endif
335
332
}
336
- size_t index_size () const
333
+ size_t index_size (bool total = false , bool ignore_child = true ) const
337
334
{
338
335
std::stack<Node *> s;
339
336
s.push (root);
@@ -344,17 +341,33 @@ class LIPP
344
341
Node *node = s.top ();
345
342
s.pop ();
346
343
bool has_child = false ;
344
+ if (ignore_child == false )
345
+ {
346
+ size += sizeof (*node);
347
+ }
347
348
for (int i = 0 ; i < node->num_items ; i++)
348
349
{
349
- if (BITMAP_GET (node-> child_bitmap , i) == 1 )
350
+ if (ignore_child == true )
350
351
{
351
352
size += sizeof (Item);
352
353
has_child = true ;
354
+ }
355
+ else
356
+ {
357
+ if (total)
358
+ size += sizeof (Item);
359
+ }
360
+ if (BITMAP_GET (node->child_bitmap , i) == 1 )
361
+ {
362
+ if (!total)
363
+ size += sizeof (Item);
353
364
s.push (node->items [i].comp .child );
354
365
}
355
366
}
356
- if (has_child)
367
+ if (ignore_child == true && has_child)
368
+ {
357
369
size += sizeof (*node);
370
+ }
358
371
}
359
372
return size;
360
373
}
@@ -390,11 +403,7 @@ class LIPP
390
403
Node *root;
391
404
std::stack<Node *> pending_two;
392
405
393
- #ifdef USE_MEM
394
406
std::allocator<Node> node_allocator;
395
- #else
396
- NVM::allocator<Node> node_allocator;
397
- #endif
398
407
Node *new_nodes (int n)
399
408
{
400
409
Node *p = node_allocator.allocate (n);
@@ -405,11 +414,8 @@ class LIPP
405
414
{
406
415
node_allocator.deallocate (p, n);
407
416
}
408
- # ifdef USE_MEM
417
+
409
418
std::allocator<Item> item_allocator;
410
- #else
411
- NVM::allocator<Item> item_allocator;
412
- #endif
413
419
Item *new_items (int n)
414
420
{
415
421
Item *p = item_allocator.allocate (n);
@@ -420,12 +426,8 @@ class LIPP
420
426
{
421
427
item_allocator.deallocate (p, n);
422
428
}
423
-
424
- #ifdef USE_MEM
429
+
425
430
std::allocator<bitmap_t > bitmap_allocator;
426
- #else
427
- NVM::allocator<bitmap_t > bitmap_allocator;
428
- #endif
429
431
bitmap_t *new_bitmap (int n)
430
432
{
431
433
bitmap_t *p = bitmap_allocator.allocate (n);
@@ -454,10 +456,7 @@ class LIPP
454
456
BITMAP_SET (node->none_bitmap , 0 );
455
457
node->child_bitmap = new_bitmap (1 );
456
458
node->child_bitmap [0 ] = 0 ;
457
- NVM::Mem_persist (node->items ,sizeof (Item));
458
- NVM::Mem_persist (node->child_bitmap ,sizeof (bitmap_t ));
459
- NVM::Mem_persist (node->none_bitmap ,sizeof (bitmap_t ));
460
- NVM::Mem_persist (node,sizeof (Node));
459
+
461
460
return node;
462
461
}
463
462
// / build a tree with two keys
@@ -487,25 +486,20 @@ class LIPP
487
486
node->child_bitmap = new_bitmap (1 );
488
487
node->none_bitmap [0 ] = 0xff ;
489
488
node->child_bitmap [0 ] = 0 ;
490
- NVM::Mem_persist (node->none_bitmap ,sizeof (Node));
491
- NVM::Mem_persist (node->child_bitmap ,sizeof (Node));
492
- NVM::Mem_persist (node->items ,sizeof (Item)*8 );
493
- NVM::Mem_persist (node,sizeof (Node));
494
489
}
495
490
else
496
491
{
497
492
node = pending_two.top ();
498
493
pending_two.pop ();
499
494
}
500
-
501
495
502
- const double mid1_key = key1;
503
- const double mid2_key = key2;
496
+ const long double mid1_key = key1;
497
+ const long double mid2_key = key2;
504
498
505
499
const double mid1_target = node->num_items / 3 ;
506
500
const double mid2_target = node->num_items * 2 / 3 ;
507
501
508
- node->model .a = (mid1_target - mid2_target ) / (mid1_key - mid2_key );
502
+ node->model .a = (mid2_target - mid1_target ) / (mid2_key - mid1_key );
509
503
node->model .b = mid1_target - node->model .a * mid1_key;
510
504
RT_ASSERT (isfinite (node->model .a ));
511
505
RT_ASSERT (isfinite (node->model .b ));
@@ -516,18 +510,15 @@ class LIPP
516
510
BITMAP_CLEAR (node->none_bitmap , pos);
517
511
node->items [pos].comp .data .key = key1;
518
512
node->items [pos].comp .data .value = value1;
519
- NVM::Mem_persist (node->items +pos,sizeof (Item));
520
513
}
521
514
{ // insert key2&value2
522
515
int pos = PREDICT_POS (node, key2);
523
516
RT_ASSERT (BITMAP_GET (node->none_bitmap , pos) == 1 );
524
517
BITMAP_CLEAR (node->none_bitmap , pos);
525
518
node->items [pos].comp .data .key = key2;
526
519
node->items [pos].comp .data .value = value2;
527
- NVM::Mem_persist (node->items +pos,sizeof (Item));
528
520
}
529
- NVM::Mem_persist (node->none_bitmap ,sizeof (Node));
530
- NVM::Mem_persist (node->child_bitmap ,sizeof (Node));
521
+
531
522
return node;
532
523
}
533
524
// / bulk build, _keys must be sorted in asc order.
@@ -595,14 +586,16 @@ class LIPP
595
586
RT_ASSERT (mid1_pos < mid2_pos);
596
587
RT_ASSERT (mid2_pos < size - 1 );
597
588
598
- const double mid1_key = static_cast <double >((static_cast <long double >(keys[mid1_pos]) + static_cast <long double >(keys[mid1_pos + 1 ])) / 2 );
599
- const double mid2_key = static_cast <double >((static_cast <long double >(keys[mid2_pos]) + static_cast <long double >(keys[mid2_pos + 1 ])) / 2 );
589
+ const long double mid1_key =
590
+ (static_cast <long double >(keys[mid1_pos]) + static_cast <long double >(keys[mid1_pos + 1 ])) / 2 ;
591
+ const long double mid2_key =
592
+ (static_cast <long double >(keys[mid2_pos]) + static_cast <long double >(keys[mid2_pos + 1 ])) / 2 ;
600
593
601
594
node->num_items = size * static_cast <int >(BUILD_GAP_CNT + 1 );
602
595
const double mid1_target = mid1_pos * static_cast <int >(BUILD_GAP_CNT + 1 ) + static_cast <int >(BUILD_GAP_CNT + 1 ) / 2 ;
603
596
const double mid2_target = mid2_pos * static_cast <int >(BUILD_GAP_CNT + 1 ) + static_cast <int >(BUILD_GAP_CNT + 1 ) / 2 ;
604
597
605
- node->model .a = (mid1_target - mid2_target ) / (mid1_key - mid2_key );
598
+ node->model .a = (mid2_target - mid1_target ) / (mid2_key - mid1_key );
606
599
node->model .b = mid1_target - node->model .a * mid1_key;
607
600
RT_ASSERT (isfinite (node->model .a ));
608
601
RT_ASSERT (isfinite (node->model .b ));
@@ -691,15 +684,13 @@ class LIPP
691
684
const int end = s.top ().end ;
692
685
const int level = s.top ().level ;
693
686
Node *node = s.top ().node ;
694
- NVM::Mem_persist (node,sizeof (Node));
695
687
s.pop ();
696
688
697
689
RT_ASSERT (end - begin >= 2 );
698
690
if (end - begin == 2 )
699
691
{
700
692
Node *_ = build_tree_two (_keys[begin], _values[begin], _keys[begin + 1 ], _values[begin + 1 ]);
701
693
memcpy (node, _, sizeof (Node));
702
- NVM::Mem_persist (node,sizeof (Node));
703
694
delete_nodes (_, 1 );
704
695
}
705
696
else
@@ -726,7 +717,9 @@ class LIPP
726
717
int i = 0 ;
727
718
int D = 1 ;
728
719
RT_ASSERT (D <= size - 1 - D);
729
- double Ut = double (keys[size - 1 - D] - keys[D]) / double (L - 2 ) + 1e-6 ;
720
+ double Ut = (static_cast <long double >(keys[size - 1 - D]) - static_cast <long double >(keys[D])) /
721
+ (static_cast <double >(L - 2 )) +
722
+ 1e-6 ;
730
723
while (i < size - 1 - D)
731
724
{
732
725
while (i + D < size && keys[i + D] - keys[i] >= Ut)
@@ -741,14 +734,18 @@ class LIPP
741
734
if (D * 3 > size)
742
735
break ;
743
736
RT_ASSERT (D <= size - 1 - D);
744
- Ut = double (keys[size - 1 - D] - keys[D]) / double (L - 2 ) + 1e-6 ;
737
+ Ut = (static_cast <long double >(keys[size - 1 - D]) - static_cast <long double >(keys[D])) /
738
+ (static_cast <double >(L - 2 )) +
739
+ 1e-6 ;
745
740
}
746
741
if (D * 3 <= size)
747
742
{
748
743
stats.fmcd_success_times ++;
749
744
750
745
node->model .a = 1.0 / Ut;
751
- node->model .b = (L - node->model .a * (keys[size - 1 - D] + keys[D])) / 2 ;
746
+ node->model .b = (L - node->model .a * (static_cast <long double >(keys[size - 1 - D]) +
747
+ static_cast <long double >(keys[D]))) /
748
+ 2 ;
752
749
RT_ASSERT (isfinite (node->model .a ));
753
750
RT_ASSERT (isfinite (node->model .b ));
754
751
node->num_items = L;
@@ -764,14 +761,18 @@ class LIPP
764
761
RT_ASSERT (mid1_pos < mid2_pos);
765
762
RT_ASSERT (mid2_pos < size - 1 );
766
763
767
- const double mid1_key = static_cast <double >((static_cast <long double >(keys[mid1_pos]) + static_cast <long double >(keys[mid1_pos + 1 ])) / 2 );
768
- const double mid2_key = static_cast <double >((static_cast <long double >(keys[mid2_pos]) + static_cast <long double >(keys[mid2_pos + 1 ])) / 2 );
764
+ const long double mid1_key = (static_cast <long double >(keys[mid1_pos]) +
765
+ static_cast <long double >(keys[mid1_pos + 1 ])) /
766
+ 2 ;
767
+ const long double mid2_key = (static_cast <long double >(keys[mid2_pos]) +
768
+ static_cast <long double >(keys[mid2_pos + 1 ])) /
769
+ 2 ;
769
770
770
771
node->num_items = size * static_cast <int >(BUILD_GAP_CNT + 1 );
771
772
const double mid1_target = mid1_pos * static_cast <int >(BUILD_GAP_CNT + 1 ) + static_cast <int >(BUILD_GAP_CNT + 1 ) / 2 ;
772
773
const double mid2_target = mid2_pos * static_cast <int >(BUILD_GAP_CNT + 1 ) + static_cast <int >(BUILD_GAP_CNT + 1 ) / 2 ;
773
774
774
- node->model .a = (mid1_target - mid2_target ) / (mid1_key - mid2_key );
775
+ node->model .a = (mid2_target - mid1_target ) / (mid2_key - mid1_key );
775
776
node->model .b = mid1_target - node->model .a * mid1_key;
776
777
RT_ASSERT (isfinite (node->model .a ));
777
778
RT_ASSERT (isfinite (node->model .b ));
@@ -793,10 +794,7 @@ class LIPP
793
794
node->child_bitmap = new_bitmap (bitmap_size);
794
795
memset (node->none_bitmap , 0xff , sizeof (bitmap_t ) * bitmap_size);
795
796
memset (node->child_bitmap , 0 , sizeof (bitmap_t ) * bitmap_size);
796
- NVM::Mem_persist (node->items ,sizeof (Item)*node->num_items );
797
- NVM::Mem_persist (node->child_bitmap ,sizeof (bitmap_t ));
798
- NVM::Mem_persist (node->none_bitmap ,sizeof (bitmap_t ));
799
- NVM::Mem_persist (node,sizeof (Node));
797
+
800
798
for (int item_i = PREDICT_POS (node, keys[0 ]), offset = 0 ; offset < size;)
801
799
{
802
800
int next = offset + 1 , next_i = -1 ;
@@ -817,18 +815,13 @@ class LIPP
817
815
BITMAP_CLEAR (node->none_bitmap , item_i);
818
816
node->items [item_i].comp .data .key = keys[offset];
819
817
node->items [item_i].comp .data .value = values[offset];
820
- NVM::Mem_persist (node->none_bitmap +item_i/8 ,sizeof (bitmap_t ));
821
- NVM::Mem_persist (node->items +item_i,sizeof (Item));
822
818
}
823
819
else
824
820
{
825
821
// ASSERT(next - offset <= (size+2) / 3);
826
822
BITMAP_CLEAR (node->none_bitmap , item_i);
827
823
BITMAP_SET (node->child_bitmap , item_i);
828
824
node->items [item_i].comp .child = new_nodes (1 );
829
- NVM::Mem_persist (node->none_bitmap +item_i/8 ,sizeof (bitmap_t ));
830
- NVM::Mem_persist (node->child_bitmap +item_i/8 ,sizeof (bitmap_t ));
831
- NVM::Mem_persist (node->items +item_i,sizeof (Item));
832
825
s.push ((Segment){begin + offset, begin + next, level + 1 , node->items [item_i].comp .child });
833
826
}
834
827
if (next >= size)
@@ -958,7 +951,7 @@ class LIPP
958
951
959
952
Node *insert_tree (Node *_node, const T &key, const P &value)
960
953
{
961
- constexpr int MAX_DEPTH = 512 ;
954
+ constexpr int MAX_DEPTH = 128 ;
962
955
Node *path[MAX_DEPTH];
963
956
int path_size = 0 ;
964
957
int insert_to_data = 0 ;
@@ -970,23 +963,18 @@ class LIPP
970
963
971
964
node->size ++;
972
965
node->num_inserts ++;
973
- NVM::Mem_persist (node,sizeof (Node));
974
966
int pos = PREDICT_POS (node, key);
975
967
if (BITMAP_GET (node->none_bitmap , pos) == 1 )
976
968
{
977
969
BITMAP_CLEAR (node->none_bitmap , pos);
978
970
node->items [pos].comp .data .key = key;
979
971
node->items [pos].comp .data .value = value;
980
- NVM::Mem_persist (node->items +pos,sizeof (Item));
981
- NVM::Mem_persist (node->none_bitmap +pos/8 ,sizeof (bitmap_t ));
982
972
break ;
983
973
}
984
974
else if (BITMAP_GET (node->child_bitmap , pos) == 0 )
985
975
{
986
976
BITMAP_SET (node->child_bitmap , pos);
987
- NVM::Mem_persist (node->child_bitmap +pos/8 ,sizeof (bitmap_t ));
988
977
node->items [pos].comp .child = build_tree_two (key, value, node->items [pos].comp .data .key , node->items [pos].comp .data .value );
989
- NVM::Mem_persist (node->items +pos,sizeof (Item));
990
978
insert_to_data = 1 ;
991
979
break ;
992
980
}
0 commit comments