h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

commit 7e6c14a2f6e9e9cbac3431f3085c42bbe474d13d
parent b1fc4886520762dfac016cae612da959078ae67e
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Thu, 29 Aug 2024 15:51:33 +0200

Refactor tools to remove duplicate code

Diffstat:
Mtools/001_gendata_h48h0k4/gendata_h48h0k4.c | 3+--
Mtools/002_stats_tables_h48/stats_tables_h48.c | 59++++++++---------------------------------------------------
Mtools/003_solve_small/solve_small.c | 55++++++-------------------------------------------------
Dtools/timerun.h | 51---------------------------------------------------
Atools/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 @@ -1,5 +1,4 @@ -#include "../timerun.h" -#include "../../src/nissy.h" +#include "../tool.h" #define MAXDEPTH 20 #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 @@ -1,22 +1,23 @@ #include <pthread.h> -#include <time.h> -#include "../timerun.h" -#include "../../src/nissy.h" + +#include "../tool.h" #define MAXMOVES 20 #define NTHREADS 32 #define NCUBES_PER_THREAD 10000 #define LOG_EVERY (NCUBES_PER_THREAD / 10) +const char *solver = "h48stats"; +const char *options = ""; +const char *filename = "tables/h48h0k4"; +char *buf; + typedef struct { int n; int thread_id; int64_t v[12][100]; } thread_arg_t; -const char *filename = "tables/h48h0k4"; -char *buf; - uint64_t rand64(void) { uint64_t i, ret; @@ -85,56 +86,12 @@ void run(void) { } } -int getdata(int64_t size) { - int64_t s; - FILE *f; - - buf = malloc(size); - - if ((f = fopen(filename, "rb")) == NULL) { - fprintf(stderr, "Table file not found, generating them." - " This can take a while.\n"); - s = nissy_gendata("h48stats", "", buf); - if (s != size) { - fprintf(stderr, "Error generating table"); - if (s != -1) - fprintf(stderr, " (got %" PRId64 " bytes)", s); - fprintf(stderr, "\n"); - return 1; - } - if ((f = fopen(filename, "wb")) == NULL) { - fprintf(stderr, "Could not write tables to file %s" - ", will be regenerated next time.\n", filename); - } else { - fwrite(buf, size, 1, f); - fclose(f); - } - } else { - fprintf(stderr, "Reading tables from file %s\n", filename); - fread(buf, size, 1, f); - fclose(f); - } - - return 0; -} - int main(void) { - int64_t size; - srand(time(NULL)); - nissy_setlogger(log_stderr); - size = nissy_datasize("h48stats", ""); - if (size == -1) { - printf("h48 stats: error in datasize\n"); - return 1; - } - if (getdata(size) != 0) { - printf("Error getting table, stopping\n"); - free(buf); + if (getdata(solver, options, &buf, filename) != 0) return 1; - } timerun(run, "h48 table stats"); diff --git a/tools/003_solve_small/solve_small.c b/tools/003_solve_small/solve_small.c @@ -1,12 +1,12 @@ #include <pthread.h> -#include <time.h> -#include "../timerun.h" -#include "../../src/nissy.h" -#define OPTIONS "0;4;20" +#include "../tool.h" +const char *solver = "h48"; +const char *options = "0;4;20"; const char *filename = "tables/h48h0k4"; char *buf; + char *scrambles[] = { "R D' R2 D R U2 R' D' R U2 R D R'", /* 12 optimal */ "RLUD RLUD RLUD", /* 12 optimal */ @@ -28,7 +28,7 @@ void run(void) { continue; } n = nissy_solve( - cube, "h48", OPTIONS, "", 0, 20, 1, -1, buf, sol); + cube, "h48", options, "", 0, 20, 1, -1, buf, sol); if (n == 0) fprintf(stderr, "No solution found, " "continuing with next scramble\n"); @@ -36,56 +36,13 @@ void run(void) { printf("\n"); } -int getdata(int64_t size) { - int64_t s; - FILE *f; - - buf = malloc(size); - - if ((f = fopen(filename, "rb")) == NULL) { - fprintf(stderr, "Table file not found, generating them." - " This can take a while.\n"); - s = nissy_gendata("h48", OPTIONS, buf); - if (s != size) { - fprintf(stderr, "Error generating table"); - if (s != -1) - fprintf(stderr, " (got %" PRId64 " bytes)", s); - fprintf(stderr, "\n"); - return 1; - } - if ((f = fopen(filename, "wb")) == NULL) { - fprintf(stderr, "Could not write tables to file %s" - ", will be regenerated next time.\n", filename); - } else { - fwrite(buf, size, 1, f); - fclose(f); - } - } else { - fprintf(stderr, "Reading tables from file %s\n", filename); - fread(buf, size, 1, f); - fclose(f); - } - - return 0; -} - int main(void) { - int64_t size; srand(time(NULL)); - nissy_setlogger(log_stderr); - size = nissy_datasize("h48", OPTIONS); - if (size == -1) { - printf("h48 stats: error in datasize\n"); - return 1; - } - if (getdata(size) != 0) { - printf("Error getting table, stopping\n"); - free(buf); + if (getdata(solver, options, &buf, filename) != 0) return 1; - } timerun(run, "small solver benchmark"); diff --git a/tools/timerun.h b/tools/timerun.h @@ -1,51 +0,0 @@ -#include <stdarg.h> -#include <stdbool.h> -#include <inttypes.h> -#include <stdio.h> -#include <stdlib.h> -#include <time.h> - -void -log_stderr(const char *str, ...) -{ - va_list args; - - va_start(args, str); - vfprintf(stderr, str, args); - va_end(args); -} - - -double -timerun(void (*run)(void), char *name) -{ - struct timespec start, end; - double tdiff, tdsec, tdnano; - - printf("\n"); - fflush(stdout); - - if (run == NULL) { - printf("> %s: nothing to run!\n", name); - fflush(stdout); - return -1.0; - } - - printf("Running tool: %s\n", name); - printf("==========\n"); - fflush(stdout); - - clock_gettime(CLOCK_MONOTONIC, &start); - run(); - clock_gettime(CLOCK_MONOTONIC, &end); - - tdsec = end.tv_sec - start.tv_sec; - tdnano = end.tv_nsec - start.tv_nsec; - tdiff = tdsec + 1e-9 * tdnano; - - printf("==========\n"); - printf("\nTotal time: %.4fs\n", tdiff); - fflush(stdout); - - return tdiff; -} diff --git a/tools/tool.h b/tools/tool.h @@ -0,0 +1,106 @@ +#include <time.h> +#include <stdarg.h> +#include <stdbool.h> +#include <inttypes.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +#include "../src/nissy.h" + +static void +log_stderr(const char *str, ...) +{ + va_list args; + + va_start(args, str); + vfprintf(stderr, str, args); + va_end(args); +} + +static double +timerun(void (*run)(void), char *name) +{ + struct timespec start, end; + double tdiff, tdsec, tdnano; + + printf("\n"); + fflush(stdout); + + if (run == NULL) { + printf("> %s: nothing to run!\n", name); + fflush(stdout); + return -1.0; + } + + printf("Running tool: %s\n", name); + printf("==========\n"); + fflush(stdout); + + clock_gettime(CLOCK_MONOTONIC, &start); + run(); + clock_gettime(CLOCK_MONOTONIC, &end); + + tdsec = end.tv_sec - start.tv_sec; + tdnano = end.tv_nsec - start.tv_nsec; + tdiff = tdsec + 1e-9 * tdnano; + + printf("==========\n"); + printf("\nTotal time: %.4fs\n", tdiff); + fflush(stdout); + + return tdiff; +} + +static int +getdata( + const char *solver, + const char *options, + char **buf, + const char *filename +) { + int64_t s, size, sizeread; + FILE *f; + + if ((size = nissy_datasize(solver, options)) == -1) { + printf("Error in datasize\n"); + goto getdata_error_nofree; + } + + *buf = malloc(size); + + if ((f = fopen(filename, "rb")) == NULL) { + fprintf(stderr, "Table file not found, generating them." + " This can take a while.\n"); + s = nissy_gendata(solver, options, *buf); + if (s != size) { + fprintf(stderr, "Error generating table"); + if (s != -1) + fprintf(stderr, " (got %" PRId64 " bytes)", s); + fprintf(stderr, "\n"); + goto getdata_error; + } + if ((f = fopen(filename, "wb")) == NULL) { + fprintf(stderr, "Could not write tables to file %s" + ", will be regenerated next time.\n", filename); + } else { + fwrite(*buf, size, 1, f); + fclose(f); + } + } else { + fprintf(stderr, "Reading tables from file %s\n", filename); + sizeread = fread(*buf, size, 1, f); + fclose(f); + if (sizeread != 1) { + fprintf(stderr, "Error reading table, stopping\n"); + goto getdata_error; + } + } + + return 0; + +getdata_error: + free(*buf); +getdata_error_nofree: + return 1; +}