reformat & remake

This commit is contained in:
NunoSempere 2023-11-29 23:10:22 +00:00
parent 186b10cddf
commit ca1f81444e
23 changed files with 55 additions and 49 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -13,7 +13,8 @@ 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));

View File

@ -17,7 +17,8 @@ int main()
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 };
double sampler_result(uint64_t* seed) {
double sampler_result(uint64_t * seed)
{
return sample_mixture(samplers, weights, n_dists, seed);
}

View File

@ -13,7 +13,8 @@ int main()
/* Option 1: parallelize taking from n samples */
// Question being asked: what is the distribution of sampling 1000 times and taking the min?
double sample_min_of_n(uint64_t* seed, int n){
double sample_min_of_n(uint64_t * seed, int n)
{
double min = sample_normal(5, 2, seed);
for (int i = 0; i < (n - 2); i++) {
double sample = sample_normal(5, 2, seed);
@ -23,7 +24,8 @@ int main()
}
return min;
}
double sample_min_of_1000(uint64_t* seed) {
double sample_min_of_1000(uint64_t * seed)
{
return sample_min_of_n(seed, 1000);
}
@ -35,7 +37,8 @@ int main()
/* Option 2: take the min from n samples cleverly using parallelism */
// Question being asked: can we take the min of n samples cleverly?
double sample_n_parallel(int n){
double sample_n_parallel(int n)
{
int n_threads = 16;
int quotient = n / 16;
@ -44,7 +47,8 @@ int main()
uint64_t seed = 1000;
double result_remainder = sample_min_of_n(&seed, remainder);
double sample_min_of_quotient(uint64_t* seed) {
double sample_min_of_quotient(uint64_t * seed)
{
return sample_min_of_n(seed, quotient);
}
double* results_quotient = malloc(quotient * sizeof(double));
@ -63,5 +67,4 @@ int main()
return min;
}
printf("Minimum of 1M samples of normal(5,2): %f\n", sample_n_parallel(1000000));
}

View File

@ -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

View File

@ -1,14 +1,15 @@
#include "squiggle.h"
#include <float.h>
#include <math.h>
#include <limits.h>
#include <math.h>
#include <omp.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "squiggle.h"
/* Parallel sampler */
void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_threads, int n_samples){
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);
@ -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);
@ -110,7 +112,8 @@ ci array_get_90_ci(double xs[], int 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,7 +121,6 @@ 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)
{
@ -390,4 +392,3 @@ void array_print(double xs[], int n)
printf("%f", xs[n - 1]);
printf("]\n");
}