From 66cbb4132f6659a6770fd278e44687af100907c5 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 23 Sep 2023 23:24:25 +0100 Subject: [PATCH] add more ergonomic & compact code using macros. --- examples/13_ergonomic_algebra/example | Bin 0 -> 22496 bytes examples/13_ergonomic_algebra/example.c | 26 ++++++++++++ examples/13_ergonomic_algebra/makefile | 53 ++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100755 examples/13_ergonomic_algebra/example create mode 100644 examples/13_ergonomic_algebra/example.c create mode 100644 examples/13_ergonomic_algebra/makefile diff --git a/examples/13_ergonomic_algebra/example b/examples/13_ergonomic_algebra/example new file mode 100755 index 0000000000000000000000000000000000000000..b3bbfc675076763fb9a79bda9e3a6bd8a130e4d6 GIT binary patch literal 22496 zcmeHPeRNbsmVZg7A&{hBqoDBvX|WlDN+;n>=VZP4d%0<9LmPij|j% z&rDfSPKNs@b2w<$wz47s}Ot@f=@z4 z@JT4`Nj9Ntp0H;!w4siYjolR`nXgLlCBqUyFCn!-Y32KYy06n-h2YCB=5mtZ^<;1) z31xdLz$Z-22`+J$!+f7;FWoqVg*_z{%`1hC4eRF4E^Mru(b&)&?3mFpZ|;n_vmI@I z$4u5T=|ydjPc6N76%9w2aKemiEGCLF)sKI1;m^)J{K(`2*Z=N6QvY1ekMA72M9QEx ziG~vK%OR#Ry!5>V6zZrNNuH;k&M5_Kin+E?#8hm3K{I)dsxxm|SC8xDOq|&D%4gN*orzQ)b zGW@ZL+;6VpE~~u9UFU7}u5V}ycv~y)S={Jv_Evh`q0W(&BGd+8UavxmwYjyQQ_EIpA~G)<2|pI~oFL1oCSiB81!L zX=qfMJdKV1TEaIQXdiC#w-Rw}hsW(}X!bNVJVFYRWT9Y_2QB$py1=0@%>`m&930}5--a~mvcPD0vT%vGho1rC6E#72K;!2;kO|JUiOJ< zh7EY33S+1j_^~L7E-`$I0dI_39R@tbIhi&a@N7L6726GXW1QbGt_2#nT~0+gw>YX!D6wa=8f0_r zx`OJc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XPg4VgadtR`;y@&6LguM+$c+9jcYSoRW98A?Wec@80=ErFVz z$r2Oo)9~uaFZGw_auUk+<+)t4{vPntGo9p@=XMefka3YnN6Jcmd4ARa9Ccap%kvKj z<+&e7SQ6r7QF$0SI#ZMU@|;A%0zoPDmvj z;FEC3$c%iFc;&}Q{PJ8V4llvKCy8I4A4xcH3H}$8_~V;nu}P*M)*F-5U*hN=ZjAkx z=TQ>oNkRdW?YCd>%kht&TNQDJY!Yrra{MIfzmXvl$uG~hB$VHol=R8%zn#P{&%q?@ zPhv=J|9$Y&_rpZkiJzCr?@=bx%d&)@fLAr}%kvm*0gyf{v3^wgFy(YX=fguBG6B)@pK05lY{Q6CvYt^3u>a4krW9v#!)f#viJd ix^qf?FIvXoACmk>wkv7rxHlF57%>jlCNU%xmH!7)9DhCl literal 0 HcmV?d00001 diff --git a/examples/13_ergonomic_algebra/example.c b/examples/13_ergonomic_algebra/example.c new file mode 100644 index 0000000..250a334 --- /dev/null +++ b/examples/13_ergonomic_algebra/example.c @@ -0,0 +1,26 @@ +#include "../../squiggle.h" +#include +#include +#include +#include + +#define ln lognormal_params +#define to(...) convert_ci_to_lognormal_params((ci) __VA_ARGS__) +#define from(...) convert_lognormal_params_to_ci((ln) __VA_ARGS__) +#define times(a,b) algebra_product_lognormals(a,b) + +int main() +{ + // set randomness seed + uint64_t* seed = malloc(sizeof(uint64_t)); + *seed = 1000; // xorshift can't start with 0 + + ln a = to({.low = 1, .high = 10}); + ln b = to({.low = 5, .high = 500}); + ln c = times(a, b); + + printf("Result: to(%f, %f)\n", from(c).low, from(c).high); + printf("One sample from it is: %f\n", sample_lognormal(c.logmean, c.logstd, seed)); + + free(seed); +} diff --git a/examples/13_ergonomic_algebra/makefile b/examples/13_ergonomic_algebra/makefile new file mode 100644 index 0000000..ef385f7 --- /dev/null +++ b/examples/13_ergonomic_algebra/makefile @@ -0,0 +1,53 @@ +# Interface: +# make +# make build +# make format +# make run + +# Compiler +CC=gcc +# CC=tcc # <= faster compilation + +# Main file +SRC=example.c ../../squiggle.c +OUTPUT=example + +## Dependencies +MATH=-lm + +## Flags +DEBUG= #'-g' +STANDARD=-std=c99 +WARNINGS=-Wall +OPTIMIZED=-O3 #-Ofast +# OPENMP=-fopenmp + +## Formatter +STYLE_BLUEPRINT=webkit +FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) + +## make build +build: $(SRC) + $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(MATH) -o $(OUTPUT) + +format: $(SRC) + $(FORMATTER) $(SRC) + +run: $(SRC) $(OUTPUT) + OMP_NUM_THREADS=1 ./$(OUTPUT) && echo + +time-linux: + @echo "Requires /bin/time, found on GNU/Linux systems" && echo + + @echo "Running 100x and taking avg time $(OUTPUT)" + @t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do $(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 1 thread: |" | sed 's|$$|ms|' && echo + +## Profiling + +profile-linux: + echo "Requires perf, which depends on the kernel version, and might be in linux-tools package or similar" + echo "Must be run as sudo" + $(CC) $(SRC) $(MATH) -o $(OUTPUT) + sudo perf record ./$(OUTPUT) + sudo perf report + rm perf.data