C-optimized tweaks.

This commit is contained in:
NunoSempere 2023-05-29 19:04:21 -04:00
parent 28d443a6cf
commit c35ddcc358
3 changed files with 26 additions and 24 deletions

View File

@ -37,7 +37,7 @@ format: $(SRC)
$(FORMATTER) $(SRC) $(FORMATTER) $(SRC)
run: $(SRC) $(OUTPUT) run: $(SRC) $(OUTPUT)
export OMP_NUM_THREADS=4; ./$(OUTPUT) OMP_NUM_THREADS=4 ./$(OUTPUT)
test: $(SRC) $(OUTPUT) test: $(SRC) $(OUTPUT)
OMP_NUM_THREADS=1 ./$(OUTPUT) OMP_NUM_THREADS=1 ./$(OUTPUT)

Binary file not shown.

View File

@ -242,35 +242,37 @@ float split_array_sum(float** meta_array, int length, int divided_into)
int main() int main()
{ {
clock_t start, end;
start = clock();
//initialize randomness //initialize randomness
srand(time(NULL)); srand(time(NULL));
// Toy example
// Declare variables in play clock_t start, end;
float p_a, p_b, p_c; start = clock();
int n_threads = omp_get_max_threads();
// printf("Max threads: %d\n", n_threads);
// omp_set_num_threads(n_threads);
float** dist_mixture = malloc(n_threads * sizeof(float*));
split_array_allocate(dist_mixture, N, n_threads);
// Initialize variables // Toy example
p_a = 0.8; // Declare variables in play
p_b = 0.5; float p_a, p_b, p_c;
p_c = p_a * p_b; int n_threads = omp_get_max_threads();
// printf("Max threads: %d\n", n_threads);
// omp_set_num_threads(n_threads);
float** dist_mixture = malloc(n_threads * sizeof(float*));
split_array_allocate(dist_mixture, N, n_threads);
// Generate mixture // Initialize variables
int n_dists = 4; p_a = 0.8;
float weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 }; p_b = 0.5;
float (*samplers[])(void) = { sample_0, sample_1, sample_few, sample_many }; p_c = p_a * p_b;
mixture_f(samplers, weights, n_dists, dist_mixture, n_threads); // Generate mixture
printf("Sum(dist_mixture, N)/N = %f\n", split_array_sum(dist_mixture, N, n_threads) / N); int n_dists = 4;
float weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
float (*samplers[])(void) = { sample_0, sample_1, sample_few, sample_many };
end = clock(); mixture_f(samplers, weights, n_dists, dist_mixture, n_threads);
split_array_free(dist_mixture, n_threads); printf("Sum(dist_mixture, N)/N = %f\n", split_array_sum(dist_mixture, N, n_threads) / N);
printf("Total time (ms): %f\n", ((double)(end - start)) / CLOCKS_PER_SEC * 1000); split_array_free(dist_mixture, n_threads);
end = clock();
printf("Time (ms): %f\n", ((double)(end - start)) / (CLOCKS_PER_SEC * 10) * 1000);
return 0; return 0;
} }