tool.h (3405B)
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 static double 26 timerun(void (*run)(void)) 27 { 28 struct timespec start, end; 29 double tdiff, tdsec, tdnano; 30 31 fflush(stdout); 32 33 if (run == NULL) { 34 printf("nothing to run!\n"); 35 fflush(stdout); 36 return -1.0; 37 } 38 39 clock_gettime(CLOCK_MONOTONIC, &start); 40 run(); 41 clock_gettime(CLOCK_MONOTONIC, &end); 42 43 tdsec = end.tv_sec - start.tv_sec; 44 tdnano = end.tv_nsec - start.tv_nsec; 45 tdiff = tdsec + 1e-9 * tdnano; 46 47 printf("---------\n"); 48 printf("\nTotal time: %.4fs\n", tdiff); 49 fflush(stdout); 50 51 return tdiff; 52 } 53 54 static void 55 writetable(const unsigned char *buf, int64_t size, const char *filename) 56 { 57 FILE *f; 58 59 if ((f = fopen(filename, "wb")) == NULL) { 60 printf("Could not write tables to file %s" 61 ", will be regenerated next time.\n", filename); 62 } else { 63 fwrite(buf, size, 1, f); 64 fclose(f); 65 printf("Table written to %s.\n", filename); 66 } 67 } 68 69 static long long int 70 generatetable( 71 const char *solver, 72 unsigned char **buf, 73 char dataid[static NISSY_SIZE_DATAID] 74 ) 75 { 76 long long int size, gensize; 77 78 size = nissy_solverinfo(solver, dataid); 79 if (size < 0) { 80 printf("Error getting table size.\n"); 81 return -1; 82 } 83 84 *buf = malloc(size); 85 gensize = nissy_gendata(solver, size, *buf); 86 87 if (gensize != size) { 88 printf("Error generating table"); 89 if (gensize == NISSY_OK) 90 printf(" (got %lld bytes)", gensize); 91 printf("\n"); 92 return -2; 93 } 94 95 return gensize; 96 } 97 98 static int 99 getdata( 100 const char *solver, 101 unsigned char **buf, 102 const char *filename 103 ) { 104 long long int size, sizeread; 105 FILE *f; 106 char dataid[NISSY_SIZE_DATAID]; 107 108 if ((f = fopen(filename, "rb")) == NULL) { 109 printf("Table file not found, generating it.\n"); 110 size = generatetable(solver, buf, dataid); 111 switch (size) { 112 case -1: 113 goto getdata_error_nofree; 114 case -2: 115 goto getdata_error; 116 default: 117 writetable(*buf, size, filename); 118 break; 119 } 120 } else { 121 printf("Reading tables from file %s\n", filename); 122 size = nissy_solverinfo(solver, dataid); 123 *buf = malloc(size); 124 sizeread = fread(*buf, size, 1, f); 125 fclose(f); 126 if (sizeread != 1) { 127 printf("Error reading table, stopping\n"); 128 goto getdata_error; 129 } 130 } 131 132 return 0; 133 134 getdata_error: 135 free(*buf); 136 getdata_error_nofree: 137 return 1; 138 } 139 140 static void 141 gendata_run( 142 const char *solver, 143 uint64_t expected[static 21] 144 ) { 145 long long int size; 146 char filename[1024], dataid[NISSY_SIZE_DATAID]; 147 unsigned char *buf; 148 149 size = generatetable(solver, &buf, dataid); 150 sprintf(filename, "tables/%s", dataid); 151 switch (size) { 152 case -1: 153 return; 154 case -2: 155 goto gendata_run_finish; 156 default: 157 printf("Succesfully generated %lld bytes. " 158 "See above for details on the tables.\n", size); 159 160 /* TODO: check that the table is correct */ 161 writetable(buf, size, filename); 162 break; 163 } 164 165 gendata_run_finish: 166 free(buf); 167 }