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 This->buffer[This->offset++] = 0;
71 if (This->offset == IS_BUFFERSIZE) {
72 flushBufferSolver(This);
76 void freeze(IncrementalSolver *This, int variable) {
77 addClauseLiteral(This, IS_FREEZE);
78 addClauseLiteral(This, variable);
81 int solve(IncrementalSolver *This) {
84 return getSolution(This);
87 void startSolve(IncrementalSolver *This) {
88 addClauseLiteral(This, IS_RUNSOLVER);
89 flushBufferSolver(This);
92 int getSolution(IncrementalSolver *This) {
93 int result = readIntSolver(This);
94 if (result == IS_SAT) {
95 int numVars = readIntSolver(This);
96 if (numVars > This->solutionsize) {
97 if (This->solution != NULL)
98 ourfree(This->solution);
99 This->solution = (int *) ourmalloc((numVars + 1) * sizeof(int));
100 This->solution[0] = 0;
102 readSolver(This, &This->solution[1], numVars * sizeof(int));
103 This->solutionsize = numVars;
108 int readIntSolver(IncrementalSolver *This) {
110 readSolver(This, &value, 4);
114 void readSolver(IncrementalSolver *This, void *tmp, ssize_t size) {
115 char *result = (char *) tmp;
116 ssize_t bytestoread = size;
117 ssize_t bytesread = 0;
119 ssize_t n = read(This->from_solver_fd, &((char *)result)[bytesread], bytestoread);
121 model_print("Read failure\n");
126 } while (bytestoread != 0);
129 bool getValueSolver(IncrementalSolver *This, int variable) {
130 return This->solution[variable];
133 void createSolver(IncrementalSolver *This) {
136 if (pipe(to_pipe) || pipe(from_pipe)) {
137 model_print("Error creating pipe.\n");
140 if ((This->solver_pid = fork()) == -1) {
141 model_print("Error forking.\n");
144 if (This->solver_pid == 0) {
148 int fd = open("log_file", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
150 if ((dup2(to_pipe[0], 0) == -1) ||
151 (dup2(from_pipe[1], IS_OUT_FD) == -1) ||
152 (dup2(fd, 1) == -1)) {
153 model_print("Error duplicating pipes\n");
156 execlp(SATSOLVER, SATSOLVER, NULL);
157 model_print("execlp Failed\n");
161 This->to_solver_fd = to_pipe[1];
162 This->from_solver_fd = from_pipe[0];
168 void killSolver(IncrementalSolver *This) {
169 close(This->to_solver_fd);
170 close(This->from_solver_fd);
172 if (This->solver_pid > 0) {
174 kill(This->solver_pid, SIGKILL);
175 waitpid(This->solver_pid, &status, 0);
179 //DEBUGGING CODE STARTS
181 //DEBUGGING CODE ENDS
183 void flushBufferSolver(IncrementalSolver *This) {
184 ssize_t bytestowrite = sizeof(int) * This->offset;
185 ssize_t byteswritten = 0;
187 for (uint i = 0; i < This->offset; i++) {
190 if (This->buffer[i] == 0) {
197 printf("%d", This->buffer[i]);
202 ssize_t n = write(This->to_solver_fd, &((char *)This->buffer)[byteswritten], bytestowrite);
204 perror("Write failure\n");
205 model_print("to_solver_fd=%d\n",This->to_solver_fd);
210 } while (bytestowrite != 0);