diff options
| author | enricotenuti <tenutz_27@outlook.it> | 2024-10-21 16:36:13 +0200 |
|---|---|---|
| committer | enricotenuti <tenutz_27@outlook.it> | 2024-10-21 16:36:13 +0200 |
| commit | 09fb2900d3811d76ef145e5f960e5b824bb0bfb6 (patch) | |
| tree | 87a9cc8caa68218e76460841164289dd1bfe1c5b /tools/402_benchmark_nodes | |
| parent | ea25a7ccad625c4e664dfd114147971b8a2677f3 (diff) | |
| download | nissy-core-09fb2900d3811d76ef145e5f960e5b824bb0bfb6.tar.gz nissy-core-09fb2900d3811d76ef145e5f960e5b824bb0bfb6.zip | |
Benchmark setup
Diffstat (limited to 'tools/402_benchmark_nodes')
| -rw-r--r-- | tools/402_benchmark_nodes/benchmark_nodes.c | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/tools/402_benchmark_nodes/benchmark_nodes.c b/tools/402_benchmark_nodes/benchmark_nodes.c new file mode 100644 index 0000000..9dc0914 --- /dev/null +++ b/tools/402_benchmark_nodes/benchmark_nodes.c | |||
| @@ -0,0 +1,201 @@ | |||
| 1 | #include "../tool.h" | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <time.h> | ||
| 6 | |||
| 7 | #define SOL_BUFFER_LEN 1000 | ||
| 8 | #define MAX_SCRAMBLES 100 | ||
| 9 | |||
| 10 | char *scrambles[MAX_SCRAMBLES + 1]; | ||
| 11 | int scrambles_count = 0; | ||
| 12 | char *solver; | ||
| 13 | int64_t size = 0; | ||
| 14 | char *buf; | ||
| 15 | |||
| 16 | struct ScrambleArgs { | ||
| 17 | int scramble_idx; | ||
| 18 | }; | ||
| 19 | |||
| 20 | static double timerun_single_scramble(void (*run)(struct ScrambleArgs *, char *, int *, long long *), struct ScrambleArgs *args, char *solution, int *solution_length, long long *stats) { | ||
| 21 | struct timespec start, end; | ||
| 22 | double tdiff, tdsec, tdnano; | ||
| 23 | fflush(stdout); | ||
| 24 | |||
| 25 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
| 26 | run(args, solution, solution_length, stats); | ||
| 27 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 28 | |||
| 29 | tdsec = end.tv_sec - start.tv_sec; | ||
| 30 | tdnano = end.tv_nsec - start.tv_nsec; | ||
| 31 | tdiff = tdsec + 1e-9 * tdnano; | ||
| 32 | |||
| 33 | return tdiff; | ||
| 34 | } | ||
| 35 | |||
| 36 | void run_scramble(struct ScrambleArgs *args, char *solution, int *solution_length, long long *stats) { | ||
| 37 | int scramble_idx = args->scramble_idx; | ||
| 38 | int64_t n; | ||
| 39 | char sol[SOL_BUFFER_LEN], cube[22]; | ||
| 40 | |||
| 41 | printf("%d. %s\n", scramble_idx + 1, scrambles[scramble_idx]); | ||
| 42 | printf("Solving scramble %s\n", scrambles[scramble_idx]); | ||
| 43 | |||
| 44 | if (nissy_applymoves(NISSY_SOLVED_CUBE, scrambles[scramble_idx], cube) == -1) { | ||
| 45 | printf("Invalid scramble\n"); | ||
| 46 | strcpy(solution, "Invalid scramble"); | ||
| 47 | *solution_length = 0; | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL, 0, 20, 1, -1, size, buf, SOL_BUFFER_LEN, sol, stats); | ||
| 52 | if (n == 0) { | ||
| 53 | printf("No solution found\n"); | ||
| 54 | strcpy(solution, "No solution found"); | ||
| 55 | *solution_length = 0; | ||
| 56 | } else { | ||
| 57 | printf("Solutions:\n%s\n", sol); | ||
| 58 | strcpy(solution, sol); | ||
| 59 | *solution_length = nissy_countmoves(solution); | ||
| 60 | printf("Nodes visited: %lld\n", stats[0]); | ||
| 61 | printf("Table fallbacks: %lld\n", stats[1]); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | void run_all_scrambles(void) { | ||
| 66 | double total_time = 0.0, average_time = 0.0; | ||
| 67 | long long total_nodes = 0, average_nodes = 0; | ||
| 68 | long long total_fallbacks = 0, average_fallbacks = 0; | ||
| 69 | long long stats[scrambles_count+1][NISSY_SIZE_SOLVE_STATS]; | ||
| 70 | |||
| 71 | printf("Solved the following scrambles with timings:\n\n"); | ||
| 72 | |||
| 73 | FILE *output_file = fopen("./tools/402_benchmark_nodes/compressed_results.txt", "w"); | ||
| 74 | if (!output_file) { | ||
| 75 | printf("Error: Could not open output file for writing\n"); | ||
| 76 | return; | ||
| 77 | } | ||
| 78 | |||
| 79 | int i; | ||
| 80 | for (i = 0; scrambles[i] != NULL; i++) { | ||
| 81 | struct ScrambleArgs args; | ||
| 82 | args.scramble_idx = i; | ||
| 83 | |||
| 84 | char solution[SOL_BUFFER_LEN]; | ||
| 85 | int solution_length = 0; | ||
| 86 | |||
| 87 | double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length, stats[i]); | ||
| 88 | total_time += scramble_time; | ||
| 89 | total_nodes += stats[i][0]; | ||
| 90 | total_fallbacks += stats[i][1]; | ||
| 91 | fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]); | ||
| 92 | fprintf(output_file, "Solution: %s\n", solution); | ||
| 93 | fprintf(output_file, "Length of solution: %d\n", solution_length); | ||
| 94 | fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time); | ||
| 95 | fprintf(output_file, "Nodes visited: %lld\n", stats[i][0]); | ||
| 96 | fprintf(output_file, "Table fallbacks %lld\n", stats[i][1]); | ||
| 97 | fprintf(output_file, "-------------------------\n"); | ||
| 98 | |||
| 99 | printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time); | ||
| 100 | printf("Lenght: %d\n", solution_length); | ||
| 101 | } | ||
| 102 | |||
| 103 | if (i > 0) { | ||
| 104 | average_time = total_time / i; | ||
| 105 | average_nodes = total_nodes / i; | ||
| 106 | average_fallbacks = total_fallbacks / i; | ||
| 107 | } | ||
| 108 | |||
| 109 | printf("---------\n"); | ||
| 110 | printf("Total time for all scrambles: %.4fs\n", total_time); | ||
| 111 | printf("Average time per scramble: %.4fs\n", average_time); | ||
| 112 | printf("---------\n"); | ||
| 113 | printf("Total nodes for all scrambles: %lld\n", total_nodes); | ||
| 114 | printf("Average nodes per scramble: %lld\n", average_nodes); | ||
| 115 | printf("Average Table fallbacks: %lld\n", average_fallbacks); | ||
| 116 | printf("---------\n"); | ||
| 117 | printf("Average Time/Node: %f\n", total_time/total_nodes*1e9); | ||
| 118 | |||
| 119 | fprintf(output_file, "---------\n"); | ||
| 120 | fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time); | ||
| 121 | fprintf(output_file, "Average time per scramble: %.4fs\n", average_time); | ||
| 122 | fprintf(output_file, "---------\n"); | ||
| 123 | fprintf(output_file, "Total nodes for all scrambles: %lld\n", total_nodes); | ||
| 124 | fprintf(output_file, "Average nodes per scramble: %lld\n", average_nodes); | ||
| 125 | fprintf(output_file, "Average Table fallbacks: %lld\n", average_fallbacks); | ||
| 126 | fprintf(output_file, "---------\n"); | ||
| 127 | fprintf(output_file, "Average Time/Node: %f\n", total_time/total_nodes*1e9); | ||
| 128 | fclose(output_file); | ||
| 129 | } | ||
| 130 | |||
| 131 | int read_scrambles_from_file(const char *filename, char **scrambles) { | ||
| 132 | FILE *file = fopen(filename, "r"); | ||
| 133 | if (!file) { | ||
| 134 | printf("Error: Could not open file %s\n", filename); | ||
| 135 | return -1; | ||
| 136 | } | ||
| 137 | |||
| 138 | char line[256]; | ||
| 139 | |||
| 140 | while (fgets(line, sizeof(line), file)) { | ||
| 141 | line[strcspn(line, "\n")] = 0; | ||
| 142 | |||
| 143 | if (strncmp(line, "//", 2) == 0 || strlen(line) == 0) | ||
| 144 | continue; | ||
| 145 | |||
| 146 | scrambles[scrambles_count] = (char *)malloc(strlen(line) + 1); | ||
| 147 | if (scrambles[scrambles_count] == NULL) { | ||
| 148 | printf("Error: Memory allocation failed\n"); | ||
| 149 | fclose(file); | ||
| 150 | return -1; | ||
| 151 | } | ||
| 152 | strcpy(scrambles[scrambles_count], line); | ||
| 153 | scrambles_count++; | ||
| 154 | |||
| 155 | if (scrambles_count >= MAX_SCRAMBLES) { | ||
| 156 | printf("Warning: Reached maximum number of scrambles (%d)\n", MAX_SCRAMBLES); | ||
| 157 | break; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | scrambles[scrambles_count] = NULL; | ||
| 162 | fclose(file); | ||
| 163 | return 0; | ||
| 164 | } | ||
| 165 | |||
| 166 | int main(int argc, char **argv) { | ||
| 167 | char tables_filename[255]; | ||
| 168 | char scrambles_filename[255] = "./tools/402_benchmark_nodes/scrambles.txt"; | ||
| 169 | |||
| 170 | if (argc < 2) { | ||
| 171 | printf("Error: not enough arguments. A solver must be given.\n"); | ||
| 172 | return 1; | ||
| 173 | } | ||
| 174 | |||
| 175 | solver = argv[1]; | ||
| 176 | |||
| 177 | srand(time(NULL)); | ||
| 178 | nissy_setlogger(log_stderr); | ||
| 179 | |||
| 180 | sprintf(tables_filename, "tables/%s", solver); | ||
| 181 | if (getdata(solver, &buf, tables_filename) != 0) | ||
| 182 | return 1; | ||
| 183 | |||
| 184 | size = nissy_datasize(solver); | ||
| 185 | |||
| 186 | if (read_scrambles_from_file(scrambles_filename, scrambles) != 0) { | ||
| 187 | printf("Error: failed to read scrambles from file\n"); | ||
| 188 | free(buf); | ||
| 189 | return 1; | ||
| 190 | } | ||
| 191 | |||
| 192 | run_all_scrambles(); | ||
| 193 | |||
| 194 | free(buf); | ||
| 195 | |||
| 196 | for (int i = 0; scrambles[i] != NULL; i++) { | ||
| 197 | free(scrambles[i]); | ||
| 198 | } | ||
| 199 | |||
| 200 | return 0; | ||
| 201 | } | ||
