xref: /petsc/src/ksp/pc/impls/bddc/bddcgraph.c (revision 1c7a958b905b87bd3b0c48d575407f05fa79ac30)
1 #include <petsc/private/petscimpl.h>
2 #include <../src/ksp/pc/impls/bddc/bddcprivate.h>
3 #include <../src/ksp/pc/impls/bddc/bddcstructs.h>
4 
5 PetscErrorCode PCBDDCGraphGetDirichletDofsB(PCBDDCGraph graph, IS* dirdofs)
6 {
7   PetscErrorCode ierr;
8 
9   PetscFunctionBegin;
10   if (graph->dirdofsB) {
11     ierr = PetscObjectReference((PetscObject)graph->dirdofsB);CHKERRQ(ierr);
12   } else if (graph->has_dirichlet) {
13     PetscInt i,size;
14     PetscInt *dirdofs_idxs;
15 
16     size = 0;
17     for (i=0;i<graph->nvtxs;i++) {
18       if (graph->count[i] && graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) size++;
19     }
20 
21     ierr = PetscMalloc1(size,&dirdofs_idxs);CHKERRQ(ierr);
22     size = 0;
23     for (i=0;i<graph->nvtxs;i++) {
24       if (graph->count[i] && graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) dirdofs_idxs[size++] = i;
25     }
26     ierr = ISCreateGeneral(PETSC_COMM_SELF,size,dirdofs_idxs,PETSC_OWN_POINTER,&graph->dirdofsB);CHKERRQ(ierr);
27     ierr = PetscObjectReference((PetscObject)graph->dirdofsB);CHKERRQ(ierr);
28   }
29   *dirdofs = graph->dirdofsB;
30   PetscFunctionReturn(0);
31 }
32 
33 PetscErrorCode PCBDDCGraphGetDirichletDofs(PCBDDCGraph graph, IS* dirdofs)
34 {
35   PetscErrorCode ierr;
36 
37   PetscFunctionBegin;
38   if (graph->dirdofs) {
39     ierr = PetscObjectReference((PetscObject)graph->dirdofs);CHKERRQ(ierr);
40   } else if (graph->has_dirichlet) {
41     PetscInt i,size;
42     PetscInt *dirdofs_idxs;
43 
44     size = 0;
45     for (i=0;i<graph->nvtxs;i++) {
46       if (graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) size++;
47     }
48 
49     ierr = PetscMalloc1(size,&dirdofs_idxs);CHKERRQ(ierr);
50     size = 0;
51     for (i=0;i<graph->nvtxs;i++) {
52       if (graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) dirdofs_idxs[size++] = i;
53     }
54     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)graph->l2gmap),size,dirdofs_idxs,PETSC_OWN_POINTER,&graph->dirdofs);CHKERRQ(ierr);
55     ierr = PetscObjectReference((PetscObject)graph->dirdofs);CHKERRQ(ierr);
56   }
57   *dirdofs = graph->dirdofs;
58   PetscFunctionReturn(0);
59 }
60 
61 PetscErrorCode PCBDDCGraphASCIIView(PCBDDCGraph graph, PetscInt verbosity_level, PetscViewer viewer)
62 {
63   PetscInt       i,j,tabs;
64   PetscInt*      queue_in_global_numbering;
65   PetscErrorCode ierr;
66 
67   PetscFunctionBegin;
68   ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
69   ierr = PetscViewerASCIIGetTab(viewer,&tabs);CHKERRQ(ierr);
70   ierr = PetscViewerASCIIPrintf(viewer,"--------------------------------------------------\n");CHKERRQ(ierr);
71   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
72   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Local BDDC graph for subdomain %04d\n",PetscGlobalRank);CHKERRQ(ierr);
73   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Number of vertices %d\n",graph->nvtxs);CHKERRQ(ierr);
74   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Custom minimal size %d\n",graph->custom_minimal_size);CHKERRQ(ierr);
75   if (graph->maxcount != PETSC_MAX_INT) {
76     ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Max count %d\n",graph->maxcount);CHKERRQ(ierr);
77   }
78   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Topological two dim? %d (set %d)\n",graph->twodim,graph->twodimset);CHKERRQ(ierr);
79   if (verbosity_level > 2) {
80     for (i=0;i<graph->nvtxs;i++) {
81       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%d:\n",i);CHKERRQ(ierr);
82       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"   which_dof: %d\n",graph->which_dof[i]);CHKERRQ(ierr);
83       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"   special_dof: %d\n",graph->special_dof[i]);CHKERRQ(ierr);
84       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"   neighbours: %d\n",graph->count[i]);CHKERRQ(ierr);
85       ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
86       if (graph->count[i]) {
87         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"     set of neighbours:");CHKERRQ(ierr);
88         for (j=0;j<graph->count[i];j++) {
89           ierr = PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->neighbours_set[i][j]);CHKERRQ(ierr);
90         }
91         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
92       }
93       ierr = PetscViewerASCIISetTab(viewer,tabs);CHKERRQ(ierr);
94       ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
95       if (graph->mirrors) {
96         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"   mirrors: %d\n",graph->mirrors[i]);CHKERRQ(ierr);
97         if (graph->mirrors[i]) {
98           ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
99           ierr = PetscViewerASCIISynchronizedPrintf(viewer,"     set of mirrors:");CHKERRQ(ierr);
100           for (j=0;j<graph->mirrors[i];j++) {
101             ierr = PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->mirrors_set[i][j]);CHKERRQ(ierr);
102           }
103           ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
104           ierr = PetscViewerASCIISetTab(viewer,tabs);CHKERRQ(ierr);
105           ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
106         }
107       }
108       if (verbosity_level > 3) {
109         if (graph->xadj) {
110           ierr = PetscViewerASCIISynchronizedPrintf(viewer,"   local adj list:");CHKERRQ(ierr);
111           ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
112           for (j=graph->xadj[i];j<graph->xadj[i+1];j++) {
113             ierr = PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->adjncy[j]);CHKERRQ(ierr);
114           }
115           ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
116           ierr = PetscViewerASCIISetTab(viewer,tabs);CHKERRQ(ierr);
117           ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
118         } else {
119           ierr = PetscViewerASCIISynchronizedPrintf(viewer,"   no adj info\n");CHKERRQ(ierr);
120         }
121       }
122       if (graph->n_local_subs) {
123         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"   local sub id: %d\n",graph->local_subs[i]);CHKERRQ(ierr);
124       }
125       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"   interface subset id: %d\n",graph->subset[i]);CHKERRQ(ierr);
126       if (graph->subset[i] && graph->subset_ncc) {
127         ierr = PetscViewerASCIISynchronizedPrintf(viewer,"   ncc for subset: %d\n",graph->subset_ncc[graph->subset[i]-1]);CHKERRQ(ierr);
128       }
129     }
130   }
131   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Total number of connected components %d\n",graph->ncc);CHKERRQ(ierr);
132   ierr = PetscMalloc1(graph->cptr[graph->ncc],&queue_in_global_numbering);CHKERRQ(ierr);
133   ierr = ISLocalToGlobalMappingApply(graph->l2gmap,graph->cptr[graph->ncc],graph->queue,queue_in_global_numbering);CHKERRQ(ierr);
134   for (i=0;i<graph->ncc;i++) {
135     PetscInt node_num=graph->queue[graph->cptr[i]];
136     PetscBool printcc = PETSC_FALSE;
137     ierr = PetscViewerASCIISynchronizedPrintf(viewer,"  cc %d (size %d, fid %d, neighs:",i,graph->cptr[i+1]-graph->cptr[i],graph->which_dof[node_num]);CHKERRQ(ierr);
138     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
139     for (j=0;j<graph->count[node_num];j++) {
140       ierr = PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->neighbours_set[node_num][j]);CHKERRQ(ierr);
141     }
142     if (verbosity_level > 1) {
143       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"):");CHKERRQ(ierr);
144       if (verbosity_level > 2 || graph->twodim || graph->count[node_num] > 1 || (graph->count[node_num] == 1 && graph->special_dof[node_num] == PCBDDCGRAPH_NEUMANN_MARK)) {
145         printcc = PETSC_TRUE;
146       }
147       if (printcc) {
148         for (j=graph->cptr[i];j<graph->cptr[i+1];j++) {
149           ierr = PetscViewerASCIISynchronizedPrintf(viewer," %d (%d)",graph->queue[j],queue_in_global_numbering[j]);CHKERRQ(ierr);
150         }
151       }
152     } else {
153       ierr = PetscViewerASCIISynchronizedPrintf(viewer,")");CHKERRQ(ierr);
154     }
155     ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
156     ierr = PetscViewerASCIISetTab(viewer,tabs);CHKERRQ(ierr);
157     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
158   }
159   ierr = PetscFree(queue_in_global_numbering);CHKERRQ(ierr);
160   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
161   PetscFunctionReturn(0);
162 }
163 
164 PetscErrorCode PCBDDCGraphRestoreCandidatesIS(PCBDDCGraph graph, PetscInt *n_faces, IS *FacesIS[], PetscInt *n_edges, IS *EdgesIS[], IS *VerticesIS)
165 {
166   PetscInt       i;
167   PetscErrorCode ierr;
168 
169   PetscFunctionBegin;
170   if (n_faces) {
171     if (FacesIS) {
172       for (i=0;i<*n_faces;i++) {
173         ierr = ISDestroy(&((*FacesIS)[i]));CHKERRQ(ierr);
174       }
175       ierr = PetscFree(*FacesIS);CHKERRQ(ierr);
176     }
177     *n_faces = 0;
178   }
179   if (n_edges) {
180     if (EdgesIS) {
181       for (i=0;i<*n_edges;i++) {
182         ierr = ISDestroy(&((*EdgesIS)[i]));CHKERRQ(ierr);
183       }
184       ierr = PetscFree(*EdgesIS);CHKERRQ(ierr);
185     }
186     *n_edges = 0;
187   }
188   if (VerticesIS) {
189     ierr = ISDestroy(VerticesIS);CHKERRQ(ierr);
190   }
191   PetscFunctionReturn(0);
192 }
193 
194 PetscErrorCode PCBDDCGraphGetCandidatesIS(PCBDDCGraph graph, PetscInt *n_faces, IS *FacesIS[], PetscInt *n_edges, IS *EdgesIS[], IS *VerticesIS)
195 {
196   IS             *ISForFaces,*ISForEdges,ISForVertices;
197   PetscInt       i,nfc,nec,nvc,*idx,*mark;
198   PetscErrorCode ierr;
199 
200   PetscFunctionBegin;
201   ierr = PetscCalloc1(graph->ncc,&mark);CHKERRQ(ierr);
202   /* loop on ccs to evalute number of faces, edges and vertices */
203   nfc = 0;
204   nec = 0;
205   nvc = 0;
206   for (i=0;i<graph->ncc;i++) {
207     PetscInt repdof = graph->queue[graph->cptr[i]];
208     if (graph->cptr[i+1]-graph->cptr[i] > graph->custom_minimal_size && graph->count[repdof] < graph->maxcount) {
209       if (!graph->twodim && graph->count[repdof] == 1 && graph->special_dof[repdof] != PCBDDCGRAPH_NEUMANN_MARK) {
210         nfc++;
211         mark[i] = 2;
212       } else {
213         nec++;
214         mark[i] = 1;
215       }
216     } else {
217       nvc += graph->cptr[i+1]-graph->cptr[i];
218     }
219   }
220 
221   /* allocate IS arrays for faces, edges. Vertices need a single index set. */
222   if (FacesIS) {
223     ierr = PetscMalloc1(nfc,&ISForFaces);CHKERRQ(ierr);
224   }
225   if (EdgesIS) {
226     ierr = PetscMalloc1(nec,&ISForEdges);CHKERRQ(ierr);
227   }
228   if (VerticesIS) {
229     ierr = PetscMalloc1(nvc,&idx);CHKERRQ(ierr);
230   }
231 
232   /* loop on ccs to compute index sets for faces and edges */
233   if (!graph->queue_sorted) {
234     PetscInt *queue_global;
235 
236     ierr = PetscMalloc1(graph->cptr[graph->ncc],&queue_global);CHKERRQ(ierr);
237     ierr = ISLocalToGlobalMappingApply(graph->l2gmap,graph->cptr[graph->ncc],graph->queue,queue_global);CHKERRQ(ierr);
238     for (i=0;i<graph->ncc;i++) {
239       ierr = PetscSortIntWithArray(graph->cptr[i+1]-graph->cptr[i],&queue_global[graph->cptr[i]],&graph->queue[graph->cptr[i]]);CHKERRQ(ierr);
240     }
241     ierr = PetscFree(queue_global);CHKERRQ(ierr);
242     graph->queue_sorted = PETSC_TRUE;
243   }
244   nfc = 0;
245   nec = 0;
246   for (i=0;i<graph->ncc;i++) {
247     if (mark[i] == 2) {
248       if (FacesIS) {
249         ierr = ISCreateGeneral(PETSC_COMM_SELF,graph->cptr[i+1]-graph->cptr[i],&graph->queue[graph->cptr[i]],PETSC_USE_POINTER,&ISForFaces[nfc]);CHKERRQ(ierr);
250       }
251       nfc++;
252     } else if (mark[i] == 1) {
253       if (EdgesIS) {
254         ierr = ISCreateGeneral(PETSC_COMM_SELF,graph->cptr[i+1]-graph->cptr[i],&graph->queue[graph->cptr[i]],PETSC_USE_POINTER,&ISForEdges[nec]);CHKERRQ(ierr);
255       }
256       nec++;
257     }
258   }
259 
260   /* index set for vertices */
261   if (VerticesIS) {
262     nvc = 0;
263     for (i=0;i<graph->ncc;i++) {
264       if (!mark[i]) {
265         PetscInt j;
266 
267         for (j=graph->cptr[i];j<graph->cptr[i+1];j++) {
268           idx[nvc]=graph->queue[j];
269           nvc++;
270         }
271       }
272     }
273     /* sort vertex set (by local ordering) */
274     ierr = PetscSortInt(nvc,idx);CHKERRQ(ierr);
275     ierr = ISCreateGeneral(PETSC_COMM_SELF,nvc,idx,PETSC_OWN_POINTER,&ISForVertices);CHKERRQ(ierr);
276   }
277   ierr = PetscFree(mark);CHKERRQ(ierr);
278 
279   /* get back info */
280   if (n_faces)       *n_faces = nfc;
281   if (FacesIS)       *FacesIS = ISForFaces;
282   if (n_edges)       *n_edges = nec;
283   if (EdgesIS)       *EdgesIS = ISForEdges;
284   if (VerticesIS) *VerticesIS = ISForVertices;
285   PetscFunctionReturn(0);
286 }
287 
288 PetscErrorCode PCBDDCGraphComputeConnectedComponents(PCBDDCGraph graph)
289 {
290   PetscBool      adapt_interface_reduced;
291   MPI_Comm       interface_comm;
292   PetscMPIInt    size;
293   PetscInt       i;
294   PetscBT        cornerp;
295   PetscErrorCode ierr;
296 
297   PetscFunctionBegin;
298   /* compute connected components locally */
299   ierr = PetscObjectGetComm((PetscObject)(graph->l2gmap),&interface_comm);CHKERRQ(ierr);
300   ierr = PCBDDCGraphComputeConnectedComponentsLocal(graph);CHKERRQ(ierr);
301 
302   cornerp = NULL;
303   if (graph->active_coords) { /* face based corner selection */
304     PetscReal      *wdist;
305     PetscInt       n_neigh,*neigh,*n_shared,**shared;
306     PetscInt       maxc, ns;
307 
308     ierr = PetscBTCreate(graph->nvtxs,&cornerp);CHKERRQ(ierr);
309     ierr = ISLocalToGlobalMappingGetInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);CHKERRQ(ierr);
310     for (ns = 1, maxc = 0; ns < n_neigh; ns++) maxc = PetscMax(maxc,n_shared[ns]);
311     ierr = PetscMalloc1(maxc*graph->cdim,&wdist);CHKERRQ(ierr);
312 
313     for (ns = 1; ns < n_neigh; ns++) { /* first proc is self */
314       PetscReal *anchor,mdist;
315       PetscInt  j,k,d,cdim = graph->cdim;
316       PetscInt  point1,point2,point3;
317 /*
318       PetscBool isface = PETSC_FALSE;
319 
320       for (i=0;i<n_shared[ns];i++) {
321         if (graph->count[shared[ns][i]] == 1) {
322           isface = PETSC_TRUE;
323           break;
324         }
325       }
326       if (!isface) continue;
327 */
328       /* import coordinates on shared interface */
329       for (j=0,k=0;j<n_shared[ns];j++)
330         for (d=0;d<cdim;d++)
331           wdist[k++] = graph->coords[shared[ns][j]*cdim+d];
332 
333       /* the dofs are sorted by global numbering, so each rank start from the same id and will detect the same corners */
334       anchor = wdist;
335 
336       /* find the farthest point from the starting one */
337       mdist = -1.0;
338       for (j=0,point1=0;j<n_shared[ns];j++) {
339         PetscReal dist = 0.0;
340 
341         for (d=0;d<cdim;d++) dist += (wdist[j*cdim+d]-anchor[d])*(wdist[j*cdim+d]-anchor[d]);
342         if (dist > mdist) { mdist = dist; point1 = j; }
343       }
344 
345       /* find the farthest point from point1 */
346       anchor = wdist + point1*cdim;
347       mdist  = -1.0;
348       for (j=0,point2=0;j<n_shared[ns];j++) {
349         PetscReal dist = 0.0;
350 
351         for (d=0;d<cdim;d++) dist += (wdist[j*cdim+d]-anchor[d])*(wdist[j*cdim+d]-anchor[d]);
352         if (dist > mdist) { mdist = dist; point2 = j; }
353       }
354 
355       /* find the third point maximizing the triangle area */
356       point3 = point2;
357       if (cdim > 2) {
358         PetscReal a = 0.0;
359 
360         for (d=0;d<cdim;d++) a += (wdist[point1*cdim+d]-wdist[point2*cdim+d])*(wdist[point1*cdim+d]-wdist[point2*cdim+d]);
361         mdist = -1.0;
362         for (j=0,point3=0;j<n_shared[ns];j++) {
363           PetscReal area,b = 0.0, c = 0.0;
364 
365           for (d=0;d<cdim;d++) {
366             b += (wdist[point1*cdim+d]-wdist[j*cdim+d])*(wdist[point1*cdim+d]-wdist[j*cdim+d]);
367             c += (wdist[point2*cdim+d]-wdist[j*cdim+d])*(wdist[point2*cdim+d]-wdist[j*cdim+d]);
368           }
369           area = (a+b+c)*(-a+b+c)*(a-b+c)*(a+b-c); /* Heron's formula without divisions by 2 */
370           if (area > mdist) { mdist = area; point3 = j; }
371         }
372       }
373 
374 #if 0
375       printf("RANK %d: start  %d (%g %g) (size %d)\n",PetscGlobalRank,shared[ns][0],wdist[0],wdist[1],n_shared[ns]);
376       printf("RANK %d: point1 %d (%g %g)\n",PetscGlobalRank,shared[ns][point1],wdist[point1*cdim + 0],wdist[point1*cdim + 1]);
377       printf("RANK %d: point2 %d (%g %g)\n",PetscGlobalRank,shared[ns][point2],wdist[point2*cdim + 0],wdist[point2*cdim + 1]);
378       printf("RANK %d: point3 %d (%g %g)\n",PetscGlobalRank,shared[ns][point3],wdist[point3*cdim + 0],wdist[point3*cdim + 1]);
379 #endif
380       /* all dofs having the same coordinates will be primal */
381       for (j=0;j<n_shared[ns];j++) {
382         PetscBool same[3] = {PETSC_TRUE,PETSC_TRUE,PETSC_TRUE};
383 
384         for (d=0;d<cdim;d++) {
385           same[0] = (PetscBool)(same[0] && (PetscAbsReal(wdist[j*cdim + d]-wdist[point1*cdim+d]) < PETSC_SMALL));
386           same[1] = (PetscBool)(same[1] && (PetscAbsReal(wdist[j*cdim + d]-wdist[point2*cdim+d]) < PETSC_SMALL));
387           same[2] = (PetscBool)(same[2] && (PetscAbsReal(wdist[j*cdim + d]-wdist[point3*cdim+d]) < PETSC_SMALL));
388         }
389         if (same[0] || same[1] || same[2]) {
390           ierr = PetscBTSet(cornerp,shared[ns][j]);CHKERRQ(ierr);
391         }
392       }
393     }
394     ierr = PetscFree(wdist);CHKERRQ(ierr);
395     ierr = ISLocalToGlobalMappingRestoreInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);CHKERRQ(ierr);
396   }
397 
398   /* check consistency of connected components among neighbouring subdomains -> it adapt them in case it is needed */
399   ierr = MPI_Comm_size(interface_comm,&size);CHKERRQ(ierr);
400   adapt_interface_reduced = PETSC_FALSE;
401   if (size > 1) {
402     PetscInt i;
403     PetscBool adapt_interface = cornerp ? PETSC_TRUE : PETSC_FALSE;
404     for (i=0;i<graph->n_subsets && !adapt_interface;i++) {
405       /* We are not sure that on a given subset of the local interface,
406          with two connected components, the latters be the same among sharing subdomains */
407       if (graph->subset_ncc[i] > 1) adapt_interface = PETSC_TRUE;
408     }
409     ierr = MPIU_Allreduce(&adapt_interface,&adapt_interface_reduced,1,MPIU_BOOL,MPI_LOR,interface_comm);CHKERRQ(ierr);
410   }
411 
412   if (graph->n_subsets && adapt_interface_reduced) {
413     PetscBT     subset_cc_adapt;
414     MPI_Request *send_requests,*recv_requests;
415     PetscInt    *send_buffer,*recv_buffer;
416     PetscInt    sum_requests,start_of_recv,start_of_send;
417     PetscInt    *cum_recv_counts;
418     PetscInt    *labels;
419     PetscInt    ncc,cum_queue,mss,mns,j,k,s;
420     PetscInt    **refine_buffer=NULL,*private_labels = NULL;
421     PetscBool   *subset_has_corn,*recv_buffer_bool;
422 
423     ierr = PetscCalloc1(graph->n_subsets,&subset_has_corn);CHKERRQ(ierr);
424     if (cornerp) {
425       for (i=0;i<graph->n_subsets;i++) {
426         for (j=0;j<graph->subset_size[i];j++) {
427           if (PetscBTLookup(cornerp,graph->subset_idxs[i][j])) {
428             subset_has_corn[i] = PETSC_TRUE;
429             break;
430           }
431         }
432       }
433     }
434     ierr = PetscMalloc1(graph->nvtxs,&labels);CHKERRQ(ierr);
435     ierr = PetscMemzero(labels,graph->nvtxs*sizeof(*labels));CHKERRQ(ierr);
436     for (i=0,k=0;i<graph->ncc;i++) {
437       PetscInt s = 1;
438       for (j=graph->cptr[i];j<graph->cptr[i+1];j++) {
439         if (cornerp && PetscBTLookup(cornerp,graph->queue[j])) {
440           labels[graph->queue[j]] = k+s;
441           s += 1;
442         } else {
443           labels[graph->queue[j]] = k;
444         }
445       }
446       k += s;
447     }
448 
449     /* allocate some space */
450     ierr = PetscMalloc1(graph->n_subsets+1,&cum_recv_counts);CHKERRQ(ierr);
451     ierr = PetscMemzero(cum_recv_counts,(graph->n_subsets+1)*sizeof(*cum_recv_counts));CHKERRQ(ierr);
452 
453     /* first count how many neighbours per connected component I will receive from */
454     cum_recv_counts[0] = 0;
455     for (i=0;i<graph->n_subsets;i++) cum_recv_counts[i+1] = cum_recv_counts[i]+graph->count[graph->subset_idxs[i][0]];
456     ierr = PetscMalloc1(cum_recv_counts[graph->n_subsets],&recv_buffer_bool);CHKERRQ(ierr);
457     ierr = PetscMalloc2(cum_recv_counts[graph->n_subsets],&send_requests,cum_recv_counts[graph->n_subsets],&recv_requests);CHKERRQ(ierr);
458     for (i=0;i<cum_recv_counts[graph->n_subsets];i++) {
459       send_requests[i] = MPI_REQUEST_NULL;
460       recv_requests[i] = MPI_REQUEST_NULL;
461     }
462 
463     /* exchange with my neighbours the number of my connected components on the subset of interface */
464     sum_requests = 0;
465     for (i=0;i<graph->n_subsets;i++) {
466       PetscMPIInt neigh,tag;
467       PetscInt    count,*neighs;
468 
469       count  = graph->count[graph->subset_idxs[i][0]];
470       neighs = graph->neighbours_set[graph->subset_idxs[i][0]];
471       ierr   = PetscMPIIntCast(2*graph->subset_ref_node[i],&tag);CHKERRQ(ierr);
472       for (k=0;k<count;k++) {
473         PetscBool ss = (PetscBool)(graph->subset_ncc[i] > 1 || subset_has_corn[i]);
474 
475         ierr = PetscMPIIntCast(neighs[k],&neigh);CHKERRQ(ierr);
476         ierr = MPI_Isend(&ss,1,MPIU_BOOL,neigh,tag,interface_comm,&send_requests[sum_requests]);CHKERRQ(ierr);
477         ierr = MPI_Irecv(&recv_buffer_bool[sum_requests],1,MPIU_BOOL,neigh,tag,interface_comm,&recv_requests[sum_requests]);CHKERRQ(ierr);
478         sum_requests++;
479       }
480     }
481     ierr = MPI_Waitall(sum_requests,recv_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr);
482     ierr = MPI_Waitall(sum_requests,send_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr);
483 
484     /* determine the subsets I have to adapt (those having more than 1 cc) */
485     ierr = PetscBTCreate(graph->n_subsets,&subset_cc_adapt);CHKERRQ(ierr);
486     ierr = PetscBTMemzero(graph->n_subsets,subset_cc_adapt);CHKERRQ(ierr);
487     for (i=0;i<graph->n_subsets;i++) {
488       if (graph->subset_ncc[i] > 1 || subset_has_corn[i]) {
489         ierr = PetscBTSet(subset_cc_adapt,i);CHKERRQ(ierr);
490         continue;
491       }
492       for (j=cum_recv_counts[i];j<cum_recv_counts[i+1];j++){
493          if (recv_buffer_bool[j]) {
494           ierr = PetscBTSet(subset_cc_adapt,i);CHKERRQ(ierr);
495           break;
496         }
497       }
498     }
499     ierr = PetscFree(recv_buffer_bool);CHKERRQ(ierr);
500     ierr = PetscFree(subset_has_corn);CHKERRQ(ierr);
501 
502     /* determine send/recv buffers sizes */
503     j = 0;
504     mss = 0;
505     for (i=0;i<graph->n_subsets;i++) {
506       if (PetscBTLookup(subset_cc_adapt,i)) {
507         j  += graph->subset_size[i];
508         mss = PetscMax(graph->subset_size[i],mss);
509       }
510     }
511     k = 0;
512     mns = 0;
513     for (i=0;i<graph->n_subsets;i++) {
514       if (PetscBTLookup(subset_cc_adapt,i)) {
515         k  += (cum_recv_counts[i+1]-cum_recv_counts[i])*graph->subset_size[i];
516         mns = PetscMax(cum_recv_counts[i+1]-cum_recv_counts[i],mns);
517       }
518     }
519     ierr = PetscMalloc2(j,&send_buffer,k,&recv_buffer);CHKERRQ(ierr);
520 
521     /* fill send buffer (order matters: subset_idxs ordered by global ordering) */
522     j = 0;
523     for (i=0;i<graph->n_subsets;i++)
524       if (PetscBTLookup(subset_cc_adapt,i))
525         for (k=0;k<graph->subset_size[i];k++)
526           send_buffer[j++] = labels[graph->subset_idxs[i][k]];
527 
528     /* now exchange the data */
529     start_of_recv = 0;
530     start_of_send = 0;
531     sum_requests  = 0;
532     for (i=0;i<graph->n_subsets;i++) {
533       if (PetscBTLookup(subset_cc_adapt,i)) {
534         PetscMPIInt neigh,tag;
535         PetscInt    size_of_send = graph->subset_size[i];
536 
537         j    = graph->subset_idxs[i][0];
538         ierr = PetscMPIIntCast(2*graph->subset_ref_node[i]+1,&tag);CHKERRQ(ierr);
539         for (k=0;k<graph->count[j];k++) {
540           ierr = PetscMPIIntCast(graph->neighbours_set[j][k],&neigh);CHKERRQ(ierr);
541           ierr = MPI_Isend(&send_buffer[start_of_send],size_of_send,MPIU_INT,neigh,tag,interface_comm,&send_requests[sum_requests]);CHKERRQ(ierr);
542           ierr = MPI_Irecv(&recv_buffer[start_of_recv],size_of_send,MPIU_INT,neigh,tag,interface_comm,&recv_requests[sum_requests]);CHKERRQ(ierr);
543           start_of_recv += size_of_send;
544           sum_requests++;
545         }
546         start_of_send += size_of_send;
547       }
548     }
549     ierr = MPI_Waitall(sum_requests,recv_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr);
550 
551     /* refine connected components */
552     start_of_recv = 0;
553     /* allocate some temporary space */
554     if (mss) {
555       ierr = PetscMalloc1(mss,&refine_buffer);CHKERRQ(ierr);
556       ierr = PetscMalloc2(mss*(mns+1),&refine_buffer[0],mss,&private_labels);CHKERRQ(ierr);
557     }
558     ncc = 0;
559     cum_queue = 0;
560     graph->cptr[0] = 0;
561     for (i=0;i<graph->n_subsets;i++) {
562       if (PetscBTLookup(subset_cc_adapt,i)) {
563         PetscInt subset_counter = 0;
564         PetscInt sharingprocs = cum_recv_counts[i+1]-cum_recv_counts[i]+1; /* count myself */
565         PetscInt buffer_size = graph->subset_size[i];
566 
567         /* compute pointers */
568         for (j=1;j<buffer_size;j++) refine_buffer[j] = refine_buffer[j-1] + sharingprocs;
569         /* analyze contributions from subdomains that share the i-th subset
570            The structure of refine_buffer is suitable to find intersections of ccs among sharingprocs.
571            supposing the current subset is shared by 3 processes and has dimension 5 with global dofs 0,1,2,3,4 (local 0,4,3,1,2)
572            sharing procs connected components:
573              neigh 0: [0 1 4], [2 3], labels [4,7]  (2 connected components)
574              neigh 1: [0 1], [2 3 4], labels [3 2]  (2 connected components)
575              neigh 2: [0 4], [1], [2 3], labels [1 5 6] (3 connected components)
576            refine_buffer will be filled as:
577              [ 4, 3, 1;
578                4, 2, 1;
579                7, 2, 6;
580                4, 3, 5;
581                7, 2, 6; ];
582            The connected components in local ordering are [0], [1], [2 3], [4] */
583         /* fill temp_buffer */
584         for (k=0;k<buffer_size;k++) refine_buffer[k][0] = labels[graph->subset_idxs[i][k]];
585         for (j=0;j<sharingprocs-1;j++) {
586           for (k=0;k<buffer_size;k++) refine_buffer[k][j+1] = recv_buffer[start_of_recv+k];
587           start_of_recv += buffer_size;
588         }
589         ierr = PetscMemzero(private_labels,buffer_size*sizeof(PetscInt));CHKERRQ(ierr);
590         for (j=0;j<buffer_size;j++) {
591           if (!private_labels[j]) { /* found a new cc  */
592             PetscBool same_set;
593 
594             graph->cptr[ncc] = cum_queue;
595             ncc++;
596             subset_counter++;
597             private_labels[j] = subset_counter;
598             graph->queue[cum_queue++] = graph->subset_idxs[i][j];
599             for (k=j+1;k<buffer_size;k++) { /* check for other nodes in new cc */
600               same_set = PETSC_TRUE;
601               for (s=0;s<sharingprocs;s++) {
602                 if (refine_buffer[j][s] != refine_buffer[k][s]) {
603                   same_set = PETSC_FALSE;
604                   break;
605                 }
606               }
607               if (same_set) {
608                 private_labels[k] = subset_counter;
609                 graph->queue[cum_queue++] = graph->subset_idxs[i][k];
610               }
611             }
612           }
613         }
614         graph->cptr[ncc]     = cum_queue;
615         graph->subset_ncc[i] = subset_counter;
616         graph->queue_sorted  = PETSC_FALSE;
617       } else { /* this subset does not need to be adapted */
618         ierr = PetscMemcpy(graph->queue+cum_queue,graph->subset_idxs[i],graph->subset_size[i]*sizeof(PetscInt));CHKERRQ(ierr);
619         ncc++;
620         cum_queue += graph->subset_size[i];
621         graph->cptr[ncc] = cum_queue;
622       }
623     }
624     graph->cptr[ncc] = cum_queue;
625     graph->ncc       = ncc;
626     if (mss) {
627       ierr = PetscFree2(refine_buffer[0],private_labels);CHKERRQ(ierr);
628       ierr = PetscFree(refine_buffer);CHKERRQ(ierr);
629     }
630     ierr = PetscFree(labels);CHKERRQ(ierr);
631     ierr = MPI_Waitall(sum_requests,send_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr);
632     ierr = PetscFree2(send_requests,recv_requests);CHKERRQ(ierr);
633     ierr = PetscFree2(send_buffer,recv_buffer);CHKERRQ(ierr);
634     ierr = PetscFree(cum_recv_counts);CHKERRQ(ierr);
635     ierr = PetscBTDestroy(&subset_cc_adapt);CHKERRQ(ierr);
636   }
637   ierr = PetscBTDestroy(&cornerp);CHKERRQ(ierr);
638 
639   /* Determine if we are in 2D or 3D */
640   if (!graph->twodimset) {
641     PetscBool twodim = PETSC_TRUE;
642     for (i=0;i<graph->ncc;i++) {
643       PetscInt repdof = graph->queue[graph->cptr[i]];
644       PetscInt ccsize = graph->cptr[i+1]-graph->cptr[i];
645       if (graph->count[repdof] > 1 && ccsize > graph->custom_minimal_size) {
646         twodim = PETSC_FALSE;
647         break;
648       }
649     }
650     ierr = MPIU_Allreduce(&twodim,&graph->twodim,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)graph->l2gmap));CHKERRQ(ierr);
651     graph->twodimset = PETSC_TRUE;
652   }
653   PetscFunctionReturn(0);
654 }
655 
656 
657 PETSC_STATIC_INLINE PetscErrorCode PCBDDCGraphComputeCC_Private(PCBDDCGraph graph,PetscInt pid,PetscInt* queue_tip,PetscInt n_prev,PetscInt* n_added)
658 {
659   PetscInt       i,j,n;
660   PetscInt       *xadj = graph->xadj,*adjncy = graph->adjncy;
661   PetscBT        touched = graph->touched;
662   PetscBool      havecsr = (PetscBool)(!!xadj);
663   PetscBool      havesubs = (PetscBool)(!!graph->n_local_subs);
664   PetscErrorCode ierr;
665 
666   PetscFunctionBegin;
667   n = 0;
668   if (havecsr && !havesubs) {
669     for (i=-n_prev;i<0;i++) {
670       PetscInt start_dof = queue_tip[i];
671       /* we assume that if a dof has a size 1 adjacency list and the corresponding entry is negative, it is connected to all dofs */
672       if (xadj[start_dof+1]-xadj[start_dof] == 1 && adjncy[xadj[start_dof]] < 0) {
673         for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */
674           PetscInt dof = graph->subset_idxs[pid-1][j];
675           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid) {
676             ierr = PetscBTSet(touched,dof);CHKERRQ(ierr);
677             queue_tip[n] = dof;
678             n++;
679           }
680         }
681       } else {
682         for (j=xadj[start_dof];j<xadj[start_dof+1];j++) {
683           PetscInt dof = adjncy[j];
684           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid) {
685             ierr = PetscBTSet(touched,dof);CHKERRQ(ierr);
686             queue_tip[n] = dof;
687             n++;
688           }
689         }
690       }
691     }
692   } else if (havecsr && havesubs) {
693     PetscInt sid = graph->local_subs[queue_tip[-n_prev]];
694     for (i=-n_prev;i<0;i++) {
695       PetscInt start_dof = queue_tip[i];
696       /* we assume that if a dof has a size 1 adjacency list and the corresponding entry is negative, it is connected to all dofs belonging to the local sub */
697       if (xadj[start_dof+1]-xadj[start_dof] == 1 && adjncy[xadj[start_dof]] < 0) {
698         for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */
699           PetscInt dof = graph->subset_idxs[pid-1][j];
700           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) {
701             ierr = PetscBTSet(touched,dof);CHKERRQ(ierr);
702             queue_tip[n] = dof;
703             n++;
704           }
705         }
706       } else {
707         for (j=xadj[start_dof];j<xadj[start_dof+1];j++) {
708           PetscInt dof = adjncy[j];
709           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) {
710             ierr = PetscBTSet(touched,dof);CHKERRQ(ierr);
711             queue_tip[n] = dof;
712             n++;
713           }
714         }
715       }
716     }
717   } else if (havesubs) { /* sub info only */
718     PetscInt sid = graph->local_subs[queue_tip[-n_prev]];
719     for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */
720       PetscInt dof = graph->subset_idxs[pid-1][j];
721       if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) {
722         ierr = PetscBTSet(touched,dof);CHKERRQ(ierr);
723         queue_tip[n] = dof;
724         n++;
725       }
726     }
727   } else {
728     for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */
729       PetscInt dof = graph->subset_idxs[pid-1][j];
730       if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid) {
731         ierr = PetscBTSet(touched,dof);CHKERRQ(ierr);
732         queue_tip[n] = dof;
733         n++;
734       }
735     }
736   }
737   *n_added = n;
738   PetscFunctionReturn(0);
739 }
740 
741 PetscErrorCode PCBDDCGraphComputeConnectedComponentsLocal(PCBDDCGraph graph)
742 {
743   PetscInt       ncc,cum_queue,n;
744   PetscMPIInt    commsize;
745   PetscErrorCode ierr;
746 
747   PetscFunctionBegin;
748   if (!graph->setupcalled) SETERRQ(PetscObjectComm((PetscObject)graph->l2gmap),PETSC_ERR_ORDER,"PCBDDCGraphSetUp should be called first");
749   /* quiet return if there isn't any local info */
750   if (!graph->xadj && !graph->n_local_subs) {
751     PetscFunctionReturn(0);
752   }
753 
754   /* reset any previous search of connected components */
755   ierr = PetscBTMemzero(graph->nvtxs,graph->touched);CHKERRQ(ierr);
756   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)graph->l2gmap),&commsize);CHKERRQ(ierr);
757   if (commsize > graph->commsizelimit) {
758     PetscInt i;
759     for (i=0;i<graph->nvtxs;i++) {
760       if (graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK || !graph->count[i]) {
761         ierr = PetscBTSet(graph->touched,i);CHKERRQ(ierr);
762       }
763     }
764   }
765 
766   /* begin search for connected components */
767   cum_queue = 0;
768   ncc = 0;
769   for (n=0;n<graph->n_subsets;n++) {
770     PetscInt pid = n+1;  /* partition labeled by 0 is discarded */
771     PetscInt found = 0,prev = 0,first = 0,ncc_pid = 0;
772     while (found != graph->subset_size[n]) {
773       PetscInt added = 0;
774       if (!prev) { /* search for new starting dof */
775         while (PetscBTLookup(graph->touched,graph->subset_idxs[n][first])) first++;
776         ierr = PetscBTSet(graph->touched,graph->subset_idxs[n][first]);CHKERRQ(ierr);
777         graph->queue[cum_queue] = graph->subset_idxs[n][first];
778         graph->cptr[ncc] = cum_queue;
779         prev = 1;
780         cum_queue++;
781         found++;
782         ncc_pid++;
783         ncc++;
784       }
785       ierr = PCBDDCGraphComputeCC_Private(graph,pid,graph->queue + cum_queue,prev,&added);CHKERRQ(ierr);
786       if (!added) {
787         graph->subset_ncc[n] = ncc_pid;
788         graph->cptr[ncc] = cum_queue;
789       }
790       prev = added;
791       found += added;
792       cum_queue += added;
793       if (added && found == graph->subset_size[n]) {
794         graph->subset_ncc[n] = ncc_pid;
795         graph->cptr[ncc] = cum_queue;
796       }
797     }
798   }
799   graph->ncc = ncc;
800   graph->queue_sorted = PETSC_FALSE;
801   PetscFunctionReturn(0);
802 }
803 
804 PetscErrorCode PCBDDCGraphSetUp(PCBDDCGraph graph, PetscInt custom_minimal_size, IS neumann_is, IS dirichlet_is, PetscInt n_ISForDofs, IS ISForDofs[], IS custom_primal_vertices)
805 {
806   IS             subset,subset_n;
807   MPI_Comm       comm;
808   const PetscInt *is_indices;
809   PetscInt       n_neigh,*neigh,*n_shared,**shared,*queue_global;
810   PetscInt       i,j,k,s,total_counts,nodes_touched,is_size;
811   PetscMPIInt    commsize;
812   PetscBool      same_set,mirrors_found;
813   PetscErrorCode ierr;
814 
815   PetscFunctionBegin;
816   PetscValidLogicalCollectiveInt(graph->l2gmap,custom_minimal_size,2);
817   if (neumann_is) {
818     PetscValidHeaderSpecific(neumann_is,IS_CLASSID,3);
819     PetscCheckSameComm(graph->l2gmap,1,neumann_is,3);
820   }
821   graph->has_dirichlet = PETSC_FALSE;
822   if (dirichlet_is) {
823     PetscValidHeaderSpecific(dirichlet_is,IS_CLASSID,4);
824     PetscCheckSameComm(graph->l2gmap,1,dirichlet_is,4);
825     graph->has_dirichlet = PETSC_TRUE;
826   }
827   PetscValidLogicalCollectiveInt(graph->l2gmap,n_ISForDofs,5);
828   for (i=0;i<n_ISForDofs;i++) {
829     PetscValidHeaderSpecific(ISForDofs[i],IS_CLASSID,6);
830     PetscCheckSameComm(graph->l2gmap,1,ISForDofs[i],6);
831   }
832   if (custom_primal_vertices) {
833     PetscValidHeaderSpecific(custom_primal_vertices,IS_CLASSID,7);
834     PetscCheckSameComm(graph->l2gmap,1,custom_primal_vertices,7);
835   }
836   ierr = PetscObjectGetComm((PetscObject)(graph->l2gmap),&comm);CHKERRQ(ierr);
837   ierr = MPI_Comm_size(comm,&commsize);CHKERRQ(ierr);
838 
839   /* custom_minimal_size */
840   graph->custom_minimal_size = custom_minimal_size;
841   /* get info l2gmap and allocate work vectors  */
842   ierr = ISLocalToGlobalMappingGetInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);CHKERRQ(ierr);
843   /* check if we have any local periodic nodes (periodic BCs) */
844   mirrors_found = PETSC_FALSE;
845   if (graph->nvtxs && n_neigh) {
846     for (i=0; i<n_shared[0]; i++) graph->count[shared[0][i]] += 1;
847     for (i=0; i<n_shared[0]; i++) {
848       if (graph->count[shared[0][i]] > 1) {
849         mirrors_found = PETSC_TRUE;
850         break;
851       }
852     }
853   }
854   /* compute local mirrors (if any) */
855   if (mirrors_found) {
856     IS       to,from;
857     PetscInt *local_indices,*global_indices;
858 
859     ierr = ISCreateStride(PETSC_COMM_SELF,graph->nvtxs,0,1,&to);CHKERRQ(ierr);
860     ierr = ISLocalToGlobalMappingApplyIS(graph->l2gmap,to,&from);CHKERRQ(ierr);
861     /* get arrays of local and global indices */
862     ierr = PetscMalloc1(graph->nvtxs,&local_indices);CHKERRQ(ierr);
863     ierr = ISGetIndices(to,(const PetscInt**)&is_indices);CHKERRQ(ierr);
864     ierr = PetscMemcpy(local_indices,is_indices,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
865     ierr = ISRestoreIndices(to,(const PetscInt**)&is_indices);CHKERRQ(ierr);
866     ierr = PetscMalloc1(graph->nvtxs,&global_indices);CHKERRQ(ierr);
867     ierr = ISGetIndices(from,(const PetscInt**)&is_indices);CHKERRQ(ierr);
868     ierr = PetscMemcpy(global_indices,is_indices,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
869     ierr = ISRestoreIndices(from,(const PetscInt**)&is_indices);CHKERRQ(ierr);
870     /* allocate space for mirrors */
871     ierr = PetscMalloc2(graph->nvtxs,&graph->mirrors,graph->nvtxs,&graph->mirrors_set);CHKERRQ(ierr);
872     ierr = PetscMemzero(graph->mirrors,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
873     graph->mirrors_set[0] = 0;
874 
875     k=0;
876     for (i=0;i<n_shared[0];i++) {
877       j=shared[0][i];
878       if (graph->count[j] > 1) {
879         graph->mirrors[j]++;
880         k++;
881       }
882     }
883     /* allocate space for set of mirrors */
884     ierr = PetscMalloc1(k,&graph->mirrors_set[0]);CHKERRQ(ierr);
885     for (i=1;i<graph->nvtxs;i++)
886       graph->mirrors_set[i]=graph->mirrors_set[i-1]+graph->mirrors[i-1];
887 
888     /* fill arrays */
889     ierr = PetscMemzero(graph->mirrors,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
890     for (j=0;j<n_shared[0];j++) {
891       i=shared[0][j];
892       if (graph->count[i] > 1)
893         graph->mirrors_set[i][graph->mirrors[i]++]=global_indices[i];
894     }
895     ierr = PetscSortIntWithArray(graph->nvtxs,global_indices,local_indices);CHKERRQ(ierr);
896     for (i=0;i<graph->nvtxs;i++) {
897       if (graph->mirrors[i] > 0) {
898         ierr = PetscFindInt(graph->mirrors_set[i][0],graph->nvtxs,global_indices,&k);CHKERRQ(ierr);
899         j = global_indices[k];
900         while ( k > 0 && global_indices[k-1] == j) k--;
901         for (j=0;j<graph->mirrors[i];j++) {
902           graph->mirrors_set[i][j]=local_indices[k+j];
903         }
904         ierr = PetscSortInt(graph->mirrors[i],graph->mirrors_set[i]);CHKERRQ(ierr);
905       }
906     }
907     ierr = PetscFree(local_indices);CHKERRQ(ierr);
908     ierr = PetscFree(global_indices);CHKERRQ(ierr);
909     ierr = ISDestroy(&to);CHKERRQ(ierr);
910     ierr = ISDestroy(&from);CHKERRQ(ierr);
911   }
912   ierr = PetscMemzero(graph->count,graph->nvtxs*sizeof(*graph->count));CHKERRQ(ierr);
913 
914   /* Count total number of neigh per node */
915   k = 0;
916   for (i=1;i<n_neigh;i++) {
917     k += n_shared[i];
918     for (j=0;j<n_shared[i];j++) {
919       graph->count[shared[i][j]] += 1;
920     }
921   }
922   /* Allocate space for storing the set of neighbours for each node */
923   if (graph->nvtxs) {
924     ierr = PetscMalloc1(k,&graph->neighbours_set[0]);CHKERRQ(ierr);
925   }
926   for (i=1;i<graph->nvtxs;i++) { /* dont count myself */
927     graph->neighbours_set[i]=graph->neighbours_set[i-1]+graph->count[i-1];
928   }
929   /* Get information for sharing subdomains */
930   ierr = PetscMemzero(graph->count,graph->nvtxs*sizeof(*graph->count));CHKERRQ(ierr);
931   for (i=1;i<n_neigh;i++) { /* dont count myself */
932     s = n_shared[i];
933     for (j=0;j<s;j++) {
934       k = shared[i][j];
935       graph->neighbours_set[k][graph->count[k]] = neigh[i];
936       graph->count[k] += 1;
937     }
938   }
939   /* sort set of sharing subdomains */
940   for (i=0;i<graph->nvtxs;i++) {
941     ierr = PetscSortRemoveDupsInt(&graph->count[i],graph->neighbours_set[i]);CHKERRQ(ierr);
942   }
943   /* free memory allocated by ISLocalToGlobalMappingGetInfo */
944   ierr = ISLocalToGlobalMappingRestoreInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);CHKERRQ(ierr);
945 
946   /*
947      Get info for dofs splitting
948      User can specify just a subset; an additional field is considered as a complementary field
949   */
950   for (i=0;i<graph->nvtxs;i++) graph->which_dof[i] = n_ISForDofs; /* by default a dof belongs to the complement set */
951   for (i=0;i<n_ISForDofs;i++) {
952     ierr = ISGetLocalSize(ISForDofs[i],&is_size);CHKERRQ(ierr);
953     ierr = ISGetIndices(ISForDofs[i],(const PetscInt**)&is_indices);CHKERRQ(ierr);
954     for (j=0;j<is_size;j++) {
955       if (is_indices[j] > -1 && is_indices[j] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */
956         graph->which_dof[is_indices[j]] = i;
957       }
958     }
959     ierr = ISRestoreIndices(ISForDofs[i],(const PetscInt**)&is_indices);CHKERRQ(ierr);
960   }
961 
962   /* Take into account Neumann nodes */
963   if (neumann_is) {
964     ierr = ISGetLocalSize(neumann_is,&is_size);CHKERRQ(ierr);
965     ierr = ISGetIndices(neumann_is,(const PetscInt**)&is_indices);CHKERRQ(ierr);
966     for (i=0;i<is_size;i++) {
967       if (is_indices[i] > -1 && is_indices[i] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */
968         graph->special_dof[is_indices[i]] = PCBDDCGRAPH_NEUMANN_MARK;
969       }
970     }
971     ierr = ISRestoreIndices(neumann_is,(const PetscInt**)&is_indices);CHKERRQ(ierr);
972   }
973   /* Take into account Dirichlet nodes (they overwrite any neumann boundary mark previously set) */
974   if (dirichlet_is) {
975     ierr = ISGetLocalSize(dirichlet_is,&is_size);CHKERRQ(ierr);
976     ierr = ISGetIndices(dirichlet_is,(const PetscInt**)&is_indices);CHKERRQ(ierr);
977     for (i=0;i<is_size;i++){
978       if (is_indices[i] > -1 && is_indices[i] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */
979         if (commsize > graph->commsizelimit) { /* dirichlet nodes treated as internal */
980           ierr = PetscBTSet(graph->touched,is_indices[i]);CHKERRQ(ierr);
981           graph->subset[is_indices[i]] = 0;
982         }
983         graph->special_dof[is_indices[i]] = PCBDDCGRAPH_DIRICHLET_MARK;
984       }
985     }
986     ierr = ISRestoreIndices(dirichlet_is,(const PetscInt**)&is_indices);CHKERRQ(ierr);
987   }
988   /* mark local periodic nodes (if any) and adapt CSR graph (if any) */
989   if (graph->mirrors) {
990     for (i=0;i<graph->nvtxs;i++)
991       if (graph->mirrors[i])
992         graph->special_dof[i] = PCBDDCGRAPH_LOCAL_PERIODIC_MARK;
993 
994     if (graph->xadj) {
995       PetscInt *new_xadj,*new_adjncy;
996       /* sort CSR graph */
997       for (i=0;i<graph->nvtxs;i++)
998         ierr = PetscSortInt(graph->xadj[i+1]-graph->xadj[i],&graph->adjncy[graph->xadj[i]]);CHKERRQ(ierr);
999 
1000       /* adapt local CSR graph in case of local periodicity */
1001       k = 0;
1002       for (i=0;i<graph->nvtxs;i++)
1003         for (j=graph->xadj[i];j<graph->xadj[i+1];j++)
1004           k += graph->mirrors[graph->adjncy[j]];
1005 
1006       ierr = PetscMalloc1(graph->nvtxs+1,&new_xadj);CHKERRQ(ierr);
1007       ierr = PetscMalloc1(k+graph->xadj[graph->nvtxs],&new_adjncy);CHKERRQ(ierr);
1008       new_xadj[0] = 0;
1009       for (i=0;i<graph->nvtxs;i++) {
1010         k = graph->xadj[i+1]-graph->xadj[i];
1011         ierr = PetscMemcpy(&new_adjncy[new_xadj[i]],&graph->adjncy[graph->xadj[i]],k*sizeof(PetscInt));CHKERRQ(ierr);
1012         new_xadj[i+1] = new_xadj[i]+k;
1013         for (j=graph->xadj[i];j<graph->xadj[i+1];j++) {
1014           k = graph->mirrors[graph->adjncy[j]];
1015           ierr = PetscMemcpy(&new_adjncy[new_xadj[i+1]],graph->mirrors_set[graph->adjncy[j]],k*sizeof(PetscInt));CHKERRQ(ierr);
1016           new_xadj[i+1] += k;
1017         }
1018         k = new_xadj[i+1]-new_xadj[i];
1019         ierr = PetscSortRemoveDupsInt(&k,&new_adjncy[new_xadj[i]]);CHKERRQ(ierr);
1020         new_xadj[i+1] = new_xadj[i]+k;
1021       }
1022       /* set new CSR into graph */
1023       ierr = PetscFree(graph->xadj);CHKERRQ(ierr);
1024       ierr = PetscFree(graph->adjncy);CHKERRQ(ierr);
1025       graph->xadj = new_xadj;
1026       graph->adjncy = new_adjncy;
1027     }
1028   }
1029 
1030   /* mark special nodes (if any) -> each will become a single node equivalence class */
1031   if (custom_primal_vertices) {
1032     ierr = ISGetLocalSize(custom_primal_vertices,&is_size);CHKERRQ(ierr);
1033     ierr = ISGetIndices(custom_primal_vertices,(const PetscInt**)&is_indices);CHKERRQ(ierr);
1034     for (i=0,j=0;i<is_size;i++){
1035       if (is_indices[i] > -1 && is_indices[i] < graph->nvtxs  && graph->special_dof[is_indices[i]] != PCBDDCGRAPH_DIRICHLET_MARK) { /* out of bounds indices (if any) are skipped */
1036         graph->special_dof[is_indices[i]] = PCBDDCGRAPH_SPECIAL_MARK-j;
1037         j++;
1038       }
1039     }
1040     ierr = ISRestoreIndices(custom_primal_vertices,(const PetscInt**)&is_indices);CHKERRQ(ierr);
1041   }
1042 
1043   /* mark interior nodes (if commsize > graph->commsizelimit) as touched and belonging to partition number 0 */
1044   if (commsize > graph->commsizelimit) {
1045     for (i=0;i<graph->nvtxs;i++) {
1046       if (!graph->count[i]) {
1047         ierr = PetscBTSet(graph->touched,i);CHKERRQ(ierr);
1048         graph->subset[i] = 0;
1049       }
1050     }
1051   }
1052 
1053   /* init graph structure and compute default subsets */
1054   nodes_touched = 0;
1055   for (i=0;i<graph->nvtxs;i++) {
1056     if (PetscBTLookup(graph->touched,i)) {
1057       nodes_touched++;
1058     }
1059   }
1060   i = 0;
1061   graph->ncc = 0;
1062   total_counts = 0;
1063 
1064   /* allocated space for queues */
1065   if (commsize == graph->commsizelimit) {
1066     ierr = PetscMalloc2(graph->nvtxs+1,&graph->cptr,graph->nvtxs,&graph->queue);CHKERRQ(ierr);
1067   } else {
1068     PetscInt nused = graph->nvtxs - nodes_touched;
1069     ierr = PetscMalloc2(nused+1,&graph->cptr,nused,&graph->queue);CHKERRQ(ierr);
1070   }
1071 
1072   while (nodes_touched<graph->nvtxs) {
1073     /*  find first untouched node in local ordering */
1074     while (PetscBTLookup(graph->touched,i)) i++;
1075     ierr = PetscBTSet(graph->touched,i);CHKERRQ(ierr);
1076     graph->subset[i] = graph->ncc+1;
1077     graph->cptr[graph->ncc] = total_counts;
1078     graph->queue[total_counts] = i;
1079     total_counts++;
1080     nodes_touched++;
1081     /* now find all other nodes having the same set of sharing subdomains */
1082     for (j=i+1;j<graph->nvtxs;j++) {
1083       /* check for same number of sharing subdomains, dof number and same special mark */
1084       if (!PetscBTLookup(graph->touched,j) && graph->count[i] == graph->count[j] && graph->which_dof[i] == graph->which_dof[j] && graph->special_dof[i] == graph->special_dof[j]) {
1085         /* check for same set of sharing subdomains */
1086         same_set = PETSC_TRUE;
1087         for (k=0;k<graph->count[j];k++){
1088           if (graph->neighbours_set[i][k] != graph->neighbours_set[j][k]) {
1089             same_set = PETSC_FALSE;
1090           }
1091         }
1092         /* I have found a friend of mine */
1093         if (same_set) {
1094           ierr = PetscBTSet(graph->touched,j);CHKERRQ(ierr);
1095           graph->subset[j] = graph->ncc+1;
1096           nodes_touched++;
1097           graph->queue[total_counts] = j;
1098           total_counts++;
1099         }
1100       }
1101     }
1102     graph->ncc++;
1103   }
1104   /* set default number of subsets (at this point no info on csr and/or local_subs has been taken into account, so n_subsets = ncc */
1105   graph->n_subsets = graph->ncc;
1106   ierr = PetscMalloc1(graph->n_subsets,&graph->subset_ncc);CHKERRQ(ierr);
1107   for (i=0;i<graph->n_subsets;i++) {
1108     graph->subset_ncc[i] = 1;
1109   }
1110   /* final pointer */
1111   graph->cptr[graph->ncc] = total_counts;
1112 
1113   /* For consistency reasons (among neighbours), I need to sort (by global ordering) each connected component */
1114   /* Get a reference node (min index in global ordering) for each subset for tagging messages */
1115   ierr = PetscMalloc1(graph->ncc,&graph->subset_ref_node);CHKERRQ(ierr);
1116   ierr = PetscMalloc1(graph->cptr[graph->ncc],&queue_global);CHKERRQ(ierr);
1117   ierr = ISLocalToGlobalMappingApply(graph->l2gmap,graph->cptr[graph->ncc],graph->queue,queue_global);CHKERRQ(ierr);
1118   for (j=0;j<graph->ncc;j++) {
1119     ierr = PetscSortIntWithArray(graph->cptr[j+1]-graph->cptr[j],&queue_global[graph->cptr[j]],&graph->queue[graph->cptr[j]]);CHKERRQ(ierr);
1120     graph->subset_ref_node[j] = graph->queue[graph->cptr[j]];
1121   }
1122   ierr = PetscFree(queue_global);CHKERRQ(ierr);
1123   graph->queue_sorted = PETSC_TRUE;
1124 
1125   /* save information on subsets (needed when analyzing the connected components) */
1126   if (graph->ncc) {
1127     ierr = PetscMalloc2(graph->ncc,&graph->subset_size,graph->ncc,&graph->subset_idxs);CHKERRQ(ierr);
1128     ierr = PetscMalloc1(graph->cptr[graph->ncc],&graph->subset_idxs[0]);CHKERRQ(ierr);
1129     ierr = PetscMemzero(graph->subset_idxs[0],graph->cptr[graph->ncc]*sizeof(PetscInt));CHKERRQ(ierr);
1130     for (j=1;j<graph->ncc;j++) {
1131       graph->subset_size[j-1] = graph->cptr[j] - graph->cptr[j-1];
1132       graph->subset_idxs[j] = graph->subset_idxs[j-1] + graph->subset_size[j-1];
1133     }
1134     graph->subset_size[graph->ncc-1] = graph->cptr[graph->ncc] - graph->cptr[graph->ncc-1];
1135     ierr = PetscMemcpy(graph->subset_idxs[0],graph->queue,graph->cptr[graph->ncc]*sizeof(PetscInt));CHKERRQ(ierr);
1136   }
1137 
1138   /* renumber reference nodes */
1139   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)(graph->l2gmap)),graph->ncc,graph->subset_ref_node,PETSC_COPY_VALUES,&subset_n);CHKERRQ(ierr);
1140   ierr = ISLocalToGlobalMappingApplyIS(graph->l2gmap,subset_n,&subset);CHKERRQ(ierr);
1141   ierr = ISDestroy(&subset_n);CHKERRQ(ierr);
1142   ierr = ISRenumber(subset,NULL,NULL,&subset_n);CHKERRQ(ierr);
1143   ierr = ISDestroy(&subset);CHKERRQ(ierr);
1144   ierr = ISGetLocalSize(subset_n,&k);CHKERRQ(ierr);
1145   if (k != graph->ncc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid size of new subset! %D != %D",k,graph->ncc);
1146   ierr = ISGetIndices(subset_n,&is_indices);CHKERRQ(ierr);
1147   ierr = PetscMemcpy(graph->subset_ref_node,is_indices,graph->ncc*sizeof(PetscInt));CHKERRQ(ierr);
1148   ierr = ISRestoreIndices(subset_n,&is_indices);CHKERRQ(ierr);
1149   ierr = ISDestroy(&subset_n);CHKERRQ(ierr);
1150 
1151   /* free workspace */
1152   graph->setupcalled = PETSC_TRUE;
1153   PetscFunctionReturn(0);
1154 }
1155 
1156 PetscErrorCode PCBDDCGraphResetCoords(PCBDDCGraph graph)
1157 {
1158   PetscErrorCode ierr;
1159 
1160   PetscFunctionBegin;
1161   if (!graph) PetscFunctionReturn(0);
1162   ierr = PetscFree(graph->coords);CHKERRQ(ierr);
1163   graph->cdim  = 0;
1164   graph->cnloc = 0;
1165   graph->cloc  = PETSC_FALSE;
1166   PetscFunctionReturn(0);
1167 }
1168 
1169 PetscErrorCode PCBDDCGraphResetCSR(PCBDDCGraph graph)
1170 {
1171   PetscErrorCode ierr;
1172 
1173   PetscFunctionBegin;
1174   if (!graph) PetscFunctionReturn(0);
1175   if (graph->freecsr) {
1176     ierr = PetscFree(graph->xadj);CHKERRQ(ierr);
1177     ierr = PetscFree(graph->adjncy);CHKERRQ(ierr);
1178   } else {
1179     graph->xadj = NULL;
1180     graph->adjncy = NULL;
1181   }
1182   graph->freecsr = PETSC_FALSE;
1183   graph->nvtxs_csr = 0;
1184   PetscFunctionReturn(0);
1185 }
1186 
1187 PetscErrorCode PCBDDCGraphReset(PCBDDCGraph graph)
1188 {
1189   PetscErrorCode ierr;
1190 
1191   PetscFunctionBegin;
1192   if (!graph) PetscFunctionReturn(0);
1193   ierr = ISLocalToGlobalMappingDestroy(&graph->l2gmap);CHKERRQ(ierr);
1194   ierr = PetscFree(graph->subset_ncc);CHKERRQ(ierr);
1195   ierr = PetscFree(graph->subset_ref_node);CHKERRQ(ierr);
1196   if (graph->nvtxs) {
1197     ierr = PetscFree(graph->neighbours_set[0]);CHKERRQ(ierr);
1198   }
1199   ierr = PetscBTDestroy(&graph->touched);CHKERRQ(ierr);
1200   ierr = PetscFree5(graph->count,
1201                     graph->neighbours_set,
1202                     graph->subset,
1203                     graph->which_dof,
1204                     graph->special_dof);CHKERRQ(ierr);
1205   ierr = PetscFree2(graph->cptr,graph->queue);CHKERRQ(ierr);
1206   if (graph->mirrors) {
1207     ierr = PetscFree(graph->mirrors_set[0]);CHKERRQ(ierr);
1208   }
1209   ierr = PetscFree2(graph->mirrors,graph->mirrors_set);CHKERRQ(ierr);
1210   if (graph->subset_idxs) {
1211     ierr = PetscFree(graph->subset_idxs[0]);CHKERRQ(ierr);
1212   }
1213   ierr = PetscFree2(graph->subset_size,graph->subset_idxs);CHKERRQ(ierr);
1214   ierr = ISDestroy(&graph->dirdofs);CHKERRQ(ierr);
1215   ierr = ISDestroy(&graph->dirdofsB);CHKERRQ(ierr);
1216   if (graph->n_local_subs) {
1217     ierr = PetscFree(graph->local_subs);CHKERRQ(ierr);
1218   }
1219   graph->has_dirichlet       = PETSC_FALSE;
1220   graph->twodimset           = PETSC_FALSE;
1221   graph->twodim              = PETSC_FALSE;
1222   graph->nvtxs               = 0;
1223   graph->nvtxs_global        = 0;
1224   graph->n_subsets           = 0;
1225   graph->custom_minimal_size = 1;
1226   graph->n_local_subs        = 0;
1227   graph->maxcount            = PETSC_MAX_INT;
1228   graph->setupcalled         = PETSC_FALSE;
1229   PetscFunctionReturn(0);
1230 }
1231 
1232 PetscErrorCode PCBDDCGraphInit(PCBDDCGraph graph, ISLocalToGlobalMapping l2gmap, PetscInt N, PetscInt maxcount)
1233 {
1234   PetscInt       n;
1235   PetscErrorCode ierr;
1236 
1237   PetscFunctionBegin;
1238   PetscValidPointer(graph,1);
1239   PetscValidHeaderSpecific(l2gmap,IS_LTOGM_CLASSID,2);
1240   PetscValidLogicalCollectiveInt(l2gmap,N,3);
1241   PetscValidLogicalCollectiveInt(l2gmap,maxcount,4);
1242   /* raise an error if already allocated */
1243   if (graph->nvtxs_global) SETERRQ(PetscObjectComm((PetscObject)l2gmap),PETSC_ERR_PLIB,"BDDCGraph already initialized");
1244   /* set number of vertices */
1245   ierr = PetscObjectReference((PetscObject)l2gmap);CHKERRQ(ierr);
1246   graph->l2gmap = l2gmap;
1247   ierr = ISLocalToGlobalMappingGetSize(l2gmap,&n);CHKERRQ(ierr);
1248   graph->nvtxs = n;
1249   graph->nvtxs_global = N;
1250   /* allocate used space */
1251   ierr = PetscBTCreate(graph->nvtxs,&graph->touched);CHKERRQ(ierr);
1252   ierr = PetscMalloc5(graph->nvtxs,&graph->count,
1253                       graph->nvtxs,&graph->neighbours_set,
1254                       graph->nvtxs,&graph->subset,
1255                       graph->nvtxs,&graph->which_dof,
1256                       graph->nvtxs,&graph->special_dof);CHKERRQ(ierr);
1257   /* zeroes memory */
1258   ierr = PetscMemzero(graph->count,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
1259   ierr = PetscMemzero(graph->subset,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
1260   /* use -1 as a default value for which_dof array */
1261   for (n=0;n<graph->nvtxs;n++) graph->which_dof[n] = -1;
1262   ierr = PetscMemzero(graph->special_dof,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
1263   /* zeroes first pointer to neighbour set */
1264   if (graph->nvtxs) {
1265     graph->neighbours_set[0] = 0;
1266   }
1267   /* zeroes workspace for values of ncc */
1268   graph->subset_ncc = 0;
1269   graph->subset_ref_node = 0;
1270   /* maxcount for cc */
1271   graph->maxcount = maxcount;
1272   PetscFunctionReturn(0);
1273 }
1274 
1275 PetscErrorCode PCBDDCGraphDestroy(PCBDDCGraph* graph)
1276 {
1277   PetscErrorCode ierr;
1278 
1279   PetscFunctionBegin;
1280   ierr = PCBDDCGraphResetCSR(*graph);CHKERRQ(ierr);
1281   ierr = PCBDDCGraphResetCoords(*graph);CHKERRQ(ierr);
1282   ierr = PCBDDCGraphReset(*graph);CHKERRQ(ierr);
1283   ierr = PetscFree(*graph);CHKERRQ(ierr);
1284   PetscFunctionReturn(0);
1285 }
1286 
1287 PetscErrorCode PCBDDCGraphCreate(PCBDDCGraph *graph)
1288 {
1289   PCBDDCGraph    new_graph;
1290   PetscErrorCode ierr;
1291 
1292   PetscFunctionBegin;
1293   ierr = PetscNew(&new_graph);CHKERRQ(ierr);
1294   new_graph->custom_minimal_size = 1;
1295   new_graph->commsizelimit = 1;
1296   *graph = new_graph;
1297   PetscFunctionReturn(0);
1298 }
1299