-
Notifications
You must be signed in to change notification settings - Fork 6
/
TopiDebugPrinting.cc
285 lines (232 loc) · 6.66 KB
/
TopiDebugPrinting.cc
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
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright(C) 2021-2023 Intel Corporation
// SPDX - License - Identifier: MIT
#include "Topi.hpp"
using namespace std;
using namespace Topor;
template <typename TLit, typename TUInd, bool Compress>
string CTopi<TLit, TUInd, Compress>::SVar(TUVar v) const
{
stringstream ss;
const string sv = to_string(v);
ss << sv << setprecision(10);
if (IsAssignedVar(v))
{
ss << "@" << m_VarInfo[v].m_DecLevel;
[[maybe_unused]] const auto s = m_VsidsHeap.get_var_score(v);
/*if (s != 0)
{
ss << "=" << s;
}*/
}
return ss.str();
}
template <typename TLit, typename TUInd, bool Compress>
std::string CTopi<TLit, TUInd, Compress>::SLit(TULit l) const
{
auto s = IsNeg(l) ? "-" + SVar(GetVar(l)) : SVar(GetVar(l));
if (IsAssigned(l))
{
s += "[" + (string)(IsSatisfied(l) ? "S" : "U") + "]";
}
return s;
}
template <typename TLit, typename TUInd, bool Compress>
string CTopi<TLit, TUInd, Compress>::SE2I()
{
stringstream ss;
ss << "E2I:\n";
for (TLit externalLit = 1; externalLit < (TLit)m_E2ILitMap.cap(); ++externalLit)
{
ss << "\t" << externalLit << " : " << SLit(m_E2ILitMap[externalLit]) << "; ";
}
ss << "\n";
return ss.str();
}
template <typename TLit, typename TUInd, bool Compress>
string CTopi<TLit, TUInd, Compress>::STrail()
{
stringstream ss;
ss << "Current trail (reversed):\n";
for (TUVar v = m_TrailEnd; v != BadUVar; v = m_VarInfo[v].m_TrailPrev)
{
TULit l = GetAssignedLitForVar(v);
ss << "\t";
[[maybe_unused]] const TUV vDecLevel = GetAssignedDecLevelVar(v);
[[maybe_unused]] const TUV prevDecLevel = m_VarInfo[v].m_TrailPrev == BadUVar ? numeric_limits<TUV>::max() : GetAssignedDecLevelVar(m_VarInfo[v].m_TrailPrev);
if (vDecLevel != prevDecLevel)
{
ss << " DL " << vDecLevel << " *** ";
}
ss << SLit(l) << " {";
const auto& ai = m_AssignmentInfo[GetVar(l)];
const auto& vi = m_VarInfo[GetVar(l)];
if (ai.m_IsAssigned && ai.IsAssignedBinary())
{
ss << SLit(vi.m_BinOtherLit);
}
else if (vi.m_ParentClsInd != BadClsInd)
{
ss << SLits(Cls(vi.m_ParentClsInd));
}
ss << "}; ";
}
ss << endl;
return ss.str();
}
template <typename TLit, typename TUInd, bool Compress>
string CTopi<TLit, TUInd, Compress>::SUserLits(const span<TLit> litSpan) const
{
stringstream ss;
for (TLit l : litSpan)
{
ss << l << " ";
}
return ss.str().substr(0, ss.str().size() - 1);
}
template <typename TLit, typename TUInd, bool Compress>
string CTopi<TLit, TUInd, Compress>::SVars(const TSpanTULit varSpan) const
{
stringstream ss;
for (TULit v : varSpan)
{
ss << SVar(v) << " ";
}
return ss.str().substr(0, ss.str().size() - 1);
}
template <typename TLit, typename TUInd, bool Compress>
bool CTopi<TLit, TUInd, Compress>::P(const string& s)
{
cout << s << flush; return true;
}
template <typename TLit, typename TUInd, bool Compress>
void CTopi<TLit, TUInd, Compress>::PrintDebugModel(TToporReturnVal trv)
{
if (trv != TToporReturnVal::RET_SAT)
{
cout << "m_DebugModel = FAILED, since the return value is not SAT; it's " << trv << endl;
return;
}
cout << "m_DebugModel = {false";
for (TLit v = 1; v <= m_Stat.m_MaxUserVar; ++v)
{
const TToporLitVal externalVal = GetValue(v);
assert(externalVal == TToporLitVal::VAL_SATISFIED || externalVal == TToporLitVal::VAL_UNSATISFIED);
cout << ", " << (externalVal == TToporLitVal::VAL_SATISFIED ? "true" : "false");
if (v % 100 == 0)
{
cout << "\n";
}
}
cout << "};\n";
}
template <typename TLit, typename TUInd, bool Compress>
void CTopi<TLit, TUInd, Compress>::VerifyDebugModel()
{
if (m_DebugModel.empty())
{
return;
}
assert((TLit)m_DebugModel.size() == m_Stat.m_MaxUserVar + 1);
// -1: undefined; 0: false; 1: true
CDynArray<uint8_t> internalVarVals((size_t)m_Stat.m_MaxInternalVar + 1, -1);
auto ExpectedVal = [&](TULit l)
{
const TUVar v = GetVar(l);
const uint8_t expectedValVar = internalVarVals[v];
if (expectedValVar == -1)
{
return (uint8_t)-1;
}
if (IsNeg(l))
{
return expectedValVar == 1 ? (uint8_t)0 : (uint8_t)1;
}
return expectedValVar;
};
auto SInternalLitData = [&](TULit l)
{
stringstream ss;
const auto elit = GetExternalLit(l);
ss << "{index " << l << " : ilit " << SLit(l) << " : elit " << elit << "; debugModel = " << (elit > 0 ? m_DebugModel[elit] : m_DebugModel[-elit]) << "}";
return ss.str();
};
//cout << "E2I: " << endl;
for (TLit externalV = 1; externalV <= m_Stat.m_MaxUserVar; ++externalV)
{
const TUVar l = m_E2ILitMap[externalV];
const TUVar v = GetVar(l);
internalVarVals[v] = IsNeg(l) ? (uint8_t)!m_DebugModel[externalV] : (uint8_t)m_DebugModel[externalV];
//cout << "m_DebugModel[" << externalV << "] = " << m_DebugModel[externalV] << " : " << SLit(l) << endl;
}
for (TUVar v = m_TrailStart; v != BadUVar; v = m_VarInfo[v].m_TrailNext)
{
const TULit l = GetLit(v, false);
const uint8_t expectedVal = ExpectedVal(l);
if (expectedVal != -1)
{
assert(IsSatisfied(l) == (bool)expectedVal || P("\t" + SLit(GetLit(v, false)) + "\n"));
if (IsSatisfied(l) != (bool)expectedVal)
{
cout << "VerifyDebugModel ERROR in verifying the trail at literal " << SInternalLitData(l) << ". Exiting...\n";
exit(-1);
}
}
}
for (TULit l = 1; l < GetNextLit(); ++l)
{
TWatchInfo& wi = m_Watches[l];
const auto expectedValL = ExpectedVal(l);
if (wi.IsEmpty() || expectedValL != 0)
{
continue;
}
if (wi.m_BinaryWatches != 0)
{
TSpanTULit binWatches = m_W.get_span_cap(wi.m_WBInd + wi.GetLongEntries(), wi.m_BinaryWatches);
for (TULit secondLit : binWatches)
{
const auto expectedValSecondLit = ExpectedVal(secondLit);
assert(expectedValSecondLit != 0 || P("\t" + SLit(l) + " " + SLit(secondLit) + "\n"));
if (expectedValSecondLit == 0)
{
cout << "[l secondLit] = " << "[" << SInternalLitData(l) << ", " << SInternalLitData(secondLit) << "]" << endl;
cout << "VerifyDebugModel ERROR in verifying a binary clause! Exiting...\n";
exit(-1);
}
}
}
}
int dbg = 0;
for (TUInd clsInd = ClsLoopFirst(false); !ClsLoopCompleted(); clsInd = ClsLoopNext(), ++dbg)
{
if (ClsChunkDeleted(clsInd))
{
continue;
}
const auto cls = ConstClsSpan(clsInd);
bool isOK = false;
for (TULit l : cls)
{
const auto expectedValL = ExpectedVal(l);
if (expectedValL != 0)
{
isOK = true;
break;
}
}
assert(isOK || P("\t" + SLits(cls) + "\n"));
if (!isOK)
{
for (TULit l : cls)
{
cout << SInternalLitData(l) << ", ";
}
cout << "\nVerifyDebugModel ERROR in verifying a long clause! Exiting...\n";
exit(-1);
}
}
cout << "VerifyDebugModel VERIFIED!\n";
}
template class Topor::CTopi<int32_t, uint32_t, false>;
template class Topor::CTopi<int32_t, uint64_t, false>;
template class Topor::CTopi<int32_t, uint64_t, true>;