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