| 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 |
package dr.evomodel.treelikelihood; |
| 27 |
|
| 28 |
import beagle.Beagle; |
| 29 |
import dr.evomodel.branchmodel.BranchModel; |
| 30 |
import dr.evomodel.substmodel.EigenDecomposition; |
| 31 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 32 |
import dr.evolution.tree.Tree; |
| 33 |
import dr.evomodel.treedatalikelihood.BufferIndexHelper; |
| 34 |
import dr.util.Timer; |
| 35 |
|
| 36 |
import java.io.Serializable; |
| 37 |
import java.util.ArrayDeque; |
| 38 |
import java.util.ArrayList; |
| 39 |
import java.util.Deque; |
| 40 |
import java.util.List; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public final class SubstitutionModelDelegate implements EvolutionaryProcessDelegate, Serializable { |
| 49 |
|
| 50 |
private static final boolean DEBUG = false; |
| 51 |
private static final boolean RUN_IN_SERIES = false; |
| 52 |
public static final boolean MEASURE_RUN_TIME = true; |
| 53 |
public double updateTime; |
| 54 |
public double convolveTime; |
| 55 |
|
| 56 |
private static final int BUFFER_POOL_SIZE_DEFAULT = 100; |
| 57 |
|
| 58 |
private final Tree tree; |
| 59 |
private final List<SubstitutionModel> substitutionModelList; |
| 60 |
private final BranchModel branchModel; |
| 61 |
|
| 62 |
private final int eigenCount; |
| 63 |
private final int nodeCount; |
| 64 |
|
| 65 |
private final int extraBufferCount; |
| 66 |
private final int reserveBufferIndex; |
| 67 |
|
| 68 |
private final BufferIndexHelper eigenBufferHelper; |
| 69 |
private BufferIndexHelper matrixBufferHelper; |
| 70 |
|
| 71 |
private Deque<Integer> availableBuffers = new ArrayDeque<Integer>(); |
| 72 |
|
| 73 |
public SubstitutionModelDelegate(Tree tree, BranchModel branchModel) { |
| 74 |
this(tree, branchModel, BUFFER_POOL_SIZE_DEFAULT); |
| 75 |
} |
| 76 |
|
| 77 |
public SubstitutionModelDelegate(Tree tree, BranchModel branchModel, int bufferPoolSize) { |
| 78 |
|
| 79 |
if (MEASURE_RUN_TIME) { |
| 80 |
updateTime = 0; |
| 81 |
convolveTime = 0; |
| 82 |
} |
| 83 |
|
| 84 |
this.tree = tree; |
| 85 |
|
| 86 |
this.substitutionModelList = branchModel.getSubstitutionModels(); |
| 87 |
|
| 88 |
this.branchModel = branchModel; |
| 89 |
|
| 90 |
eigenCount = substitutionModelList.size(); |
| 91 |
nodeCount = tree.getNodeCount(); |
| 92 |
|
| 93 |
|
| 94 |
eigenBufferHelper = new BufferIndexHelper(eigenCount, 0); |
| 95 |
|
| 96 |
|
| 97 |
matrixBufferHelper = new BufferIndexHelper(nodeCount, 0); |
| 98 |
|
| 99 |
this.extraBufferCount = branchModel.requiresMatrixConvolution() ? |
| 100 |
(bufferPoolSize > 0 ? bufferPoolSize : BUFFER_POOL_SIZE_DEFAULT) : 0; |
| 101 |
|
| 102 |
if (branchModel.requiresMatrixConvolution() && this.extraBufferCount < eigenCount) { |
| 103 |
throw new RuntimeException("SubstitutionModelDelegate requires at least " + eigenCount + " extra buffers to convolve matrices"); |
| 104 |
} |
| 105 |
|
| 106 |
for (int i = 0; i < extraBufferCount; i++) { |
| 107 |
pushAvailableBuffer(i + matrixBufferHelper.getBufferCount()); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
reserveBufferIndex = matrixBufferHelper.getBufferCount() + extraBufferCount; |
| 113 |
|
| 114 |
if (DEBUG) { |
| 115 |
System.out.println("Creating reserve buffer with index: " |
| 116 |
+ reserveBufferIndex); |
| 117 |
} |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
public boolean canReturnComplexDiagonalization() { |
| 122 |
for (SubstitutionModel model : substitutionModelList) { |
| 123 |
if (model.canReturnComplexDiagonalization()) { |
| 124 |
return true; |
| 125 |
} |
| 126 |
} |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
public int getEigenBufferCount() { |
| 131 |
return eigenBufferHelper.getBufferCount(); |
| 132 |
} |
| 133 |
|
| 134 |
public int getMatrixBufferCount() { |
| 135 |
|
| 136 |
return matrixBufferHelper.getBufferCount() + extraBufferCount + 1; |
| 137 |
} |
| 138 |
|
| 139 |
public int getSubstitutionModelCount() { |
| 140 |
return substitutionModelList.size(); |
| 141 |
} |
| 142 |
|
| 143 |
public SubstitutionModel getSubstitutionModel(int index) { |
| 144 |
return substitutionModelList.get(index); |
| 145 |
} |
| 146 |
|
| 147 |
public void updateSubstitutionModels(Beagle beagle) { |
| 148 |
for (int i = 0; i < eigenCount; i++) { |
| 149 |
eigenBufferHelper.flipOffset(i); |
| 150 |
|
| 151 |
EigenDecomposition ed = substitutionModelList.get(i).getEigenDecomposition(); |
| 152 |
|
| 153 |
beagle.setEigenDecomposition( |
| 154 |
eigenBufferHelper.getOffsetIndex(i), |
| 155 |
ed.getEigenVectors(), |
| 156 |
ed.getInverseEigenVectors(), |
| 157 |
ed.getEigenValues()); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public void updateTransitionMatrices(Beagle beagle, int[] branchIndices, double[] edgeLength, int updateCount) { |
| 162 |
|
| 163 |
int[][] probabilityIndices = new int[eigenCount][updateCount]; |
| 164 |
double[][] edgeLengths = new double[eigenCount][updateCount]; |
| 165 |
|
| 166 |
int[] counts = new int[eigenCount]; |
| 167 |
|
| 168 |
List<Deque<Integer>> convolutionList = new ArrayList<Deque<Integer>>(); |
| 169 |
|
| 170 |
for (int i = 0; i < updateCount; i++) { |
| 171 |
|
| 172 |
BranchModel.Mapping mapping = branchModel.getBranchModelMapping(tree.getNode(branchIndices[i])); |
| 173 |
int[] order = mapping.getOrder(); |
| 174 |
double[] weights = mapping.getWeights(); |
| 175 |
|
| 176 |
if (order.length == 1) { |
| 177 |
int k = order[0]; |
| 178 |
probabilityIndices[k][counts[k]] = matrixBufferHelper.getOffsetIndex(branchIndices[i]); |
| 179 |
edgeLengths[k][counts[k]] = edgeLength[i]; |
| 180 |
counts[k]++; |
| 181 |
} else { |
| 182 |
double sum = 0.0; |
| 183 |
for (double w : weights) { |
| 184 |
sum += w; |
| 185 |
} |
| 186 |
|
| 187 |
if (getAvailableBufferCount() < order.length) { |
| 188 |
|
| 189 |
computeTransitionMatrices(beagle, probabilityIndices, edgeLengths, counts); |
| 190 |
convolveMatrices(beagle, convolutionList); |
| 191 |
|
| 192 |
|
| 193 |
for (int k = 0; k < eigenCount; k++) { |
| 194 |
counts[k] = 0; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
Deque<Integer> bufferIndices = new ArrayDeque<Integer>(); |
| 199 |
for (int j = 0; j < order.length; j++) { |
| 200 |
|
| 201 |
int buffer = popAvailableBuffer(); |
| 202 |
|
| 203 |
if (buffer < 0) { |
| 204 |
|
| 205 |
throw new RuntimeException("Ran out of buffers for transition matrices - computing current list."); |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
int k = order[j]; |
| 210 |
probabilityIndices[k][counts[k]] = buffer; |
| 211 |
edgeLengths[k][counts[k]] = weights[j] * edgeLength[i] / sum; |
| 212 |
|
| 213 |
counts[k]++; |
| 214 |
|
| 215 |
bufferIndices.add(buffer); |
| 216 |
} |
| 217 |
bufferIndices.add(matrixBufferHelper.getOffsetIndex(branchIndices[i])); |
| 218 |
|
| 219 |
convolutionList.add(bufferIndices); |
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
computeTransitionMatrices(beagle, probabilityIndices, edgeLengths, counts); |
| 225 |
convolveMatrices(beagle, convolutionList); |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
private void computeTransitionMatrices(Beagle beagle, int[][] probabilityIndices, double[][] edgeLengths, int[] counts) { |
| 230 |
|
| 231 |
Timer timer; |
| 232 |
if (MEASURE_RUN_TIME) { |
| 233 |
timer = new Timer(); |
| 234 |
timer.start(); |
| 235 |
} |
| 236 |
|
| 237 |
if (DEBUG) { |
| 238 |
System.out.print("Computing matrices:"); |
| 239 |
} |
| 240 |
|
| 241 |
for (int i = 0; i < eigenCount; i++) { |
| 242 |
if (DEBUG) { |
| 243 |
for (int j = 0; j < counts[i]; j++) { |
| 244 |
|
| 245 |
System.out.print(" " + probabilityIndices[i][j] + " (" + edgeLengths[i][j] + ")"); |
| 246 |
} |
| 247 |
} |
| 248 |
if (counts[i] > 0) { |
| 249 |
beagle.updateTransitionMatrices(eigenBufferHelper.getOffsetIndex(i), |
| 250 |
probabilityIndices[i], |
| 251 |
null, |
| 252 |
null, |
| 253 |
edgeLengths[i], |
| 254 |
counts[i]); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
if (DEBUG) { |
| 259 |
System.out.println(); |
| 260 |
} |
| 261 |
|
| 262 |
if (MEASURE_RUN_TIME) { |
| 263 |
timer.stop(); |
| 264 |
double timeInSeconds = timer.toSeconds(); |
| 265 |
updateTime += timeInSeconds; |
| 266 |
} |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
private void convolveMatrices(Beagle beagle, List<Deque<Integer>> convolutionList) { |
| 271 |
|
| 272 |
Timer timer; |
| 273 |
if (MEASURE_RUN_TIME) { |
| 274 |
timer = new Timer(); |
| 275 |
timer.start(); |
| 276 |
} |
| 277 |
|
| 278 |
while (convolutionList.size() > 0) { |
| 279 |
int[] firstConvolutionBuffers = new int[nodeCount]; |
| 280 |
int[] secondConvolutionBuffers = new int[nodeCount]; |
| 281 |
int[] resultConvolutionBuffers = new int[nodeCount]; |
| 282 |
int operationsCount = 0; |
| 283 |
|
| 284 |
List<Deque<Integer>> empty = new ArrayList<Deque<Integer>>(); |
| 285 |
|
| 286 |
for (Deque<Integer> convolve : convolutionList) { |
| 287 |
|
| 288 |
if (convolve.size() > 3) { |
| 289 |
firstConvolutionBuffers[operationsCount] = convolve.pop(); |
| 290 |
secondConvolutionBuffers[operationsCount] = convolve.pop(); |
| 291 |
|
| 292 |
int buffer; |
| 293 |
boolean done; |
| 294 |
|
| 295 |
do { |
| 296 |
done = true; |
| 297 |
|
| 298 |
buffer = popAvailableBuffer(); |
| 299 |
|
| 300 |
if (buffer < 0) { |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
if (DEBUG) { |
| 306 |
System.out.println("Ran out of buffers for convolving - computing current list."); |
| 307 |
System.out.print("Convolving " + operationsCount + " matrices:"); |
| 308 |
for (int i = 0; i < operationsCount; i++) { |
| 309 |
System.out.print(" " + firstConvolutionBuffers[i] + "*" + secondConvolutionBuffers[i] + "->" + resultConvolutionBuffers[i]); |
| 310 |
} |
| 311 |
System.out.println(); |
| 312 |
} |
| 313 |
|
| 314 |
if (operationsCount > 0) { |
| 315 |
|
| 316 |
convolveAndRelease(beagle, firstConvolutionBuffers, secondConvolutionBuffers, resultConvolutionBuffers, operationsCount); |
| 317 |
|
| 318 |
|
| 319 |
firstConvolutionBuffers[0] = firstConvolutionBuffers[operationsCount]; |
| 320 |
secondConvolutionBuffers[0] = secondConvolutionBuffers[operationsCount]; |
| 321 |
|
| 322 |
|
| 323 |
operationsCount = 0; |
| 324 |
done = false; |
| 325 |
|
| 326 |
|
| 327 |
} else { |
| 328 |
|
| 329 |
|
| 330 |
resultConvolutionBuffers[operationsCount] = getReserveBuffer(); |
| 331 |
convolveAndRelease(beagle, firstConvolutionBuffers, secondConvolutionBuffers, resultConvolutionBuffers, 1); |
| 332 |
convolve.push(getReserveBuffer()); |
| 333 |
done = true; |
| 334 |
} |
| 335 |
} |
| 336 |
} while (!done); |
| 337 |
|
| 338 |
if (buffer >= 0) { |
| 339 |
|
| 340 |
|
| 341 |
resultConvolutionBuffers[operationsCount] = buffer; |
| 342 |
convolve.push(buffer); |
| 343 |
operationsCount++; |
| 344 |
} |
| 345 |
|
| 346 |
} else if (convolve.size() == 3) { |
| 347 |
firstConvolutionBuffers[operationsCount] = convolve.pop(); |
| 348 |
secondConvolutionBuffers[operationsCount] = convolve.pop(); |
| 349 |
resultConvolutionBuffers[operationsCount] = convolve.pop(); |
| 350 |
operationsCount++; |
| 351 |
} else { |
| 352 |
throw new RuntimeException("Unexpected convolve list size"); |
| 353 |
} |
| 354 |
|
| 355 |
if (convolve.size() == 0) { |
| 356 |
empty.add(convolve); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
if (DEBUG) { |
| 361 |
System.out.print("Convolving " + operationsCount+ " matrices:"); |
| 362 |
for (int i = 0; i < operationsCount; i++) { |
| 363 |
System.out.print(" " + firstConvolutionBuffers[i] + "*" + secondConvolutionBuffers[i] + "->" + resultConvolutionBuffers[i]); |
| 364 |
} |
| 365 |
System.out.println(); |
| 366 |
} |
| 367 |
|
| 368 |
convolveAndRelease(beagle, firstConvolutionBuffers, secondConvolutionBuffers, resultConvolutionBuffers, operationsCount); |
| 369 |
|
| 370 |
convolutionList.removeAll(empty); |
| 371 |
} |
| 372 |
|
| 373 |
if (MEASURE_RUN_TIME) { |
| 374 |
timer.stop(); |
| 375 |
double timeInSeconds = timer.toSeconds(); |
| 376 |
convolveTime += timeInSeconds; |
| 377 |
} |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
private void convolveAndRelease(Beagle beagle, int[] firstConvolutionBuffers, int[] secondConvolutionBuffers, int[] resultConvolutionBuffers, int operationsCount) { |
| 382 |
|
| 383 |
if (RUN_IN_SERIES) { |
| 384 |
if (operationsCount > 1) { |
| 385 |
throw new RuntimeException("Unable to convolve matrices in series"); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
beagle.convolveTransitionMatrices(firstConvolutionBuffers, |
| 390 |
secondConvolutionBuffers, |
| 391 |
resultConvolutionBuffers, |
| 392 |
operationsCount |
| 393 |
); |
| 394 |
|
| 395 |
for (int i = 0; i < operationsCount; i++) { |
| 396 |
if (firstConvolutionBuffers[i] >= matrixBufferHelper.getBufferCount() && firstConvolutionBuffers[i] != reserveBufferIndex) { |
| 397 |
pushAvailableBuffer(firstConvolutionBuffers[i]); |
| 398 |
} |
| 399 |
if (secondConvolutionBuffers[i] >= matrixBufferHelper.getBufferCount() && secondConvolutionBuffers[i] != reserveBufferIndex) { |
| 400 |
pushAvailableBuffer(secondConvolutionBuffers[i]); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
private int getAvailableBufferCount() { |
| 407 |
if (RUN_IN_SERIES) { |
| 408 |
return 0; |
| 409 |
} else { |
| 410 |
return availableBuffers.size(); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
private int popAvailableBuffer() { |
| 415 |
if (availableBuffers.isEmpty()) { |
| 416 |
return -1; |
| 417 |
} |
| 418 |
return availableBuffers.pop(); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
private int getReserveBuffer() { |
| 426 |
return reserveBufferIndex; |
| 427 |
} |
| 428 |
|
| 429 |
private void pushAvailableBuffer(int index) { |
| 430 |
availableBuffers.push(index); |
| 431 |
} |
| 432 |
|
| 433 |
public double[] getRootStateFrequencies() { |
| 434 |
return branchModel.getRootFrequencyModel().getFrequencies(); |
| 435 |
} |
| 436 |
|
| 437 |
public void flipMatrixBuffer(int branchIndex) { |
| 438 |
matrixBufferHelper.flipOffset(branchIndex); |
| 439 |
} |
| 440 |
|
| 441 |
public int getMatrixIndex(int branchIndex) { |
| 442 |
return matrixBufferHelper.getOffsetIndex(branchIndex); |
| 443 |
} |
| 444 |
|
| 445 |
public void storeState() { |
| 446 |
eigenBufferHelper.storeState(); |
| 447 |
matrixBufferHelper.storeState(); |
| 448 |
} |
| 449 |
|
| 450 |
public void restoreState() { |
| 451 |
eigenBufferHelper.restoreState(); |
| 452 |
matrixBufferHelper.restoreState(); |
| 453 |
} |
| 454 |
|
| 455 |
} |