nissy-core

The "engine" of nissy, including the H48 optimal solver.
git clone https://git.tronto.net/nissy-core
Download | Log | Files | Refs | README | LICENSE

tool.h (3890B)


      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 <string.h>
      8 
      9 #include "../src/nissy.h"
     10 
     11 static void log_stderr(const char *, void *);
     12 static double timerun(void (*)(void));
     13 static void writetable(const unsigned char *, int64_t, const char *);
     14 static long long int generatetable(const char *, unsigned char **,
     15     char [static NISSY_SIZE_DATAID]);
     16 static int getdata(const char *, unsigned char **, const char *);
     17 static void gendata_run(const char *, uint64_t[static 21]);
     18 
     19 static void
     20 log_stderr(const char *str, void *unused)
     21 {
     22 	fprintf(stderr, "%s", str);
     23 }
     24 
     25 #ifdef _WIN32
     26 
     27 #include <windows.h>
     28 
     29 static double
     30 timerun(void (*run)(void))
     31 {
     32 	LARGE_INTEGER freq, start, end;
     33 	double tdiff;
     34 	
     35 	fflush(stdout);
     36 	
     37 	if (run == NULL) {
     38 		printf("nothing to run!\n");
     39 		fflush(stdout);
     40 		return -1.0;
     41 	}
     42 
     43 	QueryPerformanceFrequency(&freq);
     44 	QueryPerformanceCounter(&start);
     45 	run();
     46 	QueryPerformanceCounter(&end);
     47 	
     48 	tdiff = (double)(end.QuadPart - start.QuadPart) / freq.QuadPart;
     49 
     50 	printf("---------\n");
     51 	printf("\nTotal time: %.4fs\n", tdiff);
     52 	fflush(stdout);
     53 
     54 	return tdiff;
     55 }
     56 
     57 #else
     58 
     59 static double
     60 timerun(void (*run)(void))
     61 {
     62 	struct timespec start, end;
     63 	double tdiff, tdsec, tdnano;
     64 
     65 	fflush(stdout);
     66 
     67 	if (run == NULL) {
     68 		printf("nothing to run!\n");
     69 		fflush(stdout);
     70 		return -1.0;
     71 	}
     72 
     73 	clock_gettime(CLOCK_MONOTONIC, &start);
     74 	run();
     75 	clock_gettime(CLOCK_MONOTONIC, &end);
     76 
     77 	tdsec = end.tv_sec - start.tv_sec;
     78 	tdnano = end.tv_nsec - start.tv_nsec;
     79 	tdiff = tdsec + 1e-9 * tdnano;
     80 
     81 	printf("---------\n");
     82 	printf("\nTotal time: %.4fs\n", tdiff);
     83 	fflush(stdout);
     84 
     85 	return tdiff;
     86 }
     87 
     88 #endif
     89 
     90 static void
     91 writetable(const unsigned char *buf, int64_t size, const char *filename)
     92 {
     93 	FILE *f;
     94 
     95 	if ((f = fopen(filename, "wb")) == NULL) {
     96 		printf("Could not write tables to file %s"
     97 		    ", will be regenerated next time.\n", filename);
     98 	} else {
     99 		fwrite(buf, size, 1, f);
    100 		fclose(f);
    101 		printf("Table written to %s.\n", filename);
    102 	}
    103 }
    104 
    105 static long long int
    106 generatetable(
    107 	const char *solver,
    108 	unsigned char **buf,
    109 	char dataid[static NISSY_SIZE_DATAID]
    110 )
    111 {
    112 	long long int size, gensize;
    113 
    114 	size = nissy_solverinfo(solver, dataid);
    115 	if (size < 0) {
    116 		printf("Error getting table size.\n");
    117 		return -1;
    118 	}
    119 
    120 	*buf = malloc(size);
    121 	gensize = nissy_gendata(solver, size, *buf);
    122 
    123 	if (gensize != size) {
    124 		printf("Error generating table");
    125 		if (gensize == NISSY_OK)
    126 			printf(" (got %lld bytes)", gensize);
    127 		printf("\n");
    128 		return -2;
    129 	}
    130 
    131 	return gensize;
    132 }
    133 
    134 static int
    135 getdata(
    136 	const char *solver,
    137 	unsigned char **buf,
    138 	const char *filename
    139 ) {
    140 	long long int size, sizeread;
    141 	FILE *f;
    142 	char dataid[NISSY_SIZE_DATAID];
    143 
    144 	if ((f = fopen(filename, "rb")) == NULL) {
    145 		printf("Table file not found, generating it.\n");
    146 		size = generatetable(solver, buf, dataid);
    147 		switch (size) {
    148 		case -1:
    149 			goto getdata_error_nofree;
    150 		case -2:
    151 			goto getdata_error;
    152 		default:
    153 			writetable(*buf, size, filename);
    154 			break;
    155 		}
    156 	} else {
    157 		printf("Reading tables from file %s\n", filename);
    158 		size = nissy_solverinfo(solver, dataid);
    159 		*buf = malloc(size);
    160 		sizeread = fread(*buf, size, 1, f);
    161 		fclose(f);
    162 		if (sizeread != 1) {
    163 			printf("Error reading table, stopping\n");
    164 			goto getdata_error;
    165 		}
    166 	}
    167 
    168 	return 0;
    169 
    170 getdata_error:
    171 	free(*buf);
    172 getdata_error_nofree:
    173 	return 1;
    174 }
    175 
    176 static void
    177 gendata_run(
    178 	const char *solver,
    179 	uint64_t expected[static 21]
    180 ) {
    181 	long long int size;
    182 	char filename[1024], dataid[NISSY_SIZE_DATAID];
    183 	unsigned char *buf;
    184 
    185 	size = generatetable(solver, &buf, dataid);
    186 	sprintf(filename, "tables/%s", dataid);
    187 	switch (size) {
    188 	case -1:
    189 		return;
    190 	case -2:
    191 		goto gendata_run_finish;
    192 	default:
    193 		printf("Succesfully generated %lld bytes. "
    194 		       "See above for details on the tables.\n", size);
    195 
    196 		writetable(buf, size, filename);
    197 		break;
    198 	}
    199 
    200 gendata_run_finish:
    201 	free(buf);
    202 }