-
Notifications
You must be signed in to change notification settings - Fork 0
/
connComp_grey_4.sc
1864 lines (1469 loc) · 53.1 KB
/
connComp_grey_4.sc
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
/* tab:8
*
* connComp_grey_4.sc - 4-connected components of grey scale images
*
*
* "Copyright (c) 1994,1995 The Regents of the University of Maryland.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF MARYLAND HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Authors: David A. Bader <dbader@umiacs.umd.edu>
* Joseph F. Ja'Ja' <joseph@umiacs.umd.edu>
* Institute for Advanced Computer Studies
* Department of Electrical Engineering
* AV Williams Building
* College Park, MD 20742
*
* Version: 1.0
* Creation Date: October 20, 1994
* Filename: connComp_grey_4.sc
* History:
*/
#include "connComp_grey_4.h"
static inline void BFS_grey_4(int v, int w, int q, int r, int ii, int jj,
int (*image)[r], int (*mark)[r], int (*lab)[r]) {
Queue_type Q;
register int
i, j, x, y,
newLabel,
curcolor;
Q.count = 0;
Q.front = 0;
Q.rear = -1;
Q.MAXQ = q*r;
if ((Q.entryI = (int*)(malloc(Q.MAXQ*sizeof(int))))==NULL)
fprintf(stderr,"ERROR: entryI could not be malloc'ed\n");
if ((Q.entryJ = (int*)(malloc(Q.MAXQ*sizeof(int))))==NULL)
fprintf(stderr,"ERROR: entryJ could not be malloc'ed\n");
for (x=0 ; x<q ; x++)
for (y=0 ; y<r ; y++) {
if (!mark[x][y]) {
if (Q.count >= Q.MAXQ) {
fprintf(stderr,"Queue is FULL\n");
exit(1);
}
Q.count++;
Q.rear = (Q.rear + 1) % Q.MAXQ;
Q.entryI[Q.rear] = x;
Q.entryJ[Q.rear] = y;
mark[x][y] = BLACK;
newLabel = (ii*q + x)*q*v + (jj*r + y) + 1;
curcolor = image[x][y];
do {
if (Q.count <= 0) {
fprintf(stderr,"Queue is EMPTY\n");
exit(1);
}
Q.count--;
i = Q.entryI[Q.front];
j = Q.entryJ[Q.front];
Q.front = (Q.front + 1) % Q.MAXQ;
lab[i][j] = newLabel; /* Visit(i,j) */
if ((j<r-1) && !mark[i][j+1] && curcolor==image[i][j+1]) {
if (Q.count >= Q.MAXQ) {
fprintf(stderr,"Queue is FULL\n");
exit(1);
}
Q.count++;
Q.rear = (Q.rear + 1) % Q.MAXQ;
Q.entryI[Q.rear] = i;
Q.entryJ[Q.rear] = j+1;
mark[i][j+1] = BLACK;
}
if ((i<q-1) && !mark[i+1][j] && curcolor==image[i+1][j]) {
if (Q.count >= Q.MAXQ) {
fprintf(stderr,"Queue is FULL\n");
exit(1);
}
Q.count++;
Q.rear = (Q.rear + 1) % Q.MAXQ;
Q.entryI[Q.rear] = i+1;
Q.entryJ[Q.rear] = j;
mark[i+1][j] = BLACK;
}
if ((j>0) && !mark[i][j-1] && curcolor==image[i][j-1]) {
if (Q.count >= Q.MAXQ) {
fprintf(stderr,"Queue is FULL\n");
exit(1);
}
Q.count++;
Q.rear = (Q.rear + 1) % Q.MAXQ;
Q.entryI[Q.rear] = i;
Q.entryJ[Q.rear] = j-1;
mark[i][j-1] = BLACK;
}
if ((i>0) && !mark[i-1][j] && curcolor==image[i-1][j]) {
if (Q.count >= Q.MAXQ) {
fprintf(stderr,"Queue is FULL\n");
exit(1);
}
Q.count++;
Q.rear = (Q.rear + 1) % Q.MAXQ;
Q.entryI[Q.rear] = i-1;
Q.entryJ[Q.rear] = j;
mark[i-1][j] = BLACK;
}
} while (!(Q.count <= 0));
}
}
free(Q.entryI);
free(Q.entryJ);
}
static inline void BFS_grey_4_recolor(int q, int r, int x, int y, int (*lab)[r],
int (*image)[r], int (*mark)[r], int newLabel) {
Queue_type Q;
register int
i, j,
curcolor;
if (!mark[x][y]) {
mark[x][y] = BLACK;
curcolor = image[x][y];
Q.count = 1;
Q.front = 0;
Q.rear = 0;
Q.MAXQ = q*r;
if ((Q.entryI = (int*)(malloc(Q.MAXQ*sizeof(int))))==NULL)
fprintf(stderr,"ERROR: entryI could not be malloc'ed\n");
if ((Q.entryJ = (int*)(malloc(Q.MAXQ*sizeof(int))))==NULL)
fprintf(stderr,"ERROR: entryJ could not be malloc'ed\n");
Q.entryI[Q.rear] = x;
Q.entryJ[Q.rear] = y;
do {
if (Q.count <= 0) {
fprintf(stderr,"Queue is EMPTY\n");
exit(1);
}
Q.count--;
i = Q.entryI[Q.front];
j = Q.entryJ[Q.front];
Q.front = (Q.front + 1) % Q.MAXQ;
lab[i][j] = newLabel;
if ((j<r-1) && !mark[i][j+1] && (curcolor==image[i][j+1])) {
if (Q.count >= Q.MAXQ) {
fprintf(stderr,"Queue is FULL\n");
exit(1);
}
Q.count++;
Q.rear = (Q.rear + 1) % Q.MAXQ;
Q.entryI[Q.rear] = i;
Q.entryJ[Q.rear] = j+1;
mark[i][j+1] = BLACK;
}
if ((i<q-1) && !mark[i+1][j] && (curcolor==image[i+1][j])) {
if (Q.count >= Q.MAXQ) {
fprintf(stderr,"Queue is FULL\n");
exit(1);
}
Q.count++;
Q.rear = (Q.rear + 1) % Q.MAXQ;
Q.entryI[Q.rear] = i+1;
Q.entryJ[Q.rear] = j;
mark[i+1][j] = BLACK;
}
if ((j>0) && !mark[i][j-1] && (curcolor==image[i][j-1])) {
if (Q.count >= Q.MAXQ) {
fprintf(stderr,"Queue is FULL\n");
exit(1);
}
Q.count++;
Q.rear = (Q.rear + 1) % Q.MAXQ;
Q.entryI[Q.rear] = i;
Q.entryJ[Q.rear] = j-1;
mark[i][j-1] = BLACK;
}
if ((i>0) && !mark[i-1][j] && (curcolor==image[i-1][j])) {
if (Q.count >= Q.MAXQ) {
fprintf(stderr,"Queue is FULL\n");
exit(1);
}
Q.count++;
Q.rear = (Q.rear + 1) % Q.MAXQ;
Q.entryI[Q.rear] = i-1;
Q.entryJ[Q.rear] = j;
mark[i-1][j] = BLACK;
}
} while (!(Q.count <= 0));
free(Q.entryI);
free(Q.entryJ);
}
}
/*************************************************************/
void all_connected_components_grey_four(int v, int w, int q, int r,
int X[v][w]::[q][r], int k)
/*************************************************************/
/* Find the 4-connected components of a (vw x qr) = (n x n) grey scale image
on a parallel machine with PROCS processors */
#ifndef _COMPILE_LITE
{
ELEMPTR border_right, /* The right border to be converted into graph problem */
border_left; /* The left border to be converted into graph problem */
elem_t
ShadowBorder[v][w]::[q*v], /* Shadow Manager's border */
*locShadowBorder; /* ptr to shadow manager's border */
register int
i, /* Loop variable */
j, /* Loop variable */
ii, /* MYPROC's row */
jj; /* MYPROC's column */
int logv, /* log2(v) */
logw, /* log2(w) */
vIt, /* The iteration num of combining up/down blocks together */
wIt, /* " " left/right blocks together */
group_manager, /* Boolean = at this iteration, I do the sequential work */
shadow_manager, /* Boolean = at this iteration, I do the shadow work */
grpManV, /* if not a group manager, this is the row of my manager */
grpManW, /* if not a group manager, this is the col of my manager */
length, /* length of the border being merged */
borderLen, /* size of the total border merge graph problem */
mark[q][r], /* Markers for initial connected components on tile */
hooks, /* Number of cc's on our tile */
beta, /* temp variable used in merge update */
*ccOut, /* results of initial connected components on a tile */
*old_label_right, /* Saved value for original right label */
*new_label_right, /* New value for original right label */
*old_label_left, /* Saved value for original left label */
*new_label_left, /* New value for original left label */
*color_left, /* The pixel value of the left border */
*color_right, /* The pixel value of the right border */
*input_labelling, /* The labelling passed into connected components */
changeSize[v][w]::, /* Proc. (v,w)'s # of (alpha,beta) changes from merge */
chSize, /* Local copy of our group manager's changeSize */
(*locX)[r], /* local pointer to our subimage */
(*locLabel)[r], /* local pointer to our subimage's labels */
*ccLabel; /* local pointer to our subimage's labels (linear) */
#if 1
int
orgV, orgW, /* original of my group in real coords */
g_grey, /* my grey code address inside my group */
g_GM, /* the Group Manager's grey code offset */
g_label, /* my final "hypercube" label inside my group */
send_i, send_j;
#endif
CHPAIRPTR changeList, /* Array of changes created by group manager */
tileHookOrig, /* Array of original (label,addr) hooks */
tempHookArr; /* Temp array of border labels and their addresses */
TILEHOOKPTR tileHookArr; /* Array of tile border hooks */
chPair_t
chList[v][w]::[2*q*v], /* Group Manager's change list */
*locChList; /* ptr to our local list */
char buf[MAXLEN];
double secs; /* Timing marker */
edge_t *auxGraph;
int *auxIndex,
edgeCount;
int Label[v][w]::[q][r];
on_one {
fprintf(outfile,"4-Connected Components of Grey Scale Image:\n");
fprintf(outfile,"k = %d\n",k);
}
barrier();
all_init_timer();
all_start_timer();
secs = get_seconds(); /* Begin timing */
ii = mapRow(v,w,MYPROC);
jj = mapCol(v,w,MYPROC);
locX = tolocal(X[ii][jj]);
locLabel = tolocal(Label[ii][jj]);
locChList = tolocal(chList[ii][jj]);
locShadowBorder = tolocal(ShadowBorder[ii][jj]);
ccLabel = &(locLabel[0][0]);
logv = (int)log2((double)v);
logw = (int)log2((double)w);
/* New tile connected components */
for (i=0 ; i<q ; i++)
for (j=0 ; j<r ; j++) {
locLabel[i][j] = 0;
mark[i][j] = (locX[i][j]>0 ? WHITE : BLACK);
}
BFS_grey_4(v,w,q,r,ii,jj, locX, mark, locLabel);
/* Create tileHookArr with size >= 2q + 2r - 4 (border of tile) */
borderLen = 2*(q+r) - 4;
if ((tempHookArr = (CHPAIRPTR)malloc(borderLen*sizeof(chPair_t)))==NULL)
fprintf(stderr,"ERROR: tempHookArr could not be malloc'ed\n");
/* For each pixel in the border of the tile, add it to the list if it
is a unique label > 0 */
chSize = 0;
for (j=0 ; j<r ; j++) {
if (locLabel [0][j] > 0) {
tempHookArr[chSize].alpha = locLabel [0][j];
tempHookArr[chSize].beta = j;
chSize++;
}
if (locLabel[q-1][j] > 0) {
tempHookArr[chSize].alpha = locLabel[q-1][j];
tempHookArr[chSize].beta = (q-1)*r +j;
chSize++;
}
}
for (i=1 ; i<q-1 ; i++) {
if (locLabel[i][0] > 0) {
tempHookArr[chSize].alpha = locLabel[i][0];
tempHookArr[chSize].beta = i*r;
chSize++;
}
if (locLabel[i][r-1] > 0) {
tempHookArr[chSize].alpha = locLabel[i][r-1];
tempHookArr[chSize].beta = i*r + r-1;
chSize++;
}
}
hooks = fill_tileHook(&tileHookArr, tempHookArr, chSize);
if ((tileHookOrig = (CHPAIRPTR)malloc(hooks*sizeof(chPair_t)))==NULL)
fprintf(stderr,"ERROR: tileHookOrig could not be malloc'ed\n");
create_hooks(tileHookOrig, tileHookArr, hooks);
all_stop_timer("Initialization");
barrier();
length = q*v; /* n = q*v */
if ((border_right = (ELEMPTR)malloc(length*sizeof(elem_t)))==NULL)
fprintf(stderr,"ERROR: border_right could not be malloc'ed\n");
if ((border_left = (ELEMPTR)malloc(length*sizeof(elem_t)))==NULL)
fprintf(stderr,"ERROR: border_left could not be malloc'ed\n");
if ((old_label_right = (int*)malloc(length*sizeof(int)))==NULL)
fprintf(stderr,"ERROR: old_label_right could not be malloc'ed\n");
if ((new_label_right = (int*)malloc(length*sizeof(int)))==NULL)
fprintf(stderr,"ERROR: new_label_right could not be malloc'ed\n");
if ((old_label_left = (int*)malloc(length*sizeof(int)))==NULL)
fprintf(stderr,"ERROR: old_label_left could not be malloc'ed\n");
if ((new_label_left = (int*)malloc(length*sizeof(int)))==NULL)
fprintf(stderr,"ERROR: new_label_left could not be malloc'ed\n");
if ((color_left = (int*)malloc(length*sizeof(int)))==NULL)
fprintf(stderr,"ERROR: color_left could not be malloc'ed\n");
if ((color_right = (int*)malloc(length*sizeof(int)))==NULL)
fprintf(stderr,"ERROR: color_right could not be malloc'ed\n");
if ((ccOut = (int*)malloc(2*length * sizeof(int)))==NULL)
fprintf(stderr, "ERROR: ccOut list could not be malloc'ed\n");
if ((input_labelling = (int*)malloc(2*length * sizeof(int)))==NULL)
fprintf(stderr, "ERROR: input_labelling list could not be malloc'ed\n");
if ((changeList = (CHPAIRPTR)malloc(2*length * sizeof(chPair_t)))==NULL)
fprintf(stderr, "ERROR: changeList could not be malloc'ed\n");
if ((auxGraph = (edge_t *)malloc(6*2*length*sizeof(edge_t)))==NULL)
fprintf(stderr, "ERROR: auxGraph could not be malloc'ed\n");
if ((auxIndex = (int *)malloc(2*length*sizeof(int)))==NULL)
fprintf(stderr, "ERROR: auxIndex could not be malloc'ed\n");
/* Combine tiles across processors */
vIt = 0;
wIt = 0;
while ((vIt<logv)||(wIt<logw)) {
/* Combine left/right */
if (wIt < logw) {
all_start_timer();
wIt++;
length = q * (1 << vIt);
/* if I am a manager this round: */
/* Then - prefetch the border from w_index ++ */
/* - find the connected components */
/* - broadcast the label changes to my clients */
/* - update my pixels */
/* Group Manager: Last wIt bits of jj = 0111.1 (a zero and wIt-1 1's)
Last vIt bits of ii each = 0 */
group_manager = ( ((jj & ~(~0<<wIt)) == (1<<(wIt-1))-1)
&& ((ii & ((1<<vIt)-1) ) == 0) );
/* Shadow Manager: The processor one to the right of the group manager.
Last wIt bits of jj = 1000.0 (a one and wIt-1 0's)
Last vIt bits of ii each = 0 */
shadow_manager = ( ((jj & ~(~0<<wIt)) == (1<<(wIt-1)))
&& ((ii & ((1<<vIt)-1) ) == 0) );
if (group_manager) {
/* Get border from right processor, and set up label arrays */
for (j=0 ; j < (1<<vIt) ; j++) {
for (i=0 ; i<q ; i++) {
border_left [(j*q) + i].color := X[ii+j][jj][i][r-1];
border_left [(j*q) + i].label := Label[ii+j][jj][i][r-1];
}
for (i=0 ; i<q ; i++) {
border_left [(j*q) + i].pos = (j*q) + i;
border_right [(j*q) + i].pos = (j*q) + i;
}
sync();
for (i=0 ; i<q ; i++) {
color_left [(j*q) + i] = border_left [(j*q) + i].color;
old_label_left [(j*q) + i] = border_left [(j*q) + i].label;
}
}
/* Prefetch Right Border */
for (j=0 ; j < (1<<vIt) ; j++) {
for (i=0 ; i<q ; i++) {
border_right [(j*q) + i].color := X[ii+j][jj+1][i][0];
border_right [(j*q) + i].label := Label[ii+j][jj+1][i][0];
}
}
/* Sort each border list by label, and then move through the list linearly,
attaching each pair that are of the same label */
fastsort_elem(border_left, length);
sync();
for (j=0 ; j < (1<<vIt) ; j++) {
for (i=0 ; i<q ; i++) {
color_right [(j*q) + i] = border_right[(j*q) + i].color;
old_label_right[(j*q) + i] = border_right[(j*q) + i].label;
}
}
}
if (shadow_manager) {
/* Get border from processors, and set up label arrays */
for (j=0 ; j < (1<<vIt) ; j++) {
for (i=0 ; i<q ; i++) {
locShadowBorder[(j*q) + i].color := X[ii+j][jj][i][0];
locShadowBorder[(j*q) + i].label := Label[ii+j][jj][i][0];
}
for (i=0 ; i<q ; i++)
locShadowBorder[(j*q) + i].pos = (j*q) + i;
sync();
}
fastsort_elem(locShadowBorder,length);
}
barrier();
/* Group Manager should retrieve results from Shadow manager */
if (group_manager) {
bulk_get(border_right, &(ShadowBorder[ii][jj+1][0]),
length*sizeof(elem_t));
borderLen = 2*length;
/* Each vertex can have at most 5 edges incident to it */
/* There are at most 2*(5*borderLen - 4) edges in the graph */
edgeCount = 0;
/* Add edges */
/* First, add edges stringing along pixels of the same label down each of
the borders */
/* Then add edges representing links between the two borders */
for (i=1 ; i<length ; i++) {
if ((border_left[i-1].label == border_left[i].label)
&& (border_left[i].label > 0))
addAuxEdge(auxGraph, &edgeCount,
border_left[i-1].pos,
border_left[i].pos);
}
sync();
for (i=1 ; i<length ; i++) {
if ((border_right[i-1].label == border_right[i].label)
&& (border_right[i].label > 0))
addAuxEdge(auxGraph, &edgeCount,
length + border_right[i-1].pos,
length + border_right[i].pos);
}
/* Scan down the left border and add edges wherever it meets the right border */
for (i=0 ; i<length ; i++) {
if ((color_left[i] > 0)&&(color_left[i]==color_right[i]))
addAuxEdge(auxGraph, &edgeCount, i, length + i);
input_labelling[i] = old_label_left[i];
input_labelling[i+length] = old_label_right[i];
ccOut[i] = input_labelling[i];
ccOut[i+length] = input_labelling[i+length];
}
if (edgeCount > 0) {
/* Sort the edges, then create an auxIndex */
fastsort_edge(auxGraph, edgeCount);
for (i=0 ; i<borderLen ; i++)
auxIndex[i] = NIL;
auxIndex[auxGraph[0].a] = 0;
if (edgeCount > 1)
for (i=1 ; i<edgeCount ; i++)
if (auxGraph[i].a > auxGraph[i-1].a)
auxIndex[auxGraph[i].a] = i;
BFS_auxGraph(auxGraph, edgeCount, borderLen, auxIndex,
ccOut, input_labelling);
}
chSize = 0;
if (edgeCount > 0) {
for (i=0 ; i<length ; i++) {
new_label_left[i] = ccOut[i];
new_label_right[i] = ccOut[length+i];
}
/* Compare old_label's with new_label's and propogate the differences */
for (i=0 ; i<length ; i++) {
if (old_label_left[i] != new_label_left[i]) {
changeList[chSize].alpha = old_label_left[i];
changeList[chSize].beta = new_label_left[i];
chSize++;
}
if (old_label_right[i] != new_label_right[i]) {
changeList[chSize].alpha = old_label_right[i];
changeList[chSize].beta = new_label_right[i];
chSize++;
}
}
fastsort_chPair(changeList, chSize);
chSize = fill_chList(locChList, changeList, chSize);
}
changeSize [ii][jj] = chSize;
grpManV = ii;
grpManW = jj;
}
else { /* Figure out who my group_manager is */
/* ii with the last vIt bits set to 0 */
grpManV = (ii >> vIt) << vIt;
/* jj with the last wIt bits set to 0111.1 (a zero and wIt-1 1's) */
grpManW = (((jj >> wIt) << wIt) | ((1<<(wIt-1))-1));
}
barrier();
#if 0
/* Prefetch Change List from Group Manager */
if (!group_manager) {
chSize = changeSize[grpManV][grpManW];
/* Fix this prefetch later to be a better broadcast algorithm */
if (chSize > 0) {
bulk_get(locChList, &(chList[grpManV][grpManW][0]),
chSize*sizeof(chPair_t));
sync();
}
}
#endif
#if 0
all_mesh_bcast(v, w, (q*v) << 1,
vIt, wIt,
ii, jj, grpManV, grpManW,
chList, changeSize);
chSize = changeSize[ii][jj];
#endif
#if 1
orgV = (ii>>vIt) << vIt;
orgW = (jj>>wIt) << wIt;
g_grey = greyEncoder(vIt, wIt, ii-orgV, jj-orgW);
g_GM = greyEncoder(vIt, wIt, grpManV-orgV, grpManW-orgW);
g_label = g_grey ^ g_GM;
j = vIt + wIt;
for (i=0 ; i<j ; i++) {
if (g_label < (1<<i)) {
greyDecoder(vIt, wIt, &send_i, &send_j, g_grey^(1<<i) );
changeSize[send_i+orgV][send_j+orgW]
= changeSize[ii][jj];
}
barrier();
}
chSize = changeSize[ii][jj];
for (i=0 ; i<j ; i++) {
if ((g_label < (1<<i)) && (chSize > 0)) {
greyDecoder(vIt, wIt, &send_i, &send_j, g_grey^(1<<i) );
bulk_write(&(chList[send_i+orgV][send_j+orgW][0]),
tolocal(chList[ii][jj]),
chSize*sizeof(chPair_t));
}
barrier();
}
#endif
/* Make changes */
/* We have a sorted array of changes of size chSize,
a data structure of tile border hooks.
Recolor only the border of each tile by binary searching the
label change for each hook's label, and then updating the label
at each of the addresses in the addr. list. */
for (i=0 ; i<hooks ; i++) { /* For Each Hook */
beta = chLookup(locChList, chSize, tileHookArr[i].label);
if (tileHookArr[i].label != beta) {
for (j=0 ; j < tileHookArr[i].size ; j++)
ccLabel[(tileHookArr[i].addr)[j]] = beta;
tileHookArr[i].label = beta;
}
}
sprintf(buf,"Merge Phase %d",vIt+wIt);
all_stop_timer(buf);
barrier();
}
/* Combine up/down */
/* left == up , right == down */
if (vIt < logv) {
all_start_timer();
vIt++;
length = r * (1 << wIt);
/* if I am a manager this round: */
/* Then - prefetch the border from v_index ++ */
/* - find the connected components */
/* - broadcast the label changes to my clients */
/* - update my pixels */
/* Group Manager: Last vIt bits of ii = 0111.1 (a zero and vIt-1 1's)
Last wIt bits of jj each = 0 */
group_manager = ( ((ii & ~(~0<<vIt)) == (1<<(vIt-1))-1)
&& ((jj & ((1<<wIt)-1) ) == 0) );
/* Shadow Manager: The processor one to the bottom of the group manager.
Last vIt bits of ii = 1000.0 (a one and vIt-1 0's)
Last wIt bits of jj each = 0 */
shadow_manager = ( ((ii & ~(~0<<vIt)) == (1<<(vIt-1)))
&& ((jj & ((1<<wIt)-1) ) == 0) );
if (group_manager) {
/* Get border from right processor, and set up label arrays */
for (j=0 ; j < (1<<wIt) ; j++) {
for (i=0 ; i<r ; i++) {
border_left [(j*r) + i].color := X[ii ][jj+j][q-1][i];
border_left [(j*r) + i].label := Label[ii ][jj+j][q-1][i];
}
for (i=0 ; i<r ; i++) {
border_left [(j*r) + i].pos = (j*r) + i;
border_right [(j*r) + i].pos = (j*r) + i;
}
sync();
for (i=0 ; i<r ; i++) {
color_left [(j*r) + i] = border_left [(j*r) + i].color;
old_label_left [(j*r) + i] = border_left [(j*r) + i].label;
}
}
/* Prefetch Right Border */
for (j=0 ; j < (1<<wIt) ; j++) {
for (i=0 ; i<r ; i++) {
border_right [(j*r) + i].color := X[ii+1][jj+j][0][i];
border_right [(j*r) + i].label := Label[ii+1][jj+j][0][i];
}
}
/* Sort each border list by label, and then move through the list linearly,
attaching each pair that are of the same label */
fastsort_elem(border_left, length);
sync();
for (j=0 ; j < (1<<wIt) ; j++) {
for (i=0 ; i<r ; i++) {
color_right [(j*r) + i] = border_right[(j*r) + i].color;
old_label_right[(j*r) + i] = border_right[(j*r) + i].label;
}
}
}
if (shadow_manager) {
/* Get border from processors, and set up label arrays */
for (j=0 ; j < (1<<wIt) ; j++) {
for (i=0 ; i<r ; i++) {
locShadowBorder[(j*r) + i].color := X[ii][jj+j][0][i];
locShadowBorder[(j*r) + i].label := Label[ii][jj+j][0][i];
}
for (i=0 ; i<r ; i++)
locShadowBorder[(j*r) + i].pos = (j*r) + i;
sync();
}
fastsort_elem(locShadowBorder,length);
}
barrier();
/* Group Manager should retrieve results from Shadow manager */
if (group_manager) {
bulk_get(border_right, &(ShadowBorder[ii+1][jj][0]),
length*sizeof(elem_t));
borderLen = 2*length;
/* Each vertex can have at most 5 edges incident to it */
/* There are at most 2*(5*borderLen - 4) edges in the graph */
edgeCount = 0;
/* Add edges */
/* First, add edges stringing along pixels of the same label down each of
the borders */
/* Then add edges representing links between the two borders */
for (i=1 ; i<length ; i++) {
if ((border_left[i-1].label == border_left[i].label)
&& (border_left[i].label > 0))
addAuxEdge(auxGraph, &edgeCount,
border_left[i-1].pos,
border_left[i].pos);
}
sync();
for (i=1 ; i<length ; i++) {
if ((border_right[i-1].label == border_right[i].label)
&& (border_right[i].label > 0))
addAuxEdge(auxGraph, &edgeCount,
length + border_right[i-1].pos,
length + border_right[i].pos);
}
/* Scan down the left border and add edges wherever it meets the right border */
for (i=0 ; i<length ; i++) {
if ((color_left[i] > 0)&&(color_left[i]==color_right[i]))
addAuxEdge(auxGraph, &edgeCount, i, length + i);
input_labelling[i] = old_label_left[i];
input_labelling[i+length] = old_label_right[i];
ccOut[i] = input_labelling[i];
ccOut[i+length] = input_labelling[i+length];
}
if (edgeCount > 0) {
/* Sort the edges, then create an auxIndex */
fastsort_edge(auxGraph, edgeCount);
for (i=0 ; i<borderLen ; i++)
auxIndex[i] = NIL;
auxIndex[auxGraph[0].a] = 0;
if (edgeCount > 1)
for (i=1 ; i<edgeCount ; i++)
if (auxGraph[i].a > auxGraph[i-1].a)
auxIndex[auxGraph[i].a] = i;
BFS_auxGraph(auxGraph, edgeCount, borderLen, auxIndex,
ccOut, input_labelling);
}
/* Compare old_label's with new_label's and propogate the differences */
chSize = 0;
if (edgeCount > 0) {
for (i=0 ; i<length ; i++) {
new_label_left[i] = ccOut[i];
new_label_right[i] = ccOut[length+i];
}
for (i=0 ; i<length ; i++) {
if (old_label_left[i] != new_label_left[i]) {
changeList[chSize].alpha = old_label_left[i];
changeList[chSize].beta = new_label_left[i];
chSize++;
}
if (old_label_right[i] != new_label_right[i]) {
changeList[chSize].alpha = old_label_right[i];
changeList[chSize].beta = new_label_right[i];
chSize++;
}
}
fastsort_chPair(changeList, chSize);
chSize = fill_chList(locChList, changeList, chSize);
}
changeSize [ii][jj] = chSize;
grpManV = ii;
grpManW = jj;
}
else { /* Figure out who my group_manager is */
/* ii with the last vIt bits set to 0111.1 (a zero and vIt-1 1's) */
grpManV = (((ii >> vIt) << vIt) | ((1<<(vIt-1))-1));
/* jj with the last wIt bits set to 0 */
grpManW = (jj >> wIt) << wIt;
}
barrier();
#if 0
/* Prefetch Change List from Group Manager */
if (!group_manager) {
chSize = changeSize[grpManV][grpManW];
/* Fix this prefetch later to be a better broadcast algorithm */
if (chSize > 0) {
bulk_get(locChList, &(chList[grpManV][grpManW][0]),
chSize*sizeof(chPair_t));
sync();
}
}
#endif
#if 0
all_mesh_bcast(v, w, (q*v) << 1,
vIt, wIt,
ii, jj, grpManV, grpManW,
chList, changeSize);
chSize = changeSize[ii][jj];
#endif
#if 1
orgV = (ii>>vIt) << vIt;
orgW = (jj>>wIt) << wIt;
g_grey = greyEncoder(vIt, wIt, ii-orgV, jj-orgW);
g_GM = greyEncoder(vIt, wIt, grpManV-orgV, grpManW-orgW);
g_label = g_grey ^ g_GM;
j = vIt + wIt;
for (i=0 ; i<j ; i++) {
if (g_label < (1<<i)) {
greyDecoder(vIt, wIt, &send_i, &send_j, g_grey^(1<<i) );
changeSize[send_i+orgV][send_j+orgW]
= changeSize[ii][jj];
}
barrier();
}
chSize = changeSize[ii][jj];
for (i=0 ; i<j ; i++) {
if ((g_label < (1<<i)) && (chSize > 0)) {
greyDecoder(vIt, wIt, &send_i, &send_j, g_grey^(1<<i) );
bulk_write(&(chList[send_i+orgV][send_j+orgW][0]),
tolocal(chList[ii][jj]),
chSize*sizeof(chPair_t));
}
barrier();
}
#endif
/* Make changes */
/* We have a sorted array of changes of size chSize,
a data structure of tile border hooks.
Recolor only the border of each tile by binary searching the
label change for each hook's label, and then updating the label
at each of the addresses in the addr. list. */
for (i=0 ; i<hooks ; i++) { /* For Each Hook */
beta = chLookup(locChList, chSize, tileHookArr[i].label);
if (tileHookArr[i].label != beta) {
for (j=0 ; j < tileHookArr[i].size ; j++)
ccLabel[(tileHookArr[i].addr)[j]] = beta;
tileHookArr[i].label = beta;
}
}
sprintf(buf,"Merge Phase %d",vIt+wIt);
all_stop_timer(buf);
barrier();
}
}
/* We must relabel the interior of our tile by searching through
tileHookOrig, and if any labels have changed, perform a depth-first search
and recolor connected pixels */
all_start_timer();
for (i=0 ; i<q ; i++)
for (j=0 ; j<r ; j++)
mark[i][j] = (locX[i][j]>0 ? WHITE : BLACK);
for (i=0 ; i<hooks ; i++)
if (tileHookOrig[i].alpha != tileHookArr[i].label)
/* Update component */