44#include <gencon-impl.h>
55#include <sort.h>
66
7- int findLocalSegments (Mesh mesh ,int i ,GenmapScalar tolSquared ){
8- Point pts = mesh -> elements .ptr ;
9- sint npts = mesh -> elements .n ;
7+ int sortSegments (Mesh mesh , struct comm * c , int dim ) {
8+ uint nPoints = mesh -> elements .n ;
9+ Point points = mesh -> elements .ptr ;
10+
11+ buffer bfr ;
12+ buffer_init (& bfr , 1024 );
13+
14+ uint s = 0 , e ;
15+ while (s < nPoints - 1 ) {
16+ // find the length of the segment
17+ for (e = s + 1 ; e < nPoints && points [e ].ifSegment == 0 ; e ++ );
18+
19+ // sort start to end based on dim
20+ switch (dim ) {
21+ case 0 :
22+ sarray_sort (struct Point_private , points + s , e - s , x [0 ], 3 , & bfr );
23+ break ;
24+ case 1 :
25+ sarray_sort (struct Point_private , points + s , e - s , x [1 ], 3 , & bfr );
26+ break ;
27+ case 2 :
28+ sarray_sort (struct Point_private , points + s , e - s , x [2 ], 3 , & bfr );
29+ break ;
30+ default :
31+ break ;
32+ }
33+
34+ s = e ;
35+ }
36+
37+ buffer_free (& bfr );
38+
39+ // set sequenceId
40+ slong out [2 ][1 ], buf [2 ][1 ], in [1 ];
41+ in [0 ] = nPoints ;
42+ comm_scan (out , c , gs_long , gs_add , in , 1 , buf );
43+ slong start = out [0 ][0 ];
44+
45+ for (e = 0 ; e < nPoints ; e ++ )
46+ points [e ].globalId = start + e ;
47+
48+ return 0 ;
49+ }
50+
51+ int findLocalSegments (Mesh mesh , int i , GenmapScalar tolSquared ){
52+ Point pts = mesh -> elements .ptr ;
53+ sint npts = mesh -> elements .n ;
54+ int nDim = mesh -> nDim ;
1055
1156 sint j ;
12- for (j = 0 ;j < npts - 1 ;j ++ ){
13- GenmapScalar d = sqrDiff (pts [j ].x [i ],pts [j + 1 ].x [i ]);
14- GenmapScalar dx = min (pts [j ].dx ,pts [j + 1 ].dx )* tolSquared ;
15- if (d > dx ) pts [j + 1 ].ifSegment = 1 ;
57+ for (j = 0 ; j < npts - 1 ; j ++ ) {
58+ #if 0
59+ GenmapScalar d ;
60+ if (nDim == 3 )
61+ d = distance3D (pts [j ], pts [j + 1 ]);
62+ else
63+ d = distance2D (pts [j ], pts [j + 1 ]);
64+ #else
65+ GenmapScalar d = sqrDiff (pts [j ].x [i ], pts [j + 1 ].x [i ]);
66+ #endif
67+ GenmapScalar dx = min (pts [j ].dx , pts [j + 1 ].dx )* tolSquared ;
68+
69+ if (d > dx )
70+ pts [j + 1 ].ifSegment = 1 ;
1671 }
1772}
1873
19- int mergeSegments (Mesh mesh ,struct comm * c ,int i ,GenmapScalar tolSquared )
20- {
21- uint nPoints = mesh -> elements .n ;
22- Point points = mesh -> elements . ptr ;
74+ int mergeSegments (Mesh mesh , struct comm * c , int i , GenmapScalar tolSquared ) {
75+ uint nPoints = mesh -> elements . n ;
76+ Point points = mesh -> elements .ptr ;
77+ int nDim = mesh -> nDim ;
2378
24- sint rank = c -> id ,size = c -> np ;
79+ sint rank = c -> id ;
80+ sint size = c -> np ;
2581
26- struct Point_private lastp = points [nPoints - 1 ];
27- lastp .proc = (rank + 1 )%size ;
82+ struct Point_private lastp = points [nPoints - 1 ];
83+ lastp .proc = (rank + 1 )%size ;
2884
2985 struct array arr ;
30- array_init (struct Point_private ,& arr ,1 );
31- array_cat (struct Point_private ,& arr ,& lastp ,1 );
32-
33- struct crystal cr ; crystal_init (& cr ,c );
34- sarray_transfer (struct Point_private ,& arr ,proc ,1 ,& cr );
35- crystal_free (& cr );
86+ array_init (struct Point_private , & arr , 1 );
87+ array_cat (struct Point_private , & arr , & lastp , 1 );
88+
89+ struct crystal cr ;
90+ crystal_init (& cr , c );
91+ sarray_transfer (struct Point_private , & arr , proc , 1 , & cr );
92+
93+ uint n = arr .n ;
94+ assert (n == 1 );
95+ lastp = ((struct Point_private * ) arr .ptr )[0 ];
96+
97+ if (rank > 0 ) {
98+ #if 0
99+ GenmapScalar d ;
100+ if (nDim == 3 )
101+ d = distance3D (lastp , points [0 ]);
102+ else
103+ d = distance2D (lastp , points [0 ]);
104+ #else
105+ GenmapScalar d = sqrDiff (lastp .x [i ], points -> x [i ]);
106+ #endif
36107
37- uint n = arr .n ; assert (n == 1 );
38- lastp = ((struct Point_private * )arr .ptr )[0 ];
108+ GenmapScalar dx = min (lastp .dx , points -> dx )* tolSquared ;
39109
40- if (rank > 0 ){
41- GenmapScalar d = sqrDiff (lastp .x [i ],points -> x [i ]);
42- GenmapScalar dx = min (lastp .dx ,points -> dx )* tolSquared ;
43- if (d > dx ) points -> ifSegment = 1 ;
110+ if (d > dx )
111+ points -> ifSegment = 1 ;
44112 }
45113
46114 array_free (& arr );
47115
116+ // If rank > 0, send i = 0,... n-1 where points[i].ifSegment == 0 to rank - 1
117+ n = 0 ;
118+ if (rank > 0 ) {
119+ for (; n < nPoints && points [n ].ifSegment == 0 ; n ++ )
120+ points [n ].proc = rank - 1 ;
121+ }
122+ for (; n < nPoints ; n ++ )
123+ points [n ].proc = rank ;
124+
125+ sarray_transfer (struct Point_private , & mesh -> elements , proc , 0 , & cr );
126+ crystal_free (& cr );
127+
128+ buffer buf ;
129+ buffer_init (& buf , 1024 );
130+ sarray_sort (struct Point_private , mesh -> elements .ptr , mesh -> elements .n , globalId , 1 , & buf );
131+ buffer_free (& buf );
132+
48133 return 0 ;
49134}
50135
51- int findSegments (Mesh mesh ,struct comm * c ,GenmapScalar tol ,int verbose ){
52- int nDim = mesh -> nDim ,nVertex = mesh -> nVertex ;
53- GenmapScalar tolSquared = tol * tol ;
136+ int findSegments (Mesh mesh , struct comm * c , GenmapScalar tol , int verbose ) {
137+ int nDim = mesh -> nDim ;
138+ int nVertex = mesh -> nVertex ;
139+ GenmapScalar tolSquared = tol * tol ;
54140
55- parallel_sort (struct Point_private ,& mesh -> elements ,x [0 ],
56- genmap_gs_scalar ,bin_sort ,0 ,c );
141+ parallel_sort (struct Point_private , & mesh -> elements , x [0 ], genmap_gs_scalar , bin_sort , 0 , c );
57142
58- uint nPoints = mesh -> elements .n ;
59- Point points = mesh -> elements .ptr ;
143+ uint nPoints = mesh -> elements .n ;
144+ Point points = mesh -> elements .ptr ;
60145
61- buffer buf ; buffer_init (& buf ,1024 );
62- if (nDim == 3 )
63- sarray_sort_2 (struct Point_private ,points ,nPoints ,x [1 ],3 ,x [2 ],3 ,& buf );
146+ buffer buf ;
147+ buffer_init (& buf , 1024 );
148+
149+ if (nDim == 3 )
150+ sarray_sort_3 (struct Point_private , points , nPoints , x [0 ], 3 , x [1 ], 3 , x [2 ], 3 , & buf );
64151 else
65- sarray_sort (struct Point_private ,points ,nPoints ,x [1 ],3 ,& buf );
152+ sarray_sort_2 (struct Point_private , points , nPoints , x [0 ], 3 , x [1 ], 3 , & buf );
153+
66154 buffer_free (& buf );
67155
68156 //TODO: load balance
69157
70- sint bin = 1 ;
71- if (nPoints == 0 )
72- bin = 0 ;
158+ sint bin = 1 ;
159+ if (nPoints == 0 )
160+ bin = 0 ;
73161
74- sint rank = c -> id ,size = c -> np ; comm_ext old = c -> c ;
162+ sint rank = c -> id ;
163+ sint size = c -> np ;
164+ comm_ext old = c -> c ;
75165 struct comm nonZeroRanks ;
76166#ifdef MPI
77167 MPI_Comm new ; MPI_Comm_split (old ,bin ,rank ,& new );
@@ -81,37 +171,42 @@ int findSegments(Mesh mesh,struct comm *c,GenmapScalar tol,int verbose){
81171 comm_init (& nonZeroRanks ,1 );
82172#endif
83173
84- rank = nonZeroRanks .id ; size = nonZeroRanks .np ;
174+ rank = nonZeroRanks .id ;
175+ size = nonZeroRanks .np ;
85176
86- if (bin > 0 ) {
87- slong out [2 ][1 ],buff [2 ][1 ],in [1 ];
88- in [0 ]= nPoints ;
89- comm_scan (out ,& nonZeroRanks ,gs_long ,gs_add ,in ,1 , buff );
90- slong start = out [0 ][0 ];
177+ if (bin > 0 ) {
178+ slong out [2 ][1 ], buff [2 ][1 ], in [1 ];
179+ in [0 ] = nPoints ;
180+ comm_scan (out , & nonZeroRanks , gs_long , gs_add , in , 1 , buff );
181+ slong start = out [0 ][0 ];
91182
92- if (verbose > 1 ) {
93- printf ("segments: rank=%d npts=%u start=%lld\n" ,rank ,nPoints ,start );
183+ if (verbose > 0 ) {
184+ printf ("segments: rank=%d npts=%u start=%lld\n" , rank , nPoints , start );
94185 fflush (stdout );
95186 }
96187
97- sint i ; for (i = 0 ;i < nPoints ;i ++ )
98- points [i ].ifSegment = 0 ,points [i ].proc = rank ;
188+ sint i ;
189+ for (i = 0 ; i < nPoints ; i ++ ) {
190+ points [i ].ifSegment = 0 ;
191+ points [i ].proc = rank ;
192+ }
99193
100194 int dim ;
101- for (dim = 0 ;dim < nDim ;dim ++ ){
102- findLocalSegments (mesh ,dim ,tolSquared );
103- mergeSegments (mesh ,& nonZeroRanks ,dim ,tolSquared );
104-
105- if (verbose > 0 ){
106- sint count = 0 ;
107- for (i = 0 ;i < nPoints ;i ++ )
108- if (points [i ].ifSegment > 0 )
195+ for (dim = 0 ; dim < nDim ; dim ++ ) {
196+ sortSegments (mesh , & nonZeroRanks , dim ); // FIXME: parallel is not working
197+ findLocalSegments (mesh , dim , tolSquared );
198+ mergeSegments (mesh , & nonZeroRanks , dim , tolSquared );
199+
200+ if (verbose > 0 ) {
201+ sint count = 0 ;
202+ for (i = 0 ; i < nPoints ; i ++ )
203+ if (points [i ].ifSegment > 0 )
109204 count ++ ;
110205
111- in [0 ]= count ;
112- comm_allreduce (& nonZeroRanks ,gs_long ,gs_add ,in ,1 , buff );
113- if (rank == 0 )
114- printf ("locglob: %d %lld\n" ,dim + 1 ,in [0 ]+ 1 );
206+ in [0 ] = count ;
207+ comm_allreduce (& nonZeroRanks , gs_long , gs_add , in , 1 , buff );
208+ if (rank == 0 )
209+ printf ("locglob: %d %lld\n" , dim + 1 , in [0 ]+ 1 );
115210 }
116211 }
117212 }
0 commit comments