DEBUG flags cleanup, some dead code removal

This commit is contained in:
2021-01-26 15:02:04 +01:00
parent c1d0085217
commit 157c64bd6e
7 changed files with 110 additions and 102 deletions

View File

@@ -13,23 +13,27 @@ CFLAGS:=$(CFLAGS) -O3
#CFLAGS:=$(CFLAGS) -pg #CFLAGS:=$(CFLAGS) -pg
##################################### DEBUG flags: ##################################### DEBUG flags:
CFLAGS:=$(CFLAGS) -DDEBUG # general CFLAGS:=$(CFLAGS) -DDEBUG # general (signal handler, etc...)
#CFLAGS:=$(CFLAGS) -DDEBUG1 # temp CFLAGS:=$(CFLAGS) -DDEBUG_MAIN # general information in main
#CFLAGS:=$(CFLAGS) -DDEBUG_MAIN # main #CFLAGS:=$(CFLAGS) -DDEBUG_MAINSLEEP # sleep 1 sec within main loop (SIGINTR test)
#CFLAGS:=$(CFLAGS) -DDEBUG_MAINLOOP # main loop (do not use this!)
#CFLAGS:=$(CFLAGS) -DDEBUG_TIMER # timer #CFLAGS:=$(CFLAGS) -DDEBUG_TIMER # timer
#CFLAGS:=$(CFLAGS) -DDEBUG_SIGNAL # signal
#CFLAGS:=$(CFLAGS) -DDEBUG_BEST # best control #CFLAGS:=$(CFLAGS) -DDEBUG_BEST # best control
#CFLAGS:=$(CFLAGS) -DDEBUG_TREE # tree #CFLAGS:=$(CFLAGS) -DDEBUG_TREE # tree
#CFLAGS:=$(CFLAGS) -DDEBUG_OPER # oper #CFLAGS:=$(CFLAGS) -DDEBUG_OPER # oper
#CFLAGS:=$(CFLAGS) -DDEBUG_STACK # stack #CFLAGS:=$(CFLAGS) -DDEBUG_STACK # stack
#CFLAGS:=$(CFLAGS) -DDEBUG_STACK2 # stack - more details
#CFLAGS:=$(CFLAGS) -DDEBUG_EVAL # eval #CFLAGS:=$(CFLAGS) -DDEBUG_EVAL # eval
#CFLAGS:=$(CFLAGS) -DDEBUG_EVAL2 # eval 2 #CFLAGS:=$(CFLAGS) -DDEBUG_EVAL2 # eval 2
#CFLAGS:=$(CFLAGS) -DDEBUG_EVAL3 # eval 3
CFLAGS:=$(CFLAGS) -DDEBUG_MEM # malloc CFLAGS:=$(CFLAGS) -DDEBUG_MEM # malloc
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n" TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
export PATH := .:$(PATH) export PATH := .:$(PATH)
TARGETS=lceb tree oper timer TARGETS=lceb tree oper timer
OBJS=lceb.o tree.o oper.o stack.o eval.o best.o timer.o stack.o OBJS=lceb.o tree.o oper.o stack.o eval.o best.o timer.o stack.o signal.o
INCLUDES=lceb.h INCLUDES=lceb.h
DEPS=$(INCLUDES) Makefile DEPS=$(INCLUDES) Makefile

26
best.c
View File

@@ -7,35 +7,24 @@
#include "lceb.h" #include "lceb.h"
static int target=0; static int target=0;
//static int best=MAXINT;
static int bestdiff=MAXINT; static int bestdiff=MAXINT;
static int bestops=MAXINT; static int bestops=MAXINT;
static BEST bests[1024*10]; /* TODO: should be dynamic */ static BEST bests[1024*10]; /* TODO: should be dynamic */
static int nbests=0; static int nbests=0;
extern int displaytimer; extern int displaytimer;
int sigint_received=0;
int displayintermediate=0; int displayintermediate=0;
int displaytype=0; int displaytype=0;
#define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a)) #define DIFF(a, b) ((a)>(b)?(a)-(b):(b)-(a))
void stopall()
{
printf("SIGINT RECEIVED: aborting eval\n");
sigint_received=1;
}
int stopped()
{
return sigint_received;
}
void set_target(n) void set_target(n)
int n; int n;
{ {
target=n; target=n;
signal(SIGINT, stopall); # ifdef DEBUG
printf("target assigned (%d).\n", target);
# endif
} }
int check_best(res, nops, node, values, ops) int check_best(res, nops, node, values, ops)
@@ -81,7 +70,6 @@ int check_best(res, nops, node, values, ops)
} }
# endif # endif
} }
//}
if (found) { if (found) {
set_timer(&(bests[nbests].timer)); set_timer(&(bests[nbests].timer));
bests[nbests].res=res; bests[nbests].res=res;
@@ -90,7 +78,6 @@ int check_best(res, nops, node, values, ops)
bests[nbests].oper=ops; bests[nbests].oper=ops;
bests[nbests].root=dup_node(node); bests[nbests].root=dup_node(node);
bests[nbests].values=values; bests[nbests].values=values;
// printf("NEW BEST! res=%d diff=%d nops=%d\n", res, diff, nops);
if (displayintermediate) { if (displayintermediate) {
if (displaytimer) { if (displaytimer) {
printf("%.5f secs: ", get_timer(bests[nbests].timer)); printf("%.5f secs: ", get_timer(bests[nbests].timer));
@@ -99,7 +86,6 @@ int check_best(res, nops, node, values, ops)
print_node(node, TREE_TOP, 0, displaytype); print_node(node, TREE_TOP, 0, displaytype);
putchar('\n'); putchar('\n');
} }
//printf("check_best: res=%d diff=%d nops=%d\n", res, diff, nops);
nbests++; nbests++;
return diff; return diff;
} }
@@ -136,12 +122,14 @@ void print_bests()
{ {
int i=0; int i=0;
printf("BEST SOLUTION: res=%d diff=%d ops=%d ", bests[i].res, bestdiff, bestops); printf("BEST SOLUTION: res=%d diff=%d ops=%d ", bests[i].res, bestdiff, bestops);
if (displaytimer) //if (displaytimer)
printf("after %.5f secs.", get_timer(bests[i].timer)); printf("found after %.5f secs.", get_timer(bests[i].timer));
putchar('\n'); putchar('\n');
for (i=0; i<nbests; ++i) { for (i=0; i<nbests; ++i) {
//print_best(bests[i].root, bests[i].values, bests[i].oper, 0); //print_best(bests[i].root, bests[i].values, bests[i].oper, 0);
//printf("%5d =", bests[i].res); //printf("%5d =", bests[i].res);
if (displaytimer)
printf("%.5f secs: ", get_timer(bests[i].timer));
print_node(bests[i].root, TREE_TOP, 0, displaytype); print_node(bests[i].root, TREE_TOP, 0, displaytype);
putchar('\n'); putchar('\n');
//printf("%3d: %d = ", i, bests[i].res); //printf("%3d: %d = ", i, bests[i].res);

27
eval.c
View File

@@ -5,6 +5,7 @@
static int nodes_calc; /* total nodes evaluated */ static int nodes_calc; /* total nodes evaluated */
static int leaves_calc; /* total leaves evaluated */ static int leaves_calc; /* total leaves evaluated */
int firstonly=0;
int eval_node(node, depth, pvals, pops, ncalcs) int eval_node(node, depth, pvals, pops, ncalcs)
NODE *node; NODE *node;
@@ -16,7 +17,7 @@ int eval_node(node, depth, pvals, pops, ncalcs)
static int *vals, *val_zero; static int *vals, *val_zero;
static char *ops, *ops_zero; static char *ops, *ops_zero;
static int totcalc; static int totcalc;
int val1, val2, op, res=-1, lcalcs, rcalcs; int val1, val2, op, res=-1, lcalcs, rcalcs, diff;
if (depth == 0) { if (depth == 0) {
val_zero=vals=pvals; val_zero=vals=pvals;
@@ -48,15 +49,12 @@ int eval_node(node, depth, pvals, pops, ncalcs)
node->op=*ops; node->op=*ops;
ops++; ops++;
totcalc++; totcalc++;
//printf("NEW node(%s)\n", ops);
val1=eval_node(node->left, depth+1, pvals, ops, &lcalcs); val1=eval_node(node->left, depth+1, pvals, ops, &lcalcs);
//printf("val1=%d ", val1);
if (val1 <= 0) if (val1 <= 0)
return -1; return val1;
val2=eval_node(node->right, depth+1, pvals, ops, &rcalcs); val2=eval_node(node->right, depth+1, pvals, ops, &rcalcs);
//printf("val2=%d\n", val2);
if (val2 <= 0) if (val2 <= 0)
return -1; return val2;
switch (op) { switch (op) {
case Add: case Add:
res=val1+val2; res=val1+val2;
@@ -103,14 +101,25 @@ int eval_node(node, depth, pvals, pops, ncalcs)
*ncalcs=lcalcs+rcalcs+1; *ncalcs=lcalcs+rcalcs+1;
} }
if (res > 0) { if (res > 0) {
if (!check_best(res, *ncalcs, node, val_zero, ops_zero)) diff=check_best(res, *ncalcs, node, val_zero, ops_zero);
res=-1; //printf("eval=%d firstonly=%d\n", eval, firstonly);
if (diff == 0) { /* exact result, we stop here */
# ifdef DEBUG_EVAL3
printf("EXACT eval=%d\n", diff);
# endif
if (firstonly) {
print_results();
exit(0);
}
res=0;
}
} }
if (sigint_received) { if (sigint_received) {
print_bests(); print_bests();
exit(1); exit(1);
} }
# ifdef DEBUG1
# ifdef DEBUG_EVAL
for (i=0; i<=depth; ++i) for (i=0; i<=depth; ++i)
printf(" "); printf(" ");
printf("res=%d\n", res); printf("res=%d\n", res);

80
lceb.c
View File

@@ -20,9 +20,19 @@
static char *cmd; static char *cmd;
static int treetype=TREE_CATALAN; static int treetype=TREE_CATALAN;
extern int displaytimer, displayintermediate, displaytype; extern int displaytimer, displayintermediate, displaytype, firstonly;
int displaysummary=1; int displaysummary=1;
void print_results()
{
struct timespec end;
print_bests();
set_timer(&end);
if (displaysummary)
printf("Total time elapsed: %.5f secs, nodes/leaves evaluated:%'d/%'d\n",
get_timer(end), get_totnodes(), get_totleaves());
}
void usage() void usage()
{ {
fprintf(stderr, "usage: %s [options] target n1 n2 [...n%d]\n", cmd, MAXINPUT); fprintf(stderr, "usage: %s [options] target n1 n2 [...n%d]\n", cmd, MAXINPUT);
@@ -32,6 +42,7 @@ void help()
{ {
usage(); usage();
fprintf(stderr, "Countdown game solver.\n"); fprintf(stderr, "Countdown game solver.\n");
fprintf(stderr, " -1: Stops immediately when one solution is found\n");
fprintf(stderr, " -c: Show solutions timer\n"); fprintf(stderr, " -c: Show solutions timer\n");
fprintf(stderr, " -d TYPE: Solutions display type. TYPE can be:\n"); fprintf(stderr, " -d TYPE: Solutions display type. TYPE can be:\n");
fprintf(stderr, " r: RPN (default)\n"); fprintf(stderr, " r: RPN (default)\n");
@@ -64,10 +75,9 @@ int main(ac, av)
TREE *tree; TREE *tree;
char intarray[1024]; char intarray[1024];
char *comb; char *comb;
struct timespec end; char *options="1thcisd:";
char *options="thcisd:";
int option; int option;
# ifdef DEBUG_MAIN # ifdef DEBUG_MAINLOOP
int eval; int eval;
# endif # endif
@@ -76,6 +86,9 @@ int main(ac, av)
while ((option = getopt(ac, av, options)) != -1) { while ((option = getopt(ac, av, options)) != -1) {
//printf("opt: %c\n", option); //printf("opt: %c\n", option);
switch (option) { switch (option) {
case '1':
firstonly=1;
break;
case 'd': case 'd':
switch(*optarg) { switch(*optarg) {
case 'r': case 'r':
@@ -135,50 +148,30 @@ int main(ac, av)
nops=stacksize-1; nops=stacksize-1;
gen_combinations(nops); gen_combinations(nops);
//ncombs=ncombinations(len_ops, nops);
ncombs=n_combs(); ncombs=n_combs();
//print_combs();
printf("target=%d\nstacksize=%d ", target, stacksize);
set_target(target);
//printf("len_ops=%d\nnops=%d\nops_comb=%d\n", len_ops, nops, ncombs);
//stack=new_stack(stacksize, "Main Stack", 1);
//strcpy(inputstack.name, "initial");
for (i=optind+1; i<ac; ++i) { for (i=optind+1; i<ac; ++i) {
val=atoi(av[i]); val=atoi(av[i]);
push_stack(&inputstack, val); push_stack(&inputstack, val);
} }
print_stack(&inputstack, 0);
//print_stack(stack, 1);
gen_stacks(&inputstack); // printf("sorting stack...\n");
//print_stacks();
//mergesort_stack(stack->stack, 0, stack->last-1);
//print_stack(stack, 1);
/*i=1;
do {
printf("permutation %2d: ", i++);
print_stack(stack, 0);
} while (permute_stack(stack->stack, stack->last));
*/
gen_stacks(&inputstack);
if (treetype==TREE_CATALAN) { if (treetype==TREE_CATALAN) {
gen_tree(intarray, nops, 0, 0); gen_tree(intarray, nops, 0, 0);
} else { } else {
gen_reduced_trees(nops); gen_reduced_trees(nops);
} }
set_target(target);
set_intr();
nstacks=n_stacks(); nstacks=n_stacks();
ntrees=n_trees(); ntrees=n_trees();
# ifdef DEBUG_MAIN
printf("stacks :%'15d\n", nstacks); printf("stacks :%'15d\n", nstacks);
printf("ops combinations:%'15d\n", ncombs); printf("ops combinations:%'15d\n", ncombs);
printf("trees :%'15d\n", ntrees); printf("trees :%'15d\n", ntrees);
printf("max evaluations :%'15d\n", nstacks*ncombs*ntrees*stacksize); printf("max evaluations :%'15d\n", nstacks*ncombs*ntrees*stacksize);
//for (k=0; k<nstacks; ++k) { # endif
// stack=nth_stack(k);
// printf("%%%%%%%%%%%%%%%%%%\n");
// print_stack(stack, 0);
// printf("%%%%%%%%%%%%%%%%%%\n");
//}
for (i=0; i<ntrees; ++i) { for (i=0; i<ntrees; ++i) {
tree=nth_tree(i); tree=nth_tree(i);
for (j=0; j<ncombs; ++j) { for (j=0; j<ncombs; ++j) {
@@ -186,21 +179,20 @@ int main(ac, av)
for (k=0; k<nstacks; ++k) { for (k=0; k<nstacks; ++k) {
int ncalcs=0; int ncalcs=0;
stack=nth_stack(k); stack=nth_stack(k);
//printf("%%%%%%%%%%%%%%%%%%\n"); # ifdef DEBUG_MAINLOOP
//print_stack(stack, 0);
//printf("%%%%%%%%%%%%%%%%%%\n");
# ifdef DEBUG_MAIN
eval=eval_node(tree->head, 0, stack->stack, comb, &ncalcs); eval=eval_node(tree->head, 0, stack->stack, comb, &ncalcs);
if (eval > 0) { if (eval > 0) {
printf("============================== %d, %d, %d\n", i, j, k); printf("================= mainloop tree=%d, comb=%d, stack=%d\n", i, j, k);
print_tree(tree, 0); printf("mainloop: ");print_tree(tree, 0);
print_comb(j); printf("mainloop: ");print_comb(j);
print_stack(stack, 0); printf("mainloop: "); print_stack(stack, 0);
printf("eval=%d - calcs=%d\n", eval, ncalcs); printf("mainloop: eval=%d - calcs=%d\n", eval, ncalcs);
} }
# else # else
eval_node(tree->head, 0, stack->stack, comb, &ncalcs); eval_node(tree->head, 0, stack->stack, comb, &ncalcs);
# endif
# ifdef DEBUG_MAINSLEEP
sleep(1);
# endif # endif
} }
} }
@@ -214,11 +206,7 @@ int main(ac, av)
//res=eval_stack(stack); //res=eval_stack(stack);
//printf("EVAL=%d\n", res); //printf("EVAL=%d\n", res);
} }
printf("\n"); //printf("\n");
print_bests(); print_results();
set_timer(&end);
if (displaysummary)
printf("Total time elapsed: %.5f secs, nodes/leaves evaluated:%'d/%'d\n",
get_timer(end), get_totnodes(), get_totleaves());
exit(0); exit(0);
} }

8
lceb.h
View File

@@ -65,6 +65,13 @@ typedef struct best {
struct timespec timer; struct timespec timer;
} BEST; } BEST;
/* lceb.c */
extern void print_results();
/* signal.c */
extern void set_intr();
extern int stopped();
/* tree.c */ /* tree.c */
extern NODE *get_node(); extern NODE *get_node();
extern void free_node(NODE *node); extern void free_node(NODE *node);
@@ -119,7 +126,6 @@ extern int get_totnodes();
extern int get_totleaves(); extern int get_totleaves();
/* best.c */ /* best.c */
extern int stopped();
extern void set_target (int n); extern void set_target (int n);
extern int check_best(int res, int nops, NODE *node, int *values, char *ops); extern int check_best(int res, int nops, NODE *node, int *values, char *ops);
extern void print_best(NODE *node, int *values, char *pops, int depth); extern void print_best(NODE *node, int *values, char *pops, int depth);

23
signal.c Normal file
View File

@@ -0,0 +1,23 @@
#include <stdio.h>
#include <signal.h>
int sigint_received=0;
static void stopall()
{
printf("SIGINT RECEIVED: aborting eval\n");
sigint_received=1;
}
int stopped()
{
return sigint_received;
}
void set_intr()
{
signal(SIGINT, stopall);
# ifdef DEBUG
printf("SIGINT armed.\n");
# endif
}

34
stack.c
View File

@@ -52,7 +52,7 @@ STACK *new_stack(size, name, keep)
STACK *stack; STACK *stack;
int i; int i;
# ifdef DEBUG_STACK # ifdef DEBUG_STACK2
printf("new_stack(size=%d, name=[%s] last=%d total=%d)\n", printf("new_stack(size=%d, name=[%s] last=%d total=%d)\n",
size, name, laststack, totalstacks); size, name, laststack, totalstacks);
# endif # endif
@@ -105,7 +105,7 @@ int *push_stack(stack, val)
int size=stack->size; int size=stack->size;
int *pelt=stack->stack+stack->last; int *pelt=stack->stack+stack->last;
# ifdef DEBUG_STACK # ifdef DEBUG_STACK2
printf("push_stack(%d:[%s]): last=%d size=%d\n", printf("push_stack(%d:[%s]): last=%d size=%d\n",
val, stack->name, stack->last, stack->size); val, stack->name, stack->last, stack->size);
# endif # endif
@@ -201,34 +201,24 @@ void gen_stacks(stack)
int n=1; int n=1;
int exists=1; int exists=1;
printf("sorting stack...\n"); # ifdef DEBUG_STACK
printf("last=%d laststack=%d totalstacks=%d\n", last, laststack, totalstacks); printf("generating stacks...\n");
//printf("before sort: "); # endif
//print_stack(stack, 0); //printf("last=%d laststack=%d totalstacks=%d\n", last, laststack, totalstacks);
# ifdef DEBUG_STACK
printf("sorting initial stack...\n");
# endif
mergesort_stack(stack->stack, 0, last-1); mergesort_stack(stack->stack, 0, last-1);
printf("last=%d total=%d\n", laststack, totalstacks); # ifdef DEBUG_STACK
//printf("after sort: "); print_stack(stack, 0);
//print_stack(stack, 0); # endif
// push initial stack
//printf("++++++++++++++++ Adding main stack... ");
dup_stack(stack, "Main stack"); dup_stack(stack, "Main stack");
//keep_stack(new);
//print_stacks();
//print_stack(stack, 1);
while (exists) { while (exists) {
sprintf(name, "Stack copy %d", n); sprintf(name, "Stack copy %d", n);
//new=dup_stack(new, name);
exists=permute_stack(stack->stack, stack->last); exists=permute_stack(stack->stack, stack->last);
//printf("Permute : ");
//print_stack(stack, 0);
if (exists) { if (exists) {
//printf("++++++++++++++++ Adding stack... ");
dup_stack(stack, name); dup_stack(stack, name);
// print_stack(new, 0);
//keep_stack(new);
} }
// printf("---------------------- Stack... ");
// print_stacks();
n++; n++;
} }
} }