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