Debian Codegraph @

src/dr/oldevomodel/indel/NativeTreeLikelihood.java

raw symbols SCIP
1 /*
2 * NativeTreeLikelihood.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.oldevomodel.indel;
27
28
29
30
31 /**
32 * NativeTreeLikelihood - calls an implementation of HomologyRecursion.treeRecursion in C.
33 *
34 * @author Gerton Lunter
35 *
36 * 10/9/2003
37 *
38 */
39
40 public class NativeTreeLikelihood {
41
42 private static boolean isNativeAvailable = false;
43 private int[][] iSequences; // rest are references to existing arrays
44 private int iNN;
45 private int iMUD;
46 private long iStaticDataHandle;
47
48 static {
49 try {
50
51 System.loadLibrary("NativeTreeLikelihood");
52 System.err.println("Fast TKF91 homology integrator code found");
53 isNativeAvailable = true;
54
55 } catch (UnsatisfiedLinkError e) {
56
57 System.err.println("Using Java TKF91 homology integrator code");
58
59 }
60 }
61
62
63 /**
64 * Constructor
65 *
66 */
67
68 public NativeTreeLikelihood() { }
69
70
71 /**
72 * Hello, anybody?
73 *
74 */
75
76 public boolean isAvailable() {
77 return isNativeAvailable;
78 }
79
80 /**
81 * Initialisation. Returns with 'handle' (actually a pointer) to static data.
82 *
83 */
84
85 private native long nativeInit(int[] iParent, double[] iEquil, double[] iTrans,
86 double[] iH, double[] iN, double[] iE, double[] iB);
87
88 /**
89 * The actual recursion. 'iSD' is the handle returned by nativeInit.
90 *
91 */
92
93 private native double nativeTreeRecursion(int[] iSignature, int[] iColumn, int iNumNucs, int iMUD, long iSD);
94
95 /**
96 * Relinquishes handle.
97 *
98 */
99
100 private native void nativeDestruct(long iSD);
101
102
103 /**
104 * Initialiser
105 *
106 */
107
108 public void init(int iNumNucs, int iMaxUnalignDimension, int[] iParent, double[] iEquil, double[][][] iTrans,
109 int[][] iSequences0,
110 double[] iN, double[] iH, double[] iE, double[] iB)
111 {
112 if (!isNativeAvailable)
113 return;
114
115 // copy data into local variables
116 iNN = iNumNucs;
117 iMUD = iMaxUnalignDimension;
118 iSequences = iSequences0;
119
120 // translate iTrans[][][] array into flat array
121 int iNumNodes = iTrans.length;
122 double[] iFlatTrans = new double[ iNN*iNN*iNumNodes ];
123 for (int i=0; i<iNumNodes; i++) {
124 for (int j=0; j<iNN; j++) {
125 for (int k=0; k<iNN; k++) {
126 iFlatTrans[ k + iNN*( j + iNN*i ) ] = iTrans[i][j][k];
127 }
128 }
129 }
130
131 // store data someplace where the C code has fast access
132 iStaticDataHandle = nativeInit(iParent, iEquil, iFlatTrans, iH, iN, iE, iB);
133 }
134
135
136 /**
137 * Make sure that memory is released when this object is garbage collected
138 *
139 */
140
141 protected void finalize() throws Throwable {
142
143 if (iStaticDataHandle != 0) {
144 nativeDestruct( iStaticDataHandle );
145 }
146 super.finalize();
147 }
148
149
150 /**
151 * Actual front-end for HomologyRecursion
152 *
153 */
154
155 public double treeRecursion(IntMathVec iSignature, IntMathVec iPos)
156 {
157
158 int[] iColumn = new int[ iSignature.iV.length ];
159 for (int i=0; i<iSignature.iV.length; i++) {
160 if (iSignature.iV[i] != 0)
161 iColumn[i] = iSequences[i][iPos.iV[i]];
162 }
163
164 return nativeTreeRecursion(iSignature.iV,
165 iColumn,
166 iNN,
167 iMUD,
168 iStaticDataHandle);
169 }
170
171 }
172
173
174