cloud-sat/workers/sharer.cpp
2022-09-08 13:54:29 +08:00

82 lines
2.5 KiB
C++

#include "../light.hpp"
#include "basesolver.hpp"
#include "sharer.hpp"
#include "clause.hpp"
#include <unistd.h>
void * share_worker(void *arg) {
sharer * sq = (sharer *)arg;
while (true) {
usleep(sq->share_intv);
if (terminated) break;
}
for (int i = 0; i < sq->producers.size(); i++) {
sq->producers[i]->export_clause_to(sq->cls);
int number = sq->cls.size();
int percent = sq->sort_clauses(i);
if (percent < 75) {
sq->producers[i]->broaden_export_limit();
}
else if (percent > 98) {
sq->producers[i]->restrict_export_limit();
}
for (int j = 0; j < sq->consumers.size(); j++) {
if (sq->producers[i]->id == sq->consumers[j]->id) continue;
for (int k = 0; k < sq->cls.size(); k++)
sq->cls[k]->increase_refs(1);
sq->consumers[j]->import_clauses_from(sq->cls);
}
for (int k = 0; k < sq->cls.size(); k++)
sq->cls[k]->free_clause();
}
return NULL;
}
int sharer::sort_clauses(int x) {
vec<vec<clause_store *>> *buck = &bucket[x];
for (int i = 0; i < cls.size(); i++) {
int sz = cls[i]->size;
while (sz > buck->size()) buck->push();
if (sz * (buck[sz - 1].size() + 1) <= share_lits)
buck[sz - 1].push(cls[i]);
else
cls[i]->free_clause();
}
cls.clear();
int space = share_lits;
for (int i = 0; i < buck->size(); i++) {
int clause_num = space / (i + 1);
if (!clause_num) return;
if (clause_num >= buck[i].size()) {
space -= buck[i].size() * (i + 1);
for (int j = 0; j < buck[i].size(); j++)
cls.push(buck[i][j]);
buck[i].clear();
}
else {
space -= clause_num * (i + 1);
for (int j = 0; j < clause_num; j++) {
cls.push(buck[i].last());
buck[i].pop();
}
}
}
return (share_lits - space) * 100 / share_lits;
}
void light::share() {
int sharers_number = 1;
for (int i = 0; i < sharers_number; i++) {
sharer* s = new sharer(i, OPT(share_intv), OPT(share_lits));
s->producers.push(workers[0]);
s->consumers.push(workers[1]);
sharers.push(s);
}
pthread_t *ptr = new pthread_t[sharers_number];
for (int i = 0; i < sharers_number; i++) {
pthread_create(&ptr[i], NULL, share_worker, shares[i]);
}
}