aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-10-18 16:35:17 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-10-18 16:35:17 +0200
commit5216fb4be01e88f5b4928a69287e8f7ee3b3af5b (patch)
tree24aa57a0e34c49e0e6307caf8cf830f8dd26ed49 /src
parent3d6c73c33276ecedba86cb871b4b5a089571629b (diff)
downloadnissy-core-5216fb4be01e88f5b4928a69287e8f7ee3b3af5b.tar.gz
nissy-core-5216fb4be01e88f5b4928a69287e8f7ee3b3af5b.zip
Add node and fallback benchmarking
Diffstat (limited to '')
-rw-r--r--src/nissy.c7
-rw-r--r--src/nissy.h41
-rw-r--r--src/solvers/h48/solve.h25
-rw-r--r--src/solvers/h48/solve_multithread.h23
4 files changed, 68 insertions, 28 deletions
diff --git a/src/nissy.c b/src/nissy.c
index 986f27f..c5cf1d5 100644
--- a/src/nissy.c
+++ b/src/nissy.c
@@ -471,7 +471,8 @@ nissy_solve(
471 unsigned long long data_size, 471 unsigned long long data_size,
472 const char data[data_size], 472 const char data[data_size],
473 unsigned sols_size, 473 unsigned sols_size,
474 char sols[sols_size] 474 char sols[sols_size],
475 long long stats[static NISSY_SIZE_SOLVE_STATS]
475) 476)
476{ 477{
477 cube_t c; 478 cube_t c;
@@ -511,9 +512,9 @@ nissy_solve(
511 } else { 512 } else {
512 return THREADS > 1 ? 513 return THREADS > 1 ?
513 solve_h48_multithread(c, minmoves, maxmoves, 514 solve_h48_multithread(c, minmoves, maxmoves,
514 maxsols, data_size, data, sols_size, sols) : 515 maxsols, data_size, data, sols_size, sols, stats) :
515 solve_h48(c, minmoves, maxmoves, maxsols, 516 solve_h48(c, minmoves, maxmoves, maxsols,
516 data_size, data, sols_size, sols); 517 data_size, data, sols_size, sols, stats);
517 } 518 }
518 } else { 519 } else {
519 LOG("solve: unknown solver '%s'\n", solver); 520 LOG("solve: unknown solver '%s'\n", solver);
diff --git a/src/nissy.h b/src/nissy.h
index 7a39927..8394fad 100644
--- a/src/nissy.h
+++ b/src/nissy.h
@@ -24,6 +24,7 @@ for example 'rotation UF' or 'mirrored BL'.
24#define NISSY_SIZE_B32 22U 24#define NISSY_SIZE_B32 22U
25#define NISSY_SIZE_H48 88U 25#define NISSY_SIZE_H48 88U
26#define NISSY_SIZE_TRANSFORMATION 12U 26#define NISSY_SIZE_TRANSFORMATION 12U
27#define NISSY_SIZE_SOLVE_STATS 10U
27 28
28/* Flags for NISS options */ 29/* Flags for NISS options */
29#define NISSY_NISSFLAG_NORMAL 1U 30#define NISSY_NISSFLAG_NORMAL 1U
@@ -58,7 +59,8 @@ Return values:
58 NISSY_ERROR_INVALID_CUBE - At least one of the given cubes is invalid. 59 NISSY_ERROR_INVALID_CUBE - At least one of the given cubes is invalid.
59 NISSY_ERROR_UNKNOWN - An unknown error occurred. 60 NISSY_ERROR_UNKNOWN - An unknown error occurred.
60*/ 61*/
61long long nissy_compose( 62long long
63nissy_compose(
62 const char cube[static NISSY_SIZE_B32], 64 const char cube[static NISSY_SIZE_B32],
63 const char permutation[static NISSY_SIZE_B32], 65 const char permutation[static NISSY_SIZE_B32],
64 char result[static NISSY_SIZE_B32] 66 char result[static NISSY_SIZE_B32]
@@ -79,7 +81,8 @@ Return values:
79 NISSY_ERROR_INVALID_CUBE - The given cube is invalid. 81 NISSY_ERROR_INVALID_CUBE - The given cube is invalid.
80 NISSY_ERROR_UNKNOWN - An unknown error occurred. 82 NISSY_ERROR_UNKNOWN - An unknown error occurred.
81*/ 83*/
82long long nissy_inverse( 84long long
85nissy_inverse(
83 const char cube[static NISSY_SIZE_B32], 86 const char cube[static NISSY_SIZE_B32],
84 char result[static NISSY_SIZE_B32] 87 char result[static NISSY_SIZE_B32]
85); 88);
@@ -101,7 +104,8 @@ Return values:
101 NISSY_ERROR_INVALID_MOVES - The given moves are invalid. 104 NISSY_ERROR_INVALID_MOVES - The given moves are invalid.
102 NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL. 105 NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL.
103*/ 106*/
104long long nissy_applymoves( 107long long
108nissy_applymoves(
105 const char cube[static NISSY_SIZE_B32], 109 const char cube[static NISSY_SIZE_B32],
106 const char *moves, 110 const char *moves,
107 char result[static NISSY_SIZE_B32] 111 char result[static NISSY_SIZE_B32]
@@ -122,7 +126,8 @@ Return values:
122 NISSY_ERROR_INVALID_CUBE - The given cube is invalid. 126 NISSY_ERROR_INVALID_CUBE - The given cube is invalid.
123 NISSY_ERROR_INVALID_TRANS - The given transformation is invalid. 127 NISSY_ERROR_INVALID_TRANS - The given transformation is invalid.
124*/ 128*/
125long long nissy_applytrans( 129long long
130nissy_applytrans(
126 const char cube[static NISSY_SIZE_B32], 131 const char cube[static NISSY_SIZE_B32],
127 const char transformation[static NISSY_SIZE_TRANSFORMATION], 132 const char transformation[static NISSY_SIZE_TRANSFORMATION],
128 char result[static NISSY_SIZE_B32] 133 char result[static NISSY_SIZE_B32]
@@ -147,7 +152,8 @@ Return values:
147 NISSY_ERROR_NULL_POINTER - At least one of 'format_in', 'format_out' or 152 NISSY_ERROR_NULL_POINTER - At least one of 'format_in', 'format_out' or
148 'cube_string' arguments is NULL. 153 'cube_string' arguments is NULL.
149*/ 154*/
150long long nissy_convert( 155long long
156nissy_convert(
151 const char *format_in, 157 const char *format_in,
152 const char *format_out, 158 const char *format_out,
153 const char *cube_string, 159 const char *cube_string,
@@ -174,7 +180,8 @@ Return values:
174 NISSY_WARNING_UNSOLVABLE - The resulting cube is unsolvable. 180 NISSY_WARNING_UNSOLVABLE - The resulting cube is unsolvable.
175 NISSY_ERROR_OPTIONS - One or more of the given parameters is invalid. 181 NISSY_ERROR_OPTIONS - One or more of the given parameters is invalid.
176*/ 182*/
177long long nissy_getcube( 183long long
184nissy_getcube(
178 long long ep, 185 long long ep,
179 long long eo, 186 long long eo,
180 long long cp, 187 long long cp,
@@ -196,7 +203,8 @@ Return values:
196 NISSY_ERROR_UNKNOWN - An unknown error occurred. 203 NISSY_ERROR_UNKNOWN - An unknown error occurred.
197 Any value >= 0 - The size of the data, in bytes. 204 Any value >= 0 - The size of the data, in bytes.
198*/ 205*/
199long long nissy_datasize( 206long long
207nissy_datasize(
200 const char *solver 208 const char *solver
201); 209);
202 210
@@ -215,7 +223,8 @@ Return values:
215 NISSY_ERROR_UNKNOWN - An error occurred while generating the data. 223 NISSY_ERROR_UNKNOWN - An error occurred while generating the data.
216 Any value >= 0 - The size of the data, in bytes. 224 Any value >= 0 - The size of the data, in bytes.
217*/ 225*/
218long long nissy_gendata( 226long long
227nissy_gendata(
219 const char *solver, 228 const char *solver,
220 unsigned long long data_size, 229 unsigned long long data_size,
221 char data[data_size] 230 char data[data_size]
@@ -232,7 +241,8 @@ Return values:
232 NISSY_OK - The data is valid. 241 NISSY_OK - The data is valid.
233 NISSY_ERROR_DATA - The data is invalid. 242 NISSY_ERROR_DATA - The data is invalid.
234*/ 243*/
235long long nissy_checkdata( 244long long
245nissy_checkdata(
236 unsigned long long data_size, 246 unsigned long long data_size,
237 const char data[data_size] 247 const char data[data_size]
238); 248);
@@ -255,6 +265,7 @@ Parameters:
255 sols - The return parameter for the solutions. The solutions are 265 sols - The return parameter for the solutions. The solutions are
256 separated by a '\n' (newline) and a '\0' (NULL character) 266 separated by a '\n' (newline) and a '\0' (NULL character)
257 terminates the list. 267 terminates the list.
268 stats - An array to store some statistics about the solve.
258 269
259Return values: 270Return values:
260 NISSY_OK - Cube solved succesfully. 271 NISSY_OK - Cube solved succesfully.
@@ -266,7 +277,8 @@ Return values:
266 NISSY_ERROR_NULL_POINTER - The 'solver' argument is null. 277 NISSY_ERROR_NULL_POINTER - The 'solver' argument is null.
267 Any value >= 0 - The number of solutions found. 278 Any value >= 0 - The number of solutions found.
268*/ 279*/
269long long nissy_solve( 280long long
281nissy_solve(
270 const char cube[static NISSY_SIZE_B32], 282 const char cube[static NISSY_SIZE_B32],
271 const char *solver, 283 const char *solver,
272 unsigned nissflag, 284 unsigned nissflag,
@@ -277,7 +289,8 @@ long long nissy_solve(
277 unsigned long long data_size, 289 unsigned long long data_size,
278 const char data[data_size], 290 const char data[data_size],
279 unsigned sols_size, 291 unsigned sols_size,
280 char sols[sols_size] 292 char sols[sols_size],
293 long long stats[static NISSY_SIZE_SOLVE_STATS]
281); 294);
282 295
283/* 296/*
@@ -289,7 +302,8 @@ Return values:
289 NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL. 302 NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL.
290 Any value >= 0 - The number of moves. 303 Any value >= 0 - The number of moves.
291*/ 304*/
292long long nissy_countmoves( 305long long
306nissy_countmoves(
293 const char *moves 307 const char *moves
294); 308);
295 309
@@ -303,7 +317,8 @@ Return values:
303 NISSY_OK - Logger set succesfully. No warning or error is goind to be given 317 NISSY_OK - Logger set succesfully. No warning or error is goind to be given
304 if the logger is NULL or invalid. 318 if the logger is NULL or invalid.
305*/ 319*/
306long long nissy_setlogger( 320long long
321nissy_setlogger(
307 void (*logger_function)(const char *, ...) 322 void (*logger_function)(const char *, ...)
308); 323);
309 324
diff --git a/src/solvers/h48/solve.h b/src/solvers/h48/solve.h
index 778155c..67c596b 100644
--- a/src/solvers/h48/solve.h
+++ b/src/solvers/h48/solve.h
@@ -17,6 +17,8 @@ typedef struct {
17 uint8_t nissbranch; 17 uint8_t nissbranch;
18 int8_t npremoves; 18 int8_t npremoves;
19 uint8_t premoves[MAXLEN]; 19 uint8_t premoves[MAXLEN];
20 _Atomic long long *nodes_visited;
21 _Atomic long long *table_fallbacks;
20} dfsarg_solveh48_t; 22} dfsarg_solveh48_t;
21 23
22STATIC uint32_t allowednextmove_h48(uint8_t *, uint8_t, uint8_t); 24STATIC uint32_t allowednextmove_h48(uint8_t *, uint8_t, uint8_t);
@@ -24,8 +26,8 @@ STATIC uint32_t allowednextmove_h48(uint8_t *, uint8_t, uint8_t);
24STATIC void solve_h48_appendsolution(dfsarg_solveh48_t *); 26STATIC void solve_h48_appendsolution(dfsarg_solveh48_t *);
25STATIC_INLINE bool solve_h48_stop(dfsarg_solveh48_t *); 27STATIC_INLINE bool solve_h48_stop(dfsarg_solveh48_t *);
26STATIC int64_t solve_h48_dfs(dfsarg_solveh48_t *); 28STATIC int64_t solve_h48_dfs(dfsarg_solveh48_t *);
27STATIC int64_t solve_h48(cube_t, int8_t, int8_t, 29STATIC int64_t solve_h48(cube_t, int8_t, int8_t, int8_t, uint64_t,
28 int8_t, uint64_t, const void *, uint64_t, char *); 30 const void *, uint64_t, char *, long long [static NISSY_SIZE_SOLVE_STATS]);
29 31
30STATIC uint32_t 32STATIC uint32_t
31allowednextmove_h48(uint8_t *moves, uint8_t n, uint8_t h48branch) 33allowednextmove_h48(uint8_t *moves, uint8_t n, uint8_t h48branch)
@@ -103,6 +105,8 @@ solve_h48_stop(dfsarg_solveh48_t *arg)
103 int8_t cbound, cbound_inv, h48bound, h48bound_inv; 105 int8_t cbound, cbound_inv, h48bound, h48bound_inv;
104 int64_t coord, coord_inv; 106 int64_t coord, coord_inv;
105 107
108 (*arg->nodes_visited)++;
109
106 arg->nissbranch = MM_NORMAL; 110 arg->nissbranch = MM_NORMAL;
107 cbound = get_h48_cdata(arg->cube, arg->cocsepdata, &data); 111 cbound = get_h48_cdata(arg->cube, arg->cocsepdata, &data);
108 if (cbound + arg->nmoves + arg->npremoves > arg->depth) 112 if (cbound + arg->nmoves + arg->npremoves > arg->depth)
@@ -120,6 +124,7 @@ solve_h48_stop(dfsarg_solveh48_t *arg)
120 124
121 if (arg->k == 2) { 125 if (arg->k == 2) {
122 if (h48bound == 0) { 126 if (h48bound == 0) {
127 (*arg->table_fallbacks)++;
123 h48bound = get_h48_pval( 128 h48bound = get_h48_pval(
124 arg->h48data_fallback, coord >> arg->h, 4); 129 arg->h48data_fallback, coord >> arg->h, 4);
125 } else { 130 } else {
@@ -136,6 +141,7 @@ solve_h48_stop(dfsarg_solveh48_t *arg)
136 h48bound_inv = get_h48_pval(arg->h48data, coord_inv, arg->k); 141 h48bound_inv = get_h48_pval(arg->h48data, coord_inv, arg->k);
137 if (arg->k == 2) { 142 if (arg->k == 2) {
138 if (h48bound_inv == 0) { 143 if (h48bound_inv == 0) {
144 (*arg->table_fallbacks)++;
139 h48bound_inv = get_h48_pval( 145 h48bound_inv = get_h48_pval(
140 arg->h48data_fallback, coord_inv >> arg->h, 4); 146 arg->h48data_fallback, coord_inv >> arg->h, 4);
141 } else { 147 } else {
@@ -209,16 +215,19 @@ solve_h48(
209 uint64_t data_size, 215 uint64_t data_size,
210 const void *data, 216 const void *data,
211 uint64_t solutions_size, 217 uint64_t solutions_size,
212 char *solutions 218 char *solutions,
219 long long stats[static NISSY_SIZE_SOLVE_STATS]
213) 220)
214{ 221{
215 _Atomic int64_t nsols; 222 _Atomic int64_t nsols;
223 _Atomic long long nodes, fallbacks;
216 dfsarg_solveh48_t arg; 224 dfsarg_solveh48_t arg;
217 tableinfo_t info, fbinfo; 225 tableinfo_t info, fbinfo;
218 226
219 if(readtableinfo_n(data_size, data, 2, &info) != NISSY_OK) 227 if(readtableinfo_n(data_size, data, 2, &info) != NISSY_OK)
220 goto solve_h48_error_data; 228 goto solve_h48_error_data;
221 229
230 nodes = fallbacks = 0;
222 arg = (dfsarg_solveh48_t) { 231 arg = (dfsarg_solveh48_t) {
223 .cube = cube, 232 .cube = cube,
224 .inverse = inverse(cube), 233 .inverse = inverse(cube),
@@ -230,7 +239,9 @@ solve_h48(
230 .cocsepdata = (uint32_t *)((char *)data + INFOSIZE), 239 .cocsepdata = (uint32_t *)((char *)data + INFOSIZE),
231 .h48data = (uint8_t *)data + COCSEP_FULLSIZE + INFOSIZE, 240 .h48data = (uint8_t *)data + COCSEP_FULLSIZE + INFOSIZE,
232 .solutions_size = solutions_size, 241 .solutions_size = solutions_size,
233 .nextsol = &solutions 242 .nextsol = &solutions,
243 .nodes_visited = &nodes,
244 .table_fallbacks = &fallbacks
234 }; 245 };
235 246
236 if (info.bits == 2) { 247 if (info.bits == 2) {
@@ -256,7 +267,11 @@ solve_h48(
256 solve_h48_dfs(&arg); 267 solve_h48_dfs(&arg);
257 } 268 }
258 **arg.nextsol = '\0'; 269 **arg.nextsol = '\0';
259 (*arg.nextsol)++; 270
271 stats[0] = nodes;
272 stats[1] = fallbacks;
273 LOG("Nodes visited: %lld\nTable fallbacks: %lld\n", nodes, fallbacks);
274
260 return nsols; 275 return nsols;
261 276
262solve_h48_error_data: 277solve_h48_error_data:
diff --git a/src/solvers/h48/solve_multithread.h b/src/solvers/h48/solve_multithread.h
index 2755372..394350e 100644
--- a/src/solvers/h48/solve_multithread.h
+++ b/src/solvers/h48/solve_multithread.h
@@ -20,8 +20,8 @@ STATIC void copy_queue(task_queue_t *, task_queue_t *, int, _Atomic int64_t *);
20STATIC void *start_thread(void *); 20STATIC void *start_thread(void *);
21STATIC int64_t solve_h48_bfs(dfsarg_solveh48_t *, task_queue_t *, int8_t); 21STATIC int64_t solve_h48_bfs(dfsarg_solveh48_t *, task_queue_t *, int8_t);
22STATIC int64_t solve_h48_single(dfsarg_solveh48_t *, task_queue_t *); 22STATIC int64_t solve_h48_single(dfsarg_solveh48_t *, task_queue_t *);
23STATIC int64_t solve_h48_multithread(cube_t, int8_t, int8_t, int8_t, 23STATIC int64_t solve_h48_multithread(cube_t, int8_t, int8_t, int8_t, uint64_t,
24 uint64_t, const void *, uint64_t, char *); 24 const void *, uint64_t, char *, long long [static NISSY_SIZE_SOLVE_STATS]);
25 25
26STATIC void 26STATIC void
27solve_h48_appendsolution_thread(dfsarg_solveh48_t *arg, task_queue_t *tq) 27solve_h48_appendsolution_thread(dfsarg_solveh48_t *arg, task_queue_t *tq)
@@ -255,10 +255,12 @@ solve_h48_multithread(
255 uint64_t data_size, 255 uint64_t data_size,
256 const void *data, 256 const void *data,
257 uint64_t solutions_size, 257 uint64_t solutions_size,
258 char *solutions 258 char *solutions,
259 long long stats[static NISSY_SIZE_SOLVE_STATS]
259) 260)
260{ 261{
261 _Atomic int64_t nsols = 0; 262 _Atomic int64_t nsols = 0;
263 _Atomic long long nodes, fallbacks;
262 int p_depth = 0; 264 int p_depth = 0;
263 dfsarg_solveh48_t arg; 265 dfsarg_solveh48_t arg;
264 tableinfo_t info, fbinfo; 266 tableinfo_t info, fbinfo;
@@ -267,6 +269,7 @@ solve_h48_multithread(
267 if (readtableinfo_n(data_size, data, 2, &info) != NISSY_OK) 269 if (readtableinfo_n(data_size, data, 2, &info) != NISSY_OK)
268 goto solve_h48_multithread_error_data; 270 goto solve_h48_multithread_error_data;
269 271
272 nodes = fallbacks = 0;
270 arg = (dfsarg_solveh48_t){ 273 arg = (dfsarg_solveh48_t){
271 .cube = cube, 274 .cube = cube,
272 .inverse = inverse(cube), 275 .inverse = inverse(cube),
@@ -279,7 +282,9 @@ solve_h48_multithread(
279 .cocsepdata = (uint32_t *)((char *)data + INFOSIZE), 282 .cocsepdata = (uint32_t *)((char *)data + INFOSIZE),
280 .h48data = (uint8_t *)data + COCSEP_FULLSIZE + INFOSIZE, 283 .h48data = (uint8_t *)data + COCSEP_FULLSIZE + INFOSIZE,
281 .solutions_size = solutions_size, 284 .solutions_size = solutions_size,
282 .nextsol = &solutions 285 .nextsol = &solutions,
286 .nodes_visited = &nodes,
287 .table_fallbacks = &fallbacks
283 }; 288 };
284 289
285 if (info.bits == 2) { 290 if (info.bits == 2) {
@@ -301,7 +306,7 @@ solve_h48_multithread(
301 task_queue_t nq; 306 task_queue_t nq;
302 init_queue(&nq); 307 init_queue(&nq);
303 308
304 for (int i = 0; i < THREADS; i++){ 309 for (int i = 0; i < THREADS; i++) {
305 pthread_create(&threads[i], NULL, &start_thread, &nq); 310 pthread_create(&threads[i], NULL, &start_thread, &nq);
306 } 311 }
307 312
@@ -323,11 +328,15 @@ solve_h48_multithread(
323 atomic_store(&nq.terminate, true); 328 atomic_store(&nq.terminate, true);
324 pthread_cond_broadcast(&nq.cond); 329 pthread_cond_broadcast(&nq.cond);
325 330
326 for (int i = 0; i < THREADS; i++){ 331 for (int i = 0; i < THREADS; i++) {
327 pthread_join(threads[i], NULL); 332 pthread_join(threads[i], NULL);
328 } 333 }
329 **arg.nextsol = '\0'; 334 **arg.nextsol = '\0';
330 (*arg.nextsol)++; 335
336 stats[0] = nodes;
337 stats[1] = fallbacks;
338 LOG("Nodes visited: %lld\nTable fallbacks: %lld\n", nodes, fallbacks);
339
331 return nsols; 340 return nsols;
332 341
333solve_h48_multithread_error_data: 342solve_h48_multithread_error_data:

Generated with cgit - Back to sebastiano.tronto.net