-
Notifications
You must be signed in to change notification settings - Fork 1
/
yals.c
3896 lines (3434 loc) · 112 KB
/
yals.c
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 2021 xnfSAT Authors */
/*-------------------------------------------------------------------------*/
#include "yals.h"
#include <stdbool.h>
/*------------------------------------------------------------------------*/
#define YALSINTERNAL
#include "yils.h"
/*------------------------------------------------------------------------*/
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <stdlib.h>
#if defined(__linux__)
#include <fpu_control.h> // Set FPU to double precision on Linux.
#endif
/*------------------------------------------------------------------------*/
#define YALS_INT64_MAX (0x7fffffffffffffffll)
#define YALS_DEFAULT_PREFIX "c "
/*------------------------------------------------------------------------*/
#define NEWN(P,N) \
do { (P) = yals_malloc (yals, (N)*sizeof *(P)); } while (0)
#define DELN(P,N) \
do { yals_free (yals, (P), (N)*sizeof *(P)); } while (0)
#define CLEARN(P,N) \
do { memset((P), 0, (N)*sizeof *(P)); } while (0)
#define RSZ(P,O,N) \
do { \
(P) = yals_realloc (yals, (P), (O)*sizeof *(P), (N)*sizeof *(P)); \
} while (0)
/*------------------------------------------------------------------------*/
#define STACK(T) \
struct { T * start; T * top; T * end; }
#define INIT(S) \
do { (S).start = (S).top = (S).end = 0; } while (0)
#define SIZE(S) \
((S).end - (S).start)
#define COUNT(S) \
((S).top - (S).start)
#define EMPTY(S) \
((S).top == (S).start)
#define FULL(S) \
((S).top == (S).end)
#define CLEAR(S) \
do { (S).top = (S).start; } while (0)
#define ENLARGE(S) \
do { \
size_t OS = SIZE (S); \
size_t OC = COUNT (S); \
size_t NS = OS ? 2*OS : 1; \
assert (OC <= OS); \
RSZ ((S).start, OS, NS); \
(S).top = (S).start + OC; \
(S).end = (S).start + NS; \
} while (0)
#define FIT(S) \
do { \
size_t OS = SIZE (S); \
size_t NS = COUNT (S); \
RSZ ((S).start, OS, NS); \
(S).top = (S).start + NS; \
(S).end = (S).start + NS; \
} while (0)
#define RESET(S,N) \
do { \
assert ((N) <= SIZE (S) ); \
(S).top = (S).start + (N); \
} while (0)
#define PUSH(S,E) \
do { \
if (FULL(S)) ENLARGE (S); \
*(S).top++ = (E); \
} while (0)
#define POP(S) \
(assert (!EMPTY (S)), *--(S).top)
#define TOP(S) \
(assert (!EMPTY (S)), (S).top[-1])
#define PEEK(S,P) \
(assert ((P) < COUNT(S)), (S).start[(P)])
#define POKE(S,P,E) \
do { assert ((P) < COUNT(S)); (S).start[(P)] = (E); } while (0)
#define RELEASE(S) \
do { \
size_t N = SIZE (S); \
DELN ((S).start, N); \
INIT (S); \
} while (0)
/*------------------------------------------------------------------------*/
typedef unsigned Word;
#define LD_BITS_PER_WORD 5
#define BITS_PER_WORD (8*sizeof (Word))
#define BITMAPMASK (BITS_PER_WORD - 1)
#define WORD(BITS,N,IDX) \
((BITS)[ \
assert ((IDX) >= 0), \
assert (((IDX) >> LD_BITS_PER_WORD) < (N)), \
((IDX) >> LD_BITS_PER_WORD)])
#define BIT(IDX) \
(((Word)1u) << ((IDX) & BITMAPMASK))
#define GETBIT(BITS,N,IDX) \
(WORD(BITS,N,IDX) & BIT(IDX))
#define SETBIT(BITS,N,IDX) \
do { WORD(BITS,N,IDX) |= BIT(IDX); } while (0)
#define CLRBIT(BITS,N,IDX) \
do { WORD(BITS,N,IDX) &= ~BIT(IDX); } while (0)
#define NOTBIT(BITS,N,IDX) \
do { WORD(BITS,N,IDX) ^= BIT(IDX); } while (0)
/*------------------------------------------------------------------------*/
#define MIN(A,B) (((A) < (B)) ? (A) : (B))
#define MAX(A,B) (((A) > (B)) ? (A) : (B))
#define ABS(A) (((A) < 0) ? (assert ((A) != INT_MIN), -(A)) : (A))
#define SWAP(T,A,B) \
do { T TMP = (A); (A) = (B); (B) = (TMP); } while (0)
/*------------------------------------------------------------------------*/
#ifndef NDEBUG
#define LOG(ARGS...) \
do { \
if (!yals->opts.logging.val) break; \
yals_log_start (yals, ##ARGS); \
yals_log_end (yals); \
} while (0)
#define LOGLITS(LITS,ARGS...) \
do { \
const int * P; \
if (!yals->opts.logging.val) break; \
yals_log_start (yals, ##ARGS); \
fprintf (yals->out, " clause :"); \
for (P = (LITS); *P; P++) \
fprintf (yals->out, " %d", *P); \
yals_log_end (yals); \
} while (0)
#define LOGCIDX(CIDX,ARGS...) \
do { \
const int * LITS; \
if ((CIDX) >= yals->nclauses) \
LITS = yals_xlits(yals, (CIDX) - yals->nclauses); \
else \
LITS = yals_lits (yals, (CIDX)); \
const int * P; \
if (!yals->opts.logging.val) break; \
yals_log_start (yals, ##ARGS); \
if ((CIDX) >= yals->nclauses) \
fprintf (yals->out, " XOR clause %d :", (CIDX) - yals->nclauses); \
else \
fprintf (yals->out, " OR clause %d :", (CIDX)); \
for (P = (LITS); *P; P++) \
fprintf (yals->out, " %d", *P); \
yals_log_end (yals); \
} while (0)
#else
#define LOG(ARGS...) do { } while (0)
#define LOGLITS(ARGS...) do { } while (0)
#define LOGCIDX(ARGS...) do { } while (0)
#endif
/*------------------------------------------------------------------------*/
#define LENSHIFT 6
#define MAXLEN ((1<<LENSHIFT)-1)
#define LENMASK MAXLEN
/*------------------------------------------------------------------------*/
enum ClausePicking {
PSEUDO_BFS_CLAUSE_PICKING = -1,
RANDOM_CLAUSE_PICKING = 0,
BFS_CLAUSE_PICKING = 1,
DFS_CLAUSE_PICKING = 2,
RELAXED_BFS_CLAUSE_PICKING = 3,
UNFAIR_BFS_CLAUSE_PICKING = 4,
};
/*------------------------------------------------------------------------*/
#define OPTSTEMPLATE \
OPT (best,0,0,1,"always pick best assignment during restart"); \
OPT (breakzero,0,0,1,"always use break zero literal if possible"); \
OPT (cached,1,0,1,"use cached assignment during restart"); \
OPT (cachedinv,0,0,1,"use inverse minimum for selecting cached assignments"); \
OPT (cacheduni,0,0,1,"pick random cached assignment uniformly"); \
OPT (cachemax,(1<<10),0,(1<<20),"max cache size of saved assignments"); \
OPT (cachemin,1,0,(1<<10),"minimum cache size of saved assignments"); \
OPT (cb,250,50,2000,"the break parameter c_b, times a 100 (range 0.5-20.0)"); \
OPT (correct,1,0,1,"correct CB value depending on maximum length"); \
OPT (crit,1,0,1,"dynamic break values (using critical lits)"); \
OPT (defrag,1,0,1,"defragemtation of unsat queue"); \
OPT (fixed,4,0,INT_MAX,"fixed default strategy frequency (1=always)"); \
OPT (geomfreq,66,0,100,"geometric picking first frequency (percent)"); \
OPT (hitlim,-1,-1,INT_MAX,"minimum hit limit"); \
OPT (keep,0,0,1,"keep assignment during restart"); \
OPT (minchunksize,(1<<8),2,(1<<20),"minium queue chunk size"); \
OPT (pick,4,-1,4,"-1=pbfs,0=rnd,1=bfs,2=dfs,3=rbfs,4=ubfs"); \
OPT (pol,-1,-1,1,"negative=-1 positive=1 or random=0 polarity"); \
OPT (prep,1,0,1,"preprocessing through unit propagation"); \
OPT (rbfsrate,10,1,INT_MAX,"relaxed BFS rate"); \
OPT (reluctant,1,0,1,"reluctant doubling of restart interval"); \
OPT (restart,100000,0,INT_MAX,"basic (inner) restart interval"); \
OPT (restartouter,0,0,1,"enable restart outer"); \
OPT (restartouterfactor,100,1,INT_MAX,"outer restart interval factor"); \
OPT (setfpu,1,0,1,"set FPU to use double precision on Linux"); \
OPT (termint,1000,0,INT_MAX,"termination call back check interval"); \
OPT (toggleuniform,0,0,1,"toggle uniform strategy"); \
OPT (unfairfreq,50,0,100,"unfair picking first frequency (percent)"); \
OPT (uni,-1,-1,1,"weighted=0,uni=1,antiuni=-1 clause weights"); \
OPT (unipick,-1,-1,4,"clause picking strategy for uniform formulas"); \
OPT (unirestarts,0,0,INT_MAX,"max number restarts for uniform formulas"); \
OPT (verbose,0,0,2,"set verbose level"); \
OPT (weight,5,1,7,"[DISABLED] maximum clause weight"); \
OPT (weight2,200,0,1000,"clause weight times 100 for clause len == 2"); \
OPT (weight3,300,0,1000,"clause weight times 100 for clause len == 3"); \
OPT (weight4,300,0,1000,"clause weight times 100 for clause len == 4"); \
OPT (weight5,300,0,1000,"clause weight times 100 for clause len == 5"); \
OPT (weight6,300,0,1000,"clause weight times 100 for clause len == 6"); \
OPT (weight7,300,0,1000,"clause weight times 100 for clause len == 7"); \
OPT (weight8,300,0,1000,"clause weight times 100 for clause len >= 8"); \
OPT (witness,0,0,1,"print witness"); \
OPT (xorweight,500,0,1000,"constant weight of XOR clauses for weighted break, times a 100 (range 0.0-10.0)"); \
OPT (maxorigvar,0,0,INT_MAX,"index (inclusive) of maximum original (non-auxilliary) variable in CNF-encoded XNF"); \
OPTSTEMPLATENDEBUG
#ifndef NDEBUG
#define OPTSTEMPLATENDEBUG \
OPT (logging, 0, 0, 1, "set logging level"); \
OPT (checking, 0, 0, 1, "set checking level");
#else
#define OPTSTEMPLATENDEBUG
#endif
#define OPT(NAME,DEFAULT,MIN,MAX,DESCRIPTION) Opt NAME
/*------------------------------------------------------------------------*/
#define STRATSTEMPLATE \
STRAT (cached,0); \
STRAT (correct,0); \
STRAT (pol,0); \
STRAT (uni,0); \
STRAT (weight,0);
#define STRAT(NAME,ENABLED) int NAME
/*------------------------------------------------------------------------*/
#ifndef NYALSMEMS
#define ADD(NAME,NUM) \
do { \
yals->stats.mems.all += (NUM); \
yals->stats.mems.NAME += (NUM); \
} while (0)
#else
#define ADD(NAME,NUM) do { } while (0)
#endif
#define INC(NAME) ADD (NAME, 1)
/*------------------------------------------------------------------------*/
#define assert_valid_occs(OCCS) \
do { assert (0 <= OCCS), assert (OCCS < yals->noccs); } while (0)
#define assert_valid_idx(IDX) \
do { assert (0 <= IDX), assert (IDX < yals->nvars); } while (0)
#define assert_valid_cidx(CIDX) \
do { assert (0 <= CIDX), assert (CIDX < yals->nclauses); } while (0)
#define assert_valid_xcidx(CIDX) \
do { assert (0 <= CIDX), assert (CIDX < yals->nxclauses); } while (0)
#define assert_valid_len(LEN) \
do { assert (0 <= LEN), assert (LEN <= MAXLEN); } while (0)
#define assert_valid_pos(POS) \
do { \
assert (0 <= POS), assert (POS < COUNT (yals->unsat.stack)); \
} while (0)
/*------------------------------------------------------------------------*/
typedef struct RDS { unsigned u, v; } RDS;
typedef struct RNG { unsigned z, w; } RNG;
typedef struct Mem {
void * mgr;
YalsMalloc malloc;
YalsRealloc realloc;
YalsFree free;
} Mem;
typedef struct Strat { STRATSTEMPLATE } Strat;
typedef struct Stats {
int best, worst, last, tmp, maxstacksize;
int64_t flips, bzflips, hits, unsum;
struct {
struct { int64_t count; } outer;
struct { int64_t count, maxint; } inner;
} restart;
struct { struct { int chunks, lnks; } max; int64_t unfair; } queue;
struct { int64_t inserted, replaced, skipped; } cache;
struct { int64_t search, neg, falsepos, truepos; } sig;
struct { int64_t def, rnd; } strat;
struct { int64_t best, cached, keep, pos, neg, rnd; } pick;
struct { int64_t count, moved; } defrag;
struct { size_t current, max; } allocated;
struct { volatile double total, defrag, restart, entered; } time;
#ifdef __GNUC__
volatile int flushing_time;
#endif
#ifndef NYALSMEMS
struct { long long all, crit, lits, occs, read, update, weight; } mems;
#endif
#ifndef NYALSTATS
int64_t * inc, * dec, broken, made; int nincdec;
struct { unsigned min, max; } wb;
#endif
} Stats;
typedef struct Limits {
#ifndef NYALSMEMS
int64_t mems;
#endif
int64_t flips;
struct {
struct { int64_t lim, interval; } outer;
struct { int64_t lim; union { int64_t interval; RDS rds; }; } inner;
} restart;
struct { int min; } report;
int term;
} Limits;
typedef struct Lnk {
int cidx;
struct Lnk * prev, * next;
} Lnk;
typedef union Chunk {
struct { int size; union Chunk * next; };
Lnk lnks[1]; // actually of 'size'
} Chunk;
typedef struct Queue {
int count, chunksize, nchunks, nlnks, nfree;
Lnk * first, * last, * free;
Chunk * chunks;
} Queue;
typedef struct Exp {
struct { STACK(double) two, cb; } table;
struct { unsigned two, cb; } max;
struct { double two, cb; } eps;
} Exp;
typedef struct Opt { int val, def, min, max; } Opt;
typedef struct Opts { char * prefix; OPTSTEMPLATE } Opts;
typedef struct Callbacks {
double (*time)(void);
struct { void * state; int (*fun)(void*); } term;
struct { void * state; void (*lock)(void*); void (*unlock)(void*); } msg;
} Callbacks;
typedef unsigned char U1;
typedef unsigned short U2;
typedef unsigned int U4;
typedef struct FPU {
#ifdef __linux__
fpu_control_t control;
#endif
int saved;
} FPU;
struct Yals {
RNG rng;
FILE * out;
struct { int usequeue; Queue queue; STACK(int) stack; } unsat;
int nvars;
// Stores offsets into `occs` of literals. See `yals_(x)refs`
int * refs;
// Number of times each var is flipped
int64_t * flips;
STACK(signed char) mark;
// Whether the current parsing clause is trivial (contains x and -x)
int trivial;
// Whether the formula is false (contains empty clause)
int mt;
// During parsing of a XOR clause, its parity. Unused elsewhere
int xorParity;
int uniform, pick;
// The working assignment, a bitfield
Word * vals;
Word * best, * tmp;
// Parts of the assignment forced by unit propagation. See `yals_set_units`
// `clear` has 0s for vars which must be false
Word * clear;
// `set` has 1s for vars which must be true
Word * set;
// Number of machine words required to store the assignment
int nvarwords;
// Clause database. Effectively just the parsed DIMACS input
STACK(int) cdb;
// XOR clause database. Like `cdb`, but only variable indices are stored - not literals
STACK(int) xcdb;
// PEEK(xparitydb, cidx) is the parity of the cidx-th XOR clause. That is,
// - 0 iff the clause is true when an odd number of *variables* in it are
// - 1 iff even number
// TODO(WN): bitfield would do
STACK(int) xparitydb;
// Trail includes all the unit clauses
STACK(int) trail;
STACK(int) phases;
// During parsing, the clause being parsed. Not used elsewhere
STACK(int) clause;
// During parsing, whether the current clause is OR (0) or XOR (1)
int isXor;
STACK(int) mins;
// Bytes needed per-OR-clause to count satisfied lits.
int satcntbytes;
// For the cidx-th OR clause, satcntN[cidx] is the number of SAT literals in it
union { U1 * satcnt1; U2 * satcnt2; U4 * satcnt4; };
// For the cidx-th XOR clause, xorsat[cidx] is 1 iff that clause is SAT
// TODO(WN): bitfield would do
U1 * xorsat;
// Variable occurrences in clauses. See `yals_(x)occs`
// Format:
// flatten([ [ (clause idx << MAXLEN | clause length) for OR clauses in which `x` occurs ] ++ [ -1 ] ++
// [ (clause idx << MAXLEN | clause length) for OR clauses in which `-x` occurs ] ++ [ -1 ] ++
// [ (clause idx << MAXLEN | clause length) for XOR clauses in which `x` occurs ] ++ [ -1 ] for x in variables ])
int * occs;
// Number of total occurrences of literals in OR clauses
int noccs;
// Number of total occurrences of literals in XOR clauses
int nxoccs;
float * weights;
float xorweight;
int * pos;
// lits[n] is the position in cdb of the n-th OR clause
int * lits;
// xlits[n] is the position in xcdb of the n-th XOR clause
int * xlits;
Lnk ** lnk;
int * crit; float * weightedbreak;
float * xweightedbreak;
// Number of OR clauses
int nclauses;
// Number of XOR clauses
int nxclauses;
// Number of binary/ternary OR clauses
int nbin, ntrn;
// Min/max lengths of any OR clause
int minlen, maxlen;
double avglen;
// These three fields are used in `yals_pick_literal`
// How many clauses flipping a literal would break
STACK(float) breaks;
STACK(double) scores;
STACK(int) cands;
STACK(Word*) cache; int cachesizetarget; STACK(Word) sigs;
STACK(int) minlits;
Callbacks cbs;
Limits limits;
Strat strat;
Stats stats;
Opts opts;
Mem mem;
FPU fpu;
Exp exp;
};
/*------------------------------------------------------------------------*/
const char * yals_default_prefix () { return YALS_DEFAULT_PREFIX; }
/*------------------------------------------------------------------------*/
static void yals_msglock (Yals * yals) {
if (yals->cbs.msg.lock) yals->cbs.msg.lock (yals->cbs.msg.state);
}
static void yals_msgunlock (Yals * yals) {
if (yals->cbs.msg.unlock) yals->cbs.msg.unlock (yals->cbs.msg.state);
}
void yals_abort (Yals * yals, const char * fmt, ...) {
va_list ap;
yals_msglock (yals);
fflush (yals->out);
fprintf (stderr, "%s*** libyals abort: ", yals->opts.prefix);
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fputc ('\n', stderr);
fflush (stderr);
yals_msgunlock (yals);
abort ();
}
void yals_exit (Yals * yals, int exit_code, const char * fmt, ...) {
va_list ap;
yals_msglock (yals);
fflush (yals->out);
fprintf (stderr, "%s*** libyals exit: ", yals->opts.prefix);
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fputc ('\n', stderr);
fflush (stderr);
yals_msgunlock (yals);
exit (exit_code);
}
void yals_warn (Yals * yals, const char * fmt, ...) {
va_list ap;
yals_msglock (yals);
fprintf (yals->out, "%sWARNING ", yals->opts.prefix);
va_start (ap, fmt);
vfprintf (yals->out, fmt, ap);
va_end (ap);
fputc ('\n', yals->out);
fflush (yals->out);
yals_msgunlock (yals);
}
void yals_msg (Yals * yals, int level, const char * fmt, ...) {
va_list ap;
if (level > 0 && (!yals || yals->opts.verbose.val < level)) return;
yals_msglock (yals);
fprintf (yals->out, "%s", yals->opts.prefix);
va_start (ap, fmt);
vfprintf (yals->out, fmt, ap);
va_end (ap);
fputc ('\n', yals->out);
fflush (yals->out);
yals_msgunlock (yals);
}
/*------------------------------------------------------------------------*/
static void yals_set_fpu (Yals * yals) {
#ifdef __linux__
fpu_control_t control;
_FPU_GETCW (yals->fpu.control);
control = yals->fpu.control;
control &= ~_FPU_EXTENDED;
control &= ~_FPU_SINGLE;
control |= _FPU_DOUBLE;
_FPU_SETCW (control);
yals_msg (yals, 1, "set FPU mode to use double precision");
#endif
yals->fpu.saved = 1;
}
static void yals_reset_fpu (Yals * yals) {
(void) yals;
assert (yals->fpu.saved);
#ifdef __linux__
_FPU_SETCW (yals->fpu.control);
yals_msg (yals, 1, "reset FPU to original double precision mode");
#endif
}
/*------------------------------------------------------------------------*/
#ifndef NDEBUG
static void yals_log_start (Yals * yals, const char * fmt, ...) {
va_list ap;
yals_msglock (yals);
assert (yals->opts.logging.val);
fputs ("c [LOGYALS] ", yals->out);
va_start (ap, fmt);
vfprintf (yals->out, fmt, ap);
va_end (ap);
}
static void yals_log_end (Yals * yals) {
(void) yals;
assert (yals->opts.logging.val);
fputc ('\n', yals->out);
fflush (yals->out);
yals_msgunlock (yals);
}
#endif
/*------------------------------------------------------------------------*/
static double yals_avg (double a, double b) { return b ? a/b : 0; }
static double yals_pct (double a, double b) { return b ? 100.0 * a / b : 0; }
double yals_process_time () {
struct rusage u;
double res;
if (getrusage (RUSAGE_SELF, &u)) return 0;
res = u.ru_utime.tv_sec + 1e-6 * u.ru_utime.tv_usec;
res += u.ru_stime.tv_sec + 1e-6 * u.ru_stime.tv_usec;
return res;
}
static double yals_time (Yals * yals) {
if (yals && yals->cbs.time) return yals->cbs.time ();
else return yals_process_time ();
}
static void yals_flush_time (Yals * yals) {
double time, entered;
#ifdef __GNUC__
int old;
// begin{atomic}
old = __sync_val_compare_and_swap (&yals->stats.flushing_time, 0, 42);
assert (old == 0 || old == 42);
if (old) return;
//
// TODO I still occasionally have way too large kflips/sec if interrupted
// and I do not know why? Either there is a bug in flushing or there is
// still a data race here and I did not apply this CAS sequence correctly.
//
#endif
time = yals_time (yals);
entered = yals->stats.time.entered;
yals->stats.time.entered = time;
assert (time >= entered);
time -= entered;
yals->stats.time.total += time;
#ifdef __GNUC__
old = __sync_val_compare_and_swap (&yals->stats.flushing_time, 42, 0);
assert (old == 42);
(void) old;
// end{atomic}
#endif
}
double yals_sec (Yals * yals) {
yals_flush_time (yals);
return yals->stats.time.total;
}
/*------------------------------------------------------------------------*/
static void yals_inc_allocated (Yals * yals, size_t bytes) {
yals->stats.allocated.current += bytes;
if (yals->stats.allocated.current > yals->stats.allocated.max)
yals->stats.allocated.max = yals->stats.allocated.current;
}
static void yals_dec_allocated (Yals * yals, size_t bytes) {
assert (yals->stats.allocated.current >= bytes);
yals->stats.allocated.current -= bytes;
}
void * yals_malloc (Yals * yals, size_t bytes) {
void * res;
if (!bytes) return 0;
res = yals->mem.malloc (yals->mem.mgr, bytes);
if (!res) yals_abort (yals, "out of memory in 'yals_malloc'");
yals_inc_allocated (yals, bytes);
memset (res, 0, bytes);
return res;
}
void yals_free (Yals * yals, void * ptr, size_t bytes) {
assert (!ptr == !bytes);
if (!ptr) return;
yals_dec_allocated (yals, bytes);
yals->mem.free (yals->mem.mgr, ptr, bytes);
}
void * yals_realloc (Yals * yals, void * ptr, size_t o, size_t n) {
char * res;
assert (!ptr == !o);
if (!n) { yals_free (yals, ptr, o); return 0; }
if (!o) return yals_malloc (yals, n);
yals_dec_allocated (yals, o);
res = yals->mem.realloc (yals->mem.mgr, ptr, o, n);
if (n && !res) yals_abort (yals, "out of memory in 'yals_realloc'");
yals_inc_allocated (yals, n);
if (n > o) memset (res + o, 0, n - o);
return res;
}
size_t yals_max_allocated (Yals * yals) {
return yals->stats.allocated.max;
}
/*------------------------------------------------------------------------*/
static char * yals_strdup (Yals * yals, const char * str) {
assert (str);
return strcpy (yals_malloc (yals, strlen (str) + 1), str);
}
static void yals_strdel (Yals * yals, char * str) {
assert (str);
yals_free (yals, str, strlen (str) + 1);
}
/*------------------------------------------------------------------------*/
static void yals_rds (RDS * r) {
if ((r->u & -r->u) == r->v) r->u++, r->v = 1; else r->v *= 2;
}
/*------------------------------------------------------------------------*/
void yals_srand (Yals * yals, unsigned long long seed) {
unsigned z = seed >> 32, w = seed;
if (!z) z = ~z;
if (!w) w = ~w;
yals->rng.z = z, yals->rng.w = w;
yals_msg (yals, 2, "setting random seed %llu", seed);
}
static unsigned yals_rand (Yals * yals) {
unsigned res;
yals->rng.z = 36969 * (yals->rng.z & 65535) + (yals->rng.z >> 16);
yals->rng.w = 18000 * (yals->rng.w & 65535) + (yals->rng.w >> 16);
res = (yals->rng.z << 16) + yals->rng.w;
return res;
}
static unsigned yals_rand_mod (Yals * yals, unsigned mod) {
unsigned res;
assert (mod >= 1);
if (mod <= 1) return 0;
res = yals_rand (yals);
res %= mod;
return res;
}
/*------------------------------------------------------------------------*/
// `*yals_refs(yals, lit)` is the offset of `lit` into `occs`
static int * yals_refs (Yals * yals, int lit) {
int idx = ABS (lit);
assert_valid_idx (idx);
assert (yals->refs);
return yals->refs + 3*idx + (lit < 0);
}
static int * yals_xrefs (Yals * yals, int idx) {
assert_valid_idx (idx);
assert (yals->refs);
return yals->refs + 3*idx + 2;
}
// OR clauses in which `lit` occurs
static int * yals_occs (Yals * yals, int lit) {
INC (occs);
int occs = *yals_refs (yals, lit);
assert_valid_occs (occs);
return yals->occs + occs;
}
// XOR clauses in which `idx` occurs
static int * yals_xoccs (Yals * yals, int idx) {
INC (occs);
int xoccs = *yals_xrefs (yals, idx);
assert_valid_occs (xoccs);
return yals->occs + xoccs;
}
static int yals_val (Yals * yals, int lit) {
int idx = ABS (lit), res = !GETBIT (yals->vals, yals->nvarwords, idx);
if (lit > 0) res = !res;
return res;
}
static int yals_best (Yals * yals, int lit) {
int idx = ABS (lit), res = !GETBIT (yals->best, yals->nvarwords, idx);
if (lit > 0) res = !res;
return res;
}
static float yals_weighted_break (Yals * yals, int lit) {
int idx = ABS (lit);
assert (yals->crit);
assert_valid_idx (idx);
return yals->weightedbreak[2*idx + (lit < 0)];
}
static float yals_xweighted_break (Yals * yals, int idx) {
assert (yals->crit);
assert_valid_idx (idx);
return yals->xweightedbreak[idx];
}
static void yals_inc_weighted_break (Yals * yals, int lit, float w) {
int idx = ABS (lit), pos;
assert (yals->crit);
assert_valid_idx (idx);
pos = 2*idx + (lit < 0);
yals->weightedbreak[pos] += w;
assert (yals->weightedbreak[pos] >= w);
INC (weight);
}
static void yals_inc_xweighted_break (Yals * yals, int idx, float w) {
assert (yals->crit);
assert_valid_idx (idx);
yals->xweightedbreak[idx] += w;
assert (yals->xweightedbreak[idx] >= w);
INC (weight);
}
static void yals_dec_weighted_break (Yals * yals, int lit, float w) {
int idx = ABS (lit), pos;
assert (yals->crit);
assert_valid_idx (idx);
pos = 2*idx + (lit < 0);
assert (yals->weightedbreak[pos] >= w);
yals->weightedbreak[pos] -= w;
INC (weight);
}
static void yals_dec_xweighted_break (Yals * yals, int idx, float w) {
assert (yals->crit);
assert_valid_idx (idx);
assert (yals->xweightedbreak[idx] >= w);
yals->xweightedbreak[idx] -= w;
INC (weight);
}
static unsigned yals_satcnt (Yals * yals, int cidx) {
assert_valid_cidx (cidx);
if (yals->satcntbytes == 1) return yals->satcnt1[cidx];
if (yals->satcntbytes == 2) return yals->satcnt2[cidx];
return yals->satcnt4[cidx];
}
static void yals_setsatcnt (Yals * yals, int cidx, unsigned satcnt) {
assert_valid_cidx (cidx);
if (yals->satcntbytes == 1) {
assert (satcnt < 256);
yals->satcnt1[cidx] = satcnt;
} else if (yals->satcntbytes == 2) {
assert (satcnt < 65536);
yals->satcnt2[cidx] = satcnt;
} else {
yals->satcnt4[cidx] = satcnt;
}
}
static unsigned yals_incsatcnt (Yals * yals, int cidx, int lit, int len) {
unsigned res;
assert_valid_cidx (cidx);
assert_valid_len (len);
if (yals->satcntbytes == 1) {
res = yals->satcnt1[cidx]++;
assert (yals->satcnt1[cidx]);
} else if (yals->satcntbytes == 2) {
res = yals->satcnt2[cidx]++;
assert (yals->satcnt2[cidx]);
} else {
res = yals->satcnt4[cidx]++;
assert (yals->satcnt4[cidx]);
}
#ifndef NYALSTATS
assert (res + 1 <= yals->maxlen);
yals->stats.inc[res]++;
#endif
if (yals->crit) {
if (res == 1) yals_dec_weighted_break (yals, yals->crit[cidx], yals->weights[len]); // TODO avoid mem on yals->weights if uniform weights
else if (!res) yals_inc_weighted_break (yals, lit, yals->weights[len]);
yals->crit[cidx] ^= lit;
assert (res || yals->crit[cidx] == lit);
}
return res;
}
static unsigned yals_decsatcnt (Yals * yals, int cidx, int lit, int len) {
unsigned res;
assert_valid_cidx (cidx);
assert_valid_len (len);
if (yals->satcntbytes == 1) {
assert (yals->satcnt1[cidx]);
res = --yals->satcnt1[cidx];
} else if (yals->satcntbytes == 2) {
assert (yals->satcnt2[cidx]);
res = --yals->satcnt2[cidx];
} else {
assert (yals->satcnt4[cidx]);
res = --yals->satcnt4[cidx];
}
#ifndef NYALSTATS
assert (res + 1 <= yals->maxlen);
yals->stats.dec[res + 1]++;
#endif
if (yals->crit) {
int other = yals->crit[cidx] ^ lit;
yals->crit[cidx] = other;
if (res == 1) yals_inc_weighted_break (yals, other, yals->weights[len]); // TODO avoid mem on yals->weights if uniform weights
else if (!res) yals_dec_weighted_break (yals, lit, yals->weights[len]);
assert (res || !yals->crit[cidx]);
}
return res;
}
static U1 yals_xorsat (Yals * yals, int cidx) {
assert_valid_xcidx(cidx);
return yals->xorsat[cidx];
}
static void yals_setxorsat (Yals * yals, int cidx, U1 xorsat) {
assert_valid_xcidx (cidx);
yals->xorsat[cidx] = xorsat;
}
static U1 yals_flipxorsat (Yals * yals, int cidx) {
assert_valid_xcidx (cidx);
yals->xorsat[cidx] ^= 1;
return yals->xorsat[cidx];
}
// Returns the literals for the cidx-th OR clause
static int * yals_lits (Yals * yals, int cidx) {
INC (lits);
assert_valid_cidx (cidx);
return yals->cdb.start + yals->lits[cidx];
}
// Returns the variable indices for the cidx-th XOR clause
static int * yals_xlits (Yals * yals, int cidx) {
INC (lits);
assert_valid_xcidx (cidx);
return yals->xcdb.start + yals->xlits[cidx];
}
/*------------------------------------------------------------------------*/
static void yals_report (Yals * yals, const char * fmt, ...) {
double t, f;
va_list ap;
assert (yals->opts.verbose.val);
yals_msglock (yals);
f = yals->stats.flips;
t = yals_sec (yals);
fprintf (yals->out, "%s", yals->opts.prefix);
va_start (ap, fmt);
vfprintf (yals->out, fmt, ap);
va_end (ap);
fprintf (yals->out,
" : best %d (tmp %d), kflips %.0f, %.2f sec, %.2f kflips/sec\n",
yals->stats.best, yals->stats.tmp, f/1e3, t, yals_avg (f/1e3, t));
fflush (yals->out);
yals_msgunlock (yals);
}