-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMICA-PE-WriteThread.c
1731 lines (1400 loc) · 90.3 KB
/
MICA-PE-WriteThread.c
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// MICA-PE-WriteThread.c
//
// mica
//
// Copyright (C) 2013, HKU
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
///////////////////////////////////////////////////////////////////////////////////////////////
#include "MICA-PE-WriteThread.h"
PEWriteThread * PEWTCreate(unsigned int maxNumReadPerCore, int maxNumOfMICThreads,
int numWriteThreadBuffer, int numAlignmentThread, int parentControllerIdx) {
int i,j;
PEWriteThread * writeThread = (PEWriteThread*) MEMManMalloc(sizeof(PEWriteThread),MEMORY_TYPE_CPU);
unsigned int memsize_readNameBufferFrame = MAX_SEQ_NAME_LENGTH*maxNumReadPerCore*maxNumOfMICThreads;
unsigned int memsize_readBodyBufferFrame = SRA_MAX_READ_LENGTH*maxNumReadPerCore*maxNumOfMICThreads;
unsigned int memsize_outputBufferFrame = MIC_SRA_OUTPUT_SIZE_PER_THREAD*maxNumOfMICThreads;
unsigned int memsize_outputBufferMeta = MIC_SRA_OUTPUT_META_PER_THREAD*maxNumOfMICThreads;
unsigned int memsize_bufferPerRead = maxNumReadPerCore*maxNumOfMICThreads;
unsigned int memsize_dpOutputBufferFrame = MIC_DP_OUTPUT_SIZE_PER_THREAD*maxNumOfMICThreads;
PEWTArgs * pewtArguments = &(writeThread->pewtArguments);
pewtArguments->sraArgTemplate = NULL;
pewtArguments->peArgTemplate = NULL;
pewtArguments->charMap = NULL;
pewtArguments->maxNumReadPerCore = maxNumReadPerCore;
pewtArguments->maxNumOfMICThreads = maxNumOfMICThreads;
writeThread->consPairCount = 0;
writeThread->consPairRead = 0;
writeThread->cpuConsSaCount = 0;
writeThread->cpuConsReadCount = 0;
writeThread->consReadCount = 0;
for (i=0;i<numWriteThreadBuffer;i++) {
writeThread->readNameBufferFrame[i] = (char*) MEMManMalloc(sizeof(char)*memsize_readNameBufferFrame,MEMORY_TYPE_CPU);
writeThread->readBodyBufferFrame[i] = (unsigned char*) MEMManMalloc(sizeof(unsigned char)*memsize_readBodyBufferFrame,MEMORY_TYPE_CPU);
writeThread->readQualityBufferFrame[i] = (char*) MEMManMalloc(sizeof(char)*memsize_readBodyBufferFrame,MEMORY_TYPE_CPU);
writeThread->readLengthBufferFrame[i] = (uint16_t*) MEMManMalloc(sizeof(uint16_t)*memsize_bufferPerRead,MEMORY_TYPE_CPU);
writeThread->mateNameBufferFrame[i] = (char*) MEMManMalloc(sizeof(char)*memsize_readNameBufferFrame,MEMORY_TYPE_CPU);
writeThread->mateBodyBufferFrame[i] = (unsigned char*) MEMManMalloc(sizeof(unsigned char)*memsize_readBodyBufferFrame,MEMORY_TYPE_CPU);
writeThread->mateQualityBufferFrame[i] = (char*) MEMManMalloc(sizeof(char)*memsize_readBodyBufferFrame,MEMORY_TYPE_CPU);
writeThread->mateLengthBufferFrame[i] = (uint16_t*) MEMManMalloc(sizeof(uint16_t)*memsize_bufferPerRead,MEMORY_TYPE_CPU);
writeThread->outputType[i] = MICA_PE_WRITE_THREAD_PAYLOAD_OUTPUT_TYPE_UNKNOWN;
writeThread->occCount[i] = (uint16_t*) MEMManMalloc(sizeof(uint16_t)*memsize_bufferPerRead,MEMORY_TYPE_CPU);
writeThread->outputBufferStatus[i] = (uint8_t*) MEMManMalloc(sizeof(uint8_t)*memsize_bufferPerRead,MEMORY_TYPE_CPU);
writeThread->outputBufferFrame[i] = (unsigned int*) MEMManMalloc(sizeof(unsigned int)*memsize_outputBufferFrame,MEMORY_TYPE_CPU);
writeThread->outputBufferMeta[i] = (MICSRAOccMetadata*) MEMManMalloc(sizeof(MICSRAOccMetadata)*memsize_outputBufferMeta,MEMORY_TYPE_CPU);
writeThread->dpOutputBufferFrame[i] = (MICDPOccurrence*) MEMManMalloc(sizeof(MICDPOccurrence) * memsize_dpOutputBufferFrame,MEMORY_TYPE_CPU);
writeThread->mappingQualities[i] = (PEMappingQuality*) MEMManMalloc(sizeof(PEMappingQuality) * memsize_bufferPerRead,MEMORY_TYPE_CPU);
writeThread->cpuSraOccCollector[i] = SRAOCCCreate(SRAOCC_FLOOD_TYPE_EXPAND);
writeThread->cpuDpOccCollector[i] = DPOCCCreate(SRAOCC_FLOOD_TYPE_EXPAND);
}
writeThread->algnmtThreadArg = (PEAlgnmtThreadArg*) MEMManMalloc(sizeof(PEAlgnmtThreadArg) * numAlignmentThread,MEMORY_TYPE_CPU);
writeThread->alignmentThreadBody = (pthread_t*) MEMManMalloc(sizeof(pthread_t) * numAlignmentThread,MEMORY_TYPE_CPU);
writeThread->outputThreadBody = 0;
for (i=0;i<numAlignmentThread;i++) {
writeThread->alignmentThreadBody[i] = 0;
writeThread->algnmtThreadArg[i].writeThread = (void*) writeThread;
writeThread->algnmtThreadArg[i].threadIdx = i;
writeThread->algnmtThreadArg[i].algnmtThreadStats.readNumHandled = 0;
writeThread->algnmtThreadArg[i].algnmtThreadStats.readNumAligned = 0;
writeThread->algnmtThreadArg[i].algnmtThreadStats.processTime = 0;
}
writeThread->performanceStats.readNumHandled = 0;
for (j=0;j<MIC_PE_OUTPUT_STATUS_COUNT;j++) {
writeThread->performanceStats.readNumByHandlers[j] = 0;
}
writeThread->performanceStats.processTime = 0;
writeThread->numAlignmentThread = numAlignmentThread;
// Set up the bufferSize according to the input
writeThread->bufferSize = numWriteThreadBuffer;
// Initialise the queue for buffer cloning
writeThread->freeQueue = SQCreate(numWriteThreadBuffer);
SQInitialiseFullQueue(writeThread->freeQueue);
// Initialise the queue for alignment thread
writeThread->alignmentQueue = SQCreate(numWriteThreadBuffer);
SQInitialiseEmptyQueue(writeThread->alignmentQueue);
// Initialise the queue for output thread
writeThread->outputQueue = SQCreate(numWriteThreadBuffer);
SQInitialiseEmptyQueue(writeThread->outputQueue);
writeThread->parentControllerIdx = parentControllerIdx;
writeThread->threadHealth = MICA_PE_WRITE_THREAD_HEALTH_OK;
// Initialise the pthread conditional variables
pthread_mutex_init(&(writeThread->mutexThreadHealth), NULL);
pthread_mutex_init(&(writeThread->mutexFreeQueue), NULL);
pthread_cond_init(&(writeThread->condFreeQueue), NULL);
pthread_mutex_init(&(writeThread->mutexAlignmentQueue), NULL);
pthread_cond_init(&(writeThread->condAlignmentQueue), NULL);
pthread_mutex_init(&(writeThread->mutexOutputQueue), NULL);
pthread_cond_init(&(writeThread->condOutputQueue), NULL);
strcpy(writeThread->healthString,"INIT");
return writeThread;
}
void PEWTRegisterArguments(PEWriteThread * writeThread,
SRAArguments * sraArgTemplate, PEArguments * peArgTemplate,
int InputAlignmentModel,
unsigned char * charMap) {
PEWTArgs * pewtArguments = &(writeThread->pewtArguments);
if ( pewtArguments->sraArgTemplate != NULL || pewtArguments->peArgTemplate != NULL ) {
printf("[SAFE-GUARD] WriteThread can only register arguments once.\n");
} else {
pewtArguments->sraArgTemplate = sraArgTemplate;
pewtArguments->peArgTemplate = peArgTemplate;
pewtArguments->charMap = charMap;
pewtArguments->InputAlignmentModel = InputAlignmentModel;
}
}
void PEWTCloneBuffer(PEWriteThread * writeThread,
unsigned int numThreads, unsigned int batchSize, unsigned int firstReadIdx, unsigned int queryInBatch,
char * readNameBufferFrame, unsigned char * readBodyBufferFrame, uint16_t * readLengthBufferFrame,
char * mateNameBufferFrame, unsigned char * mateBodyBufferFrame, uint16_t * mateLengthBufferFrame,
char * readQualityBufferFrame, char * mateQualityBufferFrame,
PEInputParam * peInputParam, PEReadOutput * peBatchOutput,
uint8_t payloadOutputType,
uint16_t * occCount, uint8_t * outputBufferStatus, unsigned int * outputBufferFrame,
MICSRAOccMetadata * outputBufferMeta, MICDPOccurrence * dpOutputBufferFrame, PEMappingQuality * mappingQualities,
int algnFuncReasonFlag) {
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - PEWTCloneBuffer - Invoked.\n");
#endif
pthread_mutex_t * mutexFreeQueue = &(writeThread->mutexFreeQueue);
pthread_cond_t * condFreeQueue = &(writeThread->condFreeQueue);
pthread_mutex_t * mutexAlignmentQueue = &(writeThread->mutexAlignmentQueue);
pthread_cond_t * condAlignmentQueue = &(writeThread->condAlignmentQueue);
int bufferIdx = 0;
PEWTArgs * pewtArguments = &(writeThread->pewtArguments);
if ( numThreads * batchSize > pewtArguments->maxNumOfMICThreads * pewtArguments->maxNumReadPerCore ) {
printf("[SAFE-GUARD] Supplied clone subject is larger than the write-thread allocated buffer size.\n");
printf("%u x %u is larger than %u x %u\n",numThreads,batchSize,pewtArguments->maxNumOfMICThreads,pewtArguments->maxNumReadPerCore);
}
unsigned int memsize_outputBufferFrame = MIC_SRA_OUTPUT_SIZE_PER_THREAD*numThreads;
unsigned int memsize_outputBufferMeta = MIC_SRA_OUTPUT_META_PER_THREAD*numThreads;
unsigned int memsize_bufferPerRead = batchSize*numThreads;
unsigned int memsize_dpOutputBufferFrame = MIC_DP_OUTPUT_SIZE_PER_THREAD*numThreads;
// Block until there is an empty buffer
pthread_mutex_lock(mutexFreeQueue);
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - PEWTCloneBuffer - Mutex lock (mutexFreeQueue) obtained. Waiting for condition (condFreeQueue)\n");
#endif
while (SQIsEmpty(writeThread->freeQueue)) {
printf("[WARNING] Blocking has occurred -- Please revisit pipelining parameters to achieve better performance.\n");
pthread_cond_wait(condFreeQueue,mutexFreeQueue);
}
if (!SQDequeue(writeThread->freeQueue,&bufferIdx)) {
printf("[SAFE-GUARD] Unexpected mutex error.\n");
printf("The freeQueue is supposed to be non-empty.\n");
}
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - PEWTCloneBuffer - Using buffer %d.\n",bufferIdx);
printf ( "PEWT - PEWTCloneBuffer - Unlock Mutex(mutexFreeQueue)\n");
#endif
pthread_mutex_unlock(mutexFreeQueue);
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - PEWTCloneBuffer - Free Queue Status\n" );
SQPrintQueue(writeThread->freeQueue);
printf ( "PEWT - PEWTCloneBuffer - Copying Payload - Configuration\n");
#endif
// Clone the payload
////////////////////////////////////////
// Thread Payload - Configuration
////////////////////////////////////////
writeThread->queryInBatch[bufferIdx] = queryInBatch;
writeThread->batchSize[bufferIdx] = batchSize;
writeThread->numThreads[bufferIdx] = numThreads;
writeThread->firstReadIdx[bufferIdx] = firstReadIdx;
writeThread->peInputParam[bufferIdx] = peInputParam;
writeThread->peBatchOutput[bufferIdx] = peBatchOutput;
writeThread->algnFuncReasonFlag[bufferIdx] = algnFuncReasonFlag;
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - PEWTCloneBuffer - Copying Payload - Input\n");
#endif
////////////////////////////////////////
// Thread Payload - Input
////////////////////////////////////////
uint32_t readNameCopySize = MAX_SEQ_NAME_LENGTH * queryInBatch;
uint32_t readBodyCopySize = SRA_MAX_READ_LENGTH * queryInBatch;
uint32_t readLengthCopySize = sizeof(uint16_t) * queryInBatch;
memcpy(writeThread->readNameBufferFrame[bufferIdx],readNameBufferFrame, readNameCopySize);
memcpy(writeThread->readBodyBufferFrame[bufferIdx],readBodyBufferFrame, readBodyCopySize);
memcpy(writeThread->readQualityBufferFrame[bufferIdx], readQualityBufferFrame, readBodyCopySize);
memcpy(writeThread->readLengthBufferFrame[bufferIdx], readLengthBufferFrame, readLengthCopySize);
memset(writeThread->readLengthBufferFrame[bufferIdx] + queryInBatch,
0, sizeof(uint16_t) * memsize_bufferPerRead - readLengthCopySize);
memcpy(writeThread->mateNameBufferFrame[bufferIdx],mateNameBufferFrame, readNameCopySize);
memcpy(writeThread->mateBodyBufferFrame[bufferIdx],mateBodyBufferFrame, readBodyCopySize);
memcpy(writeThread->mateQualityBufferFrame[bufferIdx], mateQualityBufferFrame, readBodyCopySize);
memcpy(writeThread->mateLengthBufferFrame[bufferIdx], mateLengthBufferFrame, readLengthCopySize);
memset(writeThread->mateLengthBufferFrame[bufferIdx] + queryInBatch,
0, sizeof(uint16_t) * memsize_bufferPerRead - readLengthCopySize);
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - PEWTCloneBuffer - Copying Payload - Output\n");
#endif
////////////////////////////////////////
// Thread Payload - Output
////////////////////////////////////////
writeThread->outputType[bufferIdx]=payloadOutputType;
if ( payloadOutputType == MICA_PE_WRITE_THREAD_PAYLOAD_OUTPUT_TYPE_MIC_MIX) {
memcpy(writeThread->occCount[bufferIdx],occCount,sizeof(uint16_t)*memsize_bufferPerRead);
memcpy(writeThread->outputBufferStatus[bufferIdx],outputBufferStatus,sizeof(uint8_t)*memsize_bufferPerRead);
memcpy(writeThread->outputBufferFrame[bufferIdx],outputBufferFrame,sizeof(unsigned int)*memsize_outputBufferFrame);
memcpy(writeThread->outputBufferMeta[bufferIdx],outputBufferMeta,sizeof(MICSRAOccMetadata)*memsize_outputBufferMeta);
memcpy(writeThread->dpOutputBufferFrame[bufferIdx],dpOutputBufferFrame,sizeof(MICDPOccurrence) * memsize_dpOutputBufferFrame);
memcpy(writeThread->mappingQualities[bufferIdx],mappingQualities,sizeof(PEMappingQuality) * memsize_bufferPerRead);
}
// Block until there is an empty buffer
pthread_mutex_lock(mutexAlignmentQueue);
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - PEWTCloneBuffer - Mutex lock (mutexAlignmentQueue) obtained. Waiting for condition (condAlignmentQueue)\n");
#endif
// EW: I am thinking the condition wait is a bit pointless as
// the free queue was not empty meaning the alignment must have vacancy.
//while (SQIsFull(writeThread->alignmentQueue)) {
// pthread_cond_wait(condAlignmentQueue,mutexAlignmentQueue);
//}
if (!SQEnqueue(writeThread->alignmentQueue,bufferIdx)) {
printf("[SAFE-GUARD] Unexpected mutex error.\n");
printf("The alignmentQueue is supposed to have vacancy.\n");
}
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - PEWTCloneBuffer - Signal Listener of condition(condAlignmentQueue)\n");
#endif
pthread_cond_signal(condAlignmentQueue);
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - PEWTCloneBuffer - Unlock Mutex(mutexAlignmentQueue)\n");
#endif
pthread_mutex_unlock(mutexAlignmentQueue);
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - PEWTCloneBuffer - AlignmentQueue Status\n" );
SQPrintQueue(writeThread->alignmentQueue);
#endif
}
static PEArguments * _PEWTCreatePEArgument(PEWriteThread * writeThread) {
PEWTArgs * pewtArguments = &(writeThread->pewtArguments);
// Make a mate of the SRA arguments
SRAArguments * pewtSraReadArgs = SRAARGMakeMate(pewtArguments->sraArgTemplate);
SRAArguments * pewtSraReadArgs_neg = SRAARGMakeSlave(pewtSraReadArgs);
SRAArguments * pewtSraMateArgs = SRAARGMakeMate(pewtSraReadArgs);
SRAArguments * pewtSraMateArgs_neg = SRAARGMakeSlave(pewtSraMateArgs);
SRAAlgnmtOutputInitNone(pewtSraReadArgs->AlgnmtOutput);
SRAAlgnmtOutputInitNone(pewtSraMateArgs->AlgnmtOutput);
// Make a mate of the PE arguments
PEArguments * pewtPeArg = PEARGMakeMate(pewtArguments->peArgTemplate);
pewtPeArg->PEAlgnmtInput = PEInputMakeClone(pewtPeArg->PEAlgnmtInput);
//Free-ing the output handler as it will be replaced when a write buffer is being processed
// by the output handler embedded the read buffer, and passed along.
SRAAlgnmtOutputFree(pewtPeArg->PEAlgnmtOutput_OccHandle);
pewtPeArg->PEAlgnmtOutput_OccHandle = NULL;
// Popluate the PE Argument
PEARGPopulateSRAARG(pewtPeArg,
pewtSraReadArgs,pewtSraReadArgs_neg,
pewtSraMateArgs,pewtSraMateArgs_neg);
return pewtPeArg;
}
static void _PEWTFreePEArgument(PEArguments * peArgs) {
SRAAlgnmtOutputFreeNone(peArgs->sraArgsPos->AlgnmtOutput);
SRAAlgnmtOutputFreeNone(peArgs->sraArgsPos_mate->AlgnmtOutput);
SRAARGSlaveFree(peArgs->sraArgsNeg);
SRAARGSlaveFree(peArgs->sraArgsNeg_mate);
SRAARGMateFree(peArgs->sraArgsPos);
SRAARGMateFree(peArgs->sraArgsPos_mate);
PEInputCloneFree(peArgs->PEAlgnmtInput);
PEARGMateFree(peArgs);
}
void PEWTFree(PEWriteThread * writeThread) {
int i;
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - PEWTFree - Free-ing the Write Thread\n");
#endif
PEWTSignal(writeThread, MICA_PE_WRITE_THREAD_HEALTH_DEPLETED);
PEWTThreadJoin(writeThread);
for (i=0;i<writeThread->bufferSize;i++) {
free(writeThread->readNameBufferFrame[i]);
free(writeThread->readBodyBufferFrame[i]);
free(writeThread->readLengthBufferFrame[i]);
free(writeThread->mateNameBufferFrame[i]);
free(writeThread->mateBodyBufferFrame[i]);
free(writeThread->mateLengthBufferFrame[i]);
free(writeThread->readQualityBufferFrame[i]);
free(writeThread->mateQualityBufferFrame[i]);
free(writeThread->occCount[i]);
free(writeThread->outputBufferStatus[i]);
free(writeThread->outputBufferFrame[i]);
free(writeThread->outputBufferMeta[i]);
free(writeThread->dpOutputBufferFrame[i]);
free(writeThread->mappingQualities[i]);
SRAOCCFree(writeThread->cpuSraOccCollector[i]);
DPOCCFree(writeThread->cpuDpOccCollector[i]);
}
free(writeThread->alignmentThreadBody);
free(writeThread->algnmtThreadArg);
SQFree(writeThread->freeQueue);
SQFree(writeThread->alignmentQueue);
SQFree(writeThread->outputQueue);
free(writeThread);
}
/////////////////////////////////////////////////////////////////////////
//
// ALIGNMENT THREAD BODY
//
/////////////////////////////////////////////////////////////////////////
void * _PEWTAlignmentRollBody(void * _threadArg) {
PEAlgnmtThreadArg * threadArg = (PEAlgnmtThreadArg*) _threadArg;
PEWriteThread * writeThread = (PEWriteThread*) threadArg->writeThread;
int threadIdx = threadArg->threadIdx;
pthread_mutex_t * mutexFreeQueue = &(writeThread->mutexFreeQueue);
pthread_cond_t * condFreeQueue = &(writeThread->condFreeQueue);
pthread_mutex_t * mutexAlignmentQueue = &(writeThread->mutexAlignmentQueue);
pthread_cond_t * condAlignmentQueue = &(writeThread->condAlignmentQueue);
pthread_mutex_t * mutexOutputQueue = &(writeThread->mutexOutputQueue);
pthread_cond_t * condOutputQueue = &(writeThread->condOutputQueue);
PEWTArgs * pewtArguments = &(writeThread->pewtArguments);
PEArguments * peArg = _PEWTCreatePEArgument(writeThread);
// Set up the temporary output collector for the intermediate results
peArg->PEAlgnmtOutput_OccHandle = SRAAlgnmtOutputConstruct();
SRAAlgnmtOutputInitSAMStore(peArg->PEAlgnmtOutput_OccHandle);
unsigned char * charMap = pewtArguments->charMap;
PEDPSetting * pedpSetting = peArg->pedpSetting;
// Loosen the matching criteria
SRASetting * seedSetting = SRASettingMakeClone(peArg->sraArgsPos->AlgnmtSetting);
seedSetting->OutputType = SRA_REPORT_ALL_BEST;
if (pedpSetting->SGASeedLooseCriteria>=0) {
seedSetting->MaxError = pedpSetting->SGASeedLooseCriteria;
} else {
seedSetting->MaxError += pedpSetting->SGASeedLooseCriteria;
}
// oneMoreMismatchSetting increase mismatch by 1
SRASetting * oneMoreMismatchSetting = SRASettingMakeClone(peArg->sraArgsPos->AlgnmtSetting);
oneMoreMismatchSetting->OutputType == SRA_REPORT_ALL;
oneMoreMismatchSetting->MaxError += 1;
// Construct SRA Model from SRA Settings
SRAModelSet * sraModelSet = SRAModelSetConstruct(
peArg->sraArgsPos->AlgnmtSetting,
peArg->sraArgsPos->AlgnmtIndex,
SRA_MODEL_16G,
SRA_MIN_READ_LENGTH,
SRA_MAX_READ_LENGTH);
SRAModelSet * sraModelSet_seed = SRAModelSetConstruct(
seedSetting,
peArg->sraArgsPos->AlgnmtIndex,
SRA_MODEL_16G,
SRA_MIN_READ_LENGTH,
SRA_MAX_READ_LENGTH);
SRAModelSet * sraModelSet_extend = SRAModelSetConstruct(
oneMoreMismatchSetting,
peArg->sraArgsPos->AlgnmtIndex,
SRA_MODEL_16G,
SRA_MIN_READ_LENGTH,
SRA_MAX_READ_LENGTH);
unsigned long long i, j, k;
int invalidStatus = FALSE;
//int dummyQuality[SRA_MAX_READ_LENGTH];
//memset(dummyQuality,0,sizeof(int)*SRA_MAX_READ_LENGTH);
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTAlignmentRollBody - Invoked and Entering Main-Loop\n");
#endif
// Timestamp'ed -----------
double startTime = setStartTime();
double lastEventTime = 0;
double totalElipsedTime = 0;
// ------------------------
while (1) {
int bufferIdx;
// Block until there is an non-empty buffer
pthread_mutex_lock(mutexAlignmentQueue);
// Loop wait as long as
// - alignment queue is empty
// - AND free queue is not full OR thread is healthy
while (
SQIsEmpty(writeThread->alignmentQueue) &&
(
!SQIsFull(writeThread->freeQueue) ||
writeThread->threadHealth==MICA_PE_WRITE_THREAD_HEALTH_OK
)
) {
pthread_cond_wait(condAlignmentQueue,mutexAlignmentQueue);
}
// Break in case
// - free queue is full
// - AND alignment queue is empty
// - AND thread is not healthy
if (
SQIsFull(writeThread->freeQueue) &&
SQIsEmpty(writeThread->alignmentQueue) &&
writeThread->threadHealth!=MICA_PE_WRITE_THREAD_HEALTH_OK
) {
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTAlignmentRollBody - Thread is not healthy and Buffer is empty. Exiting.\n");
#endif
pthread_cond_signal(condAlignmentQueue);
pthread_mutex_unlock(mutexAlignmentQueue);
break;
}
if (!SQDequeue(writeThread->alignmentQueue,&bufferIdx)) {
printf("[SAFE-GUARD] Unexpected mutex error.\n");
printf("The alignmentQueue is supposed to be non-empty.\n");
}
pthread_mutex_unlock(mutexAlignmentQueue);
// Timestamp'ed -----------
double timestamp = getElapsedTime(startTime);
lastEventTime = timestamp;
// ------------------------
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTAlignmentRollBody - Received Signal on condition(condAlignmentQueue)\n");
#endif
//Process Payload
//
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTAlignmentRollBody - Processing Payload from buffer %d.\n", bufferIdx);
#endif
unsigned int queryInBatch = writeThread->queryInBatch[bufferIdx];
unsigned int batchSize = writeThread->batchSize[bufferIdx];
unsigned int numThreads = writeThread->numThreads[bufferIdx];
unsigned int firstReadIdx = writeThread->firstReadIdx[bufferIdx];
int algnFuncReasonFlag = writeThread->algnFuncReasonFlag[bufferIdx];
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTAlignmentRollBody - #Thread %u / BatchSize %u / #Query %u.\n",numThreads,batchSize,queryInBatch);
#endif
char * readNameBufferFrame = writeThread->readNameBufferFrame[bufferIdx];
unsigned char * readBodyBufferFrame = writeThread->readBodyBufferFrame[bufferIdx];
char * readQualityBufferFrame= writeThread->readQualityBufferFrame[bufferIdx];
uint16_t * readLengthBufferFrame = writeThread->readLengthBufferFrame[bufferIdx];
char * mateNameBufferFrame = writeThread->mateNameBufferFrame[bufferIdx];
unsigned char * mateBodyBufferFrame = writeThread->mateBodyBufferFrame[bufferIdx];
char * mateQualityBufferFrame= writeThread->mateQualityBufferFrame[bufferIdx];
uint16_t * mateLengthBufferFrame = writeThread->mateLengthBufferFrame[bufferIdx];
PEInputParam * peInputParam = writeThread->peInputParam[bufferIdx];
PEReadOutput * peBatchOutput = writeThread->peBatchOutput[bufferIdx];
uint8_t outputType = writeThread->outputType[bufferIdx];
uint16_t * occCount = writeThread->occCount[bufferIdx];
uint8_t * outputBufferStatus = writeThread->outputBufferStatus[bufferIdx];
// Set up the temporary output collector for the intermediate results
SRAOCCCollector * cpuSraOccCollector = writeThread->cpuSraOccCollector[bufferIdx];
DPOCCCollector * cpuDpOccCollector = writeThread->cpuDpOccCollector[bufferIdx];
SRAArguments * sraReadArgs = peArg->sraArgsPos;
SRAArguments * sraReadArgs_neg = peArg->sraArgsNeg;
SRAArguments * sraMateArgs = peArg->sraArgsPos_mate;
SRAArguments * sraMateArgs_neg = peArg->sraArgsNeg_mate;
unsigned char readNegPattern[SRA_MAX_READ_LENGTH], mateNegPattern[SRA_MAX_READ_LENGTH];
unsigned char readNegQuality[SRA_MAX_READ_LENGTH], mateNegQuality[SRA_MAX_READ_LENGTH];
// Populate input parameter and output handler for batch
peArg->PEAlgnmtInput->insertUbound = peInputParam->InputPEInsertionUpperBound;
peArg->PEAlgnmtInput->insertLbound = peInputParam->InputPEInsertionLowerBound;
for (i=0;i<numThreads;i++) {
unsigned int offset = i * batchSize;
// Build up the SRA Model in case there are new read lengths
for (j=0;j<batchSize;j++) {
unsigned int readIdxInBatch = offset + j;
// buildSRAModels with each read and mate into
// sraModel, seedModel and oneMoreMismatchModel
int readLength = readLengthBufferFrame[readIdxInBatch];
int mateLength = mateLengthBufferFrame[readIdxInBatch];
// If seedLength > 1, treat input as constant seedLength or seedOverlap
// If seedLength <= 1, treat input as a ratio to readLen
// + 0.5 to do rounding, set Length to zero when readLength or mateLength = 0
int readSeedLength = ( pedpSetting->SGASeedLength > 1.0 && readLength != 0 ? pedpSetting->SGASeedLength
: (double) readLength * pedpSetting->SGASeedLength ) + 0.5;
int mateSeedLength = ( pedpSetting->SGASeedLength > 1.0 && mateLength != 0 ? pedpSetting->SGASeedLength
: (double) mateLength * pedpSetting->SGASeedLength ) + 0.5;
int readSeedLength2 = ( pedpSetting->SGASeedLength_1 > 1.0 && readLength != 0 ? pedpSetting->SGASeedLength_1
: (double) readLength * pedpSetting->SGASeedLength_1 ) + 0.5;
int mateSeedLength2 = ( pedpSetting->SGASeedLength_1 > 1.0 && mateLength != 0 ? pedpSetting->SGASeedLength_1
:(double) mateLength * pedpSetting->SGASeedLength_1 ) + 0.5;
SRAModelConstruct(sraModelSet, readLength);
SRAModelConstruct(sraModelSet, mateLength);
SRAModelConstruct(sraModelSet_seed, readSeedLength);
SRAModelConstruct(sraModelSet_seed, mateSeedLength);
SRAModelConstruct(sraModelSet_seed, readSeedLength2);
SRAModelConstruct(sraModelSet_seed, mateSeedLength2);
SRAModelConstruct(sraModelSet_extend, readLength);
SRAModelConstruct(sraModelSet_extend, mateLength);
}
for (j=0;j<batchSize;j++) {
unsigned int readIdxInBatch = offset + j;
// Buffer could be partially filled.
// Early termination w.r.t to the size of the particular buffer
if (readIdxInBatch >= queryInBatch) {
break;
}
unsigned int readIdx = firstReadIdx + readIdxInBatch;
#ifdef DEBUG_MICA_PEWT_READ_HANDLING
printf("[INFO] Handling Read %u\n",readIdx);
#endif
unsigned char * readPatternPtr = &readBodyBufferFrame[readIdxInBatch*SRA_MAX_READ_LENGTH];
char * readPatternName = &readNameBufferFrame[readIdxInBatch*MAX_SEQ_NAME_LENGTH];
char * readQualityPtr = &readQualityBufferFrame[readIdxInBatch*SRA_MAX_READ_LENGTH];
uint16_t readPatternLen = readLengthBufferFrame[readIdxInBatch];
SRAFlipPattern(charMap,readPatternPtr,readPatternLen,readNegPattern);
SRAFlipQuality(charMap,readQualityPtr,readPatternLen,readNegQuality);
unsigned char * matePatternPtr = &mateBodyBufferFrame[readIdxInBatch*SRA_MAX_READ_LENGTH];
char * matePatternName = &mateNameBufferFrame[readIdxInBatch*MAX_SEQ_NAME_LENGTH];
char * mateQualityPtr = &mateQualityBufferFrame[readIdxInBatch*SRA_MAX_READ_LENGTH]; ;
uint16_t matePatternLen = mateLengthBufferFrame[readIdxInBatch];
SRAFlipPattern(charMap,matePatternPtr,matePatternLen,mateNegPattern);
SRAFlipQuality(charMap,mateQualityPtr,readPatternLen,mateNegQuality);
// Populate the SRA information
SRAQueryInfoPopulate(sraReadArgs->QueryInfo,readIdx,readPatternName,readPatternLen,readPatternPtr,readPatternPtr,QUERY_POS_STRAND,readQualityPtr);
SRAQueryInfoPopulate(sraReadArgs_neg->QueryInfo,readIdx,readPatternName,readPatternLen,readNegPattern,readPatternPtr,QUERY_NEG_STRAND,readNegQuality);
SRAQueryInfoPopulate(sraMateArgs->QueryInfo,readIdx,matePatternName,matePatternLen,matePatternPtr,matePatternPtr,QUERY_POS_STRAND,mateQualityPtr);
SRAQueryInfoPopulate(sraMateArgs_neg->QueryInfo,readIdx,matePatternName,matePatternLen,mateNegPattern,matePatternPtr,QUERY_NEG_STRAND,mateNegQuality);
// Initialise the result counter and MAPQ buffers
SRAResultCountInitialise(sraReadArgs->AlgnmtStats);
SRAResultCountInitialise(sraMateArgs->AlgnmtStats);
MAPQCalculatorInitialise(sraReadArgs->MapqCalc);
MAPQCalculatorInitialise(sraMateArgs->MapqCalc);
MAPQCalculatorInitialise(peArg->MapqCalc);
if ( outputType == MICA_PE_WRITE_THREAD_PAYLOAD_OUTPUT_TYPE_UNKNOWN) {
#ifdef DEBUG_MICA_PEWT_READ_HANDLING
printf("[DEBUG] Payload Error - Unknown - %s / %s!\n",readPatternName,matePatternName);
#endif
printf("[INFO] Payload Error - Unknown - %s / %s!\n",readPatternName,matePatternName);
// In this case the read has never been initialised.
// therefore the read always requires CPU processing.
unsigned int runPairOcc = PEProcessReadDoubleStrand(peArg,
sraModelSet,sraModelSet_seed,sraModelSet_extend);
// As the output buffer are invalid
occCount[readIdxInBatch] = 0;
threadArg->algnmtThreadStats.readNumAligned++;
} else if ( outputType == MICA_PE_WRITE_THREAD_PAYLOAD_OUTPUT_TYPE_SKIP_MIC) {
#ifdef DEBUG_MICA_PEWT_READ_HANDLING
printf("[DEBUG] Payload unaligned by MIC - MIC unaligned - %s / %s!\n",readPatternName,matePatternName);
#endif
// In this case the read has never been passed into MIC
// therefore the read always requires CPU processing.
unsigned int runPairOcc = PEProcessReadDoubleStrand(peArg,
sraModelSet,sraModelSet_seed,sraModelSet_extend);
// As the output buffer are invalid
occCount[readIdxInBatch] = 0;
threadArg->algnmtThreadStats.readNumAligned++;
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_CLOSED) {
#ifdef DEBUG_MICA_PEWT_READ_HANDLING
printf("[DEBUG] Unaligned closed read - MIC unaligned - %s / %s!\n",readPatternName,matePatternName);
#endif
// In this case the output buffer is flooded as the number of output
// generated is larger than the MIC_SRA_OUTPUT_MAX_ALIGNMENT; or the number of header
// generated is larger than MIC_SRA_OUTPUT_MAX_META. MIC was not
// able to handle all the alignment result hence declared the read
// unaligned on MIC.
//cpuConsSaCount += ProcessReadDoubleStrand(sraReadArgs,sraReadArgs_neg,sraModelSet,sraModelSet);
unsigned int runPairOcc = PEProcessReadDoubleStrand(peArg,
sraModelSet,sraModelSet_seed,sraModelSet_extend);
threadArg->algnmtThreadStats.readNumAligned++;
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_UNHANDLE) {
#ifdef DEBUG_MICA_PEWT_READ_HANDLING
printf("[DEBUG] Unhandled read - MIC unaligned - %s / %s!\n",readPatternName,matePatternName);
#endif
// In this case the entire core shared output buffer is flooded as the number of output
// generated is larger than the MIC_SRA_OUTPUT_SIZE_PER_THREAD. MIC was not
// able to handle all the reads in the input read block hence declared all consecutive read
// unaligned on MIC.
//cpuConsSaCount += ProcessReadDoubleStrand(sraReadArgs,sraReadArgs_neg,sraModelSet,sraModelSet);
unsigned int runPairOcc = PEProcessReadDoubleStrand(peArg,
sraModelSet,sraModelSet_seed,sraModelSet_extend);
threadArg->algnmtThreadStats.readNumAligned++;
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_SKIPPED) {
} else if ( outputBufferStatus[readIdxInBatch] == MIC_OUTPUT_STATUS_OPEN) {
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_PAIR) {
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_BAD_PAIR) {
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_READ_NO_ALIGNMENT) {
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_MATE_NO_ALIGNMENT) {
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_BOTH_NO_ALIGNMENT) {
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_DP_BASE_MATE) {
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_DP_BASE_READ) {
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_DP_MIX_BASE) {
} else if ( outputBufferStatus[readIdxInBatch] == MIC_PE_OUTPUT_STATUS_DP_SEED) {
} else {
}
//Copy result into intermediate buffer from read buffer
SRAOCCCollectorPointer * occptr = SRAOCCPTCreate(peArg->PEAlgnmtOutput_OccHandle->occCollector);
SRAOccurrence * sraOcc = SRAOCCPTRead(occptr);
while ( sraOcc != NULL ) {
SRAOCCAddOccurrenceToBuckets(cpuSraOccCollector,sraOcc);
SRAOCCPTNext(occptr);
sraOcc = SRAOCCPTRead(occptr);
}
SRAOCCPTFree(occptr);
DPOCCCollectorPointer * dpoccptr = DPOCCPTCreate(peArg->dpArguments->dpOccCollector);
DPOccurrence * dpOcc = DPOCCPTRead(dpoccptr);
while ( dpOcc != NULL ) {
DPOCCAddOccurrenceToBuckets(cpuDpOccCollector,dpOcc);
DPOCCPTNext(dpoccptr);
dpOcc = DPOCCPTRead(dpoccptr);
}
DPOCCPTFree(dpoccptr);
// Clear result from read buffer
SRAOCCInitialise(peArg->PEAlgnmtOutput_OccHandle->occCollector);
DPOCCInitialise(peArg->dpArguments->dpOccCollector);
threadArg->algnmtThreadStats.readNumHandled++;
}
}
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTAlignmentRollBody - Finished Processing Payload.\n");
printf ( "PEWT - _PEWTAlignmentRollBody - Handler Statistics. Total Number of %llu/%llu Reads Aligned.\n",
threadArg->algnmtThreadStats.readNumAligned,threadArg->algnmtThreadStats.readNumHandled);
#endif
// Timestamp'ed -----------
timestamp = getElapsedTime(startTime);
totalElipsedTime += timestamp - lastEventTime;
threadArg->algnmtThreadStats.processTime += timestamp - lastEventTime;
lastEventTime = timestamp;
// ------------------------
pthread_mutex_lock(mutexOutputQueue);
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTAlignmentRollBody - Mutex Lock obtained(mutexOutputQueue). Updating Buffer Pointer.\n");
#endif
if (!SQEnqueue(writeThread->outputQueue,bufferIdx)) {
printf("[SAFE-GUARD] Unexpected mutex error.\n");
printf("The outputQueue is supposed to be non-full.\n");
}
pthread_cond_signal(condOutputQueue);
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTAlignmentRollBody - Unlock mutex(mutexOutputQueue).\n");
printf ( "PEWT - _PEWTAlignmentRollBody - Output Queue Status\n" );
SQPrintQueue(writeThread->outputQueue);
#endif
pthread_mutex_unlock(mutexOutputQueue);
fflush(0);
}
SRASettingFree(seedSetting);
SRASettingFree(oneMoreMismatchSetting);
SRAModelSetFree(sraModelSet);
SRAModelSetFree(sraModelSet_seed);
SRAModelSetFree(sraModelSet_extend);
SRAAlgnmtOutputFreeSAMStore(peArg->PEAlgnmtOutput_OccHandle);
SRAAlgnmtOutputFree(peArg->PEAlgnmtOutput_OccHandle);
peArg->PEAlgnmtOutput_OccHandle = NULL;
_PEWTFreePEArgument(peArg);
writeThread->alignmentThreadBody[threadIdx] = 0;
pthread_exit(0);
return 0;
}
/////////////////////////////////////////////////////////////////////////
//
// OUTPUT THREAD BODY
//
/////////////////////////////////////////////////////////////////////////
unsigned int _PEWTPumpOutputFromBuffer(PEArguments * peArg,
SRAOCCCollectorPointer * occptr, DPOCCCollectorPointer * dpoccptr) {
unsigned int runPairOcc = 0;
SRAOCCCollector * resultOccCollector = peArg->PEAlgnmtOutput_OccHandle->occCollector;
DPOCCCollector * resultDpOccCollector = peArg->dpArguments->dpOccCollector;
SRAOccurrence * sraOcc = SRAOCCPTRead(occptr);
DPOccurrence * dpOcc = DPOCCPTRead(dpoccptr);
if ( sraOcc != NULL && sraOcc->type == SRAOCC_TYPE_DELIMITOR_READ) {
SRAOCCPTNext(occptr);
sraOcc = SRAOCCPTRead(occptr);
}
while ( sraOcc != NULL && sraOcc->type != SRAOCC_TYPE_DELIMITOR_READ ) {
SRAOCCAddOccurrenceToBuckets(resultOccCollector,sraOcc);
switch (sraOcc->type) {
case SRAOCC_TYPE_PE_PAIR:
runPairOcc++;
break;
case SRAOCC_TYPE_PE_DP_BASE_READ:
case SRAOCC_TYPE_PE_DP_BASE_MATE:
runPairOcc+=2;
if ( dpOcc == NULL ) {
printf("[SAFE-GUARD] Insufficient number of DP occurrences for SRA output.\n");
} else {
DPOCCAddOccurrenceToBuckets(resultDpOccCollector,dpOcc);
DPOCCPTNext(dpoccptr);
dpOcc = DPOCCPTRead(dpoccptr);
}
break;
case SRAOCC_TYPE_PE_DP_SEED_OCC:
runPairOcc+=2;
if ( dpOcc == NULL ) {
printf("[SAFE-GUARD] Insufficient number of DP occurrences for SRA output.\n");
} else {
DPOCCAddOccurrenceToBuckets(resultDpOccCollector,dpOcc);
DPOCCPTNext(dpoccptr);
dpOcc = DPOCCPTRead(dpoccptr);
}
if ( dpOcc == NULL ) {
printf("[SAFE-GUARD] Insufficient number of DP occurrences for SRA output.\n");
} else {
DPOCCAddOccurrenceToBuckets(resultDpOccCollector,dpOcc);
DPOCCPTNext(dpoccptr);
dpOcc = DPOCCPTRead(dpoccptr);
}
break;
}
SRAOCCPTNext(occptr);
sraOcc = SRAOCCPTRead(occptr);
}
PEOCCFlushCache(peArg);
runPairOcc /= 2;
return runPairOcc;
}
void * _PEWTOutputRollBody(void * _writeThread) {
PEWriteThread * writeThread = (PEWriteThread*) _writeThread;
pthread_mutex_t * mutexFreeQueue = &(writeThread->mutexFreeQueue);
pthread_cond_t * condFreeQueue = &(writeThread->condFreeQueue);
pthread_mutex_t * mutexAlignmentQueue = &(writeThread->mutexAlignmentQueue);
pthread_cond_t * condAlignmentQueue = &(writeThread->condAlignmentQueue);
pthread_mutex_t * mutexOutputQueue = &(writeThread->mutexOutputQueue);
pthread_cond_t * condOutputQueue = &(writeThread->condOutputQueue);
PEWTArgs * pewtArguments = &(writeThread->pewtArguments);
unsigned char * charMap = pewtArguments->charMap;
PEArguments * peArg = _PEWTCreatePEArgument(writeThread);
unsigned long long i, j, k;
int invalidStatus = FALSE;
//int dummyQuality[SRA_MAX_READ_LENGTH];
//memset(dummyQuality,0,sizeof(int)*SRA_MAX_READ_LENGTH);
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTOutputRollBody - Invoked and Entering Main-Loop\n");
#endif
//ATTENTION
writeThread->consPairCount = 0;
writeThread->consPairRead = 0;
writeThread->cpuConsSaCount = 0;
writeThread->cpuConsReadCount = 0;
writeThread->consReadCount = 0;
// Timestamp'ed -----------
double startTime = setStartTime();
double lastEventTime = 0;
double totalElipsedTime = 0;
// ------------------------
while (1) {
int bufferIdx;
// Block until there is an non-empty buffer
pthread_mutex_lock(mutexOutputQueue);
// Loop wait as long as
// - alignment queue is empty
// - AND free queue is not full OR thread is healthy
while (
SQIsEmpty(writeThread->outputQueue) &&
(
!SQIsFull(writeThread->freeQueue) ||
!SQIsEmpty(writeThread->alignmentQueue) ||
writeThread->threadHealth==MICA_PE_WRITE_THREAD_HEALTH_OK
)
) {
pthread_cond_wait(condOutputQueue,mutexOutputQueue);
}
// Break in case
// - free queue is full
// - AND alignment queue is empty
// - AND thread is not healthy
if (
SQIsFull(writeThread->freeQueue) &&
SQIsEmpty(writeThread->alignmentQueue) &&
SQIsEmpty(writeThread->outputQueue) &&
writeThread->threadHealth!=MICA_PE_WRITE_THREAD_HEALTH_OK
) {
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTOutputRollBody - Thread is not healthy and Buffer is empty. Exiting.\n");
#endif
pthread_mutex_unlock(mutexOutputQueue);
break;
}
if (!SQDequeue(writeThread->outputQueue,&bufferIdx)) {
printf("[SAFE-GUARD] Unexpected mutex error.\n");
printf("The outputQueue is supposed to be non-empty.\n");
}
pthread_mutex_unlock(mutexOutputQueue);
// Timestamp'ed -----------
double timestamp = getElapsedTime(startTime);
lastEventTime = timestamp;
// ------------------------
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTOutputRollBody - Received Signal on condition(condOutputQueue)\n");
#endif
//Process Payload
//
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTOutputRollBody - Processing Payload from buffer %d.\n", bufferIdx);
#endif
unsigned int queryInBatch = writeThread->queryInBatch[bufferIdx];
unsigned int batchSize = writeThread->batchSize[bufferIdx];
unsigned int numThreads = writeThread->numThreads[bufferIdx];
unsigned int firstReadIdx = writeThread->firstReadIdx[bufferIdx];
int algnFuncReasonFlag = writeThread->algnFuncReasonFlag[bufferIdx];
if ( algnFuncReasonFlag == MIC_ALGNMT_FOR_UNDEFINED ) {
printf("[SAFE-GUARD] Alignment function flag indicates undefined reason for batch!\n");
}
#ifdef DEBUG_MICA_PEWT_LOGGING
printf ( "PEWT - _PEWTOutputRollBody - #Thread %u / BatchSize %u / #Query %u.\n",numThreads,batchSize,queryInBatch);
#endif
char * readNameBufferFrame = writeThread->readNameBufferFrame[bufferIdx];
unsigned char * readBodyBufferFrame = writeThread->readBodyBufferFrame[bufferIdx];
char * readQualityBufferFrame= writeThread->readQualityBufferFrame[bufferIdx];
uint16_t * readLengthBufferFrame = writeThread->readLengthBufferFrame[bufferIdx];
char * mateNameBufferFrame = writeThread->mateNameBufferFrame[bufferIdx];
unsigned char * mateBodyBufferFrame = writeThread->mateBodyBufferFrame[bufferIdx];
char * mateQualityBufferFrame= writeThread->mateQualityBufferFrame[bufferIdx];
uint16_t * mateLengthBufferFrame = writeThread->mateLengthBufferFrame[bufferIdx];
PEReadOutput * peBatchOutput = writeThread->peBatchOutput[bufferIdx];
uint8_t outputType = writeThread->outputType[bufferIdx];
uint16_t * occCount = writeThread->occCount[bufferIdx];
uint8_t * outputBufferStatus = writeThread->outputBufferStatus[bufferIdx];
unsigned int * outputBufferFrame = writeThread->outputBufferFrame[bufferIdx];
MICSRAOccMetadata * outputBufferMeta = writeThread->outputBufferMeta[bufferIdx];
MICDPOccurrence * dpOutputBufferFrame = writeThread->dpOutputBufferFrame[bufferIdx];
PEMappingQuality * mappingQualities = writeThread->mappingQualities[bufferIdx];