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