-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransistorNtkCNF.h
More file actions
273 lines (232 loc) · 8.23 KB
/
Copy pathTransistorNtkCNF.h
File metadata and controls
273 lines (232 loc) · 8.23 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
271
272
273
#pragma once
#ifndef __TRANSISTORNTK_CNF_H__
#define __TRANSISTORNTK_CNF_H__
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <assert.h>
#include <algorithm>
#include "TransistorNtk.h"
#include "PathLimitation.h"
#include <UGraphviz/UGraphviz.hpp>
#include <map>
#include <set>
#include "GenerateSpice.h"
using namespace Ubpa::UGraphviz;
using namespace std;
class TransistorCNF
{
private:
TransistorNtk TNtk;
int nCnfVars;
vector<int> p; // 0: source 1: Drain
vector<int> s, c, f;
vector<vector<int>> clauses;
// new added
int nInputs; // number of vars in boolean function
vector<vector<int>> Func;
vector<int> TruthTable;
// version: 2023/2/10
vector<int> VarIndPos, VarIndNeg; // indicate whether literals exist?
// version: 2023/2/14
vector<vector<int>> Onsets, Offsets;
vector<vector<int>> PosRepPattern, NegRepPattern;
string svars;
//version: 2023/03/06
int SatResult;
// version: 2023/04/26
vector<vector<vector<string>>> ResultPaths;
vector<vector<int>> ResultTruthTables;
// version: 2023/5/9
int AccTech1Flag, AccTech2Flag; // flag1: representative patterns; flag2: pre-define transistors for existed literals of the given factored form
public:
TransistorCNF();
TransistorCNF(TransistorNtk ntk);
~TransistorCNF();
void InitVars();
void InitVecVars();
void CreatePosClauses();
void CreateNegClauses();
void CreateANDClause(vector<int> cnfvars);
void CreateXNORClause(vector<int> cnfvars1, vector<int> cnfvars2);
void CreateORClause(vector<int> cnfvars);
void CreateAtMostOneClause(vector<int> cnfvars);
void WriteCnf(string cnfpath);
vector<string> ParseCnf(int mos, vector<transistor>& transistors, int OutINV, string cnfpath, string path = "");
void ParsePosCnf(string cnfpath);
void ParseNegCnf(string cnfpath);
void InitGlobalVars();
void CreateGlobalClauses();
void CreatePosClauses(int iP);
void CreateNegClauses(int iP);
void CreateClauses();
vector<int> DeriveTruthTable();
void CreateFinalClauses();
// version: 2023/1/14
void InputParser(string boolfunc);
void InputParser(string onFunc, string offFunc);
void CreateAccClauses();
vector<vector<int>> DeriveFunc(vector<vector<string>> vProducts, map<char, int> VarId);
void DeriveRepPattern(int flag);
void showPatterns(vector<vector<int>> patterns);
void showRepPatterns(vector<vector<int>> patterns);
void Debug();
void InitAccVecVars();
void CreateAccFinalClauses();
void CreateAccPosClauses(int iP);
void CreateAccNegClauses(int iP);
vector<int> CreatePriorClauses();
void CreateAdvancedPosClauses(vector<int> Tran2InputVars); // set flows of those edges connected with opened transistors as 0
void CreateAdvancedNegClauses(vector<int> Tran2InputVars);
// version:2023/3/17
int GetSatResult() { return SatResult; }
int GetnCnfVars() { return nCnfVars; }
TransistorNtk GetTransistorNtk() { return TNtk; }
void SetnCnfVars(int CnfVarsNum) { nCnfVars = CnfVarsNum; }
// version:2023/3/18
void CreateAccClausesWithoutGlobal();
// version:2023/3/19
void updateSvars(char var) { svars += var; }
void SetnInputs(int VarsNum) { nInputs = VarsNum; }
void SetVarIndPosNeg(map<char, int>& VarId);
void SetOnsets(vector<vector<int>> sets) { Onsets = sets; }
void SetOffsets(vector<vector<int>> sets) { Offsets = sets; }
vector<vector<int>> GetClauses() { return clauses; }
void SetClauses(vector<vector<int>> NewClauses) { clauses = NewClauses; }
// version:2023/3/20
void CreateTransistorInputClauses(int iTransistor, int iLiteral);
// version: 2023/3/21
void SetTNtk(TransistorNtk ntk) { TNtk = ntk; }
// version: 2023/3/22
vector<vector<int>> GetRepPattern(int Flag) { return (Flag) ? PosRepPattern : NegRepPattern; }
// version: 2023/3/26
void updateNtkTmpIDs() { TNtk.updateTmpID(); }
// version:2023/3/29
int GetnVars() { return svars.size(); }
// version: 2023/4/14
void CreateNoMoreNClauses(vector<int> cnfvars, int n);
// version: 2023/4/22
vector<string> ParseCnf(string cnfpath, map<pair<int, int>, int>& AllEdges, map<pair<int, int>, string>& transistors, map<pair<int, int>, int>& transistors_cnfvar, string path = "");
void AddClause(vector<int> clause) { clauses.push_back(clause); }
int getK() {
int max = 0;
for (auto Onset : Onsets) {
if (Onset.size() > max)
max = Onset.size();
}
return max;
}
// version: 2023/4/23
void CreateAtMostKClause(vector<int> cnfvars, int K);
void CreateKLimitedPathConstraints(int K);
// version: 2023/4/26
string GetSvars() { return svars; }
vector<vector<string>> GetResultPaths(int i) { return ResultPaths[i]; }
vector<vector<int>> DeriveResultFunc();
void showResultPaths() {
cout << "Resulting paths are: " << endl;
for (auto SingleOutPaths : ResultPaths) {
for (auto path : SingleOutPaths) {
for (auto t : path)
cout << t << " ";
cout << endl;
}
cout << endl;
}
}
vector<int> GetDepths() {
vector<int> MaxLength(ResultPaths.size(), 0);
for (int i = 0; i < ResultPaths.size(); i++) {
for (auto path : ResultPaths[i])
if (path.size() > MaxLength[i])
MaxLength[i] = path.size();
}
return MaxLength;
}
// version: 2023/4/30
void CreateBlockClause(vector<int> val);
int GetLiterals() {
int num = 0;
for (auto onset : Onsets)
num += onset.size();
return num;
}
/*vector<string> GetVecLiterals() {
}*/
// version: 2023/5/9
void SetAccFlags(int flag1, int flag2) {
AccTech1Flag = flag1;
AccTech2Flag = flag2;
}
int GetAccFlag1() { return AccTech1Flag; }
int GetAccFlag2() { return AccTech2Flag; }
};
class MultiOutTransistorCNF :public TransistorCNF {
private:
vector<TransistorCNF> MultiOutsCnfs;
int nOutputs;
/*int nCnfVars;
TransistorNtk TNtk;
vector<vector<int>> clauses;*/
// version: 2023/4/22
// int K; // limited path length
vector<int> Ks; // each corresponding to an output
// version: 2023/4/29
int Exact; // whether the derived transistor network after SAT solving is exactly with the user-needed function
int DepthLimited; // =1: limit the number of transistors in series
// version: 2023/5/4
vector<string> AllLiterals;
public:
MultiOutTransistorCNF();
~MultiOutTransistorCNF();
MultiOutTransistorCNF(TransistorNtk NTK, int nOuts);
void InitVecMultiOutsCnfs(vector<string> BoolFuncs, string dir = "");
void MultiOutInputParser(vector<string> BoolFuncs, string dir = "");
void MultiOutInputParser(vector<string> onFuncs, vector<string> offFuncs);
void GetAllClauses();
void MultiOutCreateClauses(vector<int> Tran2InputVars);
// version: 2023/4/22
vector<int> GetMaxPathLength() { return Ks; }
// version: 2023/4/23
TransistorCNF GetTCnf(int i) { return MultiOutsCnfs[i]; }
// version: 2023/4/26
vector<vector<int>> DeriveEachCnfTruthTable() {
vector<vector<int>> vTruthtables;
for (int i = 0; i < nOutputs; i++)
vTruthtables.push_back(MultiOutsCnfs[i].DeriveTruthTable());
return vTruthtables;
}
// version: 2023/4/29
void SetExact(int b) { Exact = b; }
int GetExact() { return Exact; }
void SetDepthLimitedFlag(int flag) { DepthLimited = flag; }
int GetIsDepthLimited() {
vector<int> Depths = GetDepths();
for (int i = 0; i < Depths.size(); i++)
if (Depths[i] > Ks[i])
return 0;
return 1;
}
// version: 2023/5/4
int GetTotalLiteralNums() {
int num = 0;
for (int i = 0; i < MultiOutsCnfs.size(); i++)
num += MultiOutsCnfs[i].GetLiterals();
return num;
}
vector<string> GetAlLiterals() {
return AllLiterals;
}
};
void removeSpace(string& s);
vector<string> split(string s, char sign);
string ParseEqn(string EqnPath);
int NPTransistorsWithoutINV(int nInternalTransistors, vector<int> OutINVFlags);
int NPTransistorsWithINV(vector<int> Tran2InputVars, int nVars, int nInternalTransistors, int OutINVFlag);
// version: 2023/3/30
int NPTransistorsWithINV(vector<int> Tran2InputVars, int nVars, int nInternalTransistors, vector<int> OutINVFlags);
// version: 2023/4/5
int NPTransistorsWithINV(set<string> InputLiterals, int nInternalTransistors, vector<int> OutINVFlags);
void ParseBoolFuncTxtFile(string TxtPath, map<string, pair<string, int>>& BoolFuncs);
#endif // !__TRANSISTORNTK_CNF_H__