commit 66cbb4132f6659a6770fd278e44687af100907c5
parent a4389e605fb477030dc3a0e52f2943b42359866a
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Sat, 23 Sep 2023 23:24:25 +0100
add more ergonomic & compact code using macros.
Diffstat:
3 files changed, 79 insertions(+), 0 deletions(-)
diff --git a/examples/13_ergonomic_algebra/example b/examples/13_ergonomic_algebra/example
Binary files differ.
diff --git a/examples/13_ergonomic_algebra/example.c b/examples/13_ergonomic_algebra/example.c
@@ -0,0 +1,26 @@
+#include "../../squiggle.h"
+#include <math.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#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
@@ -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