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