commit f458ff80041e3975216cc7eea03d37f2c9b81a30 parent 4f32ccbd21a50bc09081ddac9653e61bb2746613 Author: NunoSempere <nuno.sempere@protonmail.com> Date: Sun, 19 Nov 2023 19:27:22 +0000 add small squiggle_c makefile option, give some thoughts on squiggle(py) Diffstat:
25 files changed, 28 insertions(+), 51 deletions(-)
diff --git a/README.md b/README.md @@ -77,17 +77,23 @@ Although the above paragraphs were written in the first person, the C code was w ### squiggle.c -squiggle.c is a minimalistic library focused on understandability and being self-contained. It grew from the initial C code in this repository. You can see the code for the library [here](https://git.nunosempere.com/personal/squiggle.c), and the code for the example we are discussing [here](https://git.nunosempere.com/personal/squiggle.c/src/branch/master/examples/02_many_samples_time_to_botec). +squiggle.c is a minimalistic library focused on understandability and being self-contained. I've put a bunch of thought into how to design this in a way which is clean and fast. It grew from the initial C code in this repository. You can see the code for the library [here](https://git.nunosempere.com/personal/squiggle.c), which contains a thoughful README which I recommend people doing Monte Carlo estimation stuff read. + +I like the [operator](http://duskos.org/#operator) section of [Dusk OS](http://duskos.org/): + +> Dusk OS doesn't have users, but operators. What's the difference? Control. You use a phone, you use a coffee machine, hell you even use a car these days. But you operate a bulldozer, you operate a crane, you operate a plane. ### NodeJS and Squiggle -Using [bun](https://bun.sh/) instead of node is actually a bit slower. Also, both the NodeJS and the Squiggle code use [stdlib](https://stdlib.io/) in their innards, which has a bunch of interleaved functions that make the code slower. It's possible that not using that external library could make the code faster. But at the same time, the js approach does seem to be to use external libraries whenever possible. +Using [bun](https://bun.sh/) instead of node is actually a bit slower. Also, both the NodeJS and the Squiggle code use [stdlib](https://stdlib.io/) in their innards, which has a bunch of interleaved functions that make the code slower. It's possible that not using that external library could make the code faster. But at the same time, the js approach does seem to be to use external libraries whenever possible. + +I wasn't particularly sure that the Squiggle code was actually producing 1M samples, so I applied a [monkey patch](https://git.nunosempere.com/personal/time-to-botec/src/branch/master/squiggle/makefile#L14) to ensure this. In general, Squiggle tries to present a simple interface to the user, leading to "hiding the magic" and having a bunch of [bugs](https://github.com/quantified-uncertainty/squiggle/labels/Bug), whereas I think the right tradeoff for me is to have some simple interface that I can operate skillfully (i.e., squiggle.c). -I am not particularly sure that the Squiggle code is actually producing 1M samples, but I am also not in a rush to debug this. +### Python and Squigglepy -### Python +For the Python code, it's possible that the lack of speed is more a function of me not being as familiar with Python. It's also very possible that the code would run faster with [PyPy](https://doc.pypy.org). -For the Python code, it's possible that the lack of speed is more a function of me not being as familiar with Python. It's also very possible that the code would run faster with [PyPy](https://doc.pypy.org). +In terms of complexity, SquigglePy seems to be between squiggle.c and the original squiggle. Like the original suqiggle, it also hides its stuff behind semi-magic wrappers, leading to e.g. ambiguities like around [correlated samples](https://git.nunosempere.com/personal/squiggle.c#correlated-samples) and generally having moving pieces that I don't understand. On the other hand, the code *is* short enough so that one person could read it in a few afternoons and roughly understand it. In terms of speed, SquigglePy seems slow. ### R diff --git a/squiggle.c/makefile b/squiggle.c/makefile @@ -14,3 +14,14 @@ build: time-linux: @echo "Running 100x and taking avg time: OMP_NUM_THREADS=16 $(OUTPUT)" @t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do OMP_NUM_THREADS=16 $(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 16 threads: |" | sed 's|$$|ms|' && echo + +install-small: + rm -r squiggle_c + git clone https://git.nunosempere.com/personal/squiggle.c + mv squiggle.c squiggle_c + sudo rm -r squiggle_c/.git + cp -r squiggle_c/examples/core/02_time_to_botec/example.c samples.c + sed -i 's|../../..|squiggle_c|' samples.c + +build-small: + gcc -O3 samples.c ./squiggle_c/squiggle.c -lm -o $(OUTPUT) diff --git a/squiggle.c/squiggle_c/examples/core/00_example_template/example.c b/squiggle.c/squiggle_c/examples/core/00_example_template/example.c @@ -1,5 +1,4 @@ #include "../../../squiggle.h" -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/core/01_one_sample/example.c b/squiggle.c/squiggle_c/examples/core/01_one_sample/example.c @@ -1,5 +1,4 @@ #include "../../../squiggle.h" -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/core/02_time_to_botec/example b/squiggle.c/squiggle_c/examples/core/02_time_to_botec/example Binary files differ. diff --git a/squiggle.c/squiggle_c/examples/core/02_time_to_botec/example.c b/squiggle.c/squiggle_c/examples/core/02_time_to_botec/example.c @@ -1,29 +1,7 @@ #include "../../../squiggle.h" -#include <stdint.h> #include <stdio.h> #include <stdlib.h> -// Estimate functions -double sample_0(uint64_t* seed) -{ - return 0; -} - -double sample_1(uint64_t* seed) -{ - return 1; -} - -double sample_few(uint64_t* seed) -{ - return sample_to(1, 3, seed); -} - -double sample_many(uint64_t* seed) -{ - return sample_to(2, 10, seed); -} - int main() { // set randomness seed @@ -34,6 +12,11 @@ int main() double p_b = 0.5; double p_c = p_a * p_b; + double sample_0(uint64_t* seed){ return 0; } + double sample_1(uint64_t* seed) { return 1; } + double sample_few(uint64_t* seed) { return sample_to(1, 3, seed); } + double sample_many(uint64_t* seed) { return sample_to(2, 10, seed); } + int n_dists = 4; double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 }; double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many }; @@ -45,11 +28,5 @@ int main() } printf("Mean: %f\n", array_mean(result_many, n_samples)); - // printf("result_many: ["); - // for(int i=0; i<100; i++){ - // printf("%.2f, ", result_many[i]); - // } - // printf("]\n"); - free(seed); } diff --git a/squiggle.c/squiggle_c/examples/core/03_gcc_nested_function/example.c b/squiggle.c/squiggle_c/examples/core/03_gcc_nested_function/example.c @@ -1,5 +1,4 @@ #include "../../../squiggle.h" -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/core/04_gamma_beta/example.c b/squiggle.c/squiggle_c/examples/core/04_gamma_beta/example.c @@ -1,5 +1,4 @@ #include "../../../squiggle.h" -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/core/05_hundred_lognormals/example.c b/squiggle.c/squiggle_c/examples/core/05_hundred_lognormals/example.c @@ -1,5 +1,4 @@ #include "../../../squiggle.h" -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/more/00_example_template/example.c b/squiggle.c/squiggle_c/examples/more/00_example_template/example.c @@ -1,6 +1,5 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/more/01_sample_from_cdf/example.c b/squiggle.c/squiggle_c/examples/more/01_sample_from_cdf/example.c @@ -1,7 +1,6 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" #include <math.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <time.h> diff --git a/squiggle.c/squiggle_c/examples/more/02_sample_from_cdf_beta/example b/squiggle.c/squiggle_c/examples/more/02_sample_from_cdf_beta/example Binary files differ. diff --git a/squiggle.c/squiggle_c/examples/more/02_sample_from_cdf_beta/example.c b/squiggle.c/squiggle_c/examples/more/02_sample_from_cdf_beta/example.c @@ -1,7 +1,6 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" #include <math.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <time.h> diff --git a/squiggle.c/squiggle_c/examples/more/03_ci_beta/example.c b/squiggle.c/squiggle_c/examples/more/03_ci_beta/example.c @@ -1,6 +1,5 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/more/04_nuclear_war/example.c b/squiggle.c/squiggle_c/examples/more/04_nuclear_war/example.c @@ -1,7 +1,6 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" #include <math.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/more/05_burn_10kg_fat/example.c b/squiggle.c/squiggle_c/examples/more/05_burn_10kg_fat/example.c @@ -1,7 +1,6 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" #include <math.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/more/06_nuclear_recovery/example.c b/squiggle.c/squiggle_c/examples/more/06_nuclear_recovery/example.c @@ -1,7 +1,6 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" #include <math.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/more/07_algebra/example.c b/squiggle.c/squiggle_c/examples/more/07_algebra/example.c @@ -1,7 +1,6 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" #include <math.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/more/08_algebra_and_conversion/example.c b/squiggle.c/squiggle_c/examples/more/08_algebra_and_conversion/example.c @@ -1,7 +1,6 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" #include <math.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/more/09_ergonomic_algebra/example.c b/squiggle.c/squiggle_c/examples/more/09_ergonomic_algebra/example.c @@ -1,7 +1,6 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" #include <math.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/more/10_twitter_thread_example/example.c b/squiggle.c/squiggle_c/examples/more/10_twitter_thread_example/example.c @@ -1,6 +1,5 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/more/11_billion_lognormals_paralell/example b/squiggle.c/squiggle_c/examples/more/11_billion_lognormals_paralell/example Binary files differ. diff --git a/squiggle.c/squiggle_c/examples/more/11_billion_lognormals_paralell/example.c b/squiggle.c/squiggle_c/examples/more/11_billion_lognormals_paralell/example.c @@ -1,6 +1,5 @@ #include "../../../squiggle.h" #include "../../../squiggle_more.h" -#include <stdint.h> #include <stdio.h> #include <stdlib.h> diff --git a/squiggle.c/squiggle_c/examples/more/12_time_to_botec_parallel/example b/squiggle.c/squiggle_c/examples/more/12_time_to_botec_parallel/example Binary files differ. diff --git a/squiggle/makefile b/squiggle/makefile @@ -12,7 +12,7 @@ run-bun: bun src/samples.js patch: - sed -i 's/defaultSampleCount: 1000/defaultSampleCount: 1000000/g' node_modules/@quri/squiggle-lang/src/magicNumbers.ts node_modules/@quri/squiggle-lang/dist/magicNumbers.js + sed -i 's/defaultSampleCount: 1000/defaultSampleCount: 1000000/g' src/node_modules/@quri/squiggle-lang/src/magicNumbers.ts src/node_modules/@quri/squiggle-lang/dist/magicNumbers.js run-node: node src/samples.js