diff --git a/README.md b/README.md index 51c0c67..09489b2 100644 --- a/README.md +++ b/README.md @@ -301,7 +301,8 @@ Overall, I'd describe the error handling capabilities of this library as pretty ## To do list -- [ ] Think about whether to write a simple version of this for [uxn](https://100r.co/site/uxn.html), a minimalistic portable programming stack which, sadly, doesn't have doubles (64 bit floats) +- [ ] Document paralellism +- [ ] Document confidence intervals - [ ] Point out that, even though the C standard is ambiguous about this, this code assumes that doubles are 64 bit precision (otherwise the xorshift should be different). - [ ] Document rudimentary algebra manipulations for normal/lognormal - [ ] Think through whether to delete cdf => samples function @@ -310,11 +311,11 @@ Overall, I'd describe the error handling capabilities of this library as pretty - complexify and use boxes for everything - leave as is - [ ] Systematize references -- [ ] Publish online - [ ] Support all distribution functions in - [ ] do so efficiently - [ ] Add more functions to do algebra and get the 90% c.i. of normals, lognormals, betas, etc. - Think through which of these make sense. +- [ ] Disambiguate sample_laplace--successes vs failures || successes vs total trials as two distinct and differently named functions ## Done @@ -373,5 +374,5 @@ Overall, I'd describe the error handling capabilities of this library as pretty - [ ] Consider desirability of defining shortcuts for those functions. Adds a level of magic, though. - [ ] Test results - [x] Move to own file? Or signpost in file? => signposted in file. -- [ ] Disambiguate sample_laplace--successes vs failures || successes vs total trials as two distinct and differently named functions -- [ ] Write twitter thread. +- [x] Write twitter thread: now [here](https://twitter.com/NunoSempere/status/1707041153210564959); retweets appreciated. +- [ ] ~~Think about whether to write a simple version of this for [uxn](https://100r.co/site/uxn.html), a minimalistic portable programming stack which, sadly, doesn't have doubles (64 bit floats)~~ diff --git a/examples/00_example_template/example b/examples/00_example_template/example index bfc3417..2e60ccc 100755 Binary files a/examples/00_example_template/example and b/examples/00_example_template/example differ diff --git a/examples/04_sample_from_cdf_simple/example b/examples/04_sample_from_cdf_simple/example index 23c794b..96a5bf4 100755 Binary files a/examples/04_sample_from_cdf_simple/example and b/examples/04_sample_from_cdf_simple/example differ diff --git a/examples/04_sample_from_cdf_simple/example.c b/examples/04_sample_from_cdf_simple/example.c index 05643fb..d703666 100644 --- a/examples/04_sample_from_cdf_simple/example.c +++ b/examples/04_sample_from_cdf_simple/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include #include #include diff --git a/examples/04_sample_from_cdf_simple/makefile b/examples/04_sample_from_cdf_simple/makefile index 8b512cd..821add6 100644 --- a/examples/04_sample_from_cdf_simple/makefile +++ b/examples/04_sample_from_cdf_simple/makefile @@ -9,12 +9,13 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies +OPENMP=-fopenmp MATH=-lm -DEPENDENCIES=$(MATH) +DEPENDENCIES=$(MATH) $(OPENMP) # OPENMP=-fopenmp ## Flags diff --git a/examples/05_sample_from_cdf_beta/example b/examples/05_sample_from_cdf_beta/example index a604e4d..79b7f0f 100755 Binary files a/examples/05_sample_from_cdf_beta/example and b/examples/05_sample_from_cdf_beta/example differ diff --git a/examples/05_sample_from_cdf_beta/example.c b/examples/05_sample_from_cdf_beta/example.c index 3ee1ab3..09f7fd7 100644 --- a/examples/05_sample_from_cdf_beta/example.c +++ b/examples/05_sample_from_cdf_beta/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include #include #include diff --git a/examples/05_sample_from_cdf_beta/makefile b/examples/05_sample_from_cdf_beta/makefile index a41df3c..c1d84b2 100644 --- a/examples/05_sample_from_cdf_beta/makefile +++ b/examples/05_sample_from_cdf_beta/makefile @@ -9,7 +9,7 @@ # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/07_ci_beta/example b/examples/07_ci_beta/example index 7f37990..a94d40f 100755 Binary files a/examples/07_ci_beta/example and b/examples/07_ci_beta/example differ diff --git a/examples/07_ci_beta/example.c b/examples/07_ci_beta/example.c index 6257c21..9ca9e6c 100644 --- a/examples/07_ci_beta/example.c +++ b/examples/07_ci_beta/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include #include #include diff --git a/examples/07_ci_beta/makefile b/examples/07_ci_beta/makefile index 09f8b3c..39acff1 100644 --- a/examples/07_ci_beta/makefile +++ b/examples/07_ci_beta/makefile @@ -9,7 +9,7 @@ CC=gcc # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=example ## Dependencies diff --git a/examples/08_nuclear_war/example b/examples/08_nuclear_war/example index a70085e..fa73522 100755 Binary files a/examples/08_nuclear_war/example and b/examples/08_nuclear_war/example differ diff --git a/examples/08_nuclear_war/example.c b/examples/08_nuclear_war/example.c index df5abcb..1c44880 100644 --- a/examples/08_nuclear_war/example.c +++ b/examples/08_nuclear_war/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include #include #include diff --git a/examples/08_nuclear_war/makefile b/examples/08_nuclear_war/makefile index 09f8b3c..39acff1 100644 --- a/examples/08_nuclear_war/makefile +++ b/examples/08_nuclear_war/makefile @@ -9,7 +9,7 @@ CC=gcc # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=example ## Dependencies diff --git a/examples/09_burn_10kg_fat/example b/examples/09_burn_10kg_fat/example index 72b5d66..55e5d88 100755 Binary files a/examples/09_burn_10kg_fat/example and b/examples/09_burn_10kg_fat/example differ diff --git a/examples/09_burn_10kg_fat/example.c b/examples/09_burn_10kg_fat/example.c index 08cb757..477cb2f 100644 --- a/examples/09_burn_10kg_fat/example.c +++ b/examples/09_burn_10kg_fat/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include #include #include diff --git a/examples/09_burn_10kg_fat/makefile b/examples/09_burn_10kg_fat/makefile index 8b512cd..9f5cdc4 100644 --- a/examples/09_burn_10kg_fat/makefile +++ b/examples/09_burn_10kg_fat/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/10_nuclear_recovery/example b/examples/10_nuclear_recovery/example index 2ec9419..3d42ab5 100755 Binary files a/examples/10_nuclear_recovery/example and b/examples/10_nuclear_recovery/example differ diff --git a/examples/10_nuclear_recovery/example.c b/examples/10_nuclear_recovery/example.c index 06a026f..abbfe8e 100644 --- a/examples/10_nuclear_recovery/example.c +++ b/examples/10_nuclear_recovery/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include #include #include diff --git a/examples/10_nuclear_recovery/makefile b/examples/10_nuclear_recovery/makefile index 8b512cd..9f5cdc4 100644 --- a/examples/10_nuclear_recovery/makefile +++ b/examples/10_nuclear_recovery/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/11_algebra/example b/examples/11_algebra/example index b57bc51..c880c3b 100755 Binary files a/examples/11_algebra/example and b/examples/11_algebra/example differ diff --git a/examples/11_algebra/example.c b/examples/11_algebra/example.c index 2fde7ff..a6b6fbd 100644 --- a/examples/11_algebra/example.c +++ b/examples/11_algebra/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include #include #include diff --git a/examples/11_algebra/makefile b/examples/11_algebra/makefile index 8b512cd..9f5cdc4 100644 --- a/examples/11_algebra/makefile +++ b/examples/11_algebra/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/12_algebra_and_conversion/example b/examples/12_algebra_and_conversion/example index e2913ba..8aa4528 100755 Binary files a/examples/12_algebra_and_conversion/example and b/examples/12_algebra_and_conversion/example differ diff --git a/examples/12_algebra_and_conversion/example.c b/examples/12_algebra_and_conversion/example.c index 117f5ed..9300561 100644 --- a/examples/12_algebra_and_conversion/example.c +++ b/examples/12_algebra_and_conversion/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include #include #include @@ -17,10 +17,10 @@ int main() printf("The 90%% confidence interval of Lognormal(%f, %f) is [%f, %f]\n", ln1.logmean, ln1.logstd, ln1_ci.low, ln1_ci.high); - lognormal_params ln1_ci_paramas = convert_ci_to_lognormal_params(ln1_ci); + lognormal_params ln1_params2 = convert_ci_to_lognormal_params(ln1_ci); printf("The lognormal which has 90%% confidence interval [%f, %f] is Lognormal(%f, %f)\n", ln1_ci.low, ln1_ci.high, - ln1.logmean, ln1.logstd); + ln1_params2.logmean, ln1_params2.logstd); lognormal_params ln2 = convert_ci_to_lognormal_params((ci) { .low = 1, .high = 10 }); lognormal_params ln3 = convert_ci_to_lognormal_params((ci) { .low = 5, .high = 50 }); diff --git a/examples/12_algebra_and_conversion/makefile b/examples/12_algebra_and_conversion/makefile index 8b512cd..9f5cdc4 100644 --- a/examples/12_algebra_and_conversion/makefile +++ b/examples/12_algebra_and_conversion/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/13_ergonomic_algebra/example b/examples/13_ergonomic_algebra/example index 5ac7759..c882715 100755 Binary files a/examples/13_ergonomic_algebra/example and b/examples/13_ergonomic_algebra/example differ diff --git a/examples/13_ergonomic_algebra/example.c b/examples/13_ergonomic_algebra/example.c index 139173d..35fcfd1 100644 --- a/examples/13_ergonomic_algebra/example.c +++ b/examples/13_ergonomic_algebra/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include #include #include diff --git a/examples/13_ergonomic_algebra/makefile b/examples/13_ergonomic_algebra/makefile index 8b512cd..9f5cdc4 100644 --- a/examples/13_ergonomic_algebra/makefile +++ b/examples/13_ergonomic_algebra/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/examples/14_twitter_thread_example/example b/examples/14_twitter_thread_example/example index 100c1db..d13bff5 100755 Binary files a/examples/14_twitter_thread_example/example and b/examples/14_twitter_thread_example/example differ diff --git a/examples/14_twitter_thread_example/example.c b/examples/14_twitter_thread_example/example.c index ceed4ad..002f392 100644 --- a/examples/14_twitter_thread_example/example.c +++ b/examples/14_twitter_thread_example/example.c @@ -1,5 +1,5 @@ #include "../../squiggle.h" -#include "../../extra.h" +#include "../../squiggle_more.h" #include #include #include diff --git a/examples/14_twitter_thread_example/makefile b/examples/14_twitter_thread_example/makefile index 8b512cd..9f5cdc4 100644 --- a/examples/14_twitter_thread_example/makefile +++ b/examples/14_twitter_thread_example/makefile @@ -9,7 +9,7 @@ CC=gcc # required for nested functions # CC=tcc # <= faster compilation # Main file -SRC=example.c ../../squiggle.c ../../extra.c +SRC=example.c ../../squiggle.c ../../squiggle_more.c OUTPUT=./example ## Dependencies diff --git a/scratchpad/scratchpad b/scratchpad/scratchpad index 434102f..84baeca 100755 Binary files a/scratchpad/scratchpad and b/scratchpad/scratchpad differ diff --git a/scratchpad/scratchpad.c b/scratchpad/scratchpad.c index 2d49d32..055657d 100644 --- a/scratchpad/scratchpad.c +++ b/scratchpad/scratchpad.c @@ -9,12 +9,14 @@ int main() // set randomness seed uint64_t* seed = malloc(sizeof(uint64_t)); *seed = 1000; // xorshift can't start with a seed of 0 - + /* for (int i = 0; i < 100; i++) { double draw = sample_unit_uniform(seed); printf("%f\n", draw); - } + }*/ + // Test division + printf("\n%d\n", 10 % 3); free(seed); } diff --git a/squiggle.c b/squiggle.c index 9177e97..99caacb 100644 --- a/squiggle.c +++ b/squiggle.c @@ -1,15 +1,7 @@ -#include #include #include #include -#include #include -#include -#include - -// # Key functionality -// Define the minimum number of functions needed to do simple estimation -// Starts here, ends until the end of the mixture function // math constants #define PI 3.14159265358979323846 // M_PI in gcc gnu99 diff --git a/extra.c b/squiggle_more.c similarity index 88% rename from extra.c rename to squiggle_more.c index 94ae431..e670be2 100644 --- a/extra.c +++ b/squiggle_more.c @@ -1,12 +1,11 @@ -#include "squiggle.h" #include -#include #include +#include +#include #include #include #include -#include -#include +#include "squiggle.h" // math constants #define PI 3.14159265358979323846 // M_PI in gcc gnu99 @@ -18,7 +17,8 @@ #define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__) // Get confidence intervals, given a sampler -// Not in core yet because I'm not sure how much I like the interface, +// Not in core yet because I'm not sure how much I like the struct +// and the built-in 100k samples // to do: add n to function parameters and document typedef struct ci_t { @@ -303,3 +303,37 @@ ci convert_lognormal_params_to_ci(lognormal_params y) ci result = { .low = exp(loglow), .high = exp(loghigh) }; return result; } + +// Paralellism +/* +void paralellize(float (*sampler)(uint64_t* seed), float* results, int n_threads, int n_samples){ + if((n_samples % n_threads) != 0){ + fprintf(stderr, "Number of samples isn't divisible by number of threads, aborting\n"); + exit(1); + } + uint64_t** seeds = malloc(n_threads * sizeof(uint64_t*)); + for (uint64_t i = 0; i < n_threads; i++) { + seeds[i] = malloc(sizeof(uint64_t)); + *seeds[i] = i + 1; // xorshift can't start with 0 + } + + int i; + #pragma omp parallel private(i) + { + #pragma omp for + for (i = 0; i < n_threads; i++) { + int lower_bound = i * (n_samples / n_threads); + int upper_bound = ((i+1) * (n_samples / n_threads)) - 1; + // printf("Lower bound: %d, upper bound: %d\n", lower_bound, upper_bound); + for (int j = lower_bound; j < upper_bound; j++) { + results[j] = sampler(seeds[i]); + } + } + } + + for (uint64_t i = 0; i < n_threads; i++) { + free(seeds[i]); + } + free(seeds); +} +*/ diff --git a/extra.h b/squiggle_more.h similarity index 100% rename from extra.h rename to squiggle_more.h