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