diff --git a/README.md b/README.md index 974b6a7d..7c21b712 100644 --- 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 am not particularly sure that the Squiggle code is actually producing 1M samples, but I am also not in a rush to debug this. +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). -### Python +### Python and Squigglepy -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 index 4c79c003..b1fdbf33 100644 --- 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 index bcb38fda..41c7ef59 100644 --- 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 #include #include 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 index 82480a93..ef3b90be 100644 --- 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 #include #include 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 index 93a714de..ec138993 100755 Binary files a/squiggle.c/squiggle_c/examples/core/02_time_to_botec/example and b/squiggle.c/squiggle_c/examples/core/02_time_to_botec/example 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 index 147a02fb..dd63ef35 100644 --- 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 #include #include -// 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 index c8facb0d..54dcd0fd 100644 --- 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 #include #include 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 index 979c1a86..024ab3ec 100644 --- 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 #include #include 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 index 5b934213..eb8e07e4 100644 --- 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 #include #include 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 index 8c3e1caa..9fa72f4d 100644 --- 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 #include #include 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 index ffb7acc2..8f177908 100644 --- 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 -#include #include #include #include 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 index 1552b05a..ffb38833 100755 Binary files a/squiggle.c/squiggle_c/examples/more/02_sample_from_cdf_beta/example and b/squiggle.c/squiggle_c/examples/more/02_sample_from_cdf_beta/example 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 index 2ff66501..46105f57 100644 --- 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 -#include #include #include #include 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 index 1dddc862..1ba8207a 100644 --- 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 #include #include 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 index 11d961a1..90e9d4d2 100644 --- 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 -#include #include #include 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 index 728ca307..0914434b 100644 --- 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 -#include #include #include 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 index afc774bc..4caf57d3 100644 --- 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 -#include #include #include diff --git a/squiggle.c/squiggle_c/examples/more/07_algebra/example.c b/squiggle.c/squiggle_c/examples/more/07_algebra/example.c index a6d30d09..65b5cd6d 100644 --- 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 -#include #include #include 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 index 0dfba6fb..3f2a2c73 100644 --- 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 -#include #include #include 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 index a9446835..0cd9adf6 100644 --- 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 -#include #include #include 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 index 7d892e78..c7d8937e 100644 --- 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 #include #include 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 index 31ba7154..81d03cdd 100755 Binary files a/squiggle.c/squiggle_c/examples/more/11_billion_lognormals_paralell/example and b/squiggle.c/squiggle_c/examples/more/11_billion_lognormals_paralell/example 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 index 9e1155c0..62fc0d07 100644 --- 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 #include #include 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 index 3507dd11..6598573a 100755 Binary files a/squiggle.c/squiggle_c/examples/more/12_time_to_botec_parallel/example and b/squiggle.c/squiggle_c/examples/more/12_time_to_botec_parallel/example differ diff --git a/squiggle/makefile b/squiggle/makefile index 9f3284c1..a202675c 100644 --- 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