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