Mercurial > repos > rliterman > csp2
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/kmer/HistogramMaker.java Tue Mar 18 16:23:26 2025 -0400 @@ -0,0 +1,88 @@ +package kmer; + +import java.util.concurrent.atomic.AtomicInteger; + +import shared.Shared; +import shared.Tools; +import structures.SuperLongList; + +public final class HistogramMaker { + + + public static long[] fillHistogram(final AbstractKmerTable[] 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 AbstractKmerTable[] tables, final int histMax) { + long[] ca=new long[histMax+1]; + for(AbstractKmerTable set : tables){ + set.fillHistogram(ca, histMax); + } + return ca; + } + + private static long[] fillHistogram_MT(final AbstractKmerTable[] 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 AbstractKmerTable[] 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 AbstractKmerTable[] tables; + final AtomicInteger next; + SuperLongList sll; + + } + +}