-
Notifications
You must be signed in to change notification settings - Fork 1
/
maxsat-omp.c
376 lines (284 loc) · 9.63 KB
/
maxsat-omp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
void MAXSAT(int cur_var, int* cur_comb, int root, int* sat_clauses);
int** parseFile(char name_of_the_file[40]);
int* get_cur_comb(int cur_var, int* prev_comb);
int* alloc_int_array(int n);
int clauses_satisfied(int cur_var, int* cur_comb, int* n_clauses_unsatisfied, int* sat_clauses);
void print_sol();
void free_matrix();
void copy_array(int* dest, int* src);
int are_there_idle_threads(int cur_lvl);
int* get_cur_sat_clauses(int cur_var, int* prev_sat_clauses);
int cur_maxsat=0; //contains current best score
int n_solutions=0; //contains number of solutions that reach the best score
int* cur_sol; //contains one combination that reaches the best score
int* thread_status; //vector of thread status. each thread can be idle(0) or active(1)
int n_threads; //number of threads
int n_clauses; //number of clauses
int n_vars; //number of variables
int** clause_matrix; //matrix with all the clauses to be tested
int main(int argc, char** argv){
n_threads = omp_get_max_threads();
if(argc!=2){
printf("Usage: maxsat-omp input-file.in");
exit(1);
}
thread_status = (int *) malloc(n_threads*sizeof(int));
for(int i=0; i<n_threads; i++)
thread_status[i]=0;
clause_matrix = parseFile(argv[1]);
//allocate needed arrays
int *first_comb = alloc_int_array(n_vars);
first_comb[0]=1;
int *first_comb2 = alloc_int_array(n_vars);
first_comb2[0]=-1;
int* sat_clauses_1 = alloc_int_array(n_clauses);
int* sat_clauses_2 = alloc_int_array(n_clauses);
cur_sol = alloc_int_array(n_vars);
//root node of the binary tree.
#pragma omp parallel
{
#pragma omp single nowait
{
//here the two children nodes are spawned. if there are avaiable threads, a task is created.
//otherwise, this thread will process both nodes
if(n_threads>1){
#pragma omp task
{
MAXSAT(-1, first_comb2, 1, sat_clauses_2); //branch with first variable set to false
}
}
else{
MAXSAT(-1, first_comb2, 1, sat_clauses_2);
}
MAXSAT(1, first_comb, 1, sat_clauses_1); //branch with first variable set to true
}
}
free(thread_status);
printf("%d %d\n", cur_maxsat, n_solutions);
print_sol(cur_sol, n_vars);
free_matrix();
free(cur_sol);
exit(0);
}
void MAXSAT(int cur_var, int* cur_comb, int root, int* sat_clauses){
int n_clauses_satisfied;
int n_clauses_unsatisfied=0;
int next_var;
int tid = omp_get_thread_num();
int* next_comb, *next_sat_clauses;
int* next_comb2, *next_sat_clauses2;
#pragma atomic write
thread_status[tid] = abs(cur_var);
n_clauses_satisfied = clauses_satisfied(cur_var, cur_comb, &n_clauses_unsatisfied, sat_clauses); //calculate number of clauses satisfied and unsatisfied by current combination
if(abs(cur_var)<n_vars){ //we're not on the last variable -> we're not on a leaf
//prune condition
if(n_clauses - n_clauses_unsatisfied < cur_maxsat){
//no need to go further, no better solution we'll be found -> suggestion from the project sheet
#pragma atomic write
thread_status[tid]=0;
free(cur_comb);
free(sat_clauses);
return;
}
else{
//continue going down the tree
next_var = abs(cur_var)+1;
next_comb = get_cur_comb(-next_var, cur_comb);
next_comb2 = get_cur_comb(next_var, cur_comb);
next_sat_clauses = get_cur_sat_clauses(-next_var, sat_clauses);
next_sat_clauses2 = get_cur_sat_clauses(next_var, sat_clauses);
//here the two children nodes are spawned. if there are avaiable threads, a task is created.
//otherwise, this thread will process both nodes
if(are_there_idle_threads(abs(cur_var))){
#pragma omp task
{
MAXSAT(-next_var, next_comb, 1, next_sat_clauses); //branch with next var set to false and "root" set to true.
//this means the thread will return up until here and then become idle again.
}
}
else
MAXSAT(-next_var, next_comb, 0, next_sat_clauses); //branch with next var set to false
MAXSAT(next_var, next_comb2, 0, next_sat_clauses2); //branch with next var set to true
if(root){ //if this is a root node, the thread becomes idle here. otherwise, it still has to return until its root node
#pragma atomic write
thread_status[tid]=0;
}
return;
}
}
else{ //we're in a leaf
#pragma omp critical (solutions)
{
if(n_clauses_satisfied == cur_maxsat){ //if this combination satisfies the same number of clauses as the current best, increase the number of solutions
n_solutions++;
}
else{
if(n_clauses_satisfied > cur_maxsat){ //if this combination satisfies more clauses than the previous best...
copy_array(cur_sol,cur_comb); //store the solution
cur_maxsat = n_clauses_satisfied; //update the best score
n_solutions=1;
}
}
}
if(root){ //if this is a root node, the thread becomes idle here. otherwise, it still has to return until its root node
#pragma atomic write
thread_status[tid]=0;
}
free(cur_comb);
free(sat_clauses);
return;
}
}
// ##################################################################################################################################
// ##################################################################################################################################
// ##################################################################################################################################
// ##################################################################################################################################
// ##################################################################################################################################
// ##################################################################################################################################
// ##################################################################################################################################
// Utility functions down there
int* get_cur_sat_clauses(int cur_var, int* prev_sat_clauses){
int* cur_sat_clauses;
if(cur_var<0)
cur_sat_clauses = prev_sat_clauses;
else{
cur_sat_clauses = (int *) malloc(n_clauses*sizeof(int));
for(int i=0; i<n_clauses; i++)
cur_sat_clauses[i]=prev_sat_clauses[i];
}
return cur_sat_clauses;
}
int are_there_idle_threads(int cur_lvl){
int idles = 0;
int max = 0;
int tid = omp_get_thread_num();
for(int i=0; i<n_threads; i++){
if(i==tid)
continue;
if(thread_status[i]==0)
idles = 1;
else{
if(thread_status[i]>=max)
max = thread_status[i];
}
}
return (cur_lvl>=max);
}
void copy_array(int* dest, int* src){
for(int i=0; i<n_vars; i++){
dest[i]=src[i];
}
}
int* alloc_int_array(int n){
int* first_comb = (int *) malloc(n*sizeof(int));
for(int i=0; i<n; i++)
first_comb[i]=0;
return first_comb;
}
//to obtain the combination, we copy the previous combination and assign the current variable accordingly. actually we only copy half of the time. half of the children reuse the parent's array, the other's copy it.
int* get_cur_comb(int cur_var, int* prev_comb){
int* cur_comb;
if(cur_var<0){
cur_comb = (int *) malloc(n_vars*sizeof(int));
copy_array(cur_comb, prev_comb);
}
else
cur_comb=prev_comb;
cur_comb[abs(cur_var)-1]=cur_var;
return cur_comb;
}
//for a given combination and current variable, calculate the number of clauses satisfied (and unsatisfied)
int clauses_satisfied(int cur_var, int* cur_comb, int* unsatisfied, int* sat_clauses){
int i, j, n_clauses_satisfied=0;
int unsat = 0;
int var;
for(i=0; i<n_clauses; i++){ //for each clause
if(sat_clauses[i]==-1){
*unsatisfied = *unsatisfied + 1;
unsat=0;
continue;
}
if(sat_clauses[i]==1){
n_clauses_satisfied++;
unsat=0;
continue;
}
for(j=0; j<20; j++){ //for each variable
var = abs(clause_matrix[i][j]);
if(var==0 || var>abs(cur_var)) // end of clause or we don't know the next variable assignments
break;
if(cur_comb[var-1] == clause_matrix[i][j]){ //if the variable corresponds
sat_clauses[i]=1;
n_clauses_satisfied++;
unsat=0;
break; //only one variable needs to match for the clause to be satisfied
}
else
unsat=1;
}
if(unsat==1 && clause_matrix[i][j]==0){
sat_clauses[i]=-1;
*unsatisfied = *unsatisfied + 1;
unsat=0;
}
}
return n_clauses_satisfied;
}
void print_sol(){
int i;
for(i=0; i<n_vars; i++){
printf("%d ", cur_sol[i]);
fflush(stdout);
}
printf("\n");
}
void free_matrix(){
int i;
for(i=0; i<n_clauses; i++)
free(clause_matrix[i]);
free(clause_matrix);
}
int **parseFile(char name_of_the_file[40]){
int n,j,i = 0;
char line[80];
int matrix_line = 0, matrix_column = 0;
int field = -1;
FILE *fr;
char *start;
int ** clause_matrix;
fr = fopen (name_of_the_file, "rt"); /* open the file for reading */
/*Read n_vars and n_clauses*/
fgets(line,80,fr);
sscanf(line, "%d" "%d", &n_vars, &n_clauses);
//printf("N_vars = %d N_clauses = %d \n",*n_vars,*n_clauses);
clause_matrix = (int**)malloc( n_clauses * sizeof( int* ));
for (i = 0; i < n_clauses; i++){
clause_matrix[i] = (int *)malloc(20 * sizeof(int));
}
for (i = 0; i < n_clauses; i++){
for (j = 0; j < 20; j++)
{
clause_matrix[i][j] = 0;
}
}
//printf("-------clauses---------------\n");
while(fgets(line, 80, fr) != NULL){
start = line;
while (sscanf(start, "%d%n", &field, &n) == 1 && field != 0) {
clause_matrix[matrix_line][matrix_column] = field;
//printf("%d ", clause_matrix[matrix_line][matrix_column]);
start += n;
matrix_column ++;
}
//printf("\n");
matrix_column= 0;
matrix_line ++;
}
//printf("----------------------------\n" );
fclose(fr);
return clause_matrix;
}