-
Notifications
You must be signed in to change notification settings - Fork 0
/
pangen6.h
2878 lines (2870 loc) · 89.5 KB
/
pangen6.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: pangen6.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 *Code2e[] = {
"#if (NCORE>1 || defined(BFS_PAR)) && !defined(WIN32) && !defined(WIN64)",
" /* Test and Set assembly code */",
" #if defined(i386) || defined(__i386__) || defined(__x86_64__)",
" int",
" tas(volatile int *s) /* tested */",
" { int r;",
" __asm__ __volatile__(",
" \"xchgl %%0, %%1 \\n\\t\"",
" : \"=r\"(r), \"=m\"(*s)",
" : \"0\"(1), \"m\"(*s)",
" : \"memory\");",
" ",
" return r;",
" }",
" #elif defined(__arm__)",
" int",
" tas(volatile int *s) /* not tested */",
" { int r = 1;",
" __asm__ __volatile__(",
" \"swpb %%0, %%0, [%%3] \\n\"",
" : \"=r\"(r), \"=m\"(*s)",
" : \"0\"(r), \"r\"(s));",
"",
" return r;",
" }",
" #elif defined(sparc) || defined(__sparc__)",
" int",
" tas(volatile int *s) /* not tested */",
" { int r = 1;",
" __asm__ __volatile__(",
" \" ldstub [%%2], %%0 \\n\"",
" : \"=r\"(r), \"=m\"(*s)",
" : \"r\"(s));",
"",
" return r;",
" }",
" #elif defined(ia64) || defined(__ia64__)",
" /* Intel Itanium */",
" int",
" tas(volatile int *s) /* tested */",
" { long int r;",
" __asm__ __volatile__(",
" \" xchg4 %%0=%%1,%%2 \\n\"",
" : \"=r\"(r), \"+m\"(*s)",
" : \"r\"(1)",
" : \"memory\");",
" return (int) r;",
" }",
" #elif defined(__powerpc64__)",
" int",
" tas(volatile int *s) /* courtesy srirajpaul */",
" { int r;",
" #if 1",
" r = __sync_lock_test_and_set();",
" #else",
" /* xlc compiler only */",
" r = __fetch_and_or(s, 1);",
" __isync();",
" #endif",
" return r;",
" }",
" #else",
" #error missing definition of test and set operation for this platform",
" #endif",
"",
" #ifndef NO_CAS", /* linux, windows */
" #define cas(a,b,c) __sync_bool_compare_and_swap(a,b,c)",
" #else",
" int", /* workaround if the above is not available */
" cas(volatile uint32_t *a, uint32_t b, uint32_t c)",
" { static volatile int cas_lock;",
" while (tas(&cas_lock) != 0) { ; }",
" if (*a == b)",
" { *a = c;",
" cas_lock = 0;",
" return 1;",
" }",
" cas_lock = 0;",
" return 0;",
" }",
" #endif",
"#endif",
0,
};
static const char *Code2c[] = { /* multi-core option - Spin 5.0 and later */
"#if NCORE>1",
"#if defined(WIN32) || defined(WIN64)",
" #ifndef _CONSOLE",
" #define _CONSOLE",
" #endif",
" #ifdef WIN64",
" #undef long",
" #endif",
" #include <windows.h>",
"/*",
" #ifdef WIN64",
" #define long long long",
" #endif",
"*/",
"#else",
" #include <sys/ipc.h>",
" #include <sys/sem.h>",
" #include <sys/shm.h>",
"#endif",
"",
"/* code common to cygwin/linux and win32/win64: */",
"",
"#ifdef VERBOSE",
" #define VVERBOSE (1)",
"#else",
" #define VVERBOSE (0)",
"#endif",
"",
"/* the following values must be larger than 256 and must fit in an int */",
"#define QUIT 1024 /* terminate now command */",
"#define QUERY 512 /* termination status query message */",
"#define QUERY_F 513 /* query failed, cannot quit */",
"",
"#define GN_FRAMES (int) (GWQ_SIZE / (double) sizeof(SM_frame))",
"#define LN_FRAMES (int) (LWQ_SIZE / (double) sizeof(SM_frame))",
"",
"#ifndef VMAX",
" #define VMAX VECTORSZ",
"#endif",
"#ifndef PMAX",
" #define PMAX 64",
"#endif",
"#ifndef QMAX",
" #define QMAX 64",
"#endif",
"",
"#if VECTORSZ>32000",
" #define OFFT int",
"#else",
" #define OFFT short",
"#endif",
"",
"#ifdef SET_SEG_SIZE",
" /* no longer useful -- being recomputed for local heap size anyway */",
" double SEG_SIZE = (((double) SET_SEG_SIZE) * 1048576.);",
"#else",
" double SEG_SIZE = (1048576.*1024.); /* 1GB default shared memory pool segments */",
"#endif",
"",
"double LWQ_SIZE = 0.; /* initialized in main */",
"",
"#ifdef SET_WQ_SIZE",
" #ifdef NGQ",
" #warning SET_WQ_SIZE applies to global queue -- ignored",
" double GWQ_SIZE = 0.;",
" #else",
" double GWQ_SIZE = (((double) SET_WQ_SIZE) * 1048576.);",
" /* must match the value in pan_proxy.c, if used */",
" #endif",
"#else",
" #ifdef NGQ",
" double GWQ_SIZE = 0.;",
" #else",
" double GWQ_SIZE = (128.*1048576.); /* 128 MB default queue sizes */",
" #endif",
"#endif",
"",
"/* Crash Detection Parameters */",
"#ifndef ONESECOND",
" #define ONESECOND (1<<25)", /* name is somewhat of a misnomer */
"#endif",
"#ifndef SHORT_T",
" #define SHORT_T (0.1)",
"#endif",
"#ifndef LONG_T",
" #define LONG_T (600)",
"#endif",
"",
"double OneSecond = (double) (ONESECOND); /* waiting for a free slot -- checks crash */",
"double TenSeconds = 10. * (ONESECOND); /* waiting for a lock -- check for a crash */",
"",
"/* Termination Detection Params -- waiting for new state input in Get_Full_Frame */",
"double Delay = ((double) SHORT_T) * (ONESECOND); /* termination detection trigger */",
"double OneHour = ((double) LONG_T) * (ONESECOND); /* timeout termination detection */",
"",
"typedef struct SM_frame SM_frame;",
"typedef struct SM_results SM_results;",
"typedef struct sh_Allocater sh_Allocater;",
"",
"struct SM_frame { /* about 6K per slot */",
" volatile int m_vsize; /* 0 means free slot */",
" volatile int m_boq; /* >500 is a control message */",
"#ifdef FULL_TRAIL",
" volatile struct Stack_Tree *m_stack; /* ptr to previous state */",
"#endif",
" volatile uchar m_tau;",
" volatile uchar m_o_pm;",
" volatile int nr_handoffs; /* to compute real_depth */",
" volatile char m_now [VMAX];",
"#if !defined(NOCOMP) && !defined(HC)",
" volatile char m_mask [(VMAX + 7)/8];",
"#endif",
" volatile OFFT m_p_offset[PMAX];",
" volatile OFFT m_q_offset[QMAX];",
" volatile uchar m_p_skip [PMAX];",
" volatile uchar m_q_skip [QMAX];",
"#if defined(C_States) && (HAS_TRACK==1) && (HAS_STACK==1)",
" volatile uchar m_c_stack [StackSize];",
/* captures contents of c_stack[] for unmatched objects */
"#endif",
"};",
"",
"int proxy_pid; /* id of proxy if nonzero -- receive half */",
"int store_proxy_pid;",
"short remote_party;",
"int proxy_pid_snd; /* id of proxy if nonzero -- send half */",
"char o_cmdline[512]; /* to pass options to children */",
"",
"int iamin[CS_NR+NCORE]; /* non-shared */",
"",
"#if defined(WIN32) || defined(WIN64)",
"int tas(volatile LONG *);",
"",
"HANDLE proxy_handle_snd; /* for Windows Create and Terminate */",
"",
"struct sh_Allocater { /* shared memory for states */",
" volatile char *dc_arena; /* to allocate states from */",
" volatile long pattern; /* to detect overruns */",
" volatile long dc_size; /* nr of bytes left */",
" volatile void *dc_start; /* where memory segment starts */",
" volatile void *dc_id; /* to attach, detach, remove shared memory segments */",
" volatile sh_Allocater *nxt; /* linked list of pools */",
"};",
"DWORD worker_pids[NCORE]; /* root mem of pids of all workers created */",
"HANDLE worker_handles[NCORE]; /* for windows Create and Terminate */",
"void * shmid [NR_QS]; /* return value from CreateFileMapping */",
"void * shmid_M; /* shared mem for state allocation in hashtable */",
"",
"#ifdef SEP_STATE",
" void *shmid_X;",
"#else",
" void *shmid_S; /* shared bitstate arena or hashtable */",
"#endif",
"#else",
"int tas(volatile int *);",
"",
"struct sh_Allocater { /* shared memory for states */",
" volatile char *dc_arena; /* to allocate states from */",
" volatile long pattern; /* to detect overruns */",
" volatile long dc_size; /* nr of bytes left */",
" volatile char *dc_start; /* where memory segment starts */",
" volatile int dc_id; /* to attach, detach, remove shared memory segments */",
" volatile sh_Allocater *nxt; /* linked list of pools */",
"};",
"",
"int worker_pids[NCORE]; /* root mem of pids of all workers created */",
"int shmid [NR_QS]; /* return value from shmget */",
"int nibis = 0; /* set after shared mem has been released */",
"int shmid_M; /* shared mem for state allocation in hashtable */",
"#ifdef SEP_STATE",
" long shmid_X;",
"#else",
" int shmid_S; /* shared bitstate arena or hashtable */",
" volatile sh_Allocater *first_pool; /* of shared state memory */",
" volatile sh_Allocater *last_pool;",
"#endif", /* SEP_STATE */
"#endif", /* WIN32 || WIN64 */
"",
"struct SM_results { /* for shuttling back final stats */",
" volatile int m_vsize; /* avoid conflicts with frames */",
" volatile int m_boq; /* these 2 fields are not written in record_info */",
" /* probably not all fields really need to be volatile */",
" volatile double m_memcnt;",
" volatile double m_nstates;",
" volatile double m_truncs;",
" volatile double m_truncs2;",
" volatile double m_nShadow;",
" volatile double m_nlinks;",
" volatile double m_ngrabs;",
" volatile double m_nlost;",
" volatile double m_hcmp;",
" volatile double m_frame_wait;",
" volatile int m_hmax;",
" volatile int m_svmax;",
" volatile int m_smax;",
" volatile int m_mreached;",
" volatile int m_errors;",
" volatile int m_VMAX;",
" volatile short m_PMAX;",
" volatile short m_QMAX;",
" volatile uchar m_R; /* reached info for all proctypes */",
"};",
"",
"int core_id = 0; /* internal process nr, to know which q to use */",
"unsigned long nstates_put = 0; /* statistics */",
"unsigned long nstates_get = 0;",
"int query_in_progress = 0; /* termination detection */",
"",
"double free_wait = 0.; /* waiting for a free frame */",
"double frame_wait = 0.; /* waiting for a full frame */",
"double lock_wait = 0.; /* waiting for access to cs */",
"double glock_wait[3]; /* waiting for access to global lock */",
"",
"char *sprefix = \"rst\";",
"uchar was_interrupted, issued_kill, writing_trail;",
"",
"static SM_frame cur_Root; /* current root, to be safe with error trails */",
"",
"SM_frame *m_workq [NR_QS]; /* per cpu work queues + global q */",
"char *shared_mem[NR_QS]; /* return value from shmat */",
"#ifdef SEP_HEAP",
"char *my_heap;",
"long my_size;",
"#endif",
"volatile sh_Allocater *dc_shared; /* assigned at initialization */",
"",
"static int vmax_seen, pmax_seen, qmax_seen;",
"static double gq_tries, gq_hasroom, gq_hasnoroom;",
"",
"volatile int *prfree;", /* [NCORE] */
"volatile int *prfull;", /* [NCORE] */
"volatile int *prcnt;", /* [NCORE] */
"volatile int *prmax;", /* [NCORE] */
"",
"volatile int *sh_lock; /* mutual exclusion locks - in shared memory */",
"volatile double *is_alive; /* to detect when processes crash */",
"volatile int *grfree, *grfull, *grcnt, *grmax; /* access to shared global q */",
"volatile double *gr_readmiss, *gr_writemiss;",
"static int lrfree; /* used for temporary recording of slot */",
"static int dfs_phase2;",
"",
"void mem_put(int); /* handoff state to other cpu */",
"void mem_put_acc(void); /* liveness mode */",
"void mem_get(void); /* get state from work queue */",
"void sudden_stop(char *);",
"",
"void",
"record_info(SM_results *r)",
"{ int i;",
" uchar *ptr;",
"",
"#ifdef SEP_STATE",
" if (0)",
" { cpu_printf(\"nstates %%g nshadow %%g -- memory %%-6.3f Mb\\n\",",
" nstates, nShadow, memcnt/(1048576.));",
" }",
" r->m_memcnt = 0;",
"#else",
" #ifdef BITSTATE",
" r->m_memcnt = 0; /* it's shared */",
" #endif",
" r->m_memcnt = memcnt;",
"#endif",
" if (a_cycles && core_id == 1)",
" { r->m_nstates = nstates;",
" r->m_nShadow = nstates;",
" } else",
" { r->m_nstates = nstates;",
" r->m_nShadow = nShadow;",
" }",
" r->m_truncs = truncs;",
" r->m_truncs2 = truncs2;",
" r->m_nlinks = nlinks;",
" r->m_ngrabs = ngrabs;",
" r->m_nlost = nlost;",
" r->m_hcmp = hcmp;",
" r->m_frame_wait = frame_wait;",
" r->m_hmax = hmax;",
" r->m_svmax = svmax;",
" r->m_smax = smax;",
" r->m_mreached = mreached;",
" r->m_errors = errors;",
" r->m_VMAX = vmax_seen;",
" r->m_PMAX = (short) pmax_seen;",
" r->m_QMAX = (short) qmax_seen;",
" ptr = (uchar *) &(r->m_R);",
" for (i = 0; i <= _NP_; i++) /* all proctypes */",
" { memcpy(ptr, reached[i], NrStates[i]*sizeof(uchar));",
" ptr += NrStates[i]*sizeof(uchar);",
" }",
" if (verbose>1)",
" { cpu_printf(\"Put Results nstates %%g (sz %%d)\\n\", nstates, ptr - &(r->m_R));",
" }",
"}",
"",
"void snapshot(void);",
"",
"void",
"retrieve_info(SM_results *r)",
"{ int i, j;",
" volatile uchar *ptr;",
"",
" snapshot(); /* for a final report */",
"",
" enter_critical(GLOBAL_LOCK);",
"#ifdef SEP_HEAP",
" if (verbose)",
" { printf(\"cpu%%d: local heap-left %%ld KB (%%d MB)\\n\",",
" core_id, (long) (my_size/1024), (int) (my_size/1048576));",
" }",
"#endif",
" if (verbose && core_id == 0)",
" { printf(\"qmax: \");",
" for (i = 0; i < NCORE; i++)",
" { printf(\"%%d \", prmax[i]);",
" }",
"#ifndef NGQ",
" printf(\"G: %%d\", *grmax);",
"#endif",
" printf(\"\\n\");",
" }",
" leave_critical(GLOBAL_LOCK);",
"",
" memcnt += r->m_memcnt;",
" nstates += r->m_nstates;",
" nShadow += r->m_nShadow;",
" truncs += r->m_truncs;",
" truncs2 += r->m_truncs2;",
" nlinks += r->m_nlinks;",
" ngrabs += r->m_ngrabs;",
" nlost += r->m_nlost;",
" hcmp += r->m_hcmp;",
" /* frame_wait += r->m_frame_wait; */",
" errors += r->m_errors;",
"",
" if (hmax < r->m_hmax) hmax = r->m_hmax;",
" if (svmax < r->m_svmax) svmax = r->m_svmax;",
" if (smax < r->m_smax) smax = r->m_smax;",
" if (mreached < r->m_mreached) mreached = r->m_mreached;",
"",
" if (vmax_seen < r->m_VMAX) vmax_seen = r->m_VMAX;",
" if (pmax_seen < (int) r->m_PMAX) pmax_seen = (int) r->m_PMAX;",
" if (qmax_seen < (int) r->m_QMAX) qmax_seen = (int) r->m_QMAX;",
"",
" ptr = &(r->m_R);",
" for (i = 0; i <= _NP_; i++) /* all proctypes */",
" { for (j = 0; j < NrStates[i]; j++)",
" { if (*(ptr + j) != 0)",
" { reached[i][j] = 1;",
" } }",
" ptr += NrStates[i]*sizeof(uchar);",
" }",
" if (verbose>1)",
" { cpu_printf(\"Got Results (%%d)\\n\", (int) (ptr - &(r->m_R)));",
" snapshot();",
" }",
"}",
"",
"#if !defined(WIN32) && !defined(WIN64)",
"static void",
"rm_shared_segments(void)",
"{ int m;",
" volatile sh_Allocater *nxt_pool;",
" /*",
" * mark all shared memory segments for removal ",
" * the actual removes wont happen intil last process dies or detaches",
" * the shmctl calls can return -1 if not all procs have detached yet",
" */",
" for (m = 0; m < NR_QS; m++) /* +1 for global q */",
" { if (shmid[m] != -1)",
" { (void) shmctl(shmid[m], IPC_RMID, NULL);",
" } }",
"#ifdef SEP_STATE",
" if (shmid_M != -1)",
" { (void) shmctl(shmid_M, IPC_RMID, NULL);",
" }",
"#else",
" if (shmid_S != -1)",
" { (void) shmctl(shmid_S, IPC_RMID, NULL);",
" }",
" for (last_pool = first_pool; last_pool != NULL; last_pool = nxt_pool)",
" { shmid_M = (int) (last_pool->dc_id);",
" nxt_pool = last_pool->nxt; /* as a pre-caution only */",
" if (shmid_M != -1)",
" { (void) shmctl(shmid_M, IPC_RMID, NULL);",
" } }",
"#endif",
"}",
"#endif",
"",
"void",
"sudden_stop(char *s)",
"{ char b[64];",
" int i;",
"",
" printf(\"cpu%%d: stop - %%s\\n\", core_id, s);",
"#if !defined(WIN32) && !defined(WIN64)",
" if (proxy_pid != 0)",
" { rm_shared_segments();",
" }",
"#endif",
" if (search_terminated != NULL)",
" { if (*search_terminated != 0)",
" { if (verbose)",
" { printf(\"cpu%%d: termination initiated (%%d)\\n\",",
" core_id, (int) *search_terminated);",
" }",
" } else",
" { if (verbose)",
" { printf(\"cpu%%d: initiated termination\\n\", core_id);",
" }",
" *search_terminated |= 8; /* sudden_stop */",
" }",
" if (core_id == 0)",
" { if (((*search_terminated) & 4) /* uerror in one of the cpus */",
" && !((*search_terminated) & (8|32|128|256))) /* abnormal stop */",
" { if (errors == 0) errors++; /* we know there is at least 1 */",
" }",
" wrapup(); /* incomplete stats, but at least something */",
" }",
" return;",
" } /* else: should rarely happen, take more drastic measures */",
"",
" if (core_id == 0) /* local root process */",
" { for (i = 1; i < NCORE; i++) /* not for 0 of course */",
" { int ignore;",
"#if defined(WIN32) || defined(WIN64)",
" DWORD dwExitCode = 0;",
" GetExitCodeProcess(worker_handles[i], &dwExitCode);",
" if (dwExitCode == STILL_ACTIVE)",
" { TerminateProcess(worker_handles[i], 0);",
" }",
" printf(\"cpu0: terminate %%d %%d\\n\",",
" (int) worker_pids[i], (dwExitCode == STILL_ACTIVE));",
"#else",
" sprintf(b, \"kill -%%d %%d\", (int) SIGKILL, (int) worker_pids[i]);",
" ignore = system(b); /* if this is a proxy: receive half */",
" printf(\"cpu0: %%s\\n\", b);",
"#endif",
" }",
" issued_kill++;",
" } else",
" { /* on WIN32/WIN64 -- these merely kills the root process... */",
" if (was_interrupted == 0)", /* 2=SIGINT to root to trigger stop */
" { int ignore;",
" sprintf(b, \"kill -%%d %%d\", (int) SIGINT, (int) worker_pids[0]);",
" ignore = system(b); /* warn the root process */",
" printf(\"cpu%%d: %%s\\n\", core_id, b);",
" issued_kill++;",
" } }",
"}",
"",
"#define iam_alive() is_alive[core_id]++", /* for crash detection */
"",
"extern int crash_test(double);",
"extern void crash_reset(void);",
"",
"int",
"someone_crashed(int wait_type)",
"{ static double last_value = 0.0;",
" static int count = 0;",
"",
" if (search_terminated == NULL",
" || *search_terminated != 0)",
" {",
" if (!(*search_terminated & (8|32|128|256)))",
" { if (count++ < 100*NCORE)",
" { return 0;",
" } }",
" return 1;",
" }",
" /* check left neighbor only */",
" if (last_value == is_alive[(core_id + NCORE - 1) %% NCORE])",
" { if (count++ >= 100) /* to avoid unnecessary checks */",
" { return 1;",
" }",
" return 0;",
" }",
" last_value = is_alive[(core_id + NCORE - 1) %% NCORE];",
" count = 0;",
" crash_reset();",
" return 0;",
"}",
"",
"void",
"sleep_report(void)",
"{",
" enter_critical(GLOBAL_LOCK);",
" if (verbose)",
" {",
"#ifdef NGQ",
" printf(\"cpu%%d: locks: global %%g\\tother %%g\\t\",",
" core_id, glock_wait[0], lock_wait - glock_wait[0]);",
"#else",
" printf(\"cpu%%d: locks: GL %%g, RQ %%g, WQ %%g, HT %%g\\t\",",
" core_id, glock_wait[0], glock_wait[1], glock_wait[2],",
" lock_wait - glock_wait[0] - glock_wait[1] - glock_wait[2]);",
"#endif",
" printf(\"waits: states %%g slots %%g\\n\", frame_wait, free_wait);",
"#ifndef NGQ",
" printf(\"cpu%%d: gq [tries %%g, room %%g, noroom %%g]\\n\", core_id, gq_tries, gq_hasroom, gq_hasnoroom);",
" if (core_id == 0 && (*gr_readmiss >= 1.0 || *gr_readmiss >= 1.0 || *grcnt != 0))",
" printf(\"cpu0: gq [readmiss: %%g, writemiss: %%g cnt %%d]\\n\", *gr_readmiss, *gr_writemiss, *grcnt);",
"#endif",
" }",
" if (free_wait > 1000000.)",
" #ifndef NGQ",
" if (!a_cycles)",
" { printf(\"hint: this search may be faster with a larger work-queue\\n\");",
" printf(\" (-DSET_WQ_SIZE=N with N>%%g), and/or with -DUSE_DISK\\n\",",
" GWQ_SIZE/sizeof(SM_frame));",
" printf(\" or with a larger value for -zN (N>%%ld)\\n\", z_handoff);",
" #else",
" { printf(\"hint: this search may be faster if compiled without -DNGQ, with -DUSE_DISK, \");",
" printf(\"or with a larger -zN (N>%%d)\\n\", z_handoff);",
" #endif",
" }",
" leave_critical(GLOBAL_LOCK);",
"}",
"",
"#ifndef MAX_DSK_FILE",
" #define MAX_DSK_FILE 1000000 /* default is max 1M states per file */",
"#endif",
"",
"void",
"multi_usage(FILE *fd)",
"{ static int warned = 0;",
" if (warned > 0) { return; } else { warned++; }",
" fprintf(fd, \"\\n\");",
" fprintf(fd, \"Defining multi-core mode:\\n\\n\");",
" fprintf(fd, \" -DDUAL_CORE --> same as -DNCORE=2\\n\");",
" fprintf(fd, \" -DQUAD_CORE --> same as -DNCORE=4\\n\");",
" fprintf(fd, \" -DNCORE=N --> enables multi_core verification if N>1\\n\");",
" fprintf(fd, \"\\n\");",
" fprintf(fd, \"Additional directives supported in multi-core mode:\\n\\n\");",
" fprintf(fd, \" -DSEP_STATE --> forces separate statespaces instead of a single shared state space\\n\");",
" fprintf(fd, \" -DNUSE_DISK --> use disk for storing states when a work queue overflows\\n\");",
" fprintf(fd, \" -DMAX_DSK_FILE --> max nr of states per diskfile (%%d)\\n\", MAX_DSK_FILE);",
" fprintf(fd, \" -DFULL_TRAIL --> support full error trails (increases memory use)\\n\");",
" fprintf(fd, \"\\n\");",
" fprintf(fd, \"More advanced use (should rarely need changing):\\n\\n\");",
" fprintf(fd, \" To change the nr of states that can be stored in the global queue\\n\");",
" fprintf(fd, \" (lower numbers allow for more states to be stored, prefer multiples of 8):\\n\");",
" fprintf(fd, \" -DVMAX=N --> upperbound on statevector for handoffs (N=%%d)\\n\", VMAX);",
" fprintf(fd, \" -DPMAX=N --> upperbound on nr of procs (default: N=%%d)\\n\", PMAX);",
" fprintf(fd, \" -DQMAX=N --> upperbound on nr of channels (default: N=%%d)\\n\", QMAX);",
" fprintf(fd, \"\\n\");",
#if 0
"#if !defined(WIN32) && !defined(WIN64)",
" fprintf(fd, \" To change the size of spin's individual shared memory segments for cygwin/linux:\\n\");",
" fprintf(fd, \" -DSET_SEG_SIZE=N --> default %%g (Mbytes)\\n\", SEG_SIZE/(1048576.));",
" fprintf(fd, \"\\n\");",
"#endif",
#endif
" fprintf(fd, \" To set the total amount of memory reserved for the global workqueue:\\n\");",
" fprintf(fd, \" -DSET_WQ_SIZE=N --> default: N=128 (defined in MBytes)\\n\\n\");",
#if 0
" fprintf(fd, \" To omit the global workqueue completely (bad idea):\\n\");",
" fprintf(fd, \" -DNGQ\\n\\n\");",
#endif
" fprintf(fd, \" To force the use of a single global heap, instead of separate heaps:\\n\");",
" fprintf(fd, \" -DGLOB_HEAP\\n\");",
" fprintf(fd, \"\\n\");",
" fprintf(fd, \" To define a fct to initialize data before spawning processes (use quotes):\\n\");",
" fprintf(fd, \" \\\"-DC_INIT=fct()\\\"\\n\");",
" fprintf(fd, \"\\n\");",
" fprintf(fd, \" Timer settings for termination and crash detection:\\n\");",
" fprintf(fd, \" -DSHORT_T=N --> timeout for termination detection trigger (N=%%g)\\n\", (double) SHORT_T);",
" fprintf(fd, \" -DLONG_T=N --> timeout for giving up on termination detection (N=%%g)\\n\", (double) LONG_T);",
" fprintf(fd, \" -DONESECOND --> (1<<29) --> timeout waiting for a free slot -- to check for crash\\n\");",
" fprintf(fd, \" -DT_ALERT --> collect stats on crash alert timeouts\\n\\n\");",
" fprintf(fd, \"Help with Linux/Windows/Cygwin configuration for multi-core:\\n\");",
" fprintf(fd, \" http://spinroot.com/spin/multicore/V5_Readme.html\\n\");",
" fprintf(fd, \"\\n\");",
"}",
"#if NCORE>1 && defined(FULL_TRAIL)",
"typedef struct Stack_Tree {",
" uchar pr; /* process that made transition */",
" T_ID t_id; /* id of transition */",
" volatile struct Stack_Tree *prv; /* backward link towards root */",
"} Stack_Tree;",
"",
"H_el *grab_shared(int);",
"volatile Stack_Tree **stack_last; /* in shared memory */",
"char *stack_cache = NULL; /* local */",
"int nr_cached = 0; /* local */",
"",
"#ifndef CACHE_NR",
" #define CACHE_NR 1024",
"#endif",
"",
"volatile Stack_Tree *",
"stack_prefetch(void)",
"{ volatile Stack_Tree *st;",
"",
" if (nr_cached == 0)",
" { stack_cache = (char *) grab_shared(CACHE_NR * sizeof(Stack_Tree));",
" nr_cached = CACHE_NR;",
" }",
" st = (volatile Stack_Tree *) stack_cache;",
" stack_cache += sizeof(Stack_Tree);",
" nr_cached--;",
" return st;",
"}",
"",
"void",
"Push_Stack_Tree(short II, T_ID t_id)",
"{ volatile Stack_Tree *st;",
"",
" st = (volatile Stack_Tree *) stack_prefetch();",
" st->pr = II;",
" st->t_id = t_id;",
" st->prv = (Stack_Tree *) stack_last[core_id];",
" stack_last[core_id] = st;",
"}",
"",
"void",
"Pop_Stack_Tree(void)",
"{ volatile Stack_Tree *cf = stack_last[core_id];",
"",
" if (cf)",
" { stack_last[core_id] = cf->prv;",
" } else if (nr_handoffs * z_handoff + depth > 0)",
" { printf(\"cpu%%d: error pop_stack_tree (depth %%ld)\\n\",",
" core_id, depth);",
" }",
"}",
"#endif", /* NCORE>1 && FULL_TRAIL */
"",
"void",
"e_critical(int which)",
"{ double cnt_start;",
"",
" if (readtrail || iamin[which] > 0)",
" { if (!readtrail && verbose)",
" { printf(\"cpu%%d: Double Lock on %%d (now %%d)\\n\",",
" core_id, which, iamin[which]+1);",
" fflush(stdout);",
" }",
" iamin[which]++; /* local variable */",
" return;",
" }",
"",
" cnt_start = lock_wait;",
"",
" while (sh_lock != NULL) /* as long as we have shared memory */",
" { int r = tas(&sh_lock[which]);",
" if (r == 0)",
" { iamin[which] = 1;",
" return; /* locked */",
" }",
"",
" lock_wait++;",
"#ifndef NGQ",
" if (which < 3) { glock_wait[which]++; }",
"#else",
" if (which == 0) { glock_wait[which]++; }",
"#endif",
" iam_alive();",
"",
" if (lock_wait - cnt_start > TenSeconds)",
" { printf(\"cpu%%d: lock timeout on %%d\\n\", core_id, which);",
" cnt_start = lock_wait;",
" if (someone_crashed(1))",
" { sudden_stop(\"lock timeout\");",
" pan_exit(1);",
" } } }",
"}",
"",
"void",
"x_critical(int which)",
"{",
" if (iamin[which] != 1)",
" { if (iamin[which] > 1)",
" { iamin[which]--; /* this is thread-local - no races on this one */",
" if (!readtrail && verbose)",
" { printf(\"cpu%%d: Partial Unlock on %%d (%%d more needed)\\n\",",
" core_id, which, iamin[which]);",
" fflush(stdout);",
" }",
" return;",
" } else /* iamin[which] <= 0 */",
" { if (!readtrail)",
" { printf(\"cpu%%d: Invalid Unlock iamin[%%d] = %%d\\n\",",
" core_id, which, iamin[which]);",
" fflush(stdout);",
" }",
" return;",
" } }",
"",
" if (sh_lock != NULL)",
" { iamin[which] = 0;",
"#if defined(__powerpc64__)",
" #if 1",
" __sync_synchronize(); /* srirajpaul */",
" #else",
" __lwsync(); /* xlc compiler only */",
" #endif",
"#endif",
" sh_lock[which] = 0; /* unlock */",
" }",
"}",
"",
"void",
"#if defined(WIN32) || defined(WIN64)",
"start_proxy(char *s, DWORD r_pid)",
"#else",
"start_proxy(char *s, int r_pid)",
"#endif",
"{ char Q_arg[16], Z_arg[16], Y_arg[16];",
" char *args[32], *ptr;",
" int argcnt = 0;",
"",
" sprintf(Q_arg, \"-Q%%d\", getpid());",
" sprintf(Y_arg, \"-Y%%d\", r_pid);",
" sprintf(Z_arg, \"-Z%%d\", proxy_pid /* core_id */);",
"",
" args[argcnt++] = \"proxy\";",
" args[argcnt++] = s; /* -r or -s */",
" args[argcnt++] = Q_arg;",
" args[argcnt++] = Z_arg;",
" args[argcnt++] = Y_arg;",
"",
" if (strlen(o_cmdline) > 0)",
" { ptr = o_cmdline; /* assume args separated by spaces */",
" do { args[argcnt++] = ptr++;",
" if ((ptr = strchr(ptr, ' ')) != NULL)",
" { while (*ptr == ' ')",
" { *ptr++ = '\\0';",
" }",
" } else",
" { break;",
" }",
" } while (argcnt < 31);",
" }",
" args[argcnt] = NULL;",
"#if defined(WIN32) || defined(WIN64)",
" execvp(\"pan_proxy\", args); /* no return */",
"#else",
" execvp(\"./pan_proxy\", args); /* no return */",
"#endif",
" Uerror(\"pan_proxy exec failed\");",
"}",
"/*** end of common code fragment ***/",
"",
"#if !defined(WIN32) && !defined(WIN64)",
"void",
"init_shm(void) /* initialize shared work-queues - linux/cygwin */",
"{ key_t key[NR_QS];",
" int n, m;",
" int must_exit = 0;",
"",
" if (core_id == 0 && verbose)",
" { printf(\"cpu0: step 3: allocate shared workqueues %%g MB\\n\",",
" ((double) NCORE * LWQ_SIZE + GWQ_SIZE) / (1048576.) );",
" }",
" for (m = 0; m < NR_QS; m++) /* last q is the global q */",
" { double qsize = (m == NCORE) ? GWQ_SIZE : LWQ_SIZE;",
" key[m] = ftok(PanSource, m+1);", /* m must be nonzero, 1..NCORE */
" if (key[m] == -1)",
" { perror(\"ftok shared queues\"); must_exit = 1; break;",
" }",
"",
" if (core_id == 0) /* root creates */",
" { /* check for stale copy */",
" shmid[m] = shmget(key[m], (size_t) qsize, 0600);",
" if (shmid[m] != -1) /* yes there is one; remove it */",
" { printf(\"cpu0: removing stale q%%d, status: %%d\\n\",",
" m, shmctl(shmid[m], IPC_RMID, NULL));",
" }",
" shmid[m] = shmget(key[m], (size_t) qsize, 0600|IPC_CREAT|IPC_EXCL);",
" memcnt += qsize;",
" } else /* workers attach */",
" { shmid[m] = shmget(key[m], (size_t) qsize, 0600);",
" /* never called, since we create shm *before* we fork */",
" }",
" if (shmid[m] == -1)",
" { perror(\"shmget shared queues\"); must_exit = 1; break;",
" }",
"",
" shared_mem[m] = (char *) shmat(shmid[m], (void *) 0, 0); /* attach */",
" if (shared_mem[m] == (char *) -1)",
" { fprintf(stderr, \"error: cannot attach shared wq %%d (%%d Mb)\\n\",",
" m+1, (int) (qsize/(1048576.)));",
" perror(\"shmat shared queues\"); must_exit = 1; break;",
" }",
"",
" m_workq[m] = (SM_frame *) shared_mem[m];",
" if (core_id == 0)",
" { int nframes = (m == NCORE) ? GN_FRAMES : LN_FRAMES;",
" for (n = 0; n < nframes; n++)",
" { m_workq[m][n].m_vsize = 0;",
" m_workq[m][n].m_boq = 0;",
" } } }",
"",
" if (must_exit)",
" { rm_shared_segments();",
" fprintf(stderr, \"pan: check './pan --' for usage details\\n\");",
" pan_exit(1); /* calls cleanup_shm */",
" }",
"}",
"",
"static uchar *",
"prep_shmid_S(size_t n) /* either sets SS or H_tab, linux/cygwin */",
"{ char *rval;",
"#ifndef SEP_STATE",
" key_t key;",
"",
" if (verbose && core_id == 0)",
" {",
" #ifdef BITSTATE",
" printf(\"cpu0: step 1: allocate shared bitstate %%g Mb\\n\",",
" (double) n / (1048576.));",
" #else",
" printf(\"cpu0: step 1: allocate shared hastable %%g Mb\\n\",",
" (double) n / (1048576.));",
" #endif",
" }",
" #ifdef MEMLIM", /* memlim has a value */
" if (memcnt + (double) n > memlim)",
" { printf(\"cpu0: S %%8g + %%d Kb exceeds memory limit of %%8g Mb\\n\",",
" memcnt/1024., (int) (n/1024), memlim/(1048576.));",
" printf(\"cpu0: insufficient memory -- aborting\\n\");",
" exit(1);",
" }",
" #endif",
"",
" key = ftok(PanSource, NCORE+2); /* different from queues */",
" if (key == -1)",
" { perror(\"ftok shared bitstate or hashtable\");",
" fprintf(stderr, \"pan: check './pan --' for usage details\\n\");",
" pan_exit(1);",
" }",
"",
" if (core_id == 0) /* root */",
" { shmid_S = shmget(key, n, 0600);",
" if (shmid_S != -1)",
" { printf(\"cpu0: removing stale segment, status: %%d\\n\",",
" (int) shmctl(shmid_S, IPC_RMID, NULL));",
" }",
" shmid_S = shmget(key, n, 0600 | IPC_CREAT | IPC_EXCL);",
" memcnt += (double) n;",
" } else /* worker */",
" { shmid_S = shmget(key, n, 0600);",
" }",
" if (shmid_S == -1)",
" { perror(\"shmget shared bitstate or hashtable too large?\");",
" fprintf(stderr, \"pan: check './pan --' for usage details\\n\");",
" pan_exit(1);",
" }",
"",
" rval = (char *) shmat(shmid_S, (void *) 0, 0); /* attach */",
" if ((char *) rval == (char *) -1)",
" { perror(\"shmat shared bitstate or hashtable\");",
" fprintf(stderr, \"pan: check './pan --' for usage details\\n\");",
" pan_exit(1);",
" }",
"#else",
" rval = (char *) emalloc(n);",
"#endif",
" return (uchar *) rval;",
"}",
"",
"#define TRY_AGAIN 1",
"#define NOT_AGAIN 0",
"",
"static char shm_prep_result;",
"",
"static uchar *",
"prep_state_mem(size_t n) /* sets memory arena for states linux/cygwin */",
"{ char *rval;",
" key_t key;",
" static int cnt = 3; /* start larger than earlier ftok calls */",
"",
" shm_prep_result = NOT_AGAIN; /* default */",
" if (verbose && core_id == 0)",
" { printf(\"cpu0: step 2+: pre-allocate memory arena %%d of %%6.2g Mb\\n\",",
" cnt-3, (double) n / (1048576.));",
" }",
" #ifdef MEMLIM",
" if (memcnt + (double) n > memlim)",
" { printf(\"cpu0: error: M %%.0f + %%.0f Kb exceeds memory limit of %%.0f Mb\\n\",",
" memcnt/1024.0, (double) n/1024.0, memlim/(1048576.));",
" return NULL;",
" }",
" #endif",
"",
" key = ftok(PanSource, NCORE+cnt); cnt++;", /* starts at NCORE+3 */
" if (key == -1)",
" { perror(\"ftok T\");",
" printf(\"pan: check './pan --' for usage details\\n\");",
" pan_exit(1);",
" }",
"",
" if (core_id == 0)",
" { shmid_M = shmget(key, n, 0600);",
" if (shmid_M != -1)",
" { printf(\"cpu0: removing stale memory segment %%d, status: %%d\\n\",",
" cnt-3, shmctl(shmid_M, IPC_RMID, NULL));",
" }",
" shmid_M = shmget(key, n, 0600 | IPC_CREAT | IPC_EXCL);",
" /* memcnt += (double) n; -- only amount actually used is counted */",
" } else",
" { shmid_M = shmget(key, n, 0600);",
" ",