cloud-sat/workers/sharer.cpp

173 lines
6.5 KiB
C++
Raw Normal View History

2022-09-08 13:54:29 +08:00
#include "../light.hpp"
#include "basesolver.hpp"
#include "sharer.hpp"
#include "clause.hpp"
#include <unistd.h>
2022-12-04 23:30:36 +08:00
#include <boost/thread/thread.hpp>
2022-09-08 13:54:29 +08:00
void * share_worker(void *arg) {
2022-12-04 23:30:36 +08:00
int nums = 0;
2022-09-08 13:54:29 +08:00
sharer * sq = (sharer *)arg;
2023-02-24 07:58:40 +00:00
auto clk_st = std::chrono::high_resolution_clock::now();
2023-03-01 22:05:44 +08:00
double share_time = 0;
2022-09-08 13:54:29 +08:00
while (true) {
2022-12-04 23:30:36 +08:00
++nums;
2023-02-24 07:58:40 +00:00
if (sq->dps) {
// printf("c sharing thread start wait\n");
sq->waiting_for_all_ready();
// printf("c sharing thread start sharing with number %d\n", sq->waitings);
}
else {
usleep(sq->share_intv);
}
auto clk_now = std::chrono::high_resolution_clock::now();
int solve_time = std::chrono::duration_cast<std::chrono::milliseconds>(clk_now - clk_st).count();
printf("c round %d, time: %d.%03d\n", nums, solve_time / 1000, solve_time % 1000);
2022-12-04 23:30:36 +08:00
if (terminated) {
2023-02-24 07:58:40 +00:00
if (sq->dps) sq->sharing_finish();
2022-12-04 23:30:36 +08:00
break;
}
2022-09-15 10:31:41 +08:00
// printf("start sharing %d\n", sq->share_intv);
for (int i = 0; i < sq->producers.size(); i++) {
sq->cls.clear();
sq->producers[i]->export_clauses_to(sq->cls);
2022-12-04 23:30:36 +08:00
// printf("c size %d\n", sq->cls.size());
2022-09-15 10:31:41 +08:00
int number = sq->cls.size();
// printf("get %d exported clauses\n", number);
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 k = 0; k < sq->cls.size(); k++) {
// int x = sq->cls[k]->refs;
// printf("%d ", x);
// }
// puts("");
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();
}
2022-09-08 13:54:29 +08:00
}
2023-03-01 22:05:44 +08:00
auto clk_ed = std::chrono::high_resolution_clock::now();
share_time += 0.001 * std::chrono::duration_cast<std::chrono::milliseconds>(clk_ed - clk_now).count();
2023-02-24 07:58:40 +00:00
if (sq->dps) {
sq->sharing_finish();
}
2022-09-08 13:54:29 +08:00
}
2023-03-01 22:05:44 +08:00
printf("c sharing nums: %d\nc sharing time: %.2lf\n", nums, share_time);
2022-12-04 23:30:36 +08:00
// if (terminated) puts("terminated set to 1");
2022-09-08 13:54:29 +08:00
return NULL;
}
2022-12-04 23:30:36 +08:00
2023-03-15 14:15:44 +08:00
// void sharer::select_clauses(int id) {
// }
bool sharer::import_clauses(int id) {
int current_period = producers[id]->get_period() - 1, import_period = current_period - margin;
if (import_period < 0) return false;
basesolver *t = producers[id];
for (int i = 0; i < producers.size(); i++) {
if (i == id) continue;
basesolver *s = producers[i];
//wait for s reach period $import_period
// printf("c %d start waiting, since import_p is %d, current_p is %d\n", id, import_period, s->get_period());
// if (id == 3 && current_period == 84) printf("\tc thread 3 wait for %d\n", i);
boost::mutex::scoped_lock lock(s->mtx);
while (s->period <= import_period && s->winner_period > import_period && !s->terminated) {
s->cond.wait(lock);
}
// if (id == 3 && current_period == 84) printf("\tc thread 3 finish wait for %d\n", i);
if (s->terminated || s->winner_period <= import_period) return false;
// if (s->terminated) return;
period_clauses *pc = s->pq.find(import_period);
if (pc->period != import_period) {
printf("thread %d, now period = %d, import period = %d, import thread %d\n", id, current_period, import_period, i);
puts("*******************************************************");
}
// printf("c %d finish waiting %d %d\n", id, import_period, s->period_queue[pos]->period);
t->import_clauses_from(pc->cls);
}
return true;
// printf("c thread %d, period %d, import finish\n", id, current_period);
}
2022-09-08 13:54:29 +08:00
int sharer::sort_clauses(int x) {
for (int i = 0; i < cls.size(); i++) {
int sz = cls[i]->size;
2022-09-15 10:31:41 +08:00
// for (int j = 0; j < sz; j++) printf("%d ", cls[i]->data[j]);
// puts("");
while (sz > bucket[x].size()) bucket[x].push();
if (sz * (bucket[x][sz - 1].size() + 1) <= share_lits)
bucket[x][sz - 1].push(cls[i]);
// else
// cls[i]->free_clause();
2022-09-08 13:54:29 +08:00
}
cls.clear();
int space = share_lits;
2022-09-15 10:31:41 +08:00
for (int i = 0; i < bucket[x].size(); i++) {
2022-09-08 13:54:29 +08:00
int clause_num = space / (i + 1);
2022-09-15 10:31:41 +08:00
// printf("%d %d\n", clause_num, bucket[x][i].size());
if (!clause_num) break;
if (clause_num >= bucket[x][i].size()) {
space -= bucket[x][i].size() * (i + 1);
for (int j = 0; j < bucket[x][i].size(); j++)
cls.push(bucket[x][i][j]);
bucket[x][i].clear();
2022-09-08 13:54:29 +08:00
}
else {
space -= clause_num * (i + 1);
for (int j = 0; j < clause_num; j++) {
2022-09-15 10:31:41 +08:00
cls.push(bucket[x][i].last());
bucket[x][i].pop();
2022-09-08 13:54:29 +08:00
}
}
}
2022-09-15 10:31:41 +08:00
// for (int i = 0; i < cls.size(); i++) {
// int sz = cls[i]->size;
// for (int j = 0; j < sz; j++) printf("%d ", cls[i]->data[j]);
// puts("");
// }
2022-09-08 13:54:29 +08:00
return (share_lits - space) * 100 / share_lits;
}
void light::share() {
2022-12-04 23:30:36 +08:00
printf("c sharing start\n");
2023-03-15 14:15:44 +08:00
if (OPT(DPS) == 2) {
sharer* s = new sharer(0, OPT(share_intv), OPT(share_lits), OPT(DPS));
s->margin = OPT(margin);
2022-09-15 10:31:41 +08:00
for (int j = 0; j < OPT(threads); j++) {
s->producers.push(workers[j]);
2022-12-04 23:30:36 +08:00
workers[j]->in_sharer = s;
2022-09-15 10:31:41 +08:00
}
2022-09-08 13:54:29 +08:00
sharers.push(s);
}
2023-03-15 14:15:44 +08:00
else {
int sharers_number = 1;
for (int i = 0; i < sharers_number; i++) {
sharer* s = new sharer(i, OPT(share_intv), OPT(share_lits), OPT(DPS));
for (int j = 0; j < OPT(threads); j++) {
s->producers.push(workers[j]);
s->consumers.push(workers[j]);
workers[j]->in_sharer = s;
}
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, sharers[i]);
}
}
2022-09-08 13:54:29 +08:00
}