From 1521030421af2e8734ff6721881f348e2f0f9bb9 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 19 Nov 2023 15:50:21 +0000 Subject: [PATCH] tweak parallize function --- .../core/03_gcc_nested_function/example.c | 1 + squiggle_more.c | 56 +++++++++---------- squiggle_more.h | 2 + 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/examples/core/03_gcc_nested_function/example.c b/examples/core/03_gcc_nested_function/example.c index dbf9821..c8facb0 100644 --- a/examples/core/03_gcc_nested_function/example.c +++ b/examples/core/03_gcc_nested_function/example.c @@ -15,6 +15,7 @@ int main() int n_dists = 4; + // These are nested functions. They will not compile without gcc. double sample_0(uint64_t * seed) { return 0; } double sample_1(uint64_t * seed) { return 1; } double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); } diff --git a/squiggle_more.c b/squiggle_more.c index e670be2..f8e7bcc 100644 --- a/squiggle_more.c +++ b/squiggle_more.c @@ -7,20 +7,20 @@ #include #include "squiggle.h" -// math constants +/* Math constants */ #define PI 3.14159265358979323846 // M_PI in gcc gnu99 #define NORMAL90CONFIDENCE 1.6448536269514727 -// Some error niceties; these won't be used until later +/* Some error niceties */ +// These won't be used until later #define MAX_ERROR_LENGTH 500 #define EXIT_ON_ERROR 0 #define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__) -// Get confidence intervals, given a sampler +/* Get confidence intervals, given a sampler */ // 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 { float low; float high; @@ -58,7 +58,10 @@ ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed) return result; } -// ## Sample from an arbitrary cdf +/* Scaffolding to handle errors */ +// We are building towards sample from an arbitrary cdf +// and that operation might fail +// so we build some scaffolding here struct box { int empty; double content; @@ -78,14 +81,10 @@ struct box process_error(const char* error_msg, int should_exit, char* file, int } } -// Inverse cdf at point -// Two versions of this function: -// - raw, dealing with cdfs that return doubles -// - input: cdf: double => double, p -// - output: Box(number|error) -// - box, dealing with cdfs that return a box. -// - input: cdf: double => Box(number|error), p -// - output: Box(number|error) +/* Invert an arbitrary cdf at a point */ +// Version #1: +// - input: (cdf: double => double, p) +// - output: Box(number|error) struct box inverse_cdf_double(double cdf(double), double p) { // given a cdf: [-Inf, Inf] => [0,1] @@ -149,6 +148,9 @@ struct box inverse_cdf_double(double cdf(double), double p) } } +// Version #2: +// - input: (cdf: double => Box(number|error), p) +// - output: Box(number|error) struct box inverse_cdf_box(struct box cdf_box(double), double p) { // given a cdf: [-Inf, Inf] => Box([0,1]) @@ -225,7 +227,9 @@ struct box inverse_cdf_box(struct box cdf_box(double), double p) } } -// Sampler based on inverse cdf and randomness function +/* Sample from an arbitrary cdf */ +// Before: invert an arbitrary cdf at a point +// Now: from an arbitrary cdf, get a sample struct box sampler_cdf_box(struct box cdf(double), uint64_t* seed) { double p = sample_unit_uniform(seed); @@ -238,22 +242,18 @@ struct box sampler_cdf_double(double cdf(double), uint64_t* seed) struct box result = inverse_cdf_double(cdf, p); return result; } - -/* Could also define other variations, e.g., -double sampler_danger(struct box cdf(double), uint64_t* seed) +double sampler_cdf_danger(struct box cdf(double), uint64_t* seed) { double p = sample_unit_uniform(seed); struct box result = inverse_cdf_box(cdf, p); - if(result.empty){ - exit(1); - }else{ - return result.content; - } + if(result.empty){ + exit(1); + }else{ + return result.content; + } } -*/ - -// # Small algebra manipulations +/* Algebra manipulations */ // here I discover named structs, // which mean that I don't have to be typing // struct blah all the time. @@ -304,9 +304,8 @@ ci convert_lognormal_params_to_ci(lognormal_params y) return result; } -// Paralellism -/* -void paralellize(float (*sampler)(uint64_t* seed), float* results, int n_threads, int n_samples){ +/* Parallel sampler */ +void parallel_sampler(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); @@ -336,4 +335,3 @@ void paralellize(float (*sampler)(uint64_t* seed), float* results, int n_threads } free(seeds); } -*/ diff --git a/squiggle_more.h b/squiggle_more.h index b71ae4f..70bb07c 100644 --- a/squiggle_more.h +++ b/squiggle_more.h @@ -46,4 +46,6 @@ lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params lognormal_params convert_ci_to_lognormal_params(ci x); ci convert_lognormal_params_to_ci(lognormal_params y); +void parallel_sampler(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples); + #endif