Compare commits

..

6 Commits

26 changed files with 71 additions and 49 deletions

View File

@ -280,7 +280,13 @@ Overall, I would caution that if you really care about the very far tails of dis
In fact, squiggle.c does have a few functions for algebraic manipulations of simple distributions at the end of squiggle.c. But these are pretty rudimentary, and I don't know whether I'll end up expanding or deleting them. In fact, squiggle.c does have a few functions for algebraic manipulations of simple distributions at the end of squiggle.c. But these are pretty rudimentary, and I don't know whether I'll end up expanding or deleting them.
### Results of running clang-tidy ### Compiler warnings
#### Harsh compilation
By default, I've enabled -Warn-all, -Warn-extra. However, these produce warnings for unused variables, some of which can sometimes be inocuous. For now, I'm suppressing these using a clever UNUSED macro. If you don't like that approach, you could add -Wno-unused-parameter to your makefile and remove the macro.
#### Results of running clang-tidy
clang-tidy is a utility to detect common errors in C/C++. You can run it with: clang-tidy is a utility to detect common errors in C/C++. You can run it with:

View File

@ -5,11 +5,13 @@
// Estimate functions // Estimate functions
double sample_0(uint64_t* seed) double sample_0(uint64_t* seed)
{ {
UNUSED(seed);
return 0; return 0;
} }
double sample_1(uint64_t* seed) double sample_1(uint64_t* seed)
{ {
UNUSED(seed);
return 1; return 1;
} }

View File

@ -12,8 +12,8 @@ int main()
double p_b = 0.5; double p_b = 0.5;
double p_c = p_a * p_b; double p_c = p_a * p_b;
double sample_0(uint64_t * seed) { return 0; } double sample_0(uint64_t * seed) { UNUSED(seed); return 0; }
double sample_1(uint64_t * seed) { return 1; } double sample_1(uint64_t * seed) { UNUSED(seed); return 1; }
double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); } double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); }
double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); } double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); }

View File

@ -15,8 +15,8 @@ int main()
int n_dists = 4; int n_dists = 4;
// These are nested functions. They will not compile without gcc. // These are nested functions. They will not compile without gcc.
double sample_0(uint64_t * seed) { return 0; } double sample_0(uint64_t * seed) { UNUSED(seed); return 0; }
double sample_1(uint64_t * seed) { return 1; } double sample_1(uint64_t * seed) { UNUSED(seed); return 1; }
double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); } double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); }
double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); } double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); }

View File

@ -23,8 +23,8 @@ DEPS=$(SQUIGGLE) $(MATH)
## Flags ## Flags
DEBUG= #'-g' DEBUG= #'-g'
WARN=-Wall -Wextra
STANDARD=-std=c99 STANDARD=-std=c99
WARNINGS=-Wall
OPTIMIZED=-O3 #-Ofast OPTIMIZED=-O3 #-Ofast
## Formatter ## Formatter
@ -33,12 +33,12 @@ FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT)
## make all ## make all
all: all:
$(CC) $(OPTIMIZED) $(DEBUG) 00_example_template/$(SRC) $(DEPS) -o 00_example_template/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 00_example_template/$(SRC) $(DEPS) -o 00_example_template/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 01_one_sample/$(SRC) $(DEPS) -o 01_one_sample/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 01_one_sample/$(SRC) $(DEPS) -o 01_one_sample/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 02_time_to_botec/$(SRC) $(DEPS) -o 02_time_to_botec/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 02_time_to_botec/$(SRC) $(DEPS) -o 02_time_to_botec/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 03_gcc_nested_function/$(SRC) $(DEPS) -o 03_gcc_nested_function/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 03_gcc_nested_function/$(SRC) $(DEPS) -o 03_gcc_nested_function/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 04_gamma_beta/$(SRC) $(DEPS) -o 04_gamma_beta/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 04_gamma_beta/$(SRC) $(DEPS) -o 04_gamma_beta/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 05_hundred_lognormals/$(SRC) $(DEPS) -o 05_hundred_lognormals/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 05_hundred_lognormals/$(SRC) $(DEPS) -o 05_hundred_lognormals/$(OUTPUT)
format-all: format-all:
$(FORMATTER) 00_example_template/$(SRC) $(FORMATTER) 00_example_template/$(SRC)
@ -58,7 +58,7 @@ run-all:
## make one DIR=01_one_sample ## make one DIR=01_one_sample
one: $(DIR)/$(SRC) one: $(DIR)/$(SRC)
$(CC) $(OPTIMIZED) $(DEBUG) $(DIR)/$(SRC) $(DEPS) -o $(DIR)/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) $(DIR)/$(SRC) $(DEPS) -o $(DIR)/$(OUTPUT)
## make format-one DIR=01_one_sample ## make format-one DIR=01_one_sample
format-one: $(DIR)/$(SRC) format-one: $(DIR)/$(SRC)
@ -78,7 +78,7 @@ time-linux-one: $(DIR)/$(OUTPUT)
profile-linux-one: profile-linux-one:
echo "Requires perf, which depends on the kernel version, and might be in linux-tools package or similar" echo "Requires perf, which depends on the kernel version, and might be in linux-tools package or similar"
echo "Must be run as sudo" echo "Must be run as sudo"
$(CC) $(OPTIMIZED) $(DEBUG) $(DIR)/$(SRC) $(DEPS) -o $(DIR)/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) $(DIR)/$(SRC) $(DEPS) -o $(DIR)/$(OUTPUT)
# $(CC) $(SRC) $(DEPS) -o $(OUTPUT) # $(CC) $(SRC) $(DEPS) -o $(OUTPUT)
sudo perf record $(DIR)/$(OUTPUT) sudo perf record $(DIR)/$(OUTPUT)
sudo perf report sudo perf report

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -5,11 +5,13 @@
double sample_0(uint64_t* seed) double sample_0(uint64_t* seed)
{ {
UNUSED(seed);
return 0; return 0;
} }
double sample_1(uint64_t* seed) double sample_1(uint64_t* seed)
{ {
UNUSED(seed);
return 1; return 1;
} }

View File

@ -9,8 +9,8 @@ int main()
double p_b = 0.5; double p_b = 0.5;
double p_c = p_a * p_b; double p_c = p_a * p_b;
double sample_0(uint64_t * seed) { return 0; } double sample_0(uint64_t * seed) { UNUSED(seed); return 0; }
double sample_1(uint64_t * seed) { return 1; } double sample_1(uint64_t * seed) { UNUSED(seed); return 1; }
double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); } double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); }
double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); } double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); }

View File

@ -25,6 +25,7 @@ DEPS=$(SQUIGGLE) $(SQUIGGLE_MORE) $(MATH) $(OPENMP)
## Flags ## Flags
DEBUG= #'-g' DEBUG= #'-g'
WARN=-Wall -Wextra
STANDARD=-std=c99 STANDARD=-std=c99
WARNINGS=-Wall WARNINGS=-Wall
OPTIMIZED=-O3 #-Ofast OPTIMIZED=-O3 #-Ofast
@ -35,21 +36,21 @@ FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT)
## make all ## make all
all: all:
$(CC) $(OPTIMIZED) $(DEBUG) 00_example_template/$(SRC) $(DEPS) -o 00_example_template/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 00_example_template/$(SRC) $(DEPS) -o 00_example_template/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 01_sample_from_cdf/$(SRC) $(DEPS) -o 01_sample_from_cdf/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 01_sample_from_cdf/$(SRC) $(DEPS) -o 01_sample_from_cdf/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 02_sample_from_cdf_beta/$(SRC) $(DEPS) -o 02_sample_from_cdf_beta/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 02_sample_from_cdf_beta/$(SRC) $(DEPS) -o 02_sample_from_cdf_beta/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 03_ci_beta/$(SRC) $(DEPS) -o 03_ci_beta/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 03_ci_beta/$(SRC) $(DEPS) -o 03_ci_beta/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 04_nuclear_war/$(SRC) $(DEPS) -o 04_nuclear_war/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 04_nuclear_war/$(SRC) $(DEPS) -o 04_nuclear_war/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 05_burn_10kg_fat/$(SRC) $(DEPS) -o 05_burn_10kg_fat/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 05_burn_10kg_fat/$(SRC) $(DEPS) -o 05_burn_10kg_fat/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 06_nuclear_recovery/$(SRC) $(DEPS) -o 06_nuclear_recovery/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 06_nuclear_recovery/$(SRC) $(DEPS) -o 06_nuclear_recovery/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 07_algebra/$(SRC) $(DEPS) -o 07_algebra/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 07_algebra/$(SRC) $(DEPS) -o 07_algebra/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 08_algebra_and_conversion/$(SRC) $(DEPS) -o 08_algebra_and_conversion/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 08_algebra_and_conversion/$(SRC) $(DEPS) -o 08_algebra_and_conversion/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 09_ergonomic_algebra/$(SRC) $(DEPS) -o 09_ergonomic_algebra/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 09_ergonomic_algebra/$(SRC) $(DEPS) -o 09_ergonomic_algebra/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 10_twitter_thread_example/$(SRC) $(DEPS) -o 10_twitter_thread_example/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 10_twitter_thread_example/$(SRC) $(DEPS) -o 10_twitter_thread_example/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 11_billion_lognormals_paralell/$(SRC) $(DEPS) -o 11_billion_lognormals_paralell/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 11_billion_lognormals_paralell/$(SRC) $(DEPS) -o 11_billion_lognormals_paralell/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 12_time_to_botec_parallel/$(SRC) $(DEPS) -o 12_time_to_botec_parallel/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 12_time_to_botec_parallel/$(SRC) $(DEPS) -o 12_time_to_botec_parallel/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 13_parallelize_min/$(SRC) $(DEPS) -o 13_parallelize_min/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 13_parallelize_min/$(SRC) $(DEPS) -o 13_parallelize_min/$(OUTPUT)
$(CC) $(OPTIMIZED) $(DEBUG) 14_check_confidence_interval/$(SRC) $(DEPS) -o 14_check_confidence_interval/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) 14_check_confidence_interval/$(SRC) $(DEPS) -o 14_check_confidence_interval/$(OUTPUT)
format-all: format-all:
$(FORMATTER) 00_example_template/$(SRC) $(FORMATTER) 00_example_template/$(SRC)
@ -87,7 +88,7 @@ run-all:
## make one DIR=06_nuclear_recovery ## make one DIR=06_nuclear_recovery
one: $(DIR)/$(SRC) one: $(DIR)/$(SRC)
$(CC) $(OPTIMIZED) $(DEBUG) $(DIR)/$(SRC) $(DEPS) -o $(DIR)/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) $(DIR)/$(SRC) $(DEPS) -o $(DIR)/$(OUTPUT)
## make format-one DIR=06_nuclear_recovery ## make format-one DIR=06_nuclear_recovery
format-one: $(DIR)/$(SRC) format-one: $(DIR)/$(SRC)
@ -107,7 +108,7 @@ time-linux-one: $(DIR)/$(OUTPUT)
profile-linux-one: profile-linux-one:
echo "Requires perf, which depends on the kernel version, and might be in linux-tools package or similar" echo "Requires perf, which depends on the kernel version, and might be in linux-tools package or similar"
echo "Must be run as sudo" echo "Must be run as sudo"
$(CC) $(OPTIMIZED) $(DEBUG) $(DIR)/$(SRC) $(DEPS) -o $(DIR)/$(OUTPUT) $(CC) $(OPTIMIZED) $(DEBUG) $(WARN) $(DIR)/$(SRC) $(DEPS) -o $(DIR)/$(OUTPUT)
# $(CC) $(SRC) $(DEPS) -o $(OUTPUT) # $(CC) $(SRC) $(DEPS) -o $(OUTPUT)
sudo perf record $(DIR)/$(OUTPUT) sudo perf record $(DIR)/$(OUTPUT)
sudo perf report sudo perf report

View File

@ -3,12 +3,14 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
// math constants // Defs
#define PI 3.14159265358979323846 // M_PI in gcc gnu99 #define PI 3.14159265358979323846 // M_PI in gcc gnu99
#define NORMAL90CONFIDENCE 1.6448536269514727 #define NORMAL90CONFIDENCE 1.6448536269514727
#define UNUSED(x) (void)(x)
// ^ https://stackoverflow.com/questions/3599160/how-can-i-suppress-unused-parameter-warnings-in-c
// Pseudo Random number generator // Pseudo Random number generators
static uint64_t xorshift32(uint32_t* seed) static uint64_t xorshift64(uint64_t* seed)
{ {
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" // Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
// See: // See:
@ -19,19 +21,20 @@ static uint64_t xorshift32(uint32_t* seed)
// <https://prng.di.unimi.it/> // <https://prng.di.unimi.it/>
uint64_t x = *seed; uint64_t x = *seed;
x ^= x << 13; x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return *seed = x;
}
static uint64_t xorshift64(uint64_t* seed)
{
// same as above, but for generating doubles instead of floats
uint64_t x = *seed;
x ^= x << 13;
x ^= x >> 7; x ^= x >> 7;
x ^= x << 17; x ^= x << 17;
return *seed = x; return *seed = x;
/*
// if one wanted to generate 32 bit ints,
// from which to generate floats,
// one could do the following:
uint32_t x = *seed;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return *seed = x;
*/
} }
// Distribution & sampling functions // Distribution & sampling functions

View File

@ -30,4 +30,7 @@ double array_std(double* array, int length);
// Mixture function // Mixture function
double sample_mixture(double (*samplers[])(uint64_t*), double* weights, int n_dists, uint64_t* seed); double sample_mixture(double (*samplers[])(uint64_t*), double* weights, int n_dists, uint64_t* seed);
// Macro to mute "unused variable" warning when -Wall -Wextra is enabled. Useful for nested functions
#define UNUSED(x) (void)(x)
#endif #endif

View File

@ -30,7 +30,7 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_
uint64_t** seeds = malloc(n_threads * sizeof(uint64_t*)); uint64_t** seeds = malloc(n_threads * sizeof(uint64_t*));
srand(1); srand(1);
for (uint64_t i = 0; i < n_threads; i++) { for (int i = 0; i < n_threads; i++) {
seeds[i] = malloc(sizeof(uint64_t)); seeds[i] = malloc(sizeof(uint64_t));
// Constraints: // Constraints:
// - xorshift can't start with 0 // - xorshift can't start with 0
@ -61,7 +61,7 @@ void sampler_parallel(double (*sampler)(uint64_t* seed), double* results, int n_
// we can just reuse a seed, this isn't problematic because we are not doing multithreading // we can just reuse a seed, this isn't problematic because we are not doing multithreading
} }
for (uint64_t i = 0; i < n_threads; i++) { for (int i = 0; i < n_threads; i++) {
free(seeds[i]); free(seeds[i]);
} }
free(seeds); free(seeds);
@ -83,6 +83,10 @@ static void swp(int i, int j, double xs[])
static int partition(int low, int high, double xs[], int length) static int partition(int low, int high, double xs[], int length)
{ {
if(low > high || high >= length){
printf("Invariant violated for function partition in %s (%d)", __FILE__, __LINE__);
exit(1);
}
// Note: the scratchpad/ folder in commit 578bfa27 has printfs sprinkled throughout // Note: the scratchpad/ folder in commit 578bfa27 has printfs sprinkled throughout
int pivot = low + floor((high - low) / 2); int pivot = low + floor((high - low) / 2);
double pivot_value = xs[pivot]; double pivot_value = xs[pivot];
@ -136,6 +140,7 @@ ci array_get_90_ci(double xs[], int 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)
{ {
UNUSED(seed); // don't want to use it right now, but want to preserve ability to do so (e.g., remove parallelism from internals). Also nicer for consistency.
double* xs = malloc(n * sizeof(double)); double* xs = malloc(n * sizeof(double));
sampler_parallel(sampler, xs, 16, n); sampler_parallel(sampler, xs, 16, n);
ci result = array_get_ci(interval, xs, n); ci result = array_get_ci(interval, xs, n);
@ -216,7 +221,7 @@ typedef struct box_t {
box process_error(const char* error_msg, int should_exit, char* file, int line) box process_error(const char* error_msg, int should_exit, char* file, int line)
{ {
if (should_exit) { if (should_exit) {
printf("@, in %s (%d)", file, line); printf("%s, @, in %s (%d)", error_msg, file, line);
exit(1); exit(1);
} else { } else {
char error_msg[MAX_ERROR_LENGTH]; char error_msg[MAX_ERROR_LENGTH];