squiggle.c

Self-contained Monte Carlo estimation in C99
Log | Files | Refs | README

commit 0c22716635e1261a0cf24ff8516410140851adc7
parent 2ea32e2a47f3b25d1214f47b7169a2fec28c57c3
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Thu, 17 Aug 2023 14:30:33 +0200

prepare adding squiggle.c to time to botec.

Diffstat:
Dexamples/02_many_samples/example | 0
Dexamples/02_many_samples/example.c | 52----------------------------------------------------
Dexamples/02_many_samples/makefile | 53-----------------------------------------------------
Aexamples/02_many_samples_time_to_botec/example | 0
Aexamples/02_many_samples_time_to_botec/example.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aexamples/02_many_samples_time_to_botec/makefile | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 107 insertions(+), 105 deletions(-)

diff --git a/examples/02_many_samples/example b/examples/02_many_samples/example Binary files differ. diff --git a/examples/02_many_samples/example.c b/examples/02_many_samples/example.c @@ -1,52 +0,0 @@ -#include <stdint.h> -#include <stdlib.h> -#include <stdio.h> -#include "../../squiggle.h" - -// Estimate functions -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 main(){ - // set randomness seed - uint64_t* seed = malloc(sizeof(uint64_t)); - *seed = 1000; // xorshift can't start with 0 - - double p_a = 0.8; - double p_b = 0.5; - double p_c = p_a * p_b; - - int n_dists = 4; - double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 }; - double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many }; - - int n_samples = 1000000; - double* result_many = (double *) malloc(n_samples * sizeof(double)); - for(int i=0; i<n_samples; i++){ - result_many[i] = sample_mixture(samplers, weights, n_dists, seed); - } - - printf("result_many: ["); - for(int i=0; i<100; i++){ - printf("%.2f, ", result_many[i]); - } - printf("]\n"); - free(seed); -} diff --git a/examples/02_many_samples/makefile b/examples/02_many_samples/makefile @@ -1,53 +0,0 @@ -# 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 diff --git a/examples/02_many_samples_time_to_botec/example b/examples/02_many_samples_time_to_botec/example Binary files differ. diff --git a/examples/02_many_samples_time_to_botec/example.c b/examples/02_many_samples_time_to_botec/example.c @@ -0,0 +1,54 @@ +#include <stdint.h> +#include <stdlib.h> +#include <stdio.h> +#include "../../squiggle.h" + +// Estimate functions +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 main(){ + // set randomness seed + uint64_t* seed = malloc(sizeof(uint64_t)); + *seed = 1000; // xorshift can't start with 0 + + double p_a = 0.8; + double p_b = 0.5; + double p_c = p_a * p_b; + + int n_dists = 4; + double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 }; + double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many }; + + int n_samples = 1000000; + double* result_many = (double *) malloc(n_samples * sizeof(double)); + for(int i=0; i<n_samples; i++){ + result_many[i] = sample_mixture(samplers, weights, n_dists, seed); + } + printf("Mean: %f\n", array_mean(result_many, n_samples)); + + // printf("result_many: ["); + // for(int i=0; i<100; i++){ + // printf("%.2f, ", result_many[i]); + // } + // printf("]\n"); + + free(seed); +} diff --git a/examples/02_many_samples_time_to_botec/makefile b/examples/02_many_samples_time_to_botec/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) + ./$(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