xref: /petsc/src/ksp/pc/impls/bddc/bddcgraph.c (revision dc424cfae46ffd055acbb99fbc61fc85fc92f9ad)
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   PetscErrorCode ierr;
295 
296   PetscFunctionBegin;
297   /* compute connected components locally */
298   ierr = PetscObjectGetComm((PetscObject)(graph->l2gmap),&interface_comm);CHKERRQ(ierr);
299   ierr = PCBDDCGraphComputeConnectedComponentsLocal(graph);CHKERRQ(ierr);
300   /* check consistency of connected components among neighbouring subdomains -> it adapt them in case it is needed */
301   ierr = MPI_Comm_size(interface_comm,&size);CHKERRQ(ierr);
302   adapt_interface_reduced = PETSC_FALSE;
303   if (size > 1) {
304     PetscInt i;
305     PetscBool adapt_interface = PETSC_FALSE;
306     for (i=0;i<graph->n_subsets;i++) {
307       /* We are not sure that on a given subset of the local interface,
308          with two connected components, the latters be the same among sharing subdomains */
309       if (graph->subset_ncc[i] > 1) {
310         adapt_interface = PETSC_TRUE;
311         break;
312       }
313     }
314     ierr = MPIU_Allreduce(&adapt_interface,&adapt_interface_reduced,1,MPIU_BOOL,MPI_LOR,interface_comm);CHKERRQ(ierr);
315   }
316 
317   if (graph->n_subsets && adapt_interface_reduced) {
318     PetscBT     subset_cc_adapt;
319     MPI_Request *send_requests,*recv_requests;
320     PetscInt    *send_buffer,*recv_buffer;
321     PetscInt    sum_requests,start_of_recv,start_of_send;
322     PetscInt    *cum_recv_counts;
323     PetscInt    *labels;
324     PetscInt    ncc,cum_queue,mss,mns,j,k,s;
325     PetscInt    **refine_buffer=NULL,*private_labels = NULL;
326 
327     ierr = PetscMalloc1(graph->nvtxs,&labels);CHKERRQ(ierr);
328     ierr = PetscMemzero(labels,graph->nvtxs*sizeof(*labels));CHKERRQ(ierr);
329     for (i=0;i<graph->ncc;i++)
330       for (j=graph->cptr[i];j<graph->cptr[i+1];j++)
331         labels[graph->queue[j]] = i;
332 
333     /* allocate some space */
334     ierr = PetscMalloc1(graph->n_subsets+1,&cum_recv_counts);CHKERRQ(ierr);
335     ierr = PetscMemzero(cum_recv_counts,(graph->n_subsets+1)*sizeof(*cum_recv_counts));CHKERRQ(ierr);
336 
337     /* first count how many neighbours per connected component I will receive from */
338     cum_recv_counts[0] = 0;
339     for (i=0;i<graph->n_subsets;i++) cum_recv_counts[i+1] = cum_recv_counts[i]+graph->count[graph->subset_idxs[i][0]];
340     ierr = PetscMalloc1(cum_recv_counts[graph->n_subsets],&recv_buffer);CHKERRQ(ierr);
341     ierr = PetscMalloc2(cum_recv_counts[graph->n_subsets],&send_requests,cum_recv_counts[graph->n_subsets],&recv_requests);CHKERRQ(ierr);
342     for (i=0;i<cum_recv_counts[graph->n_subsets];i++) {
343       send_requests[i] = MPI_REQUEST_NULL;
344       recv_requests[i] = MPI_REQUEST_NULL;
345     }
346 
347     /* exchange with my neighbours the number of my connected components on the subset of interface */
348     sum_requests = 0;
349     for (i=0;i<graph->n_subsets;i++) {
350       PetscMPIInt neigh,tag;
351       PetscInt    count,*neighs;
352 
353       count = graph->count[graph->subset_idxs[i][0]];
354       neighs = graph->neighbours_set[graph->subset_idxs[i][0]];
355       ierr = PetscMPIIntCast(2*graph->subset_ref_node[i],&tag);CHKERRQ(ierr);
356       for (k=0;k<count;k++) {
357         ierr = PetscMPIIntCast(neighs[k],&neigh);CHKERRQ(ierr);
358         ierr = MPI_Isend(&graph->subset_ncc[i],1,MPIU_INT,neigh,tag,interface_comm,&send_requests[sum_requests]);CHKERRQ(ierr);
359         ierr = MPI_Irecv(&recv_buffer[sum_requests],1,MPIU_INT,neigh,tag,interface_comm,&recv_requests[sum_requests]);CHKERRQ(ierr);
360         sum_requests++;
361       }
362     }
363     ierr = MPI_Waitall(sum_requests,recv_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr);
364     ierr = MPI_Waitall(sum_requests,send_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr);
365 
366     /* determine the subsets I have to adapt (those having more than 1 cc) */
367     ierr = PetscBTCreate(graph->n_subsets,&subset_cc_adapt);CHKERRQ(ierr);
368     ierr = PetscBTMemzero(graph->n_subsets,subset_cc_adapt);CHKERRQ(ierr);
369     for (i=0;i<graph->n_subsets;i++) {
370       if (graph->subset_ncc[i] > 1) {
371         ierr = PetscBTSet(subset_cc_adapt,i);CHKERRQ(ierr);
372         continue;
373       }
374       for (j=cum_recv_counts[i];j<cum_recv_counts[i+1];j++){
375          if (recv_buffer[j] > 1) {
376           ierr = PetscBTSet(subset_cc_adapt,i);CHKERRQ(ierr);
377           break;
378         }
379       }
380     }
381     ierr = PetscFree(recv_buffer);CHKERRQ(ierr);
382 
383     /* determine send/recv buffers sizes */
384     j = 0;
385     mss = 0;
386     for (i=0;i<graph->n_subsets;i++) {
387       if (PetscBTLookup(subset_cc_adapt,i)) {
388         j += graph->subset_size[i];
389         mss = PetscMax(graph->subset_size[i],mss);
390       }
391     }
392     k = 0;
393     mns = 0;
394     for (i=0;i<graph->n_subsets;i++) {
395       if (PetscBTLookup(subset_cc_adapt,i)) {
396         k += (cum_recv_counts[i+1]-cum_recv_counts[i])*graph->subset_size[i];
397         mns = PetscMax(cum_recv_counts[i+1]-cum_recv_counts[i],mns);
398       }
399     }
400     ierr = PetscMalloc2(j,&send_buffer,k,&recv_buffer);CHKERRQ(ierr);
401 
402     /* fill send buffer (order matters: subset_idxs ordered by global ordering) */
403     j = 0;
404     for (i=0;i<graph->n_subsets;i++)
405       if (PetscBTLookup(subset_cc_adapt,i))
406         for (k=0;k<graph->subset_size[i];k++)
407           send_buffer[j++] = labels[graph->subset_idxs[i][k]];
408 
409     /* now exchange the data */
410     start_of_recv = 0;
411     start_of_send = 0;
412     sum_requests = 0;
413     for (i=0;i<graph->n_subsets;i++) {
414       if (PetscBTLookup(subset_cc_adapt,i)) {
415         PetscMPIInt neigh,tag;
416         PetscInt    size_of_send = graph->subset_size[i];
417 
418         j = graph->subset_idxs[i][0];
419         ierr = PetscMPIIntCast(2*graph->subset_ref_node[i]+1,&tag);CHKERRQ(ierr);
420         for (k=0;k<graph->count[j];k++) {
421           ierr = PetscMPIIntCast(graph->neighbours_set[j][k],&neigh);CHKERRQ(ierr);
422           ierr = MPI_Isend(&send_buffer[start_of_send],size_of_send,MPIU_INT,neigh,tag,interface_comm,&send_requests[sum_requests]);CHKERRQ(ierr);
423           ierr = MPI_Irecv(&recv_buffer[start_of_recv],size_of_send,MPIU_INT,neigh,tag,interface_comm,&recv_requests[sum_requests]);CHKERRQ(ierr);
424           start_of_recv += size_of_send;
425           sum_requests++;
426         }
427         start_of_send += size_of_send;
428       }
429     }
430     ierr = MPI_Waitall(sum_requests,recv_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr);
431 
432     /* refine connected components */
433     start_of_recv = 0;
434     /* allocate some temporary space */
435     if (mss) {
436       ierr = PetscMalloc1(mss,&refine_buffer);CHKERRQ(ierr);
437       ierr = PetscMalloc2(mss*(mns+1),&refine_buffer[0],mss,&private_labels);CHKERRQ(ierr);
438     }
439     ncc = 0;
440     cum_queue = 0;
441     graph->cptr[0] = 0;
442     for (i=0;i<graph->n_subsets;i++) {
443       if (PetscBTLookup(subset_cc_adapt,i)) {
444         PetscInt subset_counter = 0;
445         PetscInt sharingprocs = cum_recv_counts[i+1]-cum_recv_counts[i]+1; /* count myself */
446         PetscInt buffer_size = graph->subset_size[i];
447 
448         /* compute pointers */
449         for (j=1;j<buffer_size;j++) refine_buffer[j] = refine_buffer[j-1] + sharingprocs;
450         /* analyze contributions from subdomains that share the i-th subset
451            The stricture of refine_buffer is suitable to find intersections of ccs among sharingprocs.
452            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)
453            sharing procs connected components:
454              neigh 0: [0 1 4], [2 3], labels [4,7]  (2 connected components)
455              neigh 1: [0 1], [2 3 4], labels [3 2]  (2 connected components)
456              neigh 2: [0 4], [1], [2 3], labels [1 5 6] (3 connected components)
457            refine_buffer will be filled as:
458              [ 4, 3, 1;
459                4, 2, 1;
460                7, 2, 6;
461                4, 3, 5;
462                7, 2, 6; ];
463            The connected components in local ordering are [0], [1], [2 3], [4] */
464         /* fill temp_buffer */
465         for (k=0;k<buffer_size;k++) refine_buffer[k][0] = labels[graph->subset_idxs[i][k]];
466         for (j=0;j<sharingprocs-1;j++) {
467           for (k=0;k<buffer_size;k++) refine_buffer[k][j+1] = recv_buffer[start_of_recv+k];
468           start_of_recv += buffer_size;
469         }
470         ierr = PetscMemzero(private_labels,buffer_size*sizeof(PetscInt));CHKERRQ(ierr);
471         for (j=0;j<buffer_size;j++) {
472           if (!private_labels[j]) { /* found a new cc  */
473             PetscBool same_set;
474 
475             graph->cptr[ncc] = cum_queue;
476             ncc++;
477             subset_counter++;
478             private_labels[j] = subset_counter;
479             graph->queue[cum_queue++] = graph->subset_idxs[i][j];
480             for (k=j+1;k<buffer_size;k++) { /* check for other nodes in new cc */
481               same_set = PETSC_TRUE;
482               for (s=0;s<sharingprocs;s++) {
483                 if (refine_buffer[j][s] != refine_buffer[k][s]) {
484                   same_set = PETSC_FALSE;
485                   break;
486                 }
487               }
488               if (same_set) {
489                 private_labels[k] = subset_counter;
490                 graph->queue[cum_queue++] = graph->subset_idxs[i][k];
491               }
492             }
493           }
494         }
495         graph->cptr[ncc] = cum_queue;
496         graph->subset_ncc[i] = subset_counter;
497         graph->queue_sorted = PETSC_FALSE;
498       } else { /* this subset does not need to be adapted */
499         ierr = PetscMemcpy(graph->queue+cum_queue,graph->subset_idxs[i],graph->subset_size[i]*sizeof(PetscInt));CHKERRQ(ierr);
500         ncc++;
501         cum_queue += graph->subset_size[i];
502         graph->cptr[ncc] = cum_queue;
503       }
504     }
505     graph->cptr[ncc] = cum_queue;
506     graph->ncc = ncc;
507     if (mss) {
508       ierr = PetscFree2(refine_buffer[0],private_labels);CHKERRQ(ierr);
509       ierr = PetscFree(refine_buffer);CHKERRQ(ierr);
510     }
511     ierr = PetscFree(labels);CHKERRQ(ierr);
512     ierr = MPI_Waitall(sum_requests,send_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr);
513     ierr = PetscFree2(send_requests,recv_requests);CHKERRQ(ierr);
514     ierr = PetscFree2(send_buffer,recv_buffer);CHKERRQ(ierr);
515     ierr = PetscFree(cum_recv_counts);CHKERRQ(ierr);
516     ierr = PetscBTDestroy(&subset_cc_adapt);CHKERRQ(ierr);
517   }
518 
519   /* Determine if we are in 2D or 3D */
520   if (!graph->twodimset) {
521     PetscBool twodim = PETSC_TRUE;
522     for (i=0;i<graph->ncc;i++) {
523       PetscInt repdof = graph->queue[graph->cptr[i]];
524       PetscInt ccsize = graph->cptr[i+1]-graph->cptr[i];
525       if (graph->count[repdof] > 1 && ccsize > graph->custom_minimal_size) {
526         twodim = PETSC_FALSE;
527         break;
528       }
529     }
530     ierr = MPIU_Allreduce(&twodim,&graph->twodim,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)graph->l2gmap));CHKERRQ(ierr);
531     graph->twodimset = PETSC_TRUE;
532   }
533   PetscFunctionReturn(0);
534 }
535 
536 
537 PETSC_STATIC_INLINE PetscErrorCode PCBDDCGraphComputeCC_Private(PCBDDCGraph graph,PetscInt pid,PetscInt* queue_tip,PetscInt n_prev,PetscInt* n_added)
538 {
539   PetscInt       i,j,n;
540   PetscInt       *xadj = graph->xadj,*adjncy = graph->adjncy;
541   PetscBT        touched = graph->touched;
542   PetscBool      havecsr = (PetscBool)(!!xadj);
543   PetscBool      havesubs = (PetscBool)(!!graph->n_local_subs);
544   PetscErrorCode ierr;
545 
546   PetscFunctionBegin;
547   n = 0;
548   if (havecsr && !havesubs) {
549     for (i=-n_prev;i<0;i++) {
550       PetscInt start_dof = queue_tip[i];
551       /* we assume that if a dof has a size 1 adjacency list and the corresponding entry is negative, it is connected to all dofs */
552       if (xadj[start_dof+1]-xadj[start_dof] == 1 && adjncy[xadj[start_dof]] < 0) {
553         for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */
554           PetscInt dof = graph->subset_idxs[pid-1][j];
555           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid) {
556             ierr = PetscBTSet(touched,dof);CHKERRQ(ierr);
557             queue_tip[n] = dof;
558             n++;
559           }
560         }
561       } else {
562         for (j=xadj[start_dof];j<xadj[start_dof+1];j++) {
563           PetscInt dof = adjncy[j];
564           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid) {
565             ierr = PetscBTSet(touched,dof);CHKERRQ(ierr);
566             queue_tip[n] = dof;
567             n++;
568           }
569         }
570       }
571     }
572   } else if (havecsr && havesubs) {
573     PetscInt sid = graph->local_subs[queue_tip[-n_prev]];
574     for (i=-n_prev;i<0;i++) {
575       PetscInt start_dof = queue_tip[i];
576       /* 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 */
577       if (xadj[start_dof+1]-xadj[start_dof] == 1 && adjncy[xadj[start_dof]] < 0) {
578         for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */
579           PetscInt dof = graph->subset_idxs[pid-1][j];
580           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) {
581             ierr = PetscBTSet(touched,dof);CHKERRQ(ierr);
582             queue_tip[n] = dof;
583             n++;
584           }
585         }
586       } else {
587         for (j=xadj[start_dof];j<xadj[start_dof+1];j++) {
588           PetscInt dof = adjncy[j];
589           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) {
590             ierr = PetscBTSet(touched,dof);CHKERRQ(ierr);
591             queue_tip[n] = dof;
592             n++;
593           }
594         }
595       }
596     }
597   } else { /* sub info only */
598     PetscInt sid = graph->local_subs[queue_tip[-n_prev]];
599     for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */
600       PetscInt dof = graph->subset_idxs[pid-1][j];
601       if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) {
602         ierr = PetscBTSet(touched,dof);CHKERRQ(ierr);
603         queue_tip[n] = dof;
604         n++;
605       }
606     }
607   }
608   *n_added = n;
609   PetscFunctionReturn(0);
610 }
611 
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 PetscErrorCode PCBDDCGraphSetUp(PCBDDCGraph graph, PetscInt custom_minimal_size, IS neumann_is, IS dirichlet_is, PetscInt n_ISForDofs, IS ISForDofs[], IS custom_primal_vertices)
676 {
677   IS             subset,subset_n;
678   MPI_Comm       comm;
679   const PetscInt *is_indices;
680   PetscInt       n_neigh,*neigh,*n_shared,**shared,*queue_global;
681   PetscInt       i,j,k,s,total_counts,nodes_touched,is_size;
682   PetscMPIInt    commsize;
683   PetscBool      same_set,mirrors_found;
684   PetscErrorCode ierr;
685 
686   PetscFunctionBegin;
687   PetscValidLogicalCollectiveInt(graph->l2gmap,custom_minimal_size,2);
688   if (neumann_is) {
689     PetscValidHeaderSpecific(neumann_is,IS_CLASSID,3);
690     PetscCheckSameComm(graph->l2gmap,1,neumann_is,3);
691   }
692   graph->has_dirichlet = PETSC_FALSE;
693   if (dirichlet_is) {
694     PetscValidHeaderSpecific(dirichlet_is,IS_CLASSID,4);
695     PetscCheckSameComm(graph->l2gmap,1,dirichlet_is,4);
696     graph->has_dirichlet = PETSC_TRUE;
697   }
698   PetscValidLogicalCollectiveInt(graph->l2gmap,n_ISForDofs,5);
699   for (i=0;i<n_ISForDofs;i++) {
700     PetscValidHeaderSpecific(ISForDofs[i],IS_CLASSID,6);
701     PetscCheckSameComm(graph->l2gmap,1,ISForDofs[i],6);
702   }
703   if (custom_primal_vertices) {
704     PetscValidHeaderSpecific(custom_primal_vertices,IS_CLASSID,6);
705     PetscCheckSameComm(graph->l2gmap,1,custom_primal_vertices,7);
706   }
707   ierr = PetscObjectGetComm((PetscObject)(graph->l2gmap),&comm);CHKERRQ(ierr);
708   ierr = MPI_Comm_size(comm,&commsize);CHKERRQ(ierr);
709 
710   /* custom_minimal_size */
711   graph->custom_minimal_size = custom_minimal_size;
712   /* get info l2gmap and allocate work vectors  */
713   ierr = ISLocalToGlobalMappingGetInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);CHKERRQ(ierr);
714   /* check if we have any local periodic nodes (periodic BCs) */
715   mirrors_found = PETSC_FALSE;
716   if (graph->nvtxs && n_neigh) {
717     for (i=0; i<n_shared[0]; i++) graph->count[shared[0][i]] += 1;
718     for (i=0; i<n_shared[0]; i++) {
719       if (graph->count[shared[0][i]] > 1) {
720         mirrors_found = PETSC_TRUE;
721         break;
722       }
723     }
724   }
725   /* compute local mirrors (if any) */
726   if (mirrors_found) {
727     IS       to,from;
728     PetscInt *local_indices,*global_indices;
729 
730     ierr = ISCreateStride(PETSC_COMM_SELF,graph->nvtxs,0,1,&to);CHKERRQ(ierr);
731     ierr = ISLocalToGlobalMappingApplyIS(graph->l2gmap,to,&from);CHKERRQ(ierr);
732     /* get arrays of local and global indices */
733     ierr = PetscMalloc1(graph->nvtxs,&local_indices);CHKERRQ(ierr);
734     ierr = ISGetIndices(to,(const PetscInt**)&is_indices);CHKERRQ(ierr);
735     ierr = PetscMemcpy(local_indices,is_indices,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
736     ierr = ISRestoreIndices(to,(const PetscInt**)&is_indices);CHKERRQ(ierr);
737     ierr = PetscMalloc1(graph->nvtxs,&global_indices);CHKERRQ(ierr);
738     ierr = ISGetIndices(from,(const PetscInt**)&is_indices);CHKERRQ(ierr);
739     ierr = PetscMemcpy(global_indices,is_indices,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
740     ierr = ISRestoreIndices(from,(const PetscInt**)&is_indices);CHKERRQ(ierr);
741     /* allocate space for mirrors */
742     ierr = PetscMalloc2(graph->nvtxs,&graph->mirrors,graph->nvtxs,&graph->mirrors_set);CHKERRQ(ierr);
743     ierr = PetscMemzero(graph->mirrors,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
744     graph->mirrors_set[0] = 0;
745 
746     k=0;
747     for (i=0;i<n_shared[0];i++) {
748       j=shared[0][i];
749       if (graph->count[j] > 1) {
750         graph->mirrors[j]++;
751         k++;
752       }
753     }
754     /* allocate space for set of mirrors */
755     ierr = PetscMalloc1(k,&graph->mirrors_set[0]);CHKERRQ(ierr);
756     for (i=1;i<graph->nvtxs;i++)
757       graph->mirrors_set[i]=graph->mirrors_set[i-1]+graph->mirrors[i-1];
758 
759     /* fill arrays */
760     ierr = PetscMemzero(graph->mirrors,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
761     for (j=0;j<n_shared[0];j++) {
762       i=shared[0][j];
763       if (graph->count[i] > 1)
764         graph->mirrors_set[i][graph->mirrors[i]++]=global_indices[i];
765     }
766     ierr = PetscSortIntWithArray(graph->nvtxs,global_indices,local_indices);CHKERRQ(ierr);
767     for (i=0;i<graph->nvtxs;i++) {
768       if (graph->mirrors[i] > 0) {
769         ierr = PetscFindInt(graph->mirrors_set[i][0],graph->nvtxs,global_indices,&k);CHKERRQ(ierr);
770         j = global_indices[k];
771         while ( k > 0 && global_indices[k-1] == j) k--;
772         for (j=0;j<graph->mirrors[i];j++) {
773           graph->mirrors_set[i][j]=local_indices[k+j];
774         }
775         ierr = PetscSortInt(graph->mirrors[i],graph->mirrors_set[i]);CHKERRQ(ierr);
776       }
777     }
778     ierr = PetscFree(local_indices);CHKERRQ(ierr);
779     ierr = PetscFree(global_indices);CHKERRQ(ierr);
780     ierr = ISDestroy(&to);CHKERRQ(ierr);
781     ierr = ISDestroy(&from);CHKERRQ(ierr);
782   }
783   ierr = PetscMemzero(graph->count,graph->nvtxs*sizeof(*graph->count));CHKERRQ(ierr);
784 
785   /* Count total number of neigh per node */
786   k = 0;
787   for (i=1;i<n_neigh;i++) {
788     k += n_shared[i];
789     for (j=0;j<n_shared[i];j++) {
790       graph->count[shared[i][j]] += 1;
791     }
792   }
793   /* Allocate space for storing the set of neighbours for each node */
794   if (graph->nvtxs) {
795     ierr = PetscMalloc1(k,&graph->neighbours_set[0]);CHKERRQ(ierr);
796   }
797   for (i=1;i<graph->nvtxs;i++) { /* dont count myself */
798     graph->neighbours_set[i]=graph->neighbours_set[i-1]+graph->count[i-1];
799   }
800   /* Get information for sharing subdomains */
801   ierr = PetscMemzero(graph->count,graph->nvtxs*sizeof(*graph->count));CHKERRQ(ierr);
802   for (i=1;i<n_neigh;i++) { /* dont count myself */
803     s = n_shared[i];
804     for (j=0;j<s;j++) {
805       k = shared[i][j];
806       graph->neighbours_set[k][graph->count[k]] = neigh[i];
807       graph->count[k] += 1;
808     }
809   }
810   /* sort set of sharing subdomains */
811   for (i=0;i<graph->nvtxs;i++) {
812     ierr = PetscSortRemoveDupsInt(&graph->count[i],graph->neighbours_set[i]);CHKERRQ(ierr);
813   }
814   /* free memory allocated by ISLocalToGlobalMappingGetInfo */
815   ierr = ISLocalToGlobalMappingRestoreInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);CHKERRQ(ierr);
816 
817   /*
818      Get info for dofs splitting
819      User can specify just a subset; an additional field is considered as a complementary field
820   */
821   for (i=0;i<graph->nvtxs;i++) graph->which_dof[i] = n_ISForDofs; /* by default a dof belongs to the complement set */
822   for (i=0;i<n_ISForDofs;i++) {
823     ierr = ISGetLocalSize(ISForDofs[i],&is_size);CHKERRQ(ierr);
824     ierr = ISGetIndices(ISForDofs[i],(const PetscInt**)&is_indices);CHKERRQ(ierr);
825     for (j=0;j<is_size;j++) {
826       if (is_indices[j] > -1 && is_indices[j] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */
827         graph->which_dof[is_indices[j]] = i;
828       }
829     }
830     ierr = ISRestoreIndices(ISForDofs[i],(const PetscInt**)&is_indices);CHKERRQ(ierr);
831   }
832 
833   /* Take into account Neumann nodes */
834   if (neumann_is) {
835     ierr = ISGetLocalSize(neumann_is,&is_size);CHKERRQ(ierr);
836     ierr = ISGetIndices(neumann_is,(const PetscInt**)&is_indices);CHKERRQ(ierr);
837     for (i=0;i<is_size;i++) {
838       if (is_indices[i] > -1 && is_indices[i] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */
839         graph->special_dof[is_indices[i]] = PCBDDCGRAPH_NEUMANN_MARK;
840       }
841     }
842     ierr = ISRestoreIndices(neumann_is,(const PetscInt**)&is_indices);CHKERRQ(ierr);
843   }
844   /* Take into account Dirichlet nodes (they overwrite any neumann boundary mark previously set) */
845   if (dirichlet_is) {
846     ierr = ISGetLocalSize(dirichlet_is,&is_size);CHKERRQ(ierr);
847     ierr = ISGetIndices(dirichlet_is,(const PetscInt**)&is_indices);CHKERRQ(ierr);
848     for (i=0;i<is_size;i++){
849       if (is_indices[i] > -1 && is_indices[i] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */
850         if (commsize > graph->commsizelimit) { /* dirichlet nodes treated as internal */
851           ierr = PetscBTSet(graph->touched,is_indices[i]);CHKERRQ(ierr);
852           graph->subset[is_indices[i]] = 0;
853         }
854         graph->special_dof[is_indices[i]] = PCBDDCGRAPH_DIRICHLET_MARK;
855       }
856     }
857     ierr = ISRestoreIndices(dirichlet_is,(const PetscInt**)&is_indices);CHKERRQ(ierr);
858   }
859   /* mark local periodic nodes (if any) and adapt CSR graph (if any) */
860   if (graph->mirrors) {
861     for (i=0;i<graph->nvtxs;i++)
862       if (graph->mirrors[i])
863         graph->special_dof[i] = PCBDDCGRAPH_LOCAL_PERIODIC_MARK;
864 
865     if (graph->xadj) {
866       PetscInt *new_xadj,*new_adjncy;
867       /* sort CSR graph */
868       for (i=0;i<graph->nvtxs;i++)
869         ierr = PetscSortInt(graph->xadj[i+1]-graph->xadj[i],&graph->adjncy[graph->xadj[i]]);CHKERRQ(ierr);
870 
871       /* adapt local CSR graph in case of local periodicity */
872       k = 0;
873       for (i=0;i<graph->nvtxs;i++)
874         for (j=graph->xadj[i];j<graph->xadj[i+1];j++)
875           k += graph->mirrors[graph->adjncy[j]];
876 
877       ierr = PetscMalloc1(graph->nvtxs+1,&new_xadj);CHKERRQ(ierr);
878       ierr = PetscMalloc1(k+graph->xadj[graph->nvtxs],&new_adjncy);CHKERRQ(ierr);
879       new_xadj[0] = 0;
880       for (i=0;i<graph->nvtxs;i++) {
881         k = graph->xadj[i+1]-graph->xadj[i];
882         ierr = PetscMemcpy(&new_adjncy[new_xadj[i]],&graph->adjncy[graph->xadj[i]],k*sizeof(PetscInt));CHKERRQ(ierr);
883         new_xadj[i+1] = new_xadj[i]+k;
884         for (j=graph->xadj[i];j<graph->xadj[i+1];j++) {
885           k = graph->mirrors[graph->adjncy[j]];
886           ierr = PetscMemcpy(&new_adjncy[new_xadj[i+1]],graph->mirrors_set[graph->adjncy[j]],k*sizeof(PetscInt));CHKERRQ(ierr);
887           new_xadj[i+1] += k;
888         }
889         k = new_xadj[i+1]-new_xadj[i];
890         ierr = PetscSortRemoveDupsInt(&k,&new_adjncy[new_xadj[i]]);CHKERRQ(ierr);
891         new_xadj[i+1] = new_xadj[i]+k;
892       }
893       /* set new CSR into graph */
894       ierr = PetscFree(graph->xadj);CHKERRQ(ierr);
895       ierr = PetscFree(graph->adjncy);CHKERRQ(ierr);
896       graph->xadj = new_xadj;
897       graph->adjncy = new_adjncy;
898     }
899   }
900 
901   /* mark special nodes (if any) -> each will become a single node equivalence class */
902   if (custom_primal_vertices) {
903     ierr = ISGetLocalSize(custom_primal_vertices,&is_size);CHKERRQ(ierr);
904     ierr = ISGetIndices(custom_primal_vertices,(const PetscInt**)&is_indices);CHKERRQ(ierr);
905     for (i=0,j=0;i<is_size;i++){
906       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 */
907         graph->special_dof[is_indices[i]] = PCBDDCGRAPH_SPECIAL_MARK-j;
908         j++;
909       }
910     }
911     ierr = ISRestoreIndices(custom_primal_vertices,(const PetscInt**)&is_indices);CHKERRQ(ierr);
912   }
913 
914   /* mark interior nodes (if commsize > graph->commsizelimit) as touched and belonging to partition number 0 */
915   if (commsize > graph->commsizelimit) {
916     for (i=0;i<graph->nvtxs;i++) {
917       if (!graph->count[i]) {
918         ierr = PetscBTSet(graph->touched,i);CHKERRQ(ierr);
919         graph->subset[i] = 0;
920       }
921     }
922   }
923 
924   /* init graph structure and compute default subsets */
925   nodes_touched = 0;
926   for (i=0;i<graph->nvtxs;i++) {
927     if (PetscBTLookup(graph->touched,i)) {
928       nodes_touched++;
929     }
930   }
931   i = 0;
932   graph->ncc = 0;
933   total_counts = 0;
934 
935   /* allocated space for queues */
936   if (commsize == graph->commsizelimit) {
937     ierr = PetscMalloc2(graph->nvtxs+1,&graph->cptr,graph->nvtxs,&graph->queue);CHKERRQ(ierr);
938   } else {
939     PetscInt nused = graph->nvtxs - nodes_touched;
940     ierr = PetscMalloc2(nused+1,&graph->cptr,nused,&graph->queue);CHKERRQ(ierr);
941   }
942 
943   while (nodes_touched<graph->nvtxs) {
944     /*  find first untouched node in local ordering */
945     while (PetscBTLookup(graph->touched,i)) i++;
946     ierr = PetscBTSet(graph->touched,i);CHKERRQ(ierr);
947     graph->subset[i] = graph->ncc+1;
948     graph->cptr[graph->ncc] = total_counts;
949     graph->queue[total_counts] = i;
950     total_counts++;
951     nodes_touched++;
952     /* now find all other nodes having the same set of sharing subdomains */
953     for (j=i+1;j<graph->nvtxs;j++) {
954       /* check for same number of sharing subdomains, dof number and same special mark */
955       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]) {
956         /* check for same set of sharing subdomains */
957         same_set = PETSC_TRUE;
958         for (k=0;k<graph->count[j];k++){
959           if (graph->neighbours_set[i][k] != graph->neighbours_set[j][k]) {
960             same_set = PETSC_FALSE;
961           }
962         }
963         /* I found a friend of mine */
964         if (same_set) {
965           ierr = PetscBTSet(graph->touched,j);CHKERRQ(ierr);
966           graph->subset[j] = graph->ncc+1;
967           nodes_touched++;
968           graph->queue[total_counts] = j;
969           total_counts++;
970         }
971       }
972     }
973     graph->ncc++;
974   }
975   /* 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 */
976   graph->n_subsets = graph->ncc;
977   ierr = PetscMalloc1(graph->n_subsets,&graph->subset_ncc);CHKERRQ(ierr);
978   for (i=0;i<graph->n_subsets;i++) {
979     graph->subset_ncc[i] = 1;
980   }
981   /* final pointer */
982   graph->cptr[graph->ncc] = total_counts;
983 
984   /* For consistency reasons (among neighbours), I need to sort (by global ordering) each connected component */
985   /* Get a reference node (min index in global ordering) for each subset for tagging messages */
986   ierr = PetscMalloc1(graph->ncc,&graph->subset_ref_node);CHKERRQ(ierr);
987   ierr = PetscMalloc1(graph->cptr[graph->ncc],&queue_global);CHKERRQ(ierr);
988   ierr = ISLocalToGlobalMappingApply(graph->l2gmap,graph->cptr[graph->ncc],graph->queue,queue_global);CHKERRQ(ierr);
989   for (j=0;j<graph->ncc;j++) {
990     ierr = PetscSortIntWithArray(graph->cptr[j+1]-graph->cptr[j],&queue_global[graph->cptr[j]],&graph->queue[graph->cptr[j]]);CHKERRQ(ierr);
991     graph->subset_ref_node[j] = graph->queue[graph->cptr[j]];
992   }
993   ierr = PetscFree(queue_global);CHKERRQ(ierr);
994   graph->queue_sorted = PETSC_TRUE;
995 
996   /* save information on subsets (needed when analyzing the connected components) */
997   if (graph->ncc) {
998     ierr = PetscMalloc2(graph->ncc,&graph->subset_size,graph->ncc,&graph->subset_idxs);CHKERRQ(ierr);
999     ierr = PetscMalloc1(graph->cptr[graph->ncc],&graph->subset_idxs[0]);CHKERRQ(ierr);
1000     ierr = PetscMemzero(graph->subset_idxs[0],graph->cptr[graph->ncc]*sizeof(PetscInt));CHKERRQ(ierr);
1001     for (j=1;j<graph->ncc;j++) {
1002       graph->subset_size[j-1] = graph->cptr[j] - graph->cptr[j-1];
1003       graph->subset_idxs[j] = graph->subset_idxs[j-1] + graph->subset_size[j-1];
1004     }
1005     graph->subset_size[graph->ncc-1] = graph->cptr[graph->ncc] - graph->cptr[graph->ncc-1];
1006     ierr = PetscMemcpy(graph->subset_idxs[0],graph->queue,graph->cptr[graph->ncc]*sizeof(PetscInt));CHKERRQ(ierr);
1007   }
1008 
1009   /* renumber reference nodes */
1010   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)(graph->l2gmap)),graph->ncc,graph->subset_ref_node,PETSC_COPY_VALUES,&subset_n);CHKERRQ(ierr);
1011   ierr = ISLocalToGlobalMappingApplyIS(graph->l2gmap,subset_n,&subset);CHKERRQ(ierr);
1012   ierr = ISDestroy(&subset_n);CHKERRQ(ierr);
1013   ierr = ISRenumber(subset,NULL,NULL,&subset_n);CHKERRQ(ierr);
1014   ierr = ISDestroy(&subset);CHKERRQ(ierr);
1015   ierr = ISGetLocalSize(subset_n,&k);CHKERRQ(ierr);
1016   if (k != graph->ncc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid size of new subset! %D != %D",k,graph->ncc);
1017   ierr = ISGetIndices(subset_n,&is_indices);CHKERRQ(ierr);
1018   ierr = PetscMemcpy(graph->subset_ref_node,is_indices,graph->ncc*sizeof(PetscInt));CHKERRQ(ierr);
1019   ierr = ISRestoreIndices(subset_n,&is_indices);CHKERRQ(ierr);
1020   ierr = ISDestroy(&subset_n);CHKERRQ(ierr);
1021 
1022   /* free workspace */
1023   graph->setupcalled = PETSC_TRUE;
1024   PetscFunctionReturn(0);
1025 }
1026 
1027 PetscErrorCode PCBDDCGraphResetCSR(PCBDDCGraph graph)
1028 {
1029   PetscErrorCode ierr;
1030 
1031   PetscFunctionBegin;
1032   if (!graph) PetscFunctionReturn(0);
1033   if (graph->freecsr) {
1034     ierr = PetscFree(graph->xadj);CHKERRQ(ierr);
1035     ierr = PetscFree(graph->adjncy);CHKERRQ(ierr);
1036   } else {
1037     graph->xadj = NULL;
1038     graph->adjncy = NULL;
1039   }
1040   graph->freecsr = PETSC_FALSE;
1041   graph->nvtxs_csr = 0;
1042   PetscFunctionReturn(0);
1043 }
1044 
1045 PetscErrorCode PCBDDCGraphReset(PCBDDCGraph graph)
1046 {
1047   PetscErrorCode ierr;
1048 
1049   PetscFunctionBegin;
1050   if (!graph) PetscFunctionReturn(0);
1051   ierr = ISLocalToGlobalMappingDestroy(&graph->l2gmap);CHKERRQ(ierr);
1052   ierr = PetscFree(graph->subset_ncc);CHKERRQ(ierr);
1053   ierr = PetscFree(graph->subset_ref_node);CHKERRQ(ierr);
1054   if (graph->nvtxs) {
1055     ierr = PetscFree(graph->neighbours_set[0]);CHKERRQ(ierr);
1056   }
1057   ierr = PetscBTDestroy(&graph->touched);CHKERRQ(ierr);
1058   ierr = PetscFree5(graph->count,
1059                     graph->neighbours_set,
1060                     graph->subset,
1061                     graph->which_dof,
1062                     graph->special_dof);CHKERRQ(ierr);
1063   ierr = PetscFree2(graph->cptr,graph->queue);CHKERRQ(ierr);
1064   if (graph->mirrors) {
1065     ierr = PetscFree(graph->mirrors_set[0]);CHKERRQ(ierr);
1066   }
1067   ierr = PetscFree2(graph->mirrors,graph->mirrors_set);CHKERRQ(ierr);
1068   if (graph->subset_idxs) {
1069     ierr = PetscFree(graph->subset_idxs[0]);CHKERRQ(ierr);
1070   }
1071   ierr = PetscFree2(graph->subset_size,graph->subset_idxs);CHKERRQ(ierr);
1072   ierr = ISDestroy(&graph->dirdofs);CHKERRQ(ierr);
1073   ierr = ISDestroy(&graph->dirdofsB);CHKERRQ(ierr);
1074   if (graph->n_local_subs) {
1075     ierr = PetscFree(graph->local_subs);CHKERRQ(ierr);
1076   }
1077   graph->has_dirichlet       = PETSC_FALSE;
1078   graph->twodimset           = PETSC_FALSE;
1079   graph->twodim              = PETSC_FALSE;
1080   graph->nvtxs               = 0;
1081   graph->nvtxs_global        = 0;
1082   graph->n_subsets           = 0;
1083   graph->custom_minimal_size = 1;
1084   graph->n_local_subs        = 0;
1085   graph->maxcount            = PETSC_MAX_INT;
1086   graph->setupcalled         = PETSC_FALSE;
1087   PetscFunctionReturn(0);
1088 }
1089 
1090 PetscErrorCode PCBDDCGraphInit(PCBDDCGraph graph, ISLocalToGlobalMapping l2gmap, PetscInt N, PetscInt maxcount)
1091 {
1092   PetscInt       n;
1093   PetscErrorCode ierr;
1094 
1095   PetscFunctionBegin;
1096   PetscValidPointer(graph,1);
1097   PetscValidHeaderSpecific(l2gmap,IS_LTOGM_CLASSID,2);
1098   PetscValidLogicalCollectiveInt(l2gmap,N,3);
1099   PetscValidLogicalCollectiveInt(l2gmap,maxcount,4);
1100   /* raise an error if already allocated */
1101   if (graph->nvtxs_global) SETERRQ(PetscObjectComm((PetscObject)l2gmap),PETSC_ERR_PLIB,"BDDCGraph already initialized");
1102   /* set number of vertices */
1103   ierr = PetscObjectReference((PetscObject)l2gmap);CHKERRQ(ierr);
1104   graph->l2gmap = l2gmap;
1105   ierr = ISLocalToGlobalMappingGetSize(l2gmap,&n);CHKERRQ(ierr);
1106   graph->nvtxs = n;
1107   graph->nvtxs_global = N;
1108   /* allocate used space */
1109   ierr = PetscBTCreate(graph->nvtxs,&graph->touched);CHKERRQ(ierr);
1110   ierr = PetscMalloc5(graph->nvtxs,&graph->count,
1111                       graph->nvtxs,&graph->neighbours_set,
1112                       graph->nvtxs,&graph->subset,
1113                       graph->nvtxs,&graph->which_dof,
1114                       graph->nvtxs,&graph->special_dof);CHKERRQ(ierr);
1115   /* zeroes memory */
1116   ierr = PetscMemzero(graph->count,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
1117   ierr = PetscMemzero(graph->subset,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
1118   /* use -1 as a default value for which_dof array */
1119   for (n=0;n<graph->nvtxs;n++) graph->which_dof[n] = -1;
1120   ierr = PetscMemzero(graph->special_dof,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr);
1121   /* zeroes first pointer to neighbour set */
1122   if (graph->nvtxs) {
1123     graph->neighbours_set[0] = 0;
1124   }
1125   /* zeroes workspace for values of ncc */
1126   graph->subset_ncc = 0;
1127   graph->subset_ref_node = 0;
1128   /* maxcount for cc */
1129   graph->maxcount = maxcount;
1130   PetscFunctionReturn(0);
1131 }
1132 
1133 PetscErrorCode PCBDDCGraphDestroy(PCBDDCGraph* graph)
1134 {
1135   PetscErrorCode ierr;
1136 
1137   PetscFunctionBegin;
1138   ierr = PCBDDCGraphResetCSR(*graph);CHKERRQ(ierr);
1139   ierr = PCBDDCGraphReset(*graph);CHKERRQ(ierr);
1140   ierr = PetscFree(*graph);CHKERRQ(ierr);
1141   PetscFunctionReturn(0);
1142 }
1143 
1144 PetscErrorCode PCBDDCGraphCreate(PCBDDCGraph *graph)
1145 {
1146   PCBDDCGraph    new_graph;
1147   PetscErrorCode ierr;
1148 
1149   PetscFunctionBegin;
1150   ierr = PetscNew(&new_graph);CHKERRQ(ierr);
1151   new_graph->custom_minimal_size = 1;
1152   new_graph->commsizelimit = 1;
1153   *graph = new_graph;
1154   PetscFunctionReturn(0);
1155 }
1156