Debian Codegraph @

jni/IceCreamAlignerJNI.c

raw symbols SCIP
1 #include <jni.h>
2 #include <stdlib.h>
3 #include "icecream_IceCreamAlignerJNI.h"
4
5 #define pointsMatch 1
6 #define pointsSub -1
7 #define pointsDel -2
8 #define pointsIns -2
9
10 void alignForwardJNI(const jint * query, const jint * ref, jint * retArray,
11 const jint qlen, const jint rlen, const jint minScore, const float minRatio) {
12
13 const int arrayLength=rlen;
14 const int arrayLength2=rlen+1;
15
16 //Stack allocated; faster.
17 int array1[arrayLength2];
18 int array2[arrayLength2];
19
20 int * prev=array1;
21 int * next=array2;
22 int maxScore=-999999;
23 int maxQpos=-1;
24 int maxRpos=-1;
25 int iters=0;
26 retArray[0]=-999999;
27
28 const int minPassingScore=(int)(qlen*minRatio*pointsMatch);
29 const int minPassingScore3=minPassingScore-qlen*pointsMatch;
30
31 for(int i=0; i<=arrayLength-qlen; i++){next[i]=prev[i]=0;}
32 for(int i=arrayLength-qlen, score=0; i<=arrayLength; i++, score+=pointsDel) {
33 prev[i]=score;
34 next[i]=0;
35 }
36
37 for(int qpos=0; qpos<qlen; qpos++){
38 prev[0]=pointsIns*qpos;
39
40 const int q=query[qpos];
41 int maxScoreThisPass=-999999;
42 const int remainingBases=(qlen-qpos);
43 const int remainingPoints=remainingBases*pointsMatch;
44 const int minViableScore=minPassingScore3-remainingPoints;
45
46 for(int rpos=0, apos=1; rpos<rlen; rpos++, apos++){
47 const int r=ref[rpos];
48 const int match=(q==r);
49 const int vScore=prev[apos]+pointsIns;
50 const int dScore=(match ? pointsMatch : pointsSub)+prev[apos-1];
51
52 //Should be branchless conditional moves
53 int score=(dScore>=vScore ? dScore : vScore);
54 next[apos]=score;
55 }
56
57 for(int apos=1; apos<arrayLength2; apos++){
58 const int hScore=next[apos-1]+pointsDel;
59
60 //Should be branchless conditional moves
61 int score=next[apos];
62 score=(hScore>score ? hScore : score);
63 next[apos]=score;
64 maxScoreThisPass=(score>maxScoreThisPass ? score : maxScoreThisPass);
65 }
66 iters+=arrayLength;
67
68 //Aggressive early exit
69 if(maxScoreThisPass<minScore){return;}
70
71 //Safe early exit
72 if(maxScoreThisPass<minViableScore){return;}
73
74 int * temp=prev;
75 prev=next;
76 next=temp;
77 }
78
79 maxScore=-999999;
80 for(int apos=1; apos<arrayLength2; apos++){//Grab high score from last iteration
81 int score=prev[apos];
82 if(score>=maxScore){
83 maxScore=score;
84 maxQpos=qlen;
85 maxRpos=apos-1;
86 }
87 }
88 retArray[0]=maxScore;
89 retArray[1]=maxQpos;
90 retArray[2]=maxRpos;
91 retArray[3]=iters;
92 }
93
94
95 void alignForward16JNI(const jshort * query, const jshort * ref, jint * retArray,
96 const jshort qlen, const jshort rlen, const jshort minScore, const float minRatio) {
97
98 const jshort arrayLength=rlen;
99 const jshort arrayLength2=rlen+1;
100
101 //Stack allocated; faster.
102 jshort array1[arrayLength2];
103 jshort array2[arrayLength2];
104
105 jshort * prev=array1;
106 jshort * next=array2;
107 jshort maxScore=-32000;
108 jshort maxQpos=-1;
109 jshort maxRpos=-1;
110 jint iters=0;
111 retArray[0]=-32000;
112
113 const jshort minPassingScore=(int)(qlen*minRatio*pointsMatch);
114 const jshort minPassingScore3=minPassingScore-qlen*pointsMatch;
115
116 for(jshort i=0; i<=arrayLength-qlen; i++){next[i]=prev[i]=0;}
117 for(jshort i=arrayLength-qlen, score=0; i<=arrayLength; i++, score+=pointsDel) {
118 prev[i]=score;
119 next[i]=0;
120 }
121
122 for(jshort qpos=0; qpos<qlen; qpos++){
123 prev[0]=pointsIns*qpos;
124
125 const jshort q=query[qpos];
126 jshort maxScoreThisPass=-32000;
127 const jshort remainingBases=(qlen-qpos);
128 const jshort remainingPoints=remainingBases*pointsMatch;
129 const jshort minViableScore=minPassingScore3-remainingPoints;
130
131 // for(jshort rpos=0, apos=1; rpos<rlen; rpos++, apos++){
132 // const jshort r=ref[rpos];
133 // const jshort match=(q==r);
134 // const jshort vScore=prev[apos]+pointsIns;
135 // const jshort hScore=next[apos-1]+pointsDel;
136 // const jshort dScore=(match ? pointsMatch : pointsSub)+prev[apos-1];
137 //
138 // //Should be branchless conditional moves
139 // jshort score=(dScore>=vScore ? dScore : vScore);
140 // score=(hScore>score ? hScore : score);
141 // next[apos]=score;
142 // maxScoreThisPass=(score>maxScoreThisPass ? score : maxScoreThisPass);
143 // }
144
145 for(jshort rpos=0, apos=1; rpos<rlen; rpos++, apos++){
146 const jshort r=ref[rpos];
147 const jshort match=(q==r);
148 const jshort vScore=prev[apos]+pointsIns;
149 const jshort dScore=(match ? pointsMatch : pointsSub)+prev[apos-1];
150
151 //Should be branchless conditional moves
152 jshort score=(dScore>=vScore ? dScore : vScore);
153 next[apos]=score;
154 }
155
156 for(jshort apos=1; apos<arrayLength2; apos++){
157 const jshort hScore=next[apos-1]+pointsDel;
158
159 //Should be branchless conditional moves
160 jshort score=next[apos];
161 score=(hScore>score ? hScore : score);
162 next[apos]=score;
163 maxScoreThisPass=(score>maxScoreThisPass ? score : maxScoreThisPass);
164 }
165 iters+=arrayLength;
166
167 //Aggressive early exit
168 if(maxScoreThisPass<minScore){return;}
169
170 //Safe early exit
171 if(maxScoreThisPass<minViableScore){return;}
172
173 jshort * temp=prev;
174 prev=next;
175 next=temp;
176 }
177
178 maxScore=-32000;
179 for(jshort apos=1; apos<arrayLength2; apos++){//Grab high score from last iteration
180 jshort score=prev[apos];
181 if(score>=maxScore){
182 maxScore=score;
183 maxQpos=qlen;
184 maxRpos=apos-1;
185 }
186 }
187 retArray[0]=maxScore;
188 retArray[1]=maxQpos;
189 retArray[2]=maxRpos;
190 retArray[3]=iters;
191 }
192
193
194 void alignForwardShortJNI(jint * query, jint * ref, jint * retArray, const jint qlen, const jint rlen) {
195
196 const jint arrayLength=qlen;
197 const jint arrayLength2=qlen+1;
198
199 //Stack allocated; faster.
200 jint array1[arrayLength2];
201 jint array2[arrayLength2];
202
203 //jint * array1=(jint *)malloc(4*arrayLength2);
204 //jint * array2=(jint *)malloc(4*arrayLength2);
205
206 jint * prev=array1;
207 jint * next=array2;
208 jint maxScore=-999999;
209 jint maxQpos=-1;
210 jint maxRpos=-1;
211 jint itersShort=0;
212
213 for(jint i=0; i<arrayLength2; i++) {
214 prev[i]=pointsIns*i;
215 next[i]=0;
216 }
217
218 for(jint rpos=0; rpos<rlen; rpos++){
219 if(rlen-rpos<arrayLength){prev[0]=next[0]+pointsDel;}
220
221 const jint r=ref[rpos];
222
223 // //Inner loop
224 // for(jint qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){
225 // const jint q=query[qpos];
226 // const jboolean match=(q==r);
227 // const jint vScore=prev[apos]+pointsIns;
228 // const jint hScore=next[apos-1]+pointsDel;
229 // const jint dScore=(match ? pointsMatch : pointsSub)+prev[apos-1];
230 //
231 // score=(dScore>=vScore ? dScore : vScore);
232 // score=(hScore>score ? hScore : score);
233 //
234 // next[apos]=score;
235 // }
236
237 //Inner DV loop
238 for(jint qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){
239 const jint q=query[qpos];
240 const jboolean match=(q==r);
241 const jint vScore=prev[apos]+pointsIns;
242 const jint dScore=(match ? pointsMatch : pointsSub)+prev[apos-1];
243
244 const jint score=(dScore>=vScore ? dScore : vScore);
245
246 next[apos]=score;
247 }
248
249 //Inner I loop
250 for(jint qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){
251 const jint hScore=next[apos-1]+pointsDel;
252
253 jint score=next[apos];
254 score=(hScore>score ? hScore : score);
255
256 next[apos]=score;
257 }
258
259 itersShort+=arrayLength;
260
261 jint score=next[arrayLength];
262 if(score>=maxScore){
263 maxScore=score;
264 maxQpos=arrayLength-1;
265 maxRpos=rpos;
266 }
267
268 jint * temp=prev;
269 prev=next;
270 next=temp;
271 }
272 retArray[0]=maxScore;
273 retArray[1]=maxQpos;
274 retArray[2]=maxRpos;
275 retArray[3]=itersShort;
276
277 //free(array1);
278 //free(array2);
279 }
280
281
282 void alignForwardShort16JNI(jshort * query, jshort * ref, jint * retArray, const jshort qlen, const jshort rlen) {
283
284 const jshort arrayLength=qlen;
285 const jshort arrayLength2=qlen+1;
286
287 //Stack allocated; faster.
288 jshort array1[arrayLength2];
289 jshort array2[arrayLength2];
290
291 //jshort * array1=(jshort *)malloc(4*arrayLength2);
292 //jshort * array2=(jshort *)malloc(4*arrayLength2);
293
294 jshort * prev=array1;
295 jshort * next=array2;
296 jshort maxScore=-32000;
297 jshort maxQpos=-1;
298 jshort maxRpos=-1;
299 jint itersShort=0;
300
301 for(jshort i=0; i<arrayLength2; i++) {
302 prev[i]=pointsIns*i;
303 next[i]=0;
304 }
305
306 for(jshort rpos=0; rpos<rlen; rpos++){
307 if(rlen-rpos<arrayLength){prev[0]=next[0]+pointsDel;}
308
309 const jshort r=ref[rpos];
310 jshort score=0;
311
312 // //Inner loop
313 // for(jshort qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){
314 // const jshort q=query[qpos];
315 // const jboolean match=(q==r);
316 // const jshort vScore=prev[apos]+pointsIns;
317 // const jshort hScore=next[apos-1]+pointsDel;
318 // const jshort dScore=(match ? pointsMatch : pointsSub)+prev[apos-1];
319 //
320 // score=(dScore>=vScore ? dScore : vScore);
321 // score=(hScore>score ? hScore : score);
322 //
323 // next[apos]=score;
324 // }
325
326 //Inner DV loop
327 for(jshort qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){
328 const jshort q=query[qpos];
329 const jboolean match=(q==r);
330 const jshort vScore=prev[apos]+pointsIns;
331 const jshort dScore=(match ? pointsMatch : pointsSub)+prev[apos-1];
332
333 score=(dScore>=vScore ? dScore : vScore);
334
335 next[apos]=score;
336 }
337
338 //Inner I loop
339 for(jshort qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){
340 const jshort hScore=next[apos-1]+pointsDel;
341
342 score=next[apos];
343 score=(hScore>score ? hScore : score);
344
345 next[apos]=score;
346 }
347
348 itersShort+=arrayLength;
349
350 if(score>=maxScore){
351 maxScore=score;
352 maxQpos=arrayLength-1;
353 maxRpos=rpos;
354 }
355
356 jshort * temp=prev;
357 prev=next;
358 next=temp;
359 }
360 retArray[0]=maxScore;
361 retArray[1]=maxQpos;
362 retArray[2]=maxRpos;
363 retArray[3]=itersShort;
364
365 //free(array1);
366 //free(array2);
367 }
368
369
370 void alignForwardShort16JNI_352(jshort * query, jshort * ref, jint * retArray, const jshort rlen) {
371
372 const jshort arrayLength=352;
373 const jshort arrayLength2=352+1;
374
375 //Stack allocated; faster.
376 jshort array1[arrayLength2];
377 jshort array2[arrayLength2];
378
379 //jshort * array1=(jshort *)malloc(4*arrayLength2);
380 //jshort * array2=(jshort *)malloc(4*arrayLength2);
381
382 jshort * prev=array1;
383 jshort * next=array2;
384 jshort maxScore=-16000;
385 jshort maxQpos=-1;
386 jshort maxRpos=-1;
387 jint itersShort=0;
388
389 for(jshort i=0; i<arrayLength2; i++) {
390 prev[i]=pointsIns*i;
391 next[i]=0;
392 }
393
394 for(jshort rpos=0; rpos<rlen; rpos++){
395 if(rlen-rpos<arrayLength){prev[0]=next[0]+pointsDel;}
396
397 const jshort r=ref[rpos];
398 jshort score=0;
399
400 // //Inner loop
401 // for(jshort qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){
402 // const jshort q=query[qpos];
403 // const jboolean match=(q==r);
404 // const jshort vScore=prev[apos]+pointsIns;
405 // const jshort hScore=next[apos-1]+pointsDel;
406 // const jshort dScore=(match ? pointsMatch : pointsSub)+prev[apos-1];
407 //
408 // score=(dScore>=vScore ? dScore : vScore);
409 // score=(hScore>score ? hScore : score);
410 //
411 // next[apos]=score;
412 // }
413
414 //Inner DV loop
415 for(jshort qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){
416 const jshort q=query[qpos];
417 const jboolean match=(q==r);
418 const jshort vScore=prev[apos]+pointsIns;
419 const jshort dScore=(match ? pointsMatch : pointsSub)+prev[apos-1];
420
421 score=(dScore>=vScore ? dScore : vScore);
422
423 next[apos]=score;
424 }
425
426 //Inner I loop
427 for(jshort qpos=0, apos=1; qpos<arrayLength; qpos++, apos++){
428 const jshort hScore=next[apos-1]+pointsDel;
429
430 score=next[apos];
431 score=(hScore>score ? hScore : score);
432
433 next[apos]=score;
434 }
435
436 itersShort+=arrayLength;
437
438 if(score>=maxScore){
439 maxScore=score;
440 maxQpos=arrayLength-1;
441 maxRpos=rpos;
442 }
443
444 jshort * temp=prev;
445 prev=next;
446 next=temp;
447 }
448 retArray[0]=maxScore;
449 retArray[1]=maxQpos;
450 retArray[2]=maxRpos;
451 retArray[3]=itersShort;
452
453 //free(array1);
454 //free(array2);
455 }
456
457 JNIEXPORT void JNICALL Java_icecream_IceCreamAlignerJNI_alignForwardJNI
458 (JNIEnv *env, jobject obj, jintArray query, jintArray ref, jintArray rvector, jint qlen, jint rlen, jint minScore, jfloat minRatio){
459
460 // Copy arrays from Java
461 jint * jquery = (jint*)(*env)->GetPrimitiveArrayCritical(env, query, NULL);
462 jint * jref = (jint*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL);
463 jint * jrvector = (jint*)(*env)->GetPrimitiveArrayCritical(env, rvector, NULL);
464
465 alignForwardJNI(jquery, jref, jrvector, qlen, rlen, minScore, minRatio);
466
467 // Release Java arrays; 0 copies the array back to Java, JNI_ABORT does not copy the current array values to Java
468 (*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT);
469 (*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT);
470 (*env)->ReleasePrimitiveArrayCritical(env, rvector, jrvector, 0);
471 }
472
473 JNIEXPORT void JNICALL Java_icecream_IceCreamAlignerJNI_alignForward16JNI
474 (JNIEnv *env, jobject obj, jshortArray query, jshortArray ref, jintArray rvector, jshort qlen, jshort rlen, jshort minScore, jfloat minRatio){
475
476 // Copy arrays from Java
477 jshort * jquery = (jshort*)(*env)->GetPrimitiveArrayCritical(env, query, NULL);
478 jshort * jref = (jshort*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL);
479 jint * jrvector = (jint*)(*env)->GetPrimitiveArrayCritical(env, rvector, NULL);
480
481 alignForward16JNI(jquery, jref, jrvector, qlen, rlen, minScore, minRatio);
482
483 // Release Java arrays; 0 copies the array back to Java, JNI_ABORT does not copy the current array values to Java
484 (*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT);
485 (*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT);
486 (*env)->ReleasePrimitiveArrayCritical(env, rvector, jrvector, 0);
487 }
488
489 JNIEXPORT void JNICALL Java_icecream_IceCreamAlignerJNI_alignForwardShortJNI
490 (JNIEnv *env, jobject obj, jintArray query, jintArray ref, jintArray rvector, jint qlen, jint rlen){
491
492 // Copy arrays from Java
493 jint * jquery = (jint*)(*env)->GetPrimitiveArrayCritical(env, query, NULL);
494 jint * jref = (jint*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL);
495 jint * jrvector = (jint*)(*env)->GetPrimitiveArrayCritical(env, rvector, NULL);
496
497 alignForwardShortJNI(jquery, jref, jrvector, qlen, rlen);
498
499 // Release Java arrays; 0 copies the array back to Java, JNI_ABORT does not copy the current array values to Java
500 (*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT);
501 (*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT);
502 (*env)->ReleasePrimitiveArrayCritical(env, rvector, jrvector, 0);
503 }
504
505 JNIEXPORT void JNICALL Java_icecream_IceCreamAlignerJNI_alignForwardShort16JNI
506 (JNIEnv *env, jobject obj, jshortArray query, jshortArray ref, jintArray rvector, jshort qlen, jshort rlen){
507
508 // Copy arrays from Java
509 jshort * jquery = (jshort*)(*env)->GetPrimitiveArrayCritical(env, query, NULL);
510 jshort * jref = (jshort*)(*env)->GetPrimitiveArrayCritical(env, ref, NULL);
511 jint * jrvector = (jint*)(*env)->GetPrimitiveArrayCritical(env, rvector, NULL);
512
513 if(qlen==352){
514 alignForwardShort16JNI_352(jquery, jref, jrvector, rlen);
515 }else{
516 alignForwardShort16JNI(jquery, jref, jrvector, qlen, rlen);
517 }
518
519 // Release Java arrays; 0 copies the array back to Java, JNI_ABORT does not copy the current array values to Java
520 (*env)->ReleasePrimitiveArrayCritical(env, query, jquery, JNI_ABORT);
521 (*env)->ReleasePrimitiveArrayCritical(env, ref, jref, JNI_ABORT);
522 (*env)->ReleasePrimitiveArrayCritical(env, rvector, jrvector, 0);
523 }