| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include "NativeSubstitutionModel.h" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
int stateCount = 0; |
| 9 |
int stateCountSquared = 0; |
| 10 |
|
| 11 |
#define REAL double |
| 12 |
#define SIZE_REAL sizeof(REAL) |
| 13 |
|
| 14 |
#define MINPROB 1E-10f; |
| 15 |
|
| 16 |
#define MEMCPY(to,from,length,toType) { int m; \ |
| 17 |
for(m=0; m<length; m++) { \ |
| 18 |
to[m] = (toType) from[m]; \ |
| 19 |
}} |
| 20 |
|
| 21 |
|
| 22 |
JNIEXPORT jlong JNICALL Java_dr_evomodel_substmodel_NativeSubstitutionModel_allocateNativeMemoryArray |
| 23 |
(JNIEnv *env, jobject obj, jint length) { |
| 24 |
REAL *ptr = (REAL *)malloc(length * SIZE_REAL); |
| 25 |
return (jlong) ptr; |
| 26 |
} |
| 27 |
|
| 28 |
JNIEXPORT jint JNICALL Java_dr_evomodel_substmodel_NativeSubstitutionModel_getNativeRealSize |
| 29 |
(JNIEnv *env, jobject obj) { |
| 30 |
return (jint) SIZE_REAL; |
| 31 |
} |
| 32 |
|
| 33 |
JNIEXPORT void JNICALL Java_dr_evomodel_substmodel_NativeSubstitutionModel_copyNativeMemoryArray |
| 34 |
(JNIEnv *env, jobject obj, jlong inFrom, jlong inTo, jint length) { |
| 35 |
|
| 36 |
REAL *from = (REAL *) inFrom; |
| 37 |
REAL *to = (REAL *) inTo; |
| 38 |
memcpy(to,from,length*SIZE_REAL); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
JNIEXPORT void JNICALL Java_dr_evomodel_substmodel_NativeSubstitutionModel_nativeStoreState |
| 43 |
(JNIEnv *env, jobject obj) {} |
| 44 |
|
| 45 |
JNIEXPORT void JNICALL Java_dr_evomodel_substmodel_NativeSubstitutionModel_nativeRestoreState |
| 46 |
(JNIEnv *env, jobject obj) {} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
REAL *deviceCache(int stateCount) { |
| 52 |
return (REAL *)malloc(stateCount*stateCount*SIZE_REAL); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
JNIEXPORT void JNICALL Java_dr_evomodel_substmodel_NativeSubstitutionModel_nativeGetTransitionProbabilities |
| 57 |
(JNIEnv *env, jobject obj, jlong inCache, jdouble distance, jlong inPtr, jint states) { |
| 58 |
|
| 59 |
REAL *matrix, *iexp, *Ievc, *Evec, *Eval, *EvalImag; |
| 60 |
int stateCount = states; |
| 61 |
|
| 62 |
Ievc = (REAL *) inCache; |
| 63 |
Evec = Ievc + states*stateCount; |
| 64 |
Eval = Evec + stateCount*stateCount; |
| 65 |
EvalImag = Eval + stateCount; |
| 66 |
|
| 67 |
matrix = (REAL *) inPtr; |
| 68 |
|
| 69 |
iexp = deviceCache(stateCount); |
| 70 |
|
| 71 |
int i,j,k; |
| 72 |
|
| 73 |
|
| 74 |
for (i = 0; i < stateCount; i++) { |
| 75 |
|
| 76 |
if (EvalImag[i] == 0) { |
| 77 |
|
| 78 |
REAL temp = exp(distance * Eval[i]); |
| 79 |
for (j = 0; j < stateCount; j++) { |
| 80 |
iexp[i*stateCount + j] = Ievc[i*stateCount + j] * temp; |
| 81 |
} |
| 82 |
} else { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
int i2 = i + 1; |
| 87 |
REAL b = EvalImag[i]; |
| 88 |
REAL expat = exp(distance * Eval[i]); |
| 89 |
REAL expatcosbt = expat * cos(distance * b); |
| 90 |
REAL expatsinbt = expat * sin(distance * b); |
| 91 |
|
| 92 |
for (j = 0; j < stateCount; j++) { |
| 93 |
iexp[i*stateCount + j] = expatcosbt * Ievc[i*stateCount + j] + expatsinbt * Ievc[i2*stateCount + j]; |
| 94 |
iexp[i2*stateCount + j] = expatcosbt * Ievc[i2*stateCount + j] - expatsinbt * Ievc[i*stateCount + j]; |
| 95 |
} |
| 96 |
i++; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
int u = 0; |
| 101 |
for (i = 0; i < stateCount; i++) { |
| 102 |
for (j = 0; j < stateCount; j++) { |
| 103 |
REAL temp = 0.0; |
| 104 |
for (k = 0; k < stateCount; k++) { |
| 105 |
temp += Evec[i*stateCount + k] * iexp[k*stateCount + j]; |
| 106 |
} |
| 107 |
if (temp < 0.0) |
| 108 |
matrix[u] = 1E-10; |
| 109 |
else |
| 110 |
matrix[u] = temp; |
| 111 |
u++; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
free(iexp); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
JNIEXPORT void JNICALL Java_dr_evomodel_substmodel_NativeSubstitutionModel_getNativeMemoryArray |
| 120 |
(JNIEnv *env, jobject obj, jlong inNativePtr, jint fromOffset, jdoubleArray inToJavaArray, jint toOffset, jint length) |
| 121 |
{ |
| 122 |
|
| 123 |
jdouble *toJavaArray = (jdouble*)(*env)->GetPrimitiveArrayCritical(env, inToJavaArray, 0); |
| 124 |
jdouble *toOffsetJavaArray = toJavaArray + toOffset; |
| 125 |
|
| 126 |
REAL *fromPtr = (REAL *)inNativePtr + fromOffset; |
| 127 |
MEMCPY(toOffsetJavaArray,fromPtr,length,jdouble); |
| 128 |
|
| 129 |
(*env)->ReleasePrimitiveArrayCritical(env, inToJavaArray, toJavaArray, 0); |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
JNIEXPORT void JNICALL Java_dr_evomodel_substmodel_NativeSubstitutionModel_nativeSetup |
| 135 |
(JNIEnv *env, jobject obj, jlong inPtr, jobjectArray inIevc, jobjectArray inEvec, jdoubleArray inEval, |
| 136 |
jdoubleArray inEvalImag, jint stateCount) { |
| 137 |
|
| 138 |
if( inPtr == 0 ) { |
| 139 |
printf("Null pointer sent.\n"); |
| 140 |
exit(-1); |
| 141 |
} |
| 142 |
|
| 143 |
REAL *Ievc, *tmpIevc, *Evec, *tmpEvec, *Eval, *EvalImag; |
| 144 |
|
| 145 |
tmpIevc = Ievc = (REAL *) inPtr; |
| 146 |
tmpEvec = Evec = Ievc + stateCount*stateCount; |
| 147 |
Eval = Evec + stateCount*stateCount; |
| 148 |
EvalImag = Eval + stateCount; |
| 149 |
|
| 150 |
jdouble *javaEval = (jdouble*)(*env)->GetPrimitiveArrayCritical(env,inEval,0); |
| 151 |
jdouble *javaEvalImag = (jdouble*)(*env)->GetPrimitiveArrayCritical(env,inEvalImag,0); |
| 152 |
|
| 153 |
int i,j; |
| 154 |
for(i=0; i<stateCount; i++) { |
| 155 |
jdoubleArray oneDim = (*env)->GetObjectArrayElement(env,inIevc,i); |
| 156 |
jdouble *element = (jdouble*)(*env)->GetPrimitiveArrayCritical(env,oneDim,0); |
| 157 |
MEMCPY(tmpIevc,element,stateCount,REAL); |
| 158 |
tmpIevc += stateCount; |
| 159 |
(*env)->ReleasePrimitiveArrayCritical(env,oneDim,element,JNI_ABORT); |
| 160 |
|
| 161 |
oneDim = (*env)->GetObjectArrayElement(env,inEvec,i); |
| 162 |
element = (jdouble*)(*env)->GetPrimitiveArrayCritical(env,oneDim,0); |
| 163 |
MEMCPY(tmpEvec,element,stateCount,REAL); |
| 164 |
tmpEvec += stateCount; |
| 165 |
(*env)->ReleasePrimitiveArrayCritical(env,oneDim,element,JNI_ABORT); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
MEMCPY(Eval,javaEval,stateCount,REAL); |
| 170 |
MEMCPY(EvalImag, javaEvalImag,stateCount,REAL); |
| 171 |
|
| 172 |
(*env)->ReleasePrimitiveArrayCritical(env,inEval, javaEval, JNI_ABORT); |
| 173 |
(*env)->ReleasePrimitiveArrayCritical(env,inEvalImag, javaEvalImag, JNI_ABORT); |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
|