atpg-ls/circuit.h

123 lines
2.5 KiB
C
Raw Normal View History

2023-02-12 18:14:12 +08:00
#pragma once
#include <string>
#include <vector>
#include <unordered_map>
2023-02-23 11:00:24 +08:00
#include <unordered_set>
2023-02-19 19:42:50 +08:00
#include <queue>
2023-02-20 13:08:25 +08:00
using ll = long long;
class Gate {
public:
2023-02-27 02:49:59 +00:00
// gate structral info in circuit
2023-02-20 13:08:25 +08:00
int id;
std::string name;
2023-02-19 19:42:50 +08:00
enum Type { AND, NAND, OR, NOR, XOR, XNOR, NOT, BUF, INPUT } type;
bool stem;
2023-02-27 02:49:59 +00:00
bool pi;
bool po;
2023-02-19 19:42:50 +08:00
std::vector<Gate*> pre_stems;
2023-02-21 19:07:40 +08:00
std::vector<Gate*> suc_stems;
2023-02-19 19:42:50 +08:00
std::vector<Gate*> outputs;
std::vector<Gate*> inputs;
2023-02-19 19:42:50 +08:00
2023-02-27 02:49:59 +00:00
// Status Define (Two var)
bool value;
bool propagate;
// local search
bool stem_satisified;
int stem_cost;
2023-02-28 08:45:13 +00:00
// 和求得的值是否一致
2023-02-27 02:49:59 +00:00
bool propagate_satisfied;
int propagate_cost;
2023-02-28 08:45:13 +00:00
// 这个由 value 和 propagate 决定
2023-02-27 02:49:59 +00:00
int fault_satisfied[2];
int fault_cost[2];
// configuration checking
bool CC;
std::unordered_map<Gate*, std::pair<int, int>> sa_by_out;
2023-02-19 19:42:50 +08:00
int cal_value();
2023-02-28 08:45:13 +00:00
bool cal_sa(bool x);
//bool sa(bool x);
};
2023-02-16 18:51:31 +08:00
class Fault {
public:
Gate* gate;
enum Type { SA0, SA1 } type;
2023-02-20 13:08:25 +08:00
Fault(Gate* gate, Type type):gate(gate),type(type) {}
2023-02-16 18:51:31 +08:00
};
class Circuit {
public:
std::vector<Gate*> PIs;
std::vector<Gate*> POs;
2023-02-12 16:22:32 +08:00
std::vector<Gate*> gates;
2023-02-19 19:42:50 +08:00
std::vector<Gate*> stems; // PI + stems
2023-02-16 18:51:31 +08:00
std::unordered_map<std::string, Gate*> name2gate;
2023-02-19 19:42:50 +08:00
std::queue<Gate*> tmp;
std::unordered_map<Gate*, bool> tmp_used;
void parse_from_file(const char *filename);
2023-02-19 19:42:50 +08:00
void print_gates();
bool is_valid_circuit();
2023-02-16 18:51:31 +08:00
void init_topo_index();
void init_stems();
// local search
2023-02-19 19:42:50 +08:00
2023-02-27 02:49:59 +00:00
// shared info
2023-03-04 10:28:39 +00:00
static const int STEM_INC = 2;
2023-02-27 02:49:59 +00:00
static const int STEM_COST_MAX = 1e9;
static ll stem_total_cost;
static int stem_falsified_cnt;
2023-03-04 10:28:39 +00:00
static const int PROPAGATE_INC = 2;
2023-02-27 02:49:59 +00:00
static const int PROPAGATE_COST_MAX = 1e9;
static ll propagate_total_cost;
static int propagate_falsified_cnt;
static const int FAULT_INC = 1;
2023-03-04 10:39:33 +00:00
static const int FAULT_COST_MAX = 20;
2023-02-27 02:49:59 +00:00
static ll fault_total_cost;
static int fault_falsified_cnt;
2023-02-23 11:00:24 +08:00
bool local_search(std::unordered_set<Fault*> &faults);
2023-02-16 18:51:31 +08:00
2023-02-20 13:08:25 +08:00
// incremental flip struct
2023-02-21 19:07:40 +08:00
2023-02-23 11:00:24 +08:00
const double SP = 0.01;
2023-02-21 19:07:40 +08:00
2023-02-27 02:49:59 +00:00
void ls_init_circuit(const std::unordered_set<Fault*> &faults);
2023-02-21 19:07:40 +08:00
void ls_update_weight();
2023-02-20 13:08:25 +08:00
void ls_init_data_structs();
2023-02-12 18:14:12 +08:00
2023-02-20 14:41:19 +08:00
void ls_block_recal(Gate* stem);
2023-02-28 08:45:13 +00:00
void ls_block_recal_value(Gate* stem);
void ls_block_recal_fault(Gate* stem);
2023-02-20 14:41:19 +08:00
2023-02-28 08:45:13 +00:00
Gate* ls_pick_good_var(bool &value, bool &propagate);
Gate* ls_pick_falsified_var(bool &value, bool &propagate);
2023-02-27 02:49:59 +00:00
2023-02-20 14:41:19 +08:00
void ls_update(Gate* stem);
2023-02-28 08:45:13 +00:00
ll ls_pick_score(Gate* stem, bool value, bool propagate);
2023-02-21 19:07:40 +08:00
ll ls_score();
2023-03-04 10:28:39 +00:00
// simulator
int** simulate();
};