From 0422e6244e9e1a97397be147abb317f96d0154c3 Mon Sep 17 00:00:00 2001 From: enricotenuti Date: Sat, 26 Oct 2024 16:08:17 +0200 Subject: Check depth fix --- benchmark.sh | 36 +++--- src/solvers/h48/solve.h | 14 ++- src/solvers/h48/solve_multithread.h | 10 +- .../benchmark_performance.c | 136 ++++++++++++++------- 4 files changed, 126 insertions(+), 70 deletions(-) diff --git a/benchmark.sh b/benchmark.sh index 8f0ccc6..f54b046 100755 --- a/benchmark.sh +++ b/benchmark.sh @@ -15,27 +15,27 @@ THREADS=8 case $input in performance) - echo "Performance Benchmark..." - make clean - THREADS=$THREADS ./configure.sh - TOOL=benchmark_performance TOOLARGS="$ARGS" make tool + for i in 1 8 64; do + make clean + THREADS=$i ./configure.sh + TOOL=benchmark_performance TOOLARGS="$ARGS" make tool + done ;; architecture) echo "Architecture Benchmark..." - make clean - THREADS=$THREADS ./configure.sh - TOOL=benchmark_common TOOLARGS="$ARGS" make tool - make clean - THREADS=$THREADS ARCH=PORTABLE ./configure.sh - TOOL=benchmark_common TOOLARGS="$ARGS" make tool + for i in 1 8; do + make clean + THREADS=$i ARCH=PORTABLE ./configure.sh + TOOL=benchmark_performance TOOLARGS="$ARGS" make tool + done ;; memory) echo "Memory Benchmark..." - for i in {0..7}; do + for i in {1..6}; do make clean THREADS=$THREADS ./configure.sh M_ARGS="h48h${i}k2" - TOOL=benchmark_common TOOLARGS="$M_ARGS" make tool + TOOL=benchmark_performance TOOLARGS="$M_ARGS" make tool done ;; nodes) @@ -46,13 +46,18 @@ case $input in ;; multithread) echo "Multithread Benchmark..." - make clean - for i in 1 2 4 8 16; do + for i in 64 32 16 8 4 2 1; do make clean THREADS=$i ./configure.sh - TOOL=benchmark_common TOOLARGS="$ARGS" make tool + TOOL=benchmark_performance TOOLARGS="$ARGS" make tool done ;; + depth) + echo "Depth Benchmark..." + make clean + THREADS=$THREADS ./configure.sh + TOOL=benchmark_performance TOOLARGS="$ARGS" make tool + ;; *) echo "Invalid argument. Possible values:" echo " - performance" @@ -60,6 +65,7 @@ case $input in echo " - memory" echo " - nodes" echo " - multithread" + echo " - depth" exit 1 ;; esac \ No newline at end of file diff --git a/src/solvers/h48/solve.h b/src/solvers/h48/solve.h index d506596..05b76ed 100644 --- a/src/solvers/h48/solve.h +++ b/src/solvers/h48/solve.h @@ -168,14 +168,16 @@ solve_h48_dfs(dfsarg_solveh48_t *arg) if (solve_h48_stop(arg)) return 0; - - if (issolved(arg->cube)) { - if (arg->nmoves + arg->npremoves != arg->depth) + + if (arg->nmoves + arg->npremoves == arg->depth){ + if(issolved(arg->cube)) { + solve_h48_appendsolution(arg); + return 1; + } else { return 0; - solve_h48_appendsolution(arg); - return 1; + } } - + nextarg = *arg; ret = 0; uint32_t allowed; diff --git a/src/solvers/h48/solve_multithread.h b/src/solvers/h48/solve_multithread.h index 72f519e..e33ed49 100644 --- a/src/solvers/h48/solve_multithread.h +++ b/src/solvers/h48/solve_multithread.h @@ -214,11 +214,13 @@ solve_h48_single(dfsarg_solveh48_t *arg, task_queue_t *tq) if (solve_h48_stop(arg)) return 0; - if (issolved(arg->cube)){ - if (arg->nmoves + arg->npremoves != arg->depth) + if (arg->nmoves + arg->npremoves == arg->depth){ + if(issolved(arg->cube)) { + solve_h48_appendsolution_thread(arg, tq); + return 1; + } else { return 0; - solve_h48_appendsolution_thread(arg, tq); - return 1; + } } nextarg = *arg; diff --git a/tools/401_benchmark_performance/benchmark_performance.c b/tools/401_benchmark_performance/benchmark_performance.c index 97cb51a..97d9bd6 100644 --- a/tools/401_benchmark_performance/benchmark_performance.c +++ b/tools/401_benchmark_performance/benchmark_performance.c @@ -5,7 +5,7 @@ #include #define SOL_BUFFER_LEN 1000 -#define MAX_SCRAMBLES 100 // Limite massimo per il numero di scrambles che può essere letto +#define MAX_SCRAMBLES 200 // Limite massimo per il numero di scrambles che può essere letto char *scrambles[MAX_SCRAMBLES + 1]; char *solver; @@ -16,6 +16,26 @@ struct ScrambleArgs { int scramble_idx; }; +double sqrt_approx(double num) { + double guess = num / 2.0; // Inizializza una stima approssimata + double epsilon = 0.00001; // Precisione dell'approssimazione + + while ((guess * guess - num) > epsilon || (num - guess * guess) > epsilon) { + guess = (guess + num / guess) / 2.0; + } + + return guess; +} + +// Funzione di confronto per l'ordinamento dei tempi (da usare con qsort) +int compare_doubles(const void *a, const void *b) { + double arg1 = *(const double *)a; + double arg2 = *(const double *)b; + if (arg1 < arg2) return -1; + if (arg1 > arg2) return 1; + return 0; +} + // Funzione di timer per un singolo scramble static double timerun_single_scramble(void (*run)(struct ScrambleArgs *, char *, int *), struct ScrambleArgs *args, char *solution, int *solution_length) { struct timespec start, end; @@ -64,50 +84,76 @@ void run_scramble(struct ScrambleArgs *args, char *solution, int *solution_lengt } void run_all_scrambles(void) { - double total_time = 0.0, average_time = 0.0; - - printf("Solved the following scrambles with timings:\n\n"); - - FILE *output_file = fopen("./tools/401_benchmark_performance/compressed_results.txt", "w"); - if (!output_file) { - printf("Error: Could not open output file for writing\n"); - return; - } - - int i; - for (i = 0; scrambles[i] != NULL; i++) { - struct ScrambleArgs args; - args.scramble_idx = i; - - char solution[SOL_BUFFER_LEN]; - int solution_length = 0; - - double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length); - total_time += scramble_time; - - fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]); - fprintf(output_file, "Solution: %s\n", solution); - fprintf(output_file, "Length of solution: %d\n", solution_length); - fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time); - fprintf(output_file, "-------------------------\n"); - - printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time); - printf("Lenght: %d\n", solution_length); - } - - if (i > 0) { - average_time = total_time / i; - } - - printf("---------\n"); - printf("Total time for all scrambles: %.4fs\n", total_time); - printf("Average time per scramble: %.4fs\n", average_time); - - fprintf(output_file, "---------\n"); - fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time); - fprintf(output_file, "Average time per scramble: %.4fs\n", average_time); - - fclose(output_file); + double total_time = 0.0, average_time = 0.0, variance = 0.0, variance_sum = 0.0, std_deviation = 0.0, median = 0.0; + + printf("Solved the following scrambles with timings:\n\n"); + + FILE *output_file = fopen("./tools/401_benchmark_performance/compressed_results.txt", "w"); + if (!output_file) { + printf("Error: Could not open output file for writing\n"); + return; + } + + int i; + double scramble_times[MAX_SCRAMBLES]; // Array per memorizzare i tempi per ogni scramble + + for (i = 0; scrambles[i] != NULL; i++) { + struct ScrambleArgs args; + args.scramble_idx = i; + + char solution[SOL_BUFFER_LEN]; + int solution_length = 0; + + double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length); + scramble_times[i] = scramble_time; // Salva il tempo per questo scramble + total_time += scramble_time; + + fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]); + fprintf(output_file, "Solution: %s\n", solution); + fprintf(output_file, "Length of solution: %d\n", solution_length); + fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time); + fprintf(output_file, "-------------------------\n"); + + printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time); + printf("Length: %d\n", solution_length); + } + + if (i > 0) { + average_time = total_time / i; + + // Calcolo della varianza + for (int j = 0; j < i; j++) { + variance_sum += (scramble_times[j] - average_time) * (scramble_times[j] - average_time); + } + variance = variance_sum / i; + + // Calcolo della deviazione standard + std_deviation = sqrt_approx(variance); // Usa sqrt_approx invece di sqrt() + + // Ordina i tempi per calcolare la mediana + qsort(scramble_times, i, sizeof(double), compare_doubles); + + // Calcolo della mediana + if (i % 2 == 0) { + median = (scramble_times[i / 2 - 1] + scramble_times[i / 2]) / 2.0; + } else { + median = scramble_times[i / 2]; + } + } + + printf("---------\n"); + printf("Total time for all scrambles: %.4fs\n", total_time); + printf("Average time per scramble: %.4fs\n", average_time); + printf("Median time: %.4fs\n", median); // Stampa della mediana + printf("Standard deviation of times: %.4fs\n", std_deviation); // Stampa della deviazione standard + + fprintf(output_file, "---------\n"); + fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time); + fprintf(output_file, "Average time per scramble: %.4fs\n", average_time); + fprintf(output_file, "Median time: %.4fs\n", median); // Scrivi la mediana nel file + fprintf(output_file, "Standard deviation of times: %.4fs\n", std_deviation); // Scrivi la deviazione standard nel file + + fclose(output_file); } int read_scrambles_from_file(const char *filename, char **scrambles) { -- cgit v1.3