squiggle.c

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

commit 514a41c9f7b42ebe18f08a546ff956f57b4603e1
parent c11ce6b185f7a07d76a64f0dc7c71b87b528ec65
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Sun, 19 Nov 2023 15:40:13 +0000

feat: add parallelism to get 1B lognormal samples in 11s.

Diffstat:
Aexamples/more/11_billion_lognormals_paralell/example | 0
Aexamples/more/11_billion_lognormals_paralell/example.c | 30++++++++++++++++++++++++++++++
2 files changed, 30 insertions(+), 0 deletions(-)

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 @@ -0,0 +1,30 @@ +#include "../../../squiggle.h" +#include "../../../squiggle_more.h" +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> + +// Estimate functions +int main() +{ + // set randomness seed + // uint64_t* seed = malloc(sizeof(uint64_t)); + // *seed = 1000; // xorshift can't start with 0 + // ^ not necessary, because parallel_sampler takes care of the seed. + + int n_samples = 1000 * 1000 * 1000; + int n_threads = 16; + double sampler(uint64_t* seed){ + return sample_lognormal(0, 10, seed); + } + double* results = malloc(n_samples * sizeof(double)); + + parallel_sampler(sampler, results, n_threads, n_samples); + double avg = array_sum(results, n_samples)/n_samples; + printf("Average of 1B lognormal(0,10): %f", avg); + + free(results); + + // free(seed); + // ^ not necessary, because parallel_sampler takes care of the seed. +}