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