diff --git a/examples/more/00_example_template/example b/examples/more/00_example_template/example index 92e3116..f2b1432 100755 Binary files a/examples/more/00_example_template/example and b/examples/more/00_example_template/example differ diff --git a/examples/more/01_sample_from_cdf/example b/examples/more/01_sample_from_cdf/example index aefd99d..4920d36 100755 Binary files a/examples/more/01_sample_from_cdf/example and b/examples/more/01_sample_from_cdf/example differ diff --git a/examples/more/02_sample_from_cdf_beta/example b/examples/more/02_sample_from_cdf_beta/example index ffb3883..521640a 100755 Binary files a/examples/more/02_sample_from_cdf_beta/example and b/examples/more/02_sample_from_cdf_beta/example differ diff --git a/examples/more/03_ci_beta/example b/examples/more/03_ci_beta/example index f558a84..a0cde1d 100755 Binary files a/examples/more/03_ci_beta/example and b/examples/more/03_ci_beta/example differ diff --git a/examples/more/04_nuclear_war/example b/examples/more/04_nuclear_war/example index bb6534b..522f037 100755 Binary files a/examples/more/04_nuclear_war/example and b/examples/more/04_nuclear_war/example differ diff --git a/examples/more/05_burn_10kg_fat/example b/examples/more/05_burn_10kg_fat/example index 06d18ab..6482272 100755 Binary files a/examples/more/05_burn_10kg_fat/example and b/examples/more/05_burn_10kg_fat/example differ diff --git a/examples/more/06_nuclear_recovery/example b/examples/more/06_nuclear_recovery/example index 92e14a0..0a8f642 100755 Binary files a/examples/more/06_nuclear_recovery/example and b/examples/more/06_nuclear_recovery/example differ diff --git a/examples/more/07_algebra/example b/examples/more/07_algebra/example index 0a9bb23..e4cf037 100755 Binary files a/examples/more/07_algebra/example and b/examples/more/07_algebra/example differ diff --git a/examples/more/08_algebra_and_conversion/example b/examples/more/08_algebra_and_conversion/example index 1eee5c1..094dc39 100755 Binary files a/examples/more/08_algebra_and_conversion/example and b/examples/more/08_algebra_and_conversion/example differ diff --git a/examples/more/09_ergonomic_algebra/example b/examples/more/09_ergonomic_algebra/example index b5caca7..273445c 100755 Binary files a/examples/more/09_ergonomic_algebra/example and b/examples/more/09_ergonomic_algebra/example differ diff --git a/examples/more/10_twitter_thread_example/example b/examples/more/10_twitter_thread_example/example index 0095d68..1960f0d 100755 Binary files a/examples/more/10_twitter_thread_example/example and b/examples/more/10_twitter_thread_example/example differ diff --git a/examples/more/11_billion_lognormals_paralell/example b/examples/more/11_billion_lognormals_paralell/example index 81d03cd..f46aaef 100755 Binary files a/examples/more/11_billion_lognormals_paralell/example and b/examples/more/11_billion_lognormals_paralell/example differ diff --git a/examples/more/12_time_to_botec_parallel/example b/examples/more/12_time_to_botec_parallel/example index 6598573..ae9c70d 100755 Binary files a/examples/more/12_time_to_botec_parallel/example and b/examples/more/12_time_to_botec_parallel/example differ diff --git a/examples/more/13_parallelize_min/example b/examples/more/13_parallelize_min/example new file mode 100755 index 0000000..a6bce2c Binary files /dev/null and b/examples/more/13_parallelize_min/example differ diff --git a/examples/more/13_parallelize_min/example.c b/examples/more/13_parallelize_min/example.c new file mode 100644 index 0000000..cf1f9ea --- /dev/null +++ b/examples/more/13_parallelize_min/example.c @@ -0,0 +1,67 @@ +#include "../../../squiggle.h" +#include "../../../squiggle_more.h" +#include +#include + +int main() +{ + /* Question: can we parallelize this? + A = normal(5,2) + B = min(A) + B * 20 + */ + + /* Option 1: parallelize taking from n samples */ + // Question being asked: what is the distribution of sampling 1000 times and taking the min? + double sample_min_of_n(uint64_t* seed, int n){ + double min = sample_normal(5, 2, seed); + for(int i=0; i<(n-2); i++){ + double sample = sample_normal(5, 2, seed); + if(sample < min){ + min = sample; + } + } + return min; + } + double sampler_min_of_1000(uint64_t* seed) { + return sample_min_of_n(seed, 1000); + } + + int n_samples = 1000000, n_threads = 16; + double* results = malloc(n_samples * sizeof(double)); + parallel_sampler(sampler_min_of_1000, results, n_threads, n_samples); + printf("Mean of the distribution of (taking the min of 1000 samples of a normal(5,2)): %f\n", array_mean(results, n_samples)); + free(results); + + /* Option 2: take the min from n samples cleverly using parallelism */ + // Question being asked: can we take the min of n samples cleverly? + double sample_n_parallel(int n){ + + int n_threads = 16; + int quotient = n / 16; + int remainder = n % 16; + + uint64_t seed = 1000; + double result_remainder = sample_min_of_n(&seed, remainder); + + double sample_min_of_quotient(uint64_t* seed) { + return sample_min_of_n(seed, quotient); + } + double* results_quotient = malloc(quotient * sizeof(double)); + parallel_sampler(sample_min_of_quotient, results_quotient, n_threads, quotient); + + double min = results_quotient[0]; + for(int i=1; i results_quotient[i]){ + min = results_quotient[i]; + } + } + if(min > result_remainder){ + min = result_remainder; + } + free(results_quotient); + return min; + } + printf("Minimum of 1M samples of normal(5,2): %f\n", sample_n_parallel(1000000)); + +} diff --git a/examples/more/makefile b/examples/more/makefile index c03f24e..a95dccc 100644 --- a/examples/more/makefile +++ b/examples/more/makefile @@ -48,6 +48,7 @@ all: $(CC) $(OPTIMIZED) $(DEBUG) 10_twitter_thread_example/$(SRC) $(DEPS) -o 10_twitter_thread_example/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) 11_billion_lognormals_paralell/$(SRC) $(DEPS) -o 11_billion_lognormals_paralell/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) 12_time_to_botec_parallel/$(SRC) $(DEPS) -o 12_time_to_botec_parallel/$(OUTPUT) + $(CC) $(OPTIMIZED) $(DEBUG) 13_parallelize_min/$(SRC) $(DEPS) -o 13_parallelize_min/$(OUTPUT) format-all: $(FORMATTER) 00_example_template/$(SRC) @@ -63,6 +64,7 @@ format-all: $(FORMATTER) 10_twitter_thread_example/$(SRC) $(FORMATTER) 11_billion_lognormals_paralell/$(SRC) $(FORMATTER) 12_time_to_botec_parallel/$(SRC) + $(FORMATTER) 13_parallelize_min/$(SRC) run-all: 00_example_template/$(OUTPUT) @@ -78,6 +80,7 @@ run-all: 10_twitter_thread_example/$(OUTPUT) 11_billion_lognormals_paralell/$(OUTPUT) 12_time_to_botec_parallel/$(OUTPUT) + 13_parallelize_min/$(OUTPUT) ## make one DIR=06_nuclear_recovery one: $(DIR)/$(SRC) diff --git a/scratchpad/makefile b/scratchpad/makefile index a2aed9e..d515383 100644 --- a/scratchpad/makefile +++ b/scratchpad/makefile @@ -9,7 +9,7 @@ CC=gcc # CC=tcc # <= faster compilation # Main file -SRC=scratchpad.c ../squiggle.c +SRC=scratchpad.c ../squiggle.c ../squiggle_more.c OUTPUT=scratchpad ## Dependencies diff --git a/scratchpad/scratchpad b/scratchpad/scratchpad index 84baeca..c575ecd 100755 Binary files a/scratchpad/scratchpad and b/scratchpad/scratchpad differ diff --git a/scratchpad/scratchpad.c b/scratchpad/scratchpad.c index 055657d..ca8fe54 100644 --- a/scratchpad/scratchpad.c +++ b/scratchpad/scratchpad.c @@ -1,4 +1,5 @@ #include "../squiggle.h" +#include "../squiggle_more.h" #include #include #include @@ -16,7 +17,18 @@ int main() }*/ // Test division - printf("\n%d\n", 10 % 3); + // printf("\n%d\n", 10 % 3); + // + + int n_samples = 100, n_threads = 16; + double* results = malloc(n_samples * sizeof(double)); + double sampler_scratchpad(uint64_t* seed){ + return 1; + } + parallel_sampler(sampler_scratchpad, results, n_threads, n_samples); + for(int i=0; i