计算出错误传播的长度

This commit is contained in:
YuhangQ 2023-03-06 11:05:30 +00:00
parent c15186ad01
commit 6be4d80653
8 changed files with 3600 additions and 113285 deletions

BIN
atpg

Binary file not shown.

View File

@ -37,7 +37,6 @@ void Circuit::init_stems() {
//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;
@ -90,9 +89,9 @@ void Circuit::init_topo_index() {
void Circuit::print_gates() {
static const char* type2name[9] = {"AND", "NAND", "OR", "NOR", "XOR", "XNOR", "NOT", "BUF", "IN"};
for(Gate* gate : gates) {
printf("Gate: %3s (t:%4s v:%d pi:%d po:%d s:%d p:%d s0:%d s1:%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]);
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]);
for(Gate* in : gate->inputs) {
printf(" %s", in->name.c_str());
printf(" %s(%d)", in->name.c_str(), gate->is_detected(in));
}
printf("\n");
}
@ -125,6 +124,12 @@ bool Circuit::is_valid_circuit() {
stem_total_cnt++;
}
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());
print_gates();
return false;
}
if(g->sa[0]) {
fault_total_weight += fault_weight[g->id][0];
fault_total_cnt += 1;

View File

@ -19,6 +19,8 @@ public:
bool isPI;
bool isPO;
int fault_propagate_len[2];
std::vector<Gate*> pre_stems;
std::vector<Gate*> suc_stems;
@ -28,6 +30,8 @@ public:
bool is_propagated();
int cal_value();
bool cal_sa(bool x);
bool is_detected(Gate* one_of_input);
int cal_propagate_len(bool x);
};
class Fault {
@ -77,13 +81,15 @@ int* flip_need_update;
std::vector<Gate*> flip_update_queue;
// incremental stem struct
const int STEM_INC = 10;
const int STEM_INC = 20;
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;
const int FAULT_INC = 1;
const int FAULT_WEIGHT_MAX = 20;
ll fault_total_weight;
@ -103,4 +109,7 @@ void ls_update(Gate* stem);
ll ls_pick_score(Gate* stem);
ll ls_score();
int** simulate();
};

View File

@ -2,6 +2,28 @@
#include "assert.h"
int Gate::cal_propagate_len(bool x) {
int fpl[2];
fpl[0] = fpl[1] = 0;
for(Gate* out : outputs) {
if(!out->is_detected(this)) continue;
fpl[!value] = std::max(fpl[!value], out->fault_propagate_len[!out->value] + 1);
}
return fpl[x];
}
bool Gate::is_detected(Gate* one_of_input) {
one_of_input->value = !one_of_input->value;
bool detect = cal_value() != value;
one_of_input->value = !one_of_input->value;
return (cal_value() == value) && detect;
}
bool Gate::is_propagated() {
return sa[0] || sa[1];
}

69
ls.cpp
View File

@ -49,7 +49,7 @@ bool Circuit::local_search(std::unordered_set<Fault*> &faults) {
stem = t_stem;
}
if(t_score > 0) t++;
//if(i >= T) break;
if(i >= T) break;
}
if(max_score > 0) {
@ -177,30 +177,6 @@ void Circuit::ls_update_weight() {
}
}
for(int i=0; i<=1; i++) {
for(Gate* pre : g->suc_stems) {
// int inc = 0.2 * fault_weight[pre->id][i];
// inc = std::max(1, inc);
// if(g->type == Gate::NAND || g->type == Gate::NOR || g->type == Gate::NOT || g->type == Gate::XNOR) {
// if(fault_weight[g->id][!pre->value] + inc <= FAULT_WEIGHT_MAX) {
// fault_weight[g->id][!pre->value] += inc;
// if(g->sa[!pre->value]) fault_total_weight += inc;
// }
// } else {
// if(fault_weight[g->id][pre->value] + inc <= FAULT_WEIGHT_MAX) {
// fault_weight[g->id][pre->value] += inc;
// if(g->sa[pre->value]) fault_total_weight += inc;
// }
// }
// if(fault_weight[suc->id][1] + inc <= FAULT_WEIGHT_MAX) {
// fault_weight[suc->id][1] += inc;
// if(suc->sa[1]) fault_total_weight += 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;
}
@ -240,8 +216,8 @@ ll Circuit::ls_pick_score(Gate* stem) {
}
ll Circuit::ls_score() {
//ll score = -flip_total_weight -stem_total_weight + fault_total_weight;
ll score = -flip_total_weight -stem_total_weight + fault_total_weight;
//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_tatal_len;
return score;
}
@ -290,7 +266,6 @@ void Circuit::ls_init_data_structs() {
if(flip_weight == nullptr) {
CC = new int[MAX_LEN];
flip_weight = new int[MAX_LEN];
flip_need_update = new int[MAX_LEN];
@ -305,9 +280,10 @@ void Circuit::ls_init_data_structs() {
for(int i=0; i<MAX_LEN; i++) {
fault_detected[i] = new int[2];
}
}
fault_propagate_tatal_len = 0;
flip_total_weight = 0;
flip_total_cnt = 0;
@ -328,6 +304,11 @@ void Circuit::ls_init_data_structs() {
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;
}
}
@ -355,11 +336,11 @@ void Circuit::ls_block_recal(Gate* stem) {
//stem->value = !stem->value;
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;
@ -375,6 +356,7 @@ void Circuit::ls_block_recal(Gate* stem) {
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;
@ -445,7 +427,6 @@ void Circuit::ls_block_recal(Gate* stem) {
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;
@ -457,6 +438,32 @@ void Circuit::ls_block_recal(Gate* stem) {
}
}
int fpl0 = in->cal_propagate_len(0);
int fpl1 = in->cal_propagate_len(1);
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];
}
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];

View File

@ -44,6 +44,9 @@ int main(int args, char* argv[]) {
if(!ls) break;
if(!is_valid) break;
if(faults.size() == 0) break;
//circuit->print_gates();
//break;
}
//printf("[final] flip: %d, stem: %d, fault:%d\n", circuit->flip_total_weight, circuit->stem_total_weight, circuit->fault_total_weight);

116619
output.txt

File diff suppressed because it is too large Load Diff

150
simulator.cpp Normal file
View File

@ -0,0 +1,150 @@
#include "circuit.h"
#include <assert.h>
#include <unordered_map>
int cal_value(Gate *g, int *value) {
int res;
switch(g->type) {
case Gate::NOT:
res = !value[g->inputs[0]->id];
break;
case Gate::BUF:
res = value[g->inputs[0]->id];
break;
case Gate::AND:
res = value[g->inputs[0]->id];
for(int i=1; i<g->inputs.size(); i++) {
res &= value[g->inputs[i]->id];
}
break;
case Gate::NAND:
res = value[g->inputs[0]->id];
for(int i=1; i<g->inputs.size(); i++) {
res &= value[g->inputs[i]->id];
}
res = !res;
break;
case Gate::OR:
res = value[g->inputs[0]->id];
for(int i=1; i<g->inputs.size(); i++) {
res |= value[g->inputs[i]->id];
}
break;
case Gate::NOR:
res = value[g->inputs[0]->id];
for(int i=1; i<g->inputs.size(); i++) {
res |= value[g->inputs[i]->id];
}
res = !res;
break;
case Gate::XOR:
res = value[g->inputs[0]->id];
for(int i=1; i<g->inputs.size(); i++) {
res ^= value[g->inputs[i]->id];
}
break;
case Gate::XNOR:
res = value[g->inputs[0]->id];
for(int i=1; i<g->inputs.size(); i++) {
res ^= value[g->inputs[i]->id];
}
res = !res;
break;
case Gate::INPUT:
res = value[g->id];
break;
default:
assert(false);
break;
}
return res;
}
bool cal_sa(Gate* g, bool x, int** sa, int *value) {
if(g->isPO) {
if(x == 0) return value[g->id];
else return !value[g->id];
}
bool sa0 = 0;
bool sa1 = 0;
for(Gate* out : g->outputs) {
if(!sa[out->id][0] && !sa[out->id][1]) continue;
if(cal_value(out, value) != value[out->id]) continue;
value[g->id] = !value[g->id];
bool detect = cal_value(out, value) != value[out->id];
value[g->id] = !value[g->id];
if(!detect) continue;
sa0 |= value[g->id];
sa1 |= !value[g->id];
}
if(x == 0) return sa0;
else return sa1;
}
int** Circuit::simulate() {
static bool init = false;
static int** sa = nullptr;
static int* value = nullptr;
if(!init) {
const int MAXN = gates.size() + 1;
init = true;
sa = new int*[MAXN];
for(int i=0; i<MAXN; i++) {
sa[i] = new int[2];
}
value = new int[MAXN];
}
// init PI
for(Gate* pi : PIs) {
value[pi->id] = pi->value;
}
for(Gate *g : gates) {
if(g->isPI) continue;
value[g->id] = cal_value(g, value);
}
for(Gate *g : gates) {
assert(value[g->id] == cal_value(g, value));
}
std::queue<Gate*> q;
std::unordered_map<Gate*, int> topo;
// init PO
for(Gate* po : POs) {
sa[po->id][!value[po->id]] = 1;
sa[po->id][value[po->id]] = 0;
q.push(po);
}
while(!q.empty()) {
Gate* g = q.front();
q.pop();
for(Gate* in : g->inputs) {
if(++topo[in] == in->outputs.size()) {
sa[in->id][0] = cal_sa(in, 0, sa, value);
sa[in->id][1] = cal_sa(in, 1, sa, value);
q.push(in);
}
}
}
for(Gate* g : gates) {
assert(sa[g->id][0] == cal_sa(g, 0, sa, value));
assert(sa[g->id][1] == cal_sa(g, 1, sa, value));
}
return sa;
}