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