diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-08-29 15:51:33 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-08-29 15:51:33 +0200 |
| commit | 7e6c14a2f6e9e9cbac3431f3085c42bbe474d13d (patch) | |
| tree | e570ec2fd344d7251a89a0c9ec0da94162e62cfc /tools | |
| parent | b1fc4886520762dfac016cae612da959078ae67e (diff) | |
| download | nissy-core-7e6c14a2f6e9e9cbac3431f3085c42bbe474d13d.tar.gz nissy-core-7e6c14a2f6e9e9cbac3431f3085c42bbe474d13d.zip | |
Refactor tools to remove duplicate code
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/001_gendata_h48h0k4/gendata_h48h0k4.c | 3 | ||||
| -rw-r--r-- | tools/002_stats_tables_h48/stats_tables_h48.c | 59 | ||||
| -rw-r--r-- | tools/003_solve_small/solve_small.c | 55 | ||||
| -rw-r--r-- | tools/timerun.h | 51 | ||||
| -rw-r--r-- | tools/tool.h | 106 |
5 files changed, 121 insertions, 153 deletions
diff --git a/tools/001_gendata_h48h0k4/gendata_h48h0k4.c b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c index a73f818..0926a34 100644 --- a/tools/001_gendata_h48h0k4/gendata_h48h0k4.c +++ b/tools/001_gendata_h48h0k4/gendata_h48h0k4.c | |||
| @@ -1,5 +1,4 @@ | |||
| 1 | #include "../timerun.h" | 1 | #include "../tool.h" |
| 2 | #include "../../src/nissy.h" | ||
| 3 | 2 | ||
| 4 | #define MAXDEPTH 20 | 3 | #define MAXDEPTH 20 |
| 5 | #define HVALUE 0 | 4 | #define HVALUE 0 |
diff --git a/tools/002_stats_tables_h48/stats_tables_h48.c b/tools/002_stats_tables_h48/stats_tables_h48.c index 2e48497..8b65862 100644 --- a/tools/002_stats_tables_h48/stats_tables_h48.c +++ b/tools/002_stats_tables_h48/stats_tables_h48.c | |||
| @@ -1,22 +1,23 @@ | |||
| 1 | #include <pthread.h> | 1 | #include <pthread.h> |
| 2 | #include <time.h> | 2 | |
| 3 | #include "../timerun.h" | 3 | #include "../tool.h" |
| 4 | #include "../../src/nissy.h" | ||
| 5 | 4 | ||
| 6 | #define MAXMOVES 20 | 5 | #define MAXMOVES 20 |
| 7 | #define NTHREADS 32 | 6 | #define NTHREADS 32 |
| 8 | #define NCUBES_PER_THREAD 10000 | 7 | #define NCUBES_PER_THREAD 10000 |
| 9 | #define LOG_EVERY (NCUBES_PER_THREAD / 10) | 8 | #define LOG_EVERY (NCUBES_PER_THREAD / 10) |
| 10 | 9 | ||
| 10 | const char *solver = "h48stats"; | ||
| 11 | const char *options = ""; | ||
| 12 | const char *filename = "tables/h48h0k4"; | ||
| 13 | char *buf; | ||
| 14 | |||
| 11 | typedef struct { | 15 | typedef struct { |
| 12 | int n; | 16 | int n; |
| 13 | int thread_id; | 17 | int thread_id; |
| 14 | int64_t v[12][100]; | 18 | int64_t v[12][100]; |
| 15 | } thread_arg_t; | 19 | } thread_arg_t; |
| 16 | 20 | ||
| 17 | const char *filename = "tables/h48h0k4"; | ||
| 18 | char *buf; | ||
| 19 | |||
| 20 | uint64_t rand64(void) { | 21 | uint64_t rand64(void) { |
| 21 | uint64_t i, ret; | 22 | uint64_t i, ret; |
| 22 | 23 | ||
| @@ -85,56 +86,12 @@ void run(void) { | |||
| 85 | } | 86 | } |
| 86 | } | 87 | } |
| 87 | 88 | ||
| 88 | int getdata(int64_t size) { | ||
| 89 | int64_t s; | ||
| 90 | FILE *f; | ||
| 91 | |||
| 92 | buf = malloc(size); | ||
| 93 | |||
| 94 | if ((f = fopen(filename, "rb")) == NULL) { | ||
| 95 | fprintf(stderr, "Table file not found, generating them." | ||
| 96 | " This can take a while.\n"); | ||
| 97 | s = nissy_gendata("h48stats", "", buf); | ||
| 98 | if (s != size) { | ||
| 99 | fprintf(stderr, "Error generating table"); | ||
| 100 | if (s != -1) | ||
| 101 | fprintf(stderr, " (got %" PRId64 " bytes)", s); | ||
| 102 | fprintf(stderr, "\n"); | ||
| 103 | return 1; | ||
| 104 | } | ||
| 105 | if ((f = fopen(filename, "wb")) == NULL) { | ||
| 106 | fprintf(stderr, "Could not write tables to file %s" | ||
| 107 | ", will be regenerated next time.\n", filename); | ||
| 108 | } else { | ||
| 109 | fwrite(buf, size, 1, f); | ||
| 110 | fclose(f); | ||
| 111 | } | ||
| 112 | } else { | ||
| 113 | fprintf(stderr, "Reading tables from file %s\n", filename); | ||
| 114 | fread(buf, size, 1, f); | ||
| 115 | fclose(f); | ||
| 116 | } | ||
| 117 | |||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | |||
| 121 | int main(void) { | 89 | int main(void) { |
| 122 | int64_t size; | ||
| 123 | |||
| 124 | srand(time(NULL)); | 90 | srand(time(NULL)); |
| 125 | |||
| 126 | nissy_setlogger(log_stderr); | 91 | nissy_setlogger(log_stderr); |
| 127 | size = nissy_datasize("h48stats", ""); | ||
| 128 | if (size == -1) { | ||
| 129 | printf("h48 stats: error in datasize\n"); | ||
| 130 | return 1; | ||
| 131 | } | ||
| 132 | 92 | ||
| 133 | if (getdata(size) != 0) { | 93 | if (getdata(solver, options, &buf, filename) != 0) |
| 134 | printf("Error getting table, stopping\n"); | ||
| 135 | free(buf); | ||
| 136 | return 1; | 94 | return 1; |
| 137 | } | ||
| 138 | 95 | ||
| 139 | timerun(run, "h48 table stats"); | 96 | timerun(run, "h48 table stats"); |
| 140 | 97 | ||
diff --git a/tools/003_solve_small/solve_small.c b/tools/003_solve_small/solve_small.c index 406e255..dee2b55 100644 --- a/tools/003_solve_small/solve_small.c +++ b/tools/003_solve_small/solve_small.c | |||
| @@ -1,12 +1,12 @@ | |||
| 1 | #include <pthread.h> | 1 | #include <pthread.h> |
| 2 | #include <time.h> | ||
| 3 | #include "../timerun.h" | ||
| 4 | #include "../../src/nissy.h" | ||
| 5 | 2 | ||
| 6 | #define OPTIONS "0;4;20" | 3 | #include "../tool.h" |
| 7 | 4 | ||
| 5 | const char *solver = "h48"; | ||
| 6 | const char *options = "0;4;20"; | ||
| 8 | const char *filename = "tables/h48h0k4"; | 7 | const char *filename = "tables/h48h0k4"; |
| 9 | char *buf; | 8 | char *buf; |
| 9 | |||
| 10 | char *scrambles[] = { | 10 | char *scrambles[] = { |
| 11 | "R D' R2 D R U2 R' D' R U2 R D R'", /* 12 optimal */ | 11 | "R D' R2 D R U2 R' D' R U2 R D R'", /* 12 optimal */ |
| 12 | "RLUD RLUD RLUD", /* 12 optimal */ | 12 | "RLUD RLUD RLUD", /* 12 optimal */ |
| @@ -28,7 +28,7 @@ void run(void) { | |||
| 28 | continue; | 28 | continue; |
| 29 | } | 29 | } |
| 30 | n = nissy_solve( | 30 | n = nissy_solve( |
| 31 | cube, "h48", OPTIONS, "", 0, 20, 1, -1, buf, sol); | 31 | cube, "h48", options, "", 0, 20, 1, -1, buf, sol); |
| 32 | if (n == 0) | 32 | if (n == 0) |
| 33 | fprintf(stderr, "No solution found, " | 33 | fprintf(stderr, "No solution found, " |
| 34 | "continuing with next scramble\n"); | 34 | "continuing with next scramble\n"); |
| @@ -36,56 +36,13 @@ void run(void) { | |||
| 36 | printf("\n"); | 36 | printf("\n"); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | int getdata(int64_t size) { | ||
| 40 | int64_t s; | ||
| 41 | FILE *f; | ||
| 42 | |||
| 43 | buf = malloc(size); | ||
| 44 | |||
| 45 | if ((f = fopen(filename, "rb")) == NULL) { | ||
| 46 | fprintf(stderr, "Table file not found, generating them." | ||
| 47 | " This can take a while.\n"); | ||
| 48 | s = nissy_gendata("h48", OPTIONS, buf); | ||
| 49 | if (s != size) { | ||
| 50 | fprintf(stderr, "Error generating table"); | ||
| 51 | if (s != -1) | ||
| 52 | fprintf(stderr, " (got %" PRId64 " bytes)", s); | ||
| 53 | fprintf(stderr, "\n"); | ||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | if ((f = fopen(filename, "wb")) == NULL) { | ||
| 57 | fprintf(stderr, "Could not write tables to file %s" | ||
| 58 | ", will be regenerated next time.\n", filename); | ||
| 59 | } else { | ||
| 60 | fwrite(buf, size, 1, f); | ||
| 61 | fclose(f); | ||
| 62 | } | ||
| 63 | } else { | ||
| 64 | fprintf(stderr, "Reading tables from file %s\n", filename); | ||
| 65 | fread(buf, size, 1, f); | ||
| 66 | fclose(f); | ||
| 67 | } | ||
| 68 | |||
| 69 | return 0; | ||
| 70 | } | ||
| 71 | |||
| 72 | int main(void) { | 39 | int main(void) { |
| 73 | int64_t size; | ||
| 74 | 40 | ||
| 75 | srand(time(NULL)); | 41 | srand(time(NULL)); |
| 76 | |||
| 77 | nissy_setlogger(log_stderr); | 42 | nissy_setlogger(log_stderr); |
| 78 | size = nissy_datasize("h48", OPTIONS); | ||
| 79 | if (size == -1) { | ||
| 80 | printf("h48 stats: error in datasize\n"); | ||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | 43 | ||
| 84 | if (getdata(size) != 0) { | 44 | if (getdata(solver, options, &buf, filename) != 0) |
| 85 | printf("Error getting table, stopping\n"); | ||
| 86 | free(buf); | ||
| 87 | return 1; | 45 | return 1; |
| 88 | } | ||
| 89 | 46 | ||
| 90 | timerun(run, "small solver benchmark"); | 47 | timerun(run, "small solver benchmark"); |
| 91 | 48 | ||
diff --git a/tools/timerun.h b/tools/timerun.h deleted file mode 100644 index 58a8e7a..0000000 --- a/tools/timerun.h +++ /dev/null | |||
| @@ -1,51 +0,0 @@ | |||
| 1 | #include <stdarg.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <inttypes.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <time.h> | ||
| 7 | |||
| 8 | void | ||
| 9 | log_stderr(const char *str, ...) | ||
| 10 | { | ||
| 11 | va_list args; | ||
| 12 | |||
| 13 | va_start(args, str); | ||
| 14 | vfprintf(stderr, str, args); | ||
| 15 | va_end(args); | ||
| 16 | } | ||
| 17 | |||
| 18 | |||
| 19 | double | ||
| 20 | timerun(void (*run)(void), char *name) | ||
| 21 | { | ||
| 22 | struct timespec start, end; | ||
| 23 | double tdiff, tdsec, tdnano; | ||
| 24 | |||
| 25 | printf("\n"); | ||
| 26 | fflush(stdout); | ||
| 27 | |||
| 28 | if (run == NULL) { | ||
| 29 | printf("> %s: nothing to run!\n", name); | ||
| 30 | fflush(stdout); | ||
| 31 | return -1.0; | ||
| 32 | } | ||
| 33 | |||
| 34 | printf("Running tool: %s\n", name); | ||
| 35 | printf("==========\n"); | ||
| 36 | fflush(stdout); | ||
| 37 | |||
| 38 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
| 39 | run(); | ||
| 40 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 41 | |||
| 42 | tdsec = end.tv_sec - start.tv_sec; | ||
| 43 | tdnano = end.tv_nsec - start.tv_nsec; | ||
| 44 | tdiff = tdsec + 1e-9 * tdnano; | ||
| 45 | |||
| 46 | printf("==========\n"); | ||
| 47 | printf("\nTotal time: %.4fs\n", tdiff); | ||
| 48 | fflush(stdout); | ||
| 49 | |||
| 50 | return tdiff; | ||
| 51 | } | ||
diff --git a/tools/tool.h b/tools/tool.h new file mode 100644 index 0000000..d75342d --- /dev/null +++ b/tools/tool.h | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | #include <time.h> | ||
| 2 | #include <stdarg.h> | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <inttypes.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include <time.h> | ||
| 8 | |||
| 9 | #include "../src/nissy.h" | ||
| 10 | |||
| 11 | static void | ||
| 12 | log_stderr(const char *str, ...) | ||
| 13 | { | ||
| 14 | va_list args; | ||
| 15 | |||
| 16 | va_start(args, str); | ||
| 17 | vfprintf(stderr, str, args); | ||
| 18 | va_end(args); | ||
| 19 | } | ||
| 20 | |||
| 21 | static double | ||
| 22 | timerun(void (*run)(void), char *name) | ||
| 23 | { | ||
| 24 | struct timespec start, end; | ||
| 25 | double tdiff, tdsec, tdnano; | ||
| 26 | |||
| 27 | printf("\n"); | ||
| 28 | fflush(stdout); | ||
| 29 | |||
| 30 | if (run == NULL) { | ||
| 31 | printf("> %s: nothing to run!\n", name); | ||
| 32 | fflush(stdout); | ||
| 33 | return -1.0; | ||
| 34 | } | ||
| 35 | |||
| 36 | printf("Running tool: %s\n", name); | ||
| 37 | printf("==========\n"); | ||
| 38 | fflush(stdout); | ||
| 39 | |||
| 40 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
| 41 | run(); | ||
| 42 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 43 | |||
| 44 | tdsec = end.tv_sec - start.tv_sec; | ||
| 45 | tdnano = end.tv_nsec - start.tv_nsec; | ||
| 46 | tdiff = tdsec + 1e-9 * tdnano; | ||
| 47 | |||
| 48 | printf("==========\n"); | ||
| 49 | printf("\nTotal time: %.4fs\n", tdiff); | ||
| 50 | fflush(stdout); | ||
| 51 | |||
| 52 | return tdiff; | ||
| 53 | } | ||
| 54 | |||
| 55 | static int | ||
| 56 | getdata( | ||
| 57 | const char *solver, | ||
| 58 | const char *options, | ||
| 59 | char **buf, | ||
| 60 | const char *filename | ||
| 61 | ) { | ||
| 62 | int64_t s, size, sizeread; | ||
| 63 | FILE *f; | ||
| 64 | |||
| 65 | if ((size = nissy_datasize(solver, options)) == -1) { | ||
| 66 | printf("Error in datasize\n"); | ||
| 67 | goto getdata_error_nofree; | ||
| 68 | } | ||
| 69 | |||
| 70 | *buf = malloc(size); | ||
| 71 | |||
| 72 | if ((f = fopen(filename, "rb")) == NULL) { | ||
| 73 | fprintf(stderr, "Table file not found, generating them." | ||
| 74 | " This can take a while.\n"); | ||
| 75 | s = nissy_gendata(solver, options, *buf); | ||
| 76 | if (s != size) { | ||
| 77 | fprintf(stderr, "Error generating table"); | ||
| 78 | if (s != -1) | ||
| 79 | fprintf(stderr, " (got %" PRId64 " bytes)", s); | ||
| 80 | fprintf(stderr, "\n"); | ||
| 81 | goto getdata_error; | ||
| 82 | } | ||
| 83 | if ((f = fopen(filename, "wb")) == NULL) { | ||
| 84 | fprintf(stderr, "Could not write tables to file %s" | ||
| 85 | ", will be regenerated next time.\n", filename); | ||
| 86 | } else { | ||
| 87 | fwrite(*buf, size, 1, f); | ||
| 88 | fclose(f); | ||
| 89 | } | ||
| 90 | } else { | ||
| 91 | fprintf(stderr, "Reading tables from file %s\n", filename); | ||
| 92 | sizeread = fread(*buf, size, 1, f); | ||
| 93 | fclose(f); | ||
| 94 | if (sizeread != 1) { | ||
| 95 | fprintf(stderr, "Error reading table, stopping\n"); | ||
| 96 | goto getdata_error; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | return 0; | ||
| 101 | |||
| 102 | getdata_error: | ||
| 103 | free(*buf); | ||
| 104 | getdata_error_nofree: | ||
| 105 | return 1; | ||
| 106 | } | ||
