diff options
| author | enricotenuti <tenutz_27@outlook.it> | 2024-10-26 16:08:17 +0200 |
|---|---|---|
| committer | enricotenuti <tenutz_27@outlook.it> | 2024-10-26 16:08:17 +0200 |
| commit | 0422e6244e9e1a97397be147abb317f96d0154c3 (patch) | |
| tree | 3a3abd8cce12ccc0bcb38f1c32213af2647dcf04 | |
| parent | 09fb2900d3811d76ef145e5f960e5b824bb0bfb6 (diff) | |
| download | nissy-core-0422e6244e9e1a97397be147abb317f96d0154c3.tar.gz nissy-core-0422e6244e9e1a97397be147abb317f96d0154c3.zip | |
Check depth fix
| -rwxr-xr-x | benchmark.sh | 36 | ||||
| -rw-r--r-- | src/solvers/h48/solve.h | 14 | ||||
| -rw-r--r-- | src/solvers/h48/solve_multithread.h | 10 | ||||
| -rw-r--r-- | tools/401_benchmark_performance/benchmark_performance.c | 114 |
4 files changed, 115 insertions, 59 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 | |||
| 15 | 15 | ||
| 16 | case $input in | 16 | case $input in |
| 17 | performance) | 17 | performance) |
| 18 | echo "Performance Benchmark..." | 18 | for i in 1 8 64; do |
| 19 | make clean | 19 | make clean |
| 20 | THREADS=$THREADS ./configure.sh | 20 | THREADS=$i ./configure.sh |
| 21 | TOOL=benchmark_performance TOOLARGS="$ARGS" make tool | 21 | TOOL=benchmark_performance TOOLARGS="$ARGS" make tool |
| 22 | done | ||
| 22 | ;; | 23 | ;; |
| 23 | architecture) | 24 | architecture) |
| 24 | echo "Architecture Benchmark..." | 25 | echo "Architecture Benchmark..." |
| 25 | make clean | 26 | for i in 1 8; do |
| 26 | THREADS=$THREADS ./configure.sh | 27 | make clean |
| 27 | TOOL=benchmark_common TOOLARGS="$ARGS" make tool | 28 | THREADS=$i ARCH=PORTABLE ./configure.sh |
| 28 | make clean | 29 | TOOL=benchmark_performance TOOLARGS="$ARGS" make tool |
| 29 | THREADS=$THREADS ARCH=PORTABLE ./configure.sh | 30 | done |
| 30 | TOOL=benchmark_common TOOLARGS="$ARGS" make tool | ||
| 31 | ;; | 31 | ;; |
| 32 | memory) | 32 | memory) |
| 33 | echo "Memory Benchmark..." | 33 | echo "Memory Benchmark..." |
| 34 | for i in {0..7}; do | 34 | for i in {1..6}; do |
| 35 | make clean | 35 | make clean |
| 36 | THREADS=$THREADS ./configure.sh | 36 | THREADS=$THREADS ./configure.sh |
| 37 | M_ARGS="h48h${i}k2" | 37 | M_ARGS="h48h${i}k2" |
| 38 | TOOL=benchmark_common TOOLARGS="$M_ARGS" make tool | 38 | TOOL=benchmark_performance TOOLARGS="$M_ARGS" make tool |
| 39 | done | 39 | done |
| 40 | ;; | 40 | ;; |
| 41 | nodes) | 41 | nodes) |
| @@ -46,13 +46,18 @@ case $input in | |||
| 46 | ;; | 46 | ;; |
| 47 | multithread) | 47 | multithread) |
| 48 | echo "Multithread Benchmark..." | 48 | echo "Multithread Benchmark..." |
| 49 | make clean | 49 | for i in 64 32 16 8 4 2 1; do |
| 50 | for i in 1 2 4 8 16; do | ||
| 51 | make clean | 50 | make clean |
| 52 | THREADS=$i ./configure.sh | 51 | THREADS=$i ./configure.sh |
| 53 | TOOL=benchmark_common TOOLARGS="$ARGS" make tool | 52 | TOOL=benchmark_performance TOOLARGS="$ARGS" make tool |
| 54 | done | 53 | done |
| 55 | ;; | 54 | ;; |
| 55 | depth) | ||
| 56 | echo "Depth Benchmark..." | ||
| 57 | make clean | ||
| 58 | THREADS=$THREADS ./configure.sh | ||
| 59 | TOOL=benchmark_performance TOOLARGS="$ARGS" make tool | ||
| 60 | ;; | ||
| 56 | *) | 61 | *) |
| 57 | echo "Invalid argument. Possible values:" | 62 | echo "Invalid argument. Possible values:" |
| 58 | echo " - performance" | 63 | echo " - performance" |
| @@ -60,6 +65,7 @@ case $input in | |||
| 60 | echo " - memory" | 65 | echo " - memory" |
| 61 | echo " - nodes" | 66 | echo " - nodes" |
| 62 | echo " - multithread" | 67 | echo " - multithread" |
| 68 | echo " - depth" | ||
| 63 | exit 1 | 69 | exit 1 |
| 64 | ;; | 70 | ;; |
| 65 | esac \ No newline at end of file | 71 | 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) | |||
| 168 | 168 | ||
| 169 | if (solve_h48_stop(arg)) | 169 | if (solve_h48_stop(arg)) |
| 170 | return 0; | 170 | return 0; |
| 171 | 171 | ||
| 172 | if (issolved(arg->cube)) { | 172 | if (arg->nmoves + arg->npremoves == arg->depth){ |
| 173 | if (arg->nmoves + arg->npremoves != arg->depth) | 173 | if(issolved(arg->cube)) { |
| 174 | solve_h48_appendsolution(arg); | ||
| 175 | return 1; | ||
| 176 | } else { | ||
| 174 | return 0; | 177 | return 0; |
| 175 | solve_h48_appendsolution(arg); | 178 | } |
| 176 | return 1; | ||
| 177 | } | 179 | } |
| 178 | 180 | ||
| 179 | nextarg = *arg; | 181 | nextarg = *arg; |
| 180 | ret = 0; | 182 | ret = 0; |
| 181 | uint32_t allowed; | 183 | 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) | |||
| 214 | if (solve_h48_stop(arg)) | 214 | if (solve_h48_stop(arg)) |
| 215 | return 0; | 215 | return 0; |
| 216 | 216 | ||
| 217 | if (issolved(arg->cube)){ | 217 | if (arg->nmoves + arg->npremoves == arg->depth){ |
| 218 | if (arg->nmoves + arg->npremoves != arg->depth) | 218 | if(issolved(arg->cube)) { |
| 219 | solve_h48_appendsolution_thread(arg, tq); | ||
| 220 | return 1; | ||
| 221 | } else { | ||
| 219 | return 0; | 222 | return 0; |
| 220 | solve_h48_appendsolution_thread(arg, tq); | 223 | } |
| 221 | return 1; | ||
| 222 | } | 224 | } |
| 223 | 225 | ||
| 224 | nextarg = *arg; | 226 | 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 @@ | |||
| 5 | #include <time.h> | 5 | #include <time.h> |
| 6 | 6 | ||
| 7 | #define SOL_BUFFER_LEN 1000 | 7 | #define SOL_BUFFER_LEN 1000 |
| 8 | #define MAX_SCRAMBLES 100 // Limite massimo per il numero di scrambles che può essere letto | 8 | #define MAX_SCRAMBLES 200 // Limite massimo per il numero di scrambles che può essere letto |
| 9 | 9 | ||
| 10 | char *scrambles[MAX_SCRAMBLES + 1]; | 10 | char *scrambles[MAX_SCRAMBLES + 1]; |
| 11 | char *solver; | 11 | char *solver; |
| @@ -16,6 +16,26 @@ struct ScrambleArgs { | |||
| 16 | int scramble_idx; | 16 | int scramble_idx; |
| 17 | }; | 17 | }; |
| 18 | 18 | ||
| 19 | double sqrt_approx(double num) { | ||
| 20 | double guess = num / 2.0; // Inizializza una stima approssimata | ||
| 21 | double epsilon = 0.00001; // Precisione dell'approssimazione | ||
| 22 | |||
| 23 | while ((guess * guess - num) > epsilon || (num - guess * guess) > epsilon) { | ||
| 24 | guess = (guess + num / guess) / 2.0; | ||
| 25 | } | ||
| 26 | |||
| 27 | return guess; | ||
| 28 | } | ||
| 29 | |||
| 30 | // Funzione di confronto per l'ordinamento dei tempi (da usare con qsort) | ||
| 31 | int compare_doubles(const void *a, const void *b) { | ||
| 32 | double arg1 = *(const double *)a; | ||
| 33 | double arg2 = *(const double *)b; | ||
| 34 | if (arg1 < arg2) return -1; | ||
| 35 | if (arg1 > arg2) return 1; | ||
| 36 | return 0; | ||
| 37 | } | ||
| 38 | |||
| 19 | // Funzione di timer per un singolo scramble | 39 | // Funzione di timer per un singolo scramble |
| 20 | static double timerun_single_scramble(void (*run)(struct ScrambleArgs *, char *, int *), struct ScrambleArgs *args, char *solution, int *solution_length) { | 40 | static double timerun_single_scramble(void (*run)(struct ScrambleArgs *, char *, int *), struct ScrambleArgs *args, char *solution, int *solution_length) { |
| 21 | struct timespec start, end; | 41 | struct timespec start, end; |
| @@ -64,50 +84,76 @@ void run_scramble(struct ScrambleArgs *args, char *solution, int *solution_lengt | |||
| 64 | } | 84 | } |
| 65 | 85 | ||
| 66 | void run_all_scrambles(void) { | 86 | void run_all_scrambles(void) { |
| 67 | double total_time = 0.0, average_time = 0.0; | 87 | double total_time = 0.0, average_time = 0.0, variance = 0.0, variance_sum = 0.0, std_deviation = 0.0, median = 0.0; |
| 68 | 88 | ||
| 69 | printf("Solved the following scrambles with timings:\n\n"); | 89 | printf("Solved the following scrambles with timings:\n\n"); |
| 70 | 90 | ||
| 71 | FILE *output_file = fopen("./tools/401_benchmark_performance/compressed_results.txt", "w"); | 91 | FILE *output_file = fopen("./tools/401_benchmark_performance/compressed_results.txt", "w"); |
| 72 | if (!output_file) { | 92 | if (!output_file) { |
| 73 | printf("Error: Could not open output file for writing\n"); | 93 | printf("Error: Could not open output file for writing\n"); |
| 74 | return; | 94 | return; |
| 75 | } | 95 | } |
| 76 | 96 | ||
| 77 | int i; | 97 | int i; |
| 78 | for (i = 0; scrambles[i] != NULL; i++) { | 98 | double scramble_times[MAX_SCRAMBLES]; // Array per memorizzare i tempi per ogni scramble |
| 79 | struct ScrambleArgs args; | ||
| 80 | args.scramble_idx = i; | ||
| 81 | 99 | ||
| 82 | char solution[SOL_BUFFER_LEN]; | 100 | for (i = 0; scrambles[i] != NULL; i++) { |
| 83 | int solution_length = 0; | 101 | struct ScrambleArgs args; |
| 102 | args.scramble_idx = i; | ||
| 84 | 103 | ||
| 85 | double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length); | 104 | char solution[SOL_BUFFER_LEN]; |
| 86 | total_time += scramble_time; | 105 | int solution_length = 0; |
| 87 | 106 | ||
| 88 | fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]); | 107 | double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length); |
| 89 | fprintf(output_file, "Solution: %s\n", solution); | 108 | scramble_times[i] = scramble_time; // Salva il tempo per questo scramble |
| 90 | fprintf(output_file, "Length of solution: %d\n", solution_length); | 109 | total_time += scramble_time; |
| 91 | fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time); | ||
| 92 | fprintf(output_file, "-------------------------\n"); | ||
| 93 | 110 | ||
| 94 | printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time); | 111 | fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]); |
| 95 | printf("Lenght: %d\n", solution_length); | 112 | fprintf(output_file, "Solution: %s\n", solution); |
| 96 | } | 113 | fprintf(output_file, "Length of solution: %d\n", solution_length); |
| 114 | fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time); | ||
| 115 | fprintf(output_file, "-------------------------\n"); | ||
| 97 | 116 | ||
| 98 | if (i > 0) { | 117 | printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time); |
| 99 | average_time = total_time / i; | 118 | printf("Length: %d\n", solution_length); |
| 100 | } | 119 | } |
| 120 | |||
| 121 | if (i > 0) { | ||
| 122 | average_time = total_time / i; | ||
| 123 | |||
| 124 | // Calcolo della varianza | ||
| 125 | for (int j = 0; j < i; j++) { | ||
| 126 | variance_sum += (scramble_times[j] - average_time) * (scramble_times[j] - average_time); | ||
| 127 | } | ||
| 128 | variance = variance_sum / i; | ||
| 129 | |||
| 130 | // Calcolo della deviazione standard | ||
| 131 | std_deviation = sqrt_approx(variance); // Usa sqrt_approx invece di sqrt() | ||
| 132 | |||
| 133 | // Ordina i tempi per calcolare la mediana | ||
| 134 | qsort(scramble_times, i, sizeof(double), compare_doubles); | ||
| 135 | |||
| 136 | // Calcolo della mediana | ||
| 137 | if (i % 2 == 0) { | ||
| 138 | median = (scramble_times[i / 2 - 1] + scramble_times[i / 2]) / 2.0; | ||
| 139 | } else { | ||
| 140 | median = scramble_times[i / 2]; | ||
| 141 | } | ||
| 142 | } | ||
| 101 | 143 | ||
| 102 | printf("---------\n"); | 144 | printf("---------\n"); |
| 103 | printf("Total time for all scrambles: %.4fs\n", total_time); | 145 | printf("Total time for all scrambles: %.4fs\n", total_time); |
| 104 | printf("Average time per scramble: %.4fs\n", average_time); | 146 | printf("Average time per scramble: %.4fs\n", average_time); |
| 147 | printf("Median time: %.4fs\n", median); // Stampa della mediana | ||
| 148 | printf("Standard deviation of times: %.4fs\n", std_deviation); // Stampa della deviazione standard | ||
| 105 | 149 | ||
| 106 | fprintf(output_file, "---------\n"); | 150 | fprintf(output_file, "---------\n"); |
| 107 | fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time); | 151 | fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time); |
| 108 | fprintf(output_file, "Average time per scramble: %.4fs\n", average_time); | 152 | fprintf(output_file, "Average time per scramble: %.4fs\n", average_time); |
| 153 | fprintf(output_file, "Median time: %.4fs\n", median); // Scrivi la mediana nel file | ||
| 154 | fprintf(output_file, "Standard deviation of times: %.4fs\n", std_deviation); // Scrivi la deviazione standard nel file | ||
| 109 | 155 | ||
| 110 | fclose(output_file); | 156 | fclose(output_file); |
| 111 | } | 157 | } |
| 112 | 158 | ||
| 113 | int read_scrambles_from_file(const char *filename, char **scrambles) { | 159 | int read_scrambles_from_file(const char *filename, char **scrambles) { |
