more work on squiggle_c
This commit is contained in:
parent
39652b3bfe
commit
3b29ad7e45
Binary file not shown.
50
C/squiggle_c/examples/01_one_sample/example.c
Normal file
50
C/squiggle_c/examples/01_one_sample/example.c
Normal file
|
@ -0,0 +1,50 @@
|
|||
#include "../../squiggle.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Estimate functions
|
||||
float sample_0(uint32_t* seed)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
float sample_1(uint32_t* seed)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
float sample_few(uint32_t* seed)
|
||||
{
|
||||
return random_to(1, 3, seed);
|
||||
}
|
||||
|
||||
float sample_many(uint32_t* seed)
|
||||
{
|
||||
return random_to(2, 10, seed);
|
||||
}
|
||||
|
||||
int main(){
|
||||
// set randomness seed
|
||||
uint32_t* seed = malloc(sizeof(uint32_t));
|
||||
*seed = 1000; // xorshift can't start with 0
|
||||
|
||||
float p_a = 0.8;
|
||||
float p_b = 0.5;
|
||||
float p_c = p_a * p_b;
|
||||
|
||||
int n_dists = 4;
|
||||
float weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
|
||||
float (*samplers[])(uint32_t*) = { sample_0, sample_1, sample_few, sample_many };
|
||||
|
||||
float result_one = mixture(samplers, weights, n_dists, seed);
|
||||
printf("result_one: %f\n", result_one);
|
||||
}
|
||||
|
||||
/*
|
||||
Aggregation mechanisms:
|
||||
- Quantiles (requires a sort)
|
||||
- Sum
|
||||
- Average
|
||||
- Std
|
||||
*/
|
BIN
C/squiggle_c/examples/02_many_samples/example
Executable file
BIN
C/squiggle_c/examples/02_many_samples/example
Executable file
Binary file not shown.
|
@ -1,7 +1,7 @@
|
|||
#include "../squiggle.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "../../squiggle.h"
|
||||
|
||||
// Estimate functions
|
||||
float sample_0(uint32_t* seed)
|
||||
|
@ -37,9 +37,6 @@ int main(){
|
|||
float weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
|
||||
float (*samplers[])(uint32_t*) = { sample_0, sample_1, sample_few, sample_many };
|
||||
|
||||
float result_one = mixture(samplers, weights, n_dists, seed);
|
||||
printf("result_one: %f\n", result_one);
|
||||
|
||||
int n_samples = 1000000;
|
||||
float* result_many = (float *) malloc(n_samples * sizeof(float));
|
||||
for(int i=0; i<n_samples; i++){
|
53
C/squiggle_c/examples/02_many_samples/makefile
Normal file
53
C/squiggle_c/examples/02_many_samples/makefile
Normal file
|
@ -0,0 +1,53 @@
|
|||
# Interface:
|
||||
# make
|
||||
# make build
|
||||
# make format
|
||||
# make run
|
||||
|
||||
# Compiler
|
||||
CC=gcc
|
||||
# CC=tcc # <= faster compilation
|
||||
|
||||
# Main file
|
||||
SRC=example.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
|
Loading…
Reference in New Issue
Block a user