commit c35ddcc358ddc727c4cdfbc6a27bf9a40bd3ff1e
parent 28d443a6cfe032f852d32493670db34eb611e525
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Mon, 29 May 2023 19:04:21 -0400
C-optimized tweaks.
Diffstat:
3 files changed, 31 insertions(+), 29 deletions(-)
diff --git a/C-optimized/makefile b/C-optimized/makefile
@@ -37,7 +37,7 @@ format: $(SRC)
$(FORMATTER) $(SRC)
run: $(SRC) $(OUTPUT)
- export OMP_NUM_THREADS=4; ./$(OUTPUT)
+ OMP_NUM_THREADS=4 ./$(OUTPUT)
test: $(SRC) $(OUTPUT)
OMP_NUM_THREADS=1 ./$(OUTPUT)
diff --git a/C-optimized/out/samples b/C-optimized/out/samples
Binary files differ.
diff --git a/C-optimized/samples.c b/C-optimized/samples.c
@@ -242,35 +242,37 @@ float split_array_sum(float** meta_array, int length, int divided_into)
int main()
{
- clock_t start, end;
- start = clock();
//initialize randomness
srand(time(NULL));
- // Toy example
- // Declare variables in play
- float p_a, p_b, p_c;
- 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
- p_a = 0.8;
- p_b = 0.5;
- p_c = p_a * p_b;
-
- // Generate mixture
- 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 };
-
- mixture_f(samplers, weights, n_dists, dist_mixture, n_threads);
- printf("Sum(dist_mixture, N)/N = %f\n", split_array_sum(dist_mixture, N, n_threads) / N);
-
- end = clock();
- split_array_free(dist_mixture, n_threads);
-
- printf("Total time (ms): %f\n", ((double)(end - start)) / CLOCKS_PER_SEC * 1000);
+
+ clock_t start, end;
+ start = clock();
+
+ // Toy example
+ // Declare variables in play
+ float p_a, p_b, p_c;
+ 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
+ p_a = 0.8;
+ p_b = 0.5;
+ p_c = p_a * p_b;
+
+ // Generate mixture
+ 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 };
+
+ mixture_f(samplers, weights, n_dists, dist_mixture, n_threads);
+ printf("Sum(dist_mixture, N)/N = %f\n", split_array_sum(dist_mixture, N, n_threads) / N);
+
+ split_array_free(dist_mixture, n_threads);
+
+ end = clock();
+ printf("Time (ms): %f\n", ((double)(end - start)) / (CLOCKS_PER_SEC * 10) * 1000);
return 0;
}