1 /* Copyright (c) 2015 Regents of the University of California
3 * Author: Brian Demsky <bdemsky@uci.edu>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
10 #include "inc_solver.h"
11 #define SATSOLVER "sat_solver"
15 IncrementalSolver * allocIncrementalSolver() {
16 IncrementalSolver *This=(IncrementalSolver *)ourmalloc(sizeof(IncrementalSolver));
17 This->buffer=((int *)ourmalloc(sizeof(int)*IS_BUFFERSIZE));
25 void deleteIncrementalSolver(IncrementalSolver * This) {
27 ourfree(This->buffer);
28 if (This->solution != NULL)
29 ourfree(This->solution);
33 void resetSolver(IncrementalSolver * This) {
39 void addClauseLiteral(IncrementalSolver * This, int literal) {
40 This->buffer[This->offset++]=literal;
41 if (This->offset==IS_BUFFERSIZE) {
42 flushBufferSolver(This);
46 void addArrayClauseLiteral(IncrementalSolver * This, uint numliterals, int * literals) {
47 for(uint i=0;i<numliterals; i++) {
48 This->buffer[This->offset++]=literals[i];
49 if (This->offset==IS_BUFFERSIZE) {
50 flushBufferSolver(This);
53 This->buffer[This->offset++]=0;
54 if (This->offset==IS_BUFFERSIZE) {
55 flushBufferSolver(This);
59 void finishedClauses(IncrementalSolver * This) {
60 addClauseLiteral(This, 0);
63 void freeze(IncrementalSolver * This, int variable) {
64 addClauseLiteral(This, IS_FREEZE);
65 addClauseLiteral(This, variable);
68 int solve(IncrementalSolver * This) {
71 return getSolution(This);
75 void startSolve(IncrementalSolver *This) {
76 addClauseLiteral(This, IS_RUNSOLVER);
77 flushBufferSolver(This);
80 int getSolution(IncrementalSolver * This) {
81 int result=readIntSolver(This);
82 if (result == IS_SAT) {
83 int numVars=readIntSolver(This);
84 if (numVars > This->solutionsize) {
85 if (This->solution != NULL)
86 ourfree(This->solution);
87 This->solution = (int *) ourmalloc((numVars+1)*sizeof(int));
88 This->solution[0] = 0;
90 readSolver(This, &This->solution[1], numVars * sizeof(int));
91 This->solutionsize = numVars;
96 int readIntSolver(IncrementalSolver * This) {
98 readSolver(This, &value, 4);
102 void readSolver(IncrementalSolver * This, void * tmp, ssize_t size) {
103 char *result = (char *) tmp;
104 ssize_t bytestoread=size;
107 ssize_t n=read(This->from_solver_fd, &((char *)result)[bytesread], bytestoread);
109 model_print("Read failure\n");
114 } while(bytestoread != 0);
117 bool getValueSolver(IncrementalSolver * This, int variable) {
118 return This->solution[variable];
121 void createSolver(IncrementalSolver * This) {
124 if (pipe(to_pipe) || pipe(from_pipe)) {
125 model_print("Error creating pipe.\n");
128 if ((This->solver_pid = fork()) == -1) {
129 model_print("Error forking.\n");
132 if (This->solver_pid == 0) {
136 int fd=open("log_file", O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
138 if ((dup2(to_pipe[0], 0) == -1) ||
139 (dup2(from_pipe[1], IS_OUT_FD) == -1) ||
140 (dup2(fd, 1) == -1)) {
141 model_print("Error duplicating pipes\n");
144 execlp(SATSOLVER, SATSOLVER, NULL);
145 model_print("execlp Failed\n");
149 This->to_solver_fd = to_pipe[1];
150 This->from_solver_fd = from_pipe[0];
156 void killSolver(IncrementalSolver * This) {
157 close(This->to_solver_fd);
158 close(This->from_solver_fd);
160 if (This->solver_pid > 0) {
162 kill(This->solver_pid, SIGKILL);
163 waitpid(This->solver_pid, &status, 0);
167 void flushBufferSolver(IncrementalSolver * This) {
168 ssize_t bytestowrite=sizeof(int)*This->offset;
169 ssize_t byteswritten=0;
171 ssize_t n=write(This->to_solver_fd, &((char *)This->buffer)[byteswritten], bytestowrite);
173 perror("Write failure\n");
174 model_print("to_solver_fd=%d\n",This->to_solver_fd);
179 } while(bytestowrite != 0);