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