-
Notifications
You must be signed in to change notification settings - Fork 6
/
Main.cc
1412 lines (1249 loc) · 42.6 KB
/
Main.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright(C) 2021 Intel Corporation
// SPDX - License - Identifier: MIT
#include <iostream>
#include <cassert>
#include <fstream>
#include <vector>
#include <array>
#include <string>
#include <filesystem>
#include <functional>
#include <cstring>
#include <iterator>
#include <cstdio>
#include <unordered_set>
#include <type_traits>
#ifdef __CYGWIN__
extern "C" FILE * popen(const char* command, const char* mode);
extern "C" void pclose(FILE * pipe);
#endif
#ifndef SKIP_ZLIB
#ifdef WIN32
# include "win/zlib.h"
#else
# include <zlib.h>
#endif
#endif
#include "SetInScope.h"
#ifdef WIN32
#define popen _popen
#define pclose _pclose
#define fileno _fileno
#endif
template<char delimiter>
class WordDelimitedBy : public std::string
{};
namespace fs = std::filesystem;
#include "Topor.hpp"
using namespace std;
using namespace Topor;
// Converts enum class to the underlying type
template <typename E>
constexpr auto U(E e) noexcept
{
return static_cast<std::underlying_type_t<E>>(e);
}
// The supported archive file types
enum class TArchiveFileType : uint8_t
{
XZ = 0,
LZMA = 1,
BZ = 2,
GZ = 3,
SevenZ = 4,
None = 5
};
// The supported archive file types' file signatures
constexpr uint8_t MaxSigLength = 7;
constexpr static array<array<int, MaxSigLength + 1>, U(TArchiveFileType::None)> fileSig = { {
{ 0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00, 0x00, EOF},
{ 0x5D, 0x00, 0x00, 0x80, 0x00, EOF, EOF, EOF},
{ 0x42, 0x5A, 0x68, EOF, EOF, EOF, EOF, EOF },
{ 0x1F, 0x8B, EOF, EOF, EOF, EOF, EOF, EOF },
{ 0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C, EOF, EOF }
} };
static constexpr bool AllSigsDifferByFirstInt()
{
for (size_t i = 0; i < fileSig.size(); ++i)
{
for (size_t j = i + 1; j < fileSig.size(); ++j)
{
if (fileSig[i][0] == fileSig[j][0])
{
return false;
}
}
}
return true;
}
// We count all signatures' first integer being different in the code, which determines the file's type
static_assert(AllSigsDifferByFirstInt());
// Strings before and after the command for each archive type
static array<pair<string, string>, U(TArchiveFileType::None)> commandStringBeforeAndAfter = {
make_pair("xz -c -d",""),
make_pair("lzma -c -d",""),
make_pair("bzip2 -c -d",""),
make_pair("gzip -c -d",""),
make_pair("7z x -so","2>/dev/null"),
};
static constexpr int BadRetVal = -1;
using TLit = int32_t;
template <typename TTopor>
int OnFinishingSolving(TTopor& topor, TToporReturnVal ret, bool printModel, bool printUcore, const std::span<TLit> assumps = {}, vector<TLit>* varsToPrint = nullptr)
{
CApplyFuncOnExitFromScope<> printStatusExplanation([&]()
{
const string expl = topor.GetStatusExplanation();
if (!expl.empty())
{
cout << "c " << expl << endl;
}
});
switch (ret)
{
case Topor::TToporReturnVal::RET_SAT:
cout << "s SATISFIABLE" << endl;
if (printModel)
{
auto PrintVal = [&](TLit v)
{
const auto vVal = topor.GetLitValue(v);
assert(vVal != TToporLitVal::VAL_UNASSIGNED);
cout << " " << (vVal != TToporLitVal::VAL_UNSATISFIED ? v : -v);
};
cout << "v";
if (!varsToPrint)
{
for (TLit v = 1; v <= topor.GetMaxUserVar(); ++v)
{
PrintVal(v);
}
}
else
{
for (auto v : *varsToPrint)
{
PrintVal(v);
}
}
cout << " 0" << endl;
}
return 10;
case Topor::TToporReturnVal::RET_UNSAT:
cout << "s UNSATISFIABLE" << endl;
if (printUcore)
{
cout << "v";
for (size_t assumpInd = 0; assumpInd < assumps.size(); ++assumpInd)
{
TLit currAssump = assumps[assumpInd];
if (topor.IsAssumptionRequired(assumpInd))
{
cout << " " << currAssump;
}
}
cout << " 0" << endl;
}
return 20;
case Topor::TToporReturnVal::RET_TIMEOUT_LOCAL:
cout << "s TIMEOUT_LOCAL" << endl;
return BadRetVal;
case Topor::TToporReturnVal::RET_CONFLICT_OUT:
cout << "s CONFLICT_OUT" << endl;
return 30;
case Topor::TToporReturnVal::RET_MEM_OUT:
cout << "s MEMORY_OUT" << endl;
return BadRetVal;
case Topor::TToporReturnVal::RET_USER_INTERRUPT:
cout << "s USER_INTERRUPT" << endl;
return BadRetVal;
case Topor::TToporReturnVal::RET_INDEX_TOO_NARROW:
cout << "s INDEX_TOO_NARROW" << endl;
return BadRetVal;
case Topor::TToporReturnVal::RET_PARAM_ERROR:
cout << "s PARAM_ERROR" << endl;
return BadRetVal;
case Topor::TToporReturnVal::RET_TIMEOUT_GLOBAL:
cout << "s TIMEOUT_GLOBAL" << endl;
return BadRetVal;
case Topor::TToporReturnVal::RET_DRAT_FILE_PROBLEM:
cout << "s DRAT_FILE_PROBLEM" << endl;
return BadRetVal;
case Topor::TToporReturnVal::RET_EXOTIC_ERROR:
cout << "s EXOTIC_ERROR" << endl;
return BadRetVal;
default:
cout << "s UNEXPECTED_ERROR" << endl;
return BadRetVal;
}
}
int main(int argc, char** argv)
{
if (argc == 1 || strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)
{
cout << print_as_color <ansi_color_code::red>("c Usage:") << endl;
cout << "\tc <Intel(R) SAT Solver Executable> <CNF> OPTIONAL: <Param1> <Val1> <Param2> <Val2> ... <ParamN> <ValN>" << endl;
cout << "\tc <CNF> can either be a text file or an archive file in one of the following formats: .xz, .lzma, .bz2, .gz, .7z (the test is based on the file signature)" << endl;
cout << "\tc <CNF> is expected to be in simplified DIMACS format, used at SAT Competitions (http://www.satcompetition.org/2011/format-benchmarks2011.html) with the following optional extension to support incrementality:" << endl;
cout << "\tc The following Intel(R) SAT Solver Executable-specific commands are also legal (ignore \"c \" below): " << endl;
cout << "\tc r <ParamName> <ParamVal>" << endl;
cout << "\tc ot <TimeOut> <IsCpuTimeOut>" << endl;
cout << "\tc oc <ConflictThreshold>" << endl;
cout << "\tc lb <BoostScoreLit> <Mult>" << endl;
cout << "\tc lf <FixPolarityLit> <OnlyOnce>" << endl;
cout << "\tc ll <LitToCreateInternalLit>" << endl;
cout << "\tc lc <ClearUserPolarityInfoLit>" << endl;
cout << "\tc b <BacktrackLevel>" << endl;
cout << "\tc n <ConfigNumber>" << endl;
cout << "\tc s <Lit1 <Lit2> ... <Litn>: solve under the assumptions {<Lit1 <Lit2> ... <Litn>}" << endl;
cout << "\tc The solver parses the p cnf vars clss line, but it ignores the number of clauses and uses the number of variables as a non-mandatory hint" << endl;
cout << print_as_color <ansi_color_code::red>("c Intel(R) SAT Solver executable parameters:") << endl;
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/solver_mode") << " : enum (0, 1, or 2); default = " << print_as_color<ansi_color_code::green>("0") << " : " << "what type of solver to use in terms of clause buffer indexing and compression: 0 -- 32-bit index, uncompressed, 1 -- 64-bit index, uncompressed, 2 -- 64-bit index, bit-array compression \n";
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/bin_drat_file") << " : string; default = " << print_as_color<ansi_color_code::green>("\"\"") << " : " << "path to a file to write down a binary DRAT proof\n";
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/text_drat_file") << " : string; default = " << print_as_color<ansi_color_code::green>("\"\"") << " : " << "path to a file to write down a text DRAT proof (if more than one /topor_tool/bin_drat_file and /topor_tool/text_drat_file parameters provided, only the last one is applied, rest are ignored)\n";
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/drat_sort_every_clause") << " : bool (0 or 1); default = " << print_as_color<ansi_color_code::green>("0") << " : " << "sort every clause in DRAT proof (can be helpful for debugging)\n";
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/print_model") << " : bool (0 or 1); default = " << print_as_color<ansi_color_code::green>("1") << " : " << "print the models for satisfiable invocations?\n";
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/print_ucore") << " : bool (0 or 1); default = " << print_as_color<ansi_color_code::green>("1") << " : " << "print the indices of the assumptions in the unsatisfiable core for unsatisfiable invocations (0-indexed)?\n";
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/verify_model") << " : bool (0 or 1); default = " << print_as_color<ansi_color_code::green>("0") << " : " << "verify the models for satisfiable invocations?\n";
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/verify_ucore") << " : bool (0 or 1); default = " << print_as_color<ansi_color_code::green>("0") << " : " << "verify the unsatisfiable cores in terms of assumptions for unsatisfiable invocations?\n";
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/ignore_file_params") << " : bool (0 or 1); default = " << print_as_color<ansi_color_code::green>("0") << " : " << "ignore parameter settings in the input file (lines starting with 'r')?\n";
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/allsat_models_number") << " : unsigned long integer; default = 1" << print_as_color<ansi_color_code::green>("1") << " : " << "the maximal number of models for AllSAT. AllSAT with blocking clauses over /topor_tool/allsat_blocking_variables's variables is invoked if: (1) this parameter is greater than 1; (2) the CNF format is DIMACS without Topor-specific commands; (3) /topor_tool/allsat_blocking_variables is non-empty\n";
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/allsat_blocking_variables") << " : string; default = " << print_as_color<ansi_color_code::green>("\"\"") << " : " << "if /topor_tool/allsat_models_number > 1, specifies the variables which will be used for blocking clauses, sperated by a comma, e.g., 1,4,5,6,7,15.\n";
cout << "\tc " << print_as_color <ansi_color_code::cyan>("/topor_tool/allsat_blocking_variables_file_alg") << " : string; default = " << print_as_color<ansi_color_code::green>("3") << " : " << "if /topor_tool/allsat_models_number > 1 and our parameter > 0, read the blocking variables from the first comment line in the file (format: c 1,4,5,6,7,15), where the value means: 1 -- assign lowest internal SAT variables to blocking; 2 -- assign highest internal SAT variables to blocking; >=3 -- assign their own internal SAT variables to blocking \n";
CTopor topor;
cout << topor.GetParamsDescr();
return 0;
}
cout << "c Intel(R) SAT Solver started" << endl;
if (argc & 1)
{
cout << "c topor_tool ERROR: the number of arguments (excluding the executable name) must be odd. Run without parameters for more information." << endl;
return BadRetVal;
}
bool isDratBinary = true;
string dratName("");
bool dratSortEveryClause = false;
bool printModel = true;
bool printUcore = false;
bool verifyModel = false;
bool verifyUcore = false;
bool ignoreFileParams = false;
unsigned long allsatModels = 0;
vector<TLit> blockingVars;
unsigned long allsatBlockingFromInstanceAlg = 3;
// 0: 32-bit clause buffer index; 1: 64-bit clause buffer index; 2: 64-bit clause buffer index & bit-array-compression
uint8_t type_indexing_and_compression = 0;
/*
* Identify the input file type, read it, read the parameters too
*/
const string inputFileName = argv[1];
if (!filesystem::exists(inputFileName))
{
cout << "c topor_tool ERROR: the input file " << inputFileName << " doesn't exist" << endl;
return BadRetVal;
}
// Does the signature match one of the archive types we support?
FILE* tmp = fopen(inputFileName.c_str(), "r");
if (tmp == nullptr)
{
cout << "c topor_tool ERROR: couldn't open the input file " << inputFileName << " to verify the signature" << endl;
return BadRetVal;
}
TArchiveFileType aFileType = TArchiveFileType::None;
int c = getc(tmp);
for (size_t currType = 0; c != EOF && currType < fileSig.size() && aFileType == TArchiveFileType::None; ++currType)
{
const auto& currSig = fileSig[currType];
if (c == currSig[0])
{
bool rightType = true;
c = getc(tmp);
for (uint8_t i = 1; c != EOF && currSig[i] != EOF; ++i, c = getc(tmp))
{
if (c != currSig[i])
{
rightType = false;
break;
}
}
if (rightType)
{
aFileType = (TArchiveFileType)currType;
cout << "c topor_tool: file type determined to an archive file.";
#ifndef SKIP_ZLIB
if (aFileType != TArchiveFileType::GZ)
{
cout << " The following command will be used to read it through a pipe : " <<
commandStringBeforeAndAfter[U(aFileType)].first << " " << inputFileName << " " << commandStringBeforeAndAfter[U(aFileType)].second;
}
else
{
cout << " It will be read using gzlib.";
}
#else
cout << " The following command will be used to read it through a pipe : " <<
commandStringBeforeAndAfter[U(aFileType)].first << " " << inputFileName << " " << commandStringBeforeAndAfter[U(aFileType)].second;
#endif
cout << endl;
}
}
}
fclose(tmp);
// Open the input file: it can be an archive file, in which case it is read as a pipe
// or a plain text file, which is read as a file
#ifndef SKIP_ZLIB
const auto useZlib = aFileType == TArchiveFileType::GZ || aFileType == TArchiveFileType::None;
FILE* f = useZlib ? (FILE*)gzopen(inputFileName.c_str(), "r") :
popen((commandStringBeforeAndAfter[U(aFileType)].first + " " + inputFileName + " " + commandStringBeforeAndAfter[U(aFileType)].second).c_str(), "r");
#else
const auto useFopen = aFileType == TArchiveFileType::None;
FILE* f = useFopen ? (FILE*)fopen(inputFileName.c_str(), "r") : popen((commandStringBeforeAndAfter[U(aFileType)].first + " " + inputFileName + " " + commandStringBeforeAndAfter[U(aFileType)].second).c_str(), "r");
#endif
if (f == nullptr)
{
cout << "c topor_tool ERROR: couldn't open the input file" << endl;
return BadRetVal;
}
CTopor<int32_t, uint32_t, false>* topor32 = nullptr;
CTopor<int32_t, uint64_t, false>* topor64 = nullptr;
CTopor<int32_t, uint64_t, true>* toporc = nullptr;
auto AllToporsNull = [&] { return topor32 == nullptr && topor64 == nullptr && toporc == nullptr; };
auto ToporSetParam = [&](const std::string& paramName, double newVal)
{
assert(!AllToporsNull());
topor32 ? topor32->SetParam(paramName, newVal) : topor64 ? topor64->SetParam(paramName, newVal) : toporc->SetParam(paramName, newVal);
};
auto ToporIsError = [&]()
{
assert(!AllToporsNull());
return topor32 ? topor32->IsError() : topor64 ? topor64->IsError() : toporc->IsError();
};
auto ToporGetStatusExplanation = [&]()
{
assert(!AllToporsNull());
return topor32 ? topor32->GetStatusExplanation() : topor64 ? topor64->GetStatusExplanation() : toporc->GetStatusExplanation();
};
auto ToporDumpDrat = [&](std::ofstream& openedDratFile, bool isDratBinary, bool dratSortEveryClause)
{
assert(!AllToporsNull());
topor32 ? topor32->DumpDrat(openedDratFile, isDratBinary, dratSortEveryClause) : topor64 ? topor64->DumpDrat(openedDratFile, isDratBinary, dratSortEveryClause) : toporc->DumpDrat(openedDratFile, isDratBinary, dratSortEveryClause);
};
auto ToporGetLitValue = [&](TLit l)
{
assert(!AllToporsNull());
return topor32 ? topor32->GetLitValue(l) : topor64 ? topor64->GetLitValue(l) : toporc->GetLitValue(l);
};
auto ToporClearUserPolarityInfo = [&](TLit v)
{
assert(!AllToporsNull());
topor32 ? topor32->ClearUserPolarityInfo(v) : topor64 ? topor64->ClearUserPolarityInfo(v) : toporc->ClearUserPolarityInfo(v);
};
auto ToporFixPolarity = [&](TLit l, bool onlyOnce = false)
{
assert(!AllToporsNull());
topor32 ? topor32->FixPolarity(l, onlyOnce) : topor64 ? topor64->FixPolarity(l, onlyOnce) : toporc->FixPolarity(l, onlyOnce);
};
auto ToporCreateInternalLit = [&](TLit v)
{
assert(!AllToporsNull());
topor32 ? topor32->CreateInternalLit(v) : topor64 ? topor64->CreateInternalLit(v) : toporc->CreateInternalLit(v);
};
auto ToporBoostScore = [&](TLit v, double value = 1.0)
{
assert(!AllToporsNull());
topor32 ? topor32->BoostScore(v, value) : topor64 ? topor64->BoostScore(v, value) : toporc->BoostScore(v, value);
};
auto ToporBacktrack = [&](TLit decLevel)
{
assert(!AllToporsNull());
topor32 ? topor32->Backtrack(decLevel) : topor64 ? topor64->Backtrack(decLevel) : toporc->Backtrack(decLevel);
};
auto ToporChangeConfigToGiven = [&](uint16_t configNum)
{
assert(!AllToporsNull());
return topor32 ? topor32->ChangeConfigToGiven(configNum) : topor64 ? topor64->ChangeConfigToGiven(configNum) : toporc->ChangeConfigToGiven(configNum);
};
auto ToporGetLitDecLevel = [&](TLit l)
{
assert(!AllToporsNull());
return topor32 ? topor32->GetLitDecLevel(l) : topor64 ? topor64->GetLitDecLevel(l) : toporc->GetLitDecLevel(l);
};
auto ToporSolve = [&](const std::span<TLit> assumps = {}, std::pair<double, bool> toInSecIsCpuTime = std::make_pair((std::numeric_limits<double>::max)(), true), uint64_t confThr = (std::numeric_limits<uint64_t>::max)())
{
assert(!AllToporsNull());
return topor32 ? topor32->Solve(assumps, toInSecIsCpuTime, confThr) : topor64 ? topor64->Solve(assumps, toInSecIsCpuTime, confThr) : toporc->Solve(assumps, toInSecIsCpuTime, confThr);
};
auto ToporAddClause = [&](const std::span<TLit> c)
{
assert(!AllToporsNull());
topor32 ? topor32->AddClause(c) : topor64 ? topor64->AddClause(c) : toporc->AddClause(c);
};
auto ToporGetSolveInvs = [&]()
{
assert(!AllToporsNull());
return topor32 ? topor32->GetSolveInvs() : topor64 ? topor64->GetSolveInvs() : toporc->GetSolveInvs();
};
auto ToporOnFinishedSolving = [&](TToporReturnVal ret, bool printModel, bool printUcore, const std::span<TLit> assumps, vector<TLit>& varsToPrint)
{
return topor32 ? OnFinishingSolving(*topor32, ret, printModel, printUcore, assumps, varsToPrint.empty() ? nullptr : &varsToPrint) : topor64 ? OnFinishingSolving(*topor64, ret, printModel, printUcore, assumps, varsToPrint.empty() ? nullptr : &varsToPrint) : OnFinishingSolving(*toporc, ret, printModel, printUcore, assumps, varsToPrint.empty() ? nullptr : &varsToPrint);
};
auto ToporIsAssumptionRequired = [&](size_t assumpInd)
{
assert(!AllToporsNull());
return topor32 ? topor32->IsAssumptionRequired(assumpInd) : topor64 ? topor64->IsAssumptionRequired(assumpInd) : toporc->IsAssumptionRequired(assumpInd);
};
TToporReturnVal ret = TToporReturnVal::RET_EXOTIC_ERROR;
ofstream dratFile;
CApplyFuncOnExitFromScope<> onExit([&]()
{
#ifndef SKIP_ZLIB
useZlib ? gzclose(gzFile(f)) : pclose(f);
#else
pclose(f);
#endif
if (dratName != "")
{
dratFile.close();
}
delete topor32;
delete topor64;
delete toporc;
});
/*
* File reading loop
*/
// Inside the loop, we expect to read either:
// c...: a comment
// p cnf vars clss: the header (must appear before clauses; handled as recommendation only)
// Lit1 Lit2 ... LitN 0: 0-ended clause
// "s Lit1 Lit2 ... LitN": solve under the given assumption
uint64_t lineNum = 1;
bool pLineRead = false;
int retValBasedOnLatestSolve = BadRetVal;
auto ReadCommaSeparatedVarList = [&](string& errMsg, const string& s)
{
vector<TLit> varList;
stringstream ss;
try
{
stringstream ss(s); //create string stream from the string
while (ss.good())
{
string substr;
// Get first string delimited by comma
getline(ss, substr, ',');
TLit l = (TLit)stoll(substr);
if (l <= 0)
{
ss << "c ERROR: couldn't convert " << s << " to a variable list, since " << l << " is not a positive variable" << endl;
}
varList.push_back(l);
}
}
catch (...)
{
ss << "c ERROR: couldn't convert " << s << " to a variable list" << endl;
}
errMsg = move(ss.str());
return varList;
};
auto CreateToporInst = [&](TLit varsNumHint = 0)
{
pLineRead = true;
auto CreateToporsIfRequired = [&]()
{
if (AllToporsNull())
{
if (type_indexing_and_compression == 2)
{
toporc = new CTopor<int32_t, uint64_t, true>(varsNumHint);
}
else if (type_indexing_and_compression == 1)
{
topor64 = new CTopor<int32_t, uint64_t, false>(varsNumHint);
}
else
{
topor32 = new CTopor<int32_t, uint32_t, false>(varsNumHint);
}
}
};
auto ParseParameters = [&]()
{
// Parse parameters
for (int currArgNum = 2; currArgNum < argc; currArgNum += 2)
{
const string paramNameStr = (string)argv[currArgNum];
const string paramValStr = (string)argv[currArgNum + 1];
auto ReadBoolParam = [&](string& errMsg)
{
int intVal = 0;
stringstream ss;
try
{
intVal = stoi(paramValStr);
}
catch (...)
{
ss << "c ERROR: couldn't convert " << paramValStr << " to an integer" << endl;
}
if (intVal != 0 && intVal != 1)
{
ss << "c ERROR: " << paramValStr << " must be 0 or 1" << endl;
}
errMsg = move(ss.str());
return (bool)intVal;
};
auto Read0to2Param = [&](string& errMsg)
{
int intVal = 0;
stringstream ss;
try
{
intVal = stoi(paramValStr);
}
catch (...)
{
ss << "c ERROR: couldn't convert " << paramValStr << " to an integer" << endl;
}
if (intVal != 0 && intVal != 1 && intVal != 2)
{
ss << "c ERROR: " << paramValStr << " must be 0 or 1 or 2" << endl;
}
errMsg = move(ss.str());
return (uint8_t)intVal;
};
auto ReadULongParam = [&](string& errMsg)
{
unsigned long ulVal = 0;
stringstream ss;
try
{
ulVal = stoul(paramValStr);
}
catch (...)
{
ss << "c ERROR: couldn't convert " << paramValStr << " to an unsigned long" << endl;
}
errMsg = move(ss.str());
return ulVal;
};
// /topor_tool/ prefix length
static constexpr size_t ttPrefixLen = 12;
if (paramNameStr.substr(0, ttPrefixLen) == "/topor_tool/")
{
const string param = paramNameStr.substr(ttPrefixLen, paramNameStr.size() - ttPrefixLen);
if (param == "bin_drat_file")
{
dratName = paramValStr;
isDratBinary = true;
cout << "c /topor_tool/bin_drat_file " << dratName << endl;
}
else if (param == "text_drat_file")
{
dratName = paramValStr;
isDratBinary = false;
cout << "c /topor_tool/text_drat_file " << dratName << endl;
}
else if (param == "drat_sort_every_clause")
{
cout << "c /topor_tool/drat_sort_every_clause " << paramValStr << endl;
string errMsg;
dratSortEveryClause = ReadBoolParam(errMsg);
if (!errMsg.empty())
{
cout << errMsg;
return true;
}
}
else if (param == "print_model")
{
cout << "c /topor_tool/print_model " << paramValStr << endl;
string errMsg;
printModel = ReadBoolParam(errMsg);
if (!errMsg.empty())
{
cout << errMsg;
return true;
}
}
else if (param == "print_ucore")
{
cout << "c /topor_tool/print_ucore " << paramValStr << endl;
string errMsg;
printUcore = ReadBoolParam(errMsg);
if (!errMsg.empty())
{
cout << errMsg;
return true;
}
}
else if (param == "verify_model")
{
cout << "c /topor_tool/verify_model " << paramValStr << endl;
string errMsg;
verifyModel = ReadBoolParam(errMsg);
if (!errMsg.empty())
{
cout << errMsg;
return true;
}
}
else if (param == "verify_ucore")
{
cout << "c /topor_tool/verify_ucore " << paramValStr << endl;
string errMsg;
verifyUcore = ReadBoolParam(errMsg);
if (!errMsg.empty())
{
cout << errMsg;
return true;
}
}
else if (param == "ignore_file_params")
{
cout << "c /topor_tool/ignore_file_params " << paramValStr << endl;
string errMsg;
ignoreFileParams = ReadBoolParam(errMsg);
if (!errMsg.empty())
{
cout << errMsg;
return true;
}
}
else if (param == "solver_mode")
{
cout << "c /topor_tool/solver_mode " << paramValStr << endl;
string errMsg;
type_indexing_and_compression = Read0to2Param(errMsg);
if (!errMsg.empty())
{
cout << errMsg;
return true;
}
if (!AllToporsNull())
{
cout << "c topor_tool ERROR: /topor_tool/solver_mode should be provided before any other parameters" << endl;
return true;
}
}
else if (param == "allsat_models_number")
{
cout << "c /topor_tool/allsat_models_number " << paramValStr << endl;
string errMsg;
allsatModels = ReadULongParam(errMsg);
if (!errMsg.empty())
{
cout << errMsg;
return true;
}
}
else if (param == "allsat_blocking_variables")
{
cout << "c /topor_tool/allsat_blocking_variables " << paramValStr << endl;
string errMsg;
blockingVars = ReadCommaSeparatedVarList(errMsg, paramValStr);
if (!errMsg.empty())
{
cout << errMsg;
return true;
}
}
else if (param == "allsat_blocking_variables_file_alg")
{
cout << "c /topor_tool/allsat_blocking_variables_file_alg " << paramValStr << endl;
string errMsg;
allsatBlockingFromInstanceAlg = ReadULongParam(errMsg);
if (!errMsg.empty())
{
cout << errMsg;
return true;
}
}
else
{
cout << "c ERROR: unrecognized /topor_tool/ parameter: " << paramNameStr << endl;
return true;
}
}
else
{
CreateToporsIfRequired();
if (AllToporsNull())
{
cout << "c topor_tool ERROR: couldn't create Topor instance" << endl;
return true;
}
auto [paramName, paramValue] = make_pair(paramNameStr, (double)0.0);
try
{
paramValue = std::stod(paramValStr);
}
catch (...)
{
cout << "c topor_tool ERROR: could not convert " << argv[currArgNum + 1] << " to double" << endl;
return true;
}
ToporSetParam(paramName, paramValue);
const bool isError = ToporIsError();
if (isError)
{
const string errorDescr = ToporGetStatusExplanation();
cout << "c ERROR in Topor parameter: " << errorDescr << endl;
return true;
}
}
}
CreateToporsIfRequired();
if (AllToporsNull())
{
cout << "c topor_tool ERROR: couldn't create Topor instance" << endl;
return true;
}
return false;
};
if (ParseParameters()) return BadRetVal;
if (dratName != "")
{
dratFile.open(dratName.c_str());
if (dratFile.bad())
{
cout << "c topor_tool ERROR: couldn't open DRAT file " << dratName << endl;
return BadRetVal;
}
ToporDumpDrat(dratFile, isDratBinary, dratSortEveryClause);
}
return 0;
};
// Populated if verify_model is on
vector<vector<TLit>> vmClss;
// Returns 10 upon success and BadRetVal upon failure
auto VerifyModel = [&](vector<TLit>* assumps = nullptr)
{
// Verify the model
cout << "c topor_tool: before verifying that the model satisfies " << (assumps == nullptr ? "the clauses" : "the assumptions and the clauses") << endl;
if (assumps != nullptr)
{
// Verify the assumptions first
for (TLit a : *assumps)
{
if (a != 0)
{
TToporLitVal v = ToporGetLitValue(a);
if (v != TToporLitVal::VAL_SATISFIED && v != TToporLitVal::VAL_DONT_CARE)
{
cout << "c ERROR: assumptions " << a << " is not satisfied!" << endl;
return BadRetVal;
}
}
}
cout << "c topor_tool: assumptions verified!" << endl;
}
for (vector<TLit>& cls : vmClss)
{
bool isVerified = false;
for (TLit l : cls)
{
if (l != 0)
{
TToporLitVal v = ToporGetLitValue(l);
if (v == TToporLitVal::VAL_SATISFIED || v == TToporLitVal::VAL_DONT_CARE)
{
isVerified = true;
break;
}
}
}
if (!isVerified)
{
cout << "c ERROR: the following clause is not satisfied:";
for (TLit l : cls)
{
cout << " " << l;
}
cout << endl;
return BadRetVal;
}
}
cout << "c topor_tool: clauses verified!" << endl;
return 10;
};
constexpr size_t maxSz = (size_t)1 << 28;
char* line = (char*)malloc(maxSz);
if (line == nullptr)
{
cout << "c topor_tool ERROR: couldn't allocate " + to_string(maxSz) + " bytes for reading the lines" << endl;
return BadRetVal;
}
auto ReadLine = [&](FILE* f, char* l, size_t maxChars)
{
#ifndef SKIP_ZLIB
return useZlib ? gzgets((gzFile)f, line, maxSz) : fgets(line, maxSz, f);
#else
return fgets(line, (int)maxSz, f);
#endif
};
pair<double, bool> nextSolveToInSecIsCpuTime = make_pair(numeric_limits<double>::max(), false);
uint64_t nextSolveConfThr = numeric_limits<uint64_t>::max();
TLit varsInPCnf = 0;
auto Solve = [&](vector<TLit>* assumpsPtr)
{
vector<TLit> assumpsEmpty;
ret = ToporSolve(assumpsPtr ? *assumpsPtr : assumpsEmpty, nextSolveToInSecIsCpuTime, nextSolveConfThr);
nextSolveToInSecIsCpuTime = make_pair(numeric_limits<double>::max(), false);
nextSolveConfThr = numeric_limits<uint64_t>::max();
retValBasedOnLatestSolve = AllToporsNull() ? BadRetVal :
topor32 ? OnFinishingSolving(*topor32, ret, printModel, printUcore, assumpsPtr ? *assumpsPtr : assumpsEmpty) : topor64 ? OnFinishingSolving(*topor64, ret, printModel, printUcore, assumpsPtr ? *assumpsPtr : assumpsEmpty) : OnFinishingSolving(*toporc, ret, printModel, printUcore, assumpsPtr ? *assumpsPtr : assumpsEmpty);
if (verifyModel && retValBasedOnLatestSolve == 10)
{
if (VerifyModel(assumpsPtr) == BadRetVal) return BadRetVal;
}
if (verifyUcore && retValBasedOnLatestSolve == 20)
{
vector<TLit> ucAssumps;
for (unsigned i = 0; i < assumpsPtr->size() && (*assumpsPtr)[i] != 0; ++i)
{
cout << "Assumption #" << to_string(i) << " -- " << (*assumpsPtr)[i] << " : " << ToporIsAssumptionRequired(i) << endl;
if (ToporIsAssumptionRequired(i))
{
ucAssumps.emplace_back((*assumpsPtr)[i]);
}
}
ret = ToporSolve(ucAssumps, nextSolveToInSecIsCpuTime, nextSolveConfThr);
retValBasedOnLatestSolve = AllToporsNull() ? BadRetVal :
topor32 ? OnFinishingSolving(*topor32, ret, printModel, printUcore, assumpsPtr ? *assumpsPtr : assumpsEmpty) : topor64 ? OnFinishingSolving(*topor64, ret, printModel, printUcore, assumpsPtr ? *assumpsPtr : assumpsEmpty) : OnFinishingSolving(*toporc, ret, printModel, printUcore, assumpsPtr ? *assumpsPtr : assumpsEmpty);
if (retValBasedOnLatestSolve != 20)
{
cout << "ret == " << to_string(retValBasedOnLatestSolve) << ": UNSAT CORE BUG!!!!!\n";
return BadRetVal;
}
}
return retValBasedOnLatestSolve;
};
while (ReadLine(f, line, maxSz) != nullptr)
{
const size_t len = strlen(line);
CApplyFuncOnExitFromScope<> beforeNextLoop([&]() { ++lineNum; });
size_t currLineI = 0;
auto SkipWhitespaces = [&]()
{
while (line[currLineI] == ' ' && currLineI < len)
{
++currLineI;
}
};
SkipWhitespaces();
if (currLineI >= len)
{
// Empty line
continue;
}
if (line[currLineI] == 'c')
{
// A comment
if (allsatModels > 1 && allsatBlockingFromInstanceAlg > 0 && blockingVars.empty())
{
string errMsg, blockingVarsStr;
blockingVarsStr.assign(line + 2, line + strlen(line));
blockingVars = ReadCommaSeparatedVarList(errMsg, blockingVarsStr);
if (!errMsg.empty() || blockingVars.empty())
{
throw logic_error("c topor_tool ERROR: expected the first comment to contain blocking variables at line number " + to_string(lineNum) + ". Error message: " + errMsg);
}
if (allsatBlockingFromInstanceAlg == 1)
{
for (TLit l : blockingVars)
{
ToporCreateInternalLit(l);
}
}
if (allsatBlockingFromInstanceAlg == 2)
{
unordered_set<TLit> blockingVarsSet(blockingVars.begin(), blockingVars.end());
for (TLit l = 1; l < varsInPCnf; ++l)
{
if (blockingVarsSet.find(l) == blockingVarsSet.end())
{
ToporCreateInternalLit(l);
}
}
}
}
continue;
}
if (line[currLineI] == 'r')
{
if (!ignoreFileParams)
{
string lStr = line;
const size_t paramNameStart = 2;
const size_t paramNameEnd = lStr.find(' ', 2);
if (paramNameEnd == string::npos)
{
throw logic_error("c topor_tool ERROR: expected <paramName> never ended at line number " + to_string(lineNum));
}
const string paramName = lStr.substr(paramNameStart, paramNameEnd - paramNameStart);
const string paramVal = lStr.substr(paramNameEnd + 1);
double paramValDouble = numeric_limits<double>::infinity();
try
{
paramValDouble = stod(paramVal);
}
catch (...)
{