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