| 1 |
package ukmer; |
| 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 HistogramMakerU { |
| 10 |
|
| 11 |
public static long[] fillHistogram(final AbstractKmerTableU[] tables, final int histMax) { |
| 12 |
if(Shared.threads()>2){ |
| 13 |
return fillHistogram_MT(tables, histMax); |
| 14 |
}else{ |
| 15 |
return fillHistogram_ST(tables, histMax); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
private static long[] fillHistogram_ST(final AbstractKmerTableU[] tables, final int histMax) { |
| 20 |
long[] ca=new long[histMax+1]; |
| 21 |
for(AbstractKmerTableU set : tables){ |
| 22 |
set.fillHistogram(ca, histMax); |
| 23 |
} |
| 24 |
return ca; |
| 25 |
} |
| 26 |
|
| 27 |
private static long[] fillHistogram_MT(final AbstractKmerTableU[] tables, final int histMax) { |
| 28 |
boolean errorState=false; |
| 29 |
int threads=Shared.threads(); |
| 30 |
threads=Tools.min((threads>20 ? threads/2 : threads), (tables.length+1)/2, 32); |
| 31 |
if(threads<2){return fillHistogram_ST(tables, histMax);} |
| 32 |
|
| 33 |
final FillThread[] array=new FillThread[threads]; |
| 34 |
final AtomicInteger next=new AtomicInteger(0); |
| 35 |
for(int i=0; i<threads; i++){array[i]=new FillThread(tables, histMax, next);} |
| 36 |
for(int i=0; i<threads; i++){array[i].start();} |
| 37 |
|
| 38 |
|
| 39 |
final long[] ca=new long[histMax+1]; |
| 40 |
boolean success=true; |
| 41 |
for(FillThread pt : array){ |
| 42 |
|
| 43 |
|
| 44 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 45 |
try { |
| 46 |
|
| 47 |
pt.join(); |
| 48 |
} catch (InterruptedException e) { |
| 49 |
|
| 50 |
e.printStackTrace(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
pt.sll.addTo(ca); |
| 57 |
pt.sll=null; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
if(!success){errorState=true;} |
| 62 |
|
| 63 |
return ca; |
| 64 |
} |
| 65 |
|
| 66 |
private static class FillThread extends Thread{ |
| 67 |
|
| 68 |
FillThread(final AbstractKmerTableU[] tables_, int histMax_, AtomicInteger next_){ |
| 69 |
tables=tables_; |
| 70 |
next=next_; |
| 71 |
sll=new SuperLongList(Tools.mid(5000, histMax_, 100000)); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void run(){ |
| 76 |
for(int tnum=next.getAndIncrement(); tnum<tables.length; tnum=next.getAndIncrement()){ |
| 77 |
tables[tnum].fillHistogram(sll); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
final AbstractKmerTableU[] tables; |
| 82 |
final AtomicInteger next; |
| 83 |
SuperLongList sll; |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
} |