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