From c6851b22c00bfe56c32e39b18231d5af980a1eb2 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 27 Jan 2021 13:56:03 +0100 Subject: [PATCH] added timer for maxed calculation time. --- best.c | 14 +++++++++----- eval.c | 4 ++-- lceb.c | 11 +++++++++-- lceb.h | 4 ++++ signal.c | 39 ++++++++++++++++++++++++++++++++------- timer.c | 4 +--- 6 files changed, 57 insertions(+), 19 deletions(-) diff --git a/best.c b/best.c index b7e0b49..3e13b95 100644 --- a/best.c +++ b/best.c @@ -12,7 +12,7 @@ static int bestops=MAXINT; static BEST bests[1024*10]; /* TODO: should be dynamic */ static int nbests=0; -extern int displaytimer; +extern int displaytimer, firstonly; int displayintermediate=0; int displaytype=0; @@ -120,12 +120,16 @@ void print_best(node, values, pops, depth) void print_bests() { - int i=0; - printf("BEST SOLUTION: res=%d diff=%d ops=%d ", bests[i].res, bestdiff, bestops); + int i=0, j=firstonly? 1: nbests; + if (bestdiff==0) { + printf("Le compte est bon: %d solutions with %d ops ", nbests, bestops); + } else { + printf("Found %d results with difference %d and %d ops ", nbests, bestdiff, bestops); + } //if (displaytimer) - printf("found after %.5f secs.", get_timer(bests[i].timer)); + printf("(1st after %.5f secs).", get_timer(bests[i].timer)); putchar('\n'); - for (i=0; ileft=node->right; node->right=tmp; @@ -115,7 +115,7 @@ int eval_node(node, depth, pvals, pops, ncalcs) } } if (sigint_received) { - print_bests(); + print_results(); exit(1); } diff --git a/lceb.c b/lceb.c index fe3ae38..a959faa 100644 --- a/lceb.c +++ b/lceb.c @@ -20,6 +20,7 @@ static char *cmd; static int treetype=TREE_CATALAN; +static int maxmillisecs=0; extern int displaytimer, displayintermediate, displaytype, firstonly; int displaysummary=1; @@ -42,7 +43,7 @@ void help() { usage(); fprintf(stderr, "Countdown game solver.\n"); - fprintf(stderr, " -1: Stops immediately when one solution is found\n"); + fprintf(stderr, " -1: Show only one solution (immediate stop when exact solution found\n"); fprintf(stderr, " -c: Show solutions timer\n"); fprintf(stderr, " -d TYPE: Solutions display type. TYPE can be:\n"); fprintf(stderr, " r: RPN (default)\n"); @@ -54,6 +55,7 @@ void help() fprintf(stderr, " -i: Show intermediate solutions\n"); fprintf(stderr, " -s: Do not show summary (time, nodes evaluated)\n"); fprintf(stderr, " -t: Use less trees (Wedderburn–Etherington instead of Catalan)\n"); + fprintf(stderr, " -T TIMER: Will stop after TIMER 1/10th seconds\n"); fprintf(stderr, " -h: This help\n"); } @@ -75,7 +77,7 @@ int main(ac, av) TREE *tree; char intarray[1024]; char *comb; - char *options="1thcisd:"; + char *options="1thcisd:T:"; int option; # ifdef DEBUG_MAINLOOP int eval; @@ -124,6 +126,9 @@ int main(ac, av) case 's': displaysummary=0; break; + case 'T': + maxmillisecs=atoi(optarg)*100; + break; case 't': treetype=TREE_WEDDERBURN; break; @@ -143,6 +148,8 @@ int main(ac, av) } setlocale(LC_ALL, ""); /* to use "%'d" in printf */ start_timer(); + if (maxmillisecs) + set_alarm(maxmillisecs); target=atoi(av[optind]); stacksize=ac-optind-1; nops=stacksize-1; diff --git a/lceb.h b/lceb.h index 681c567..f56ca17 100644 --- a/lceb.h +++ b/lceb.h @@ -7,6 +7,9 @@ #define MAX(a,b) (((a)>(b))?(a):(b)) #define ABS(a) (((a)<0)?-(a):(a)) +#define NANOSEC 1000000000.0 +#define MILLISEC 1000 + typedef enum { Nop='N', Add='+', @@ -71,6 +74,7 @@ extern void print_results(); /* signal.c */ extern void set_intr(); extern int stopped(); +extern void set_alarm(int ms); /* tree.c */ extern NODE *get_node(); diff --git a/signal.c b/signal.c index 8b594cb..24af9f7 100644 --- a/signal.c +++ b/signal.c @@ -1,11 +1,15 @@ #include #include +#include +#include +#include "lceb.h" int sigint_received=0; -static void stopall() +static void stopall(signum) + int signum; { - printf("SIGINT RECEIVED: aborting eval\n"); + printf("SIGNAL %d RECEIVED: aborting eval\n", signum); sigint_received=1; } @@ -20,14 +24,35 @@ void set_intr() sig.sa_handler = stopall; sigemptyset(&sig.sa_mask); - //sigaddset(&new.sa_mask, SIGINT); - //sigaddset(&new.sa_mask, SIGALRM); sig.sa_flags = 0; - //sigaction(SIGINT, NULL, &old); - //if (old_action.sa_handler != SIG_IGN) { sigaction(SIGINT, &sig, NULL); sigaction(SIGALRM, &sig, NULL); # ifdef DEBUG - printf("SIGINT and SIGALRM armed.\n"); + printf("SIGINT armed.\n"); # endif } + +void set_alarm(ms) + int ms; +{ + struct sigaction sa; + struct itimerval timer; + + /* Install timer_handler as the signal handler for SIGVTALRM. */ + memset (&sa, 0, sizeof (sa)); + sa.sa_handler = stopall; + sigaction (SIGUSR1, &sa, NULL); + + timer.it_value.tv_sec = ms/1000; + timer.it_value.tv_usec = (ms%1000)*1000; +# ifdef DEBUG + printf("alarm clock set to %.2f secs.\n", + timer.it_value.tv_sec + timer.it_value.tv_usec / 1000000.); +# endif + /* ... and every 250 msec after that. */ + timer.it_interval.tv_sec = 0; + timer.it_interval.tv_usec = 0; + /* Start a virtual timer. It counts down whenever this process is executing. */ + setitimer (ITIMER_REAL, &timer, NULL); + +} diff --git a/timer.c b/timer.c index eb83785..2749c68 100644 --- a/timer.c +++ b/timer.c @@ -1,9 +1,7 @@ #include #include #include - -#define NANOSEC 1000000000.0 -#define MILLISEC 1000 +#include "lceb.h" int displaytimer=0; static struct timespec start;