atpg-ls/circuit.h

118 lines
2.1 KiB
C
Raw Permalink Normal View History

2023-02-12 18:14:12 +08:00
#pragma once
2023-03-13 05:44:49 +00:00
#include "option.h"
#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-03-13 05:44:49 +00:00
2023-02-20 13:08:25 +08:00
using ll = long long;
class Gate {
public:
2023-03-13 05:44:49 +00:00
// 门的原始信息
2023-02-20 13:08:25 +08:00
int id;
2023-03-13 05:44:49 +00:00
int rtopo;
std::string name;
2023-02-19 19:42:50 +08:00
enum Type { AND, NAND, OR, NOR, XOR, XNOR, NOT, BUF, INPUT } type;
2023-02-12 18:14:12 +08:00
int value;
2023-03-13 05:44:49 +00:00
bool pi;
bool po;
std::vector<Gate*> fan_outs;
std::vector<Gate*> fan_ins;
2023-03-06 11:05:30 +00:00
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
2023-03-13 05:44:49 +00:00
// 记录全局已经发现的错误
bool global_fault_detected[2];
2023-02-19 19:42:50 +08:00
2023-03-13 05:44:49 +00:00
// atpg-ls 附加信息
int CC;
bool stem;
bool propagate;
2023-03-13 05:44:49 +00:00
int stem_weight;
bool stem_satisfied;
int fault_weight[2];
bool fault_detected[2];
int fault_propagate_length[2];
// 计算此门的信息
int recal_value();
void recal_fault(bool fd[2]);
void recal_propagate_len(int fpl[2]);
// 门的某个输入产生的错误是否可以通过这个门传播
bool is_detected(Gate* one_of_input);
2023-02-16 18:51:31 +08:00
};
class Circuit {
public:
2023-03-13 05:44:49 +00:00
// 电路的基本信息
std::vector<Gate*> PIs;
std::vector<Gate*> POs;
2023-02-12 16:22:32 +08:00
std::vector<Gate*> gates;
2023-03-13 05:44:49 +00:00
std::vector<Gate*> rtopo_gates;
std::vector<Gate*> stems; // PIs and POs are stems by default
2023-03-17 05:37:29 +00:00
std::unordered_map<int, Gate*> id2gate;
std::unordered_map<std::string, Gate*> name2gate;
2023-02-19 19:42:50 +08:00
2023-03-13 05:44:49 +00:00
// 读入和输出电路
void parse_from_file(const char *filename);
2023-03-13 05:44:49 +00:00
void print_circuit();
2023-02-19 19:42:50 +08:00
2023-03-13 05:44:49 +00:00
// 初始化电路统计信息
int MAX_GATE_LEVEL;
2023-02-16 18:51:31 +08:00
void init_topo_index();
2023-03-09 02:51:10 +00:00
2023-03-13 05:44:49 +00:00
// 电路状态 checker
bool is_valid_circuit();
2023-02-16 18:51:31 +08:00
// local search
2023-02-19 19:42:50 +08:00
2023-03-13 05:44:49 +00:00
bool local_search();
2023-02-21 19:07:40 +08:00
2023-03-13 05:44:49 +00:00
int global_fault_undetected_count;
2023-02-21 19:07:40 +08:00
2023-03-13 05:44:49 +00:00
ll stem_total_cost;
2023-02-21 19:07:40 +08:00
int stem_total_cnt;
2023-02-20 13:08:25 +08:00
2023-03-11 07:02:02 +00:00
ll fault_propagate_score;
2023-03-06 11:05:30 +00:00
2023-02-20 13:08:25 +08:00
ll fault_total_weight;
2023-02-21 19:07:40 +08:00
int fault_total_cnt;
2023-02-20 13:08:25 +08:00
2023-03-13 05:44:49 +00:00
void ls_reset_data();
void ls_init_stems();
void ls_init_weight();
2023-02-12 18:14:12 +08:00
2023-03-13 05:44:49 +00:00
void ls_random_circuit();
2023-02-20 14:41:19 +08:00
2023-03-13 05:44:49 +00:00
void ls_statistics();
2023-02-21 19:07:40 +08:00
2023-03-13 05:44:49 +00:00
void ls_update_weight();
2023-03-06 11:05:30 +00:00
2023-03-17 05:37:29 +00:00
int ls_pick();
2023-03-13 05:44:49 +00:00
Gate* ls_pick_falsified();
2023-03-06 11:05:30 +00:00
2023-03-17 05:37:29 +00:00
void ls_flip_var(int var);
void ls_flip_stem(Gate* stem);
2023-03-11 07:02:02 +00:00
2023-03-17 05:37:29 +00:00
ll ls_pick_score(int var);
2023-03-13 05:44:49 +00:00
ll ls_circuit_score();
2023-03-11 07:02:02 +00:00
int flip_cnt = 0;
double flip_time = 0;
int update_cnt = 0;
double update_time = 0;
};