squiggle.c

Self-contained Monte Carlo estimation in C99
Log | Files | Refs | README

commit ddf76e79b0edb86f877cfe3b643e6930889046ff
parent b22eb34835205be71c792af64fba47b3ba052b36
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Wed, 29 Nov 2023 23:32:18 +0000

recover better parallelism function from git history, recompile

Diffstat:
Mexamples/more/00_example_template/example | 0
Mexamples/more/01_sample_from_cdf/example | 0
Mexamples/more/02_sample_from_cdf_beta/example | 0
Mexamples/more/03_ci_beta/example | 0
Mexamples/more/04_nuclear_war/example | 0
Mexamples/more/05_burn_10kg_fat/example | 0
Mexamples/more/06_nuclear_recovery/example | 0
Mexamples/more/07_algebra/example | 0
Mexamples/more/08_algebra_and_conversion/example | 0
Mexamples/more/09_ergonomic_algebra/example | 0
Mexamples/more/10_twitter_thread_example/example | 0
Mexamples/more/11_billion_lognormals_paralell/example | 0
Mexamples/more/11_billion_lognormals_paralell/example.c | 2+-
Mexamples/more/12_time_to_botec_parallel/example | 0
Mexamples/more/13_parallelize_min/example | 0
Mexamples/more/14_check_confidence_interval/example | 0
Msquiggle_more.c | 46++++++++++++++++++++++++++++++++++++++--------
17 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/examples/more/00_example_template/example b/examples/more/00_example_template/example Binary files differ. diff --git a/examples/more/01_sample_from_cdf/example b/examples/more/01_sample_from_cdf/example Binary files differ. diff --git a/examples/more/02_sample_from_cdf_beta/example b/examples/more/02_sample_from_cdf_beta/example Binary files differ. diff --git a/examples/more/03_ci_beta/example b/examples/more/03_ci_beta/example Binary files differ. diff --git a/examples/more/04_nuclear_war/example b/examples/more/04_nuclear_war/example Binary files differ. diff --git a/examples/more/05_burn_10kg_fat/example b/examples/more/05_burn_10kg_fat/example Binary files differ. diff --git a/examples/more/06_nuclear_recovery/example b/examples/more/06_nuclear_recovery/example Binary files differ. diff --git a/examples/more/07_algebra/example b/examples/more/07_algebra/example Binary files differ. diff --git a/examples/more/08_algebra_and_conversion/example b/examples/more/08_algebra_and_conversion/example Binary files differ. diff --git a/examples/more/09_ergonomic_algebra/example b/examples/more/09_ergonomic_algebra/example Binary files differ. diff --git a/examples/more/10_twitter_thread_example/example b/examples/more/10_twitter_thread_example/example Binary files differ. diff --git a/examples/more/11_billion_lognormals_paralell/example b/examples/more/11_billion_lognormals_paralell/example Binary files differ. diff --git a/examples/more/11_billion_lognormals_paralell/example.c b/examples/more/11_billion_lognormals_paralell/example.c @@ -21,7 +21,7 @@ int main() sampler_parallel(sampler, results, n_threads, n_samples); double avg = array_sum(results, n_samples) / n_samples; - printf("Average of 1B lognormal(0,10): %f", avg); + printf("Average of 1B lognormal(0,10): %f\n", avg); free(results); diff --git a/examples/more/12_time_to_botec_parallel/example b/examples/more/12_time_to_botec_parallel/example Binary files differ. diff --git a/examples/more/13_parallelize_min/example b/examples/more/13_parallelize_min/example Binary files differ. diff --git a/examples/more/14_check_confidence_interval/example b/examples/more/14_check_confidence_interval/example Binary files differ. diff --git a/squiggle_more.c b/squiggle_more.c @@ -10,14 +10,40 @@ /* Parallel sampler */ void sampler_parallel(double (*sampler)(uint64_t* seed), double* 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); - } + + // Division terminology: + // a = b * quotient + reminder + // a = (a/b)*b + (a%b) + // dividend: a + // divisor: b + // quotient = a / b + // reminder = a % b + // "divisor's multiple" := (a/b)*b + + // now, we have n_samples and n_threads + // to make our life easy, each thread will have a number of samples of: a/b (quotient) + // and we'll compute the remainder of samples separately + // to possibly do by Jorge: improve so that the remainder is included in the threads + + int quotient = n_samples / n_threads; + int remainder = n_samples % n_threads; + int divisor_multiple = quotient * n_threads; + uint64_t** seeds = malloc(n_threads * sizeof(uint64_t*)); + // printf("UINT64_MAX: %lu\n", UINT64_MAX); + srand(1); 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 + // Constraints: + // - xorshift can't start with 0 + // - the seeds should be reasonably separated and not correlated + *seeds[i] = (uint64_t)rand() * (UINT64_MAX / RAND_MAX); + // printf("#%ld: %lu\n",i, *seeds[i]); + + // Other initializations tried: + // *seeds[i] = 1 + i; + // *seeds[i] = (i + 0.5)*(UINT64_MAX/n_threads); + // *seeds[i] = (i + 0.5)*(UINT64_MAX/n_threads) + constant * i; } int i; @@ -25,14 +51,18 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_ { #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; + int lower_bound_inclusive = i * quotient; + int upper_bound_not_inclusive = ((i + 1) * quotient); // note the < in the for loop below, // printf("Lower bound: %d, upper bound: %d\n", lower_bound, upper_bound); - for (int j = lower_bound; j < upper_bound; j++) { + for (int j = lower_bound_inclusive; j < upper_bound_not_inclusive; j++) { results[j] = sampler(seeds[i]); } } } + for (int j = divisor_multiple; j < n_samples; j++) { + results[j] = sampler(seeds[0]); + // we can just reuse a seed, this isn't problematic because we are not doing multithreading + } for (uint64_t i = 0; i < n_threads; i++) { free(seeds[i]);