@@ -28,18 +28,11 @@ static int sortSegments(Mesh mesh, struct comm *c, int dim, buffer *bfr) {
2828 break ;
2929 }
3030
31- s = e ;
31+ points [s ].ifSegment = 1 ;
32+ for (s = s + 1 ; s < e ; s ++ )
33+ points [s ].ifSegment = 0 ;
3234 }
3335
34- // Set globalId
35- slong out [2 ][1 ], buf [2 ][1 ], in [1 ];
36- in [0 ] = nPoints ;
37- comm_scan (out , c , gs_long , gs_add , in , 1 , buf );
38- slong start = out [0 ][0 ];
39-
40- for (e = 0 ; e < nPoints ; e ++ )
41- points [e ].globalId = start + e ;
42-
4336 return 0 ;
4437}
4538
@@ -48,21 +41,25 @@ static int findLocalSegments(Mesh mesh, int i, GenmapScalar tolSquared) {
4841 sint npts = mesh -> elements .n ;
4942 int nDim = mesh -> nDim ;
5043
44+ //if (npts > 0)
45+ // printf("%d %d %0.12lf %0.12lf %0.12lf %llu\n", 0, pts[0].ifSegment, pts[0].x[0], pts[0].x[1], pts[0].x[2], pts[0].globalId);
46+
5147 sint j ;
52- for (j = 0 ; j < npts - 1 ; j ++ ) {
53- GenmapScalar d = sqrDiff (pts [j ].x [i ], pts [j + 1 ].x [i ]);
48+ for (j = 1 ; j < npts ; j ++ ) {
49+ GenmapScalar d = sqrDiff (pts [j ].x [i ], pts [j - 1 ].x [i ]);
5450
55- GenmapScalar dx = min (pts [j ].dx , pts [j + 1 ].dx )* tolSquared ;
51+ GenmapScalar dx = min (pts [j ].dx , pts [j - 1 ].dx )* tolSquared ;
5652
5753 if (d > dx )
58- pts [j + 1 ].ifSegment = 1 ;
54+ pts [j ].ifSegment = 1 ;
55+
56+ //printf("%d %d %0.12lf %0.12lf %0.12lf %llu\n", j, pts[j].ifSegment, pts[j].x[0], pts[j].x[1], pts[j].x[2], pts[j].globalId);
5957 }
6058}
6159
6260static int mergeSegments (Mesh mesh , struct comm * c , int i , GenmapScalar tolSquared , buffer * bfr ) {
6361 uint nPoints = mesh -> elements .n ;
6462 Point points = mesh -> elements .ptr ;
65- int nDim = mesh -> nDim ;
6663
6764 sint rank = c -> id ;
6865 sint size = c -> np ;
@@ -95,10 +92,8 @@ static int mergeSegments(Mesh mesh, struct comm *c, int i, GenmapScalar tolSquar
9592
9693 // If rank > 0, send i = 0,... n-1 where points[i].ifSegment == 0 to rank - 1
9794 n = 0 ;
98- if (rank > 0 ) {
99- for (; n < nPoints && points [n ].ifSegment == 0 ; n ++ )
100- points [n ].proc = rank - 1 ;
101- }
95+ for (; n < nPoints && points [n ].ifSegment == 0 ; n ++ )
96+ points [n ].proc = rank - 1 ;
10297 for (; n < nPoints ; n ++ )
10398 points [n ].proc = rank ;
10499
@@ -127,37 +122,34 @@ int findSegments(Mesh mesh, struct comm *c, GenmapScalar tol, int verbose, buffe
127122
128123 //TODO: load balance
129124
130- // Initialize
125+ // Initialize globalId and ifSegment
126+ slong out [2 ][1 ], buf [2 ][1 ], in [1 ];
127+ in [0 ] = nPoints ;
128+ comm_scan (out , c , gs_long , gs_add , in , 1 , buf );
129+ slong start = out [0 ][0 ];
131130 uint i ;
132- for (i = 0 ; i < nPoints ; i ++ )
131+ for (i = 0 ; i < nPoints ; i ++ ) {
133132 points [i ].ifSegment = 0 ;
133+ points [i ].globalId = start + i ;
134+ }
134135
135- slong out [2 ][1 ], buf [2 ][1 ], in [1 ];
136- // in[0] = nPoints;
137- // comm_scan(out, &nonZeroRanks, gs_long, gs_add, in, 1, buf);
138- // slong start = out[0][0];
139-
140- // if (verbose > 1 && rank == 0) {
141- // printf("\tsegments: rank=%d npts=%u start=%lld\n", rank, nPoints, start);
142- // fflush(stdout);
143- // }
144- // comm_barrier(&nonZeroRanks);
136+ //FIXME: first rank with nPoints>0 should do this
137+ if (c -> id == 0 && nPoints > 0 )
138+ points [0 ].ifSegment = 1 ;
145139
146140 comm_ext orig ;
147141#ifdef MPI
148142 MPI_Comm_dup (c -> c , & orig );
149143#endif
150144 struct comm nonZeroRanks ;
151-
152- int dim , bin , rank , t ;
145+ int rank = c -> id ;
146+
147+ int dim , bin , t , merged = 0 ;
153148 for (t = 0 ; t < nDim ; t ++ ) {
154149 for (dim = 0 ; dim < nDim ; dim ++ ) {
155150 nPoints = mesh -> elements .n ;
156151
157- bin = 1 ;
158- if (nPoints == 0 )
159- bin = 0 ;
160-
152+ bin = (nPoints > 0 );
161153#ifdef MPI
162154 MPI_Comm new ;
163155 MPI_Comm_split (orig , bin , rank , & new );
@@ -168,28 +160,27 @@ int findSegments(Mesh mesh, struct comm *c, GenmapScalar tol, int verbose, buffe
168160#endif
169161
170162 rank = nonZeroRanks .id ;
171- for (i = 0 ; i < nPoints ; i ++ )
172- points [i ].proc = rank ;
173163
174- if (bin == 1 ) {
175- sortSegments (mesh , & nonZeroRanks , dim , bfr );
164+ if (bin > 0 && verbose > 0 ) {
165+ nPoints = mesh -> elements .n ;
166+ points = mesh -> elements .ptr ;
167+ sint count = 0 ;
168+ for (i = 0 ; i < nPoints ; i ++ )
169+ if (points [i ].ifSegment > 0 )
170+ count ++ ;
171+
172+ in [0 ] = count ;
173+ comm_allreduce (& nonZeroRanks , gs_long , gs_add , in , 1 , buf );
174+ if (rank == 0 )
175+ printf ("\tlocglob: %d %d %lld\n" , t + 1 , dim + 1 , in [0 ]);
176+ }
176177
178+ if (bin > 0 ) {
179+ sortSegments (mesh , & nonZeroRanks , dim , bfr );
177180 findLocalSegments (mesh , dim , tolSquared );
178-
179- mergeSegments (mesh , & nonZeroRanks , dim , tolSquared , bfr );
180-
181- if (verbose > 0 ) {
182- nPoints = mesh -> elements .n ;
183- points = mesh -> elements .ptr ;
184- sint count = 0 ;
185- for (i = 0 ; i < nPoints ; i ++ )
186- if (points [i ].ifSegment > 0 )
187- count ++ ;
188-
189- in [0 ] = count ;
190- comm_allreduce (& nonZeroRanks , gs_long , gs_add , in , 1 , buf );
191- if (rank == 0 )
192- printf ("\tlocglob: %d %d %lld\n" , t + 1 , dim + 1 , in [0 ] + 1 );
181+ if (merged == 0 ) {
182+ mergeSegments (mesh , & nonZeroRanks , dim , tolSquared , bfr );
183+ merged = 1 ;
193184 }
194185 }
195186
0 commit comments