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
|
package ukmer;
import java.util.concurrent.atomic.AtomicInteger;
import shared.Shared;
import shared.Tools;
import structures.SuperLongList;
public final class HistogramMakerU {
public static long[] fillHistogram(final AbstractKmerTableU[] tables, final int histMax) {
if(Shared.threads()>2){
return fillHistogram_MT(tables, histMax);
}else{
return fillHistogram_ST(tables, histMax);
}
}
private static long[] fillHistogram_ST(final AbstractKmerTableU[] tables, final int histMax) {
long[] ca=new long[histMax+1];
for(AbstractKmerTableU set : tables){
set.fillHistogram(ca, histMax);
}
return ca;
}
private static long[] fillHistogram_MT(final AbstractKmerTableU[] tables, final int histMax) {
boolean errorState=false;
int threads=Shared.threads();
threads=Tools.min((threads>20 ? threads/2 : threads), (tables.length+1)/2, 32);
if(threads<2){return fillHistogram_ST(tables, histMax);}
final FillThread[] array=new FillThread[threads];
final AtomicInteger next=new AtomicInteger(0);
for(int i=0; i<threads; i++){array[i]=new FillThread(tables, histMax, next);}
for(int i=0; i<threads; i++){array[i].start();}
//Wait for completion of all threads
final long[] ca=new long[histMax+1];
boolean success=true;
for(FillThread pt : array){
//Wait until this thread has terminated
while(pt.getState()!=Thread.State.TERMINATED){
try {
//Attempt a join operation
pt.join();
} catch (InterruptedException e) {
//Potentially handle this, if it is expected to occur
e.printStackTrace();
}
}
//Accumulate per-thread statistics
pt.sll.addTo(ca);
pt.sll=null;
}
//Track whether any threads failed
if(!success){errorState=true;}
return ca;
}
private static class FillThread extends Thread{
FillThread(final AbstractKmerTableU[] tables_, int histMax_, AtomicInteger next_){
tables=tables_;
next=next_;
sll=new SuperLongList(Tools.mid(5000, histMax_, 100000));
}
@Override
public void run(){
for(int tnum=next.getAndIncrement(); tnum<tables.length; tnum=next.getAndIncrement()){
tables[tnum].fillHistogram(sll);
}
}
final AbstractKmerTableU[] tables;
final AtomicInteger next;
SuperLongList sll;
}
}
|