-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBtreeSF.java
More file actions
5482 lines (4880 loc) · 306 KB
/
BtreeSF.java
File metadata and controls
5482 lines (4880 loc) · 306 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
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
//------------------------------------------------------------------------------
// BtreeDM with pipelined free chain and single access memory.
// Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2024-2025
//------------------------------------------------------------------------------
package com.AppaApps.Silicon; // Btree in a block on the surface of a silicon chip.
// Node confirm that a load or a save identified by the trace back is actually changing the node - eliminate those that never have any effect
// Double allocation would be faster as allocations are often done in pairs
// Stuck - get penultimate element. Use currentSize field directly instead of copy in it to a temporary variable
// Start splitting lower down and merge only along split path
// Try memory operations in parallel in class Node without getting congestion complaints from silicon compiler
// Investigate whether it would be worth changing from a block of variables in T to individual memories for each variable now in T. This would localize access which would simplify routing at the cost of more silicon spent on registers.
// Can MemoryLayoutDM be merged with Layout? I.e. each field laid out would know what memory it resides in.
// Testing the effect of split branch having its own node buffers
// Parallel for find/First/Next/Last and findNext findandDelete etc.
import java.util.*;
import java.nio.file.*;
abstract class BtreeSF extends Test // Manipulate a btree using static methods and memory
{final BtreeSF thisBTree = this; // Direct access to this btree even when this goes out of range
final MemoryLayoutDM F; // Free chain of nodes not currently in use
final MemoryLayoutDM M; // The memory layout of the btree
final MemoryLayoutDM T; // The memory used to hold temporary variable used during a transaction on the btree
final ProgramDM P = new ProgramDM(); // Program in which to generate instructions
final Variable numberOfPairs = new Variable(P, "size", 32); // Count the number of key/data pairs in the leaves of the tree
final boolean OpCodes = true; // Refactor op codes
final boolean runVerilog = true; // Run verilog tests alongside java tests and check they produce the same results
final String designDescription = "count number of elements"; // Description of latest change
final String processTechnology = "freepdk45"; // Process technology from: https://docs.siliconcompiler.com/en/stable/#supported-technologies . Ask chat for details of each.
abstract int maxSize(); // The maximum number of leaves plus branches in the bree
abstract int bitsPerKey(); // The number of bits per key
abstract int bitsPerData(); // The number of bits per data
abstract int maxKeysPerLeaf(); // Maximum number of leafs in a key
abstract int maxKeysPerBranch(); // Maximum number of keys in a branch
final int splitLeafSize; // The number of key, data pairs to split out of a leaf
final int splitBranchSize; // The number of key, next pairs to split out of a branch
final int bitsPerAddress; // The number of bits required to address a bit in memory
final int bitsPerNext; // The number of bits in a next field
final int bitsPerSize; // The number of bits in stuck size field
final static String verilogFolder = "verilog"; // The folder to write verilog into
final static Stack<VerilogCode> verilogTests = new Stack<>(); // The verilog tests executed
static int widthOfMarginInExecutionTrace = 18; // The width of the left margin in verilog traces that is to be protected from differential writing
Layout.Field leaf; // Layout of a leaf in the memory used by btree
Layout.Field branch; // Layout of a branch in the memory used by btree
Layout.Union branchOrLeaf; // Layout of either a leaf or a branch in the memory used by btree
Layout.Bit isLeaf; // Whether the current node is a leaf or a branch
Layout.Variable free; // Free list chain
Layout.Field node; // Layout of a node in the memory used by btree
Layout.Array bTreeNodes; // Layout of an array of nodes in the memory used by btree
Layout.Variable freeList; // Single linked list of nodes that have been freed and so can be reused without fragmenting memory
Layout.Structure bTree; // Btree
Layout.Field bTree_free; // Free list in node observed from btree main memory and thus likely to add to memory congestion unless separated out into a spearate memory block
Layout.Field node_size; // Size of a node observed from a node
Layout.Field freeChainHead; // Head of the free list
Layout nodeLayout; // Layout of a node in the btree
Layout freeChainLayout; // Layout of the head of the free chain
final static int
linesToPrintABranch = 4, // The number of lines required to print a branch
maxPrintLevels = 10, // Maximum number of levels to print in a tree
maxDepth = 99; // Maximum depth of any realistic tree
int nodeUsed = 0; // Number of nodes currently in use
final int root = 0; // The root of the tree is always node zero
final int Branch_Size = 0; // Get the size of a stuck
final int Branch_Leaf = 0; // Check whether a node has leaves for children
final int Branch_Top = 0; // Get the top element of a branch
final int Branch_FirstBranch = 0; // Locate the first greater or equal key in a branch
final int Branch_T = 1; // Process a parent node
final int Branch_tl = 2; // Process a left node
final int Branch_tr = 3; // Process a right node
final int Branch_length = 4; // Number of transaction types
final int Leaf_Size = 0; // Get the size of a stuck
final int Leaf_Leaf = 0; // Check whether a node has leaves for children
final int Leaf_Equal = 0; // Get the top element of a branch
final int Leaf_FirstLeaf = 0; // Locate the first greater or equal key in a branch
final int Leaf_T = 1; // Process a parent node
final int Leaf_tl = 2; // Process a left node
final int Leaf_tr = 3; // Process a right node
final int Leaf_length = 4; // Number of transaction types
final StuckDM bSize; // Branch size
final StuckDM bLeaf; // Check whether a node has leaves for children
final StuckDM bTop; // Get the size of a stuck
final StuckDM bFirstBranch; // Locate the first greater or equal key in a branch
final StuckDM bT; // Process a parent node
final StuckDM bL; // Process a left node
final StuckDM bR; // Process a right node
final StuckDM lSize; // Branch size
final StuckDM lLeaf; // Check whether a node has leaves for children
final StuckDM lEqual; // Locate an equal key
final StuckDM lFirstLeaf; // Locate the first greater or equal key in a leaf
final StuckDM lT; // Process a parent node as a leaf
final StuckDM lL; // Process a left node
final StuckDM lR; // Process a right node
final Node nT; // Memory sufficient to contain a single parent node
final Node nL; // Memory sufficient to contain a single left node
final Node nR; // Memory sufficient to contain a single right node
final Node nC; // Memory sufficient to contain a single child node - equated to a left node until we dicover the need for a separate memory area
final Node memoryIn; // The nodes that interfaces with memory
static int testNumber = 0; // Number of the current test
boolean debug = false; // Debugging enabled
//D1 Construction // Create a Btree from nodes which can be branches or leaves. The data associated with the BTree is stored only in the leaves opposite the keys
BtreeSF() // Define a Btree with user specified dimensions
{zz();
splitLeafSize = maxKeysPerLeaf() >> 1; // The number of key, data pairs to split out of a leaf
splitBranchSize = maxKeysPerBranch() >> 1; // The number of key, next pairs to split out of a branch
bitsPerNext = logTwo(maxSize()); // The number of bits in a next field sufficient to index any node
bitsPerSize = logTwo(max(bitsPerKey(), bitsPerData())+1); // The number of bits in stuck size field sufficient to index an key or data element including top
F = new MemoryLayoutDM(freeChainLayout(), "F"); // The memory layout of the free chain list header
M = new MemoryLayoutDM(layout(), "M"); // The memory layout of the btree
bitsPerAddress = logTwo(M.memory().size()); // Number of bits to address any bit in memory
T = new MemoryLayoutDM(transactionLayout(), "T"); // The memory used to hold temporary variable used during a transaction on the btree
F.program(P); M.program(P); T.program(P);
T.at(maxKeysPerLeaf) .setInt(maxKeysPerLeaf());
T.at(maxKeysPerBranch).setInt(maxKeysPerBranch());
T.at(two) .setInt(2);
// T.at(MaxDepth) .setInt(maxDepth); // Prevent runaway searches of the btree by limiting the number of levels to be searched
bSize = //branchTransactions[Branch_Size ]; // Branch size
bLeaf = //branchTransactions[Branch_Leaf ]; // Check whether a node has leaves for children
bTop = //branchTransactions[Branch_Top ]; // Get the size of a stuck
bFirstBranch = //branchTransactions[Branch_FirstBranch]; // Locate the first greater or equal key in a branch
bT = createBranchStuck("bT"); // Process a parent node
bL = createBranchStuck("bL"); // Process a left node
bR = createBranchStuck("bR"); // Process a right node
lSize = //leafTransactions[Leaf_Size ]; // Leaf size
lLeaf = //leafTransactions[Leaf_Leaf ]; // Print a leaf
lEqual = //leafTransactions[Leaf_Equal ]; // Locate an equal key
lFirstLeaf = //leafTransactions[Leaf_FirstLeaf ]; // Locate the first greater or equal key in a leaf
lT = createLeafStuck("lT"); // Process a parent node
lL = createLeafStuck("lL"); // Process a left node
lR = createLeafStuck("lR"); // Process a right node
nT = new Node("nT");
nL = new Node("nL"); nC = nL;
nR = new Node("nR");
memoryIn = new Node("memoryIn");
P.new I()
{void a()
{final int N = maxSize(); // Put all the nodes on the free chain at the start with low nodes first
for (int i = N; i > 0; --i) setInt(bTree_free, (i == N ? 0 : i), i - 1);// Link this node to the previous node
F.setInt(freeChainHead, root); // Root is first on free chain
}
String v() {return "/* Construct Free list */";}
};
allocate(false); // The root is always at zero, which frees zero to act as the end of list marker on the free chain
nC.loadRoot(); // Load the allocated node
nC.setLeaf(); // Set the root as a leaf
nC.saveRoot(); // Write back into memory
numberOfPairs.zero(); // Initially the tree is empty
}
StuckDM createBranchStuck(String name) // Create a branch Stuck
{zz();
final StuckDM b = new StuckDM(name) // Branch stucks
{int maxSize() {return BtreeSF.this.maxKeysPerBranch()+1;} // Not forgetting top next
int bitsPerKey() {return BtreeSF.this.bitsPerKey();}
int bitsPerData() {return BtreeSF.this.bitsPerNext;}
int bitsPerSize() {return BtreeSF.this.bitsPerSize;}
};
b.M.layout.layoutName = name+"_M";
b.T.layout.layoutName = name+"_T";
b.program(P);
return b;
}
StuckDM createLeafStuck(String name) // Create a leaf Stuck
{zz();
final StuckDM l = new StuckDM(name) // Leaf stucks
{int maxSize() {return BtreeSF.this.maxKeysPerLeaf();}
int bitsPerKey() {return BtreeSF.this.bitsPerKey();}
int bitsPerData() {return BtreeSF.this.bitsPerData();}
int bitsPerSize() {return BtreeSF.this.bitsPerSize;}
};
l.M.layout.layoutName = name+"_M";
l.T.layout.layoutName = name+"_T";
l.program(P);
return l;
}
private static BtreeSF BtreeSF(int leafKeys, int branchKeys, int maxSize) // Define a test btree with the specified dimensions
{zz();
return new BtreeSF()
{int maxSize () {return maxSize;}
int maxKeysPerLeaf () {return leafKeys;}
int maxKeysPerBranch() {return branchKeys;}
int bitsPerKey () {return 32;}
int bitsPerData () {return 32;}
};
}
private static BtreeSF allTreeOps() // Define a tree capable of performing all operations
{zz();
return new BtreeSF()
{int maxSize () {return 16;}
int maxKeysPerLeaf () {return 2;}
int maxKeysPerBranch() {return 3;}
int bitsPerKey () {return 5;}
int bitsPerData () {return 4;}
};
}
private static BtreeSF wideTree() // Define a tree with nodes wide enough to test logarithmic moves and searching
{zz();
return new BtreeSF()
{int maxSize () {return 16;}
int maxKeysPerLeaf () {return 8;}
int maxKeysPerBranch() {return 9;}
int bitsPerKey () {return 64;}
int bitsPerData () {return 64;}
};
}
Layout nodeLayout() // Layout describing a node in btree
{zz();
final BtreeSF btree = this;
final StuckDM leafStuck = new StuckDM("leaf") // Leaf
{int maxSize() {return btree.maxKeysPerLeaf();}
int bitsPerKey() {return btree.bitsPerKey();}
int bitsPerData() {return btree.bitsPerData();}
int bitsPerSize() {return btree.bitsPerSize;}
};
leafStuck.T.layout.layoutName = "leaf";
final StuckDM branchStuck = new StuckDM("branch") // Branch
{int maxSize() {return btree.maxKeysPerBranch()+1;} // Not forgetting top next
int bitsPerKey() {return btree.bitsPerKey();}
int bitsPerData() {return btree.bitsPerNext;}
int bitsPerSize() {return btree.bitsPerSize;}
};
branchStuck.T.layout.layoutName = "branch";
final Layout l = Layout.layout();
final int n = max(leafStuck.M.size(), branchStuck.M.size()) // Size of the data in the node
+ 1 + btree.bitsPerNext;
final int p = nextPowerOfTwo(n) - n; // Amount of padding - which might be zero
final Layout.Variable pad = p > 0 ? l.variable("pad", p) : null; // Padding might be required
leaf = l.duplicate("leaf", leafStuck.layout());
branch = l.duplicate("branch", branchStuck.layout());
branchOrLeaf = l.union ("branchOrLeaf", leaf, branch);
isLeaf = l.bit ("isLeaf");
free = l.variable ("free", btree.bitsPerNext);
node = pad == null ?
l.structure("node", isLeaf, free, branchOrLeaf): // No padding required
l.structure("node", isLeaf, free, branchOrLeaf, pad);// Padding to the next power of two to make indexing of nodes easy
nodeLayout = l.compile();
node_size = l.get("branchOrLeaf.branch.currentSize"); // Relies on size being in the same position and having the same size in branches and leaves
return nodeLayout;
}
Layout freeChainLayout() // Layout describing free chain head
{zz();
final Layout l = Layout.layout();
freeChainHead = l.variable ("freeList", bitsPerNext);
freeChainLayout = l.compile();
return l;
}
Layout layout() // Layout describing memory used by btree
{zz();
final BtreeSF btree = this;
nodeLayout();
final Layout l = Layout.layout();
node = l.duplicate(nodeLayout);
bTreeNodes = l.array("nodes", node, maxSize());
l.compile();
// bTree_isLeaf = l.get("node.isLeaf");
bTree_free = l.get("node.free");
return l;
}
BtreePA btreePA() // Convert to an earlier version known to work correctly so we can print it without having to write and execute pribt code.
{zz();
final BtreeSF s = this; // Source
final BtreePA t = new BtreePA() // Target
{int maxSize () {return s.maxSize () ;}
int maxKeysPerLeaf () {return s.maxKeysPerLeaf () ;}
int maxKeysPerBranch() {return s.maxKeysPerBranch() ;}
int bitsPerKey () {return s.bitsPerKey () ;}
int bitsPerData () {return s.bitsPerData () ;}
};
t.M.memory().copy(s.F.memory()); // The difference between BTreeDM and SF. I was listening to the Caves of Steel at the time.
final int N = t.maxSize(), P = t.Node.width, Q = s.nodeLayout.size();
for (int i = 0, p = s.F.memory().size(), q = 0; i < N; i++, p += P, q += Q) // Copy all the nodes out of the source tree and place thme in the target
{t.M.memory().copy(p, s.M.memory(), q, P);
}
return t;
}
static BtreeSF btreeDM(BtreePA p) // Convert from an earlier version known to work correctly
{zz();
final BtreeSF t = new BtreeSF()
{int maxSize () {return p.maxSize () ;}
int maxKeysPerLeaf () {return p.maxKeysPerLeaf () ;}
int maxKeysPerBranch() {return p.maxKeysPerBranch() ;}
int bitsPerKey () {return p.bitsPerKey () ;}
int bitsPerData () {return p.bitsPerData () ;}
};
t.M.memory().copy(p.M.memory());
return t;
}
//D1 Control // Testing, control and integrity
private void ok(String expected) {zz(); Test.ok(toString(), expected);} // Confirm tree is as expected
private void stop() {zz(); Test.stop(toString());} // Stop after printing the tree
public String toString() {zz(); return ""+btreePA();} // Print the tree
//D1 Memory access // Access to memory
private void checkMainField(Layout.Field field) // Check that a variable is in main memory
{zz();
if (field.container() != M.layout)
{final String name = field.container().layoutName;
stop("Field:", field.name, "is part of memory layout:", name, "not main");
}
}
private void checkTransactionField(Layout.Field field) // Check that a variable is in transaction memory
{zz();
if (field.container() != T.layout)
{final String name = field.container().layoutName;
stop("Field:", field.name, "is part of memory layout:",
name, "not transaction");
}
}
private void setInt(Layout.Field field, int value) // Set an integer in main memory
{zz(); checkMainField(field); M.setInt(field, value);
}
private void setInt(Layout.Field field, int value, int index)
{zz(); checkMainField(field); M.setInt(field, value, index);
}
void tt(Layout.Variable target, Layout.Variable source) // Copy the value of one transaction variable into another
{zz(); checkTransactionField(target); checkTransactionField(source);
T.at(target).move(T.at(source));
}
void tm(Layout.Variable target, Layout.Variable source) // Copy the value of a main memory variable into transaction memory
{zz(); checkTransactionField(target); checkMainField(source);
T.at(target).move(M.at(source));
}
//D1 Components // Fields used to perform transactions on the tree
private Layout.Variable Key; // Key being found, inserted or deleted
private Layout.Variable Data; // Data associated with the key being inserted
private Layout.Bit found; // Whether the key was found
private Layout.Variable key; // Key to insert
private Layout.Variable data; // Data associated with the key found
private Layout.Variable allocate; // The latest allocation result
private Layout.Variable nextFree; // Next element of the free chain
private Layout.Bit success; // Inserted or updated if true
private Layout.Bit inserted; // Inserted if true
private Layout.Variable first; // Index of first key greater than or equal to the search key
private Layout.Variable next; // The corresponding next field or top if no such key was found
private Layout.Variable search; // Search key
private Layout.Variable firstKey; // First of right leaf
private Layout.Variable lastKey; // Last of left leaf
private Layout.Variable flKey; // Key mid way between last of left and first of right
private Layout.Variable parentKey; // Parent key
private Layout.Variable lk; // Left child key
private Layout.Variable ld; // Left child data
private Layout.Variable rk; // Right child key
private Layout.Variable rd; // Right child data
private Layout.Variable index; // Index of a slot in a node
private Layout.Variable nl; // Number in the left child
private Layout.Variable nr; // Number in the right child
private Layout.Variable l; // Left node
private Layout.Variable r; // Right node
private Layout.Variable splitParent; // The parent during a splitting operation
private Layout.Bit IsLeaf; // On a leaf
private Layout.Bit isFull; // The node is full
private Layout.Bit leafIsFull; // The leaf node is full
private Layout.Bit branchIsFull; // The branch node is full
private Layout.Bit parentIsFull; // The parent branch node is full
private Layout.Bit isEmpty; // The node is empty
private Layout.Bit isLow; // The node has too few children for a delete
private Layout.Bit hasLeavesForChildren; // The node has leaves for children
private Layout.Bit stolenOrMerged; // A merge or steal operation succeeded
private Layout.Bit pastMaxDepth; // A merge or steal operation succeeded
private Layout.Bit nodeMerged; // All sequential pairs of siblings have been offered a chance to merge
private Layout.Bit mergeable; // The left and right children are mergable
private Layout.Bit deleted; // Whether the delete request actually deleted the specified key
private Layout.Variable branchBase; // The offset of a branch in memory
private Layout.Variable leafSize; // Number of children in body of leaf
private Layout.Variable branchSize; // Number of children in body of branch taking top for granted as it is always there
private Layout.Variable childSize; // Number of children in body of a child node that might be a branch or a leaf - fortuately the size field of a stuck is in the same location reagrdless of whether it represents a leaf or a branch
private Layout.Variable top; // The top next element of a branch - only used in printing
// Find, insert, delete - the public entry points to this module
private Layout.Variable find; // Results of a find operation
private Layout.Variable findAndInsert; // Results of a find and insert operation
private Layout.Variable parent; // Parent node in a descent through the tree
private Layout.Variable child; // Child node in a descent through the tree
private Layout.Variable leafFound; // Leaf found by find
private Layout.Variable maxKeysPerLeaf; // Maximum keys per leaf
private Layout.Variable maxKeysPerBranch; // Maximum keys per branch
private Layout.Variable two; // The value two
private Layout.Variable mergeIndex; // Current index of node being merged across
private Layout.Variable memoryIOAddress; // Pipelined input or outoput to memory
private Layout.Bit memoryIODirection; // If true we are writing into memory else we are reading from memory
private Layout.Variable node_isLeaf; // The node to be used to implicitly parameterize each method call
private Layout.Variable node_setLeaf;
private Layout.Variable node_setBranch;
private Layout.Variable node_assertLeaf;
private Layout.Variable node_assertBranch;
private Layout.Variable allocLeaf;
private Layout.Variable allocBranch;
private Layout.Variable node_free;
private Layout.Variable node_clear;
private Layout.Variable node_erase;
private Layout.Variable node_leafBase;
private Layout.Variable node_branchBase;
private Layout.Variable node_leafSize;
private Layout.Variable node_branchSize;
private Layout.Variable node_isFull;
private Layout.Variable node_leafIsFull;
private Layout.Variable node_branchIsFull;
private Layout.Variable node_parentIsFull;
private Layout.Variable node_isEmpty;
private Layout.Variable node_isLow;
private Layout.Variable node_hasLeavesForChildren;
private Layout.Variable node_top;
private Layout.Variable node_findEqualInLeaf;
private Layout.Variable node_findFirstGreaterThanOrEqualInLeaf;
private Layout.Variable node_findFirstGreaterThanOrEqualInBranch;
private Layout.Variable node_splitLeaf;
private Layout.Variable node_splitBranch;
private Layout.Variable node_stealFromLeft;
private Layout.Variable node_stealFromRight;
private Layout.Variable node_mergeRoot;
private Layout.Variable node_mergeLeftSibling;
private Layout.Variable node_mergeRightSibling;
private Layout.Variable node_balance;
private Layout transactionLayout() // Layout of temporary storage used during a transaction against the btree
{zz();
final Layout L = new Layout();
allocate = L.variable ("allocate" , bitsPerNext);
nextFree = L.variable ("nextFree" , bitsPerNext);
success = L.bit ("success" );
inserted = L.bit ("inserted" );
first = L.variable ("first" , bitsPerSize);
next = L.variable ("next" , bitsPerNext);
search = L.variable ("search" , bitsPerKey());
found = L.bit ("found" );
key = L.variable ("key" , bitsPerKey());
data = L.variable ("data" , bitsPerData());
firstKey = L.variable ("firstKey" , bitsPerKey());
lastKey = L.variable ("lastKey" , bitsPerKey());
flKey = L.variable ("flKey" , bitsPerKey());
parentKey = L.variable ("parentKey" , bitsPerKey());
lk = L.variable ("lk" , bitsPerKey());
ld = L.variable ("ld" , bitsPerData());
rk = L.variable ("rk" , bitsPerKey());
rd = L.variable ("rd" , bitsPerData());
index = L.variable ("index" , bitsPerSize);
nl = L.variable ("nl" , bitsPerSize);
nr = L.variable ("nr" , bitsPerSize);
l = L.variable ("l" , bitsPerNext);
r = L.variable ("r" , bitsPerNext);
splitParent = L.variable ("splitParent" , bitsPerNext);
IsLeaf = //L.bit ("IsLeaf" );
isFull = //L.bit ("isFull" );
leafIsFull = //L.bit ("leafIsFull" );
branchIsFull = //L.bit ("branchIsFull" );
parentIsFull = //L.bit ("parentIsFull" );
isEmpty = //L.bit ("isEmpty" );
isLow = //L.bit ("isLow" );
hasLeavesForChildren = //L.bit ("hasLeavesForChildren" );
stolenOrMerged = //L.bit ("stolenOrMerged" );
pastMaxDepth = //L.bit ("pastMaxDepth" );
nodeMerged = //L.bit ("nodeMerged" );
mergeable = L.bit ("mergeable" );
deleted = L.bit ("deleted" );
branchBase = L.variable ("branchBase" , bitsPerAddress);
leafSize = L.variable ("leafSize" , bitsPerSize);
childSize = branchSize = L.variable ("branchSize" , bitsPerSize);
top = L.variable ("top" , bitsPerNext);
Key = L.variable ("Key" , bitsPerKey());
Data = L.variable ("Data" , bitsPerData());
find = L.variable ("find" , bitsPerNext);
findAndInsert = L.variable ("findAndInsert" , bitsPerNext);
parent = L.variable ("parent" , bitsPerNext);
child = L.variable ("child" , bitsPerNext);
leafFound = L.variable ("leafFound" , bitsPerNext);
maxKeysPerLeaf = L.variable ("maxKeysPerLeaf" , bitsPerSize);
maxKeysPerBranch = L.variable ("maxKeysPerBranch" , bitsPerSize);
two = L.variable ("two" , bitsPerSize);
mergeIndex = L.variable ("mergeIndex" , bitsPerSize);
memoryIOAddress = L.variable ("memoryIOAddress" , bitsPerNext);
memoryIODirection = L.bit ("memoryIODirection");
node_isLeaf = //L.variable ("node_isLeaf" , bitsPerNext);
node_setLeaf = //L.variable ("node_setLeaf" , bitsPerNext);
node_setBranch = L.variable ("node_setBranch" , bitsPerNext);
node_assertLeaf = //L.variable ("node_assertLeaf" , bitsPerNext);
node_assertBranch = L.variable ("node_assertBranch" , bitsPerNext);
allocLeaf = //L.variable ("allocLeaf" , bitsPerNext);
allocBranch = L.variable ("allocBranch" , bitsPerNext);
node_free = //L.variable ("node_free" , bitsPerNext);
node_clear = //L.variable ("node_clear" , bitsPerNext);
node_erase = L.variable ("node_erase" , bitsPerNext);
node_leafBase = //L.variable ("node_leafBase" , bitsPerNext);
node_branchBase = //L.variable ("node_branchBase" , bitsPerNext);
node_leafSize = //L.variable ("node_leafSize" , bitsPerNext);
node_branchSize = //L.variable ("node_branchSize" , bitsPerNext);
node_isFull = //L.variable ("node_isFull" , bitsPerNext);
node_leafIsFull = //L.variable ("node_leafIsFull" , bitsPerNext);
node_branchIsFull = //L.variable ("node_branchIsFull" , bitsPerNext);
node_parentIsFull = //L.variable ("node_parentIsFull" , bitsPerNext);
node_isEmpty = //L.variable ("node_isEmpty" , bitsPerNext);
node_isLow = L.variable ("node_isLow" , bitsPerNext);
node_hasLeavesForChildren = //L.variable ("node_hasLeavesForChildren" , bitsPerNext);
node_top = //L.variable ("node_top" , bitsPerNext);
node_findEqualInLeaf = //L.variable ("node_findEqualInLeaf" , bitsPerNext);
node_findFirstGreaterThanOrEqualInLeaf = //L.variable ("node_findFirstGreaterThanOrEqualInLeaf" , bitsPerNext);
node_findFirstGreaterThanOrEqualInBranch = //L.variable ("node_findFirstGreaterThanOrEqualInBranch" , bitsPerNext);
node_splitLeaf = //L.variable ("node_splitLeaf" , bitsPerNext);
node_splitBranch = //L.variable ("node_splitBranch" , bitsPerNext);
node_stealFromLeft = //L.variable ("node_stealFromLeft" , bitsPerNext);
node_stealFromRight = //L.variable ("node_stealFromRight" , bitsPerNext);
node_mergeRoot = //L.variable ("node_mergeRoot" , bitsPerNext);
node_mergeLeftSibling = //L.variable ("node_mergeLeftSibling" , bitsPerNext);
node_mergeRightSibling = //L.variable ("node_mergeRightSibling" , bitsPerNext);
node_balance = L.variable ("node_balance" , bitsPerNext);
final Layout.Structure transaction = L.structure("transaction",
allocate,
nextFree,
success,
inserted,
first,
next,
search,
found,
key,
data,
firstKey,
lastKey,
flKey,
parentKey,
lk,
ld,
rk,
rd,
index,
nl,
nr,
l,
r,
splitParent,
//IsLeaf,
//isFull,
//leafIsFull,
//branchIsFull,
//parentIsFull,
//isEmpty,
//isLow,
//hasLeavesForChildren,
//stolenOrMerged,
//pastMaxDepth,
//nodeMerged,
mergeable,
deleted,
branchBase,
leafSize,
//childSize
branchSize,
top,
Key,
Data,
find,
findAndInsert,
parent,
child,
leafFound,
maxKeysPerLeaf,
maxKeysPerBranch,
two,
mergeIndex,
memoryIOAddress,
memoryIODirection,
//node_isLeaf,
//node_setLeaf,
node_setBranch,
//node_assertLeaf,
node_assertBranch,
//allocLeaf,
allocBranch,
//node_free,
//node_clear,
node_erase,
//node_leafBase,
//node_branchBase,
//node_leafSize,
//node_branchSize,
//node_isFull,
//node_leafIsFull,
//node_branchIsFull,
//node_parentIsFull,
//node_isEmpty,
node_isLow,
//node_hasLeavesForChildren,
//node_top,
//node_findEqualInLeaf,
//node_findFirstGreaterThanOrEqualInLeaf,
//node_findFirstGreaterThanOrEqualInBranch,
//node_splitLeaf,
//node_splitBranch,
//node_stealFromLeft,
//node_stealFromRight,
//node_mergeRoot,
//node_mergeLeftSibling,
//node_mergeRightSibling,
node_balance);
return L.compile();
}
//D1 Memory allocation // Allocate and free memory
private void allocate(boolean check) // Allocate a node with or without checking for sufficient free space
{zz();
T.at(allocate).move(F.at(freeChainHead)); // Node at head of free nodes list
if (check)
{P.new If (T.at(allocate))
{void Else()
{P.new I()
{void a() {stop("No more memory available");} // No more free nodes available
String v() {return "/* No more memory available */";}
};
}
};
}
nC.loadNode(T.at(allocate)); // Load allocated node
F.at(freeChainHead).move(nC.N.at(free)); // Second node on free list
nC.zero(); // Clear the node
nC.saveNode(T.at(allocate)); // Construct and clear the node
}
private void allocate() {z(); allocate(true);} // Allocate a node checking for free space
private void allocLeaf() // Allocate leaf
{zz();
allocate();
tt(allocLeaf, allocate);
tt(node_setLeaf, allocate);
nC.loadNode(T.at(allocate)); // Load the allocated node
nC.setLeaf(); // Set as a leaf
nC.saveNode(T.at(allocate)); // Write back into memory
}
private void allocBranch() // Allocate branch
{zz();
allocate();
tt(allocBranch , allocate);
tt(node_setBranch, allocate);
nC.loadNode(T.at(allocate)); // Load the allocated node
nC.setBranch(); // Set as a branch
nC.saveNode(T.at(allocate)); // Write back into memory
}
private void free(Layout.Variable node_free) // Free a node to make it available for reuse
{zz();
nC.ones(); // Clear the node
nC.N.at(free).move(F.at(freeChainHead)); // Chain this node in front of the last freed node
nC.saveNode(T.at(node_free)); // Save node
F.at(freeChainHead).move(T.at(node_free)); // Make this node the head of the free chain
}
private void hasLeavesForChildren(StuckDM bLeaf) // The node has leaves for children
{zz();
bLeaf.firstElement(); // Was lastElement but firstElement() is faster
nC.loadNode(bLeaf.T.at(bLeaf.tData));
nC.isLeaf(T.at(hasLeavesForChildren));
}
//D2 Node // Description of a node
class Node // Node in a btree. Assumes that both leaves and branches occupy the same amount of memory and compute their size in the same way avoiding the need to differentiate between the two.
{final String name;
final MemoryLayoutDM N;
Node(String Name) // Create a node.
{zz(); name = Name;
N = new MemoryLayoutDM(nodeLayout, name);
N.program(P);
}
void zero() // Clear the memory associated with this node to zeroes
{zz();
P.new I()
{void a() {N.zero();}
String v() {return N.name + " <= 0; /* MemoryLayoutDM.zero */";}
};
}
void ones() // Set the memory associated with this node to ones
{zz();
P.new I()
{void a() {N.ones();}
String v() {return N.name + " <= -1; /* MemoryLayoutDM.ones */";}
};
}
void loadRoot() {zz(); loadNode(null);} // Read the root from memory into this node
void loadNode(MemoryLayoutDM.At at) // Load this node from addressed memory
{zz();
P.parallelStart();
if (at != null) T.at(memoryIOAddress).move(at); // Index of node in memory
else T.at(memoryIOAddress).zero();
P.parallelSection(); T.at(memoryIODirection).zero(); // Read
P.parallelEnd();
P.nop(); // Let memory catch up
P.new I()
{void a()
{N.top().moveBits(M.at(node, T.at(memoryIOAddress)));
}
String v()
{return N.name()+" <= memoryOut; /* loadNode */\n";
}
};
}
void saveRoot() {zz(); saveNode(null);} // Write the root into memory from this node
void saveNode(MemoryLayoutDM.At at) // Save the node indexed by this variable into memory
{zz();
P.parallelStart();
if (at != null) T.at(memoryIOAddress).move(at); // Index of node in memory
else T.at(memoryIOAddress).zero();
P.parallelSection(); T.at(memoryIODirection).ones(); // Write
P.parallelSection(); memoryIn.N.top().move(N.top()); // Transfer from memory interface buffer into node
P.parallelEnd();
P.new I()
{void a()
{M.at(node, T.at(memoryIOAddress)).moveBits(memoryIn.N.top());
}
String v()
{return "/* loadNode */";
}
};
T.at(memoryIODirection).zero(); // Reset the write flag to prevent further writes from occurring
}
void loadRootStuck(StuckDM Stuck) // Load a root stuck from main memory
{zz();
loadRoot();
loadStuck(Stuck);
}
void loadStuck(StuckDM Stuck) // Load a stuck from a node
{zz(); Stuck.M.copy(N.at(branchOrLeaf));
}
void loadStuck(StuckDM Stuck, Layout.Variable at) // Load a stuck from indexed main memory via this node with the index in transaction memory
{zz();
loadNode(T.at(at));
loadStuck(Stuck);
}
void loadStuck(StuckDM Stuck, MemoryLayoutDM.At at) // Load a stuck from indexed main memory via this node with the index in any memory
{zz();
loadNode(at);
loadStuck(Stuck);
}
void loadLeafStuckAndSize // Load a stuck from indexed main memory and store its size in a temporary variable,
(StuckDM Stuck, Layout.Variable at, Layout.Variable field)
{zz();
loadStuck(Stuck, at); // Load stuck from node memory
T.at(field).move(Stuck.M.at(Stuck.currentSize)); // Save size
}
void loadBranchStuckAndSize // Load a stuck from indexed main memory and store its size in a temporary variable,
(StuckDM Stuck, Layout.Variable at, Layout.Variable field)
{zz();
loadStuck(Stuck, at);
T.at(field).add(Stuck.M.at(Stuck.currentSize), -1); // Account for top
}
void saveRootStuck(StuckDM Stuck) // Save root stuck into main memory
{zz();
saveStuck(Stuck);
saveRoot();
}
void saveStuck(StuckDM Stuck) // Save a stuck from a node
{zz(); N.at(branchOrLeaf).copy(Stuck.M);
}
void saveStuck(StuckDM Stuck, Layout.Variable at) // Save a stuck into indexed main memory
{zz();
saveStuck(Stuck);
saveNode(T.at(at));
}
void setLeaf() // Mark a node as a leaf
{zz(); N.at(isLeaf).ones();
}
void setBranch() // Mark a node as a bramch
{zz(); N.at(isLeaf).zero();
}
void isLeaf(MemoryLayoutDM.At at) // Check for a leaf
{zz(); at.copy(N.at(isLeaf));
}
void leafSize(MemoryLayoutDM.At at) // Get size of leaf stuck without having to load the stuck
{zz();
at.copy(N.at(node_size)); // Relies on size being in the same position and having the same size in both branches and leaves
}
void branchSize(MemoryLayoutDM.At at) // Get size of branch stuck without having to load the stuck
{zz();
at.copy(N.at(node_size)); // Relies on size being in the same position and having the same size in both branches and leaves
at.dec(); // Account for top
}
void isRootEmpty(MemoryLayoutDM.At at) // Set the specified reference to one if the root of is empty else zero
{zz();
loadRoot();
P.new If (N.at(node_size))
{void Then() {at.zero();};
void Else() {at.one ();};
};
}
void isFull() // Set isFull to show whether the node is full or not, incidentlly setting IsLeaf as well
{zz();
isLeaf(T.at(IsLeaf));
P.new If (T.at(IsLeaf))
{void Then()
{z();
N.at(node_size).equal(T.at(maxKeysPerLeaf), T.at(isFull));
}
void Else()
{z();
N.at(node_size).greaterThan(T.at(maxKeysPerBranch), T.at(isFull)); // The presence of top adds one more to the size so greater than is required rather than equals
}
};
}
void isRootLeaf(MemoryLayoutDM.At at) // Set the specified reference to one if the root is a leaf else zero
{zz();
loadRoot();
isLeaf(at);
}
MemoryLayoutDM.At isLeaf() {zz(); return N.at(isLeaf);} // Return a variable that shows whether a node contains a leaf
void isLeafFull() // Set isFull to show whether the node known to be a leaf is full or not
{zz();
N.at(node_size).equal(T.at(maxKeysPerLeaf), T.at(isFull));
}
public String toString() {return ""+N;} // As string
String print(int at) // Print the indexed node
{N.memory().copy(M.memory(), M.at(node, at).at);
return ""+N;
}
}
//D2 Search // Search within a node and update the node description with the results
private void findEqualInLeaf(MemoryLayoutDM.At Key, Node Node) // Find the first key in the node that is equal to the search key
{zz();
Node.loadStuck(lEqual);
lEqual.search(Key, T.at(found), T.at(index), T.at(data));
}
private void findFirstGreaterThanOrEqualInLeaf // Find the first key in the leaf that is equal to or greater than the search key
(StuckDM Leaf, MemoryLayoutDM.At Search,
MemoryLayoutDM.At Found, MemoryLayoutDM.At Index)
{zz();
Leaf.searchFirstGreaterThanOrEqual(true, Search, Found, Index, null, null);
}
private void findFirstGreaterInLeaf // Find the first key in the leaf that is greater than the search key
(StuckDM Leaf, MemoryLayoutDM.At Search,
MemoryLayoutDM.At Found, MemoryLayoutDM.At Index)
{zz();
Leaf.searchFirstGreater(true, Search, Found, Index, null, null);
}
private void findFirstGreaterThanOrEqualInBranch // Find the first key in the branch that is equal to or greater than the search key
(Node Node, MemoryLayoutDM.At Search, MemoryLayoutDM.At Found,
MemoryLayoutDM.At Index, MemoryLayoutDM.At Data)
{zz();
Node.loadStuck(bFirstBranch);
bFirstBranch.searchFirstGreaterThanOrEqual
(false, Search, Found, Index, null, Data);
}
//D2 Split // Split nodes in half to increase the number of nodes in the tree
private void splitLeafRoot() // Split a leaf which happens to be a full root into two half full leaves while transforming the root leaf into a branch
{zz();
allocLeaf(); tt(l, allocLeaf); // New left leaf
allocLeaf(); tt(r, allocLeaf); // New right leaf
nT.loadRootStuck(lT); // Load root
nL.loadStuck(lL, l); // Clear left stuck
nR.loadStuck(lR, r); // Clear right stuck
lT.split(lL, lR); // Split root leaf into child leaves
P.parallelStart(); lR.firstElement(); // First of right
P.parallelSection(); lL. lastElement(); // Lat of left
P.parallelSection(); nT.setBranch(); // The root is a branch
P.parallelSection(); bT.clear(); // Clear the root
P.parallelEnd();
P.parallelStart(); T.at(firstKey).move(lR.T.at(lR.tKey)); // First of right leaf
P.parallelSection(); T.at(lastKey ).move(lL.T.at(lL.tKey)); // Last of left leaf
P.parallelEnd();
P.new I() // Mid key - keys are likely to be bigger than 31 bits
{void a()
{T.at(flKey).setInt((T.at(firstKey).getInt()+T.at(lastKey).getInt())/2);
}
String v()
{return T.at(flKey) .verilogLoad() + "<= " +
"("+T.at(firstKey).verilogLoad() + " + " +
T.at(lastKey) .verilogLoad() + ") / 2; /* MidKey1 */";
}
};
P.parallelStart(); bT.T.at(bT.tKey ).move(T.at(flKey));
P.parallelSection(); bT.T.at(bT.tData).move(T.at(l));
P.parallelEnd();
bT.push(); // Insert left leaf into root
P.parallelStart(); bT.T.at(bT.tKey).zero();
P.parallelSection(); bT.T.at(bT.tData).move(T.at(r));
P.parallelEnd();
bT.push(); // Insert right into root. This will be the top node and so ignored by search ... except last.
nT.saveRootStuck(bT);
nL.saveStuck(lL, l);
nR.saveStuck(lR, r);
}
private void splitBranchRoot() // Split a branch which happens to be a full root into two half full branches while retaining the current branch as the root