同步代码
This commit is contained in:
parent
78df039924
commit
cc370586ae
18
.vscode/settings.json
vendored
18
.vscode/settings.json
vendored
@ -37,6 +37,22 @@
|
|||||||
"type_traits": "cpp",
|
"type_traits": "cpp",
|
||||||
"tuple": "cpp",
|
"tuple": "cpp",
|
||||||
"typeinfo": "cpp",
|
"typeinfo": "cpp",
|
||||||
"utility": "cpp"
|
"utility": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"bit": "cpp",
|
||||||
|
"bitset": "cpp",
|
||||||
|
"set": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"regex": "cpp",
|
||||||
|
"fstream": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"cinttypes": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
2
crun
2
crun
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
clear
|
clear
|
||||||
|
|
||||||
make -j 16
|
make -j
|
||||||
|
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "compile failed."
|
echo "compile failed."
|
||||||
|
251
src/circuit.cpp
251
src/circuit.cpp
@ -5,67 +5,13 @@
|
|||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include "assert.h"
|
#include "assert.h"
|
||||||
|
|
||||||
void Circuit::init_stems() {
|
|
||||||
for(auto& gate: gates) {
|
|
||||||
if(gate->outputs.size() >= 2) {
|
|
||||||
gate->stem = true;
|
|
||||||
}
|
|
||||||
if(gate->stem) {
|
|
||||||
stems.push_back(gate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(Gate *g : gates) {
|
|
||||||
if(g->isPI) continue;
|
|
||||||
std::queue<Gate*> q;
|
|
||||||
std::unordered_map<Gate*, bool> used;
|
|
||||||
q.push(g);
|
|
||||||
|
|
||||||
while(!q.empty()) {
|
|
||||||
Gate* now = q.front();
|
|
||||||
q.pop();
|
|
||||||
for(Gate* in : now->inputs) {
|
|
||||||
if(in->stem) {
|
|
||||||
g->pre_stems.push_back(in);
|
|
||||||
} else if(!used[in]) {
|
|
||||||
used[in] = true;
|
|
||||||
q.push(in);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("pre: %s %d\n", g->name.c_str(), g->pre_stems.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
for(Gate *g : gates) {
|
|
||||||
if(g->isPO) continue;
|
|
||||||
std::queue<Gate*> q;
|
|
||||||
std::unordered_map<Gate*, bool> used;
|
|
||||||
q.push(g);
|
|
||||||
|
|
||||||
while(!q.empty()) {
|
|
||||||
Gate* now = q.front();
|
|
||||||
q.pop();
|
|
||||||
for(Gate* out : now->outputs) {
|
|
||||||
if(out->stem) {
|
|
||||||
g->suc_stems.push_back(out);
|
|
||||||
} else if(!used[out]) {
|
|
||||||
used[out] = true;
|
|
||||||
q.push(out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//printf("pre: %s %d\n", g->name.c_str(), g->pre_stems.size());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void Circuit::init_topo_index() {
|
void Circuit::init_topo_index() {
|
||||||
int topo = 1;
|
int topo = 1;
|
||||||
std::queue<Gate*> q;
|
std::queue<Gate*> q;
|
||||||
|
|
||||||
std::unordered_map<Gate*, int> ins;
|
std::unordered_map<Gate*, int> ins;
|
||||||
for(Gate* gate : gates) {
|
for(Gate* gate : gates) {
|
||||||
ins[gate] = gate->inputs.size();
|
ins[gate] = gate->fanins.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto in : PIs) {
|
for(auto in : PIs) {
|
||||||
@ -75,7 +21,7 @@ void Circuit::init_topo_index() {
|
|||||||
|
|
||||||
while(!q.empty()) {
|
while(!q.empty()) {
|
||||||
Gate* g = q.front(); q.pop();
|
Gate* g = q.front(); q.pop();
|
||||||
for(Gate* out : g->outputs) {
|
for(Gate* out : g->fanouts) {
|
||||||
ins[out]--;
|
ins[out]--;
|
||||||
if(ins[out] == 0) {
|
if(ins[out] == 0) {
|
||||||
out->id = topo++;
|
out->id = topo++;
|
||||||
@ -83,6 +29,27 @@ void Circuit::init_topo_index() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 计算反向拓扑序
|
||||||
|
topo = 1;
|
||||||
|
std::unordered_map<Gate*, int> outs;
|
||||||
|
|
||||||
|
for(auto out : POs) {
|
||||||
|
out->rtopo = topo++;
|
||||||
|
q.push(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
while(!q.empty()) {
|
||||||
|
Gate* g = q.front(); q.pop();
|
||||||
|
rtopo_gates.push_back(g);
|
||||||
|
for(Gate* in : g->fanins) {
|
||||||
|
outs[in]++;
|
||||||
|
if(outs[in] == in->fanouts.size()) {
|
||||||
|
in->rtopo = topo++;
|
||||||
|
q.push(in);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Circuit::init_gate_level() {
|
void Circuit::init_gate_level() {
|
||||||
@ -103,7 +70,7 @@ void Circuit::init_gate_level() {
|
|||||||
|
|
||||||
MAX_GATE_LEVEL = std::max(MAX_GATE_LEVEL, g->level);
|
MAX_GATE_LEVEL = std::max(MAX_GATE_LEVEL, g->level);
|
||||||
|
|
||||||
for(Gate* out : g->outputs) {
|
for(Gate* out : g->fanouts) {
|
||||||
if(out->level == -1) {
|
if(out->level == -1) {
|
||||||
out->level = g->level + 1;
|
out->level = g->level + 1;
|
||||||
q.push(out);
|
q.push(out);
|
||||||
@ -119,8 +86,8 @@ void Circuit::init_gate_level() {
|
|||||||
void Circuit::print_gates() {
|
void Circuit::print_gates() {
|
||||||
static const char* type2name[9] = {"AND", "NAND", "OR", "NOR", "XOR", "XNOR", "NOT", "BUF", "IN"};
|
static const char* type2name[9] = {"AND", "NAND", "OR", "NOR", "XOR", "XNOR", "NOT", "BUF", "IN"};
|
||||||
for(Gate* gate : gates) {
|
for(Gate* gate : gates) {
|
||||||
printf("Gate: %3s (t:%4s v:%d pi:%d po:%d s:%d p:%d s0:%d s1:%d fpl0:%d fpl1:%d) Inputs:", gate->name.c_str(), type2name[gate->type], gate->value, gate->isPI, gate->isPO, gate->stem, gate->is_propagated(), gate->sa[0], gate->sa[1], gate->fault_propagate_len[0], gate->fault_propagate_len[1]);
|
printf("Gate: %3s (t:%4s v:%d pi:%d po:%d s0:%d s1:%d fpl0:%d fpl1:%d) Inputs:", gate->name.c_str(), type2name[gate->type], gate->value, gate->isPI, gate->isPO, gate->fault_detected[0], gate->fault_detected[1], gate->fault_propagated_len[0], gate->fault_propagated_len[1]);
|
||||||
for(Gate* in : gate->inputs) {
|
for(Gate* in : gate->fanins) {
|
||||||
printf(" %s(%d)", in->name.c_str(), gate->is_detected(in));
|
printf(" %s(%d)", in->name.c_str(), gate->is_detected(in));
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
@ -129,113 +96,113 @@ void Circuit::print_gates() {
|
|||||||
|
|
||||||
bool Circuit::is_valid_circuit() {
|
bool Circuit::is_valid_circuit() {
|
||||||
|
|
||||||
ll flip_total_weight = 0;
|
// ll flip_total_weight = 0;
|
||||||
ll stem_total_weight = 0;
|
// ll stem_total_weight = 0;
|
||||||
ll fault_total_weight = 0;
|
// ll fault_total_weight = 0;
|
||||||
|
|
||||||
int flip_total_cnt = 0;
|
// int flip_total_cnt = 0;
|
||||||
int stem_total_cnt = 0;
|
// int stem_total_cnt = 0;
|
||||||
int fault_total_cnt = 0;
|
// int fault_total_cnt = 0;
|
||||||
|
|
||||||
ll fault_propagate_score = 0;
|
// ll fault_propagate_score = 0;
|
||||||
|
|
||||||
//printf("flip: %d, stem: %d, fault:%d\n", flip_total_weight, stem_total_weight, fault_total_weight);
|
// //printf("flip: %d, stem: %d, fault:%d\n", flip_total_weight, stem_total_weight, fault_total_weight);
|
||||||
|
|
||||||
for(Gate* g : gates) {
|
// for(Gate* g : gates) {
|
||||||
|
|
||||||
fault_propagate_score += g->fault_propagate_len[0] * fault_weight[g->id][0];
|
// fault_propagate_score += g->fault_propagate_len[0] * fault_weight[g->id][0];
|
||||||
fault_propagate_score += g->fault_propagate_len[1] * fault_weight[g->id][1];
|
// fault_propagate_score += g->fault_propagate_len[1] * fault_weight[g->id][1];
|
||||||
|
|
||||||
if(flip_need_update[g->id]) {
|
// if(flip_need_update[g->id]) {
|
||||||
flip_total_weight += flip_weight[g->id];
|
// flip_total_weight += flip_weight[g->id];
|
||||||
flip_total_cnt++;
|
// flip_total_cnt++;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if(g->stem && g->cal_value() != g->value) {
|
// if(g->stem && g->cal_value() != g->value) {
|
||||||
stem_total_weight += stem_weight[g->id];
|
// stem_total_weight += stem_weight[g->id];
|
||||||
}
|
// }
|
||||||
|
|
||||||
if(g->stem && g->cal_value() == g->value) {
|
// if(g->stem && g->cal_value() == g->value) {
|
||||||
stem_total_cnt++;
|
// stem_total_cnt++;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if(g->cal_propagate_len(0) != g->fault_propagate_len[0] || g->cal_propagate_len(1) != g->fault_propagate_len[1]) {
|
// if(g->cal_propagate_len(0) != g->fault_propagate_len[0] || g->cal_propagate_len(1) != g->fault_propagate_len[1]) {
|
||||||
printf("WRONG-PRO-LEN: %s \n", g->name.c_str());
|
// printf("WRONG-PRO-LEN: %s \n", g->name.c_str());
|
||||||
print_gates();
|
// print_gates();
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if(g->sa[0]) {
|
// if(g->sa[0]) {
|
||||||
fault_total_weight += fault_weight[g->id][0];
|
// fault_total_weight += fault_weight[g->id][0];
|
||||||
fault_total_cnt += 1;
|
// fault_total_cnt += 1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if(g->sa[1]) {
|
// if(g->sa[1]) {
|
||||||
fault_total_weight += fault_weight[g->id][1];
|
// fault_total_weight += fault_weight[g->id][1];
|
||||||
fault_total_cnt += 1;
|
// fault_total_cnt += 1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if(g->stem) {
|
// if(g->stem) {
|
||||||
assert(stem_satisfied[g->id] == (g->cal_value() == g->value));
|
// assert(stem_satisfied[g->id] == (g->cal_value() == g->value));
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 检查门的赋值情况
|
// // 检查门的赋值情况
|
||||||
if(g->cal_value() != g->value) {
|
// if(g->cal_value() != g->value) {
|
||||||
printf("WRONG-ASSGIN: %s \n", g->name.c_str());
|
// printf("WRONG-ASSGIN: %s \n", g->name.c_str());
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 检查 PO 的传播设定是否正确
|
// // 检查 PO 的传播设定是否正确
|
||||||
if(g->isPO) {
|
// if(g->isPO) {
|
||||||
if(g->sa[g->value] != 0 || g->sa[!g->value] == 0 ) {
|
// if(g->sa[g->value] != 0 || g->sa[!g->value] == 0 ) {
|
||||||
printf("WRONG-PO: %s \n", g->name.c_str());
|
// printf("WRONG-PO: %s \n", g->name.c_str());
|
||||||
}
|
// }
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 非 PO 情况下检查故障传播是否正确
|
// // 非 PO 情况下检查故障传播是否正确
|
||||||
|
|
||||||
bool sa0 = false;
|
// bool sa0 = false;
|
||||||
bool sa1 = false;
|
// bool sa1 = false;
|
||||||
|
|
||||||
for(Gate* out : g->outputs) {
|
// for(Gate* out : g->fan_outs) {
|
||||||
if(out->cal_value() != out->value) {
|
// if(out->cal_value() != out->value) {
|
||||||
assert(out->stem);
|
// assert(out->stem);
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
g->value = !g->value;
|
// g->value = !g->value;
|
||||||
|
|
||||||
if(out->cal_value() != out->value) {
|
// if(out->cal_value() != out->value) {
|
||||||
sa0 |= out->is_propagated() && !g->value;
|
// sa0 |= out->is_propagated() && !g->value;
|
||||||
sa1 |= out->is_propagated() && g->value;
|
// sa1 |= out->is_propagated() && g->value;
|
||||||
}
|
// }
|
||||||
|
|
||||||
g->value = !g->value;
|
// g->value = !g->value;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if(sa0 != g->sa[0] || sa1 != g->sa[1]) {
|
// if(sa0 != g->sa[0] || sa1 != g->sa[1]) {
|
||||||
printf("WRONG-SA: %s \n", g->name.c_str());
|
// printf("WRONG-SA: %s \n", g->name.c_str());
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
if(this->flip_total_weight != flip_total_weight || this->stem_total_weight != stem_total_weight || this->fault_total_weight != fault_total_weight) {
|
// if(this->flip_total_weight != flip_total_weight || this->stem_total_weight != stem_total_weight || this->fault_total_weight != fault_total_weight) {
|
||||||
printf("CIRCUIT CHECK FAILED!\n");
|
// printf("CIRCUIT CHECK FAILED!\n");
|
||||||
printf("[wrong] flip: %d, stem: %d, fault:%d\n", this->flip_total_weight, this->stem_total_weight, this->fault_total_weight);
|
// printf("[wrong] flip: %d, stem: %d, fault:%d\n", this->flip_total_weight, this->stem_total_weight, this->fault_total_weight);
|
||||||
printf("[right] flip: %d, stem: %d, fault:%d\n", flip_total_weight, stem_total_weight, fault_total_weight);
|
// printf("[right] flip: %d, stem: %d, fault:%d\n", flip_total_weight, stem_total_weight, fault_total_weight);
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if(this->flip_total_cnt != flip_total_cnt || this->stem_total_cnt != stem_total_cnt || this->fault_total_cnt != fault_total_cnt) {
|
// if(this->flip_total_cnt != flip_total_cnt || this->stem_total_cnt != stem_total_cnt || this->fault_total_cnt != fault_total_cnt) {
|
||||||
printf("CIRCUIT CHECK FAILED!\n");
|
// printf("CIRCUIT CHECK FAILED!\n");
|
||||||
printf("[wrong] flip_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", this->flip_total_cnt, this->stem_total_cnt, this->fault_total_cnt);
|
// printf("[wrong] flip_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", this->flip_total_cnt, this->stem_total_cnt, this->fault_total_cnt);
|
||||||
printf("[right] flip_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", flip_total_cnt, stem_total_cnt, fault_total_weight);
|
// printf("[right] flip_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", flip_total_cnt, stem_total_cnt, fault_total_weight);
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
printf("%lld %lld\n", fault_propagate_score , this->fault_propagate_score);
|
// printf("%lld %lld\n", fault_propagate_score , this->fault_propagate_score);
|
||||||
assert(fault_propagate_score == this->fault_propagate_score);
|
// assert(fault_propagate_score == this->fault_propagate_score);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
140
src/circuit.h
140
src/circuit.h
@ -10,29 +10,80 @@ using ll = long long;
|
|||||||
|
|
||||||
class Gate {
|
class Gate {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
// gate basic info
|
||||||
|
|
||||||
int id;
|
int id;
|
||||||
int level;
|
int level;
|
||||||
|
int rtopo;
|
||||||
std::string name;
|
std::string name;
|
||||||
enum Type { AND, NAND, OR, NOR, XOR, XNOR, NOT, BUF, INPUT } type;
|
enum Type { AND, NAND, OR, NOR, XOR, XNOR, NOT, BUF, INPUT } type;
|
||||||
int value;
|
int value;
|
||||||
bool sa[2];
|
|
||||||
bool stem;
|
|
||||||
bool isPI;
|
bool isPI;
|
||||||
bool isPO;
|
bool isPO;
|
||||||
|
std::vector<Gate*> fanouts;
|
||||||
|
std::vector<Gate*> fanins;
|
||||||
|
|
||||||
int fault_propagate_len[2];
|
// circuit-ls info
|
||||||
|
int value_satisfied;
|
||||||
|
int value_unsatisfied_cost;
|
||||||
|
|
||||||
std::vector<Gate*> pre_stems;
|
int fault_propagated_len[2];
|
||||||
std::vector<Gate*> suc_stems;
|
int fault_propagated_weight[2];
|
||||||
|
|
||||||
std::vector<Gate*> outputs;
|
int fault_propagated_satisfied[2];
|
||||||
std::vector<Gate*> inputs;
|
int fault_propagated_unsatisfied_cost[2];
|
||||||
|
|
||||||
|
int fault_detected[2];
|
||||||
|
int fault_detected_weight[2];
|
||||||
|
|
||||||
|
int fault_detected_satisfied[2];
|
||||||
|
int fault_detected_unsatisfied_cost[2];
|
||||||
|
|
||||||
|
int CC;
|
||||||
|
|
||||||
bool is_propagated();
|
bool is_propagated();
|
||||||
|
|
||||||
int cal_value();
|
int cal_value();
|
||||||
bool cal_sa(bool x);
|
|
||||||
|
bool cal_fault_detected(bool x);
|
||||||
|
|
||||||
bool is_detected(Gate* one_of_input);
|
bool is_detected(Gate* one_of_input);
|
||||||
|
|
||||||
int cal_propagate_len(bool x);
|
int cal_propagate_len(bool x);
|
||||||
|
|
||||||
|
// ( partical ) score
|
||||||
|
|
||||||
|
// score(x) = for y in neibor(x) { ~V_x,V_y make - ~V_x,V_y break }
|
||||||
|
// [ cal_FPL(~Vx, fanouts) - cal_FPL(Vx, fanouts) ] * FLP_WEIGHT(x)
|
||||||
|
// [ cal_FD(~Vx, fanouts) - cal_FD(Vx, fanouts) ] * FD_WEIGHT(x)
|
||||||
|
|
||||||
|
// [ cal_FPLS(~Vx, fanouts) - cal_FPLS(Vx, fanouts) ] * - FPLS_COST(x)
|
||||||
|
// [ cal_FDS(~Vx, fanouts) - cal_FDS(Vx, fanouts) ] * - FDS_COST(x)
|
||||||
|
|
||||||
|
int score;
|
||||||
|
|
||||||
|
int score_value_unsatisfied_cost;
|
||||||
|
|
||||||
|
int score_fault_propagated_weight[2];
|
||||||
|
int score_fault_propagated_unsatisfied_cost[2];
|
||||||
|
|
||||||
|
int score_fault_detected_weight[2];
|
||||||
|
int score_fault_detected_unsatisfied_cost[2];
|
||||||
|
|
||||||
|
// score calculation function
|
||||||
|
|
||||||
|
int cal_score();
|
||||||
|
|
||||||
|
int cal_value_unsatisfied_cost();
|
||||||
|
|
||||||
|
int cal_score_fault_propagated_weight(int sa);
|
||||||
|
|
||||||
|
int cal_score_fault_propagated_unsatisfied_cost(int sa);
|
||||||
|
|
||||||
|
int cal_score_fault_detected_weight(int sa);
|
||||||
|
|
||||||
|
int cal_score_fault_detected_unsatisfied_cost(int sa);
|
||||||
};
|
};
|
||||||
|
|
||||||
class Fault {
|
class Fault {
|
||||||
@ -47,84 +98,31 @@ public:
|
|||||||
std::vector<Gate*> PIs;
|
std::vector<Gate*> PIs;
|
||||||
std::vector<Gate*> POs;
|
std::vector<Gate*> POs;
|
||||||
std::vector<Gate*> gates;
|
std::vector<Gate*> gates;
|
||||||
std::vector<Gate*> stems; // PI + stems
|
std::vector<Gate*> rtopo_gates;
|
||||||
|
|
||||||
|
|
||||||
std::unordered_map<std::string, Gate*> name2gate;
|
std::unordered_map<std::string, Gate*> name2gate;
|
||||||
|
|
||||||
std::queue<Gate*> tmp;
|
// std::queue<Gate*> tmp;
|
||||||
std::unordered_map<Gate*, bool> tmp_used;
|
// std::unordered_map<Gate*, bool> tmp_used;
|
||||||
|
|
||||||
|
void init_topo_index();
|
||||||
|
int MAX_GATE_LEVEL;
|
||||||
|
void init_gate_level();
|
||||||
|
|
||||||
void parse_from_file(const char *filename);
|
void parse_from_file(const char *filename);
|
||||||
void print_gates();
|
void print_gates();
|
||||||
|
|
||||||
bool is_valid_circuit();
|
bool is_valid_circuit();
|
||||||
|
|
||||||
void init_topo_index();
|
|
||||||
|
|
||||||
int MAX_GATE_LEVEL;
|
|
||||||
void init_gate_level();
|
|
||||||
void init_stems();
|
|
||||||
|
|
||||||
// local search
|
// local search
|
||||||
|
|
||||||
bool local_search(std::unordered_set<Fault*> &faults);
|
bool local_search(std::unordered_set<Fault*> &faults);
|
||||||
|
|
||||||
// incremental flip struct
|
void ls_init_circuit(std::unordered_set<Fault*> &faults);
|
||||||
|
|
||||||
const double SP = 0.01;
|
|
||||||
|
|
||||||
const int FLIP_INC = 1;
|
|
||||||
const int FLIP_WEIGHT_MAX = 1e9;
|
|
||||||
|
|
||||||
int* CC;
|
|
||||||
|
|
||||||
ll flip_total_weight;
|
|
||||||
int flip_total_cnt;
|
|
||||||
int* flip_weight;
|
|
||||||
int* flip_need_update;
|
|
||||||
std::vector<Gate*> flip_update_queue;
|
|
||||||
|
|
||||||
// incremental stem struct
|
|
||||||
int STEM_INC = 0;
|
|
||||||
const int STEM_WEIGHT_MAX = 1e9;
|
|
||||||
ll stem_total_weight;
|
|
||||||
int stem_total_cnt;
|
|
||||||
int* stem_weight;
|
|
||||||
int* stem_satisfied;
|
|
||||||
|
|
||||||
int fault_propagate_tatal_len;
|
|
||||||
ll fault_propagate_score;
|
|
||||||
|
|
||||||
const int FAULT_INC = 1;
|
|
||||||
const int FAULT_WEIGHT_MAX = 20;
|
|
||||||
ll fault_total_weight;
|
|
||||||
int fault_total_cnt;
|
|
||||||
int** fault_weight;
|
|
||||||
int** fault_detected;
|
|
||||||
|
|
||||||
void ls_init_circuit();
|
|
||||||
void ls_init_weight(const std::unordered_set<Fault*> &faults);
|
|
||||||
void ls_update_weight();
|
void ls_update_weight();
|
||||||
void ls_init_data_structs();
|
|
||||||
|
|
||||||
void ls_block_recal(Gate* stem);
|
|
||||||
|
|
||||||
|
Gate* ls_pick();
|
||||||
void ls_flip(Gate* stem);
|
void ls_flip(Gate* stem);
|
||||||
void ls_update(Gate* stem);
|
void ls_update(Gate* stem);
|
||||||
ll ls_pick_score(Gate* stem);
|
|
||||||
|
|
||||||
ll ls_score();
|
|
||||||
|
|
||||||
int** simulate();
|
|
||||||
|
|
||||||
// time status
|
|
||||||
|
|
||||||
int flip_cnt = 0;
|
|
||||||
double flip_time = 0;
|
|
||||||
|
|
||||||
int update_cnt = 0;
|
|
||||||
double update_time = 0;
|
|
||||||
|
|
||||||
std::unordered_set<Gate*> stem_unsatisfied_set;
|
|
||||||
|
|
||||||
};
|
};
|
50
src/gate.cpp
50
src/gate.cpp
@ -7,9 +7,9 @@ int Gate::cal_propagate_len(bool x) {
|
|||||||
int fpl[2];
|
int fpl[2];
|
||||||
fpl[0] = fpl[1] = 0;
|
fpl[0] = fpl[1] = 0;
|
||||||
|
|
||||||
for(Gate* out : outputs) {
|
for(Gate* out : fanouts) {
|
||||||
if(!out->is_detected(this)) continue;
|
if(!out->is_detected(this)) continue;
|
||||||
fpl[!value] = std::max(fpl[!value], out->fault_propagate_len[!out->value] + 1);
|
fpl[!value] = std::max(fpl[!value], out->fault_propagated_len[!out->value] + 1);
|
||||||
}
|
}
|
||||||
return fpl[x];
|
return fpl[x];
|
||||||
}
|
}
|
||||||
@ -22,10 +22,10 @@ bool Gate::is_detected(Gate* one_of_input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Gate::is_propagated() {
|
bool Gate::is_propagated() {
|
||||||
return sa[0] || sa[1];
|
return fault_detected[0] || fault_detected[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Gate::cal_sa(bool x) {
|
bool Gate::cal_fault_detected(bool x) {
|
||||||
if(isPO) {
|
if(isPO) {
|
||||||
if(x == 0) return value;
|
if(x == 0) return value;
|
||||||
else return !value;
|
else return !value;
|
||||||
@ -34,7 +34,7 @@ bool Gate::cal_sa(bool x) {
|
|||||||
bool sa0 = 0;
|
bool sa0 = 0;
|
||||||
bool sa1 = 0;
|
bool sa1 = 0;
|
||||||
|
|
||||||
for(Gate* out : outputs) {
|
for(Gate* out : fanouts) {
|
||||||
if(!out->is_propagated()) continue;
|
if(!out->is_propagated()) continue;
|
||||||
|
|
||||||
if(out->cal_value() != out->value) continue;
|
if(out->cal_value() != out->value) continue;
|
||||||
@ -56,47 +56,47 @@ int Gate::cal_value() {
|
|||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case NOT:
|
case NOT:
|
||||||
res = !inputs[0]->value;
|
res = !fanins[0]->value;
|
||||||
break;
|
break;
|
||||||
case BUF:
|
case BUF:
|
||||||
res = inputs[0]->value;
|
res = fanins[0]->value;
|
||||||
break;
|
break;
|
||||||
case AND:
|
case AND:
|
||||||
res = inputs[0]->value;
|
res = fanins[0]->value;
|
||||||
for(int i=1; i<inputs.size(); i++) {
|
for(int i=1; i<fanins.size(); i++) {
|
||||||
res &= inputs[i]->value;
|
res &= fanins[i]->value;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case NAND:
|
case NAND:
|
||||||
res = inputs[0]->value;
|
res = fanins[0]->value;
|
||||||
for(int i=1; i<inputs.size(); i++) {
|
for(int i=1; i<fanins.size(); i++) {
|
||||||
res &= inputs[i]->value;
|
res &= fanins[i]->value;
|
||||||
}
|
}
|
||||||
res = !res;
|
res = !res;
|
||||||
break;
|
break;
|
||||||
case OR:
|
case OR:
|
||||||
res = inputs[0]->value;
|
res = fanins[0]->value;
|
||||||
for(int i=1; i<inputs.size(); i++) {
|
for(int i=1; i<fanins.size(); i++) {
|
||||||
res |= inputs[i]->value;
|
res |= fanins[i]->value;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case NOR:
|
case NOR:
|
||||||
res = inputs[0]->value;
|
res = fanins[0]->value;
|
||||||
for(int i=1; i<inputs.size(); i++) {
|
for(int i=1; i<fanins.size(); i++) {
|
||||||
res |= inputs[i]->value;
|
res |= fanins[i]->value;
|
||||||
}
|
}
|
||||||
res = !res;
|
res = !res;
|
||||||
break;
|
break;
|
||||||
case XOR:
|
case XOR:
|
||||||
res = inputs[0]->value;
|
res = fanins[0]->value;
|
||||||
for(int i=1; i<inputs.size(); i++) {
|
for(int i=1; i<fanins.size(); i++) {
|
||||||
res ^= inputs[i]->value;
|
res ^= fanins[i]->value;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case XNOR:
|
case XNOR:
|
||||||
res = inputs[0]->value;
|
res = fanins[0]->value;
|
||||||
for(int i=1; i<inputs.size(); i++) {
|
for(int i=1; i<fanins.size(); i++) {
|
||||||
res ^= inputs[i]->value;
|
res ^= fanins[i]->value;
|
||||||
}
|
}
|
||||||
res = !res;
|
res = !res;
|
||||||
break;
|
break;
|
||||||
|
545
src/ls.cpp
545
src/ls.cpp
@ -10,113 +10,19 @@
|
|||||||
|
|
||||||
bool Circuit::local_search(std::unordered_set<Fault*> &faults) {
|
bool Circuit::local_search(std::unordered_set<Fault*> &faults) {
|
||||||
|
|
||||||
// 初始化并重置所有 ls 数据结构
|
|
||||||
ls_init_data_structs();
|
|
||||||
|
|
||||||
// 赋值初始权重
|
|
||||||
ls_init_weight(faults);
|
|
||||||
|
|
||||||
// 随机生成初始电路
|
// 随机生成初始电路
|
||||||
ls_init_circuit();
|
ls_init_circuit(faults);
|
||||||
|
|
||||||
|
printf("generate random circuit done!\n");
|
||||||
|
exit(0);
|
||||||
|
|
||||||
//printf("local search!\n");
|
//printf("local search!\n");
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
|
Gate* gate = ls_pick();
|
||||||
// int ustem = 0;
|
if(gate == nullptr) {
|
||||||
// for(Gate* stem : stems) {
|
|
||||||
// ustem += !stem_satisfied[stem->id];
|
|
||||||
// }
|
|
||||||
// printf("ustem: %d %d\n", ustem, stem_unsatisfied_set.size());
|
|
||||||
// assert(ustem == stem_unsatisfied_set.size());
|
|
||||||
|
|
||||||
auto start = std::chrono::system_clock::now();
|
|
||||||
|
|
||||||
Gate* stem = nullptr;
|
|
||||||
ll max_score = 0;
|
|
||||||
|
|
||||||
const int T = 20;
|
|
||||||
|
|
||||||
for(int i=0; i<T; i++) {
|
|
||||||
Gate* t_stem = stems[rand()%stems.size()];
|
|
||||||
if(!CC[t_stem->id]) continue;
|
|
||||||
|
|
||||||
ll t_score = ls_pick_score(t_stem);
|
|
||||||
if(t_score > max_score) {
|
|
||||||
max_score = t_score;
|
|
||||||
stem = t_stem;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(max_score > 0) {
|
|
||||||
// printf("FLIP: %s (+%lld)\n", stem->name.c_str(), max_score);
|
|
||||||
// printf("[LS] flip: %lld, stem: %lld, fault:%lld. flip_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", flip_total_weight, stem_total_weight, fault_total_weight, flip_total_cnt, stem_total_cnt, fault_total_cnt);
|
|
||||||
ls_flip(stem);
|
|
||||||
|
|
||||||
CC[stem->id] = 0;
|
|
||||||
|
|
||||||
for(Gate* pre : stem->pre_stems) {
|
|
||||||
CC[pre->id] = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(Gate* suc : stem->suc_stems) {
|
|
||||||
CC[suc->id] = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto end = std::chrono::system_clock::now();
|
|
||||||
std::chrono::duration<double> elapsed_seconds = end - start;
|
|
||||||
flip_cnt++;
|
|
||||||
flip_time += elapsed_seconds.count();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
ls_update_weight();
|
|
||||||
|
|
||||||
if(stem_total_cnt == stems.size()) {
|
|
||||||
while(!flip_update_queue.empty()) {
|
|
||||||
Gate* g = flip_update_queue.back();
|
|
||||||
flip_update_queue.pop_back();
|
|
||||||
if(!flip_need_update[g->id]) continue;
|
|
||||||
flip_need_update[g->id] = false;
|
|
||||||
flip_total_weight -= flip_weight[g->id];
|
|
||||||
flip_total_cnt -= 1;
|
|
||||||
ls_update(g);
|
|
||||||
}
|
|
||||||
//printf("FIND SOLUTION!\n");
|
|
||||||
printf("[SOL] flip: %lld, stem: %lld, fault:%lld. flip_cnt: %d, stem_cnt: %d, fault_cnt:%d\n", flip_total_weight, stem_total_weight, fault_total_weight, flip_total_cnt, stem_total_cnt, fault_total_cnt);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Gate*> candidates;
|
|
||||||
for(Gate *g : stems) {
|
|
||||||
if(g->isPO) continue;
|
|
||||||
if(stem_satisfied[g->id]) continue;
|
|
||||||
candidates.push_back(g);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(candidates.size() == 0) {
|
|
||||||
candidates.push_back(stems[rand()%stems.size()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Gate* pick = candidates[rand()%candidates.size()];
|
|
||||||
|
|
||||||
ls_flip(pick);
|
|
||||||
|
|
||||||
CC[pick->id] = 0;
|
|
||||||
|
|
||||||
for(Gate* pre : pick->pre_stems) {
|
|
||||||
CC[pre->id] = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(Gate* suc : pick->suc_stems) {
|
|
||||||
CC[suc->id] = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto end = std::chrono::system_clock::now();
|
|
||||||
std::chrono::duration<double> elapsed_seconds = end - start;
|
|
||||||
update_cnt++;
|
|
||||||
update_time += elapsed_seconds.count();
|
|
||||||
//printf("[UP] flip: %lld, stem: %lld, fault:%lld. flip_cnt: %lld, stem_cnt: %lld, fault_cnt:%lld\n", flip_total_weight, stem_total_weight, fault_total_weight, flip_total_cnt, stem_total_cnt, fault_total_cnt);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int original_faults = -1;
|
static int original_faults = -1;
|
||||||
@ -128,7 +34,7 @@ bool Circuit::local_search(std::unordered_set<Fault*> &faults) {
|
|||||||
std::unordered_set<Fault*> tmp = faults;
|
std::unordered_set<Fault*> tmp = faults;
|
||||||
|
|
||||||
for(Fault* f : tmp) {
|
for(Fault* f : tmp) {
|
||||||
if(f->gate->sa[f->type]) {
|
if(f->gate->fault_detected[f->type]) {
|
||||||
faults.erase(f);
|
faults.erase(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -137,425 +43,66 @@ bool Circuit::local_search(std::unordered_set<Fault*> &faults) {
|
|||||||
|
|
||||||
printf("coverage: %.3f%%\tpattern: %d\tbefore: %d\tnow: %d\n", (double)(original_faults - faults.size()) / (original_faults) * 100, ++pattern, tmp.size(), faults.size());
|
printf("coverage: %.3f%%\tpattern: %d\tbefore: %d\tnow: %d\n", (double)(original_faults - faults.size()) / (original_faults) * 100, ++pattern, tmp.size(), faults.size());
|
||||||
|
|
||||||
printf("flip-cnt: %d flip-time: %.3fs update-cnt: %d update-time: %.3fs\n", flip_cnt, flip_time, update_cnt, update_time);
|
if(tmp.size() == faults.size()) return false;
|
||||||
printf("time-per-flip: %.2fms time-per-update: %.2fms\n", flip_time / flip_cnt * 1000, update_time / update_cnt * 1000);
|
|
||||||
|
|
||||||
//if(tmp.size() == faults.size()) return false;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Gate* Circuit::ls_pick() {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Circuit::ls_flip(Gate* gate) {
|
||||||
|
|
||||||
|
gate->value ^= 1;
|
||||||
|
|
||||||
|
for(Gate* g : gate->fanouts) {
|
||||||
|
g->value_satisfied = ( g->cal_value() == g->value );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void Circuit::ls_update_weight() {
|
void Circuit::ls_update_weight() {
|
||||||
|
|
||||||
STEM_INC += 1;
|
|
||||||
|
|
||||||
if(rand() % 10000 <= SP * 10000) {
|
|
||||||
for(Gate* g : gates) {
|
|
||||||
if(g->stem && stem_satisfied[g->id] && (stem_weight[g->id] - STEM_INC >= 1)) {
|
|
||||||
stem_weight[g->id] -= STEM_INC;
|
|
||||||
for(Gate* suc : g->suc_stems) {
|
|
||||||
if(stem_weight[suc->id] + STEM_INC <= STEM_WEIGHT_MAX) {
|
|
||||||
stem_weight[suc->id] += STEM_INC;
|
|
||||||
if(!stem_satisfied[suc->id]) {
|
|
||||||
stem_total_weight += STEM_INC;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for(Gate* g : gates) {
|
|
||||||
if(flip_need_update[g->id] && (flip_weight[g->id] + FLIP_INC < FLIP_WEIGHT_MAX)) {
|
|
||||||
flip_weight[g->id] += FLIP_INC;
|
|
||||||
flip_total_weight += FLIP_INC;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(g->stem && !stem_satisfied[g->id] && (stem_weight[g->id] + STEM_INC < STEM_WEIGHT_MAX)) {
|
|
||||||
stem_weight[g->id] += STEM_INC;
|
|
||||||
stem_total_weight += STEM_INC;
|
|
||||||
|
|
||||||
for(Gate* suc : g->suc_stems) {
|
|
||||||
if(stem_weight[suc->id] - STEM_INC > 1) {
|
|
||||||
stem_weight[suc->id] -= STEM_INC;
|
|
||||||
if(!stem_satisfied[suc->id]) {
|
|
||||||
stem_total_weight -= STEM_INC;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!g->sa[0] && fault_weight[g->id][0] > 0 && (fault_weight[g->id][0] + FAULT_INC < FAULT_WEIGHT_MAX)) {
|
|
||||||
fault_weight[g->id][0] += FAULT_INC;
|
|
||||||
fault_propagate_score += FAULT_INC * (g->fault_propagate_len[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!g->sa[1] && fault_weight[g->id][1] > 0 && (fault_weight[g->id][1] + FAULT_INC < FAULT_WEIGHT_MAX)) {
|
|
||||||
fault_weight[g->id][1] += FAULT_INC;
|
|
||||||
fault_propagate_score += FAULT_INC * (g->fault_propagate_len[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool cmp(Gate* a, Gate *b) {
|
|
||||||
return a->id > b->id;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Circuit::ls_flip(Gate* stem) {
|
|
||||||
stem->value = !stem->value;
|
|
||||||
ls_block_recal(stem);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Circuit::ls_update(Gate* stem) {
|
void Circuit::ls_update(Gate* stem) {
|
||||||
ls_block_recal(stem);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ll Circuit::ls_pick_score(Gate* stem) {
|
void Circuit::ls_init_circuit(std::unordered_set<Fault*> &faults) {
|
||||||
|
|
||||||
ll old_score = ls_score();
|
// init value, weight and cost
|
||||||
|
for(Gate* g : gates) {
|
||||||
ls_flip(stem);
|
g->value = rand() % 2;
|
||||||
|
g->value_unsatisfied_cost = 1;
|
||||||
ll new_score = ls_score();
|
g->fault_propagated_weight[0] = g->fault_propagated_weight[1] = 0;
|
||||||
|
g->fault_propagated_unsatisfied_cost[0] = g->fault_propagated_unsatisfied_cost[1] = 1;
|
||||||
ls_flip(stem);
|
g->fault_detected_weight[0] = g->fault_detected_weight[1] = 0;
|
||||||
|
g->fault_detected_unsatisfied_cost[0] = g->fault_detected_unsatisfied_cost[1] = 1;
|
||||||
old_score = std::max(old_score, ls_score());
|
|
||||||
|
|
||||||
return new_score - old_score;
|
|
||||||
}
|
|
||||||
|
|
||||||
ll Circuit::ls_score() {
|
|
||||||
//ll score = - flip_total_weight - stem_total_weight + fault_total_weight + fault_propagate_tatal_len;
|
|
||||||
ll score = - flip_total_weight - stem_total_weight + fault_propagate_score + fault_total_weight;
|
|
||||||
return score;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Circuit::ls_init_weight(const std::unordered_set<Fault*> &faults) {
|
|
||||||
for(Gate* s : stems) {
|
|
||||||
stem_weight[s->id] = 1;
|
|
||||||
stem_total_weight += stem_weight[s->id];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(Fault* f : faults) {
|
for(Fault* f : faults) {
|
||||||
fault_weight[f->gate->id][f->type] = 1;
|
f->gate->fault_propagated_weight[f->type] = 1;
|
||||||
|
f->gate->fault_detected_weight[f->type] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// int r = rand() % faults.size();
|
for(Gate* g : rtopo_gates) {
|
||||||
// auto it = faults.begin();
|
g->value_satisfied = ( g->cal_value() == g->value );
|
||||||
// std::advance(it, r);
|
g->fault_propagated_len[0] = g->cal_propagate_len(0);
|
||||||
|
g->fault_propagated_len[1] = g->cal_propagate_len(1);
|
||||||
// fault_weight[(*it)->gate->id][(*it)->type] = 1000000;
|
g->fault_propagated_satisfied[0] = g->fault_propagated_satisfied[1] = 0;
|
||||||
|
g->fault_detected_satisfied[0] = g->fault_detected_satisfied[1] = 0;
|
||||||
for(Gate* s: stems) {
|
|
||||||
flip_weight[s->id] = 1;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void Circuit::ls_init_circuit() {
|
|
||||||
|
|
||||||
|
// cal score
|
||||||
for(Gate* g : gates) {
|
for(Gate* g : gates) {
|
||||||
g->sa[0] = 0;
|
g->score = g->cal_score();
|
||||||
g->sa[1] = 0;
|
printf("%d ", g->score);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(Gate* s : stems) {
|
|
||||||
s->value = rand() % 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int i=stems.size()-1; i>=0; i--) {
|
|
||||||
ls_update(stems[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
while(!flip_update_queue.empty()) {
|
|
||||||
Gate* g = flip_update_queue.back();
|
|
||||||
flip_update_queue.pop_back();
|
|
||||||
if(!flip_need_update[g->id]) continue;
|
|
||||||
flip_need_update[g->id] = false;
|
|
||||||
flip_total_weight -= flip_weight[g->id];
|
|
||||||
flip_total_cnt -= 1;
|
|
||||||
ls_update(g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Circuit::ls_init_data_structs() {
|
|
||||||
const int MAX_LEN = gates.size() + 1;
|
|
||||||
|
|
||||||
if(flip_weight == nullptr) {
|
|
||||||
CC = new int[MAX_LEN];
|
|
||||||
|
|
||||||
flip_weight = new int[MAX_LEN];
|
|
||||||
flip_need_update = new int[MAX_LEN];
|
|
||||||
|
|
||||||
stem_weight = new int[MAX_LEN];
|
|
||||||
stem_satisfied = new int[MAX_LEN];
|
|
||||||
|
|
||||||
fault_weight = new int*[MAX_LEN];
|
|
||||||
for(int i=0; i<MAX_LEN; i++) {
|
|
||||||
fault_weight[i] = new int[2];
|
|
||||||
}
|
|
||||||
fault_detected = new int*[MAX_LEN];
|
|
||||||
for(int i=0; i<MAX_LEN; i++) {
|
|
||||||
fault_detected[i] = new int[2];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
STEM_INC = 1;
|
|
||||||
|
|
||||||
fault_propagate_score = 0;
|
|
||||||
|
|
||||||
fault_propagate_tatal_len = 0;
|
|
||||||
|
|
||||||
flip_total_weight = 0;
|
|
||||||
flip_total_cnt = 0;
|
|
||||||
|
|
||||||
stem_total_weight = 0;
|
|
||||||
stem_total_cnt = 0;
|
|
||||||
|
|
||||||
fault_total_weight = 0;
|
|
||||||
fault_total_cnt = 0;
|
|
||||||
|
|
||||||
for(int i=0; i<MAX_LEN; i++) {
|
|
||||||
CC[i] = 1;
|
|
||||||
flip_weight[i] = 0;
|
|
||||||
flip_need_update[i] = 0;
|
|
||||||
stem_weight[i] = 0;
|
|
||||||
stem_satisfied[i] = 0;
|
|
||||||
fault_weight[i][0] = 0;
|
|
||||||
fault_weight[i][1] = 0;
|
|
||||||
fault_detected[i][0] = 0;
|
|
||||||
fault_detected[i][1] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(Gate *g : gates) {
|
|
||||||
g->fault_propagate_len[0] = 0;
|
|
||||||
g->fault_propagate_len[1] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void Circuit::ls_block_recal(Gate* stem) {
|
|
||||||
if(flip_need_update[stem->id]) {
|
|
||||||
flip_need_update[stem->id] = false;
|
|
||||||
flip_total_weight -= flip_weight[stem->id];
|
|
||||||
flip_total_cnt -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(stem->cal_value() == stem->value && !stem_satisfied[stem->id]){
|
|
||||||
stem_unsatisfied_set.erase(stem);
|
|
||||||
|
|
||||||
stem_satisfied[stem->id] = true;
|
|
||||||
stem_total_weight -= stem_weight[stem->id];
|
|
||||||
stem_total_cnt += 1;
|
|
||||||
for(Gate* pre : stem->pre_stems) {
|
|
||||||
if(flip_need_update[pre->id]) continue;
|
|
||||||
|
|
||||||
flip_need_update[pre->id] = true;
|
|
||||||
flip_update_queue.push_back(pre);
|
|
||||||
|
|
||||||
flip_total_weight += flip_weight[pre->id];
|
|
||||||
flip_total_cnt += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(stem->cal_value() != stem->value && stem_satisfied[stem->id]) {
|
|
||||||
stem_unsatisfied_set.insert(stem);
|
|
||||||
|
|
||||||
stem_satisfied[stem->id] = false;
|
|
||||||
stem_total_weight += stem_weight[stem->id];
|
|
||||||
stem_total_cnt -= 1;
|
|
||||||
for(Gate* pre : stem->pre_stems) {
|
|
||||||
if(flip_need_update[pre->id]) continue;
|
|
||||||
|
|
||||||
flip_need_update[pre->id] = true;
|
|
||||||
flip_update_queue.push_back(pre);
|
|
||||||
|
|
||||||
flip_total_weight += flip_weight[pre->id];
|
|
||||||
flip_total_cnt += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(stem->isPO) {
|
|
||||||
if(stem->sa[!stem->value] == false) {
|
|
||||||
fault_total_weight += fault_weight[stem->id][!stem->value];
|
|
||||||
fault_total_cnt += 1;
|
|
||||||
stem->sa[!stem->value] = true;
|
|
||||||
|
|
||||||
for(Gate* pre : stem->pre_stems) {
|
|
||||||
if(flip_need_update[pre->id]) continue;
|
|
||||||
|
|
||||||
flip_need_update[pre->id] = true;
|
|
||||||
flip_update_queue.push_back(pre);
|
|
||||||
|
|
||||||
flip_total_weight += flip_weight[pre->id];
|
|
||||||
flip_total_cnt += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(stem->sa[stem->value] == true) {
|
|
||||||
fault_total_weight -= fault_weight[stem->id][stem->value];
|
|
||||||
fault_total_cnt -= 1;
|
|
||||||
stem->sa[stem->value] = false;
|
|
||||||
|
|
||||||
for(Gate* pre : stem->pre_stems) {
|
|
||||||
if(flip_need_update[pre->id]) continue;
|
|
||||||
|
|
||||||
flip_need_update[pre->id] = true;
|
|
||||||
flip_update_queue.push_back(pre);
|
|
||||||
|
|
||||||
flip_total_weight += flip_weight[pre->id];
|
|
||||||
flip_total_cnt += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::queue<Gate*> q;
|
|
||||||
static std::unordered_map<Gate*, int> used;
|
|
||||||
assert(q.empty());
|
|
||||||
used.clear();
|
|
||||||
|
|
||||||
q.push(stem);
|
|
||||||
|
|
||||||
while(!q.empty()) {
|
|
||||||
Gate* g = q.front();
|
|
||||||
q.pop();
|
|
||||||
used[g] = false;
|
|
||||||
for(Gate* out : g->outputs) {
|
|
||||||
if(out->stem) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
out->value = out->cal_value();
|
|
||||||
if(!used[out]) {
|
|
||||||
used[out] = true;
|
|
||||||
q.push(out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(q.empty());
|
|
||||||
used.clear();
|
|
||||||
|
|
||||||
for(Gate* stem : stem->suc_stems) {
|
|
||||||
q.push(stem);
|
|
||||||
|
|
||||||
int fpl0 = stem->cal_propagate_len(0);
|
|
||||||
int fpl1 = stem->cal_propagate_len(1);
|
|
||||||
|
|
||||||
if(fault_weight[stem->id][0]) {
|
|
||||||
fault_propagate_tatal_len += (fpl0 - stem->fault_propagate_len[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fault_weight[stem->id][1]) {
|
|
||||||
fault_propagate_tatal_len += (fpl1 - stem->fault_propagate_len[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
fault_propagate_score += (fault_weight[stem->id][0] * (fpl0 - stem->fault_propagate_len[0]));
|
|
||||||
fault_propagate_score += (fault_weight[stem->id][1] * (fpl1 - stem->fault_propagate_len[1]));
|
|
||||||
|
|
||||||
stem->fault_propagate_len[0] = fpl0;
|
|
||||||
stem->fault_propagate_len[1] = fpl1;
|
|
||||||
|
|
||||||
if(stem->cal_value() == stem->value && !stem_satisfied[stem->id]){
|
|
||||||
stem_unsatisfied_set.erase(stem);
|
|
||||||
stem_satisfied[stem->id] = true;
|
|
||||||
stem_total_weight -= stem_weight[stem->id];
|
|
||||||
stem_total_cnt += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(stem->cal_value() != stem->value && stem_satisfied[stem->id]) {
|
|
||||||
stem_unsatisfied_set.insert(stem);
|
|
||||||
stem_satisfied[stem->id] = false;
|
|
||||||
stem_total_weight += stem_weight[stem->id];
|
|
||||||
stem_total_cnt -= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while(!q.empty()) {
|
|
||||||
Gate *g = q.front();
|
|
||||||
q.pop();
|
|
||||||
|
|
||||||
used[g] = false;
|
|
||||||
|
|
||||||
for(Gate* in : g->inputs) {
|
|
||||||
|
|
||||||
bool old_sa[2];
|
|
||||||
old_sa[0] = in->sa[0];
|
|
||||||
old_sa[1] = in->sa[1];
|
|
||||||
|
|
||||||
in->sa[0] = in->cal_sa(0);
|
|
||||||
in->sa[1] = in->cal_sa(1);
|
|
||||||
|
|
||||||
if(in->stem && !in->isPI && (in->sa[0] != old_sa[0] || in->sa[1] != old_sa[1])) {
|
|
||||||
for(Gate* pre : in->pre_stems) {
|
|
||||||
if(flip_need_update[pre->id]) continue;
|
|
||||||
|
|
||||||
flip_need_update[pre->id] = true;
|
|
||||||
flip_update_queue.push_back(pre);
|
|
||||||
|
|
||||||
flip_total_weight += flip_weight[pre->id];
|
|
||||||
flip_total_cnt += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int fpl0 = in->cal_propagate_len(0);
|
|
||||||
int fpl1 = in->cal_propagate_len(1);
|
|
||||||
|
|
||||||
// if(in->name == "422") {
|
|
||||||
// printf("%s changed: %d fpl0: %d fpl1: %d \n", in->name.c_str(), (in->fault_propagate_len[0] != fpl0 || in->fault_propagate_len[1] != fpl1), fpl0, fpl1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
if(in->stem && !in->isPI && (in->fault_propagate_len[0] != fpl0 || in->fault_propagate_len[1] != fpl1)) {
|
|
||||||
|
|
||||||
for(Gate* pre : in->pre_stems) {
|
|
||||||
if(flip_need_update[pre->id]) continue;
|
|
||||||
|
|
||||||
flip_need_update[pre->id] = true;
|
|
||||||
flip_update_queue.push_back(pre);
|
|
||||||
|
|
||||||
flip_total_weight += flip_weight[pre->id];
|
|
||||||
flip_total_cnt += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fault_weight[in->id][0]) {
|
|
||||||
fault_propagate_tatal_len += fpl0 - in->fault_propagate_len[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fault_weight[in->id][1]) {
|
|
||||||
fault_propagate_tatal_len += fpl1 - in->fault_propagate_len[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
fault_propagate_score += fault_weight[in->id][0] * (fpl0 - in->fault_propagate_len[0]);
|
|
||||||
fault_propagate_score += fault_weight[in->id][1] * (fpl1 - in->fault_propagate_len[1]);
|
|
||||||
|
|
||||||
in->fault_propagate_len[0] = fpl0;
|
|
||||||
in->fault_propagate_len[1] = fpl1;
|
|
||||||
|
|
||||||
if(old_sa[0] != in->sa[0]) {
|
|
||||||
if(in->sa[0]) {
|
|
||||||
fault_total_weight += fault_weight[in->id][0];
|
|
||||||
fault_total_cnt += 1;
|
|
||||||
} else {
|
|
||||||
fault_total_weight -= fault_weight[in->id][0];
|
|
||||||
fault_total_cnt -= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(old_sa[1] != in->sa[1]) {
|
|
||||||
if(in->sa[1]) {
|
|
||||||
fault_total_weight += fault_weight[in->id][1];
|
|
||||||
fault_total_cnt += 1;
|
|
||||||
} else {
|
|
||||||
fault_total_weight -= fault_weight[in->id][1];
|
|
||||||
fault_total_cnt -= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!in->stem && !used[in]) {
|
|
||||||
used[in] = true;
|
|
||||||
q.push(in);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -17,7 +17,6 @@ int main(int args, char* argv[]) {
|
|||||||
|
|
||||||
printf("parsing file %s ...", argv[1]);
|
printf("parsing file %s ...", argv[1]);
|
||||||
circuit->parse_from_file(argv[1]);
|
circuit->parse_from_file(argv[1]);
|
||||||
circuit->init_stems();
|
|
||||||
circuit->init_topo_index();
|
circuit->init_topo_index();
|
||||||
circuit->init_gate_level();
|
circuit->init_gate_level();
|
||||||
printf(" Done.\n");
|
printf(" Done.\n");
|
||||||
@ -26,7 +25,6 @@ int main(int args, char* argv[]) {
|
|||||||
printf("PI:\t%ld\n", circuit->PIs.size());
|
printf("PI:\t%ld\n", circuit->PIs.size());
|
||||||
printf("PO:\t%ld\n", circuit->POs.size());
|
printf("PO:\t%ld\n", circuit->POs.size());
|
||||||
printf("Gate:\t%ld\n", circuit->name2gate.size());
|
printf("Gate:\t%ld\n", circuit->name2gate.size());
|
||||||
printf("Stem:\t%ld\n", circuit->stems.size());
|
|
||||||
printf("Level:\t%d\n", circuit->MAX_GATE_LEVEL);
|
printf("Level:\t%d\n", circuit->MAX_GATE_LEVEL);
|
||||||
printf("================================ \n");
|
printf("================================ \n");
|
||||||
|
|
||||||
|
7
src/options.h
Normal file
7
src/options.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
const double SP = 0.01;
|
||||||
|
|
||||||
|
const int STEM_INC = 0;
|
||||||
|
const int STEM_WEIGHT_MAX = 1e9;
|
||||||
|
|
||||||
|
const int FAULT_INC = 1;
|
||||||
|
const int FAULT_WEIGHT_MAX = 20;
|
@ -73,8 +73,6 @@ void Circuit::parse_from_file(const char *filename) {
|
|||||||
|
|
||||||
Gate* gate = new Gate();
|
Gate* gate = new Gate();
|
||||||
gate->name = tokens[0];
|
gate->name = tokens[0];
|
||||||
gate->sa[0] = gate->sa[1] = false;
|
|
||||||
gate->stem = false;
|
|
||||||
gate->isPI = false;
|
gate->isPI = false;
|
||||||
gate->isPO = false;
|
gate->isPO = false;
|
||||||
|
|
||||||
@ -87,8 +85,8 @@ void Circuit::parse_from_file(const char *filename) {
|
|||||||
|
|
||||||
auto in_gate = name2gate[in];
|
auto in_gate = name2gate[in];
|
||||||
|
|
||||||
gate->inputs.push_back(in_gate);
|
gate->fanins.push_back(in_gate);
|
||||||
in_gate->outputs.push_back(gate);
|
in_gate->fanouts.push_back(gate);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(tokens[2] == "AND") { gate->type = Gate::AND; }
|
if(tokens[2] == "AND") { gate->type = Gate::AND; }
|
||||||
@ -113,8 +111,6 @@ void Circuit::parse_from_file(const char *filename) {
|
|||||||
Gate* gate = new Gate();
|
Gate* gate = new Gate();
|
||||||
gate->name = tokens[2];
|
gate->name = tokens[2];
|
||||||
gate->type = Gate::INPUT;
|
gate->type = Gate::INPUT;
|
||||||
gate->sa[0] = gate->sa[1] = false;
|
|
||||||
gate->stem = true;
|
|
||||||
gate->isPI = true;
|
gate->isPI = true;
|
||||||
gate->isPO = false;
|
gate->isPO = false;
|
||||||
|
|
||||||
@ -142,7 +138,6 @@ void Circuit::parse_from_file(const char *filename) {
|
|||||||
|
|
||||||
Gate* po = name2gate[po_name];
|
Gate* po = name2gate[po_name];
|
||||||
po->isPO = true;
|
po->isPO = true;
|
||||||
po->stem = true;
|
|
||||||
POs.push_back(po);
|
POs.push_back(po);
|
||||||
}
|
}
|
||||||
}
|
}
|
129
src/score.cpp
Normal file
129
src/score.cpp
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
#include "circuit.h"
|
||||||
|
|
||||||
|
|
||||||
|
int Gate::cal_score() {
|
||||||
|
score_value_unsatisfied_cost = cal_value_unsatisfied_cost();
|
||||||
|
score_fault_propagated_weight[0] = cal_score_fault_propagated_weight(0);
|
||||||
|
score_fault_propagated_weight[1] = cal_score_fault_propagated_weight(1);
|
||||||
|
|
||||||
|
score_fault_propagated_unsatisfied_cost[0] = cal_score_fault_propagated_unsatisfied_cost(0);
|
||||||
|
score_fault_propagated_unsatisfied_cost[1] = cal_score_fault_propagated_unsatisfied_cost(1);
|
||||||
|
|
||||||
|
score_fault_detected_weight[0] = cal_score_fault_detected_weight(0);
|
||||||
|
score_fault_detected_weight[1] = cal_score_fault_detected_weight(1);
|
||||||
|
|
||||||
|
score_fault_detected_unsatisfied_cost[0] = cal_score_fault_detected_unsatisfied_cost(0);
|
||||||
|
score_fault_detected_unsatisfied_cost[1] = cal_score_fault_detected_unsatisfied_cost(1);
|
||||||
|
|
||||||
|
return - score_value_unsatisfied_cost
|
||||||
|
+ score_fault_propagated_weight[0] + score_fault_propagated_weight[1]
|
||||||
|
- score_fault_propagated_unsatisfied_cost[0] - score_fault_propagated_unsatisfied_cost[1]
|
||||||
|
+ score_fault_detected_weight[0] + score_fault_detected_weight[1]
|
||||||
|
- score_fault_detected_unsatisfied_cost[0] - score_fault_detected_unsatisfied_cost[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
int Gate::cal_value_unsatisfied_cost() {
|
||||||
|
int res = 0;
|
||||||
|
value ^= 1;
|
||||||
|
for(Gate* g : fanouts) {
|
||||||
|
if(g->value_satisfied && g->cal_value() != g->value) {
|
||||||
|
res += g->value_unsatisfied_cost;
|
||||||
|
}
|
||||||
|
if(!g->value_satisfied && g->cal_value() == g->value) {
|
||||||
|
res -= g->value_unsatisfied_cost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(this->value_satisfied) {
|
||||||
|
res += this->value_unsatisfied_cost;
|
||||||
|
} else {
|
||||||
|
res -= this->value_unsatisfied_cost;
|
||||||
|
}
|
||||||
|
value ^= 1;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Gate::cal_score_fault_propagated_weight(int sa) {
|
||||||
|
int res = 0;
|
||||||
|
value ^= 1;
|
||||||
|
|
||||||
|
int origin_fault_propagated_len = this->fault_propagated_len[sa];
|
||||||
|
this->fault_propagated_len[sa] = this->cal_propagate_len(sa);
|
||||||
|
|
||||||
|
res += (this->fault_propagated_len[sa] - origin_fault_propagated_len ) * this->fault_propagated_weight[sa];
|
||||||
|
for(Gate* g : fanins) {
|
||||||
|
res += ( g->cal_propagate_len(sa) - g->fault_propagated_len[sa] ) * g->fault_propagated_weight[sa];
|
||||||
|
}
|
||||||
|
|
||||||
|
this->fault_propagated_len[sa] = origin_fault_propagated_len;
|
||||||
|
|
||||||
|
value ^= 1;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Gate::cal_score_fault_propagated_unsatisfied_cost(int sa) {
|
||||||
|
int res = 0;
|
||||||
|
value ^= 1;
|
||||||
|
int origin_fault_propagated_len = this->fault_propagated_len[sa];
|
||||||
|
this->fault_propagated_len[sa] = this->cal_propagate_len(sa);
|
||||||
|
|
||||||
|
if(!this->fault_propagated_satisfied[sa]) {
|
||||||
|
res -= this->fault_propagated_unsatisfied_cost[res];
|
||||||
|
}
|
||||||
|
|
||||||
|
for(Gate* g : fanins) {
|
||||||
|
if(g->fault_propagated_satisfied[sa] && g->cal_propagate_len(sa) != g->fault_propagated_len[sa]) {
|
||||||
|
res += g->fault_propagated_unsatisfied_cost[sa];
|
||||||
|
}
|
||||||
|
if(!g->fault_propagated_satisfied[sa] && g->cal_propagate_len(sa) == g->fault_propagated_len[sa]) {
|
||||||
|
res -= g->fault_propagated_unsatisfied_cost[sa];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this->fault_propagated_len[sa] = origin_fault_propagated_len;
|
||||||
|
value ^= 1;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Gate::cal_score_fault_detected_weight(int sa) {
|
||||||
|
int res = 0;
|
||||||
|
value ^= 1;
|
||||||
|
|
||||||
|
int origin_fault_detected = this->fault_detected[sa];
|
||||||
|
this->fault_detected[sa] = this->cal_fault_detected(sa);
|
||||||
|
res += ( this->fault_detected[sa] - origin_fault_detected ) * this->fault_detected_weight[sa];
|
||||||
|
|
||||||
|
for(Gate* g : fanins) {
|
||||||
|
res += ( g->cal_fault_detected(sa) - g->fault_detected[sa] ) * this->fault_detected_weight[sa];
|
||||||
|
}
|
||||||
|
|
||||||
|
this->fault_detected[sa] = origin_fault_detected;
|
||||||
|
|
||||||
|
value ^= 1;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Gate::cal_score_fault_detected_unsatisfied_cost(int sa) {
|
||||||
|
int res = 0;
|
||||||
|
value ^= 1;
|
||||||
|
|
||||||
|
int origin_fault_detected = this->fault_detected[sa];
|
||||||
|
this->fault_detected[sa] = this->cal_fault_detected(sa);
|
||||||
|
|
||||||
|
if(!this->fault_detected_satisfied[sa]) {
|
||||||
|
res -= this->fault_detected_unsatisfied_cost[sa];
|
||||||
|
}
|
||||||
|
|
||||||
|
for(Gate* g : fanins) {
|
||||||
|
if(g->fault_detected_satisfied[sa] && g->cal_fault_detected(sa) != g->fault_detected[sa]) {
|
||||||
|
res += g->fault_detected_unsatisfied_cost[sa];
|
||||||
|
}
|
||||||
|
if(!g->fault_detected_satisfied[sa] && g->cal_fault_detected(sa) == g->fault_detected[sa]) {
|
||||||
|
res -= g->fault_detected_unsatisfied_cost[sa];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this->fault_detected[sa] = origin_fault_detected;
|
||||||
|
|
||||||
|
value ^= 1;
|
||||||
|
return res;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user