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