Debian Codegraph @

native/NativeMemoryLikelihoodCore.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "NativeMemoryLikelihoodCore.h"
5
6 /**** COMMENT OUT TO TURN OFF DEBUGGING ****/
7 //#define DEBUG
8
9 #define DLIM 100
10
11 /* Definition of REAL can be switched between 'double' and 'float' */
12
13 #define REAL double
14 #define SIZE_REAL sizeof(REAL)
15 #define INT int
16 #define SIZE_INT sizeof(int)
17
18
19 JNIEXPORT jint JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_getNativeRealSize
20 (JNIEnv *env, jobject obj) {
21 return (jint) SIZE_REAL;
22 }
23
24 JNIEXPORT jint JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_getNativeIntSize
25 (JNIEnv *env, jobject obj) {
26 return (jint) SIZE_INT;
27 }
28
29 JNIEXPORT jlong JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_allocateNativeMemoryArray
30 (JNIEnv *env, jobject obj, jint length)
31 {
32
33
34
35 #ifdef DEBUG
36 fprintf(stderr,"Entering ANMA\n");
37 #endif
38
39 REAL *data = (REAL *)malloc(SIZE_REAL * length);
40 if (data == NULL) {
41 fprintf(stderr,"Failed to allocate memory!");
42 exit(-1);
43 }
44
45 #ifdef DEBUG
46 fprintf(stderr,"Leaving ANMA\n");
47 #endif
48
49 return (jlong) data;
50 }
51
52 JNIEXPORT jlong JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_allocateNativeIntMemoryArray
53 (JNIEnv *env, jobject obj, jint length)
54 {
55
56 #ifdef DEBUG
57 fprintf(stderr,"Entering ANMA\n");
58 #endif
59
60 INT *data = (INT *)malloc(SIZE_INT * length);
61 if (data == NULL) {
62 fprintf(stderr,"Failed to allocate memory!");
63 exit(-1);
64 }
65
66 #ifdef DEBUG
67 fprintf(stderr,"Leaving ANMA\n");
68 #endif
69
70 return (jlong) data;
71 }
72
73
74 JNIEXPORT void JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_freeNativeMemoryArray
75 (JNIEnv *env, jobject obj, jlong inNativePtr)
76 {
77
78 #ifdef DEBUG
79 fprintf(stderr,"Entering FNMA\n");
80 #endif
81
82 if (inNativePtr != NULL) {
83 free((void*)inNativePtr);
84 }
85
86 #ifdef DEBUG
87 fprintf(stderr,"Leaving ANMA\n");
88 #endif
89
90 }
91
92
93 JNIEXPORT void JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_setNativeMemoryArray___3DIJII
94 (JNIEnv *env, jobject obj, jdoubleArray inFromJavaArray, jint fromOffset, jlong inNativePtr, jint toOffset, jint length)
95 {
96
97 #ifdef DEBUG
98 fprintf(stderr,"Entering SNMA\n");
99 #endif
100
101 jdouble *fromJavaArray = (jdouble*)(*env)->GetPrimitiveArrayCritical(env, inFromJavaArray, 0);
102
103 // if (inNativePtr == 0) {
104 // fprintf(stderr,"Throwing null pointer native exception\n");
105 // jclass nullPtr = (*env)->FindClass(env,"java/lang/NullPointerException");
106 // (*env)->ThrowNew(env,nullPtr,"Null native pointer in setNativeMemoryArray(double[])");
107 // }
108
109 // copy entry-by-entry to ensure conversion to REAL
110 REAL *toPtr = (REAL *)(inNativePtr + SIZE_REAL*toOffset);
111 int i;
112 for(i=0; i<length; i++) {
113 toPtr[i] = (REAL) fromJavaArray[i+fromOffset]; // Can speed-up by moving starting pointers by offset, TODO
114 }
115
116 (*env)->ReleasePrimitiveArrayCritical(env, inFromJavaArray, fromJavaArray, JNI_ABORT);
117
118 #ifdef DEBUG
119 fprintf(stderr,"Leaving SNMA\n");
120 #endif
121
122 }
123
124 JNIEXPORT void JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_setNativeMemoryArray___3IIJII
125 (JNIEnv *env, jobject obj, jintArray inFromJavaArray, jint fromOffset, jlong inNativePtr, jint toOffset, jint length)
126 {
127
128 #ifdef DEBUG
129 fprintf(stderr,"Entering SNMA\n");
130 #endif
131
132 jint *fromJavaArray = (jint*)(*env)->GetPrimitiveArrayCritical(env, inFromJavaArray, 0);
133
134 if (inNativePtr == 0) {
135 fprintf(stderr,"Throwing null pointer native exception\n");
136 jclass nullPtr = (*env)->FindClass(env,"java/lang/NullPointerException");
137 (*env)->ThrowNew(env,nullPtr,"Null native pointer in setNativeMemoryArray(double[])");
138 }
139 // copy entry-by-entry to ensure conversion to REAL
140 INT *toPtr = (INT *)inNativePtr;
141 int i;
142 for(i=0; i<length; i++) {
143 toPtr[i+toOffset] = (INT) fromJavaArray[i+fromOffset]; // Can speed-up by moving starting pointers by offset, TODO
144 }
145
146 (*env)->ReleasePrimitiveArrayCritical(env, inFromJavaArray, fromJavaArray, JNI_ABORT);
147
148 #ifdef DEBUG
149 fprintf(stderr,"Leaving SNMA\n");
150 #endif
151
152 }
153
154 JNIEXPORT void JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_getNativeMemoryArray__JI_3DII
155 (JNIEnv *env, jobject obj, jlong inNativePtr, jint fromOffset, jdoubleArray inToJavaArray, jint toOffset, jint length)
156 {
157
158 #ifdef DEBUG
159 fprintf(stderr,"Entering GA\n");
160 #endif
161
162 jdouble *toJavaArray = (jdouble*)(*env)->GetPrimitiveArrayCritical(env, inToJavaArray, 0);
163
164 // copy entry-by-entry to ensure conversion to Java type
165 REAL *fromPtr = (REAL *)inNativePtr;
166 int i;
167 for(i=0; i<length; i++) {
168 toJavaArray[i+toOffset] = (jdouble) fromPtr[i+fromOffset];
169 }
170
171 (*env)->ReleasePrimitiveArrayCritical(env, inToJavaArray, toJavaArray, 0); // need to save, not abort
172
173 #ifdef DEBUG
174 fprintf(stderr,"Leaving GA\n");
175 #endif
176
177 }
178
179 JNIEXPORT void JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_getNativeMemoryArray__JI_3III
180 (JNIEnv *env, jobject obj, jlong inNativePtr, jint fromOffset, jintArray inToJavaArray, jint toOffset, jint length)
181 {
182 jint *toJavaArray = (jint*)(*env)->GetPrimitiveArrayCritical(env, inToJavaArray, 0);
183
184 #ifdef DEBUG
185 fprintf(stderr,"Entering GA\n");
186 // if (inNativePtr < 100) fprintf(stderr, "inNativePtr is null in getNativeMemoryArray\n");
187 // if (inNativePtr == 0) {
188 // fprintf(stderr,"Throwing null pointer native exception\n");
189 // jclass nullPtr = (*env)->FindClass(env,"java/lang/NullPointerException");
190 // (*env)->ThrowNew(env,nullPtr,"Null native pointer in setNativeMemoryArray(double[])");
191 // }
192 #endif
193
194 // copy entry-by-entry to ensure conversion to Java type
195 INT *fromPtr = (INT *)inNativePtr;
196 int i;
197 for(i=0; i<length; i++) {
198 toJavaArray[i+toOffset] = (jint) fromPtr[i+fromOffset]; // TODO precompute offsets
199 }
200
201 (*env)->ReleasePrimitiveArrayCritical(env, inToJavaArray, toJavaArray, 0); // need to save, not abort
202
203 #ifdef DEBUG
204 fprintf(stderr,"Leaving GA\n");
205 #endif
206
207 }
208
209
210
211 JNIEXPORT void JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_nativePartialsPartialsPruning
212 (JNIEnv *env, jobject obj, jlong inPartials1, jlong inMatrices1, jlong inPartials2, jlong inMatrices2,
213 jint patternCount, jint matrixCount, jlong outPartials, jint stateCount)
214 {
215 REAL *partials1 = (REAL *)inPartials1;
216 REAL *matrices1 = (REAL *)inMatrices1;
217 REAL *partials2 = (REAL *)inPartials2;
218 REAL *matrices2 = (REAL *)inMatrices2;
219 REAL *partials3 = (REAL *)outPartials;
220
221 #ifdef DEBUG
222 fprintf(stderr,"Entering PP\n");
223 // fprintf(stderr,"Checking PartialsPartials\n");
224 // if (partials1 < 100)
225 // fprintf(stderr,"partials1 is null in PartialsPartials\n");
226 // if (partials2 < 100)
227 // fprintf(stderr,"partials2 is null in PartialsPartials\n");
228 // if (partials3 < 100)
229 // fprintf(stderr,"partials3 is null in PartialsPartials\n");
230 // if (matrices1 < 100)
231 // fprintf(stderr,"matrices1 is null in PartialsPartials\n");
232 // if (matrices2 < 100)
233 // fprintf(stderr,"matrices2 is null in PartialsPartials\n");
234 #endif
235
236 int i, k, l, j;
237 double sum1, sum2;
238
239 int u = 0;
240 int v = 0;
241 int w = 0;
242 int x,y;
243
244 for (l = 0; l < matrixCount; l++) {
245 for (k = 0; k < patternCount; k++) {
246
247 x = w;
248 for (i = 0; i < stateCount; i++) {
249 y = v;
250 sum1 = 0;
251 sum2 = 0;
252 for (j =0; j < stateCount; j++) {
253 sum1 += matrices1[x] * partials1[y];
254 sum2 += matrices2[x] * partials2[y]; x++; y++;
255 }
256 partials3[u] = sum1 * sum2;
257 u++;
258 }
259
260 v += stateCount;
261 }
262
263 w += stateCount * stateCount;
264 }
265
266 #ifdef DEBUG
267 fprintf(stderr,"Leaving SP\n");
268 #endif
269 }
270
271
272 JNIEXPORT void JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_nativeStatesPartialsPruning
273 (JNIEnv *env, jobject obj, jlong inStates1, jlong inMatrices1, jlong inPartials2, jlong inMatrices2,
274 jint patternCount, jint matrixCount, jlong outPartials, jint stateCount)
275 {
276 INT *states1 = (INT *) inStates1;
277 REAL *matrices1 = (REAL *)inMatrices1;
278 REAL *partials2 = (REAL *)inPartials2;
279 REAL *matrices2 = (REAL *)inMatrices2;
280 REAL *partials3 = (REAL *)outPartials;
281
282 #ifdef DEBUG
283 fprintf(stderr,"Entering SP\n");
284 if (states1 < DLIM)
285 fprintf(stderr,"states1\n");
286 if (matrices1 < DLIM)
287 fprintf(stderr,"matrices1\n");
288 if (partials2 < DLIM)
289 fprintf(stderr,"partials2\n");
290 if (matrices2 < DLIM)
291 fprintf(stderr,"matrices2\n");
292 if (partials3 < DLIM)
293 fprintf(stderr,"partials3\n");
294
295 fprintf(stderr,"states1 = %d\nmatrices1 = %d\npartials2 = %d\nmatrices2 = %d\npartials3 = %d\n",
296 states1,matrices1,partials2,matrices2,partials3);
297 fprintf(stderr,"<done>\n");
298 #endif
299
300 int i, l, k, state1, m;
301 REAL sum;
302 int u = 0;
303 int v = 0;
304 int w = 0;
305 int x, y;
306
307 for (l = 0; l < matrixCount; l++) {
308 for (k = 0; k < patternCount; k++) {
309
310 int state1 = states1[k];
311
312 #ifdef DEBUG
313 fprintf(stderr,"Read datum %d for m = %d, p = %d\n",state1,l,k);
314 #endif
315 if (state1 < stateCount) {
316
317 x = w;
318 for (i = 0; i < stateCount; i++) {
319
320 y = v;
321 REAL value = matrices1[x + state1];
322 #ifdef DEBUG
323 fprintf(stderr,"Read m1 %1.2e at index %d\n",value,(x+state1));
324 #endif
325
326 sum = 0;
327 for (m = 0; m < stateCount; m++) {
328 sum += matrices2[x] * partials2[y];
329 #ifdef DEBUG
330 fprintf(stderr,"Read m2 %1.2e at index %d and p2 %1.2e at index %d\n",matrices2[x],x,partials2[y],y);
331 #endif
332 x++; y++;
333 }
334
335 partials3[u] = value * sum;
336 u++;
337 }
338
339 v += stateCount;
340
341 } else {
342
343 x = w;
344 for (i = 0; i < stateCount; i++) {
345
346 y = v;
347 sum = 0;
348 for (m = 0; m < stateCount; m++) {
349 sum += matrices2[x] * partials2[y]; x++; y++;
350 }
351
352 partials3[u] = sum;
353 u++;
354 }
355
356 v += stateCount;
357 }
358 }
359
360 w += stateCount * stateCount;
361 }
362
363 #ifdef DEBUG
364 fprintf(stderr,"Leaving SP\n");
365 #endif
366
367 }
368
369
370
371 JNIEXPORT void JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_nativeIntegratePartials
372 (JNIEnv *env, jobject obj, jlong inPartials, jdoubleArray inProportions, jint patternCount, jint matrixCount,
373 jdoubleArray outPartials, jint stateCount)
374 {
375 REAL *partials1 = (REAL *) inPartials;
376
377 #ifdef DEBUG
378 fprintf(stderr,"Entering IP\n");
379 // fprintf(stderr,"Checking IntegratePartials\n");
380 // if (partials1 < 100) fprintf(stderr,"partials1 in IntegratePartials is null\n");
381 // fprintf(stderr,"Done IntegratePartials\n");
382 #endif
383
384 // REAL *tmp = reinterpret_cast<REAL *>(inPartials); // This statement should work without warning in C++
385
386 jdouble *proportions = (jdouble*)(*env)->GetPrimitiveArrayCritical(env, inProportions, 0);
387 jdouble *partials3 = (jdouble*)(*env)->GetPrimitiveArrayCritical(env, outPartials, 0);
388
389 int j, k, m;
390
391 int u = 0;
392 int v = 0;
393
394 for (k = 0; k < patternCount; k++) {
395
396 for (m = 0; m < stateCount; m++) {
397 partials3[u] = partials1[v] * proportions[0]; u++; v++;
398 }
399 }
400
401
402 for (j = 1; j < matrixCount; j++) {
403 u = 0;
404 for (k = 0; k < patternCount; k++) {
405
406 for (m = 0; m < stateCount; m++) {
407 partials3[u] += partials1[v] * proportions[j]; u++; v++;
408 }
409 }
410 }
411
412 (*env)->ReleasePrimitiveArrayCritical(env, outPartials, partials3, 0);
413 (*env)->ReleasePrimitiveArrayCritical(env, inProportions, proportions, JNI_ABORT);
414
415 #ifdef DEBUG
416 fprintf(stderr,"Leaving IP\n");
417 #endif
418 }
419
420
421 JNIEXPORT void JNICALL Java_dr_evomodel_treelikelihood_NativeMemoryLikelihoodCore_nativeStatesStatesPruning
422 (JNIEnv *env, jobject obj, jlong inStates1, jlong inMatrices1, jlong inStates2, jlong inMatrices2, jint patternCount,
423 jint matrixCount, jlong inPartials3, jint stateCount)
424 {
425 int j, k, u, v, w, state1, state2, m;
426
427 INT *states1 = (INT *) inStates1;
428 INT *states2 = (INT *) inStates2;
429
430 REAL *matrices1 = (REAL *) inMatrices1;
431 REAL *matrices2 = (REAL *) inMatrices2;
432
433 REAL *partials3 = (REAL *) inPartials3;
434
435 #ifdef DEBUG
436 fprintf(stderr,"Entering SS\n");
437 #endif
438
439 //#ifdef DEBUG
440 // fprintf(stderr,"Checking StatesStates\n");
441 // if (states1 < 100)
442 // fprintf(stderr,"states1 is null in StatesStates\n");
443 // if (states2 < 100)
444 // fprintf(stderr,"statess2 is null in StatesStates\n");
445 // if (partials3 < 100)
446 // fprintf(stderr,"partials3 is null in StatesStates\n");
447 // if (matrices1 < 100)
448 // fprintf(stderr,"matrices1 is null in StatesStates\n");
449 // if (matrices2 < 100)
450 // fprintf(stderr,"matrices2 is null in StatesStates\n");
451 //#endif
452
453 v = 0;
454 u = 0;
455 for (j = 0; j < matrixCount; j++) {
456
457 for (k = 0; k < patternCount; k++) {
458
459 w = u;
460
461 state1 = states1[k];
462 state2 = states2[k];
463
464 // fprintf(stderr,"p = %d:%d\n",state1,state2);
465
466 if (state1 < stateCount && state2 < stateCount) {
467
468 for (m = 0; m < stateCount; m++) {
469 partials3[v] = matrices1[w + state1] * matrices2[w + state2];
470 // fprintf(stderr,"m: %d, p:%d, m:%d, %1.2e = %1.2e * %1.2e\n",j,k,m,
471 // partials3[v],
472 // matrices1[w+state1],
473 // matrices2[w+state2]);
474 v++; w += stateCount;
475 }
476
477 } else if (state1 < stateCount) {
478
479 for (m = 0; m < stateCount; m++) {
480 partials3[v] = matrices1[w + state1];
481 v++; w += stateCount;
482 }
483
484 } else if (state2 < stateCount) {
485
486 for (m = 0; m < stateCount; m++) {
487 partials3[v] = matrices2[w + state2];
488 v++; w += stateCount;
489 }
490
491 } else {
492 // both children have a gap or unknown state so set partials to 1
493 for (m = 0; m < stateCount; m++) {
494 partials3[v] = 1.0;
495 v++;
496 }
497 }
498 }
499
500 u += stateCount * stateCount;
501 }
502 // fprintf(stderr,"NATIVE: v_last = %d\n",v);
503
504 #ifdef DEBUG
505 fprintf(stderr,"Leaving SS\n");
506 #endif
507
508 }
509