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