@@ -65,6 +65,46 @@ public class CoalescentIntervalTraversal extends TreeTraversal {
6565
6666 private int kIndexOffsetPat = SLAB_DUMP_K_INDEX_OFFSET_PAT_DEFAULT ;
6767
68+ private int [] denseVal ;
69+ private int [] denseGen ;
70+ private int denseGenCounter = 0 ;
71+ private int denseUsed ;
72+ private int denseTipCount ;
73+
74+ private void resetBufferDensification () {
75+ denseTipCount = treeModel .getExternalNodeCount ();
76+ denseUsed = denseTipCount ;
77+ denseGenCounter ++;
78+ if (denseVal == null ) {
79+ int initial = Math .max (1024 , treeModel .getNodeCount () * 4 );
80+ denseVal = new int [initial ];
81+ denseGen = new int [initial ];
82+ }
83+ }
84+
85+ private int densifyBuffer (int raw ) {
86+ if (raw < denseTipCount ) {
87+ return raw ;
88+ }
89+ if (raw >= denseVal .length ) {
90+ int n = denseVal .length ;
91+ while (n <= raw ) {
92+ n <<= 1 ;
93+ }
94+ denseVal = Arrays .copyOf (denseVal , n );
95+ denseGen = Arrays .copyOf (denseGen , n );
96+ }
97+ if (denseGen [raw ] != denseGenCounter ) {
98+ denseGen [raw ] = denseGenCounter ;
99+ denseVal [raw ] = denseUsed ++;
100+ }
101+ return denseVal [raw ];
102+ }
103+
104+ private int mapBuffer (int raw ) {
105+ return slabMetadataRecording ? densifyBuffer (raw ) : raw ;
106+ }
107+
68108 private static boolean isEnvFlagSet (String name ) {
69109 try {
70110 String v = System .getenv (name );
@@ -129,6 +169,7 @@ public final void dispatchTreeTraversalCollectBranchAndNodeOperations() {
129169 treeModel .getExternalNodeCount (),
130170 treeModel .getExternalNodeCount (),
131171 kIndexOffsetPat );
172+ resetBufferDensification ();
132173 }
133174
134175 if (traversalType == TraversalType .REVERSE_LEVEL_ORDER ) {
@@ -143,6 +184,24 @@ public void setSlabMetadataRecording(boolean enabled) {
143184 this .slabMetadataRecording = enabled ;
144185 }
145186
187+ public boolean isSlabMetadataRecording () {
188+ return slabMetadataRecording ;
189+ }
190+
191+ public int bufferForNode (int nodeNumber ) {
192+ if (!slabMetadataRecording ) {
193+ return nodeNumber ;
194+ }
195+ if (nodeNumber < denseTipCount ) {
196+ return nodeNumber ;
197+ }
198+ if (denseVal != null && nodeNumber >= 0 && nodeNumber < denseVal .length
199+ && denseGen [nodeNumber ] == denseGenCounter ) {
200+ return denseVal [nodeNumber ];
201+ }
202+ return -1 ;
203+ }
204+
146205 public int [] buildAndPackSlabMetadata (int slabOpsPerBlock ) {
147206 if (!slabMetadataRecording ) {
148207 throw new IllegalStateException (
@@ -424,9 +483,9 @@ private int computeTransmissionProbabilities(int subInterval, NodeRef node, doub
424483 private void propagateTransmissionProbabilities (int subInterval , NodeRef node , double length ,
425484 ActiveNodesForInterval activeNodesForInterval ) {
426485
427- final int inputBuffer1 = activeNodesForInterval .getActiveBuffer (node );
486+ final int inputBuffer1 = mapBuffer ( activeNodesForInterval .getActiveBuffer (node ) );
428487 activeNodesForInterval .incrementActiveBuffer (node );
429- final int outputBuffer = activeNodesForInterval .getActiveBuffer (node );
488+ final int outputBuffer = mapBuffer ( activeNodesForInterval .getActiveBuffer (node ) );
430489 final int executionOrder = activeNodesForInterval .getExecutionOrder (node ) + 1 ;
431490
432491 final int inputMatrix1 = computeTransmissionProbabilities (subInterval , node , length );
@@ -455,13 +514,13 @@ private void coalescenceTransmissionProbabilities(int subInterval, NodeRef nodeA
455514 NodeRef leftChild , NodeRef rightChild , double length ,
456515 ActiveNodesForInterval activeNodesForInterval ) {
457516
458- final int inputBuffer1 = activeNodesForInterval .getActiveBuffer (leftChild );
459- final int inputBuffer2 = activeNodesForInterval .getActiveBuffer (rightChild );
517+ final int inputBuffer1 = mapBuffer ( activeNodesForInterval .getActiveBuffer (leftChild ) );
518+ final int inputBuffer2 = mapBuffer ( activeNodesForInterval .getActiveBuffer (rightChild ) );
460519
461- final int extraBuffer1 = activeNodesForInterval .getAccumulationBuffer (leftChild );
462- final int extraBuffer2 = activeNodesForInterval .getAccumulationBuffer (rightChild );
520+ final int extraBuffer1 = mapBuffer ( activeNodesForInterval .getAccumulationBuffer (leftChild ) );
521+ final int extraBuffer2 = mapBuffer ( activeNodesForInterval .getAccumulationBuffer (rightChild ) );
463522
464- final int outputBuffer = activeNodesForInterval .getActiveBuffer (nodeAtTopOfInterval );
523+ final int outputBuffer = mapBuffer ( activeNodesForInterval .getActiveBuffer (nodeAtTopOfInterval ) );
465524 final int executionOrder = Math .max (
466525 activeNodesForInterval .getExecutionOrder (leftChild ),
467526 activeNodesForInterval .getExecutionOrder (rightChild )) + 1 ;
0 commit comments