commit ffd6e5dcbb9e213b8adf8f3988dbea99a9f6837d parent 66d33c0fb75ce010c5bc54b9f949f5faadda8ab0 Author: NunoSempere <nuno.sempere@protonmail.com> Date: Mon, 27 Nov 2023 16:46:57 +0000 tweak parallelism example Diffstat:
16 files changed, 27 insertions(+), 14 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/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/13_parallelize_min/example.c b/examples/more/13_parallelize_min/example.c @@ -15,7 +15,7 @@ int main() // 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++){ + for(int i=0; i<(n-1); i++){ double sample = sample_normal(5, 2, seed); if(sample < min){ min = sample; @@ -27,7 +27,7 @@ int main() return sample_min_of_n(seed, 1000); } - int n_samples = 1000000, n_threads = 16; + int n_samples = 10000, 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)); @@ -41,27 +41,29 @@ int main() int quotient = n / 16; int remainder = n % 16; - uint64_t seed = 1000; + uint64_t seed = 100; 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 result = sample_min_of_n(seed, quotient); + // printf("Result: %f\n", result); + return result; } - double* results_quotient = malloc(quotient * sizeof(double)); - parallel_sampler(sample_min_of_quotient, results_quotient, n_threads, quotient); + double* results = malloc(n_threads * sizeof(double)); + parallel_sampler(sample_min_of_quotient, results, n_threads, n_threads); - double min = results_quotient[0]; - for(int i=1; i<quotient; i++){ - if(min > results_quotient[i]){ - min = results_quotient[i]; + double min = results[0]; + for(int i=1; i<n_threads; i++){ + if(min > results[i]){ + min = results[i]; } } if(min > result_remainder){ min = result_remainder; } - free(results_quotient); + free(results); return min; } - printf("Minimum of 1M samples of normal(5,2): %f\n", sample_n_parallel(1000000)); + printf("Minimum of 10M samples of normal(5,2): %f\n", sample_n_parallel(1000 * 1000)); } diff --git a/squiggle_more.c b/squiggle_more.c @@ -326,14 +326,25 @@ void parallel_sampler(double (*sampler)(uint64_t* seed), double* results, int n_ 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; #pragma omp parallel private(i) - { + { #pragma omp for for (i = 0; i < n_threads; i++) { int lower_bound_inclusive = i * quotient;