-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycleInterval.c
2222 lines (1817 loc) · 55.4 KB
/
cycleInterval.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
/*
$Log: cycleInterval.c,v $
Revision 1.6 1999/07/15 01:00:31 dbader
Increased the memory allocation for initial transarcs.
Revision 1.5 1999/07/14 09:55:01 dbader
Removed PEG Format.
Changed expGraph format not to record every express arc,
rather, just the convex notation of the interval of reachable
exit vertices.
Revision 1.4 1999/07/14 09:24:09 dbader
Modified Find_Local_Cycles to record the initial transarcs,
thus, removing the second local DFS from the Discovery routine.
Revision 1.3 1999/07/14 02:01:28 dbader
Modified the Discovery routine to use convex notation rather than reachlists.
Revision 1.2 1999/07/12 21:27:09 dbader
Working copy of the new Interval Graph representation.
Revision 1.1 1999/07/12 15:50:04 dbader
Initial revision
Revision 1.6 1999/07/11 15:42:13 dbader
Fixed cases where a NULL ptr is freed.
Revision 1.5 1999/07/11 13:57:34 dbader
Removed old Packed Convex Graph (PCG) representation code.
Fixed a minor bug with PIG --> an uninitialized pointer that was being
freed.
Revision 1.4 1999/07/11 03:56:50 dbader
Added Packed Interval Graph (PIG) representation, analog to
the Packed Express Graph (PEG). Instead of storing express arcs,
intervals are saved instead. Intervals can be used since the input
contains convex bipartite graphs on each processor. This
drastically reduces the packed graph size.
Revision 1.3 1999/07/05 01:44:06 dbader
Split Express Graph LUT into entr and exit vertices, and
removed entrLUT.
Revision 1.2 1999/07/04 20:24:40 dbader
Modified the code so that a Lattice Input Graph could be used
in the current framework. This involved modifying several functions
that map between index and label, so that it is not assumed that
an index has a linear mapping to the vertices' labels.
Also, since several index to label (and vice versa) mapping functions
exist that are input-dependent, a new style for choosing inputs is
given in the code preamble.
Revision 1.1 1999/07/03 23:03:44 dbader
Initial revision
Revision 1.1 1999/07/03 23:02:53 dbader
Initial revision
Revision 1.27 1999/07/03 23:00:39 dbader
Converted assert_malloc to SAFE_MALLOC
Revision 1.26 1999/07/03 22:42:35 dbader
PRINT_MERGESIZE defined
Revision 1.25 1999/06/23 21:39:52 dbader
Changed order of loop in Free_Input so that Linux wouldn't hang.
Revision 1.24 1999/06/08 19:01:18 dbader
Added -DUSE_RAND option to select rand()/srand() instead of
random()/srandom().
Revision 1.23 1999/06/08 18:57:30 dbader
Added printing of input graph description
Revision 1.22 1999/06/07 16:51:49 dbader
Changed packed express graph express-arc lists on entrance
vertices to be kept in sorted order per each entrance vertex.
Linear searches for express arcs have been changed to binary
searches.
Revision 1.21 1999/06/05 21:38:37 dbader
Fixed a color[a->index] -> color[aidx] problem in Phase 1.
Added a new graph type with local cycles.
Fixed several warning conditions (unused or unset variables)
Revision 1.20 1999/06/04 12:58:16 dbader
Changed DEBUG_PRINT to define.
Revision 1.19 1999/06/04 12:21:26 dbader
Fixed memory leak in CleanUp function.
Revision 1.18 1999/06/03 12:45:09 dbader
Changed reporting to include problem size.
Revision 1.17 1999/06/02 13:26:48 dbader
Discontinued detection if a cycle is detected during the local phase.
Revision 1.16 1999/06/02 03:25:42 dbader
Added argc/argv tests.
Revision 1.15 1999/06/02 03:21:24 dbader
Miscellaneous minor changes to N_DEFAULT
Revision 1.14 1999/06/02 01:28:24 dbader
Added new assert_malloc()
Revision 1.13 1999/06/01 21:17:18 dbader
Fixed misplaced RemoveVertex call.
Revision 1.12 1999/06/01 19:50:28 dbader
Added timing features.
Revision 1.11 1999/06/01 13:15:12 dbader
Detected multiple exit vertices with the same label.
WORKING implementation at this point.
Merge Express Packed graphs finished.
Revision 1.10 1999/05/29 15:52:28 dbader
Implementing the merge of packed express graphs with new arcs and deleted
vertices. When a vertex is deleted, it's marked with a negative label.
Arcs are added to a linked list, and then the CleanUp routine flattens
these changes back to a packed express graph
Revision 1.9 1999/05/28 23:06:21 dbader
Added more merge testing, printing out new express arcs.
Revision 1.8 1999/05/28 17:56:41 dbader
Added check for cycles during merge.
Correctly identifies predessors of exit0 and
successors of entr1 during merge.
Revision 1.7 1999/05/28 14:00:21 dbader
Determined an error: after an express merge, an entrance
vertex can have adjacent transarcs from two different
exit vertices from different processors.
Revision 1.6 1999/05/27 23:17:10 dbader
Daily Update 990527a
Revision 1.5 1999/05/27 19:25:13 dbader
Daily Update 990527
Revision 1.4 1999/05/27 19:22:55 dbader
Daily Update 990526
Revision 1.3 1999/05/27 19:20:54 dbader
Daily Update 990525
Revision 1.2 1999/05/27 19:15:54 dbader
Daily Update 990523
Revision 1.1 1999/05/27 19:09:08 dbader
Initial revision
*/
static char rcsid[] = "$Id: cycleInterval.c,v 1.6 1999/07/15 01:00:31 dbader Exp $";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mpi.h"
#include "misc.h"
#include "cycleInterval.h"
#include "queue.h"
#include "mpi-printf.h"
#include "timing.h"
#define N_DEFAULT (1<<10)
#define MAXARCS 4
#define INPUTCYCLE 1
#define PRINT_MERGESIZE 0
/*********************************************************/
#define INPUT_LATTICE 0
#define INPUT_LINEAR 1
#define INPUT_LOCALCYCLES 2
#define INPUT_NUM INPUT_LATTICE
/**********/
void Create_Input_Lattice(int, vertexList_t *);
void Create_Input_Linear(int, vertexList_t *);
void Create_Input_LocalCycles(int, vertexList_t *);
void (*Create_Input_func[])() = {
Create_Input_Lattice,
Create_Input_Linear,
Create_Input_LocalCycles
};
#define Create_Input(n, verts) ((Create_Input_func[INPUT_NUM])((n), (verts)))
/**********/
int localIndex_Lattice(vertexList_t, int);
int localIndex_Default(vertexList_t, int);
int (*localIndex_func[])() = {
localIndex_Lattice,
localIndex_Default, /* Linear */
localIndex_Default /* LocalCycles */
};
#define localIndex(verts, n) ((localIndex_func[INPUT_NUM])((verts), (n)))
/*********************************************************/
BOOL CYCLE_FOUND;
int MYNODE, NODES;
FILE *outfile;
FILE *errfile;
char *INPUT_GRAPH_NAME;
#define MPI_TAG_SIZES 0
#define MPI_TAG_DATA 1
#define ENTRV 0
#define EXITV 1
int randmax(int m) {
/* Return a random integer (0 <= r < m) */
#ifdef USE_RAND
return(rand() % m);
#else
return(random() % m);
#endif
}
int randrange(int a, int b) {
/* Return a random integer (a <= r < b) */
int range;
range = b-a;
return (randmax(range) + a);
}
#if 0
static int intCompare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
#endif
static int transArcCompareHead(const void *a, const void *b) {
return (((transArc_t *)a)->headAssn - ((transArc_t *)b)->headAssn);
}
static int expGraphLabelCompare(const void *a, const void *b) {
return (((labelLookup_t)a)->label - ((labelLookup_t)b)->label);
}
int calcLatticeLabel(int offset, int vLocRow, int vLocCol, int nRow, int nCol) {
int lab;
if ((vLocRow == 0) || (vLocRow == nRow-1) ||
(vLocCol == 0) || (vLocCol == nCol-1)) {
/* Boundary vertex */
if (vLocRow==0) {
/* Top Row, left to right */
lab = vLocCol;
}
else {
if (vLocCol == nCol-1) {
/* Right Column, top to bottom */
lab = nCol + vLocRow - 1;
}
else {
if (vLocRow == nRow-1) {
/* Bottom Row, right to left */
lab = 2*nCol + nRow - vLocCol - 3;
}
else /* (vLocCol == 0) */ {
/* Left Column, bottom to top */
lab = 2*(nCol+nRow) - vLocRow - 4;
}
}
}
}
else {
/* Interior vertex */
lab = 2*(nRow + nCol) - 4;
lab += (vLocRow-1)*(nCol-2) + (vLocCol-1);
}
return (offset+lab);
}
void Create_Input_Lattice(int n, vertexList_t *verts) {
int num;
int offset;
int i;
vertex_t v;
arc_t a;
int pRow, pCol, myRow, myCol, nRow, nCol, vLoc, vLocRow, vLocCol;
MPI_Barrier(MPI_COMM_WORLD);
#if INPUTCYCLE
INPUT_GRAPH_NAME = "Lattice with cycle";
#else
INPUT_GRAPH_NAME = "Lattice with no cycle";
#endif
*verts = (vertexList_t)SAFE_MALLOC(sizeof(struct vertexList_s),
"(cycle.c) *verts");
num = (*verts)->num = (int)floor((double)n/(double)NODES);
(*verts)->vlist = (vertex_t)SAFE_MALLOC(num*sizeof(struct vertex_s),
"(cycle.c) (*verts)->vlist");
pRow = (int)floor(sqrt((double)NODES));
pCol = (int)floor(sqrt((double)NODES));
if (pRow*pCol != NODES) {
fprintf(errfile,"ERROR: Must use a square number of procs\n");
/* exit(-1); */
}
myRow = MYNODE / pCol;
myCol = MYNODE % pCol;
nRow = (int)floor(sqrt((double)num));
nCol = (int)floor(sqrt((double)num));
if (nRow*nCol != num) {
fprintf(errfile,"ERROR: Must use a square for vertices per proc\n");
/* exit(-1); */
}
offset = num*MYNODE;
for (i=0 ; i<num ; i++) {
v = (*verts)->vlist + i;
vLoc = i;
vLocRow = vLoc / nCol;
vLocCol = vLoc % nCol;
v->label = calcLatticeLabel(offset, vLocRow, vLocCol, nRow, nCol);
v->C0 = -1;
v->C1 = -1;
v->arcs = 0;
if (vLocCol < nCol - 1)
v->arcs++;
if (vLocRow < nRow - 1)
v->arcs++;
if ((vLocCol == nCol-1) && (myCol < pCol-1))
v->arcs++;
if ((vLocRow == nRow-1) && (myRow < pRow-1))
v->arcs++;
#if INPUTCYCLE
/* Add arc from last vertex to first vertex */
#if 0
if (v->label == (num*NODES)-1) {
v->arcs++;
}
#else
if ((MYNODE == NODES-1) && (vLocRow == nRow-1) && (vLocCol == nCol-1)) {
v->arcs++;
}
#endif
#endif
if (v->arcs > 0)
v->alist = (arc_t)SAFE_MALLOC((v->arcs)*sizeof(struct arc_s),
"(cycle.c) v->alist");
else
v->alist = (arc_t)NULL;
a = v->alist;
if (vLocCol < nCol - 1) {
a->head = calcLatticeLabel(offset, vLocRow, vLocCol+1, nRow, nCol);
a->assn = MYNODE;
a++;
}
if (vLocRow < nRow - 1) {
a->head = calcLatticeLabel(offset, vLocRow+1, vLocCol, nRow, nCol);
a->assn = MYNODE;
a++;
}
if ((vLocCol == nCol-1) && (myCol < pCol-1)) {
a->head = calcLatticeLabel(offset+num, vLocRow, 0, nRow, nCol);
a->assn = MYNODE + 1;
a++;
}
if ((vLocRow == nRow-1) && (myRow < pRow-1)) {
a->head = calcLatticeLabel(offset+(pCol*num), 0, vLocCol, nRow, nCol);
a->assn = MYNODE + pCol;
a++;
}
#if INPUTCYCLE
/* Add arc from last vertex to first vertex */
#if 0
if (v->label == (num*NODES)-1)
#else
if ((MYNODE == NODES-1) && (vLocRow == nRow-1) && (vLocCol == nCol-1))
#endif
{
a->head = 0;
a->assn = 0;
a++;
}
#endif
}
(*verts)->color = (unsigned char *)SAFE_MALLOC(num*sizeof(unsigned char),
"(cycle.c) (*verts)->color");
return;
}
void Create_Input_Linear(int n, vertexList_t *verts) {
int num;
int offset;
int i;
vertex_t v;
arc_t a;
MPI_Barrier(MPI_COMM_WORLD);
#if INPUTCYCLE
INPUT_GRAPH_NAME = "Linear with cycle";
#else
INPUT_GRAPH_NAME = "Linear with no cycle";
#endif
*verts = (vertexList_t)SAFE_MALLOC(sizeof(struct vertexList_s),
"(cycle.c) *verts");
num = (*verts)->num = (int)floor((double)n/(double)NODES);
(*verts)->vlist = (vertex_t)SAFE_MALLOC(num*sizeof(struct vertex_s),
"(cycle.c) (*verts)->vlist");
if (num*NODES != n) {
fprintf(errfile,"ERROR: n does not divide p evenly\n");
/* exit(-1); */
}
offset = num*MYNODE;
for (i=0 ; i<num ; i++) {
v = (*verts)->vlist + i;
v->label = offset + i;
v->C0 = -1;
v->C1 = -1;
v->arcs = 1;
if (v->label == n-1) {
#if INPUTCYCLE
v->arcs = 1;
#else
v->arcs = 0;
#endif
}
v->alist = (arc_t)SAFE_MALLOC((v->arcs)*sizeof(struct arc_s),
"(cycle.c) v->alist");
a = v->alist;
if (v->label < n-1) {
a->head = v->label + 1;
a->assn = MYNODE;
if (i==num-1)
a->assn++;
}
else {
#if INPUTCYCLE
a->head = 0;
a->assn = 0;
#endif
}
}
(*verts)->color = (unsigned char *)SAFE_MALLOC(num*sizeof(unsigned char),
"(cycle.c) (*verts)->color");
return;
}
void Create_Input_LocalCycles(int n, vertexList_t *verts) {
int num;
int offset;
int i;
vertex_t v;
arc_t a;
MPI_Barrier(MPI_COMM_WORLD);
INPUT_GRAPH_NAME = "LocalCycles";
*verts = (vertexList_t)SAFE_MALLOC(sizeof(struct vertexList_s),
"(cycle.c) *verts");
num = (*verts)->num = (int)floor((double)n/(double)NODES);
(*verts)->vlist = (vertex_t)SAFE_MALLOC(num*sizeof(struct vertex_s),
"(cycle.c) (*verts)->vlist");
if (num*NODES != n) {
fprintf(errfile,"ERROR: n does not divide p evenly\n");
/* exit(-1); */
}
offset = num*MYNODE;
for (i=0 ; i<num ; i++) {
v = (*verts)->vlist + i;
v->label = offset + i;
v->C0 = -1;
v->C1 = -1;
v->arcs = 1;
v->alist = (arc_t)SAFE_MALLOC((v->arcs)*sizeof(struct arc_s),
"(cycle.c) v->alist");
a = v->alist;
if (i < num-1) {
a->head = v->label + 1;
a->assn = MYNODE;
}
else {
a->head = offset;
a->assn = MYNODE;
}
}
(*verts)->color = (unsigned char *)SAFE_MALLOC(num*sizeof(unsigned char),
"(cycle.c) (*verts)->color");
return;
}
void Print_Input(vertexList_t verts) {
int i, k, p;
vertex_t v;
arc_t a;
MPI_Barrier(MPI_COMM_WORLD);
for (p=0 ; p<NODES ; p++) {
if (p==MYNODE) {
fprintf(outfile,"PE%3d: vertices: %12d\n",MYNODE, verts->num);
for (i=0 ; i<verts->num ; i++) {
v = verts->vlist + i;
fprintf(outfile,"PE%3d: vert[%12d]: label %12d arcs %2d\n",
MYNODE, i,v->label,v->arcs);
for (k=0 ; k<v->arcs ; k++) {
a = v->alist + k;
fprintf(outfile,"PE%3d: vert[%12d]: arc[%2d]: (%12d, %3d)\n",
MYNODE, i, k,
a->head, a->assn);
}
}
fflush(outfile);
}
MPI_Barrier(MPI_COMM_WORLD);
}
return;
}
int localIndex_Default(vertexList_t verts, int label) {
int idx;
/* Map the local vertex label to index */
if ((label / verts->num) != MYNODE)
fprintf(errfile,
"PE%3d: ERROR: localIndex of label off processor! l:%d n:%d d:%d\n",
MYNODE, label, verts->num, label/verts->num);
idx = label % verts->num;
if (verts->vlist[idx].label != label)
fprintf(errfile, "PE%3d: ERROR: localIndex of label %d incorrect.\n",
MYNODE, label);
return(idx);
}
int localIndex_Lattice(vertexList_t verts, int label) {
int idx;
int nRow, nCol;
int vLocRow, vLocCol;
int borderCnt, locLabel;
/* Map the local vertex label to index */
if ((label / verts->num) != MYNODE)
fprintf(errfile,
"PE%3d: ERROR: localIndex of label off processor! l:%d n:%d d:%d\n",
MYNODE, label, verts->num, label/verts->num);
nRow = (int)floor(sqrt((double)verts->num));
nCol = (int)floor(sqrt((double)verts->num));
locLabel = label - (verts->num * MYNODE);
borderCnt = 2*(nRow + nCol) - 4;
if (locLabel < borderCnt) {
/* Boundary */
if (locLabel < nCol) {
/* Top Row */
vLocRow = 0;
vLocCol = locLabel;
}
else {
if (locLabel < nCol + nRow - 1) {
/* Right Column */
vLocRow = locLabel - nCol + 1;
vLocCol = nCol - 1;
}
else {
if (locLabel < 2*nCol + nRow - 2) {
/* Bottom Row */
vLocRow = nRow - 1;
vLocCol = 2*nCol + nRow -locLabel - 3;
}
else {
/* Left Column */
vLocRow = 2*(nCol+nRow) - locLabel - 4;
vLocCol = 0;
}
}
}
}
else {
/* Interior */
if (nCol >= 2) {
locLabel -= borderCnt;
vLocRow = locLabel / (nCol - 2);
vLocRow++;
vLocCol = locLabel % (nCol - 2);
vLocCol++;
}
else {
/* Single vertex */
vLocRow =0;
vLocCol =0;
}
}
idx = vLocRow * nCol + vLocCol;
if ((idx < 0) || (idx >= verts->num))
fprintf(errfile, "PE%3d: ERROR: localIndex_Lattice %d incorrect. idx OOB\n",
MYNODE, label);
if (verts->vlist[idx].label != label)
fprintf(errfile, "PE%3d: ERROR: localIndex of label %d incorrect.\n",
MYNODE, label);
return(idx);
}
int globalLabel(vertexList_t verts, int idx) {
/* Map the local index into the vertex label */
int label;
if ((idx < 0) || (idx >= verts->num))
fprintf(errfile,"PE%3d: ERROR: globalLabel of idx bad!\n",MYNODE);
#if 0
label = idx + (verts->num * MYNODE);
if (label != verts->vlist[idx].label)
fprintf(errfile,"PE%3d: ERROR: globalLabel (%d) of idx (%d) doesn't match!\n",
MYNODE, label, idx);
#else
label = verts->vlist[idx].label;
#endif
return(label);
}
void visitLocal(vertexList_t verts, int vidx,
int *transArcNum, transArc_t *initTransArcs) {
int i;
vertex_t v;
arc_t a;
int aidx;
transArc_t *tarc;
verts->color[vidx] = RED;
v = verts->vlist + vidx;
for (i=0 ; i<v->arcs ; i++) {
a = v->alist + i;
if (a->assn == MYNODE) { /* local-arc */
#if 1
/* CHECK */
aidx = localIndex(verts, a->head);
if ((aidx < 0) || (aidx > verts->num)) {
fprintf(errfile,"PE%3d: ERROR: aidx (%d) > verts->num (%d)\n",
MYNODE, aidx, verts->num);
}
#endif
switch (verts->color[aidx]) {
case WHITE:
visitLocal(verts, aidx, transArcNum, initTransArcs);
break;
case RED:
fprintf(outfile,"PE%3d: A local cycle (%d, %d) has been found!\n",
MYNODE, v->label, a->head);
CYCLE_FOUND = TRUE;
break;
case BLACK:
default: ;
}
#if 1
if (verts->vlist[vidx].C0 < 0) {
verts->vlist[vidx].C0 = verts->vlist[aidx].C0;
verts->vlist[vidx].C1 = verts->vlist[aidx].C1;
}
else {
verts->vlist[vidx].C0 = min(verts->vlist[vidx].C0, verts->vlist[aidx].C0);
verts->vlist[vidx].C1 = max(verts->vlist[vidx].C1, verts->vlist[aidx].C1);
}
#endif
}
else {
tarc = initTransArcs + *transArcNum;
tarc->tail = v->label;
tarc->head = a->head;
tarc->tailAssn = MYNODE;
tarc->headAssn = a->assn;
(*transArcNum)++;
if (verts->vlist[vidx].C0 < 0) {
verts->vlist[vidx].C0 = v->label;
verts->vlist[vidx].C1 = v->label;
}
else {
verts->vlist[vidx].C0 = min(verts->vlist[vidx].C0, v->label);
verts->vlist[vidx].C1 = max(verts->vlist[vidx].C1, v->label);
}
}
}
verts->color[vidx] = BLACK;
return;
}
void Find_Local_Cycles(vertexList_t verts, int *transArcNum,
transArc_t **transArcs) {
int i, num;
num = verts->num;
*transArcNum = 0;
*transArcs = (transArc_t *)SAFE_MALLOC(MAXARCS * num * sizeof(transArc_t),
"(cycle.c) transArcs");
for (i=0 ; i<num ; i++)
verts->color[i] = WHITE;
for (i=0 ; i<num ; i++) {
if ((verts->color[i] == WHITE) && (!CYCLE_FOUND)) {
/* vertex i has not yet been visited */
visitLocal(verts, i, transArcNum, *transArcs);
}
}
return;
}
void Discovery(vertexList_t verts,
int initTransArcCount,
transArc_t *initTransArcs,
int *termTransArcCount,
transArc_t **termTransArcs)
{
int i;
int *send_cnt, *send_offset;
int *recv_cnt, *recv_offset;
MPI_Datatype MPI_TRANSARC;
#ifdef DEBUG_PRINT
MPI_fprintf(outfile,"I have %d initial transarc%s\n",
initTransArcCount, (initTransArcCount==1)?"":"s");
#endif
if (initTransArcs > 0)
qsort(initTransArcs, initTransArcCount,
sizeof(transArc_t), transArcCompareHead);
#ifdef DEBUG_PRINT
{
int k;
MPI_Barrier(MPI_COMM_WORLD);
for (k=0 ; k<NODES ; k++) {
if (MYNODE==k) {
for (i=0 ; i<initTransArcCount ; i++) {
fprintf(outfile,"PE%3d: Send init arc[%2d]: (%3d, %3d, %3d, %3d)\n",
MYNODE, i,
initTransArcs[i].tail,
initTransArcs[i].head,
initTransArcs[i].tailAssn,
initTransArcs[i].headAssn);
}
fflush(outfile);
}
MPI_Barrier(MPI_COMM_WORLD);
}
}
#endif
send_cnt = (int *)SAFE_MALLOC(NODES*sizeof(int),
"(cycle.c) send_cnt");
send_offset = (int *)SAFE_MALLOC(NODES*sizeof(int),
"(cycle.c) send_offset");
recv_cnt = (int *)SAFE_MALLOC(NODES*sizeof(int),
"(cycle.c) recv_cnt");
recv_offset = (int *)SAFE_MALLOC(NODES*sizeof(int),
"(cycle.c) recv_offset");
for (i=0 ; i<NODES ; i++)
send_cnt[i] = 0;
for (i=0 ; i<initTransArcCount ; i++)
send_cnt[initTransArcs[i].headAssn]++;
send_offset[0] = 0;
for (i=1 ; i<NODES ; i++)
send_offset[i] = send_offset[i-1] + send_cnt[i-1];
MPI_Alltoall(send_cnt, 1, MPI_INT,
recv_cnt, 1, MPI_INT,
MPI_COMM_WORLD);
recv_offset[0] = 0;
for (i=1 ; i<NODES ; i++)
recv_offset[i] = recv_offset[i-1] + recv_cnt[i-1];
*termTransArcCount = recv_offset[NODES-1] + recv_cnt[NODES-1];
*termTransArcs = (transArc_t *)SAFE_MALLOC(*termTransArcCount * sizeof(transArc_t),
"(cycle.c) *termTransArcs");
MPI_Type_contiguous(sizeof(transArc_t)/sizeof(int),MPI_INT,&MPI_TRANSARC);
MPI_Type_commit(&MPI_TRANSARC);
#ifdef DEBUG_PRINT
{
int k;
MPI_Barrier(MPI_COMM_WORLD);
for (k=0 ; k<NODES ; k++) {
if (MYNODE==k) {
for (i=0 ; i<NODES ; i++) {
fprintf(outfile,"PE%3d: [%d] send_cnt: %d recv_cnt: %d send_offset: %d recv_offset: %d\n",
MYNODE, i,
send_cnt[i], recv_cnt[i],
send_offset[i], recv_offset[i]);
}
fflush(outfile);
}
MPI_Barrier(MPI_COMM_WORLD);
}
}
#endif
MPI_Alltoallv(initTransArcs, send_cnt, send_offset, MPI_TRANSARC,
*termTransArcs, recv_cnt, recv_offset, MPI_TRANSARC,
MPI_COMM_WORLD);
MPI_Type_free(&MPI_TRANSARC);
#ifdef DEBUG_PRINT
{
int k;
MPI_Barrier(MPI_COMM_WORLD);
for (k=0 ; k<NODES ; k++) {
if (MYNODE==k) {
for (i=0 ; i<*termTransArcCount ; i++) {
fprintf(outfile,"PE%3d: Rec'd term arc[%2d]: (%3d, %3d, %3d, %3d)\n",
MYNODE, i,
(*termTransArcs)[i].tail,
(*termTransArcs)[i].head,
(*termTransArcs)[i].tailAssn,
(*termTransArcs)[i].headAssn);
}
fflush(outfile);
}
MPI_Barrier(MPI_COMM_WORLD);
}
}
#endif
MPI_Barrier(MPI_COMM_WORLD);
free(recv_offset);
free(recv_cnt);
free(send_offset);
free(send_cnt);
return;
}
void AddEntrVertex(eGraph_t expGraph, transArc_t *tarc) {
expVertex_t entrVertex;
entrVertex = (expVertex_t)SAFE_MALLOC(sizeof(struct expVertex_s),
"(cycle.c) entrVertex");
entrVertex->transArc = tarc;
entrVertex->next = expGraph->entrV;
expGraph->entrV = entrVertex;
expGraph->entrNum++;
#if ENTR_LUT
(expGraph->entrLUT + expGraph->entrLUTNum)->label = entrVertex->transArc->head;
(expGraph->entrLUT + expGraph->entrLUTNum)->expVertex = entrVertex;
expGraph->entrLUTNum++;
#endif
return;
}
void AddExitVertex(eGraph_t expGraph, transArc_t *tarc) {
expVertex_t exitVertex;
exitVertex = (expVertex_t)SAFE_MALLOC(sizeof(struct expVertex_s),
"(cycle.c) exitVertex");
exitVertex->transArc = tarc;
exitVertex->next = expGraph->exitV;
expGraph->exitV = exitVertex;
expGraph->exitNum++;
(expGraph->exitLUT + expGraph->exitLUTNum)->label = exitVertex->transArc->tail;
(expGraph->exitLUT + expGraph->exitLUTNum)->expVertex = exitVertex;
expGraph->exitLUTNum++;
return;
}
int binSearchLUT(labelLookup_t lut, int minIdx, int maxIdx, int target) {
/* Search searchList recursively using binary search to find target.
Return -1 if it does not exist, or target's index if it does. */
register int midIdx;
midIdx = minIdx + (maxIdx - minIdx) / 2;
if (maxIdx < minIdx)
return(-1);
else if (target < lut[midIdx].label)
return binSearchLUT(lut, minIdx, midIdx - 1, target);
else if (target > lut[midIdx].label)
return binSearchLUT(lut, midIdx + 1, maxIdx , target);
else
return midIdx;
}
#if ENTR_LUT
expVertex_t getEntrVertexLUT(eGraph_t expGraph, int label) {
int idx;
expVertex_t v;
idx = binSearchLUT(expGraph->entrLUT, 0, expGraph->entrLUTNum - 1, label);
if (idx>=0)
v = expGraph->entrLUT[idx].expVertex;
else
v = (expVertex_t)NULL;
return v;
}
#endif
expVertex_t getExitVertexLUT(eGraph_t expGraph, int label) {
int idx;
expVertex_t v;
idx = binSearchLUT(expGraph->exitLUT, 0, expGraph->exitLUTNum - 1, label);
if (idx>=0)
v = expGraph->exitLUT[idx].expVertex;
else
v = (expVertex_t)NULL;
return v;
}
void AddExpressArcSet(vertexList_t verts, eGraph_t expGraph,
expVertex_t entrVertex) {
int idx;
int vlabel;
vlabel = entrVertex->transArc->head;
idx = localIndex(verts, vlabel);
entrVertex->C0 = verts->vlist[idx].C0;
entrVertex->C1 = verts->vlist[idx].C1;
return;
}
void SortExpGraphLUT(eGraph_t expGraph) {
#if ENTR_LUT
qsort(expGraph->entrLUT, expGraph->entrLUTNum, sizeof(struct labelLookup_s),
expGraphLabelCompare);
#endif
qsort(expGraph->exitLUT, expGraph->exitLUTNum, sizeof(struct labelLookup_s),