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