Date: 2022-03-31T04:48:03+00:00

Actual source code: bddcfetidp.c

      1: #include <../src/ksp/pc/impls/bddc/bddc.h>
      2: #include <../src/ksp/pc/impls/bddc/bddcprivate.h>
      3: #include <petscblaslapack.h>

      5: static PetscErrorCode MatMult_BDdelta_deluxe_nonred(Mat A, Vec x, Vec y)
      6: {
      7:   BDdelta_DN     ctx;

      9:   MatShellGetContext(A,&ctx);
     10:   MatMultTranspose(ctx->BD,x,ctx->work);
     11:   KSPSolveTranspose(ctx->kBD,ctx->work,y);
     12:   /* No PC so cannot propagate up failure in KSPSolveTranspose() */
     13:   return 0;
     14: }

     16: static PetscErrorCode MatMultTranspose_BDdelta_deluxe_nonred(Mat A, Vec x, Vec y)
     17: {
     18:   BDdelta_DN     ctx;

     20:   MatShellGetContext(A,&ctx);
     21:   KSPSolve(ctx->kBD,x,ctx->work);
     22:   /* No PC so cannot propagate up failure in KSPSolve() */
     23:   MatMult(ctx->BD,ctx->work,y);
     24:   return 0;
     25: }

     27: static PetscErrorCode MatDestroy_BDdelta_deluxe_nonred(Mat A)
     28: {
     29:   BDdelta_DN     ctx;

     31:   MatShellGetContext(A,&ctx);
     32:   MatDestroy(&ctx->BD);
     33:   KSPDestroy(&ctx->kBD);
     34:   VecDestroy(&ctx->work);
     35:   PetscFree(ctx);
     36:   return 0;
     37: }

     39: PetscErrorCode PCBDDCCreateFETIDPMatContext(PC pc, FETIDPMat_ctx *fetidpmat_ctx)
     40: {
     41:   FETIDPMat_ctx  newctx;

     43:   PetscNew(&newctx);
     44:   /* increase the reference count for BDDC preconditioner */
     45:   PetscObjectReference((PetscObject)pc);
     46:   newctx->pc              = pc;
     47:   *fetidpmat_ctx          = newctx;
     48:   return 0;
     49: }

     51: PetscErrorCode PCBDDCCreateFETIDPPCContext(PC pc, FETIDPPC_ctx *fetidppc_ctx)
     52: {
     53:   FETIDPPC_ctx   newctx;

     55:   PetscNew(&newctx);
     56:   /* increase the reference count for BDDC preconditioner */
     57:   PetscObjectReference((PetscObject)pc);
     58:   newctx->pc              = pc;
     59:   *fetidppc_ctx           = newctx;
     60:   return 0;
     61: }

     63: PetscErrorCode PCBDDCDestroyFETIDPMat(Mat A)
     64: {
     65:   FETIDPMat_ctx  mat_ctx;

     67:   MatShellGetContext(A,&mat_ctx);
     68:   VecDestroy(&mat_ctx->lambda_local);
     69:   VecDestroy(&mat_ctx->temp_solution_D);
     70:   VecDestroy(&mat_ctx->temp_solution_B);
     71:   MatDestroy(&mat_ctx->B_delta);
     72:   MatDestroy(&mat_ctx->B_Ddelta);
     73:   MatDestroy(&mat_ctx->B_BB);
     74:   MatDestroy(&mat_ctx->B_BI);
     75:   MatDestroy(&mat_ctx->Bt_BB);
     76:   MatDestroy(&mat_ctx->Bt_BI);
     77:   MatDestroy(&mat_ctx->C);
     78:   VecDestroy(&mat_ctx->rhs_flip);
     79:   VecDestroy(&mat_ctx->vP);
     80:   VecDestroy(&mat_ctx->xPg);
     81:   VecDestroy(&mat_ctx->yPg);
     82:   VecScatterDestroy(&mat_ctx->l2g_lambda);
     83:   VecScatterDestroy(&mat_ctx->l2g_lambda_only);
     84:   VecScatterDestroy(&mat_ctx->l2g_p);
     85:   VecScatterDestroy(&mat_ctx->g2g_p);
     86:   PCDestroy(&mat_ctx->pc); /* decrease PCBDDC reference count */
     87:   ISDestroy(&mat_ctx->pressure);
     88:   ISDestroy(&mat_ctx->lagrange);
     89:   PetscFree(mat_ctx);
     90:   return 0;
     91: }

     93: PetscErrorCode PCBDDCDestroyFETIDPPC(PC pc)
     94: {
     95:   FETIDPPC_ctx   pc_ctx;

     97:   PCShellGetContext(pc,&pc_ctx);
     98:   VecDestroy(&pc_ctx->lambda_local);
     99:   MatDestroy(&pc_ctx->B_Ddelta);
    100:   VecScatterDestroy(&pc_ctx->l2g_lambda);
    101:   MatDestroy(&pc_ctx->S_j);
    102:   PCDestroy(&pc_ctx->pc); /* decrease PCBDDC reference count */
    103:   VecDestroy(&pc_ctx->xPg);
    104:   VecDestroy(&pc_ctx->yPg);
    105:   PetscFree(pc_ctx);
    106:   return 0;
    107: }

    109: PetscErrorCode PCBDDCSetupFETIDPMatContext(FETIDPMat_ctx fetidpmat_ctx)
    110: {
    111:   PC_IS          *pcis=(PC_IS*)fetidpmat_ctx->pc->data;
    112:   PC_BDDC        *pcbddc=(PC_BDDC*)fetidpmat_ctx->pc->data;
    113:   PCBDDCGraph    mat_graph=pcbddc->mat_graph;
    114:   Mat_IS         *matis  = (Mat_IS*)fetidpmat_ctx->pc->pmat->data;
    115:   MPI_Comm       comm;
    116:   Mat            ScalingMat,BD1,BD2;
    117:   Vec            fetidp_global;
    118:   IS             IS_l2g_lambda;
    119:   IS             subset,subset_mult,subset_n,isvert;
    120:   PetscBool      skip_node,fully_redundant;
    121:   PetscInt       i,j,k,s,n_boundary_dofs,n_global_lambda,n_vertices,partial_sum;
    122:   PetscInt       cum,n_local_lambda,n_lambda_for_dof,dual_size,n_neg_values,n_pos_values;
    123:   PetscMPIInt    rank,size,buf_size,neigh;
    124:   PetscScalar    scalar_value;
    125:   const PetscInt *vertex_indices;
    126:   PetscInt       *dual_dofs_boundary_indices,*aux_local_numbering_1;
    127:   const PetscInt *aux_global_numbering;
    128:   PetscInt       *aux_sums,*cols_B_delta,*l2g_indices;
    129:   PetscScalar    *array,*scaling_factors,*vals_B_delta;
    130:   PetscScalar    **all_factors;
    131:   PetscInt       *aux_local_numbering_2;
    132:   PetscLayout    llay;

    134:   /* saddlepoint */
    135:   ISLocalToGlobalMapping l2gmap_p;
    136:   PetscLayout            play;
    137:   IS                     gP,pP;
    138:   PetscInt               nPl,nPg,nPgl;

    140:   PetscObjectGetComm((PetscObject)(fetidpmat_ctx->pc),&comm);
    141:   MPI_Comm_rank(comm,&rank);
    142:   MPI_Comm_size(comm,&size);

    144:   /* saddlepoint */
    145:   nPl      = 0;
    146:   nPg      = 0;
    147:   nPgl     = 0;
    148:   gP       = NULL;
    149:   pP       = NULL;
    150:   l2gmap_p = NULL;
    151:   play     = NULL;
    152:   PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_pP",(PetscObject*)&pP);
    153:   if (pP) { /* saddle point */
    154:     /* subdomain pressures in global numbering */
    155:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_gP",(PetscObject*)&gP);
    157:     ISGetLocalSize(gP,&nPl);
    158:     VecCreate(PETSC_COMM_SELF,&fetidpmat_ctx->vP);
    159:     VecSetSizes(fetidpmat_ctx->vP,nPl,nPl);
    160:     VecSetType(fetidpmat_ctx->vP,VECSTANDARD);
    161:     VecSetUp(fetidpmat_ctx->vP);

    163:     /* pressure matrix */
    164:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_C",(PetscObject*)&fetidpmat_ctx->C);
    165:     if (!fetidpmat_ctx->C) { /* null pressure block, compute layout and global numbering for pressures */
    166:       IS Pg;

    168:       ISRenumber(gP,NULL,&nPg,&Pg);
    169:       ISLocalToGlobalMappingCreateIS(Pg,&l2gmap_p);
    170:       ISDestroy(&Pg);
    171:       PetscLayoutCreate(comm,&play);
    172:       PetscLayoutSetBlockSize(play,1);
    173:       PetscLayoutSetSize(play,nPg);
    174:       ISGetLocalSize(pP,&nPgl);
    175:       PetscLayoutSetLocalSize(play,nPgl);
    176:       PetscLayoutSetUp(play);
    177:     } else {
    178:       PetscObjectReference((PetscObject)fetidpmat_ctx->C);
    179:       MatISGetLocalToGlobalMapping(fetidpmat_ctx->C,&l2gmap_p,NULL);
    180:       PetscObjectReference((PetscObject)l2gmap_p);
    181:       MatGetSize(fetidpmat_ctx->C,&nPg,NULL);
    182:       MatGetLocalSize(fetidpmat_ctx->C,NULL,&nPgl);
    183:       MatGetLayouts(fetidpmat_ctx->C,NULL,&llay);
    184:       PetscLayoutReference(llay,&play);
    185:     }
    186:     VecCreateMPIWithArray(comm,1,nPgl,nPg,NULL,&fetidpmat_ctx->xPg);
    187:     VecCreateMPIWithArray(comm,1,nPgl,nPg,NULL,&fetidpmat_ctx->yPg);

    189:     /* import matrices for pressures coupling */
    190:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_B_BI",(PetscObject*)&fetidpmat_ctx->B_BI);
    192:     PetscObjectReference((PetscObject)fetidpmat_ctx->B_BI);

    194:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_B_BB",(PetscObject*)&fetidpmat_ctx->B_BB);
    196:     PetscObjectReference((PetscObject)fetidpmat_ctx->B_BB);

    198:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_Bt_BI",(PetscObject*)&fetidpmat_ctx->Bt_BI);
    200:     PetscObjectReference((PetscObject)fetidpmat_ctx->Bt_BI);

    202:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_Bt_BB",(PetscObject*)&fetidpmat_ctx->Bt_BB);
    204:     PetscObjectReference((PetscObject)fetidpmat_ctx->Bt_BB);

    206:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_flip" ,(PetscObject*)&fetidpmat_ctx->rhs_flip);
    207:     if (fetidpmat_ctx->rhs_flip) {
    208:       PetscObjectReference((PetscObject)fetidpmat_ctx->rhs_flip);
    209:     }
    210:   }

    212:   /* Default type of lagrange multipliers is non-redundant */
    213:   fully_redundant = fetidpmat_ctx->fully_redundant;

    215:   /* Evaluate local and global number of lagrange multipliers */
    216:   VecSet(pcis->vec1_N,0.0);
    217:   n_local_lambda = 0;
    218:   partial_sum = 0;
    219:   n_boundary_dofs = 0;
    220:   s = 0;

    222:   /* Get Vertices used to define the BDDC */
    223:   PCBDDCGraphGetCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&isvert);
    224:   ISGetLocalSize(isvert,&n_vertices);
    225:   ISGetIndices(isvert,&vertex_indices);

    227:   dual_size = pcis->n_B-n_vertices;
    228:   PetscMalloc1(dual_size,&dual_dofs_boundary_indices);
    229:   PetscMalloc1(dual_size,&aux_local_numbering_1);
    230:   PetscMalloc1(dual_size,&aux_local_numbering_2);

    232:   VecGetArray(pcis->vec1_N,&array);
    233:   for (i=0;i<pcis->n;i++) {
    234:     j = mat_graph->count[i]; /* RECALL: mat_graph->count[i] does not count myself */
    235:     if (j > 0) n_boundary_dofs++;
    236:     skip_node = PETSC_FALSE;
    237:     if (s < n_vertices && vertex_indices[s] == i) { /* it works for a sorted set of vertices */
    238:       skip_node = PETSC_TRUE;
    239:       s++;
    240:     }
    241:     if (j < 1) skip_node = PETSC_TRUE;
    242:     if (mat_graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) skip_node = PETSC_TRUE;
    243:     if (!skip_node) {
    244:       if (fully_redundant) {
    245:         /* fully redundant set of lagrange multipliers */
    246:         n_lambda_for_dof = (j*(j+1))/2;
    247:       } else {
    248:         n_lambda_for_dof = j;
    249:       }
    250:       n_local_lambda += j;
    251:       /* needed to evaluate global number of lagrange multipliers */
    252:       array[i]=(1.0*n_lambda_for_dof)/(j+1.0); /* already scaled for the next global sum */
    253:       /* store some data needed */
    254:       dual_dofs_boundary_indices[partial_sum] = n_boundary_dofs-1;
    255:       aux_local_numbering_1[partial_sum] = i;
    256:       aux_local_numbering_2[partial_sum] = n_lambda_for_dof;
    257:       partial_sum++;
    258:     }
    259:   }
    260:   VecRestoreArray(pcis->vec1_N,&array);
    261:   ISRestoreIndices(isvert,&vertex_indices);
    262:   PCBDDCGraphRestoreCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&isvert);
    263:   dual_size = partial_sum;

    265:   /* compute global ordering of lagrange multipliers and associate l2g map */
    266:   ISCreateGeneral(comm,partial_sum,aux_local_numbering_1,PETSC_COPY_VALUES,&subset_n);
    267:   ISLocalToGlobalMappingApplyIS(pcis->mapping,subset_n,&subset);
    268:   ISDestroy(&subset_n);
    269:   ISCreateGeneral(comm,partial_sum,aux_local_numbering_2,PETSC_OWN_POINTER,&subset_mult);
    270:   ISRenumber(subset,subset_mult,&fetidpmat_ctx->n_lambda,&subset_n);
    271:   ISDestroy(&subset);

    273:   if (PetscDefined(USE_DEBUG)) {
    274:     VecSet(pcis->vec1_global,0.0);
    275:     VecScatterBegin(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
    276:     VecScatterEnd(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
    277:     VecSum(pcis->vec1_global,&scalar_value);
    278:     i = (PetscInt)PetscRealPart(scalar_value);
    280:   }

    282:   /* init data for scaling factors exchange */
    283:   if (!pcbddc->use_deluxe_scaling) {
    284:     PetscInt    *ptrs_buffer,neigh_position;
    285:     PetscScalar *send_buffer,*recv_buffer;
    286:     MPI_Request *send_reqs,*recv_reqs;

    288:     partial_sum = 0;
    289:     PetscMalloc1(pcis->n_neigh,&ptrs_buffer);
    290:     PetscMalloc1(PetscMax(pcis->n_neigh-1,0),&send_reqs);
    291:     PetscMalloc1(PetscMax(pcis->n_neigh-1,0),&recv_reqs);
    292:     PetscMalloc1(pcis->n+1,&all_factors);
    293:     if (pcis->n_neigh > 0) ptrs_buffer[0]=0;
    294:     for (i=1;i<pcis->n_neigh;i++) {
    295:       partial_sum += pcis->n_shared[i];
    296:       ptrs_buffer[i] = ptrs_buffer[i-1]+pcis->n_shared[i];
    297:     }
    298:     PetscMalloc1(partial_sum,&send_buffer);
    299:     PetscMalloc1(partial_sum,&recv_buffer);
    300:     PetscMalloc1(partial_sum,&all_factors[0]);
    301:     for (i=0;i<pcis->n-1;i++) {
    302:       j = mat_graph->count[i];
    303:       all_factors[i+1]=all_factors[i]+j;
    304:     }

    306:     /* scatter B scaling to N vec */
    307:     VecScatterBegin(pcis->N_to_B,pcis->D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    308:     VecScatterEnd(pcis->N_to_B,pcis->D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    309:     /* communications */
    310:     VecGetArrayRead(pcis->vec1_N,(const PetscScalar**)&array);
    311:     for (i=1;i<pcis->n_neigh;i++) {
    312:       for (j=0;j<pcis->n_shared[i];j++) {
    313:         send_buffer[ptrs_buffer[i-1]+j]=array[pcis->shared[i][j]];
    314:       }
    315:       PetscMPIIntCast(ptrs_buffer[i]-ptrs_buffer[i-1],&buf_size);
    316:       PetscMPIIntCast(pcis->neigh[i],&neigh);
    317:       MPI_Isend(&send_buffer[ptrs_buffer[i-1]],buf_size,MPIU_SCALAR,neigh,0,comm,&send_reqs[i-1]);
    318:       MPI_Irecv(&recv_buffer[ptrs_buffer[i-1]],buf_size,MPIU_SCALAR,neigh,0,comm,&recv_reqs[i-1]);
    319:     }
    320:     VecRestoreArrayRead(pcis->vec1_N,(const PetscScalar**)&array);
    321:     if (pcis->n_neigh > 0) {
    322:       MPI_Waitall(pcis->n_neigh-1,recv_reqs,MPI_STATUSES_IGNORE);
    323:     }
    324:     /* put values in correct places */
    325:     for (i=1;i<pcis->n_neigh;i++) {
    326:       for (j=0;j<pcis->n_shared[i];j++) {
    327:         k = pcis->shared[i][j];
    328:         neigh_position = 0;
    329:         while (mat_graph->neighbours_set[k][neigh_position] != pcis->neigh[i]) {neigh_position++;}
    330:         all_factors[k][neigh_position]=recv_buffer[ptrs_buffer[i-1]+j];
    331:       }
    332:     }
    333:     if (pcis->n_neigh > 0) {
    334:       MPI_Waitall(pcis->n_neigh-1,send_reqs,MPI_STATUSES_IGNORE);
    335:     }
    336:     PetscFree(send_reqs);
    337:     PetscFree(recv_reqs);
    338:     PetscFree(send_buffer);
    339:     PetscFree(recv_buffer);
    340:     PetscFree(ptrs_buffer);
    341:   }

    343:   /* Compute B and B_delta (local actions) */
    344:   PetscMalloc1(pcis->n_neigh,&aux_sums);
    345:   PetscMalloc1(n_local_lambda,&l2g_indices);
    346:   PetscMalloc1(n_local_lambda,&vals_B_delta);
    347:   PetscMalloc1(n_local_lambda,&cols_B_delta);
    348:   if (!pcbddc->use_deluxe_scaling) {
    349:     PetscMalloc1(n_local_lambda,&scaling_factors);
    350:   } else {
    351:     scaling_factors = NULL;
    352:     all_factors     = NULL;
    353:   }
    354:   ISGetIndices(subset_n,&aux_global_numbering);
    355:   partial_sum=0;
    356:   cum = 0;
    357:   for (i=0;i<dual_size;i++) {
    358:     n_global_lambda = aux_global_numbering[cum];
    359:     j = mat_graph->count[aux_local_numbering_1[i]];
    360:     aux_sums[0]=0;
    361:     for (s=1;s<j;s++) {
    362:       aux_sums[s]=aux_sums[s-1]+j-s+1;
    363:     }
    364:     if (all_factors) array = all_factors[aux_local_numbering_1[i]];
    365:     n_neg_values = 0;
    366:     while (n_neg_values < j && mat_graph->neighbours_set[aux_local_numbering_1[i]][n_neg_values] < rank) {n_neg_values++;}
    367:     n_pos_values = j - n_neg_values;
    368:     if (fully_redundant) {
    369:       for (s=0;s<n_neg_values;s++) {
    370:         l2g_indices    [partial_sum+s]=aux_sums[s]+n_neg_values-s-1+n_global_lambda;
    371:         cols_B_delta   [partial_sum+s]=dual_dofs_boundary_indices[i];
    372:         vals_B_delta   [partial_sum+s]=-1.0;
    373:         if (!pcbddc->use_deluxe_scaling) scaling_factors[partial_sum+s]=array[s];
    374:       }
    375:       for (s=0;s<n_pos_values;s++) {
    376:         l2g_indices    [partial_sum+s+n_neg_values]=aux_sums[n_neg_values]+s+n_global_lambda;
    377:         cols_B_delta   [partial_sum+s+n_neg_values]=dual_dofs_boundary_indices[i];
    378:         vals_B_delta   [partial_sum+s+n_neg_values]=1.0;
    379:         if (!pcbddc->use_deluxe_scaling) scaling_factors[partial_sum+s+n_neg_values]=array[s+n_neg_values];
    380:       }
    381:       partial_sum += j;
    382:     } else {
    383:       /* l2g_indices and default cols and vals of B_delta */
    384:       for (s=0;s<j;s++) {
    385:         l2g_indices    [partial_sum+s]=n_global_lambda+s;
    386:         cols_B_delta   [partial_sum+s]=dual_dofs_boundary_indices[i];
    387:         vals_B_delta   [partial_sum+s]=0.0;
    388:       }
    389:       /* B_delta */
    390:       if (n_neg_values > 0) { /* there's a rank next to me to the left */
    391:         vals_B_delta   [partial_sum+n_neg_values-1]=-1.0;
    392:       }
    393:       if (n_neg_values < j) { /* there's a rank next to me to the right */
    394:         vals_B_delta   [partial_sum+n_neg_values]=1.0;
    395:       }
    396:       /* scaling as in Klawonn-Widlund 1999 */
    397:       if (!pcbddc->use_deluxe_scaling) {
    398:         for (s=0;s<n_neg_values;s++) {
    399:           scalar_value = 0.0;
    400:           for (k=0;k<s+1;k++) scalar_value += array[k];
    401:           scaling_factors[partial_sum+s] = -scalar_value;
    402:         }
    403:         for (s=0;s<n_pos_values;s++) {
    404:           scalar_value = 0.0;
    405:           for (k=s+n_neg_values;k<j;k++) scalar_value += array[k];
    406:           scaling_factors[partial_sum+s+n_neg_values] = scalar_value;
    407:         }
    408:       }
    409:       partial_sum += j;
    410:     }
    411:     cum += aux_local_numbering_2[i];
    412:   }
    413:   ISRestoreIndices(subset_n,&aux_global_numbering);
    414:   ISDestroy(&subset_mult);
    415:   ISDestroy(&subset_n);
    416:   PetscFree(aux_sums);
    417:   PetscFree(aux_local_numbering_1);
    418:   PetscFree(dual_dofs_boundary_indices);
    419:   if (all_factors) {
    420:     PetscFree(all_factors[0]);
    421:     PetscFree(all_factors);
    422:   }

    424:   /* Create local part of B_delta */
    425:   MatCreate(PETSC_COMM_SELF,&fetidpmat_ctx->B_delta);
    426:   MatSetSizes(fetidpmat_ctx->B_delta,n_local_lambda,pcis->n_B,n_local_lambda,pcis->n_B);
    427:   MatSetType(fetidpmat_ctx->B_delta,MATSEQAIJ);
    428:   MatSeqAIJSetPreallocation(fetidpmat_ctx->B_delta,1,NULL);
    429:   MatSetOption(fetidpmat_ctx->B_delta,MAT_IGNORE_ZERO_ENTRIES,PETSC_TRUE);
    430:   for (i=0;i<n_local_lambda;i++) {
    431:     MatSetValue(fetidpmat_ctx->B_delta,i,cols_B_delta[i],vals_B_delta[i],INSERT_VALUES);
    432:   }
    433:   PetscFree(vals_B_delta);
    434:   MatAssemblyBegin(fetidpmat_ctx->B_delta,MAT_FINAL_ASSEMBLY);
    435:   MatAssemblyEnd(fetidpmat_ctx->B_delta,MAT_FINAL_ASSEMBLY);

    437:   BD1 = NULL;
    438:   BD2 = NULL;
    439:   if (fully_redundant) {
    441:     MatCreate(PETSC_COMM_SELF,&ScalingMat);
    442:     MatSetSizes(ScalingMat,n_local_lambda,n_local_lambda,n_local_lambda,n_local_lambda);
    443:     MatSetType(ScalingMat,MATSEQAIJ);
    444:     MatSeqAIJSetPreallocation(ScalingMat,1,NULL);
    445:     for (i=0;i<n_local_lambda;i++) {
    446:       MatSetValue(ScalingMat,i,i,scaling_factors[i],INSERT_VALUES);
    447:     }
    448:     MatAssemblyBegin(ScalingMat,MAT_FINAL_ASSEMBLY);
    449:     MatAssemblyEnd(ScalingMat,MAT_FINAL_ASSEMBLY);
    450:     MatMatMult(ScalingMat,fetidpmat_ctx->B_delta,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&fetidpmat_ctx->B_Ddelta);
    451:     MatDestroy(&ScalingMat);
    452:   } else {
    453:     MatCreate(PETSC_COMM_SELF,&fetidpmat_ctx->B_Ddelta);
    454:     MatSetSizes(fetidpmat_ctx->B_Ddelta,n_local_lambda,pcis->n_B,n_local_lambda,pcis->n_B);
    455:     if (!pcbddc->use_deluxe_scaling || !pcbddc->sub_schurs) {
    456:       MatSetType(fetidpmat_ctx->B_Ddelta,MATSEQAIJ);
    457:       MatSeqAIJSetPreallocation(fetidpmat_ctx->B_Ddelta,1,NULL);
    458:       for (i=0;i<n_local_lambda;i++) {
    459:         MatSetValue(fetidpmat_ctx->B_Ddelta,i,cols_B_delta[i],scaling_factors[i],INSERT_VALUES);
    460:       }
    461:       MatAssemblyBegin(fetidpmat_ctx->B_Ddelta,MAT_FINAL_ASSEMBLY);
    462:       MatAssemblyEnd(fetidpmat_ctx->B_Ddelta,MAT_FINAL_ASSEMBLY);
    463:     } else {
    464:       /* scaling as in Klawonn-Widlund 1999 */
    465:       PCBDDCDeluxeScaling deluxe_ctx = pcbddc->deluxe_ctx;
    466:       PCBDDCSubSchurs     sub_schurs = pcbddc->sub_schurs;
    467:       Mat                 T;
    468:       PetscScalar         *W,lwork,*Bwork;
    469:       const PetscInt      *idxs = NULL;
    470:       PetscInt            cum,mss,*nnz;
    471:       PetscBLASInt        *pivots,B_lwork,B_N,B_ierr;

    474:       mss  = 0;
    475:       PetscCalloc1(pcis->n_B,&nnz);
    476:       if (sub_schurs->is_Ej_all) {
    477:         ISGetIndices(sub_schurs->is_Ej_all,&idxs);
    478:         for (i=0,cum=0;i<sub_schurs->n_subs;i++) {
    479:           PetscInt subset_size;

    481:           ISGetLocalSize(sub_schurs->is_subs[i],&subset_size);
    482:           for (j=0;j<subset_size;j++) nnz[idxs[j+cum]] = subset_size;
    483:           mss  = PetscMax(mss,subset_size);
    484:           cum += subset_size;
    485:         }
    486:       }
    487:       MatCreate(PETSC_COMM_SELF,&T);
    488:       MatSetSizes(T,pcis->n_B,pcis->n_B,pcis->n_B,pcis->n_B);
    489:       MatSetType(T,MATSEQAIJ);
    490:       MatSeqAIJSetPreallocation(T,0,nnz);
    491:       PetscFree(nnz);

    493:       /* workspace allocation */
    494:       B_lwork = 0;
    495:       if (mss) {
    496:         PetscScalar dummy = 1;

    498:         B_lwork = -1;
    499:         PetscBLASIntCast(mss,&B_N);
    500:         PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    501:         PetscStackCallBLAS("LAPACKgetri",LAPACKgetri_(&B_N,&dummy,&B_N,&B_N,&lwork,&B_lwork,&B_ierr));
    502:         PetscFPTrapPop();
    504:         PetscBLASIntCast((PetscInt)PetscRealPart(lwork),&B_lwork);
    505:       }
    506:       PetscMalloc3(mss*mss,&W,mss,&pivots,B_lwork,&Bwork);

    508:       for (i=0,cum=0;i<sub_schurs->n_subs;i++) {
    509:         const PetscScalar *M;
    510:         PetscInt          subset_size;

    512:         ISGetLocalSize(sub_schurs->is_subs[i],&subset_size);
    513:         PetscBLASIntCast(subset_size,&B_N);
    514:         MatDenseGetArrayRead(deluxe_ctx->seq_mat[i],&M);
    515:         PetscArraycpy(W,M,subset_size*subset_size);
    516:         MatDenseRestoreArrayRead(deluxe_ctx->seq_mat[i],&M);
    517:         PetscFPTrapPush(PETSC_FP_TRAP_OFF);
    518:         PetscStackCallBLAS("LAPACKgetrf",LAPACKgetrf_(&B_N,&B_N,W,&B_N,pivots,&B_ierr));
    520:         PetscStackCallBLAS("LAPACKgetri",LAPACKgetri_(&B_N,W,&B_N,pivots,Bwork,&B_lwork,&B_ierr));
    522:         PetscFPTrapPop();
    523:         /* silent static analyzer */
    525:         MatSetValues(T,subset_size,idxs+cum,subset_size,idxs+cum,W,INSERT_VALUES);
    526:         cum += subset_size;
    527:       }
    528:       MatAssemblyBegin(T,MAT_FINAL_ASSEMBLY);
    529:       MatAssemblyEnd(T,MAT_FINAL_ASSEMBLY);
    530:       MatMatTransposeMult(T,fetidpmat_ctx->B_delta,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&BD1);
    531:       MatMatMult(fetidpmat_ctx->B_delta,BD1,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&BD2);
    532:       MatDestroy(&T);
    533:       PetscFree3(W,pivots,Bwork);
    534:       if (sub_schurs->is_Ej_all) {
    535:         ISRestoreIndices(sub_schurs->is_Ej_all,&idxs);
    536:       }
    537:     }
    538:   }
    539:   PetscFree(scaling_factors);
    540:   PetscFree(cols_B_delta);

    542:   /* Layout of multipliers */
    543:   PetscLayoutCreate(comm,&llay);
    544:   PetscLayoutSetBlockSize(llay,1);
    545:   PetscLayoutSetSize(llay,fetidpmat_ctx->n_lambda);
    546:   PetscLayoutSetUp(llay);
    547:   PetscLayoutGetLocalSize(llay,&fetidpmat_ctx->n);

    549:   /* Local work vector of multipliers */
    550:   VecCreate(PETSC_COMM_SELF,&fetidpmat_ctx->lambda_local);
    551:   VecSetSizes(fetidpmat_ctx->lambda_local,n_local_lambda,n_local_lambda);
    552:   VecSetType(fetidpmat_ctx->lambda_local,VECSEQ);

    554:   if (BD2) {
    555:     ISLocalToGlobalMapping l2g;
    556:     Mat                    T,TA,*pT;
    557:     IS                     is;
    558:     PetscInt               nl,N;
    559:     BDdelta_DN             ctx;

    561:     PetscLayoutGetLocalSize(llay,&nl);
    562:     PetscLayoutGetSize(llay,&N);
    563:     MatCreate(comm,&T);
    564:     MatSetSizes(T,nl,nl,N,N);
    565:     MatSetType(T,MATIS);
    566:     ISLocalToGlobalMappingCreate(comm,1,n_local_lambda,l2g_indices,PETSC_COPY_VALUES,&l2g);
    567:     MatSetLocalToGlobalMapping(T,l2g,l2g);
    568:     ISLocalToGlobalMappingDestroy(&l2g);
    569:     MatISSetLocalMat(T,BD2);
    570:     MatAssemblyBegin(T,MAT_FINAL_ASSEMBLY);
    571:     MatAssemblyEnd(T,MAT_FINAL_ASSEMBLY);
    572:     MatDestroy(&BD2);
    573:     MatConvert(T,MATAIJ,MAT_INITIAL_MATRIX,&TA);
    574:     MatDestroy(&T);
    575:     ISCreateGeneral(comm,n_local_lambda,l2g_indices,PETSC_USE_POINTER,&is);
    576:     MatCreateSubMatrices(TA,1,&is,&is,MAT_INITIAL_MATRIX,&pT);
    577:     MatDestroy(&TA);
    578:     ISDestroy(&is);
    579:     BD2  = pT[0];
    580:     PetscFree(pT);

    582:     /* B_Ddelta for non-redundant multipliers with deluxe scaling */
    583:     PetscNew(&ctx);
    584:     MatSetType(fetidpmat_ctx->B_Ddelta,MATSHELL);
    585:     MatShellSetContext(fetidpmat_ctx->B_Ddelta,ctx);
    586:     MatShellSetOperation(fetidpmat_ctx->B_Ddelta,MATOP_MULT,(void (*)(void))MatMult_BDdelta_deluxe_nonred);
    587:     MatShellSetOperation(fetidpmat_ctx->B_Ddelta,MATOP_MULT_TRANSPOSE,(void (*)(void))MatMultTranspose_BDdelta_deluxe_nonred);
    588:     MatShellSetOperation(fetidpmat_ctx->B_Ddelta,MATOP_DESTROY,(void (*)(void))MatDestroy_BDdelta_deluxe_nonred);
    589:     MatSetUp(fetidpmat_ctx->B_Ddelta);

    591:     PetscObjectReference((PetscObject)BD1);
    592:     ctx->BD = BD1;
    593:     KSPCreate(PETSC_COMM_SELF,&ctx->kBD);
    594:     KSPSetOperators(ctx->kBD,BD2,BD2);
    595:     VecDuplicate(fetidpmat_ctx->lambda_local,&ctx->work);
    596:     fetidpmat_ctx->deluxe_nonred = PETSC_TRUE;
    597:   }
    598:   MatDestroy(&BD1);
    599:   MatDestroy(&BD2);

    601:   /* fetidpmat sizes */
    602:   fetidpmat_ctx->n += nPgl;
    603:   fetidpmat_ctx->N  = fetidpmat_ctx->n_lambda+nPg;

    605:   /* Global vector for FETI-DP linear system */
    606:   VecCreate(comm,&fetidp_global);
    607:   VecSetSizes(fetidp_global,fetidpmat_ctx->n,fetidpmat_ctx->N);
    608:   VecSetType(fetidp_global,VECMPI);
    609:   VecSetUp(fetidp_global);

    611:   /* Decide layout for fetidp dofs: if it is a saddle point problem
    612:      pressure is ordered first in the local part of the global vector
    613:      of the FETI-DP linear system */
    614:   if (nPg) {
    615:     Vec            v;
    616:     IS             IS_l2g_p,ais;
    617:     PetscLayout    alay;
    618:     const PetscInt *idxs,*pranges,*aranges,*lranges;
    619:     PetscInt       *l2g_indices_p,rst;
    620:     PetscMPIInt    rank;

    622:     PetscMalloc1(nPl,&l2g_indices_p);
    623:     VecGetLayout(fetidp_global,&alay);
    624:     PetscLayoutGetRanges(alay,&aranges);
    625:     PetscLayoutGetRanges(play,&pranges);
    626:     PetscLayoutGetRanges(llay,&lranges);

    628:     MPI_Comm_rank(PetscObjectComm((PetscObject)fetidp_global),&rank);
    629:     ISCreateStride(PetscObjectComm((PetscObject)fetidp_global),pranges[rank+1]-pranges[rank],aranges[rank],1,&fetidpmat_ctx->pressure);
    630:     PetscObjectSetName((PetscObject)fetidpmat_ctx->pressure,"F_P");
    631:     ISCreateStride(PetscObjectComm((PetscObject)fetidp_global),lranges[rank+1]-lranges[rank],aranges[rank]+pranges[rank+1]-pranges[rank],1,&fetidpmat_ctx->lagrange);
    632:     PetscObjectSetName((PetscObject)fetidpmat_ctx->lagrange,"F_L");
    633:     ISLocalToGlobalMappingGetIndices(l2gmap_p,&idxs);
    634:     /* shift local to global indices for pressure */
    635:     for (i=0;i<nPl;i++) {
    636:       PetscMPIInt owner;

    638:       PetscLayoutFindOwner(play,idxs[i],&owner);
    639:       l2g_indices_p[i] = idxs[i]-pranges[owner]+aranges[owner];
    640:     }
    641:     ISLocalToGlobalMappingRestoreIndices(l2gmap_p,&idxs);
    642:     ISCreateGeneral(comm,nPl,l2g_indices_p,PETSC_OWN_POINTER,&IS_l2g_p);

    644:     /* local to global scatter for pressure */
    645:     VecScatterCreate(fetidpmat_ctx->vP,NULL,fetidp_global,IS_l2g_p,&fetidpmat_ctx->l2g_p);
    646:     ISDestroy(&IS_l2g_p);

    648:     /* scatter for lagrange multipliers only */
    649:     VecCreate(comm,&v);
    650:     VecSetType(v,VECSTANDARD);
    651:     VecSetLayout(v,llay);
    652:     VecSetUp(v);
    653:     ISCreateGeneral(comm,n_local_lambda,l2g_indices,PETSC_COPY_VALUES,&ais);
    654:     VecScatterCreate(fetidpmat_ctx->lambda_local,NULL,v,ais,&fetidpmat_ctx->l2g_lambda_only);
    655:     ISDestroy(&ais);
    656:     VecDestroy(&v);

    658:     /* shift local to global indices for multipliers */
    659:     for (i=0;i<n_local_lambda;i++) {
    660:       PetscInt    ps;
    661:       PetscMPIInt owner;

    663:       PetscLayoutFindOwner(llay,l2g_indices[i],&owner);
    664:       ps = pranges[owner+1]-pranges[owner];
    665:       l2g_indices[i] = l2g_indices[i]-lranges[owner]+aranges[owner]+ps;
    666:     }

    668:     /* scatter from alldofs to pressures global fetidp vector */
    669:     PetscLayoutGetRange(alay,&rst,NULL);
    670:     ISCreateStride(comm,nPgl,rst,1,&ais);
    671:     VecScatterCreate(pcis->vec1_global,pP,fetidp_global,ais,&fetidpmat_ctx->g2g_p);
    672:     ISDestroy(&ais);
    673:   }
    674:   PetscLayoutDestroy(&llay);
    675:   PetscLayoutDestroy(&play);
    676:   ISCreateGeneral(comm,n_local_lambda,l2g_indices,PETSC_OWN_POINTER,&IS_l2g_lambda);

    678:   /* scatter from local to global multipliers */
    679:   VecScatterCreate(fetidpmat_ctx->lambda_local,NULL,fetidp_global,IS_l2g_lambda,&fetidpmat_ctx->l2g_lambda);
    680:   ISDestroy(&IS_l2g_lambda);
    681:   ISLocalToGlobalMappingDestroy(&l2gmap_p);
    682:   VecDestroy(&fetidp_global);

    684:   /* Create some work vectors needed by fetidp */
    685:   VecDuplicate(pcis->vec1_B,&fetidpmat_ctx->temp_solution_B);
    686:   VecDuplicate(pcis->vec1_D,&fetidpmat_ctx->temp_solution_D);
    687:   return 0;
    688: }

    690: PetscErrorCode PCBDDCSetupFETIDPPCContext(Mat fetimat, FETIDPPC_ctx fetidppc_ctx)
    691: {
    692:   FETIDPMat_ctx  mat_ctx;
    693:   PC_BDDC        *pcbddc = (PC_BDDC*)fetidppc_ctx->pc->data;
    694:   PC_IS          *pcis = (PC_IS*)fetidppc_ctx->pc->data;
    695:   PetscBool      lumped = PETSC_FALSE;

    697:   MatShellGetContext(fetimat,&mat_ctx);
    698:   /* get references from objects created when setting up feti mat context */
    699:   PetscObjectReference((PetscObject)mat_ctx->lambda_local);
    700:   fetidppc_ctx->lambda_local = mat_ctx->lambda_local;
    701:   PetscObjectReference((PetscObject)mat_ctx->B_Ddelta);
    702:   fetidppc_ctx->B_Ddelta = mat_ctx->B_Ddelta;
    703:   if (mat_ctx->deluxe_nonred) {
    704:     PC               pc,mpc;
    705:     BDdelta_DN       ctx;
    706:     MatSolverType    solver;
    707:     const char       *prefix;

    709:     MatShellGetContext(mat_ctx->B_Ddelta,&ctx);
    710:     KSPSetType(ctx->kBD,KSPPREONLY);
    711:     KSPGetPC(ctx->kBD,&mpc);
    712:     KSPGetPC(pcbddc->ksp_D,&pc);
    713:     PCSetType(mpc,PCLU);
    714:     PCFactorGetMatSolverType(pc,(MatSolverType*)&solver);
    715:     if (solver) {
    716:       PCFactorSetMatSolverType(mpc,solver);
    717:     }
    718:     MatGetOptionsPrefix(fetimat,&prefix);
    719:     KSPSetOptionsPrefix(ctx->kBD,prefix);
    720:     KSPAppendOptionsPrefix(ctx->kBD,"bddelta_");
    721:     KSPSetFromOptions(ctx->kBD);
    722:   }

    724:   if (mat_ctx->l2g_lambda_only) {
    725:     PetscObjectReference((PetscObject)mat_ctx->l2g_lambda_only);
    726:     fetidppc_ctx->l2g_lambda = mat_ctx->l2g_lambda_only;
    727:   } else {
    728:     PetscObjectReference((PetscObject)mat_ctx->l2g_lambda);
    729:     fetidppc_ctx->l2g_lambda = mat_ctx->l2g_lambda;
    730:   }
    731:   /* Dirichlet preconditioner */
    732:   PetscOptionsGetBool(NULL,((PetscObject)fetimat)->prefix,"-pc_lumped",&lumped,NULL);
    733:   if (!lumped) {
    734:     IS        iV;
    735:     PetscBool discrete_harmonic = PETSC_FALSE;

    737:     PetscObjectQuery((PetscObject)fetidppc_ctx->pc,"__KSPFETIDP_iV",(PetscObject*)&iV);
    738:     if (iV) {
    739:       PetscOptionsGetBool(NULL,((PetscObject)fetimat)->prefix,"-pc_discrete_harmonic",&discrete_harmonic,NULL);
    740:     }
    741:     if (discrete_harmonic) {
    742:       KSP             sksp;
    743:       PC              pc;
    744:       PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs;
    745:       Mat             A_II,A_IB,A_BI;
    746:       IS              iP = NULL;
    747:       PetscBool       isshell,reuse = PETSC_FALSE;
    748:       KSPType         ksptype;
    749:       const char      *prefix;

    751:       /*
    752:         We constructs a Schur complement for

    754:         | A_II A_ID |
    755:         | A_DI A_DD |

    757:         instead of

    759:         | A_II  B^t_II A_ID |
    760:         | B_II -C_II   B_ID |
    761:         | A_DI  B^t_ID A_DD |

    763:       */
    764:       if (sub_schurs && sub_schurs->reuse_solver) {
    765:         PetscObjectQuery((PetscObject)sub_schurs->A,"__KSPFETIDP_iP",(PetscObject*)&iP);
    766:         if (iP) reuse = PETSC_TRUE;
    767:       }
    768:       if (!reuse) {
    769:         IS       aB;
    770:         PetscInt nb;
    771:         ISGetLocalSize(pcis->is_B_local,&nb);
    772:         ISCreateStride(PetscObjectComm((PetscObject)pcis->A_II),nb,0,1,&aB);
    773:         MatCreateSubMatrix(pcis->A_II,iV,iV,MAT_INITIAL_MATRIX,&A_II);
    774:         MatCreateSubMatrix(pcis->A_IB,iV,aB,MAT_INITIAL_MATRIX,&A_IB);
    775:         MatCreateSubMatrix(pcis->A_BI,aB,iV,MAT_INITIAL_MATRIX,&A_BI);
    776:         ISDestroy(&aB);
    777:       } else {
    778:         MatCreateSubMatrix(sub_schurs->A,pcis->is_I_local,pcis->is_B_local,MAT_INITIAL_MATRIX,&A_IB);
    779:         MatCreateSubMatrix(sub_schurs->A,pcis->is_B_local,pcis->is_I_local,MAT_INITIAL_MATRIX,&A_BI);
    780:         PetscObjectReference((PetscObject)pcis->A_II);
    781:         A_II = pcis->A_II;
    782:       }
    783:       MatCreateSchurComplement(A_II,A_II,A_IB,A_BI,pcis->A_BB,&fetidppc_ctx->S_j);

    785:       /* propagate settings of solver */
    786:       MatSchurComplementGetKSP(fetidppc_ctx->S_j,&sksp);
    787:       KSPGetType(pcis->ksp_D,&ksptype);
    788:       KSPSetType(sksp,ksptype);
    789:       KSPGetPC(pcis->ksp_D,&pc);
    790:       PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&isshell);
    791:       if (!isshell) {
    792:         MatSolverType    solver;
    793:         PCType           pctype;

    795:         PCGetType(pc,&pctype);
    796:         PCFactorGetMatSolverType(pc,(MatSolverType*)&solver);
    797:         KSPGetPC(sksp,&pc);
    798:         PCSetType(pc,pctype);
    799:         if (solver) {
    800:           PCFactorSetMatSolverType(pc,solver);
    801:         }
    802:       } else {
    803:         KSPGetPC(sksp,&pc);
    804:         PCSetType(pc,PCLU);
    805:       }
    806:       MatDestroy(&A_II);
    807:       MatDestroy(&A_IB);
    808:       MatDestroy(&A_BI);
    809:       MatGetOptionsPrefix(fetimat,&prefix);
    810:       KSPSetOptionsPrefix(sksp,prefix);
    811:       KSPAppendOptionsPrefix(sksp,"harmonic_");
    812:       KSPSetFromOptions(sksp);
    813:       if (reuse) {
    814:         KSPSetPC(sksp,sub_schurs->reuse_solver->interior_solver);
    815:         PetscObjectIncrementTabLevel((PetscObject)sub_schurs->reuse_solver->interior_solver,(PetscObject)sksp,0);
    816:       }
    817:     } else { /* default Dirichlet preconditioner is pde-harmonic */
    818:       MatCreateSchurComplement(pcis->A_II,pcis->A_II,pcis->A_IB,pcis->A_BI,pcis->A_BB,&fetidppc_ctx->S_j);
    819:       MatSchurComplementSetKSP(fetidppc_ctx->S_j,pcis->ksp_D);
    820:     }
    821:   } else {
    822:     PetscObjectReference((PetscObject)pcis->A_BB);
    823:     fetidppc_ctx->S_j = pcis->A_BB;
    824:   }
    825:   /* saddle-point */
    826:   if (mat_ctx->xPg) {
    827:     PetscObjectReference((PetscObject)mat_ctx->xPg);
    828:     fetidppc_ctx->xPg = mat_ctx->xPg;
    829:     PetscObjectReference((PetscObject)mat_ctx->yPg);
    830:     fetidppc_ctx->yPg = mat_ctx->yPg;
    831:   }
    832:   return 0;
    833: }

    835: PetscErrorCode FETIDPMatMult_Kernel(Mat fetimat, Vec x, Vec y, PetscBool trans)
    836: {
    837:   FETIDPMat_ctx  mat_ctx;
    838:   PC_BDDC        *pcbddc;
    839:   PC_IS          *pcis;

    841:   MatShellGetContext(fetimat,&mat_ctx);
    842:   pcis = (PC_IS*)mat_ctx->pc->data;
    843:   pcbddc = (PC_BDDC*)mat_ctx->pc->data;
    844:   /* Application of B_delta^T */
    845:   VecSet(pcis->vec1_B,0.);
    846:   VecScatterBegin(mat_ctx->l2g_lambda,x,mat_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
    847:   VecScatterEnd(mat_ctx->l2g_lambda,x,mat_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
    848:   MatMultTranspose(mat_ctx->B_delta,mat_ctx->lambda_local,pcis->vec1_B);

    850:   /* Add contribution from saddle point */
    851:   if (mat_ctx->l2g_p) {
    852:     VecScatterBegin(mat_ctx->l2g_p,x,mat_ctx->vP,INSERT_VALUES,SCATTER_REVERSE);
    853:     VecScatterEnd(mat_ctx->l2g_p,x,mat_ctx->vP,INSERT_VALUES,SCATTER_REVERSE);
    854:     if (pcbddc->switch_static) {
    855:       if (trans) {
    856:         MatMultTranspose(mat_ctx->B_BI,mat_ctx->vP,pcis->vec1_D);
    857:       } else {
    858:         MatMult(mat_ctx->Bt_BI,mat_ctx->vP,pcis->vec1_D);
    859:       }
    860:     }
    861:     if (trans) {
    862:       MatMultTransposeAdd(mat_ctx->B_BB,mat_ctx->vP,pcis->vec1_B,pcis->vec1_B);
    863:     } else {
    864:       MatMultAdd(mat_ctx->Bt_BB,mat_ctx->vP,pcis->vec1_B,pcis->vec1_B);
    865:     }
    866:   } else {
    867:     if (pcbddc->switch_static) {
    868:       VecSet(pcis->vec1_D,0.0);
    869:     }
    870:   }
    871:   /* Application of \widetilde{S}^-1 */
    872:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
    873:   PCBDDCApplyInterfacePreconditioner(mat_ctx->pc,trans);
    874:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
    875:   VecSet(y,0.0);
    876:   /* Application of B_delta */
    877:   MatMult(mat_ctx->B_delta,pcis->vec1_B,mat_ctx->lambda_local);
    878:   /* Contribution from boundary pressures */
    879:   if (mat_ctx->C) {
    880:     const PetscScalar *lx;
    881:     PetscScalar       *ly;

    883:     /* pressure ordered first in the local part of x and y */
    884:     VecGetArrayRead(x,&lx);
    885:     VecGetArray(y,&ly);
    886:     VecPlaceArray(mat_ctx->xPg,lx);
    887:     VecPlaceArray(mat_ctx->yPg,ly);
    888:     if (trans) {
    889:       MatMultTranspose(mat_ctx->C,mat_ctx->xPg,mat_ctx->yPg);
    890:     } else {
    891:       MatMult(mat_ctx->C,mat_ctx->xPg,mat_ctx->yPg);
    892:     }
    893:     VecResetArray(mat_ctx->xPg);
    894:     VecResetArray(mat_ctx->yPg);
    895:     VecRestoreArrayRead(x,&lx);
    896:     VecRestoreArray(y,&ly);
    897:   }
    898:   /* Add contribution from saddle point */
    899:   if (mat_ctx->l2g_p) {
    900:     if (trans) {
    901:       MatMultTranspose(mat_ctx->Bt_BB,pcis->vec1_B,mat_ctx->vP);
    902:     } else {
    903:       MatMult(mat_ctx->B_BB,pcis->vec1_B,mat_ctx->vP);
    904:     }
    905:     if (pcbddc->switch_static) {
    906:       if (trans) {
    907:         MatMultTransposeAdd(mat_ctx->Bt_BI,pcis->vec1_D,mat_ctx->vP,mat_ctx->vP);
    908:       } else {
    909:         MatMultAdd(mat_ctx->B_BI,pcis->vec1_D,mat_ctx->vP,mat_ctx->vP);
    910:       }
    911:     }
    912:     VecScatterBegin(mat_ctx->l2g_p,mat_ctx->vP,y,ADD_VALUES,SCATTER_FORWARD);
    913:     VecScatterEnd(mat_ctx->l2g_p,mat_ctx->vP,y,ADD_VALUES,SCATTER_FORWARD);
    914:   }
    915:   VecScatterBegin(mat_ctx->l2g_lambda,mat_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
    916:   VecScatterEnd(mat_ctx->l2g_lambda,mat_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
    917:   return 0;
    918: }

    920: PetscErrorCode FETIDPMatMult(Mat fetimat, Vec x, Vec y)
    921: {
    922:   FETIDPMatMult_Kernel(fetimat,x,y,PETSC_FALSE);
    923:   return 0;
    924: }

    926: PetscErrorCode FETIDPMatMultTranspose(Mat fetimat, Vec x, Vec y)
    927: {
    928:   FETIDPMatMult_Kernel(fetimat,x,y,PETSC_TRUE);
    929:   return 0;
    930: }

    932: PetscErrorCode FETIDPPCApply_Kernel(PC fetipc, Vec x, Vec y, PetscBool trans)
    933: {
    934:   FETIDPPC_ctx   pc_ctx;
    935:   PC_IS          *pcis;

    937:   PCShellGetContext(fetipc,&pc_ctx);
    938:   pcis = (PC_IS*)pc_ctx->pc->data;
    939:   /* Application of B_Ddelta^T */
    940:   VecScatterBegin(pc_ctx->l2g_lambda,x,pc_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
    941:   VecScatterEnd(pc_ctx->l2g_lambda,x,pc_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
    942:   VecSet(pcis->vec2_B,0.0);
    943:   MatMultTranspose(pc_ctx->B_Ddelta,pc_ctx->lambda_local,pcis->vec2_B);
    944:   /* Application of local Schur complement */
    945:   if (trans) {
    946:     MatMultTranspose(pc_ctx->S_j,pcis->vec2_B,pcis->vec1_B);
    947:   } else {
    948:     MatMult(pc_ctx->S_j,pcis->vec2_B,pcis->vec1_B);
    949:   }
    950:   /* Application of B_Ddelta */
    951:   MatMult(pc_ctx->B_Ddelta,pcis->vec1_B,pc_ctx->lambda_local);
    952:   VecSet(y,0.0);
    953:   VecScatterBegin(pc_ctx->l2g_lambda,pc_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
    954:   VecScatterEnd(pc_ctx->l2g_lambda,pc_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
    955:   return 0;
    956: }

    958: PetscErrorCode FETIDPPCApply(PC pc, Vec x, Vec y)
    959: {
    960:   FETIDPPCApply_Kernel(pc,x,y,PETSC_FALSE);
    961:   return 0;
    962: }

    964: PetscErrorCode FETIDPPCApplyTranspose(PC pc, Vec x, Vec y)
    965: {
    966:   FETIDPPCApply_Kernel(pc,x,y,PETSC_TRUE);
    967:   return 0;
    968: }

    970: PetscErrorCode FETIDPPCView(PC pc, PetscViewer viewer)
    971: {
    972:   FETIDPPC_ctx      pc_ctx;
    973:   PetscBool         iascii;
    974:   PetscViewer       sviewer;

    976:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    977:   if (iascii) {
    978:     PetscMPIInt rank;
    979:     PetscBool   isschur,isshell;

    981:     PCShellGetContext(pc,&pc_ctx);
    982:     MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
    983:     PetscObjectTypeCompare((PetscObject)pc_ctx->S_j,MATSCHURCOMPLEMENT,&isschur);
    984:     if (isschur) {
    985:       PetscViewerASCIIPrintf(viewer,"  Dirichlet preconditioner (just from rank 0)\n");
    986:     } else {
    987:       PetscViewerASCIIPrintf(viewer,"  Lumped preconditioner (just from rank 0)\n");
    988:     }
    989:     PetscViewerGetSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
    990:     if (rank == 0) {
    991:       PetscViewerPushFormat(sviewer,PETSC_VIEWER_ASCII_INFO);
    992:       PetscViewerASCIIPushTab(sviewer);
    993:       MatView(pc_ctx->S_j,sviewer);
    994:       PetscViewerASCIIPopTab(sviewer);
    995:       PetscViewerPopFormat(sviewer);
    996:     }
    997:     PetscViewerRestoreSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
    998:     PetscObjectTypeCompare((PetscObject)pc_ctx->B_Ddelta,MATSHELL,&isshell);
    999:     if (isshell) {
    1000:       BDdelta_DN ctx;
    1001:       PetscViewerASCIIPrintf(viewer,"  FETI-DP BDdelta: DB^t * (B D^-1 B^t)^-1 for deluxe scaling (just from rank 0)\n");
    1002:       MatShellGetContext(pc_ctx->B_Ddelta,&ctx);
    1003:       PetscViewerGetSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
    1004:       if (rank == 0) {
    1005:         PetscInt tl;

    1007:         PetscViewerASCIIGetTab(sviewer,&tl);
    1008:         PetscObjectSetTabLevel((PetscObject)ctx->kBD,tl);
    1009:         KSPView(ctx->kBD,sviewer);
    1010:         PetscViewerPushFormat(sviewer,PETSC_VIEWER_ASCII_INFO);
    1011:         MatView(ctx->BD,sviewer);
    1012:         PetscViewerPopFormat(sviewer);
    1013:       }
    1014:       PetscViewerRestoreSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
    1015:     }
    1016:     PetscViewerFlush(viewer);
    1017:   }
    1018:   return 0;
    1019: }