forked from arminbiere/lingeling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plingeling.c
1370 lines (1285 loc) · 42.9 KB
/
plingeling.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 2010-2020 Armin Biere Johannes Kepler University Linz Austria */
/*-------------------------------------------------------------------------*/
#include "lglib.h"
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <stdarg.h>
#include <signal.h>
#include <unistd.h>
#include <math.h>
/*------------------------------------------------------------------------*/
#define NWORKERS 8
#define MAXGB 12
#define NUNITS (1<<9)
/*------------------------------------------------------------------------*/
#if 0
static int memdbgcnt;
#define LOGMEM(TYPE,PTR,BYTES) \
do { \
fprintf (stderr, "%ld %10d " #TYPE " %ld\n", \
(long) PTR, memdbgcnt++, (long) BYTES); \
fflush (stderr); \
} while (0)
#else
#define LOGMEM(...) do { } while (0)
#endif
/*------------------------------------------------------------------------*/
#define NEW(PTR,NUM) \
do { \
size_t BYTES = (NUM) * sizeof *(PTR); \
if (!((PTR) = malloc (BYTES))) { die ("out of memory"); exit (1); } \
LOGMEM (a, (PTR), BYTES); \
memset ((PTR), 0, BYTES); \
incmem (BYTES); \
} while (0)
#define DEL(PTR,NUM) \
do { \
size_t BYTES = (NUM) * sizeof *(PTR); \
LOGMEM (d, (PTR), BYTES); \
decmem (BYTES); \
free (PTR); \
} while (0)
/*------------------------------------------------------------------------*/
typedef struct Cls { int wid, count, glue, lits[1]; } Cls;
typedef struct Worker {
LGL * lgl;
pthread_t thread;
int res, fixed;
int units[NUNITS], nunits;
int * cls, szcls;
long clsimported;
Cls * dead;
struct {
struct { int calls, produced, consumed; } units;
struct { int produced, consumed; } cls, eqs;
int produced, consumed;
} stats;
} Worker;
/*------------------------------------------------------------------------*/
static int verbose, plain, nounits, noeqs, nocls, gclim = 1, gclimset;
#ifndef NLGLOG
static int loglevel;
#endif
#ifndef NLGLYALSAT
static int forcelocs;
#endif
/*------------------------------------------------------------------------*/
static int nworkers, nconsumers, leavebehind, locs;
static int64_t memlimit, softmemlimit;
static Worker * workers;
static int nvars, nclauses;
static int * vals, * fixed, * repr;
struct { Cls ** start; long first, num, added, collected, size; } clauses;
static int nfixed, globalres, gcs;
static const char * name;
static int nworkers2;
struct { size_t max, current;} mem;
static int catchedsig;
static double start;
static FILE * file;
struct { int units, cls, eqs; } syncs;
static int done, termchks, units, eqs, flushed;
static pthread_mutex_t donemutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t msgmutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t fixedmutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t reprmutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t memutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t clsmutex = PTHREAD_MUTEX_INITIALIZER;
/*------------------------------------------------------------------------*/
static double currentime (void) {
double res = 0;
struct timeval tv;
if (!gettimeofday (&tv, 0)) res = 1e-6 * tv.tv_usec, res += tv.tv_sec;
return res;
}
static double getime () {
return currentime () - start;
}
static void msg (int wid, int level, const char * fmt, ...) {
va_list ap;
if (verbose < level) return;
pthread_mutex_lock (&msgmutex);
fflush (stderr);
if (wid < 0) printf ("c - "); else printf ("c %d ", wid);
printf ("W %6.1f ", getime ());
va_start (ap, fmt);
vfprintf (stdout, fmt, ap);
fputc ('\n', stdout);
fflush (stdout);
pthread_mutex_unlock (&msgmutex);
}
static void die (const char * fmt, ...) {
va_list ap;
fflush (stdout);
fputs ("c *** plingeling error: ", stderr);
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
fputc ('\n', stderr);
fflush (stderr);
exit (1);
}
static void warn (const char * fmt, ...) {
va_list ap;
fflush (stdout);
fputs ("c *** plingeling warning: ", stderr);
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
fputc ('\n', stderr);
fflush (stderr);
}
/*------------------------------------------------------------------------*/
static double percent (double a, double b) { return b ? (100 * a) / b : 0; }
static void stats (void) {
double real, process, mpps, cps, mb;
int64_t decs, confs, props;
int i, unitcalls;
Worker * w;
unitcalls = decs = confs = 0;
props = 0;
mb = mem.max / (double)(1<<20);
for (i = 0; i < nworkers; i++) {
w = workers + i;
if (!w->lgl) continue;
decs += lglgetdecs (w->lgl);
confs += lglgetconfs (w->lgl);
props += lglgetprops (w->lgl);
mb += lglmaxmb (w->lgl);
unitcalls += w->stats.units.calls;
}
real = getime ();
process = lglprocesstime ();
cps = real > 0 ? confs / real : 0;
mpps = real > 0 ? (props/1e6) / real : 0;
printf ("c %d termination checks\n", termchks);
printf ("c\n");
printf ("c units: %d found, %d publications, %d syncs, %d flushed\n",
units, unitcalls, syncs.units, flushed);
printf ("c clauses: %ld clauses added, %ld collected %.0f%%, %d gcs\n",
clauses.added,
clauses.collected, percent (clauses.collected, clauses.added),
gcs);
printf ("c equivalences: %d found, %d syncs\n", eqs, syncs.eqs);
printf ("c\n");
printf ("c %lld decisions, %lld conflicts, %.1f conflicts/sec\n",
(long long)decs, (long long)confs, cps);
printf ("c %lld0 propagations, %.1f megaprops/sec\n",
(long long)props, mpps);
printf ("c %.1f process time, %.0f%% utilization\n",
process, real > 0 ? (100.0 * process) / real / nworkers : 0.0);
printf ("c\n");
printf ("c %.1f seconds, %.1f MB\n", real, mb);
fflush (stdout);
}
/*------------------------------------------------------------------------*/
static void incmem (size_t bytes) {
if (pthread_mutex_lock (&memutex))
warn ("failed to lock 'mem' mutex in 'incmem'");
mem.current += bytes;
if (mem.current > mem.max) mem.max = mem.current;
if (pthread_mutex_unlock (&memutex))
warn ("failed to unlock 'mem' mutex in 'incmem'");
}
static void decmem (size_t bytes) {
if (pthread_mutex_lock (&memutex))
warn ("failed to lock 'mem' mutex in 'decmem'");
assert (mem.current >= bytes);
mem.current -= bytes;
if (pthread_mutex_unlock (&memutex))
warn ("failed to unlock 'mem' mutex in 'decmem'");
}
static void * alloc (void * dummy, size_t bytes) {
char * res;
NEW (res, bytes);
(void) dummy;
return res;
}
static void dealloc (void * dummy, void * void_ptr, size_t bytes) {
char * char_ptr = void_ptr;
LOGMEM (d, void_ptr, bytes);
DEL (char_ptr, bytes);
(void) dummy;
}
static void * resize (void * dummy, void * ptr,
size_t old_bytes, size_t new_bytes) {
void * res;
if (pthread_mutex_lock (&memutex))
warn ("failed to lock 'mem' mutex in 'resize'");
assert (mem.current >= old_bytes);
mem.current -= old_bytes;
mem.current += new_bytes;
if (mem.current > mem.max) mem.max = mem.current;
if (pthread_mutex_unlock (&memutex))
warn ("failed to unlock 'mem' mutex in 'resize'");
LOGMEM (d, ptr, old_bytes);
res = realloc (ptr, new_bytes);
LOGMEM (a, res, new_bytes);
(void) dummy;
return res;
}
/*------------------------------------------------------------------------*/
static void (*sig_int_handler)(int);
static void (*sig_segv_handler)(int);
static void (*sig_abrt_handler)(int);
static void (*sig_term_handler)(int);
static void resetsighandlers (void) {
(void) signal (SIGINT, sig_int_handler);
(void) signal (SIGSEGV, sig_segv_handler);
(void) signal (SIGABRT, sig_abrt_handler);
(void) signal (SIGTERM, sig_term_handler);
}
static void caughtsigmsg (int sig) {
if (!verbose) return;
printf ("c\nc CAUGHT SIGNAL %d\nc\n", sig);
fflush (stdout);
}
static void catchsig (int sig) {
if (!catchedsig) {
fputs ("c s UNKNOWN\n", stdout);
fflush (stdout);
catchedsig = 1;
caughtsigmsg (sig);
stats ();
caughtsigmsg (sig);
}
resetsighandlers ();
if (getenv ("LGLSLEEPONABORT")) {
const int sec = 1000;
fprintf (stderr,
"plingeling: plingeling.c:%d: "
"process %ld sleeping for %d seconds\n",
__LINE__, (long) getpid (), sec); fflush (stderr); sleep (sec);
}
if (!getenv ("LGLNABORT")) raise (sig); else exit (1);
}
static void setsighandlers (void) {
sig_int_handler = signal (SIGINT, catchsig);
// sig_segv_handler = signal (SIGSEGV, catchsig);
sig_abrt_handler = signal (SIGABRT, catchsig);
sig_term_handler = signal (SIGTERM, catchsig);
}
/*------------------------------------------------------------------------*/
static const char * parse (void) {
int ch, lit, sign, minlen, maxlen, len, * occs;
HEADER:
ch = getc (file);
if (ch == 'c') {
while ((ch = getc (file)) != '\n')
if (ch == EOF) return "EOF in comment";
goto HEADER;
}
if (ch != 'p') return "expected header or comment";
ungetc (ch, file);
if (fscanf (file, "p cnf %d %d", &nvars, &nclauses) != 2)
return "can not parse header";
msg (-1, 1, "p cnf %d %d", nvars, nclauses);
NEW (fixed, nvars + 1);
NEW (vals, nvars + 1);
NEW (occs, 2*nvars + 1);
occs += nvars;
if (!noeqs) NEW (repr, nvars + 1);
minlen = INT_MAX, maxlen = -1, len = 0;
LIT:
ch = getc (file);
if (ch == 'c') {
while ((ch = getc (file)) != '\n')
if (ch == EOF) return "EOF in comment";
goto LIT;
}
if (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t') goto LIT;
if (ch == EOF) {
double avg, std;
if (nclauses > 0) return "not enough clauses";
if (minlen == maxlen) msg (-1, 0, "uniform clause length %d", minlen);
else msg (-1, 0, "clause length between %d and %d", minlen, maxlen);
if (nvars > 0) {
avg = 0;
for (lit = -nvars; lit <= nvars; lit++)
if (lit) avg += occs[lit];
avg /= 2*nvars;
msg (-1, 0, "average literal occurrence %.2f", avg);
std = 0;
for (lit = -nvars; lit <= nvars; lit++)
if (lit) {
double diff = avg - (double) occs[lit];
std += diff*diff;
}
std = sqrt (std / (2*nvars));
msg (-1, 0, "literal occurrence standard deviation %.2f", std);
if (avg > 0)
msg (-1, 0,
"relative literal occurrence standard deviation %.2f%%",
100.0*std/avg);
} else avg = std = 0;
occs -= nvars;
DEL (occs, 2*nvars + 1);
if (avg > 5 && std/avg < 0.5) {
if (minlen == maxlen) {
locs = 2;
msg (-1, 0, "looks like a real uniform random instance");
} else {
locs = 1;
msg (-1, 0,
"looks like random instance without uniform clause length");
}
} else {
locs = 0;
msg (-1, 0, "instance does not seem to be uniform random");
}
msg (-1, 1, "finished parsing in %.1f seconds", getime ());
return 0;
}
if (ch == '-') {
ch = getc (file);
sign = -1;
} else sign = 1;
if (!isdigit (ch)) return "expected digit";
if (!nclauses) return "too many clauses";
lit = ch - '0';
while (isdigit (ch = getc (file)))
lit = 10 * lit + (ch - '0');
if (lit < 0 || lit > nvars) return "invalid variable index";
lit *= sign;
lgladd (workers[0].lgl, lit);
if (!lit) {
nclauses--;
if (len > maxlen) maxlen = len;
if (len < minlen) minlen = len;
len = 0;
} else len++, occs[lit]++;
goto LIT;
}
static int isposnum (const char * str) {
int ch;
if (!(ch = *str++) || !isdigit (ch)) return 0;
while (isdigit (ch = *str++))
;
return !ch;
}
static int term (void * voidptr) {
Worker * worker = voidptr;
int wid = worker - workers, res;
assert (0 <= wid && wid < nworkers);
msg (wid, 3, "checking early termination");
if (pthread_mutex_lock (&donemutex))
warn ("failed to lock 'done' mutex in termination check");
res = done;
termchks++;
if (pthread_mutex_unlock (&donemutex))
warn ("failed to unlock 'done' mutex in termination check");
msg (wid, 3, "early termination check %s", res ? "succeeded" : "failed");
return res;
}
static void flush (Worker * worker, int keep_locked) {
int wid = worker - workers;
int lit, idx, val, tmp, i;
assert (worker->nunits);
msg (wid, 2, "flushing %d units", worker->nunits);
if (pthread_mutex_lock (&fixedmutex))
warn ("failed to lock 'fixed' mutex in flush");
flushed++;
for (i = 0; i < worker->nunits; i++) {
lit = worker->units[i];
idx = abs (lit);
assert (1 <= idx && idx <= nvars);
assert (0 <= wid && wid < nworkers);
worker->stats.units.calls++;
val = (lit < 0) ? -1 : 1;
tmp = vals[idx];
if (!tmp) {
assert (nfixed < nvars);
fixed[nfixed++] = lit;
vals[idx] = val;
assert (!fixed[nfixed]);
worker->stats.units.produced++;
worker->stats.produced++;
units++;
} else if (tmp == -val) {
if (pthread_mutex_lock (&donemutex))
warn ("failed to lock 'done' mutex flushing unit");
if (!globalres) msg (wid, 1, "mismatched unit");
globalres = 20;
done = 1;
if (pthread_mutex_unlock (&donemutex))
warn ("failed to unlock 'done' mutex flushing unit");
break;
} else assert (tmp == val);
}
worker->nunits = 0;
if (keep_locked) return;
if (pthread_mutex_unlock (&fixedmutex))
warn ("failed to unlock 'fixed' mutex in flush");
}
static void produceunit (void * voidptr, int lit) {
Worker * worker = voidptr;
int wid = worker - workers;
assert (worker->nunits < NUNITS);
worker->units[worker->nunits++] = lit;
msg (wid, 3, "producing unit %d", lit);
if (worker->nunits == NUNITS) flush (worker, 0);
}
static void consumeunits (void * voidptr, int ** fromptr, int ** toptr) {
Worker * worker = voidptr;
int wid = worker - workers;
if (worker->nunits) flush (worker, 1);
else if (pthread_mutex_lock (&fixedmutex))
warn ("failed to lock 'fixed' mutex in consume");
msg (wid, 3, "starting unit synchronization");
syncs.units++;
*fromptr = fixed + worker->fixed;
*toptr = fixed + nfixed;
if (pthread_mutex_unlock (&fixedmutex))
warn ("failed to unlock 'fixed' in 'consumeunits'");
}
static void consumedunits (void * voidptr, int consumed) {
Worker * worker = voidptr;
int wid = worker - workers;
worker->stats.units.consumed += consumed;
worker->stats.consumed += consumed;
msg (wid, 3, "consuming %d units", consumed);
}
static void * resize (void*,void*,size_t,size_t);
static size_t sizecls (int len) { return sizeof (Cls) + len * sizeof (int); }
static int lencls (int * c) { int res = 0; while (*c++) res++; return res; }
static void producecls (void * voidptr, int * c, int glue) {
Worker * worker = voidptr;
int wid = worker - workers;
int len, * q, lit;
const int * p;
size_t bytes;
Cls * cls;
len = lencls (c);
bytes = sizecls (len);
cls = malloc (bytes);
if (!cls) { die ("out of memory in 'producecls'"); exit (1); }
LOGMEM (a, cls, bytes);
incmem (bytes);
cls->wid = wid;
cls->glue = glue;
cls->count = 0;
p = c, q = cls->lits;
while ((lit = *p++)) *q++ = lit;
*q++ = 0;
if (pthread_mutex_lock (&clsmutex))
warn ("failed to lock 'cls' mutex in 'producecls'");
msg (wid, 3, "producing glue %d length %d clause %ld",
glue, len, clauses.added);
if (clauses.num == clauses.size) {
int newsize = clauses.size ? 2*clauses.size : 1;
size_t old_bytes = clauses.size * sizeof *clauses.start;
size_t new_bytes = newsize * sizeof *clauses.start;
clauses.start = resize (0, clauses.start, old_bytes, new_bytes);
clauses.size = newsize;
}
clauses.start[clauses.num++] = cls;
clauses.added++;
if (pthread_mutex_unlock (&clsmutex))
warn ("failed to unlock 'cls' mutex in 'producecls'");
worker->stats.cls.produced++;
worker->stats.produced++;
}
static void deletecls (Cls * cls) {
int len = lencls (cls->lits);
size_t bytes = sizecls (len);
LOGMEM (d, cls, bytes);
decmem (bytes);
free (cls);
}
static void deleteallcls () {
Cls * c;
int i;
for (i = clauses.first; i < clauses.num; i++) {
c = clauses.start[i];
if (c) deletecls (c);
}
DEL (clauses.start, clauses.size);
}
static void gcls (void) {
long j, k, l;
Cls * c;
int i;
assert (0 <= clauses.first);
assert (clauses.first <= clauses.num);
gcs++;
msg (-1, 1, "garbage collecting clauses: first=%ld, num=%ld, SIZE=%ld",
clauses.first, clauses.num, clauses.size);
for (k = 0; k < clauses.first; k++) assert (!clauses.start[k]);
while (k < clauses.num) {
c = clauses.start[k];
if (c->count > gclim) break;
clauses.start[k++] = 0;
deletecls (c);
}
assert (clauses.first <= k);
for (i = 0; i < nworkers; i++) {
Worker * worker = workers + i;
if (k < worker->clsimported) worker->clsimported -= k;
else worker->clsimported = 0;
}
j = 0;
for (l = k; l < clauses.num; l++)
clauses.start[j++] = clauses.start[l];
clauses.collected += k;
clauses.first = 0;
clauses.num -= k;
assert (clauses.num >= 0);
}
static void consumecls (void * voidptr, int ** cptr, int * glueptr) {
Worker * worker = voidptr;
int wid = worker - workers;
int res, len, newsize;
Cls * cls;
if (pthread_mutex_lock (&clsmutex))
warn ("failed to lock 'cls' mutex in 'consumecls'");
assert (worker->clsimported <= clauses.num);
RESTART:
if (worker->dead) deletecls (worker->dead), worker->dead = 0;
if (worker->clsimported == clauses.num) {
msg (wid, 3, "all %d clauses already consumed", clauses.num);
*cptr = 0;
} else {
res = worker->clsimported++;
cls = clauses.start[res];
if (!cls) goto RESTART;
cls->count++;
if (cls->count + leavebehind >= nconsumers) {
clauses.start[res] = 0;
assert (!worker->dead);
worker->dead = cls;
assert (clauses.first <= res);
clauses.first++;
if (clauses.num > 10000 &&
clauses.first > clauses.num/(nconsumers + 1)) gcls ();
}
if (cls->wid == wid) goto RESTART;
len = lencls (cls->lits);
if (len + 1 >= worker->szcls) {
newsize = 2*(len + 1);
worker->cls = resize (0, worker->cls,
worker->szcls * sizeof (int),
newsize * sizeof (int));
worker->szcls = newsize;
}
memcpy (worker->cls, cls->lits, (len+1) * sizeof (int));
*cptr = worker->cls;
*glueptr = cls->glue;
msg (wid, 3,
"consuming glue %d length %d clause %d",
*glueptr, len, res);
}
if (pthread_mutex_unlock (&clsmutex))
warn ("failed to unlock 'cls' mutex in 'consumecls'");
}
static void consumedcls (void * voidptr, int consumed) {
Worker * worker = voidptr;
int wid = worker - workers;
worker->stats.cls.consumed += consumed;
worker->stats.consumed += consumed;
msg (wid, 3, "consuming %d clause", consumed);
}
static int * lockrepr (void * voidptr) {
Worker * worker = voidptr;
int wid = worker - workers;
if (pthread_mutex_lock (&reprmutex))
warn ("failed to lock 'repr' mutex");
msg (wid, 3, "starting equivalences synchronization");
syncs.eqs++;
return repr;
}
static void unlockrepr (void * voidptr, int consumed, int produced) {
Worker * worker = voidptr;
int wid = worker - workers;
msg (wid, 3,
"finished equivalences synchronization: %d consumed, %d produced",
consumed, produced);
worker->stats.eqs.consumed += consumed;
worker->stats.eqs.produced += produced;
worker->stats.consumed += consumed;
worker->stats.produced += produced;
eqs += produced;
assert (eqs < nvars);
if (pthread_mutex_unlock (&reprmutex))
warn ("failed to unlock 'repr' mutex");
}
static void msglock (void * voidptr) {
(void) voidptr;
pthread_mutex_lock (&msgmutex);
}
static void msgunlock (void * voidptr) {
(void) voidptr;
pthread_mutex_unlock (&msgmutex);
}
static void * work (void * voidptr) {
Worker * worker = voidptr;
int wid = worker - workers;
LGL * lgl = worker->lgl;
assert (0 <= wid && wid < nworkers);
msg (wid, 1, "running");
assert (workers <= worker && worker < workers + nworkers);
worker->res = lglsat (lgl);
msg (wid, 1, "result %d", worker->res);
if (!worker->res) return 0;
if (pthread_mutex_lock (&donemutex))
warn ("failed to lock 'done' mutex in worker");
done = 1;
if (pthread_mutex_unlock (&donemutex))
warn ("failed to unlock 'done' mutex in worker");
msg (wid, 2, "%d decisions, %d conflicts, %.0f props, %.1f MB",
lglgetdecs (lgl), lglgetconfs (lgl), lglgetprops (lgl), lglmb (lgl));
if (verbose >= 2) {
if (pthread_mutex_lock (&fixedmutex))
warn ("failed to lock 'fixed' in work");
msg (wid, 2, "consumed %d units %.0f%%, produced %d units %.0f%%",
worker->stats.units.consumed,
percent (worker->stats.units.consumed, nfixed),
worker->stats.units.produced,
percent (worker->stats.units.produced, nfixed));
if (pthread_mutex_unlock (&fixedmutex))
warn ("failed to unlock 'fixed' in work");
}
return worker;
}
static int64_t getsystemtotalmem (int explain) {
long long res;
FILE * p = popen ("grep MemTotal /proc/meminfo", "r");
if (p && fscanf (p, "MemTotal: %lld kB", &res) == 1) {
if (explain)
msg (-1, 0, "%lld KB total memory according to /proc/meminfo", res);
res <<= 10;
} else {
res = ((long long) MAXGB) << 30;;
if (explain)
msg (-1, 0, "assuming compiled in memory size of %d GB", MAXGB);
}
if (p) pclose (p);
return (int64_t) res;
}
static int getsystemcores (int explain) {
int syscores, coreids, physids, procpuinfocores;
int usesyscores, useprocpuinfo, amd, intel, res;
FILE * p;
syscores = sysconf (_SC_NPROCESSORS_ONLN);
if (explain) {
if (syscores > 0)
msg (-1, 1, "'sysconf' reports %d processors online", syscores);
else
msg (-1, 1, "'sysconf' fails to determine number of online processors");
}
p = popen ("grep '^core id' /proc/cpuinfo 2>/dev/null|sort|uniq|wc -l", "r");
if (p) {
if (fscanf (p, "%d", &coreids) != 1) coreids = 0;
if (explain) {
if (coreids > 0)
msg (-1, 1, "found %d unique core ids in '/proc/cpuinfo'", coreids);
else
msg (-1, 1, "failed to extract core ids from '/proc/cpuinfo'");
}
pclose (p);
} else coreids = 0;
p = popen (
"grep '^physical id' /proc/cpuinfo 2>/dev/null|sort|uniq|wc -l", "r");
if (p) {
if (fscanf (p, "%d", &physids) != 1) physids = 0;
if (explain) {
if (physids > 0)
msg (-1, 1, "found %d unique physical ids in '/proc/cpuinfo'",
physids);
else
msg (-1, 1, "failed to extract physical ids from '/proc/cpuinfo'");
}
pclose (p);
} else physids = 0;
if (coreids > 0 && physids > 0 &&
(procpuinfocores = coreids * physids) > 0) {
if (explain)
msg (-1, 1,
"%d cores = %d core times %d physical ids in '/proc/cpuinfo'",
procpuinfocores, coreids, physids);
} else procpuinfocores = 0;
usesyscores = useprocpuinfo = 0;
if (procpuinfocores > 0 && procpuinfocores == syscores) {
if (explain) msg (-1, 1, "'sysconf' and '/proc/cpuinfo' results match");
usesyscores = 1;
} else if (procpuinfocores > 0 && syscores <= 0) {
if (explain) msg (-1, 1, "only '/proc/cpuinfo' result valid");
useprocpuinfo = 1;
} else if (procpuinfocores <= 0 && syscores > 0) {
if (explain) msg (-1, 1, "only 'sysconf' result valid");
usesyscores = 1;
} else {
intel = !system ("grep vendor /proc/cpuinfo 2>/dev/null|grep -q Intel");
if (intel && explain)
msg (-1, 1, "found Intel as vendor in '/proc/cpuinfo'");
amd = !system ("grep vendor /proc/cpuinfo 2>/dev/null|grep -q AMD");
if (amd && explain)
msg (-1, 1, "found AMD as vendor in '/proc/cpuinfo'");
assert (syscores > 0);
assert (procpuinfocores > 0);
assert (syscores != procpuinfocores);
if (amd) {
if (explain) msg (-1, 1, "trusting 'sysconf' on AMD");
usesyscores = 1;
} else if (intel) {
if (explain) {
msg (-1, 1,
"'sysconf' result off by a factor of %f on Intel",
syscores / (double) procpuinfocores);
msg (-1, 1, "trusting '/proc/cpuinfo' on Intel");
}
useprocpuinfo = 1;
} else {
if (explain)
msg (-1, 1, "trusting 'sysconf' on unknown vendor machine");
usesyscores = 1;
}
}
if (useprocpuinfo) {
if (explain)
msg (-1, 0,
"assuming cores = core * physical ids in '/proc/cpuinfo' = %d",
procpuinfocores);
res = procpuinfocores;
} else if (usesyscores) {
if (explain)
msg (-1, 0,
"assuming cores = number of processors reported by 'sysconf' = %d",
syscores);
res = syscores;
} else {
if (explain)
msg (-1, 0, "using compiled in default value of %d workers", NWORKERS);
res = NWORKERS;
}
return res;
}
static int cmproduced (const void * p, const void * q) {
Worker * u = *(Worker**) p;
Worker * v = *(Worker**) q;
int res = v->stats.produced - u->stats.produced;
if (res) return res;
return u - v;
}
static int cmpconsumed (const void * p, const void * q) {
Worker * u = *(Worker**) p;
Worker * v = *(Worker**) q;
int res = v->stats.consumed - u->stats.consumed;
if (res) return res;
return u - v;
}
static int parsenbcoreenv (void) {
const char * str = getenv ("NBCORE");
if (!str) return 0;
if (!isposnum (str))
die ("invalid value '%s' for environment variable NBCORE", str);
return atoi (str);
}
static void setopt (int i, LGL * lgl, const char * opt, int val) {
int oldval, newval;
if (!lglhasopt (lgl, opt))
msg (i, 1, "option '%s' does not exist", opt);
else if ((oldval = lglgetopt (lgl, opt)) == val)
msg (i, 1, "option '%s' already set to '%d'", opt, val);
else {
lglsetopt (lgl, opt, val);
newval = lglgetopt (lgl, opt);
if (newval != val)
msg (i, 1, "option '%s' set to '%d' (but requested %d)",
opt, newval, val);
else
msg (i, 1, "option '%s' set to '%d'", opt, val);
}
}
static void setopts (LGL * lgl, int i) {
Worker * w = workers + i;
int j;
w->lgl = lgl;
lglsetid (lgl, i, nworkers);
lglsetime (lgl, getime);
setopt (i, lgl, "verbose", verbose);
#ifndef NLGLOG
if (!verbose)
setopt (i, lgl, "log", loglevel);
#endif
setopt (i, lgl, "seed", i);
setopt (i, lgl, "classify", 0);
if (i && (locs >= 2 || (locs && (i > 7 || (i & 1))))) {
setopt (i, lgl, "plain", locs == 2 || (i & 3) == 1);
setopt (i, lgl, "locs", -1);
setopt (i, lgl, "locsrtc", 1);
setopt (i, lgl, "locswait", 0);
setopt (i, lgl, "locsclim", (1<<24));
} else {
j = locs ? i/2 : i;
switch (j % 13) {
case 0: default: break;
case 1: setopt (i, lgl, "plain", 1),
setopt (i, lgl, "decompose", 1); break;
case 2: setopt (i, lgl, "restartint", 1000); break;
case 3: setopt (i, lgl, "elmresched", 7); break;
case 4: setopt (i, lgl, "scincincmin", 250); break;
case 5: setopt (i, lgl, "block", 0),
setopt (i, lgl, "cce", 0); break;
case 6: setopt (i, lgl, "scincinc", 50); break;
case 7: setopt (i, lgl, "phase", -1); break;
case 8: setopt (i, lgl, "phase", 1); break;
case 9: setopt (i, lgl, "sweeprtc", 1); break;
case 10: setopt (i, lgl, "restartint", 100); break;
case 11: setopt (i, lgl, "reduceinit", 10000);
setopt (i, lgl, "reducefixed", 1); break;
case 12: setopt (i, lgl, "restartint", 4); break;
}
}
lglseterm (lgl, term, w);
lglsetmsglock (lgl, msglock, msgunlock, w);
if (!nounits) {
lglsetproduceunit (lgl, produceunit, w);
lglsetconsumeunits (lgl, consumeunits, w);
lglsetconsumedunits (lgl, consumedunits, w);
}
if (!nocls) {
lglsetproducecls (lgl, producecls, w);
lglsetconsumecls (lgl, consumecls, w);
lglsetconsumedcls (lgl, consumedcls, w);
}
if (!noeqs) {
lglsetlockeq (lgl, lockrepr, w);
lglsetunlockeq (lgl, unlockrepr, w);
}
msg (i, 2, "initialized");
}
static long long bytes2mbll (int64_t bytes) {
return (bytes + (1ll<<20) - 1) >> 20;
}
static long long bytes2gbll (int64_t bytes) {
return (bytes + (1ll<<30) - 1) >> 30;
}
static int cloneworker (int i) {
assert (0 < i && i < nworkers);
assert (!workers[i].lgl);
msg (-1, 0, "trying to clone worker %d from worker 0", i);
msg (-1, 0,
"prediction: %lld MB to be cloned + allocated %lld MB = %lld MB",
bytes2mbll (lglbytes (workers[0].lgl)),
bytes2mbll (mem.current),
bytes2mbll (lglbytes (workers[0].lgl) + mem.current));
if (lglbytes (workers[0].lgl) + mem.current >= (size_t) softmemlimit) {
msg (-1, 0,
"will not clone worker %d since soft memory limit %lld MB would be hit",
i, bytes2mbll (softmemlimit));
return 0;
}
workers[i].lgl = lglclone (workers[0].lgl);
setopts (workers[i].lgl, i);
msg (-1, 0,
"%lld MB total allocated memory after cloning worker %d from worker 0",
bytes2mbll (mem.current), i);
return 1;
}
static void version () { printf ("%s\n", lglversion ()); exit (0); }
int main (int argc, char ** argv) {
int i, res, clin, lit, val, id, nbcore, witness = 1, tobecloned, tobestarted;
Worker * w, * winner, *maxconsumer, * maxproducer, ** sorted, *earlyworker;
int sumconsumed, sumconsumedunits, sumconsumedcls, sumconsumedeqs;
const char * errstr, * arg;
size_t bytes;
char * cmd;
start = currentime ();
clin = 0;
for (i = 1; i < argc; i++) {
size_t totalmem = getsystemtotalmem (0);
if (!strcmp (argv[i], "-h")) {
printf (
"usage: plingeling [ <option> ... ] [ <dimacs>[.gz|.bz2|.7z|.zip|.lzma] [<threads>] ]\n"
"\n"
"where <option> is one of the following:\n"
"\n"
" -h print this command line option summary\n"
" --version print version and exit\n"
" -v increase verbose level\n"
#ifndef NLGLOG
" -l increase logging level\n"
#endif
" -n do not print solution / witness\n"
#ifndef NLGLYALSAT
"\n"
" --force-locs force (mainy) local search\n"
#endif
"\n"
" -t <num> number of worker threads (default %d on this machine)\n"
" -m <num> maximal memory in MB (default %lld MB on this machine)\n"
" -g <num> maximal memory in GB (default %lld GB on this machine)\n"
" -l <num> limit on number of workers before collecting shared clause\n"
"\n"
" -p plain portfolio, no sharing, e.g. implies the following:\n"
"\n"