From: bdemsky Date: Wed, 31 Dec 2014 14:07:26 +0000 (+0900) Subject: incremental support X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=3f0fa250ecf4199909bc99ec80d0417f5e6fcdcf;p=satlib.git incremental support --- diff --git a/lingeling/code/lglincremental.c b/lingeling/code/lglincremental.c new file mode 100644 index 0000000..6c73a7b --- /dev/null +++ b/lingeling/code/lglincremental.c @@ -0,0 +1,479 @@ +/*-------------------------------------------------------------------------*/ +/* Copyright 2010-2014 Armin Biere Johannes Kepler University Linz Austria */ +/*-------------------------------------------------------------------------*/ + +#include "lglib.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "solver_interface.h" + +static LGL * lgl4sigh; +static int catchedsig, verbose, ignmissingheader, ignaddcls; + +static void (*sig_int_handler)(int); +static void (*sig_segv_handler)(int); +static void (*sig_abrt_handler)(int); +static void (*sig_term_handler)(int); +static void (*sig_bus_handler)(int); +static void (*sig_alrm_handler)(int); + +static void resetsighandlers (void) { + (void) signal (SIGINT, sig_int_handler); + (void) signal (SIGSEGV, sig_segv_handler); + (void) signal (SIGABRT, sig_abrt_handler); + (void) signal (SIGTERM, sig_term_handler); + (void) signal (SIGBUS, sig_bus_handler); +} + +static void caughtsigmsg (int sig) { + if (verbose < 0) return; + printf ("c\nc CAUGHT SIGNAL %d", sig); + switch (sig) { + case SIGINT: printf (" SIGINT"); break; + case SIGSEGV: printf (" SIGSEGV"); break; + case SIGABRT: printf (" SIGABRT"); break; + case SIGTERM: printf (" SIGTERM"); break; + case SIGBUS: printf (" SIGBUS"); break; + case SIGALRM: printf (" SIGALRM"); break; + default: break; + } + printf ("\nc\n"); + fflush (stdout); +} + +static void catchsig (int sig) { + if (!catchedsig) { + catchedsig = 1; + caughtsigmsg (sig); + fputs ("c s UNKNOWN\n", stdout); + fflush (stdout); + if (verbose >= 0) { + lglflushtimers (lgl4sigh); + lglstats (lgl4sigh); + caughtsigmsg (sig); + } + } + resetsighandlers (); + if (!getenv ("LGLNABORT")) raise (sig); else exit (1); +} + +static void setsighandlers (void) { + sig_int_handler = signal (SIGINT, catchsig); + sig_segv_handler = signal (SIGSEGV, catchsig); + sig_abrt_handler = signal (SIGABRT, catchsig); + sig_term_handler = signal (SIGTERM, catchsig); + sig_bus_handler = signal (SIGBUS, catchsig); +} + +static int timelimit = -1, caughtalarm = 0; + +static void catchalrm (int sig) { + assert (sig == SIGALRM); + if (!caughtalarm) { + caughtalarm = 1; + caughtsigmsg (sig); + if (timelimit >= 0) { + printf ("c time limit of %d reached after %.1f seconds\nc\n", + timelimit, lglsec (lgl4sigh)); + fflush (stdout); + } + } +} + +static int checkalarm (void * ptr) { + assert (ptr == (void*) &caughtalarm); + return caughtalarm; +} + +static int primes[] = { + 200000033, 200000039, 200000051, 200000069, 200000081, +}; + +static int nprimes = sizeof primes / sizeof *primes; + + + +int *buffer; +int length; +int offset; + +int *outbuffer; +int outoffset; + +int getInt() { + if (offset>=length) { + ssize_t ptr; + offset = 0; + do { + ptr=read(0, buffer, sizeof(int)*IS_BUFFERSIZE); + if (ptr == -1) + exit(-1); + } while(ptr==0); + ssize_t bytestoread=(4-(ptr & 3)) & 3; + while(bytestoread != 0) { + ssize_t p=read(0, &((char *)buffer)[ptr], bytestoread); + if (p == -1) + exit(-1); + bytestoread -= p; + ptr += p; + } + length = ptr / 4; + offset = 0; + } + + return buffer[offset++]; +} + +void flushInts() { + ssize_t bytestowrite=sizeof(int)*outoffset; + ssize_t byteswritten=0; + do { + ssize_t n=write(IS_OUT_FD, &((char *)outbuffer)[byteswritten], bytestowrite); + if (n == -1) { + fprintf(stderr, "Write failure\n"); + exit(-1); + } + bytestowrite -= n; + byteswritten += n; + } while(bytestowrite != 0); + outoffset = 0; +} + +void putInt(int value) { + if (outoffset>=IS_BUFFERSIZE) { + flushInts(); + } + outbuffer[outoffset++]=value; +} + +void readClauses(LGL *solver) { + bool haveClause = false; + while(true) { + int lit=getInt(); + if (lit!=0) { + haveClause = true; + lgladd(solver, lit); + } else { + if (haveClause) { + lgladd(solver, 0); + haveClause = false; + } else { + //done with clauses + return; + } + } + } +} + +void processCommands(LGL *solver) { + while(true) { + int command=getInt(); + switch(command) { + case IS_FREEZE: { + int var=getInt(); + lglfreeze(solver, var); + break; + } + case IS_RUNSOLVER: { + int ret = lglsat(solver); + if (ret == 10) { + putInt(IS_SAT); + int numvars=lglmaxvar(solver); + putInt(numvars); + for(int i=1;i<=numvars;i++) { + putInt(lglderef(solver, i) > 0); + } + } else if (ret == 20) { + putInt(IS_UNSAT); + } else { + putInt(IS_INDETER); + } + flushInts(); + return; + } + default: + fprintf(stderr, "Unreconized command\n"); + exit(-1); + } + } +} + +void processSAT(LGL *solver) { + buffer=(int *) malloc(sizeof(int)*IS_BUFFERSIZE); + offset=0; + length=0; + outbuffer=(int *) malloc(sizeof(int)*IS_BUFFERSIZE); + outoffset=0; + + while(true) { + double initial_time = cpuTime(); + readClauses(solver); + double parse_time = cpuTime(); + processCommands(solver); + double finish_time = cpuTime(); + printf("Parse time: %12.2f s Solve time:%12.2f s\n", parse_time-initial_time, finish_time-parse_time); + } +} + + + +int main (int argc, char ** argv) { + int res, i, j, val, len, lineno, simponly, count; + const char * pname, * match, * p, * err, * thanks; + int maxvar, lit, nopts, simplevel; + FILE * pfile; + char * tmp; + LGL * lgl; + lineno = 1; + res = simponly = simplevel = 0; + pname = thanks = 0; + lgl4sigh = lgl = lglinit (); + setsighandlers (); + for (i = 1; i < argc; i++) { + if (!strcmp (argv[i], "-h") || !strcmp (argv[i], "--help")) { + printf ("usage: lingeling [