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));
19 This->solution = NULL;
20 This->solutionsize = 0;
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);
84 void startSolve(IncrementalSolver *This) {
85 addClauseLiteral(This, IS_RUNSOLVER);
86 flushBufferSolver(This);
89 int getSolution(IncrementalSolver *This) {
90 int result = readIntSolver(This);
91 if (result == IS_SAT) {
92 int numVars = readIntSolver(This);
93 if (numVars > This->solutionsize) {
94 if (This->solution != NULL)
95 ourfree(This->solution);
96 This->solution = (int *) ourmalloc((numVars + 1) * sizeof(int));
97 This->solution[0] = 0;
99 readSolver(This, &This->solution[1], numVars * sizeof(int));
100 This->solutionsize = numVars;
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;
114 ssize_t bytesread = 0;
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 //DEBUGGING CODE STARTS
178 //DEBUGGING CODE ENDS
180 void flushBufferSolver(IncrementalSolver *This) {
181 ssize_t bytestowrite = sizeof(int) * This->offset;
182 ssize_t byteswritten = 0;
184 for (uint i = 0; i < This->offset; i++) {
187 if (This->buffer[i] == 0) {
194 printf("%d", This->buffer[i]);
199 ssize_t n = write(This->to_solver_fd, &((char *)This->buffer)[byteswritten], bytestowrite);
201 perror("Write failure\n");
202 model_print("to_solver_fd=%d\n",This->to_solver_fd);
207 } while (bytestowrite != 0);