Date: 2022-03-04T17:38:56+00:00

Actual source code: bddcprivate.c

      1: #include <../src/mat/impls/aij/seq/aij.h>
      2: #include <../src/ksp/pc/impls/bddc/bddc.h>
      3: #include <../src/ksp/pc/impls/bddc/bddcprivate.h>
      4: #include <../src/mat/impls/dense/seq/dense.h>
      5: #include <petscdmplex.h>
      6: #include <petscblaslapack.h>
      7: #include <petsc/private/sfimpl.h>
      8: #include <petsc/private/dmpleximpl.h>
      9: #include <petscdmda.h>

     11: static PetscErrorCode MatMPIAIJRestrict(Mat,MPI_Comm,Mat*);

     13: /* if range is true,  it returns B s.t. span{B} = range(A)
     14:    if range is false, it returns B s.t. range(B) _|_ range(A) */
     15: PetscErrorCode MatDenseOrthogonalRangeOrComplement(Mat A, PetscBool range, PetscInt lw, PetscScalar *work, PetscReal *rwork, Mat *B)
     16: {
     17:   PetscScalar    *uwork,*data,*U, ds = 0.;
     18:   PetscReal      *sing;
     19:   PetscBLASInt   bM,bN,lwork,lierr,di = 1;
     20:   PetscInt       ulw,i,nr,nc,n;
     22: #if defined(PETSC_USE_COMPLEX)
     23:   PetscReal      *rwork2;
     24: #endif

     27:   MatGetSize(A,&nr,&nc);
     28:   if (!nr || !nc) return(0);

     30:   /* workspace */
     31:   if (!work) {
     32:     ulw  = PetscMax(PetscMax(1,5*PetscMin(nr,nc)),3*PetscMin(nr,nc)+PetscMax(nr,nc));
     33:     PetscMalloc1(ulw,&uwork);
     34:   } else {
     35:     ulw   = lw;
     36:     uwork = work;
     37:   }
     38:   n = PetscMin(nr,nc);
     39:   if (!rwork) {
     40:     PetscMalloc1(n,&sing);
     41:   } else {
     42:     sing = rwork;
     43:   }

     45:   /* SVD */
     46:   PetscMalloc1(nr*nr,&U);
     47:   PetscBLASIntCast(nr,&bM);
     48:   PetscBLASIntCast(nc,&bN);
     49:   PetscBLASIntCast(ulw,&lwork);
     50:   MatDenseGetArray(A,&data);
     51:   PetscFPTrapPush(PETSC_FP_TRAP_OFF);
     52: #if !defined(PETSC_USE_COMPLEX)
     53:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("A","N",&bM,&bN,data,&bM,sing,U,&bM,&ds,&di,uwork,&lwork,&lierr));
     54: #else
     55:   PetscMalloc1(5*n,&rwork2);
     56:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("A","N",&bM,&bN,data,&bM,sing,U,&bM,&ds,&di,uwork,&lwork,rwork2,&lierr));
     57:   PetscFree(rwork2);
     58: #endif
     59:   PetscFPTrapPop();
     60:   if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in GESVD Lapack routine %d",(int)lierr);
     61:   MatDenseRestoreArray(A,&data);
     62:   for (i=0;i<n;i++) if (sing[i] < PETSC_SMALL) break;
     63:   if (!rwork) {
     64:     PetscFree(sing);
     65:   }
     66:   if (!work) {
     67:     PetscFree(uwork);
     68:   }
     69:   /* create B */
     70:   if (!range) {
     71:     MatCreateSeqDense(PETSC_COMM_SELF,nr,nr-i,NULL,B);
     72:     MatDenseGetArray(*B,&data);
     73:     PetscArraycpy(data,U+nr*i,(nr-i)*nr);
     74:   } else {
     75:     MatCreateSeqDense(PETSC_COMM_SELF,nr,i,NULL,B);
     76:     MatDenseGetArray(*B,&data);
     77:     PetscArraycpy(data,U,i*nr);
     78:   }
     79:   MatDenseRestoreArray(*B,&data);
     80:   PetscFree(U);
     81:   return(0);
     82: }

     84: /* TODO REMOVE */
     85: #if defined(PRINT_GDET)
     86: static int inc = 0;
     87: static int lev = 0;
     88: #endif

     90: PetscErrorCode PCBDDCComputeNedelecChangeEdge(Mat lG, IS edge, IS extrow, IS extcol, IS corners, Mat* Gins, Mat* GKins, PetscScalar cvals[2], PetscScalar *work, PetscReal *rwork)
     91: {
     93:   Mat            GE,GEd;
     94:   PetscInt       rsize,csize,esize;
     95:   PetscScalar    *ptr;

     98:   ISGetSize(edge,&esize);
     99:   if (!esize) return(0);
    100:   ISGetSize(extrow,&rsize);
    101:   ISGetSize(extcol,&csize);

    103:   /* gradients */
    104:   ptr  = work + 5*esize;
    105:   MatCreateSubMatrix(lG,extrow,extcol,MAT_INITIAL_MATRIX,&GE);
    106:   MatCreateSeqDense(PETSC_COMM_SELF,rsize,csize,ptr,Gins);
    107:   MatConvert(GE,MATSEQDENSE,MAT_REUSE_MATRIX,Gins);
    108:   MatDestroy(&GE);

    110:   /* constants */
    111:   ptr += rsize*csize;
    112:   MatCreateSeqDense(PETSC_COMM_SELF,esize,csize,ptr,&GEd);
    113:   MatCreateSubMatrix(lG,edge,extcol,MAT_INITIAL_MATRIX,&GE);
    114:   MatConvert(GE,MATSEQDENSE,MAT_REUSE_MATRIX,&GEd);
    115:   MatDestroy(&GE);
    116:   MatDenseOrthogonalRangeOrComplement(GEd,PETSC_FALSE,5*esize,work,rwork,GKins);
    117:   MatDestroy(&GEd);

    119:   if (corners) {
    120:     Mat               GEc;
    121:     const PetscScalar *vals;
    122:     PetscScalar       v;

    124:     MatCreateSubMatrix(lG,edge,corners,MAT_INITIAL_MATRIX,&GEc);
    125:     MatTransposeMatMult(GEc,*GKins,MAT_INITIAL_MATRIX,1.0,&GEd);
    126:     MatDenseGetArrayRead(GEd,&vals);
    127:     /* v    = PetscAbsScalar(vals[0]) */;
    128:     v    = 1.;
    129:     cvals[0] = vals[0]/v;
    130:     cvals[1] = vals[1]/v;
    131:     MatDenseRestoreArrayRead(GEd,&vals);
    132:     MatScale(*GKins,1./v);
    133: #if defined(PRINT_GDET)
    134:     {
    135:       PetscViewer viewer;
    136:       char filename[256];
    137:       sprintf(filename,"Gdet_l%d_r%d_cc%d.m",lev,PetscGlobalRank,inc++);
    138:       PetscViewerASCIIOpen(PETSC_COMM_SELF,filename,&viewer);
    139:       PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
    140:       PetscObjectSetName((PetscObject)GEc,"GEc");
    141:       MatView(GEc,viewer);
    142:       PetscObjectSetName((PetscObject)(*GKins),"GK");
    143:       MatView(*GKins,viewer);
    144:       PetscObjectSetName((PetscObject)GEd,"Gproj");
    145:       MatView(GEd,viewer);
    146:       PetscViewerDestroy(&viewer);
    147:     }
    148: #endif
    149:     MatDestroy(&GEd);
    150:     MatDestroy(&GEc);
    151:   }

    153:   return(0);
    154: }

    156: PetscErrorCode PCBDDCNedelecSupport(PC pc)
    157: {
    158:   PC_BDDC                *pcbddc = (PC_BDDC*)pc->data;
    159:   Mat_IS                 *matis = (Mat_IS*)pc->pmat->data;
    160:   Mat                    G,T,conn,lG,lGt,lGis,lGall,lGe,lGinit;
    161:   Vec                    tvec;
    162:   PetscSF                sfv;
    163:   ISLocalToGlobalMapping el2g,vl2g,fl2g,al2g;
    164:   MPI_Comm               comm;
    165:   IS                     lned,primals,allprimals,nedfieldlocal;
    166:   IS                     *eedges,*extrows,*extcols,*alleedges;
    167:   PetscBT                btv,bte,btvc,btb,btbd,btvcand,btvi,btee,bter;
    168:   PetscScalar            *vals,*work;
    169:   PetscReal              *rwork;
    170:   const PetscInt         *idxs,*ii,*jj,*iit,*jjt;
    171:   PetscInt               ne,nv,Lv,order,n,field;
    172:   PetscInt               n_neigh,*neigh,*n_shared,**shared;
    173:   PetscInt               i,j,extmem,cum,maxsize,nee;
    174:   PetscInt               *extrow,*extrowcum,*marks,*vmarks,*gidxs;
    175:   PetscInt               *sfvleaves,*sfvroots;
    176:   PetscInt               *corners,*cedges;
    177:   PetscInt               *ecount,**eneighs,*vcount,**vneighs;
    178:   PetscInt               *emarks;
    179:   PetscBool              print,eerr,done,lrc[2],conforming,global,singular,setprimal;
    180:   PetscErrorCode         ierr;

    183:   /* If the discrete gradient is defined for a subset of dofs and global is true,
    184:      it assumes G is given in global ordering for all the dofs.
    185:      Otherwise, the ordering is global for the Nedelec field */
    186:   order      = pcbddc->nedorder;
    187:   conforming = pcbddc->conforming;
    188:   field      = pcbddc->nedfield;
    189:   global     = pcbddc->nedglobal;
    190:   setprimal  = PETSC_FALSE;
    191:   print      = PETSC_FALSE;
    192:   singular   = PETSC_FALSE;

    194:   /* Command line customization */
    195:   PetscOptionsBegin(PetscObjectComm((PetscObject)pc),((PetscObject)pc)->prefix,"BDDC Nedelec options","PC");
    196:   PetscOptionsBool("-pc_bddc_nedelec_field_primal","All edge dofs set as primals: Toselli's algorithm C",NULL,setprimal,&setprimal,NULL);
    197:   PetscOptionsBool("-pc_bddc_nedelec_singular","Infer nullspace from discrete gradient",NULL,singular,&singular,NULL);
    198:   PetscOptionsInt("-pc_bddc_nedelec_order","Test variable order code (to be removed)",NULL,order,&order,NULL);
    199:   /* print debug info TODO: to be removed */
    200:   PetscOptionsBool("-pc_bddc_nedelec_print","Print debug info",NULL,print,&print,NULL);
    201:   PetscOptionsEnd();

    203:   /* Return if there are no edges in the decomposition and the problem is not singular */
    204:   MatGetLocalToGlobalMapping(pc->pmat,&al2g,NULL);
    205:   ISLocalToGlobalMappingGetSize(al2g,&n);
    206:   PetscObjectGetComm((PetscObject)pc,&comm);
    207:   if (!singular) {
    208:     VecGetArrayRead(matis->counter,(const PetscScalar**)&vals);
    209:     lrc[0] = PETSC_FALSE;
    210:     for (i=0;i<n;i++) {
    211:       if (PetscRealPart(vals[i]) > 2.) {
    212:         lrc[0] = PETSC_TRUE;
    213:         break;
    214:       }
    215:     }
    216:     VecRestoreArrayRead(matis->counter,(const PetscScalar**)&vals);
    217:     MPIU_Allreduce(&lrc[0],&lrc[1],1,MPIU_BOOL,MPI_LOR,comm);
    218:     if (!lrc[1]) return(0);
    219:   }

    221:   /* Get Nedelec field */
    222:   if (pcbddc->n_ISForDofsLocal && field >= pcbddc->n_ISForDofsLocal) SETERRQ2(comm,PETSC_ERR_USER,"Invalid field for Nedelec %D: number of fields is %D",field,pcbddc->n_ISForDofsLocal);
    223:   if (pcbddc->n_ISForDofsLocal && field >= 0) {
    224:     PetscObjectReference((PetscObject)pcbddc->ISForDofsLocal[field]);
    225:     nedfieldlocal = pcbddc->ISForDofsLocal[field];
    226:     ISGetLocalSize(nedfieldlocal,&ne);
    227:   } else if (!pcbddc->n_ISForDofsLocal && field != PETSC_DECIDE) {
    228:     ne            = n;
    229:     nedfieldlocal = NULL;
    230:     global        = PETSC_TRUE;
    231:   } else if (field == PETSC_DECIDE) {
    232:     PetscInt rst,ren,*idx;

    234:     PetscArrayzero(matis->sf_leafdata,n);
    235:     PetscArrayzero(matis->sf_rootdata,pc->pmat->rmap->n);
    236:     MatGetOwnershipRange(pcbddc->discretegradient,&rst,&ren);
    237:     for (i=rst;i<ren;i++) {
    238:       PetscInt nc;

    240:       MatGetRow(pcbddc->discretegradient,i,&nc,NULL,NULL);
    241:       if (nc > 1) matis->sf_rootdata[i-rst] = 1;
    242:       MatRestoreRow(pcbddc->discretegradient,i,&nc,NULL,NULL);
    243:     }
    244:     PetscSFBcastBegin(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata,MPI_REPLACE);
    245:     PetscSFBcastEnd(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata,MPI_REPLACE);
    246:     PetscMalloc1(n,&idx);
    247:     for (i=0,ne=0;i<n;i++) if (matis->sf_leafdata[i]) idx[ne++] = i;
    248:     ISCreateGeneral(comm,ne,idx,PETSC_OWN_POINTER,&nedfieldlocal);
    249:   } else {
    250:     SETERRQ(comm,PETSC_ERR_USER,"When multiple fields are present, the Nedelec field has to be specified");
    251:   }

    253:   /* Sanity checks */
    254:   if (!order && !conforming) SETERRQ(comm,PETSC_ERR_SUP,"Variable order and non-conforming spaces are not supported at the same time");
    255:   if (pcbddc->user_ChangeOfBasisMatrix) SETERRQ(comm,PETSC_ERR_SUP,"Cannot generate Nedelec support with user defined change of basis");
    256:   if (order && ne%order) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"The number of local edge dofs %D it's not a multiple of the order %D",ne,order);

    258:   /* Just set primal dofs and return */
    259:   if (setprimal) {
    260:     IS       enedfieldlocal;
    261:     PetscInt *eidxs;

    263:     PetscMalloc1(ne,&eidxs);
    264:     VecGetArrayRead(matis->counter,(const PetscScalar**)&vals);
    265:     if (nedfieldlocal) {
    266:       ISGetIndices(nedfieldlocal,&idxs);
    267:       for (i=0,cum=0;i<ne;i++) {
    268:         if (PetscRealPart(vals[idxs[i]]) > 2.) {
    269:           eidxs[cum++] = idxs[i];
    270:         }
    271:       }
    272:       ISRestoreIndices(nedfieldlocal,&idxs);
    273:     } else {
    274:       for (i=0,cum=0;i<ne;i++) {
    275:         if (PetscRealPart(vals[i]) > 2.) {
    276:           eidxs[cum++] = i;
    277:         }
    278:       }
    279:     }
    280:     VecRestoreArrayRead(matis->counter,(const PetscScalar**)&vals);
    281:     ISCreateGeneral(comm,cum,eidxs,PETSC_COPY_VALUES,&enedfieldlocal);
    282:     PCBDDCSetPrimalVerticesLocalIS(pc,enedfieldlocal);
    283:     PetscFree(eidxs);
    284:     ISDestroy(&nedfieldlocal);
    285:     ISDestroy(&enedfieldlocal);
    286:     return(0);
    287:   }

    289:   /* Compute some l2g maps */
    290:   if (nedfieldlocal) {
    291:     IS is;

    293:     /* need to map from the local Nedelec field to local numbering */
    294:     ISLocalToGlobalMappingCreateIS(nedfieldlocal,&fl2g);
    295:     /* need to map from the local Nedelec field to global numbering for the whole dofs*/
    296:     ISLocalToGlobalMappingApplyIS(al2g,nedfieldlocal,&is);
    297:     ISLocalToGlobalMappingCreateIS(is,&al2g);
    298:     /* need to map from the local Nedelec field to global numbering (for Nedelec only) */
    299:     if (global) {
    300:       PetscObjectReference((PetscObject)al2g);
    301:       el2g = al2g;
    302:     } else {
    303:       IS gis;

    305:       ISRenumber(is,NULL,NULL,&gis);
    306:       ISLocalToGlobalMappingCreateIS(gis,&el2g);
    307:       ISDestroy(&gis);
    308:     }
    309:     ISDestroy(&is);
    310:   } else {
    311:     /* restore default */
    312:     pcbddc->nedfield = -1;
    313:     /* one ref for the destruction of al2g, one for el2g */
    314:     PetscObjectReference((PetscObject)al2g);
    315:     PetscObjectReference((PetscObject)al2g);
    316:     el2g = al2g;
    317:     fl2g = NULL;
    318:   }

    320:   /* Start communication to drop connections for interior edges (for cc analysis only) */
    321:   PetscArrayzero(matis->sf_leafdata,n);
    322:   PetscArrayzero(matis->sf_rootdata,pc->pmat->rmap->n);
    323:   if (nedfieldlocal) {
    324:     ISGetIndices(nedfieldlocal,&idxs);
    325:     for (i=0;i<ne;i++) matis->sf_leafdata[idxs[i]] = 1;
    326:     ISRestoreIndices(nedfieldlocal,&idxs);
    327:   } else {
    328:     for (i=0;i<ne;i++) matis->sf_leafdata[i] = 1;
    329:   }
    330:   PetscSFReduceBegin(matis->sf,MPIU_INT,matis->sf_leafdata,matis->sf_rootdata,MPI_SUM);
    331:   PetscSFReduceEnd(matis->sf,MPIU_INT,matis->sf_leafdata,matis->sf_rootdata,MPI_SUM);

    333:   if (!singular) { /* drop connections with interior edges to avoid unneeded communications and memory movements */
    334:     MatDuplicate(pcbddc->discretegradient,MAT_COPY_VALUES,&G);
    335:     MatSetOption(G,MAT_KEEP_NONZERO_PATTERN,PETSC_FALSE);
    336:     if (global) {
    337:       PetscInt rst;

    339:       MatGetOwnershipRange(G,&rst,NULL);
    340:       for (i=0,cum=0;i<pc->pmat->rmap->n;i++) {
    341:         if (matis->sf_rootdata[i] < 2) {
    342:           matis->sf_rootdata[cum++] = i + rst;
    343:         }
    344:       }
    345:       MatSetOption(G,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE);
    346:       MatZeroRows(G,cum,matis->sf_rootdata,0.,NULL,NULL);
    347:     } else {
    348:       PetscInt *tbz;

    350:       PetscMalloc1(ne,&tbz);
    351:       PetscSFBcastBegin(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata,MPI_REPLACE);
    352:       PetscSFBcastEnd(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata,MPI_REPLACE);
    353:       ISGetIndices(nedfieldlocal,&idxs);
    354:       for (i=0,cum=0;i<ne;i++)
    355:         if (matis->sf_leafdata[idxs[i]] == 1)
    356:           tbz[cum++] = i;
    357:       ISRestoreIndices(nedfieldlocal,&idxs);
    358:       ISLocalToGlobalMappingApply(el2g,cum,tbz,tbz);
    359:       MatZeroRows(G,cum,tbz,0.,NULL,NULL);
    360:       PetscFree(tbz);
    361:     }
    362:   } else { /* we need the entire G to infer the nullspace */
    363:     PetscObjectReference((PetscObject)pcbddc->discretegradient);
    364:     G    = pcbddc->discretegradient;
    365:   }

    367:   /* Extract subdomain relevant rows of G */
    368:   ISLocalToGlobalMappingGetIndices(el2g,&idxs);
    369:   ISCreateGeneral(comm,ne,idxs,PETSC_USE_POINTER,&lned);
    370:   MatCreateSubMatrix(G,lned,NULL,MAT_INITIAL_MATRIX,&lGall);
    371:   ISLocalToGlobalMappingRestoreIndices(el2g,&idxs);
    372:   ISDestroy(&lned);
    373:   MatConvert(lGall,MATIS,MAT_INITIAL_MATRIX,&lGis);
    374:   MatDestroy(&lGall);
    375:   MatISGetLocalMat(lGis,&lG);

    377:   /* SF for nodal dofs communications */
    378:   MatGetLocalSize(G,NULL,&Lv);
    379:   MatGetLocalToGlobalMapping(lGis,NULL,&vl2g);
    380:   PetscObjectReference((PetscObject)vl2g);
    381:   ISLocalToGlobalMappingGetSize(vl2g,&nv);
    382:   PetscSFCreate(comm,&sfv);
    383:   ISLocalToGlobalMappingGetIndices(vl2g,&idxs);
    384:   PetscSFSetGraphLayout(sfv,lGis->cmap,nv,NULL,PETSC_OWN_POINTER,idxs);
    385:   ISLocalToGlobalMappingRestoreIndices(vl2g,&idxs);
    386:   i    = singular ? 2 : 1;
    387:   PetscMalloc2(i*nv,&sfvleaves,i*Lv,&sfvroots);

    389:   /* Destroy temporary G created in MATIS format and modified G */
    390:   PetscObjectReference((PetscObject)lG);
    391:   MatDestroy(&lGis);
    392:   MatDestroy(&G);

    394:   if (print) {
    395:     PetscObjectSetName((PetscObject)lG,"initial_lG");
    396:     MatView(lG,NULL);
    397:   }

    399:   /* Save lG for values insertion in change of basis */
    400:   MatDuplicate(lG,MAT_COPY_VALUES,&lGinit);

    402:   /* Analyze the edge-nodes connections (duplicate lG) */
    403:   MatDuplicate(lG,MAT_COPY_VALUES,&lGe);
    404:   MatSetOption(lGe,MAT_KEEP_NONZERO_PATTERN,PETSC_FALSE);
    405:   PetscBTCreate(nv,&btv);
    406:   PetscBTCreate(ne,&bte);
    407:   PetscBTCreate(ne,&btb);
    408:   PetscBTCreate(ne,&btbd);
    409:   PetscBTCreate(nv,&btvcand);
    410:   /* need to import the boundary specification to ensure the
    411:      proper detection of coarse edges' endpoints */
    412:   if (pcbddc->DirichletBoundariesLocal) {
    413:     IS is;

    415:     if (fl2g) {
    416:       ISGlobalToLocalMappingApplyIS(fl2g,IS_GTOLM_MASK,pcbddc->DirichletBoundariesLocal,&is);
    417:     } else {
    418:       is = pcbddc->DirichletBoundariesLocal;
    419:     }
    420:     ISGetLocalSize(is,&cum);
    421:     ISGetIndices(is,&idxs);
    422:     for (i=0;i<cum;i++) {
    423:       if (idxs[i] >= 0) {
    424:         PetscBTSet(btb,idxs[i]);
    425:         PetscBTSet(btbd,idxs[i]);
    426:       }
    427:     }
    428:     ISRestoreIndices(is,&idxs);
    429:     if (fl2g) {
    430:       ISDestroy(&is);
    431:     }
    432:   }
    433:   if (pcbddc->NeumannBoundariesLocal) {
    434:     IS is;

    436:     if (fl2g) {
    437:       ISGlobalToLocalMappingApplyIS(fl2g,IS_GTOLM_MASK,pcbddc->NeumannBoundariesLocal,&is);
    438:     } else {
    439:       is = pcbddc->NeumannBoundariesLocal;
    440:     }
    441:     ISGetLocalSize(is,&cum);
    442:     ISGetIndices(is,&idxs);
    443:     for (i=0;i<cum;i++) {
    444:       if (idxs[i] >= 0) {
    445:         PetscBTSet(btb,idxs[i]);
    446:       }
    447:     }
    448:     ISRestoreIndices(is,&idxs);
    449:     if (fl2g) {
    450:       ISDestroy(&is);
    451:     }
    452:   }

    454:   /* Count neighs per dof */
    455:   ISLocalToGlobalMappingGetNodeInfo(el2g,NULL,&ecount,&eneighs);
    456:   ISLocalToGlobalMappingGetNodeInfo(vl2g,NULL,&vcount,&vneighs);

    458:   /* need to remove coarse faces' dofs and coarse edges' dirichlet dofs
    459:      for proper detection of coarse edges' endpoints */
    460:   PetscBTCreate(ne,&btee);
    461:   for (i=0;i<ne;i++) {
    462:     if ((ecount[i] > 2 && !PetscBTLookup(btbd,i)) || (ecount[i] == 2 && PetscBTLookup(btb,i))) {
    463:       PetscBTSet(btee,i);
    464:     }
    465:   }
    466:   PetscMalloc1(ne,&marks);
    467:   if (!conforming) {
    468:     MatTranspose(lGe,MAT_INITIAL_MATRIX,&lGt);
    469:     MatGetRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&iit,&jjt,&done);
    470:   }
    471:   MatGetRowIJ(lGe,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    472:   MatSeqAIJGetArray(lGe,&vals);
    473:   cum  = 0;
    474:   for (i=0;i<ne;i++) {
    475:     /* eliminate rows corresponding to edge dofs belonging to coarse faces */
    476:     if (!PetscBTLookup(btee,i)) {
    477:       marks[cum++] = i;
    478:       continue;
    479:     }
    480:     /* set badly connected edge dofs as primal */
    481:     if (!conforming) {
    482:       if (ii[i+1]-ii[i] != order + 1) { /* every row of G on the coarse edge should list order+1 nodal dofs */
    483:         marks[cum++] = i;
    484:         PetscBTSet(bte,i);
    485:         for (j=ii[i];j<ii[i+1];j++) {
    486:           PetscBTSet(btv,jj[j]);
    487:         }
    488:       } else {
    489:         /* every edge dofs should be connected trough a certain number of nodal dofs
    490:            to other edge dofs belonging to coarse edges
    491:            - at most 2 endpoints
    492:            - order-1 interior nodal dofs
    493:            - no undefined nodal dofs (nconn < order)
    494:         */
    495:         PetscInt ends = 0,ints = 0, undef = 0;
    496:         for (j=ii[i];j<ii[i+1];j++) {
    497:           PetscInt v = jj[j],k;
    498:           PetscInt nconn = iit[v+1]-iit[v];
    499:           for (k=iit[v];k<iit[v+1];k++) if (!PetscBTLookup(btee,jjt[k])) nconn--;
    500:           if (nconn > order) ends++;
    501:           else if (nconn == order) ints++;
    502:           else undef++;
    503:         }
    504:         if (undef || ends > 2 || ints != order -1) {
    505:           marks[cum++] = i;
    506:           PetscBTSet(bte,i);
    507:           for (j=ii[i];j<ii[i+1];j++) {
    508:             PetscBTSet(btv,jj[j]);
    509:           }
    510:         }
    511:       }
    512:     }
    513:     /* We assume the order on the element edge is ii[i+1]-ii[i]-1 */
    514:     if (!order && ii[i+1] != ii[i]) {
    515:       PetscScalar val = 1./(ii[i+1]-ii[i]-1);
    516:       for (j=ii[i];j<ii[i+1];j++) vals[j] = val;
    517:     }
    518:   }
    519:   PetscBTDestroy(&btee);
    520:   MatSeqAIJRestoreArray(lGe,&vals);
    521:   MatRestoreRowIJ(lGe,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    522:   if (!conforming) {
    523:     MatRestoreRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&iit,&jjt,&done);
    524:     MatDestroy(&lGt);
    525:   }
    526:   MatZeroRows(lGe,cum,marks,0.,NULL,NULL);

    528:   /* identify splitpoints and corner candidates */
    529:   MatTranspose(lGe,MAT_INITIAL_MATRIX,&lGt);
    530:   if (print) {
    531:     PetscObjectSetName((PetscObject)lGe,"edgerestr_lG");
    532:     MatView(lGe,NULL);
    533:     PetscObjectSetName((PetscObject)lGt,"edgerestr_lGt");
    534:     MatView(lGt,NULL);
    535:   }
    536:   MatGetRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    537:   MatSeqAIJGetArray(lGt,&vals);
    538:   for (i=0;i<nv;i++) {
    539:     PetscInt  ord = order, test = ii[i+1]-ii[i], vc = vcount[i];
    540:     PetscBool sneighs = PETSC_TRUE, bdir = PETSC_FALSE;
    541:     if (!order) { /* variable order */
    542:       PetscReal vorder = 0.;

    544:       for (j=ii[i];j<ii[i+1];j++) vorder += PetscRealPart(vals[j]);
    545:       test = PetscFloorReal(vorder+10.*PETSC_SQRT_MACHINE_EPSILON);
    546:       if (vorder-test > PETSC_SQRT_MACHINE_EPSILON) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected value for vorder: %g (%D)",vorder,test);
    547:       ord  = 1;
    548:     }
    549:     if (PetscUnlikelyDebug(test%ord)) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected number of edge dofs %D connected with nodal dof %D with order %D",test,i,ord);
    550:     for (j=ii[i];j<ii[i+1] && sneighs;j++) {
    551:       if (PetscBTLookup(btbd,jj[j])) {
    552:         bdir = PETSC_TRUE;
    553:         break;
    554:       }
    555:       if (vc != ecount[jj[j]]) {
    556:         sneighs = PETSC_FALSE;
    557:       } else {
    558:         PetscInt k,*vn = vneighs[i], *en = eneighs[jj[j]];
    559:         for (k=0;k<vc;k++) {
    560:           if (vn[k] != en[k]) {
    561:             sneighs = PETSC_FALSE;
    562:             break;
    563:           }
    564:         }
    565:       }
    566:     }
    567:     if (!sneighs || test >= 3*ord || bdir) { /* splitpoints */
    568:       if (print) PetscPrintf(PETSC_COMM_SELF,"SPLITPOINT %D (%D %D %D)\n",i,!sneighs,test >= 3*ord,bdir);
    569:       PetscBTSet(btv,i);
    570:     } else if (test == ord) {
    571:       if (order == 1 || (!order && ii[i+1]-ii[i] == 1)) {
    572:         if (print) PetscPrintf(PETSC_COMM_SELF,"ENDPOINT %D\n",i);
    573:         PetscBTSet(btv,i);
    574:       } else {
    575:         if (print) PetscPrintf(PETSC_COMM_SELF,"CORNER CANDIDATE %D\n",i);
    576:         PetscBTSet(btvcand,i);
    577:       }
    578:     }
    579:   }
    580:   ISLocalToGlobalMappingRestoreNodeInfo(el2g,NULL,&ecount,&eneighs);
    581:   ISLocalToGlobalMappingRestoreNodeInfo(vl2g,NULL,&vcount,&vneighs);
    582:   PetscBTDestroy(&btbd);

    584:   /* a candidate is valid if it is connected to another candidate via a non-primal edge dof */
    585:   if (order != 1) {
    586:     if (print) PetscPrintf(PETSC_COMM_SELF,"INSPECTING CANDIDATES\n");
    587:     MatGetRowIJ(lGe,0,PETSC_FALSE,PETSC_FALSE,&i,&iit,&jjt,&done);
    588:     for (i=0;i<nv;i++) {
    589:       if (PetscBTLookup(btvcand,i)) {
    590:         PetscBool found = PETSC_FALSE;
    591:         for (j=ii[i];j<ii[i+1] && !found;j++) {
    592:           PetscInt k,e = jj[j];
    593:           if (PetscBTLookup(bte,e)) continue;
    594:           for (k=iit[e];k<iit[e+1];k++) {
    595:             PetscInt v = jjt[k];
    596:             if (v != i && PetscBTLookup(btvcand,v)) {
    597:               found = PETSC_TRUE;
    598:               break;
    599:             }
    600:           }
    601:         }
    602:         if (!found) {
    603:           if (print) PetscPrintf(PETSC_COMM_SELF,"  CANDIDATE %D CLEARED\n",i);
    604:           PetscBTClear(btvcand,i);
    605:         } else {
    606:           if (print) PetscPrintf(PETSC_COMM_SELF,"  CANDIDATE %D ACCEPTED\n",i);
    607:         }
    608:       }
    609:     }
    610:     MatRestoreRowIJ(lGe,0,PETSC_FALSE,PETSC_FALSE,&i,&iit,&jjt,&done);
    611:   }
    612:   MatSeqAIJRestoreArray(lGt,&vals);
    613:   MatRestoreRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    614:   MatDestroy(&lGe);

    616:   /* Get the local G^T explicitly */
    617:   MatDestroy(&lGt);
    618:   MatTranspose(lG,MAT_INITIAL_MATRIX,&lGt);
    619:   MatSetOption(lGt,MAT_KEEP_NONZERO_PATTERN,PETSC_FALSE);

    621:   /* Mark interior nodal dofs */
    622:   ISLocalToGlobalMappingGetInfo(vl2g,&n_neigh,&neigh,&n_shared,&shared);
    623:   PetscBTCreate(nv,&btvi);
    624:   for (i=1;i<n_neigh;i++) {
    625:     for (j=0;j<n_shared[i];j++) {
    626:       PetscBTSet(btvi,shared[i][j]);
    627:     }
    628:   }
    629:   ISLocalToGlobalMappingRestoreInfo(vl2g,&n_neigh,&neigh,&n_shared,&shared);

    631:   /* communicate corners and splitpoints */
    632:   PetscMalloc1(nv,&vmarks);
    633:   PetscArrayzero(sfvleaves,nv);
    634:   PetscArrayzero(sfvroots,Lv);
    635:   for (i=0;i<nv;i++) if (PetscUnlikely(PetscBTLookup(btv,i))) sfvleaves[i] = 1;

    637:   if (print) {
    638:     IS tbz;

    640:     cum = 0;
    641:     for (i=0;i<nv;i++)
    642:       if (sfvleaves[i])
    643:         vmarks[cum++] = i;

    645:     ISCreateGeneral(PETSC_COMM_SELF,cum,vmarks,PETSC_COPY_VALUES,&tbz);
    646:     PetscObjectSetName((PetscObject)tbz,"corners_to_be_zeroed_local");
    647:     ISView(tbz,NULL);
    648:     ISDestroy(&tbz);
    649:   }

    651:   PetscSFReduceBegin(sfv,MPIU_INT,sfvleaves,sfvroots,MPI_SUM);
    652:   PetscSFReduceEnd(sfv,MPIU_INT,sfvleaves,sfvroots,MPI_SUM);
    653:   PetscSFBcastBegin(sfv,MPIU_INT,sfvroots,sfvleaves,MPI_REPLACE);
    654:   PetscSFBcastEnd(sfv,MPIU_INT,sfvroots,sfvleaves,MPI_REPLACE);

    656:   /* Zero rows of lGt corresponding to identified corners
    657:      and interior nodal dofs */
    658:   cum = 0;
    659:   for (i=0;i<nv;i++) {
    660:     if (sfvleaves[i]) {
    661:       vmarks[cum++] = i;
    662:       PetscBTSet(btv,i);
    663:     }
    664:     if (!PetscBTLookup(btvi,i)) vmarks[cum++] = i;
    665:   }
    666:   PetscBTDestroy(&btvi);
    667:   if (print) {
    668:     IS tbz;

    670:     ISCreateGeneral(PETSC_COMM_SELF,cum,vmarks,PETSC_COPY_VALUES,&tbz);
    671:     PetscObjectSetName((PetscObject)tbz,"corners_to_be_zeroed_with_interior");
    672:     ISView(tbz,NULL);
    673:     ISDestroy(&tbz);
    674:   }
    675:   MatZeroRows(lGt,cum,vmarks,0.,NULL,NULL);
    676:   PetscFree(vmarks);
    677:   PetscSFDestroy(&sfv);
    678:   PetscFree2(sfvleaves,sfvroots);

    680:   /* Recompute G */
    681:   MatDestroy(&lG);
    682:   MatTranspose(lGt,MAT_INITIAL_MATRIX,&lG);
    683:   if (print) {
    684:     PetscObjectSetName((PetscObject)lG,"used_lG");
    685:     MatView(lG,NULL);
    686:     PetscObjectSetName((PetscObject)lGt,"used_lGt");
    687:     MatView(lGt,NULL);
    688:   }

    690:   /* Get primal dofs (if any) */
    691:   cum = 0;
    692:   for (i=0;i<ne;i++) {
    693:     if (PetscUnlikely(PetscBTLookup(bte,i))) marks[cum++] = i;
    694:   }
    695:   if (fl2g) {
    696:     ISLocalToGlobalMappingApply(fl2g,cum,marks,marks);
    697:   }
    698:   ISCreateGeneral(comm,cum,marks,PETSC_COPY_VALUES,&primals);
    699:   if (print) {
    700:     PetscObjectSetName((PetscObject)primals,"prescribed_primal_dofs");
    701:     ISView(primals,NULL);
    702:   }
    703:   PetscBTDestroy(&bte);
    704:   /* TODO: what if the user passed in some of them ?  */
    705:   PCBDDCSetPrimalVerticesLocalIS(pc,primals);
    706:   ISDestroy(&primals);

    708:   /* Compute edge connectivity */
    709:   PetscObjectSetOptionsPrefix((PetscObject)lG,"econn_");

    711:   /* Symbolic conn = lG*lGt */
    712:   MatProductCreate(lG,lGt,NULL,&conn);
    713:   MatProductSetType(conn,MATPRODUCT_AB);
    714:   MatProductSetAlgorithm(conn,"default");
    715:   MatProductSetFill(conn,PETSC_DEFAULT);
    716:   PetscObjectSetOptionsPrefix((PetscObject)conn,"econn_");
    717:   MatProductSetFromOptions(conn);
    718:   MatProductSymbolic(conn);

    720:   MatGetRowIJ(conn,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    721:   if (fl2g) {
    722:     PetscBT   btf;
    723:     PetscInt  *iia,*jja,*iiu,*jju;
    724:     PetscBool rest = PETSC_FALSE,free = PETSC_FALSE;

    726:     /* create CSR for all local dofs */
    727:     PetscMalloc1(n+1,&iia);
    728:     if (pcbddc->mat_graph->nvtxs_csr) { /* the user has passed in a CSR graph */
    729:       if (pcbddc->mat_graph->nvtxs_csr != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Invalid size of CSR graph %D. Should be %D",pcbddc->mat_graph->nvtxs_csr,n);
    730:       iiu = pcbddc->mat_graph->xadj;
    731:       jju = pcbddc->mat_graph->adjncy;
    732:     } else if (pcbddc->use_local_adj) {
    733:       rest = PETSC_TRUE;
    734:       MatGetRowIJ(matis->A,0,PETSC_TRUE,PETSC_FALSE,&i,(const PetscInt**)&iiu,(const PetscInt**)&jju,&done);
    735:     } else {
    736:       free   = PETSC_TRUE;
    737:       PetscMalloc2(n+1,&iiu,n,&jju);
    738:       iiu[0] = 0;
    739:       for (i=0;i<n;i++) {
    740:         iiu[i+1] = i+1;
    741:         jju[i]   = -1;
    742:       }
    743:     }

    745:     /* import sizes of CSR */
    746:     iia[0] = 0;
    747:     for (i=0;i<n;i++) iia[i+1] = iiu[i+1]-iiu[i];

    749:     /* overwrite entries corresponding to the Nedelec field */
    750:     PetscBTCreate(n,&btf);
    751:     ISGetIndices(nedfieldlocal,&idxs);
    752:     for (i=0;i<ne;i++) {
    753:       PetscBTSet(btf,idxs[i]);
    754:       iia[idxs[i]+1] = ii[i+1]-ii[i];
    755:     }

    757:     /* iia in CSR */
    758:     for (i=0;i<n;i++) iia[i+1] += iia[i];

    760:     /* jja in CSR */
    761:     PetscMalloc1(iia[n],&jja);
    762:     for (i=0;i<n;i++)
    763:       if (!PetscBTLookup(btf,i))
    764:         for (j=0;j<iiu[i+1]-iiu[i];j++)
    765:           jja[iia[i]+j] = jju[iiu[i]+j];

    767:     /* map edge dofs connectivity */
    768:     if (jj) {
    769:       ISLocalToGlobalMappingApply(fl2g,ii[ne],jj,(PetscInt *)jj);
    770:       for (i=0;i<ne;i++) {
    771:         PetscInt e = idxs[i];
    772:         for (j=0;j<ii[i+1]-ii[i];j++) jja[iia[e]+j] = jj[ii[i]+j];
    773:       }
    774:     }
    775:     ISRestoreIndices(nedfieldlocal,&idxs);
    776:     PCBDDCSetLocalAdjacencyGraph(pc,n,iia,jja,PETSC_OWN_POINTER);
    777:     if (rest) {
    778:       MatRestoreRowIJ(matis->A,0,PETSC_TRUE,PETSC_FALSE,&i,(const PetscInt**)&iiu,(const PetscInt**)&jju,&done);
    779:     }
    780:     if (free) {
    781:       PetscFree2(iiu,jju);
    782:     }
    783:     PetscBTDestroy(&btf);
    784:   } else {
    785:     PCBDDCSetLocalAdjacencyGraph(pc,n,ii,jj,PETSC_USE_POINTER);
    786:   }

    788:   /* Analyze interface for edge dofs */
    789:   PCBDDCAnalyzeInterface(pc);
    790:   pcbddc->mat_graph->twodim = PETSC_FALSE;

    792:   /* Get coarse edges in the edge space */
    793:   PCBDDCGraphGetCandidatesIS(pcbddc->mat_graph,NULL,NULL,&nee,&alleedges,&allprimals);
    794:   MatRestoreRowIJ(conn,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);

    796:   if (fl2g) {
    797:     ISGlobalToLocalMappingApplyIS(fl2g,IS_GTOLM_DROP,allprimals,&primals);
    798:     PetscMalloc1(nee,&eedges);
    799:     for (i=0;i<nee;i++) {
    800:       ISGlobalToLocalMappingApplyIS(fl2g,IS_GTOLM_DROP,alleedges[i],&eedges[i]);
    801:     }
    802:   } else {
    803:     eedges  = alleedges;
    804:     primals = allprimals;
    805:   }

    807:   /* Mark fine edge dofs with their coarse edge id */
    808:   PetscArrayzero(marks,ne);
    809:   ISGetLocalSize(primals,&cum);
    810:   ISGetIndices(primals,&idxs);
    811:   for (i=0;i<cum;i++) marks[idxs[i]] = nee+1;
    812:   ISRestoreIndices(primals,&idxs);
    813:   if (print) {
    814:     PetscObjectSetName((PetscObject)primals,"obtained_primal_dofs");
    815:     ISView(primals,NULL);
    816:   }

    818:   maxsize = 0;
    819:   for (i=0;i<nee;i++) {
    820:     PetscInt size,mark = i+1;

    822:     ISGetLocalSize(eedges[i],&size);
    823:     ISGetIndices(eedges[i],&idxs);
    824:     for (j=0;j<size;j++) marks[idxs[j]] = mark;
    825:     ISRestoreIndices(eedges[i],&idxs);
    826:     maxsize = PetscMax(maxsize,size);
    827:   }

    829:   /* Find coarse edge endpoints */
    830:   MatGetRowIJ(lG,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    831:   MatGetRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&iit,&jjt,&done);
    832:   for (i=0;i<nee;i++) {
    833:     PetscInt mark = i+1,size;

    835:     ISGetLocalSize(eedges[i],&size);
    836:     if (!size && nedfieldlocal) continue;
    837:     if (!size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected zero sized edge %D",i);
    838:     ISGetIndices(eedges[i],&idxs);
    839:     if (print) {
    840:       PetscPrintf(PETSC_COMM_SELF,"ENDPOINTS ANALYSIS EDGE %D\n",i);
    841:       ISView(eedges[i],NULL);
    842:     }
    843:     for (j=0;j<size;j++) {
    844:       PetscInt k, ee = idxs[j];
    845:       if (print) PetscPrintf(PETSC_COMM_SELF,"  idx %D\n",ee);
    846:       for (k=ii[ee];k<ii[ee+1];k++) {
    847:         if (print) PetscPrintf(PETSC_COMM_SELF,"    inspect %D\n",jj[k]);
    848:         if (PetscBTLookup(btv,jj[k])) {
    849:           if (print) PetscPrintf(PETSC_COMM_SELF,"      corner found (already set) %D\n",jj[k]);
    850:         } else if (PetscBTLookup(btvcand,jj[k])) { /* is it ok? */
    851:           PetscInt  k2;
    852:           PetscBool corner = PETSC_FALSE;
    853:           for (k2 = iit[jj[k]];k2 < iit[jj[k]+1];k2++) {
    854:             if (print) PetscPrintf(PETSC_COMM_SELF,"        INSPECTING %D: mark %D (ref mark %D), boundary %D\n",jjt[k2],marks[jjt[k2]],mark,!!PetscBTLookup(btb,jjt[k2]));
    855:             /* it's a corner if either is connected with an edge dof belonging to a different cc or
    856:                if the edge dof lie on the natural part of the boundary */
    857:             if ((marks[jjt[k2]] && marks[jjt[k2]] != mark) || (!marks[jjt[k2]] && PetscBTLookup(btb,jjt[k2]))) {
    858:               corner = PETSC_TRUE;
    859:               break;
    860:             }
    861:           }
    862:           if (corner) { /* found the nodal dof corresponding to the endpoint of the edge */
    863:             if (print) PetscPrintf(PETSC_COMM_SELF,"        corner found %D\n",jj[k]);
    864:             PetscBTSet(btv,jj[k]);
    865:           } else {
    866:             if (print) PetscPrintf(PETSC_COMM_SELF,"        no corners found\n");
    867:           }
    868:         }
    869:       }
    870:     }
    871:     ISRestoreIndices(eedges[i],&idxs);
    872:   }
    873:   MatRestoreRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&iit,&jjt,&done);
    874:   MatRestoreRowIJ(lG,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    875:   PetscBTDestroy(&btb);

    877:   /* Reset marked primal dofs */
    878:   ISGetLocalSize(primals,&cum);
    879:   ISGetIndices(primals,&idxs);
    880:   for (i=0;i<cum;i++) marks[idxs[i]] = 0;
    881:   ISRestoreIndices(primals,&idxs);

    883:   /* Now use the initial lG */
    884:   MatDestroy(&lG);
    885:   MatDestroy(&lGt);
    886:   lG   = lGinit;
    887:   MatTranspose(lG,MAT_INITIAL_MATRIX,&lGt);

    889:   /* Compute extended cols indices */
    890:   PetscBTCreate(nv,&btvc);
    891:   PetscBTCreate(nee,&bter);
    892:   MatGetRowIJ(lG,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    893:   MatSeqAIJGetMaxRowNonzeros(lG,&i);
    894:   i   *= maxsize;
    895:   PetscCalloc1(nee,&extcols);
    896:   PetscMalloc2(i,&extrow,i,&gidxs);
    897:   eerr = PETSC_FALSE;
    898:   for (i=0;i<nee;i++) {
    899:     PetscInt size,found = 0;

    901:     cum  = 0;
    902:     ISGetLocalSize(eedges[i],&size);
    903:     if (!size && nedfieldlocal) continue;
    904:     if (!size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected zero sized edge %D",i);
    905:     ISGetIndices(eedges[i],&idxs);
    906:     PetscBTMemzero(nv,btvc);
    907:     for (j=0;j<size;j++) {
    908:       PetscInt k,ee = idxs[j];
    909:       for (k=ii[ee];k<ii[ee+1];k++) {
    910:         PetscInt vv = jj[k];
    911:         if (!PetscBTLookup(btv,vv)) extrow[cum++] = vv;
    912:         else if (!PetscBTLookupSet(btvc,vv)) found++;
    913:       }
    914:     }
    915:     ISRestoreIndices(eedges[i],&idxs);
    916:     PetscSortRemoveDupsInt(&cum,extrow);
    917:     ISLocalToGlobalMappingApply(vl2g,cum,extrow,gidxs);
    918:     PetscSortIntWithArray(cum,gidxs,extrow);
    919:     ISCreateGeneral(PETSC_COMM_SELF,cum,extrow,PETSC_COPY_VALUES,&extcols[i]);
    920:     /* it may happen that endpoints are not defined at this point
    921:        if it is the case, mark this edge for a second pass */
    922:     if (cum != size -1 || found != 2) {
    923:       PetscBTSet(bter,i);
    924:       if (print) {
    925:         PetscObjectSetName((PetscObject)eedges[i],"error_edge");
    926:         ISView(eedges[i],NULL);
    927:         PetscObjectSetName((PetscObject)extcols[i],"error_extcol");
    928:         ISView(extcols[i],NULL);
    929:       }
    930:       eerr = PETSC_TRUE;
    931:     }
    932:   }
    933:   /* if (eerr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected SIZE OF EDGE > EXTCOL FIRST PASS"); */
    934:   MPIU_Allreduce(&eerr,&done,1,MPIU_BOOL,MPI_LOR,comm);
    935:   if (done) {
    936:     PetscInt *newprimals;

    938:     PetscMalloc1(ne,&newprimals);
    939:     ISGetLocalSize(primals,&cum);
    940:     ISGetIndices(primals,&idxs);
    941:     PetscArraycpy(newprimals,idxs,cum);
    942:     ISRestoreIndices(primals,&idxs);
    943:     MatGetRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&iit,&jjt,&done);
    944:     if (print) PetscPrintf(PETSC_COMM_SELF,"DOING SECOND PASS (eerr %D)\n",eerr);
    945:     for (i=0;i<nee;i++) {
    946:       PetscBool has_candidates = PETSC_FALSE;
    947:       if (PetscBTLookup(bter,i)) {
    948:         PetscInt size,mark = i+1;

    950:         ISGetLocalSize(eedges[i],&size);
    951:         ISGetIndices(eedges[i],&idxs);
    952:         /* for (j=0;j<size;j++) newprimals[cum++] = idxs[j]; */
    953:         for (j=0;j<size;j++) {
    954:           PetscInt k,ee = idxs[j];
    955:           if (print) PetscPrintf(PETSC_COMM_SELF,"Inspecting edge dof %D [%D %D)\n",ee,ii[ee],ii[ee+1]);
    956:           for (k=ii[ee];k<ii[ee+1];k++) {
    957:             /* set all candidates located on the edge as corners */
    958:             if (PetscBTLookup(btvcand,jj[k])) {
    959:               PetscInt k2,vv = jj[k];
    960:               has_candidates = PETSC_TRUE;
    961:               if (print) PetscPrintf(PETSC_COMM_SELF,"  Candidate set to vertex %D\n",vv);
    962:               PetscBTSet(btv,vv);
    963:               /* set all edge dofs connected to candidate as primals */
    964:               for (k2=iit[vv];k2<iit[vv+1];k2++) {
    965:                 if (marks[jjt[k2]] == mark) {
    966:                   PetscInt k3,ee2 = jjt[k2];
    967:                   if (print) PetscPrintf(PETSC_COMM_SELF,"    Connected edge dof set to primal %D\n",ee2);
    968:                   newprimals[cum++] = ee2;
    969:                   /* finally set the new corners */
    970:                   for (k3=ii[ee2];k3<ii[ee2+1];k3++) {
    971:                     if (print) PetscPrintf(PETSC_COMM_SELF,"      Connected nodal dof set to vertex %D\n",jj[k3]);
    972:                     PetscBTSet(btv,jj[k3]);
    973:                   }
    974:                 }
    975:               }
    976:             } else {
    977:               if (print) PetscPrintf(PETSC_COMM_SELF,"  Not a candidate vertex %D\n",jj[k]);
    978:             }
    979:           }
    980:         }
    981:         if (!has_candidates) { /* circular edge */
    982:           PetscInt k, ee = idxs[0],*tmarks;

    984:           PetscCalloc1(ne,&tmarks);
    985:           if (print) PetscPrintf(PETSC_COMM_SELF,"  Circular edge %D\n",i);
    986:           for (k=ii[ee];k<ii[ee+1];k++) {
    987:             PetscInt k2;
    988:             if (print) PetscPrintf(PETSC_COMM_SELF,"    Set to corner %D\n",jj[k]);
    989:             PetscBTSet(btv,jj[k]);
    990:             for (k2=iit[jj[k]];k2<iit[jj[k]+1];k2++) tmarks[jjt[k2]]++;
    991:           }
    992:           for (j=0;j<size;j++) {
    993:             if (tmarks[idxs[j]] > 1) {
    994:               if (print) PetscPrintf(PETSC_COMM_SELF,"  Edge dof set to primal %D\n",idxs[j]);
    995:               newprimals[cum++] = idxs[j];
    996:             }
    997:           }
    998:           PetscFree(tmarks);
    999:         }
    1000:         ISRestoreIndices(eedges[i],&idxs);
    1001:       }
    1002:       ISDestroy(&extcols[i]);
    1003:     }
    1004:     PetscFree(extcols);
    1005:     MatRestoreRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&iit,&jjt,&done);
    1006:     PetscSortRemoveDupsInt(&cum,newprimals);
    1007:     if (fl2g) {
    1008:       ISLocalToGlobalMappingApply(fl2g,cum,newprimals,newprimals);
    1009:       ISDestroy(&primals);
    1010:       for (i=0;i<nee;i++) {
    1011:         ISDestroy(&eedges[i]);
    1012:       }
    1013:       PetscFree(eedges);
    1014:     }
    1015:     PCBDDCGraphRestoreCandidatesIS(pcbddc->mat_graph,NULL,NULL,&nee,&alleedges,&allprimals);
    1016:     ISCreateGeneral(comm,cum,newprimals,PETSC_COPY_VALUES,&primals);
    1017:     PetscFree(newprimals);
    1018:     PCBDDCSetPrimalVerticesLocalIS(pc,primals);
    1019:     ISDestroy(&primals);
    1020:     PCBDDCAnalyzeInterface(pc);
    1021:     pcbddc->mat_graph->twodim = PETSC_FALSE;
    1022:     PCBDDCGraphGetCandidatesIS(pcbddc->mat_graph,NULL,NULL,&nee,&alleedges,&allprimals);
    1023:     if (fl2g) {
    1024:       ISGlobalToLocalMappingApplyIS(fl2g,IS_GTOLM_DROP,allprimals,&primals);
    1025:       PetscMalloc1(nee,&eedges);
    1026:       for (i=0;i<nee;i++) {
    1027:         ISGlobalToLocalMappingApplyIS(fl2g,IS_GTOLM_DROP,alleedges[i],&eedges[i]);
    1028:       }
    1029:     } else {
    1030:       eedges  = alleedges;
    1031:       primals = allprimals;
    1032:     }
    1033:     PetscCalloc1(nee,&extcols);

    1035:     /* Mark again */
    1036:     PetscArrayzero(marks,ne);
    1037:     for (i=0;i<nee;i++) {
    1038:       PetscInt size,mark = i+1;

    1040:       ISGetLocalSize(eedges[i],&size);
    1041:       ISGetIndices(eedges[i],&idxs);
    1042:       for (j=0;j<size;j++) marks[idxs[j]] = mark;
    1043:       ISRestoreIndices(eedges[i],&idxs);
    1044:     }
    1045:     if (print) {
    1046:       PetscObjectSetName((PetscObject)primals,"obtained_primal_dofs_secondpass");
    1047:       ISView(primals,NULL);
    1048:     }

    1050:     /* Recompute extended cols */
    1051:     eerr = PETSC_FALSE;
    1052:     for (i=0;i<nee;i++) {
    1053:       PetscInt size;

    1055:       cum  = 0;
    1056:       ISGetLocalSize(eedges[i],&size);
    1057:       if (!size && nedfieldlocal) continue;
    1058:       if (!size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected zero sized edge %D",i);
    1059:       ISGetIndices(eedges[i],&idxs);
    1060:       for (j=0;j<size;j++) {
    1061:         PetscInt k,ee = idxs[j];
    1062:         for (k=ii[ee];k<ii[ee+1];k++) if (!PetscBTLookup(btv,jj[k])) extrow[cum++] = jj[k];
    1063:       }
    1064:       ISRestoreIndices(eedges[i],&idxs);
    1065:       PetscSortRemoveDupsInt(&cum,extrow);
    1066:       ISLocalToGlobalMappingApply(vl2g,cum,extrow,gidxs);
    1067:       PetscSortIntWithArray(cum,gidxs,extrow);
    1068:       ISCreateGeneral(PETSC_COMM_SELF,cum,extrow,PETSC_COPY_VALUES,&extcols[i]);
    1069:       if (cum != size -1) {
    1070:         if (print) {
    1071:           PetscObjectSetName((PetscObject)eedges[i],"error_edge_secondpass");
    1072:           ISView(eedges[i],NULL);
    1073:           PetscObjectSetName((PetscObject)extcols[i],"error_extcol_secondpass");
    1074:           ISView(extcols[i],NULL);
    1075:         }
    1076:         eerr = PETSC_TRUE;
    1077:       }
    1078:     }
    1079:   }
    1080:   MatRestoreRowIJ(lG,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    1081:   PetscFree2(extrow,gidxs);
    1082:   PetscBTDestroy(&bter);
    1083:   if (print) { PCBDDCGraphASCIIView(pcbddc->mat_graph,5,PETSC_VIEWER_STDOUT_SELF); }
    1084:   /* an error should not occur at this point */
    1085:   if (eerr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected SIZE OF EDGE > EXTCOL SECOND PASS");

    1087:   /* Check the number of endpoints */
    1088:   MatGetRowIJ(lG,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    1089:   PetscMalloc1(2*nee,&corners);
    1090:   PetscMalloc1(nee,&cedges);
    1091:   for (i=0;i<nee;i++) {
    1092:     PetscInt size, found = 0, gc[2];

    1094:     /* init with defaults */
    1095:     cedges[i] = corners[i*2] = corners[i*2+1] = -1;
    1096:     ISGetLocalSize(eedges[i],&size);
    1097:     if (!size && nedfieldlocal) continue;
    1098:     if (!size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected zero sized edge %D",i);
    1099:     ISGetIndices(eedges[i],&idxs);
    1100:     PetscBTMemzero(nv,btvc);
    1101:     for (j=0;j<size;j++) {
    1102:       PetscInt k,ee = idxs[j];
    1103:       for (k=ii[ee];k<ii[ee+1];k++) {
    1104:         PetscInt vv = jj[k];
    1105:         if (PetscBTLookup(btv,vv) && !PetscBTLookupSet(btvc,vv)) {
    1106:           if (found == 2) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Found more then two corners for edge %D",i);
    1107:           corners[i*2+found++] = vv;
    1108:         }
    1109:       }
    1110:     }
    1111:     if (found != 2) {
    1112:       PetscInt e;
    1113:       if (fl2g) {
    1114:         ISLocalToGlobalMappingApply(fl2g,1,idxs,&e);
    1115:       } else {
    1116:         e = idxs[0];
    1117:       }
    1118:       SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Found %D corners for edge %D (astart %D, estart %D)",found,i,e,idxs[0]);
    1119:     }

    1121:     /* get primal dof index on this coarse edge */
    1122:     ISLocalToGlobalMappingApply(vl2g,2,corners+2*i,gc);
    1123:     if (gc[0] > gc[1]) {
    1124:       PetscInt swap  = corners[2*i];
    1125:       corners[2*i]   = corners[2*i+1];
    1126:       corners[2*i+1] = swap;
    1127:     }
    1128:     cedges[i] = idxs[size-1];
    1129:     ISRestoreIndices(eedges[i],&idxs);
    1130:     if (print) PetscPrintf(PETSC_COMM_SELF,"EDGE %D: ce %D, corners (%D,%D)\n",i,cedges[i],corners[2*i],corners[2*i+1]);
    1131:   }
    1132:   MatRestoreRowIJ(lG,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    1133:   PetscBTDestroy(&btvc);

    1135:   if (PetscDefined(USE_DEBUG)) {
    1136:     /* Inspects columns of lG (rows of lGt) and make sure the change of basis will
    1137:      not interfere with neighbouring coarse edges */
    1138:     PetscMalloc1(nee+1,&emarks);
    1139:     MatGetRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    1140:     for (i=0;i<nv;i++) {
    1141:       PetscInt emax = 0,eemax = 0;

    1143:       if (ii[i+1]==ii[i] || PetscBTLookup(btv,i)) continue;
    1144:       PetscArrayzero(emarks,nee+1);
    1145:       for (j=ii[i];j<ii[i+1];j++) emarks[marks[jj[j]]]++;
    1146:       for (j=1;j<nee+1;j++) {
    1147:         if (emax < emarks[j]) {
    1148:           emax = emarks[j];
    1149:           eemax = j;
    1150:         }
    1151:       }
    1152:       /* not relevant for edges */
    1153:       if (!eemax) continue;

    1155:       for (j=ii[i];j<ii[i+1];j++) {
    1156:         if (marks[jj[j]] && marks[jj[j]] != eemax) {
    1157:           SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_SUP,"Found 2 coarse edges (id %D and %D) connected through the %D nodal dof at edge dof %D",marks[jj[j]]-1,eemax,i,jj[j]);
    1158:         }
    1159:       }
    1160:     }
    1161:     PetscFree(emarks);
    1162:     MatRestoreRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    1163:   }

    1165:   /* Compute extended rows indices for edge blocks of the change of basis */
    1166:   MatGetRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    1167:   MatSeqAIJGetMaxRowNonzeros(lGt,&extmem);
    1168:   extmem *= maxsize;
    1169:   PetscMalloc1(extmem*nee,&extrow);
    1170:   PetscMalloc1(nee,&extrows);
    1171:   PetscCalloc1(nee,&extrowcum);
    1172:   for (i=0;i<nv;i++) {
    1173:     PetscInt mark = 0,size,start;

    1175:     if (ii[i+1]==ii[i] || PetscBTLookup(btv,i)) continue;
    1176:     for (j=ii[i];j<ii[i+1];j++)
    1177:       if (marks[jj[j]] && !mark)
    1178:         mark = marks[jj[j]];

    1180:     /* not relevant */
    1181:     if (!mark) continue;

    1183:     /* import extended row */
    1184:     mark--;
    1185:     start = mark*extmem+extrowcum[mark];
    1186:     size = ii[i+1]-ii[i];
    1187:     if (extrowcum[mark] + size > extmem) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not enough memory allocated %D > %D",extrowcum[mark] + size,extmem);
    1188:     PetscArraycpy(extrow+start,jj+ii[i],size);
    1189:     extrowcum[mark] += size;
    1190:   }
    1191:   MatRestoreRowIJ(lGt,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&done);
    1192:   MatDestroy(&lGt);
    1193:   PetscFree(marks);

    1195:   /* Compress extrows */
    1196:   cum  = 0;
    1197:   for (i=0;i<nee;i++) {
    1198:     PetscInt size = extrowcum[i],*start = extrow + i*extmem;
    1199:     PetscSortRemoveDupsInt(&size,start);
    1200:     ISCreateGeneral(PETSC_COMM_SELF,size,start,PETSC_USE_POINTER,&extrows[i]);
    1201:     cum  = PetscMax(cum,size);
    1202:   }
    1203:   PetscFree(extrowcum);
    1204:   PetscBTDestroy(&btv);
    1205:   PetscBTDestroy(&btvcand);

    1207:   /* Workspace for lapack inner calls and VecSetValues */
    1208:   PetscMalloc2((5+cum+maxsize)*maxsize,&work,maxsize,&rwork);

    1210:   /* Create change of basis matrix (preallocation can be improved) */
    1211:   MatCreate(comm,&T);
    1212:   MatSetSizes(T,pc->pmat->rmap->n,pc->pmat->rmap->n,
    1213:                        pc->pmat->rmap->N,pc->pmat->rmap->N);
    1214:   MatSetType(T,MATAIJ);
    1215:   MatSeqAIJSetPreallocation(T,10,NULL);
    1216:   MatMPIAIJSetPreallocation(T,10,NULL,10,NULL);
    1217:   MatSetLocalToGlobalMapping(T,al2g,al2g);
    1218:   MatSetOption(T,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
    1219:   MatSetOption(T,MAT_ROW_ORIENTED,PETSC_FALSE);
    1220:   ISLocalToGlobalMappingDestroy(&al2g);

    1222:   /* Defaults to identity */
    1223:   MatCreateVecs(pc->pmat,&tvec,NULL);
    1224:   VecSet(tvec,1.0);
    1225:   MatDiagonalSet(T,tvec,INSERT_VALUES);
    1226:   VecDestroy(&tvec);

    1228:   /* Create discrete gradient for the coarser level if needed */
    1229:   MatDestroy(&pcbddc->nedcG);
    1230:   ISDestroy(&pcbddc->nedclocal);
    1231:   if (pcbddc->current_level < pcbddc->max_levels) {
    1232:     ISLocalToGlobalMapping cel2g,cvl2g;
    1233:     IS                     wis,gwis;
    1234:     PetscInt               cnv,cne;

    1236:     ISCreateGeneral(comm,nee,cedges,PETSC_COPY_VALUES,&wis);
    1237:     if (fl2g) {
    1238:       ISLocalToGlobalMappingApplyIS(fl2g,wis,&pcbddc->nedclocal);
    1239:     } else {
    1240:       PetscObjectReference((PetscObject)wis);
    1241:       pcbddc->nedclocal = wis;
    1242:     }
    1243:     ISLocalToGlobalMappingApplyIS(el2g,wis,&gwis);
    1244:     ISDestroy(&wis);
    1245:     ISRenumber(gwis,NULL,&cne,&wis);
    1246:     ISLocalToGlobalMappingCreateIS(wis,&cel2g);
    1247:     ISDestroy(&wis);
    1248:     ISDestroy(&gwis);

    1250:     ISCreateGeneral(comm,2*nee,corners,PETSC_USE_POINTER,&wis);
    1251:     ISLocalToGlobalMappingApplyIS(vl2g,wis,&gwis);
    1252:     ISDestroy(&wis);
    1253:     ISRenumber(gwis,NULL,&cnv,&wis);
    1254:     ISLocalToGlobalMappingCreateIS(wis,&cvl2g);
    1255:     ISDestroy(&wis);
    1256:     ISDestroy(&gwis);

    1258:     MatCreate(comm,&pcbddc->nedcG);
    1259:     MatSetSizes(pcbddc->nedcG,PETSC_DECIDE,PETSC_DECIDE,cne,cnv);
    1260:     MatSetType(pcbddc->nedcG,MATAIJ);
    1261:     MatSeqAIJSetPreallocation(pcbddc->nedcG,2,NULL);
    1262:     MatMPIAIJSetPreallocation(pcbddc->nedcG,2,NULL,2,NULL);
    1263:     MatSetLocalToGlobalMapping(pcbddc->nedcG,cel2g,cvl2g);
    1264:     ISLocalToGlobalMappingDestroy(&cel2g);
    1265:     ISLocalToGlobalMappingDestroy(&cvl2g);
    1266:   }
    1267:   ISLocalToGlobalMappingDestroy(&vl2g);

    1269: #if defined(PRINT_GDET)
    1270:   inc = 0;
    1271:   lev = pcbddc->current_level;
    1272: #endif

    1274:   /* Insert values in the change of basis matrix */
    1275:   for (i=0;i<nee;i++) {
    1276:     Mat         Gins = NULL, GKins = NULL;
    1277:     IS          cornersis = NULL;
    1278:     PetscScalar cvals[2];

    1280:     if (pcbddc->nedcG) {
    1281:       ISCreateGeneral(PETSC_COMM_SELF,2,corners+2*i,PETSC_USE_POINTER,&cornersis);
    1282:     }
    1283:     PCBDDCComputeNedelecChangeEdge(lG,eedges[i],extrows[i],extcols[i],cornersis,&Gins,&GKins,cvals,work,rwork);
    1284:     if (Gins && GKins) {
    1285:       const PetscScalar *data;
    1286:       const PetscInt    *rows,*cols;
    1287:       PetscInt          nrh,nch,nrc,ncc;

    1289:       ISGetIndices(eedges[i],&cols);
    1290:       /* H1 */
    1291:       ISGetIndices(extrows[i],&rows);
    1292:       MatGetSize(Gins,&nrh,&nch);
    1293:       MatDenseGetArrayRead(Gins,&data);
    1294:       MatSetValuesLocal(T,nrh,rows,nch,cols,data,INSERT_VALUES);
    1295:       MatDenseRestoreArrayRead(Gins,&data);
    1296:       ISRestoreIndices(extrows[i],&rows);
    1297:       /* complement */
    1298:       MatGetSize(GKins,&nrc,&ncc);
    1299:       if (!ncc) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Constant function has not been generated for coarse edge %D",i);
    1300:       if (ncc + nch != nrc) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB,"The sum of the number of columns of GKins %D and Gins %D does not match %D for coarse edge %D",ncc,nch,nrc,i);
    1301:       if (ncc != 1 && pcbddc->nedcG) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot generate the coarse discrete gradient for coarse edge %D with ncc %D",i,ncc);
    1302:       MatDenseGetArrayRead(GKins,&data);
    1303:       MatSetValuesLocal(T,nrc,cols,ncc,cols+nch,data,INSERT_VALUES);
    1304:       MatDenseRestoreArrayRead(GKins,&data);

    1306:       /* coarse discrete gradient */
    1307:       if (pcbddc->nedcG) {
    1308:         PetscInt cols[2];

    1310:         cols[0] = 2*i;
    1311:         cols[1] = 2*i+1;
    1312:         MatSetValuesLocal(pcbddc->nedcG,1,&i,2,cols,cvals,INSERT_VALUES);
    1313:       }
    1314:       ISRestoreIndices(eedges[i],&cols);
    1315:     }
    1316:     ISDestroy(&extrows[i]);
    1317:     ISDestroy(&extcols[i]);
    1318:     ISDestroy(&cornersis);
    1319:     MatDestroy(&Gins);
    1320:     MatDestroy(&GKins);
    1321:   }
    1322:   ISLocalToGlobalMappingDestroy(&el2g);

    1324:   /* Start assembling */
    1325:   MatAssemblyBegin(T,MAT_FINAL_ASSEMBLY);
    1326:   if (pcbddc->nedcG) {
    1327:     MatAssemblyBegin(pcbddc->nedcG,MAT_FINAL_ASSEMBLY);
    1328:   }

    1330:   /* Free */
    1331:   if (fl2g) {
    1332:     ISDestroy(&primals);
    1333:     for (i=0;i<nee;i++) {
    1334:       ISDestroy(&eedges[i]);
    1335:     }
    1336:     PetscFree(eedges);
    1337:   }

    1339:   /* hack mat_graph with primal dofs on the coarse edges */
    1340:   {
    1341:     PCBDDCGraph graph   = pcbddc->mat_graph;
    1342:     PetscInt    *oqueue = graph->queue;
    1343:     PetscInt    *ocptr  = graph->cptr;
    1344:     PetscInt    ncc,*idxs;

    1346:     /* find first primal edge */
    1347:     if (pcbddc->nedclocal) {
    1348:       ISGetIndices(pcbddc->nedclocal,(const PetscInt**)&idxs);
    1349:     } else {
    1350:       if (fl2g) {
    1351:         ISLocalToGlobalMappingApply(fl2g,nee,cedges,cedges);
    1352:       }
    1353:       idxs = cedges;
    1354:     }
    1355:     cum = 0;
    1356:     while (cum < nee && cedges[cum] < 0) cum++;

    1358:     /* adapt connected components */
    1359:     PetscMalloc2(graph->nvtxs+1,&graph->cptr,ocptr[graph->ncc],&graph->queue);
    1360:     graph->cptr[0] = 0;
    1361:     for (i=0,ncc=0;i<graph->ncc;i++) {
    1362:       PetscInt lc = ocptr[i+1]-ocptr[i];
    1363:       if (cum != nee && oqueue[ocptr[i+1]-1] == cedges[cum]) { /* this cc has a primal dof */
    1364:         graph->cptr[ncc+1] = graph->cptr[ncc]+1;
    1365:         graph->queue[graph->cptr[ncc]] = cedges[cum];
    1366:         ncc++;
    1367:         lc--;
    1368:         cum++;
    1369:         while (cum < nee && cedges[cum] < 0) cum++;
    1370:       }
    1371:       graph->cptr[ncc+1] = graph->cptr[ncc] + lc;
    1372:       for (j=0;j<lc;j++) graph->queue[graph->cptr[ncc]+j] = oqueue[ocptr[i]+j];
    1373:       ncc++;
    1374:     }
    1375:     graph->ncc = ncc;
    1376:     if (pcbddc->nedclocal) {
    1377:       ISRestoreIndices(pcbddc->nedclocal,(const PetscInt**)&idxs);
    1378:     }
    1379:     PetscFree2(ocptr,oqueue);
    1380:   }
    1381:   ISLocalToGlobalMappingDestroy(&fl2g);
    1382:   PCBDDCGraphRestoreCandidatesIS(pcbddc->mat_graph,NULL,NULL,&nee,&alleedges,&allprimals);
    1383:   PCBDDCGraphResetCSR(pcbddc->mat_graph);
    1384:   MatDestroy(&conn);

    1386:   ISDestroy(&nedfieldlocal);
    1387:   PetscFree(extrow);
    1388:   PetscFree2(work,rwork);
    1389:   PetscFree(corners);
    1390:   PetscFree(cedges);
    1391:   PetscFree(extrows);
    1392:   PetscFree(extcols);
    1393:   MatDestroy(&lG);

    1395:   /* Complete assembling */
    1396:   MatAssemblyEnd(T,MAT_FINAL_ASSEMBLY);
    1397:   if (pcbddc->nedcG) {
    1398:     MatAssemblyEnd(pcbddc->nedcG,MAT_FINAL_ASSEMBLY);
    1399: #if 0
    1400:     PetscObjectSetName((PetscObject)pcbddc->nedcG,"coarse_G");
    1401:     MatView(pcbddc->nedcG,NULL);
    1402: #endif
    1403:   }

    1405:   /* set change of basis */
    1406:   PCBDDCSetChangeOfBasisMat(pc,T,singular);
    1407:   MatDestroy(&T);

    1409:   return(0);
    1410: }

    1412: /* the near-null space of BDDC carries information on quadrature weights,
    1413:    and these can be collinear -> so cheat with MatNullSpaceCreate
    1414:    and create a suitable set of basis vectors first */
    1415: PetscErrorCode PCBDDCNullSpaceCreate(MPI_Comm comm, PetscBool has_const, PetscInt nvecs, Vec quad_vecs[], MatNullSpace *nnsp)
    1416: {
    1418:   PetscInt       i;

    1421:   for (i=0;i<nvecs;i++) {
    1422:     PetscInt first,last;

    1424:     VecGetOwnershipRange(quad_vecs[i],&first,&last);
    1425:     if (last-first < 2*nvecs && has_const) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented");
    1426:     if (i>=first && i < last) {
    1427:       PetscScalar *data;
    1428:       VecGetArray(quad_vecs[i],&data);
    1429:       if (!has_const) {
    1430:         data[i-first] = 1.;
    1431:       } else {
    1432:         data[2*i-first] = 1./PetscSqrtReal(2.);
    1433:         data[2*i-first+1] = -1./PetscSqrtReal(2.);
    1434:       }
    1435:       VecRestoreArray(quad_vecs[i],&data);
    1436:     }
    1437:     PetscObjectStateIncrease((PetscObject)quad_vecs[i]);
    1438:   }
    1439:   MatNullSpaceCreate(comm,has_const,nvecs,quad_vecs,nnsp);
    1440:   for (i=0;i<nvecs;i++) { /* reset vectors */
    1441:     PetscInt first,last;
    1442:     VecLockReadPop(quad_vecs[i]);
    1443:     VecGetOwnershipRange(quad_vecs[i],&first,&last);
    1444:     if (i>=first && i < last) {
    1445:       PetscScalar *data;
    1446:       VecGetArray(quad_vecs[i],&data);
    1447:       if (!has_const) {
    1448:         data[i-first] = 0.;
    1449:       } else {
    1450:         data[2*i-first] = 0.;
    1451:         data[2*i-first+1] = 0.;
    1452:       }
    1453:       VecRestoreArray(quad_vecs[i],&data);
    1454:     }
    1455:     PetscObjectStateIncrease((PetscObject)quad_vecs[i]);
    1456:     VecLockReadPush(quad_vecs[i]);
    1457:   }
    1458:   return(0);
    1459: }

    1461: PetscErrorCode PCBDDCComputeNoNetFlux(Mat A, Mat divudotp, PetscBool transpose, IS vl2l, PCBDDCGraph graph, MatNullSpace *nnsp)
    1462: {
    1463:   Mat                    loc_divudotp;
    1464:   Vec                    p,v,vins,quad_vec,*quad_vecs;
    1465:   ISLocalToGlobalMapping map;
    1466:   PetscScalar            *vals;
    1467:   const PetscScalar      *array;
    1468:   PetscInt               i,maxneighs = 0,maxsize,*gidxs;
    1469:   PetscInt               n_neigh,*neigh,*n_shared,**shared;
    1470:   PetscMPIInt            rank;
    1471:   PetscErrorCode         ierr;

    1474:   ISLocalToGlobalMappingGetInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);
    1475:   for (i=0;i<n_neigh;i++) maxneighs = PetscMax(graph->count[shared[i][0]]+1,maxneighs);
    1476:   MPIU_Allreduce(MPI_IN_PLACE,&maxneighs,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)A));
    1477:   if (!maxneighs) {
    1478:     ISLocalToGlobalMappingRestoreInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);
    1479:     *nnsp = NULL;
    1480:     return(0);
    1481:   }
    1482:   maxsize = 0;
    1483:   for (i=0;i<n_neigh;i++) maxsize = PetscMax(n_shared[i],maxsize);
    1484:   PetscMalloc2(maxsize,&gidxs,maxsize,&vals);
    1485:   /* create vectors to hold quadrature weights */
    1486:   MatCreateVecs(A,&quad_vec,NULL);
    1487:   if (!transpose) {
    1488:     MatGetLocalToGlobalMapping(A,&map,NULL);
    1489:   } else {
    1490:     MatGetLocalToGlobalMapping(A,NULL,&map);
    1491:   }
    1492:   VecDuplicateVecs(quad_vec,maxneighs,&quad_vecs);
    1493:   VecDestroy(&quad_vec);
    1494:   PCBDDCNullSpaceCreate(PetscObjectComm((PetscObject)A),PETSC_FALSE,maxneighs,quad_vecs,nnsp);
    1495:   for (i=0;i<maxneighs;i++) {
    1496:     VecLockReadPop(quad_vecs[i]);
    1497:   }

    1499:   /* compute local quad vec */
    1500:   MatISGetLocalMat(divudotp,&loc_divudotp);
    1501:   if (!transpose) {
    1502:     MatCreateVecs(loc_divudotp,&v,&p);
    1503:   } else {
    1504:     MatCreateVecs(loc_divudotp,&p,&v);
    1505:   }
    1506:   VecSet(p,1.);
    1507:   if (!transpose) {
    1508:     MatMultTranspose(loc_divudotp,p,v);
    1509:   } else {
    1510:     MatMult(loc_divudotp,p,v);
    1511:   }
    1512:   if (vl2l) {
    1513:     Mat        lA;
    1514:     VecScatter sc;

    1516:     MatISGetLocalMat(A,&lA);
    1517:     MatCreateVecs(lA,&vins,NULL);
    1518:     VecScatterCreate(v,NULL,vins,vl2l,&sc);
    1519:     VecScatterBegin(sc,v,vins,INSERT_VALUES,SCATTER_FORWARD);
    1520:     VecScatterEnd(sc,v,vins,INSERT_VALUES,SCATTER_FORWARD);
    1521:     VecScatterDestroy(&sc);
    1522:   } else {
    1523:     vins = v;
    1524:   }
    1525:   VecGetArrayRead(vins,&array);
    1526:   VecDestroy(&p);

    1528:   /* insert in global quadrature vecs */
    1529:   MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);
    1530:   for (i=1;i<n_neigh;i++) {
    1531:     const PetscInt    *idxs;
    1532:     PetscInt          idx,nn,j;

    1534:     idxs = shared[i];
    1535:     nn   = n_shared[i];
    1536:     for (j=0;j<nn;j++) vals[j] = array[idxs[j]];
    1537:     PetscFindInt(rank,graph->count[idxs[0]],graph->neighbours_set[idxs[0]],&idx);
    1538:     idx  = -(idx+1);
    1539:     if (idx < 0 || idx >= maxneighs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid index %D not in [0,%D)",idx,maxneighs);
    1540:     ISLocalToGlobalMappingApply(map,nn,idxs,gidxs);
    1541:     VecSetValues(quad_vecs[idx],nn,gidxs,vals,INSERT_VALUES);
    1542:   }
    1543:   ISLocalToGlobalMappingRestoreInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);
    1544:   VecRestoreArrayRead(vins,&array);
    1545:   if (vl2l) {
    1546:     VecDestroy(&vins);
    1547:   }
    1548:   VecDestroy(&v);
    1549:   PetscFree2(gidxs,vals);

    1551:   /* assemble near null space */
    1552:   for (i=0;i<maxneighs;i++) {
    1553:     VecAssemblyBegin(quad_vecs[i]);
    1554:   }
    1555:   for (i=0;i<maxneighs;i++) {
    1556:     VecAssemblyEnd(quad_vecs[i]);
    1557:     VecViewFromOptions(quad_vecs[i],NULL,"-pc_bddc_quad_vecs_view");
    1558:     VecLockReadPush(quad_vecs[i]);
    1559:   }
    1560:   VecDestroyVecs(maxneighs,&quad_vecs);
    1561:   return(0);
    1562: }

    1564: PetscErrorCode PCBDDCAddPrimalVerticesLocalIS(PC pc, IS primalv)
    1565: {
    1566:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;

    1570:   if (primalv) {
    1571:     if (pcbddc->user_primal_vertices_local) {
    1572:       IS list[2], newp;

    1574:       list[0] = primalv;
    1575:       list[1] = pcbddc->user_primal_vertices_local;
    1576:       ISConcatenate(PetscObjectComm((PetscObject)pc),2,list,&newp);
    1577:       ISSortRemoveDups(newp);
    1578:       ISDestroy(&list[1]);
    1579:       pcbddc->user_primal_vertices_local = newp;
    1580:     } else {
    1581:       PCBDDCSetPrimalVerticesLocalIS(pc,primalv);
    1582:     }
    1583:   }
    1584:   return(0);
    1585: }

    1587: static PetscErrorCode func_coords_private(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nf, PetscScalar *out, void *ctx)
    1588: {
    1589:   PetscInt f, *comp  = (PetscInt *)ctx;

    1592:   for (f=0;f<Nf;f++) out[f] = X[*comp];
    1593:   return(0);
    1594: }

    1596: PetscErrorCode PCBDDCComputeLocalTopologyInfo(PC pc)
    1597: {
    1599:   Vec            local,global;
    1600:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    1601:   Mat_IS         *matis = (Mat_IS*)pc->pmat->data;
    1602:   PetscBool      monolithic = PETSC_FALSE;

    1605:   PetscOptionsBegin(PetscObjectComm((PetscObject)pc),((PetscObject)pc)->prefix,"BDDC topology options","PC");
    1606:   PetscOptionsBool("-pc_bddc_monolithic","Discard any information on dofs splitting",NULL,monolithic,&monolithic,NULL);
    1607:   PetscOptionsEnd();
    1608:   /* need to convert from global to local topology information and remove references to information in global ordering */
    1609:   MatCreateVecs(pc->pmat,&global,NULL);
    1610:   MatCreateVecs(matis->A,&local,NULL);
    1611:   VecBindToCPU(global,PETSC_TRUE);
    1612:   VecBindToCPU(local,PETSC_TRUE);
    1613:   if (monolithic) { /* just get block size to properly compute vertices */
    1614:     if (pcbddc->vertex_size == 1) {
    1615:       MatGetBlockSize(pc->pmat,&pcbddc->vertex_size);
    1616:     }
    1617:     goto boundary;
    1618:   }

    1620:   if (pcbddc->user_provided_isfordofs) {
    1621:     if (pcbddc->n_ISForDofs) {
    1622:       PetscInt i;

    1624:       PetscMalloc1(pcbddc->n_ISForDofs,&pcbddc->ISForDofsLocal);
    1625:       for (i=0;i<pcbddc->n_ISForDofs;i++) {
    1626:         PetscInt bs;

    1628:         PCBDDCGlobalToLocal(matis->rctx,global,local,pcbddc->ISForDofs[i],&pcbddc->ISForDofsLocal[i]);
    1629:         ISGetBlockSize(pcbddc->ISForDofs[i],&bs);
    1630:         ISSetBlockSize(pcbddc->ISForDofsLocal[i],bs);
    1631:         ISDestroy(&pcbddc->ISForDofs[i]);
    1632:       }
    1633:       pcbddc->n_ISForDofsLocal = pcbddc->n_ISForDofs;
    1634:       pcbddc->n_ISForDofs = 0;
    1635:       PetscFree(pcbddc->ISForDofs);
    1636:     }
    1637:   } else {
    1638:     if (!pcbddc->n_ISForDofsLocal) { /* field split not present */
    1639:       DM dm;

    1641:       MatGetDM(pc->pmat, &dm);
    1642:       if (!dm) {
    1643:         PCGetDM(pc, &dm);
    1644:       }
    1645:       if (dm) {
    1646:         IS      *fields;
    1647:         PetscInt nf,i;

    1649:         DMCreateFieldDecomposition(dm,&nf,NULL,&fields,NULL);
    1650:         PetscMalloc1(nf,&pcbddc->ISForDofsLocal);
    1651:         for (i=0;i<nf;i++) {
    1652:           PetscInt bs;

    1654:           PCBDDCGlobalToLocal(matis->rctx,global,local,fields[i],&pcbddc->ISForDofsLocal[i]);
    1655:           ISGetBlockSize(fields[i],&bs);
    1656:           ISSetBlockSize(pcbddc->ISForDofsLocal[i],bs);
    1657:           ISDestroy(&fields[i]);
    1658:         }
    1659:         PetscFree(fields);
    1660:         pcbddc->n_ISForDofsLocal = nf;
    1661:       } else { /* See if MATIS has fields attached by the conversion from MatNest */
    1662:         PetscContainer   c;

    1664:         PetscObjectQuery((PetscObject)pc->pmat,"_convert_nest_lfields",(PetscObject*)&c);
    1665:         if (c) {
    1666:           MatISLocalFields lf;
    1667:           PetscContainerGetPointer(c,(void**)&lf);
    1668:           PCBDDCSetDofsSplittingLocal(pc,lf->nr,lf->rf);
    1669:         } else { /* fallback, create the default fields if bs > 1 */
    1670:           PetscInt i, n = matis->A->rmap->n;
    1671:           MatGetBlockSize(pc->pmat,&i);
    1672:           if (i > 1) {
    1673:             pcbddc->n_ISForDofsLocal = i;
    1674:             PetscMalloc1(pcbddc->n_ISForDofsLocal,&pcbddc->ISForDofsLocal);
    1675:             for (i=0;i<pcbddc->n_ISForDofsLocal;i++) {
    1676:               ISCreateStride(PetscObjectComm((PetscObject)pc),n/pcbddc->n_ISForDofsLocal,i,pcbddc->n_ISForDofsLocal,&pcbddc->ISForDofsLocal[i]);
    1677:             }
    1678:           }
    1679:         }
    1680:       }
    1681:     } else {
    1682:       PetscInt i;
    1683:       for (i=0;i<pcbddc->n_ISForDofsLocal;i++) {
    1684:         PCBDDCConsistencyCheckIS(pc,MPI_LAND,&pcbddc->ISForDofsLocal[i]);
    1685:       }
    1686:     }
    1687:   }

    1689: boundary:
    1690:   if (!pcbddc->DirichletBoundariesLocal && pcbddc->DirichletBoundaries) {
    1691:     PCBDDCGlobalToLocal(matis->rctx,global,local,pcbddc->DirichletBoundaries,&pcbddc->DirichletBoundariesLocal);
    1692:   } else if (pcbddc->DirichletBoundariesLocal) {
    1693:     PCBDDCConsistencyCheckIS(pc,MPI_LAND,&pcbddc->DirichletBoundariesLocal);
    1694:   }
    1695:   if (!pcbddc->NeumannBoundariesLocal && pcbddc->NeumannBoundaries) {
    1696:     PCBDDCGlobalToLocal(matis->rctx,global,local,pcbddc->NeumannBoundaries,&pcbddc->NeumannBoundariesLocal);
    1697:   } else if (pcbddc->NeumannBoundariesLocal) {
    1698:     PCBDDCConsistencyCheckIS(pc,MPI_LOR,&pcbddc->NeumannBoundariesLocal);
    1699:   }
    1700:   if (!pcbddc->user_primal_vertices_local && pcbddc->user_primal_vertices) {
    1701:     PCBDDCGlobalToLocal(matis->rctx,global,local,pcbddc->user_primal_vertices,&pcbddc->user_primal_vertices_local);
    1702:   }
    1703:   VecDestroy(&global);
    1704:   VecDestroy(&local);
    1705:   /* detect local disconnected subdomains if requested (use matis->A) */
    1706:   if (pcbddc->detect_disconnected) {
    1707:     IS        primalv = NULL;
    1708:     PetscInt  i;
    1709:     PetscBool filter = pcbddc->detect_disconnected_filter;

    1711:     for (i=0;i<pcbddc->n_local_subs;i++) {
    1712:       ISDestroy(&pcbddc->local_subs[i]);
    1713:     }
    1714:     PetscFree(pcbddc->local_subs);
    1715:     PCBDDCDetectDisconnectedComponents(pc,filter,&pcbddc->n_local_subs,&pcbddc->local_subs,&primalv);
    1716:     PCBDDCAddPrimalVerticesLocalIS(pc,primalv);
    1717:     ISDestroy(&primalv);
    1718:   }
    1719:   /* early stage corner detection */
    1720:   {
    1721:     DM dm;

    1723:     MatGetDM(pc->pmat,&dm);
    1724:     if (!dm) {
    1725:       PCGetDM(pc,&dm);
    1726:     }
    1727:     if (dm) {
    1728:       PetscBool isda;

    1730:       PetscObjectTypeCompare((PetscObject)dm,DMDA,&isda);
    1731:       if (isda) {
    1732:         ISLocalToGlobalMapping l2l;
    1733:         IS                     corners;
    1734:         Mat                    lA;
    1735:         PetscBool              gl,lo;

    1737:         {
    1738:           Vec               cvec;
    1739:           const PetscScalar *coords;
    1740:           PetscInt          dof,n,cdim;
    1741:           PetscBool         memc = PETSC_TRUE;

    1743:           DMDAGetInfo(dm,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL);
    1744:           DMGetCoordinates(dm,&cvec);
    1745:           VecGetLocalSize(cvec,&n);
    1746:           VecGetBlockSize(cvec,&cdim);
    1747:           n   /= cdim;
    1748:           PetscFree(pcbddc->mat_graph->coords);
    1749:           PetscMalloc1(dof*n*cdim,&pcbddc->mat_graph->coords);
    1750:           VecGetArrayRead(cvec,&coords);
    1751: #if defined(PETSC_USE_COMPLEX)
    1752:           memc = PETSC_FALSE;
    1753: #endif
    1754:           if (dof != 1) memc = PETSC_FALSE;
    1755:           if (memc) {
    1756:             PetscArraycpy(pcbddc->mat_graph->coords,coords,cdim*n*dof);
    1757:           } else { /* BDDC graph does not use any blocked information, we need to replicate the data */
    1758:             PetscReal *bcoords = pcbddc->mat_graph->coords;
    1759:             PetscInt  i, b, d;

    1761:             for (i=0;i<n;i++) {
    1762:               for (b=0;b<dof;b++) {
    1763:                 for (d=0;d<cdim;d++) {
    1764:                   bcoords[i*dof*cdim + b*cdim + d] = PetscRealPart(coords[i*cdim+d]);
    1765:                 }
    1766:               }
    1767:             }
    1768:           }
    1769:           VecRestoreArrayRead(cvec,&coords);
    1770:           pcbddc->mat_graph->cdim  = cdim;
    1771:           pcbddc->mat_graph->cnloc = dof*n;
    1772:           pcbddc->mat_graph->cloc  = PETSC_FALSE;
    1773:         }
    1774:         DMDAGetSubdomainCornersIS(dm,&corners);
    1775:         MatISGetLocalMat(pc->pmat,&lA);
    1776:         MatGetLocalToGlobalMapping(lA,&l2l,NULL);
    1777:         MatISRestoreLocalMat(pc->pmat,&lA);
    1778:         lo   = (PetscBool)(l2l && corners);
    1779:         MPIU_Allreduce(&lo,&gl,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)pc));
    1780:         if (gl) { /* From PETSc's DMDA */
    1781:           const PetscInt    *idx;
    1782:           PetscInt          dof,bs,*idxout,n;

    1784:           DMDAGetInfo(dm,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL);
    1785:           ISLocalToGlobalMappingGetBlockSize(l2l,&bs);
    1786:           ISGetLocalSize(corners,&n);
    1787:           ISGetIndices(corners,&idx);
    1788:           if (bs == dof) {
    1789:             PetscMalloc1(n,&idxout);
    1790:             ISLocalToGlobalMappingApplyBlock(l2l,n,idx,idxout);
    1791:           } else { /* the original DMDA local-to-local map have been modified */
    1792:             PetscInt i,d;

    1794:             PetscMalloc1(dof*n,&idxout);
    1795:             for (i=0;i<n;i++) for (d=0;d<dof;d++) idxout[dof*i+d] = dof*idx[i]+d;
    1796:             ISLocalToGlobalMappingApply(l2l,dof*n,idxout,idxout);

    1798:             bs = 1;
    1799:             n *= dof;
    1800:           }
    1801:           ISRestoreIndices(corners,&idx);
    1802:           DMDARestoreSubdomainCornersIS(dm,&corners);
    1803:           ISCreateBlock(PetscObjectComm((PetscObject)pc),bs,n,idxout,PETSC_OWN_POINTER,&corners);
    1804:           PCBDDCAddPrimalVerticesLocalIS(pc,corners);
    1805:           ISDestroy(&corners);
    1806:           pcbddc->corner_selected  = PETSC_TRUE;
    1807:           pcbddc->corner_selection = PETSC_TRUE;
    1808:         }
    1809:         if (corners) {
    1810:           DMDARestoreSubdomainCornersIS(dm,&corners);
    1811:         }
    1812:       }
    1813:     }
    1814:   }
    1815:   if (pcbddc->corner_selection && !pcbddc->mat_graph->cdim) {
    1816:     DM dm;

    1818:     MatGetDM(pc->pmat,&dm);
    1819:     if (!dm) {
    1820:       PCGetDM(pc,&dm);
    1821:     }
    1822:     if (dm) { /* this can get very expensive, I need to find a faster alternative */
    1823:       Vec            vcoords;
    1824:       PetscSection   section;
    1825:       PetscReal      *coords;
    1826:       PetscInt       d,cdim,nl,nf,**ctxs;
    1827:       PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *);

    1829:       DMGetCoordinateDim(dm,&cdim);
    1830:       DMGetLocalSection(dm,&section);
    1831:       PetscSectionGetNumFields(section,&nf);
    1832:       DMCreateGlobalVector(dm,&vcoords);
    1833:       VecGetLocalSize(vcoords,&nl);
    1834:       PetscMalloc1(nl*cdim,&coords);
    1835:       PetscMalloc2(nf,&funcs,nf,&ctxs);
    1836:       PetscMalloc1(nf,&ctxs[0]);
    1837:       for (d=0;d<nf;d++) funcs[d] = func_coords_private;
    1838:       for (d=1;d<nf;d++) ctxs[d] = ctxs[d-1] + 1;
    1839:       for (d=0;d<cdim;d++) {
    1840:         PetscInt          i;
    1841:         const PetscScalar *v;

    1843:         for (i=0;i<nf;i++) ctxs[i][0] = d;
    1844:         DMProjectFunction(dm,0.0,funcs,(void**)ctxs,INSERT_VALUES,vcoords);
    1845:         VecGetArrayRead(vcoords,&v);
    1846:         for (i=0;i<nl;i++) coords[i*cdim+d] = PetscRealPart(v[i]);
    1847:         VecRestoreArrayRead(vcoords,&v);
    1848:       }
    1849:       VecDestroy(&vcoords);
    1850:       PCSetCoordinates(pc,cdim,nl,coords);
    1851:       PetscFree(coords);
    1852:       PetscFree(ctxs[0]);
    1853:       PetscFree2(funcs,ctxs);
    1854:     }
    1855:   }
    1856:   return(0);
    1857: }

    1859: PetscErrorCode PCBDDCConsistencyCheckIS(PC pc, MPI_Op mop, IS *is)
    1860: {
    1861:   Mat_IS          *matis = (Mat_IS*)(pc->pmat->data);
    1862:   PetscErrorCode  ierr;
    1863:   IS              nis;
    1864:   const PetscInt  *idxs;
    1865:   PetscInt        i,nd,n = matis->A->rmap->n,*nidxs,nnd;

    1868:   if (mop != MPI_LAND && mop != MPI_LOR) SETERRQ(PetscObjectComm((PetscObject)(pc)),PETSC_ERR_SUP,"Supported are MPI_LAND and MPI_LOR");
    1869:   if (mop == MPI_LAND) {
    1870:     /* init rootdata with true */
    1871:     for (i=0;i<pc->pmat->rmap->n;i++) matis->sf_rootdata[i] = 1;
    1872:   } else {
    1873:     PetscArrayzero(matis->sf_rootdata,pc->pmat->rmap->n);
    1874:   }
    1875:   PetscArrayzero(matis->sf_leafdata,n);
    1876:   ISGetLocalSize(*is,&nd);
    1877:   ISGetIndices(*is,&idxs);
    1878:   for (i=0;i<nd;i++)
    1879:     if (-1 < idxs[i] && idxs[i] < n)
    1880:       matis->sf_leafdata[idxs[i]] = 1;
    1881:   ISRestoreIndices(*is,&idxs);
    1882:   PetscSFReduceBegin(matis->sf,MPIU_INT,matis->sf_leafdata,matis->sf_rootdata,mop);
    1883:   PetscSFReduceEnd(matis->sf,MPIU_INT,matis->sf_leafdata,matis->sf_rootdata,mop);
    1884:   PetscSFBcastBegin(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata,MPI_REPLACE);
    1885:   PetscSFBcastEnd(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata,MPI_REPLACE);
    1886:   if (mop == MPI_LAND) {
    1887:     PetscMalloc1(nd,&nidxs);
    1888:   } else {
    1889:     PetscMalloc1(n,&nidxs);
    1890:   }
    1891:   for (i=0,nnd=0;i<n;i++)
    1892:     if (matis->sf_leafdata[i])
    1893:       nidxs[nnd++] = i;
    1894:   ISCreateGeneral(PetscObjectComm((PetscObject)(*is)),nnd,nidxs,PETSC_OWN_POINTER,&nis);
    1895:   ISDestroy(is);
    1896:   *is  = nis;
    1897:   return(0);
    1898: }

    1900: PetscErrorCode PCBDDCBenignRemoveInterior(PC pc,Vec r,Vec z)
    1901: {
    1902:   PC_IS             *pcis = (PC_IS*)(pc->data);
    1903:   PC_BDDC           *pcbddc = (PC_BDDC*)(pc->data);
    1904:   PetscErrorCode    ierr;

    1907:   if (!pcbddc->benign_have_null) {
    1908:     return(0);
    1909:   }
    1910:   if (pcbddc->ChangeOfBasisMatrix) {
    1911:     Vec swap;

    1913:     MatMultTranspose(pcbddc->ChangeOfBasisMatrix,r,pcbddc->work_change);
    1914:     swap = pcbddc->work_change;
    1915:     pcbddc->work_change = r;
    1916:     r = swap;
    1917:   }
    1918:   VecScatterBegin(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    1919:   VecScatterEnd(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    1920:   PetscLogEventBegin(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    1921:   KSPSolve(pcbddc->ksp_D,pcis->vec1_D,pcis->vec2_D);
    1922:   PetscLogEventEnd(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    1923:   KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec2_D);
    1924:   VecSet(z,0.);
    1925:   VecScatterBegin(pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
    1926:   VecScatterEnd(pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
    1927:   if (pcbddc->ChangeOfBasisMatrix) {
    1928:     pcbddc->work_change = r;
    1929:     VecCopy(z,pcbddc->work_change);
    1930:     MatMult(pcbddc->ChangeOfBasisMatrix,pcbddc->work_change,z);
    1931:   }
    1932:   return(0);
    1933: }

    1935: PetscErrorCode PCBDDCBenignMatMult_Private_Private(Mat A, Vec x, Vec y, PetscBool transpose)
    1936: {
    1937:   PCBDDCBenignMatMult_ctx ctx;
    1938:   PetscErrorCode          ierr;
    1939:   PetscBool               apply_right,apply_left,reset_x;

    1942:   MatShellGetContext(A,&ctx);
    1943:   if (transpose) {
    1944:     apply_right = ctx->apply_left;
    1945:     apply_left = ctx->apply_right;
    1946:   } else {
    1947:     apply_right = ctx->apply_right;
    1948:     apply_left = ctx->apply_left;
    1949:   }
    1950:   reset_x = PETSC_FALSE;
    1951:   if (apply_right) {
    1952:     const PetscScalar *ax;
    1953:     PetscInt          nl,i;

    1955:     VecGetLocalSize(x,&nl);
    1956:     VecGetArrayRead(x,&ax);
    1957:     PetscArraycpy(ctx->work,ax,nl);
    1958:     VecRestoreArrayRead(x,&ax);
    1959:     for (i=0;i<ctx->benign_n;i++) {
    1960:       PetscScalar    sum,val;
    1961:       const PetscInt *idxs;
    1962:       PetscInt       nz,j;
    1963:       ISGetLocalSize(ctx->benign_zerodiag_subs[i],&nz);
    1964:       ISGetIndices(ctx->benign_zerodiag_subs[i],&idxs);
    1965:       sum = 0.;
    1966:       if (ctx->apply_p0) {
    1967:         val = ctx->work[idxs[nz-1]];
    1968:         for (j=0;j<nz-1;j++) {
    1969:           sum += ctx->work[idxs[j]];
    1970:           ctx->work[idxs[j]] += val;
    1971:         }
    1972:       } else {
    1973:         for (j=0;j<nz-1;j++) {
    1974:           sum += ctx->work[idxs[j]];
    1975:         }
    1976:       }
    1977:       ctx->work[idxs[nz-1]] -= sum;
    1978:       ISRestoreIndices(ctx->benign_zerodiag_subs[i],&idxs);
    1979:     }
    1980:     VecPlaceArray(x,ctx->work);
    1981:     reset_x = PETSC_TRUE;
    1982:   }
    1983:   if (transpose) {
    1984:     MatMultTranspose(ctx->A,x,y);
    1985:   } else {
    1986:     MatMult(ctx->A,x,y);
    1987:   }
    1988:   if (reset_x) {
    1989:     VecResetArray(x);
    1990:   }
    1991:   if (apply_left) {
    1992:     PetscScalar *ay;
    1993:     PetscInt    i;

    1995:     VecGetArray(y,&ay);
    1996:     for (i=0;i<ctx->benign_n;i++) {
    1997:       PetscScalar    sum,val;
    1998:       const PetscInt *idxs;
    1999:       PetscInt       nz,j;
    2000:       ISGetLocalSize(ctx->benign_zerodiag_subs[i],&nz);
    2001:       ISGetIndices(ctx->benign_zerodiag_subs[i],&idxs);
    2002:       val = -ay[idxs[nz-1]];
    2003:       if (ctx->apply_p0) {
    2004:         sum = 0.;
    2005:         for (j=0;j<nz-1;j++) {
    2006:           sum += ay[idxs[j]];
    2007:           ay[idxs[j]] += val;
    2008:         }
    2009:         ay[idxs[nz-1]] += sum;
    2010:       } else {
    2011:         for (j=0;j<nz-1;j++) {
    2012:           ay[idxs[j]] += val;
    2013:         }
    2014:         ay[idxs[nz-1]] = 0.;
    2015:       }
    2016:       ISRestoreIndices(ctx->benign_zerodiag_subs[i],&idxs);
    2017:     }
    2018:     VecRestoreArray(y,&ay);
    2019:   }
    2020:   return(0);
    2021: }

    2023: PetscErrorCode PCBDDCBenignMatMultTranspose_Private(Mat A, Vec x, Vec y)
    2024: {

    2028:   PCBDDCBenignMatMult_Private_Private(A,x,y,PETSC_TRUE);
    2029:   return(0);
    2030: }

    2032: PetscErrorCode PCBDDCBenignMatMult_Private(Mat A, Vec x, Vec y)
    2033: {

    2037:   PCBDDCBenignMatMult_Private_Private(A,x,y,PETSC_FALSE);
    2038:   return(0);
    2039: }

    2041: PetscErrorCode PCBDDCBenignShellMat(PC pc, PetscBool restore)
    2042: {
    2043:   PC_IS                   *pcis = (PC_IS*)pc->data;
    2044:   PC_BDDC                 *pcbddc = (PC_BDDC*)pc->data;
    2045:   PCBDDCBenignMatMult_ctx ctx;
    2046:   PetscErrorCode          ierr;

    2049:   if (!restore) {
    2050:     Mat                A_IB,A_BI;
    2051:     PetscScalar        *work;
    2052:     PCBDDCReuseSolvers reuse = pcbddc->sub_schurs ? pcbddc->sub_schurs->reuse_solver : NULL;

    2054:     if (pcbddc->benign_original_mat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Benign original mat has not been restored");
    2055:     if (!pcbddc->benign_change || !pcbddc->benign_n || pcbddc->benign_change_explicit) return(0);
    2056:     PetscMalloc1(pcis->n,&work);
    2057:     MatCreate(PETSC_COMM_SELF,&A_IB);
    2058:     MatSetSizes(A_IB,pcis->n-pcis->n_B,pcis->n_B,PETSC_DECIDE,PETSC_DECIDE);
    2059:     MatSetType(A_IB,MATSHELL);
    2060:     MatShellSetOperation(A_IB,MATOP_MULT,(void (*)(void))PCBDDCBenignMatMult_Private);
    2061:     MatShellSetOperation(A_IB,MATOP_MULT_TRANSPOSE,(void (*)(void))PCBDDCBenignMatMultTranspose_Private);
    2062:     PetscNew(&ctx);
    2063:     MatShellSetContext(A_IB,ctx);
    2064:     ctx->apply_left = PETSC_TRUE;
    2065:     ctx->apply_right = PETSC_FALSE;
    2066:     ctx->apply_p0 = PETSC_FALSE;
    2067:     ctx->benign_n = pcbddc->benign_n;
    2068:     if (reuse) {
    2069:       ctx->benign_zerodiag_subs = reuse->benign_zerodiag_subs;
    2070:       ctx->free = PETSC_FALSE;
    2071:     } else { /* TODO: could be optimized for successive solves */
    2072:       ISLocalToGlobalMapping N_to_D;
    2073:       PetscInt               i;

    2075:       ISLocalToGlobalMappingCreateIS(pcis->is_I_local,&N_to_D);
    2076:       PetscMalloc1(pcbddc->benign_n,&ctx->benign_zerodiag_subs);
    2077:       for (i=0;i<pcbddc->benign_n;i++) {
    2078:         ISGlobalToLocalMappingApplyIS(N_to_D,IS_GTOLM_DROP,pcbddc->benign_zerodiag_subs[i],&ctx->benign_zerodiag_subs[i]);
    2079:       }
    2080:       ISLocalToGlobalMappingDestroy(&N_to_D);
    2081:       ctx->free = PETSC_TRUE;
    2082:     }
    2083:     ctx->A = pcis->A_IB;
    2084:     ctx->work = work;
    2085:     MatSetUp(A_IB);
    2086:     MatAssemblyBegin(A_IB,MAT_FINAL_ASSEMBLY);
    2087:     MatAssemblyEnd(A_IB,MAT_FINAL_ASSEMBLY);
    2088:     pcis->A_IB = A_IB;

    2090:     /* A_BI as A_IB^T */
    2091:     MatCreateTranspose(A_IB,&A_BI);
    2092:     pcbddc->benign_original_mat = pcis->A_BI;
    2093:     pcis->A_BI = A_BI;
    2094:   } else {
    2095:     if (!pcbddc->benign_original_mat) {
    2096:       return(0);
    2097:     }
    2098:     MatShellGetContext(pcis->A_IB,&ctx);
    2099:     MatDestroy(&pcis->A_IB);
    2100:     pcis->A_IB = ctx->A;
    2101:     ctx->A = NULL;
    2102:     MatDestroy(&pcis->A_BI);
    2103:     pcis->A_BI = pcbddc->benign_original_mat;
    2104:     pcbddc->benign_original_mat = NULL;
    2105:     if (ctx->free) {
    2106:       PetscInt i;
    2107:       for (i=0;i<ctx->benign_n;i++) {
    2108:         ISDestroy(&ctx->benign_zerodiag_subs[i]);
    2109:       }
    2110:       PetscFree(ctx->benign_zerodiag_subs);
    2111:     }
    2112:     PetscFree(ctx->work);
    2113:     PetscFree(ctx);
    2114:   }
    2115:   return(0);
    2116: }

    2118: /* used just in bddc debug mode */
    2119: PetscErrorCode PCBDDCBenignProject(PC pc, IS is1, IS is2, Mat *B)
    2120: {
    2121:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    2122:   Mat_IS         *matis = (Mat_IS*)pc->pmat->data;
    2123:   Mat            An;

    2127:   MatPtAP(matis->A,pcbddc->benign_change,MAT_INITIAL_MATRIX,2.0,&An);
    2128:   MatZeroRowsColumns(An,pcbddc->benign_n,pcbddc->benign_p0_lidx,1.0,NULL,NULL);
    2129:   if (is1) {
    2130:     MatCreateSubMatrix(An,is1,is2,MAT_INITIAL_MATRIX,B);
    2131:     MatDestroy(&An);
    2132:   } else {
    2133:     *B = An;
    2134:   }
    2135:   return(0);
    2136: }

    2138: /* TODO: add reuse flag */
    2139: PetscErrorCode MatSeqAIJCompress(Mat A, Mat *B)
    2140: {
    2141:   Mat            Bt;
    2142:   PetscScalar    *a,*bdata;
    2143:   const PetscInt *ii,*ij;
    2144:   PetscInt       m,n,i,nnz,*bii,*bij;
    2145:   PetscBool      flg_row;

    2149:   MatGetSize(A,&n,&m);
    2150:   MatGetRowIJ(A,0,PETSC_FALSE,PETSC_FALSE,&n,&ii,&ij,&flg_row);
    2151:   MatSeqAIJGetArray(A,&a);
    2152:   nnz = n;
    2153:   for (i=0;i<ii[n];i++) {
    2154:     if (PetscLikely(PetscAbsScalar(a[i]) > PETSC_SMALL)) nnz++;
    2155:   }
    2156:   PetscMalloc1(n+1,&bii);
    2157:   PetscMalloc1(nnz,&bij);
    2158:   PetscMalloc1(nnz,&bdata);
    2159:   nnz = 0;
    2160:   bii[0] = 0;
    2161:   for (i=0;i<n;i++) {
    2162:     PetscInt j;
    2163:     for (j=ii[i];j<ii[i+1];j++) {
    2164:       PetscScalar entry = a[j];
    2165:       if (PetscLikely(PetscAbsScalar(entry) > PETSC_SMALL) || (n == m && ij[j] == i)) {
    2166:         bij[nnz] = ij[j];
    2167:         bdata[nnz] = entry;
    2168:         nnz++;
    2169:       }
    2170:     }
    2171:     bii[i+1] = nnz;
    2172:   }
    2173:   MatSeqAIJRestoreArray(A,&a);
    2174:   MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),n,m,bii,bij,bdata,&Bt);
    2175:   MatRestoreRowIJ(A,0,PETSC_FALSE,PETSC_FALSE,&n,&ii,&ij,&flg_row);
    2176:   {
    2177:     Mat_SeqAIJ *b = (Mat_SeqAIJ*)(Bt->data);
    2178:     b->free_a = PETSC_TRUE;
    2179:     b->free_ij = PETSC_TRUE;
    2180:   }
    2181:   if (*B == A) {
    2182:     MatDestroy(&A);
    2183:   }
    2184:   *B = Bt;
    2185:   return(0);
    2186: }

    2188: PetscErrorCode PCBDDCDetectDisconnectedComponents(PC pc, PetscBool filter, PetscInt *ncc, IS* cc[], IS* primalv)
    2189: {
    2190:   Mat                    B = NULL;
    2191:   DM                     dm;
    2192:   IS                     is_dummy,*cc_n;
    2193:   ISLocalToGlobalMapping l2gmap_dummy;
    2194:   PCBDDCGraph            graph;
    2195:   PetscInt               *xadj_filtered = NULL,*adjncy_filtered = NULL;
    2196:   PetscInt               i,n;
    2197:   PetscInt               *xadj,*adjncy;
    2198:   PetscBool              isplex = PETSC_FALSE;
    2199:   PetscErrorCode         ierr;

    2202:   if (ncc) *ncc = 0;
    2203:   if (cc) *cc = NULL;
    2204:   if (primalv) *primalv = NULL;
    2205:   PCBDDCGraphCreate(&graph);
    2206:   MatGetDM(pc->pmat,&dm);
    2207:   if (!dm) {
    2208:     PCGetDM(pc,&dm);
    2209:   }
    2210:   if (dm) {
    2211:     PetscObjectTypeCompare((PetscObject)dm,DMPLEX,&isplex);
    2212:   }
    2213:   if (filter) isplex = PETSC_FALSE;

    2215:   if (isplex) { /* this code has been modified from plexpartition.c */
    2216:     PetscInt       p, pStart, pEnd, a, adjSize, idx, size, nroots;
    2217:     PetscInt      *adj = NULL;
    2218:     IS             cellNumbering;
    2219:     const PetscInt *cellNum;
    2220:     PetscBool      useCone, useClosure;
    2221:     PetscSection   section;
    2222:     PetscSegBuffer adjBuffer;
    2223:     PetscSF        sfPoint;

    2226:     DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);
    2227:     DMGetPointSF(dm, &sfPoint);
    2228:     PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);
    2229:     /* Build adjacency graph via a section/segbuffer */
    2230:     PetscSectionCreate(PetscObjectComm((PetscObject) dm), &section);
    2231:     PetscSectionSetChart(section, pStart, pEnd);
    2232:     PetscSegBufferCreate(sizeof(PetscInt),1000,&adjBuffer);
    2233:     /* Always use FVM adjacency to create partitioner graph */
    2234:     DMGetBasicAdjacency(dm, &useCone, &useClosure);
    2235:     DMSetBasicAdjacency(dm, PETSC_TRUE, PETSC_FALSE);
    2236:     DMPlexGetCellNumbering(dm, &cellNumbering);
    2237:     ISGetIndices(cellNumbering, &cellNum);
    2238:     for (n = 0, p = pStart; p < pEnd; p++) {
    2239:       /* Skip non-owned cells in parallel (ParMetis expects no overlap) */
    2240:       if (nroots > 0) {if (cellNum[p] < 0) continue;}
    2241:       adjSize = PETSC_DETERMINE;
    2242:       DMPlexGetAdjacency(dm, p, &adjSize, &adj);
    2243:       for (a = 0; a < adjSize; ++a) {
    2244:         const PetscInt point = adj[a];
    2245:         if (pStart <= point && point < pEnd) {
    2246:           PetscInt *PETSC_RESTRICT pBuf;
    2247:           PetscSectionAddDof(section, p, 1);
    2248:           PetscSegBufferGetInts(adjBuffer, 1, &pBuf);
    2249:           *pBuf = point;
    2250:         }
    2251:       }
    2252:       n++;
    2253:     }
    2254:     DMSetBasicAdjacency(dm, useCone, useClosure);
    2255:     /* Derive CSR graph from section/segbuffer */
    2256:     PetscSectionSetUp(section);
    2257:     PetscSectionGetStorageSize(section, &size);
    2258:     PetscMalloc1(n+1, &xadj);
    2259:     for (idx = 0, p = pStart; p < pEnd; p++) {
    2260:       if (nroots > 0) {if (cellNum[p] < 0) continue;}
    2261:       PetscSectionGetOffset(section, p, &(xadj[idx++]));
    2262:     }
    2263:     xadj[n] = size;
    2264:     PetscSegBufferExtractAlloc(adjBuffer, &adjncy);
    2265:     /* Clean up */
    2266:     PetscSegBufferDestroy(&adjBuffer);
    2267:     PetscSectionDestroy(&section);
    2268:     PetscFree(adj);
    2269:     graph->xadj = xadj;
    2270:     graph->adjncy = adjncy;
    2271:   } else {
    2272:     Mat       A;
    2273:     PetscBool isseqaij, flg_row;

    2275:     MatISGetLocalMat(pc->pmat,&A);
    2276:     if (!A->rmap->N || !A->cmap->N) {
    2277:       PCBDDCGraphDestroy(&graph);
    2278:       return(0);
    2279:     }
    2280:     PetscObjectBaseTypeCompare((PetscObject)A,MATSEQAIJ,&isseqaij);
    2281:     if (!isseqaij && filter) {
    2282:       PetscBool isseqdense;

    2284:       PetscObjectTypeCompare((PetscObject)A,MATSEQDENSE,&isseqdense);
    2285:       if (!isseqdense) {
    2286:         MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);
    2287:       } else { /* TODO: rectangular case and LDA */
    2288:         PetscScalar *array;
    2289:         PetscReal   chop=1.e-6;

    2291:         MatDuplicate(A,MAT_COPY_VALUES,&B);
    2292:         MatDenseGetArray(B,&array);
    2293:         MatGetSize(B,&n,NULL);
    2294:         for (i=0;i<n;i++) {
    2295:           PetscInt j;
    2296:           for (j=i+1;j<n;j++) {
    2297:             PetscReal thresh = chop*(PetscAbsScalar(array[i*(n+1)])+PetscAbsScalar(array[j*(n+1)]));
    2298:             if (PetscAbsScalar(array[i*n+j]) < thresh) array[i*n+j] = 0.;
    2299:             if (PetscAbsScalar(array[j*n+i]) < thresh) array[j*n+i] = 0.;
    2300:           }
    2301:         }
    2302:         MatDenseRestoreArray(B,&array);
    2303:         MatConvert(B,MATSEQAIJ,MAT_INPLACE_MATRIX,&B);
    2304:       }
    2305:     } else {
    2306:       PetscObjectReference((PetscObject)A);
    2307:       B = A;
    2308:     }
    2309:     MatGetRowIJ(B,0,PETSC_TRUE,PETSC_FALSE,&n,(const PetscInt**)&xadj,(const PetscInt**)&adjncy,&flg_row);

    2311:     /* if filter is true, then removes entries lower than PETSC_SMALL in magnitude */
    2312:     if (filter) {
    2313:       PetscScalar *data;
    2314:       PetscInt    j,cum;

    2316:       PetscCalloc2(n+1,&xadj_filtered,xadj[n],&adjncy_filtered);
    2317:       MatSeqAIJGetArray(B,&data);
    2318:       cum = 0;
    2319:       for (i=0;i<n;i++) {
    2320:         PetscInt t;

    2322:         for (j=xadj[i];j<xadj[i+1];j++) {
    2323:           if (PetscUnlikely(PetscAbsScalar(data[j]) < PETSC_SMALL)) {
    2324:             continue;
    2325:           }
    2326:           adjncy_filtered[cum+xadj_filtered[i]++] = adjncy[j];
    2327:         }
    2328:         t = xadj_filtered[i];
    2329:         xadj_filtered[i] = cum;
    2330:         cum += t;
    2331:       }
    2332:       MatSeqAIJRestoreArray(B,&data);
    2333:       graph->xadj = xadj_filtered;
    2334:       graph->adjncy = adjncy_filtered;
    2335:     } else {
    2336:       graph->xadj = xadj;
    2337:       graph->adjncy = adjncy;
    2338:     }
    2339:   }
    2340:   /* compute local connected components using PCBDDCGraph */
    2341:   ISCreateStride(PETSC_COMM_SELF,n,0,1,&is_dummy);
    2342:   ISLocalToGlobalMappingCreateIS(is_dummy,&l2gmap_dummy);
    2343:   ISDestroy(&is_dummy);
    2344:   PCBDDCGraphInit(graph,l2gmap_dummy,n,PETSC_MAX_INT);
    2345:   ISLocalToGlobalMappingDestroy(&l2gmap_dummy);
    2346:   PCBDDCGraphSetUp(graph,1,NULL,NULL,0,NULL,NULL);
    2347:   PCBDDCGraphComputeConnectedComponents(graph);

    2349:   /* partial clean up */
    2350:   PetscFree2(xadj_filtered,adjncy_filtered);
    2351:   if (B) {
    2352:     PetscBool flg_row;
    2353:     MatRestoreRowIJ(B,0,PETSC_TRUE,PETSC_FALSE,&n,(const PetscInt**)&xadj,(const PetscInt**)&adjncy,&flg_row);
    2354:     MatDestroy(&B);
    2355:   }
    2356:   if (isplex) {
    2357:     PetscFree(xadj);
    2358:     PetscFree(adjncy);
    2359:   }

    2361:   /* get back data */
    2362:   if (isplex) {
    2363:     if (ncc) *ncc = graph->ncc;
    2364:     if (cc || primalv) {
    2365:       Mat          A;
    2366:       PetscBT      btv,btvt;
    2367:       PetscSection subSection;
    2368:       PetscInt     *ids,cum,cump,*cids,*pids;

    2370:       DMPlexGetSubdomainSection(dm,&subSection);
    2371:       MatISGetLocalMat(pc->pmat,&A);
    2372:       PetscMalloc3(A->rmap->n,&ids,graph->ncc+1,&cids,A->rmap->n,&pids);
    2373:       PetscBTCreate(A->rmap->n,&btv);
    2374:       PetscBTCreate(A->rmap->n,&btvt);

    2376:       cids[0] = 0;
    2377:       for (i = 0, cump = 0, cum = 0; i < graph->ncc; i++) {
    2378:         PetscInt j;

    2380:         PetscBTMemzero(A->rmap->n,btvt);
    2381:         for (j = graph->cptr[i]; j < graph->cptr[i+1]; j++) {
    2382:           PetscInt k, size, *closure = NULL, cell = graph->queue[j];

    2384:           DMPlexGetTransitiveClosure(dm,cell,PETSC_TRUE,&size,&closure);
    2385:           for (k = 0; k < 2*size; k += 2) {
    2386:             PetscInt s, pp, p = closure[k], off, dof, cdof;

    2388:             PetscSectionGetConstraintDof(subSection,p,&cdof);
    2389:             PetscSectionGetOffset(subSection,p,&off);
    2390:             PetscSectionGetDof(subSection,p,&dof);
    2391:             for (s = 0; s < dof-cdof; s++) {
    2392:               if (PetscBTLookupSet(btvt,off+s)) continue;
    2393:               if (!PetscBTLookup(btv,off+s)) {
    2394:                 ids[cum++] = off+s;
    2395:               } else { /* cross-vertex */
    2396:                 pids[cump++] = off+s;
    2397:               }
    2398:             }
    2399:             DMPlexGetTreeParent(dm,p,&pp,NULL);
    2400:             if (pp != p) {
    2401:               PetscSectionGetConstraintDof(subSection,pp,&cdof);
    2402:               PetscSectionGetOffset(subSection,pp,&off);
    2403:               PetscSectionGetDof(subSection,pp,&dof);
    2404:               for (s = 0; s < dof-cdof; s++) {
    2405:                 if (PetscBTLookupSet(btvt,off+s)) continue;
    2406:                 if (!PetscBTLookup(btv,off+s)) {
    2407:                   ids[cum++] = off+s;
    2408:                 } else { /* cross-vertex */
    2409:                   pids[cump++] = off+s;
    2410:                 }
    2411:               }
    2412:             }
    2413:           }
    2414:           DMPlexRestoreTransitiveClosure(dm,cell,PETSC_TRUE,&size,&closure);
    2415:         }
    2416:         cids[i+1] = cum;
    2417:         /* mark dofs as already assigned */
    2418:         for (j = cids[i]; j < cids[i+1]; j++) {
    2419:           PetscBTSet(btv,ids[j]);
    2420:         }
    2421:       }
    2422:       if (cc) {
    2423:         PetscMalloc1(graph->ncc,&cc_n);
    2424:         for (i = 0; i < graph->ncc; i++) {
    2425:           ISCreateGeneral(PETSC_COMM_SELF,cids[i+1]-cids[i],ids+cids[i],PETSC_COPY_VALUES,&cc_n[i]);
    2426:         }
    2427:         *cc = cc_n;
    2428:       }
    2429:       if (primalv) {
    2430:         ISCreateGeneral(PetscObjectComm((PetscObject)pc),cump,pids,PETSC_COPY_VALUES,primalv);
    2431:       }
    2432:       PetscFree3(ids,cids,pids);
    2433:       PetscBTDestroy(&btv);
    2434:       PetscBTDestroy(&btvt);
    2435:     }
    2436:   } else {
    2437:     if (ncc) *ncc = graph->ncc;
    2438:     if (cc) {
    2439:       PetscMalloc1(graph->ncc,&cc_n);
    2440:       for (i=0;i<graph->ncc;i++) {
    2441:         ISCreateGeneral(PETSC_COMM_SELF,graph->cptr[i+1]-graph->cptr[i],graph->queue+graph->cptr[i],PETSC_COPY_VALUES,&cc_n[i]);
    2442:       }
    2443:       *cc = cc_n;
    2444:     }
    2445:   }
    2446:   /* clean up graph */
    2447:   graph->xadj = NULL;
    2448:   graph->adjncy = NULL;
    2449:   PCBDDCGraphDestroy(&graph);
    2450:   return(0);
    2451: }

    2453: PetscErrorCode PCBDDCBenignCheck(PC pc, IS zerodiag)
    2454: {
    2455:   PC_BDDC*       pcbddc = (PC_BDDC*)pc->data;
    2456:   PC_IS*         pcis = (PC_IS*)(pc->data);
    2457:   IS             dirIS = NULL;
    2458:   PetscInt       i;

    2462:   PCBDDCGraphGetDirichletDofs(pcbddc->mat_graph,&dirIS);
    2463:   if (zerodiag) {
    2464:     Mat            A;
    2465:     Vec            vec3_N;
    2466:     PetscScalar    *vals;
    2467:     const PetscInt *idxs;
    2468:     PetscInt       nz,*count;

    2470:     /* p0 */
    2471:     VecSet(pcis->vec1_N,0.);
    2472:     PetscMalloc1(pcis->n,&vals);
    2473:     ISGetLocalSize(zerodiag,&nz);
    2474:     ISGetIndices(zerodiag,&idxs);
    2475:     for (i=0;i<nz;i++) vals[i] = 1.;
    2476:     VecSetValues(pcis->vec1_N,nz,idxs,vals,INSERT_VALUES);
    2477:     VecAssemblyBegin(pcis->vec1_N);
    2478:     VecAssemblyEnd(pcis->vec1_N);
    2479:     /* v_I */
    2480:     VecSetRandom(pcis->vec2_N,NULL);
    2481:     for (i=0;i<nz;i++) vals[i] = 0.;
    2482:     VecSetValues(pcis->vec2_N,nz,idxs,vals,INSERT_VALUES);
    2483:     ISRestoreIndices(zerodiag,&idxs);
    2484:     ISGetIndices(pcis->is_B_local,&idxs);
    2485:     for (i=0;i<pcis->n_B;i++) vals[i] = 0.;
    2486:     VecSetValues(pcis->vec2_N,pcis->n_B,idxs,vals,INSERT_VALUES);
    2487:     ISRestoreIndices(pcis->is_B_local,&idxs);
    2488:     if (dirIS) {
    2489:       PetscInt n;

    2491:       ISGetLocalSize(dirIS,&n);
    2492:       ISGetIndices(dirIS,&idxs);
    2493:       for (i=0;i<n;i++) vals[i] = 0.;
    2494:       VecSetValues(pcis->vec2_N,n,idxs,vals,INSERT_VALUES);
    2495:       ISRestoreIndices(dirIS,&idxs);
    2496:     }
    2497:     VecAssemblyBegin(pcis->vec2_N);
    2498:     VecAssemblyEnd(pcis->vec2_N);
    2499:     VecDuplicate(pcis->vec1_N,&vec3_N);
    2500:     VecSet(vec3_N,0.);
    2501:     MatISGetLocalMat(pc->pmat,&A);
    2502:     MatMult(A,pcis->vec1_N,vec3_N);
    2503:     VecDot(vec3_N,pcis->vec2_N,&vals[0]);
    2504:     if (PetscAbsScalar(vals[0]) > 1.e-1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Benign trick can not be applied! b(v_I,p_0) = %1.6e (should be numerically 0.)",PetscAbsScalar(vals[0]));
    2505:     PetscFree(vals);
    2506:     VecDestroy(&vec3_N);

    2508:     /* there should not be any pressure dofs lying on the interface */
    2509:     PetscCalloc1(pcis->n,&count);
    2510:     ISGetIndices(pcis->is_B_local,&idxs);
    2511:     for (i=0;i<pcis->n_B;i++) count[idxs[i]]++;
    2512:     ISRestoreIndices(pcis->is_B_local,&idxs);
    2513:     ISGetIndices(zerodiag,&idxs);
    2514:     for (i=0;i<nz;i++) if (count[idxs[i]]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Benign trick can not be applied! pressure dof %D is an interface dof",idxs[i]);
    2515:     ISRestoreIndices(zerodiag,&idxs);
    2516:     PetscFree(count);
    2517:   }
    2518:   ISDestroy(&dirIS);

    2520:   /* check PCBDDCBenignGetOrSetP0 */
    2521:   VecSetRandom(pcis->vec1_global,NULL);
    2522:   for (i=0;i<pcbddc->benign_n;i++) pcbddc->benign_p0[i] = -PetscGlobalRank-i;
    2523:   PCBDDCBenignGetOrSetP0(pc,pcis->vec1_global,PETSC_FALSE);
    2524:   for (i=0;i<pcbddc->benign_n;i++) pcbddc->benign_p0[i] = 1;
    2525:   PCBDDCBenignGetOrSetP0(pc,pcis->vec1_global,PETSC_TRUE);
    2526:   for (i=0;i<pcbddc->benign_n;i++) {
    2527:     PetscInt val = PetscRealPart(pcbddc->benign_p0[i]);
    2528:     if (val != -PetscGlobalRank-i) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Error testing PCBDDCBenignGetOrSetP0! Found %g at %D instead of %g",PetscRealPart(pcbddc->benign_p0[i]),i,-PetscGlobalRank-i);
    2529:   }
    2530:   return(0);
    2531: }

    2533: PetscErrorCode PCBDDCBenignDetectSaddlePoint(PC pc, PetscBool reuse, IS *zerodiaglocal)
    2534: {
    2535:   PC_BDDC*       pcbddc = (PC_BDDC*)pc->data;
    2536:   IS             pressures = NULL,zerodiag = NULL,*bzerodiag = NULL,zerodiag_save,*zerodiag_subs;
    2537:   PetscInt       nz,n,benign_n,bsp = 1;
    2538:   PetscInt       *interior_dofs,n_interior_dofs,nneu;
    2539:   PetscBool      sorted,have_null,has_null_pressures,recompute_zerodiag,checkb;

    2543:   if (reuse) goto project_b0;
    2544:   PetscSFDestroy(&pcbddc->benign_sf);
    2545:   MatDestroy(&pcbddc->benign_B0);
    2546:   for (n=0;n<pcbddc->benign_n;n++) {
    2547:     ISDestroy(&pcbddc->benign_zerodiag_subs[n]);
    2548:   }
    2549:   PetscFree(pcbddc->benign_zerodiag_subs);
    2550:   has_null_pressures = PETSC_TRUE;
    2551:   have_null = PETSC_TRUE;
    2552:   /* if a local information on dofs is present, gets pressure dofs from command line (uses the last field is not provided)
    2553:      Without local information, it uses only the zerodiagonal dofs (ok if the pressure block is all zero and it is a scalar field)
    2554:      Checks if all the pressure dofs in each subdomain have a zero diagonal
    2555:      If not, a change of basis on pressures is not needed
    2556:      since the local Schur complements are already SPD
    2557:   */
    2558:   if (pcbddc->n_ISForDofsLocal) {
    2559:     IS        iP = NULL;
    2560:     PetscInt  p,*pp;
    2561:     PetscBool flg;

    2563:     PetscMalloc1(pcbddc->n_ISForDofsLocal,&pp);
    2564:     n    = pcbddc->n_ISForDofsLocal;
    2565:     PetscOptionsBegin(PetscObjectComm((PetscObject)pc),((PetscObject)pc)->prefix,"BDDC benign options","PC");
    2566:     PetscOptionsIntArray("-pc_bddc_pressure_field","Field id for pressures",NULL,pp,&n,&flg);
    2567:     PetscOptionsEnd();
    2568:     if (!flg) {
    2569:       n = 1;
    2570:       pp[0] = pcbddc->n_ISForDofsLocal-1;
    2571:     }

    2573:     bsp = 0;
    2574:     for (p=0;p<n;p++) {
    2575:       PetscInt bs;

    2577:       if (pp[p] < 0 || pp[p] > pcbddc->n_ISForDofsLocal-1) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"Invalid field id for pressures %D",pp[p]);
    2578:       ISGetBlockSize(pcbddc->ISForDofsLocal[pp[p]],&bs);
    2579:       bsp += bs;
    2580:     }
    2581:     PetscMalloc1(bsp,&bzerodiag);
    2582:     bsp  = 0;
    2583:     for (p=0;p<n;p++) {
    2584:       const PetscInt *idxs;
    2585:       PetscInt       b,bs,npl,*bidxs;

    2587:       ISGetBlockSize(pcbddc->ISForDofsLocal[pp[p]],&bs);
    2588:       ISGetLocalSize(pcbddc->ISForDofsLocal[pp[p]],&npl);
    2589:       ISGetIndices(pcbddc->ISForDofsLocal[pp[p]],&idxs);
    2590:       PetscMalloc1(npl/bs,&bidxs);
    2591:       for (b=0;b<bs;b++) {
    2592:         PetscInt i;

    2594:         for (i=0;i<npl/bs;i++) bidxs[i] = idxs[bs*i+b];
    2595:         ISCreateGeneral(PETSC_COMM_SELF,npl/bs,bidxs,PETSC_COPY_VALUES,&bzerodiag[bsp]);
    2596:         bsp++;
    2597:       }
    2598:       PetscFree(bidxs);
    2599:       ISRestoreIndices(pcbddc->ISForDofsLocal[pp[p]],&idxs);
    2600:     }
    2601:     ISConcatenate(PETSC_COMM_SELF,bsp,bzerodiag,&pressures);

    2603:     /* remove zeroed out pressures if we are setting up a BDDC solver for a saddle-point FETI-DP */
    2604:     PetscObjectQuery((PetscObject)pc,"__KSPFETIDP_lP",(PetscObject*)&iP);
    2605:     if (iP) {
    2606:       IS newpressures;

    2608:       ISDifference(pressures,iP,&newpressures);
    2609:       ISDestroy(&pressures);
    2610:       pressures = newpressures;
    2611:     }
    2612:     ISSorted(pressures,&sorted);
    2613:     if (!sorted) {
    2614:       ISSort(pressures);
    2615:     }
    2616:     PetscFree(pp);
    2617:   }

    2619:   /* pcis has not been setup yet, so get the local size from the subdomain matrix */
    2620:   MatGetLocalSize(pcbddc->local_mat,&n,NULL);
    2621:   if (!n) pcbddc->benign_change_explicit = PETSC_TRUE;
    2622:   MatFindZeroDiagonals(pcbddc->local_mat,&zerodiag);
    2623:   ISSorted(zerodiag,&sorted);
    2624:   if (!sorted) {
    2625:     ISSort(zerodiag);
    2626:   }
    2627:   PetscObjectReference((PetscObject)zerodiag);
    2628:   zerodiag_save = zerodiag;
    2629:   ISGetLocalSize(zerodiag,&nz);
    2630:   if (!nz) {
    2631:     if (n) have_null = PETSC_FALSE;
    2632:     has_null_pressures = PETSC_FALSE;
    2633:     ISDestroy(&zerodiag);
    2634:   }
    2635:   recompute_zerodiag = PETSC_FALSE;

    2637:   /* in case disconnected subdomains info is present, split the pressures accordingly (otherwise the benign trick could fail) */
    2638:   zerodiag_subs    = NULL;
    2639:   benign_n         = 0;
    2640:   n_interior_dofs  = 0;
    2641:   interior_dofs    = NULL;
    2642:   nneu             = 0;
    2643:   if (pcbddc->NeumannBoundariesLocal) {
    2644:     ISGetLocalSize(pcbddc->NeumannBoundariesLocal,&nneu);
    2645:   }
    2646:   checkb = (PetscBool)(!pcbddc->NeumannBoundariesLocal || pcbddc->current_level);
    2647:   if (checkb) { /* need to compute interior nodes */
    2648:     PetscInt n,i,j;
    2649:     PetscInt n_neigh,*neigh,*n_shared,**shared;
    2650:     PetscInt *iwork;

    2652:     ISLocalToGlobalMappingGetSize(pc->pmat->rmap->mapping,&n);
    2653:     ISLocalToGlobalMappingGetInfo(pc->pmat->rmap->mapping,&n_neigh,&neigh,&n_shared,&shared);
    2654:     PetscCalloc1(n,&iwork);
    2655:     PetscMalloc1(n,&interior_dofs);
    2656:     for (i=1;i<n_neigh;i++)
    2657:       for (j=0;j<n_shared[i];j++)
    2658:           iwork[shared[i][j]] += 1;
    2659:     for (i=0;i<n;i++)
    2660:       if (!iwork[i])
    2661:         interior_dofs[n_interior_dofs++] = i;
    2662:     PetscFree(iwork);
    2663:     ISLocalToGlobalMappingRestoreInfo(pc->pmat->rmap->mapping,&n_neigh,&neigh,&n_shared,&shared);
    2664:   }
    2665:   if (has_null_pressures) {
    2666:     IS             *subs;
    2667:     PetscInt       nsubs,i,j,nl;
    2668:     const PetscInt *idxs;
    2669:     PetscScalar    *array;
    2670:     Vec            *work;
    2671:     Mat_IS*        matis = (Mat_IS*)(pc->pmat->data);

    2673:     subs  = pcbddc->local_subs;
    2674:     nsubs = pcbddc->n_local_subs;
    2675:     /* these vectors are needed to check if the constant on pressures is in the kernel of the local operator B (i.e. B(v_I,p0) should be zero) */
    2676:     if (checkb) {
    2677:       VecDuplicateVecs(matis->y,2,&work);
    2678:       ISGetLocalSize(zerodiag,&nl);
    2679:       ISGetIndices(zerodiag,&idxs);
    2680:       /* work[0] = 1_p */
    2681:       VecSet(work[0],0.);
    2682:       VecGetArray(work[0],&array);
    2683:       for (j=0;j<nl;j++) array[idxs[j]] = 1.;
    2684:       VecRestoreArray(work[0],&array);
    2685:       /* work[0] = 1_v */
    2686:       VecSet(work[1],1.);
    2687:       VecGetArray(work[1],&array);
    2688:       for (j=0;j<nl;j++) array[idxs[j]] = 0.;
    2689:       VecRestoreArray(work[1],&array);
    2690:       ISRestoreIndices(zerodiag,&idxs);
    2691:     }

    2693:     if (nsubs > 1 || bsp > 1) {
    2694:       IS       *is;
    2695:       PetscInt b,totb;

    2697:       totb  = bsp;
    2698:       is    = bsp > 1 ? bzerodiag : &zerodiag;
    2699:       nsubs = PetscMax(nsubs,1);
    2700:       PetscCalloc1(nsubs*totb,&zerodiag_subs);
    2701:       for (b=0;b<totb;b++) {
    2702:         for (i=0;i<nsubs;i++) {
    2703:           ISLocalToGlobalMapping l2g;
    2704:           IS                     t_zerodiag_subs;
    2705:           PetscInt               nl;

    2707:           if (subs) {
    2708:             ISLocalToGlobalMappingCreateIS(subs[i],&l2g);
    2709:           } else {
    2710:             IS tis;

    2712:             MatGetLocalSize(pcbddc->local_mat,&nl,NULL);
    2713:             ISCreateStride(PETSC_COMM_SELF,nl,0,1,&tis);
    2714:             ISLocalToGlobalMappingCreateIS(tis,&l2g);
    2715:             ISDestroy(&tis);
    2716:           }
    2717:           ISGlobalToLocalMappingApplyIS(l2g,IS_GTOLM_DROP,is[b],&t_zerodiag_subs);
    2718:           ISGetLocalSize(t_zerodiag_subs,&nl);
    2719:           if (nl) {
    2720:             PetscBool valid = PETSC_TRUE;

    2722:             if (checkb) {
    2723:               VecSet(matis->x,0);
    2724:               ISGetLocalSize(subs[i],&nl);
    2725:               ISGetIndices(subs[i],&idxs);
    2726:               VecGetArray(matis->x,&array);
    2727:               for (j=0;j<nl;j++) array[idxs[j]] = 1.;
    2728:               VecRestoreArray(matis->x,&array);
    2729:               ISRestoreIndices(subs[i],&idxs);
    2730:               VecPointwiseMult(matis->x,work[0],matis->x);
    2731:               MatMult(matis->A,matis->x,matis->y);
    2732:               VecPointwiseMult(matis->y,work[1],matis->y);
    2733:               VecGetArray(matis->y,&array);
    2734:               for (j=0;j<n_interior_dofs;j++) {
    2735:                 if (PetscAbsScalar(array[interior_dofs[j]]) > PETSC_SMALL) {
    2736:                   valid = PETSC_FALSE;
    2737:                   break;
    2738:                 }
    2739:               }
    2740:               VecRestoreArray(matis->y,&array);
    2741:             }
    2742:             if (valid && nneu) {
    2743:               const PetscInt *idxs;
    2744:               PetscInt       nzb;

    2746:               ISGetIndices(pcbddc->NeumannBoundariesLocal,&idxs);
    2747:               ISGlobalToLocalMappingApply(l2g,IS_GTOLM_DROP,nneu,idxs,&nzb,NULL);
    2748:               ISRestoreIndices(pcbddc->NeumannBoundariesLocal,&idxs);
    2749:               if (nzb) valid = PETSC_FALSE;
    2750:             }
    2751:             if (valid && pressures) {
    2752:               IS       t_pressure_subs,tmp;
    2753:               PetscInt i1,i2;

    2755:               ISGlobalToLocalMappingApplyIS(l2g,IS_GTOLM_DROP,pressures,&t_pressure_subs);
    2756:               ISEmbed(t_zerodiag_subs,t_pressure_subs,PETSC_TRUE,&tmp);
    2757:               ISGetLocalSize(tmp,&i1);
    2758:               ISGetLocalSize(t_zerodiag_subs,&i2);
    2759:               if (i2 != i1) valid = PETSC_FALSE;
    2760:               ISDestroy(&t_pressure_subs);
    2761:               ISDestroy(&tmp);
    2762:             }
    2763:             if (valid) {
    2764:               ISLocalToGlobalMappingApplyIS(l2g,t_zerodiag_subs,&zerodiag_subs[benign_n]);
    2765:               benign_n++;
    2766:             } else recompute_zerodiag = PETSC_TRUE;
    2767:           }
    2768:           ISDestroy(&t_zerodiag_subs);
    2769:           ISLocalToGlobalMappingDestroy(&l2g);
    2770:         }
    2771:       }
    2772:     } else { /* there's just one subdomain (or zero if they have not been detected */
    2773:       PetscBool valid = PETSC_TRUE;

    2775:       if (nneu) valid = PETSC_FALSE;
    2776:       if (valid && pressures) {
    2777:         ISEqual(pressures,zerodiag,&valid);
    2778:       }
    2779:       if (valid && checkb) {
    2780:         MatMult(matis->A,work[0],matis->x);
    2781:         VecPointwiseMult(matis->x,work[1],matis->x);
    2782:         VecGetArray(matis->x,&array);
    2783:         for (j=0;j<n_interior_dofs;j++) {
    2784:           if (PetscAbsScalar(array[interior_dofs[j]]) > PETSC_SMALL) {
    2785:             valid = PETSC_FALSE;
    2786:             break;
    2787:           }
    2788:         }
    2789:         VecRestoreArray(matis->x,&array);
    2790:       }
    2791:       if (valid) {
    2792:         benign_n = 1;
    2793:         PetscMalloc1(benign_n,&zerodiag_subs);
    2794:         PetscObjectReference((PetscObject)zerodiag);
    2795:         zerodiag_subs[0] = zerodiag;
    2796:       }
    2797:     }
    2798:     if (checkb) {
    2799:       VecDestroyVecs(2,&work);
    2800:     }
    2801:   }
    2802:   PetscFree(interior_dofs);

    2804:   if (!benign_n) {
    2805:     PetscInt n;

    2807:     ISDestroy(&zerodiag);
    2808:     recompute_zerodiag = PETSC_FALSE;
    2809:     MatGetLocalSize(pcbddc->local_mat,&n,NULL);
    2810:     if (n) have_null = PETSC_FALSE;
    2811:   }

    2813:   /* final check for null pressures */
    2814:   if (zerodiag && pressures) {
    2815:     ISEqual(pressures,zerodiag,&have_null);
    2816:   }

    2818:   if (recompute_zerodiag) {
    2819:     ISDestroy(&zerodiag);
    2820:     if (benign_n == 1) {
    2821:       PetscObjectReference((PetscObject)zerodiag_subs[0]);
    2822:       zerodiag = zerodiag_subs[0];
    2823:     } else {
    2824:       PetscInt i,nzn,*new_idxs;

    2826:       nzn = 0;
    2827:       for (i=0;i<benign_n;i++) {
    2828:         PetscInt ns;
    2829:         ISGetLocalSize(zerodiag_subs[i],&ns);
    2830:         nzn += ns;
    2831:       }
    2832:       PetscMalloc1(nzn,&new_idxs);
    2833:       nzn = 0;
    2834:       for (i=0;i<benign_n;i++) {
    2835:         PetscInt ns,*idxs;
    2836:         ISGetLocalSize(zerodiag_subs[i],&ns);
    2837:         ISGetIndices(zerodiag_subs[i],(const PetscInt**)&idxs);
    2838:         PetscArraycpy(new_idxs+nzn,idxs,ns);
    2839:         ISRestoreIndices(zerodiag_subs[i],(const PetscInt**)&idxs);
    2840:         nzn += ns;
    2841:       }
    2842:       PetscSortInt(nzn,new_idxs);
    2843:       ISCreateGeneral(PETSC_COMM_SELF,nzn,new_idxs,PETSC_OWN_POINTER,&zerodiag);
    2844:     }
    2845:     have_null = PETSC_FALSE;
    2846:   }

    2848:   /* determines if the coarse solver will be singular or not */
    2849:   MPIU_Allreduce(&have_null,&pcbddc->benign_null,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)pc));

    2851:   /* Prepare matrix to compute no-net-flux */
    2852:   if (pcbddc->compute_nonetflux && !pcbddc->divudotp) {
    2853:     Mat                    A,loc_divudotp;
    2854:     ISLocalToGlobalMapping rl2g,cl2g,l2gmap;
    2855:     IS                     row,col,isused = NULL;
    2856:     PetscInt               M,N,n,st,n_isused;

    2858:     if (pressures) {
    2859:       isused = pressures;
    2860:     } else {
    2861:       isused = zerodiag_save;
    2862:     }
    2863:     MatGetLocalToGlobalMapping(pc->pmat,&l2gmap,NULL);
    2864:     MatISGetLocalMat(pc->pmat,&A);
    2865:     MatGetLocalSize(A,&n,NULL);
    2866:     if (!isused && n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Don't know how to extract div u dot p! Please provide the pressure field");
    2867:     n_isused = 0;
    2868:     if (isused) {
    2869:       ISGetLocalSize(isused,&n_isused);
    2870:     }
    2871:     MPI_Scan(&n_isused,&st,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)pc));
    2872:     st = st-n_isused;
    2873:     if (n) {
    2874:       const PetscInt *gidxs;

    2876:       MatCreateSubMatrix(A,isused,NULL,MAT_INITIAL_MATRIX,&loc_divudotp);
    2877:       ISLocalToGlobalMappingGetIndices(l2gmap,&gidxs);
    2878:       /* TODO: extend ISCreateStride with st = PETSC_DECIDE */
    2879:       ISCreateStride(PetscObjectComm((PetscObject)pc),n_isused,st,1,&row);
    2880:       ISCreateGeneral(PetscObjectComm((PetscObject)pc),n,gidxs,PETSC_COPY_VALUES,&col);
    2881:       ISLocalToGlobalMappingRestoreIndices(l2gmap,&gidxs);
    2882:     } else {
    2883:       MatCreateSeqAIJ(PETSC_COMM_SELF,0,0,1,NULL,&loc_divudotp);
    2884:       ISCreateStride(PetscObjectComm((PetscObject)pc),n_isused,st,1,&row);
    2885:       ISCreateGeneral(PetscObjectComm((PetscObject)pc),0,NULL,PETSC_COPY_VALUES,&col);
    2886:     }
    2887:     MatGetSize(pc->pmat,NULL,&N);
    2888:     ISGetSize(row,&M);
    2889:     ISLocalToGlobalMappingCreateIS(row,&rl2g);
    2890:     ISLocalToGlobalMappingCreateIS(col,&cl2g);
    2891:     ISDestroy(&row);
    2892:     ISDestroy(&col);
    2893:     MatCreate(PetscObjectComm((PetscObject)pc),&pcbddc->divudotp);
    2894:     MatSetType(pcbddc->divudotp,MATIS);
    2895:     MatSetSizes(pcbddc->divudotp,PETSC_DECIDE,PETSC_DECIDE,M,N);
    2896:     MatSetLocalToGlobalMapping(pcbddc->divudotp,rl2g,cl2g);
    2897:     ISLocalToGlobalMappingDestroy(&rl2g);
    2898:     ISLocalToGlobalMappingDestroy(&cl2g);
    2899:     MatISSetLocalMat(pcbddc->divudotp,loc_divudotp);
    2900:     MatDestroy(&loc_divudotp);
    2901:     MatAssemblyBegin(pcbddc->divudotp,MAT_FINAL_ASSEMBLY);
    2902:     MatAssemblyEnd(pcbddc->divudotp,MAT_FINAL_ASSEMBLY);
    2903:   }
    2904:   ISDestroy(&zerodiag_save);
    2905:   ISDestroy(&pressures);
    2906:   if (bzerodiag) {
    2907:     PetscInt i;

    2909:     for (i=0;i<bsp;i++) {
    2910:       ISDestroy(&bzerodiag[i]);
    2911:     }
    2912:     PetscFree(bzerodiag);
    2913:   }
    2914:   pcbddc->benign_n = benign_n;
    2915:   pcbddc->benign_zerodiag_subs = zerodiag_subs;

    2917:   /* determines if the problem has subdomains with 0 pressure block */
    2918:   have_null = (PetscBool)(!!pcbddc->benign_n);
    2919:   MPIU_Allreduce(&have_null,&pcbddc->benign_have_null,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)pc));

    2921: project_b0:
    2922:   MatGetLocalSize(pcbddc->local_mat,&n,NULL);
    2923:   /* change of basis and p0 dofs */
    2924:   if (pcbddc->benign_n) {
    2925:     PetscInt i,s,*nnz;

    2927:     /* local change of basis for pressures */
    2928:     MatDestroy(&pcbddc->benign_change);
    2929:     MatCreate(PetscObjectComm((PetscObject)pcbddc->local_mat),&pcbddc->benign_change);
    2930:     MatSetType(pcbddc->benign_change,MATAIJ);
    2931:     MatSetSizes(pcbddc->benign_change,n,n,PETSC_DECIDE,PETSC_DECIDE);
    2932:     PetscMalloc1(n,&nnz);
    2933:     for (i=0;i<n;i++) nnz[i] = 1; /* defaults to identity */
    2934:     for (i=0;i<pcbddc->benign_n;i++) {
    2935:       const PetscInt *idxs;
    2936:       PetscInt       nzs,j;

    2938:       ISGetLocalSize(pcbddc->benign_zerodiag_subs[i],&nzs);
    2939:       ISGetIndices(pcbddc->benign_zerodiag_subs[i],&idxs);
    2940:       for (j=0;j<nzs-1;j++) nnz[idxs[j]] = 2; /* change on pressures */
    2941:       nnz[idxs[nzs-1]] = nzs; /* last local pressure dof in subdomain */
    2942:       ISRestoreIndices(pcbddc->benign_zerodiag_subs[i],&idxs);
    2943:     }
    2944:     MatSeqAIJSetPreallocation(pcbddc->benign_change,0,nnz);
    2945:     MatSetOption(pcbddc->benign_change,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
    2946:     PetscFree(nnz);
    2947:     /* set identity by default */
    2948:     for (i=0;i<n;i++) {
    2949:       MatSetValue(pcbddc->benign_change,i,i,1.,INSERT_VALUES);
    2950:     }
    2951:     PetscFree3(pcbddc->benign_p0_lidx,pcbddc->benign_p0_gidx,pcbddc->benign_p0);
    2952:     PetscMalloc3(pcbddc->benign_n,&pcbddc->benign_p0_lidx,pcbddc->benign_n,&pcbddc->benign_p0_gidx,pcbddc->benign_n,&pcbddc->benign_p0);
    2953:     /* set change on pressures */
    2954:     for (s=0;s<pcbddc->benign_n;s++) {
    2955:       PetscScalar    *array;
    2956:       const PetscInt *idxs;
    2957:       PetscInt       nzs;

    2959:       ISGetLocalSize(pcbddc->benign_zerodiag_subs[s],&nzs);
    2960:       ISGetIndices(pcbddc->benign_zerodiag_subs[s],&idxs);
    2961:       for (i=0;i<nzs-1;i++) {
    2962:         PetscScalar vals[2];
    2963:         PetscInt    cols[2];

    2965:         cols[0] = idxs[i];
    2966:         cols[1] = idxs[nzs-1];
    2967:         vals[0] = 1.;
    2968:         vals[1] = 1.;
    2969:         MatSetValues(pcbddc->benign_change,1,cols,2,cols,vals,INSERT_VALUES);
    2970:       }
    2971:       PetscMalloc1(nzs,&array);
    2972:       for (i=0;i<nzs-1;i++) array[i] = -1.;
    2973:       array[nzs-1] = 1.;
    2974:       MatSetValues(pcbddc->benign_change,1,idxs+nzs-1,nzs,idxs,array,INSERT_VALUES);
    2975:       /* store local idxs for p0 */
    2976:       pcbddc->benign_p0_lidx[s] = idxs[nzs-1];
    2977:       ISRestoreIndices(pcbddc->benign_zerodiag_subs[s],&idxs);
    2978:       PetscFree(array);
    2979:     }
    2980:     MatAssemblyBegin(pcbddc->benign_change,MAT_FINAL_ASSEMBLY);
    2981:     MatAssemblyEnd(pcbddc->benign_change,MAT_FINAL_ASSEMBLY);

    2983:     /* project if needed */
    2984:     if (pcbddc->benign_change_explicit) {
    2985:       Mat M;

    2987:       MatPtAP(pcbddc->local_mat,pcbddc->benign_change,MAT_INITIAL_MATRIX,2.0,&M);
    2988:       MatDestroy(&pcbddc->local_mat);
    2989:       MatSeqAIJCompress(M,&pcbddc->local_mat);
    2990:       MatDestroy(&M);
    2991:     }
    2992:     /* store global idxs for p0 */
    2993:     ISLocalToGlobalMappingApply(pc->pmat->rmap->mapping,pcbddc->benign_n,pcbddc->benign_p0_lidx,pcbddc->benign_p0_gidx);
    2994:   }
    2995:   *zerodiaglocal = zerodiag;
    2996:   return(0);
    2997: }

    2999: PetscErrorCode PCBDDCBenignGetOrSetP0(PC pc, Vec v, PetscBool get)
    3000: {
    3001:   PC_BDDC*       pcbddc = (PC_BDDC*)pc->data;
    3002:   PetscScalar    *array;

    3006:   if (!pcbddc->benign_sf) {
    3007:     PetscSFCreate(PetscObjectComm((PetscObject)pc),&pcbddc->benign_sf);
    3008:     PetscSFSetGraphLayout(pcbddc->benign_sf,pc->pmat->rmap,pcbddc->benign_n,NULL,PETSC_OWN_POINTER,pcbddc->benign_p0_gidx);
    3009:   }
    3010:   if (get) {
    3011:     VecGetArrayRead(v,(const PetscScalar**)&array);
    3012:     PetscSFBcastBegin(pcbddc->benign_sf,MPIU_SCALAR,array,pcbddc->benign_p0,MPI_REPLACE);
    3013:     PetscSFBcastEnd(pcbddc->benign_sf,MPIU_SCALAR,array,pcbddc->benign_p0,MPI_REPLACE);
    3014:     VecRestoreArrayRead(v,(const PetscScalar**)&array);
    3015:   } else {
    3016:     VecGetArray(v,&array);
    3017:     PetscSFReduceBegin(pcbddc->benign_sf,MPIU_SCALAR,pcbddc->benign_p0,array,MPI_REPLACE);
    3018:     PetscSFReduceEnd(pcbddc->benign_sf,MPIU_SCALAR,pcbddc->benign_p0,array,MPI_REPLACE);
    3019:     VecRestoreArray(v,&array);
    3020:   }
    3021:   return(0);
    3022: }

    3024: PetscErrorCode PCBDDCBenignPopOrPushB0(PC pc, PetscBool pop)
    3025: {
    3026:   PC_BDDC*       pcbddc = (PC_BDDC*)pc->data;

    3030:   /* TODO: add error checking
    3031:     - avoid nested pop (or push) calls.
    3032:     - cannot push before pop.
    3033:     - cannot call this if pcbddc->local_mat is NULL
    3034:   */
    3035:   if (!pcbddc->benign_n) {
    3036:     return(0);
    3037:   }
    3038:   if (pop) {
    3039:     if (pcbddc->benign_change_explicit) {
    3040:       IS       is_p0;
    3041:       MatReuse reuse;

    3043:       /* extract B_0 */
    3044:       reuse = MAT_INITIAL_MATRIX;
    3045:       if (pcbddc->benign_B0) {
    3046:         reuse = MAT_REUSE_MATRIX;
    3047:       }
    3048:       ISCreateGeneral(PETSC_COMM_SELF,pcbddc->benign_n,pcbddc->benign_p0_lidx,PETSC_COPY_VALUES,&is_p0);
    3049:       MatCreateSubMatrix(pcbddc->local_mat,is_p0,NULL,reuse,&pcbddc->benign_B0);
    3050:       /* remove rows and cols from local problem */
    3051:       MatSetOption(pcbddc->local_mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE);
    3052:       MatSetOption(pcbddc->local_mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
    3053:       MatZeroRowsColumnsIS(pcbddc->local_mat,is_p0,1.0,NULL,NULL);
    3054:       ISDestroy(&is_p0);
    3055:     } else {
    3056:       Mat_IS      *matis = (Mat_IS*)pc->pmat->data;
    3057:       PetscScalar *vals;
    3058:       PetscInt    i,n,*idxs_ins;

    3060:       VecGetLocalSize(matis->y,&n);
    3061:       PetscMalloc2(n,&idxs_ins,n,&vals);
    3062:       if (!pcbddc->benign_B0) {
    3063:         PetscInt *nnz;
    3064:         MatCreate(PetscObjectComm((PetscObject)pcbddc->local_mat),&pcbddc->benign_B0);
    3065:         MatSetType(pcbddc->benign_B0,MATAIJ);
    3066:         MatSetSizes(pcbddc->benign_B0,pcbddc->benign_n,n,PETSC_DECIDE,PETSC_DECIDE);
    3067:         PetscMalloc1(pcbddc->benign_n,&nnz);
    3068:         for (i=0;i<pcbddc->benign_n;i++) {
    3069:           ISGetLocalSize(pcbddc->benign_zerodiag_subs[i],&nnz[i]);
    3070:           nnz[i] = n - nnz[i];
    3071:         }
    3072:         MatSeqAIJSetPreallocation(pcbddc->benign_B0,0,nnz);
    3073:         MatSetOption(pcbddc->benign_B0,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
    3074:         PetscFree(nnz);
    3075:       }

    3077:       for (i=0;i<pcbddc->benign_n;i++) {
    3078:         PetscScalar *array;
    3079:         PetscInt    *idxs,j,nz,cum;

    3081:         VecSet(matis->x,0.);
    3082:         ISGetLocalSize(pcbddc->benign_zerodiag_subs[i],&nz);
    3083:         ISGetIndices(pcbddc->benign_zerodiag_subs[i],(const PetscInt**)&idxs);
    3084:         for (j=0;j<nz;j++) vals[j] = 1.;
    3085:         VecSetValues(matis->x,nz,idxs,vals,INSERT_VALUES);
    3086:         VecAssemblyBegin(matis->x);
    3087:         VecAssemblyEnd(matis->x);
    3088:         VecSet(matis->y,0.);
    3089:         MatMult(matis->A,matis->x,matis->y);
    3090:         VecGetArray(matis->y,&array);
    3091:         cum = 0;
    3092:         for (j=0;j<n;j++) {
    3093:           if (PetscUnlikely(PetscAbsScalar(array[j]) > PETSC_SMALL)) {
    3094:             vals[cum] = array[j];
    3095:             idxs_ins[cum] = j;
    3096:             cum++;
    3097:           }
    3098:         }
    3099:         MatSetValues(pcbddc->benign_B0,1,&i,cum,idxs_ins,vals,INSERT_VALUES);
    3100:         VecRestoreArray(matis->y,&array);
    3101:         ISRestoreIndices(pcbddc->benign_zerodiag_subs[i],(const PetscInt**)&idxs);
    3102:       }
    3103:       MatAssemblyBegin(pcbddc->benign_B0,MAT_FINAL_ASSEMBLY);
    3104:       MatAssemblyEnd(pcbddc->benign_B0,MAT_FINAL_ASSEMBLY);
    3105:       PetscFree2(idxs_ins,vals);
    3106:     }
    3107:   } else { /* push */
    3108:     if (pcbddc->benign_change_explicit) {
    3109:       PetscInt i;

    3111:       for (i=0;i<pcbddc->benign_n;i++) {
    3112:         PetscScalar *B0_vals;
    3113:         PetscInt    *B0_cols,B0_ncol;

    3115:         MatGetRow(pcbddc->benign_B0,i,&B0_ncol,(const PetscInt**)&B0_cols,(const PetscScalar**)&B0_vals);
    3116:         MatSetValues(pcbddc->local_mat,1,pcbddc->benign_p0_lidx+i,B0_ncol,B0_cols,B0_vals,INSERT_VALUES);
    3117:         MatSetValues(pcbddc->local_mat,B0_ncol,B0_cols,1,pcbddc->benign_p0_lidx+i,B0_vals,INSERT_VALUES);
    3118:         MatSetValue(pcbddc->local_mat,pcbddc->benign_p0_lidx[i],pcbddc->benign_p0_lidx[i],0.0,INSERT_VALUES);
    3119:         MatRestoreRow(pcbddc->benign_B0,i,&B0_ncol,(const PetscInt**)&B0_cols,(const PetscScalar**)&B0_vals);
    3120:       }
    3121:       MatAssemblyBegin(pcbddc->local_mat,MAT_FINAL_ASSEMBLY);
    3122:       MatAssemblyEnd(pcbddc->local_mat,MAT_FINAL_ASSEMBLY);
    3123:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cannot push B0!");
    3124:   }
    3125:   return(0);
    3126: }

    3128: PetscErrorCode PCBDDCAdaptiveSelection(PC pc)
    3129: {
    3130:   PC_BDDC*        pcbddc = (PC_BDDC*)pc->data;
    3131:   PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs;
    3132:   PetscBLASInt    B_dummyint,B_neigs,B_ierr,B_lwork;
    3133:   PetscBLASInt    *B_iwork,*B_ifail;
    3134:   PetscScalar     *work,lwork;
    3135:   PetscScalar     *St,*S,*eigv;
    3136:   PetscScalar     *Sarray,*Starray;
    3137:   PetscReal       *eigs,thresh,lthresh,uthresh;
    3138:   PetscInt        i,nmax,nmin,nv,cum,mss,cum2,cumarray,maxneigs;
    3139:   PetscBool       allocated_S_St;
    3140: #if defined(PETSC_USE_COMPLEX)
    3141:   PetscReal       *rwork;
    3142: #endif
    3143:   PetscErrorCode  ierr;

    3146:   if (!sub_schurs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Adaptive selection of constraints requires SubSchurs data");
    3147:   if (!sub_schurs->schur_explicit) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Adaptive selection of constraints requires MUMPS and/or MKL_CPARDISO");
    3148:   if (sub_schurs->n_subs && (!sub_schurs->is_symmetric)) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_SUP,"Adaptive selection not yet implemented for this matrix pencil (herm %d, symm %d, posdef %d)",sub_schurs->is_hermitian,sub_schurs->is_symmetric,sub_schurs->is_posdef);
    3149:   PetscLogEventBegin(PC_BDDC_AdaptiveSetUp[pcbddc->current_level],pc,0,0,0);

    3151:   if (pcbddc->dbg_flag) {
    3152:     PetscViewerFlush(pcbddc->dbg_viewer);
    3153:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"--------------------------------------------------\n");
    3154:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"Check adaptive selection of constraints\n");
    3155:     PetscViewerASCIIPushSynchronized(pcbddc->dbg_viewer);
    3156:   }

    3158:   if (pcbddc->dbg_flag) {
    3159:     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d cc %D (%d,%d).\n",PetscGlobalRank,sub_schurs->n_subs,sub_schurs->is_hermitian,sub_schurs->is_posdef);
    3160:   }

    3162:   /* max size of subsets */
    3163:   mss = 0;
    3164:   for (i=0;i<sub_schurs->n_subs;i++) {
    3165:     PetscInt subset_size;

    3167:     ISGetLocalSize(sub_schurs->is_subs[i],&subset_size);
    3168:     mss = PetscMax(mss,subset_size);
    3169:   }

    3171:   /* min/max and threshold */
    3172:   nmax = pcbddc->adaptive_nmax > 0 ? pcbddc->adaptive_nmax : mss;
    3173:   nmin = pcbddc->adaptive_nmin > 0 ? pcbddc->adaptive_nmin : 0;
    3174:   nmax = PetscMax(nmin,nmax);
    3175:   allocated_S_St = PETSC_FALSE;
    3176:   if (nmin || !sub_schurs->is_posdef) { /* XXX */
    3177:     allocated_S_St = PETSC_TRUE;
    3178:   }

    3180:   /* allocate lapack workspace */
    3181:   cum = cum2 = 0;
    3182:   maxneigs = 0;
    3183:   for (i=0;i<sub_schurs->n_subs;i++) {
    3184:     PetscInt n,subset_size;

    3186:     ISGetLocalSize(sub_schurs->is_subs[i],&subset_size);
    3187:     n = PetscMin(subset_size,nmax);
    3188:     cum += subset_size;
    3189:     cum2 += subset_size*n;
    3190:     maxneigs = PetscMax(maxneigs,n);
    3191:   }
    3192:   lwork = 0;
    3193:   if (mss) {
    3194:     if (sub_schurs->is_symmetric) {
    3195:       PetscScalar  sdummy = 0.;
    3196:       PetscBLASInt B_itype = 1;
    3197:       PetscBLASInt B_N = mss, idummy = 0;
    3198:       PetscReal    rdummy = 0.,zero = 0.0;
    3199:       PetscReal    eps = 0.0; /* dlamch? */

    3201:       B_lwork = -1;
    3202:       /* some implementations may complain about NULL pointers, even if we are querying */
    3203:       S = &sdummy;
    3204:       St = &sdummy;
    3205:       eigs = &rdummy;
    3206:       eigv = &sdummy;
    3207:       B_iwork = &idummy;
    3208:       B_ifail = &idummy;
    3209: #if defined(PETSC_USE_COMPLEX)
    3210:       rwork = &rdummy;
    3211: #endif
    3212:       thresh = 1.0;
    3213:       PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    3214: #if defined(PETSC_USE_COMPLEX)
    3215:       PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&zero,&thresh,&B_dummyint,&B_dummyint,&eps,&B_neigs,eigs,eigv,&B_N,&lwork,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3216: #else
    3217:       PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&zero,&thresh,&B_dummyint,&B_dummyint,&eps,&B_neigs,eigs,eigv,&B_N,&lwork,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3218: #endif
    3219:       if (B_ierr != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in query to SYGVX Lapack routine %d",(int)B_ierr);
    3220:       PetscFPTrapPop();
    3221:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet implemented");
    3222:   }

    3224:   nv = 0;
    3225:   if (sub_schurs->is_vertices && pcbddc->use_vertices) { /* complement set of active subsets, each entry is a vertex (boundary made by active subsets, vertices and dirichlet dofs) */
    3226:     ISGetLocalSize(sub_schurs->is_vertices,&nv);
    3227:   }
    3228:   PetscBLASIntCast((PetscInt)PetscRealPart(lwork),&B_lwork);
    3229:   if (allocated_S_St) {
    3230:     PetscMalloc2(mss*mss,&S,mss*mss,&St);
    3231:   }
    3232:   PetscMalloc5(mss*mss,&eigv,mss,&eigs,B_lwork,&work,5*mss,&B_iwork,mss,&B_ifail);
    3233: #if defined(PETSC_USE_COMPLEX)
    3234:   PetscMalloc1(7*mss,&rwork);
    3235: #endif
    3236:   PetscMalloc5(nv+sub_schurs->n_subs,&pcbddc->adaptive_constraints_n,
    3237:                       nv+sub_schurs->n_subs+1,&pcbddc->adaptive_constraints_idxs_ptr,
    3238:                       nv+sub_schurs->n_subs+1,&pcbddc->adaptive_constraints_data_ptr,
    3239:                       nv+cum,&pcbddc->adaptive_constraints_idxs,
    3240:                       nv+cum2,&pcbddc->adaptive_constraints_data);
    3241:   PetscArrayzero(pcbddc->adaptive_constraints_n,nv+sub_schurs->n_subs);

    3243:   maxneigs = 0;
    3244:   cum = cumarray = 0;
    3245:   pcbddc->adaptive_constraints_idxs_ptr[0] = 0;
    3246:   pcbddc->adaptive_constraints_data_ptr[0] = 0;
    3247:   if (sub_schurs->is_vertices && pcbddc->use_vertices) {
    3248:     const PetscInt *idxs;

    3250:     ISGetIndices(sub_schurs->is_vertices,&idxs);
    3251:     for (cum=0;cum<nv;cum++) {
    3252:       pcbddc->adaptive_constraints_n[cum] = 1;
    3253:       pcbddc->adaptive_constraints_idxs[cum] = idxs[cum];
    3254:       pcbddc->adaptive_constraints_data[cum] = 1.0;
    3255:       pcbddc->adaptive_constraints_idxs_ptr[cum+1] = pcbddc->adaptive_constraints_idxs_ptr[cum]+1;
    3256:       pcbddc->adaptive_constraints_data_ptr[cum+1] = pcbddc->adaptive_constraints_data_ptr[cum]+1;
    3257:     }
    3258:     ISRestoreIndices(sub_schurs->is_vertices,&idxs);
    3259:   }

    3261:   if (mss) { /* multilevel */
    3262:     MatSeqAIJGetArray(sub_schurs->sum_S_Ej_inv_all,&Sarray);
    3263:     MatSeqAIJGetArray(sub_schurs->sum_S_Ej_tilda_all,&Starray);
    3264:   }

    3266:   lthresh = pcbddc->adaptive_threshold[0];
    3267:   uthresh = pcbddc->adaptive_threshold[1];
    3268:   for (i=0;i<sub_schurs->n_subs;i++) {
    3269:     const PetscInt *idxs;
    3270:     PetscReal      upper,lower;
    3271:     PetscInt       j,subset_size,eigs_start = 0;
    3272:     PetscBLASInt   B_N;
    3273:     PetscBool      same_data = PETSC_FALSE;
    3274:     PetscBool      scal = PETSC_FALSE;

    3276:     if (pcbddc->use_deluxe_scaling) {
    3277:       upper = PETSC_MAX_REAL;
    3278:       lower = uthresh;
    3279:     } else {
    3280:       if (!sub_schurs->is_posdef) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet implemented without deluxe scaling");
    3281:       upper = 1./uthresh;
    3282:       lower = 0.;
    3283:     }
    3284:     ISGetLocalSize(sub_schurs->is_subs[i],&subset_size);
    3285:     ISGetIndices(sub_schurs->is_subs[i],&idxs);
    3286:     PetscBLASIntCast(subset_size,&B_N);
    3287:     /* this is experimental: we assume the dofs have been properly grouped to have
    3288:        the diagonal blocks Schur complements either positive or negative definite (true for Stokes) */
    3289:     if (!sub_schurs->is_posdef) {
    3290:       Mat T;

    3292:       for (j=0;j<subset_size;j++) {
    3293:         if (PetscRealPart(*(Sarray+cumarray+j*(subset_size+1))) < 0.0) {
    3294:           MatCreateSeqDense(PETSC_COMM_SELF,subset_size,subset_size,Sarray+cumarray,&T);
    3295:           MatScale(T,-1.0);
    3296:           MatDestroy(&T);
    3297:           MatCreateSeqDense(PETSC_COMM_SELF,subset_size,subset_size,Starray+cumarray,&T);
    3298:           MatScale(T,-1.0);
    3299:           MatDestroy(&T);
    3300:           if (sub_schurs->change_primal_sub) {
    3301:             PetscInt       nz,k;
    3302:             const PetscInt *idxs;

    3304:             ISGetLocalSize(sub_schurs->change_primal_sub[i],&nz);
    3305:             ISGetIndices(sub_schurs->change_primal_sub[i],&idxs);
    3306:             for (k=0;k<nz;k++) {
    3307:               *( Sarray + cumarray + idxs[k]*(subset_size+1)) *= -1.0;
    3308:               *(Starray + cumarray + idxs[k]*(subset_size+1))  = 0.0;
    3309:             }
    3310:             ISRestoreIndices(sub_schurs->change_primal_sub[i],&idxs);
    3311:           }
    3312:           scal = PETSC_TRUE;
    3313:           break;
    3314:         }
    3315:       }
    3316:     }

    3318:     if (allocated_S_St) { /* S and S_t should be copied since we could need them later */
    3319:       if (sub_schurs->is_symmetric) {
    3320:         PetscInt j,k;
    3321:         if (sub_schurs->n_subs == 1) { /* zeroing memory to use PetscArraycmp() later */
    3322:           PetscArrayzero(S,subset_size*subset_size);
    3323:           PetscArrayzero(St,subset_size*subset_size);
    3324:         }
    3325:         for (j=0;j<subset_size;j++) {
    3326:           for (k=j;k<subset_size;k++) {
    3327:             S [j*subset_size+k] = Sarray [cumarray+j*subset_size+k];
    3328:             St[j*subset_size+k] = Starray[cumarray+j*subset_size+k];
    3329:           }
    3330:         }
    3331:       } else {
    3332:         PetscArraycpy(S,Sarray+cumarray,subset_size*subset_size);
    3333:         PetscArraycpy(St,Starray+cumarray,subset_size*subset_size);
    3334:       }
    3335:     } else {
    3336:       S = Sarray + cumarray;
    3337:       St = Starray + cumarray;
    3338:     }
    3339:     /* see if we can save some work */
    3340:     if (sub_schurs->n_subs == 1 && pcbddc->use_deluxe_scaling) {
    3341:       PetscArraycmp(S,St,subset_size*subset_size,&same_data);
    3342:     }

    3344:     if (same_data && !sub_schurs->change) { /* there's no need of constraints here */
    3345:       B_neigs = 0;
    3346:     } else {
    3347:       if (sub_schurs->is_symmetric) {
    3348:         PetscBLASInt B_itype = 1;
    3349:         PetscBLASInt B_IL, B_IU;
    3350:         PetscReal    eps = -1.0; /* dlamch? */
    3351:         PetscInt     nmin_s;
    3352:         PetscBool    compute_range;

    3354:         B_neigs = 0;
    3355:         compute_range = (PetscBool)!same_data;
    3356:         if (nmin >= subset_size) compute_range = PETSC_FALSE;

    3358:         if (pcbddc->dbg_flag) {
    3359:           PetscInt nc = 0;

    3361:           if (sub_schurs->change_primal_sub) {
    3362:             ISGetLocalSize(sub_schurs->change_primal_sub[i],&nc);
    3363:           }
    3364:           PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Computing for sub %D/%D size %D count %D fid %D (range %d) (change %D).\n",i,sub_schurs->n_subs,subset_size,pcbddc->mat_graph->count[idxs[0]]+1,pcbddc->mat_graph->which_dof[idxs[0]],compute_range,nc);
    3365:         }

    3367:         PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    3368:         if (compute_range) {

    3370:           /* ask for eigenvalues larger than thresh */
    3371:           if (sub_schurs->is_posdef) {
    3372: #if defined(PETSC_USE_COMPLEX)
    3373:             PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&lower,&upper,&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3374: #else
    3375:             PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&lower,&upper,&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3376: #endif
    3377:             PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3378:           } else { /* no theory so far, but it works nicely */
    3379:             PetscInt  recipe = 0,recipe_m = 1;
    3380:             PetscReal bb[2];

    3382:             PetscOptionsGetInt(NULL,((PetscObject)pc)->prefix,"-pc_bddc_adaptive_recipe",&recipe,NULL);
    3383:             switch (recipe) {
    3384:             case 0:
    3385:               if (scal) { bb[0] = PETSC_MIN_REAL; bb[1] = lthresh; }
    3386:               else { bb[0] = uthresh; bb[1] = PETSC_MAX_REAL; }
    3387: #if defined(PETSC_USE_COMPLEX)
    3388:               PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3389: #else
    3390:               PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3391: #endif
    3392:               PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3393:               break;
    3394:             case 1:
    3395:               bb[0] = PETSC_MIN_REAL; bb[1] = lthresh*lthresh;
    3396: #if defined(PETSC_USE_COMPLEX)
    3397:               PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3398: #else
    3399:               PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3400: #endif
    3401:               PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3402:               if (!scal) {
    3403:                 PetscBLASInt B_neigs2 = 0;

    3405:                 bb[0] = PetscMax(lthresh*lthresh,uthresh); bb[1] = PETSC_MAX_REAL;
    3406:                 PetscArraycpy(S,Sarray+cumarray,subset_size*subset_size);
    3407:                 PetscArraycpy(St,Starray+cumarray,subset_size*subset_size);
    3408: #if defined(PETSC_USE_COMPLEX)
    3409:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs2,eigs+B_neigs,eigv+B_neigs*B_N,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3410: #else
    3411:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs2,eigs+B_neigs,eigv+B_neigs*B_N,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3412: #endif
    3413:                 PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3414:                 B_neigs += B_neigs2;
    3415:               }
    3416:               break;
    3417:             case 2:
    3418:               if (scal) {
    3419:                 bb[0] = PETSC_MIN_REAL;
    3420:                 bb[1] = 0;
    3421: #if defined(PETSC_USE_COMPLEX)
    3422:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3423: #else
    3424:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3425: #endif
    3426:                 PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3427:               } else {
    3428:                 PetscBLASInt B_neigs2 = 0;
    3429:                 PetscBool    import = PETSC_FALSE;

    3431:                 lthresh = PetscMax(lthresh,0.0);
    3432:                 if (lthresh > 0.0) {
    3433:                   bb[0] = PETSC_MIN_REAL;
    3434:                   bb[1] = lthresh*lthresh;

    3436:                   import = PETSC_TRUE;
    3437: #if defined(PETSC_USE_COMPLEX)
    3438:                   PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3439: #else
    3440:                   PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3441: #endif
    3442:                   PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3443:                 }
    3444:                 bb[0] = PetscMax(lthresh*lthresh,uthresh);
    3445:                 bb[1] = PETSC_MAX_REAL;
    3446:                 if (import) {
    3447:                   PetscArraycpy(S,Sarray+cumarray,subset_size*subset_size);
    3448:                   PetscArraycpy(St,Starray+cumarray,subset_size*subset_size);
    3449:                 }
    3450: #if defined(PETSC_USE_COMPLEX)
    3451:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs2,eigs+B_neigs,eigv+B_neigs*B_N,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3452: #else
    3453:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs2,eigs+B_neigs,eigv+B_neigs*B_N,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3454: #endif
    3455:                 PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3456:                 B_neigs += B_neigs2;
    3457:               }
    3458:               break;
    3459:             case 3:
    3460:               if (scal) {
    3461:                 PetscOptionsGetInt(NULL,((PetscObject)pc)->prefix,"-pc_bddc_adaptive_recipe3_min_scal",&recipe_m,NULL);
    3462:               } else {
    3463:                 PetscOptionsGetInt(NULL,((PetscObject)pc)->prefix,"-pc_bddc_adaptive_recipe3_min",&recipe_m,NULL);
    3464:               }
    3465:               if (!scal) {
    3466:                 bb[0] = uthresh;
    3467:                 bb[1] = PETSC_MAX_REAL;
    3468: #if defined(PETSC_USE_COMPLEX)
    3469:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3470: #else
    3471:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3472: #endif
    3473:                 PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3474:               }
    3475:               if (recipe_m > 0 && B_N - B_neigs > 0) {
    3476:                 PetscBLASInt B_neigs2 = 0;

    3478:                 B_IL = 1;
    3479:                 PetscBLASIntCast(PetscMin(recipe_m,B_N - B_neigs),&B_IU);
    3480:                 PetscArraycpy(S,Sarray+cumarray,subset_size*subset_size);
    3481:                 PetscArraycpy(St,Starray+cumarray,subset_size*subset_size);
    3482: #if defined(PETSC_USE_COMPLEX)
    3483:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","I","L",&B_N,St,&B_N,S,&B_N,&lower,&upper,&B_IL,&B_IU,&eps,&B_neigs2,eigs+B_neigs,eigv+B_neigs*B_N,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3484: #else
    3485:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","I","L",&B_N,St,&B_N,S,&B_N,&lower,&upper,&B_IL,&B_IU,&eps,&B_neigs2,eigs+B_neigs,eigv+B_neigs*B_N,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3486: #endif
    3487:                 PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3488:                 B_neigs += B_neigs2;
    3489:               }
    3490:               break;
    3491:             case 4:
    3492:               bb[0] = PETSC_MIN_REAL; bb[1] = lthresh;
    3493: #if defined(PETSC_USE_COMPLEX)
    3494:               PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3495: #else
    3496:               PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3497: #endif
    3498:               PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3499:               {
    3500:                 PetscBLASInt B_neigs2 = 0;

    3502:                 bb[0] = PetscMax(lthresh+PETSC_SMALL,uthresh); bb[1] = PETSC_MAX_REAL;
    3503:                 PetscArraycpy(S,Sarray+cumarray,subset_size*subset_size);
    3504:                 PetscArraycpy(St,Starray+cumarray,subset_size*subset_size);
    3505: #if defined(PETSC_USE_COMPLEX)
    3506:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs2,eigs+B_neigs,eigv+B_neigs*B_N,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3507: #else
    3508:                 PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","V","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs2,eigs+B_neigs,eigv+B_neigs*B_N,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3509: #endif
    3510:                 PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3511:                 B_neigs += B_neigs2;
    3512:               }
    3513:               break;
    3514:             case 5: /* same as before: first compute all eigenvalues, then filter */
    3515: #if defined(PETSC_USE_COMPLEX)
    3516:               PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","A","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3517: #else
    3518:               PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","A","L",&B_N,St,&B_N,S,&B_N,&bb[0],&bb[1],&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3519: #endif
    3520:               PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3521:               {
    3522:                 PetscInt e,k,ne;
    3523:                 for (e=0,ne=0;e<B_neigs;e++) {
    3524:                   if (eigs[e] < lthresh || eigs[e] > uthresh) {
    3525:                     for (k=0;k<B_N;k++) S[ne*B_N+k] = eigv[e*B_N+k];
    3526:                     eigs[ne] = eigs[e];
    3527:                     ne++;
    3528:                   }
    3529:                 }
    3530:                 PetscArraycpy(eigv,S,B_N*ne);
    3531:                 B_neigs = ne;
    3532:               }
    3533:               break;
    3534:             default:
    3535:               SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Unknown recipe %D",recipe);
    3536:             }
    3537:           }
    3538:         } else if (!same_data) { /* this is just to see all the eigenvalues */
    3539:           B_IU = PetscMax(1,PetscMin(B_N,nmax));
    3540:           B_IL = 1;
    3541: #if defined(PETSC_USE_COMPLEX)
    3542:           PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","I","L",&B_N,St,&B_N,S,&B_N,&lower,&upper,&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3543: #else
    3544:           PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","I","L",&B_N,St,&B_N,S,&B_N,&lower,&upper,&B_IL,&B_IU,&eps,&B_neigs,eigs,eigv,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3545: #endif
    3546:           PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3547:         } else { /* same_data is true, so just get the adaptive functional requested by the user */
    3548:           PetscInt k;
    3549:           if (!sub_schurs->change_primal_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"This should not happen");
    3550:           ISGetLocalSize(sub_schurs->change_primal_sub[i],&nmax);
    3551:           PetscBLASIntCast(nmax,&B_neigs);
    3552:           nmin = nmax;
    3553:           PetscArrayzero(eigv,subset_size*nmax);
    3554:           for (k=0;k<nmax;k++) {
    3555:             eigs[k] = 1./PETSC_SMALL;
    3556:             eigv[k*(subset_size+1)] = 1.0;
    3557:           }
    3558:         }
    3559:         PetscFPTrapPop();
    3560:         if (B_ierr) {
    3561:           if (B_ierr < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in SYGVX Lapack routine: illegal value for argument %d",-(int)B_ierr);
    3562:           else if (B_ierr <= B_N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in SYGVX Lapack routine: %d eigenvalues failed to converge",(int)B_ierr);
    3563:           else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in SYGVX Lapack routine: leading minor of order %d is not positive definite",(int)B_ierr-B_N-1);
    3564:         }

    3566:         if (B_neigs > nmax) {
    3567:           if (pcbddc->dbg_flag) {
    3568:             PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"   found %d eigs, more than maximum required %D.\n",B_neigs,nmax);
    3569:           }
    3570:           if (pcbddc->use_deluxe_scaling) eigs_start = scal ? 0 : B_neigs-nmax;
    3571:           B_neigs = nmax;
    3572:         }

    3574:         nmin_s = PetscMin(nmin,B_N);
    3575:         if (B_neigs < nmin_s) {
    3576:           PetscBLASInt B_neigs2 = 0;

    3578:           if (pcbddc->use_deluxe_scaling) {
    3579:             if (scal) {
    3580:               B_IU = nmin_s;
    3581:               B_IL = B_neigs + 1;
    3582:             } else {
    3583:               B_IL = B_N - nmin_s + 1;
    3584:               B_IU = B_N - B_neigs;
    3585:             }
    3586:           } else {
    3587:             B_IL = B_neigs + 1;
    3588:             B_IU = nmin_s;
    3589:           }
    3590:           if (pcbddc->dbg_flag) {
    3591:             PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"   found %d eigs, less than minimum required %D. Asking for %d to %d incl (fortran like)\n",B_neigs,nmin,B_IL,B_IU);
    3592:           }
    3593:           if (sub_schurs->is_symmetric) {
    3594:             PetscInt j,k;
    3595:             for (j=0;j<subset_size;j++) {
    3596:               for (k=j;k<subset_size;k++) {
    3597:                 S [j*subset_size+k] = Sarray [cumarray+j*subset_size+k];
    3598:                 St[j*subset_size+k] = Starray[cumarray+j*subset_size+k];
    3599:               }
    3600:             }
    3601:           } else {
    3602:             PetscArraycpy(S,Sarray+cumarray,subset_size*subset_size);
    3603:             PetscArraycpy(St,Starray+cumarray,subset_size*subset_size);
    3604:           }
    3605:           PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    3606: #if defined(PETSC_USE_COMPLEX)
    3607:           PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","I","L",&B_N,St,&B_N,S,&B_N,&lower,&upper,&B_IL,&B_IU,&eps,&B_neigs2,eigs+B_neigs,eigv+B_neigs*subset_size,&B_N,work,&B_lwork,rwork,B_iwork,B_ifail,&B_ierr));
    3608: #else
    3609:           PetscStackCallBLAS("LAPACKsygvx",LAPACKsygvx_(&B_itype,"V","I","L",&B_N,St,&B_N,S,&B_N,&lower,&upper,&B_IL,&B_IU,&eps,&B_neigs2,eigs+B_neigs,eigv+B_neigs*subset_size,&B_N,work,&B_lwork,B_iwork,B_ifail,&B_ierr));
    3610: #endif
    3611:           PetscLogFlops((4.0*subset_size*subset_size*subset_size)/3.0);
    3612:           PetscFPTrapPop();
    3613:           B_neigs += B_neigs2;
    3614:         }
    3615:         if (B_ierr) {
    3616:           if (B_ierr < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in SYGVX Lapack routine: illegal value for argument %d",-(int)B_ierr);
    3617:           else if (B_ierr <= B_N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in SYGVX Lapack routine: %d eigenvalues failed to converge",(int)B_ierr);
    3618:           else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in SYGVX Lapack routine: leading minor of order %d is not positive definite",(int)B_ierr-B_N-1);
    3619:         }
    3620:         if (pcbddc->dbg_flag) {
    3621:           PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"   -> Got %d eigs\n",B_neigs);
    3622:           for (j=0;j<B_neigs;j++) {
    3623:             if (eigs[j] == 0.0) {
    3624:               PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"     Inf\n");
    3625:             } else {
    3626:               if (pcbddc->use_deluxe_scaling) {
    3627:                 PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"     %1.6e\n",eigs[j+eigs_start]);
    3628:               } else {
    3629:                 PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"     %1.6e\n",1./eigs[j+eigs_start]);
    3630:               }
    3631:             }
    3632:           }
    3633:         }
    3634:       } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet implemented");
    3635:     }
    3636:     /* change the basis back to the original one */
    3637:     if (sub_schurs->change) {
    3638:       Mat change,phi,phit;

    3640:       if (pcbddc->dbg_flag > 2) {
    3641:         PetscInt ii;
    3642:         for (ii=0;ii<B_neigs;ii++) {
    3643:           PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"   -> Eigenvector (old basis) %d/%d (%d)\n",ii,B_neigs,B_N);
    3644:           for (j=0;j<B_N;j++) {
    3645: #if defined(PETSC_USE_COMPLEX)
    3646:             PetscReal r = PetscRealPart(eigv[(ii+eigs_start)*subset_size+j]);
    3647:             PetscReal c = PetscImaginaryPart(eigv[(ii+eigs_start)*subset_size+j]);
    3648:             PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"       %1.4e + %1.4e i\n",r,c);
    3649: #else
    3650:             PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"       %1.4e\n",eigv[(ii+eigs_start)*subset_size+j]);
    3651: #endif
    3652:           }
    3653:         }
    3654:       }
    3655:       KSPGetOperators(sub_schurs->change[i],&change,NULL);
    3656:       MatCreateSeqDense(PETSC_COMM_SELF,subset_size,B_neigs,eigv+eigs_start*subset_size,&phit);
    3657:       MatMatMult(change,phit,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&phi);
    3658:       MatCopy(phi,phit,SAME_NONZERO_PATTERN);
    3659:       MatDestroy(&phit);
    3660:       MatDestroy(&phi);
    3661:     }
    3662:     maxneigs = PetscMax(B_neigs,maxneigs);
    3663:     pcbddc->adaptive_constraints_n[i+nv] = B_neigs;
    3664:     if (B_neigs) {
    3665:       PetscArraycpy(pcbddc->adaptive_constraints_data+pcbddc->adaptive_constraints_data_ptr[cum],eigv+eigs_start*subset_size,B_neigs*subset_size);

    3667:       if (pcbddc->dbg_flag > 1) {
    3668:         PetscInt ii;
    3669:         for (ii=0;ii<B_neigs;ii++) {
    3670:           PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"   -> Eigenvector %d/%d (%d)\n",ii,B_neigs,B_N);
    3671:           for (j=0;j<B_N;j++) {
    3672: #if defined(PETSC_USE_COMPLEX)
    3673:             PetscReal r = PetscRealPart(pcbddc->adaptive_constraints_data[ii*subset_size+j+pcbddc->adaptive_constraints_data_ptr[cum]]);
    3674:             PetscReal c = PetscImaginaryPart(pcbddc->adaptive_constraints_data[ii*subset_size+j+pcbddc->adaptive_constraints_data_ptr[cum]]);
    3675:             PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"       %1.4e + %1.4e i\n",r,c);
    3676: #else
    3677:             PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"       %1.4e\n",pcbddc->adaptive_constraints_data[ii*subset_size+j+pcbddc->adaptive_constraints_data_ptr[cum]]);
    3678: #endif
    3679:           }
    3680:         }
    3681:       }
    3682:       PetscArraycpy(pcbddc->adaptive_constraints_idxs+pcbddc->adaptive_constraints_idxs_ptr[cum],idxs,subset_size);
    3683:       pcbddc->adaptive_constraints_idxs_ptr[cum+1] = pcbddc->adaptive_constraints_idxs_ptr[cum] + subset_size;
    3684:       pcbddc->adaptive_constraints_data_ptr[cum+1] = pcbddc->adaptive_constraints_data_ptr[cum] + subset_size*B_neigs;
    3685:       cum++;
    3686:     }
    3687:     ISRestoreIndices(sub_schurs->is_subs[i],&idxs);
    3688:     /* shift for next computation */
    3689:     cumarray += subset_size*subset_size;
    3690:   }
    3691:   if (pcbddc->dbg_flag) {
    3692:     PetscViewerFlush(pcbddc->dbg_viewer);
    3693:   }

    3695:   if (mss) {
    3696:     MatSeqAIJRestoreArray(sub_schurs->sum_S_Ej_inv_all,&Sarray);
    3697:     MatSeqAIJRestoreArray(sub_schurs->sum_S_Ej_tilda_all,&Starray);
    3698:     /* destroy matrices (junk) */
    3699:     MatDestroy(&sub_schurs->sum_S_Ej_inv_all);
    3700:     MatDestroy(&sub_schurs->sum_S_Ej_tilda_all);
    3701:   }
    3702:   if (allocated_S_St) {
    3703:     PetscFree2(S,St);
    3704:   }
    3705:   PetscFree5(eigv,eigs,work,B_iwork,B_ifail);
    3706: #if defined(PETSC_USE_COMPLEX)
    3707:   PetscFree(rwork);
    3708: #endif
    3709:   if (pcbddc->dbg_flag) {
    3710:     PetscInt maxneigs_r;
    3711:     MPIU_Allreduce(&maxneigs,&maxneigs_r,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)pc));
    3712:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"Maximum number of constraints per cc %D\n",maxneigs_r);
    3713:   }
    3714:   PetscLogEventEnd(PC_BDDC_AdaptiveSetUp[pcbddc->current_level],pc,0,0,0);
    3715:   return(0);
    3716: }

    3718: PetscErrorCode PCBDDCSetUpSolvers(PC pc)
    3719: {
    3720:   PetscScalar    *coarse_submat_vals;

    3724:   /* Setup local scatters R_to_B and (optionally) R_to_D */
    3725:   /* PCBDDCSetUpLocalWorkVectors should be called first! */
    3726:   PCBDDCSetUpLocalScatters(pc);

    3728:   /* Setup local neumann solver ksp_R */
    3729:   /* PCBDDCSetUpLocalScatters should be called first! */
    3730:   PCBDDCSetUpLocalSolvers(pc,PETSC_FALSE,PETSC_TRUE);

    3732:   /*
    3733:      Setup local correction and local part of coarse basis.
    3734:      Gives back the dense local part of the coarse matrix in column major ordering
    3735:   */
    3736:   PCBDDCSetUpCorrection(pc,&coarse_submat_vals);

    3738:   /* Compute total number of coarse nodes and setup coarse solver */
    3739:   PCBDDCSetUpCoarseSolver(pc,coarse_submat_vals);

    3741:   /* free */
    3742:   PetscFree(coarse_submat_vals);
    3743:   return(0);
    3744: }

    3746: PetscErrorCode PCBDDCResetCustomization(PC pc)
    3747: {
    3748:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;

    3752:   ISDestroy(&pcbddc->user_primal_vertices);
    3753:   ISDestroy(&pcbddc->user_primal_vertices_local);
    3754:   ISDestroy(&pcbddc->NeumannBoundaries);
    3755:   ISDestroy(&pcbddc->NeumannBoundariesLocal);
    3756:   ISDestroy(&pcbddc->DirichletBoundaries);
    3757:   MatNullSpaceDestroy(&pcbddc->onearnullspace);
    3758:   PetscFree(pcbddc->onearnullvecs_state);
    3759:   ISDestroy(&pcbddc->DirichletBoundariesLocal);
    3760:   PCBDDCSetDofsSplitting(pc,0,NULL);
    3761:   PCBDDCSetDofsSplittingLocal(pc,0,NULL);
    3762:   return(0);
    3763: }

    3765: PetscErrorCode PCBDDCResetTopography(PC pc)
    3766: {
    3767:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    3768:   PetscInt       i;

    3772:   MatDestroy(&pcbddc->nedcG);
    3773:   ISDestroy(&pcbddc->nedclocal);
    3774:   MatDestroy(&pcbddc->discretegradient);
    3775:   MatDestroy(&pcbddc->user_ChangeOfBasisMatrix);
    3776:   MatDestroy(&pcbddc->ChangeOfBasisMatrix);
    3777:   MatDestroy(&pcbddc->switch_static_change);
    3778:   VecDestroy(&pcbddc->work_change);
    3779:   MatDestroy(&pcbddc->ConstraintMatrix);
    3780:   MatDestroy(&pcbddc->divudotp);
    3781:   ISDestroy(&pcbddc->divudotp_vl2l);
    3782:   PCBDDCGraphDestroy(&pcbddc->mat_graph);
    3783:   for (i=0;i<pcbddc->n_local_subs;i++) {
    3784:     ISDestroy(&pcbddc->local_subs[i]);
    3785:   }
    3786:   pcbddc->n_local_subs = 0;
    3787:   PetscFree(pcbddc->local_subs);
    3788:   PCBDDCSubSchursDestroy(&pcbddc->sub_schurs);
    3789:   pcbddc->graphanalyzed        = PETSC_FALSE;
    3790:   pcbddc->recompute_topography = PETSC_TRUE;
    3791:   pcbddc->corner_selected      = PETSC_FALSE;
    3792:   return(0);
    3793: }

    3795: PetscErrorCode PCBDDCResetSolvers(PC pc)
    3796: {
    3797:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;

    3801:   VecDestroy(&pcbddc->coarse_vec);
    3802:   if (pcbddc->coarse_phi_B) {
    3803:     PetscScalar *array;
    3804:     MatDenseGetArray(pcbddc->coarse_phi_B,&array);
    3805:     PetscFree(array);
    3806:   }
    3807:   MatDestroy(&pcbddc->coarse_phi_B);
    3808:   MatDestroy(&pcbddc->coarse_phi_D);
    3809:   MatDestroy(&pcbddc->coarse_psi_B);
    3810:   MatDestroy(&pcbddc->coarse_psi_D);
    3811:   VecDestroy(&pcbddc->vec1_P);
    3812:   VecDestroy(&pcbddc->vec1_C);
    3813:   MatDestroy(&pcbddc->local_auxmat2);
    3814:   MatDestroy(&pcbddc->local_auxmat1);
    3815:   VecDestroy(&pcbddc->vec1_R);
    3816:   VecDestroy(&pcbddc->vec2_R);
    3817:   ISDestroy(&pcbddc->is_R_local);
    3818:   VecScatterDestroy(&pcbddc->R_to_B);
    3819:   VecScatterDestroy(&pcbddc->R_to_D);
    3820:   VecScatterDestroy(&pcbddc->coarse_loc_to_glob);
    3821:   KSPReset(pcbddc->ksp_D);
    3822:   KSPReset(pcbddc->ksp_R);
    3823:   KSPReset(pcbddc->coarse_ksp);
    3824:   MatDestroy(&pcbddc->local_mat);
    3825:   PetscFree(pcbddc->primal_indices_local_idxs);
    3826:   PetscFree2(pcbddc->local_primal_ref_node,pcbddc->local_primal_ref_mult);
    3827:   PetscFree(pcbddc->global_primal_indices);
    3828:   ISDestroy(&pcbddc->coarse_subassembling);
    3829:   MatDestroy(&pcbddc->benign_change);
    3830:   VecDestroy(&pcbddc->benign_vec);
    3831:   PCBDDCBenignShellMat(pc,PETSC_TRUE);
    3832:   MatDestroy(&pcbddc->benign_B0);
    3833:   PetscSFDestroy(&pcbddc->benign_sf);
    3834:   if (pcbddc->benign_zerodiag_subs) {
    3835:     PetscInt i;
    3836:     for (i=0;i<pcbddc->benign_n;i++) {
    3837:       ISDestroy(&pcbddc->benign_zerodiag_subs[i]);
    3838:     }
    3839:     PetscFree(pcbddc->benign_zerodiag_subs);
    3840:   }
    3841:   PetscFree3(pcbddc->benign_p0_lidx,pcbddc->benign_p0_gidx,pcbddc->benign_p0);
    3842:   return(0);
    3843: }

    3845: PetscErrorCode PCBDDCSetUpLocalWorkVectors(PC pc)
    3846: {
    3847:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    3848:   PC_IS          *pcis = (PC_IS*)pc->data;
    3849:   VecType        impVecType;
    3850:   PetscInt       n_constraints,n_R,old_size;

    3854:   n_constraints = pcbddc->local_primal_size - pcbddc->benign_n - pcbddc->n_vertices;
    3855:   n_R = pcis->n - pcbddc->n_vertices;
    3856:   VecGetType(pcis->vec1_N,&impVecType);
    3857:   /* local work vectors (try to avoid unneeded work)*/
    3858:   /* R nodes */
    3859:   old_size = -1;
    3860:   if (pcbddc->vec1_R) {
    3861:     VecGetSize(pcbddc->vec1_R,&old_size);
    3862:   }
    3863:   if (n_R != old_size) {
    3864:     VecDestroy(&pcbddc->vec1_R);
    3865:     VecDestroy(&pcbddc->vec2_R);
    3866:     VecCreate(PetscObjectComm((PetscObject)pcis->vec1_N),&pcbddc->vec1_R);
    3867:     VecSetSizes(pcbddc->vec1_R,PETSC_DECIDE,n_R);
    3868:     VecSetType(pcbddc->vec1_R,impVecType);
    3869:     VecDuplicate(pcbddc->vec1_R,&pcbddc->vec2_R);
    3870:   }
    3871:   /* local primal dofs */
    3872:   old_size = -1;
    3873:   if (pcbddc->vec1_P) {
    3874:     VecGetSize(pcbddc->vec1_P,&old_size);
    3875:   }
    3876:   if (pcbddc->local_primal_size != old_size) {
    3877:     VecDestroy(&pcbddc->vec1_P);
    3878:     VecCreate(PetscObjectComm((PetscObject)pcis->vec1_N),&pcbddc->vec1_P);
    3879:     VecSetSizes(pcbddc->vec1_P,PETSC_DECIDE,pcbddc->local_primal_size);
    3880:     VecSetType(pcbddc->vec1_P,impVecType);
    3881:   }
    3882:   /* local explicit constraints */
    3883:   old_size = -1;
    3884:   if (pcbddc->vec1_C) {
    3885:     VecGetSize(pcbddc->vec1_C,&old_size);
    3886:   }
    3887:   if (n_constraints && n_constraints != old_size) {
    3888:     VecDestroy(&pcbddc->vec1_C);
    3889:     VecCreate(PetscObjectComm((PetscObject)pcis->vec1_N),&pcbddc->vec1_C);
    3890:     VecSetSizes(pcbddc->vec1_C,PETSC_DECIDE,n_constraints);
    3891:     VecSetType(pcbddc->vec1_C,impVecType);
    3892:   }
    3893:   return(0);
    3894: }

    3896: PetscErrorCode PCBDDCSetUpCorrection(PC pc, PetscScalar **coarse_submat_vals_n)
    3897: {
    3898:   PetscErrorCode  ierr;
    3899:   /* pointers to pcis and pcbddc */
    3900:   PC_IS*          pcis = (PC_IS*)pc->data;
    3901:   PC_BDDC*        pcbddc = (PC_BDDC*)pc->data;
    3902:   PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs;
    3903:   /* submatrices of local problem */
    3904:   Mat             A_RV,A_VR,A_VV,local_auxmat2_R;
    3905:   /* submatrices of local coarse problem */
    3906:   Mat             S_VV,S_CV,S_VC,S_CC;
    3907:   /* working matrices */
    3908:   Mat             C_CR;
    3909:   /* additional working stuff */
    3910:   PC              pc_R;
    3911:   Mat             F,Brhs = NULL;
    3912:   Vec             dummy_vec;
    3913:   PetscBool       isLU,isCHOL,need_benign_correction,sparserhs;
    3914:   PetscScalar     *coarse_submat_vals; /* TODO: use a PETSc matrix */
    3915:   PetscScalar     *work;
    3916:   PetscInt        *idx_V_B;
    3917:   PetscInt        lda_rhs,n,n_vertices,n_constraints,*p0_lidx_I;
    3918:   PetscInt        i,n_R,n_D,n_B;
    3919:   PetscScalar     one=1.0,m_one=-1.0;

    3922:   if (!pcbddc->symmetric_primal && pcbddc->benign_n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Non-symmetric primal basis computation with benign trick not yet implemented");
    3923:   PetscLogEventBegin(PC_BDDC_CorrectionSetUp[pcbddc->current_level],pc,0,0,0);

    3925:   /* Set Non-overlapping dimensions */
    3926:   n_vertices = pcbddc->n_vertices;
    3927:   n_constraints = pcbddc->local_primal_size - pcbddc->benign_n - n_vertices;
    3928:   n_B = pcis->n_B;
    3929:   n_D = pcis->n - n_B;
    3930:   n_R = pcis->n - n_vertices;

    3932:   /* vertices in boundary numbering */
    3933:   PetscMalloc1(n_vertices,&idx_V_B);
    3934:   ISGlobalToLocalMappingApply(pcis->BtoNmap,IS_GTOLM_DROP,n_vertices,pcbddc->local_primal_ref_node,&i,idx_V_B);
    3935:   if (i != n_vertices) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Error in boundary numbering for BDDC vertices! %D != %D",n_vertices,i);

    3937:   /* Subdomain contribution (Non-overlapping) to coarse matrix  */
    3938:   PetscCalloc1(pcbddc->local_primal_size*pcbddc->local_primal_size,&coarse_submat_vals);
    3939:   MatCreateSeqDense(PETSC_COMM_SELF,n_vertices,n_vertices,coarse_submat_vals,&S_VV);
    3940:   MatDenseSetLDA(S_VV,pcbddc->local_primal_size);
    3941:   MatCreateSeqDense(PETSC_COMM_SELF,n_constraints,n_vertices,coarse_submat_vals+n_vertices,&S_CV);
    3942:   MatDenseSetLDA(S_CV,pcbddc->local_primal_size);
    3943:   MatCreateSeqDense(PETSC_COMM_SELF,n_vertices,n_constraints,coarse_submat_vals+pcbddc->local_primal_size*n_vertices,&S_VC);
    3944:   MatDenseSetLDA(S_VC,pcbddc->local_primal_size);
    3945:   MatCreateSeqDense(PETSC_COMM_SELF,n_constraints,n_constraints,coarse_submat_vals+(pcbddc->local_primal_size+1)*n_vertices,&S_CC);
    3946:   MatDenseSetLDA(S_CC,pcbddc->local_primal_size);

    3948:   /* determine if can use MatSolve routines instead of calling KSPSolve on ksp_R */
    3949:   KSPGetPC(pcbddc->ksp_R,&pc_R);
    3950:   PCSetUp(pc_R);
    3951:   PetscObjectTypeCompare((PetscObject)pc_R,PCLU,&isLU);
    3952:   PetscObjectTypeCompare((PetscObject)pc_R,PCCHOLESKY,&isCHOL);
    3953:   lda_rhs = n_R;
    3954:   need_benign_correction = PETSC_FALSE;
    3955:   if (isLU || isCHOL) {
    3956:     PCFactorGetMatrix(pc_R,&F);
    3957:   } else if (sub_schurs && sub_schurs->reuse_solver) {
    3958:     PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;
    3959:     MatFactorType      type;

    3961:     F = reuse_solver->F;
    3962:     MatGetFactorType(F,&type);
    3963:     if (type == MAT_FACTOR_CHOLESKY) isCHOL = PETSC_TRUE;
    3964:     if (type == MAT_FACTOR_LU) isLU = PETSC_TRUE;
    3965:     MatGetSize(F,&lda_rhs,NULL);
    3966:     need_benign_correction = (PetscBool)(!!reuse_solver->benign_n);
    3967:   } else F = NULL;

    3969:   /* determine if we can use a sparse right-hand side */
    3970:   sparserhs = PETSC_FALSE;
    3971:   if (F) {
    3972:     MatSolverType solver;

    3974:     MatFactorGetSolverType(F,&solver);
    3975:     PetscStrcmp(solver,MATSOLVERMUMPS,&sparserhs);
    3976:   }

    3978:   /* allocate workspace */
    3979:   n = 0;
    3980:   if (n_constraints) {
    3981:     n += lda_rhs*n_constraints;
    3982:   }
    3983:   if (n_vertices) {
    3984:     n = PetscMax(2*lda_rhs*n_vertices,n);
    3985:     n = PetscMax((lda_rhs+n_B)*n_vertices,n);
    3986:   }
    3987:   if (!pcbddc->symmetric_primal) {
    3988:     n = PetscMax(2*lda_rhs*pcbddc->local_primal_size,n);
    3989:   }
    3990:   PetscMalloc1(n,&work);

    3992:   /* create dummy vector to modify rhs and sol of MatMatSolve (work array will never be used) */
    3993:   dummy_vec = NULL;
    3994:   if (need_benign_correction && lda_rhs != n_R && F) {
    3995:     VecCreate(PetscObjectComm((PetscObject)pcis->vec1_N),&dummy_vec);
    3996:     VecSetSizes(dummy_vec,lda_rhs,PETSC_DECIDE);
    3997:     VecSetType(dummy_vec,((PetscObject)pcis->vec1_N)->type_name);
    3998:   }

    4000:   MatDestroy(&pcbddc->local_auxmat1);
    4001:   MatDestroy(&pcbddc->local_auxmat2);

    4003:   /* Precompute stuffs needed for preprocessing and application of BDDC*/
    4004:   if (n_constraints) {
    4005:     Mat         M3,C_B;
    4006:     IS          is_aux;

    4008:     /* Extract constraints on R nodes: C_{CR}  */
    4009:     ISCreateStride(PETSC_COMM_SELF,n_constraints,n_vertices,1,&is_aux);
    4010:     MatCreateSubMatrix(pcbddc->ConstraintMatrix,is_aux,pcbddc->is_R_local,MAT_INITIAL_MATRIX,&C_CR);
    4011:     MatCreateSubMatrix(pcbddc->ConstraintMatrix,is_aux,pcis->is_B_local,MAT_INITIAL_MATRIX,&C_B);

    4013:     /* Assemble         local_auxmat2_R =        (- A_{RR}^{-1} C^T_{CR}) needed by BDDC setup */
    4014:     /* Assemble pcbddc->local_auxmat2   = R_to_B (- A_{RR}^{-1} C^T_{CR}) needed by BDDC application */
    4015:     if (!sparserhs) {
    4016:       PetscArrayzero(work,lda_rhs*n_constraints);
    4017:       for (i=0;i<n_constraints;i++) {
    4018:         const PetscScalar *row_cmat_values;
    4019:         const PetscInt    *row_cmat_indices;
    4020:         PetscInt          size_of_constraint,j;

    4022:         MatGetRow(C_CR,i,&size_of_constraint,&row_cmat_indices,&row_cmat_values);
    4023:         for (j=0;j<size_of_constraint;j++) {
    4024:           work[row_cmat_indices[j]+i*lda_rhs] = -row_cmat_values[j];
    4025:         }
    4026:         MatRestoreRow(C_CR,i,&size_of_constraint,&row_cmat_indices,&row_cmat_values);
    4027:       }
    4028:       MatCreateSeqDense(PETSC_COMM_SELF,lda_rhs,n_constraints,work,&Brhs);
    4029:     } else {
    4030:       Mat tC_CR;

    4032:       MatScale(C_CR,-1.0);
    4033:       if (lda_rhs != n_R) {
    4034:         PetscScalar *aa;
    4035:         PetscInt    r,*ii,*jj;
    4036:         PetscBool   done;

    4038:         MatGetRowIJ(C_CR,0,PETSC_FALSE,PETSC_FALSE,&r,(const PetscInt**)&ii,(const PetscInt**)&jj,&done);
    4039:         if (!done) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"GetRowIJ failed");
    4040:         MatSeqAIJGetArray(C_CR,&aa);
    4041:         MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,n_constraints,lda_rhs,ii,jj,aa,&tC_CR);
    4042:         MatRestoreRowIJ(C_CR,0,PETSC_FALSE,PETSC_FALSE,&r,(const PetscInt**)&ii,(const PetscInt**)&jj,&done);
    4043:         if (!done) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"RestoreRowIJ failed");
    4044:       } else {
    4045:         PetscObjectReference((PetscObject)C_CR);
    4046:         tC_CR = C_CR;
    4047:       }
    4048:       MatCreateTranspose(tC_CR,&Brhs);
    4049:       MatDestroy(&tC_CR);
    4050:     }
    4051:     MatCreateSeqDense(PETSC_COMM_SELF,lda_rhs,n_constraints,NULL,&local_auxmat2_R);
    4052:     if (F) {
    4053:       if (need_benign_correction) {
    4054:         PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;

    4056:         /* rhs is already zero on interior dofs, no need to change the rhs */
    4057:         PetscArrayzero(reuse_solver->benign_save_vals,pcbddc->benign_n);
    4058:       }
    4059:       MatMatSolve(F,Brhs,local_auxmat2_R);
    4060:       if (need_benign_correction) {
    4061:         PetscScalar        *marr;
    4062:         PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;

    4064:         MatDenseGetArray(local_auxmat2_R,&marr);
    4065:         if (lda_rhs != n_R) {
    4066:           for (i=0;i<n_constraints;i++) {
    4067:             VecPlaceArray(dummy_vec,marr+i*lda_rhs);
    4068:             PCBDDCReuseSolversBenignAdapt(reuse_solver,dummy_vec,NULL,PETSC_TRUE,PETSC_TRUE);
    4069:             VecResetArray(dummy_vec);
    4070:           }
    4071:         } else {
    4072:           for (i=0;i<n_constraints;i++) {
    4073:             VecPlaceArray(pcbddc->vec1_R,marr+i*lda_rhs);
    4074:             PCBDDCReuseSolversBenignAdapt(reuse_solver,pcbddc->vec1_R,NULL,PETSC_TRUE,PETSC_TRUE);
    4075:             VecResetArray(pcbddc->vec1_R);
    4076:           }
    4077:         }
    4078:         MatDenseRestoreArray(local_auxmat2_R,&marr);
    4079:       }
    4080:     } else {
    4081:       PetscScalar *marr;

    4083:       MatDenseGetArray(local_auxmat2_R,&marr);
    4084:       for (i=0;i<n_constraints;i++) {
    4085:         VecPlaceArray(pcbddc->vec1_R,work+i*lda_rhs);
    4086:         VecPlaceArray(pcbddc->vec2_R,marr+i*lda_rhs);
    4087:         KSPSolve(pcbddc->ksp_R,pcbddc->vec1_R,pcbddc->vec2_R);
    4088:         KSPCheckSolve(pcbddc->ksp_R,pc,pcbddc->vec2_R);
    4089:         VecResetArray(pcbddc->vec1_R);
    4090:         VecResetArray(pcbddc->vec2_R);
    4091:       }
    4092:       MatDenseRestoreArray(local_auxmat2_R,&marr);
    4093:     }
    4094:     if (sparserhs) {
    4095:       MatScale(C_CR,-1.0);
    4096:     }
    4097:     MatDestroy(&Brhs);
    4098:     if (!pcbddc->switch_static) {
    4099:       MatCreateSeqDense(PETSC_COMM_SELF,n_B,n_constraints,NULL,&pcbddc->local_auxmat2);
    4100:       for (i=0;i<n_constraints;i++) {
    4101:         Vec r, b;
    4102:         MatDenseGetColumnVecRead(local_auxmat2_R,i,&r);
    4103:         MatDenseGetColumnVec(pcbddc->local_auxmat2,i,&b);
    4104:         VecScatterBegin(pcbddc->R_to_B,r,b,INSERT_VALUES,SCATTER_FORWARD);
    4105:         VecScatterEnd(pcbddc->R_to_B,r,b,INSERT_VALUES,SCATTER_FORWARD);
    4106:         MatDenseRestoreColumnVec(pcbddc->local_auxmat2,i,&b);
    4107:         MatDenseRestoreColumnVecRead(local_auxmat2_R,i,&r);
    4108:       }
    4109:       MatMatMult(C_B,pcbddc->local_auxmat2,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&M3);
    4110:     } else {
    4111:       if (lda_rhs != n_R) {
    4112:         IS dummy;

    4114:         ISCreateStride(PETSC_COMM_SELF,n_R,0,1,&dummy);
    4115:         MatCreateSubMatrix(local_auxmat2_R,dummy,NULL,MAT_INITIAL_MATRIX,&pcbddc->local_auxmat2);
    4116:         ISDestroy(&dummy);
    4117:       } else {
    4118:         PetscObjectReference((PetscObject)local_auxmat2_R);
    4119:         pcbddc->local_auxmat2 = local_auxmat2_R;
    4120:       }
    4121:       MatMatMult(C_CR,pcbddc->local_auxmat2,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&M3);
    4122:     }
    4123:     ISDestroy(&is_aux);
    4124:     /* Assemble explicitly S_CC = ( C_{CR} A_{RR}^{-1} C^T_{CR})^{-1}  */
    4125:     MatScale(M3,m_one);
    4126:     if (isCHOL) {
    4127:       MatCholeskyFactor(M3,NULL,NULL);
    4128:     } else {
    4129:       MatLUFactor(M3,NULL,NULL,NULL);
    4130:     }
    4131:     MatSeqDenseInvertFactors_Private(M3);
    4132:     /* Assemble local_auxmat1 = S_CC*C_{CB} needed by BDDC application in KSP and in preproc */
    4133:     MatMatMult(M3,C_B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&pcbddc->local_auxmat1);
    4134:     MatDestroy(&C_B);
    4135:     MatCopy(M3,S_CC,SAME_NONZERO_PATTERN); /* S_CC can have a different LDA, MatMatSolve doesn't support it */
    4136:     MatDestroy(&M3);
    4137:   }

    4139:   /* Get submatrices from subdomain matrix */
    4140:   if (n_vertices) {
    4141: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
    4142:     PetscBool oldpin;
    4143: #endif
    4144:     PetscBool isaij;
    4145:     IS        is_aux;

    4147:     if (sub_schurs && sub_schurs->reuse_solver) { /* is_R_local is not sorted, ISComplement doesn't like it */
    4148:       IS tis;

    4150:       ISDuplicate(pcbddc->is_R_local,&tis);
    4151:       ISSort(tis);
    4152:       ISComplement(tis,0,pcis->n,&is_aux);
    4153:       ISDestroy(&tis);
    4154:     } else {
    4155:       ISComplement(pcbddc->is_R_local,0,pcis->n,&is_aux);
    4156:     }
    4157: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
    4158:     oldpin = pcbddc->local_mat->boundtocpu;
    4159: #endif
    4160:     MatBindToCPU(pcbddc->local_mat,PETSC_TRUE);
    4161:     MatCreateSubMatrix(pcbddc->local_mat,pcbddc->is_R_local,is_aux,MAT_INITIAL_MATRIX,&A_RV);
    4162:     MatCreateSubMatrix(pcbddc->local_mat,is_aux,pcbddc->is_R_local,MAT_INITIAL_MATRIX,&A_VR);
    4163:     PetscObjectBaseTypeCompare((PetscObject)A_VR,MATSEQAIJ,&isaij);
    4164:     if (!isaij) { /* TODO REMOVE: MatMatMult(A_VR,A_RRmA_RV) below may raise an error */
    4165:       MatConvert(A_VR,MATSEQAIJ,MAT_INPLACE_MATRIX,&A_VR);
    4166:     }
    4167:     MatCreateSubMatrix(pcbddc->local_mat,is_aux,is_aux,MAT_INITIAL_MATRIX,&A_VV);
    4168: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
    4169:     MatBindToCPU(pcbddc->local_mat,oldpin);
    4170: #endif
    4171:     ISDestroy(&is_aux);
    4172:   }

    4174:   /* Matrix of coarse basis functions (local) */
    4175:   if (pcbddc->coarse_phi_B) {
    4176:     PetscInt on_B,on_primal,on_D=n_D;
    4177:     if (pcbddc->coarse_phi_D) {
    4178:       MatGetSize(pcbddc->coarse_phi_D,&on_D,NULL);
    4179:     }
    4180:     MatGetSize(pcbddc->coarse_phi_B,&on_B,&on_primal);
    4181:     if (on_B != n_B || on_primal != pcbddc->local_primal_size || on_D != n_D) {
    4182:       PetscScalar *marray;

    4184:       MatDenseGetArray(pcbddc->coarse_phi_B,&marray);
    4185:       PetscFree(marray);
    4186:       MatDestroy(&pcbddc->coarse_phi_B);
    4187:       MatDestroy(&pcbddc->coarse_psi_B);
    4188:       MatDestroy(&pcbddc->coarse_phi_D);
    4189:       MatDestroy(&pcbddc->coarse_psi_D);
    4190:     }
    4191:   }

    4193:   if (!pcbddc->coarse_phi_B) {
    4194:     PetscScalar *marr;

    4196:     /* memory size */
    4197:     n = n_B*pcbddc->local_primal_size;
    4198:     if (pcbddc->switch_static || pcbddc->dbg_flag) n += n_D*pcbddc->local_primal_size;
    4199:     if (!pcbddc->symmetric_primal) n *= 2;
    4200:     PetscCalloc1(n,&marr);
    4201:     MatCreateSeqDense(PETSC_COMM_SELF,n_B,pcbddc->local_primal_size,marr,&pcbddc->coarse_phi_B);
    4202:     marr += n_B*pcbddc->local_primal_size;
    4203:     if (pcbddc->switch_static || pcbddc->dbg_flag) {
    4204:       MatCreateSeqDense(PETSC_COMM_SELF,n_D,pcbddc->local_primal_size,marr,&pcbddc->coarse_phi_D);
    4205:       marr += n_D*pcbddc->local_primal_size;
    4206:     }
    4207:     if (!pcbddc->symmetric_primal) {
    4208:       MatCreateSeqDense(PETSC_COMM_SELF,n_B,pcbddc->local_primal_size,marr,&pcbddc->coarse_psi_B);
    4209:       marr += n_B*pcbddc->local_primal_size;
    4210:       if (pcbddc->switch_static || pcbddc->dbg_flag) {
    4211:         MatCreateSeqDense(PETSC_COMM_SELF,n_D,pcbddc->local_primal_size,marr,&pcbddc->coarse_psi_D);
    4212:       }
    4213:     } else {
    4214:       PetscObjectReference((PetscObject)pcbddc->coarse_phi_B);
    4215:       pcbddc->coarse_psi_B = pcbddc->coarse_phi_B;
    4216:       if (pcbddc->switch_static || pcbddc->dbg_flag) {
    4217:         PetscObjectReference((PetscObject)pcbddc->coarse_phi_D);
    4218:         pcbddc->coarse_psi_D = pcbddc->coarse_phi_D;
    4219:       }
    4220:     }
    4221:   }

    4223:   /* We are now ready to evaluate coarse basis functions and subdomain contribution to coarse problem */
    4224:   p0_lidx_I = NULL;
    4225:   if (pcbddc->benign_n && (pcbddc->switch_static || pcbddc->dbg_flag)) {
    4226:     const PetscInt *idxs;

    4228:     ISGetIndices(pcis->is_I_local,&idxs);
    4229:     PetscMalloc1(pcbddc->benign_n,&p0_lidx_I);
    4230:     for (i=0;i<pcbddc->benign_n;i++) {
    4231:       PetscFindInt(pcbddc->benign_p0_lidx[i],pcis->n-pcis->n_B,idxs,&p0_lidx_I[i]);
    4232:     }
    4233:     ISRestoreIndices(pcis->is_I_local,&idxs);
    4234:   }

    4236:   /* vertices */
    4237:   if (n_vertices) {
    4238:     PetscBool restoreavr = PETSC_FALSE;

    4240:     MatConvert(A_VV,MATDENSE,MAT_INPLACE_MATRIX,&A_VV);

    4242:     if (n_R) {
    4243:       Mat               A_RRmA_RV,A_RV_bcorr=NULL,S_VVt; /* S_VVt with LDA=N */
    4244:       PetscBLASInt      B_N,B_one = 1;
    4245:       const PetscScalar *x;
    4246:       PetscScalar       *y;

    4248:       MatScale(A_RV,m_one);
    4249:       if (need_benign_correction) {
    4250:         ISLocalToGlobalMapping RtoN;
    4251:         IS                     is_p0;
    4252:         PetscInt               *idxs_p0,n;

    4254:         PetscMalloc1(pcbddc->benign_n,&idxs_p0);
    4255:         ISLocalToGlobalMappingCreateIS(pcbddc->is_R_local,&RtoN);
    4256:         ISGlobalToLocalMappingApply(RtoN,IS_GTOLM_DROP,pcbddc->benign_n,pcbddc->benign_p0_lidx,&n,idxs_p0);
    4257:         if (n != pcbddc->benign_n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Error in R numbering for benign p0! %D != %D",n,pcbddc->benign_n);
    4258:         ISLocalToGlobalMappingDestroy(&RtoN);
    4259:         ISCreateGeneral(PETSC_COMM_SELF,n,idxs_p0,PETSC_OWN_POINTER,&is_p0);
    4260:         MatCreateSubMatrix(A_RV,is_p0,NULL,MAT_INITIAL_MATRIX,&A_RV_bcorr);
    4261:         ISDestroy(&is_p0);
    4262:       }

    4264:       MatCreateSeqDense(PETSC_COMM_SELF,lda_rhs,n_vertices,work,&A_RRmA_RV);
    4265:       if (!sparserhs || need_benign_correction) {
    4266:         if (lda_rhs == n_R) {
    4267:           MatConvert(A_RV,MATDENSE,MAT_INPLACE_MATRIX,&A_RV);
    4268:         } else {
    4269:           PetscScalar    *av,*array;
    4270:           const PetscInt *xadj,*adjncy;
    4271:           PetscInt       n;
    4272:           PetscBool      flg_row;

    4274:           array = work+lda_rhs*n_vertices;
    4275:           PetscArrayzero(array,lda_rhs*n_vertices);
    4276:           MatConvert(A_RV,MATSEQAIJ,MAT_INPLACE_MATRIX,&A_RV);
    4277:           MatGetRowIJ(A_RV,0,PETSC_FALSE,PETSC_FALSE,&n,&xadj,&adjncy,&flg_row);
    4278:           MatSeqAIJGetArray(A_RV,&av);
    4279:           for (i=0;i<n;i++) {
    4280:             PetscInt j;
    4281:             for (j=xadj[i];j<xadj[i+1];j++) array[lda_rhs*adjncy[j]+i] = av[j];
    4282:           }
    4283:           MatRestoreRowIJ(A_RV,0,PETSC_FALSE,PETSC_FALSE,&n,&xadj,&adjncy,&flg_row);
    4284:           MatDestroy(&A_RV);
    4285:           MatCreateSeqDense(PETSC_COMM_SELF,lda_rhs,n_vertices,array,&A_RV);
    4286:         }
    4287:         if (need_benign_correction) {
    4288:           PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;
    4289:           PetscScalar        *marr;

    4291:           MatDenseGetArray(A_RV,&marr);
    4292:           /* need \Phi^T A_RV = (I+L)A_RV, L given by

    4294:                  | 0 0  0 | (V)
    4295:              L = | 0 0 -1 | (P-p0)
    4296:                  | 0 0 -1 | (p0)

    4298:           */
    4299:           for (i=0;i<reuse_solver->benign_n;i++) {
    4300:             const PetscScalar *vals;
    4301:             const PetscInt    *idxs,*idxs_zero;
    4302:             PetscInt          n,j,nz;

    4304:             ISGetLocalSize(reuse_solver->benign_zerodiag_subs[i],&nz);
    4305:             ISGetIndices(reuse_solver->benign_zerodiag_subs[i],&idxs_zero);
    4306:             MatGetRow(A_RV_bcorr,i,&n,&idxs,&vals);
    4307:             for (j=0;j<n;j++) {
    4308:               PetscScalar val = vals[j];
    4309:               PetscInt    k,col = idxs[j];
    4310:               for (k=0;k<nz;k++) marr[idxs_zero[k]+lda_rhs*col] -= val;
    4311:             }
    4312:             MatRestoreRow(A_RV_bcorr,i,&n,&idxs,&vals);
    4313:             ISRestoreIndices(reuse_solver->benign_zerodiag_subs[i],&idxs_zero);
    4314:           }
    4315:           MatDenseRestoreArray(A_RV,&marr);
    4316:         }
    4317:         PetscObjectReference((PetscObject)A_RV);
    4318:         Brhs = A_RV;
    4319:       } else {
    4320:         Mat tA_RVT,A_RVT;

    4322:         if (!pcbddc->symmetric_primal) {
    4323:           /* A_RV already scaled by -1 */
    4324:           MatTranspose(A_RV,MAT_INITIAL_MATRIX,&A_RVT);
    4325:         } else {
    4326:           restoreavr = PETSC_TRUE;
    4327:           MatScale(A_VR,-1.0);
    4328:           PetscObjectReference((PetscObject)A_VR);
    4329:           A_RVT = A_VR;
    4330:         }
    4331:         if (lda_rhs != n_R) {
    4332:           PetscScalar *aa;
    4333:           PetscInt    r,*ii,*jj;
    4334:           PetscBool   done;

    4336:           MatGetRowIJ(A_RVT,0,PETSC_FALSE,PETSC_FALSE,&r,(const PetscInt**)&ii,(const PetscInt**)&jj,&done);
    4337:           if (!done) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"GetRowIJ failed");
    4338:           MatSeqAIJGetArray(A_RVT,&aa);
    4339:           MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,n_vertices,lda_rhs,ii,jj,aa,&tA_RVT);
    4340:           MatRestoreRowIJ(A_RVT,0,PETSC_FALSE,PETSC_FALSE,&r,(const PetscInt**)&ii,(const PetscInt**)&jj,&done);
    4341:           if (!done) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"RestoreRowIJ failed");
    4342:         } else {
    4343:           PetscObjectReference((PetscObject)A_RVT);
    4344:           tA_RVT = A_RVT;
    4345:         }
    4346:         MatCreateTranspose(tA_RVT,&Brhs);
    4347:         MatDestroy(&tA_RVT);
    4348:         MatDestroy(&A_RVT);
    4349:       }
    4350:       if (F) {
    4351:         /* need to correct the rhs */
    4352:         if (need_benign_correction) {
    4353:           PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;
    4354:           PetscScalar        *marr;

    4356:           MatDenseGetArray(Brhs,&marr);
    4357:           if (lda_rhs != n_R) {
    4358:             for (i=0;i<n_vertices;i++) {
    4359:               VecPlaceArray(dummy_vec,marr+i*lda_rhs);
    4360:               PCBDDCReuseSolversBenignAdapt(reuse_solver,dummy_vec,NULL,PETSC_FALSE,PETSC_TRUE);
    4361:               VecResetArray(dummy_vec);
    4362:             }
    4363:           } else {
    4364:             for (i=0;i<n_vertices;i++) {
    4365:               VecPlaceArray(pcbddc->vec1_R,marr+i*lda_rhs);
    4366:               PCBDDCReuseSolversBenignAdapt(reuse_solver,pcbddc->vec1_R,NULL,PETSC_FALSE,PETSC_TRUE);
    4367:               VecResetArray(pcbddc->vec1_R);
    4368:             }
    4369:           }
    4370:           MatDenseRestoreArray(Brhs,&marr);
    4371:         }
    4372:         MatMatSolve(F,Brhs,A_RRmA_RV);
    4373:         if (restoreavr) {
    4374:           MatScale(A_VR,-1.0);
    4375:         }
    4376:         /* need to correct the solution */
    4377:         if (need_benign_correction) {
    4378:           PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;
    4379:           PetscScalar        *marr;

    4381:           MatDenseGetArray(A_RRmA_RV,&marr);
    4382:           if (lda_rhs != n_R) {
    4383:             for (i=0;i<n_vertices;i++) {
    4384:               VecPlaceArray(dummy_vec,marr+i*lda_rhs);
    4385:               PCBDDCReuseSolversBenignAdapt(reuse_solver,dummy_vec,NULL,PETSC_TRUE,PETSC_TRUE);
    4386:               VecResetArray(dummy_vec);
    4387:             }
    4388:           } else {
    4389:             for (i=0;i<n_vertices;i++) {
    4390:               VecPlaceArray(pcbddc->vec1_R,marr+i*lda_rhs);
    4391:               PCBDDCReuseSolversBenignAdapt(reuse_solver,pcbddc->vec1_R,NULL,PETSC_TRUE,PETSC_TRUE);
    4392:               VecResetArray(pcbddc->vec1_R);
    4393:             }
    4394:           }
    4395:           MatDenseRestoreArray(A_RRmA_RV,&marr);
    4396:         }
    4397:       } else {
    4398:         MatDenseGetArray(Brhs,&y);
    4399:         for (i=0;i<n_vertices;i++) {
    4400:           VecPlaceArray(pcbddc->vec1_R,y+i*lda_rhs);
    4401:           VecPlaceArray(pcbddc->vec2_R,work+i*lda_rhs);
    4402:           KSPSolve(pcbddc->ksp_R,pcbddc->vec1_R,pcbddc->vec2_R);
    4403:           KSPCheckSolve(pcbddc->ksp_R,pc,pcbddc->vec2_R);
    4404:           VecResetArray(pcbddc->vec1_R);
    4405:           VecResetArray(pcbddc->vec2_R);
    4406:         }
    4407:         MatDenseRestoreArray(Brhs,&y);
    4408:       }
    4409:       MatDestroy(&A_RV);
    4410:       MatDestroy(&Brhs);
    4411:       /* S_VV and S_CV */
    4412:       if (n_constraints) {
    4413:         Mat B;

    4415:         PetscArrayzero(work+lda_rhs*n_vertices,n_B*n_vertices);
    4416:         for (i=0;i<n_vertices;i++) {
    4417:           VecPlaceArray(pcbddc->vec1_R,work+i*lda_rhs);
    4418:           VecPlaceArray(pcis->vec1_B,work+lda_rhs*n_vertices+i*n_B);
    4419:           VecScatterBegin(pcbddc->R_to_B,pcbddc->vec1_R,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    4420:           VecScatterEnd(pcbddc->R_to_B,pcbddc->vec1_R,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    4421:           VecResetArray(pcis->vec1_B);
    4422:           VecResetArray(pcbddc->vec1_R);
    4423:         }
    4424:         MatCreateSeqDense(PETSC_COMM_SELF,n_B,n_vertices,work+lda_rhs*n_vertices,&B);
    4425:         /* Reuse dense S_C = pcbddc->local_auxmat1 * B */
    4426:         MatProductCreateWithMat(pcbddc->local_auxmat1,B,NULL,S_CV);
    4427:         MatProductSetType(S_CV,MATPRODUCT_AB);
    4428:         MatProductSetFromOptions(S_CV);
    4429:         MatProductSymbolic(S_CV);
    4430:         MatProductNumeric(S_CV);
    4431:         MatProductClear(S_CV);

    4433:         MatDestroy(&B);
    4434:         MatCreateSeqDense(PETSC_COMM_SELF,lda_rhs,n_vertices,work+lda_rhs*n_vertices,&B);
    4435:         /* Reuse B = local_auxmat2_R * S_CV */
    4436:         MatProductCreateWithMat(local_auxmat2_R,S_CV,NULL,B);
    4437:         MatProductSetType(B,MATPRODUCT_AB);
    4438:         MatProductSetFromOptions(B);
    4439:         MatProductSymbolic(B);
    4440:         MatProductNumeric(B);

    4442:         MatScale(S_CV,m_one);
    4443:         PetscBLASIntCast(lda_rhs*n_vertices,&B_N);
    4444:         PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&B_N,&one,work+lda_rhs*n_vertices,&B_one,work,&B_one));
    4445:         MatDestroy(&B);
    4446:       }
    4447:       if (lda_rhs != n_R) {
    4448:         MatDestroy(&A_RRmA_RV);
    4449:         MatCreateSeqDense(PETSC_COMM_SELF,n_R,n_vertices,work,&A_RRmA_RV);
    4450:         MatDenseSetLDA(A_RRmA_RV,lda_rhs);
    4451:       }
    4452:       MatMatMult(A_VR,A_RRmA_RV,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&S_VVt);
    4453:       /* need A_VR * \Phi * A_RRmA_RV = A_VR * (I+L)^T * A_RRmA_RV, L given as before */
    4454:       if (need_benign_correction) {
    4455:         PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;
    4456:         PetscScalar        *marr,*sums;

    4458:         PetscMalloc1(n_vertices,&sums);
    4459:         MatDenseGetArray(S_VVt,&marr);
    4460:         for (i=0;i<reuse_solver->benign_n;i++) {
    4461:           const PetscScalar *vals;
    4462:           const PetscInt    *idxs,*idxs_zero;
    4463:           PetscInt          n,j,nz;

    4465:           ISGetLocalSize(reuse_solver->benign_zerodiag_subs[i],&nz);
    4466:           ISGetIndices(reuse_solver->benign_zerodiag_subs[i],&idxs_zero);
    4467:           for (j=0;j<n_vertices;j++) {
    4468:             PetscInt k;
    4469:             sums[j] = 0.;
    4470:             for (k=0;k<nz;k++) sums[j] += work[idxs_zero[k]+j*lda_rhs];
    4471:           }
    4472:           MatGetRow(A_RV_bcorr,i,&n,&idxs,&vals);
    4473:           for (j=0;j<n;j++) {
    4474:             PetscScalar val = vals[j];
    4475:             PetscInt k;
    4476:             for (k=0;k<n_vertices;k++) {
    4477:               marr[idxs[j]+k*n_vertices] += val*sums[k];
    4478:             }
    4479:           }
    4480:           MatRestoreRow(A_RV_bcorr,i,&n,&idxs,&vals);
    4481:           ISRestoreIndices(reuse_solver->benign_zerodiag_subs[i],&idxs_zero);
    4482:         }
    4483:         PetscFree(sums);
    4484:         MatDenseRestoreArray(S_VVt,&marr);
    4485:         MatDestroy(&A_RV_bcorr);
    4486:       }
    4487:       MatDestroy(&A_RRmA_RV);
    4488:       PetscBLASIntCast(n_vertices*n_vertices,&B_N);
    4489:       MatDenseGetArrayRead(A_VV,&x);
    4490:       MatDenseGetArray(S_VVt,&y);
    4491:       PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&B_N,&one,x,&B_one,y,&B_one));
    4492:       MatDenseRestoreArrayRead(A_VV,&x);
    4493:       MatDenseRestoreArray(S_VVt,&y);
    4494:       MatCopy(S_VVt,S_VV,SAME_NONZERO_PATTERN);
    4495:       MatDestroy(&S_VVt);
    4496:     } else {
    4497:       MatCopy(A_VV,S_VV,SAME_NONZERO_PATTERN);
    4498:     }
    4499:     MatDestroy(&A_VV);

    4501:     /* coarse basis functions */
    4502:     for (i=0;i<n_vertices;i++) {
    4503:       Vec         v;
    4504:       PetscScalar one = 1.0,zero = 0.0;

    4506:       VecPlaceArray(pcbddc->vec1_R,work+lda_rhs*i);
    4507:       MatDenseGetColumnVec(pcbddc->coarse_phi_B,i,&v);
    4508:       VecScatterBegin(pcbddc->R_to_B,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4509:       VecScatterEnd(pcbddc->R_to_B,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4510:       if (PetscDefined(USE_DEBUG)) { /* The following VecSetValues() expects a sequential matrix */
    4511:         PetscMPIInt rank;
    4512:         MPI_Comm_rank(PetscObjectComm((PetscObject)pcbddc->coarse_phi_B),&rank);
    4513:         if (rank > 1) SETERRQ(PetscObjectComm((PetscObject)pcbddc->coarse_phi_B),PETSC_ERR_PLIB,"Expected a sequential dense matrix");
    4514:       }
    4515:       VecSetValues(v,1,&idx_V_B[i],&one,INSERT_VALUES);
    4516:       VecAssemblyBegin(v); /* If v is on device, hope VecSetValues() eventually implemented by a host to device memcopy */
    4517:       VecAssemblyEnd(v);
    4518:       MatDenseRestoreColumnVec(pcbddc->coarse_phi_B,i,&v);

    4520:       if (pcbddc->switch_static || pcbddc->dbg_flag) {
    4521:         PetscInt j;

    4523:         MatDenseGetColumnVec(pcbddc->coarse_phi_D,i,&v);
    4524:         VecScatterBegin(pcbddc->R_to_D,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4525:         VecScatterEnd(pcbddc->R_to_D,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4526:         if (PetscDefined(USE_DEBUG)) { /* The following VecSetValues() expects a sequential matrix */
    4527:           PetscMPIInt rank;
    4528:           MPI_Comm_rank(PetscObjectComm((PetscObject)pcbddc->coarse_phi_D),&rank);
    4529:           if (rank > 1) SETERRQ(PetscObjectComm((PetscObject)pcbddc->coarse_phi_D),PETSC_ERR_PLIB,"Expected a sequential dense matrix");
    4530:         }
    4531:         for (j=0;j<pcbddc->benign_n;j++) {VecSetValues(v,1,&p0_lidx_I[j],&zero,INSERT_VALUES);}
    4532:         VecAssemblyBegin(v);
    4533:         VecAssemblyEnd(v);
    4534:         MatDenseRestoreColumnVec(pcbddc->coarse_phi_D,i,&v);
    4535:       }
    4536:       VecResetArray(pcbddc->vec1_R);
    4537:     }
    4538:     /* if n_R == 0 the object is not destroyed */
    4539:     MatDestroy(&A_RV);
    4540:   }
    4541:   VecDestroy(&dummy_vec);

    4543:   if (n_constraints) {
    4544:     Mat B;

    4546:     MatCreateSeqDense(PETSC_COMM_SELF,lda_rhs,n_constraints,work,&B);
    4547:     MatScale(S_CC,m_one);
    4548:     MatProductCreateWithMat(local_auxmat2_R,S_CC,NULL,B);
    4549:     MatProductSetType(B,MATPRODUCT_AB);
    4550:     MatProductSetFromOptions(B);
    4551:     MatProductSymbolic(B);
    4552:     MatProductNumeric(B);

    4554:     MatScale(S_CC,m_one);
    4555:     if (n_vertices) {
    4556:       if (isCHOL || need_benign_correction) { /* if we can solve the interior problem with cholesky, we should also be fine with transposing here */
    4557:         MatTranspose(S_CV,MAT_REUSE_MATRIX,&S_VC);
    4558:       } else {
    4559:         Mat S_VCt;

    4561:         if (lda_rhs != n_R) {
    4562:           MatDestroy(&B);
    4563:           MatCreateSeqDense(PETSC_COMM_SELF,n_R,n_constraints,work,&B);
    4564:           MatDenseSetLDA(B,lda_rhs);
    4565:         }
    4566:         MatMatMult(A_VR,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&S_VCt);
    4567:         MatCopy(S_VCt,S_VC,SAME_NONZERO_PATTERN);
    4568:         MatDestroy(&S_VCt);
    4569:       }
    4570:     }
    4571:     MatDestroy(&B);
    4572:     /* coarse basis functions */
    4573:     for (i=0;i<n_constraints;i++) {
    4574:       Vec v;

    4576:       VecPlaceArray(pcbddc->vec1_R,work+lda_rhs*i);
    4577:       MatDenseGetColumnVec(pcbddc->coarse_phi_B,i+n_vertices,&v);
    4578:       VecScatterBegin(pcbddc->R_to_B,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4579:       VecScatterEnd(pcbddc->R_to_B,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4580:       MatDenseRestoreColumnVec(pcbddc->coarse_phi_B,i+n_vertices,&v);
    4581:       if (pcbddc->switch_static || pcbddc->dbg_flag) {
    4582:         PetscInt    j;
    4583:         PetscScalar zero = 0.0;
    4584:         MatDenseGetColumnVec(pcbddc->coarse_phi_D,i+n_vertices,&v);
    4585:         VecScatterBegin(pcbddc->R_to_D,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4586:         VecScatterEnd(pcbddc->R_to_D,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4587:         for (j=0;j<pcbddc->benign_n;j++) {VecSetValues(v,1,&p0_lidx_I[j],&zero,INSERT_VALUES);}
    4588:         VecAssemblyBegin(v);
    4589:         VecAssemblyEnd(v);
    4590:         MatDenseRestoreColumnVec(pcbddc->coarse_phi_D,i+n_vertices,&v);
    4591:       }
    4592:       VecResetArray(pcbddc->vec1_R);
    4593:     }
    4594:   }
    4595:   if (n_constraints) {
    4596:     MatDestroy(&local_auxmat2_R);
    4597:   }
    4598:   PetscFree(p0_lidx_I);

    4600:   /* coarse matrix entries relative to B_0 */
    4601:   if (pcbddc->benign_n) {
    4602:     Mat               B0_B,B0_BPHI;
    4603:     IS                is_dummy;
    4604:     const PetscScalar *data;
    4605:     PetscInt          j;

    4607:     ISCreateStride(PETSC_COMM_SELF,pcbddc->benign_n,0,1,&is_dummy);
    4608:     MatCreateSubMatrix(pcbddc->benign_B0,is_dummy,pcis->is_B_local,MAT_INITIAL_MATRIX,&B0_B);
    4609:     ISDestroy(&is_dummy);
    4610:     MatMatMult(B0_B,pcbddc->coarse_phi_B,MAT_INITIAL_MATRIX,1.0,&B0_BPHI);
    4611:     MatConvert(B0_BPHI,MATSEQDENSE,MAT_INPLACE_MATRIX,&B0_BPHI);
    4612:     MatDenseGetArrayRead(B0_BPHI,&data);
    4613:     for (j=0;j<pcbddc->benign_n;j++) {
    4614:       PetscInt primal_idx = pcbddc->local_primal_size - pcbddc->benign_n + j;
    4615:       for (i=0;i<pcbddc->local_primal_size;i++) {
    4616:         coarse_submat_vals[primal_idx*pcbddc->local_primal_size+i] = data[i*pcbddc->benign_n+j];
    4617:         coarse_submat_vals[i*pcbddc->local_primal_size+primal_idx] = data[i*pcbddc->benign_n+j];
    4618:       }
    4619:     }
    4620:     MatDenseRestoreArrayRead(B0_BPHI,&data);
    4621:     MatDestroy(&B0_B);
    4622:     MatDestroy(&B0_BPHI);
    4623:   }

    4625:   /* compute other basis functions for non-symmetric problems */
    4626:   if (!pcbddc->symmetric_primal) {
    4627:     Mat         B_V=NULL,B_C=NULL;
    4628:     PetscScalar *marray;

    4630:     if (n_constraints) {
    4631:       Mat S_CCT,C_CRT;

    4633:       MatTranspose(C_CR,MAT_INITIAL_MATRIX,&C_CRT);
    4634:       MatTranspose(S_CC,MAT_INITIAL_MATRIX,&S_CCT);
    4635:       MatMatMult(C_CRT,S_CCT,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&B_C);
    4636:       MatDestroy(&S_CCT);
    4637:       if (n_vertices) {
    4638:         Mat S_VCT;

    4640:         MatTranspose(S_VC,MAT_INITIAL_MATRIX,&S_VCT);
    4641:         MatMatMult(C_CRT,S_VCT,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&B_V);
    4642:         MatDestroy(&S_VCT);
    4643:       }
    4644:       MatDestroy(&C_CRT);
    4645:     } else {
    4646:       MatCreateSeqDense(PETSC_COMM_SELF,n_R,n_vertices,NULL,&B_V);
    4647:     }
    4648:     if (n_vertices && n_R) {
    4649:       PetscScalar    *av,*marray;
    4650:       const PetscInt *xadj,*adjncy;
    4651:       PetscInt       n;
    4652:       PetscBool      flg_row;

    4654:       /* B_V = B_V - A_VR^T */
    4655:       MatConvert(A_VR,MATSEQAIJ,MAT_INPLACE_MATRIX,&A_VR);
    4656:       MatGetRowIJ(A_VR,0,PETSC_FALSE,PETSC_FALSE,&n,&xadj,&adjncy,&flg_row);
    4657:       MatSeqAIJGetArray(A_VR,&av);
    4658:       MatDenseGetArray(B_V,&marray);
    4659:       for (i=0;i<n;i++) {
    4660:         PetscInt j;
    4661:         for (j=xadj[i];j<xadj[i+1];j++) marray[i*n_R + adjncy[j]] -= av[j];
    4662:       }
    4663:       MatDenseRestoreArray(B_V,&marray);
    4664:       MatRestoreRowIJ(A_VR,0,PETSC_FALSE,PETSC_FALSE,&n,&xadj,&adjncy,&flg_row);
    4665:       MatDestroy(&A_VR);
    4666:     }

    4668:     /* currently there's no support for MatTransposeMatSolve(F,B,X) */
    4669:     if (n_vertices) {
    4670:       MatDenseGetArray(B_V,&marray);
    4671:       for (i=0;i<n_vertices;i++) {
    4672:         VecPlaceArray(pcbddc->vec1_R,marray+i*n_R);
    4673:         VecPlaceArray(pcbddc->vec2_R,work+i*n_R);
    4674:         KSPSolveTranspose(pcbddc->ksp_R,pcbddc->vec1_R,pcbddc->vec2_R);
    4675:         KSPCheckSolve(pcbddc->ksp_R,pc,pcbddc->vec2_R);
    4676:         VecResetArray(pcbddc->vec1_R);
    4677:         VecResetArray(pcbddc->vec2_R);
    4678:       }
    4679:       MatDenseRestoreArray(B_V,&marray);
    4680:     }
    4681:     if (B_C) {
    4682:       MatDenseGetArray(B_C,&marray);
    4683:       for (i=n_vertices;i<n_constraints+n_vertices;i++) {
    4684:         VecPlaceArray(pcbddc->vec1_R,marray+(i-n_vertices)*n_R);
    4685:         VecPlaceArray(pcbddc->vec2_R,work+i*n_R);
    4686:         KSPSolveTranspose(pcbddc->ksp_R,pcbddc->vec1_R,pcbddc->vec2_R);
    4687:         KSPCheckSolve(pcbddc->ksp_R,pc,pcbddc->vec2_R);
    4688:         VecResetArray(pcbddc->vec1_R);
    4689:         VecResetArray(pcbddc->vec2_R);
    4690:       }
    4691:       MatDenseRestoreArray(B_C,&marray);
    4692:     }
    4693:     /* coarse basis functions */
    4694:     for (i=0;i<pcbddc->local_primal_size;i++) {
    4695:       Vec  v;

    4697:       VecPlaceArray(pcbddc->vec1_R,work+i*n_R);
    4698:       MatDenseGetColumnVec(pcbddc->coarse_psi_B,i,&v);
    4699:       VecScatterBegin(pcbddc->R_to_B,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4700:       VecScatterEnd(pcbddc->R_to_B,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4701:       if (i<n_vertices) {
    4702:         PetscScalar one = 1.0;
    4703:         VecSetValues(v,1,&idx_V_B[i],&one,INSERT_VALUES);
    4704:         VecAssemblyBegin(v);
    4705:         VecAssemblyEnd(v);
    4706:       }
    4707:       MatDenseRestoreColumnVec(pcbddc->coarse_psi_B,i,&v);

    4709:       if (pcbddc->switch_static || pcbddc->dbg_flag) {
    4710:         MatDenseGetColumnVec(pcbddc->coarse_psi_D,i,&v);
    4711:         VecScatterBegin(pcbddc->R_to_D,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4712:         VecScatterEnd(pcbddc->R_to_D,pcbddc->vec1_R,v,INSERT_VALUES,SCATTER_FORWARD);
    4713:         MatDenseRestoreColumnVec(pcbddc->coarse_psi_D,i,&v);
    4714:       }
    4715:       VecResetArray(pcbddc->vec1_R);
    4716:     }
    4717:     MatDestroy(&B_V);
    4718:     MatDestroy(&B_C);
    4719:   }

    4721:   /* free memory */
    4722:   PetscFree(idx_V_B);
    4723:   MatDestroy(&S_VV);
    4724:   MatDestroy(&S_CV);
    4725:   MatDestroy(&S_VC);
    4726:   MatDestroy(&S_CC);
    4727:   PetscFree(work);
    4728:   if (n_vertices) {
    4729:     MatDestroy(&A_VR);
    4730:   }
    4731:   if (n_constraints) {
    4732:     MatDestroy(&C_CR);
    4733:   }
    4734:   PetscLogEventEnd(PC_BDDC_CorrectionSetUp[pcbddc->current_level],pc,0,0,0);

    4736:   /* Checking coarse_sub_mat and coarse basis functios */
    4737:   /* Symmetric case     : It should be \Phi^{(j)^T} A^{(j)} \Phi^{(j)}=coarse_sub_mat */
    4738:   /* Non-symmetric case : It should be \Psi^{(j)^T} A^{(j)} \Phi^{(j)}=coarse_sub_mat */
    4739:   if (pcbddc->dbg_flag) {
    4740:     Mat         coarse_sub_mat;
    4741:     Mat         AUXMAT,TM1,TM2,TM3,TM4;
    4742:     Mat         coarse_phi_D,coarse_phi_B;
    4743:     Mat         coarse_psi_D,coarse_psi_B;
    4744:     Mat         A_II,A_BB,A_IB,A_BI;
    4745:     Mat         C_B,CPHI;
    4746:     IS          is_dummy;
    4747:     Vec         mones;
    4748:     MatType     checkmattype=MATSEQAIJ;
    4749:     PetscReal   real_value;

    4751:     if (pcbddc->benign_n && !pcbddc->benign_change_explicit) {
    4752:       Mat A;
    4753:       PCBDDCBenignProject(pc,NULL,NULL,&A);
    4754:       MatCreateSubMatrix(A,pcis->is_I_local,pcis->is_I_local,MAT_INITIAL_MATRIX,&A_II);
    4755:       MatCreateSubMatrix(A,pcis->is_I_local,pcis->is_B_local,MAT_INITIAL_MATRIX,&A_IB);
    4756:       MatCreateSubMatrix(A,pcis->is_B_local,pcis->is_I_local,MAT_INITIAL_MATRIX,&A_BI);
    4757:       MatCreateSubMatrix(A,pcis->is_B_local,pcis->is_B_local,MAT_INITIAL_MATRIX,&A_BB);
    4758:       MatDestroy(&A);
    4759:     } else {
    4760:       MatConvert(pcis->A_II,checkmattype,MAT_INITIAL_MATRIX,&A_II);
    4761:       MatConvert(pcis->A_IB,checkmattype,MAT_INITIAL_MATRIX,&A_IB);
    4762:       MatConvert(pcis->A_BI,checkmattype,MAT_INITIAL_MATRIX,&A_BI);
    4763:       MatConvert(pcis->A_BB,checkmattype,MAT_INITIAL_MATRIX,&A_BB);
    4764:     }
    4765:     MatConvert(pcbddc->coarse_phi_D,checkmattype,MAT_INITIAL_MATRIX,&coarse_phi_D);
    4766:     MatConvert(pcbddc->coarse_phi_B,checkmattype,MAT_INITIAL_MATRIX,&coarse_phi_B);
    4767:     if (!pcbddc->symmetric_primal) {
    4768:       MatConvert(pcbddc->coarse_psi_D,checkmattype,MAT_INITIAL_MATRIX,&coarse_psi_D);
    4769:       MatConvert(pcbddc->coarse_psi_B,checkmattype,MAT_INITIAL_MATRIX,&coarse_psi_B);
    4770:     }
    4771:     MatCreateSeqDense(PETSC_COMM_SELF,pcbddc->local_primal_size,pcbddc->local_primal_size,coarse_submat_vals,&coarse_sub_mat);

    4773:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"--------------------------------------------------\n");
    4774:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"Check coarse sub mat computation (symmetric %d)\n",pcbddc->symmetric_primal);
    4775:     PetscViewerFlush(pcbddc->dbg_viewer);
    4776:     if (!pcbddc->symmetric_primal) {
    4777:       MatMatMult(A_II,coarse_phi_D,MAT_INITIAL_MATRIX,1.0,&AUXMAT);
    4778:       MatTransposeMatMult(coarse_psi_D,AUXMAT,MAT_INITIAL_MATRIX,1.0,&TM1);
    4779:       MatDestroy(&AUXMAT);
    4780:       MatMatMult(A_BB,coarse_phi_B,MAT_INITIAL_MATRIX,1.0,&AUXMAT);
    4781:       MatTransposeMatMult(coarse_psi_B,AUXMAT,MAT_INITIAL_MATRIX,1.0,&TM2);
    4782:       MatDestroy(&AUXMAT);
    4783:       MatMatMult(A_IB,coarse_phi_B,MAT_INITIAL_MATRIX,1.0,&AUXMAT);
    4784:       MatTransposeMatMult(coarse_psi_D,AUXMAT,MAT_INITIAL_MATRIX,1.0,&TM3);
    4785:       MatDestroy(&AUXMAT);
    4786:       MatMatMult(A_BI,coarse_phi_D,MAT_INITIAL_MATRIX,1.0,&AUXMAT);
    4787:       MatTransposeMatMult(coarse_psi_B,AUXMAT,MAT_INITIAL_MATRIX,1.0,&TM4);
    4788:       MatDestroy(&AUXMAT);
    4789:     } else {
    4790:       MatPtAP(A_II,coarse_phi_D,MAT_INITIAL_MATRIX,1.0,&TM1);
    4791:       MatPtAP(A_BB,coarse_phi_B,MAT_INITIAL_MATRIX,1.0,&TM2);
    4792:       MatMatMult(A_IB,coarse_phi_B,MAT_INITIAL_MATRIX,1.0,&AUXMAT);
    4793:       MatTransposeMatMult(coarse_phi_D,AUXMAT,MAT_INITIAL_MATRIX,1.0,&TM3);
    4794:       MatDestroy(&AUXMAT);
    4795:       MatMatMult(A_BI,coarse_phi_D,MAT_INITIAL_MATRIX,1.0,&AUXMAT);
    4796:       MatTransposeMatMult(coarse_phi_B,AUXMAT,MAT_INITIAL_MATRIX,1.0,&TM4);
    4797:       MatDestroy(&AUXMAT);
    4798:     }
    4799:     MatAXPY(TM1,one,TM2,DIFFERENT_NONZERO_PATTERN);
    4800:     MatAXPY(TM1,one,TM3,DIFFERENT_NONZERO_PATTERN);
    4801:     MatAXPY(TM1,one,TM4,DIFFERENT_NONZERO_PATTERN);
    4802:     MatConvert(TM1,MATSEQDENSE,MAT_INPLACE_MATRIX,&TM1);
    4803:     if (pcbddc->benign_n) {
    4804:       Mat               B0_B,B0_BPHI;
    4805:       const PetscScalar *data2;
    4806:       PetscScalar       *data;
    4807:       PetscInt          j;

    4809:       ISCreateStride(PETSC_COMM_SELF,pcbddc->benign_n,0,1,&is_dummy);
    4810:       MatCreateSubMatrix(pcbddc->benign_B0,is_dummy,pcis->is_B_local,MAT_INITIAL_MATRIX,&B0_B);
    4811:       MatMatMult(B0_B,coarse_phi_B,MAT_INITIAL_MATRIX,1.0,&B0_BPHI);
    4812:       MatConvert(B0_BPHI,MATSEQDENSE,MAT_INPLACE_MATRIX,&B0_BPHI);
    4813:       MatDenseGetArray(TM1,&data);
    4814:       MatDenseGetArrayRead(B0_BPHI,&data2);
    4815:       for (j=0;j<pcbddc->benign_n;j++) {
    4816:         PetscInt primal_idx = pcbddc->local_primal_size - pcbddc->benign_n + j;
    4817:         for (i=0;i<pcbddc->local_primal_size;i++) {
    4818:           data[primal_idx*pcbddc->local_primal_size+i] += data2[i*pcbddc->benign_n+j];
    4819:           data[i*pcbddc->local_primal_size+primal_idx] += data2[i*pcbddc->benign_n+j];
    4820:         }
    4821:       }
    4822:       MatDenseRestoreArray(TM1,&data);
    4823:       MatDenseRestoreArrayRead(B0_BPHI,&data2);
    4824:       MatDestroy(&B0_B);
    4825:       ISDestroy(&is_dummy);
    4826:       MatDestroy(&B0_BPHI);
    4827:     }
    4828: #if 0
    4829:   {
    4830:     PetscViewer viewer;
    4831:     char filename[256];
    4832:     sprintf(filename,"details_local_coarse_mat%d_level%d.m",PetscGlobalRank,pcbddc->current_level);
    4833:     PetscViewerASCIIOpen(PETSC_COMM_SELF,filename,&viewer);
    4834:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
    4835:     PetscObjectSetName((PetscObject)coarse_sub_mat,"computed");
    4836:     MatView(coarse_sub_mat,viewer);
    4837:     PetscObjectSetName((PetscObject)TM1,"projected");
    4838:     MatView(TM1,viewer);
    4839:     if (pcbddc->coarse_phi_B) {
    4840:       PetscObjectSetName((PetscObject)pcbddc->coarse_phi_B,"phi_B");
    4841:       MatView(pcbddc->coarse_phi_B,viewer);
    4842:     }
    4843:     if (pcbddc->coarse_phi_D) {
    4844:       PetscObjectSetName((PetscObject)pcbddc->coarse_phi_D,"phi_D");
    4845:       MatView(pcbddc->coarse_phi_D,viewer);
    4846:     }
    4847:     if (pcbddc->coarse_psi_B) {
    4848:       PetscObjectSetName((PetscObject)pcbddc->coarse_psi_B,"psi_B");
    4849:       MatView(pcbddc->coarse_psi_B,viewer);
    4850:     }
    4851:     if (pcbddc->coarse_psi_D) {
    4852:       PetscObjectSetName((PetscObject)pcbddc->coarse_psi_D,"psi_D");
    4853:       MatView(pcbddc->coarse_psi_D,viewer);
    4854:     }
    4855:     PetscObjectSetName((PetscObject)pcbddc->local_mat,"A");
    4856:     MatView(pcbddc->local_mat,viewer);
    4857:     PetscObjectSetName((PetscObject)pcbddc->ConstraintMatrix,"C");
    4858:     MatView(pcbddc->ConstraintMatrix,viewer);
    4859:     PetscObjectSetName((PetscObject)pcis->is_I_local,"I");
    4860:     ISView(pcis->is_I_local,viewer);
    4861:     PetscObjectSetName((PetscObject)pcis->is_B_local,"B");
    4862:     ISView(pcis->is_B_local,viewer);
    4863:     PetscObjectSetName((PetscObject)pcbddc->is_R_local,"R");
    4864:     ISView(pcbddc->is_R_local,viewer);
    4865:     PetscViewerDestroy(&viewer);
    4866:   }
    4867: #endif
    4868:     MatAXPY(TM1,m_one,coarse_sub_mat,DIFFERENT_NONZERO_PATTERN);
    4869:     MatNorm(TM1,NORM_FROBENIUS,&real_value);
    4870:     PetscViewerASCIIPushSynchronized(pcbddc->dbg_viewer);
    4871:     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d          matrix error % 1.14e\n",PetscGlobalRank,real_value);

    4873:     /* check constraints */
    4874:     ISCreateStride(PETSC_COMM_SELF,pcbddc->local_primal_size-pcbddc->benign_n,0,1,&is_dummy);
    4875:     MatCreateSubMatrix(pcbddc->ConstraintMatrix,is_dummy,pcis->is_B_local,MAT_INITIAL_MATRIX,&C_B);
    4876:     if (!pcbddc->benign_n) { /* TODO: add benign case */
    4877:       MatMatMult(C_B,coarse_phi_B,MAT_INITIAL_MATRIX,1.0,&CPHI);
    4878:     } else {
    4879:       PetscScalar *data;
    4880:       Mat         tmat;
    4881:       MatDenseGetArray(pcbddc->coarse_phi_B,&data);
    4882:       MatCreateSeqDense(PETSC_COMM_SELF,pcis->n_B,pcbddc->local_primal_size-pcbddc->benign_n,data,&tmat);
    4883:       MatDenseRestoreArray(pcbddc->coarse_phi_B,&data);
    4884:       MatMatMult(C_B,tmat,MAT_INITIAL_MATRIX,1.0,&CPHI);
    4885:       MatDestroy(&tmat);
    4886:     }
    4887:     MatCreateVecs(CPHI,&mones,NULL);
    4888:     VecSet(mones,-1.0);
    4889:     MatDiagonalSet(CPHI,mones,ADD_VALUES);
    4890:     MatNorm(CPHI,NORM_FROBENIUS,&real_value);
    4891:     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d phi constraints error % 1.14e\n",PetscGlobalRank,real_value);
    4892:     if (!pcbddc->symmetric_primal) {
    4893:       MatMatMult(C_B,coarse_psi_B,MAT_REUSE_MATRIX,1.0,&CPHI);
    4894:       VecSet(mones,-1.0);
    4895:       MatDiagonalSet(CPHI,mones,ADD_VALUES);
    4896:       MatNorm(CPHI,NORM_FROBENIUS,&real_value);
    4897:       PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d psi constraints error % 1.14e\n",PetscGlobalRank,real_value);
    4898:     }
    4899:     MatDestroy(&C_B);
    4900:     MatDestroy(&CPHI);
    4901:     ISDestroy(&is_dummy);
    4902:     VecDestroy(&mones);
    4903:     PetscViewerFlush(pcbddc->dbg_viewer);
    4904:     MatDestroy(&A_II);
    4905:     MatDestroy(&A_BB);
    4906:     MatDestroy(&A_IB);
    4907:     MatDestroy(&A_BI);
    4908:     MatDestroy(&TM1);
    4909:     MatDestroy(&TM2);
    4910:     MatDestroy(&TM3);
    4911:     MatDestroy(&TM4);
    4912:     MatDestroy(&coarse_phi_D);
    4913:     MatDestroy(&coarse_phi_B);
    4914:     if (!pcbddc->symmetric_primal) {
    4915:       MatDestroy(&coarse_psi_D);
    4916:       MatDestroy(&coarse_psi_B);
    4917:     }
    4918:     MatDestroy(&coarse_sub_mat);
    4919:   }
    4920:   /* FINAL CUDA support (we cannot currently mix viennacl and cuda vectors */
    4921:   {
    4922:     PetscBool gpu;

    4924:     PetscObjectTypeCompare((PetscObject)pcis->vec1_N,VECSEQCUDA,&gpu);
    4925:     if (gpu) {
    4926:       if (pcbddc->local_auxmat1) {
    4927:         MatConvert(pcbddc->local_auxmat1,MATSEQDENSECUDA,MAT_INPLACE_MATRIX,&pcbddc->local_auxmat1);
    4928:       }
    4929:       if (pcbddc->local_auxmat2) {
    4930:         MatConvert(pcbddc->local_auxmat2,MATSEQDENSECUDA,MAT_INPLACE_MATRIX,&pcbddc->local_auxmat2);
    4931:       }
    4932:       if (pcbddc->coarse_phi_B) {
    4933:         MatConvert(pcbddc->coarse_phi_B,MATSEQDENSECUDA,MAT_INPLACE_MATRIX,&pcbddc->coarse_phi_B);
    4934:       }
    4935:       if (pcbddc->coarse_phi_D) {
    4936:         MatConvert(pcbddc->coarse_phi_D,MATSEQDENSECUDA,MAT_INPLACE_MATRIX,&pcbddc->coarse_phi_D);
    4937:       }
    4938:       if (pcbddc->coarse_psi_B) {
    4939:         MatConvert(pcbddc->coarse_psi_B,MATSEQDENSECUDA,MAT_INPLACE_MATRIX,&pcbddc->coarse_psi_B);
    4940:       }
    4941:       if (pcbddc->coarse_psi_D) {
    4942:         MatConvert(pcbddc->coarse_psi_D,MATSEQDENSECUDA,MAT_INPLACE_MATRIX,&pcbddc->coarse_psi_D);
    4943:       }
    4944:     }
    4945:   }
    4946:   /* get back data */
    4947:   *coarse_submat_vals_n = coarse_submat_vals;
    4948:   return(0);
    4949: }

    4951: PetscErrorCode MatCreateSubMatrixUnsorted(Mat A, IS isrow, IS iscol, Mat* B)
    4952: {
    4953:   Mat            *work_mat;
    4954:   IS             isrow_s,iscol_s;
    4955:   PetscBool      rsorted,csorted;
    4956:   PetscInt       rsize,*idxs_perm_r=NULL,csize,*idxs_perm_c=NULL;

    4960:   ISSorted(isrow,&rsorted);
    4961:   ISSorted(iscol,&csorted);
    4962:   ISGetLocalSize(isrow,&rsize);
    4963:   ISGetLocalSize(iscol,&csize);

    4965:   if (!rsorted) {
    4966:     const PetscInt *idxs;
    4967:     PetscInt *idxs_sorted,i;

    4969:     PetscMalloc1(rsize,&idxs_perm_r);
    4970:     PetscMalloc1(rsize,&idxs_sorted);
    4971:     for (i=0;i<rsize;i++) {
    4972:       idxs_perm_r[i] = i;
    4973:     }
    4974:     ISGetIndices(isrow,&idxs);
    4975:     PetscSortIntWithPermutation(rsize,idxs,idxs_perm_r);
    4976:     for (i=0;i<rsize;i++) {
    4977:       idxs_sorted[i] = idxs[idxs_perm_r[i]];
    4978:     }
    4979:     ISRestoreIndices(isrow,&idxs);
    4980:     ISCreateGeneral(PETSC_COMM_SELF,rsize,idxs_sorted,PETSC_OWN_POINTER,&isrow_s);
    4981:   } else {
    4982:     PetscObjectReference((PetscObject)isrow);
    4983:     isrow_s = isrow;
    4984:   }

    4986:   if (!csorted) {
    4987:     if (isrow == iscol) {
    4988:       PetscObjectReference((PetscObject)isrow_s);
    4989:       iscol_s = isrow_s;
    4990:     } else {
    4991:       const PetscInt *idxs;
    4992:       PetscInt       *idxs_sorted,i;

    4994:       PetscMalloc1(csize,&idxs_perm_c);
    4995:       PetscMalloc1(csize,&idxs_sorted);
    4996:       for (i=0;i<csize;i++) {
    4997:         idxs_perm_c[i] = i;
    4998:       }
    4999:       ISGetIndices(iscol,&idxs);
    5000:       PetscSortIntWithPermutation(csize,idxs,idxs_perm_c);
    5001:       for (i=0;i<csize;i++) {
    5002:         idxs_sorted[i] = idxs[idxs_perm_c[i]];
    5003:       }
    5004:       ISRestoreIndices(iscol,&idxs);
    5005:       ISCreateGeneral(PETSC_COMM_SELF,csize,idxs_sorted,PETSC_OWN_POINTER,&iscol_s);
    5006:     }
    5007:   } else {
    5008:     PetscObjectReference((PetscObject)iscol);
    5009:     iscol_s = iscol;
    5010:   }

    5012:   MatCreateSubMatrices(A,1,&isrow_s,&iscol_s,MAT_INITIAL_MATRIX,&work_mat);

    5014:   if (!rsorted || !csorted) {
    5015:     Mat      new_mat;
    5016:     IS       is_perm_r,is_perm_c;

    5018:     if (!rsorted) {
    5019:       PetscInt *idxs_r,i;
    5020:       PetscMalloc1(rsize,&idxs_r);
    5021:       for (i=0;i<rsize;i++) {
    5022:         idxs_r[idxs_perm_r[i]] = i;
    5023:       }
    5024:       PetscFree(idxs_perm_r);
    5025:       ISCreateGeneral(PETSC_COMM_SELF,rsize,idxs_r,PETSC_OWN_POINTER,&is_perm_r);
    5026:     } else {
    5027:       ISCreateStride(PETSC_COMM_SELF,rsize,0,1,&is_perm_r);
    5028:     }
    5029:     ISSetPermutation(is_perm_r);

    5031:     if (!csorted) {
    5032:       if (isrow_s == iscol_s) {
    5033:         PetscObjectReference((PetscObject)is_perm_r);
    5034:         is_perm_c = is_perm_r;
    5035:       } else {
    5036:         PetscInt *idxs_c,i;
    5037:         if (!idxs_perm_c) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Permutation array not present");
    5038:         PetscMalloc1(csize,&idxs_c);
    5039:         for (i=0;i<csize;i++) {
    5040:           idxs_c[idxs_perm_c[i]] = i;
    5041:         }
    5042:         PetscFree(idxs_perm_c);
    5043:         ISCreateGeneral(PETSC_COMM_SELF,csize,idxs_c,PETSC_OWN_POINTER,&is_perm_c);
    5044:       }
    5045:     } else {
    5046:       ISCreateStride(PETSC_COMM_SELF,csize,0,1,&is_perm_c);
    5047:     }
    5048:     ISSetPermutation(is_perm_c);

    5050:     MatPermute(work_mat[0],is_perm_r,is_perm_c,&new_mat);
    5051:     MatDestroy(&work_mat[0]);
    5052:     work_mat[0] = new_mat;
    5053:     ISDestroy(&is_perm_r);
    5054:     ISDestroy(&is_perm_c);
    5055:   }

    5057:   PetscObjectReference((PetscObject)work_mat[0]);
    5058:   *B = work_mat[0];
    5059:   MatDestroyMatrices(1,&work_mat);
    5060:   ISDestroy(&isrow_s);
    5061:   ISDestroy(&iscol_s);
    5062:   return(0);
    5063: }

    5065: PetscErrorCode PCBDDCComputeLocalMatrix(PC pc, Mat ChangeOfBasisMatrix)
    5066: {
    5067:   Mat_IS*        matis = (Mat_IS*)pc->pmat->data;
    5068:   PC_BDDC*       pcbddc = (PC_BDDC*)pc->data;
    5069:   Mat            new_mat,lA;
    5070:   IS             is_local,is_global;
    5071:   PetscInt       local_size;
    5072:   PetscBool      isseqaij;

    5076:   MatDestroy(&pcbddc->local_mat);
    5077:   MatGetSize(matis->A,&local_size,NULL);
    5078:   ISCreateStride(PetscObjectComm((PetscObject)matis->A),local_size,0,1,&is_local);
    5079:   ISLocalToGlobalMappingApplyIS(pc->pmat->rmap->mapping,is_local,&is_global);
    5080:   ISDestroy(&is_local);
    5081:   MatCreateSubMatrixUnsorted(ChangeOfBasisMatrix,is_global,is_global,&new_mat);
    5082:   ISDestroy(&is_global);

    5084:   if (pcbddc->dbg_flag) {
    5085:     Vec       x,x_change;
    5086:     PetscReal error;

    5088:     MatCreateVecs(ChangeOfBasisMatrix,&x,&x_change);
    5089:     VecSetRandom(x,NULL);
    5090:     MatMult(ChangeOfBasisMatrix,x,x_change);
    5091:     VecScatterBegin(matis->cctx,x,matis->x,INSERT_VALUES,SCATTER_FORWARD);
    5092:     VecScatterEnd(matis->cctx,x,matis->x,INSERT_VALUES,SCATTER_FORWARD);
    5093:     MatMult(new_mat,matis->x,matis->y);
    5094:     if (!pcbddc->change_interior) {
    5095:       const PetscScalar *x,*y,*v;
    5096:       PetscReal         lerror = 0.;
    5097:       PetscInt          i;

    5099:       VecGetArrayRead(matis->x,&x);
    5100:       VecGetArrayRead(matis->y,&y);
    5101:       VecGetArrayRead(matis->counter,&v);
    5102:       for (i=0;i<local_size;i++)
    5103:         if (PetscRealPart(v[i]) < 1.5 && PetscAbsScalar(x[i]-y[i]) > lerror)
    5104:           lerror = PetscAbsScalar(x[i]-y[i]);
    5105:       VecRestoreArrayRead(matis->x,&x);
    5106:       VecRestoreArrayRead(matis->y,&y);
    5107:       VecRestoreArrayRead(matis->counter,&v);
    5108:       MPIU_Allreduce(&lerror,&error,1,MPIU_REAL,MPI_MAX,PetscObjectComm((PetscObject)pc));
    5109:       if (error > PETSC_SMALL) {
    5110:         if (!pcbddc->user_ChangeOfBasisMatrix || pcbddc->current_level) {
    5111:           SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"Error global vs local change on I: %1.6e",error);
    5112:         } else {
    5113:           SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"Error global vs local change on I: %1.6e",error);
    5114:         }
    5115:       }
    5116:     }
    5117:     VecScatterBegin(matis->rctx,matis->y,x,INSERT_VALUES,SCATTER_REVERSE);
    5118:     VecScatterEnd(matis->rctx,matis->y,x,INSERT_VALUES,SCATTER_REVERSE);
    5119:     VecAXPY(x,-1.0,x_change);
    5120:     VecNorm(x,NORM_INFINITY,&error);
    5121:     if (error > PETSC_SMALL) {
    5122:       if (!pcbddc->user_ChangeOfBasisMatrix || pcbddc->current_level) {
    5123:         SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"Error global vs local change on N: %1.6e",error);
    5124:       } else {
    5125:         SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"Error global vs local change on N: %1.6e",error);
    5126:       }
    5127:     }
    5128:     VecDestroy(&x);
    5129:     VecDestroy(&x_change);
    5130:   }

    5132:   /* lA is present if we are setting up an inner BDDC for a saddle point FETI-DP */
    5133:   PetscObjectQuery((PetscObject)pc,"__KSPFETIDP_lA" ,(PetscObject*)&lA);

    5135:   /* TODO: HOW TO WORK WITH BAIJ and SBAIJ and SEQDENSE? */
    5136:   PetscObjectBaseTypeCompare((PetscObject)matis->A,MATSEQAIJ,&isseqaij);
    5137:   if (isseqaij) {
    5138:     MatDestroy(&pcbddc->local_mat);
    5139:     MatPtAP(matis->A,new_mat,MAT_INITIAL_MATRIX,2.0,&pcbddc->local_mat);
    5140:     if (lA) {
    5141:       Mat work;
    5142:       MatPtAP(lA,new_mat,MAT_INITIAL_MATRIX,2.0,&work);
    5143:       PetscObjectCompose((PetscObject)pc,"__KSPFETIDP_lA" ,(PetscObject)work);
    5144:       MatDestroy(&work);
    5145:     }
    5146:   } else {
    5147:     Mat work_mat;

    5149:     MatDestroy(&pcbddc->local_mat);
    5150:     MatConvert(matis->A,MATSEQAIJ,MAT_INITIAL_MATRIX,&work_mat);
    5151:     MatPtAP(work_mat,new_mat,MAT_INITIAL_MATRIX,2.0,&pcbddc->local_mat);
    5152:     MatDestroy(&work_mat);
    5153:     if (lA) {
    5154:       Mat work;
    5155:       MatConvert(lA,MATSEQAIJ,MAT_INITIAL_MATRIX,&work_mat);
    5156:       MatPtAP(work_mat,new_mat,MAT_INITIAL_MATRIX,2.0,&work);
    5157:       PetscObjectCompose((PetscObject)pc,"__KSPFETIDP_lA" ,(PetscObject)work);
    5158:       MatDestroy(&work);
    5159:     }
    5160:   }
    5161:   if (matis->A->symmetric_set) {
    5162:     MatSetOption(pcbddc->local_mat,MAT_SYMMETRIC,matis->A->symmetric);
    5163: #if !defined(PETSC_USE_COMPLEX)
    5164:     MatSetOption(pcbddc->local_mat,MAT_HERMITIAN,matis->A->symmetric);
    5165: #endif
    5166:   }
    5167:   MatDestroy(&new_mat);
    5168:   return(0);
    5169: }

    5171: PetscErrorCode PCBDDCSetUpLocalScatters(PC pc)
    5172: {
    5173:   PC_IS*          pcis = (PC_IS*)(pc->data);
    5174:   PC_BDDC*        pcbddc = (PC_BDDC*)pc->data;
    5175:   PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs;
    5176:   PetscInt        *idx_R_local=NULL;
    5177:   PetscInt        n_vertices,i,j,n_R,n_D,n_B;
    5178:   PetscInt        vbs,bs;
    5179:   PetscBT         bitmask=NULL;
    5180:   PetscErrorCode  ierr;

    5183:   /*
    5184:     No need to setup local scatters if
    5185:       - primal space is unchanged
    5186:         AND
    5187:       - we actually have locally some primal dofs (could not be true in multilevel or for isolated subdomains)
    5188:         AND
    5189:       - we are not in debugging mode (this is needed since there are Synchronized prints at the end of the subroutine
    5190:   */
    5191:   if (!pcbddc->new_primal_space_local && pcbddc->local_primal_size && !pcbddc->dbg_flag) {
    5192:     return(0);
    5193:   }
    5194:   /* destroy old objects */
    5195:   ISDestroy(&pcbddc->is_R_local);
    5196:   VecScatterDestroy(&pcbddc->R_to_B);
    5197:   VecScatterDestroy(&pcbddc->R_to_D);
    5198:   /* Set Non-overlapping dimensions */
    5199:   n_B = pcis->n_B;
    5200:   n_D = pcis->n - n_B;
    5201:   n_vertices = pcbddc->n_vertices;

    5203:   /* Dohrmann's notation: dofs splitted in R (Remaining: all dofs but the vertices) and V (Vertices) */

    5205:   /* create auxiliary bitmask and allocate workspace */
    5206:   if (!sub_schurs || !sub_schurs->reuse_solver) {
    5207:     PetscMalloc1(pcis->n-n_vertices,&idx_R_local);
    5208:     PetscBTCreate(pcis->n,&bitmask);
    5209:     for (i=0;i<n_vertices;i++) {
    5210:       PetscBTSet(bitmask,pcbddc->local_primal_ref_node[i]);
    5211:     }

    5213:     for (i=0, n_R=0; i<pcis->n; i++) {
    5214:       if (!PetscBTLookup(bitmask,i)) {
    5215:         idx_R_local[n_R++] = i;
    5216:       }
    5217:     }
    5218:   } else { /* A different ordering (already computed) is present if we are reusing the Schur solver */
    5219:     PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;

    5221:     ISGetIndices(reuse_solver->is_R,(const PetscInt**)&idx_R_local);
    5222:     ISGetLocalSize(reuse_solver->is_R,&n_R);
    5223:   }

    5225:   /* Block code */
    5226:   vbs = 1;
    5227:   MatGetBlockSize(pcbddc->local_mat,&bs);
    5228:   if (bs>1 && !(n_vertices%bs)) {
    5229:     PetscBool is_blocked = PETSC_TRUE;
    5230:     PetscInt  *vary;
    5231:     if (!sub_schurs || !sub_schurs->reuse_solver) {
    5232:       PetscMalloc1(pcis->n/bs,&vary);
    5233:       PetscArrayzero(vary,pcis->n/bs);
    5234:       /* Verify that the vertex indices correspond to each element in a block (code taken from sbaij2.c) */
    5235:       /* it is ok to check this way since local_primal_ref_node are always sorted by local numbering and idx_R_local is obtained as a complement */
    5236:       for (i=0; i<n_vertices; i++) vary[pcbddc->local_primal_ref_node[i]/bs]++;
    5237:       for (i=0; i<pcis->n/bs; i++) {
    5238:         if (vary[i]!=0 && vary[i]!=bs) {
    5239:           is_blocked = PETSC_FALSE;
    5240:           break;
    5241:         }
    5242:       }
    5243:       PetscFree(vary);
    5244:     } else {
    5245:       /* Verify directly the R set */
    5246:       for (i=0; i<n_R/bs; i++) {
    5247:         PetscInt j,node=idx_R_local[bs*i];
    5248:         for (j=1; j<bs; j++) {
    5249:           if (node != idx_R_local[bs*i+j]-j) {
    5250:             is_blocked = PETSC_FALSE;
    5251:             break;
    5252:           }
    5253:         }
    5254:       }
    5255:     }
    5256:     if (is_blocked) { /* build compressed IS for R nodes (complement of vertices) */
    5257:       vbs = bs;
    5258:       for (i=0;i<n_R/vbs;i++) {
    5259:         idx_R_local[i] = idx_R_local[vbs*i]/vbs;
    5260:       }
    5261:     }
    5262:   }
    5263:   ISCreateBlock(PETSC_COMM_SELF,vbs,n_R/vbs,idx_R_local,PETSC_COPY_VALUES,&pcbddc->is_R_local);
    5264:   if (sub_schurs && sub_schurs->reuse_solver) {
    5265:     PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;

    5267:     ISRestoreIndices(reuse_solver->is_R,(const PetscInt**)&idx_R_local);
    5268:     ISDestroy(&reuse_solver->is_R);
    5269:     PetscObjectReference((PetscObject)pcbddc->is_R_local);
    5270:     reuse_solver->is_R = pcbddc->is_R_local;
    5271:   } else {
    5272:     PetscFree(idx_R_local);
    5273:   }

    5275:   /* print some info if requested */
    5276:   if (pcbddc->dbg_flag) {
    5277:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"--------------------------------------------------\n");
    5278:     PetscViewerFlush(pcbddc->dbg_viewer);
    5279:     PetscViewerASCIIPushSynchronized(pcbddc->dbg_viewer);
    5280:     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d local dimensions\n",PetscGlobalRank);
    5281:     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"local_size = %D, dirichlet_size = %D, boundary_size = %D\n",pcis->n,n_D,n_B);
    5282:     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"r_size = %D, v_size = %D, constraints = %D, local_primal_size = %D\n",n_R,n_vertices,pcbddc->local_primal_size-n_vertices-pcbddc->benign_n,pcbddc->local_primal_size);
    5283:     PetscViewerFlush(pcbddc->dbg_viewer);
    5284:   }

    5286:   /* VecScatters pcbddc->R_to_B and (optionally) pcbddc->R_to_D */
    5287:   if (!sub_schurs || !sub_schurs->reuse_solver) {
    5288:     IS       is_aux1,is_aux2;
    5289:     PetscInt *aux_array1,*aux_array2,*is_indices,*idx_R_local;

    5291:     ISGetIndices(pcbddc->is_R_local,(const PetscInt**)&idx_R_local);
    5292:     PetscMalloc1(pcis->n_B-n_vertices,&aux_array1);
    5293:     PetscMalloc1(pcis->n_B-n_vertices,&aux_array2);
    5294:     ISGetIndices(pcis->is_I_local,(const PetscInt**)&is_indices);
    5295:     for (i=0; i<n_D; i++) {
    5296:       PetscBTSet(bitmask,is_indices[i]);
    5297:     }
    5298:     ISRestoreIndices(pcis->is_I_local,(const PetscInt**)&is_indices);
    5299:     for (i=0, j=0; i<n_R; i++) {
    5300:       if (!PetscBTLookup(bitmask,idx_R_local[i])) {
    5301:         aux_array1[j++] = i;
    5302:       }
    5303:     }
    5304:     ISCreateGeneral(PETSC_COMM_SELF,j,aux_array1,PETSC_OWN_POINTER,&is_aux1);
    5305:     ISGetIndices(pcis->is_B_local,(const PetscInt**)&is_indices);
    5306:     for (i=0, j=0; i<n_B; i++) {
    5307:       if (!PetscBTLookup(bitmask,is_indices[i])) {
    5308:         aux_array2[j++] = i;
    5309:       }
    5310:     }
    5311:     ISRestoreIndices(pcis->is_B_local,(const PetscInt**)&is_indices);
    5312:     ISCreateGeneral(PETSC_COMM_SELF,j,aux_array2,PETSC_OWN_POINTER,&is_aux2);
    5313:     VecScatterCreate(pcbddc->vec1_R,is_aux1,pcis->vec1_B,is_aux2,&pcbddc->R_to_B);
    5314:     ISDestroy(&is_aux1);
    5315:     ISDestroy(&is_aux2);

    5317:     if (pcbddc->switch_static || pcbddc->dbg_flag) {
    5318:       PetscMalloc1(n_D,&aux_array1);
    5319:       for (i=0, j=0; i<n_R; i++) {
    5320:         if (PetscBTLookup(bitmask,idx_R_local[i])) {
    5321:           aux_array1[j++] = i;
    5322:         }
    5323:       }
    5324:       ISCreateGeneral(PETSC_COMM_SELF,j,aux_array1,PETSC_OWN_POINTER,&is_aux1);
    5325:       VecScatterCreate(pcbddc->vec1_R,is_aux1,pcis->vec1_D,(IS)0,&pcbddc->R_to_D);
    5326:       ISDestroy(&is_aux1);
    5327:     }
    5328:     PetscBTDestroy(&bitmask);
    5329:     ISRestoreIndices(pcbddc->is_R_local,(const PetscInt**)&idx_R_local);
    5330:   } else {
    5331:     PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;
    5332:     IS                 tis;
    5333:     PetscInt           schur_size;

    5335:     ISGetLocalSize(reuse_solver->is_B,&schur_size);
    5336:     ISCreateStride(PETSC_COMM_SELF,schur_size,n_D,1,&tis);
    5337:     VecScatterCreate(pcbddc->vec1_R,tis,pcis->vec1_B,reuse_solver->is_B,&pcbddc->R_to_B);
    5338:     ISDestroy(&tis);
    5339:     if (pcbddc->switch_static || pcbddc->dbg_flag) {
    5340:       ISCreateStride(PETSC_COMM_SELF,n_D,0,1,&tis);
    5341:       VecScatterCreate(pcbddc->vec1_R,tis,pcis->vec1_D,(IS)0,&pcbddc->R_to_D);
    5342:       ISDestroy(&tis);
    5343:     }
    5344:   }
    5345:   return(0);
    5346: }

    5348: static PetscErrorCode MatNullSpacePropagateAny_Private(Mat A, IS is, Mat B)
    5349: {
    5350:   MatNullSpace   NullSpace;
    5351:   Mat            dmat;
    5352:   const Vec      *nullvecs;
    5353:   Vec            v,v2,*nullvecs2;
    5354:   VecScatter     sct = NULL;
    5355:   PetscContainer c;
    5356:   PetscScalar    *ddata;
    5357:   PetscInt       k,nnsp_size,bsiz,bsiz2,n,N,bs;
    5358:   PetscBool      nnsp_has_cnst;

    5362:   if (!is && !B) { /* MATIS */
    5363:     Mat_IS* matis = (Mat_IS*)A->data;

    5365:     if (!B) {
    5366:       MatISGetLocalMat(A,&B);
    5367:     }
    5368:     sct  = matis->cctx;
    5369:     PetscObjectReference((PetscObject)sct);
    5370:   } else {
    5371:     MatGetNullSpace(B,&NullSpace);
    5372:     if (!NullSpace) {
    5373:       MatGetNearNullSpace(B,&NullSpace);
    5374:     }
    5375:     if (NullSpace) return(0);
    5376:   }
    5377:   MatGetNullSpace(A,&NullSpace);
    5378:   if (!NullSpace) {
    5379:     MatGetNearNullSpace(A,&NullSpace);
    5380:   }
    5381:   if (!NullSpace) return(0);

    5383:   MatCreateVecs(A,&v,NULL);
    5384:   MatCreateVecs(B,&v2,NULL);
    5385:   if (!sct) {
    5386:     VecScatterCreate(v,is,v2,NULL,&sct);
    5387:   }
    5388:   MatNullSpaceGetVecs(NullSpace,&nnsp_has_cnst,&nnsp_size,(const Vec**)&nullvecs);
    5389:   bsiz = bsiz2 = nnsp_size+!!nnsp_has_cnst;
    5390:   PetscMalloc1(bsiz,&nullvecs2);
    5391:   VecGetBlockSize(v2,&bs);
    5392:   VecGetSize(v2,&N);
    5393:   VecGetLocalSize(v2,&n);
    5394:   PetscMalloc1(n*bsiz,&ddata);
    5395:   for (k=0;k<nnsp_size;k++) {
    5396:     VecCreateMPIWithArray(PetscObjectComm((PetscObject)B),bs,n,N,ddata + n*k,&nullvecs2[k]);
    5397:     VecScatterBegin(sct,nullvecs[k],nullvecs2[k],INSERT_VALUES,SCATTER_FORWARD);
    5398:     VecScatterEnd(sct,nullvecs[k],nullvecs2[k],INSERT_VALUES,SCATTER_FORWARD);
    5399:   }
    5400:   if (nnsp_has_cnst) {
    5401:     VecCreateMPIWithArray(PetscObjectComm((PetscObject)B),bs,n,N,ddata + n*nnsp_size,&nullvecs2[nnsp_size]);
    5402:     VecSet(nullvecs2[nnsp_size],1.0);
    5403:   }
    5404:   PCBDDCOrthonormalizeVecs(&bsiz2,nullvecs2);
    5405:   MatNullSpaceCreate(PetscObjectComm((PetscObject)B),PETSC_FALSE,bsiz2,nullvecs2,&NullSpace);

    5407:   MatCreateDense(PetscObjectComm((PetscObject)B),n,PETSC_DECIDE,N,bsiz2,ddata,&dmat);
    5408:   PetscContainerCreate(PetscObjectComm((PetscObject)B),&c);
    5409:   PetscContainerSetPointer(c,ddata);
    5410:   PetscContainerSetUserDestroy(c,PetscContainerUserDestroyDefault);
    5411:   PetscObjectCompose((PetscObject)dmat,"_PBDDC_Null_dmat_arr",(PetscObject)c);
    5412:   PetscContainerDestroy(&c);
    5413:   PetscObjectCompose((PetscObject)NullSpace,"_PBDDC_Null_dmat",(PetscObject)dmat);
    5414:   MatDestroy(&dmat);

    5416:   for (k=0;k<bsiz;k++) {
    5417:     VecDestroy(&nullvecs2[k]);
    5418:   }
    5419:   PetscFree(nullvecs2);
    5420:   MatSetNearNullSpace(B,NullSpace);
    5421:   MatNullSpaceDestroy(&NullSpace);
    5422:   VecDestroy(&v);
    5423:   VecDestroy(&v2);
    5424:   VecScatterDestroy(&sct);
    5425:   return(0);
    5426: }

    5428: PetscErrorCode PCBDDCSetUpLocalSolvers(PC pc, PetscBool dirichlet, PetscBool neumann)
    5429: {
    5430:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    5431:   PC_IS          *pcis = (PC_IS*)pc->data;
    5432:   PC             pc_temp;
    5433:   Mat            A_RR;
    5434:   MatNullSpace   nnsp;
    5435:   MatReuse       reuse;
    5436:   PetscScalar    m_one = -1.0;
    5437:   PetscReal      value;
    5438:   PetscInt       n_D,n_R;
    5439:   PetscBool      issbaij,opts;
    5441:   void           (*f)(void) = NULL;
    5442:   char           dir_prefix[256],neu_prefix[256],str_level[16];
    5443:   size_t         len;

    5446:   PetscLogEventBegin(PC_BDDC_LocalSolvers[pcbddc->current_level],pc,0,0,0);
    5447:   /* approximate solver, propagate NearNullSpace if needed */
    5448:   if (!pc->setupcalled && (pcbddc->NullSpace_corr[0] || pcbddc->NullSpace_corr[2])) {
    5449:     MatNullSpace gnnsp1,gnnsp2;
    5450:     PetscBool    lhas,ghas;

    5452:     MatGetNearNullSpace(pcbddc->local_mat,&nnsp);
    5453:     MatGetNearNullSpace(pc->pmat,&gnnsp1);
    5454:     MatGetNullSpace(pc->pmat,&gnnsp2);
    5455:     lhas = nnsp ? PETSC_TRUE : PETSC_FALSE;
    5456:     MPIU_Allreduce(&lhas,&ghas,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)pc));
    5457:     if (!ghas && (gnnsp1 || gnnsp2)) {
    5458:       MatNullSpacePropagateAny_Private(pc->pmat,NULL,NULL);
    5459:     }
    5460:   }

    5462:   /* compute prefixes */
    5463:   PetscStrcpy(dir_prefix,"");
    5464:   PetscStrcpy(neu_prefix,"");
    5465:   if (!pcbddc->current_level) {
    5466:     PetscStrncpy(dir_prefix,((PetscObject)pc)->prefix,sizeof(dir_prefix));
    5467:     PetscStrncpy(neu_prefix,((PetscObject)pc)->prefix,sizeof(neu_prefix));
    5468:     PetscStrlcat(dir_prefix,"pc_bddc_dirichlet_",sizeof(dir_prefix));
    5469:     PetscStrlcat(neu_prefix,"pc_bddc_neumann_",sizeof(neu_prefix));
    5470:   } else {
    5471:     PetscSNPrintf(str_level,sizeof(str_level),"l%d_",(int)(pcbddc->current_level));
    5472:     PetscStrlen(((PetscObject)pc)->prefix,&len);
    5473:     len -= 15; /* remove "pc_bddc_coarse_" */
    5474:     if (pcbddc->current_level>1) len -= 3; /* remove "lX_" with X level number */
    5475:     if (pcbddc->current_level>10) len -= 1; /* remove another char from level number */
    5476:     /* Nonstandard use of PetscStrncpy() to only copy a portion of the input string */
    5477:     PetscStrncpy(dir_prefix,((PetscObject)pc)->prefix,len+1);
    5478:     PetscStrncpy(neu_prefix,((PetscObject)pc)->prefix,len+1);
    5479:     PetscStrlcat(dir_prefix,"pc_bddc_dirichlet_",sizeof(dir_prefix));
    5480:     PetscStrlcat(neu_prefix,"pc_bddc_neumann_",sizeof(neu_prefix));
    5481:     PetscStrlcat(dir_prefix,str_level,sizeof(dir_prefix));
    5482:     PetscStrlcat(neu_prefix,str_level,sizeof(neu_prefix));
    5483:   }

    5485:   /* DIRICHLET PROBLEM */
    5486:   if (dirichlet) {
    5487:     PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs;
    5488:     if (pcbddc->benign_n && !pcbddc->benign_change_explicit) {
    5489:       if (!sub_schurs || !sub_schurs->reuse_solver) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet implemented");
    5490:       if (pcbddc->dbg_flag) {
    5491:         Mat    A_IIn;

    5493:         PCBDDCBenignProject(pc,pcis->is_I_local,pcis->is_I_local,&A_IIn);
    5494:         MatDestroy(&pcis->A_II);
    5495:         pcis->A_II = A_IIn;
    5496:       }
    5497:     }
    5498:     if (pcbddc->local_mat->symmetric_set) {
    5499:       MatSetOption(pcis->A_II,MAT_SYMMETRIC,pcbddc->local_mat->symmetric);
    5500:     }
    5501:     /* Matrix for Dirichlet problem is pcis->A_II */
    5502:     n_D  = pcis->n - pcis->n_B;
    5503:     opts = PETSC_FALSE;
    5504:     if (!pcbddc->ksp_D) { /* create object if not yet build */
    5505:       opts = PETSC_TRUE;
    5506:       KSPCreate(PETSC_COMM_SELF,&pcbddc->ksp_D);
    5507:       PetscObjectIncrementTabLevel((PetscObject)pcbddc->ksp_D,(PetscObject)pc,1);
    5508:       /* default */
    5509:       KSPSetType(pcbddc->ksp_D,KSPPREONLY);
    5510:       KSPSetOptionsPrefix(pcbddc->ksp_D,dir_prefix);
    5511:       PetscObjectTypeCompare((PetscObject)pcis->pA_II,MATSEQSBAIJ,&issbaij);
    5512:       KSPGetPC(pcbddc->ksp_D,&pc_temp);
    5513:       if (issbaij) {
    5514:         PCSetType(pc_temp,PCCHOLESKY);
    5515:       } else {
    5516:         PCSetType(pc_temp,PCLU);
    5517:       }
    5518:       KSPSetErrorIfNotConverged(pcbddc->ksp_D,pc->erroriffailure);
    5519:     }
    5520:     MatSetOptionsPrefix(pcis->pA_II,((PetscObject)pcbddc->ksp_D)->prefix);
    5521:     KSPSetOperators(pcbddc->ksp_D,pcis->A_II,pcis->pA_II);
    5522:     /* Allow user's customization */
    5523:     if (opts) {
    5524:       KSPSetFromOptions(pcbddc->ksp_D);
    5525:     }
    5526:     MatGetNearNullSpace(pcis->pA_II,&nnsp);
    5527:     if (pcbddc->NullSpace_corr[0] && !nnsp) { /* approximate solver, propagate NearNullSpace */
    5528:       MatNullSpacePropagateAny_Private(pcbddc->local_mat,pcis->is_I_local,pcis->pA_II);
    5529:     }
    5530:     MatGetNearNullSpace(pcis->pA_II,&nnsp);
    5531:     KSPGetPC(pcbddc->ksp_D,&pc_temp);
    5532:     PetscObjectQueryFunction((PetscObject)pc_temp,"PCSetCoordinates_C",&f);
    5533:     if (f && pcbddc->mat_graph->cloc && !nnsp) {
    5534:       PetscReal      *coords = pcbddc->mat_graph->coords,*scoords;
    5535:       const PetscInt *idxs;
    5536:       PetscInt       cdim = pcbddc->mat_graph->cdim,nl,i,d;

    5538:       ISGetLocalSize(pcis->is_I_local,&nl);
    5539:       ISGetIndices(pcis->is_I_local,&idxs);
    5540:       PetscMalloc1(nl*cdim,&scoords);
    5541:       for (i=0;i<nl;i++) {
    5542:         for (d=0;d<cdim;d++) {
    5543:           scoords[i*cdim+d] = coords[idxs[i]*cdim+d];
    5544:         }
    5545:       }
    5546:       ISRestoreIndices(pcis->is_I_local,&idxs);
    5547:       PCSetCoordinates(pc_temp,cdim,nl,scoords);
    5548:       PetscFree(scoords);
    5549:     }
    5550:     if (sub_schurs && sub_schurs->reuse_solver) {
    5551:       PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;

    5553:       KSPSetPC(pcbddc->ksp_D,reuse_solver->interior_solver);
    5554:     }

    5556:     /* umfpack interface has a bug when matrix dimension is zero. TODO solve from umfpack interface */
    5557:     if (!n_D) {
    5558:       KSPGetPC(pcbddc->ksp_D,&pc_temp);
    5559:       PCSetType(pc_temp,PCNONE);
    5560:     }
    5561:     KSPSetUp(pcbddc->ksp_D);
    5562:     /* set ksp_D into pcis data */
    5563:     PetscObjectReference((PetscObject)pcbddc->ksp_D);
    5564:     KSPDestroy(&pcis->ksp_D);
    5565:     pcis->ksp_D = pcbddc->ksp_D;
    5566:   }

    5568:   /* NEUMANN PROBLEM */
    5569:   A_RR = NULL;
    5570:   if (neumann) {
    5571:     PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs;
    5572:     PetscInt        ibs,mbs;
    5573:     PetscBool       issbaij, reuse_neumann_solver;
    5574:     Mat_IS*         matis = (Mat_IS*)pc->pmat->data;

    5576:     reuse_neumann_solver = PETSC_FALSE;
    5577:     if (sub_schurs && sub_schurs->reuse_solver) {
    5578:       IS iP;

    5580:       reuse_neumann_solver = PETSC_TRUE;
    5581:       PetscObjectQuery((PetscObject)sub_schurs->A,"__KSPFETIDP_iP",(PetscObject*)&iP);
    5582:       if (iP) reuse_neumann_solver = PETSC_FALSE;
    5583:     }
    5584:     /* Matrix for Neumann problem is A_RR -> we need to create/reuse it at this point */
    5585:     ISGetSize(pcbddc->is_R_local,&n_R);
    5586:     if (pcbddc->ksp_R) { /* already created ksp */
    5587:       PetscInt nn_R;
    5588:       KSPGetOperators(pcbddc->ksp_R,NULL,&A_RR);
    5589:       PetscObjectReference((PetscObject)A_RR);
    5590:       MatGetSize(A_RR,&nn_R,NULL);
    5591:       if (nn_R != n_R) { /* old ksp is not reusable, so reset it */
    5592:         KSPReset(pcbddc->ksp_R);
    5593:         MatDestroy(&A_RR);
    5594:         reuse = MAT_INITIAL_MATRIX;
    5595:       } else { /* same sizes, but nonzero pattern depend on primal vertices so it can be changed */
    5596:         if (pcbddc->new_primal_space_local) { /* we are not sure the matrix will have the same nonzero pattern */
    5597:           MatDestroy(&A_RR);
    5598:           reuse = MAT_INITIAL_MATRIX;
    5599:         } else { /* safe to reuse the matrix */
    5600:           reuse = MAT_REUSE_MATRIX;
    5601:         }
    5602:       }
    5603:       /* last check */
    5604:       if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
    5605:         MatDestroy(&A_RR);
    5606:         reuse = MAT_INITIAL_MATRIX;
    5607:       }
    5608:     } else { /* first time, so we need to create the matrix */
    5609:       reuse = MAT_INITIAL_MATRIX;
    5610:     }
    5611:     /* convert pcbddc->local_mat if needed later in PCBDDCSetUpCorrection
    5612:        TODO: Get Rid of these conversions */
    5613:     MatGetBlockSize(pcbddc->local_mat,&mbs);
    5614:     ISGetBlockSize(pcbddc->is_R_local,&ibs);
    5615:     PetscObjectTypeCompare((PetscObject)pcbddc->local_mat,MATSEQSBAIJ,&issbaij);
    5616:     if (ibs != mbs) { /* need to convert to SEQAIJ to extract any submatrix with is_R_local */
    5617:       if (matis->A == pcbddc->local_mat) {
    5618:         MatDestroy(&pcbddc->local_mat);
    5619:         MatConvert(matis->A,MATSEQAIJ,MAT_INITIAL_MATRIX,&pcbddc->local_mat);
    5620:       } else {
    5621:         MatConvert(pcbddc->local_mat,MATSEQAIJ,MAT_INPLACE_MATRIX,&pcbddc->local_mat);
    5622:       }
    5623:     } else if (issbaij) { /* need to convert to BAIJ to get offdiagonal blocks */
    5624:       if (matis->A == pcbddc->local_mat) {
    5625:         MatDestroy(&pcbddc->local_mat);
    5626:         MatConvert(matis->A,mbs > 1 ? MATSEQBAIJ : MATSEQAIJ,MAT_INITIAL_MATRIX,&pcbddc->local_mat);
    5627:       } else {
    5628:         MatConvert(pcbddc->local_mat,mbs > 1 ? MATSEQBAIJ : MATSEQAIJ,MAT_INPLACE_MATRIX,&pcbddc->local_mat);
    5629:       }
    5630:     }
    5631:     /* extract A_RR */
    5632:     if (reuse_neumann_solver) {
    5633:       PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;

    5635:       if (pcbddc->dbg_flag) { /* we need A_RR to test the solver later */
    5636:         MatDestroy(&A_RR);
    5637:         if (reuse_solver->benign_n) { /* we are not using the explicit change of basis on the pressures */
    5638:           PCBDDCBenignProject(pc,pcbddc->is_R_local,pcbddc->is_R_local,&A_RR);
    5639:         } else {
    5640:           MatCreateSubMatrix(pcbddc->local_mat,pcbddc->is_R_local,pcbddc->is_R_local,MAT_INITIAL_MATRIX,&A_RR);
    5641:         }
    5642:       } else {
    5643:         MatDestroy(&A_RR);
    5644:         PCGetOperators(reuse_solver->correction_solver,&A_RR,NULL);
    5645:         PetscObjectReference((PetscObject)A_RR);
    5646:       }
    5647:     } else { /* we have to build the neumann solver, so we need to extract the relevant matrix */
    5648:       MatCreateSubMatrix(pcbddc->local_mat,pcbddc->is_R_local,pcbddc->is_R_local,reuse,&A_RR);
    5649:     }
    5650:     if (pcbddc->local_mat->symmetric_set) {
    5651:       MatSetOption(A_RR,MAT_SYMMETRIC,pcbddc->local_mat->symmetric);
    5652:     }
    5653:     opts = PETSC_FALSE;
    5654:     if (!pcbddc->ksp_R) { /* create object if not present */
    5655:       opts = PETSC_TRUE;
    5656:       KSPCreate(PETSC_COMM_SELF,&pcbddc->ksp_R);
    5657:       PetscObjectIncrementTabLevel((PetscObject)pcbddc->ksp_R,(PetscObject)pc,1);
    5658:       /* default */
    5659:       KSPSetType(pcbddc->ksp_R,KSPPREONLY);
    5660:       KSPSetOptionsPrefix(pcbddc->ksp_R,neu_prefix);
    5661:       KSPGetPC(pcbddc->ksp_R,&pc_temp);
    5662:       PetscObjectTypeCompare((PetscObject)A_RR,MATSEQSBAIJ,&issbaij);
    5663:       if (issbaij) {
    5664:         PCSetType(pc_temp,PCCHOLESKY);
    5665:       } else {
    5666:         PCSetType(pc_temp,PCLU);
    5667:       }
    5668:       KSPSetErrorIfNotConverged(pcbddc->ksp_R,pc->erroriffailure);
    5669:     }
    5670:     KSPSetOperators(pcbddc->ksp_R,A_RR,A_RR);
    5671:     MatSetOptionsPrefix(A_RR,((PetscObject)pcbddc->ksp_R)->prefix);
    5672:     if (opts) { /* Allow user's customization once */
    5673:       KSPSetFromOptions(pcbddc->ksp_R);
    5674:     }
    5675:     MatGetNearNullSpace(A_RR,&nnsp);
    5676:     if (pcbddc->NullSpace_corr[2] && !nnsp) { /* approximate solver, propagate NearNullSpace */
    5677:       MatNullSpacePropagateAny_Private(pcbddc->local_mat,pcbddc->is_R_local,A_RR);
    5678:     }
    5679:     MatGetNearNullSpace(A_RR,&nnsp);
    5680:     KSPGetPC(pcbddc->ksp_R,&pc_temp);
    5681:     PetscObjectQueryFunction((PetscObject)pc_temp,"PCSetCoordinates_C",&f);
    5682:     if (f && pcbddc->mat_graph->cloc && !nnsp) {
    5683:       PetscReal      *coords = pcbddc->mat_graph->coords,*scoords;
    5684:       const PetscInt *idxs;
    5685:       PetscInt       cdim = pcbddc->mat_graph->cdim,nl,i,d;

    5687:       ISGetLocalSize(pcbddc->is_R_local,&nl);
    5688:       ISGetIndices(pcbddc->is_R_local,&idxs);
    5689:       PetscMalloc1(nl*cdim,&scoords);
    5690:       for (i=0;i<nl;i++) {
    5691:         for (d=0;d<cdim;d++) {
    5692:           scoords[i*cdim+d] = coords[idxs[i]*cdim+d];
    5693:         }
    5694:       }
    5695:       ISRestoreIndices(pcbddc->is_R_local,&idxs);
    5696:       PCSetCoordinates(pc_temp,cdim,nl,scoords);
    5697:       PetscFree(scoords);
    5698:     }

    5700:     /* umfpack interface has a bug when matrix dimension is zero. TODO solve from umfpack interface */
    5701:     if (!n_R) {
    5702:       KSPGetPC(pcbddc->ksp_R,&pc_temp);
    5703:       PCSetType(pc_temp,PCNONE);
    5704:     }
    5705:     /* Reuse solver if it is present */
    5706:     if (reuse_neumann_solver) {
    5707:       PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;

    5709:       KSPSetPC(pcbddc->ksp_R,reuse_solver->correction_solver);
    5710:     }
    5711:     KSPSetUp(pcbddc->ksp_R);
    5712:   }

    5714:   if (pcbddc->dbg_flag) {
    5715:     PetscViewerFlush(pcbddc->dbg_viewer);
    5716:     PetscViewerASCIIPushSynchronized(pcbddc->dbg_viewer);
    5717:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"--------------------------------------------------\n");
    5718:   }
    5719:   PetscLogEventEnd(PC_BDDC_LocalSolvers[pcbddc->current_level],pc,0,0,0);

    5721:   /* adapt Dirichlet and Neumann solvers if a nullspace correction has been requested */
    5722:   if (pcbddc->NullSpace_corr[0]) {
    5723:     PCBDDCSetUseExactDirichlet(pc,PETSC_FALSE);
    5724:   }
    5725:   if (dirichlet && pcbddc->NullSpace_corr[0] && !pcbddc->switch_static) {
    5726:     PCBDDCNullSpaceAssembleCorrection(pc,PETSC_TRUE,pcbddc->NullSpace_corr[1]);
    5727:   }
    5728:   if (neumann && pcbddc->NullSpace_corr[2]) {
    5729:     PCBDDCNullSpaceAssembleCorrection(pc,PETSC_FALSE,pcbddc->NullSpace_corr[3]);
    5730:   }
    5731:   /* check Dirichlet and Neumann solvers */
    5732:   if (pcbddc->dbg_flag) {
    5733:     if (dirichlet) { /* Dirichlet */
    5734:       VecSetRandom(pcis->vec1_D,NULL);
    5735:       MatMult(pcis->A_II,pcis->vec1_D,pcis->vec2_D);
    5736:       KSPSolve(pcbddc->ksp_D,pcis->vec2_D,pcis->vec2_D);
    5737:       KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec2_D);
    5738:       VecAXPY(pcis->vec1_D,m_one,pcis->vec2_D);
    5739:       VecNorm(pcis->vec1_D,NORM_INFINITY,&value);
    5740:       PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d infinity error for Dirichlet solve (%s) = % 1.14e \n",PetscGlobalRank,((PetscObject)(pcbddc->ksp_D))->prefix,value);
    5741:       PetscViewerFlush(pcbddc->dbg_viewer);
    5742:     }
    5743:     if (neumann) { /* Neumann */
    5744:       VecSetRandom(pcbddc->vec1_R,NULL);
    5745:       MatMult(A_RR,pcbddc->vec1_R,pcbddc->vec2_R);
    5746:       KSPSolve(pcbddc->ksp_R,pcbddc->vec2_R,pcbddc->vec2_R);
    5747:       KSPCheckSolve(pcbddc->ksp_R,pc,pcbddc->vec2_R);
    5748:       VecAXPY(pcbddc->vec1_R,m_one,pcbddc->vec2_R);
    5749:       VecNorm(pcbddc->vec1_R,NORM_INFINITY,&value);
    5750:       PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d infinity error for Neumann solve (%s) = % 1.14e\n",PetscGlobalRank,((PetscObject)(pcbddc->ksp_R))->prefix,value);
    5751:       PetscViewerFlush(pcbddc->dbg_viewer);
    5752:     }
    5753:   }
    5754:   /* free Neumann problem's matrix */
    5755:   MatDestroy(&A_RR);
    5756:   return(0);
    5757: }

    5759: static PetscErrorCode  PCBDDCSolveSubstructureCorrection(PC pc, Vec inout_B, Vec inout_D, PetscBool applytranspose)
    5760: {
    5761:   PetscErrorCode  ierr;
    5762:   PC_BDDC*        pcbddc = (PC_BDDC*)(pc->data);
    5763:   PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs;
    5764:   PetscBool       reuse_solver = sub_schurs ? ( sub_schurs->reuse_solver ? PETSC_TRUE : PETSC_FALSE) : PETSC_FALSE;

    5767:   if (!reuse_solver) {
    5768:     VecSet(pcbddc->vec1_R,0.);
    5769:   }
    5770:   if (!pcbddc->switch_static) {
    5771:     if (applytranspose && pcbddc->local_auxmat1) {
    5772:       MatMultTranspose(pcbddc->local_auxmat2,inout_B,pcbddc->vec1_C);
    5773:       MatMultTransposeAdd(pcbddc->local_auxmat1,pcbddc->vec1_C,inout_B,inout_B);
    5774:     }
    5775:     if (!reuse_solver) {
    5776:       VecScatterBegin(pcbddc->R_to_B,inout_B,pcbddc->vec1_R,INSERT_VALUES,SCATTER_REVERSE);
    5777:       VecScatterEnd(pcbddc->R_to_B,inout_B,pcbddc->vec1_R,INSERT_VALUES,SCATTER_REVERSE);
    5778:     } else {
    5779:       PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;

    5781:       VecScatterBegin(reuse_solver->correction_scatter_B,inout_B,reuse_solver->rhs_B,INSERT_VALUES,SCATTER_FORWARD);
    5782:       VecScatterEnd(reuse_solver->correction_scatter_B,inout_B,reuse_solver->rhs_B,INSERT_VALUES,SCATTER_FORWARD);
    5783:     }
    5784:   } else {
    5785:     VecScatterBegin(pcbddc->R_to_B,inout_B,pcbddc->vec1_R,INSERT_VALUES,SCATTER_REVERSE);
    5786:     VecScatterEnd(pcbddc->R_to_B,inout_B,pcbddc->vec1_R,INSERT_VALUES,SCATTER_REVERSE);
    5787:     VecScatterBegin(pcbddc->R_to_D,inout_D,pcbddc->vec1_R,INSERT_VALUES,SCATTER_REVERSE);
    5788:     VecScatterEnd(pcbddc->R_to_D,inout_D,pcbddc->vec1_R,INSERT_VALUES,SCATTER_REVERSE);
    5789:     if (applytranspose && pcbddc->local_auxmat1) {
    5790:       MatMultTranspose(pcbddc->local_auxmat2,pcbddc->vec1_R,pcbddc->vec1_C);
    5791:       MatMultTransposeAdd(pcbddc->local_auxmat1,pcbddc->vec1_C,inout_B,inout_B);
    5792:       VecScatterBegin(pcbddc->R_to_B,inout_B,pcbddc->vec1_R,INSERT_VALUES,SCATTER_REVERSE);
    5793:       VecScatterEnd(pcbddc->R_to_B,inout_B,pcbddc->vec1_R,INSERT_VALUES,SCATTER_REVERSE);
    5794:     }
    5795:   }
    5796:   PetscLogEventBegin(PC_BDDC_Solves[pcbddc->current_level][1],pc,0,0,0);
    5797:   if (!reuse_solver || pcbddc->switch_static) {
    5798:     if (applytranspose) {
    5799:       KSPSolveTranspose(pcbddc->ksp_R,pcbddc->vec1_R,pcbddc->vec1_R);
    5800:     } else {
    5801:       KSPSolve(pcbddc->ksp_R,pcbddc->vec1_R,pcbddc->vec1_R);
    5802:     }
    5803:     KSPCheckSolve(pcbddc->ksp_R,pc,pcbddc->vec1_R);
    5804:   } else {
    5805:     PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;

    5807:     if (applytranspose) {
    5808:       MatFactorSolveSchurComplementTranspose(reuse_solver->F,reuse_solver->rhs_B,reuse_solver->sol_B);
    5809:     } else {
    5810:       MatFactorSolveSchurComplement(reuse_solver->F,reuse_solver->rhs_B,reuse_solver->sol_B);
    5811:     }
    5812:   }
    5813:   PetscLogEventEnd(PC_BDDC_Solves[pcbddc->current_level][1],pc,0,0,0);
    5814:   VecSet(inout_B,0.);
    5815:   if (!pcbddc->switch_static) {
    5816:     if (!reuse_solver) {
    5817:       VecScatterBegin(pcbddc->R_to_B,pcbddc->vec1_R,inout_B,INSERT_VALUES,SCATTER_FORWARD);
    5818:       VecScatterEnd(pcbddc->R_to_B,pcbddc->vec1_R,inout_B,INSERT_VALUES,SCATTER_FORWARD);
    5819:     } else {
    5820:       PCBDDCReuseSolvers reuse_solver = sub_schurs->reuse_solver;

    5822:       VecScatterBegin(reuse_solver->correction_scatter_B,reuse_solver->sol_B,inout_B,INSERT_VALUES,SCATTER_REVERSE);
    5823:       VecScatterEnd(reuse_solver->correction_scatter_B,reuse_solver->sol_B,inout_B,INSERT_VALUES,SCATTER_REVERSE);
    5824:     }
    5825:     if (!applytranspose && pcbddc->local_auxmat1) {
    5826:       MatMult(pcbddc->local_auxmat1,inout_B,pcbddc->vec1_C);
    5827:       MatMultAdd(pcbddc->local_auxmat2,pcbddc->vec1_C,inout_B,inout_B);
    5828:     }
    5829:   } else {
    5830:     VecScatterBegin(pcbddc->R_to_B,pcbddc->vec1_R,inout_B,INSERT_VALUES,SCATTER_FORWARD);
    5831:     VecScatterEnd(pcbddc->R_to_B,pcbddc->vec1_R,inout_B,INSERT_VALUES,SCATTER_FORWARD);
    5832:     VecScatterBegin(pcbddc->R_to_D,pcbddc->vec1_R,inout_D,INSERT_VALUES,SCATTER_FORWARD);
    5833:     VecScatterEnd(pcbddc->R_to_D,pcbddc->vec1_R,inout_D,INSERT_VALUES,SCATTER_FORWARD);
    5834:     if (!applytranspose && pcbddc->local_auxmat1) {
    5835:       MatMult(pcbddc->local_auxmat1,inout_B,pcbddc->vec1_C);
    5836:       MatMultAdd(pcbddc->local_auxmat2,pcbddc->vec1_C,pcbddc->vec1_R,pcbddc->vec1_R);
    5837:     }
    5838:     VecScatterBegin(pcbddc->R_to_B,pcbddc->vec1_R,inout_B,INSERT_VALUES,SCATTER_FORWARD);
    5839:     VecScatterEnd(pcbddc->R_to_B,pcbddc->vec1_R,inout_B,INSERT_VALUES,SCATTER_FORWARD);
    5840:     VecScatterBegin(pcbddc->R_to_D,pcbddc->vec1_R,inout_D,INSERT_VALUES,SCATTER_FORWARD);
    5841:     VecScatterEnd(pcbddc->R_to_D,pcbddc->vec1_R,inout_D,INSERT_VALUES,SCATTER_FORWARD);
    5842:   }
    5843:   return(0);
    5844: }

    5846: /* parameter apply transpose determines if the interface preconditioner should be applied transposed or not */
    5847: PetscErrorCode  PCBDDCApplyInterfacePreconditioner(PC pc, PetscBool applytranspose)
    5848: {
    5850:   PC_BDDC*        pcbddc = (PC_BDDC*)(pc->data);
    5851:   PC_IS*            pcis = (PC_IS*)  (pc->data);
    5852:   const PetscScalar zero = 0.0;

    5855:   /* Application of PSI^T or PHI^T (depending on applytranspose, see comment above) */
    5856:   if (!pcbddc->benign_apply_coarse_only) {
    5857:     if (applytranspose) {
    5858:       MatMultTranspose(pcbddc->coarse_phi_B,pcis->vec1_B,pcbddc->vec1_P);
    5859:       if (pcbddc->switch_static) { MatMultTransposeAdd(pcbddc->coarse_phi_D,pcis->vec1_D,pcbddc->vec1_P,pcbddc->vec1_P); }
    5860:     } else {
    5861:       MatMultTranspose(pcbddc->coarse_psi_B,pcis->vec1_B,pcbddc->vec1_P);
    5862:       if (pcbddc->switch_static) { MatMultTransposeAdd(pcbddc->coarse_psi_D,pcis->vec1_D,pcbddc->vec1_P,pcbddc->vec1_P); }
    5863:     }
    5864:   } else {
    5865:     VecSet(pcbddc->vec1_P,zero);
    5866:   }

    5868:   /* add p0 to the last value of vec1_P holding the coarse dof relative to p0 */
    5869:   if (pcbddc->benign_n) {
    5870:     PetscScalar *array;
    5871:     PetscInt    j;

    5873:     VecGetArray(pcbddc->vec1_P,&array);
    5874:     for (j=0;j<pcbddc->benign_n;j++) array[pcbddc->local_primal_size-pcbddc->benign_n+j] += pcbddc->benign_p0[j];
    5875:     VecRestoreArray(pcbddc->vec1_P,&array);
    5876:   }

    5878:   /* start communications from local primal nodes to rhs of coarse solver */
    5879:   VecSet(pcbddc->coarse_vec,zero);
    5880:   PCBDDCScatterCoarseDataBegin(pc,ADD_VALUES,SCATTER_FORWARD);
    5881:   PCBDDCScatterCoarseDataEnd(pc,ADD_VALUES,SCATTER_FORWARD);

    5883:   /* Coarse solution -> rhs and sol updated inside PCBDDCScattarCoarseDataBegin/End */
    5884:   if (pcbddc->coarse_ksp) {
    5885:     Mat          coarse_mat;
    5886:     Vec          rhs,sol;
    5887:     MatNullSpace nullsp;
    5888:     PetscBool    isbddc = PETSC_FALSE;

    5890:     if (pcbddc->benign_have_null) {
    5891:       PC        coarse_pc;

    5893:       KSPGetPC(pcbddc->coarse_ksp,&coarse_pc);
    5894:       PetscObjectTypeCompare((PetscObject)coarse_pc,PCBDDC,&isbddc);
    5895:       /* we need to propagate to coarser levels the need for a possible benign correction */
    5896:       if (isbddc && pcbddc->benign_apply_coarse_only && !pcbddc->benign_skip_correction) {
    5897:         PC_BDDC* coarsepcbddc = (PC_BDDC*)(coarse_pc->data);
    5898:         coarsepcbddc->benign_skip_correction = PETSC_FALSE;
    5899:         coarsepcbddc->benign_apply_coarse_only = PETSC_TRUE;
    5900:       }
    5901:     }
    5902:     KSPGetRhs(pcbddc->coarse_ksp,&rhs);
    5903:     KSPGetSolution(pcbddc->coarse_ksp,&sol);
    5904:     KSPGetOperators(pcbddc->coarse_ksp,&coarse_mat,NULL);
    5905:     if (applytranspose) {
    5906:       if (pcbddc->benign_apply_coarse_only) SETERRQ(PetscObjectComm((PetscObject)pcbddc->coarse_ksp),PETSC_ERR_SUP,"Not yet implemented");
    5907:       PetscLogEventBegin(PC_BDDC_Solves[pcbddc->current_level][2],pc,0,0,0);
    5908:       KSPSolveTranspose(pcbddc->coarse_ksp,rhs,sol);
    5909:       PetscLogEventEnd(PC_BDDC_Solves[pcbddc->current_level][2],pc,0,0,0);
    5910:       KSPCheckSolve(pcbddc->coarse_ksp,pc,sol);
    5911:       MatGetTransposeNullSpace(coarse_mat,&nullsp);
    5912:       if (nullsp) {
    5913:         MatNullSpaceRemove(nullsp,sol);
    5914:       }
    5915:     } else {
    5916:       MatGetNullSpace(coarse_mat,&nullsp);
    5917:       if (pcbddc->benign_apply_coarse_only && isbddc) { /* need just to apply the coarse preconditioner during presolve */
    5918:         PC        coarse_pc;

    5920:         if (nullsp) {
    5921:           MatNullSpaceRemove(nullsp,rhs);
    5922:         }
    5923:         KSPGetPC(pcbddc->coarse_ksp,&coarse_pc);
    5924:         PCPreSolve(coarse_pc,pcbddc->coarse_ksp);
    5925:         PCBDDCBenignRemoveInterior(coarse_pc,rhs,sol);
    5926:         PCPostSolve(coarse_pc,pcbddc->coarse_ksp);
    5927:       } else {
    5928:         PetscLogEventBegin(PC_BDDC_Solves[pcbddc->current_level][2],pc,0,0,0);
    5929:         KSPSolve(pcbddc->coarse_ksp,rhs,sol);
    5930:         PetscLogEventEnd(PC_BDDC_Solves[pcbddc->current_level][2],pc,0,0,0);
    5931:         KSPCheckSolve(pcbddc->coarse_ksp,pc,sol);
    5932:         if (nullsp) {
    5933:           MatNullSpaceRemove(nullsp,sol);
    5934:         }
    5935:       }
    5936:     }
    5937:     /* we don't need the benign correction at coarser levels anymore */
    5938:     if (pcbddc->benign_have_null && isbddc) {
    5939:       PC        coarse_pc;
    5940:       PC_BDDC*  coarsepcbddc;

    5942:       KSPGetPC(pcbddc->coarse_ksp,&coarse_pc);
    5943:       coarsepcbddc = (PC_BDDC*)(coarse_pc->data);
    5944:       coarsepcbddc->benign_skip_correction = PETSC_TRUE;
    5945:       coarsepcbddc->benign_apply_coarse_only = PETSC_FALSE;
    5946:     }
    5947:   }

    5949:   /* Local solution on R nodes */
    5950:   if (pcis->n && !pcbddc->benign_apply_coarse_only) {
    5951:     PCBDDCSolveSubstructureCorrection(pc,pcis->vec1_B,pcis->vec1_D,applytranspose);
    5952:   }
    5953:   /* communications from coarse sol to local primal nodes */
    5954:   PCBDDCScatterCoarseDataBegin(pc,INSERT_VALUES,SCATTER_REVERSE);
    5955:   PCBDDCScatterCoarseDataEnd(pc,INSERT_VALUES,SCATTER_REVERSE);

    5957:   /* Sum contributions from the two levels */
    5958:   if (!pcbddc->benign_apply_coarse_only) {
    5959:     if (applytranspose) {
    5960:       MatMultAdd(pcbddc->coarse_psi_B,pcbddc->vec1_P,pcis->vec1_B,pcis->vec1_B);
    5961:       if (pcbddc->switch_static) { MatMultAdd(pcbddc->coarse_psi_D,pcbddc->vec1_P,pcis->vec1_D,pcis->vec1_D); }
    5962:     } else {
    5963:       MatMultAdd(pcbddc->coarse_phi_B,pcbddc->vec1_P,pcis->vec1_B,pcis->vec1_B);
    5964:       if (pcbddc->switch_static) { MatMultAdd(pcbddc->coarse_phi_D,pcbddc->vec1_P,pcis->vec1_D,pcis->vec1_D); }
    5965:     }
    5966:     /* store p0 */
    5967:     if (pcbddc->benign_n) {
    5968:       PetscScalar *array;
    5969:       PetscInt    j;

    5971:       VecGetArray(pcbddc->vec1_P,&array);
    5972:       for (j=0;j<pcbddc->benign_n;j++) pcbddc->benign_p0[j] = array[pcbddc->local_primal_size-pcbddc->benign_n+j];
    5973:       VecRestoreArray(pcbddc->vec1_P,&array);
    5974:     }
    5975:   } else { /* expand the coarse solution */
    5976:     if (applytranspose) {
    5977:       MatMult(pcbddc->coarse_psi_B,pcbddc->vec1_P,pcis->vec1_B);
    5978:     } else {
    5979:       MatMult(pcbddc->coarse_phi_B,pcbddc->vec1_P,pcis->vec1_B);
    5980:     }
    5981:   }
    5982:   return(0);
    5983: }

    5985: PetscErrorCode PCBDDCScatterCoarseDataBegin(PC pc,InsertMode imode, ScatterMode smode)
    5986: {
    5987:   PC_BDDC*          pcbddc = (PC_BDDC*)(pc->data);
    5988:   Vec               from,to;
    5989:   const PetscScalar *array;
    5990:   PetscErrorCode    ierr;

    5993:   if (smode == SCATTER_REVERSE) { /* from global to local -> get data from coarse solution */
    5994:     from = pcbddc->coarse_vec;
    5995:     to = pcbddc->vec1_P;
    5996:     if (pcbddc->coarse_ksp) { /* get array from coarse processes */
    5997:       Vec tvec;

    5999:       KSPGetRhs(pcbddc->coarse_ksp,&tvec);
    6000:       VecResetArray(tvec);
    6001:       KSPGetSolution(pcbddc->coarse_ksp,&tvec);
    6002:       VecGetArrayRead(tvec,&array);
    6003:       VecPlaceArray(from,array);
    6004:       VecRestoreArrayRead(tvec,&array);
    6005:     }
    6006:   } else { /* from local to global -> put data in coarse right hand side */
    6007:     from = pcbddc->vec1_P;
    6008:     to = pcbddc->coarse_vec;
    6009:   }
    6010:   VecScatterBegin(pcbddc->coarse_loc_to_glob,from,to,imode,smode);
    6011:   return(0);
    6012: }

    6014: PetscErrorCode PCBDDCScatterCoarseDataEnd(PC pc, InsertMode imode, ScatterMode smode)
    6015: {
    6016:   PC_BDDC*          pcbddc = (PC_BDDC*)(pc->data);
    6017:   Vec               from,to;
    6018:   const PetscScalar *array;
    6019:   PetscErrorCode    ierr;

    6022:   if (smode == SCATTER_REVERSE) { /* from global to local -> get data from coarse solution */
    6023:     from = pcbddc->coarse_vec;
    6024:     to = pcbddc->vec1_P;
    6025:   } else { /* from local to global -> put data in coarse right hand side */
    6026:     from = pcbddc->vec1_P;
    6027:     to = pcbddc->coarse_vec;
    6028:   }
    6029:   VecScatterEnd(pcbddc->coarse_loc_to_glob,from,to,imode,smode);
    6030:   if (smode == SCATTER_FORWARD) {
    6031:     if (pcbddc->coarse_ksp) { /* get array from coarse processes */
    6032:       Vec tvec;

    6034:       KSPGetRhs(pcbddc->coarse_ksp,&tvec);
    6035:       VecGetArrayRead(to,&array);
    6036:       VecPlaceArray(tvec,array);
    6037:       VecRestoreArrayRead(to,&array);
    6038:     }
    6039:   } else {
    6040:     if (pcbddc->coarse_ksp) { /* restore array of pcbddc->coarse_vec */
    6041:      VecResetArray(from);
    6042:     }
    6043:   }
    6044:   return(0);
    6045: }

    6047: PetscErrorCode PCBDDCConstraintsSetUp(PC pc)
    6048: {
    6049:   PetscErrorCode    ierr;
    6050:   PC_IS*            pcis = (PC_IS*)(pc->data);
    6051:   PC_BDDC*          pcbddc = (PC_BDDC*)pc->data;
    6052:   Mat_IS*           matis = (Mat_IS*)pc->pmat->data;
    6053:   /* one and zero */
    6054:   PetscScalar       one=1.0,zero=0.0;
    6055:   /* space to store constraints and their local indices */
    6056:   PetscScalar       *constraints_data;
    6057:   PetscInt          *constraints_idxs,*constraints_idxs_B;
    6058:   PetscInt          *constraints_idxs_ptr,*constraints_data_ptr;
    6059:   PetscInt          *constraints_n;
    6060:   /* iterators */
    6061:   PetscInt          i,j,k,total_counts,total_counts_cc,cum;
    6062:   /* BLAS integers */
    6063:   PetscBLASInt      lwork,lierr;
    6064:   PetscBLASInt      Blas_N,Blas_M,Blas_K,Blas_one=1;
    6065:   PetscBLASInt      Blas_LDA,Blas_LDB,Blas_LDC;
    6066:   /* reuse */
    6067:   PetscInt          olocal_primal_size,olocal_primal_size_cc;
    6068:   PetscInt          *olocal_primal_ref_node,*olocal_primal_ref_mult;
    6069:   /* change of basis */
    6070:   PetscBool         qr_needed;
    6071:   PetscBT           change_basis,qr_needed_idx;
    6072:   /* auxiliary stuff */
    6073:   PetscInt          *nnz,*is_indices;
    6074:   PetscInt          ncc;
    6075:   /* some quantities */
    6076:   PetscInt          n_vertices,total_primal_vertices,valid_constraints;
    6077:   PetscInt          size_of_constraint,max_size_of_constraint=0,max_constraints,temp_constraints;
    6078:   PetscReal         tol; /* tolerance for retaining eigenmodes */

    6081:   tol  = PetscSqrtReal(PETSC_SMALL);
    6082:   /* Destroy Mat objects computed previously */
    6083:   MatDestroy(&pcbddc->ChangeOfBasisMatrix);
    6084:   MatDestroy(&pcbddc->ConstraintMatrix);
    6085:   MatDestroy(&pcbddc->switch_static_change);
    6086:   /* save info on constraints from previous setup (if any) */
    6087:   olocal_primal_size = pcbddc->local_primal_size;
    6088:   olocal_primal_size_cc = pcbddc->local_primal_size_cc;
    6089:   PetscMalloc2(olocal_primal_size_cc,&olocal_primal_ref_node,olocal_primal_size_cc,&olocal_primal_ref_mult);
    6090:   PetscArraycpy(olocal_primal_ref_node,pcbddc->local_primal_ref_node,olocal_primal_size_cc);
    6091:   PetscArraycpy(olocal_primal_ref_mult,pcbddc->local_primal_ref_mult,olocal_primal_size_cc);
    6092:   PetscFree2(pcbddc->local_primal_ref_node,pcbddc->local_primal_ref_mult);
    6093:   PetscFree(pcbddc->primal_indices_local_idxs);

    6095:   if (!pcbddc->adaptive_selection) {
    6096:     IS           ISForVertices,*ISForFaces,*ISForEdges;
    6097:     MatNullSpace nearnullsp;
    6098:     const Vec    *nearnullvecs;
    6099:     Vec          *localnearnullsp;
    6100:     PetscScalar  *array;
    6101:     PetscInt     n_ISForFaces,n_ISForEdges,nnsp_size;
    6102:     PetscBool    nnsp_has_cnst;
    6103:     /* LAPACK working arrays for SVD or POD */
    6104:     PetscBool    skip_lapack,boolforchange;
    6105:     PetscScalar  *work;
    6106:     PetscReal    *singular_vals;
    6107: #if defined(PETSC_USE_COMPLEX)
    6108:     PetscReal    *rwork;
    6109: #endif
    6110:     PetscScalar  *temp_basis = NULL,*correlation_mat = NULL;
    6111:     PetscBLASInt dummy_int=1;
    6112:     PetscScalar  dummy_scalar=1.;
    6113:     PetscBool    use_pod = PETSC_FALSE;

    6115:     /* MKL SVD with same input gives different results on different processes! */
    6116: #if defined(PETSC_MISSING_LAPACK_GESVD) || defined(PETSC_HAVE_MKL)
    6117:     use_pod = PETSC_TRUE;
    6118: #endif
    6119:     /* Get index sets for faces, edges and vertices from graph */
    6120:     PCBDDCGraphGetCandidatesIS(pcbddc->mat_graph,&n_ISForFaces,&ISForFaces,&n_ISForEdges,&ISForEdges,&ISForVertices);
    6121:     /* print some info */
    6122:     if (pcbddc->dbg_flag && (!pcbddc->sub_schurs || pcbddc->sub_schurs_rebuild)) {
    6123:       PetscInt nv;

    6125:       PCBDDCGraphASCIIView(pcbddc->mat_graph,pcbddc->dbg_flag,pcbddc->dbg_viewer);
    6126:       ISGetSize(ISForVertices,&nv);
    6127:       PetscViewerASCIIPushSynchronized(pcbddc->dbg_viewer);
    6128:       PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"--------------------------------------------------------------\n");
    6129:       PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d got %02d local candidate vertices (%D)\n",PetscGlobalRank,nv,pcbddc->use_vertices);
    6130:       PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d got %02d local candidate edges    (%D)\n",PetscGlobalRank,n_ISForEdges,pcbddc->use_edges);
    6131:       PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d got %02d local candidate faces    (%D)\n",PetscGlobalRank,n_ISForFaces,pcbddc->use_faces);
    6132:       PetscViewerFlush(pcbddc->dbg_viewer);
    6133:       PetscViewerASCIIPopSynchronized(pcbddc->dbg_viewer);
    6134:     }

    6136:     /* free unneeded index sets */
    6137:     if (!pcbddc->use_vertices) {
    6138:       ISDestroy(&ISForVertices);
    6139:     }
    6140:     if (!pcbddc->use_edges) {
    6141:       for (i=0;i<n_ISForEdges;i++) {
    6142:         ISDestroy(&ISForEdges[i]);
    6143:       }
    6144:       PetscFree(ISForEdges);
    6145:       n_ISForEdges = 0;
    6146:     }
    6147:     if (!pcbddc->use_faces) {
    6148:       for (i=0;i<n_ISForFaces;i++) {
    6149:         ISDestroy(&ISForFaces[i]);
    6150:       }
    6151:       PetscFree(ISForFaces);
    6152:       n_ISForFaces = 0;
    6153:     }

    6155:     /* check if near null space is attached to global mat */
    6156:     if (pcbddc->use_nnsp) {
    6157:       MatGetNearNullSpace(pc->pmat,&nearnullsp);
    6158:     } else nearnullsp = NULL;

    6160:     if (nearnullsp) {
    6161:       MatNullSpaceGetVecs(nearnullsp,&nnsp_has_cnst,&nnsp_size,&nearnullvecs);
    6162:       /* remove any stored info */
    6163:       MatNullSpaceDestroy(&pcbddc->onearnullspace);
    6164:       PetscFree(pcbddc->onearnullvecs_state);
    6165:       /* store information for BDDC solver reuse */
    6166:       PetscObjectReference((PetscObject)nearnullsp);
    6167:       pcbddc->onearnullspace = nearnullsp;
    6168:       PetscMalloc1(nnsp_size,&pcbddc->onearnullvecs_state);
    6169:       for (i=0;i<nnsp_size;i++) {
    6170:         PetscObjectStateGet((PetscObject)nearnullvecs[i],&pcbddc->onearnullvecs_state[i]);
    6171:       }
    6172:     } else { /* if near null space is not provided BDDC uses constants by default */
    6173:       nnsp_size = 0;
    6174:       nnsp_has_cnst = PETSC_TRUE;
    6175:     }
    6176:     /* get max number of constraints on a single cc */
    6177:     max_constraints = nnsp_size;
    6178:     if (nnsp_has_cnst) max_constraints++;

    6180:     /*
    6181:          Evaluate maximum storage size needed by the procedure
    6182:          - Indices for connected component i stored at "constraints_idxs + constraints_idxs_ptr[i]"
    6183:          - Values for constraints on connected component i stored at "constraints_data + constraints_data_ptr[i]"
    6184:          There can be multiple constraints per connected component
    6185:                                                                                                                                                            */
    6186:     n_vertices = 0;
    6187:     if (ISForVertices) {
    6188:       ISGetSize(ISForVertices,&n_vertices);
    6189:     }
    6190:     ncc = n_vertices+n_ISForFaces+n_ISForEdges;
    6191:     PetscMalloc3(ncc+1,&constraints_idxs_ptr,ncc+1,&constraints_data_ptr,ncc,&constraints_n);

    6193:     total_counts = n_ISForFaces+n_ISForEdges;
    6194:     total_counts *= max_constraints;
    6195:     total_counts += n_vertices;
    6196:     PetscBTCreate(total_counts,&change_basis);

    6198:     total_counts = 0;
    6199:     max_size_of_constraint = 0;
    6200:     for (i=0;i<n_ISForEdges+n_ISForFaces;i++) {
    6201:       IS used_is;
    6202:       if (i<n_ISForEdges) {
    6203:         used_is = ISForEdges[i];
    6204:       } else {
    6205:         used_is = ISForFaces[i-n_ISForEdges];
    6206:       }
    6207:       ISGetSize(used_is,&j);
    6208:       total_counts += j;
    6209:       max_size_of_constraint = PetscMax(j,max_size_of_constraint);
    6210:     }
    6211:     PetscMalloc3(total_counts*max_constraints+n_vertices,&constraints_data,total_counts+n_vertices,&constraints_idxs,total_counts+n_vertices,&constraints_idxs_B);

    6213:     /* get local part of global near null space vectors */
    6214:     PetscMalloc1(nnsp_size,&localnearnullsp);
    6215:     for (k=0;k<nnsp_size;k++) {
    6216:       VecDuplicate(pcis->vec1_N,&localnearnullsp[k]);
    6217:       VecScatterBegin(matis->rctx,nearnullvecs[k],localnearnullsp[k],INSERT_VALUES,SCATTER_FORWARD);
    6218:       VecScatterEnd(matis->rctx,nearnullvecs[k],localnearnullsp[k],INSERT_VALUES,SCATTER_FORWARD);
    6219:     }

    6221:     /* whether or not to skip lapack calls */
    6222:     skip_lapack = PETSC_TRUE;
    6223:     if (n_ISForFaces+n_ISForEdges && max_constraints > 1 && !pcbddc->use_nnsp_true) skip_lapack = PETSC_FALSE;

    6225:     /* First we issue queries to allocate optimal workspace for LAPACKgesvd (or LAPACKsyev if SVD is missing) */
    6226:     if (!skip_lapack) {
    6227:       PetscScalar temp_work;

    6229:       if (use_pod) {
    6230:         /* Proper Orthogonal Decomposition (POD) using the snapshot method */
    6231:         PetscMalloc1(max_constraints*max_constraints,&correlation_mat);
    6232:         PetscMalloc1(max_constraints,&singular_vals);
    6233:         PetscMalloc1(max_size_of_constraint*max_constraints,&temp_basis);
    6234: #if defined(PETSC_USE_COMPLEX)
    6235:         PetscMalloc1(3*max_constraints,&rwork);
    6236: #endif
    6237:         /* now we evaluate the optimal workspace using query with lwork=-1 */
    6238:         PetscBLASIntCast(max_constraints,&Blas_N);
    6239:         PetscBLASIntCast(max_constraints,&Blas_LDA);
    6240:         lwork = -1;
    6241:         PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    6242: #if !defined(PETSC_USE_COMPLEX)
    6243:         PetscStackCallBLAS("LAPACKsyev",LAPACKsyev_("V","U",&Blas_N,correlation_mat,&Blas_LDA,singular_vals,&temp_work,&lwork,&lierr));
    6244: #else
    6245:         PetscStackCallBLAS("LAPACKsyev",LAPACKsyev_("V","U",&Blas_N,correlation_mat,&Blas_LDA,singular_vals,&temp_work,&lwork,rwork,&lierr));
    6246: #endif
    6247:         PetscFPTrapPop();
    6248:         if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in query to SYEV Lapack routine %d",(int)lierr);
    6249:       } else {
    6250: #if !defined(PETSC_MISSING_LAPACK_GESVD)
    6251:         /* SVD */
    6252:         PetscInt max_n,min_n;
    6253:         max_n = max_size_of_constraint;
    6254:         min_n = max_constraints;
    6255:         if (max_size_of_constraint < max_constraints) {
    6256:           min_n = max_size_of_constraint;
    6257:           max_n = max_constraints;
    6258:         }
    6259:         PetscMalloc1(min_n,&singular_vals);
    6260: #if defined(PETSC_USE_COMPLEX)
    6261:         PetscMalloc1(5*min_n,&rwork);
    6262: #endif
    6263:         /* now we evaluate the optimal workspace using query with lwork=-1 */
    6264:         lwork = -1;
    6265:         PetscBLASIntCast(max_n,&Blas_M);
    6266:         PetscBLASIntCast(min_n,&Blas_N);
    6267:         PetscBLASIntCast(max_n,&Blas_LDA);
    6268:         PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    6269: #if !defined(PETSC_USE_COMPLEX)
    6270:         PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("O","N",&Blas_M,&Blas_N,&constraints_data[0],&Blas_LDA,singular_vals,&dummy_scalar,&dummy_int,&dummy_scalar,&dummy_int,&temp_work,&lwork,&lierr));
    6271: #else
    6272:         PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("O","N",&Blas_M,&Blas_N,&constraints_data[0],&Blas_LDA,singular_vals,&dummy_scalar,&dummy_int,&dummy_scalar,&dummy_int,&temp_work,&lwork,rwork,&lierr));
    6273: #endif
    6274:         PetscFPTrapPop();
    6275:         if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in query to GESVD Lapack routine %d",(int)lierr);
    6276: #else
    6277:         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"This should not happen");
    6278: #endif /* on missing GESVD */
    6279:       }
    6280:       /* Allocate optimal workspace */
    6281:       PetscBLASIntCast((PetscInt)PetscRealPart(temp_work),&lwork);
    6282:       PetscMalloc1(lwork,&work);
    6283:     }
    6284:     /* Now we can loop on constraining sets */
    6285:     total_counts = 0;
    6286:     constraints_idxs_ptr[0] = 0;
    6287:     constraints_data_ptr[0] = 0;
    6288:     /* vertices */
    6289:     if (n_vertices) {
    6290:       ISGetIndices(ISForVertices,(const PetscInt**)&is_indices);
    6291:       PetscArraycpy(constraints_idxs,is_indices,n_vertices);
    6292:       for (i=0;i<n_vertices;i++) {
    6293:         constraints_n[total_counts] = 1;
    6294:         constraints_data[total_counts] = 1.0;
    6295:         constraints_idxs_ptr[total_counts+1] = constraints_idxs_ptr[total_counts]+1;
    6296:         constraints_data_ptr[total_counts+1] = constraints_data_ptr[total_counts]+1;
    6297:         total_counts++;
    6298:       }
    6299:       ISRestoreIndices(ISForVertices,(const PetscInt**)&is_indices);
    6300:       n_vertices = total_counts;
    6301:     }

    6303:     /* edges and faces */
    6304:     total_counts_cc = total_counts;
    6305:     for (ncc=0;ncc<n_ISForEdges+n_ISForFaces;ncc++) {
    6306:       IS        used_is;
    6307:       PetscBool idxs_copied = PETSC_FALSE;

    6309:       if (ncc<n_ISForEdges) {
    6310:         used_is = ISForEdges[ncc];
    6311:         boolforchange = pcbddc->use_change_of_basis; /* change or not the basis on the edge */
    6312:       } else {
    6313:         used_is = ISForFaces[ncc-n_ISForEdges];
    6314:         boolforchange = (PetscBool)(pcbddc->use_change_of_basis && pcbddc->use_change_on_faces); /* change or not the basis on the face */
    6315:       }
    6316:       temp_constraints = 0;          /* zero the number of constraints I have on this conn comp */

    6318:       ISGetSize(used_is,&size_of_constraint);
    6319:       ISGetIndices(used_is,(const PetscInt**)&is_indices);
    6320:       /* change of basis should not be performed on local periodic nodes */
    6321:       if (pcbddc->mat_graph->mirrors && pcbddc->mat_graph->mirrors[is_indices[0]]) boolforchange = PETSC_FALSE;
    6322:       if (nnsp_has_cnst) {
    6323:         PetscScalar quad_value;

    6325:         PetscArraycpy(constraints_idxs + constraints_idxs_ptr[total_counts_cc],is_indices,size_of_constraint);
    6326:         idxs_copied = PETSC_TRUE;

    6328:         if (!pcbddc->use_nnsp_true) {
    6329:           quad_value = (PetscScalar)(1.0/PetscSqrtReal((PetscReal)size_of_constraint));
    6330:         } else {
    6331:           quad_value = 1.0;
    6332:         }
    6333:         for (j=0;j<size_of_constraint;j++) {
    6334:           constraints_data[constraints_data_ptr[total_counts_cc]+j] = quad_value;
    6335:         }
    6336:         temp_constraints++;
    6337:         total_counts++;
    6338:       }
    6339:       for (k=0;k<nnsp_size;k++) {
    6340:         PetscReal real_value;
    6341:         PetscScalar *ptr_to_data;

    6343:         VecGetArrayRead(localnearnullsp[k],(const PetscScalar**)&array);
    6344:         ptr_to_data = &constraints_data[constraints_data_ptr[total_counts_cc]+temp_constraints*size_of_constraint];
    6345:         for (j=0;j<size_of_constraint;j++) {
    6346:           ptr_to_data[j] = array[is_indices[j]];
    6347:         }
    6348:         VecRestoreArrayRead(localnearnullsp[k],(const PetscScalar**)&array);
    6349:         /* check if array is null on the connected component */
    6350:         PetscBLASIntCast(size_of_constraint,&Blas_N);
    6351:         PetscStackCallBLAS("BLASasum",real_value = BLASasum_(&Blas_N,ptr_to_data,&Blas_one));
    6352:         if (real_value > tol*size_of_constraint) { /* keep indices and values */
    6353:           temp_constraints++;
    6354:           total_counts++;
    6355:           if (!idxs_copied) {
    6356:             PetscArraycpy(constraints_idxs + constraints_idxs_ptr[total_counts_cc],is_indices,size_of_constraint);
    6357:             idxs_copied = PETSC_TRUE;
    6358:           }
    6359:         }
    6360:       }
    6361:       ISRestoreIndices(used_is,(const PetscInt**)&is_indices);
    6362:       valid_constraints = temp_constraints;
    6363:       if (!pcbddc->use_nnsp_true && temp_constraints) {
    6364:         if (temp_constraints == 1) { /* just normalize the constraint */
    6365:           PetscScalar norm,*ptr_to_data;

    6367:           ptr_to_data = &constraints_data[constraints_data_ptr[total_counts_cc]];
    6368:           PetscBLASIntCast(size_of_constraint,&Blas_N);
    6369:           PetscStackCallBLAS("BLASdot",norm = BLASdot_(&Blas_N,ptr_to_data,&Blas_one,ptr_to_data,&Blas_one));
    6370:           norm = 1.0/PetscSqrtReal(PetscRealPart(norm));
    6371:           PetscStackCallBLAS("BLASscal",BLASscal_(&Blas_N,&norm,ptr_to_data,&Blas_one));
    6372:         } else { /* perform SVD */
    6373:           PetscScalar *ptr_to_data = &constraints_data[constraints_data_ptr[total_counts_cc]];

    6375:           if (use_pod) {
    6376:             /* SVD: Y = U*S*V^H                -> U (eigenvectors of Y*Y^H) = Y*V*(S)^\dag
    6377:                POD: Y^H*Y = V*D*V^H, D = S^H*S -> U = Y*V*D^(-1/2)
    6378:                -> When PETSC_USE_COMPLEX and PETSC_MISSING_LAPACK_GESVD are defined
    6379:                   the constraints basis will differ (by a complex factor with absolute value equal to 1)
    6380:                   from that computed using LAPACKgesvd
    6381:                -> This is due to a different computation of eigenvectors in LAPACKheev
    6382:                -> The quality of the POD-computed basis will be the same */
    6383:             PetscArrayzero(correlation_mat,temp_constraints*temp_constraints);
    6384:             /* Store upper triangular part of correlation matrix */
    6385:             PetscBLASIntCast(size_of_constraint,&Blas_N);
    6386:             PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    6387:             for (j=0;j<temp_constraints;j++) {
    6388:               for (k=0;k<j+1;k++) {
    6389:                 PetscStackCallBLAS("BLASdot",correlation_mat[j*temp_constraints+k] = BLASdot_(&Blas_N,ptr_to_data+k*size_of_constraint,&Blas_one,ptr_to_data+j*size_of_constraint,&Blas_one));
    6390:               }
    6391:             }
    6392:             /* compute eigenvalues and eigenvectors of correlation matrix */
    6393:             PetscBLASIntCast(temp_constraints,&Blas_N);
    6394:             PetscBLASIntCast(temp_constraints,&Blas_LDA);
    6395: #if !defined(PETSC_USE_COMPLEX)
    6396:             PetscStackCallBLAS("LAPACKsyev",LAPACKsyev_("V","U",&Blas_N,correlation_mat,&Blas_LDA,singular_vals,work,&lwork,&lierr));
    6397: #else
    6398:             PetscStackCallBLAS("LAPACKsyev",LAPACKsyev_("V","U",&Blas_N,correlation_mat,&Blas_LDA,singular_vals,work,&lwork,rwork,&lierr));
    6399: #endif
    6400:             PetscFPTrapPop();
    6401:             if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in SYEV Lapack routine %d",(int)lierr);
    6402:             /* retain eigenvalues greater than tol: note that LAPACKsyev gives eigs in ascending order */
    6403:             j = 0;
    6404:             while (j < temp_constraints && singular_vals[j]/singular_vals[temp_constraints-1] < tol) j++;
    6405:             total_counts = total_counts-j;
    6406:             valid_constraints = temp_constraints-j;
    6407:             /* scale and copy POD basis into used quadrature memory */
    6408:             PetscBLASIntCast(size_of_constraint,&Blas_M);
    6409:             PetscBLASIntCast(temp_constraints,&Blas_N);
    6410:             PetscBLASIntCast(temp_constraints,&Blas_K);
    6411:             PetscBLASIntCast(size_of_constraint,&Blas_LDA);
    6412:             PetscBLASIntCast(temp_constraints,&Blas_LDB);
    6413:             PetscBLASIntCast(size_of_constraint,&Blas_LDC);
    6414:             if (j<temp_constraints) {
    6415:               PetscInt ii;
    6416:               for (k=j;k<temp_constraints;k++) singular_vals[k] = 1.0/PetscSqrtReal(singular_vals[k]);
    6417:               PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    6418:               PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&Blas_M,&Blas_N,&Blas_K,&one,ptr_to_data,&Blas_LDA,correlation_mat,&Blas_LDB,&zero,temp_basis,&Blas_LDC));
    6419:               PetscFPTrapPop();
    6420:               for (k=0;k<temp_constraints-j;k++) {
    6421:                 for (ii=0;ii<size_of_constraint;ii++) {
    6422:                   ptr_to_data[k*size_of_constraint+ii] = singular_vals[temp_constraints-1-k]*temp_basis[(temp_constraints-1-k)*size_of_constraint+ii];
    6423:                 }
    6424:               }
    6425:             }
    6426:           } else {
    6427: #if !defined(PETSC_MISSING_LAPACK_GESVD)
    6428:             PetscBLASIntCast(size_of_constraint,&Blas_M);
    6429:             PetscBLASIntCast(temp_constraints,&Blas_N);
    6430:             PetscBLASIntCast(size_of_constraint,&Blas_LDA);
    6431:             PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    6432: #if !defined(PETSC_USE_COMPLEX)
    6433:             PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("O","N",&Blas_M,&Blas_N,ptr_to_data,&Blas_LDA,singular_vals,&dummy_scalar,&dummy_int,&dummy_scalar,&dummy_int,work,&lwork,&lierr));
    6434: #else
    6435:             PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("O","N",&Blas_M,&Blas_N,ptr_to_data,&Blas_LDA,singular_vals,&dummy_scalar,&dummy_int,&dummy_scalar,&dummy_int,work,&lwork,rwork,&lierr));
    6436: #endif
    6437:             if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in GESVD Lapack routine %d",(int)lierr);
    6438:             PetscFPTrapPop();
    6439:             /* retain eigenvalues greater than tol: note that LAPACKgesvd gives eigs in descending order */
    6440:             k = temp_constraints;
    6441:             if (k > size_of_constraint) k = size_of_constraint;
    6442:             j = 0;
    6443:             while (j < k && singular_vals[k-j-1]/singular_vals[0] < tol) j++;
    6444:             valid_constraints = k-j;
    6445:             total_counts = total_counts-temp_constraints+valid_constraints;
    6446: #else
    6447:             SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"This should not happen");
    6448: #endif /* on missing GESVD */
    6449:           }
    6450:         }
    6451:       }
    6452:       /* update pointers information */
    6453:       if (valid_constraints) {
    6454:         constraints_n[total_counts_cc] = valid_constraints;
    6455:         constraints_idxs_ptr[total_counts_cc+1] = constraints_idxs_ptr[total_counts_cc]+size_of_constraint;
    6456:         constraints_data_ptr[total_counts_cc+1] = constraints_data_ptr[total_counts_cc]+size_of_constraint*valid_constraints;
    6457:         /* set change_of_basis flag */
    6458:         if (boolforchange) {
    6459:           PetscBTSet(change_basis,total_counts_cc);
    6460:         }
    6461:         total_counts_cc++;
    6462:       }
    6463:     }
    6464:     /* free workspace */
    6465:     if (!skip_lapack) {
    6466:       PetscFree(work);
    6467: #if defined(PETSC_USE_COMPLEX)
    6468:       PetscFree(rwork);
    6469: #endif
    6470:       PetscFree(singular_vals);
    6471:       PetscFree(correlation_mat);
    6472:       PetscFree(temp_basis);
    6473:     }
    6474:     for (k=0;k<nnsp_size;k++) {
    6475:       VecDestroy(&localnearnullsp[k]);
    6476:     }
    6477:     PetscFree(localnearnullsp);
    6478:     /* free index sets of faces, edges and vertices */
    6479:     for (i=0;i<n_ISForFaces;i++) {
    6480:       ISDestroy(&ISForFaces[i]);
    6481:     }
    6482:     if (n_ISForFaces) {
    6483:       PetscFree(ISForFaces);
    6484:     }
    6485:     for (i=0;i<n_ISForEdges;i++) {
    6486:       ISDestroy(&ISForEdges[i]);
    6487:     }
    6488:     if (n_ISForEdges) {
    6489:       PetscFree(ISForEdges);
    6490:     }
    6491:     ISDestroy(&ISForVertices);
    6492:   } else {
    6493:     PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs;

    6495:     total_counts = 0;
    6496:     n_vertices = 0;
    6497:     if (sub_schurs->is_vertices && pcbddc->use_vertices) {
    6498:       ISGetLocalSize(sub_schurs->is_vertices,&n_vertices);
    6499:     }
    6500:     max_constraints = 0;
    6501:     total_counts_cc = 0;
    6502:     for (i=0;i<sub_schurs->n_subs+n_vertices;i++) {
    6503:       total_counts += pcbddc->adaptive_constraints_n[i];
    6504:       if (pcbddc->adaptive_constraints_n[i]) total_counts_cc++;
    6505:       max_constraints = PetscMax(max_constraints,pcbddc->adaptive_constraints_n[i]);
    6506:     }
    6507:     constraints_idxs_ptr = pcbddc->adaptive_constraints_idxs_ptr;
    6508:     constraints_data_ptr = pcbddc->adaptive_constraints_data_ptr;
    6509:     constraints_idxs = pcbddc->adaptive_constraints_idxs;
    6510:     constraints_data = pcbddc->adaptive_constraints_data;
    6511:     /* constraints_n differs from pcbddc->adaptive_constraints_n */
    6512:     PetscMalloc1(total_counts_cc,&constraints_n);
    6513:     total_counts_cc = 0;
    6514:     for (i=0;i<sub_schurs->n_subs+n_vertices;i++) {
    6515:       if (pcbddc->adaptive_constraints_n[i]) {
    6516:         constraints_n[total_counts_cc++] = pcbddc->adaptive_constraints_n[i];
    6517:       }
    6518:     }

    6520:     max_size_of_constraint = 0;
    6521:     for (i=0;i<total_counts_cc;i++) max_size_of_constraint = PetscMax(max_size_of_constraint,constraints_idxs_ptr[i+1]-constraints_idxs_ptr[i]);
    6522:     PetscMalloc1(constraints_idxs_ptr[total_counts_cc],&constraints_idxs_B);
    6523:     /* Change of basis */
    6524:     PetscBTCreate(total_counts_cc,&change_basis);
    6525:     if (pcbddc->use_change_of_basis) {
    6526:       for (i=0;i<sub_schurs->n_subs;i++) {
    6527:         if (PetscBTLookup(sub_schurs->is_edge,i) || pcbddc->use_change_on_faces) {
    6528:           PetscBTSet(change_basis,i+n_vertices);
    6529:         }
    6530:       }
    6531:     }
    6532:   }
    6533:   pcbddc->local_primal_size = total_counts;
    6534:   PetscMalloc1(pcbddc->local_primal_size+pcbddc->benign_n,&pcbddc->primal_indices_local_idxs);

    6536:   /* map constraints_idxs in boundary numbering */
    6537:   ISGlobalToLocalMappingApply(pcis->BtoNmap,IS_GTOLM_DROP,constraints_idxs_ptr[total_counts_cc],constraints_idxs,&i,constraints_idxs_B);
    6538:   if (i != constraints_idxs_ptr[total_counts_cc]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Error in boundary numbering for constraints indices %D != %D",constraints_idxs_ptr[total_counts_cc],i);

    6540:   /* Create constraint matrix */
    6541:   MatCreate(PETSC_COMM_SELF,&pcbddc->ConstraintMatrix);
    6542:   MatSetType(pcbddc->ConstraintMatrix,MATAIJ);
    6543:   MatSetSizes(pcbddc->ConstraintMatrix,pcbddc->local_primal_size,pcis->n,pcbddc->local_primal_size,pcis->n);

    6545:   /* find primal_dofs: subdomain corners plus dofs selected as primal after change of basis */
    6546:   /* determine if a QR strategy is needed for change of basis */
    6547:   qr_needed = pcbddc->use_qr_single;
    6548:   PetscBTCreate(total_counts_cc,&qr_needed_idx);
    6549:   total_primal_vertices=0;
    6550:   pcbddc->local_primal_size_cc = 0;
    6551:   for (i=0;i<total_counts_cc;i++) {
    6552:     size_of_constraint = constraints_idxs_ptr[i+1]-constraints_idxs_ptr[i];
    6553:     if (size_of_constraint == 1 && pcbddc->mat_graph->custom_minimal_size) {
    6554:       pcbddc->primal_indices_local_idxs[total_primal_vertices++] = constraints_idxs[constraints_idxs_ptr[i]];
    6555:       pcbddc->local_primal_size_cc += 1;
    6556:     } else if (PetscBTLookup(change_basis,i)) {
    6557:       for (k=0;k<constraints_n[i];k++) {
    6558:         pcbddc->primal_indices_local_idxs[total_primal_vertices++] = constraints_idxs[constraints_idxs_ptr[i]+k];
    6559:       }
    6560:       pcbddc->local_primal_size_cc += constraints_n[i];
    6561:       if (constraints_n[i] > 1 || pcbddc->use_qr_single) {
    6562:         PetscBTSet(qr_needed_idx,i);
    6563:         qr_needed = PETSC_TRUE;
    6564:       }
    6565:     } else {
    6566:       pcbddc->local_primal_size_cc += 1;
    6567:     }
    6568:   }
    6569:   /* note that the local variable n_vertices used below stores the number of pointwise constraints */
    6570:   pcbddc->n_vertices = total_primal_vertices;
    6571:   /* permute indices in order to have a sorted set of vertices */
    6572:   PetscSortInt(total_primal_vertices,pcbddc->primal_indices_local_idxs);
    6573:   PetscMalloc2(pcbddc->local_primal_size_cc+pcbddc->benign_n,&pcbddc->local_primal_ref_node,pcbddc->local_primal_size_cc+pcbddc->benign_n,&pcbddc->local_primal_ref_mult);
    6574:   PetscArraycpy(pcbddc->local_primal_ref_node,pcbddc->primal_indices_local_idxs,total_primal_vertices);
    6575:   for (i=0;i<total_primal_vertices;i++) pcbddc->local_primal_ref_mult[i] = 1;

    6577:   /* nonzero structure of constraint matrix */
    6578:   /* and get reference dof for local constraints */
    6579:   PetscMalloc1(pcbddc->local_primal_size,&nnz);
    6580:   for (i=0;i<total_primal_vertices;i++) nnz[i] = 1;

    6582:   j = total_primal_vertices;
    6583:   total_counts = total_primal_vertices;
    6584:   cum = total_primal_vertices;
    6585:   for (i=n_vertices;i<total_counts_cc;i++) {
    6586:     if (!PetscBTLookup(change_basis,i)) {
    6587:       pcbddc->local_primal_ref_node[cum] = constraints_idxs[constraints_idxs_ptr[i]];
    6588:       pcbddc->local_primal_ref_mult[cum] = constraints_n[i];
    6589:       cum++;
    6590:       size_of_constraint = constraints_idxs_ptr[i+1]-constraints_idxs_ptr[i];
    6591:       for (k=0;k<constraints_n[i];k++) {
    6592:         pcbddc->primal_indices_local_idxs[total_counts++] = constraints_idxs[constraints_idxs_ptr[i]+k];
    6593:         nnz[j+k] = size_of_constraint;
    6594:       }
    6595:       j += constraints_n[i];
    6596:     }
    6597:   }
    6598:   MatSeqAIJSetPreallocation(pcbddc->ConstraintMatrix,0,nnz);
    6599:   MatSetOption(pcbddc->ConstraintMatrix,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
    6600:   PetscFree(nnz);

    6602:   /* set values in constraint matrix */
    6603:   for (i=0;i<total_primal_vertices;i++) {
    6604:     MatSetValue(pcbddc->ConstraintMatrix,i,pcbddc->local_primal_ref_node[i],1.0,INSERT_VALUES);
    6605:   }
    6606:   total_counts = total_primal_vertices;
    6607:   for (i=n_vertices;i<total_counts_cc;i++) {
    6608:     if (!PetscBTLookup(change_basis,i)) {
    6609:       PetscInt *cols;

    6611:       size_of_constraint = constraints_idxs_ptr[i+1]-constraints_idxs_ptr[i];
    6612:       cols = constraints_idxs+constraints_idxs_ptr[i];
    6613:       for (k=0;k<constraints_n[i];k++) {
    6614:         PetscInt    row = total_counts+k;
    6615:         PetscScalar *vals;

    6617:         vals = constraints_data+constraints_data_ptr[i]+k*size_of_constraint;
    6618:         MatSetValues(pcbddc->ConstraintMatrix,1,&row,size_of_constraint,cols,vals,INSERT_VALUES);
    6619:       }
    6620:       total_counts += constraints_n[i];
    6621:     }
    6622:   }
    6623:   /* assembling */
    6624:   MatAssemblyBegin(pcbddc->ConstraintMatrix,MAT_FINAL_ASSEMBLY);
    6625:   MatAssemblyEnd(pcbddc->ConstraintMatrix,MAT_FINAL_ASSEMBLY);
    6626:   MatViewFromOptions(pcbddc->ConstraintMatrix,(PetscObject)pc,"-pc_bddc_constraint_mat_view");

    6628:   /* Create matrix for change of basis. We don't need it in case pcbddc->use_change_of_basis is FALSE */
    6629:   if (pcbddc->use_change_of_basis) {
    6630:     /* dual and primal dofs on a single cc */
    6631:     PetscInt     dual_dofs,primal_dofs;
    6632:     /* working stuff for GEQRF */
    6633:     PetscScalar  *qr_basis = NULL,*qr_tau = NULL,*qr_work = NULL,lqr_work_t;
    6634:     PetscBLASInt lqr_work;
    6635:     /* working stuff for UNGQR */
    6636:     PetscScalar  *gqr_work = NULL,lgqr_work_t=0.0;
    6637:     PetscBLASInt lgqr_work;
    6638:     /* working stuff for TRTRS */
    6639:     PetscScalar  *trs_rhs = NULL;
    6640:     PetscBLASInt Blas_NRHS;
    6641:     /* pointers for values insertion into change of basis matrix */
    6642:     PetscInt     *start_rows,*start_cols;
    6643:     PetscScalar  *start_vals;
    6644:     /* working stuff for values insertion */
    6645:     PetscBT      is_primal;
    6646:     PetscInt     *aux_primal_numbering_B;
    6647:     /* matrix sizes */
    6648:     PetscInt     global_size,local_size;
    6649:     /* temporary change of basis */
    6650:     Mat          localChangeOfBasisMatrix;
    6651:     /* extra space for debugging */
    6652:     PetscScalar  *dbg_work = NULL;

    6654:     /* local temporary change of basis acts on local interfaces -> dimension is n_B x n_B */
    6655:     MatCreate(PETSC_COMM_SELF,&localChangeOfBasisMatrix);
    6656:     MatSetType(localChangeOfBasisMatrix,MATAIJ);
    6657:     MatSetSizes(localChangeOfBasisMatrix,pcis->n,pcis->n,pcis->n,pcis->n);
    6658:     /* nonzeros for local mat */
    6659:     PetscMalloc1(pcis->n,&nnz);
    6660:     if (!pcbddc->benign_change || pcbddc->fake_change) {
    6661:       for (i=0;i<pcis->n;i++) nnz[i]=1;
    6662:     } else {
    6663:       const PetscInt *ii;
    6664:       PetscInt       n;
    6665:       PetscBool      flg_row;
    6666:       MatGetRowIJ(pcbddc->benign_change,0,PETSC_FALSE,PETSC_FALSE,&n,&ii,NULL,&flg_row);
    6667:       for (i=0;i<n;i++) nnz[i] = ii[i+1]-ii[i];
    6668:       MatRestoreRowIJ(pcbddc->benign_change,0,PETSC_FALSE,PETSC_FALSE,&n,&ii,NULL,&flg_row);
    6669:     }
    6670:     for (i=n_vertices;i<total_counts_cc;i++) {
    6671:       if (PetscBTLookup(change_basis,i)) {
    6672:         size_of_constraint = constraints_idxs_ptr[i+1]-constraints_idxs_ptr[i];
    6673:         if (PetscBTLookup(qr_needed_idx,i)) {
    6674:           for (j=0;j<size_of_constraint;j++) nnz[constraints_idxs[constraints_idxs_ptr[i]+j]] = size_of_constraint;
    6675:         } else {
    6676:           nnz[constraints_idxs[constraints_idxs_ptr[i]]] = size_of_constraint;
    6677:           for (j=1;j<size_of_constraint;j++) nnz[constraints_idxs[constraints_idxs_ptr[i]+j]] = 2;
    6678:         }
    6679:       }
    6680:     }
    6681:     MatSeqAIJSetPreallocation(localChangeOfBasisMatrix,0,nnz);
    6682:     MatSetOption(localChangeOfBasisMatrix,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
    6683:     PetscFree(nnz);
    6684:     /* Set interior change in the matrix */
    6685:     if (!pcbddc->benign_change || pcbddc->fake_change) {
    6686:       for (i=0;i<pcis->n;i++) {
    6687:         MatSetValue(localChangeOfBasisMatrix,i,i,1.0,INSERT_VALUES);
    6688:       }
    6689:     } else {
    6690:       const PetscInt *ii,*jj;
    6691:       PetscScalar    *aa;
    6692:       PetscInt       n;
    6693:       PetscBool      flg_row;
    6694:       MatGetRowIJ(pcbddc->benign_change,0,PETSC_FALSE,PETSC_FALSE,&n,&ii,&jj,&flg_row);
    6695:       MatSeqAIJGetArray(pcbddc->benign_change,&aa);
    6696:       for (i=0;i<n;i++) {
    6697:         MatSetValues(localChangeOfBasisMatrix,1,&i,ii[i+1]-ii[i],jj+ii[i],aa+ii[i],INSERT_VALUES);
    6698:       }
    6699:       MatSeqAIJRestoreArray(pcbddc->benign_change,&aa);
    6700:       MatRestoreRowIJ(pcbddc->benign_change,0,PETSC_FALSE,PETSC_FALSE,&n,&ii,&jj,&flg_row);
    6701:     }

    6703:     if (pcbddc->dbg_flag) {
    6704:       PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"--------------------------------------------------------------\n");
    6705:       PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Checking change of basis computation for subdomain %04d\n",PetscGlobalRank);
    6706:     }

    6708:     /* Now we loop on the constraints which need a change of basis */
    6709:     /*
    6710:        Change of basis matrix is evaluated similarly to the FIRST APPROACH in
    6711:        Klawonn and Widlund, Dual-primal FETI-DP methods for linear elasticity, (see Sect 6.2.1)

    6713:        Basic blocks of change of basis matrix T computed by

    6715:           - Using the following block transformation if there is only a primal dof on the cc (and -pc_bddc_use_qr_single is not specified)

    6717:             | 1        0   ...        0         s_1/S |
    6718:             | 0        1   ...        0         s_2/S |
    6719:             |              ...                        |
    6720:             | 0        ...            1     s_{n-1}/S |
    6721:             | -s_1/s_n ...    -s_{n-1}/s_n      s_n/S |

    6723:             with S = \sum_{i=1}^n s_i^2
    6724:             NOTE: in the above example, the primal dof is the last one of the edge in LOCAL ordering
    6725:                   in the current implementation, the primal dof is the first one of the edge in GLOBAL ordering

    6727:           - QR decomposition of constraints otherwise
    6728:     */
    6729:     if (qr_needed && max_size_of_constraint) {
    6730:       /* space to store Q */
    6731:       PetscMalloc1(max_size_of_constraint*max_size_of_constraint,&qr_basis);
    6732:       /* array to store scaling factors for reflectors */
    6733:       PetscMalloc1(max_constraints,&qr_tau);
    6734:       /* first we issue queries for optimal work */
    6735:       PetscBLASIntCast(max_size_of_constraint,&Blas_M);
    6736:       PetscBLASIntCast(max_constraints,&Blas_N);
    6737:       PetscBLASIntCast(max_size_of_constraint,&Blas_LDA);
    6738:       lqr_work = -1;
    6739:       PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&Blas_M,&Blas_N,qr_basis,&Blas_LDA,qr_tau,&lqr_work_t,&lqr_work,&lierr));
    6740:       if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in query to GEQRF Lapack routine %d",(int)lierr);
    6741:       PetscBLASIntCast((PetscInt)PetscRealPart(lqr_work_t),&lqr_work);
    6742:       PetscMalloc1((PetscInt)PetscRealPart(lqr_work_t),&qr_work);
    6743:       lgqr_work = -1;
    6744:       PetscBLASIntCast(max_size_of_constraint,&Blas_M);
    6745:       PetscBLASIntCast(max_size_of_constraint,&Blas_N);
    6746:       PetscBLASIntCast(max_constraints,&Blas_K);
    6747:       PetscBLASIntCast(max_size_of_constraint,&Blas_LDA);
    6748:       if (Blas_K>Blas_M) Blas_K=Blas_M; /* adjust just for computing optimal work */
    6749:       PetscStackCallBLAS("LAPACKorgqr",LAPACKorgqr_(&Blas_M,&Blas_N,&Blas_K,qr_basis,&Blas_LDA,qr_tau,&lgqr_work_t,&lgqr_work,&lierr));
    6750:       if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in query to ORGQR/UNGQR Lapack routine %d",(int)lierr);
    6751:       PetscBLASIntCast((PetscInt)PetscRealPart(lgqr_work_t),&lgqr_work);
    6752:       PetscMalloc1((PetscInt)PetscRealPart(lgqr_work_t),&gqr_work);
    6753:       /* array to store rhs and solution of triangular solver */
    6754:       PetscMalloc1(max_constraints*max_constraints,&trs_rhs);
    6755:       /* allocating workspace for check */
    6756:       if (pcbddc->dbg_flag) {
    6757:         PetscMalloc1(max_size_of_constraint*(max_constraints+max_size_of_constraint),&dbg_work);
    6758:       }
    6759:     }
    6760:     /* array to store whether a node is primal or not */
    6761:     PetscBTCreate(pcis->n_B,&is_primal);
    6762:     PetscMalloc1(total_primal_vertices,&aux_primal_numbering_B);
    6763:     ISGlobalToLocalMappingApply(pcis->BtoNmap,IS_GTOLM_DROP,total_primal_vertices,pcbddc->local_primal_ref_node,&i,aux_primal_numbering_B);
    6764:     if (i != total_primal_vertices) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Error in boundary numbering for BDDC vertices! %D != %D",total_primal_vertices,i);
    6765:     for (i=0;i<total_primal_vertices;i++) {
    6766:       PetscBTSet(is_primal,aux_primal_numbering_B[i]);
    6767:     }
    6768:     PetscFree(aux_primal_numbering_B);

    6770:     /* loop on constraints and see whether or not they need a change of basis and compute it */
    6771:     for (total_counts=n_vertices;total_counts<total_counts_cc;total_counts++) {
    6772:       size_of_constraint = constraints_idxs_ptr[total_counts+1]-constraints_idxs_ptr[total_counts];
    6773:       if (PetscBTLookup(change_basis,total_counts)) {
    6774:         /* get constraint info */
    6775:         primal_dofs = constraints_n[total_counts];
    6776:         dual_dofs = size_of_constraint-primal_dofs;

    6778:         if (pcbddc->dbg_flag) {
    6779:           PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Constraints %D: %D need a change of basis (size %D)\n",total_counts,primal_dofs,size_of_constraint);
    6780:         }

    6782:         if (PetscBTLookup(qr_needed_idx,total_counts)) { /* QR */

    6784:           /* copy quadrature constraints for change of basis check */
    6785:           if (pcbddc->dbg_flag) {
    6786:             PetscArraycpy(dbg_work,&constraints_data[constraints_data_ptr[total_counts]],size_of_constraint*primal_dofs);
    6787:           }
    6788:           /* copy temporary constraints into larger work vector (in order to store all columns of Q) */
    6789:           PetscArraycpy(qr_basis,&constraints_data[constraints_data_ptr[total_counts]],size_of_constraint*primal_dofs);

    6791:           /* compute QR decomposition of constraints */
    6792:           PetscBLASIntCast(size_of_constraint,&Blas_M);
    6793:           PetscBLASIntCast(primal_dofs,&Blas_N);
    6794:           PetscBLASIntCast(size_of_constraint,&Blas_LDA);
    6795:           PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    6796:           PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&Blas_M,&Blas_N,qr_basis,&Blas_LDA,qr_tau,qr_work,&lqr_work,&lierr));
    6797:           if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in GEQRF Lapack routine %d",(int)lierr);
    6798:           PetscFPTrapPop();

    6800:           /* explicitly compute R^-T */
    6801:           PetscArrayzero(trs_rhs,primal_dofs*primal_dofs);
    6802:           for (j=0;j<primal_dofs;j++) trs_rhs[j*(primal_dofs+1)] = 1.0;
    6803:           PetscBLASIntCast(primal_dofs,&Blas_N);
    6804:           PetscBLASIntCast(primal_dofs,&Blas_NRHS);
    6805:           PetscBLASIntCast(size_of_constraint,&Blas_LDA);
    6806:           PetscBLASIntCast(primal_dofs,&Blas_LDB);
    6807:           PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    6808:           PetscStackCallBLAS("LAPACKtrtrs",LAPACKtrtrs_("U","T","N",&Blas_N,&Blas_NRHS,qr_basis,&Blas_LDA,trs_rhs,&Blas_LDB,&lierr));
    6809:           if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in TRTRS Lapack routine %d",(int)lierr);
    6810:           PetscFPTrapPop();

    6812:           /* explicitly compute all columns of Q (Q = [Q1 | Q2]) overwriting QR factorization in qr_basis */
    6813:           PetscBLASIntCast(size_of_constraint,&Blas_M);
    6814:           PetscBLASIntCast(size_of_constraint,&Blas_N);
    6815:           PetscBLASIntCast(primal_dofs,&Blas_K);
    6816:           PetscBLASIntCast(size_of_constraint,&Blas_LDA);
    6817:           PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    6818:           PetscStackCallBLAS("LAPACKorgqr",LAPACKorgqr_(&Blas_M,&Blas_N,&Blas_K,qr_basis,&Blas_LDA,qr_tau,gqr_work,&lgqr_work,&lierr));
    6819:           if (lierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in ORGQR/UNGQR Lapack routine %d",(int)lierr);
    6820:           PetscFPTrapPop();

    6822:           /* first primal_dofs columns of Q need to be re-scaled in order to be unitary w.r.t constraints
    6823:              i.e. C_{pxn}*Q_{nxn} should be equal to [I_pxp | 0_pxd] (see check below)
    6824:              where n=size_of_constraint, p=primal_dofs, d=dual_dofs (n=p+d), I and 0 identity and null matrix resp. */
    6825:           PetscBLASIntCast(size_of_constraint,&Blas_M);
    6826:           PetscBLASIntCast(primal_dofs,&Blas_N);
    6827:           PetscBLASIntCast(primal_dofs,&Blas_K);
    6828:           PetscBLASIntCast(size_of_constraint,&Blas_LDA);
    6829:           PetscBLASIntCast(primal_dofs,&Blas_LDB);
    6830:           PetscBLASIntCast(size_of_constraint,&Blas_LDC);
    6831:           PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    6832:           PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&Blas_M,&Blas_N,&Blas_K,&one,qr_basis,&Blas_LDA,trs_rhs,&Blas_LDB,&zero,constraints_data+constraints_data_ptr[total_counts],&Blas_LDC));
    6833:           PetscFPTrapPop();
    6834:           PetscArraycpy(qr_basis,&constraints_data[constraints_data_ptr[total_counts]],size_of_constraint*primal_dofs);

    6836:           /* insert values in change of basis matrix respecting global ordering of new primal dofs */
    6837:           start_rows = &constraints_idxs[constraints_idxs_ptr[total_counts]];
    6838:           /* insert cols for primal dofs */
    6839:           for (j=0;j<primal_dofs;j++) {
    6840:             start_vals = &qr_basis[j*size_of_constraint];
    6841:             start_cols = &constraints_idxs[constraints_idxs_ptr[total_counts]+j];
    6842:             MatSetValues(localChangeOfBasisMatrix,size_of_constraint,start_rows,1,start_cols,start_vals,INSERT_VALUES);
    6843:           }
    6844:           /* insert cols for dual dofs */
    6845:           for (j=0,k=0;j<dual_dofs;k++) {
    6846:             if (!PetscBTLookup(is_primal,constraints_idxs_B[constraints_idxs_ptr[total_counts]+k])) {
    6847:               start_vals = &qr_basis[(primal_dofs+j)*size_of_constraint];
    6848:               start_cols = &constraints_idxs[constraints_idxs_ptr[total_counts]+k];
    6849:               MatSetValues(localChangeOfBasisMatrix,size_of_constraint,start_rows,1,start_cols,start_vals,INSERT_VALUES);
    6850:               j++;
    6851:             }
    6852:           }

    6854:           /* check change of basis */
    6855:           if (pcbddc->dbg_flag) {
    6856:             PetscInt   ii,jj;
    6857:             PetscBool valid_qr=PETSC_TRUE;
    6858:             PetscBLASIntCast(primal_dofs,&Blas_M);
    6859:             PetscBLASIntCast(size_of_constraint,&Blas_N);
    6860:             PetscBLASIntCast(size_of_constraint,&Blas_K);
    6861:             PetscBLASIntCast(size_of_constraint,&Blas_LDA);
    6862:             PetscBLASIntCast(size_of_constraint,&Blas_LDB);
    6863:             PetscBLASIntCast(primal_dofs,&Blas_LDC);
    6864:             PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    6865:             PetscStackCallBLAS("BLASgemm",BLASgemm_("T","N",&Blas_M,&Blas_N,&Blas_K,&one,dbg_work,&Blas_LDA,qr_basis,&Blas_LDB,&zero,&dbg_work[size_of_constraint*primal_dofs],&Blas_LDC));
    6866:             PetscFPTrapPop();
    6867:             for (jj=0;jj<size_of_constraint;jj++) {
    6868:               for (ii=0;ii<primal_dofs;ii++) {
    6869:                 if (ii != jj && PetscAbsScalar(dbg_work[size_of_constraint*primal_dofs+jj*primal_dofs+ii]) > 1.e-12) valid_qr = PETSC_FALSE;
    6870:                 if (ii == jj && PetscAbsScalar(dbg_work[size_of_constraint*primal_dofs+jj*primal_dofs+ii]-(PetscReal)1) > 1.e-12) valid_qr = PETSC_FALSE;
    6871:               }
    6872:             }
    6873:             if (!valid_qr) {
    6874:               PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"\t-> wrong change of basis!\n");
    6875:               for (jj=0;jj<size_of_constraint;jj++) {
    6876:                 for (ii=0;ii<primal_dofs;ii++) {
    6877:                   if (ii != jj && PetscAbsScalar(dbg_work[size_of_constraint*primal_dofs+jj*primal_dofs+ii]) > 1.e-12) {
    6878:                     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"\tQr basis function %D is not orthogonal to constraint %D (%1.14e)!\n",jj,ii,PetscAbsScalar(dbg_work[size_of_constraint*primal_dofs+jj*primal_dofs+ii]));
    6879:                   }
    6880:                   if (ii == jj && PetscAbsScalar(dbg_work[size_of_constraint*primal_dofs+jj*primal_dofs+ii]-(PetscReal)1) > 1.e-12) {
    6881:                     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"\tQr basis function %D is not unitary w.r.t constraint %D (%1.14e)!\n",jj,ii,PetscAbsScalar(dbg_work[size_of_constraint*primal_dofs+jj*primal_dofs+ii]));
    6882:                   }
    6883:                 }
    6884:               }
    6885:             } else {
    6886:               PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"\t-> right change of basis!\n");
    6887:             }
    6888:           }
    6889:         } else { /* simple transformation block */
    6890:           PetscInt    row,col;
    6891:           PetscScalar val,norm;

    6893:           PetscBLASIntCast(size_of_constraint,&Blas_N);
    6894:           PetscStackCallBLAS("BLASdot",norm = BLASdot_(&Blas_N,constraints_data+constraints_data_ptr[total_counts],&Blas_one,constraints_data+constraints_data_ptr[total_counts],&Blas_one));
    6895:           for (j=0;j<size_of_constraint;j++) {
    6896:             PetscInt row_B = constraints_idxs_B[constraints_idxs_ptr[total_counts]+j];
    6897:             row = constraints_idxs[constraints_idxs_ptr[total_counts]+j];
    6898:             if (!PetscBTLookup(is_primal,row_B)) {
    6899:               col = constraints_idxs[constraints_idxs_ptr[total_counts]];
    6900:               MatSetValue(localChangeOfBasisMatrix,row,row,1.0,INSERT_VALUES);
    6901:               MatSetValue(localChangeOfBasisMatrix,row,col,constraints_data[constraints_data_ptr[total_counts]+j]/norm,INSERT_VALUES);
    6902:             } else {
    6903:               for (k=0;k<size_of_constraint;k++) {
    6904:                 col = constraints_idxs[constraints_idxs_ptr[total_counts]+k];
    6905:                 if (row != col) {
    6906:                   val = -constraints_data[constraints_data_ptr[total_counts]+k]/constraints_data[constraints_data_ptr[total_counts]];
    6907:                 } else {
    6908:                   val = constraints_data[constraints_data_ptr[total_counts]]/norm;
    6909:                 }
    6910:                 MatSetValue(localChangeOfBasisMatrix,row,col,val,INSERT_VALUES);
    6911:               }
    6912:             }
    6913:           }
    6914:           if (pcbddc->dbg_flag) {
    6915:             PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"\t-> using standard change of basis\n");
    6916:           }
    6917:         }
    6918:       } else {
    6919:         if (pcbddc->dbg_flag) {
    6920:           PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Constraint %D does not need a change of basis (size %D)\n",total_counts,size_of_constraint);
    6921:         }
    6922:       }
    6923:     }

    6925:     /* free workspace */
    6926:     if (qr_needed) {
    6927:       if (pcbddc->dbg_flag) {
    6928:         PetscFree(dbg_work);
    6929:       }
    6930:       PetscFree(trs_rhs);
    6931:       PetscFree(qr_tau);
    6932:       PetscFree(qr_work);
    6933:       PetscFree(gqr_work);
    6934:       PetscFree(qr_basis);
    6935:     }
    6936:     PetscBTDestroy(&is_primal);
    6937:     MatAssemblyBegin(localChangeOfBasisMatrix,MAT_FINAL_ASSEMBLY);
    6938:     MatAssemblyEnd(localChangeOfBasisMatrix,MAT_FINAL_ASSEMBLY);

    6940:     /* assembling of global change of variable */
    6941:     if (!pcbddc->fake_change) {
    6942:       Mat      tmat;
    6943:       PetscInt bs;

    6945:       VecGetSize(pcis->vec1_global,&global_size);
    6946:       VecGetLocalSize(pcis->vec1_global,&local_size);
    6947:       MatDuplicate(pc->pmat,MAT_DO_NOT_COPY_VALUES,&tmat);
    6948:       MatISSetLocalMat(tmat,localChangeOfBasisMatrix);
    6949:       MatAssemblyBegin(tmat,MAT_FINAL_ASSEMBLY);
    6950:       MatAssemblyEnd(tmat,MAT_FINAL_ASSEMBLY);
    6951:       MatCreate(PetscObjectComm((PetscObject)pc),&pcbddc->ChangeOfBasisMatrix);
    6952:       MatSetType(pcbddc->ChangeOfBasisMatrix,MATAIJ);
    6953:       MatGetBlockSize(pc->pmat,&bs);
    6954:       MatSetBlockSize(pcbddc->ChangeOfBasisMatrix,bs);
    6955:       MatSetSizes(pcbddc->ChangeOfBasisMatrix,local_size,local_size,global_size,global_size);
    6956:       MatISSetMPIXAIJPreallocation_Private(tmat,pcbddc->ChangeOfBasisMatrix,PETSC_TRUE);
    6957:       MatConvert(tmat,MATAIJ,MAT_REUSE_MATRIX,&pcbddc->ChangeOfBasisMatrix);
    6958:       MatDestroy(&tmat);
    6959:       VecSet(pcis->vec1_global,0.0);
    6960:       VecSet(pcis->vec1_N,1.0);
    6961:       VecScatterBegin(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
    6962:       VecScatterEnd(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
    6963:       VecReciprocal(pcis->vec1_global);
    6964:       MatDiagonalScale(pcbddc->ChangeOfBasisMatrix,pcis->vec1_global,NULL);

    6966:       /* check */
    6967:       if (pcbddc->dbg_flag) {
    6968:         PetscReal error;
    6969:         Vec       x,x_change;

    6971:         VecDuplicate(pcis->vec1_global,&x);
    6972:         VecDuplicate(pcis->vec1_global,&x_change);
    6973:         VecSetRandom(x,NULL);
    6974:         VecCopy(x,pcis->vec1_global);
    6975:         VecScatterBegin(matis->rctx,x,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD);
    6976:         VecScatterEnd(matis->rctx,x,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD);
    6977:         MatMult(localChangeOfBasisMatrix,pcis->vec1_N,pcis->vec2_N);
    6978:         VecScatterBegin(matis->rctx,pcis->vec2_N,x,INSERT_VALUES,SCATTER_REVERSE);
    6979:         VecScatterEnd(matis->rctx,pcis->vec2_N,x,INSERT_VALUES,SCATTER_REVERSE);
    6980:         MatMult(pcbddc->ChangeOfBasisMatrix,pcis->vec1_global,x_change);
    6981:         VecAXPY(x,-1.0,x_change);
    6982:         VecNorm(x,NORM_INFINITY,&error);
    6983:         if (error > PETSC_SMALL) {
    6984:           SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"Error global vs local change on N: %1.6e",error);
    6985:         }
    6986:         VecDestroy(&x);
    6987:         VecDestroy(&x_change);
    6988:       }
    6989:       /* adapt sub_schurs computed (if any) */
    6990:       if (pcbddc->use_deluxe_scaling) {
    6991:         PCBDDCSubSchurs sub_schurs=pcbddc->sub_schurs;

    6993:         if (pcbddc->use_change_of_basis && pcbddc->adaptive_userdefined) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cannot mix automatic change of basis, adaptive selection and user-defined constraints");
    6994:         if (sub_schurs && sub_schurs->S_Ej_all) {
    6995:           Mat                    S_new,tmat;
    6996:           IS                     is_all_N,is_V_Sall = NULL;

    6998:           ISLocalToGlobalMappingApplyIS(pcis->BtoNmap,sub_schurs->is_Ej_all,&is_all_N);
    6999:           MatCreateSubMatrix(localChangeOfBasisMatrix,is_all_N,is_all_N,MAT_INITIAL_MATRIX,&tmat);
    7000:           if (pcbddc->deluxe_zerorows) {
    7001:             ISLocalToGlobalMapping NtoSall;
    7002:             IS                     is_V;
    7003:             ISCreateGeneral(PETSC_COMM_SELF,pcbddc->n_vertices,pcbddc->local_primal_ref_node,PETSC_COPY_VALUES,&is_V);
    7004:             ISLocalToGlobalMappingCreateIS(is_all_N,&NtoSall);
    7005:             ISGlobalToLocalMappingApplyIS(NtoSall,IS_GTOLM_DROP,is_V,&is_V_Sall);
    7006:             ISLocalToGlobalMappingDestroy(&NtoSall);
    7007:             ISDestroy(&is_V);
    7008:           }
    7009:           ISDestroy(&is_all_N);
    7010:           MatPtAP(sub_schurs->S_Ej_all,tmat,MAT_INITIAL_MATRIX,1.0,&S_new);
    7011:           MatDestroy(&sub_schurs->S_Ej_all);
    7012:           PetscObjectReference((PetscObject)S_new);
    7013:           if (pcbddc->deluxe_zerorows) {
    7014:             const PetscScalar *array;
    7015:             const PetscInt    *idxs_V,*idxs_all;
    7016:             PetscInt          i,n_V;

    7018:             MatZeroRowsColumnsIS(S_new,is_V_Sall,1.,NULL,NULL);
    7019:             ISGetLocalSize(is_V_Sall,&n_V);
    7020:             ISGetIndices(is_V_Sall,&idxs_V);
    7021:             ISGetIndices(sub_schurs->is_Ej_all,&idxs_all);
    7022:             VecGetArrayRead(pcis->D,&array);
    7023:             for (i=0;i<n_V;i++) {
    7024:               PetscScalar val;
    7025:               PetscInt    idx;

    7027:               idx = idxs_V[i];
    7028:               val = array[idxs_all[idxs_V[i]]];
    7029:               MatSetValue(S_new,idx,idx,val,INSERT_VALUES);
    7030:             }
    7031:             MatAssemblyBegin(S_new,MAT_FINAL_ASSEMBLY);
    7032:             MatAssemblyEnd(S_new,MAT_FINAL_ASSEMBLY);
    7033:             VecRestoreArrayRead(pcis->D,&array);
    7034:             ISRestoreIndices(sub_schurs->is_Ej_all,&idxs_all);
    7035:             ISRestoreIndices(is_V_Sall,&idxs_V);
    7036:           }
    7037:           sub_schurs->S_Ej_all = S_new;
    7038:           MatDestroy(&S_new);
    7039:           if (sub_schurs->sum_S_Ej_all) {
    7040:             MatPtAP(sub_schurs->sum_S_Ej_all,tmat,MAT_INITIAL_MATRIX,1.0,&S_new);
    7041:             MatDestroy(&sub_schurs->sum_S_Ej_all);
    7042:             PetscObjectReference((PetscObject)S_new);
    7043:             if (pcbddc->deluxe_zerorows) {
    7044:               MatZeroRowsColumnsIS(S_new,is_V_Sall,1.,NULL,NULL);
    7045:             }
    7046:             sub_schurs->sum_S_Ej_all = S_new;
    7047:             MatDestroy(&S_new);
    7048:           }
    7049:           ISDestroy(&is_V_Sall);
    7050:           MatDestroy(&tmat);
    7051:         }
    7052:         /* destroy any change of basis context in sub_schurs */
    7053:         if (sub_schurs && sub_schurs->change) {
    7054:           PetscInt i;

    7056:           for (i=0;i<sub_schurs->n_subs;i++) {
    7057:             KSPDestroy(&sub_schurs->change[i]);
    7058:           }
    7059:           PetscFree(sub_schurs->change);
    7060:         }
    7061:       }
    7062:       if (pcbddc->switch_static) { /* need to save the local change */
    7063:         pcbddc->switch_static_change = localChangeOfBasisMatrix;
    7064:       } else {
    7065:         MatDestroy(&localChangeOfBasisMatrix);
    7066:       }
    7067:       /* determine if any process has changed the pressures locally */
    7068:       pcbddc->change_interior = pcbddc->benign_have_null;
    7069:     } else { /* fake change (get back change of basis into ConstraintMatrix and info on qr) */
    7070:       MatDestroy(&pcbddc->ConstraintMatrix);
    7071:       pcbddc->ConstraintMatrix = localChangeOfBasisMatrix;
    7072:       pcbddc->use_qr_single = qr_needed;
    7073:     }
    7074:   } else if (pcbddc->user_ChangeOfBasisMatrix || pcbddc->benign_saddle_point) {
    7075:     if (!pcbddc->benign_have_null && pcbddc->user_ChangeOfBasisMatrix) {
    7076:       PetscObjectReference((PetscObject)pcbddc->user_ChangeOfBasisMatrix);
    7077:       pcbddc->ChangeOfBasisMatrix = pcbddc->user_ChangeOfBasisMatrix;
    7078:     } else {
    7079:       Mat benign_global = NULL;
    7080:       if (pcbddc->benign_have_null) {
    7081:         Mat M;

    7083:         pcbddc->change_interior = PETSC_TRUE;
    7084:         VecCopy(matis->counter,pcis->vec1_N);
    7085:         VecReciprocal(pcis->vec1_N);
    7086:         MatDuplicate(pc->pmat,MAT_DO_NOT_COPY_VALUES,&benign_global);
    7087:         if (pcbddc->benign_change) {
    7088:           MatDuplicate(pcbddc->benign_change,MAT_COPY_VALUES,&M);
    7089:           MatDiagonalScale(M,pcis->vec1_N,NULL);
    7090:         } else {
    7091:           MatCreateSeqAIJ(PETSC_COMM_SELF,pcis->n,pcis->n,1,NULL,&M);
    7092:           MatDiagonalSet(M,pcis->vec1_N,INSERT_VALUES);
    7093:         }
    7094:         MatISSetLocalMat(benign_global,M);
    7095:         MatDestroy(&M);
    7096:         MatAssemblyBegin(benign_global,MAT_FINAL_ASSEMBLY);
    7097:         MatAssemblyEnd(benign_global,MAT_FINAL_ASSEMBLY);
    7098:       }
    7099:       if (pcbddc->user_ChangeOfBasisMatrix) {
    7100:         MatMatMult(pcbddc->user_ChangeOfBasisMatrix,benign_global,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&pcbddc->ChangeOfBasisMatrix);
    7101:         MatDestroy(&benign_global);
    7102:       } else if (pcbddc->benign_have_null) {
    7103:         pcbddc->ChangeOfBasisMatrix = benign_global;
    7104:       }
    7105:     }
    7106:     if (pcbddc->switch_static && pcbddc->ChangeOfBasisMatrix) { /* need to save the local change */
    7107:       IS             is_global;
    7108:       const PetscInt *gidxs;

    7110:       ISLocalToGlobalMappingGetIndices(pc->pmat->rmap->mapping,&gidxs);
    7111:       ISCreateGeneral(PetscObjectComm((PetscObject)pc),pcis->n,gidxs,PETSC_COPY_VALUES,&is_global);
    7112:       ISLocalToGlobalMappingRestoreIndices(pc->pmat->rmap->mapping,&gidxs);
    7113:       MatCreateSubMatrixUnsorted(pcbddc->ChangeOfBasisMatrix,is_global,is_global,&pcbddc->switch_static_change);
    7114:       ISDestroy(&is_global);
    7115:     }
    7116:   }
    7117:   if (!pcbddc->fake_change && pcbddc->ChangeOfBasisMatrix && !pcbddc->work_change) {
    7118:     VecDuplicate(pcis->vec1_global,&pcbddc->work_change);
    7119:   }

    7121:   if (!pcbddc->fake_change) {
    7122:     /* add pressure dofs to set of primal nodes for numbering purposes */
    7123:     for (i=0;i<pcbddc->benign_n;i++) {
    7124:       pcbddc->local_primal_ref_node[pcbddc->local_primal_size_cc] = pcbddc->benign_p0_lidx[i];
    7125:       pcbddc->primal_indices_local_idxs[pcbddc->local_primal_size] = pcbddc->benign_p0_lidx[i];
    7126:       pcbddc->local_primal_ref_mult[pcbddc->local_primal_size_cc] = 1;
    7127:       pcbddc->local_primal_size_cc++;
    7128:       pcbddc->local_primal_size++;
    7129:     }

    7131:     /* check if a new primal space has been introduced (also take into account benign trick) */
    7132:     pcbddc->new_primal_space_local = PETSC_TRUE;
    7133:     if (olocal_primal_size == pcbddc->local_primal_size) {
    7134:       PetscArraycmp(pcbddc->local_primal_ref_node,olocal_primal_ref_node,olocal_primal_size_cc,&pcbddc->new_primal_space_local);
    7135:       pcbddc->new_primal_space_local = (PetscBool)(!pcbddc->new_primal_space_local);
    7136:       if (!pcbddc->new_primal_space_local) {
    7137:         PetscArraycmp(pcbddc->local_primal_ref_mult,olocal_primal_ref_mult,olocal_primal_size_cc,&pcbddc->new_primal_space_local);
    7138:         pcbddc->new_primal_space_local = (PetscBool)(!pcbddc->new_primal_space_local);
    7139:       }
    7140:     }
    7141:     /* new_primal_space will be used for numbering of coarse dofs, so it should be the same across all subdomains */
    7142:     MPIU_Allreduce(&pcbddc->new_primal_space_local,&pcbddc->new_primal_space,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)pc));
    7143:   }
    7144:   PetscFree2(olocal_primal_ref_node,olocal_primal_ref_mult);

    7146:   /* flush dbg viewer */
    7147:   if (pcbddc->dbg_flag) {
    7148:     PetscViewerFlush(pcbddc->dbg_viewer);
    7149:   }

    7151:   /* free workspace */
    7152:   PetscBTDestroy(&qr_needed_idx);
    7153:   PetscBTDestroy(&change_basis);
    7154:   if (!pcbddc->adaptive_selection) {
    7155:     PetscFree3(constraints_idxs_ptr,constraints_data_ptr,constraints_n);
    7156:     PetscFree3(constraints_data,constraints_idxs,constraints_idxs_B);
    7157:   } else {
    7158:     PetscFree5(pcbddc->adaptive_constraints_n,
    7159:                       pcbddc->adaptive_constraints_idxs_ptr,
    7160:                       pcbddc->adaptive_constraints_data_ptr,
    7161:                       pcbddc->adaptive_constraints_idxs,
    7162:                       pcbddc->adaptive_constraints_data);
    7163:     PetscFree(constraints_n);
    7164:     PetscFree(constraints_idxs_B);
    7165:   }
    7166:   return(0);
    7167: }

    7169: PetscErrorCode PCBDDCAnalyzeInterface(PC pc)
    7170: {
    7171:   ISLocalToGlobalMapping map;
    7172:   PC_BDDC                *pcbddc = (PC_BDDC*)pc->data;
    7173:   Mat_IS                 *matis  = (Mat_IS*)pc->pmat->data;
    7174:   PetscInt               i,N;
    7175:   PetscBool              rcsr = PETSC_FALSE;
    7176:   PetscErrorCode         ierr;

    7179:   if (pcbddc->recompute_topography) {
    7180:     pcbddc->graphanalyzed = PETSC_FALSE;
    7181:     /* Reset previously computed graph */
    7182:     PCBDDCGraphReset(pcbddc->mat_graph);
    7183:     /* Init local Graph struct */
    7184:     MatGetSize(pc->pmat,&N,NULL);
    7185:     MatGetLocalToGlobalMapping(pc->pmat,&map,NULL);
    7186:     PCBDDCGraphInit(pcbddc->mat_graph,map,N,pcbddc->graphmaxcount);

    7188:     if (pcbddc->user_primal_vertices_local && !pcbddc->user_primal_vertices) {
    7189:       PCBDDCConsistencyCheckIS(pc,MPI_LOR,&pcbddc->user_primal_vertices_local);
    7190:     }
    7191:     /* Check validity of the csr graph passed in by the user */
    7192:     if (pcbddc->mat_graph->nvtxs_csr && pcbddc->mat_graph->nvtxs_csr != pcbddc->mat_graph->nvtxs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid size of local CSR graph! Found %D, expected %D",pcbddc->mat_graph->nvtxs_csr,pcbddc->mat_graph->nvtxs);

    7194:     /* Set default CSR adjacency of local dofs if not provided by the user with PCBDDCSetLocalAdjacencyGraph */
    7195:     if (!pcbddc->mat_graph->xadj && pcbddc->use_local_adj) {
    7196:       PetscInt  *xadj,*adjncy;
    7197:       PetscInt  nvtxs;
    7198:       PetscBool flg_row=PETSC_FALSE;

    7200:       MatGetRowIJ(matis->A,0,PETSC_TRUE,PETSC_FALSE,&nvtxs,(const PetscInt**)&xadj,(const PetscInt**)&adjncy,&flg_row);
    7201:       if (flg_row) {
    7202:         PCBDDCSetLocalAdjacencyGraph(pc,nvtxs,xadj,adjncy,PETSC_COPY_VALUES);
    7203:         pcbddc->computed_rowadj = PETSC_TRUE;
    7204:       }
    7205:       MatRestoreRowIJ(matis->A,0,PETSC_TRUE,PETSC_FALSE,&nvtxs,(const PetscInt**)&xadj,(const PetscInt**)&adjncy,&flg_row);
    7206:       rcsr = PETSC_TRUE;
    7207:     }
    7208:     if (pcbddc->dbg_flag) {
    7209:       PetscViewerFlush(pcbddc->dbg_viewer);
    7210:     }

    7212:     if (pcbddc->mat_graph->cdim && !pcbddc->mat_graph->cloc) {
    7213:       PetscReal    *lcoords;
    7214:       PetscInt     n;
    7215:       MPI_Datatype dimrealtype;

    7217:       /* TODO: support for blocked */
    7218:       if (pcbddc->mat_graph->cnloc != pc->pmat->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Invalid number of local coordinates! Got %D, expected %D",pcbddc->mat_graph->cnloc,pc->pmat->rmap->n);
    7219:       MatGetLocalSize(matis->A,&n,NULL);
    7220:       PetscMalloc1(pcbddc->mat_graph->cdim*n,&lcoords);
    7221:       MPI_Type_contiguous(pcbddc->mat_graph->cdim,MPIU_REAL,&dimrealtype);
    7222:       MPI_Type_commit(&dimrealtype);
    7223:       PetscSFBcastBegin(matis->sf,dimrealtype,pcbddc->mat_graph->coords,lcoords,MPI_REPLACE);
    7224:       PetscSFBcastEnd(matis->sf,dimrealtype,pcbddc->mat_graph->coords,lcoords,MPI_REPLACE);
    7225:       MPI_Type_free(&dimrealtype);
    7226:       PetscFree(pcbddc->mat_graph->coords);

    7228:       pcbddc->mat_graph->coords = lcoords;
    7229:       pcbddc->mat_graph->cloc   = PETSC_TRUE;
    7230:       pcbddc->mat_graph->cnloc  = n;
    7231:     }
    7232:     if (pcbddc->mat_graph->cnloc && pcbddc->mat_graph->cnloc != pcbddc->mat_graph->nvtxs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Invalid number of local subdomain coordinates! Got %D, expected %D",pcbddc->mat_graph->cnloc,pcbddc->mat_graph->nvtxs);
    7233:     pcbddc->mat_graph->active_coords = (PetscBool)(pcbddc->corner_selection && pcbddc->mat_graph->cdim && !pcbddc->corner_selected);

    7235:     /* Setup of Graph */
    7236:     pcbddc->mat_graph->commsizelimit = 0; /* don't use the COMM_SELF variant of the graph */
    7237:     PCBDDCGraphSetUp(pcbddc->mat_graph,pcbddc->vertex_size,pcbddc->NeumannBoundariesLocal,pcbddc->DirichletBoundariesLocal,pcbddc->n_ISForDofsLocal,pcbddc->ISForDofsLocal,pcbddc->user_primal_vertices_local);

    7239:     /* attach info on disconnected subdomains if present */
    7240:     if (pcbddc->n_local_subs) {
    7241:       PetscInt *local_subs,n,totn;

    7243:       MatGetLocalSize(matis->A,&n,NULL);
    7244:       PetscMalloc1(n,&local_subs);
    7245:       for (i=0;i<n;i++) local_subs[i] = pcbddc->n_local_subs;
    7246:       for (i=0;i<pcbddc->n_local_subs;i++) {
    7247:         const PetscInt *idxs;
    7248:         PetscInt       nl,j;

    7250:         ISGetLocalSize(pcbddc->local_subs[i],&nl);
    7251:         ISGetIndices(pcbddc->local_subs[i],&idxs);
    7252:         for (j=0;j<nl;j++) local_subs[idxs[j]] = i;
    7253:         ISRestoreIndices(pcbddc->local_subs[i],&idxs);
    7254:       }
    7255:       for (i=0,totn=0;i<n;i++) totn = PetscMax(totn,local_subs[i]);
    7256:       pcbddc->mat_graph->n_local_subs = totn + 1;
    7257:       pcbddc->mat_graph->local_subs = local_subs;
    7258:     }
    7259:   }

    7261:   if (!pcbddc->graphanalyzed) {
    7262:     /* Graph's connected components analysis */
    7263:     PCBDDCGraphComputeConnectedComponents(pcbddc->mat_graph);
    7264:     pcbddc->graphanalyzed = PETSC_TRUE;
    7265:     pcbddc->corner_selected = pcbddc->corner_selection;
    7266:   }
    7267:   if (rcsr) pcbddc->mat_graph->nvtxs_csr = 0;
    7268:   return(0);
    7269: }

    7271: PetscErrorCode PCBDDCOrthonormalizeVecs(PetscInt *nio, Vec vecs[])
    7272: {
    7273:   PetscInt       i,j,n;
    7274:   PetscScalar    *alphas;
    7275:   PetscReal      norm,*onorms;

    7279:   n = *nio;
    7280:   if (!n) return(0);
    7281:   PetscMalloc2(n,&alphas,n,&onorms);
    7282:   VecNormalize(vecs[0],&norm);
    7283:   if (norm < PETSC_SMALL) {
    7284:     onorms[0] = 0.0;
    7285:     VecSet(vecs[0],0.0);
    7286:   } else {
    7287:     onorms[0] = norm;
    7288:   }

    7290:   for (i=1;i<n;i++) {
    7291:     VecMDot(vecs[i],i,vecs,alphas);
    7292:     for (j=0;j<i;j++) alphas[j] = PetscConj(-alphas[j]);
    7293:     VecMAXPY(vecs[i],i,alphas,vecs);
    7294:     VecNormalize(vecs[i],&norm);
    7295:     if (norm < PETSC_SMALL) {
    7296:       onorms[i] = 0.0;
    7297:       VecSet(vecs[i],0.0);
    7298:     } else {
    7299:       onorms[i] = norm;
    7300:     }
    7301:   }
    7302:   /* push nonzero vectors at the beginning */
    7303:   for (i=0;i<n;i++) {
    7304:     if (onorms[i] == 0.0) {
    7305:       for (j=i+1;j<n;j++) {
    7306:         if (onorms[j] != 0.0) {
    7307:           VecCopy(vecs[j],vecs[i]);
    7308:           onorms[j] = 0.0;
    7309:         }
    7310:       }
    7311:     }
    7312:   }
    7313:   for (i=0,*nio=0;i<n;i++) *nio += onorms[i] != 0.0 ? 1 : 0;
    7314:   PetscFree2(alphas,onorms);
    7315:   return(0);
    7316: }

    7318: PetscErrorCode PCBDDCMatISGetSubassemblingPattern(Mat mat, PetscInt *n_subdomains, PetscInt redprocs, IS* is_sends, PetscBool *have_void)
    7319: {
    7320:   Mat            A;
    7321:   PetscInt       n_neighs,*neighs,*n_shared,**shared;
    7322:   PetscMPIInt    size,rank,color;
    7323:   PetscInt       *xadj,*adjncy;
    7324:   PetscInt       *adjncy_wgt,*v_wgt,*ranks_send_to_idx;
    7325:   PetscInt       im_active,active_procs,N,n,i,j,threshold = 2;
    7326:   PetscInt       void_procs,*procs_candidates = NULL;
    7327:   PetscInt       xadj_count,*count;
    7328:   PetscBool      ismatis,use_vwgt=PETSC_FALSE;
    7329:   PetscSubcomm   psubcomm;
    7330:   MPI_Comm       subcomm;

    7335:   PetscObjectTypeCompare((PetscObject)mat,MATIS,&ismatis);
    7336:   if (!ismatis) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot use %s on a matrix object which is not of type MATIS",PETSC_FUNCTION_NAME);
    7339:   if (*n_subdomains <=0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Invalid number of subdomains requested %D",*n_subdomains);

    7341:   if (have_void) *have_void = PETSC_FALSE;
    7342:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
    7343:   MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
    7344:   MatISGetLocalMat(mat,&A);
    7345:   MatGetLocalSize(A,&n,NULL);
    7346:   im_active = !!n;
    7347:   MPIU_Allreduce(&im_active,&active_procs,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)mat));
    7348:   void_procs = size - active_procs;
    7349:   /* get ranks of of non-active processes in mat communicator */
    7350:   if (void_procs) {
    7351:     PetscInt ncand;

    7353:     if (have_void) *have_void = PETSC_TRUE;
    7354:     PetscMalloc1(size,&procs_candidates);
    7355:     MPI_Allgather(&im_active,1,MPIU_INT,procs_candidates,1,MPIU_INT,PetscObjectComm((PetscObject)mat));
    7356:     for (i=0,ncand=0;i<size;i++) {
    7357:       if (!procs_candidates[i]) {
    7358:         procs_candidates[ncand++] = i;
    7359:       }
    7360:     }
    7361:     /* force n_subdomains to be not greater that the number of non-active processes */
    7362:     *n_subdomains = PetscMin(void_procs,*n_subdomains);
    7363:   }

    7365:   /* number of subdomains requested greater than active processes or matrix size -> just shift the matrix
    7366:      number of subdomains requested 1 -> send to rank-0 or first candidate in voids  */
    7367:   MatGetSize(mat,&N,NULL);
    7368:   if (active_procs < *n_subdomains || *n_subdomains == 1 || N <= *n_subdomains) {
    7369:     PetscInt issize,isidx,dest;
    7370:     if (*n_subdomains == 1) dest = 0;
    7371:     else dest = rank;
    7372:     if (im_active) {
    7373:       issize = 1;
    7374:       if (procs_candidates) { /* shift the pattern on non-active candidates (if any) */
    7375:         isidx = procs_candidates[dest];
    7376:       } else {
    7377:         isidx = dest;
    7378:       }
    7379:     } else {
    7380:       issize = 0;
    7381:       isidx = -1;
    7382:     }
    7383:     if (*n_subdomains != 1) *n_subdomains = active_procs;
    7384:     ISCreateGeneral(PetscObjectComm((PetscObject)mat),issize,&isidx,PETSC_COPY_VALUES,is_sends);
    7385:     PetscFree(procs_candidates);
    7386:     return(0);
    7387:   }
    7388:   PetscOptionsGetBool(NULL,NULL,"-matis_partitioning_use_vwgt",&use_vwgt,NULL);
    7389:   PetscOptionsGetInt(NULL,NULL,"-matis_partitioning_threshold",&threshold,NULL);
    7390:   threshold = PetscMax(threshold,2);

    7392:   /* Get info on mapping */
    7393:   ISLocalToGlobalMappingGetInfo(mat->rmap->mapping,&n_neighs,&neighs,&n_shared,&shared);

    7395:   /* build local CSR graph of subdomains' connectivity */
    7396:   PetscMalloc1(2,&xadj);
    7397:   xadj[0] = 0;
    7398:   xadj[1] = PetscMax(n_neighs-1,0);
    7399:   PetscMalloc1(xadj[1],&adjncy);
    7400:   PetscMalloc1(xadj[1],&adjncy_wgt);
    7401:   PetscCalloc1(n,&count);
    7402:   for (i=1;i<n_neighs;i++)
    7403:     for (j=0;j<n_shared[i];j++)
    7404:       count[shared[i][j]] += 1;

    7406:   xadj_count = 0;
    7407:   for (i=1;i<n_neighs;i++) {
    7408:     for (j=0;j<n_shared[i];j++) {
    7409:       if (count[shared[i][j]] < threshold) {
    7410:         adjncy[xadj_count] = neighs[i];
    7411:         adjncy_wgt[xadj_count] = n_shared[i];
    7412:         xadj_count++;
    7413:         break;
    7414:       }
    7415:     }
    7416:   }
    7417:   xadj[1] = xadj_count;
    7418:   PetscFree(count);
    7419:   ISLocalToGlobalMappingRestoreInfo(mat->rmap->mapping,&n_neighs,&neighs,&n_shared,&shared);
    7420:   PetscSortIntWithArray(xadj[1],adjncy,adjncy_wgt);

    7422:   PetscMalloc1(1,&ranks_send_to_idx);

    7424:   /* Restrict work on active processes only */
    7425:   PetscMPIIntCast(im_active,&color);
    7426:   if (void_procs) {
    7427:     PetscSubcommCreate(PetscObjectComm((PetscObject)mat),&psubcomm);
    7428:     PetscSubcommSetNumber(psubcomm,2); /* 2 groups, active process and not active processes */
    7429:     PetscSubcommSetTypeGeneral(psubcomm,color,rank);
    7430:     subcomm = PetscSubcommChild(psubcomm);
    7431:   } else {
    7432:     psubcomm = NULL;
    7433:     subcomm = PetscObjectComm((PetscObject)mat);
    7434:   }

    7436:   v_wgt = NULL;
    7437:   if (!color) {
    7438:     PetscFree(xadj);
    7439:     PetscFree(adjncy);
    7440:     PetscFree(adjncy_wgt);
    7441:   } else {
    7442:     Mat             subdomain_adj;
    7443:     IS              new_ranks,new_ranks_contig;
    7444:     MatPartitioning partitioner;
    7445:     PetscInt        rstart=0,rend=0;
    7446:     PetscInt        *is_indices,*oldranks;
    7447:     PetscMPIInt     size;
    7448:     PetscBool       aggregate;

    7450:     MPI_Comm_size(subcomm,&size);
    7451:     if (void_procs) {
    7452:       PetscInt prank = rank;
    7453:       PetscMalloc1(size,&oldranks);
    7454:       MPI_Allgather(&prank,1,MPIU_INT,oldranks,1,MPIU_INT,subcomm);
    7455:       for (i=0;i<xadj[1];i++) {
    7456:         PetscFindInt(adjncy[i],size,oldranks,&adjncy[i]);
    7457:       }
    7458:       PetscSortIntWithArray(xadj[1],adjncy,adjncy_wgt);
    7459:     } else {
    7460:       oldranks = NULL;
    7461:     }
    7462:     aggregate = ((redprocs > 0 && redprocs < size) ? PETSC_TRUE : PETSC_FALSE);
    7463:     if (aggregate) { /* TODO: all this part could be made more efficient */
    7464:       PetscInt    lrows,row,ncols,*cols;
    7465:       PetscMPIInt nrank;
    7466:       PetscScalar *vals;

    7468:       MPI_Comm_rank(subcomm,&nrank);
    7469:       lrows = 0;
    7470:       if (nrank<redprocs) {
    7471:         lrows = size/redprocs;
    7472:         if (nrank<size%redprocs) lrows++;
    7473:       }
    7474:       MatCreateAIJ(subcomm,lrows,lrows,size,size,50,NULL,50,NULL,&subdomain_adj);
    7475:       MatGetOwnershipRange(subdomain_adj,&rstart,&rend);
    7476:       MatSetOption(subdomain_adj,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
    7477:       MatSetOption(subdomain_adj,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
    7478:       row = nrank;
    7479:       ncols = xadj[1]-xadj[0];
    7480:       cols = adjncy;
    7481:       PetscMalloc1(ncols,&vals);
    7482:       for (i=0;i<ncols;i++) vals[i] = adjncy_wgt[i];
    7483:       MatSetValues(subdomain_adj,1,&row,ncols,cols,vals,INSERT_VALUES);
    7484:       MatAssemblyBegin(subdomain_adj,MAT_FINAL_ASSEMBLY);
    7485:       MatAssemblyEnd(subdomain_adj,MAT_FINAL_ASSEMBLY);
    7486:       PetscFree(xadj);
    7487:       PetscFree(adjncy);
    7488:       PetscFree(adjncy_wgt);
    7489:       PetscFree(vals);
    7490:       if (use_vwgt) {
    7491:         Vec               v;
    7492:         const PetscScalar *array;
    7493:         PetscInt          nl;

    7495:         MatCreateVecs(subdomain_adj,&v,NULL);
    7496:         VecSetValue(v,row,(PetscScalar)n,INSERT_VALUES);
    7497:         VecAssemblyBegin(v);
    7498:         VecAssemblyEnd(v);
    7499:         VecGetLocalSize(v,&nl);
    7500:         VecGetArrayRead(v,&array);
    7501:         PetscMalloc1(nl,&v_wgt);
    7502:         for (i=0;i<nl;i++) v_wgt[i] = (PetscInt)PetscRealPart(array[i]);
    7503:         VecRestoreArrayRead(v,&array);
    7504:         VecDestroy(&v);
    7505:       }
    7506:     } else {
    7507:       MatCreateMPIAdj(subcomm,1,(PetscInt)size,xadj,adjncy,adjncy_wgt,&subdomain_adj);
    7508:       if (use_vwgt) {
    7509:         PetscMalloc1(1,&v_wgt);
    7510:         v_wgt[0] = n;
    7511:       }
    7512:     }
    7513:     /* MatView(subdomain_adj,0); */

    7515:     /* Partition */
    7516:     MatPartitioningCreate(subcomm,&partitioner);
    7517: #if defined(PETSC_HAVE_PTSCOTCH)
    7518:     MatPartitioningSetType(partitioner,MATPARTITIONINGPTSCOTCH);
    7519: #elif defined(PETSC_HAVE_PARMETIS)
    7520:     MatPartitioningSetType(partitioner,MATPARTITIONINGPARMETIS);
    7521: #else
    7522:     MatPartitioningSetType(partitioner,MATPARTITIONINGAVERAGE);
    7523: #endif
    7524:     MatPartitioningSetAdjacency(partitioner,subdomain_adj);
    7525:     if (v_wgt) {
    7526:       MatPartitioningSetVertexWeights(partitioner,v_wgt);
    7527:     }
    7528:     *n_subdomains = PetscMin((PetscInt)size,*n_subdomains);
    7529:     MatPartitioningSetNParts(partitioner,*n_subdomains);
    7530:     MatPartitioningSetFromOptions(partitioner);
    7531:     MatPartitioningApply(partitioner,&new_ranks);
    7532:     /* MatPartitioningView(partitioner,0); */

    7534:     /* renumber new_ranks to avoid "holes" in new set of processors */
    7535:     ISRenumber(new_ranks,NULL,NULL,&new_ranks_contig);
    7536:     ISDestroy(&new_ranks);
    7537:     ISGetIndices(new_ranks_contig,(const PetscInt**)&is_indices);
    7538:     if (!aggregate) {
    7539:       if (procs_candidates) { /* shift the pattern on non-active candidates (if any) */
    7540:         if (PetscUnlikelyDebug(!oldranks)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"This should not happen");
    7541:         ranks_send_to_idx[0] = procs_candidates[oldranks[is_indices[0]]];
    7542:       } else if (oldranks) {
    7543:         ranks_send_to_idx[0] = oldranks[is_indices[0]];
    7544:       } else {
    7545:         ranks_send_to_idx[0] = is_indices[0];
    7546:       }
    7547:     } else {
    7548:       PetscInt    idx = 0;
    7549:       PetscMPIInt tag;
    7550:       MPI_Request *reqs;

    7552:       PetscObjectGetNewTag((PetscObject)subdomain_adj,&tag);
    7553:       PetscMalloc1(rend-rstart,&reqs);
    7554:       for (i=rstart;i<rend;i++) {
    7555:         MPI_Isend(is_indices+i-rstart,1,MPIU_INT,i,tag,subcomm,&reqs[i-rstart]);
    7556:       }
    7557:       MPI_Recv(&idx,1,MPIU_INT,MPI_ANY_SOURCE,tag,subcomm,MPI_STATUS_IGNORE);
    7558:       MPI_Waitall(rend-rstart,reqs,MPI_STATUSES_IGNORE);
    7559:       PetscFree(reqs);
    7560:       if (procs_candidates) { /* shift the pattern on non-active candidates (if any) */
    7561:         if (PetscUnlikelyDebug(!oldranks)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"This should not happen");
    7562:         ranks_send_to_idx[0] = procs_candidates[oldranks[idx]];
    7563:       } else if (oldranks) {
    7564:         ranks_send_to_idx[0] = oldranks[idx];
    7565:       } else {
    7566:         ranks_send_to_idx[0] = idx;
    7567:       }
    7568:     }
    7569:     ISRestoreIndices(new_ranks_contig,(const PetscInt**)&is_indices);
    7570:     /* clean up */
    7571:     PetscFree(oldranks);
    7572:     ISDestroy(&new_ranks_contig);
    7573:     MatDestroy(&subdomain_adj);
    7574:     MatPartitioningDestroy(&partitioner);
    7575:   }
    7576:   PetscSubcommDestroy(&psubcomm);
    7577:   PetscFree(procs_candidates);

    7579:   /* assemble parallel IS for sends */
    7580:   i = 1;
    7581:   if (!color) i=0;
    7582:   ISCreateGeneral(PetscObjectComm((PetscObject)mat),i,ranks_send_to_idx,PETSC_OWN_POINTER,is_sends);
    7583:   return(0);
    7584: }

    7586: typedef enum {MATDENSE_PRIVATE=0,MATAIJ_PRIVATE,MATBAIJ_PRIVATE,MATSBAIJ_PRIVATE}MatTypePrivate;

    7588: PetscErrorCode PCBDDCMatISSubassemble(Mat mat, IS is_sends, PetscInt n_subdomains, PetscBool restrict_comm, PetscBool restrict_full, PetscBool reuse, Mat *mat_n, PetscInt nis, IS isarray[], PetscInt nvecs, Vec nnsp_vec[])
    7589: {
    7590:   Mat                    local_mat;
    7591:   IS                     is_sends_internal;
    7592:   PetscInt               rows,cols,new_local_rows;
    7593:   PetscInt               i,bs,buf_size_idxs,buf_size_idxs_is,buf_size_vals,buf_size_vecs;
    7594:   PetscBool              ismatis,isdense,newisdense,destroy_mat;
    7595:   ISLocalToGlobalMapping l2gmap;
    7596:   PetscInt*              l2gmap_indices;
    7597:   const PetscInt*        is_indices;
    7598:   MatType                new_local_type;
    7599:   /* buffers */
    7600:   PetscInt               *ptr_idxs,*send_buffer_idxs,*recv_buffer_idxs;
    7601:   PetscInt               *ptr_idxs_is,*send_buffer_idxs_is,*recv_buffer_idxs_is;
    7602:   PetscInt               *recv_buffer_idxs_local;
    7603:   PetscScalar            *ptr_vals,*recv_buffer_vals;
    7604:   const PetscScalar      *send_buffer_vals;
    7605:   PetscScalar            *ptr_vecs,*send_buffer_vecs,*recv_buffer_vecs;
    7606:   /* MPI */
    7607:   MPI_Comm               comm,comm_n;
    7608:   PetscSubcomm           subcomm;
    7609:   PetscMPIInt            n_sends,n_recvs,size;
    7610:   PetscMPIInt            *iflags,*ilengths_idxs,*ilengths_vals,*ilengths_idxs_is;
    7611:   PetscMPIInt            *onodes,*onodes_is,*olengths_idxs,*olengths_idxs_is,*olengths_vals;
    7612:   PetscMPIInt            len,tag_idxs,tag_idxs_is,tag_vals,tag_vecs,source_dest;
    7613:   MPI_Request            *send_req_idxs,*send_req_idxs_is,*send_req_vals,*send_req_vecs;
    7614:   MPI_Request            *recv_req_idxs,*recv_req_idxs_is,*recv_req_vals,*recv_req_vecs;
    7615:   PetscErrorCode         ierr;

    7619:   PetscObjectTypeCompare((PetscObject)mat,MATIS,&ismatis);
    7620:   if (!ismatis) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot use %s on a matrix object which is not of type MATIS",PETSC_FUNCTION_NAME);
    7627:   if (nvecs) {
    7628:     if (nvecs > 1) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Just 1 vector supported");
    7630:   }
    7631:   /* further checks */
    7632:   MatISGetLocalMat(mat,&local_mat);
    7633:   PetscObjectTypeCompare((PetscObject)local_mat,MATSEQDENSE,&isdense);
    7634:   if (!isdense) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Currently cannot subassemble MATIS when local matrix type is not of type SEQDENSE");
    7635:   MatGetSize(local_mat,&rows,&cols);
    7636:   if (rows != cols) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Local MATIS matrices should be square");
    7637:   if (reuse && *mat_n) {
    7638:     PetscInt mrows,mcols,mnrows,mncols;
    7640:     PetscObjectTypeCompare((PetscObject)*mat_n,MATIS,&ismatis);
    7641:     if (!ismatis) SETERRQ(PetscObjectComm((PetscObject)*mat_n),PETSC_ERR_SUP,"Cannot reuse a matrix which is not of type MATIS");
    7642:     MatGetSize(mat,&mrows,&mcols);
    7643:     MatGetSize(*mat_n,&mnrows,&mncols);
    7644:     if (mrows != mnrows) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot reuse matrix! Wrong number of rows %D != %D",mrows,mnrows);
    7645:     if (mcols != mncols) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot reuse matrix! Wrong number of cols %D != %D",mcols,mncols);
    7646:   }
    7647:   MatGetBlockSize(local_mat,&bs);

    7650:   /* prepare IS for sending if not provided */
    7651:   if (!is_sends) {
    7652:     if (!n_subdomains) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"You should specify either an IS or a target number of subdomains");
    7653:     PCBDDCMatISGetSubassemblingPattern(mat,&n_subdomains,0,&is_sends_internal,NULL);
    7654:   } else {
    7655:     PetscObjectReference((PetscObject)is_sends);
    7656:     is_sends_internal = is_sends;
    7657:   }

    7659:   /* get comm */
    7660:   PetscObjectGetComm((PetscObject)mat,&comm);

    7662:   /* compute number of sends */
    7663:   ISGetLocalSize(is_sends_internal,&i);
    7664:   PetscMPIIntCast(i,&n_sends);

    7666:   /* compute number of receives */
    7667:   MPI_Comm_size(comm,&size);
    7668:   PetscMalloc1(size,&iflags);
    7669:   PetscArrayzero(iflags,size);
    7670:   ISGetIndices(is_sends_internal,&is_indices);
    7671:   for (i=0;i<n_sends;i++) iflags[is_indices[i]] = 1;
    7672:   PetscGatherNumberOfMessages(comm,iflags,NULL,&n_recvs);
    7673:   PetscFree(iflags);

    7675:   /* restrict comm if requested */
    7676:   subcomm = NULL;
    7677:   destroy_mat = PETSC_FALSE;
    7678:   if (restrict_comm) {
    7679:     PetscMPIInt color,subcommsize;

    7681:     color = 0;
    7682:     if (restrict_full) {
    7683:       if (!n_recvs) color = 1; /* processes not receiving anything will not partecipate in new comm (full restriction) */
    7684:     } else {
    7685:       if (!n_recvs && n_sends) color = 1; /* just those processes that are sending but not receiving anything will not partecipate in new comm */
    7686:     }
    7687:     MPIU_Allreduce(&color,&subcommsize,1,MPI_INT,MPI_SUM,comm);
    7688:     subcommsize = size - subcommsize;
    7689:     /* check if reuse has been requested */
    7690:     if (reuse) {
    7691:       if (*mat_n) {
    7692:         PetscMPIInt subcommsize2;
    7693:         MPI_Comm_size(PetscObjectComm((PetscObject)*mat_n),&subcommsize2);
    7694:         if (subcommsize != subcommsize2) SETERRQ2(PetscObjectComm((PetscObject)*mat_n),PETSC_ERR_PLIB,"Cannot reuse matrix! wrong subcomm size %d != %d",subcommsize,subcommsize2);
    7695:         comm_n = PetscObjectComm((PetscObject)*mat_n);
    7696:       } else {
    7697:         comm_n = PETSC_COMM_SELF;
    7698:       }
    7699:     } else { /* MAT_INITIAL_MATRIX */
    7700:       PetscMPIInt rank;

    7702:       MPI_Comm_rank(comm,&rank);
    7703:       PetscSubcommCreate(comm,&subcomm);
    7704:       PetscSubcommSetNumber(subcomm,2);
    7705:       PetscSubcommSetTypeGeneral(subcomm,color,rank);
    7706:       comm_n = PetscSubcommChild(subcomm);
    7707:     }
    7708:     /* flag to destroy *mat_n if not significative */
    7709:     if (color) destroy_mat = PETSC_TRUE;
    7710:   } else {
    7711:     comm_n = comm;
    7712:   }

    7714:   /* prepare send/receive buffers */
    7715:   PetscMalloc1(size,&ilengths_idxs);
    7716:   PetscArrayzero(ilengths_idxs,size);
    7717:   PetscMalloc1(size,&ilengths_vals);
    7718:   PetscArrayzero(ilengths_vals,size);
    7719:   if (nis) {
    7720:     PetscCalloc1(size,&ilengths_idxs_is);
    7721:   }

    7723:   /* Get data from local matrices */
    7724:   if (!isdense) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Subassembling of AIJ local matrices not yet implemented");
    7725:     /* TODO: See below some guidelines on how to prepare the local buffers */
    7726:     /*
    7727:        send_buffer_vals should contain the raw values of the local matrix
    7728:        send_buffer_idxs should contain:
    7729:        - MatType_PRIVATE type
    7730:        - PetscInt        size_of_l2gmap
    7731:        - PetscInt        global_row_indices[size_of_l2gmap]
    7732:        - PetscInt        all_other_info_which_is_needed_to_compute_preallocation_and_set_values
    7733:     */
    7734:   else {
    7735:     MatDenseGetArrayRead(local_mat,&send_buffer_vals);
    7736:     ISLocalToGlobalMappingGetSize(mat->rmap->mapping,&i);
    7737:     PetscMalloc1(i+2,&send_buffer_idxs);
    7738:     send_buffer_idxs[0] = (PetscInt)MATDENSE_PRIVATE;
    7739:     send_buffer_idxs[1] = i;
    7740:     ISLocalToGlobalMappingGetIndices(mat->rmap->mapping,(const PetscInt**)&ptr_idxs);
    7741:     PetscArraycpy(&send_buffer_idxs[2],ptr_idxs,i);
    7742:     ISLocalToGlobalMappingRestoreIndices(mat->rmap->mapping,(const PetscInt**)&ptr_idxs);
    7743:     PetscMPIIntCast(i,&len);
    7744:     for (i=0;i<n_sends;i++) {
    7745:       ilengths_vals[is_indices[i]] = len*len;
    7746:       ilengths_idxs[is_indices[i]] = len+2;
    7747:     }
    7748:   }
    7749:   PetscGatherMessageLengths2(comm,n_sends,n_recvs,ilengths_idxs,ilengths_vals,&onodes,&olengths_idxs,&olengths_vals);
    7750:   /* additional is (if any) */
    7751:   if (nis) {
    7752:     PetscMPIInt psum;
    7753:     PetscInt j;
    7754:     for (j=0,psum=0;j<nis;j++) {
    7755:       PetscInt plen;
    7756:       ISGetLocalSize(isarray[j],&plen);
    7757:       PetscMPIIntCast(plen,&len);
    7758:       psum += len+1; /* indices + lenght */
    7759:     }
    7760:     PetscMalloc1(psum,&send_buffer_idxs_is);
    7761:     for (j=0,psum=0;j<nis;j++) {
    7762:       PetscInt plen;
    7763:       const PetscInt *is_array_idxs;
    7764:       ISGetLocalSize(isarray[j],&plen);
    7765:       send_buffer_idxs_is[psum] = plen;
    7766:       ISGetIndices(isarray[j],&is_array_idxs);
    7767:       PetscArraycpy(&send_buffer_idxs_is[psum+1],is_array_idxs,plen);
    7768:       ISRestoreIndices(isarray[j],&is_array_idxs);
    7769:       psum += plen+1; /* indices + lenght */
    7770:     }
    7771:     for (i=0;i<n_sends;i++) {
    7772:       ilengths_idxs_is[is_indices[i]] = psum;
    7773:     }
    7774:     PetscGatherMessageLengths(comm,n_sends,n_recvs,ilengths_idxs_is,&onodes_is,&olengths_idxs_is);
    7775:   }
    7776:   MatISRestoreLocalMat(mat,&local_mat);

    7778:   buf_size_idxs = 0;
    7779:   buf_size_vals = 0;
    7780:   buf_size_idxs_is = 0;
    7781:   buf_size_vecs = 0;
    7782:   for (i=0;i<n_recvs;i++) {
    7783:     buf_size_idxs += (PetscInt)olengths_idxs[i];
    7784:     buf_size_vals += (PetscInt)olengths_vals[i];
    7785:     if (nis) buf_size_idxs_is += (PetscInt)olengths_idxs_is[i];
    7786:     if (nvecs) buf_size_vecs += (PetscInt)olengths_idxs[i];
    7787:   }
    7788:   PetscMalloc1(buf_size_idxs,&recv_buffer_idxs);
    7789:   PetscMalloc1(buf_size_vals,&recv_buffer_vals);
    7790:   PetscMalloc1(buf_size_idxs_is,&recv_buffer_idxs_is);
    7791:   PetscMalloc1(buf_size_vecs,&recv_buffer_vecs);

    7793:   /* get new tags for clean communications */
    7794:   PetscObjectGetNewTag((PetscObject)mat,&tag_idxs);
    7795:   PetscObjectGetNewTag((PetscObject)mat,&tag_vals);
    7796:   PetscObjectGetNewTag((PetscObject)mat,&tag_idxs_is);
    7797:   PetscObjectGetNewTag((PetscObject)mat,&tag_vecs);

    7799:   /* allocate for requests */
    7800:   PetscMalloc1(n_sends,&send_req_idxs);
    7801:   PetscMalloc1(n_sends,&send_req_vals);
    7802:   PetscMalloc1(n_sends,&send_req_idxs_is);
    7803:   PetscMalloc1(n_sends,&send_req_vecs);
    7804:   PetscMalloc1(n_recvs,&recv_req_idxs);
    7805:   PetscMalloc1(n_recvs,&recv_req_vals);
    7806:   PetscMalloc1(n_recvs,&recv_req_idxs_is);
    7807:   PetscMalloc1(n_recvs,&recv_req_vecs);

    7809:   /* communications */
    7810:   ptr_idxs = recv_buffer_idxs;
    7811:   ptr_vals = recv_buffer_vals;
    7812:   ptr_idxs_is = recv_buffer_idxs_is;
    7813:   ptr_vecs = recv_buffer_vecs;
    7814:   for (i=0;i<n_recvs;i++) {
    7815:     source_dest = onodes[i];
    7816:     MPI_Irecv(ptr_idxs,olengths_idxs[i],MPIU_INT,source_dest,tag_idxs,comm,&recv_req_idxs[i]);
    7817:     MPI_Irecv(ptr_vals,olengths_vals[i],MPIU_SCALAR,source_dest,tag_vals,comm,&recv_req_vals[i]);
    7818:     ptr_idxs += olengths_idxs[i];
    7819:     ptr_vals += olengths_vals[i];
    7820:     if (nis) {
    7821:       source_dest = onodes_is[i];
    7822:       MPI_Irecv(ptr_idxs_is,olengths_idxs_is[i],MPIU_INT,source_dest,tag_idxs_is,comm,&recv_req_idxs_is[i]);
    7823:       ptr_idxs_is += olengths_idxs_is[i];
    7824:     }
    7825:     if (nvecs) {
    7826:       source_dest = onodes[i];
    7827:       MPI_Irecv(ptr_vecs,olengths_idxs[i]-2,MPIU_SCALAR,source_dest,tag_vecs,comm,&recv_req_vecs[i]);
    7828:       ptr_vecs += olengths_idxs[i]-2;
    7829:     }
    7830:   }
    7831:   for (i=0;i<n_sends;i++) {
    7832:     PetscMPIIntCast(is_indices[i],&source_dest);
    7833:     MPI_Isend(send_buffer_idxs,ilengths_idxs[source_dest],MPIU_INT,source_dest,tag_idxs,comm,&send_req_idxs[i]);
    7834:     MPI_Isend((PetscScalar*)send_buffer_vals,ilengths_vals[source_dest],MPIU_SCALAR,source_dest,tag_vals,comm,&send_req_vals[i]);
    7835:     if (nis) {
    7836:       MPI_Isend(send_buffer_idxs_is,ilengths_idxs_is[source_dest],MPIU_INT,source_dest,tag_idxs_is,comm,&send_req_idxs_is[i]);
    7837:     }
    7838:     if (nvecs) {
    7839:       VecGetArray(nnsp_vec[0],&send_buffer_vecs);
    7840:       MPI_Isend(send_buffer_vecs,ilengths_idxs[source_dest]-2,MPIU_SCALAR,source_dest,tag_vecs,comm,&send_req_vecs[i]);
    7841:     }
    7842:   }
    7843:   ISRestoreIndices(is_sends_internal,&is_indices);
    7844:   ISDestroy(&is_sends_internal);

    7846:   /* assemble new l2g map */
    7847:   MPI_Waitall(n_recvs,recv_req_idxs,MPI_STATUSES_IGNORE);
    7848:   ptr_idxs = recv_buffer_idxs;
    7849:   new_local_rows = 0;
    7850:   for (i=0;i<n_recvs;i++) {
    7851:     new_local_rows += *(ptr_idxs+1); /* second element is the local size of the l2gmap */
    7852:     ptr_idxs += olengths_idxs[i];
    7853:   }
    7854:   PetscMalloc1(new_local_rows,&l2gmap_indices);
    7855:   ptr_idxs = recv_buffer_idxs;
    7856:   new_local_rows = 0;
    7857:   for (i=0;i<n_recvs;i++) {
    7858:     PetscArraycpy(&l2gmap_indices[new_local_rows],ptr_idxs+2,*(ptr_idxs+1));
    7859:     new_local_rows += *(ptr_idxs+1); /* second element is the local size of the l2gmap */
    7860:     ptr_idxs += olengths_idxs[i];
    7861:   }
    7862:   PetscSortRemoveDupsInt(&new_local_rows,l2gmap_indices);
    7863:   ISLocalToGlobalMappingCreate(comm_n,1,new_local_rows,l2gmap_indices,PETSC_COPY_VALUES,&l2gmap);
    7864:   PetscFree(l2gmap_indices);

    7866:   /* infer new local matrix type from received local matrices type */
    7867:   /* currently if all local matrices are of type X, then the resulting matrix will be of type X, except for the dense case */
    7868:   /* it also assumes that if the block size is set, than it is the same among all local matrices (see checks at the beginning of the function) */
    7869:   if (n_recvs) {
    7870:     MatTypePrivate new_local_type_private = (MatTypePrivate)send_buffer_idxs[0];
    7871:     ptr_idxs = recv_buffer_idxs;
    7872:     for (i=0;i<n_recvs;i++) {
    7873:       if ((PetscInt)new_local_type_private != *ptr_idxs) {
    7874:         new_local_type_private = MATAIJ_PRIVATE;
    7875:         break;
    7876:       }
    7877:       ptr_idxs += olengths_idxs[i];
    7878:     }
    7879:     switch (new_local_type_private) {
    7880:       case MATDENSE_PRIVATE:
    7881:         new_local_type = MATSEQAIJ;
    7882:         bs = 1;
    7883:         break;
    7884:       case MATAIJ_PRIVATE:
    7885:         new_local_type = MATSEQAIJ;
    7886:         bs = 1;
    7887:         break;
    7888:       case MATBAIJ_PRIVATE:
    7889:         new_local_type = MATSEQBAIJ;
    7890:         break;
    7891:       case MATSBAIJ_PRIVATE:
    7892:         new_local_type = MATSEQSBAIJ;
    7893:         break;
    7894:       default:
    7895:         SETERRQ2(comm,PETSC_ERR_SUP,"Unsupported private type %d in %s",new_local_type_private,PETSC_FUNCTION_NAME);
    7896:     }
    7897:   } else { /* by default, new_local_type is seqaij */
    7898:     new_local_type = MATSEQAIJ;
    7899:     bs = 1;
    7900:   }

    7902:   /* create MATIS object if needed */
    7903:   if (!reuse) {
    7904:     MatGetSize(mat,&rows,&cols);
    7905:     MatCreateIS(comm_n,bs,PETSC_DECIDE,PETSC_DECIDE,rows,cols,l2gmap,NULL,mat_n);
    7906:   } else {
    7907:     /* it also destroys the local matrices */
    7908:     if (*mat_n) {
    7909:       MatSetLocalToGlobalMapping(*mat_n,l2gmap,l2gmap);
    7910:     } else { /* this is a fake object */
    7911:       MatCreateIS(comm_n,bs,PETSC_DECIDE,PETSC_DECIDE,rows,cols,l2gmap,NULL,mat_n);
    7912:     }
    7913:   }
    7914:   MatISGetLocalMat(*mat_n,&local_mat);
    7915:   MatSetType(local_mat,new_local_type);

    7917:   MPI_Waitall(n_recvs,recv_req_vals,MPI_STATUSES_IGNORE);

    7919:   /* Global to local map of received indices */
    7920:   PetscMalloc1(buf_size_idxs,&recv_buffer_idxs_local); /* needed for values insertion */
    7921:   ISGlobalToLocalMappingApply(l2gmap,IS_GTOLM_MASK,buf_size_idxs,recv_buffer_idxs,&i,recv_buffer_idxs_local);
    7922:   ISLocalToGlobalMappingDestroy(&l2gmap);

    7924:   /* restore attributes -> type of incoming data and its size */
    7925:   buf_size_idxs = 0;
    7926:   for (i=0;i<n_recvs;i++) {
    7927:     recv_buffer_idxs_local[buf_size_idxs] = recv_buffer_idxs[buf_size_idxs];
    7928:     recv_buffer_idxs_local[buf_size_idxs+1] = recv_buffer_idxs[buf_size_idxs+1];
    7929:     buf_size_idxs += (PetscInt)olengths_idxs[i];
    7930:   }
    7931:   PetscFree(recv_buffer_idxs);

    7933:   /* set preallocation */
    7934:   PetscObjectTypeCompare((PetscObject)local_mat,MATSEQDENSE,&newisdense);
    7935:   if (!newisdense) {
    7936:     PetscInt *new_local_nnz=NULL;

    7938:     ptr_idxs = recv_buffer_idxs_local;
    7939:     if (n_recvs) {
    7940:       PetscCalloc1(new_local_rows,&new_local_nnz);
    7941:     }
    7942:     for (i=0;i<n_recvs;i++) {
    7943:       PetscInt j;
    7944:       if (*ptr_idxs == (PetscInt)MATDENSE_PRIVATE) { /* preallocation provided for dense case only */
    7945:         for (j=0;j<*(ptr_idxs+1);j++) {
    7946:           new_local_nnz[*(ptr_idxs+2+j)] += *(ptr_idxs+1);
    7947:         }
    7948:       } else {
    7949:         /* TODO */
    7950:       }
    7951:       ptr_idxs += olengths_idxs[i];
    7952:     }
    7953:     if (new_local_nnz) {
    7954:       for (i=0;i<new_local_rows;i++) new_local_nnz[i] = PetscMin(new_local_nnz[i],new_local_rows);
    7955:       MatSeqAIJSetPreallocation(local_mat,0,new_local_nnz);
    7956:       for (i=0;i<new_local_rows;i++) new_local_nnz[i] /= bs;
    7957:       MatSeqBAIJSetPreallocation(local_mat,bs,0,new_local_nnz);
    7958:       for (i=0;i<new_local_rows;i++) new_local_nnz[i] = PetscMax(new_local_nnz[i]-i,0);
    7959:       MatSeqSBAIJSetPreallocation(local_mat,bs,0,new_local_nnz);
    7960:     } else {
    7961:       MatSetUp(local_mat);
    7962:     }
    7963:     PetscFree(new_local_nnz);
    7964:   } else {
    7965:     MatSetUp(local_mat);
    7966:   }

    7968:   /* set values */
    7969:   ptr_vals = recv_buffer_vals;
    7970:   ptr_idxs = recv_buffer_idxs_local;
    7971:   for (i=0;i<n_recvs;i++) {
    7972:     if (*ptr_idxs == (PetscInt)MATDENSE_PRIVATE) { /* values insertion provided for dense case only */
    7973:       MatSetOption(local_mat,MAT_ROW_ORIENTED,PETSC_FALSE);
    7974:       MatSetValues(local_mat,*(ptr_idxs+1),ptr_idxs+2,*(ptr_idxs+1),ptr_idxs+2,ptr_vals,ADD_VALUES);
    7975:       MatAssemblyBegin(local_mat,MAT_FLUSH_ASSEMBLY);
    7976:       MatAssemblyEnd(local_mat,MAT_FLUSH_ASSEMBLY);
    7977:       MatSetOption(local_mat,MAT_ROW_ORIENTED,PETSC_TRUE);
    7978:     } else {
    7979:       /* TODO */
    7980:     }
    7981:     ptr_idxs += olengths_idxs[i];
    7982:     ptr_vals += olengths_vals[i];
    7983:   }
    7984:   MatAssemblyBegin(local_mat,MAT_FINAL_ASSEMBLY);
    7985:   MatAssemblyEnd(local_mat,MAT_FINAL_ASSEMBLY);
    7986:   MatISRestoreLocalMat(*mat_n,&local_mat);
    7987:   MatAssemblyBegin(*mat_n,MAT_FINAL_ASSEMBLY);
    7988:   MatAssemblyEnd(*mat_n,MAT_FINAL_ASSEMBLY);
    7989:   PetscFree(recv_buffer_vals);

    7991: #if 0
    7992:   if (!restrict_comm) { /* check */
    7993:     Vec       lvec,rvec;
    7994:     PetscReal infty_error;

    7996:     MatCreateVecs(mat,&rvec,&lvec);
    7997:     VecSetRandom(rvec,NULL);
    7998:     MatMult(mat,rvec,lvec);
    7999:     VecScale(lvec,-1.0);
    8000:     MatMultAdd(*mat_n,rvec,lvec,lvec);
    8001:     VecNorm(lvec,NORM_INFINITY,&infty_error);
    8002:     PetscPrintf(PetscObjectComm((PetscObject)mat),"Infinity error subassembling %1.6e\n",infty_error);
    8003:     VecDestroy(&rvec);
    8004:     VecDestroy(&lvec);
    8005:   }
    8006: #endif

    8008:   /* assemble new additional is (if any) */
    8009:   if (nis) {
    8010:     PetscInt **temp_idxs,*count_is,j,psum;

    8012:     MPI_Waitall(n_recvs,recv_req_idxs_is,MPI_STATUSES_IGNORE);
    8013:     PetscCalloc1(nis,&count_is);
    8014:     ptr_idxs = recv_buffer_idxs_is;
    8015:     psum = 0;
    8016:     for (i=0;i<n_recvs;i++) {
    8017:       for (j=0;j<nis;j++) {
    8018:         PetscInt plen = *(ptr_idxs); /* first element is the local size of IS's indices */
    8019:         count_is[j] += plen; /* increment counting of buffer for j-th IS */
    8020:         psum += plen;
    8021:         ptr_idxs += plen+1; /* shift pointer to received data */
    8022:       }
    8023:     }
    8024:     PetscMalloc1(nis,&temp_idxs);
    8025:     PetscMalloc1(psum,&temp_idxs[0]);
    8026:     for (i=1;i<nis;i++) {
    8027:       temp_idxs[i] = temp_idxs[i-1]+count_is[i-1];
    8028:     }
    8029:     PetscArrayzero(count_is,nis);
    8030:     ptr_idxs = recv_buffer_idxs_is;
    8031:     for (i=0;i<n_recvs;i++) {
    8032:       for (j=0;j<nis;j++) {
    8033:         PetscInt plen = *(ptr_idxs); /* first element is the local size of IS's indices */
    8034:         PetscArraycpy(&temp_idxs[j][count_is[j]],ptr_idxs+1,plen);
    8035:         count_is[j] += plen; /* increment starting point of buffer for j-th IS */
    8036:         ptr_idxs += plen+1; /* shift pointer to received data */
    8037:       }
    8038:     }
    8039:     for (i=0;i<nis;i++) {
    8040:       ISDestroy(&isarray[i]);
    8041:       PetscSortRemoveDupsInt(&count_is[i],temp_idxs[i]);
    8042:       ISCreateGeneral(comm_n,count_is[i],temp_idxs[i],PETSC_COPY_VALUES,&isarray[i]);
    8043:     }
    8044:     PetscFree(count_is);
    8045:     PetscFree(temp_idxs[0]);
    8046:     PetscFree(temp_idxs);
    8047:   }
    8048:   /* free workspace */
    8049:   PetscFree(recv_buffer_idxs_is);
    8050:   MPI_Waitall(n_sends,send_req_idxs,MPI_STATUSES_IGNORE);
    8051:   PetscFree(send_buffer_idxs);
    8052:   MPI_Waitall(n_sends,send_req_vals,MPI_STATUSES_IGNORE);
    8053:   if (isdense) {
    8054:     MatISGetLocalMat(mat,&local_mat);
    8055:     MatDenseRestoreArrayRead(local_mat,&send_buffer_vals);
    8056:     MatISRestoreLocalMat(mat,&local_mat);
    8057:   } else {
    8058:     /* PetscFree(send_buffer_vals); */
    8059:   }
    8060:   if (nis) {
    8061:     MPI_Waitall(n_sends,send_req_idxs_is,MPI_STATUSES_IGNORE);
    8062:     PetscFree(send_buffer_idxs_is);
    8063:   }

    8065:   if (nvecs) {
    8066:     MPI_Waitall(n_recvs,recv_req_vecs,MPI_STATUSES_IGNORE);
    8067:     MPI_Waitall(n_sends,send_req_vecs,MPI_STATUSES_IGNORE);
    8068:     VecRestoreArray(nnsp_vec[0],&send_buffer_vecs);
    8069:     VecDestroy(&nnsp_vec[0]);
    8070:     VecCreate(comm_n,&nnsp_vec[0]);
    8071:     VecSetSizes(nnsp_vec[0],new_local_rows,PETSC_DECIDE);
    8072:     VecSetType(nnsp_vec[0],VECSTANDARD);
    8073:     /* set values */
    8074:     ptr_vals = recv_buffer_vecs;
    8075:     ptr_idxs = recv_buffer_idxs_local;
    8076:     VecGetArray(nnsp_vec[0],&send_buffer_vecs);
    8077:     for (i=0;i<n_recvs;i++) {
    8078:       PetscInt j;
    8079:       for (j=0;j<*(ptr_idxs+1);j++) {
    8080:         send_buffer_vecs[*(ptr_idxs+2+j)] += *(ptr_vals + j);
    8081:       }
    8082:       ptr_idxs += olengths_idxs[i];
    8083:       ptr_vals += olengths_idxs[i]-2;
    8084:     }
    8085:     VecRestoreArray(nnsp_vec[0],&send_buffer_vecs);
    8086:     VecAssemblyBegin(nnsp_vec[0]);
    8087:     VecAssemblyEnd(nnsp_vec[0]);
    8088:   }

    8090:   PetscFree(recv_buffer_vecs);
    8091:   PetscFree(recv_buffer_idxs_local);
    8092:   PetscFree(recv_req_idxs);
    8093:   PetscFree(recv_req_vals);
    8094:   PetscFree(recv_req_vecs);
    8095:   PetscFree(recv_req_idxs_is);
    8096:   PetscFree(send_req_idxs);
    8097:   PetscFree(send_req_vals);
    8098:   PetscFree(send_req_vecs);
    8099:   PetscFree(send_req_idxs_is);
    8100:   PetscFree(ilengths_vals);
    8101:   PetscFree(ilengths_idxs);
    8102:   PetscFree(olengths_vals);
    8103:   PetscFree(olengths_idxs);
    8104:   PetscFree(onodes);
    8105:   if (nis) {
    8106:     PetscFree(ilengths_idxs_is);
    8107:     PetscFree(olengths_idxs_is);
    8108:     PetscFree(onodes_is);
    8109:   }
    8110:   PetscSubcommDestroy(&subcomm);
    8111:   if (destroy_mat) { /* destroy mat is true only if restrict comm is true and process will not partecipate */
    8112:     MatDestroy(mat_n);
    8113:     for (i=0;i<nis;i++) {
    8114:       ISDestroy(&isarray[i]);
    8115:     }
    8116:     if (nvecs) { /* need to match VecDestroy nnsp_vec called in the other code path */
    8117:       VecDestroy(&nnsp_vec[0]);
    8118:     }
    8119:     *mat_n = NULL;
    8120:   }
    8121:   return(0);
    8122: }

    8124: /* temporary hack into ksp private data structure */
    8125: #include <petsc/private/kspimpl.h>

    8127: PetscErrorCode PCBDDCSetUpCoarseSolver(PC pc,PetscScalar* coarse_submat_vals)
    8128: {
    8129:   PC_BDDC                *pcbddc = (PC_BDDC*)pc->data;
    8130:   PC_IS                  *pcis = (PC_IS*)pc->data;
    8131:   Mat                    coarse_mat,coarse_mat_is,coarse_submat_dense;
    8132:   Mat                    coarsedivudotp = NULL;
    8133:   Mat                    coarseG,t_coarse_mat_is;
    8134:   MatNullSpace           CoarseNullSpace = NULL;
    8135:   ISLocalToGlobalMapping coarse_islg;
    8136:   IS                     coarse_is,*isarray,corners;
    8137:   PetscInt               i,im_active=-1,active_procs=-1;
    8138:   PetscInt               nis,nisdofs,nisneu,nisvert;
    8139:   PetscInt               coarse_eqs_per_proc;
    8140:   PC                     pc_temp;
    8141:   PCType                 coarse_pc_type;
    8142:   KSPType                coarse_ksp_type;
    8143:   PetscBool              multilevel_requested,multilevel_allowed;
    8144:   PetscBool              coarse_reuse;
    8145:   PetscInt               ncoarse,nedcfield;
    8146:   PetscBool              compute_vecs = PETSC_FALSE;
    8147:   PetscScalar            *array;
    8148:   MatReuse               coarse_mat_reuse;
    8149:   PetscBool              restr, full_restr, have_void;
    8150:   PetscMPIInt            size;
    8151:   PetscErrorCode         ierr;

    8154:   PetscLogEventBegin(PC_BDDC_CoarseSetUp[pcbddc->current_level],pc,0,0,0);
    8155:   /* Assign global numbering to coarse dofs */
    8156:   if (pcbddc->new_primal_space || pcbddc->coarse_size == -1) { /* a new primal space is present or it is the first initialization, so recompute global numbering */
    8157:     PetscInt ocoarse_size;
    8158:     compute_vecs = PETSC_TRUE;

    8160:     pcbddc->new_primal_space = PETSC_TRUE;
    8161:     ocoarse_size = pcbddc->coarse_size;
    8162:     PetscFree(pcbddc->global_primal_indices);
    8163:     PCBDDCComputePrimalNumbering(pc,&pcbddc->coarse_size,&pcbddc->global_primal_indices);
    8164:     /* see if we can avoid some work */
    8165:     if (pcbddc->coarse_ksp) { /* coarse ksp has already been created */
    8166:       /* if the coarse size is different or we are using adaptive selection, better to not reuse the coarse matrix */
    8167:       if (ocoarse_size != pcbddc->coarse_size || pcbddc->adaptive_selection) {
    8168:         KSPReset(pcbddc->coarse_ksp);
    8169:         coarse_reuse = PETSC_FALSE;
    8170:       } else { /* we can safely reuse already computed coarse matrix */
    8171:         coarse_reuse = PETSC_TRUE;
    8172:       }
    8173:     } else { /* there's no coarse ksp, so we need to create the coarse matrix too */
    8174:       coarse_reuse = PETSC_FALSE;
    8175:     }
    8176:     /* reset any subassembling information */
    8177:     if (!coarse_reuse || pcbddc->recompute_topography) {
    8178:       ISDestroy(&pcbddc->coarse_subassembling);
    8179:     }
    8180:   } else { /* primal space is unchanged, so we can reuse coarse matrix */
    8181:     coarse_reuse = PETSC_TRUE;
    8182:   }
    8183:   if (coarse_reuse && pcbddc->coarse_ksp) {
    8184:     KSPGetOperators(pcbddc->coarse_ksp,&coarse_mat,NULL);
    8185:     PetscObjectReference((PetscObject)coarse_mat);
    8186:     coarse_mat_reuse = MAT_REUSE_MATRIX;
    8187:   } else {
    8188:     coarse_mat = NULL;
    8189:     coarse_mat_reuse = MAT_INITIAL_MATRIX;
    8190:   }

    8192:   /* creates temporary l2gmap and IS for coarse indexes */
    8193:   ISCreateGeneral(PetscObjectComm((PetscObject)pc),pcbddc->local_primal_size,pcbddc->global_primal_indices,PETSC_COPY_VALUES,&coarse_is);
    8194:   ISLocalToGlobalMappingCreateIS(coarse_is,&coarse_islg);

    8196:   /* creates temporary MATIS object for coarse matrix */
    8197:   MatCreateSeqDense(PETSC_COMM_SELF,pcbddc->local_primal_size,pcbddc->local_primal_size,coarse_submat_vals,&coarse_submat_dense);
    8198:   MatCreateIS(PetscObjectComm((PetscObject)pc),1,PETSC_DECIDE,PETSC_DECIDE,pcbddc->coarse_size,pcbddc->coarse_size,coarse_islg,NULL,&t_coarse_mat_is);
    8199:   MatISSetLocalMat(t_coarse_mat_is,coarse_submat_dense);
    8200:   MatAssemblyBegin(t_coarse_mat_is,MAT_FINAL_ASSEMBLY);
    8201:   MatAssemblyEnd(t_coarse_mat_is,MAT_FINAL_ASSEMBLY);
    8202:   MatDestroy(&coarse_submat_dense);

    8204:   /* count "active" (i.e. with positive local size) and "void" processes */
    8205:   im_active = !!(pcis->n);
    8206:   MPIU_Allreduce(&im_active,&active_procs,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)pc));

    8208:   /* determine number of processes partecipating to coarse solver and compute subassembling pattern */
    8209:   /* restr : whether we want to exclude senders (which are not receivers) from the subassembling pattern */
    8210:   /* full_restr : just use the receivers from the subassembling pattern */
    8211:   MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);
    8212:   coarse_mat_is        = NULL;
    8213:   multilevel_allowed   = PETSC_FALSE;
    8214:   multilevel_requested = PETSC_FALSE;
    8215:   coarse_eqs_per_proc  = PetscMin(PetscMax(pcbddc->coarse_size,1),pcbddc->coarse_eqs_per_proc);
    8216:   if (coarse_eqs_per_proc < 0) coarse_eqs_per_proc = pcbddc->coarse_size;
    8217:   if (pcbddc->current_level < pcbddc->max_levels) multilevel_requested = PETSC_TRUE;
    8218:   if (pcbddc->coarse_size <= pcbddc->coarse_eqs_limit) multilevel_requested = PETSC_FALSE;
    8219:   if (multilevel_requested) {
    8220:     ncoarse    = active_procs/pcbddc->coarsening_ratio;
    8221:     restr      = PETSC_FALSE;
    8222:     full_restr = PETSC_FALSE;
    8223:   } else {
    8224:     ncoarse    = pcbddc->coarse_size/coarse_eqs_per_proc + !!(pcbddc->coarse_size%coarse_eqs_per_proc);
    8225:     restr      = PETSC_TRUE;
    8226:     full_restr = PETSC_TRUE;
    8227:   }
    8228:   if (!pcbddc->coarse_size || size == 1) multilevel_allowed = multilevel_requested = restr = full_restr = PETSC_FALSE;
    8229:   ncoarse = PetscMax(1,ncoarse);
    8230:   if (!pcbddc->coarse_subassembling) {
    8231:     if (pcbddc->coarsening_ratio > 1) {
    8232:       if (multilevel_requested) {
    8233:         PCBDDCMatISGetSubassemblingPattern(pc->pmat,&ncoarse,pcbddc->coarse_adj_red,&pcbddc->coarse_subassembling,&have_void);
    8234:       } else {
    8235:         PCBDDCMatISGetSubassemblingPattern(t_coarse_mat_is,&ncoarse,pcbddc->coarse_adj_red,&pcbddc->coarse_subassembling,&have_void);
    8236:       }
    8237:     } else {
    8238:       PetscMPIInt rank;

    8240:       MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
    8241:       have_void = (active_procs == (PetscInt)size) ? PETSC_FALSE : PETSC_TRUE;
    8242:       ISCreateStride(PetscObjectComm((PetscObject)pc),1,rank,1,&pcbddc->coarse_subassembling);
    8243:     }
    8244:   } else { /* if a subassembling pattern exists, then we can reuse the coarse ksp and compute the number of process involved */
    8245:     PetscInt    psum;
    8246:     if (pcbddc->coarse_ksp) psum = 1;
    8247:     else psum = 0;
    8248:     MPIU_Allreduce(&psum,&ncoarse,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)pc));
    8249:     have_void = ncoarse < size ? PETSC_TRUE : PETSC_FALSE;
    8250:   }
    8251:   /* determine if we can go multilevel */
    8252:   if (multilevel_requested) {
    8253:     if (ncoarse > 1) multilevel_allowed = PETSC_TRUE; /* found enough processes */
    8254:     else restr = full_restr = PETSC_TRUE; /* 1 subdomain, use a direct solver */
    8255:   }
    8256:   if (multilevel_allowed && have_void) restr = PETSC_TRUE;

    8258:   /* dump subassembling pattern */
    8259:   if (pcbddc->dbg_flag && multilevel_allowed) {
    8260:     ISView(pcbddc->coarse_subassembling,pcbddc->dbg_viewer);
    8261:   }
    8262:   /* compute dofs splitting and neumann boundaries for coarse dofs */
    8263:   nedcfield = -1;
    8264:   corners = NULL;
    8265:   if (multilevel_allowed && !coarse_reuse && (pcbddc->n_ISForDofsLocal || pcbddc->NeumannBoundariesLocal || pcbddc->nedclocal || pcbddc->corner_selected)) { /* protects from unneeded computations */
    8266:     PetscInt               *tidxs,*tidxs2,nout,tsize,i;
    8267:     const PetscInt         *idxs;
    8268:     ISLocalToGlobalMapping tmap;

    8270:     /* create map between primal indices (in local representative ordering) and local primal numbering */
    8271:     ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,1,pcbddc->local_primal_size,pcbddc->primal_indices_local_idxs,PETSC_COPY_VALUES,&tmap);
    8272:     /* allocate space for temporary storage */
    8273:     PetscMalloc1(pcbddc->local_primal_size,&tidxs);
    8274:     PetscMalloc1(pcbddc->local_primal_size,&tidxs2);
    8275:     /* allocate for IS array */
    8276:     nisdofs = pcbddc->n_ISForDofsLocal;
    8277:     if (pcbddc->nedclocal) {
    8278:       if (pcbddc->nedfield > -1) {
    8279:         nedcfield = pcbddc->nedfield;
    8280:       } else {
    8281:         nedcfield = 0;
    8282:         if (nisdofs) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"This should not happen (%D)",nisdofs);
    8283:         nisdofs = 1;
    8284:       }
    8285:     }
    8286:     nisneu = !!pcbddc->NeumannBoundariesLocal;
    8287:     nisvert = 0; /* nisvert is not used */
    8288:     nis = nisdofs + nisneu + nisvert;
    8289:     PetscMalloc1(nis,&isarray);
    8290:     /* dofs splitting */
    8291:     for (i=0;i<nisdofs;i++) {
    8292:       /* ISView(pcbddc->ISForDofsLocal[i],0); */
    8293:       if (nedcfield != i) {
    8294:         ISGetLocalSize(pcbddc->ISForDofsLocal[i],&tsize);
    8295:         ISGetIndices(pcbddc->ISForDofsLocal[i],&idxs);
    8296:         ISGlobalToLocalMappingApply(tmap,IS_GTOLM_DROP,tsize,idxs,&nout,tidxs);
    8297:         ISRestoreIndices(pcbddc->ISForDofsLocal[i],&idxs);
    8298:       } else {
    8299:         ISGetLocalSize(pcbddc->nedclocal,&tsize);
    8300:         ISGetIndices(pcbddc->nedclocal,&idxs);
    8301:         ISGlobalToLocalMappingApply(tmap,IS_GTOLM_DROP,tsize,idxs,&nout,tidxs);
    8302:         if (tsize != nout) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Failed when mapping coarse nedelec field! %D != %D",tsize,nout);
    8303:         ISRestoreIndices(pcbddc->nedclocal,&idxs);
    8304:       }
    8305:       ISLocalToGlobalMappingApply(coarse_islg,nout,tidxs,tidxs2);
    8306:       ISCreateGeneral(PetscObjectComm((PetscObject)pc),nout,tidxs2,PETSC_COPY_VALUES,&isarray[i]);
    8307:       /* ISView(isarray[i],0); */
    8308:     }
    8309:     /* neumann boundaries */
    8310:     if (pcbddc->NeumannBoundariesLocal) {
    8311:       /* ISView(pcbddc->NeumannBoundariesLocal,0); */
    8312:       ISGetLocalSize(pcbddc->NeumannBoundariesLocal,&tsize);
    8313:       ISGetIndices(pcbddc->NeumannBoundariesLocal,&idxs);
    8314:       ISGlobalToLocalMappingApply(tmap,IS_GTOLM_DROP,tsize,idxs,&nout,tidxs);
    8315:       ISRestoreIndices(pcbddc->NeumannBoundariesLocal,&idxs);
    8316:       ISLocalToGlobalMappingApply(coarse_islg,nout,tidxs,tidxs2);
    8317:       ISCreateGeneral(PetscObjectComm((PetscObject)pc),nout,tidxs2,PETSC_COPY_VALUES,&isarray[nisdofs]);
    8318:       /* ISView(isarray[nisdofs],0); */
    8319:     }
    8320:     /* coordinates */
    8321:     if (pcbddc->corner_selected) {
    8322:       PCBDDCGraphGetCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&corners);
    8323:       ISGetLocalSize(corners,&tsize);
    8324:       ISGetIndices(corners,&idxs);
    8325:       ISGlobalToLocalMappingApply(tmap,IS_GTOLM_DROP,tsize,idxs,&nout,tidxs);
    8326:       if (tsize != nout) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Failed when mapping corners! %D != %D",tsize,nout);
    8327:       ISRestoreIndices(corners,&idxs);
    8328:       PCBDDCGraphRestoreCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&corners);
    8329:       ISLocalToGlobalMappingApply(coarse_islg,nout,tidxs,tidxs2);
    8330:       ISCreateGeneral(PetscObjectComm((PetscObject)pc),nout,tidxs2,PETSC_COPY_VALUES,&corners);
    8331:     }
    8332:     PetscFree(tidxs);
    8333:     PetscFree(tidxs2);
    8334:     ISLocalToGlobalMappingDestroy(&tmap);
    8335:   } else {
    8336:     nis = 0;
    8337:     nisdofs = 0;
    8338:     nisneu = 0;
    8339:     nisvert = 0;
    8340:     isarray = NULL;
    8341:   }
    8342:   /* destroy no longer needed map */
    8343:   ISLocalToGlobalMappingDestroy(&coarse_islg);

    8345:   /* subassemble */
    8346:   if (multilevel_allowed) {
    8347:     Vec       vp[1];
    8348:     PetscInt  nvecs = 0;
    8349:     PetscBool reuse,reuser;

    8351:     if (coarse_mat) reuse = PETSC_TRUE;
    8352:     else reuse = PETSC_FALSE;
    8353:     MPIU_Allreduce(&reuse,&reuser,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)pc));
    8354:     vp[0] = NULL;
    8355:     if (pcbddc->benign_have_null) { /* propagate no-net-flux quadrature to coarser level */
    8356:       VecCreate(PetscObjectComm((PetscObject)pc),&vp[0]);
    8357:       VecSetSizes(vp[0],pcbddc->local_primal_size,PETSC_DECIDE);
    8358:       VecSetType(vp[0],VECSTANDARD);
    8359:       nvecs = 1;

    8361:       if (pcbddc->divudotp) {
    8362:         Mat      B,loc_divudotp;
    8363:         Vec      v,p;
    8364:         IS       dummy;
    8365:         PetscInt np;

    8367:         MatISGetLocalMat(pcbddc->divudotp,&loc_divudotp);
    8368:         MatGetSize(loc_divudotp,&np,NULL);
    8369:         ISCreateStride(PETSC_COMM_SELF,np,0,1,&dummy);
    8370:         MatCreateSubMatrix(loc_divudotp,dummy,pcis->is_B_local,MAT_INITIAL_MATRIX,&B);
    8371:         MatCreateVecs(B,&v,&p);
    8372:         VecSet(p,1.);
    8373:         MatMultTranspose(B,p,v);
    8374:         VecDestroy(&p);
    8375:         MatDestroy(&B);
    8376:         VecGetArray(vp[0],&array);
    8377:         VecPlaceArray(pcbddc->vec1_P,array);
    8378:         MatMultTranspose(pcbddc->coarse_phi_B,v,pcbddc->vec1_P);
    8379:         VecResetArray(pcbddc->vec1_P);
    8380:         VecRestoreArray(vp[0],&array);
    8381:         ISDestroy(&dummy);
    8382:         VecDestroy(&v);
    8383:       }
    8384:     }
    8385:     if (reuser) {
    8386:       PCBDDCMatISSubassemble(t_coarse_mat_is,pcbddc->coarse_subassembling,0,restr,full_restr,PETSC_TRUE,&coarse_mat,nis,isarray,nvecs,vp);
    8387:     } else {
    8388:       PCBDDCMatISSubassemble(t_coarse_mat_is,pcbddc->coarse_subassembling,0,restr,full_restr,PETSC_FALSE,&coarse_mat_is,nis,isarray,nvecs,vp);
    8389:     }
    8390:     if (vp[0]) { /* vp[0] could have been placed on a different set of processes */
    8391:       PetscScalar       *arraym;
    8392:       const PetscScalar *arrayv;
    8393:       PetscInt          nl;
    8394:       VecGetLocalSize(vp[0],&nl);
    8395:       MatCreateSeqDense(PETSC_COMM_SELF,1,nl,NULL,&coarsedivudotp);
    8396:       MatDenseGetArray(coarsedivudotp,&arraym);
    8397:       VecGetArrayRead(vp[0],&arrayv);
    8398:       PetscArraycpy(arraym,arrayv,nl);
    8399:       VecRestoreArrayRead(vp[0],&arrayv);
    8400:       MatDenseRestoreArray(coarsedivudotp,&arraym);
    8401:       VecDestroy(&vp[0]);
    8402:     } else {
    8403:       MatCreateSeqAIJ(PETSC_COMM_SELF,0,0,1,NULL,&coarsedivudotp);
    8404:     }
    8405:   } else {
    8406:     PCBDDCMatISSubassemble(t_coarse_mat_is,pcbddc->coarse_subassembling,0,restr,full_restr,PETSC_FALSE,&coarse_mat_is,0,NULL,0,NULL);
    8407:   }
    8408:   if (coarse_mat_is || coarse_mat) {
    8409:     if (!multilevel_allowed) {
    8410:       MatConvert(coarse_mat_is,MATAIJ,coarse_mat_reuse,&coarse_mat);
    8411:     } else {
    8412:       /* if this matrix is present, it means we are not reusing the coarse matrix */
    8413:       if (coarse_mat_is) {
    8414:         if (coarse_mat) SETERRQ(PetscObjectComm((PetscObject)coarse_mat_is),PETSC_ERR_PLIB,"This should not happen");
    8415:         PetscObjectReference((PetscObject)coarse_mat_is);
    8416:         coarse_mat = coarse_mat_is;
    8417:       }
    8418:     }
    8419:   }
    8420:   MatDestroy(&t_coarse_mat_is);
    8421:   MatDestroy(&coarse_mat_is);

    8423:   /* create local to global scatters for coarse problem */
    8424:   if (compute_vecs) {
    8425:     PetscInt lrows;
    8426:     VecDestroy(&pcbddc->coarse_vec);
    8427:     if (coarse_mat) {
    8428:       MatGetLocalSize(coarse_mat,&lrows,NULL);
    8429:     } else {
    8430:       lrows = 0;
    8431:     }
    8432:     VecCreate(PetscObjectComm((PetscObject)pc),&pcbddc->coarse_vec);
    8433:     VecSetSizes(pcbddc->coarse_vec,lrows,PETSC_DECIDE);
    8434:     VecSetType(pcbddc->coarse_vec,coarse_mat ? coarse_mat->defaultvectype : VECSTANDARD);
    8435:     VecScatterDestroy(&pcbddc->coarse_loc_to_glob);
    8436:     VecScatterCreate(pcbddc->vec1_P,NULL,pcbddc->coarse_vec,coarse_is,&pcbddc->coarse_loc_to_glob);
    8437:   }
    8438:   ISDestroy(&coarse_is);

    8440:   /* set defaults for coarse KSP and PC */
    8441:   if (multilevel_allowed) {
    8442:     coarse_ksp_type = KSPRICHARDSON;
    8443:     coarse_pc_type  = PCBDDC;
    8444:   } else {
    8445:     coarse_ksp_type = KSPPREONLY;
    8446:     coarse_pc_type  = PCREDUNDANT;
    8447:   }

    8449:   /* print some info if requested */
    8450:   if (pcbddc->dbg_flag) {
    8451:     if (!multilevel_allowed) {
    8452:       PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"--------------------------------------------------\n");
    8453:       if (multilevel_requested) {
    8454:         PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"Not enough active processes on level %D (active processes %D, coarsening ratio %D)\n",pcbddc->current_level,active_procs,pcbddc->coarsening_ratio);
    8455:       } else if (pcbddc->max_levels) {
    8456:         PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"Maximum number of requested levels reached (%D)\n",pcbddc->max_levels);
    8457:       }
    8458:       PetscViewerFlush(pcbddc->dbg_viewer);
    8459:     }
    8460:   }

    8462:   /* communicate coarse discrete gradient */
    8463:   coarseG = NULL;
    8464:   if (pcbddc->nedcG && multilevel_allowed) {
    8465:     MPI_Comm ccomm;
    8466:     if (coarse_mat) {
    8467:       ccomm = PetscObjectComm((PetscObject)coarse_mat);
    8468:     } else {
    8469:       ccomm = MPI_COMM_NULL;
    8470:     }
    8471:     MatMPIAIJRestrict(pcbddc->nedcG,ccomm,&coarseG);
    8472:   }

    8474:   /* create the coarse KSP object only once with defaults */
    8475:   if (coarse_mat) {
    8476:     PetscBool   isredundant,isbddc,force,valid;
    8477:     PetscViewer dbg_viewer = NULL;

    8479:     if (pcbddc->dbg_flag) {
    8480:       dbg_viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)coarse_mat));
    8481:       PetscViewerASCIIAddTab(dbg_viewer,2*pcbddc->current_level);
    8482:     }
    8483:     if (!pcbddc->coarse_ksp) {
    8484:       char   prefix[256],str_level[16];
    8485:       size_t len;

    8487:       KSPCreate(PetscObjectComm((PetscObject)coarse_mat),&pcbddc->coarse_ksp);
    8488:       KSPSetErrorIfNotConverged(pcbddc->coarse_ksp,pc->erroriffailure);
    8489:       PetscObjectIncrementTabLevel((PetscObject)pcbddc->coarse_ksp,(PetscObject)pc,1);
    8490:       KSPSetTolerances(pcbddc->coarse_ksp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,1);
    8491:       KSPSetOperators(pcbddc->coarse_ksp,coarse_mat,coarse_mat);
    8492:       KSPSetType(pcbddc->coarse_ksp,coarse_ksp_type);
    8493:       KSPSetNormType(pcbddc->coarse_ksp,KSP_NORM_NONE);
    8494:       KSPGetPC(pcbddc->coarse_ksp,&pc_temp);
    8495:       /* TODO is this logic correct? should check for coarse_mat type */
    8496:       PCSetType(pc_temp,coarse_pc_type);
    8497:       /* prefix */
    8498:       PetscStrcpy(prefix,"");
    8499:       PetscStrcpy(str_level,"");
    8500:       if (!pcbddc->current_level) {
    8501:         PetscStrncpy(prefix,((PetscObject)pc)->prefix,sizeof(prefix));
    8502:         PetscStrlcat(prefix,"pc_bddc_coarse_",sizeof(prefix));
    8503:       } else {
    8504:         PetscStrlen(((PetscObject)pc)->prefix,&len);
    8505:         if (pcbddc->current_level>1) len -= 3; /* remove "lX_" with X level number */
    8506:         if (pcbddc->current_level>10) len -= 1; /* remove another char from level number */
    8507:         /* Nonstandard use of PetscStrncpy() to copy only a portion of the string */
    8508:         PetscStrncpy(prefix,((PetscObject)pc)->prefix,len+1);
    8509:         PetscSNPrintf(str_level,sizeof(str_level),"l%d_",(int)(pcbddc->current_level));
    8510:         PetscStrlcat(prefix,str_level,sizeof(prefix));
    8511:       }
    8512:       KSPSetOptionsPrefix(pcbddc->coarse_ksp,prefix);
    8513:       /* propagate BDDC info to the next level (these are dummy calls if pc_temp is not of type PCBDDC) */
    8514:       PCBDDCSetLevel(pc_temp,pcbddc->current_level+1);
    8515:       PCBDDCSetCoarseningRatio(pc_temp,pcbddc->coarsening_ratio);
    8516:       PCBDDCSetLevels(pc_temp,pcbddc->max_levels);
    8517:       /* allow user customization */
    8518:       KSPSetFromOptions(pcbddc->coarse_ksp);
    8519:       /* get some info after set from options */
    8520:       KSPGetPC(pcbddc->coarse_ksp,&pc_temp);
    8521:       /* multilevel cannot be done with coarse PC different from BDDC, NN, HPDDM, unless forced to */
    8522:       force = PETSC_FALSE;
    8523:       PetscOptionsGetBool(NULL,((PetscObject)pc_temp)->prefix,"-pc_type_forced",&force,NULL);
    8524:       PetscObjectTypeCompareAny((PetscObject)pc_temp,&valid,PCBDDC,PCNN,PCHPDDM,"");
    8525:       PetscObjectTypeCompare((PetscObject)pc_temp,PCBDDC,&isbddc);
    8526:       if (multilevel_allowed && !force && !valid) {
    8527:         isbddc = PETSC_TRUE;
    8528:         PCSetType(pc_temp,PCBDDC);
    8529:         PCBDDCSetLevel(pc_temp,pcbddc->current_level+1);
    8530:         PCBDDCSetCoarseningRatio(pc_temp,pcbddc->coarsening_ratio);
    8531:         PCBDDCSetLevels(pc_temp,pcbddc->max_levels);
    8532:         if (pc_temp->ops->setfromoptions) { /* need to setfromoptions again, skipping the pc_type */
    8533:           PetscObjectOptionsBegin((PetscObject)pc_temp);
    8534:           (*pc_temp->ops->setfromoptions)(PetscOptionsObject,pc_temp);
    8535:           PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)pc_temp);
    8536:           PetscOptionsEnd();
    8537:           pc_temp->setfromoptionscalled++;
    8538:         }
    8539:       }
    8540:     }
    8541:     /* propagate BDDC info to the next level (these are dummy calls if pc_temp is not of type PCBDDC) */
    8542:     KSPGetPC(pcbddc->coarse_ksp,&pc_temp);
    8543:     if (nisdofs) {
    8544:       PCBDDCSetDofsSplitting(pc_temp,nisdofs,isarray);
    8545:       for (i=0;i<nisdofs;i++) {
    8546:         ISDestroy(&isarray[i]);
    8547:       }
    8548:     }
    8549:     if (nisneu) {
    8550:       PCBDDCSetNeumannBoundaries(pc_temp,isarray[nisdofs]);
    8551:       ISDestroy(&isarray[nisdofs]);
    8552:     }
    8553:     if (nisvert) {
    8554:       PCBDDCSetPrimalVerticesIS(pc_temp,isarray[nis-1]);
    8555:       ISDestroy(&isarray[nis-1]);
    8556:     }
    8557:     if (coarseG) {
    8558:       PCBDDCSetDiscreteGradient(pc_temp,coarseG,1,nedcfield,PETSC_FALSE,PETSC_TRUE);
    8559:     }

    8561:     /* get some info after set from options */
    8562:     PetscObjectTypeCompare((PetscObject)pc_temp,PCBDDC,&isbddc);

    8564:     /* multilevel can only be requested via -pc_bddc_levels or PCBDDCSetLevels */
    8565:     if (isbddc && !multilevel_allowed) {
    8566:       PCSetType(pc_temp,coarse_pc_type);
    8567:     }
    8568:     /* multilevel cannot be done with coarse PC different from BDDC, NN, HPDDM, unless forced to */
    8569:     force = PETSC_FALSE;
    8570:     PetscOptionsGetBool(NULL,((PetscObject)pc_temp)->prefix,"-pc_type_forced",&force,NULL);
    8571:     PetscObjectTypeCompareAny((PetscObject)pc_temp,&valid,PCBDDC,PCNN,PCHPDDM,"");
    8572:     if (multilevel_requested && multilevel_allowed && !valid && !force) {
    8573:       PCSetType(pc_temp,PCBDDC);
    8574:     }
    8575:     PetscObjectTypeCompare((PetscObject)pc_temp,PCREDUNDANT,&isredundant);
    8576:     if (isredundant) {
    8577:       KSP inner_ksp;
    8578:       PC  inner_pc;

    8580:       PCRedundantGetKSP(pc_temp,&inner_ksp);
    8581:       KSPGetPC(inner_ksp,&inner_pc);
    8582:     }

    8584:     /* parameters which miss an API */
    8585:     PetscObjectTypeCompare((PetscObject)pc_temp,PCBDDC,&isbddc);
    8586:     if (isbddc) {
    8587:       PC_BDDC* pcbddc_coarse = (PC_BDDC*)pc_temp->data;

    8589:       pcbddc_coarse->detect_disconnected = PETSC_TRUE;
    8590:       pcbddc_coarse->coarse_eqs_per_proc = pcbddc->coarse_eqs_per_proc;
    8591:       pcbddc_coarse->coarse_eqs_limit    = pcbddc->coarse_eqs_limit;
    8592:       pcbddc_coarse->benign_saddle_point = pcbddc->benign_have_null;
    8593:       if (pcbddc_coarse->benign_saddle_point) {
    8594:         Mat                    coarsedivudotp_is;
    8595:         ISLocalToGlobalMapping l2gmap,rl2g,cl2g;
    8596:         IS                     row,col;
    8597:         const PetscInt         *gidxs;
    8598:         PetscInt               n,st,M,N;

    8600:         MatGetSize(coarsedivudotp,&n,NULL);
    8601:         MPI_Scan(&n,&st,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)coarse_mat));
    8602:         st   = st-n;
    8603:         ISCreateStride(PetscObjectComm((PetscObject)coarse_mat),1,st,1,&row);
    8604:         MatGetLocalToGlobalMapping(coarse_mat,&l2gmap,NULL);
    8605:         ISLocalToGlobalMappingGetSize(l2gmap,&n);
    8606:         ISLocalToGlobalMappingGetIndices(l2gmap,&gidxs);
    8607:         ISCreateGeneral(PetscObjectComm((PetscObject)coarse_mat),n,gidxs,PETSC_COPY_VALUES,&col);
    8608:         ISLocalToGlobalMappingRestoreIndices(l2gmap,&gidxs);
    8609:         ISLocalToGlobalMappingCreateIS(row,&rl2g);
    8610:         ISLocalToGlobalMappingCreateIS(col,&cl2g);
    8611:         ISGetSize(row,&M);
    8612:         MatGetSize(coarse_mat,&N,NULL);
    8613:         ISDestroy(&row);
    8614:         ISDestroy(&col);
    8615:         MatCreate(PetscObjectComm((PetscObject)coarse_mat),&coarsedivudotp_is);
    8616:         MatSetType(coarsedivudotp_is,MATIS);
    8617:         MatSetSizes(coarsedivudotp_is,PETSC_DECIDE,PETSC_DECIDE,M,N);
    8618:         MatSetLocalToGlobalMapping(coarsedivudotp_is,rl2g,cl2g);
    8619:         ISLocalToGlobalMappingDestroy(&rl2g);
    8620:         ISLocalToGlobalMappingDestroy(&cl2g);
    8621:         MatISSetLocalMat(coarsedivudotp_is,coarsedivudotp);
    8622:         MatDestroy(&coarsedivudotp);
    8623:         PCBDDCSetDivergenceMat(pc_temp,coarsedivudotp_is,PETSC_FALSE,NULL);
    8624:         MatDestroy(&coarsedivudotp_is);
    8625:         pcbddc_coarse->adaptive_userdefined = PETSC_TRUE;
    8626:         if (pcbddc->adaptive_threshold[0] == 0.0) pcbddc_coarse->deluxe_zerorows = PETSC_TRUE;
    8627:       }
    8628:     }

    8630:     /* propagate symmetry info of coarse matrix */
    8631:     MatSetOption(coarse_mat,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);
    8632:     if (pc->pmat->symmetric_set) {
    8633:       MatSetOption(coarse_mat,MAT_SYMMETRIC,pc->pmat->symmetric);
    8634:     }
    8635:     if (pc->pmat->hermitian_set) {
    8636:       MatSetOption(coarse_mat,MAT_HERMITIAN,pc->pmat->hermitian);
    8637:     }
    8638:     if (pc->pmat->spd_set) {
    8639:       MatSetOption(coarse_mat,MAT_SPD,pc->pmat->spd);
    8640:     }
    8641:     if (pcbddc->benign_saddle_point && !pcbddc->benign_have_null) {
    8642:       MatSetOption(coarse_mat,MAT_SPD,PETSC_TRUE);
    8643:     }
    8644:     /* set operators */
    8645:     MatViewFromOptions(coarse_mat,(PetscObject)pc,"-pc_bddc_coarse_mat_view");
    8646:     MatSetOptionsPrefix(coarse_mat,((PetscObject)pcbddc->coarse_ksp)->prefix);
    8647:     KSPSetOperators(pcbddc->coarse_ksp,coarse_mat,coarse_mat);
    8648:     if (pcbddc->dbg_flag) {
    8649:       PetscViewerASCIISubtractTab(dbg_viewer,2*pcbddc->current_level);
    8650:     }
    8651:   }
    8652:   MatDestroy(&coarseG);
    8653:   PetscFree(isarray);
    8654: #if 0
    8655:   {
    8656:     PetscViewer viewer;
    8657:     char filename[256];
    8658:     sprintf(filename,"coarse_mat_level%d.m",pcbddc->current_level);
    8659:     PetscViewerASCIIOpen(PetscObjectComm((PetscObject)coarse_mat),filename,&viewer);
    8660:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
    8661:     MatView(coarse_mat,viewer);
    8662:     PetscViewerPopFormat(viewer);
    8663:     PetscViewerDestroy(&viewer);
    8664:   }
    8665: #endif

    8667:   if (corners) {
    8668:     Vec            gv;
    8669:     IS             is;
    8670:     const PetscInt *idxs;
    8671:     PetscInt       i,d,N,n,cdim = pcbddc->mat_graph->cdim;
    8672:     PetscScalar    *coords;

    8674:     if (!pcbddc->mat_graph->cloc) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing local coordinates");
    8675:     VecGetSize(pcbddc->coarse_vec,&N);
    8676:     VecGetLocalSize(pcbddc->coarse_vec,&n);
    8677:     VecCreate(PetscObjectComm((PetscObject)pcbddc->coarse_vec),&gv);
    8678:     VecSetBlockSize(gv,cdim);
    8679:     VecSetSizes(gv,n*cdim,N*cdim);
    8680:     VecSetType(gv,VECSTANDARD);
    8681:     VecSetFromOptions(gv);
    8682:     VecSet(gv,PETSC_MAX_REAL); /* we only propagate coordinates from vertices constraints */

    8684:     PCBDDCGraphGetCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&is);
    8685:     ISGetLocalSize(is,&n);
    8686:     ISGetIndices(is,&idxs);
    8687:     PetscMalloc1(n*cdim,&coords);
    8688:     for (i=0;i<n;i++) {
    8689:       for (d=0;d<cdim;d++) {
    8690:         coords[cdim*i+d] = pcbddc->mat_graph->coords[cdim*idxs[i]+d];
    8691:       }
    8692:     }
    8693:     ISRestoreIndices(is,&idxs);
    8694:     PCBDDCGraphRestoreCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&is);

    8696:     ISGetLocalSize(corners,&n);
    8697:     ISGetIndices(corners,&idxs);
    8698:     VecSetValuesBlocked(gv,n,idxs,coords,INSERT_VALUES);
    8699:     ISRestoreIndices(corners,&idxs);
    8700:     PetscFree(coords);
    8701:     VecAssemblyBegin(gv);
    8702:     VecAssemblyEnd(gv);
    8703:     VecGetArray(gv,&coords);
    8704:     if (pcbddc->coarse_ksp) {
    8705:       PC        coarse_pc;
    8706:       PetscBool isbddc;

    8708:       KSPGetPC(pcbddc->coarse_ksp,&coarse_pc);
    8709:       PetscObjectTypeCompare((PetscObject)coarse_pc,PCBDDC,&isbddc);
    8710:       if (isbddc) { /* coarse coordinates have PETSC_MAX_REAL, specific for BDDC */
    8711:         PetscReal *realcoords;

    8713:         VecGetLocalSize(gv,&n);
    8714: #if defined(PETSC_USE_COMPLEX)
    8715:         PetscMalloc1(n,&realcoords);
    8716:         for (i=0;i<n;i++) realcoords[i] = PetscRealPart(coords[i]);
    8717: #else
    8718:         realcoords = coords;
    8719: #endif
    8720:         PCSetCoordinates(coarse_pc,cdim,n/cdim,realcoords);
    8721: #if defined(PETSC_USE_COMPLEX)
    8722:         PetscFree(realcoords);
    8723: #endif
    8724:       }
    8725:     }
    8726:     VecRestoreArray(gv,&coords);
    8727:     VecDestroy(&gv);
    8728:   }
    8729:   ISDestroy(&corners);

    8731:   if (pcbddc->coarse_ksp) {
    8732:     Vec crhs,csol;

    8734:     KSPGetSolution(pcbddc->coarse_ksp,&csol);
    8735:     KSPGetRhs(pcbddc->coarse_ksp,&crhs);
    8736:     if (!csol) {
    8737:       MatCreateVecs(coarse_mat,&((pcbddc->coarse_ksp)->vec_sol),NULL);
    8738:     }
    8739:     if (!crhs) {
    8740:       MatCreateVecs(coarse_mat,NULL,&((pcbddc->coarse_ksp)->vec_rhs));
    8741:     }
    8742:   }
    8743:   MatDestroy(&coarsedivudotp);

    8745:   /* compute null space for coarse solver if the benign trick has been requested */
    8746:   if (pcbddc->benign_null) {

    8748:     VecSet(pcbddc->vec1_P,0.);
    8749:     for (i=0;i<pcbddc->benign_n;i++) {
    8750:       VecSetValue(pcbddc->vec1_P,pcbddc->local_primal_size-pcbddc->benign_n+i,1.0,INSERT_VALUES);
    8751:     }
    8752:     VecAssemblyBegin(pcbddc->vec1_P);
    8753:     VecAssemblyEnd(pcbddc->vec1_P);
    8754:     VecScatterBegin(pcbddc->coarse_loc_to_glob,pcbddc->vec1_P,pcbddc->coarse_vec,INSERT_VALUES,SCATTER_FORWARD);
    8755:     VecScatterEnd(pcbddc->coarse_loc_to_glob,pcbddc->vec1_P,pcbddc->coarse_vec,INSERT_VALUES,SCATTER_FORWARD);
    8756:     if (coarse_mat) {
    8757:       Vec         nullv;
    8758:       PetscScalar *array,*array2;
    8759:       PetscInt    nl;

    8761:       MatCreateVecs(coarse_mat,&nullv,NULL);
    8762:       VecGetLocalSize(nullv,&nl);
    8763:       VecGetArrayRead(pcbddc->coarse_vec,(const PetscScalar**)&array);
    8764:       VecGetArray(nullv,&array2);
    8765:       PetscArraycpy(array2,array,nl);
    8766:       VecRestoreArray(nullv,&array2);
    8767:       VecRestoreArrayRead(pcbddc->coarse_vec,(const PetscScalar**)&array);
    8768:       VecNormalize(nullv,NULL);
    8769:       MatNullSpaceCreate(PetscObjectComm((PetscObject)coarse_mat),PETSC_FALSE,1,&nullv,&CoarseNullSpace);
    8770:       VecDestroy(&nullv);
    8771:     }
    8772:   }
    8773:   PetscLogEventEnd(PC_BDDC_CoarseSetUp[pcbddc->current_level],pc,0,0,0);

    8775:   PetscLogEventBegin(PC_BDDC_CoarseSolver[pcbddc->current_level],pc,0,0,0);
    8776:   if (pcbddc->coarse_ksp) {
    8777:     PetscBool ispreonly;

    8779:     if (CoarseNullSpace) {
    8780:       PetscBool isnull;
    8781:       MatNullSpaceTest(CoarseNullSpace,coarse_mat,&isnull);
    8782:       if (isnull) {
    8783:         MatSetNullSpace(coarse_mat,CoarseNullSpace);
    8784:       }
    8785:       /* TODO: add local nullspaces (if any) */
    8786:     }
    8787:     /* setup coarse ksp */
    8788:     KSPSetUp(pcbddc->coarse_ksp);
    8789:     /* Check coarse problem if in debug mode or if solving with an iterative method */
    8790:     PetscObjectTypeCompare((PetscObject)pcbddc->coarse_ksp,KSPPREONLY,&ispreonly);
    8791:     if (pcbddc->dbg_flag || (!ispreonly && pcbddc->use_coarse_estimates)) {
    8792:       KSP       check_ksp;
    8793:       KSPType   check_ksp_type;
    8794:       PC        check_pc;
    8795:       Vec       check_vec,coarse_vec;
    8796:       PetscReal abs_infty_error,infty_error,lambda_min=1.0,lambda_max=1.0;
    8797:       PetscInt  its;
    8798:       PetscBool compute_eigs;
    8799:       PetscReal *eigs_r,*eigs_c;
    8800:       PetscInt  neigs;
    8801:       const char *prefix;

    8803:       /* Create ksp object suitable for estimation of extreme eigenvalues */
    8804:       KSPCreate(PetscObjectComm((PetscObject)pcbddc->coarse_ksp),&check_ksp);
    8805:       PetscObjectIncrementTabLevel((PetscObject)check_ksp,(PetscObject)pcbddc->coarse_ksp,0);
    8806:       KSPSetErrorIfNotConverged(pcbddc->coarse_ksp,PETSC_FALSE);
    8807:       KSPSetOperators(check_ksp,coarse_mat,coarse_mat);
    8808:       KSPSetTolerances(check_ksp,1.e-12,1.e-12,PETSC_DEFAULT,pcbddc->coarse_size);
    8809:       /* prevent from setup unneeded object */
    8810:       KSPGetPC(check_ksp,&check_pc);
    8811:       PCSetType(check_pc,PCNONE);
    8812:       if (ispreonly) {
    8813:         check_ksp_type = KSPPREONLY;
    8814:         compute_eigs = PETSC_FALSE;
    8815:       } else {
    8816:         check_ksp_type = KSPGMRES;
    8817:         compute_eigs = PETSC_TRUE;
    8818:       }
    8819:       KSPSetType(check_ksp,check_ksp_type);
    8820:       KSPSetComputeSingularValues(check_ksp,compute_eigs);
    8821:       KSPSetComputeEigenvalues(check_ksp,compute_eigs);
    8822:       KSPGMRESSetRestart(check_ksp,pcbddc->coarse_size+1);
    8823:       KSPGetOptionsPrefix(pcbddc->coarse_ksp,&prefix);
    8824:       KSPSetOptionsPrefix(check_ksp,prefix);
    8825:       KSPAppendOptionsPrefix(check_ksp,"check_");
    8826:       KSPSetFromOptions(check_ksp);
    8827:       KSPSetUp(check_ksp);
    8828:       KSPGetPC(pcbddc->coarse_ksp,&check_pc);
    8829:       KSPSetPC(check_ksp,check_pc);
    8830:       /* create random vec */
    8831:       MatCreateVecs(coarse_mat,&coarse_vec,&check_vec);
    8832:       VecSetRandom(check_vec,NULL);
    8833:       MatMult(coarse_mat,check_vec,coarse_vec);
    8834:       /* solve coarse problem */
    8835:       KSPSolve(check_ksp,coarse_vec,coarse_vec);
    8836:       KSPCheckSolve(check_ksp,pc,coarse_vec);
    8837:       /* set eigenvalue estimation if preonly has not been requested */
    8838:       if (compute_eigs) {
    8839:         PetscMalloc1(pcbddc->coarse_size+1,&eigs_r);
    8840:         PetscMalloc1(pcbddc->coarse_size+1,&eigs_c);
    8841:         KSPComputeEigenvalues(check_ksp,pcbddc->coarse_size+1,eigs_r,eigs_c,&neigs);
    8842:         if (neigs) {
    8843:           lambda_max = eigs_r[neigs-1];
    8844:           lambda_min = eigs_r[0];
    8845:           if (pcbddc->use_coarse_estimates) {
    8846:             if (lambda_max>=lambda_min) { /* using PETSC_SMALL since lambda_max == lambda_min is not allowed by KSPChebyshevSetEigenvalues */
    8847:               KSPChebyshevSetEigenvalues(pcbddc->coarse_ksp,lambda_max+PETSC_SMALL,lambda_min);
    8848:               KSPRichardsonSetScale(pcbddc->coarse_ksp,2.0/(lambda_max+lambda_min));
    8849:             }
    8850:           }
    8851:         }
    8852:       }

    8854:       /* check coarse problem residual error */
    8855:       if (pcbddc->dbg_flag) {
    8856:         PetscViewer dbg_viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)pcbddc->coarse_ksp));
    8857:         PetscViewerASCIIAddTab(dbg_viewer,2*(pcbddc->current_level+1));
    8858:         VecAXPY(check_vec,-1.0,coarse_vec);
    8859:         VecNorm(check_vec,NORM_INFINITY,&infty_error);
    8860:         MatMult(coarse_mat,check_vec,coarse_vec);
    8861:         VecNorm(coarse_vec,NORM_INFINITY,&abs_infty_error);
    8862:         PetscViewerASCIIPrintf(dbg_viewer,"Coarse problem details (use estimates %d)\n",pcbddc->use_coarse_estimates);
    8863:         PetscObjectPrintClassNamePrefixType((PetscObject)(pcbddc->coarse_ksp),dbg_viewer);
    8864:         PetscObjectPrintClassNamePrefixType((PetscObject)(check_pc),dbg_viewer);
    8865:         PetscViewerASCIIPrintf(dbg_viewer,"Coarse problem exact infty_error   : %1.6e\n",infty_error);
    8866:         PetscViewerASCIIPrintf(dbg_viewer,"Coarse problem residual infty_error: %1.6e\n",abs_infty_error);
    8867:         if (CoarseNullSpace) {
    8868:           PetscViewerASCIIPrintf(dbg_viewer,"Coarse problem is singular\n");
    8869:         }
    8870:         if (compute_eigs) {
    8871:           PetscReal          lambda_max_s,lambda_min_s;
    8872:           KSPConvergedReason reason;
    8873:           KSPGetType(check_ksp,&check_ksp_type);
    8874:           KSPGetIterationNumber(check_ksp,&its);
    8875:           KSPGetConvergedReason(check_ksp,&reason);
    8876:           KSPComputeExtremeSingularValues(check_ksp,&lambda_max_s,&lambda_min_s);
    8877:           PetscViewerASCIIPrintf(dbg_viewer,"Coarse problem eigenvalues (estimated with %d iterations of %s, conv reason %d): %1.6e %1.6e (%1.6e %1.6e)\n",its,check_ksp_type,reason,lambda_min,lambda_max,lambda_min_s,lambda_max_s);
    8878:           for (i=0;i<neigs;i++) {
    8879:             PetscViewerASCIIPrintf(dbg_viewer,"%1.6e %1.6ei\n",eigs_r[i],eigs_c[i]);
    8880:           }
    8881:         }
    8882:         PetscViewerFlush(dbg_viewer);
    8883:         PetscViewerASCIISubtractTab(dbg_viewer,2*(pcbddc->current_level+1));
    8884:       }
    8885:       VecDestroy(&check_vec);
    8886:       VecDestroy(&coarse_vec);
    8887:       KSPDestroy(&check_ksp);
    8888:       if (compute_eigs) {
    8889:         PetscFree(eigs_r);
    8890:         PetscFree(eigs_c);
    8891:       }
    8892:     }
    8893:   }
    8894:   MatNullSpaceDestroy(&CoarseNullSpace);
    8895:   /* print additional info */
    8896:   if (pcbddc->dbg_flag) {
    8897:     /* waits until all processes reaches this point */
    8898:     PetscBarrier((PetscObject)pc);
    8899:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"Coarse solver setup completed at level %D\n",pcbddc->current_level);
    8900:     PetscViewerFlush(pcbddc->dbg_viewer);
    8901:   }

    8903:   /* free memory */
    8904:   MatDestroy(&coarse_mat);
    8905:   PetscLogEventEnd(PC_BDDC_CoarseSolver[pcbddc->current_level],pc,0,0,0);
    8906:   return(0);
    8907: }

    8909: PetscErrorCode PCBDDCComputePrimalNumbering(PC pc,PetscInt* coarse_size_n,PetscInt** local_primal_indices_n)
    8910: {
    8911:   PC_BDDC*       pcbddc = (PC_BDDC*)pc->data;
    8912:   PC_IS*         pcis = (PC_IS*)pc->data;
    8913:   Mat_IS*        matis = (Mat_IS*)pc->pmat->data;
    8914:   IS             subset,subset_mult,subset_n;
    8915:   PetscInt       local_size,coarse_size=0;
    8916:   PetscInt       *local_primal_indices=NULL;
    8917:   const PetscInt *t_local_primal_indices;

    8921:   /* Compute global number of coarse dofs */
    8922:   if (pcbddc->local_primal_size && !pcbddc->local_primal_ref_node) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"BDDC ConstraintsSetUp should be called first");
    8923:   ISCreateGeneral(PetscObjectComm((PetscObject)(pc->pmat)),pcbddc->local_primal_size_cc,pcbddc->local_primal_ref_node,PETSC_COPY_VALUES,&subset_n);
    8924:   ISLocalToGlobalMappingApplyIS(pcis->mapping,subset_n,&subset);
    8925:   ISDestroy(&subset_n);
    8926:   ISCreateGeneral(PetscObjectComm((PetscObject)(pc->pmat)),pcbddc->local_primal_size_cc,pcbddc->local_primal_ref_mult,PETSC_COPY_VALUES,&subset_mult);
    8927:   ISRenumber(subset,subset_mult,&coarse_size,&subset_n);
    8928:   ISDestroy(&subset);
    8929:   ISDestroy(&subset_mult);
    8930:   ISGetLocalSize(subset_n,&local_size);
    8931:   if (local_size != pcbddc->local_primal_size) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid number of local primal indices computed %D != %D",local_size,pcbddc->local_primal_size);
    8932:   PetscMalloc1(local_size,&local_primal_indices);
    8933:   ISGetIndices(subset_n,&t_local_primal_indices);
    8934:   PetscArraycpy(local_primal_indices,t_local_primal_indices,local_size);
    8935:   ISRestoreIndices(subset_n,&t_local_primal_indices);
    8936:   ISDestroy(&subset_n);

    8938:   /* check numbering */
    8939:   if (pcbddc->dbg_flag) {
    8940:     PetscScalar coarsesum,*array,*array2;
    8941:     PetscInt    i;
    8942:     PetscBool   set_error = PETSC_FALSE,set_error_reduced = PETSC_FALSE;

    8944:     PetscViewerFlush(pcbddc->dbg_viewer);
    8945:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"--------------------------------------------------\n");
    8946:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"Check coarse indices\n");
    8947:     PetscViewerASCIIPushSynchronized(pcbddc->dbg_viewer);
    8948:     /* counter */
    8949:     VecSet(pcis->vec1_global,0.0);
    8950:     VecSet(pcis->vec1_N,1.0);
    8951:     VecScatterBegin(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
    8952:     VecScatterEnd(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
    8953:     VecScatterBegin(matis->rctx,pcis->vec1_global,pcis->vec2_N,INSERT_VALUES,SCATTER_FORWARD);
    8954:     VecScatterEnd(matis->rctx,pcis->vec1_global,pcis->vec2_N,INSERT_VALUES,SCATTER_FORWARD);
    8955:     VecSet(pcis->vec1_N,0.0);
    8956:     for (i=0;i<pcbddc->local_primal_size;i++) {
    8957:       VecSetValue(pcis->vec1_N,pcbddc->primal_indices_local_idxs[i],1.0,INSERT_VALUES);
    8958:     }
    8959:     VecAssemblyBegin(pcis->vec1_N);
    8960:     VecAssemblyEnd(pcis->vec1_N);
    8961:     VecSet(pcis->vec1_global,0.0);
    8962:     VecScatterBegin(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
    8963:     VecScatterEnd(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
    8964:     VecScatterBegin(matis->rctx,pcis->vec1_global,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD);
    8965:     VecScatterEnd(matis->rctx,pcis->vec1_global,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD);
    8966:     VecGetArray(pcis->vec1_N,&array);
    8967:     VecGetArray(pcis->vec2_N,&array2);
    8968:     for (i=0;i<pcis->n;i++) {
    8969:       if (array[i] != 0.0 && array[i] != array2[i]) {
    8970:         PetscInt owned = (PetscInt)PetscRealPart(array[i]),gi;
    8971:         PetscInt neigh = (PetscInt)PetscRealPart(array2[i]);
    8972:         set_error = PETSC_TRUE;
    8973:         ISLocalToGlobalMappingApply(pcis->mapping,1,&i,&gi);
    8974:         PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d: local index %D (gid %D) owned by %D processes instead of %D!\n",PetscGlobalRank,i,gi,owned,neigh);
    8975:       }
    8976:     }
    8977:     VecRestoreArray(pcis->vec2_N,&array2);
    8978:     MPIU_Allreduce(&set_error,&set_error_reduced,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)pc));
    8979:     PetscViewerFlush(pcbddc->dbg_viewer);
    8980:     for (i=0;i<pcis->n;i++) {
    8981:       if (PetscRealPart(array[i]) > 0.0) array[i] = 1.0/PetscRealPart(array[i]);
    8982:     }
    8983:     VecRestoreArray(pcis->vec1_N,&array);
    8984:     VecSet(pcis->vec1_global,0.0);
    8985:     VecScatterBegin(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
    8986:     VecScatterEnd(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
    8987:     VecSum(pcis->vec1_global,&coarsesum);
    8988:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"Size of coarse problem is %D (%lf)\n",coarse_size,PetscRealPart(coarsesum));
    8989:     if (pcbddc->dbg_flag > 1 || set_error_reduced) {
    8990:       PetscInt *gidxs;

    8992:       PetscMalloc1(pcbddc->local_primal_size,&gidxs);
    8993:       ISLocalToGlobalMappingApply(pcis->mapping,pcbddc->local_primal_size,pcbddc->primal_indices_local_idxs,gidxs);
    8994:       PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"Distribution of local primal indices\n");
    8995:       PetscViewerFlush(pcbddc->dbg_viewer);
    8996:       PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d\n",PetscGlobalRank);
    8997:       for (i=0;i<pcbddc->local_primal_size;i++) {
    8998:         PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"local_primal_indices[%D]=%D (%D,%D)\n",i,local_primal_indices[i],pcbddc->primal_indices_local_idxs[i],gidxs[i]);
    8999:       }
    9000:       PetscViewerFlush(pcbddc->dbg_viewer);
    9001:       PetscFree(gidxs);
    9002:     }
    9003:     PetscViewerFlush(pcbddc->dbg_viewer);
    9004:     PetscViewerASCIIPushSynchronized(pcbddc->dbg_viewer);
    9005:     if (set_error_reduced) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"BDDC Numbering of coarse dofs failed");
    9006:   }

    9008:   /* get back data */
    9009:   *coarse_size_n = coarse_size;
    9010:   *local_primal_indices_n = local_primal_indices;
    9011:   return(0);
    9012: }

    9014: PetscErrorCode PCBDDCGlobalToLocal(VecScatter g2l_ctx,Vec gwork, Vec lwork, IS globalis, IS* localis)
    9015: {
    9016:   IS             localis_t;
    9017:   PetscInt       i,lsize,*idxs,n;
    9018:   PetscScalar    *vals;

    9022:   /* get indices in local ordering exploiting local to global map */
    9023:   ISGetLocalSize(globalis,&lsize);
    9024:   PetscMalloc1(lsize,&vals);
    9025:   for (i=0;i<lsize;i++) vals[i] = 1.0;
    9026:   ISGetIndices(globalis,(const PetscInt**)&idxs);
    9027:   VecSet(gwork,0.0);
    9028:   VecSet(lwork,0.0);
    9029:   if (idxs) { /* multilevel guard */
    9030:     VecSetOption(gwork,VEC_IGNORE_NEGATIVE_INDICES,PETSC_TRUE);
    9031:     VecSetValues(gwork,lsize,idxs,vals,INSERT_VALUES);
    9032:   }
    9033:   VecAssemblyBegin(gwork);
    9034:   ISRestoreIndices(globalis,(const PetscInt**)&idxs);
    9035:   PetscFree(vals);
    9036:   VecAssemblyEnd(gwork);
    9037:   /* now compute set in local ordering */
    9038:   VecScatterBegin(g2l_ctx,gwork,lwork,INSERT_VALUES,SCATTER_FORWARD);
    9039:   VecScatterEnd(g2l_ctx,gwork,lwork,INSERT_VALUES,SCATTER_FORWARD);
    9040:   VecGetArrayRead(lwork,(const PetscScalar**)&vals);
    9041:   VecGetSize(lwork,&n);
    9042:   for (i=0,lsize=0;i<n;i++) {
    9043:     if (PetscRealPart(vals[i]) > 0.5) {
    9044:       lsize++;
    9045:     }
    9046:   }
    9047:   PetscMalloc1(lsize,&idxs);
    9048:   for (i=0,lsize=0;i<n;i++) {
    9049:     if (PetscRealPart(vals[i]) > 0.5) {
    9050:       idxs[lsize++] = i;
    9051:     }
    9052:   }
    9053:   VecRestoreArrayRead(lwork,(const PetscScalar**)&vals);
    9054:   ISCreateGeneral(PetscObjectComm((PetscObject)gwork),lsize,idxs,PETSC_OWN_POINTER,&localis_t);
    9055:   *localis = localis_t;
    9056:   return(0);
    9057: }

    9059: PetscErrorCode PCBDDCSetUpSubSchurs(PC pc)
    9060: {
    9061:   PC_IS               *pcis=(PC_IS*)pc->data;
    9062:   PC_BDDC             *pcbddc=(PC_BDDC*)pc->data;
    9063:   PCBDDCSubSchurs     sub_schurs=pcbddc->sub_schurs;
    9064:   Mat                 S_j;
    9065:   PetscInt            *used_xadj,*used_adjncy;
    9066:   PetscBool           free_used_adj;
    9067:   PetscErrorCode      ierr;

    9070:   PetscLogEventBegin(PC_BDDC_Schurs[pcbddc->current_level],pc,0,0,0);
    9071:   /* decide the adjacency to be used for determining internal problems for local schur on subsets */
    9072:   free_used_adj = PETSC_FALSE;
    9073:   if (pcbddc->sub_schurs_layers == -1) {
    9074:     used_xadj = NULL;
    9075:     used_adjncy = NULL;
    9076:   } else {
    9077:     if (pcbddc->sub_schurs_use_useradj && pcbddc->mat_graph->xadj) {
    9078:       used_xadj = pcbddc->mat_graph->xadj;
    9079:       used_adjncy = pcbddc->mat_graph->adjncy;
    9080:     } else if (pcbddc->computed_rowadj) {
    9081:       used_xadj = pcbddc->mat_graph->xadj;
    9082:       used_adjncy = pcbddc->mat_graph->adjncy;
    9083:     } else {
    9084:       PetscBool      flg_row=PETSC_FALSE;
    9085:       const PetscInt *xadj,*adjncy;
    9086:       PetscInt       nvtxs;

    9088:       MatGetRowIJ(pcbddc->local_mat,0,PETSC_TRUE,PETSC_FALSE,&nvtxs,&xadj,&adjncy,&flg_row);
    9089:       if (flg_row) {
    9090:         PetscMalloc2(nvtxs+1,&used_xadj,xadj[nvtxs],&used_adjncy);
    9091:         PetscArraycpy(used_xadj,xadj,nvtxs+1);
    9092:         PetscArraycpy(used_adjncy,adjncy,xadj[nvtxs]);
    9093:         free_used_adj = PETSC_TRUE;
    9094:       } else {
    9095:         pcbddc->sub_schurs_layers = -1;
    9096:         used_xadj = NULL;
    9097:         used_adjncy = NULL;
    9098:       }
    9099:       MatRestoreRowIJ(pcbddc->local_mat,0,PETSC_TRUE,PETSC_FALSE,&nvtxs,&xadj,&adjncy,&flg_row);
    9100:     }
    9101:   }

    9103:   /* setup sub_schurs data */
    9104:   MatCreateSchurComplement(pcis->A_II,pcis->pA_II,pcis->A_IB,pcis->A_BI,pcis->A_BB,&S_j);
    9105:   if (!sub_schurs->schur_explicit) {
    9106:     /* pcbddc->ksp_D up to date only if not using MatFactor with Schur complement support */
    9107:     MatSchurComplementSetKSP(S_j,pcbddc->ksp_D);
    9108:     PCBDDCSubSchursSetUp(sub_schurs,NULL,S_j,PETSC_FALSE,used_xadj,used_adjncy,pcbddc->sub_schurs_layers,NULL,pcbddc->adaptive_selection,PETSC_FALSE,PETSC_FALSE,0,NULL,NULL,NULL,NULL);
    9109:   } else {
    9110:     Mat       change = NULL;
    9111:     Vec       scaling = NULL;
    9112:     IS        change_primal = NULL, iP;
    9113:     PetscInt  benign_n;
    9114:     PetscBool reuse_solvers = (PetscBool)!pcbddc->use_change_of_basis;
    9115:     PetscBool need_change = PETSC_FALSE;
    9116:     PetscBool discrete_harmonic = PETSC_FALSE;

    9118:     if (!pcbddc->use_vertices && reuse_solvers) {
    9119:       PetscInt n_vertices;

    9121:       ISGetLocalSize(sub_schurs->is_vertices,&n_vertices);
    9122:       reuse_solvers = (PetscBool)!n_vertices;
    9123:     }
    9124:     if (!pcbddc->benign_change_explicit) {
    9125:       benign_n = pcbddc->benign_n;
    9126:     } else {
    9127:       benign_n = 0;
    9128:     }
    9129:     /* sub_schurs->change is a local object; instead, PCBDDCConstraintsSetUp and the quantities used in the test below are logically collective on pc.
    9130:        We need a global reduction to avoid possible deadlocks.
    9131:        We assume that sub_schurs->change is created once, and then reused for different solves, unless the topography has been recomputed */
    9132:     if (pcbddc->adaptive_userdefined || (pcbddc->deluxe_zerorows && !pcbddc->use_change_of_basis)) {
    9133:       PetscBool have_loc_change = (PetscBool)(!!sub_schurs->change);
    9134:       MPIU_Allreduce(&have_loc_change,&need_change,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)pc));
    9135:       need_change = (PetscBool)(!need_change);
    9136:     }
    9137:     /* If the user defines additional constraints, we import them here.
    9138:        We need to compute the change of basis according to the quadrature weights attached to pmat via MatSetNearNullSpace, and this could not be done (at the moment) without some hacking */
    9139:     if (need_change) {
    9140:       PC_IS   *pcisf;
    9141:       PC_BDDC *pcbddcf;
    9142:       PC      pcf;

    9144:       if (pcbddc->sub_schurs_rebuild) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot compute change of basis with a different graph");
    9145:       PCCreate(PetscObjectComm((PetscObject)pc),&pcf);
    9146:       PCSetOperators(pcf,pc->mat,pc->pmat);
    9147:       PCSetType(pcf,PCBDDC);

    9149:       /* hacks */
    9150:       pcisf                        = (PC_IS*)pcf->data;
    9151:       pcisf->is_B_local            = pcis->is_B_local;
    9152:       pcisf->vec1_N                = pcis->vec1_N;
    9153:       pcisf->BtoNmap               = pcis->BtoNmap;
    9154:       pcisf->n                     = pcis->n;
    9155:       pcisf->n_B                   = pcis->n_B;
    9156:       pcbddcf                      = (PC_BDDC*)pcf->data;
    9157:       PetscFree(pcbddcf->mat_graph);
    9158:       pcbddcf->mat_graph           = pcbddc->mat_graph;
    9159:       pcbddcf->use_faces           = PETSC_TRUE;
    9160:       pcbddcf->use_change_of_basis = PETSC_TRUE;
    9161:       pcbddcf->use_change_on_faces = PETSC_TRUE;
    9162:       pcbddcf->use_qr_single       = PETSC_TRUE;
    9163:       pcbddcf->fake_change         = PETSC_TRUE;

    9165:       /* setup constraints so that we can get information on primal vertices and change of basis (in local numbering) */
    9166:       PCBDDCConstraintsSetUp(pcf);
    9167:       sub_schurs->change_with_qr = pcbddcf->use_qr_single;
    9168:       ISCreateGeneral(PETSC_COMM_SELF,pcbddcf->n_vertices,pcbddcf->local_primal_ref_node,PETSC_COPY_VALUES,&change_primal);
    9169:       change = pcbddcf->ConstraintMatrix;
    9170:       pcbddcf->ConstraintMatrix = NULL;

    9172:       /* free unneeded memory allocated in PCBDDCConstraintsSetUp */
    9173:       PetscFree(pcbddcf->sub_schurs);
    9174:       MatNullSpaceDestroy(&pcbddcf->onearnullspace);
    9175:       PetscFree2(pcbddcf->local_primal_ref_node,pcbddcf->local_primal_ref_mult);
    9176:       PetscFree(pcbddcf->primal_indices_local_idxs);
    9177:       PetscFree(pcbddcf->onearnullvecs_state);
    9178:       PetscFree(pcf->data);
    9179:       pcf->ops->destroy = NULL;
    9180:       pcf->ops->reset   = NULL;
    9181:       PCDestroy(&pcf);
    9182:     }
    9183:     if (!pcbddc->use_deluxe_scaling) scaling = pcis->D;

    9185:     PetscObjectQuery((PetscObject)pc,"__KSPFETIDP_iP",(PetscObject*)&iP);
    9186:     if (iP) {
    9187:       PetscOptionsBegin(PetscObjectComm((PetscObject)iP),sub_schurs->prefix,"BDDC sub_schurs options","PC");
    9188:       PetscOptionsBool("-sub_schurs_discrete_harmonic",NULL,NULL,discrete_harmonic,&discrete_harmonic,NULL);
    9189:       PetscOptionsEnd();
    9190:     }
    9191:     if (discrete_harmonic) {
    9192:       Mat A;
    9193:       MatDuplicate(pcbddc->local_mat,MAT_COPY_VALUES,&A);
    9194:       MatZeroRowsColumnsIS(A,iP,1.0,NULL,NULL);
    9195:       PetscObjectCompose((PetscObject)A,"__KSPFETIDP_iP",(PetscObject)iP);
    9196:       PCBDDCSubSchursSetUp(sub_schurs,A,S_j,pcbddc->sub_schurs_exact_schur,used_xadj,used_adjncy,pcbddc->sub_schurs_layers,scaling,pcbddc->adaptive_selection,reuse_solvers,pcbddc->benign_saddle_point,benign_n,pcbddc->benign_p0_lidx,pcbddc->benign_zerodiag_subs,change,change_primal);
    9197:       MatDestroy(&A);
    9198:     } else {
    9199:       PCBDDCSubSchursSetUp(sub_schurs,pcbddc->local_mat,S_j,pcbddc->sub_schurs_exact_schur,used_xadj,used_adjncy,pcbddc->sub_schurs_layers,scaling,pcbddc->adaptive_selection,reuse_solvers,pcbddc->benign_saddle_point,benign_n,pcbddc->benign_p0_lidx,pcbddc->benign_zerodiag_subs,change,change_primal);
    9200:     }
    9201:     MatDestroy(&change);
    9202:     ISDestroy(&change_primal);
    9203:   }
    9204:   MatDestroy(&S_j);

    9206:   /* free adjacency */
    9207:   if (free_used_adj) {
    9208:     PetscFree2(used_xadj,used_adjncy);
    9209:   }
    9210:   PetscLogEventEnd(PC_BDDC_Schurs[pcbddc->current_level],pc,0,0,0);
    9211:   return(0);
    9212: }

    9214: PetscErrorCode PCBDDCInitSubSchurs(PC pc)
    9215: {
    9216:   PC_IS               *pcis=(PC_IS*)pc->data;
    9217:   PC_BDDC             *pcbddc=(PC_BDDC*)pc->data;
    9218:   PCBDDCGraph         graph;
    9219:   PetscErrorCode      ierr;

    9222:   /* attach interface graph for determining subsets */
    9223:   if (pcbddc->sub_schurs_rebuild) { /* in case rebuild has been requested, it uses a graph generated only by the neighbouring information */
    9224:     IS       verticesIS,verticescomm;
    9225:     PetscInt vsize,*idxs;

    9227:     PCBDDCGraphGetCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&verticesIS);
    9228:     ISGetSize(verticesIS,&vsize);
    9229:     ISGetIndices(verticesIS,(const PetscInt**)&idxs);
    9230:     ISCreateGeneral(PetscObjectComm((PetscObject)pc),vsize,idxs,PETSC_COPY_VALUES,&verticescomm);
    9231:     ISRestoreIndices(verticesIS,(const PetscInt**)&idxs);
    9232:     PCBDDCGraphRestoreCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&verticesIS);
    9233:     PCBDDCGraphCreate(&graph);
    9234:     PCBDDCGraphInit(graph,pcbddc->mat_graph->l2gmap,pcbddc->mat_graph->nvtxs_global,pcbddc->graphmaxcount);
    9235:     PCBDDCGraphSetUp(graph,pcbddc->mat_graph->custom_minimal_size,NULL,pcbddc->DirichletBoundariesLocal,0,NULL,verticescomm);
    9236:     ISDestroy(&verticescomm);
    9237:     PCBDDCGraphComputeConnectedComponents(graph);
    9238:   } else {
    9239:     graph = pcbddc->mat_graph;
    9240:   }
    9241:   /* print some info */
    9242:   if (pcbddc->dbg_flag && !pcbddc->sub_schurs_rebuild) {
    9243:     IS       vertices;
    9244:     PetscInt nv,nedges,nfaces;
    9245:     PCBDDCGraphASCIIView(graph,pcbddc->dbg_flag,pcbddc->dbg_viewer);
    9246:     PCBDDCGraphGetCandidatesIS(graph,&nfaces,NULL,&nedges,NULL,&vertices);
    9247:     ISGetSize(vertices,&nv);
    9248:     PetscViewerASCIIPushSynchronized(pcbddc->dbg_viewer);
    9249:     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"--------------------------------------------------------------\n");
    9250:     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d got %02d local candidate vertices (%D)\n",PetscGlobalRank,(int)nv,pcbddc->use_vertices);
    9251:     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d got %02d local candidate edges    (%D)\n",PetscGlobalRank,(int)nedges,pcbddc->use_edges);
    9252:     PetscViewerASCIISynchronizedPrintf(pcbddc->dbg_viewer,"Subdomain %04d got %02d local candidate faces    (%D)\n",PetscGlobalRank,(int)nfaces,pcbddc->use_faces);
    9253:     PetscViewerFlush(pcbddc->dbg_viewer);
    9254:     PetscViewerASCIIPopSynchronized(pcbddc->dbg_viewer);
    9255:     PCBDDCGraphRestoreCandidatesIS(graph,&nfaces,NULL,&nedges,NULL,&vertices);
    9256:   }

    9258:   /* sub_schurs init */
    9259:   if (!pcbddc->sub_schurs) {
    9260:     PCBDDCSubSchursCreate(&pcbddc->sub_schurs);
    9261:   }
    9262:   PCBDDCSubSchursInit(pcbddc->sub_schurs,((PetscObject)pc)->prefix,pcis->is_I_local,pcis->is_B_local,graph,pcis->BtoNmap,pcbddc->sub_schurs_rebuild);

    9264:   /* free graph struct */
    9265:   if (pcbddc->sub_schurs_rebuild) {
    9266:     PCBDDCGraphDestroy(&graph);
    9267:   }
    9268:   return(0);
    9269: }

    9271: PetscErrorCode PCBDDCCheckOperator(PC pc)
    9272: {
    9273:   PC_IS               *pcis=(PC_IS*)pc->data;
    9274:   PC_BDDC             *pcbddc=(PC_BDDC*)pc->data;
    9275:   PetscErrorCode      ierr;

    9278:   if (pcbddc->n_vertices == pcbddc->local_primal_size) {
    9279:     IS             zerodiag = NULL;
    9280:     Mat            S_j,B0_B=NULL;
    9281:     Vec            dummy_vec=NULL,vec_check_B,vec_scale_P;
    9282:     PetscScalar    *p0_check,*array,*array2;
    9283:     PetscReal      norm;
    9284:     PetscInt       i;

    9286:     /* B0 and B0_B */
    9287:     if (zerodiag) {
    9288:       IS       dummy;

    9290:       ISCreateStride(PETSC_COMM_SELF,pcbddc->benign_n,0,1,&dummy);
    9291:       MatCreateSubMatrix(pcbddc->benign_B0,dummy,pcis->is_B_local,MAT_INITIAL_MATRIX,&B0_B);
    9292:       MatCreateVecs(B0_B,NULL,&dummy_vec);
    9293:       ISDestroy(&dummy);
    9294:     }
    9295:     /* I need a primal vector to scale primal nodes since BDDC sums contibutions */
    9296:     VecDuplicate(pcbddc->vec1_P,&vec_scale_P);
    9297:     VecSet(pcbddc->vec1_P,1.0);
    9298:     VecScatterBegin(pcbddc->coarse_loc_to_glob,pcbddc->vec1_P,pcbddc->coarse_vec,ADD_VALUES,SCATTER_FORWARD);
    9299:     VecScatterEnd(pcbddc->coarse_loc_to_glob,pcbddc->vec1_P,pcbddc->coarse_vec,ADD_VALUES,SCATTER_FORWARD);
    9300:     VecScatterBegin(pcbddc->coarse_loc_to_glob,pcbddc->coarse_vec,vec_scale_P,INSERT_VALUES,SCATTER_REVERSE);
    9301:     VecScatterEnd(pcbddc->coarse_loc_to_glob,pcbddc->coarse_vec,vec_scale_P,INSERT_VALUES,SCATTER_REVERSE);
    9302:     VecReciprocal(vec_scale_P);
    9303:     /* S_j */
    9304:     MatCreateSchurComplement(pcis->A_II,pcis->pA_II,pcis->A_IB,pcis->A_BI,pcis->A_BB,&S_j);
    9305:     MatSchurComplementSetKSP(S_j,pcbddc->ksp_D);

    9307:     /* mimic vector in \widetilde{W}_\Gamma */
    9308:     VecSetRandom(pcis->vec1_N,NULL);
    9309:     /* continuous in primal space */
    9310:     VecSetRandom(pcbddc->coarse_vec,NULL);
    9311:     VecScatterBegin(pcbddc->coarse_loc_to_glob,pcbddc->coarse_vec,pcbddc->vec1_P,INSERT_VALUES,SCATTER_REVERSE);
    9312:     VecScatterEnd(pcbddc->coarse_loc_to_glob,pcbddc->coarse_vec,pcbddc->vec1_P,INSERT_VALUES,SCATTER_REVERSE);
    9313:     VecGetArray(pcbddc->vec1_P,&array);
    9314:     PetscCalloc1(pcbddc->benign_n,&p0_check);
    9315:     for (i=0;i<pcbddc->benign_n;i++) p0_check[i] = array[pcbddc->local_primal_size-pcbddc->benign_n+i];
    9316:     VecSetValues(pcis->vec1_N,pcbddc->local_primal_size,pcbddc->local_primal_ref_node,array,INSERT_VALUES);
    9317:     VecRestoreArray(pcbddc->vec1_P,&array);
    9318:     VecAssemblyBegin(pcis->vec1_N);
    9319:     VecAssemblyEnd(pcis->vec1_N);
    9320:     VecScatterBegin(pcis->N_to_B,pcis->vec1_N,pcis->vec2_B,INSERT_VALUES,SCATTER_FORWARD);
    9321:     VecScatterEnd(pcis->N_to_B,pcis->vec1_N,pcis->vec2_B,INSERT_VALUES,SCATTER_FORWARD);
    9322:     VecDuplicate(pcis->vec2_B,&vec_check_B);
    9323:     VecCopy(pcis->vec2_B,vec_check_B);

    9325:     /* assemble rhs for coarse problem */
    9326:     /* widetilde{S}_\Gamma w_\Gamma + \widetilde{B0}^T_B p0 */
    9327:     /* local with Schur */
    9328:     MatMult(S_j,pcis->vec2_B,pcis->vec1_B);
    9329:     if (zerodiag) {
    9330:       VecGetArray(dummy_vec,&array);
    9331:       for (i=0;i<pcbddc->benign_n;i++) array[i] = p0_check[i];
    9332:       VecRestoreArray(dummy_vec,&array);
    9333:       MatMultTransposeAdd(B0_B,dummy_vec,pcis->vec1_B,pcis->vec1_B);
    9334:     }
    9335:     /* sum on primal nodes the local contributions */
    9336:     VecScatterBegin(pcis->N_to_B,pcis->vec1_B,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    9337:     VecScatterEnd(pcis->N_to_B,pcis->vec1_B,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    9338:     VecGetArray(pcis->vec1_N,&array);
    9339:     VecGetArray(pcbddc->vec1_P,&array2);
    9340:     for (i=0;i<pcbddc->local_primal_size;i++) array2[i] = array[pcbddc->local_primal_ref_node[i]];
    9341:     VecRestoreArray(pcbddc->vec1_P,&array2);
    9342:     VecRestoreArray(pcis->vec1_N,&array);
    9343:     VecSet(pcbddc->coarse_vec,0.);
    9344:     VecScatterBegin(pcbddc->coarse_loc_to_glob,pcbddc->vec1_P,pcbddc->coarse_vec,ADD_VALUES,SCATTER_FORWARD);
    9345:     VecScatterEnd(pcbddc->coarse_loc_to_glob,pcbddc->vec1_P,pcbddc->coarse_vec,ADD_VALUES,SCATTER_FORWARD);
    9346:     VecScatterBegin(pcbddc->coarse_loc_to_glob,pcbddc->coarse_vec,pcbddc->vec1_P,INSERT_VALUES,SCATTER_REVERSE);
    9347:     VecScatterEnd(pcbddc->coarse_loc_to_glob,pcbddc->coarse_vec,pcbddc->vec1_P,INSERT_VALUES,SCATTER_REVERSE);
    9348:     VecGetArray(pcbddc->vec1_P,&array);
    9349:     /* scale primal nodes (BDDC sums contibutions) */
    9350:     VecPointwiseMult(pcbddc->vec1_P,vec_scale_P,pcbddc->vec1_P);
    9351:     VecSetValues(pcis->vec1_N,pcbddc->local_primal_size,pcbddc->local_primal_ref_node,array,INSERT_VALUES);
    9352:     VecRestoreArray(pcbddc->vec1_P,&array);
    9353:     VecAssemblyBegin(pcis->vec1_N);
    9354:     VecAssemblyEnd(pcis->vec1_N);
    9355:     VecScatterBegin(pcis->N_to_B,pcis->vec1_N,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    9356:     VecScatterEnd(pcis->N_to_B,pcis->vec1_N,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    9357:     /* global: \widetilde{B0}_B w_\Gamma */
    9358:     if (zerodiag) {
    9359:       MatMult(B0_B,pcis->vec2_B,dummy_vec);
    9360:       VecGetArray(dummy_vec,&array);
    9361:       for (i=0;i<pcbddc->benign_n;i++) pcbddc->benign_p0[i] = array[i];
    9362:       VecRestoreArray(dummy_vec,&array);
    9363:     }
    9364:     /* BDDC */
    9365:     VecSet(pcis->vec1_D,0.);
    9366:     PCBDDCApplyInterfacePreconditioner(pc,PETSC_FALSE);

    9368:     VecCopy(pcis->vec1_B,pcis->vec2_B);
    9369:     VecAXPY(pcis->vec1_B,-1.0,vec_check_B);
    9370:     VecNorm(pcis->vec1_B,NORM_INFINITY,&norm);
    9371:     PetscPrintf(PETSC_COMM_SELF,"[%d] BDDC local error is %1.4e\n",PetscGlobalRank,norm);
    9372:     for (i=0;i<pcbddc->benign_n;i++) {
    9373:       PetscPrintf(PETSC_COMM_SELF,"[%d] BDDC p0[%D] error is %1.4e\n",PetscGlobalRank,i,PetscAbsScalar(pcbddc->benign_p0[i]-p0_check[i]));
    9374:     }
    9375:     PetscFree(p0_check);
    9376:     VecDestroy(&vec_scale_P);
    9377:     VecDestroy(&vec_check_B);
    9378:     VecDestroy(&dummy_vec);
    9379:     MatDestroy(&S_j);
    9380:     MatDestroy(&B0_B);
    9381:   }
    9382:   return(0);
    9383: }

    9385: #include <../src/mat/impls/aij/mpi/mpiaij.h>
    9386: PetscErrorCode MatMPIAIJRestrict(Mat A, MPI_Comm ccomm, Mat *B)
    9387: {
    9388:   Mat            At;
    9389:   IS             rows;
    9390:   PetscInt       rst,ren;
    9392:   PetscLayout    rmap;

    9395:   rst = ren = 0;
    9396:   if (ccomm != MPI_COMM_NULL) {
    9397:     PetscLayoutCreate(ccomm,&rmap);
    9398:     PetscLayoutSetSize(rmap,A->rmap->N);
    9399:     PetscLayoutSetBlockSize(rmap,1);
    9400:     PetscLayoutSetUp(rmap);
    9401:     PetscLayoutGetRange(rmap,&rst,&ren);
    9402:   }
    9403:   ISCreateStride(PetscObjectComm((PetscObject)A),ren-rst,rst,1,&rows);
    9404:   MatCreateSubMatrix(A,rows,NULL,MAT_INITIAL_MATRIX,&At);
    9405:   ISDestroy(&rows);

    9407:   if (ccomm != MPI_COMM_NULL) {
    9408:     Mat_MPIAIJ *a,*b;
    9409:     IS         from,to;
    9410:     Vec        gvec;
    9411:     PetscInt   lsize;

    9413:     MatCreate(ccomm,B);
    9414:     MatSetSizes(*B,ren-rst,PETSC_DECIDE,PETSC_DECIDE,At->cmap->N);
    9415:     MatSetType(*B,MATAIJ);
    9416:     PetscLayoutDestroy(&((*B)->rmap));
    9417:     PetscLayoutSetUp((*B)->cmap);
    9418:     a    = (Mat_MPIAIJ*)At->data;
    9419:     b    = (Mat_MPIAIJ*)(*B)->data;
    9420:     MPI_Comm_size(ccomm,&b->size);
    9421:     MPI_Comm_rank(ccomm,&b->rank);
    9422:     PetscObjectReference((PetscObject)a->A);
    9423:     PetscObjectReference((PetscObject)a->B);
    9424:     b->A = a->A;
    9425:     b->B = a->B;

    9427:     b->donotstash      = a->donotstash;
    9428:     b->roworiented     = a->roworiented;
    9429:     b->rowindices      = NULL;
    9430:     b->rowvalues       = NULL;
    9431:     b->getrowactive    = PETSC_FALSE;

    9433:     (*B)->rmap         = rmap;
    9434:     (*B)->factortype   = A->factortype;
    9435:     (*B)->assembled    = PETSC_TRUE;
    9436:     (*B)->insertmode   = NOT_SET_VALUES;
    9437:     (*B)->preallocated = PETSC_TRUE;

    9439:     if (a->colmap) {
    9440: #if defined(PETSC_USE_CTABLE)
    9441:       PetscTableCreateCopy(a->colmap,&b->colmap);
    9442: #else
    9443:       PetscMalloc1(At->cmap->N,&b->colmap);
    9444:       PetscLogObjectMemory((PetscObject)*B,At->cmap->N*sizeof(PetscInt));
    9445:       PetscArraycpy(b->colmap,a->colmap,At->cmap->N);
    9446: #endif
    9447:     } else b->colmap = NULL;
    9448:     if (a->garray) {
    9449:       PetscInt len;
    9450:       len  = a->B->cmap->n;
    9451:       PetscMalloc1(len+1,&b->garray);
    9452:       PetscLogObjectMemory((PetscObject)(*B),len*sizeof(PetscInt));
    9453:       if (len) { PetscArraycpy(b->garray,a->garray,len); }
    9454:     } else b->garray = NULL;

    9456:     PetscObjectReference((PetscObject)a->lvec);
    9457:     b->lvec = a->lvec;
    9458:     PetscLogObjectParent((PetscObject)*B,(PetscObject)b->lvec);

    9460:     /* cannot use VecScatterCopy */
    9461:     VecGetLocalSize(b->lvec,&lsize);
    9462:     ISCreateGeneral(ccomm,lsize,b->garray,PETSC_USE_POINTER,&from);
    9463:     ISCreateStride(PETSC_COMM_SELF,lsize,0,1,&to);
    9464:     MatCreateVecs(*B,&gvec,NULL);
    9465:     VecScatterCreate(gvec,from,b->lvec,to,&b->Mvctx);
    9466:     PetscLogObjectParent((PetscObject)*B,(PetscObject)b->Mvctx);
    9467:     ISDestroy(&from);
    9468:     ISDestroy(&to);
    9469:     VecDestroy(&gvec);
    9470:   }
    9471:   MatDestroy(&At);
    9472:   return(0);
    9473: }