Upload MAIN.CPP

This commit is contained in:
PKU-ZLR 2020-02-26 23:12:28 +08:00 committed by GitHub
parent 5f62f443bb
commit 64da250dd2
1 changed files with 229 additions and 175 deletions

View File

@ -24,6 +24,7 @@ int Luts = 6, Dep = 0;
vector<string> Top;
map<string, int> counter;
map<string, int> vis;
map<string, int> mark;
void get_file_name(string path, vector<string> &files) {
struct dirent *ptr;
@ -61,7 +62,63 @@ bool Dcmp_Depth2(DoubleCut A, DoubleCut B) {
return A.mindep < B.mindep || (A.mindep == B.mindep && A.Area < B.Area)
|| (A.mindep == B.mindep && A.Area == B.Area && A.AreaFlow < B.AreaFlow);
}
int DFS(Circuit &c, string now) {
int res = 1;
if (c.graph[now]->Rcut.size() == 0)
return 1;
mark[now] = 1;
for (string s : c.graph[now]->Rcut) {
if (counter[s] == 1 && mark[s] == 0)
res += DFS(c, s);
}
return res;
}
void Recovery(Circuit &c) {
queue<string> Q;
vis.clear();
for (string st : Top) {
counter[st] = 0;
}
int area = 0, dep = 0;
for (string s : c.output) {
Q.push(s);
vis[s] = 1;
dep = max(dep, c.graph[s]->mindep);
}
while (!Q.empty()) {
string now = Q.front();
Q.pop();
c.graph[now]->fin = counter[now];
if (c.graph[now]->pre.size() == 0)
continue;
area++;
for (string st : c.graph[now]->Fcut.names) {
counter[st] = counter[st] + 1;
if (!vis[st]) {
Q.push(st);
vis[st] = 1;
}
}
}
for (string now : Top) {
if (c.graph[now]->pre.size() == 0) {
c.graph[now]->Area = 1;
c.graph[now]->AreaFlow = 1.0 / counter[now];
continue;
}
if (vis[now] == 0) {
c.graph[now]->Area = 1;
c.graph[now]->AreaFlow = 1.0;
continue;
}
mark.clear();
c.graph[now]->Area = DFS(c, now);
c.graph[now]->AreaFlow = c.graph[now]->Area;
for (string st : c.graph[now]->Rcut)
c.graph[now]->AreaFlow += c.graph[st]->Area;
c.graph[now]->AreaFlow = c.graph[now]->AreaFlow / max(1.0, 1.0 * counter[now]);
}
}
pair<int, int> Output(Circuit &c) {
queue<string> Q;
vis.clear();
@ -111,10 +168,10 @@ pair<int, int> Output2(Circuit &c) {
//cout << now << endl;
if (c.graph[now]->pre.size() == 0)
continue;
if (c.graph[now]->Rcut[1].size() == Luts) {
if (c.graph[now]->Rcut.size() == Luts) {
fprintf(out2, "input:\n");
area++;
for (string st : c.graph[now]->Rcut[1]) {
for (string st : c.graph[now]->Rcut) {
fprintf(out2, "%s ", st.c_str());
if (!vis[st]) {
Q.insert(c.graph[st]->Fcut);
@ -136,7 +193,7 @@ pair<int, int> Output2(Circuit &c) {
int sucsize = tp.size();
for (int i = 2; i < Luts; i++) {
for (set<string> ct : c.graph[nxt]->cuts[i]) {
set<string> tmp = c.graph[now]->Rcut[1];
set<string> tmp = c.graph[now]->Rcut;
tmp.insert(ct.begin(), ct.end());
if (tmp.size() >= Luts)
continue;
@ -159,7 +216,7 @@ pair<int, int> Output2(Circuit &c) {
if (L.size() == 0) {
fprintf(out2, "input:\n");
area++;
for (string st : c.graph[now]->Rcut[1]) {
for (string st : c.graph[now]->Rcut) {
fprintf(out2, "%s ", st.c_str());
if (!vis[st]) {
Q.insert(c.graph[st]->Fcut);
@ -192,19 +249,6 @@ pair<int, int> Output2(Circuit &c) {
return make_pair(area, c.Dep);
}
int DFS(Circuit &c, string now) {
int res = 1;
//cout << now << endl;
if (c.graph[now]->Rcut[1].size() == 0)
return 1;
vis[now] = 1;
for (string s : c.graph[now]->Rcut[1]) {
if (counter[s] == 1 && vis[s] == 0)
res += DFS(c, s);
}
return res;
}
int main(int argc, char *argv[]) {
vector<string> benchmark;
queue<string> Q;
@ -222,7 +266,7 @@ int main(int argc, char *argv[]) {
fout << "case,PI,PO,size,area_single,depth_single,area_dual,depth_dual\n";
for (string str : benchmark) {
cout << str << endl;
//string str = "arbiter.blif"; {
//string str = "s38417.blif"; {
double total_time;
clock_t start = clock();
@ -238,8 +282,7 @@ int main(int argc, char *argv[]) {
for (pair<string, Var*> p : c.graph) {
ind[p.first] = p.second->pre.size();
p.second->mindep = 0;
for (int i = 0; i < 15; i++)
p.second->Rcut[i].clear();
p.second->Rcut.clear();
// p.second->Rdcut.clear();
}
@ -248,8 +291,9 @@ int main(int argc, char *argv[]) {
c.graph[s]->mindep = 0;
c.graph[s]->Area = 0;
c.graph[s]->AreaFlow = 0;
c.graph[s]->Rcut[0] = set<string> { s };
c.graph[s]->Fcut = Cut(set<string> { s }, s, 0, 0, 0);
c.graph[s]->fin = 0;
c.graph[s]->Rcut = set<string> { s };
c.graph[s]->Fcut = Cut(set<string> { s }, s, 0, 0, 0, 0);
Top.push_back(s);
for (string nt : c.graph[s]->suc) {
ind[nt]--;
@ -300,6 +344,7 @@ int main(int argc, char *argv[]) {
c.graph[now]->mindep = INF;
c.graph[now]->Area = INF;
c.graph[now]->AreaFlow = inf;
c.graph[now]->fin = 0;
for (int k = 1; k <= Luts; k++) {
for (set<string> ct : c.graph[now]->cuts[k]) {
int dep = 0, area = 0;
@ -312,8 +357,8 @@ int main(int argc, char *argv[]) {
dep++, area++;
AF /= max(1.0, 1.0 * c.graph[now]->suc.size());
Cuts[k].push_back(Cut(ct, now, dep, area, AF));
Ct.push_back(Cut(ct, now, dep, area, AF));
Cuts[k].push_back(Cut(ct, now, dep, area, 0, AF));
Ct.push_back(Cut(ct, now, dep, area, 0, AF));
}
}
for (int k = 1; k <= Luts; k++) {
@ -334,164 +379,173 @@ int main(int argc, char *argv[]) {
c.graph[now]->AreaFlow = Ct[0].AreaFlow;
c.graph[now]->Area = Ct[0].Area;
unsigned int Ctmp = Ct.size();
c.graph[now]->Rcut[0] = set<string> { now };
for (unsigned int i = 1; i <= min(C, Ctmp); i++)
{
if (Ct[i - 1].mindep > Ct[0].mindep) break;
c.graph[now]->Rcut[i] = Ct[i - 1].names;
//printf("%d ", Ct[i].names.size());
}
//printf("\n");
c.graph[now]->Rcut = Ct[0].names;
c.graph[now]->Fcut = Ct[0];
for (string s : Ct[0].names) {
counter[s]++;
}
//cout << c.graph[now]->mindep << endl;
}
/*for (string nw : Top) {
vis.clear();
c.graph[nw]->Area = DFS(c, nw);
for (int turn = 1; turn <= 10; turn++) {
Recovery(c);
for (string now : Top) {
//cout << now << " " << c.graph[now]->AreaFlow << " " << c.graph[now]->Area << endl;
if (c.graph[now]->pre.size() == 0)
continue;
for (int i = 0; i <= Luts; i++)
c.graph[now]->cuts[i].clear();
}
for (string now : Top) {
if (c.graph[now]->pre.size() == 0)
continue;
//c.graph[now]->cuts[1].insert(set<string> { now });
assert(c.graph[now]->pre.size() <= 2);
if (c.graph[now]->pre.size() == 2) {
string input1 = c.graph[now]->pre[0];
string input2 = c.graph[now]->pre[1];
for (int t = 1; t <= Luts; t++)
for (set<string> c1 : c.graph[input1]->cuts[t]) {
for (int k = 1; k <= Luts; k++)
for (set<string> c2 : c.graph[input2]->cuts[k]) {
set<string> st = c1;
st.insert(c2.begin(), c2.end());
if (st.size() > (unsigned) Luts)
continue;
c.graph[now]->cuts[st.size()].insert(st);
}
}
}
if (c.graph[now]->pre.size() == 1) {
string input1 = c.graph[now]->pre[0];
for (int k = 1; k <= Luts; k++)
for (set<string> c1 : c.graph[input1]->cuts[k])
c.graph[now]->cuts[k].insert(c1);
}
/*Calculate dep, areaflow, exact area*/
vector<Cut> Cuts[10], Ct;
for (int k = 1; k <= Luts; k++) {
for (set<string> ct : c.graph[now]->cuts[k]) {
int dep = 0, area = 0, fin = 0;
double AF = c.graph[now]->Area;
for (string nt : ct) {
dep = max(dep, c.graph[nt]->mindep);
AF += c.graph[nt]->AreaFlow;
fin += c.graph[nt]->fin;
}
dep++;
AF /= max(1.0, 1.0 * c.graph[now]->fin);
if (dep <= c.graph[now]->mindep) {
Cuts[k].push_back(Cut(ct, now, dep, area, fin, AF));
Ct.push_back(Cut(ct, now, dep, area, fin, AF));
}
}
}
for (int k = 1; k <= Luts; k++) {
sort(Cuts[k].begin(), Cuts[k].end(), cmp_AreaFlow);
c.graph[now]->cuts[k].clear();
unsigned int len = Cuts[k].size();
//cout << "now:" << now << endl;
for (unsigned int i = 0; i < min(C, len); i++) {
c.graph[now]->cuts[k].insert(Cuts[k][i].names);
//cout << Cuts[i].mindep << endl;
}
}
//if (c.graph[now]->pre.size() != 1)
c.graph[now]->cuts[1].insert(set<string> { now });
sort(Ct.begin(), Ct.end(), cmp_AreaFlow);
if (Ct.size() == 0) continue;
int Depth = Ct[0].mindep;
if (c.graph[now]->Area > Ct[0].Area) {
c.graph[now]->AreaFlow = Ct[0].AreaFlow;
unsigned int Ctmp = Ct.size();
c.graph[now]->Rcut = Ct[0].names;
c.graph[now]->Fcut = Ct[0];
}
}
for (string now : Top) {
vector<Cut> Ct;
if (c.graph[now]->pre.size() == 0) {
c.graph[now]->AreaFlow = 1.0 / counter[now];
continue;
}
if (c.graph[now]->pre.size() == 1) {
string input1 = c.graph[now]->pre[0];
for (int i = 0; i <= C; i++)
if (c.graph[input1]->Rcut[i].size() != 0)
Ct.push_back(Cut(c.graph[input1]->Rcut[i],
now, 0, 0, 0));
}
if (c.graph[now]->pre.size() == 2) {
string input1 = c.graph[now]->pre[0];
string input2 = c.graph[now]->pre[1];
for (int i = 0; i <= C; i++)
{
if (c.graph[input1]->Rcut[i].size() == 0)
continue;
set<string> names = c.graph[input1]->Rcut[i];
for (int j = 0; j <= C; j++) {
if (c.graph[input2]->Rcut[j].size() == 0)
continue;
set<string> comb = c.graph[input2]->Rcut[j];
comb.insert(names.begin(), names.end());
if (comb.size() > Luts) continue;
Ct.push_back(Cut(comb, now, 0, 0, 0));
}
}
}
vector<Cut>::iterator ct;
for (ct = Ct.begin(); ct != Ct.end(); ct++) {
int fin = 0, dep = 0;
double AreaFlow = c.graph[now]->Area;
for (string st : ct->names) {
fin += counter[st];
dep = max(dep, c.graph[st]->mindep);
AreaFlow += c.graph[st]->AreaFlow;
}
AreaFlow /= fin;
dep = dep + 1;
ct->mindep = dep;
ct->AreaFlow = AreaFlow;
ct->fin = fin;
//for (string look : ct->names)
// cout << look << endl;
}
sort(Ct.begin(), Ct.end(), cmp_AreaFlow);
c.graph[now]->mindep = Ct[0].mindep;
c.graph[now]->AreaFlow = Ct[0].AreaFlow;
//c.graph[now]->fin = Ct[0].fin;
unsigned int Ctmp = Ct.size();
for (int i = 0; i < 15; i++)
c.graph[now]->Rcut[i].clear();
c.graph[now]->Rcut[0] = set<string> { now };
for (unsigned int i = 1; i <= min(C, Ctmp); i++)
{
//for (string look : Ct[i - 1].names)
// cout << look << endl;
c.graph[now]->Rcut[i] = Ct[i - 1].names;
//printf("%d ", Ct[i - 1].names.size());
}
//printf("\n");
c.graph[now]->Fcut = Ct[0];
}*/
/*counter.clear();
Recovery(c);
for (string now : Top) {
for (string st : c.graph[now]->Rcut[1])
counter[st]++;
}*/
/*for (string now : Top) {
vector<Cut> Ct;
if (c.graph[now]->pre.size() == 0) {
//c.graph[now]->AreaFlow = 1.0 / counter[now];
if (c.graph[now]->pre.size() == 0)
continue;
}
if (c.graph[now]->pre.size() == 1) {
string input1 = c.graph[now]->pre[0];
for (int i = 0; i <= C; i++)
if (c.graph[input1]->Rcut[i].size() != 0)
Ct.push_back(Cut(c.graph[input1]->Rcut[i],
now, 0, 0, 0));
}
if (c.graph[now]->pre.size() == 2) {
string input1 = c.graph[now]->pre[0];
string input2 = c.graph[now]->pre[1];
for (int i = 0; i <= C; i++)
{
if (c.graph[input1]->Rcut[i].size() == 0)
continue;
set<string> names = c.graph[input1]->Rcut[i];
for (int j = 0; j <= C; j++) {
if (c.graph[input2]->Rcut[j].size() == 0)
continue;
set<string> comb = c.graph[input2]->Rcut[j];
comb.insert(names.begin(), names.end());
if (comb.size() >= Luts) continue;
Ct.push_back(Cut(comb, now, 0, 0, 0));
}
}
}
vector<Cut>::iterator ct;
for (ct = Ct.begin(); ct != Ct.end(); ct++) {
int fin = 0, dep = 0;
c.graph[now]->Rcut[1] = ct->names;
vis.clear();
int Area = DFS(c, now);
printf("%d\n", Area);
for (string st : ct->names) {
fin += counter[st];
dep = max(dep, c.graph[st]->mindep);
//AreaFlow += c.graph[st]->AreaFlow;
}
dep = dep + 1;
ct->mindep = dep;
ct->fin = fin;
ct->Area = Area;
}
sort(Ct.begin(), Ct.end(), cmp_Area);
c.graph[now]->mindep = Ct[0].mindep;
//c.graph[now]->fin = Ct[0].fin;
c.graph[now]->Area = Ct[0].Area;
unsigned int Ctmp = Ct.size();
for (int i = 0; i < 15; i++)
c.graph[now]->Rcut[i].clear();
c.graph[now]->Rcut[0] = set<string> { now };
for (unsigned int i = 1; i <= min(C, Ctmp); i++)
c.graph[now]->Rcut[i] = Ct[i - 1].names;
c.graph[now]->Fcut = Ct[0];
for (string ss : Ct[0].names)
counter[ss]++;
}*/
for (int i = 0; i <= Luts; i++)
c.graph[now]->cuts[i].clear();
}
for (string now : Top) {
if (c.graph[now]->pre.size() == 0)
continue;
//c.graph[now]->cuts[1].insert(set<string> { now });
assert(c.graph[now]->pre.size() <= 2);
if (c.graph[now]->pre.size() == 2) {
string input1 = c.graph[now]->pre[0];
string input2 = c.graph[now]->pre[1];
for (int t = 1; t <= Luts; t++)
for (set<string> c1 : c.graph[input1]->cuts[t]) {
for (int k = 1; k <= Luts; k++)
for (set<string> c2 : c.graph[input2]->cuts[k]) {
set<string> st = c1;
st.insert(c2.begin(), c2.end());
if (st.size() > (unsigned) Luts)
continue;
c.graph[now]->cuts[st.size()].insert(st);
}
}
}
if (c.graph[now]->pre.size() == 1) {
string input1 = c.graph[now]->pre[0];
for (int k = 1; k <= Luts; k++)
for (set<string> c1 : c.graph[input1]->cuts[k])
c.graph[now]->cuts[k].insert(c1);
}
/*Calculate dep, areaflow, exact area*/
vector<Cut> Cuts[10], Ct;
for (int k = 1; k <= Luts; k++) {
for (set<string> ct : c.graph[now]->cuts[k]) {
int dep = 0, area = 0, fin = 0;
double AF = 1;
for (string nt : ct) {
dep = max(dep, c.graph[nt]->mindep);
mark.clear();
area += DFS(c, nt);
fin += c.graph[nt]->fin;
}
dep++, area++;
if (dep <= c.graph[now]->mindep) {
Cuts[k].push_back(Cut(ct, now, dep, area, fin, AF));
Ct.push_back(Cut(ct, now, dep, area, fin, AF));
}
}
}
for (int k = 1; k <= Luts; k++) {
sort(Cuts[k].begin(), Cuts[k].end(), cmp_Area);
c.graph[now]->cuts[k].clear();
unsigned int len = Cuts[k].size();
//cout << "now:" << now << endl;
for (unsigned int i = 0; i < min(C, len); i++) {
c.graph[now]->cuts[k].insert(Cuts[k][i].names);
//cout << Cuts[i].mindep << endl;
}
}
//if (c.graph[now]->pre.size() != 1)
c.graph[now]->cuts[1].insert(set<string> { now });
sort(Ct.begin(), Ct.end(), cmp_Area);
if (Ct.size() == 0) continue;
int Depth = Ct[0].mindep;
if (c.graph[now]->Area > Ct[0].Area) {
c.graph[now]->Area = Ct[0].Area;
unsigned int Ctmp = Ct.size();
c.graph[now]->Rcut = Ct[0].names;
c.graph[now]->Fcut = Ct[0];
}
}
}
pair<int, int> single = Output(c);
pair<int, int> dual = Output2(c);
fout << str.substr(0, str.find(".")) << ',' << c.input.size() << ',' << c.output.size() << ','