-
Notifications
You must be signed in to change notification settings - Fork 0
/
pangen7.h
2413 lines (2409 loc) · 66.4 KB
/
pangen7.h
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
/***** spin: pangen7.h *****/
/*
* This file is part of the public release of Spin. It is subject to the
* terms in the LICENSE file that is included in this source directory.
* Tool documentation is available at http://spinroot.com
*/
static const char *pan_par[] = { /* generates pan.p */
"#include <sys/ipc.h>",
"#include <sys/shm.h>",
"#include <time.h>", /* for nanosleep */
"#include <assert.h>",
"#include <limits.h>",
"#ifdef BFS_DISK",
"#include <unistd.h>", /* for rmdir */
"#include <sys/stat.h>", /* for mkdir */
"#include <sys/types.h>",
"#include <fcntl.h>", /* for open */
"#endif",
"",
"#define Max(a,b) (((a)>(b))?(a):(b))",
"#ifndef WAIT_MAX",
" #define WAIT_MAX 2 /* seconds */",
"#endif",
"#define BFS_GEN 2 /* current and next generation */",
"",
"typedef struct BFS_Slot BFS_Slot;",
"typedef struct BFS_shared BFS_shared;",
"typedef struct BFS_data BFS_data;",
"",
"struct BFS_Slot {",
" #ifdef BFS_FIFO",
" enum bfs_types type; /* message type */",
" #endif",
" BFS_State *s_data; /* state data */",
" #ifndef BFS_QSZ",
" BFS_Slot *nxt; /* linked list */",
" #endif",
"};",
"",
"struct BFS_data {",
" double memcnt;",
" double nstates;",
" double nlinks;",
" double truncs;",
" ulong mreached;",
" ulong vsize;",
" ulong memory_left;",
" ulong punted;",
" ulong errors;",
" int override; /* after crash, if another proc clears locks */",
"};",
"",
"struct BFS_shared { /* about 13K for BFS_MAXPROCS=16 and BFS_MAXLOCKS=1028 */",
" volatile ulong quit; /* set to signal termination -- one word */",
" volatile ulong started;",
"",
" volatile uchar sh_owner[BFS_MAXLOCKS]; /* optional */",
"#ifdef BFS_CHECK",
" volatile uchar in_count[BFS_MAXLOCKS]; /* optional */",
"#endif",
" volatile int sh_locks[BFS_MAXLOCKS];",
" volatile ulong wait_count[BFS_MAXLOCKS]; /* optional */",
"",
" volatile BFS_data bfs_data[BFS_MAXPROCS];",
" volatile uchar bfs_flag[BFS_MAXPROCS]; /* running 0, normal exit 1, abnormal 2 */",
" volatile uchar bfs_idle[BFS_MAXPROCS]; /* set when all input queues are empty */",
"#ifdef BFS_DISK",
" volatile uchar bfs_out_cnt[BFS_MAXPROCS]; /* set when core writes a state */",
"#endif",
"",
"#ifdef BFS_QSZ",
" #define BFS_NORECYCLE",
" #if BFS_QSZ<=0",
" #error BFS_QSZ must be positive",
" #endif",
" #ifdef BFS_FIFO",
" #error BFS_QSZ cannot be combined with BFS_FIFO",
" #endif",
" #ifdef BFS_DISK",
" #error BFS_QSZ cannot be combined with BFS_DISK",
" #endif",
" volatile BFS_Slot bfsq[BFS_GEN][BFS_MAXPROCS][BFS_MAXPROCS][BFS_QSZ];",
" volatile uint bfs_ix[BFS_GEN][BFS_MAXPROCS][BFS_MAXPROCS];",
"#else",
" volatile BFS_Slot *head[BFS_GEN][BFS_MAXPROCS][BFS_MAXPROCS];",
"#endif",
"",
"#ifdef BFS_FIFO",
" volatile BFS_Slot *tail[BFS_GEN][BFS_MAXPROCS][BFS_MAXPROCS];",
" volatile BFS_Slot *dels[BFS_GEN][BFS_MAXPROCS][BFS_MAXPROCS];",
"#endif",
"#ifdef BFS_LOGMEM",
" volatile ulong logmem[1024];",
"#endif",
" volatile ulong mem_left;",
" volatile uchar *allocator; /* start of shared heap, must be last */",
"};",
"",
"enum bfs_types { EMPTY = 0, STATE, DELETED };",
"",
"extern volatile uchar *bfs_get_shared_mem(key_t, size_t);",
"extern BFS_Slot * bfs_new_slot(BFS_Trail *);",
"extern BFS_Slot * bfs_prep_slot(BFS_Trail *, BFS_Slot *);",
"extern BFS_Slot * bfs_next(void);",
"extern BFS_Slot * bfs_pack_state(Trail *, BFS_Trail *, int, BFS_Slot *);",
"extern SV_Hold * bfs_new_sv(int);",
"#if NRUNS>0",
"extern EV_Hold * bfs_new_sv_mask(int);",
"#endif",
"extern BFS_Trail * bfs_grab_trail(void);",
"extern BFS_Trail * bfs_unpack_state(BFS_Slot *);",
"extern int bfs_all_empty(void);",
"extern int bfs_all_idle(void);",
"extern int bfs_all_running(void);",
"extern int bfs_idle_and_empty(void);",
"extern size_t bfs_find_largest(key_t);",
"",
"extern void bfs_clear_locks(void);",
"extern void bfs_drop_shared_memory(void);",
"extern void bfs_explore_state(BFS_Slot *);",
"extern void bfs_initial_state(void);",
"extern void bfs_mark_done(int);",
"extern void bfs_printf(const char *fmt, ...);",
"extern void bfs_push_state(Trail *, BFS_Trail *, int);",
"extern void bfs_recycle(BFS_Slot *);",
"extern void bfs_release_trail(BFS_Trail *);",
"extern void bfs_run(void);",
"extern void bfs_setup_mem(void);",
"extern void bfs_setup(void);",
"extern void bfs_shutdown(const char *);",
"extern void bfs_statistics(void);",
"extern void bfs_store_state(Trail *, short);",
"extern void bfs_set_toggle(void);",
"extern void bfs_update(void);",
"",
"#ifdef MA",
" #error cannot combine -DMA with -DBFS_PAR",
" /* would require us to parallelize g_store */",
"#endif",
"#ifdef BCS",
" #error cannot combine -DBCS with -DBFS_PAR",
"#endif",
"#ifdef BFS_DISK",
" #ifdef BFS_FIFO",
" #error cannot combine BFS_DISK and BFS_FIFO",
" #endif",
" extern void bfs_disk_start(void);",
" extern void bfs_disk_stop(void);",
" extern void bfs_disk_out(void);",
" extern void bfs_disk_inp(void);",
" extern void bfs_disk_iclose(void);",
" extern void bfs_disk_oclose(void);",
" int bfs_out_fd[BFS_MAXPROCS];",
" int bfs_inp_fd[BFS_MAXPROCS];",
"#endif",
"",
"static BFS_shared *shared_memory;",
"#ifndef BFS_QSZ",
"static BFS_Slot *bfs_free_slot; /* local free list */",
"#endif",
"static BFS_Slot bfs_null;",
"static SV_Hold *bfs_svfree[VECTORSZ];",
"static uchar *bfs_heap; /* local pointer into heap */",
"static ulong bfs_left; /* local part of shared heap */",
"#if NRUNS>0",
"static void bfs_keep(EV_Hold *);",
"#endif",
"static long bfs_sent; /* nr msgs sent -- local to each process */",
"static long bfs_rcvd; /* nr msgs rcvd */",
"static long bfs_sleep_cnt; /* stats */",
"static long bfs_wcount;",
"static long bfs_gcount;",
"static ulong bfs_total_shared;",
"#ifdef BFS_STAGGER",
" static int bfs_stage_cnt = 0;",
" static void bfs_stagger_flush(void);",
"#endif",
"static int bfs_toggle; /* local variable, 0 or 1 */",
"static int bfs_qscan; /* scan input queues in order */",
"static ulong bfs_snapped;",
"static int shared_mem_id;",
"#ifndef NOREDUCE",
"static int bfs_nps; /* no preselection */",
"#endif",
"ulong bfs_punt; /* states dropped for lack of memory */",
"#if defined(VERBOSE) || defined(BFS_CHECK)",
"static const char *bfs_sname[] = {",
" \"EMPTY\", /* 0 */",
" \"STATE\", /* 1 */",
" \"STATE\", /* 2 = DELETED */",
" 0",
"};",
"#endif",
"static const char *bfs_lname[] = { /* match values defined in pangen2.c */",
" \"global lock\", /* BFS_GLOB */",
" \"ordinal\", /* BFS_ORD */",
" \"shared memory\", /* BFS_MEM */",
" \"print to stdout\", /* BFS_PRINT */",
" \"hashtable\", /* BFS_STATE */",
" 0",
"};",
"",
"static ulong bfs_count[DELETED+1]; /* indexed with bfs_types: EMPTY=0, STATE=1, DELETED=2 */",
"",
"static int bfs_keep_state;",
"",
"int Cores = 1;",
"int who_am_i = 0; /* root */",
"",
"#ifdef L_BOUND",
" int L_bound = L_BOUND;",
"#endif",
"",
"#ifdef BFS_CHECK",
"void",
"bfs_dump_now(char *s)",
"{ int i; char *p = (char *) &now;",
"",
" e_critical(BFS_PRINT);",
" printf(\"%%s\\t\", s);",
" printf(\"%%3lu: \", vsize);",
" for (i = 0; i < vsize; i++)",
" { printf(\"%%3d \", *p++);",
" }",
" printf(\" %%s\\noffsets:\\t\", s);",
" for (i = 0; i < now._nr_pr; i++)",
" { printf(\"%%3d \", proc_offset[i]);",
" }",
" printf(\"\\n\");",
" x_critical(BFS_PRINT);",
"}",
"",
"void",
"view_state(char *s) /* debugging */",
"{ int i;",
" char *p;",
" e_critical(BFS_PRINT);",
" printf(\"cpu%%02d %%s: \", who_am_i, s);",
" p = (char *)&now;",
" for (i = 0; i < vsize; i++)",
" printf(\"%%3d, \", *p++);",
" printf(\"\\n\"); fflush(stdout);",
" x_critical(BFS_PRINT);",
"}",
"#endif",
"",
"void",
"bfs_main(int ncores, int cycles)",
"{",
" if (cycles)",
" { fprintf(stderr, \"pan: cycle detection is not supported in this mode\\n\");",
" exit(1);",
" }",
"",
" if (ncores == 0) /* i.e., find out */",
" { FILE *fd;",
" char buf[512];",
" if ((fd = fopen(\"/proc/cpuinfo\", \"r\")) == NULL)",
" { /* cannot tell */",
" ncores = Cores; /* use the default */",
" } else",
" { while (fgets(buf, sizeof(buf), fd))",
" { if (strncmp(buf, \"processor\", strlen(\"processor\")) == 0)",
" { ncores++;",
" } }",
" fclose(fd);",
" ncores--;",
" if (verbose)",
" { printf(\"pan: %%d available cores\\n\", ncores+1);",
" } } }",
" if (ncores >= BFS_MAXPROCS)",
" { Cores = BFS_MAXPROCS-1; /* why -1? */",
" } else if (ncores < 1)",
" { Cores = 1;",
" } else",
" { Cores = ncores;",
" }",
" printf(\"pan: using %%d core%%s\\n\", Cores, (Cores>1)?\"s\":\"\");",
" fflush(stdout);",
"#ifdef BFS_DISK",
" bfs_disk_start();", /* create .spin */
"#endif",
" bfs_setup(); /* shared memory segments and fork */",
" bfs_run();",
" if (who_am_i == 0)",
" { stop_timer(0);",
" }",
" bfs_statistics();",
" bfs_mark_done(1);",
" if (who_am_i == 0)",
" { report_time();",
"#ifdef BFS_DISK",
" bfs_disk_stop();",
"#endif",
" }",
"#ifdef C_EXIT",
" C_EXIT; /* trust that it defines a fct */",
"#endif",
" bfs_drop_shared_memory();",
" exit(0);",
"}",
"",
"void",
"bfs_setup_mem(void)",
"{ size_t n;",
" key_t key;",
"#ifdef BFS_FIFO",
" bfs_null.type = EMPTY;",
"#endif",
" ntrpt = (Trail *) emalloc(sizeof(Trail));", /* just once */
"",
" if ((key = ftok(\".\", (int) 'L')) == -1)",
" { perror(\"ftok shared memory\");",
" exit(1);",
" }",
" n = bfs_find_largest(key);",
" bfs_total_shared = (ulong) n;",
"",
" shared_memory = (BFS_shared *) bfs_get_shared_mem(key, n); /* root */",
" shared_memory->allocator = (uchar *) shared_memory + sizeof(BFS_shared);",
" shared_memory->mem_left = (ulong) (n - sizeof(BFS_shared));",
"}",
"",
"ulong bfs_LowLim;",
"#ifndef BFS_RESERVE",
" #define BFS_RESERVE 5",
/* keep memory on global heap in reserve for first few cores */
/* that run out of their local allocation of shared mem */
/* 1~50 percent, 2~30 percent, 5~20 percent, >Cores=none */
"#else",
" #if BFS_RESERVE<1",
" #error BFS_RESERVE must be at least 1",
" #endif",
"#endif",
"",
"void",
"bfs_setup(void) /* executed by root */",
"{ int i, j;",
" ulong n; /* share of shared memory allocated to each core */",
"",
" n = shared_memory->mem_left / (Cores + Cores/(BFS_RESERVE)); /* keep some reserve */",
"",
" if ((n%%sizeof(void *)) != 0)",
" { n -= (n%%sizeof(void *)); /* align, without exceeding total */",
" }",
" for (i = 0; i < Cores-1; i++)",
" { j = fork();",
" if (j == -1)",
" { bfs_printf(\"fork failed\\n\");",
" exit(1);",
" }",
" if (j == 0)",
" { who_am_i = i+1; /* 1..Cores-1 */",
" break;",
" } }",
"",
" e_critical(BFS_MEM);",
" bfs_heap = (uchar *) shared_memory->allocator;",
" shared_memory->allocator += n;",
" shared_memory->mem_left -= n;",
" x_critical(BFS_MEM);",
"",
" bfs_left = n;",
" bfs_runs = 1;",
" bfs_LowLim = n / (2 * (ulong) Cores);", /* 50% */
"}",
"",
"void",
"bfs_run(void)",
"{ BFS_Slot *v;",
"",
"#ifdef BFS_DISK",
" bfs_disk_out();", /* create outqs */
"#endif",
" if (who_am_i == 0)",
" { bfs_initial_state();",
" }",
"#ifdef BFS_DISK",
" #ifdef BFS_STAGGER",
" bfs_stagger_flush();",
" #endif",
" bfs_disk_oclose();", /* sync and close outqs */
"#endif",
"#ifdef BFS_FIFO",
" static int i_count;",
"#endif",
"",
" srand(s_rand+HASH_NR);",
" bfs_qscan = 0;",
" bfs_toggle = 1 - bfs_toggle; /* after initial state */",
" e_critical(BFS_GLOB);",
" shared_memory->started++;",
" x_critical(BFS_GLOB);",
"",
" while (shared_memory->started != Cores) /* wait for all cores to connect */",
" { usleep(1);",
" }",
"",
"#ifdef BFS_DISK",
" bfs_disk_out();",
" bfs_disk_inp();",
"#endif",
"",
" start_timer();",
" while (shared_memory->quit == 0)",
" { v = bfs_next(); /* get next message from current generation */",
" if (v->s_data) /* v->type == STATE || v->type == DELETED */",
" { bfs_count[STATE]++;",
"#ifdef VERBOSE",
" bfs_printf(\"GOT STATE (depth %%d, nr %%u)\\n\",",
" v->s_data->t_info->o_tt, v->s_data->nr);",
"#endif",
" /* last resort: start dropping states when out of memory */",
" if (bfs_left > 1024 || shared_memory->mem_left > 1024)",
" { bfs_explore_state(v);",
" } else",
" { static int warned_loss = 0;",
" if (warned_loss == 0 && who_am_i == 0)",
" { warned_loss++;",
" bfs_printf(\"out of shared memory - losing states\\n\");",
" }",
" bfs_punt++;",
" }",
"#if !defined(BFS_FIFO) && !defined(BFS_NORECYCLE)",
" bfs_recycle(v);",
"#endif",
"#ifdef BFS_FIFO",
" i_count = 0;",
"#endif",
" } else",
" { bfs_count[EMPTY]++;",
"#if defined(BFS_FIFO) && defined(BFS_CHECK)",
" assert(v->type == EMPTY);",
"#endif",
"#ifdef BFS_FIFO",
" if (who_am_i == 0)",
" { if (bfs_idle_and_empty())",
" { if (i_count++ > 10)",
" { shared_memory->quit = 1;",
" }",
" else usleep(1);",
" }",
" } else if (!bfs_all_running())",
" { bfs_shutdown(\"early termination\");",
" }",
"#else",
" if (who_am_i == 0)",
" { if (bfs_all_idle()) /* wait for it */",
" { if (!bfs_all_empty()) /* more states to process */",
" { bfs_set_toggle();",
" goto do_toggle;",
" } else /* done */",
" { shared_memory->quit = 1; /* step 4 */",
" }",
" } else",
" { bfs_sleep_cnt++;",
" }",
" } else",
" { /* wait for quit or idle bit to be reset by root */",
" while (shared_memory->bfs_idle[who_am_i] == 1",
" && shared_memory->quit == 0)",
" { if (bfs_all_running())",
" { bfs_sleep_cnt++;",
" usleep(10); /* new 6.2.3 */",
" } else",
" { bfs_shutdown(\"early termination\");",
" /* no return */",
" } }",
"do_toggle: bfs_qscan = 0;",
"#ifdef BFS_DISK",
" bfs_disk_iclose();",
" bfs_disk_oclose();",
"#endif",
" bfs_toggle = 1 - bfs_toggle;",
"#ifdef BFS_DISK",
" bfs_disk_out();",
" bfs_disk_inp();",
"#endif",
" #ifdef BFS_CHECK",
" bfs_printf(\"toggle: recv from %%d, send to %%d\\n\",",
" bfs_toggle, 1 - bfs_toggle);",
" #endif",
" }",
"#endif",
" } }",
"#ifdef BFS_CHECK",
" bfs_printf(\"done, sent %%5ld recvd %%5ld punt %%5lu sleep: %%ld\\n\",",
" bfs_sent, bfs_rcvd, bfs_punt, bfs_sleep_cnt);",
"#endif",
"}",
"",
"void",
"bfs_report_mem(void) /* called from within wrapup() */",
"{",
" printf(\"%%9.3f total shared memory usage\\n\\n\",",
" ((double) bfs_total_shared - (double) bfs_left)/(1024.*1024.));",
"}",
"",
"void",
"bfs_statistics(void)",
"{",
" #ifdef VERBOSE",
" enum bfs_types i;",
" #endif",
" if (verbose)",
" bfs_printf(\"states sent %%7ld recvd %%7ld stored %%8g sleeps: %%4ld, %%4ld, %%ld\\n\",",
" bfs_sent, bfs_rcvd, nstates, bfs_wcount, bfs_gcount, bfs_sleep_cnt);",
" if (0) bfs_printf(\"states punted %%7lu\\n\", bfs_punt);",
" #ifdef VERBOSE",
" for (i = EMPTY; i <= DELETED; i++)",
" { if (bfs_count[i] > 0)",
" { bfs_printf(\"%%6s %%8lu\\n\",",
" bfs_sname[i], bfs_count[i]);",
" } }",
" #endif",
" bfs_update();",
"",
" if (who_am_i == 0 && shared_memory)",
" { int i; ulong count = 0L;",
" done = 1;",
"",
" e_critical(BFS_PRINT);",
" wrapup();",
" if (verbose)",
" { printf(\"\\nlock-wait counts:\\n\");",
" for (i = 0; i < BFS_STATE; i++)",
" printf(\"%%16s %%9lu\\n\",",
" bfs_lname[i], shared_memory->wait_count[i]);",
"#ifndef BITSTATE",
" for (i = BFS_STATE; i < BFS_MAXLOCKS; i++)",
" { if (0)",
" printf(\" [%%6d] %%9lu\\n\",",
" i, shared_memory->wait_count[i]);",
" count += shared_memory->wait_count[i];",
" }",
" printf(\"%%16s %%9lu (avg per region)\\n\",",
" bfs_lname[BFS_STATE], count/(BFS_MAXLOCKS - BFS_STATE));",
"#endif",
" }",
" fflush(stdout);",
" x_critical(BFS_PRINT);",
" }",
"}",
"",
"void",
"bfs_snapshot(void)",
"{ clock_t stop_time;",
" double delta_time;",
" struct tms stop_tm;",
" volatile BFS_data *s;",
"",
" e_critical(BFS_PRINT);",
" printf(\"cpu%%02d Depth= %%7lu States= %%8.3g Transitions= %%8.3g \",",
" who_am_i, mreached, nstates, nstates+truncs);",
" printf(\"Memory= %%9.3f\\t\", memcnt/1048576.);",
" printf(\"SharedMLeft= %%4lu \", bfs_left/1048576);",
" stop_time = times(&stop_tm);",
" delta_time = ((double) (stop_time - start_time))/((double) sysconf(_SC_CLK_TCK));",
" if (delta_time > 0.01)",
" { printf(\"t= %%6.3g R= %%6.0g\\n\", delta_time, nstates/delta_time);",
" } else",
" { printf(\"t= %%6.3g R= %%6.0g\\n\", 0., 0.);",
" }",
" fflush(stdout);",
" x_critical(BFS_PRINT);",
"",
" s = &shared_memory->bfs_data[who_am_i];",
" s->mreached = (ulong) mreached;",
" s->vsize = (ulong) vsize;",
" s->errors = (int) errors;",
" s->memcnt = (double) memcnt;",
" s->nstates = (double) nstates;",
" s->nlinks = (double) nlinks;",
" s->truncs = (double) truncs;",
" s->memory_left = (ulong) bfs_left;",
" s->punted = (ulong) bfs_punt;",
" bfs_snapped++; /* for bfs_best */",
"}",
"",
"void",
"bfs_shutdown(const char *s)",
"{",
" bfs_clear_locks(); /* in case we interrupted at a bad point */",
" if (!strstr(s, \"early \") || verbose)",
" { bfs_printf(\"stop (%%s)\\n\", s);",
" }",
" bfs_update();",
" if (who_am_i == 0)",
" { wrapup();",
"#ifdef BFS_DISK",
" bfs_disk_stop();",
"#endif",
" }",
" bfs_mark_done(2);",
" pan_exit(2);",
"}",
"",
"SV_Hold *bfs_free_hold;",
"",
"SV_Hold *",
"bfs_get_hold(void)",
"{ SV_Hold *x;",
" if (bfs_free_hold)",
" { x = bfs_free_hold;",
" bfs_free_hold = bfs_free_hold->nxt;",
" } else",
" { x = (SV_Hold *) sh_malloc((ulong) sizeof(SV_Hold));",
" }",
" return x;",
"}",
"",
"BFS_Trail *",
"bfs_unpack_state(BFS_Slot *n) /* called in bfs_explore_state */",
"{ BFS_Trail *otrpt;",
" BFS_State *bfs_t;",
" int vecsz;",
"",
" if (!n || !n->s_data || !n->s_data->t_info)",
" { bfs_Uerror(\"internal error\");",
" }",
" otrpt = (BFS_Trail *) ((BFS_State *) n->s_data)->t_info;",
"",
" trpt->ostate = otrpt->ostate;",
" trpt->st = otrpt->st;",
" trpt->o_tt = otrpt->o_tt;",
" trpt->pr = otrpt->pr;",
" trpt->tau = otrpt->tau;",
" trpt->o_pm = otrpt->o_pm;",
" if (trpt->ostate)",
" trpt->o_t = t_id_lkup[otrpt->t_id];",
"#if defined(C_States) && (HAS_TRACK==1)",
" c_revert((uchar *) &(now.c_state[0]));",
"#endif",
" if (trpt->o_pm & 4) /* rv succeeded */",
" { return (BFS_Trail *) 0; /* revisit not needed */",
" }",
"#ifndef NOREDUCE",
" bfs_nps = 0;",
"#endif",
" if (trpt->o_pm & 8) /* rv attempt failed */",
" { revrv++;",
" if (trpt->tau&8)",
" { trpt->tau &= ~8; /* break atomic */",
"#ifndef NOREDUCE",
" } else if (trpt->tau&32) /* void preselection */",
" { trpt->tau &= ~32;",
" bfs_nps = 1; /* no preselection in repeat */",
"#endif",
" } }",
" trpt->o_pm &= ~(4|8);",
" if (trpt->o_tt > mreached)",
" { static ulong nr = 0L, nc;",
" mreached = trpt->o_tt;",
" nc = (long) nstates/FREQ;",
" if (nc > nr)",
" { nr = nc;",
" bfs_snapshot();",
" } }",
" depth = trpt->o_tt;",
" if (depth >= maxdepth)",
" {",
"#if SYNC",
" if (boq != -1)",
" { BFS_Trail *x = (BFS_Trail *) trpt->ostate;",
" if (x) x->o_pm |= 4; /* rv not failing */",
" }",
"#endif",
" truncs++;",
" if (!warned)",
" { warned = 1;",
" bfs_printf(\"error: max search depth too small\\n\");",
" }",
" if (bounded)",
" { bfs_uerror(\"depth limit reached\");",
" }",
" return (BFS_Trail *) 0;",
" }",
"",
" bfs_t = n->s_data;",
"#if NRUNS>0",
" vsize = bfs_t->omask->sz;",
"#else",
" vsize = ((State *) (bfs_t->osv))->_vsz;",
"#endif",
"#if SYNC",
" boq = bfs_t->boq;",
"#endif",
"",
"#if defined(Q_PROVISO) && !defined(BITSTATE) && defined(FULLSTACK)",
" #ifdef USE_TDH",
" if (((uchar *)(bfs_t->lstate))) /* if BFS_INQ is set */",
" { *((uchar *) bfs_t->lstate) = 0; /* turn it off */",
" }",
" #else",
" if (bfs_t->lstate) /* bfs_par */",
" { bfs_t->lstate->tagged = 0; /* bfs_par state removed from q */",
" }",
" #endif",
"#endif",
" memcpy((char *) &now, (uchar *) bfs_t->osv, vsize);",
"#if !defined(NOCOMP) && !defined(HC) && NRUNS>0",
" Mask = (uchar *) bfs_t->omask->sv; /* in shared memory */",
"#endif",
"#ifdef BFS_CHECK",
" if (0) bfs_dump_now(\"got1\");",
"#endif",
"#ifdef TRIX",
" re_populate();",
"#else",
" #if NRUNS>0",
" if (now._nr_pr > 0)",
" {",
" #if VECTORSZ>32000",
" proc_offset = (int *) bfs_t->omask->po;",
" #else",
" proc_offset = (short *) bfs_t->omask->po;",
" #endif",
" proc_skip = (uchar *) bfs_t->omask->ps;",
" }",
" if (now._nr_qs > 0)",
" {",
" #if VECTORSZ>32000",
" q_offset = (int *) bfs_t->omask->qo;",
" #else",
" q_offset = (short *) bfs_t->omask->qo;",
" #endif",
" q_skip = (uchar *) bfs_t->omask->qs;",
" }",
" #endif",
"#endif",
" vecsz = ((State *) bfs_t->osv)->_vsz;",
"#ifdef BFS_CHECK",
" assert(vecsz > 0 && vecsz < VECTORSZ);",
"#endif",
" { SV_Hold *x = bfs_get_hold();",
" x->sv = bfs_t->osv;",
" x->nxt = bfs_svfree[vecsz];",
" bfs_svfree[vecsz] = x;",
" bfs_t->osv = (State *) 0;",
" }",
"#if NRUNS>0",
" bfs_keep(bfs_t->omask);",
"#endif",
"",
"#ifdef BFS_CHECK",
" if (0) bfs_dump_now(\"got2\");",
" if (0) view_state(\"after\");",
"#endif",
" return (BFS_Trail *) bfs_t->t_info;",
"}",
"void",
"bfs_initial_state(void)",
"{",
"#ifdef BFS_CHECK",
" assert(trpt != NULL);",
"#endif",
" trpt->ostate = (H_el *) 0;",
" trpt->o_tt = -1;",
" trpt->tau = 0;",
"#ifdef VERI",
" trpt->tau |= 4; /* claim moves first */",
"#endif",
" bfs_store_state(trpt, boq); /* initial state : bfs_lib.c */",
"}",
"",
"#ifdef BITSTATE",
" #define bfs_do_store(v, n) b_store(v, n)",
"#else",
" #ifdef USE_TDH",
" #define bfs_do_store(v, n) o_store(v, n)",
" #else",
" #define bfs_do_store(v, n) h_store(v, n)",
" #endif",
"#endif",
"",
"#ifdef BFS_SEP_HASH",
"int",
"bfs_seen_before(void)",
"{ /* cannot set trpt->tau |= 64 to mark successors outside stack */",
" /* since the check is done remotely and the trpt value is gone */",
" #ifdef VERI",
" if (!trpt->ostate /* initial state */",
" || ((trpt->tau&4) /* starting claim moves(s) */",
" && !(((BFS_Trail *)trpt->ostate)->tau&4))) /* prev move: prog */",
" { return 0; /* claim move: mid-state not stored */",
" } /* else */",
" #endif",
" if (!bfs_do_store((char *)&now, vsize)) /* sep_hash */",
" { nstates++; /* local count */",
" return 0; /* new state */",
" }",
" #ifdef BFS_CHECK",
" bfs_printf(\"seen before\\n\");",
" #endif",
" truncs++;",
" return 1; /* old state */",
"}",
"#endif",
"",
"void",
"bfs_explore_state(BFS_Slot *v) /* generate all successors of v */",
"{ BFS_Trail *otrpt;",
" Trans *t;",
"#ifdef HAS_UNLESS",
" int E_state;",
"#endif",
" int tt;",
" short II, To = BASE, From = (short) (now._nr_pr-1);",
" short oboq = boq;",
" uchar _n, _m, ot;",
"",
" memset(ntrpt, 0, sizeof(Trail));",
" otrpt = bfs_unpack_state(v); /* BFS_Trail */",
"",
" if (!otrpt) { return; } /* e.g., depth limit reached */",
"#ifdef L_BOUND",
" #if defined(VERBOSE)",
" bfs_printf(\"Unpacked state with l_bound %%d -- sds %%p\\n\",",
" now._l_bnd, now._l_sds);",
" #endif",
"#endif",
"",
"#if defined(C_States) && (HAS_TRACK==1)",
" c_revert((uchar *) &(now.c_state[0]));",
"#endif",
"",
"#ifdef BFS_SEP_HASH",
" if (bfs_seen_before()) return;",
"#endif",
"",
"#ifdef VERI", /* could move to just before store_state */
" if (now._nr_pr == 0 /* claim terminated */",
" || stopstate[((Pclaim *)pptr(0))->_t][((Pclaim *)pptr(0))->_p])",
" { bfs_uerror(\"end state in claim reached\");",
" }",
"#endif",
" trpt->tau &= ~1; /* timeout off */",
"#ifdef VERI",
" if (trpt->tau&4) /* claim move */",
" { trpt->tau |= (otrpt->tau)&1; /* inherit from prog move */",
" From = To = 0; /* claim */",
" goto Repeat_two;",
" }",
"#endif",
"#ifndef NOREDUCE",
" if (boq == -1 && !(trpt->tau&8) && bfs_nps == 0)",
" for (II = now._nr_pr-1; II >= BASE; II -= 1)",
" {",
"Pickup: this = pptr(II);",
" tt = (int) ((P0 *)this)->_p;",
" ot = (uchar) ((P0 *)this)->_t;",
" if (trans[ot][tt]->atom & 8)",
" { t = trans[ot][tt];",
" if (t->qu[0] != 0)",
" { if (!q_cond(II, t))",
" continue;",
" }",
" From = To = II;",
" trpt->tau |= 32; /* preselect marker */",
" #ifdef VERBOSE",
" bfs_printf(\"%%3ld: proc %%d PreSelected (tau=%%d)\\n\", ",
" depth, II, trpt->tau);",
" #endif",
" goto Repeat_two;",
" } }",
" trpt->tau &= ~32;",
"#endif",
"",
"Repeat_one:",
" if (trpt->tau&8)",
" { From = To = (short ) trpt->pr; /* atomic */",
" } else",
" { From = now._nr_pr-1;",
" To = BASE;",
" }",
"#if defined(VERI) || !defined(NOREDUCE) || defined(ETIM)",
"Repeat_two: /* MainLoop */",
"#endif",
" _n = _m = 0;",
" for (II = From; II >= To; II -= 1) /* all processes */",
" {",
"#ifdef BFS_CHECK",
" bfs_printf(\"proc %%d (%%d - %%d)\\n\", II, From, To);",
"#endif",
"#if SYNC ",
" if (boq != -1 && trpt->pr == II)",
" { continue; /* no rendezvous with same proc */",
" }",
"#endif",
" this = pptr(II);",
" tt = (int) ((P0 *)this)->_p;",
" ot = (uchar) ((P0 *)this)->_t;",
" ntrpt->pr = (uchar) II;",
" ntrpt->st = tt; ",
" trpt->o_pm &= ~1; /* no move yet */",
"#ifdef EVENT_TRACE",
" trpt->o_event = now._event;",
"#endif",
"#ifdef HAS_PRIORITY",
" if (!highest_priority(((P0 *)this)->_pid, II, t))",
" { continue;",
" }",
"#else",
" #ifdef HAS_PROVIDED",
" if (!provided(II, ot, tt, t))",
" { continue;",
" }",
" #endif",
"#endif",
"#ifdef HAS_UNLESS",
" E_state = 0;",
"#endif",
" for (t = trans[ot][tt]; t; t = t->nxt) /* all process transitions */",
" {",
"#ifdef BFS_CHECK",
" assert(t_id_lkup[t->t_id] == t); /* for reverse lookup in bfs_unpack_state */",
"#endif",
"#ifdef VERBOSE",
" if (0) bfs_printf(\"\\tproc %%d tr %%d\\n\", II, t->forw);",
"#endif",
"#ifdef HAS_UNLESS",
" if (E_state > 0",
" && E_state != t->e_trans)",
" break;",
"#endif",
" /* trpt->o_t = */ ntrpt->o_t = t;",
" oboq = boq;",
"",
" if (!(_m = do_transit(t, II)))",
" continue;",
"",
" trpt->o_pm |= 1; /* we moved */",
" (trpt+1)->o_m = _m; /* for unsend */",
"#ifdef PEG",
" peg[t->forw]++;",
"#endif",
"#ifdef VERBOSE",
" e_critical(BFS_PRINT);",
" printf(\"%%3ld: proc %%d exec %%d, \",",
" depth, II, t->forw);",
" printf(\"%%d to %%d, %%s %%s %%s\",",
" tt, t->st, t->tp,",
" (t->atom&2)?\"atomic\":\"\",",
" (boq != -1)?\"rendez-vous\":\"\");",
" #ifdef HAS_UNLESS",
" if (t->e_trans)",
" printf(\" (escapes to state %%d)\", t->st);",
" #endif",
" printf(\" %%saccepting [tau=%%d]\\n\",",
" (trpt->o_pm&2)?\"\":\"non-\", trpt->tau);",
" x_critical(BFS_PRINT);",
"#endif",
"#ifdef HAS_UNLESS",
" E_state = t->e_trans;",
" #if SYNC>0",
" if (t->e_trans > 0 && boq != -1)",
" { fprintf(efd, \"error: rendezvous stmnt in the escape clause\\n\");",
" fprintf(efd, \" of unless stmnt not compatible with -DBFS\\n\");",
" pan_exit(1);",
" }",
" #endif",
"#endif",
" if (t->st > 0)",
" { ((P0 *)this)->_p = t->st;",
" }",
" /* use the ostate ptr, with type *H_el, to temporarily store *BFS_Trail */",
"#ifdef BFS_NOTRAIL",
" ntrpt->ostate = (H_el *) 0; /* no error-traces in this mode */",
"#else",
" ntrpt->ostate = (H_el *) otrpt; /* parent stackframe */",
"#endif",
" /* ntrpt->st = tt; * was already set above */",
"",
" if (boq == -1 && (t->atom&2)) /* atomic */",
" { ntrpt->tau = 8; /* record for next move */",
" } else",
" { ntrpt->tau = 0; /* no timeout or preselect etc */",
" }",
"#ifdef VERI",
" ntrpt->tau |= trpt->tau&4; /* if claim, inherit */",
" if (boq == -1 && !(ntrpt->tau&8)) /* unless rv or atomic */",
" { if (ntrpt->tau&4) /* claim */",
" { ntrpt->tau &= ~4; /* switch to prog */",
" } else",
" { ntrpt->tau |= 4; /* switch to claim */",
" } }",
"#endif",
"#ifdef L_BOUND",
" { uchar obnd = now._l_bnd;",
" uchar *os = now._l_sds;",
" #ifdef VERBOSE",
" bfs_printf(\"saving bound %%d -- sds %%p\\n\", obnd, (void *) os);",
" #endif",
"#endif",
"",
" bfs_store_state(ntrpt, oboq);",
"#ifdef EVENT_TRACE",
" now._event = trpt->o_event;",