jpayne@68
|
1 package bloom;
|
jpayne@68
|
2
|
jpayne@68
|
3 import java.util.Arrays;
|
jpayne@68
|
4 import java.util.Random;
|
jpayne@68
|
5 import java.util.concurrent.ArrayBlockingQueue;
|
jpayne@68
|
6
|
jpayne@68
|
7 import shared.Primes;
|
jpayne@68
|
8 import shared.Shared;
|
jpayne@68
|
9 import shared.Timer;
|
jpayne@68
|
10 import shared.Tools;
|
jpayne@68
|
11 import structures.ByteBuilder;
|
jpayne@68
|
12
|
jpayne@68
|
13
|
jpayne@68
|
14 /**
|
jpayne@68
|
15 *
|
jpayne@68
|
16 * Uses prime numbers for array lengths.
|
jpayne@68
|
17 *
|
jpayne@68
|
18 * @author Brian Bushnell
|
jpayne@68
|
19 * @date Aug 17, 2012
|
jpayne@68
|
20 *
|
jpayne@68
|
21 */
|
jpayne@68
|
22 public class KCountArray7MT extends KCountArray {
|
jpayne@68
|
23
|
jpayne@68
|
24 /**
|
jpayne@68
|
25 *
|
jpayne@68
|
26 */
|
jpayne@68
|
27 private static final long serialVersionUID = -8767643111803866913L;
|
jpayne@68
|
28
|
jpayne@68
|
29 public static void main(String[] args){
|
jpayne@68
|
30 long cells=Long.parseLong(args[0]);
|
jpayne@68
|
31 int bits=Integer.parseInt(args[1]);
|
jpayne@68
|
32 int hashes=Integer.parseInt(args[2]);
|
jpayne@68
|
33
|
jpayne@68
|
34 verbose=false;
|
jpayne@68
|
35
|
jpayne@68
|
36 KCountArray7MT kca=new KCountArray7MT(cells, bits, hashes);
|
jpayne@68
|
37
|
jpayne@68
|
38 System.out.println(kca.read(0));
|
jpayne@68
|
39 kca.increment(0);
|
jpayne@68
|
40 System.out.println(kca.read(0));
|
jpayne@68
|
41 kca.increment(0);
|
jpayne@68
|
42 System.out.println(kca.read(0));
|
jpayne@68
|
43 System.out.println();
|
jpayne@68
|
44
|
jpayne@68
|
45 System.out.println(kca.read(1));
|
jpayne@68
|
46 kca.increment(1);
|
jpayne@68
|
47 System.out.println(kca.read(1));
|
jpayne@68
|
48 kca.increment(1);
|
jpayne@68
|
49 System.out.println(kca.read(1));
|
jpayne@68
|
50 System.out.println();
|
jpayne@68
|
51
|
jpayne@68
|
52 System.out.println(kca.read(100));
|
jpayne@68
|
53 kca.increment(100);
|
jpayne@68
|
54 System.out.println(kca.read(100));
|
jpayne@68
|
55 kca.increment(100);
|
jpayne@68
|
56 System.out.println(kca.read(100));
|
jpayne@68
|
57 kca.increment(100);
|
jpayne@68
|
58 System.out.println(kca.read(100));
|
jpayne@68
|
59 System.out.println();
|
jpayne@68
|
60
|
jpayne@68
|
61
|
jpayne@68
|
62 System.out.println(kca.read(150));
|
jpayne@68
|
63 kca.increment(150);
|
jpayne@68
|
64 System.out.println(kca.read(150));
|
jpayne@68
|
65 System.out.println();
|
jpayne@68
|
66
|
jpayne@68
|
67 }
|
jpayne@68
|
68
|
jpayne@68
|
69 public KCountArray7MT(long cells_, int bits_, int hashes_){
|
jpayne@68
|
70 super(getPrimeCells(cells_, bits_), bits_, getDesiredArrays(cells_, bits_));
|
jpayne@68
|
71 // verbose=false;
|
jpayne@68
|
72 // assert(false);
|
jpayne@68
|
73 // System.out.println(cells);
|
jpayne@68
|
74 cellsPerArray=cells/numArrays;
|
jpayne@68
|
75 wordsPerArray=(int)((cellsPerArray%cellsPerWord)==0 ? (cellsPerArray/cellsPerWord) : (cellsPerArray/cellsPerWord+1));
|
jpayne@68
|
76 cellMod=cellsPerArray;
|
jpayne@68
|
77 hashes=hashes_;
|
jpayne@68
|
78 // System.out.println("cells="+cells+", words="+words+", wordsPerArray="+wordsPerArray+", numArrays="+numArrays+", hashes="+hashes);
|
jpayne@68
|
79 // assert(false);
|
jpayne@68
|
80 matrix=new int[numArrays][];
|
jpayne@68
|
81 assert(hashes>0 && hashes<=hashMasks.length);
|
jpayne@68
|
82 }
|
jpayne@68
|
83
|
jpayne@68
|
84 private static int getDesiredArrays(long desiredCells, int bits){
|
jpayne@68
|
85
|
jpayne@68
|
86 long words=Tools.max((desiredCells*bits+31)/32, minArrays);
|
jpayne@68
|
87 int arrays=minArrays;
|
jpayne@68
|
88 while(words/arrays>=Integer.MAX_VALUE){
|
jpayne@68
|
89 arrays*=2;
|
jpayne@68
|
90 }
|
jpayne@68
|
91 // assert(false) : arrays;
|
jpayne@68
|
92 return arrays;
|
jpayne@68
|
93 }
|
jpayne@68
|
94
|
jpayne@68
|
95 private static long getPrimeCells(long desiredCells, int bits){
|
jpayne@68
|
96
|
jpayne@68
|
97 int arrays=getDesiredArrays(desiredCells, bits);
|
jpayne@68
|
98
|
jpayne@68
|
99 long x=(desiredCells+arrays-1)/arrays;
|
jpayne@68
|
100 long x2=Primes.primeAtMost(x);
|
jpayne@68
|
101 return x2*arrays;
|
jpayne@68
|
102 }
|
jpayne@68
|
103
|
jpayne@68
|
104 @Override
|
jpayne@68
|
105 public int read(final long rawKey){
|
jpayne@68
|
106 assert(finished);
|
jpayne@68
|
107 if(verbose){System.err.println("Reading raw key "+rawKey);}
|
jpayne@68
|
108 long key2=hash(rawKey, 0);
|
jpayne@68
|
109 int min=readHashed(key2);
|
jpayne@68
|
110 for(int i=1; i<hashes && min>0; i++){
|
jpayne@68
|
111 if(verbose){System.err.println("Reading. i="+i+", key2="+key2);}
|
jpayne@68
|
112 key2=Long.rotateRight(key2, hashBits);
|
jpayne@68
|
113 key2=hash(key2, i);
|
jpayne@68
|
114 if(verbose){System.err.println("Rot/hash. i="+i+", key2="+key2);}
|
jpayne@68
|
115 min=min(min, readHashed(key2));
|
jpayne@68
|
116 }
|
jpayne@68
|
117 return min;
|
jpayne@68
|
118 }
|
jpayne@68
|
119
|
jpayne@68
|
120 private int readHashed(long key){
|
jpayne@68
|
121 if(verbose){System.err.print("Reading hashed key "+key);}
|
jpayne@68
|
122 // System.out.println("key="+key);
|
jpayne@68
|
123 int arrayNum=(int)(key&arrayMask);
|
jpayne@68
|
124 key=(key>>>arrayBits)%(cellMod);
|
jpayne@68
|
125 // key=(key>>>(arrayBits+1))%(cellMod);
|
jpayne@68
|
126 // System.out.println("array="+arrayNum);
|
jpayne@68
|
127 // System.out.println("key2="+key);
|
jpayne@68
|
128 int[] array=matrix[arrayNum];
|
jpayne@68
|
129 int index=(int)(key>>>indexShift);
|
jpayne@68
|
130 // assert(false) : indexShift;
|
jpayne@68
|
131 // System.out.println("index="+index);
|
jpayne@68
|
132 int word=array[index];
|
jpayne@68
|
133 // System.out.println("word="+Integer.toHexString(word));
|
jpayne@68
|
134 assert(word>>>(cellBits*key) == word>>>(cellBits*(key&cellMask)));
|
jpayne@68
|
135 // int cellShift=(int)(cellBits*(key&cellMask));
|
jpayne@68
|
136 int cellShift=(int)(cellBits*key);
|
jpayne@68
|
137 if(verbose){System.err.println(", array="+arrayNum+", index="+index+", cellShift="+(cellShift%32)+", value="+((int)((word>>>cellShift)&valueMask)));}
|
jpayne@68
|
138 // System.out.println("cellShift="+cellShift);
|
jpayne@68
|
139 return (int)((word>>>cellShift)&valueMask);
|
jpayne@68
|
140 }
|
jpayne@68
|
141
|
jpayne@68
|
142 @Override
|
jpayne@68
|
143 public void write(final long key, int value){
|
jpayne@68
|
144 throw new RuntimeException("Not allowed for this class.");
|
jpayne@68
|
145 }
|
jpayne@68
|
146
|
jpayne@68
|
147 @Override
|
jpayne@68
|
148 /** This should increase speed by doing the first hash outside the critical section, but it does not seem to help. */
|
jpayne@68
|
149 public void increment(long[] keys){
|
jpayne@68
|
150 for(int i=0; i<keys.length; i++){
|
jpayne@68
|
151 keys[i]=hash(keys[i], 0);
|
jpayne@68
|
152 }
|
jpayne@68
|
153 synchronized(buffers){
|
jpayne@68
|
154 for(long key : keys){
|
jpayne@68
|
155 incrementPartiallyHashed(key);
|
jpayne@68
|
156 }
|
jpayne@68
|
157 }
|
jpayne@68
|
158 }
|
jpayne@68
|
159
|
jpayne@68
|
160 //Slow
|
jpayne@68
|
161 @Override
|
jpayne@68
|
162 public void increment(final long rawKey, int amt){
|
jpayne@68
|
163 for(int i=0; i<amt; i++){increment0(rawKey);}
|
jpayne@68
|
164 }
|
jpayne@68
|
165
|
jpayne@68
|
166 public void increment0(final long rawKey){
|
jpayne@68
|
167 if(verbose){System.err.println("\n*** Incrementing raw key "+rawKey+" ***");}
|
jpayne@68
|
168
|
jpayne@68
|
169 long key2=rawKey;
|
jpayne@68
|
170 for(int i=0; i<hashes; i++){
|
jpayne@68
|
171 key2=hash(key2, i);
|
jpayne@68
|
172 if(verbose){System.err.println("key2="+key2+", value="+readHashed(key2));}
|
jpayne@68
|
173 // assert(readHashed(key2)==0);
|
jpayne@68
|
174
|
jpayne@68
|
175 int bnum=(int)(key2&arrayMask);
|
jpayne@68
|
176 long[] array=buffers[bnum];
|
jpayne@68
|
177 int loc=bufferlen[bnum];
|
jpayne@68
|
178 array[loc]=key2;
|
jpayne@68
|
179 bufferlen[bnum]++;
|
jpayne@68
|
180 if(verbose){System.err.println("bufferlen["+bnum+"] = "+bufferlen[bnum]);}
|
jpayne@68
|
181 if(bufferlen[bnum]>=array.length){
|
jpayne@68
|
182
|
jpayne@68
|
183 if(verbose){System.err.println("Moving array.");}
|
jpayne@68
|
184 bufferlen[bnum]=0;
|
jpayne@68
|
185 buffers[bnum]=new long[array.length];
|
jpayne@68
|
186
|
jpayne@68
|
187 writers[bnum].add(array);
|
jpayne@68
|
188 if(verbose){System.err.println("Moved.");}
|
jpayne@68
|
189 }
|
jpayne@68
|
190 // assert(read(rawKey)<=min+incr) : "i="+i+", original="+min+", new should be <="+(min+incr)+", new="+read(rawKey)+", max="+maxValue+", key="+rawKey;
|
jpayne@68
|
191 // assert(readHashed(key2)>=min+incr) : "i="+i+", original="+min+", new should be <="+(min+incr)+", new="+read(rawKey)+", max="+maxValue+", key="+rawKey;
|
jpayne@68
|
192 key2=Long.rotateRight(key2, hashBits);
|
jpayne@68
|
193 }
|
jpayne@68
|
194 }
|
jpayne@68
|
195
|
jpayne@68
|
196 private void incrementPartiallyHashed(final long pKey){
|
jpayne@68
|
197 if(verbose){System.err.println("\n*** Incrementing key "+pKey+" ***");}
|
jpayne@68
|
198
|
jpayne@68
|
199 long key2=pKey;
|
jpayne@68
|
200
|
jpayne@68
|
201 {
|
jpayne@68
|
202 int bnum=(int)(key2&arrayMask);
|
jpayne@68
|
203 long[] array=buffers[bnum];
|
jpayne@68
|
204 int loc=bufferlen[bnum];
|
jpayne@68
|
205 array[loc]=key2;
|
jpayne@68
|
206 bufferlen[bnum]++;
|
jpayne@68
|
207 if(verbose){System.err.println("bufferlen["+bnum+"] = "+bufferlen[bnum]);}
|
jpayne@68
|
208 if(bufferlen[bnum]>=array.length){
|
jpayne@68
|
209
|
jpayne@68
|
210 if(verbose){System.err.println("Moving array.");}
|
jpayne@68
|
211 bufferlen[bnum]=0;
|
jpayne@68
|
212 buffers[bnum]=new long[array.length];
|
jpayne@68
|
213
|
jpayne@68
|
214 writers[bnum].add(array);
|
jpayne@68
|
215 if(verbose){System.err.println("Moved.");}
|
jpayne@68
|
216 }
|
jpayne@68
|
217 }
|
jpayne@68
|
218
|
jpayne@68
|
219 for(int i=1; i<hashes; i++){
|
jpayne@68
|
220 key2=Long.rotateRight(key2, hashBits);
|
jpayne@68
|
221 key2=hash(key2, i);
|
jpayne@68
|
222 if(verbose){System.err.println("key2="+key2+", value="+readHashed(key2));}
|
jpayne@68
|
223 // assert(readHashed(key2)==0);
|
jpayne@68
|
224
|
jpayne@68
|
225 int bnum=(int)(key2&arrayMask);
|
jpayne@68
|
226 long[] array=buffers[bnum];
|
jpayne@68
|
227 int loc=bufferlen[bnum];
|
jpayne@68
|
228 array[loc]=key2;
|
jpayne@68
|
229 bufferlen[bnum]++;
|
jpayne@68
|
230 if(verbose){System.err.println("bufferlen["+bnum+"] = "+bufferlen[bnum]);}
|
jpayne@68
|
231 if(bufferlen[bnum]>=array.length){
|
jpayne@68
|
232
|
jpayne@68
|
233 if(verbose){System.err.println("Moving array.");}
|
jpayne@68
|
234 bufferlen[bnum]=0;
|
jpayne@68
|
235 buffers[bnum]=new long[array.length];
|
jpayne@68
|
236
|
jpayne@68
|
237 writers[bnum].add(array);
|
jpayne@68
|
238 if(verbose){System.err.println("Moved.");}
|
jpayne@68
|
239 }
|
jpayne@68
|
240 }
|
jpayne@68
|
241 }
|
jpayne@68
|
242
|
jpayne@68
|
243 /** Returns unincremented value */
|
jpayne@68
|
244 @Override
|
jpayne@68
|
245 public int incrementAndReturnUnincremented(long key, int incr){
|
jpayne@68
|
246 throw new RuntimeException("Operation not supported.");
|
jpayne@68
|
247 }
|
jpayne@68
|
248
|
jpayne@68
|
249 @Override
|
jpayne@68
|
250 public long[] transformToFrequency(){
|
jpayne@68
|
251 return transformToFrequency(matrix);
|
jpayne@68
|
252 }
|
jpayne@68
|
253
|
jpayne@68
|
254 @Override
|
jpayne@68
|
255 public ByteBuilder toContentsString(){
|
jpayne@68
|
256 ByteBuilder sb=new ByteBuilder();
|
jpayne@68
|
257 sb.append("[");
|
jpayne@68
|
258 String comma="";
|
jpayne@68
|
259 for(int[] array : matrix){
|
jpayne@68
|
260 for(int i=0; i<array.length; i++){
|
jpayne@68
|
261 int word=array[i];
|
jpayne@68
|
262 for(int j=0; j<cellsPerWord; j++){
|
jpayne@68
|
263 int x=word&valueMask;
|
jpayne@68
|
264 sb.append(comma);
|
jpayne@68
|
265 sb.append(x);
|
jpayne@68
|
266 word>>>=cellBits;
|
jpayne@68
|
267 comma=", ";
|
jpayne@68
|
268 }
|
jpayne@68
|
269 }
|
jpayne@68
|
270 }
|
jpayne@68
|
271 sb.append("]");
|
jpayne@68
|
272 return sb;
|
jpayne@68
|
273 }
|
jpayne@68
|
274
|
jpayne@68
|
275 @Override
|
jpayne@68
|
276 public double usedFraction(){return cellsUsed/(double)cells;}
|
jpayne@68
|
277
|
jpayne@68
|
278 @Override
|
jpayne@68
|
279 public double usedFraction(int mindepth){return cellsUsed(mindepth)/(double)cells;}
|
jpayne@68
|
280
|
jpayne@68
|
281 @Override
|
jpayne@68
|
282 public long cellsUsed(int mindepth){
|
jpayne@68
|
283 long count=0;
|
jpayne@68
|
284 // System.out.println("A");
|
jpayne@68
|
285 for(int[] array : matrix){
|
jpayne@68
|
286 // System.out.println("B");
|
jpayne@68
|
287 if(array!=null){
|
jpayne@68
|
288 // System.out.println("C");
|
jpayne@68
|
289 for(int word : array){
|
jpayne@68
|
290 // System.out.println("D: "+Integer.toHexString(word));
|
jpayne@68
|
291 while(word>0){
|
jpayne@68
|
292 int x=word&valueMask;
|
jpayne@68
|
293 // System.out.println("E: "+x+", "+mindepth);
|
jpayne@68
|
294 if(x>=mindepth){count++;}
|
jpayne@68
|
295 word>>>=cellBits;
|
jpayne@68
|
296 }
|
jpayne@68
|
297 }
|
jpayne@68
|
298 }
|
jpayne@68
|
299 }
|
jpayne@68
|
300 return count;
|
jpayne@68
|
301 }
|
jpayne@68
|
302
|
jpayne@68
|
303
|
jpayne@68
|
304 @Override
|
jpayne@68
|
305 final long hash(long key, int row){
|
jpayne@68
|
306 int cell=(int)((Long.MAX_VALUE&key)%(hashArrayLength-1));
|
jpayne@68
|
307 // int cell=(int)(hashCellMask&(key));
|
jpayne@68
|
308
|
jpayne@68
|
309 if(row==0){//Doublehash only first time
|
jpayne@68
|
310 key=key^hashMasks[(row+4)%hashMasks.length][cell];
|
jpayne@68
|
311 cell=(int)(hashCellMask&(key>>5));
|
jpayne@68
|
312 // cell=(int)(hashCellMask&(key>>hashBits));
|
jpayne@68
|
313 // cell=(int)((Long.MAX_VALUE&key)%(hashArrayLength-1));
|
jpayne@68
|
314 }
|
jpayne@68
|
315
|
jpayne@68
|
316 return key^hashMasks[row][cell];
|
jpayne@68
|
317 }
|
jpayne@68
|
318
|
jpayne@68
|
319 /**
|
jpayne@68
|
320 * @param i
|
jpayne@68
|
321 * @param j
|
jpayne@68
|
322 * @return
|
jpayne@68
|
323 */
|
jpayne@68
|
324 private static long[][] makeMasks(int rows, int cols) {
|
jpayne@68
|
325
|
jpayne@68
|
326 long seed;
|
jpayne@68
|
327 synchronized(KCountArray7MT.class){
|
jpayne@68
|
328 seed=counter;
|
jpayne@68
|
329 counter++;
|
jpayne@68
|
330 }
|
jpayne@68
|
331
|
jpayne@68
|
332 Timer t=new Timer();
|
jpayne@68
|
333 long[][] r=new long[rows][cols];
|
jpayne@68
|
334 Random randy=Shared.threadLocalRandom(seed);
|
jpayne@68
|
335 for(int i=0; i<r.length; i++){
|
jpayne@68
|
336 fillMasks(r[i], randy);
|
jpayne@68
|
337 }
|
jpayne@68
|
338 t.stop();
|
jpayne@68
|
339 if(t.elapsed>200000000L){System.out.println("Mask-creation time: "+t);}
|
jpayne@68
|
340 return r;
|
jpayne@68
|
341 }
|
jpayne@68
|
342
|
jpayne@68
|
343 private static void fillMasks(long[] r, Random randy) {
|
jpayne@68
|
344 // for(int i=0; i<r.length; i++){
|
jpayne@68
|
345 // long x=0;
|
jpayne@68
|
346 // while(Long.bitCount(x&0xFFFFFFFF)!=16){
|
jpayne@68
|
347 // x=randy.nextLong();
|
jpayne@68
|
348 // }
|
jpayne@68
|
349 // r[i]=(x&Long.MAX_VALUE);
|
jpayne@68
|
350 // }
|
jpayne@68
|
351
|
jpayne@68
|
352 final int hlen=(1<<hashBits);
|
jpayne@68
|
353 assert(r.length==hlen);
|
jpayne@68
|
354 int[] count1=new int[hlen];
|
jpayne@68
|
355 int[] count2=new int[hlen];
|
jpayne@68
|
356 final long mask=hlen-1;
|
jpayne@68
|
357
|
jpayne@68
|
358 for(int i=0; i<r.length; i++){
|
jpayne@68
|
359 long x=0;
|
jpayne@68
|
360 int y=0;
|
jpayne@68
|
361 int z=0;
|
jpayne@68
|
362 while(Long.bitCount(x&0xFFFFFFFFL)!=16){
|
jpayne@68
|
363 x=randy.nextLong();
|
jpayne@68
|
364 while(Long.bitCount(x&0xFFFFFFFFL)<16){
|
jpayne@68
|
365 x|=(1L<<randy.nextInt(32));
|
jpayne@68
|
366 }
|
jpayne@68
|
367 while(Long.bitCount(x&0xFFFFFFFFL)>16){
|
jpayne@68
|
368 x&=(~(1L<<randy.nextInt(32)));
|
jpayne@68
|
369 }
|
jpayne@68
|
370 while(Long.bitCount(x&0xFFFFFFFF00000000L)<16){
|
jpayne@68
|
371 x|=(1L<<(randy.nextInt(32)+32));
|
jpayne@68
|
372 }
|
jpayne@68
|
373 while(Long.bitCount(x&0xFFFFFFFF00000000L)>16){
|
jpayne@68
|
374 x&=(~(1L<<(randy.nextInt(32)+32)));
|
jpayne@68
|
375 }
|
jpayne@68
|
376
|
jpayne@68
|
377 // System.out.print(".");
|
jpayne@68
|
378 // y=(((int)(x&mask))^i);
|
jpayne@68
|
379 y=(((int)(x&mask)));
|
jpayne@68
|
380 z=(int)((x>>hashBits)&mask);
|
jpayne@68
|
381 if(count1[y]>0 || count2[z]>0){
|
jpayne@68
|
382 x=0;
|
jpayne@68
|
383 }
|
jpayne@68
|
384 }
|
jpayne@68
|
385 // System.out.println(Long.toBinaryString(x));
|
jpayne@68
|
386 r[i]=(x&Long.MAX_VALUE);
|
jpayne@68
|
387 count1[y]++;
|
jpayne@68
|
388 count2[z]++;
|
jpayne@68
|
389 }
|
jpayne@68
|
390
|
jpayne@68
|
391 }
|
jpayne@68
|
392
|
jpayne@68
|
393
|
jpayne@68
|
394 @Override
|
jpayne@68
|
395 public void initialize(){
|
jpayne@68
|
396 for(int i=0; i<writers.length; i++){
|
jpayne@68
|
397 writers[i]=new WriteThread(i);
|
jpayne@68
|
398 writers[i].start();
|
jpayne@68
|
399
|
jpayne@68
|
400 // while(!writers[i].isAlive()){
|
jpayne@68
|
401 // System.out.print(".");
|
jpayne@68
|
402 // }
|
jpayne@68
|
403 }
|
jpayne@68
|
404 // assert(false) : writers.length;
|
jpayne@68
|
405 }
|
jpayne@68
|
406
|
jpayne@68
|
407 @Override
|
jpayne@68
|
408 public void shutdown(){
|
jpayne@68
|
409 if(finished){return;}
|
jpayne@68
|
410 synchronized(this){
|
jpayne@68
|
411 if(finished){return;}
|
jpayne@68
|
412
|
jpayne@68
|
413 //Clear buffers
|
jpayne@68
|
414 for(int i=0; i<numArrays; i++){
|
jpayne@68
|
415 long[] array=buffers[i];
|
jpayne@68
|
416 int len=bufferlen[i];
|
jpayne@68
|
417 buffers[i]=null;
|
jpayne@68
|
418 bufferlen[i]=0;
|
jpayne@68
|
419
|
jpayne@68
|
420 if(len<array.length){
|
jpayne@68
|
421 array=Arrays.copyOf(array, len);
|
jpayne@68
|
422 }
|
jpayne@68
|
423
|
jpayne@68
|
424 if(array.length>0){
|
jpayne@68
|
425 writers[i].add(array);
|
jpayne@68
|
426 }
|
jpayne@68
|
427 }
|
jpayne@68
|
428
|
jpayne@68
|
429 //Add poison
|
jpayne@68
|
430 for(WriteThread wt : writers){
|
jpayne@68
|
431 wt.add(poison);
|
jpayne@68
|
432 }
|
jpayne@68
|
433
|
jpayne@68
|
434 //Wait for termination
|
jpayne@68
|
435 for(WriteThread wt : writers){
|
jpayne@68
|
436 // System.out.println("wt"+wt.num+" is alive: "+wt.isAlive());
|
jpayne@68
|
437 while(wt.isAlive()){
|
jpayne@68
|
438 // System.out.println("wt"+wt.num+" is alive: "+wt.isAlive());
|
jpayne@68
|
439 try {
|
jpayne@68
|
440 wt.join(10000);
|
jpayne@68
|
441 } catch (InterruptedException e) {
|
jpayne@68
|
442 // TODO Auto-generated catch block
|
jpayne@68
|
443 e.printStackTrace();
|
jpayne@68
|
444 }
|
jpayne@68
|
445 if(wt.isAlive()){System.err.println(wt.getClass().getCanonicalName()+" is taking a long time to die.");}
|
jpayne@68
|
446 }
|
jpayne@68
|
447 cellsUsed+=wt.cellsUsedPersonal;
|
jpayne@68
|
448 // System.out.println("cellsUsed="+cellsUsed);
|
jpayne@68
|
449 // System.err.println("wt.cellsUsedPersonal="+wt.cellsUsedPersonal);
|
jpayne@68
|
450 }
|
jpayne@68
|
451
|
jpayne@68
|
452 assert(!finished);
|
jpayne@68
|
453 finished=true;
|
jpayne@68
|
454 }
|
jpayne@68
|
455 }
|
jpayne@68
|
456
|
jpayne@68
|
457 private class WriteThread extends Thread{
|
jpayne@68
|
458
|
jpayne@68
|
459 public WriteThread(int tnum){
|
jpayne@68
|
460 num=tnum;
|
jpayne@68
|
461 }
|
jpayne@68
|
462
|
jpayne@68
|
463 @Override
|
jpayne@68
|
464 public void run(){
|
jpayne@68
|
465 assert(matrix[num]==null);
|
jpayne@68
|
466 array=new int[wordsPerArray]; //Makes NUMA systems use local memory.
|
jpayne@68
|
467
|
jpayne@68
|
468 matrix[num]=array;
|
jpayne@68
|
469
|
jpayne@68
|
470 long[] keys=null;
|
jpayne@68
|
471 while(!shutdown){
|
jpayne@68
|
472
|
jpayne@68
|
473 if(verbose){System.err.println(" - Reading keys for wt"+num+".");}
|
jpayne@68
|
474 while(keys==null){
|
jpayne@68
|
475 try {
|
jpayne@68
|
476 keys=writeQueue.take();
|
jpayne@68
|
477 } catch (InterruptedException e) {
|
jpayne@68
|
478 // TODO Auto-generated catch block
|
jpayne@68
|
479 e.printStackTrace();
|
jpayne@68
|
480 }
|
jpayne@68
|
481 }
|
jpayne@68
|
482 if(keys==poison){
|
jpayne@68
|
483 // assert(false);
|
jpayne@68
|
484 shutdown=true;
|
jpayne@68
|
485 }else{
|
jpayne@68
|
486 for(long key : keys){
|
jpayne@68
|
487 incrementHashedLocal(key);
|
jpayne@68
|
488 }
|
jpayne@68
|
489 }
|
jpayne@68
|
490 // System.out.println(" -- Read keys for wt"+num+". poison="+(keys==poison)+", len="+keys.length);
|
jpayne@68
|
491 if(verbose){System.err.println(" -- Read keys for wt"+num+". (success)");}
|
jpayne@68
|
492 keys=null;
|
jpayne@68
|
493 if(verbose){System.err.println("shutdown="+shutdown);}
|
jpayne@68
|
494 }
|
jpayne@68
|
495
|
jpayne@68
|
496 // System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I died: "+shutdown+", "+(keys==null)+".");
|
jpayne@68
|
497 // assert(false) : ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I died: "+shutdown+", "+(keys==null)+".";
|
jpayne@68
|
498
|
jpayne@68
|
499 array=null;
|
jpayne@68
|
500 }
|
jpayne@68
|
501
|
jpayne@68
|
502 void add(long[] keys){
|
jpayne@68
|
503 // assert(isAlive());
|
jpayne@68
|
504 assert(!shutdown);
|
jpayne@68
|
505 if(shutdown){return;}
|
jpayne@68
|
506 // assert(keys!=poison);
|
jpayne@68
|
507 if(verbose){System.err.println(" + Adding keys to wt"+num+".");}
|
jpayne@68
|
508 boolean success=false;
|
jpayne@68
|
509 while(!success){
|
jpayne@68
|
510 try {
|
jpayne@68
|
511 writeQueue.put(keys);
|
jpayne@68
|
512 success=true;
|
jpayne@68
|
513 } catch (InterruptedException e) {
|
jpayne@68
|
514 // TODO Auto-generated catch block
|
jpayne@68
|
515 e.printStackTrace();
|
jpayne@68
|
516 }
|
jpayne@68
|
517 }
|
jpayne@68
|
518 if(verbose){System.err.println(" ++ Added keys to wt"+num+". (success)");}
|
jpayne@68
|
519 }
|
jpayne@68
|
520
|
jpayne@68
|
521 private int incrementHashedLocal(long key){
|
jpayne@68
|
522 assert((key&arrayMask)==num);
|
jpayne@68
|
523 key=(key>>>arrayBits)%(cellMod);
|
jpayne@68
|
524 // key=(key>>>(arrayBits+1))%(cellMod);
|
jpayne@68
|
525 int index=(int)(key>>>indexShift);
|
jpayne@68
|
526 int word=array[index];
|
jpayne@68
|
527 int cellShift=(int)(cellBits*key);
|
jpayne@68
|
528 int value=((word>>>cellShift)&valueMask);
|
jpayne@68
|
529 if(value==0){cellsUsedPersonal++;}
|
jpayne@68
|
530 value=min(value+1, maxValue);
|
jpayne@68
|
531 word=(value<<cellShift)|(word&~((valueMask)<<cellShift));
|
jpayne@68
|
532 array[index]=word;
|
jpayne@68
|
533 return value;
|
jpayne@68
|
534 }
|
jpayne@68
|
535
|
jpayne@68
|
536 private int[] array;
|
jpayne@68
|
537 private final int num;
|
jpayne@68
|
538 public long cellsUsedPersonal=0;
|
jpayne@68
|
539
|
jpayne@68
|
540 public ArrayBlockingQueue<long[]> writeQueue=new ArrayBlockingQueue<long[]>(16);
|
jpayne@68
|
541 public boolean shutdown=false;
|
jpayne@68
|
542
|
jpayne@68
|
543 }
|
jpayne@68
|
544
|
jpayne@68
|
545
|
jpayne@68
|
546 public long cellsUsed(){return cellsUsed;}
|
jpayne@68
|
547
|
jpayne@68
|
548 private boolean finished=false;
|
jpayne@68
|
549
|
jpayne@68
|
550 private long cellsUsed;
|
jpayne@68
|
551 final int[][] matrix;
|
jpayne@68
|
552 private final WriteThread[] writers=new WriteThread[numArrays];
|
jpayne@68
|
553 private final int hashes;
|
jpayne@68
|
554 final int wordsPerArray;
|
jpayne@68
|
555 private final long cellsPerArray;
|
jpayne@68
|
556 final long cellMod;
|
jpayne@68
|
557 private final long[][] hashMasks=makeMasks(8, hashArrayLength);
|
jpayne@68
|
558
|
jpayne@68
|
559 private final long[][] buffers=new long[numArrays][500];
|
jpayne@68
|
560 private final int[] bufferlen=new int[numArrays];
|
jpayne@68
|
561
|
jpayne@68
|
562 private static final int hashBits=6;
|
jpayne@68
|
563 private static final int hashArrayLength=1<<hashBits;
|
jpayne@68
|
564 private static final int hashCellMask=hashArrayLength-1;
|
jpayne@68
|
565 static final long[] poison=new long[0];
|
jpayne@68
|
566
|
jpayne@68
|
567 private static long counter=0;
|
jpayne@68
|
568
|
jpayne@68
|
569 }
|