-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproba.c
181 lines (165 loc) · 5.85 KB
/
proba.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2018 Alexey Tourbin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#define FPSET_PROBA
#include "fpset.c"
// CPU intrinsics recognized by gcc.
static inline uint64_t rotl64(uint64_t x, int r) { return (x << r) | (x >> (64 - r)); }
static inline uint64_t rotr64(uint64_t x, int r) { return (x >> r) | (x << (64 - r)); }
// The PRNG state is initialized with the first outputs of splitmix64
// seeded with zero (much like suggested in xoroshiro128plus.c).
static uint64_t prngState = 0xe220a8397b1dcdaf;
#include <string.h>
#include <sys/auxv.h>
static void randomizePrng(void)
{
void *auxrnd = (void *) getauxval(AT_RANDOM);
assert(auxrnd);
memcpy(&prngState, auxrnd, sizeof prngState);
}
// A fast LCG-based PRNG, medium quality. Just happens to be good enough
// for the job. Another reason for using this generator is that its state
// is only a single 64-bit variable, and so it yields unique 64-bit numbers.
// This makes it possible to disable dup detection during simulations.
static inline uint64_t rnd(void)
{
// Use the previous state as the basis for the return value, to improve
// instruction-level parallelism. Rotate the worst 12 bits away into
// the higher half. Would like to rotate even more, possibly by 16 bits,
// but need to keep at least 20 good bits in the higher half to address
// up to 1M buckets.
uint64_t ret = rotr64(prngState, 12);
// The state is updated in parallel even as the caller makes use of "ret"
// to index into the buckets. Constants are Knuth's.
prngState = prngState * 0x5851f42d4c957f2d + 0x14057b7ef767814f;
return ret;
}
// A simplified fpset_add() clone, returns the number of kicks.
static inline int proba_add(struct fpset *set, uint64_t fp)
{
dFP2IB;
// No dups during simulations, because of full-period LCG.
if (justAdd(fp, b1, i1, b2, i2))
return set->cnt++, 0;
return kickAdd(set, fp, b1, i1, &fp);
}
// Reset fpset structure to the initial state.
static inline void proba_clear(struct fpset *set)
{
set->cnt = 0;
memset(set->bb, 0, (set->mask + 1) * sizeof *set->bb);
for (int n = 0; n < FPSET_BUCKETSIZE; n++)
set->bb[0][n] = -1;
}
#include <stdio.h>
#include "qsort.h"
// Estimate the fill factor achievable 99% of the time.
static void fillfactor(void)
{
int logfix = FPSET_BUCKETSIZE > 2;
for (int logsize = 4 + logfix; logsize <= 16 + logfix; logsize++) {
int tries[4000];
for (int i = 0; i < 4000; i++) {
struct fpset *set = fpset_new(logsize);
int added = 0;
while (proba_add(set, rnd()) >= 0)
added++;
tries[i] = added;
fpset_free(set);
}
int try1;
#define tryLess(i, j) tries[i] < tries[j]
#define trySwap(i, j) try1 = tries[i], tries[i] = tries[j], tries[j] = try1
QSORT(4000, tryLess, trySwap);
size_t i = sizeof(tries)/sizeof(int)/100; // 100 for q=1%, 4 for q=25%
double q = (tries[i-1] + tries[i-2] + tries[i-3] + tries[i-4] +
tries[i+0] + tries[i+1] + tries[i+2] + tries[i+3]) / 8.0;
int bucketlog = logsize - logfix;
int slots = (1 << bucketlog) * FPSET_BUCKETSIZE;
printf("bucketlog=%d\tslots=%d\tq=%.1f\tfillfactor=%.3f\n",
bucketlog, slots, q, q / slots);
}
}
static void failurerate(void)
{
int logfix = FPSET_BUCKETSIZE > 2;
int maxbucket = FPSET_BUCKETSIZE == 4 ? 7 :
FPSET_BUCKETSIZE == 3 ? 9 : 12;
for (int logsize = 4 + logfix, bucketlog = logsize - logfix;
bucketlog <= maxbucket; logsize++, bucketlog++) {
int slots = (1 << bucketlog) * FPSET_BUCKETSIZE;
int failures = 0;
uint64_t tries = 0;
struct fpset *set = fpset_new(logsize);
do {
for (int i = 0; i < (1<<20); i++) {
proba_clear(set);
int j = 0;
while (j < slots/2) {
if (proba_add(set, rnd()) < 0) break;
if (proba_add(set, rnd()) < 0) break;
j += 2;
}
if (j < slots/2) {
failures++;
putc('.', stderr);
}
tries++;
}
/*
* See "Estimating Bernoulli trial probability from a small sample".
* The expected probability is E=(m+1)/(n+2), and 17 failures
* are required for the upper bound (3) not to exceed the real
* probability by a factor of more than 1.5 with 90% confidence.
*
> library(zipfR)
> C=.90; m=17; n=17e6
> Rbeta.inv((1+C)/2, m+1, n-m+1)
[1] 1.499954e-06
*/
} while (failures < 17);
fpset_free(set);
putc('\n', stderr);
printf("%d\t%.1e\n", bucketlog, (failures + 1.0) / (tries + 2));
}
}
#include <getopt.h>
enum {
OPT_RANDOMIZE = 256,
};
static const struct option longopts[] = {
{ "randomize", no_argument, NULL, OPT_RANDOMIZE },
};
int main(int argc, char **argv)
{
int c;
while ((c = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
switch (c) {
case 0:
break;
case OPT_RANDOMIZE:
randomizePrng();
break;
default:
fprintf(stderr, "Usage: %s [OPTIONS...] [ARGS...]\n", argv[0]);
return 1;
}
failurerate();
return 0;
}