package bloom; import java.lang.Thread.State; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.atomic.AtomicIntegerArray; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import shared.KillSwitch; import shared.Primes; import shared.Shared; import shared.Timer; import shared.Tools; import structures.ByteBuilder; /** * * Uses prime numbers for array lengths. * Uses atomic integers for concurrency control. * Allows an optional prefilter. * * @author Brian Bushnell * @date Aug 17, 2012 * */ public final class KCountArray7MTA extends KCountArray { /** * */ private static final long serialVersionUID = 568264681638739631L; public static void main(String[] args){ long cells=Long.parseLong(args[0]); int bits=Integer.parseInt(args[1]); int hashes=Integer.parseInt(args[2]); verbose=false; KCountArray7MTA kca=new KCountArray7MTA(cells, bits, hashes, null, 0); System.out.println(kca.read(0)); kca.increment(0); System.out.println(kca.read(0)); kca.increment(0); System.out.println(kca.read(0)); System.out.println(); System.out.println(kca.read(1)); kca.increment(1); System.out.println(kca.read(1)); kca.increment(1); System.out.println(kca.read(1)); System.out.println(); System.out.println(kca.read(100)); kca.increment(100); System.out.println(kca.read(100)); kca.increment(100); System.out.println(kca.read(100)); kca.increment(100); System.out.println(kca.read(100)); System.out.println(); System.out.println(kca.read(150)); kca.increment(150); System.out.println(kca.read(150)); System.out.println(); } public KCountArray7MTA(long cells_, int bits_, int hashes_, KCountArray prefilter_, int prefilterLimit_){ super(getPrimeCells(cells_, bits_), bits_, getDesiredArrays(cells_, bits_)); // verbose=false; // System.out.println(cells); cellsPerArray=cells/numArrays; wordsPerArray=(int)((cellsPerArray%cellsPerWord)==0 ? (cellsPerArray/cellsPerWord) : (cellsPerArray/cellsPerWord+1)); cellMod=cellsPerArray; hashes=hashes_; prefilter=prefilter_; prefilterLimit=(prefilter==null ? 0 : Tools.min(prefilter.maxValue, prefilterLimit_)); // System.out.println("cells="+cells+", words="+words+", wordsPerArray="+wordsPerArray+", numArrays="+numArrays+", hashes="+hashes); matrix=allocMatrix(numArrays, wordsPerArray); useLocks=(cellBits>1 && hashes>1) && (LOCKED_INCREMENT || !SET_LOCKED_INCREMENT); if(useLocks){ locks=new Lock[NUM_LOCKS]; for(int i=0; i0 && hashes<=hashMasks.length); } private static int getDesiredArrays(long desiredCells, int bits){ long words=Tools.max((desiredCells*bits+31)/32, minArrays); int arrays=minArrays; while(words/arrays>=Integer.MAX_VALUE){ arrays*=2; } // assert(false) : arrays; return arrays; // return Tools.max(arrays, Data.LOGICAL_PROCESSORS*4); } private static long getPrimeCells(long desiredCells, int bits){ int arrays=getDesiredArrays(desiredCells, bits); long x=(desiredCells+arrays-1)/arrays; long x2=Primes.primeAtMost(x); return x2*arrays; } @Override public final int read(final long rawKey){ if(verbose){System.err.println("Reading raw key "+rawKey);} if(prefilter!=null){ int pre=prefilter.read(rawKey); if(pre0; i++){ if(verbose){System.err.println("Reading. i="+i+", key2="+key2);} key2=Long.rotateRight(key2, hashBits); key2=hash(key2, i); if(verbose){System.err.println("Rot/hash. i="+i+", key2="+key2);} min=min(min, readHashed(key2)); } return min; } @Override public final int read(final long[] rawKeys){ if(verbose){System.err.println("Reading raw key "+Arrays.toString(rawKeys));} if(prefilter!=null){ int pre=prefilter.read(rawKeys); if(pre0) : incr; final Lock lock=getLock(rawKey); lock.lock(); final int min=read(rawKey); if(min>=maxValue){ lock.unlock(); return min; } final int newMin=(int)Tools.min(min+(long)incr, maxValue); long key2=rawKey; int value=maxValue; for(int i=0; i>>=cellBits; comma=", "; } } } sb.append(']'); return sb; } @Override public double usedFraction(){return cellsUsed()/(double)cells;} @Override public double usedFraction(int mindepth){return cellsUsed(mindepth)/(double)cells;} @Override public long cellsUsed(int mindepth){ return cellsUsedMT(mindepth); } public long cellsUsedMT(int mindepth){ // assert(false) : matrix.length; ArrayList list=new ArrayList(matrix.length); for(AtomicIntegerArray aia : matrix){ CountUsedThread ctt=new CountUsedThread(aia, mindepth); ctt.start(); list.add(ctt); } long x=0; for(CountUsedThread ctt : list){ while(ctt.getState()!=State.TERMINATED){ try { ctt.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } x+=ctt.count; } return x; } private class CountUsedThread extends Thread{ public CountUsedThread(AtomicIntegerArray a_, int mindepth_){ array=a_; mindepth=mindepth_; } @Override public void run(){ long temp=0; if(array!=null){ // System.out.println("C"); // assert(false) : Integer.toBinaryString(valueMask); if(cellBits==32){ for(int i=0, max=array.length(); i=mindepth){temp++;} } } }else{ for(int i=0, max=array.length(); i=mindepth){temp++;} word=word>>>cellBits; } } } } count=temp; } private final AtomicIntegerArray array; private final int mindepth; public long count; } @Override final long hash(long key, int row){ int cell=(int)((Long.MAX_VALUE&key)%(hashArrayLength-1)); // int cell=(int)(hashCellMask&(key)); if(row==0){//Doublehash only first time key=key^hashMasks[(row+4)&7][cell]; // key=key^hashMasks[4][cell]; cell=(int)(hashCellMask&(key>>5)); // cell=(int)(hashCellMask&(key>>hashBits)); // cell=(int)((Long.MAX_VALUE&key)%(hashArrayLength-1)); } assert(row>=0 && row=0 && cell>=6; // } // return code; // } /** * @param i * @param j * @return */ private static long[][] makeMasks(int rows, int cols) { long seed; synchronized(KCountArray7MTA.class){ seed=counter^SEEDMASK; counter+=7; } Timer t=new Timer(); long[][] r=new long[rows][cols]; Random randy=Shared.threadLocalRandom(seed); for(int i=0; i200000000L){System.out.println("Mask-creation time: "+t);} return r; } private static void fillMasks(long[] r, Random randy) { // for(int i=0; i16){ x&=(~(1L<16){ x&=(~(1L<<(randy.nextInt(32)+32))); } // System.out.print("."); // y=(((int)(x&mask))^i); y=(((int)(x&mask))); z=(int)((x>>hashBits)&mask); if(count1[y]>0 || count2[z]>0){ x=0; } } // System.out.println(Long.toBinaryString(x)); r[i]=(x&Long.MAX_VALUE); count1[y]++; count2[z]++; } } @Override public void initialize(){} @Override public void shutdown(){ if(finished){return;} synchronized(this){ if(finished){return;} cellsUsed=-1; // for(int i=0; i>>arrayBits)%(cellMod); // key=(key>>>(arrayBits+1))%(cellMod); int index=(int)(key>>>indexShift); int cellShift=(int)(cellBits*key); int value, word, word2; do{ assert(index>=0) : key+", "+cellMod+", "+cellBits+", "+valueMask+", "+arrayMask+", "+index+", "+num; word=array.get(index); value=((word>>>cellShift)&valueMask); value=(int)min(value+(long)amt, maxValue); word2=(value<>>arrayBits)%(cellMod); //// key=(key>>>(arrayBits+1))%(cellMod); // int index=(int)(key>>>indexShift); // int cellShift=(int)(cellBits*key); // int value, word, word2; // do{ // assert(index>=0) : key+", "+cellMod+", "+cellBits+", "+valueMask+", "+arrayMask+", "+index+", "+num; // word=array.get(index); // value=((word>>>cellShift)&valueMask); // if(value>thresh){return value;}//Too high; don't increment // value=min(value+1, maxValue); // word2=(value<>>arrayBits)%(cellMod); // key=(key>>>(arrayBits+1))%(cellMod); int index=(int)(key>>>indexShift); int cellShift=(int)(cellBits*key); int value, word, word2; do{ assert(index>=0) : key+", "+cellMod+", "+cellBits+", "+valueMask+", "+arrayMask+", "+index+", "+num; word=array.get(index); value=((word>>>cellShift)&valueMask); if(value>=newMin){return value;}//Too high; don't increment value=min(value+1, maxValue); word2=(value<=0); final int num=(int)(key&arrayMask); final AtomicIntegerArray array=matrix[num]; key=(key>>>arrayBits)%(cellMod); // key=(key>>>(arrayBits+1))%(cellMod); int index=(int)(key>>>indexShift); int cellShift=(int)(cellBits*key); int value, word, word2; do{ word=array.get(index); value=((word>>>cellShift)&valueMask); int value2=min(value+incr, maxValue); word2=(value2<=0); // final int num=(int)(key&arrayMask); // final AtomicIntegerArray array=matrix[num]; // key=(key>>>arrayBits)%(cellMod); //// key=(key>>>(arrayBits+1))%(cellMod); // int index=(int)(key>>>indexShift); // int cellShift=(int)(cellBits*key); // int value, word, word2; // do{ // word=array.get(index); // value=((word>>>cellShift)&valueMask); // if(value>thresh){return value;}//Too high; don't increment // int value2=min(value+incr, maxValue); // word2=(value2<0 && newMin<=maxValue) : newMin; final int num=(int)(key&arrayMask); final AtomicIntegerArray array=matrix[num]; key=(key>>>arrayBits)%(cellMod); // key=(key>>>(arrayBits+1))%(cellMod); int index=(int)(key>>>indexShift); int cellShift=(int)(cellBits*key); int value, word, word2; final int value2s=newMin<>>cellShift)&valueMask); if(value>=newMin){return value;}//Too high; don't increment word2=value2s|(word&~((valueMask)<>>arrayBits)%(cellMod); // key=(key>>>(arrayBits+1))%(cellMod); int index=(int)(key>>>indexShift); int cellShift=(int)(cellBits*key); int value, word, word2; do{ word=array.get(index); value=((word>>>cellShift)&valueMask); value=max(value-amt, 0); word2=(value<=0){SEEDMASK=seed;} else{ Random randy=new Random(); SEEDMASK=randy.nextLong(); } } private boolean finished=false; private long cellsUsed=-1; private final AtomicIntegerArray[] matrix; private final int hashes; private final int wordsPerArray; private final long cellsPerArray; private final long cellMod; private final long[][] hashMasks=makeMasks(8, hashArrayLength); private final int prefilterLimit; private static final int hashBits=6; private static final int hashArrayLength=1<