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"
16 IncrementalSolver * allocIncrementalSolver() {
17 IncrementalSolver *This=(IncrementalSolver *)ourmalloc(sizeof(IncrementalSolver));
18 This->buffer=((int *)ourmalloc(sizeof(int)*IS_BUFFERSIZE));
26 void deleteIncrementalSolver(IncrementalSolver * This) {
28 ourfree(This->buffer);
29 if (This->solution != NULL)
30 ourfree(This->solution);
34 void resetSolver(IncrementalSolver * This) {
40 void addClauseLiteral(IncrementalSolver * This, int literal) {
41 This->buffer[This->offset++]=literal;
42 if (This->offset==IS_BUFFERSIZE) {
43 flushBufferSolver(This);
47 void addArrayClauseLiteral(IncrementalSolver * This, uint numliterals, int * literals) {
50 uint bufferspace=IS_BUFFERSIZE-This->offset;
51 uint numtowrite=numliterals-index;
52 if (bufferspace > numtowrite) {
53 memcpy(&This->buffer[This->offset], &literals[index], numtowrite*sizeof(int));
54 This->offset+=numtowrite;
55 This->buffer[This->offset++]=0; //have one extra spot always
56 if (This->offset==IS_BUFFERSIZE) {//Check if full
57 flushBufferSolver(This);
61 memcpy(&This->buffer[This->offset], &literals[index], bufferspace*sizeof(int));
62 This->offset+=bufferspace;
64 flushBufferSolver(This);
69 void finishedClauses(IncrementalSolver * This) {
70 addClauseLiteral(This, 0);
73 void freeze(IncrementalSolver * This, int variable) {
74 addClauseLiteral(This, IS_FREEZE);
75 addClauseLiteral(This, variable);
78 int solve(IncrementalSolver * This) {
81 return getSolution(This);
85 void startSolve(IncrementalSolver *This) {
86 addClauseLiteral(This, IS_RUNSOLVER);
87 flushBufferSolver(This);
90 int getSolution(IncrementalSolver * This) {
91 int result=readIntSolver(This);
92 if (result == IS_SAT) {
93 int numVars=readIntSolver(This);
94 if (numVars > This->solutionsize) {
95 if (This->solution != NULL)
96 ourfree(This->solution);
97 This->solution = (int *) ourmalloc((numVars+1)*sizeof(int));
98 This->solution[0] = 0;
100 readSolver(This, &This->solution[1], numVars * sizeof(int));
105 int readIntSolver(IncrementalSolver * This) {
107 readSolver(This, &value, 4);
111 void readSolver(IncrementalSolver * This, void * tmp, ssize_t size) {
112 char *result = (char *) tmp;
113 ssize_t bytestoread=size;
116 ssize_t n=read(This->from_solver_fd, &((char *)result)[bytesread], bytestoread);
118 model_print("Read failure\n");
123 } while(bytestoread != 0);
126 bool getValueSolver(IncrementalSolver * This, int variable) {
127 return This->solution[variable];
130 void createSolver(IncrementalSolver * This) {
133 if (pipe(to_pipe) || pipe(from_pipe)) {
134 model_print("Error creating pipe.\n");
137 if ((This->solver_pid = fork()) == -1) {
138 model_print("Error forking.\n");
141 if (This->solver_pid == 0) {
145 int fd=open("log_file", O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
147 if ((dup2(to_pipe[0], 0) == -1) ||
148 (dup2(from_pipe[1], IS_OUT_FD) == -1) ||
149 (dup2(fd, 1) == -1)) {
150 model_print("Error duplicating pipes\n");
153 execlp(SATSOLVER, SATSOLVER, NULL);
154 model_print("execlp Failed\n");
158 This->to_solver_fd = to_pipe[1];
159 This->from_solver_fd = from_pipe[0];
165 void killSolver(IncrementalSolver * This) {
166 close(This->to_solver_fd);
167 close(This->from_solver_fd);
169 if (This->solver_pid > 0) {
171 kill(This->solver_pid, SIGKILL);
172 waitpid(This->solver_pid, &status, 0);
176 void flushBufferSolver(IncrementalSolver * This) {
177 ssize_t bytestowrite=sizeof(int)*This->offset;
178 ssize_t byteswritten=0;
180 ssize_t n=write(This->to_solver_fd, &((char *)This->buffer)[byteswritten], bytestowrite);
182 perror("Write failure\n");
183 model_print("to_solver_fd=%d\n",This->to_solver_fd);
188 } while(bytestowrite != 0);