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