Debian Codegraph @

current/cardinality/LogLog2.java

raw symbols SCIP
1 package cardinality;
2
3 import java.util.concurrent.atomic.AtomicIntegerArray;
4
5 import shared.Parser;
6 import shared.Tools;
7 import structures.LongList;
8
9 /**
10 * @author Brian Bushnell
11 * @date Feb 20, 2020
12 *
13 */
14 public final class LogLog2 extends CardinalityTracker {
15
16 /*--------------------------------------------------------------*/
17 /*---------------- Initialization ----------------*/
18 /*--------------------------------------------------------------*/
19
20 /** Create a LogLog with default parameters */
21 LogLog2(){
22 this(2048, 31, -1, 0);
23 }
24
25 /** Create a LogLog with parsed parameters */
26 LogLog2(Parser p){
27 super(p);
28 //assert(atomic);
29 maxArrayA=(atomic ? new AtomicIntegerArray(buckets) : null);
30 maxArray=(atomic ? null : new int[buckets]);
31 }
32
33 /**
34 * Create a LogLog with specified parameters
35 * @param buckets_ Number of buckets (counters)
36 * @param k_ Kmer length
37 * @param seed Random number generator seed; -1 for a random seed
38 * @param minProb_ Ignore kmers with under this probability of being correct
39 */
40 LogLog2(int buckets_, int k_, long seed, float minProb_){
41 super(buckets_, k_, seed, minProb_);
42 //assert(atomic);
43 maxArrayA=(atomic ? new AtomicIntegerArray(buckets) : null);
44 maxArray=(atomic ? null : new int[buckets]);
45 }
46
47 /*--------------------------------------------------------------*/
48 /*---------------- Methods ----------------*/
49 /*--------------------------------------------------------------*/
50
51 //Restores floating point to integer
52 private long restore(int score){
53 long lowbits=(~score)&mask;
54 int leading=(int)(score>>>mantissabits);
55 long mantissa=(1L<<mantissabits)|lowbits;
56 int shift=wordlen-leading-mantissabits-1;
57 long original=mantissa<<shift;
58 return original;
59 }
60
61 @Override
62 public final long cardinality(){
63 double difSum=0;
64 double hSum=0;
65 double gSum=0;
66 double rSum=0;
67 double estLogSum=0;
68 int count=0;
69 LongList list=new LongList(buckets);
70 //assert(atomic);
71 if(atomic){
72 for(int i=0; i<maxArrayA.length(); i++){
73 int max=maxArrayA.get(i);
74 long val=restore(max);
75 if(max>0 && val>0){
76 // long val=restore(max);
77 // System.err.println("val="+val);
78 long dif=val;
79 difSum+=dif;
80 hSum+=1.0/Tools.max(1, dif);
81 gSum+=Math.log(Tools.max(1, dif));
82 rSum+=Math.sqrt(dif);
83 count++;
84 double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD;
85 estLogSum+=Math.log(est);
86 list.add(dif);
87 }
88 }
89 }else{
90 for(int i=0; i<maxArray.length; i++){
91 int max=maxArray[i];
92 long val=restore(max);
93 if(max>0 && val>0){
94 // long val=restore(max);
95 long dif=val;
96 difSum+=dif;
97 hSum+=1.0/Tools.max(1, dif);
98 gSum+=Math.log(Tools.max(1, dif));
99 rSum+=Math.sqrt(dif);
100 count++;
101 double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD;
102 estLogSum+=Math.log(est);
103 list.add(dif);
104 }
105 }
106 }
107 final int div=Tools.max(count, 1);//Could be count or buckets but one causes problems
108 final double mean=difSum/div;
109 double hmean=hSum/div;
110 double gmean=gSum/div;
111 double rmean=rSum/div;
112 hmean=1.0/hmean;
113 gmean=Math.exp(gmean);
114 rmean=rmean*rmean;
115 list.sort();
116 final long median=list.median();
117 final double mwa=list.medianWeightedAverage();
118
119 //What to use as the value from the counters
120 final double proxy=(USE_MEAN ? mean : USE_MEDIAN ? median : USE_MWA ? mwa : USE_HMEAN ? hmean : USE_GMEAN ? gmean : mean);
121
122 final double estimatePerSet=2*(Long.MAX_VALUE/proxy)*SKIPMOD;
123 final double total=estimatePerSet*div*((count+buckets)/(float)(buckets+buckets));
124
125 final double estSum=div*Math.exp(estLogSum/(Tools.max(div, 1)));
126 double medianEst=2*(Long.MAX_VALUE/(double)median)*SKIPMOD*div;
127
128 // new Exception().printStackTrace();
129
130 // System.err.println(maxArray);
131 //// Overall, it looks like "total" is the best, then "estSum", then "medianEst" is the worst, in terms of variance.
132 // System.err.println("difSum="+difSum+", count="+count+", mean="+mean+", est="+estimatePerSet+", total="+(long)total);
133 // System.err.println("estSum="+(long)estSum+", median="+median+", medianEst="+(long)medianEst);
134
135 long cardinality=(long)(total);
136 lastCardinality=cardinality;
137 return cardinality;
138 }
139
140 // @Override
141 // public final long cardinality(){
142 // long sum=0;
143 // //assert(atomic);
144 // if(atomic){
145 // for(int i=0; i<maxArray.length(); i++){
146 // sum+=maxArray.get(i);
147 // }
148 // }else{
149 // for(int i=0; i<maxArray2.length; i++){
150 // sum+=maxArray2[i];
151 // }
152 // }
153 // double mean=sum/((1<<mantissabits)*(double)buckets);
154 // double correction=0.56326183361037098678934414274035;//0.56403894240204307426602541326855;
155 // //Better: //0.56326183361037098678934414274035
156 // long cardinality=(long)((((Math.pow(2, mean)-1)*buckets*SKIPMOD))*correction);
157 // lastCardinality=cardinality;
158 // return cardinality;
159 // }
160
161 public final long cardinalityH(){
162 double sum=0;
163 for(int i=0; i<maxArrayA.length(); i++){
164 int x=Tools.max(1, maxArrayA.get(i));
165 sum+=1.0/x;
166 }
167 double mean=buckets/sum;
168 return (long)((Math.pow(2, mean)*buckets*SKIPMOD));
169 }
170
171 @Override
172 public final void add(CardinalityTracker log){
173 assert(log.getClass()==this.getClass());
174 add((LogLog2)log);
175 }
176
177 public void add(LogLog2 log){
178 if(atomic && maxArrayA!=log.maxArrayA){
179 for(int i=0; i<buckets; i++){
180 maxArrayA.set(i, Tools.max(maxArrayA.get(i), log.maxArrayA.get(i)));
181 }
182 }else if(maxArray!=log.maxArray){
183 for(int i=0; i<buckets; i++){
184 maxArray[i]=Tools.max(maxArray[i], log.maxArray[i]);
185 }
186 }
187 }
188
189 @Override
190 public void hashAndStore(final long number){
191 // if(number%SKIPMOD!=0){return;} //Slows down moderately
192 long key=number;
193
194 // key=hash(key, tables[((int)number)&numTablesMask]);
195
196 key=Tools.hash64shift(key);
197 // if(key<0 || key>maxHashedValue){return;}//Slows things down by 50% lot, mysteriously
198 int leading=Long.numberOfLeadingZeros(key);
199
200 // counts[leading]++;
201
202 // if(leading<3){return;}//Speeds up by 20%, even more at 4. Slows at 2.
203
204 int shift=wordlen-leading-mantissabits-1;
205
206 int score=(leading<<mantissabits)+(int)((~(key>>>shift))&mask);
207 // assert(false) : "\n"+Long.toBinaryString(key)+", leading="+leading+", shift="+shift+"\n"+Long.toBinaryString(score);
208
209 //+"\n"+score+"\n"+restore(score);
210
211 // final int bucket=(int)((number&Integer.MAX_VALUE)%buckets);
212 final int bucket=(int)(key&bucketMask);
213
214 if(atomic){
215 int x=maxArrayA.get(bucket);
216 while(score>x){
217 // System.err.println("\n"+Long.toBinaryString(key)+", leading="+leading+", score="+score+", x="+x+"\n"+Long.toBinaryString(score));
218 // System.err.println("\n"+Long.toBinaryString(restore(score)));
219 boolean b=maxArrayA.compareAndSet(bucket, x, score);
220 if(b){x=score;}
221 else{x=maxArrayA.get(bucket);}
222 // assert(leading<9);
223 }
224 }else{
225 maxArray[bucket]=Tools.max(score, maxArray[bucket]);
226 }
227 }
228
229 @Override
230 public final float[] compensationFactorLogBucketsArray(){
231 return null;
232 }
233
234 /*--------------------------------------------------------------*/
235 /*---------------- Fields ----------------*/
236 /*--------------------------------------------------------------*/
237
238 /** Maintains state. These are the actual buckets. */
239 private final int[] maxArray;
240 /** Atomic version of maxArray. */
241 private final AtomicIntegerArray maxArrayA;
242
243 /*--------------------------------------------------------------*/
244 /*---------------- Statics ----------------*/
245 /*--------------------------------------------------------------*/
246
247 public static void setMantissaBits(int x){
248 assert(x>=0 && x<25);
249 assert(x+6<32);
250 mantissabits=x;
251 mask=(1<<mantissabits)-1;
252 }
253
254 private static final int wordlen=64;
255 /** Precision or mantissa bits.
256 * This should not be changed. As long as it is >10 the result will be accurate.
257 * At low values like 2 the cardinality estimate becomes too high due to a loss of precision,
258 * and would need a fixed multiplier.
259 */
260 private static int mantissabits=20;
261 private static int mask=(1<<mantissabits)-1;
262 // private static final int shift=wordlen-leading-mantissabits-1;
263
264 }