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