-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.c
478 lines (412 loc) · 12 KB
/
stats.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2020 Marcelo Diop-Gonzalez
#include <errno.h>
#include <glib.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_roots.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "stats.h"
struct bucket {
long *left_edges;
long *right_edges;
int count;
float race_probability;
};
struct learning_sampler {
int num_params;
long *params;
struct bucket *buckets;
GTree *ordered_buckets;
struct bucket *current_bucket;
float explore_probability;
int found_something;
};
static int bucket_cmp(const void *a, const void *b) {
const struct bucket *bucket_a = a;
const struct bucket *bucket_b = b;
if (bucket_a->race_probability > bucket_b->race_probability)
return -1;
if (bucket_a->race_probability < bucket_b->race_probability)
return 1;
if (bucket_a > bucket_b)
return -1;
if (bucket_a < bucket_b)
return 1;
return 0;
}
static void random_point(int n, long *left_edges, long *right_edges, long *dst) {
for (int i = 0; i < n; i++) {
dst[i] = left_edges[i] + random() % (right_edges[i] - left_edges[i]);
}
}
static void set_current_bucket(struct sampler *s, struct bucket *b) {
struct learning_sampler *ls = s->private;
ls->current_bucket = b;
random_point(s->num_params, b->left_edges, b->right_edges, ls->params);
}
struct top_n_arg {
int idx;
struct bucket *bucket;
};
// Take a random bucket from among the top n rather
// than just the top one, because the top bucket in this tree
// is the top with respect the measured number of times that
// "triggered_by" happens between "opened_by" and "closed_by".
// This is only a proxy for what we really want (triggering the
// real race), so we could be stuck hammering away at a bucket
// that isn't the "true" optimal one if the config gives a wide window.
// would be good to do something smarter than just the top 10...
static int tree_top_n(void *k, void *v, void *p) {
struct top_n_arg *arg = p;
struct bucket *b = k;
if (b->race_probability < 0.0001 && arg->bucket)
return 1;
arg->bucket = b;
if (arg->idx == 0)
return 1;
arg->idx--;
return 0;
}
static struct bucket *random_top_bucket(GTree *tree) {
struct top_n_arg arg;
int size = g_tree_nnodes(tree);
int n = size < 10 ? size : 10;
arg.idx = random() % n;
arg.bucket = NULL;
g_tree_foreach(tree, tree_top_n, &arg);
return arg.bucket;
}
static long *learning_next_params(struct sampler *s) {
struct learning_sampler *ls = s->private;
if (ls->found_something &&
(float)random() / (float) RAND_MAX > ls->explore_probability) {
set_current_bucket(s, random_top_bucket(ls->ordered_buckets));
return ls->params;
}
int idx = random() % g_tree_nnodes(ls->ordered_buckets);
set_current_bucket(s, &ls->buckets[idx]);
return ls->params;
}
static void learning_report(struct sampler *s, int count, int triggers) {
if (count < 1)
return;
struct learning_sampler *ls = s->private;
if (triggers > 0)
ls->found_something = 1;
float p = (float)triggers / (float)count;
struct bucket *b = ls->current_bucket;
g_tree_steal(ls->ordered_buckets, b);
b->race_probability += ((p - b->race_probability) *
(float)count / (float)(count + b->count));
b->count += count;
g_tree_insert(ls->ordered_buckets, b, NULL);
}
static void rand_init(void) {
int n, seed;
FILE *f = fopen("/dev/urandom", "r");
if (!f) {
fprintf(stderr, "not seeding RNG. opening /dev/urandom: %m\n");
return;
}
n = fread(&seed, sizeof(seed), 1, f);
if (fclose(f) || n != 1) {
fprintf(stderr, "not seeding RNG. reading from /dev/urandom: %s\n", strerror(ferror(f)));
return;
}
srandom(seed);
}
static void free_learning_sampler(struct sampler *s) {
struct learning_sampler *ls = s->private;
for (int i = 0; i < g_tree_nnodes(ls->ordered_buckets); i++) {
struct bucket *b = &ls->buckets[i];
free(b->left_edges);
free(b->right_edges);
}
free(ls->buckets);
g_tree_unref(ls->ordered_buckets);
free(ls->params);
free(ls);
free(s);
}
static int get_param_boundaries(int num_dimensions, long *durations,
long **left_edges, long **right_edges) {
*left_edges = malloc(sizeof(long) * num_dimensions);
if (!*left_edges) {
fprintf(stderr, "%s out of memory\n", __func__);
return -1;
}
*right_edges = malloc(sizeof(long) * num_dimensions);
if (!*right_edges) {
fprintf(stderr, "%s out of memory\n", __func__);
return -1;
}
for (int i = 0; i < num_dimensions; i++) {
(*left_edges)[i] = 0;
(*right_edges)[i] = 0;
for (int j = 0; j < num_dimensions; j++) {
(*left_edges)[i] -= durations[j];
if (i != j)
(*right_edges)[i] += durations[j];
}
(*right_edges)[i] += durations[num_dimensions];
}
return 0;
}
static struct sampler *alloc_sampler(int num_params, long *(*next_params)(struct sampler *),
void (*destroy)(struct sampler *), void (*report)(struct sampler *, int, int),
void *private) {
rand_init();
struct sampler *sampler = malloc(sizeof(*sampler));
if (!sampler)
return NULL;
sampler->num_params = num_params;
sampler->next_params = next_params;
sampler->destroy = destroy;
sampler->report = report;
sampler->private = private;
return sampler;
}
struct polynomial_params {
int exp;
double c;
};
// to find nth root of Y, find positive real root of p(x) = x^n - Y
static double polynomial(double x, void *params) {
const struct polynomial_params *p = params;
double ret = x;
for (int i = 0; i < p->exp-1; i++)
ret *= x;
return ret - p->c;
}
static double dpolynomial(double x, void *params) {
const struct polynomial_params *p = params;
double ret = p->exp;
for (int i = 0; i < p->exp-1; i++)
ret *= x;
return ret;
}
static void fdfpolynomial(double x, void *params, double *f, double *df) {
const struct polynomial_params *p = params;
double y = x;
for (int i = 0; i < p->exp-2; i++)
y *= x;
*df = y * p->exp;
*f = y * x - p->c;
}
static long nth_root(int n, long x) {
if (n == 1 || x == 1)
return x;
gsl_root_fdfsolver *s = gsl_root_fdfsolver_alloc(gsl_root_fdfsolver_newton);
if (!s)
return -ENOMEM;
struct polynomial_params p = {
.exp = n,
.c = x,
};
gsl_function_fdf fdf = {
.f = polynomial,
.df = dpolynomial,
.fdf = fdfpolynomial,
.params = &p,
};
double root, root_old = p.c/5+1;
gsl_root_fdfsolver_set(s, &fdf, root_old);
int i;
for (i = 0; i < 10000; i++) {
int status = gsl_root_fdfsolver_iterate(s);
if (status != GSL_SUCCESS) {
fprintf(stderr, "math error: %s\n", gsl_strerror(status));
gsl_root_fdfsolver_free(s);
return -1;
}
root = gsl_root_fdfsolver_root(s);
status = gsl_root_test_delta(root, root_old, 0.1, 0);
if (status == GSL_SUCCESS)
break;
if (status != GSL_CONTINUE) {
fprintf(stderr, "math error: %s\n", gsl_strerror(status));
gsl_root_fdfsolver_free(s);
return -1;
}
root_old = root;
}
if (i == 10000) {
// should not happen...
fprintf(stderr, "math error: couldn't find nth root quickly enough. Something is wrong\n");
gsl_root_fdfsolver_free(s);
return -1;
}
gsl_root_fdfsolver_free(s);
return root;
}
static int get_bucket_shape(int num_dimensions, const long *left_edges, const long *right_edges,
int *num_buckets, long *edge_length, int *dimension_num_buckets) {
// TODO: instead of hardcoding this totally arbitrary value,
// would be good to start with a small number of big buckets
// and split the good ones into smaller ones as we go along
#define MAX_BUCKETS 100000
long bucket_volume = 1;
for (int i = 0; i < num_dimensions; i++) {
long x = bucket_volume * (right_edges[i] - left_edges[i]);
if (bucket_volume != x / (right_edges[i] - left_edges[i])) {
// TODO: handle this. unless the above TODO gets done, in which case it doesnt matter
fprintf(stderr, "Multiplication overflow. Too many k_race_targets given\n");
return -1;
}
bucket_volume = x;
}
bucket_volume /= MAX_BUCKETS;
bucket_volume++;
*edge_length = nth_root(num_dimensions, bucket_volume);
if (*edge_length < 0)
return -*edge_length;
if (*edge_length < 100)
*edge_length = 100;
*num_buckets = 1;
for (int i = 0; i < num_dimensions; i++) {
// round up division
dimension_num_buckets[i] = (right_edges[i] - left_edges[i] + *edge_length - 1) / *edge_length;
*num_buckets *= dimension_num_buckets[i];
}
return 0;
}
// splits the possible params into different buckets, and then treats
// the problem like a multi armed bandit
struct sampler *alloc_learning_sampler(int num_funcs, long *durations,
float explore_probability) {
struct learning_sampler *ls = malloc(sizeof(*ls));
if (!ls)
return NULL;
int err = ENOMEM;
int num_dimensions = num_funcs - 1;
ls->explore_probability = explore_probability;
ls->found_something = 0;
long *left_edges, *right_edges;
if (get_param_boundaries(num_dimensions, durations,
&left_edges, &right_edges))
goto out_free_ls;
int *dimension_num_buckets = malloc(sizeof(int) * num_dimensions);
if (!dimension_num_buckets) {
free(left_edges);
free(right_edges);
goto out_free_ls;
}
long edge_length;
int num_buckets;
err = get_bucket_shape(num_dimensions, left_edges, right_edges,
&num_buckets, &edge_length, dimension_num_buckets);
if (err) {
free(dimension_num_buckets);
free(left_edges);
free(right_edges);
goto out_free_ls;
}
ls->buckets = malloc(sizeof(struct bucket) * num_buckets);
if (!ls->buckets) {
free(dimension_num_buckets);
free(left_edges);
free(right_edges);
goto out_free_ls;
}
memset(ls->buckets, 0, sizeof(struct bucket) * num_buckets);
ls->ordered_buckets = g_tree_new(bucket_cmp);
ls->params = malloc(sizeof(long) * num_dimensions);
if (!ls->params) {
free(dimension_num_buckets);
free(left_edges);
free(right_edges);
goto out_free_tree;
}
for (int i = 0; i < num_buckets; i++) {
struct bucket *b = &ls->buckets[i];
b->left_edges = malloc(sizeof(long) * num_dimensions);
b->right_edges = malloc(sizeof(long) * num_dimensions);
if (!b->left_edges || !b->right_edges) {
free(left_edges);
free(right_edges);
free(dimension_num_buckets);
goto out_free_buckets;
}
int q = 1;
for (int j = 0; j < num_dimensions; j++) {
int idx = i / q % dimension_num_buckets[j];
b->left_edges[j] = left_edges[j] + edge_length * idx;
b->right_edges[j] = b->left_edges[j] + edge_length;
q *= dimension_num_buckets[j];
}
g_tree_insert(ls->ordered_buckets, b, NULL);
}
free(dimension_num_buckets);
free(left_edges);
free(right_edges);
struct sampler *s = alloc_sampler(num_dimensions, learning_next_params,
free_learning_sampler, learning_report, ls);
if (!s)
goto out_free_buckets;
return s;
out_free_buckets:
for (int i = 0; i < num_buckets; i++) {
struct bucket *b = &ls->buckets[i];
if (b->left_edges)
free(b->left_edges);
if (b->right_edges)
free(b->right_edges);
}
free(ls->params);
out_free_tree:
g_tree_unref(ls->ordered_buckets);
free(ls->buckets);
out_free_ls:
free(ls);
if (err == ENOMEM)
fprintf(stderr, "%s: OOM\n", __func__);
return NULL;
}
struct random_sampler {
long *left_edges;
long *right_edges;
long *params;
};
static long *random_next_params(struct sampler *s) {
struct random_sampler *rs = s->private;
random_point(s->num_params, rs->left_edges,
rs->right_edges, rs->params);
return rs->params;
}
static void random_report(struct sampler *s, int foo, int bar) {}
static void random_destroy(struct sampler *s) {
struct random_sampler *rs = s->private;
free(rs->params);
free(rs->left_edges);
free(rs->right_edges);
free(rs);
free(s);
}
struct sampler *alloc_random_sampler(int num_funcs, long *durations) {
struct random_sampler *rs = malloc(sizeof(*rs));
if (!rs)
return NULL;
int num_dimensions = num_funcs - 1;
if (get_param_boundaries(num_dimensions, durations,
&rs->left_edges, &rs->right_edges)) {
free(rs);
return NULL;
}
rs->params = malloc(num_dimensions * sizeof(long));
if (!rs->params)
goto free_edges;
struct sampler *s = alloc_sampler(num_dimensions, random_next_params,
random_destroy, random_report, rs);
if (!s)
goto free_params;
return s;
free_params:
free(rs->params);
free_edges:
free(rs->left_edges);
free(rs->right_edges);
free(rs);
return NULL;
}