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