From ca1f81444e691a84c232f91590c8ee4b936a5785 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Wed, 29 Nov 2023 23:10:22 +0000 Subject: [PATCH] reformat & remake --- examples/core/02_time_to_botec/example.c | 8 ++-- examples/more/00_example_template/example | Bin 27360 -> 27360 bytes examples/more/01_sample_from_cdf/example | Bin 27648 -> 27648 bytes examples/more/02_sample_from_cdf_beta/example | Bin 27632 -> 27632 bytes examples/more/03_ci_beta/example | Bin 27408 -> 27408 bytes examples/more/04_nuclear_war/example | Bin 27488 -> 27488 bytes examples/more/05_burn_10kg_fat/example | Bin 27440 -> 27440 bytes examples/more/06_nuclear_recovery/example | Bin 27736 -> 27736 bytes examples/more/07_algebra/example | Bin 27360 -> 27360 bytes .../more/08_algebra_and_conversion/example | Bin 27360 -> 27360 bytes examples/more/09_ergonomic_algebra/example | Bin 27360 -> 27360 bytes .../more/10_twitter_thread_example/example | Bin 27520 -> 27520 bytes .../11_billion_lognormals_paralell/example | Bin 27400 -> 27400 bytes .../11_billion_lognormals_paralell/example.c | 5 ++- .../more/12_time_to_botec_parallel/example | Bin 27568 -> 27568 bytes .../more/12_time_to_botec_parallel/example.c | 15 +++---- examples/more/13_parallelize_min/example | Bin 27464 -> 27464 bytes examples/more/13_parallelize_min/example.c | 27 ++++++------ .../more/14_check_confidence_interval/example | Bin 27360 -> 27360 bytes .../14_check_confidence_interval/example.c | 2 +- makefile | 4 +- squiggle_more.c | 39 +++++++++--------- squiggle_more.h | 4 +- 23 files changed, 55 insertions(+), 49 deletions(-) diff --git a/examples/core/02_time_to_botec/example.c b/examples/core/02_time_to_botec/example.c index dd63ef3..908b8df 100644 --- a/examples/core/02_time_to_botec/example.c +++ b/examples/core/02_time_to_botec/example.c @@ -12,10 +12,10 @@ int main() double p_b = 0.5; double p_c = p_a * p_b; - 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); } - double sample_many(uint64_t* seed) { return sample_to(2, 10, seed); } + 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); } + double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); } int n_dists = 4; double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 }; diff --git a/examples/more/00_example_template/example b/examples/more/00_example_template/example index b854df73ad441cd666c8d88d40bd7a11fce471e9..fb88ad8b9836f4fb4b4d45d082065dbd5cf07cfc 100755 GIT binary patch delta 103 zcmaEGmGQw<#tj+FB9r=b&+s$~FrE%?`yJ8a_>pVP@7B#V%#*Ykzi#H&*JNUJm~3hI r&`^hwf#Id>|NsB_|NsB_O|<|3TlFV& diff --git a/examples/more/01_sample_from_cdf/example b/examples/more/01_sample_from_cdf/example index 8776ea9592ace65b7409c19f6bd994c259a836e0..23b3a0e0c02898dc4e8f02038fc929aaacf82d51 100755 GIT binary patch delta 103 zcmZp;!PszvaYF{P$U=|R&$q3=c@aT3mI+-dV diff --git a/examples/more/03_ci_beta/example b/examples/more/03_ci_beta/example index 655d4615778747ba223c49d5bd13ada7b1d0ec3f..0407d0546579e08bb997970500f59ceab7167aeb 100755 GIT binary patch delta 103 zcmbPmjd8*?#tj+FB3FKP7iK$dem(uK<+r;oJ@=iqFdW-l!_1_^_;qu=z9ti+!{n8Q r4-It~85my5{{R144M&C{NSqZ@+<)_KqfiR~H$W;w delta 103 zcmbPmjd8*?#tj+FBH=E3^VIFt*j5{Gwd}I&y6Ch0bK~Y3W+olR&ztM@HJKRgCa*Mn rXsE@=!0=M`|NsB|@-7Sv9=$Ot1s=_BI5PA>;;fkBzMFR&g<1dr_2nj^ diff --git a/examples/more/04_nuclear_war/example b/examples/more/04_nuclear_war/example index 96f98a77c57d3faedde94c1440c776fffc73037b..2cd828f883833c4b21f587ee83335166bc3eed24 100755 GIT binary patch delta 103 zcmaEGjq$-X#tj+FB2x}s(RuyrN0GPo|Lb-;w>e2k&CuLj!>pvw_;vGqBTXhohsh^R r9vbQ}GBCW9{r~?zzq|_rgGXf>%mM>XvqtrInFe~XZe%?IaNRx@tZt_W! rhlW~=3=A)2|NsBbFYm&@;L#hSQsB}2h9g5CB+iN{?z{Q6X{#v!_;o4U diff --git a/examples/more/05_burn_10kg_fat/example b/examples/more/05_burn_10kg_fat/example index cd93c3f648c8abe84f2dbe995ff25764e6e3da58..2b18cf34bd61a217546c5c135ff860a35ddea7d0 100755 GIT binary patch delta 103 zcmdmRjd24IWiX4pin!@$`}e9Ut1#=5h9_Nl`n|@lH`g#5=`w!Zyxu^QiP2&5O{0f~ pI*beqFJ=G#|IaV)!oc9s8>3R-(fo!Z!w@9SiYe~D`L(gDIRKhQD4zfT delta 103 zcmdmRjd24IWiX38Z)jf1lq*s#V{DLR?vlc_Fks1?%{9zMx{RMUuQ$+SVzirl)99h0 p79#`0OWFVb|MSbcFfe%Z#;6o{G{51<&|NsB_|NsB_C@g^!@Nm{@$+VV15GAIyUCG8 r4-K^#85my5{{R144M&DPNSqZ@+;?-faj7`~fzT@A diff --git a/examples/more/08_algebra_and_conversion/example b/examples/more/08_algebra_and_conversion/example index 4fa2fd274b109065d699f65dd074a850cc6ad3ee..b7ee46f6d1b391095d4051e5553c31a6d14b6237 100755 GIT binary patch delta 103 zcmaEGmGQw<#tj+FBC(r4*T|NsB_*ja^O(sT%$(=?I q4Rsh97+%W$|Noy~-i3j|qc=vSz@zyMM}{FtoE1~tfAehPr{(}hxhNI@ delta 103 zcmZp;&e(9BaYF{PNS5E?fQD-w@*6f<8tv6NV;`&;D!#df`H>Fe=gsj3noNv#lRJ$b q8fq~zFuaug|NlR~ybA+^M{kTufk*QjjtqT}I4h>O@8;RYPt5@x3MXFx diff --git a/examples/more/11_billion_lognormals_paralell/example b/examples/more/11_billion_lognormals_paralell/example index 1e4e4633abc24bfdaa4e3d1746a2d332299f7216..6f05d599393a55f8aa79484f13aad54440cf69ac 100755 GIT binary patch delta 103 zcmeCU#@KO;;fkBzMD@Q*;)VqT#_fA diff --git a/examples/more/11_billion_lognormals_paralell/example.c b/examples/more/11_billion_lognormals_paralell/example.c index b9fa6c6..c9d49d7 100644 --- a/examples/more/11_billion_lognormals_paralell/example.c +++ b/examples/more/11_billion_lognormals_paralell/example.c @@ -13,13 +13,14 @@ int main() int n_samples = 1000 * 1000 * 1000; int n_threads = 16; - double sampler(uint64_t* seed){ + double sampler(uint64_t * seed) + { return sample_lognormal(0, 10, seed); } double* results = malloc(n_samples * sizeof(double)); sampler_parallel(sampler, results, n_threads, n_samples); - double avg = array_sum(results, n_samples)/n_samples; + double avg = array_sum(results, n_samples) / n_samples; printf("Average of 1B lognormal(0,10): %f", avg); free(results); diff --git a/examples/more/12_time_to_botec_parallel/example b/examples/more/12_time_to_botec_parallel/example index 636baa5570f6f56efc2ce8881c5810da38e70403..fb4e50f083e18ef137552e67cca4c98146a7c814 100755 GIT binary patch delta 103 zcmdmRopA#YWiX3q>I6Odv$jCC-}`(-QN#Nu+h0ar-CV=$q|5kq^L_(OCPs(JFO41= p>M$}eyp;X_|3AOH3j>2kZ;VQTNAnww3`3AOE2g;r=HJF!%>gc{D;)p; delta 103 zcmdmRopA#YWiX3mtiO|R&|E`_KgXCQBJ9p99v4rR%{9zUx{RMU?>Eq7Vzit5(&(X~ p79#`0OWFVb|MSbcFfe%Z#;6o{G{51<&b{9Sn1nXebMC5?5mq=n2q!pKW|=dsL8}=H~FUV rLqjb_28Nfi|NsBzmv>=c@aT results_quotient[i]){ + for (int i = 1; i < quotient; i++) { + if (min > results_quotient[i]) { min = results_quotient[i]; } } - if(min > result_remainder){ + if (min > result_remainder) { min = result_remainder; } free(results_quotient); return min; } printf("Minimum of 1M samples of normal(5,2): %f\n", sample_n_parallel(1000000)); - } diff --git a/examples/more/14_check_confidence_interval/example b/examples/more/14_check_confidence_interval/example index edd76c31dc554b2ff38531760501807fb3951697..2eeba348327363ffcc8aad22a4b2cd9a9da39cb8 100755 GIT binary patch delta 103 zcmaEGmGQw<#tj+FA_i9%^39*O_U~0P{R8fdBvi delta 103 zcmaEGmGQw<#tj+FBJ2xQm&b&@5{#CVVHIIf+Yo3lUubg;vyl$t=gsT&HJKRgCf_uC rXsE@=!0=M`|NsB|@-7Sv9=$Ot1s=_BI5PA>;;fkBzMEegeKiLFiz_AV diff --git a/examples/more/14_check_confidence_interval/example.c b/examples/more/14_check_confidence_interval/example.c index 317985f..24953fa 100644 --- a/examples/more/14_check_confidence_interval/example.c +++ b/examples/more/14_check_confidence_interval/example.c @@ -8,7 +8,7 @@ int main() // set randomness seed uint64_t* seed = malloc(sizeof(uint64_t)); *seed = 1000; // xorshift can't start with a seed of 0 - + int n = 1000000; double* xs = malloc(sizeof(double) * n); for (int i = 0; i < n; i++) { diff --git a/makefile b/makefile index 876a34b..6cf73b7 100644 --- a/makefile +++ b/makefile @@ -13,8 +13,8 @@ format-examples: cd examples/more && make format-all format: squiggle.c squiggle.h - $(FORMATTER) squiggle.c - $(FORMATTER) squiggle.h + $(FORMATTER) squiggle.c squiggle.h + $(FORMATTER) squiggle_more.c squiggle_more.h lint: clang-tidy squiggle.c -- -lm diff --git a/squiggle_more.c b/squiggle_more.c index 44d799d..e560d46 100644 --- a/squiggle_more.c +++ b/squiggle_more.c @@ -1,15 +1,16 @@ +#include "squiggle.h" #include -#include #include +#include #include #include #include #include -#include "squiggle.h" /* Parallel sampler */ -void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples){ - if((n_samples % n_threads) != 0){ +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); } @@ -20,12 +21,12 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_ } int i; - #pragma omp parallel private(i) +#pragma omp parallel private(i) { - #pragma omp for +#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 upper_bound = ((i + 1) * (n_samples / n_threads)) - 1; // printf("Lower bound: %d, upper bound: %d\n", lower_bound, upper_bound); for (int j = lower_bound; j < upper_bound; j++) { results[j] = sampler(seeds[i]); @@ -95,7 +96,8 @@ static double quickselect(int k, double xs[], int n) } } -ci array_get_ci(ci interval, double* xs, int n){ +ci array_get_ci(ci interval, double* xs, int n) +{ int low_k = floor(interval.low * n); int high_k = ceil(interval.high * n); @@ -107,10 +109,11 @@ ci array_get_ci(ci interval, double* xs, int n){ } ci array_get_90_ci(double xs[], int n) { - return array_get_ci((ci) {.low = 0.05, .high = 0.95}, xs, n); + return array_get_ci((ci) { .low = 0.05, .high = 0.95 }, xs, n); } -ci sampler_get_ci(ci interval, double (*sampler)(uint64_t*), int n, uint64_t* seed){ +ci sampler_get_ci(ci interval, double (*sampler)(uint64_t*), int n, uint64_t* seed) +{ double* xs = malloc(n * sizeof(double)); for (int i = 0; i < n; i++) { xs[i] = sampler(seed); @@ -118,11 +121,10 @@ ci sampler_get_ci(ci interval, double (*sampler)(uint64_t*), int n, uint64_t* se ci result = array_get_ci(interval, xs, n); free(xs); return result; - } ci sampler_get_90_ci(double (*sampler)(uint64_t*), int n, uint64_t* seed) { - return sampler_get_ci((ci) {.low = 0.05, .high = 0.95}, sampler, n, seed); + return sampler_get_ci((ci) { .low = 0.05, .high = 0.95 }, sampler, n, seed); } /* Algebra manipulations */ @@ -274,7 +276,7 @@ struct box inverse_cdf_double(double cdf(double), double p) } } -// Version #2: +// 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) @@ -372,11 +374,11 @@ 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; + } } /* array print: potentially useful for debugging */ @@ -390,4 +392,3 @@ void array_print(double xs[], int n) printf("%f", xs[n - 1]); printf("]\n"); } - diff --git a/squiggle_more.h b/squiggle_more.h index 06747c1..947b164 100644 --- a/squiggle_more.h +++ b/squiggle_more.h @@ -1,5 +1,5 @@ #ifndef SQUIGGLE_C_EXTRA -#define SQUIGGLE_C_EXTRA +#define SQUIGGLE_C_EXTRA /* Parallel sampling */ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples); @@ -29,7 +29,7 @@ typedef struct lognormal_params_t { lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params b); 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); /* Error handling */ struct box {