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