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