-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.cpp
More file actions
349 lines (313 loc) · 7.83 KB
/
parallel.cpp
File metadata and controls
349 lines (313 loc) · 7.83 KB
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
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <unordered_set>
#include <set>
#include <iterator>
#include <mutex>
#include <semaphore.h>
#include <pthread.h>
#define LITS true
#define PRINT true
#define THREADS 12
// g++ -std=c++17 -O2 -lpthread main.cpp -o prog
using namespace std;
typedef struct {
int inicio;
int fim;
} Intervalo;
int n_variaveis, n_clausulas;
vector<vector<int> > clausulas;
vector<vector<bool> > l_clausulas;
set<int> idx_clausulas_nao_satisfeitas;
#if LITS
vector<int> literais_falsos;
#endif
vector<bool> valores;
vector<Intervalo> intervalos;
// ==== THREADS ====
int valorFlip = -1;
bool rodando = true;
bool ehFlip = false;
sem_t semaforos_execucao[THREADS];
sem_t semaforos_impressao[THREADS];
pthread_t threads[THREADS];
mutex mtx;
// =================
void le_entrada();
void le_comandos();
void executa_full();
void executa_flip();
void imprime_resultado();
void calcula_intervalos();
void executa_comando(Intervalo intervalo);
// ==== THREADS ====
void inicia_threads();
void inicia_semaforos();
void finaliza_threads();
void combina_threads();
void destroi_semaforos();
void executa_threads();
// =================
bool compara(pair<int, int>& a, pair<int, int>& b) {
if (a.second == b.second) {
return abs(a.first) > abs(b.first);
}
return a.second > b.second;
}
Intervalo calcula_intervalo(int atual, int tamanho, int pedaco) {
int inicio = (tamanho * atual) / pedaco;
int fim = ((tamanho * (atual + 1)) / pedaco) - 1;
Intervalo intervalo;
intervalo.inicio = inicio;
intervalo.fim = fim;
return intervalo;
}
// ==== THREADS ====
void* thread(void* arg) {
int indice = *((int *) arg);
delete (int *) arg;
while (rodando) {
sem_wait(&semaforos_execucao[indice]);
if (rodando) {
Intervalo intervalo = intervalos[indice];
// printf("executa comando com intervalo %d, %d\n", intervalo.inicio, intervalo.fim);
executa_comando(intervalo);
sem_post(&semaforos_impressao[indice]);
}
}
return NULL;
}
// =================
int main() {
// ==== THREADS ====
inicia_semaforos();
// printf("inicia semaforos\n");
inicia_threads();
// printf("inicia threads\n");
// =================
// printf("le entrada\n");
le_entrada();
// printf("calcula intervalos\n");
calcula_intervalos();
// printf("le comandos\n");
le_comandos();
// ==== THREADS ====
// printf("combina threads\n");
combina_threads();
// printf("destroi semaforos\n");
destroi_semaforos();
// =================
return 0;
}
void calcula_intervalos() {
for (int i = 0; i < THREADS; i++) {
Intervalo intervalo = calcula_intervalo(i, n_clausulas, THREADS);
intervalos.push_back(intervalo);
}
}
void le_entrada() {
scanf("%d", &n_variaveis);
scanf("%d", &n_clausulas);
l_clausulas.resize(n_variaveis, vector<bool>(n_clausulas));
valores.resize(n_variaveis, false);
#if LITS
literais_falsos.resize(n_variaveis, 0);
#endif
for (int i = 0; i < n_clausulas; i++) {
vector<int> literais;
int num;
do {
scanf("%d", &num);
if (num != 0) {
literais.push_back(num);
l_clausulas[abs(num) - 1][i] = true;
}
} while (num != 0);
clausulas.push_back(literais);
literais.clear();
}
}
void le_comandos() {
char comando[4];
while(scanf("%4s", comando) != EOF) {
if (strcmp(comando, "full") == 0) {
// printf("full\n");
executa_full();
}
if (strcmp(comando, "flip") == 0) {
// printf("flip\n");
executa_flip();
}
for (int i = 0; i < THREADS; i++) {
sem_wait(&semaforos_impressao[i]);
}
// printf("imprime resultado\n");
imprime_resultado();
}
// printf("finaliza threads\n");
finaliza_threads();
}
void executa_full() {
idx_clausulas_nao_satisfeitas.clear();
#if LITS
literais_falsos.clear();
#endif
int valor;
for (int i = 0; i < n_variaveis; i++) {
scanf("%d", &valor);
valores[abs(valor) - 1] = valor > 0 ? true : false;
}
// printf("executa threads full\n");
ehFlip = false;
executa_threads();
}
void executa_flip() {
int valor;
scanf("%d", &valor);
valores[abs(valor) - 1] = !valores[abs(valor) - 1];
ehFlip = true;
valorFlip = valor;
executa_threads();
}
void executa_comando(Intervalo intervalo) {
for (int i = intervalo.inicio; i <= intervalo.fim; i++) {
if (ehFlip) {
// printf("ehFlip\n");
if (!l_clausulas[abs(valorFlip) - 1][i]) {
continue;
}
}
bool satisfaz = false;
for (uint j = 0; j < clausulas[i].size(); j++) {
int base = clausulas[i][j];
int valor = valores[abs(base) - 1] ? base : base * -1;
if (valor > 0) {
satisfaz = true;
break;
}
}
// printf("index: %d\n", i);
if (satisfaz) {
if (ehFlip) {
if (idx_clausulas_nao_satisfeitas.find(i) != idx_clausulas_nao_satisfeitas.end()) {
#if LITS
for (uint j = 0; j < clausulas[i].size(); j++) {
int base = clausulas[i][j];
bool flip = valores[abs(base) - 1];
if (abs(base) == valorFlip) {
flip = !flip;
}
int valor = flip ? base : base * -1;
if (valor < 0) {
literais_falsos[abs(base) - 1] -= 1;
}
}
#endif
mtx.lock();
idx_clausulas_nao_satisfeitas.erase(i);
mtx.unlock();
}
}
} else {
if (idx_clausulas_nao_satisfeitas.find(i) == idx_clausulas_nao_satisfeitas.end()) {
// printf("ultimo index que entrou: %d\n", i);
mtx.lock();
idx_clausulas_nao_satisfeitas.insert(i);
mtx.unlock();
#if LITS
for (uint j = 0; j < clausulas[i].size(); j++) {
int base = clausulas[i][j];
int valor = valores[abs(base) - 1] ? base : base * -1;
if (valor < 0) {
literais_falsos[abs(base) - 1] += 1;
}
}
#endif
}
}
}
// printf("executa comando finalizado\n");
}
void imprime_resultado() {
if (idx_clausulas_nao_satisfeitas.empty()) {
#if PRINT
printf("SAT\n");
#endif
} else {
int size = idx_clausulas_nao_satisfeitas.size();
#if PRINT
printf("[%d clausulas falsas]", size);
#endif
set<int> ordered(idx_clausulas_nao_satisfeitas.begin(), idx_clausulas_nao_satisfeitas.end());
for (const auto& element: ordered) {
#if PRINT
printf(" %d", element);
#endif
}
#if PRINT
printf("\n");
#endif
#if LITS
#if PRINT
printf("[lits]");
#endif
vector<pair<int, int>> lits;
for (int i = 0; i < n_variaveis; i++) {
if (literais_falsos[i] > 0) {
int literal = (i + 1) * (valores[i] ? 1 : -1) * -1;
int contador = literais_falsos[i];
lits.push_back(make_pair(literal, contador));
}
}
sort(lits.begin(), lits.end(), compara);
for (uint i = 0; i < lits.size(); i++) {
#if PRINT
printf(" %d", lits[i].first);
#endif
}
#if PRINT
printf("\n");
#endif
#endif
}
}
// MARK: - Threads
void inicia_semaforos() {
for (int i = 0; i < THREADS; i++) {
sem_init(&semaforos_execucao[i], 0, 0);
sem_init(&semaforos_impressao[i], 0, 0);
}
}
void destroi_semaforos() {
for (int i = 0; i < THREADS; i++) {
sem_destroy(&semaforos_execucao[i]);
sem_destroy(&semaforos_impressao[i]);
}
}
void inicia_threads() {
for (int i = 0; i < THREADS; i++) {
// int* arg = (int*) malloc(sizeof(int*));
// *arg = i;
pthread_create(&threads[i], NULL, thread, new int(i));
}
}
void combina_threads() {
for (int i = 0; i < THREADS; i++) {
pthread_join(threads[i], NULL);
}
}
void finaliza_threads() {
rodando = false;
for (int i = 0; i < THREADS; i++) {
sem_post(&semaforos_execucao[i]);
}
}
void executa_threads() {
for (int i = 0; i < THREADS; i++) {
sem_post(&semaforos_execucao[i]);
}
}