-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.h
More file actions
270 lines (239 loc) · 6.9 KB
/
utils.h
File metadata and controls
270 lines (239 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#pragma once
#include <iostream>
#include <time.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include<string>
#include<fstream>
#include <cmath>
#include<stack>
#include <algorithm>
#include <random>
#include "mockturtle\mockturtle.hpp"
#include "lorina\aiger.hpp"
#include "C:\\gurobi1001\\win64\\include\\gurobi_c++.h"
#include "z3include\\z3++.h"
#pragma comment(lib, "C:\\z3\\libZ3.lib")
#pragma comment(lib, "gurobi100.lib")
#pragma comment(lib, "gurobi_c++md2017.lib")
using namespace mockturtle;
using namespace std;
inline xmg_network Aig2Xmg(string strAigName)
{
mockturtle::aig_network aig;
lorina::read_aiger(strAigName, mockturtle::aiger_reader(aig));
xmg_npn_resynthesis resyn;
exact_library<xmg_network, xmg_npn_resynthesis> lib(resyn);
map_params ps;
ps.skip_delay_round = true;
ps.required_time = numeric_limits<double>::max();
xmg_network xmg = mockturtle::map(aig, lib, ps);
functional_reduction(xmg);
xmg = cleanup_dangling(xmg);
if (strAigName == "bench\\sqrt.aig")
return xmg;
depth_view xmg_depth{ xmg };
fanout_view xmg_fanout{ xmg_depth };
xmg_resubstitution(xmg_fanout);
xmg_network xmg_res = xmg_fanout;
xmg_res = cleanup_dangling(xmg);
if (xmg_res.size() < xmg.size())
xmg = xmg_res;
return xmg;
}
inline xmg_network Bliff2Xmg(string strBliffName)
{
cover_network cover;
lorina::read_blif(strBliffName, blif_reader(cover));
aig_network aig;
convert_cover_to_graph(aig, cover);
xmg_npn_resynthesis resyn;
exact_library<xmg_network, xmg_npn_resynthesis> lib(resyn);
map_params ps;
ps.skip_delay_round = true;
ps.required_time = numeric_limits<double>::max();
xmg_network xmg = mockturtle::map(aig, lib, ps);
functional_reduction(xmg);
xmg = cleanup_dangling(xmg);
depth_view xmg_depth{ xmg };
fanout_view xmg_fanout{ xmg_depth };
xmg_resubstitution(xmg_fanout);
xmg_network xmg_res = xmg_fanout;
xmg_res = cleanup_dangling(xmg);
if (xmg_res.size() < xmg.size())
xmg = xmg_res;
return xmg;
}
inline void Trim(string& s)
{//remove ' ' and '\\'
s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
s.erase(std::remove(s.begin(), s.end(), '\\'), s.end());
}
inline string ExtractStr(string& s, string strLeft, string strRight)
{//extract substring from strLeft to strRight in s
size_t left = s.find(strLeft);
if (left == string::npos)
return "";
size_t right = s.find(strRight, left + strLeft.length());
if (right == string::npos)
return "";
//cout << left << " " << right << "\n";
string res = s.substr(left + strLeft.length(), right - left - strLeft.length());
s.erase(left, right - left);
return res;
}
inline int GetIndexInVector(int n, vector<int>& vecn)
{//get index of n in vecn
for (int i = 0; i < vecn.size(); i++)
{
if (vecn[i] == n)
return i;
}
return -1;
}
inline int GetFromArray(vector<int>& vecMem, int nArray, int nArraySize = 256)
{//get empty row in nArray
int nRow = nArray * nArraySize;
int nBack = (nArray + 1) * nArraySize;
for (; nRow < nBack; nRow++)
{
if (vecMem[nRow] == 0)
break;
}
if (nRow == nBack)
nRow = -1;
return nRow;
}
inline bool IsInVector(int n, vector<int>& vecn)
{//whether n is in vecn
for (int i = 0; i < vecn.size(); i++)
{
if (vecn[i] == n)
return true;
}
return false;
}
inline void RemapVector(vector<int>& vecN, std::map<int, int> mapOldNew, bool bSkip = false)
{//remap vecN with mapOldNew
for (int i = 0; i < vecN.size(); i++)
{
if (mapOldNew.find(vecN[i]) == mapOldNew.end())
{
if (bSkip)
vecN[i] = -100;
continue;
}
vecN[i] = mapOldNew[vecN[i]];
}
if (bSkip)
vecN.erase(remove(vecN.begin(), vecN.end(), -100), vecN.end());
}
inline float xmg_depth_rewrite(xmg_network& xmg, bool allow_size_increase = false, char strat = '0', float overhead = 1.0)
{
depth_view xmg_depth{ xmg };
xmg_algebraic_depth_rewriting_params ps;
cut_rewriting_stats st;
if (strat == 's')
ps.strategy = ps.selective;
else if (strat == 'a')
ps.strategy = ps.aggressive;
else
ps.strategy = ps.dfs;
ps.overhead = overhead;
ps.allow_area_increase = allow_size_increase;
xmg_algebraic_depth_rewriting(xmg_depth, ps);
xmg = xmg_depth;
xmg = cleanup_dangling(xmg);
return to_seconds(st.time_total);
}
inline void xmg_resub(xmg_network& xmg, int max_insert = 2)
{
depth_view xmg_depth{ xmg };
fanout_view xmg_fanout{ xmg_depth };
resubstitution_params ps;
ps.max_inserts = max_insert;
xmg_resubstitution(xmg_fanout, ps);
xmg = xmg_fanout;
xmg = cleanup_dangling(xmg);
}
inline float xmg_node_resynthesis(xmg_network& xmg, int cut_size = 4)
{
mapping_view<xmg_network, true> mapped_xmg{ xmg };
lut_mapping_params ps;
node_resynthesis_stats st;
ps.cut_enumeration_ps.cut_size = cut_size;
lut_mapping<mapping_view<xmg_network, true>, true>(mapped_xmg, ps);
const auto klut = *collapse_mapped_network<klut_network>(mapped_xmg);
xmg_npn_resynthesis resyn;
xmg = node_resynthesis<xmg_network>(klut, resyn);
xmg = cleanup_dangling(xmg);
return to_seconds(st.time_total);
}
inline float xmg_cut_rewrite(xmg_network& xmg, int cut_size = 4, bool bZero = true)
{
exact_xmg_resynthesis<xmg_network> resyn;
cut_rewriting_params ps;
cut_rewriting_stats st;
ps.cut_enumeration_ps.cut_size = cut_size;
ps.allow_zero_gain = bZero;
xmg = cut_rewriting(xmg, resyn, ps);
xmg = cleanup_dangling(xmg);
return to_seconds(st.time_total);
}
inline void Balance(xmg_network& xmg)
{
xmg_network n = xmg.clone();
sop_rebalancing<xmg_network> balance_fn;
n = balancing(n, { balance_fn });
if (n.num_gates() <= xmg.num_gates())
xmg = n.clone();
}
inline void Rewrite(xmg_network& xmg, bool bZero = false)
{
xmg_network n = xmg.clone();
exact_xmg_resynthesis<xmg_network> resyn;
cut_rewriting_params ps;
ps.cut_enumeration_ps.cut_size = 4;
ps.allow_zero_gain = bZero;
n = cut_rewriting(n, resyn, ps);
n = cleanup_dangling(n);
if (n.num_gates() <= xmg.num_gates())
xmg = n.clone();
}
inline void Refactor(xmg_network& xmg, bool bZero = false)
{
xmg_network n = xmg.clone();
akers_resynthesis<xmg_network> resyn;
refactoring_params ps;
ps.allow_zero_gain = bZero;
refactoring(n, resyn, ps);
n = cleanup_dangling(n);
if (n.num_gates() <= xmg.num_gates())
xmg = n.clone();
}
inline void Resyn2(xmg_network& xmg)
{
Balance(xmg);
//cout << xmg.num_gates() << " ";
Rewrite(xmg);
//cout << xmg.num_gates() << " ";
Refactor(xmg);
//cout << xmg.num_gates() << " ";
Balance(xmg);
//cout << xmg.num_gates() << " ";
Rewrite(xmg);
//cout << xmg.num_gates() << " ";
Rewrite(xmg, true);
//cout << xmg.num_gates() << " ";
Balance(xmg);
//cout << xmg.num_gates() << " ";
Refactor(xmg, true);
//cout << xmg.num_gates() << " ";
Rewrite(xmg, true);
//cout << xmg.num_gates() << " ";
Balance(xmg);
//cout << xmg.num_gates() << "\n";
}