Commit | Line | Data |
---|---|---|
e25839d4 JA |
1 | #include <math.h> |
2 | #include <string.h> | |
e25839d4 JA |
3 | #include "zipf.h" |
4 | #include "../minmax.h" | |
ed1860cd | 5 | #include "../hash.h" |
e25839d4 | 6 | |
e6f735f0 | 7 | #define ZIPF_MAX_GEN 10000000UL |
e25839d4 JA |
8 | |
9 | static void zipf_update(struct zipf_state *zs) | |
10 | { | |
4c9060e3 | 11 | unsigned long to_gen; |
e25839d4 JA |
12 | unsigned int i; |
13 | ||
4c9060e3 JA |
14 | /* |
15 | * It can become very costly to generate long sequences. Just cap it at | |
e4839591 JA |
16 | * 10M max, that should be doable in 1-2s on even slow machines. |
17 | * Precision will take a slight hit, but nothing major. | |
4c9060e3 | 18 | */ |
761c2729 | 19 | to_gen = min(zs->nranges, (uint64_t) ZIPF_MAX_GEN); |
e25839d4 | 20 | |
4c9060e3 JA |
21 | for (i = 0; i < to_gen; i++) |
22 | zs->zetan += pow(1.0 / (double) (i + 1), zs->theta); | |
e25839d4 JA |
23 | } |
24 | ||
2316296a JA |
25 | static void shared_rand_init(struct zipf_state *zs, unsigned long nranges, |
26 | unsigned int seed) | |
b2b0b753 JA |
27 | { |
28 | memset(zs, 0, sizeof(*zs)); | |
29 | zs->nranges = nranges; | |
30 | ||
c3546b53 | 31 | init_rand_seed(&zs->rand, seed, 0); |
b2b0b753 JA |
32 | zs->rand_off = __rand(&zs->rand); |
33 | } | |
34 | ||
2316296a JA |
35 | void zipf_init(struct zipf_state *zs, unsigned long nranges, double theta, |
36 | unsigned int seed) | |
e25839d4 | 37 | { |
2316296a | 38 | shared_rand_init(zs, nranges, seed); |
e25839d4 | 39 | |
e25839d4 | 40 | zs->theta = theta; |
1442ba18 | 41 | zs->zeta2 = pow(1.0, zs->theta) + pow(0.5, zs->theta); |
e25839d4 | 42 | |
4c9060e3 | 43 | zipf_update(zs); |
e25839d4 JA |
44 | } |
45 | ||
46 | unsigned long long zipf_next(struct zipf_state *zs) | |
47 | { | |
e25839d4 JA |
48 | double alpha, eta, rand_uni, rand_z; |
49 | unsigned long long n = zs->nranges; | |
50 | unsigned long long val; | |
51 | ||
52 | alpha = 1.0 / (1.0 - zs->theta); | |
53 | eta = (1.0 - pow(2.0 / n, 1.0 - zs->theta)) / (1.0 - zs->zeta2 / zs->zetan); | |
54 | ||
c3546b53 | 55 | rand_uni = (double) __rand(&zs->rand) / (double) FRAND32_MAX; |
e25839d4 JA |
56 | rand_z = rand_uni * zs->zetan; |
57 | ||
58 | if (rand_z < 1.0) | |
59 | val = 1; | |
60 | else if (rand_z < (1.0 + pow(0.5, zs->theta))) | |
61 | val = 2; | |
62 | else | |
63 | val = 1 + (unsigned long long)(n * pow(eta*rand_uni - eta + 1.0, alpha)); | |
64 | ||
8c5e96a4 JA |
65 | val--; |
66 | ||
8348daf9 | 67 | if (!zs->disable_hash) |
8c5e96a4 | 68 | val = __hash_u64(val); |
8348daf9 | 69 | |
8c5e96a4 | 70 | return (val + zs->rand_off) % zs->nranges; |
e25839d4 | 71 | } |
925fee33 | 72 | |
2316296a JA |
73 | void pareto_init(struct zipf_state *zs, unsigned long nranges, double h, |
74 | unsigned int seed) | |
925fee33 | 75 | { |
2316296a | 76 | shared_rand_init(zs, nranges, seed); |
925fee33 | 77 | zs->pareto_pow = log(h) / log(1.0 - h); |
925fee33 JA |
78 | } |
79 | ||
80 | unsigned long long pareto_next(struct zipf_state *zs) | |
81 | { | |
c3546b53 | 82 | double rand = (double) __rand(&zs->rand) / (double) FRAND32_MAX; |
8c5e96a4 JA |
83 | unsigned long long n; |
84 | ||
85 | n = (zs->nranges - 1) * pow(rand, zs->pareto_pow); | |
925fee33 | 86 | |
8348daf9 | 87 | if (!zs->disable_hash) |
8c5e96a4 | 88 | n = __hash_u64(n); |
8348daf9 | 89 | |
8c5e96a4 | 90 | return (n + zs->rand_off) % zs->nranges; |
8348daf9 JA |
91 | } |
92 | ||
93 | void zipf_disable_hash(struct zipf_state *zs) | |
94 | { | |
95 | zs->disable_hash = true; | |
925fee33 | 96 | } |