comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/kmer/HistogramMaker.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 68:5028fdace37b
1 package kmer;
2
3 import java.util.concurrent.atomic.AtomicInteger;
4
5 import shared.Shared;
6 import shared.Tools;
7 import structures.SuperLongList;
8
9 public final class HistogramMaker {
10
11
12 public static long[] fillHistogram(final AbstractKmerTable[] tables, final int histMax) {
13 if(Shared.threads()>2){
14 return fillHistogram_MT(tables, histMax);
15 }else{
16 return fillHistogram_ST(tables, histMax);
17 }
18 }
19
20 private static long[] fillHistogram_ST(final AbstractKmerTable[] tables, final int histMax) {
21 long[] ca=new long[histMax+1];
22 for(AbstractKmerTable set : tables){
23 set.fillHistogram(ca, histMax);
24 }
25 return ca;
26 }
27
28 private static long[] fillHistogram_MT(final AbstractKmerTable[] tables, final int histMax) {
29 boolean errorState=false;
30 int threads=Shared.threads();
31 threads=Tools.min((threads>20 ? threads/2 : threads), (tables.length+1)/2, 32);
32 if(threads<2){return fillHistogram_ST(tables, histMax);}
33
34 final FillThread[] array=new FillThread[threads];
35 final AtomicInteger next=new AtomicInteger(0);
36 for(int i=0; i<threads; i++){array[i]=new FillThread(tables, histMax, next);}
37 for(int i=0; i<threads; i++){array[i].start();}
38
39 //Wait for completion of all threads
40 final long[] ca=new long[histMax+1];
41 boolean success=true;
42 for(FillThread pt : array){
43
44 //Wait until this thread has terminated
45 while(pt.getState()!=Thread.State.TERMINATED){
46 try {
47 //Attempt a join operation
48 pt.join();
49 } catch (InterruptedException e) {
50 //Potentially handle this, if it is expected to occur
51 e.printStackTrace();
52 }
53 }
54
55 //Accumulate per-thread statistics
56
57 pt.sll.addTo(ca);
58 pt.sll=null;
59 }
60
61 //Track whether any threads failed
62 if(!success){errorState=true;}
63
64 return ca;
65 }
66
67 private static class FillThread extends Thread{
68
69 FillThread(final AbstractKmerTable[] tables_, int histMax_, AtomicInteger next_){
70 tables=tables_;
71 next=next_;
72 sll=new SuperLongList(Tools.mid(5000, histMax_, 100000));
73 }
74
75 @Override
76 public void run(){
77 for(int tnum=next.getAndIncrement(); tnum<tables.length; tnum=next.getAndIncrement()){
78 tables[tnum].fillHistogram(sll);
79 }
80 }
81
82 final AbstractKmerTable[] tables;
83 final AtomicInteger next;
84 SuperLongList sll;
85
86 }
87
88 }