diff --git a/examples/01_one_sample/example b/examples/01_one_sample/example index b3db352..0be17e7 100755 Binary files a/examples/01_one_sample/example and b/examples/01_one_sample/example differ diff --git a/examples/02_many_samples/example b/examples/02_many_samples/example index b3db352..02502b3 100755 Binary files a/examples/02_many_samples/example and b/examples/02_many_samples/example differ diff --git a/examples/03_gcc_nested_function/example b/examples/03_gcc_nested_function/example new file mode 100755 index 0000000..1935e2d Binary files /dev/null and b/examples/03_gcc_nested_function/example differ diff --git a/examples/03_gcc_nested_function/example.c b/examples/03_gcc_nested_function/example.c new file mode 100644 index 0000000..bc862f7 --- /dev/null +++ b/examples/03_gcc_nested_function/example.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include "../../squiggle.h" + +int main(){ + // set randomness seed + uint32_t* seed = malloc(sizeof(uint32_t)); + *seed = 1000; // xorshift can't start with 0 + + float p_a = 0.8; + float p_b = 0.5; + float p_c = p_a * p_b; + + int n_dists = 4; + + float sample_0(uint32_t* seed){ return 0; } + float sample_1(uint32_t* seed) { return 1; } + float sample_few(uint32_t* seed){ return random_to(1, 3, seed); } + float sample_many(uint32_t* seed){ return random_to(2, 10, seed); } + + float (*samplers[])(uint32_t*) = { sample_0, sample_1, sample_few, sample_many }; + float weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 }; + + int n_samples = 1000000; + float* result_many = (float *) malloc(n_samples * sizeof(float)); + for(int i=0; i&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 1 thread: |" | sed 's|$$|ms|' && echo + +## Profiling + +profile-linux: + echo "Requires perf, which depends on the kernel version, and might be in linux-tools package or similar" + echo "Must be run as sudo" + $(CC) $(SRC) $(MATH) -o $(OUTPUT) + sudo perf record $(OUTPUT) + sudo perf report + rm perf.data diff --git a/squiggle.h b/squiggle.h index 5409e01..d887b21 100644 --- a/squiggle.h +++ b/squiggle.h @@ -95,14 +95,17 @@ float mixture(float (*samplers[])(uint32_t*), float* weights, int n_dists, uint3 cumsummed_normalized_weights[i] = cumsummed_normalized_weights[i - 1] + weights[i]/sum_weights; } - float p = random_uniform(0, 1, seed); float result; + int result_set_flag = 0; + float p = random_uniform(0, 1, seed); for (int k = 0; k < n_dists; k++) { if (p < cumsummed_normalized_weights[k]) { result = samplers[k](seed); + result_set_flag = 1; break; } } + if(result_set_flag == 0) result = samplers[n_dists-1](seed); free(cumsummed_normalized_weights); return result; diff --git a/to-do.md b/to-do.md index 3be7ffb..1f32078 100644 --- a/to-do.md +++ b/to-do.md @@ -1,9 +1,10 @@ -- [ ] Add example for only one sample -- [ ] Add example for many samples +- [x] Add example for only one sample +- [x] Add example for many samples - [ ] Use gcc extension to define functions nested inside main. - [ ] Use OpenMP for acceleration - [ ] Chain various mixture functions - [ ] Have some more complicated & realistic example - [ ] Add summarization functions, like mean, std, 90% ci (or all c.i.?) - [ ] Add beta distribution +- [ ] Add a custom preprocessor to allow simple nested functions that don't rely on local scope?