forked from personal/squiggle.c
tweak parallize function
This commit is contained in:
parent
0a660ce6a5
commit
1521030421
|
@ -15,6 +15,7 @@ int main()
|
||||||
|
|
||||||
int n_dists = 4;
|
int n_dists = 4;
|
||||||
|
|
||||||
|
// These are nested functions. They will not compile without gcc.
|
||||||
double sample_0(uint64_t * seed) { return 0; }
|
double sample_0(uint64_t * seed) { return 0; }
|
||||||
double sample_1(uint64_t * seed) { return 1; }
|
double sample_1(uint64_t * seed) { return 1; }
|
||||||
double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); }
|
double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); }
|
||||||
|
|
|
@ -7,20 +7,20 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "squiggle.h"
|
#include "squiggle.h"
|
||||||
|
|
||||||
// math constants
|
/* Math constants */
|
||||||
#define PI 3.14159265358979323846 // M_PI in gcc gnu99
|
#define PI 3.14159265358979323846 // M_PI in gcc gnu99
|
||||||
#define NORMAL90CONFIDENCE 1.6448536269514727
|
#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 MAX_ERROR_LENGTH 500
|
||||||
#define EXIT_ON_ERROR 0
|
#define EXIT_ON_ERROR 0
|
||||||
#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__)
|
#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
|
// Not in core yet because I'm not sure how much I like the struct
|
||||||
// and the built-in 100k samples
|
// and the built-in 100k samples
|
||||||
// to do: add n to function parameters and document
|
// to do: add n to function parameters and document
|
||||||
|
|
||||||
typedef struct ci_t {
|
typedef struct ci_t {
|
||||||
float low;
|
float low;
|
||||||
float high;
|
float high;
|
||||||
|
@ -58,7 +58,10 @@ ci get_90_confidence_interval(double (*sampler)(uint64_t*), uint64_t* seed)
|
||||||
return result;
|
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 {
|
struct box {
|
||||||
int empty;
|
int empty;
|
||||||
double content;
|
double content;
|
||||||
|
@ -78,13 +81,9 @@ struct box process_error(const char* error_msg, int should_exit, char* file, int
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inverse cdf at point
|
/* Invert an arbitrary cdf at a point */
|
||||||
// Two versions of this function:
|
// Version #1:
|
||||||
// - raw, dealing with cdfs that return doubles
|
// - input: (cdf: double => double, p)
|
||||||
// - 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)
|
// - output: Box(number|error)
|
||||||
struct box inverse_cdf_double(double cdf(double), double p)
|
struct box inverse_cdf_double(double cdf(double), double p)
|
||||||
{
|
{
|
||||||
|
@ -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)
|
struct box inverse_cdf_box(struct box cdf_box(double), double p)
|
||||||
{
|
{
|
||||||
// given a cdf: [-Inf, Inf] => Box([0,1])
|
// 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)
|
struct box sampler_cdf_box(struct box cdf(double), uint64_t* seed)
|
||||||
{
|
{
|
||||||
double p = sample_unit_uniform(seed);
|
double p = sample_unit_uniform(seed);
|
||||||
|
@ -238,9 +242,7 @@ struct box sampler_cdf_double(double cdf(double), uint64_t* seed)
|
||||||
struct box result = inverse_cdf_double(cdf, p);
|
struct box result = inverse_cdf_double(cdf, p);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
double sampler_cdf_danger(struct box cdf(double), uint64_t* seed)
|
||||||
/* Could also define other variations, e.g.,
|
|
||||||
double sampler_danger(struct box cdf(double), uint64_t* seed)
|
|
||||||
{
|
{
|
||||||
double p = sample_unit_uniform(seed);
|
double p = sample_unit_uniform(seed);
|
||||||
struct box result = inverse_cdf_box(cdf, p);
|
struct box result = inverse_cdf_box(cdf, p);
|
||||||
|
@ -250,10 +252,8 @@ double sampler_danger(struct box cdf(double), uint64_t* seed)
|
||||||
return result.content;
|
return result.content;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
// # Small algebra manipulations
|
|
||||||
|
|
||||||
|
/* Algebra manipulations */
|
||||||
// here I discover named structs,
|
// here I discover named structs,
|
||||||
// which mean that I don't have to be typing
|
// which mean that I don't have to be typing
|
||||||
// struct blah all the time.
|
// struct blah all the time.
|
||||||
|
@ -304,9 +304,8 @@ ci convert_lognormal_params_to_ci(lognormal_params y)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Paralellism
|
/* Parallel sampler */
|
||||||
/*
|
void parallel_sampler(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples){
|
||||||
void paralellize(float (*sampler)(uint64_t* seed), float* results, int n_threads, int n_samples){
|
|
||||||
if((n_samples % n_threads) != 0){
|
if((n_samples % n_threads) != 0){
|
||||||
fprintf(stderr, "Number of samples isn't divisible by number of threads, aborting\n");
|
fprintf(stderr, "Number of samples isn't divisible by number of threads, aborting\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -336,4 +335,3 @@ void paralellize(float (*sampler)(uint64_t* seed), float* results, int n_threads
|
||||||
}
|
}
|
||||||
free(seeds);
|
free(seeds);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
|
@ -46,4 +46,6 @@ lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params
|
||||||
lognormal_params convert_ci_to_lognormal_params(ci x);
|
lognormal_params convert_ci_to_lognormal_params(ci x);
|
||||||
ci convert_lognormal_params_to_ci(lognormal_params y);
|
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
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user