Actual source code: bddcgraph.c

petsc-3.8.4 2018-03-24
Report Typos and Errors
  1:  #include <petsc/private/petscimpl.h>
  2:  #include <../src/ksp/pc/impls/bddc/bddcprivate.h>
  3:  #include <../src/ksp/pc/impls/bddc/bddcstructs.h>

  5: PetscErrorCode PCBDDCGraphGetDirichletDofsB(PCBDDCGraph graph, IS* dirdofs)
  6: {

 10:   if (graph->dirdofsB) {
 11:     PetscObjectReference((PetscObject)graph->dirdofsB);
 12:   } else if (graph->has_dirichlet) {
 13:     PetscInt i,size;
 14:     PetscInt *dirdofs_idxs;

 16:     size = 0;
 17:     for (i=0;i<graph->nvtxs;i++) {
 18:       if (graph->count[i] && graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) size++;
 19:     }

 21:     PetscMalloc1(size,&dirdofs_idxs);
 22:     size = 0;
 23:     for (i=0;i<graph->nvtxs;i++) {
 24:       if (graph->count[i] && graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) dirdofs_idxs[size++] = i;
 25:     }
 26:     ISCreateGeneral(PETSC_COMM_SELF,size,dirdofs_idxs,PETSC_OWN_POINTER,&graph->dirdofsB);
 27:     PetscObjectReference((PetscObject)graph->dirdofsB);
 28:   }
 29:   *dirdofs = graph->dirdofsB;
 30:   return(0);
 31: }

 33: PetscErrorCode PCBDDCGraphGetDirichletDofs(PCBDDCGraph graph, IS* dirdofs)
 34: {

 38:   if (graph->dirdofs) {
 39:     PetscObjectReference((PetscObject)graph->dirdofs);
 40:   } else if (graph->has_dirichlet) {
 41:     PetscInt i,size;
 42:     PetscInt *dirdofs_idxs;

 44:     size = 0;
 45:     for (i=0;i<graph->nvtxs;i++) {
 46:       if (graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) size++;
 47:     }

 49:     PetscMalloc1(size,&dirdofs_idxs);
 50:     size = 0;
 51:     for (i=0;i<graph->nvtxs;i++) {
 52:       if (graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) dirdofs_idxs[size++] = i;
 53:     }
 54:     ISCreateGeneral(PetscObjectComm((PetscObject)graph->l2gmap),size,dirdofs_idxs,PETSC_OWN_POINTER,&graph->dirdofs);
 55:     PetscObjectReference((PetscObject)graph->dirdofs);
 56:   }
 57:   *dirdofs = graph->dirdofs;
 58:   return(0);
 59: }

 61: PetscErrorCode PCBDDCGraphASCIIView(PCBDDCGraph graph, PetscInt verbosity_level, PetscViewer viewer)
 62: {
 63:   PetscInt       i,j,tabs;
 64:   PetscInt*      queue_in_global_numbering;

 68:   PetscViewerASCIIPushSynchronized(viewer);
 69:   PetscViewerASCIIGetTab(viewer,&tabs);
 70:   PetscViewerASCIIPrintf(viewer,"--------------------------------------------------\n");
 71:   PetscViewerFlush(viewer);
 72:   PetscViewerASCIISynchronizedPrintf(viewer,"Local BDDC graph for subdomain %04d\n",PetscGlobalRank);
 73:   PetscViewerASCIISynchronizedPrintf(viewer,"Number of vertices %d\n",graph->nvtxs);
 74:   PetscViewerASCIISynchronizedPrintf(viewer,"Custom minimal size %d\n",graph->custom_minimal_size);
 75:   if (graph->maxcount != PETSC_MAX_INT) {
 76:     PetscViewerASCIISynchronizedPrintf(viewer,"Max count %d\n",graph->maxcount);
 77:   }
 78:   PetscViewerASCIISynchronizedPrintf(viewer,"Topological two dim? %d (set %d)\n",graph->twodim,graph->twodimset);
 79:   if (verbosity_level > 2) {
 80:     for (i=0;i<graph->nvtxs;i++) {
 81:       PetscViewerASCIISynchronizedPrintf(viewer,"%d:\n",i);
 82:       PetscViewerASCIISynchronizedPrintf(viewer,"   which_dof: %d\n",graph->which_dof[i]);
 83:       PetscViewerASCIISynchronizedPrintf(viewer,"   special_dof: %d\n",graph->special_dof[i]);
 84:       PetscViewerASCIISynchronizedPrintf(viewer,"   neighbours: %d\n",graph->count[i]);
 85:       PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
 86:       if (graph->count[i]) {
 87:         PetscViewerASCIISynchronizedPrintf(viewer,"     set of neighbours:");
 88:         for (j=0;j<graph->count[i];j++) {
 89:           PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->neighbours_set[i][j]);
 90:         }
 91:         PetscViewerASCIISynchronizedPrintf(viewer,"\n");
 92:       }
 93:       PetscViewerASCIISetTab(viewer,tabs);
 94:       PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
 95:       if (graph->mirrors) {
 96:         PetscViewerASCIISynchronizedPrintf(viewer,"   mirrors: %d\n",graph->mirrors[i]);
 97:         if (graph->mirrors[i]) {
 98:           PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
 99:           PetscViewerASCIISynchronizedPrintf(viewer,"     set of mirrors:");
100:           for (j=0;j<graph->mirrors[i];j++) {
101:             PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->mirrors_set[i][j]);
102:           }
103:           PetscViewerASCIISynchronizedPrintf(viewer,"\n");
104:           PetscViewerASCIISetTab(viewer,tabs);
105:           PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
106:         }
107:       }
108:       if (verbosity_level > 3) {
109:         if (graph->xadj) {
110:           PetscViewerASCIISynchronizedPrintf(viewer,"   local adj list:");
111:           PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
112:           for (j=graph->xadj[i];j<graph->xadj[i+1];j++) {
113:             PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->adjncy[j]);
114:           }
115:           PetscViewerASCIISynchronizedPrintf(viewer,"\n");
116:           PetscViewerASCIISetTab(viewer,tabs);
117:           PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
118:         } else {
119:           PetscViewerASCIISynchronizedPrintf(viewer,"   no adj info\n");
120:         }
121:       }
122:       if (graph->n_local_subs) {
123:         PetscViewerASCIISynchronizedPrintf(viewer,"   local sub id: %d\n",graph->local_subs[i]);
124:       }
125:       PetscViewerASCIISynchronizedPrintf(viewer,"   interface subset id: %d\n",graph->subset[i]);
126:       if (graph->subset[i] && graph->subset_ncc) {
127:         PetscViewerASCIISynchronizedPrintf(viewer,"   ncc for subset: %d\n",graph->subset_ncc[graph->subset[i]-1]);
128:       }
129:     }
130:   }
131:   PetscViewerASCIISynchronizedPrintf(viewer,"Total number of connected components %d\n",graph->ncc);
132:   PetscMalloc1(graph->cptr[graph->ncc],&queue_in_global_numbering);
133:   ISLocalToGlobalMappingApply(graph->l2gmap,graph->cptr[graph->ncc],graph->queue,queue_in_global_numbering);
134:   for (i=0;i<graph->ncc;i++) {
135:     PetscInt node_num=graph->queue[graph->cptr[i]];
136:     PetscBool printcc = PETSC_FALSE;
137:     PetscViewerASCIISynchronizedPrintf(viewer,"  cc %d (size %d, fid %d, neighs:",i,graph->cptr[i+1]-graph->cptr[i],graph->which_dof[node_num]);
138:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
139:     for (j=0;j<graph->count[node_num];j++) {
140:       PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->neighbours_set[node_num][j]);
141:     }
142:     if (verbosity_level > 1) {
143:       PetscViewerASCIISynchronizedPrintf(viewer,"):");
144:       if (verbosity_level > 2 || graph->twodim || graph->count[node_num] > 1 || (graph->count[node_num] == 1 && graph->special_dof[node_num] == PCBDDCGRAPH_NEUMANN_MARK)) {
145:         printcc = PETSC_TRUE;
146:       }
147:       if (printcc) {
148:         for (j=graph->cptr[i];j<graph->cptr[i+1];j++) {
149:           PetscViewerASCIISynchronizedPrintf(viewer," %d (%d)",graph->queue[j],queue_in_global_numbering[j]);
150:         }
151:       }
152:     } else {
153:       PetscViewerASCIISynchronizedPrintf(viewer,")");
154:     }
155:     PetscViewerASCIISynchronizedPrintf(viewer,"\n");
156:     PetscViewerASCIISetTab(viewer,tabs);
157:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
158:   }
159:   PetscFree(queue_in_global_numbering);
160:   PetscViewerFlush(viewer);
161:   return(0);
162: }

164: PetscErrorCode PCBDDCGraphRestoreCandidatesIS(PCBDDCGraph graph, PetscInt *n_faces, IS *FacesIS[], PetscInt *n_edges, IS *EdgesIS[], IS *VerticesIS)
165: {
166:   PetscInt       i;

170:   if (n_faces) {
171:     if (FacesIS) {
172:       for (i=0;i<*n_faces;i++) {
173:         ISDestroy(&((*FacesIS)[i]));
174:       }
175:       PetscFree(*FacesIS);
176:     }
177:     *n_faces = 0;
178:   }
179:   if (n_edges) {
180:     if (EdgesIS) {
181:       for (i=0;i<*n_edges;i++) {
182:         ISDestroy(&((*EdgesIS)[i]));
183:       }
184:       PetscFree(*EdgesIS);
185:     }
186:     *n_edges = 0;
187:   }
188:   if (VerticesIS) {
189:     ISDestroy(VerticesIS);
190:   }
191:   return(0);
192: }

194: PetscErrorCode PCBDDCGraphGetCandidatesIS(PCBDDCGraph graph, PetscInt *n_faces, IS *FacesIS[], PetscInt *n_edges, IS *EdgesIS[], IS *VerticesIS)
195: {
196:   IS             *ISForFaces,*ISForEdges,ISForVertices;
197:   PetscInt       i,nfc,nec,nvc,*idx,*mark;

201:   PetscCalloc1(graph->ncc,&mark);
202:   /* loop on ccs to evalute number of faces, edges and vertices */
203:   nfc = 0;
204:   nec = 0;
205:   nvc = 0;
206:   for (i=0;i<graph->ncc;i++) {
207:     PetscInt repdof = graph->queue[graph->cptr[i]];
208:     if (graph->cptr[i+1]-graph->cptr[i] > graph->custom_minimal_size && graph->count[repdof] < graph->maxcount) {
209:       if (!graph->twodim && graph->count[repdof] == 1 && graph->special_dof[repdof] != PCBDDCGRAPH_NEUMANN_MARK) {
210:         nfc++;
211:         mark[i] = 2;
212:       } else {
213:         nec++;
214:         mark[i] = 1;
215:       }
216:     } else {
217:       nvc += graph->cptr[i+1]-graph->cptr[i];
218:     }
219:   }

221:   /* allocate IS arrays for faces, edges. Vertices need a single index set. */
222:   if (FacesIS) {
223:     PetscMalloc1(nfc,&ISForFaces);
224:   }
225:   if (EdgesIS) {
226:     PetscMalloc1(nec,&ISForEdges);
227:   }
228:   if (VerticesIS) {
229:     PetscMalloc1(nvc,&idx);
230:   }

232:   /* loop on ccs to compute index sets for faces and edges */
233:   if (!graph->queue_sorted) {
234:     PetscInt *queue_global;

236:     PetscMalloc1(graph->cptr[graph->ncc],&queue_global);
237:     ISLocalToGlobalMappingApply(graph->l2gmap,graph->cptr[graph->ncc],graph->queue,queue_global);
238:     for (i=0;i<graph->ncc;i++) {
239:       PetscSortIntWithArray(graph->cptr[i+1]-graph->cptr[i],&queue_global[graph->cptr[i]],&graph->queue[graph->cptr[i]]);
240:     }
241:     PetscFree(queue_global);
242:     graph->queue_sorted = PETSC_TRUE;
243:   }
244:   nfc = 0;
245:   nec = 0;
246:   for (i=0;i<graph->ncc;i++) {
247:     if (mark[i] == 2) {
248:       if (FacesIS) {
249:         ISCreateGeneral(PETSC_COMM_SELF,graph->cptr[i+1]-graph->cptr[i],&graph->queue[graph->cptr[i]],PETSC_USE_POINTER,&ISForFaces[nfc]);
250:       }
251:       nfc++;
252:     } else if (mark[i] == 1) {
253:       if (EdgesIS) {
254:         ISCreateGeneral(PETSC_COMM_SELF,graph->cptr[i+1]-graph->cptr[i],&graph->queue[graph->cptr[i]],PETSC_USE_POINTER,&ISForEdges[nec]);
255:       }
256:       nec++;
257:     }
258:   }

260:   /* index set for vertices */
261:   if (VerticesIS) {
262:     nvc = 0;
263:     for (i=0;i<graph->ncc;i++) {
264:       if (!mark[i]) {
265:         PetscInt j;

267:         for (j=graph->cptr[i];j<graph->cptr[i+1];j++) {
268:           idx[nvc]=graph->queue[j];
269:           nvc++;
270:         }
271:       }
272:     }
273:     /* sort vertex set (by local ordering) */
274:     PetscSortInt(nvc,idx);
275:     ISCreateGeneral(PETSC_COMM_SELF,nvc,idx,PETSC_OWN_POINTER,&ISForVertices);
276:   }
277:   PetscFree(mark);

279:   /* get back info */
280:   if (n_faces)       *n_faces = nfc;
281:   if (FacesIS)       *FacesIS = ISForFaces;
282:   if (n_edges)       *n_edges = nec;
283:   if (EdgesIS)       *EdgesIS = ISForEdges;
284:   if (VerticesIS) *VerticesIS = ISForVertices;
285:   return(0);
286: }

288: PetscErrorCode PCBDDCGraphComputeConnectedComponents(PCBDDCGraph graph)
289: {
290:   PetscBool      adapt_interface_reduced;
291:   MPI_Comm       interface_comm;
292:   PetscMPIInt    size;
293:   PetscInt       i;

297:   /* compute connected components locally */
298:   PetscObjectGetComm((PetscObject)(graph->l2gmap),&interface_comm);
299:   PCBDDCGraphComputeConnectedComponentsLocal(graph);
300:   /* check consistency of connected components among neighbouring subdomains -> it adapt them in case it is needed */
301:   MPI_Comm_size(interface_comm,&size);
302:   adapt_interface_reduced = PETSC_FALSE;
303:   if (size > 1) {
304:     PetscInt i;
305:     PetscBool adapt_interface = PETSC_FALSE;
306:     for (i=0;i<graph->n_subsets;i++) {
307:       /* We are not sure that on a given subset of the local interface,
308:          with two connected components, the latters be the same among sharing subdomains */
309:       if (graph->subset_ncc[i] > 1) {
310:         adapt_interface = PETSC_TRUE;
311:         break;
312:       }
313:     }
314:     MPIU_Allreduce(&adapt_interface,&adapt_interface_reduced,1,MPIU_BOOL,MPI_LOR,interface_comm);
315:   }

317:   if (graph->n_subsets && adapt_interface_reduced) {
318:     PetscBT     subset_cc_adapt;
319:     MPI_Request *send_requests,*recv_requests;
320:     PetscInt    *send_buffer,*recv_buffer;
321:     PetscInt    sum_requests,start_of_recv,start_of_send;
322:     PetscInt    *cum_recv_counts;
323:     PetscInt    *labels;
324:     PetscInt    ncc,cum_queue,mss,mns,j,k,s;
325:     PetscInt    **refine_buffer=NULL,*private_labels = NULL;

327:     PetscMalloc1(graph->nvtxs,&labels);
328:     PetscMemzero(labels,graph->nvtxs*sizeof(*labels));
329:     for (i=0;i<graph->ncc;i++)
330:       for (j=graph->cptr[i];j<graph->cptr[i+1];j++)
331:         labels[graph->queue[j]] = i;

333:     /* allocate some space */
334:     PetscMalloc1(graph->n_subsets+1,&cum_recv_counts);
335:     PetscMemzero(cum_recv_counts,(graph->n_subsets+1)*sizeof(*cum_recv_counts));

337:     /* first count how many neighbours per connected component I will receive from */
338:     cum_recv_counts[0] = 0;
339:     for (i=0;i<graph->n_subsets;i++) cum_recv_counts[i+1] = cum_recv_counts[i]+graph->count[graph->subset_idxs[i][0]];
340:     PetscMalloc1(cum_recv_counts[graph->n_subsets],&recv_buffer);
341:     PetscMalloc2(cum_recv_counts[graph->n_subsets],&send_requests,cum_recv_counts[graph->n_subsets],&recv_requests);
342:     for (i=0;i<cum_recv_counts[graph->n_subsets];i++) {
343:       send_requests[i] = MPI_REQUEST_NULL;
344:       recv_requests[i] = MPI_REQUEST_NULL;
345:     }

347:     /* exchange with my neighbours the number of my connected components on the subset of interface */
348:     sum_requests = 0;
349:     for (i=0;i<graph->n_subsets;i++) {
350:       PetscMPIInt neigh,tag;
351:       PetscInt    count,*neighs;

353:       count = graph->count[graph->subset_idxs[i][0]];
354:       neighs = graph->neighbours_set[graph->subset_idxs[i][0]];
355:       PetscMPIIntCast(2*graph->subset_ref_node[i],&tag);
356:       for (k=0;k<count;k++) {
357:         PetscMPIIntCast(neighs[k],&neigh);
358:         MPI_Isend(&graph->subset_ncc[i],1,MPIU_INT,neigh,tag,interface_comm,&send_requests[sum_requests]);
359:         MPI_Irecv(&recv_buffer[sum_requests],1,MPIU_INT,neigh,tag,interface_comm,&recv_requests[sum_requests]);
360:         sum_requests++;
361:       }
362:     }
363:     MPI_Waitall(sum_requests,recv_requests,MPI_STATUSES_IGNORE);
364:     MPI_Waitall(sum_requests,send_requests,MPI_STATUSES_IGNORE);

366:     /* determine the subsets I have to adapt (those having more than 1 cc) */
367:     PetscBTCreate(graph->n_subsets,&subset_cc_adapt);
368:     PetscBTMemzero(graph->n_subsets,subset_cc_adapt);
369:     for (i=0;i<graph->n_subsets;i++) {
370:       if (graph->subset_ncc[i] > 1) {
371:         PetscBTSet(subset_cc_adapt,i);
372:         continue;
373:       }
374:       for (j=cum_recv_counts[i];j<cum_recv_counts[i+1];j++){
375:          if (recv_buffer[j] > 1) {
376:           PetscBTSet(subset_cc_adapt,i);
377:           break;
378:         }
379:       }
380:     }
381:     PetscFree(recv_buffer);

383:     /* determine send/recv buffers sizes */
384:     j = 0;
385:     mss = 0;
386:     for (i=0;i<graph->n_subsets;i++) {
387:       if (PetscBTLookup(subset_cc_adapt,i)) {
388:         j += graph->subset_size[i];
389:         mss = PetscMax(graph->subset_size[i],mss);
390:       }
391:     }
392:     k = 0;
393:     mns = 0;
394:     for (i=0;i<graph->n_subsets;i++) {
395:       if (PetscBTLookup(subset_cc_adapt,i)) {
396:         k += (cum_recv_counts[i+1]-cum_recv_counts[i])*graph->subset_size[i];
397:         mns = PetscMax(cum_recv_counts[i+1]-cum_recv_counts[i],mns);
398:       }
399:     }
400:     PetscMalloc2(j,&send_buffer,k,&recv_buffer);

402:     /* fill send buffer (order matters: subset_idxs ordered by global ordering) */
403:     j = 0;
404:     for (i=0;i<graph->n_subsets;i++)
405:       if (PetscBTLookup(subset_cc_adapt,i))
406:         for (k=0;k<graph->subset_size[i];k++)
407:           send_buffer[j++] = labels[graph->subset_idxs[i][k]];

409:     /* now exchange the data */
410:     start_of_recv = 0;
411:     start_of_send = 0;
412:     sum_requests = 0;
413:     for (i=0;i<graph->n_subsets;i++) {
414:       if (PetscBTLookup(subset_cc_adapt,i)) {
415:         PetscMPIInt neigh,tag;
416:         PetscInt    size_of_send = graph->subset_size[i];

418:         j = graph->subset_idxs[i][0];
419:         PetscMPIIntCast(2*graph->subset_ref_node[i]+1,&tag);
420:         for (k=0;k<graph->count[j];k++) {
421:           PetscMPIIntCast(graph->neighbours_set[j][k],&neigh);
422:           MPI_Isend(&send_buffer[start_of_send],size_of_send,MPIU_INT,neigh,tag,interface_comm,&send_requests[sum_requests]);
423:           MPI_Irecv(&recv_buffer[start_of_recv],size_of_send,MPIU_INT,neigh,tag,interface_comm,&recv_requests[sum_requests]);
424:           start_of_recv += size_of_send;
425:           sum_requests++;
426:         }
427:         start_of_send += size_of_send;
428:       }
429:     }
430:     MPI_Waitall(sum_requests,recv_requests,MPI_STATUSES_IGNORE);

432:     /* refine connected components */
433:     start_of_recv = 0;
434:     /* allocate some temporary space */
435:     if (mss) {
436:       PetscMalloc1(mss,&refine_buffer);
437:       PetscMalloc2(mss*(mns+1),&refine_buffer[0],mss,&private_labels);
438:     }
439:     ncc = 0;
440:     cum_queue = 0;
441:     graph->cptr[0] = 0;
442:     for (i=0;i<graph->n_subsets;i++) {
443:       if (PetscBTLookup(subset_cc_adapt,i)) {
444:         PetscInt subset_counter = 0;
445:         PetscInt sharingprocs = cum_recv_counts[i+1]-cum_recv_counts[i]+1; /* count myself */
446:         PetscInt buffer_size = graph->subset_size[i];

448:         /* compute pointers */
449:         for (j=1;j<buffer_size;j++) refine_buffer[j] = refine_buffer[j-1] + sharingprocs;
450:         /* analyze contributions from subdomains that share the i-th subset
451:            The stricture of refine_buffer is suitable to find intersections of ccs among sharingprocs.
452:            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)
453:            sharing procs connected components:
454:              neigh 0: [0 1 4], [2 3], labels [4,7]  (2 connected components)
455:              neigh 1: [0 1], [2 3 4], labels [3 2]  (2 connected components)
456:              neigh 2: [0 4], [1], [2 3], labels [1 5 6] (3 connected components)
457:            refine_buffer will be filled as:
458:              [ 4, 3, 1;
459:                4, 2, 1;
460:                7, 2, 6;
461:                4, 3, 5;
462:                7, 2, 6; ];
463:            The connected components in local ordering are [0], [1], [2 3], [4] */
464:         /* fill temp_buffer */
465:         for (k=0;k<buffer_size;k++) refine_buffer[k][0] = labels[graph->subset_idxs[i][k]];
466:         for (j=0;j<sharingprocs-1;j++) {
467:           for (k=0;k<buffer_size;k++) refine_buffer[k][j+1] = recv_buffer[start_of_recv+k];
468:           start_of_recv += buffer_size;
469:         }
470:         PetscMemzero(private_labels,buffer_size*sizeof(PetscInt));
471:         for (j=0;j<buffer_size;j++) {
472:           if (!private_labels[j]) { /* found a new cc  */
473:             PetscBool same_set;

475:             graph->cptr[ncc] = cum_queue;
476:             ncc++;
477:             subset_counter++;
478:             private_labels[j] = subset_counter;
479:             graph->queue[cum_queue++] = graph->subset_idxs[i][j];
480:             for (k=j+1;k<buffer_size;k++) { /* check for other nodes in new cc */
481:               same_set = PETSC_TRUE;
482:               for (s=0;s<sharingprocs;s++) {
483:                 if (refine_buffer[j][s] != refine_buffer[k][s]) {
484:                   same_set = PETSC_FALSE;
485:                   break;
486:                 }
487:               }
488:               if (same_set) {
489:                 private_labels[k] = subset_counter;
490:                 graph->queue[cum_queue++] = graph->subset_idxs[i][k];
491:               }
492:             }
493:           }
494:         }
495:         graph->cptr[ncc] = cum_queue;
496:         graph->subset_ncc[i] = subset_counter;
497:         graph->queue_sorted = PETSC_FALSE;
498:       } else { /* this subset does not need to be adapted */
499:         PetscMemcpy(graph->queue+cum_queue,graph->subset_idxs[i],graph->subset_size[i]*sizeof(PetscInt));
500:         ncc++;
501:         cum_queue += graph->subset_size[i];
502:         graph->cptr[ncc] = cum_queue;
503:       }
504:     }
505:     graph->cptr[ncc] = cum_queue;
506:     graph->ncc = ncc;
507:     if (mss) {
508:       PetscFree2(refine_buffer[0],private_labels);
509:       PetscFree(refine_buffer);
510:     }
511:     PetscFree(labels);
512:     MPI_Waitall(sum_requests,send_requests,MPI_STATUSES_IGNORE);
513:     PetscFree2(send_requests,recv_requests);
514:     PetscFree2(send_buffer,recv_buffer);
515:     PetscFree(cum_recv_counts);
516:     PetscBTDestroy(&subset_cc_adapt);
517:   }

519:   /* Determine if we are in 2D or 3D */
520:   if (!graph->twodimset) {
521:     PetscBool twodim = PETSC_TRUE;
522:     for (i=0;i<graph->ncc;i++) {
523:       PetscInt repdof = graph->queue[graph->cptr[i]];
524:       PetscInt ccsize = graph->cptr[i+1]-graph->cptr[i];
525:       if (graph->count[repdof] > 1 && ccsize > graph->custom_minimal_size) {
526:         twodim = PETSC_FALSE;
527:         break;
528:       }
529:     }
530:     MPIU_Allreduce(&twodim,&graph->twodim,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)graph->l2gmap));
531:     graph->twodimset = PETSC_TRUE;
532:   }
533:   return(0);
534: }


537: PETSC_STATIC_INLINE PetscErrorCode PCBDDCGraphComputeCC_Private(PCBDDCGraph graph,PetscInt pid,PetscInt* queue_tip,PetscInt n_prev,PetscInt* n_added)
538: {
539:   PetscInt       i,j,n;
540:   PetscInt       *xadj = graph->xadj,*adjncy = graph->adjncy;
541:   PetscBT        touched = graph->touched;
542:   PetscBool      havecsr = (PetscBool)(!!xadj);
543:   PetscBool      havesubs = (PetscBool)(!!graph->n_local_subs);

547:   n = 0;
548:   if (havecsr && !havesubs) {
549:     for (i=-n_prev;i<0;i++) {
550:       PetscInt start_dof = queue_tip[i];
551:       /* we assume that if a dof has a size 1 adjacency list and the corresponding entry is negative, it is connected to all dofs */
552:       if (xadj[start_dof+1]-xadj[start_dof] == 1 && adjncy[xadj[start_dof]] < 0) {
553:         for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */
554:           PetscInt dof = graph->subset_idxs[pid-1][j];
555:           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid) {
556:             PetscBTSet(touched,dof);
557:             queue_tip[n] = dof;
558:             n++;
559:           }
560:         }
561:       } else {
562:         for (j=xadj[start_dof];j<xadj[start_dof+1];j++) {
563:           PetscInt dof = adjncy[j];
564:           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid) {
565:             PetscBTSet(touched,dof);
566:             queue_tip[n] = dof;
567:             n++;
568:           }
569:         }
570:       }
571:     }
572:   } else if (havecsr && havesubs) {
573:     PetscInt sid = graph->local_subs[queue_tip[-n_prev]];
574:     for (i=-n_prev;i<0;i++) {
575:       PetscInt start_dof = queue_tip[i];
576:       /* we assume that if a dof has a size 1 adjacency list and the corresponding entry is negative, it is connected to all dofs belonging to the local sub */
577:       if (xadj[start_dof+1]-xadj[start_dof] == 1 && adjncy[xadj[start_dof]] < 0) {
578:         for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */
579:           PetscInt dof = graph->subset_idxs[pid-1][j];
580:           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) {
581:             PetscBTSet(touched,dof);
582:             queue_tip[n] = dof;
583:             n++;
584:           }
585:         }
586:       } else {
587:         for (j=xadj[start_dof];j<xadj[start_dof+1];j++) {
588:           PetscInt dof = adjncy[j];
589:           if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) {
590:             PetscBTSet(touched,dof);
591:             queue_tip[n] = dof;
592:             n++;
593:           }
594:         }
595:       }
596:     }
597:   } else { /* sub info only */
598:     PetscInt sid = graph->local_subs[queue_tip[-n_prev]];
599:     for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */
600:       PetscInt dof = graph->subset_idxs[pid-1][j];
601:       if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) {
602:         PetscBTSet(touched,dof);
603:         queue_tip[n] = dof;
604:         n++;
605:       }
606:     }
607:   }
608:   *n_added = n;
609:   return(0);
610: }

612: PetscErrorCode PCBDDCGraphComputeConnectedComponentsLocal(PCBDDCGraph graph)
613: {
614:   PetscInt       ncc,cum_queue,n;
615:   PetscMPIInt    commsize;

619:   if (!graph->setupcalled) SETERRQ(PetscObjectComm((PetscObject)graph->l2gmap),PETSC_ERR_ORDER,"PCBDDCGraphSetUp should be called first");
620:   /* quiet return if there isn't any local info */
621:   if (!graph->xadj && !graph->n_local_subs) {
622:     return(0);
623:   }

625:   /* reset any previous search of connected components */
626:   PetscBTMemzero(graph->nvtxs,graph->touched);
627:   MPI_Comm_size(PetscObjectComm((PetscObject)graph->l2gmap),&commsize);
628:   if (commsize > graph->commsizelimit) {
629:     PetscInt i;
630:     for (i=0;i<graph->nvtxs;i++) {
631:       if (graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK || !graph->count[i]) {
632:         PetscBTSet(graph->touched,i);
633:       }
634:     }
635:   }

637:   /* begin search for connected components */
638:   cum_queue = 0;
639:   ncc = 0;
640:   for (n=0;n<graph->n_subsets;n++) {
641:     PetscInt pid = n+1;  /* partition labeled by 0 is discarded */
642:     PetscInt found = 0,prev = 0,first = 0,ncc_pid = 0;
643:     while (found != graph->subset_size[n]) {
644:       PetscInt added = 0;
645:       if (!prev) { /* search for new starting dof */
646:         while (PetscBTLookup(graph->touched,graph->subset_idxs[n][first])) first++;
647:         PetscBTSet(graph->touched,graph->subset_idxs[n][first]);
648:         graph->queue[cum_queue] = graph->subset_idxs[n][first];
649:         graph->cptr[ncc] = cum_queue;
650:         prev = 1;
651:         cum_queue++;
652:         found++;
653:         ncc_pid++;
654:         ncc++;
655:       }
656:       PCBDDCGraphComputeCC_Private(graph,pid,graph->queue + cum_queue,prev,&added);
657:       if (!added) {
658:         graph->subset_ncc[n] = ncc_pid;
659:         graph->cptr[ncc] = cum_queue;
660:       }
661:       prev = added;
662:       found += added;
663:       cum_queue += added;
664:       if (added && found == graph->subset_size[n]) {
665:         graph->subset_ncc[n] = ncc_pid;
666:         graph->cptr[ncc] = cum_queue;
667:       }
668:     }
669:   }
670:   graph->ncc = ncc;
671:   graph->queue_sorted = PETSC_FALSE;
672:   return(0);
673: }

675: PetscErrorCode PCBDDCGraphSetUp(PCBDDCGraph graph, PetscInt custom_minimal_size, IS neumann_is, IS dirichlet_is, PetscInt n_ISForDofs, IS ISForDofs[], IS custom_primal_vertices)
676: {
677:   IS             subset,subset_n;
678:   MPI_Comm       comm;
679:   const PetscInt *is_indices;
680:   PetscInt       n_neigh,*neigh,*n_shared,**shared,*queue_global;
681:   PetscInt       i,j,k,s,total_counts,nodes_touched,is_size;
682:   PetscMPIInt    commsize;
683:   PetscBool      same_set,mirrors_found;

688:   if (neumann_is) {
691:   }
692:   graph->has_dirichlet = PETSC_FALSE;
693:   if (dirichlet_is) {
696:     graph->has_dirichlet = PETSC_TRUE;
697:   }
699:   for (i=0;i<n_ISForDofs;i++) {
702:   }
703:   if (custom_primal_vertices) {
706:   }
707:   PetscObjectGetComm((PetscObject)(graph->l2gmap),&comm);
708:   MPI_Comm_size(comm,&commsize);

710:   /* custom_minimal_size */
711:   graph->custom_minimal_size = custom_minimal_size;
712:   /* get info l2gmap and allocate work vectors  */
713:   ISLocalToGlobalMappingGetInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);
714:   /* check if we have any local periodic nodes (periodic BCs) */
715:   mirrors_found = PETSC_FALSE;
716:   if (graph->nvtxs && n_neigh) {
717:     for (i=0; i<n_shared[0]; i++) graph->count[shared[0][i]] += 1;
718:     for (i=0; i<n_shared[0]; i++) {
719:       if (graph->count[shared[0][i]] > 1) {
720:         mirrors_found = PETSC_TRUE;
721:         break;
722:       }
723:     }
724:   }
725:   /* compute local mirrors (if any) */
726:   if (mirrors_found) {
727:     IS       to,from;
728:     PetscInt *local_indices,*global_indices;

730:     ISCreateStride(PETSC_COMM_SELF,graph->nvtxs,0,1,&to);
731:     ISLocalToGlobalMappingApplyIS(graph->l2gmap,to,&from);
732:     /* get arrays of local and global indices */
733:     PetscMalloc1(graph->nvtxs,&local_indices);
734:     ISGetIndices(to,(const PetscInt**)&is_indices);
735:     PetscMemcpy(local_indices,is_indices,graph->nvtxs*sizeof(PetscInt));
736:     ISRestoreIndices(to,(const PetscInt**)&is_indices);
737:     PetscMalloc1(graph->nvtxs,&global_indices);
738:     ISGetIndices(from,(const PetscInt**)&is_indices);
739:     PetscMemcpy(global_indices,is_indices,graph->nvtxs*sizeof(PetscInt));
740:     ISRestoreIndices(from,(const PetscInt**)&is_indices);
741:     /* allocate space for mirrors */
742:     PetscMalloc2(graph->nvtxs,&graph->mirrors,graph->nvtxs,&graph->mirrors_set);
743:     PetscMemzero(graph->mirrors,graph->nvtxs*sizeof(PetscInt));
744:     graph->mirrors_set[0] = 0;

746:     k=0;
747:     for (i=0;i<n_shared[0];i++) {
748:       j=shared[0][i];
749:       if (graph->count[j] > 1) {
750:         graph->mirrors[j]++;
751:         k++;
752:       }
753:     }
754:     /* allocate space for set of mirrors */
755:     PetscMalloc1(k,&graph->mirrors_set[0]);
756:     for (i=1;i<graph->nvtxs;i++)
757:       graph->mirrors_set[i]=graph->mirrors_set[i-1]+graph->mirrors[i-1];

759:     /* fill arrays */
760:     PetscMemzero(graph->mirrors,graph->nvtxs*sizeof(PetscInt));
761:     for (j=0;j<n_shared[0];j++) {
762:       i=shared[0][j];
763:       if (graph->count[i] > 1)
764:         graph->mirrors_set[i][graph->mirrors[i]++]=global_indices[i];
765:     }
766:     PetscSortIntWithArray(graph->nvtxs,global_indices,local_indices);
767:     for (i=0;i<graph->nvtxs;i++) {
768:       if (graph->mirrors[i] > 0) {
769:         PetscFindInt(graph->mirrors_set[i][0],graph->nvtxs,global_indices,&k);
770:         j = global_indices[k];
771:         while ( k > 0 && global_indices[k-1] == j) k--;
772:         for (j=0;j<graph->mirrors[i];j++) {
773:           graph->mirrors_set[i][j]=local_indices[k+j];
774:         }
775:         PetscSortInt(graph->mirrors[i],graph->mirrors_set[i]);
776:       }
777:     }
778:     PetscFree(local_indices);
779:     PetscFree(global_indices);
780:     ISDestroy(&to);
781:     ISDestroy(&from);
782:   }
783:   PetscMemzero(graph->count,graph->nvtxs*sizeof(*graph->count));

785:   /* Count total number of neigh per node */
786:   k = 0;
787:   for (i=1;i<n_neigh;i++) {
788:     k += n_shared[i];
789:     for (j=0;j<n_shared[i];j++) {
790:       graph->count[shared[i][j]] += 1;
791:     }
792:   }
793:   /* Allocate space for storing the set of neighbours for each node */
794:   if (graph->nvtxs) {
795:     PetscMalloc1(k,&graph->neighbours_set[0]);
796:   }
797:   for (i=1;i<graph->nvtxs;i++) { /* dont count myself */
798:     graph->neighbours_set[i]=graph->neighbours_set[i-1]+graph->count[i-1];
799:   }
800:   /* Get information for sharing subdomains */
801:   PetscMemzero(graph->count,graph->nvtxs*sizeof(*graph->count));
802:   for (i=1;i<n_neigh;i++) { /* dont count myself */
803:     s = n_shared[i];
804:     for (j=0;j<s;j++) {
805:       k = shared[i][j];
806:       graph->neighbours_set[k][graph->count[k]] = neigh[i];
807:       graph->count[k] += 1;
808:     }
809:   }
810:   /* sort set of sharing subdomains */
811:   for (i=0;i<graph->nvtxs;i++) {
812:     PetscSortRemoveDupsInt(&graph->count[i],graph->neighbours_set[i]);
813:   }
814:   /* free memory allocated by ISLocalToGlobalMappingGetInfo */
815:   ISLocalToGlobalMappingRestoreInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);

817:   /*
818:      Get info for dofs splitting
819:      User can specify just a subset; an additional field is considered as a complementary field
820:   */
821:   for (i=0;i<graph->nvtxs;i++) graph->which_dof[i] = n_ISForDofs; /* by default a dof belongs to the complement set */
822:   for (i=0;i<n_ISForDofs;i++) {
823:     ISGetLocalSize(ISForDofs[i],&is_size);
824:     ISGetIndices(ISForDofs[i],(const PetscInt**)&is_indices);
825:     for (j=0;j<is_size;j++) {
826:       if (is_indices[j] > -1 && is_indices[j] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */
827:         graph->which_dof[is_indices[j]] = i;
828:       }
829:     }
830:     ISRestoreIndices(ISForDofs[i],(const PetscInt**)&is_indices);
831:   }

833:   /* Take into account Neumann nodes */
834:   if (neumann_is) {
835:     ISGetLocalSize(neumann_is,&is_size);
836:     ISGetIndices(neumann_is,(const PetscInt**)&is_indices);
837:     for (i=0;i<is_size;i++) {
838:       if (is_indices[i] > -1 && is_indices[i] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */
839:         graph->special_dof[is_indices[i]] = PCBDDCGRAPH_NEUMANN_MARK;
840:       }
841:     }
842:     ISRestoreIndices(neumann_is,(const PetscInt**)&is_indices);
843:   }
844:   /* Take into account Dirichlet nodes (they overwrite any neumann boundary mark previously set) */
845:   if (dirichlet_is) {
846:     ISGetLocalSize(dirichlet_is,&is_size);
847:     ISGetIndices(dirichlet_is,(const PetscInt**)&is_indices);
848:     for (i=0;i<is_size;i++){
849:       if (is_indices[i] > -1 && is_indices[i] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */
850:         if (commsize > graph->commsizelimit) { /* dirichlet nodes treated as internal */
851:           PetscBTSet(graph->touched,is_indices[i]);
852:           graph->subset[is_indices[i]] = 0;
853:         }
854:         graph->special_dof[is_indices[i]] = PCBDDCGRAPH_DIRICHLET_MARK;
855:       }
856:     }
857:     ISRestoreIndices(dirichlet_is,(const PetscInt**)&is_indices);
858:   }
859:   /* mark local periodic nodes (if any) and adapt CSR graph (if any) */
860:   if (graph->mirrors) {
861:     for (i=0;i<graph->nvtxs;i++)
862:       if (graph->mirrors[i])
863:         graph->special_dof[i] = PCBDDCGRAPH_LOCAL_PERIODIC_MARK;

865:     if (graph->xadj) {
866:       PetscInt *new_xadj,*new_adjncy;
867:       /* sort CSR graph */
868:       for (i=0;i<graph->nvtxs;i++)
869:         PetscSortInt(graph->xadj[i+1]-graph->xadj[i],&graph->adjncy[graph->xadj[i]]);

871:       /* adapt local CSR graph in case of local periodicity */
872:       k = 0;
873:       for (i=0;i<graph->nvtxs;i++)
874:         for (j=graph->xadj[i];j<graph->xadj[i+1];j++)
875:           k += graph->mirrors[graph->adjncy[j]];

877:       PetscMalloc1(graph->nvtxs+1,&new_xadj);
878:       PetscMalloc1(k+graph->xadj[graph->nvtxs],&new_adjncy);
879:       new_xadj[0] = 0;
880:       for (i=0;i<graph->nvtxs;i++) {
881:         k = graph->xadj[i+1]-graph->xadj[i];
882:         PetscMemcpy(&new_adjncy[new_xadj[i]],&graph->adjncy[graph->xadj[i]],k*sizeof(PetscInt));
883:         new_xadj[i+1] = new_xadj[i]+k;
884:         for (j=graph->xadj[i];j<graph->xadj[i+1];j++) {
885:           k = graph->mirrors[graph->adjncy[j]];
886:           PetscMemcpy(&new_adjncy[new_xadj[i+1]],graph->mirrors_set[graph->adjncy[j]],k*sizeof(PetscInt));
887:           new_xadj[i+1] += k;
888:         }
889:         k = new_xadj[i+1]-new_xadj[i];
890:         PetscSortRemoveDupsInt(&k,&new_adjncy[new_xadj[i]]);
891:         new_xadj[i+1] = new_xadj[i]+k;
892:       }
893:       /* set new CSR into graph */
894:       PetscFree(graph->xadj);
895:       PetscFree(graph->adjncy);
896:       graph->xadj = new_xadj;
897:       graph->adjncy = new_adjncy;
898:     }
899:   }

901:   /* mark special nodes (if any) -> each will become a single node equivalence class */
902:   if (custom_primal_vertices) {
903:     ISGetLocalSize(custom_primal_vertices,&is_size);
904:     ISGetIndices(custom_primal_vertices,(const PetscInt**)&is_indices);
905:     for (i=0,j=0;i<is_size;i++){
906:       if (is_indices[i] > -1 && is_indices[i] < graph->nvtxs  && graph->special_dof[is_indices[i]] != PCBDDCGRAPH_DIRICHLET_MARK) { /* out of bounds indices (if any) are skipped */
907:         graph->special_dof[is_indices[i]] = PCBDDCGRAPH_SPECIAL_MARK-j;
908:         j++;
909:       }
910:     }
911:     ISRestoreIndices(custom_primal_vertices,(const PetscInt**)&is_indices);
912:   }

914:   /* mark interior nodes (if commsize > graph->commsizelimit) as touched and belonging to partition number 0 */
915:   if (commsize > graph->commsizelimit) {
916:     for (i=0;i<graph->nvtxs;i++) {
917:       if (!graph->count[i]) {
918:         PetscBTSet(graph->touched,i);
919:         graph->subset[i] = 0;
920:       }
921:     }
922:   }

924:   /* init graph structure and compute default subsets */
925:   nodes_touched = 0;
926:   for (i=0;i<graph->nvtxs;i++) {
927:     if (PetscBTLookup(graph->touched,i)) {
928:       nodes_touched++;
929:     }
930:   }
931:   i = 0;
932:   graph->ncc = 0;
933:   total_counts = 0;

935:   /* allocated space for queues */
936:   if (commsize == graph->commsizelimit) {
937:     PetscMalloc2(graph->nvtxs+1,&graph->cptr,graph->nvtxs,&graph->queue);
938:   } else {
939:     PetscInt nused = graph->nvtxs - nodes_touched;
940:     PetscMalloc2(nused+1,&graph->cptr,nused,&graph->queue);
941:   }

943:   while (nodes_touched<graph->nvtxs) {
944:     /*  find first untouched node in local ordering */
945:     while (PetscBTLookup(graph->touched,i)) i++;
946:     PetscBTSet(graph->touched,i);
947:     graph->subset[i] = graph->ncc+1;
948:     graph->cptr[graph->ncc] = total_counts;
949:     graph->queue[total_counts] = i;
950:     total_counts++;
951:     nodes_touched++;
952:     /* now find all other nodes having the same set of sharing subdomains */
953:     for (j=i+1;j<graph->nvtxs;j++) {
954:       /* check for same number of sharing subdomains, dof number and same special mark */
955:       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]) {
956:         /* check for same set of sharing subdomains */
957:         same_set = PETSC_TRUE;
958:         for (k=0;k<graph->count[j];k++){
959:           if (graph->neighbours_set[i][k] != graph->neighbours_set[j][k]) {
960:             same_set = PETSC_FALSE;
961:           }
962:         }
963:         /* I found a friend of mine */
964:         if (same_set) {
965:           PetscBTSet(graph->touched,j);
966:           graph->subset[j] = graph->ncc+1;
967:           nodes_touched++;
968:           graph->queue[total_counts] = j;
969:           total_counts++;
970:         }
971:       }
972:     }
973:     graph->ncc++;
974:   }
975:   /* 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 */
976:   graph->n_subsets = graph->ncc;
977:   PetscMalloc1(graph->n_subsets,&graph->subset_ncc);
978:   for (i=0;i<graph->n_subsets;i++) {
979:     graph->subset_ncc[i] = 1;
980:   }
981:   /* final pointer */
982:   graph->cptr[graph->ncc] = total_counts;

984:   /* For consistency reasons (among neighbours), I need to sort (by global ordering) each connected component */
985:   /* Get a reference node (min index in global ordering) for each subset for tagging messages */
986:   PetscMalloc1(graph->ncc,&graph->subset_ref_node);
987:   PetscMalloc1(graph->cptr[graph->ncc],&queue_global);
988:   ISLocalToGlobalMappingApply(graph->l2gmap,graph->cptr[graph->ncc],graph->queue,queue_global);
989:   for (j=0;j<graph->ncc;j++) {
990:     PetscSortIntWithArray(graph->cptr[j+1]-graph->cptr[j],&queue_global[graph->cptr[j]],&graph->queue[graph->cptr[j]]);
991:     graph->subset_ref_node[j] = graph->queue[graph->cptr[j]];
992:   }
993:   PetscFree(queue_global);
994:   graph->queue_sorted = PETSC_TRUE;

996:   /* save information on subsets (needed when analyzing the connected components) */
997:   if (graph->ncc) {
998:     PetscMalloc2(graph->ncc,&graph->subset_size,graph->ncc,&graph->subset_idxs);
999:     PetscMalloc1(graph->cptr[graph->ncc],&graph->subset_idxs[0]);
1000:     PetscMemzero(graph->subset_idxs[0],graph->cptr[graph->ncc]*sizeof(PetscInt));
1001:     for (j=1;j<graph->ncc;j++) {
1002:       graph->subset_size[j-1] = graph->cptr[j] - graph->cptr[j-1];
1003:       graph->subset_idxs[j] = graph->subset_idxs[j-1] + graph->subset_size[j-1];
1004:     }
1005:     graph->subset_size[graph->ncc-1] = graph->cptr[graph->ncc] - graph->cptr[graph->ncc-1];
1006:     PetscMemcpy(graph->subset_idxs[0],graph->queue,graph->cptr[graph->ncc]*sizeof(PetscInt));
1007:   }

1009:   /* renumber reference nodes */
1010:   ISCreateGeneral(PetscObjectComm((PetscObject)(graph->l2gmap)),graph->ncc,graph->subset_ref_node,PETSC_COPY_VALUES,&subset_n);
1011:   ISLocalToGlobalMappingApplyIS(graph->l2gmap,subset_n,&subset);
1012:   ISDestroy(&subset_n);
1013:   ISRenumber(subset,NULL,NULL,&subset_n);
1014:   ISDestroy(&subset);
1015:   ISGetLocalSize(subset_n,&k);
1016:   if (k != graph->ncc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid size of new subset! %D != %D",k,graph->ncc);
1017:   ISGetIndices(subset_n,&is_indices);
1018:   PetscMemcpy(graph->subset_ref_node,is_indices,graph->ncc*sizeof(PetscInt));
1019:   ISRestoreIndices(subset_n,&is_indices);
1020:   ISDestroy(&subset_n);

1022:   /* free workspace */
1023:   graph->setupcalled = PETSC_TRUE;
1024:   return(0);
1025: }

1027: PetscErrorCode PCBDDCGraphResetCSR(PCBDDCGraph graph)
1028: {

1032:   if (!graph) return(0);
1033:   if (graph->freecsr) {
1034:     PetscFree(graph->xadj);
1035:     PetscFree(graph->adjncy);
1036:   } else {
1037:     graph->xadj = NULL;
1038:     graph->adjncy = NULL;
1039:   }
1040:   graph->freecsr = PETSC_FALSE;
1041:   graph->nvtxs_csr = 0;
1042:   return(0);
1043: }

1045: PetscErrorCode PCBDDCGraphReset(PCBDDCGraph graph)
1046: {

1050:   if (!graph) return(0);
1051:   ISLocalToGlobalMappingDestroy(&graph->l2gmap);
1052:   PetscFree(graph->subset_ncc);
1053:   PetscFree(graph->subset_ref_node);
1054:   if (graph->nvtxs) {
1055:     PetscFree(graph->neighbours_set[0]);
1056:   }
1057:   PetscBTDestroy(&graph->touched);
1058:   PetscFree5(graph->count,
1059:                     graph->neighbours_set,
1060:                     graph->subset,
1061:                     graph->which_dof,
1062:                     graph->special_dof);
1063:   PetscFree2(graph->cptr,graph->queue);
1064:   if (graph->mirrors) {
1065:     PetscFree(graph->mirrors_set[0]);
1066:   }
1067:   PetscFree2(graph->mirrors,graph->mirrors_set);
1068:   if (graph->subset_idxs) {
1069:     PetscFree(graph->subset_idxs[0]);
1070:   }
1071:   PetscFree2(graph->subset_size,graph->subset_idxs);
1072:   ISDestroy(&graph->dirdofs);
1073:   ISDestroy(&graph->dirdofsB);
1074:   if (graph->n_local_subs) {
1075:     PetscFree(graph->local_subs);
1076:   }
1077:   graph->has_dirichlet       = PETSC_FALSE;
1078:   graph->twodimset           = PETSC_FALSE;
1079:   graph->twodim              = PETSC_FALSE;
1080:   graph->nvtxs               = 0;
1081:   graph->nvtxs_global        = 0;
1082:   graph->n_subsets           = 0;
1083:   graph->custom_minimal_size = 1;
1084:   graph->n_local_subs        = 0;
1085:   graph->maxcount            = PETSC_MAX_INT;
1086:   graph->setupcalled         = PETSC_FALSE;
1087:   return(0);
1088: }

1090: PetscErrorCode PCBDDCGraphInit(PCBDDCGraph graph, ISLocalToGlobalMapping l2gmap, PetscInt N, PetscInt maxcount)
1091: {
1092:   PetscInt       n;

1100:   /* raise an error if already allocated */
1101:   if (graph->nvtxs_global) SETERRQ(PetscObjectComm((PetscObject)l2gmap),PETSC_ERR_PLIB,"BDDCGraph already initialized");
1102:   /* set number of vertices */
1103:   PetscObjectReference((PetscObject)l2gmap);
1104:   graph->l2gmap = l2gmap;
1105:   ISLocalToGlobalMappingGetSize(l2gmap,&n);
1106:   graph->nvtxs = n;
1107:   graph->nvtxs_global = N;
1108:   /* allocate used space */
1109:   PetscBTCreate(graph->nvtxs,&graph->touched);
1110:   PetscMalloc5(graph->nvtxs,&graph->count,
1111:                       graph->nvtxs,&graph->neighbours_set,
1112:                       graph->nvtxs,&graph->subset,
1113:                       graph->nvtxs,&graph->which_dof,
1114:                       graph->nvtxs,&graph->special_dof);
1115:   /* zeroes memory */
1116:   PetscMemzero(graph->count,graph->nvtxs*sizeof(PetscInt));
1117:   PetscMemzero(graph->subset,graph->nvtxs*sizeof(PetscInt));
1118:   /* use -1 as a default value for which_dof array */
1119:   for (n=0;n<graph->nvtxs;n++) graph->which_dof[n] = -1;
1120:   PetscMemzero(graph->special_dof,graph->nvtxs*sizeof(PetscInt));
1121:   /* zeroes first pointer to neighbour set */
1122:   if (graph->nvtxs) {
1123:     graph->neighbours_set[0] = 0;
1124:   }
1125:   /* zeroes workspace for values of ncc */
1126:   graph->subset_ncc = 0;
1127:   graph->subset_ref_node = 0;
1128:   /* maxcount for cc */
1129:   graph->maxcount = maxcount;
1130:   return(0);
1131: }

1133: PetscErrorCode PCBDDCGraphDestroy(PCBDDCGraph* graph)
1134: {

1138:   PCBDDCGraphResetCSR(*graph);
1139:   PCBDDCGraphReset(*graph);
1140:   PetscFree(*graph);
1141:   return(0);
1142: }

1144: PetscErrorCode PCBDDCGraphCreate(PCBDDCGraph *graph)
1145: {
1146:   PCBDDCGraph    new_graph;

1150:   PetscNew(&new_graph);
1151:   new_graph->custom_minimal_size = 1;
1152:   new_graph->commsizelimit = 1;
1153:   *graph = new_graph;
1154:   return(0);
1155: }