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