Date: 2016-07-24T17:04:31+00:00

Actual source code: ilu.c
petsc-3.7.3 2016-07-24
Report Typos and Errors

      2: /*
      3:    Defines a ILU factorization preconditioner for any Mat implementation
      4: */
      5: #include <../src/ksp/pc/impls/factor/ilu/ilu.h>     /*I "petscpc.h"  I*/

      7: /* ------------------------------------------------------------------------------------------*/
     10: PetscErrorCode  PCFactorSetReuseFill_ILU(PC pc,PetscBool flag)
     11: {
     12:   PC_ILU *lu = (PC_ILU*)pc->data;

     15:   lu->reusefill = flag;
     16:   return(0);
     17: }

     21: PetscErrorCode  PCFactorReorderForNonzeroDiagonal_ILU(PC pc,PetscReal z)
     22: {
     23:   PC_ILU *ilu = (PC_ILU*)pc->data;

     26:   ilu->nonzerosalongdiagonal = PETSC_TRUE;
     27:   if (z == PETSC_DECIDE) ilu->nonzerosalongdiagonaltol = 1.e-10;
     28:   else ilu->nonzerosalongdiagonaltol = z;
     29:   return(0);
     30: }

     34: PetscErrorCode PCReset_ILU(PC pc)
     35: {
     36:   PC_ILU         *ilu = (PC_ILU*)pc->data;

     40:   if (!ilu->inplace) {MatDestroy(&((PC_Factor*)ilu)->fact);}
     41:   if (ilu->row && ilu->col && ilu->row != ilu->col) {ISDestroy(&ilu->row);}
     42:   ISDestroy(&ilu->col);
     43:   return(0);
     44: }

     48: PetscErrorCode  PCFactorSetDropTolerance_ILU(PC pc,PetscReal dt,PetscReal dtcol,PetscInt dtcount)
     49: {
     50:   PC_ILU *ilu = (PC_ILU*)pc->data;

     53:   if (pc->setupcalled && (((PC_Factor*)ilu)->info.dt != dt || ((PC_Factor*)ilu)->info.dtcol != dtcol || ((PC_Factor*)ilu)->info.dtcount != dtcount)) {
     54:     SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cannot change drop tolerance after using PC");
     55:   }
     56:   ((PC_Factor*)ilu)->info.dt      = dt;
     57:   ((PC_Factor*)ilu)->info.dtcol   = dtcol;
     58:   ((PC_Factor*)ilu)->info.dtcount = dtcount;
     59:   ((PC_Factor*)ilu)->info.usedt   = 1.0;
     60:   return(0);
     61: }

     65: PetscErrorCode  PCFactorSetReuseOrdering_ILU(PC pc,PetscBool flag)
     66: {
     67:   PC_ILU *ilu = (PC_ILU*)pc->data;

     70:   ilu->reuseordering = flag;
     71:   return(0);
     72: }

     76: PetscErrorCode  PCFactorSetUseInPlace_ILU(PC pc,PetscBool flg)
     77: {
     78:   PC_ILU *dir = (PC_ILU*)pc->data;

     81:   dir->inplace = flg;
     82:   return(0);
     83: }

     87: PetscErrorCode  PCFactorGetUseInPlace_ILU(PC pc,PetscBool *flg)
     88: {
     89:   PC_ILU *dir = (PC_ILU*)pc->data;

     92:   *flg = dir->inplace;
     93:   return(0);
     94: }

     98: static PetscErrorCode PCSetFromOptions_ILU(PetscOptionItems *PetscOptionsObject,PC pc)
     99: {
    101:   PetscInt       itmp;
    102:   PetscBool      flg,set;
    103:   PC_ILU         *ilu = (PC_ILU*)pc->data;
    104:   PetscReal      tol;

    107:   PetscOptionsHead(PetscOptionsObject,"ILU Options");
    108:   PCSetFromOptions_Factor(PetscOptionsObject,pc);

    110:   PetscOptionsInt("-pc_factor_levels","levels of fill","PCFactorSetLevels",(PetscInt)((PC_Factor*)ilu)->info.levels,&itmp,&flg);
    111:   if (flg) ((PC_Factor*)ilu)->info.levels = itmp;

    113:   PetscOptionsBool("-pc_factor_diagonal_fill","Allow fill into empty diagonal entry","PCFactorSetAllowDiagonalFill",((PC_Factor*)ilu)->info.diagonal_fill ? PETSC_TRUE : PETSC_FALSE,&flg,&set);
    114:   if (set) ((PC_Factor*)ilu)->info.diagonal_fill = (PetscReal) flg;
    115:   PetscOptionsName("-pc_factor_nonzeros_along_diagonal","Reorder to remove zeros from diagonal","PCFactorReorderForNonzeroDiagonal",&flg);
    116:   if (flg) {
    117:     tol  = PETSC_DECIDE;
    118:     PetscOptionsReal("-pc_factor_nonzeros_along_diagonal","Reorder to remove zeros from diagonal","PCFactorReorderForNonzeroDiagonal",ilu->nonzerosalongdiagonaltol,&tol,0);
    119:     PCFactorReorderForNonzeroDiagonal(pc,tol);
    120:   }

    122:   PetscOptionsTail();
    123:   return(0);
    124: }

    128: static PetscErrorCode PCView_ILU(PC pc,PetscViewer viewer)
    129: {
    130:   PC_ILU         *ilu = (PC_ILU*)pc->data;
    132:   PetscBool      iascii;

    135:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    136:   if (iascii) {
    137:     if (ilu->inplace) {
    138:       PetscViewerASCIIPrintf(viewer,"  ILU: in-place factorization\n");
    139:     } else {
    140:       PetscViewerASCIIPrintf(viewer,"  ILU: out-of-place factorization\n");
    141:     }

    143:     if (ilu->reusefill)     {PetscViewerASCIIPrintf(viewer,"  ILU: Reusing fill from past factorization\n");}
    144:     if (ilu->reuseordering) {PetscViewerASCIIPrintf(viewer,"  ILU: Reusing reordering from past factorization\n");}
    145:   }
    146:   PCView_Factor(pc,viewer);
    147:   return(0);
    148: }

    152: static PetscErrorCode PCSetUp_ILU(PC pc)
    153: {
    155:   PC_ILU         *ilu = (PC_ILU*)pc->data;
    156:   MatInfo        info;
    157:   PetscBool      flg;
    158:   const MatSolverPackage stype;

    161:   /* ugly hack to change default, since it is not support by some matrix types */
    162:   if (((PC_Factor*)ilu)->info.shifttype == (PetscReal)MAT_SHIFT_NONZERO) {
    163:     PetscObjectTypeCompare((PetscObject)pc->pmat,MATSEQAIJ,&flg);
    164:     if (!flg) {
    165:       PetscObjectTypeCompare((PetscObject)pc->pmat,MATMPIAIJ,&flg);
    166:       if (!flg) {
    167:         ((PC_Factor*)ilu)->info.shifttype = (PetscReal)MAT_SHIFT_INBLOCKS;
    168:         PetscInfo(pc,"Changing shift type from NONZERO to INBLOCKS because block matrices do not support NONZERO\n");
    169:       }
    170:     }
    171:   }

    173:   MatSetErrorIfFailure(pc->pmat,pc->erroriffailure);
    174:   if (ilu->inplace) {
    175:     if (!pc->setupcalled) {

    177:       /* In-place factorization only makes sense with the natural ordering,
    178:          so we only need to get the ordering once, even if nonzero structure changes */
    179:       MatGetOrdering(pc->pmat,((PC_Factor*)ilu)->ordering,&ilu->row,&ilu->col);
    180:       if (ilu->row) {PetscLogObjectParent((PetscObject)pc,(PetscObject)ilu->row);}
    181:       if (ilu->col) {PetscLogObjectParent((PetscObject)pc,(PetscObject)ilu->col);}
    182:     }

    184:     /* In place ILU only makes sense with fill factor of 1.0 because
    185:        cannot have levels of fill */
    186:     ((PC_Factor*)ilu)->info.fill          = 1.0;
    187:     ((PC_Factor*)ilu)->info.diagonal_fill = 0.0;

    189:     MatILUFactor(pc->pmat,ilu->row,ilu->col,&((PC_Factor*)ilu)->info);
    190:     if (pc->pmat->errortype) { /* Factor() fails */
    191:       pc->failedreason = (PCFailedReason)pc->pmat->errortype;
    192:       return(0);
    193:     }

    195:     ((PC_Factor*)ilu)->fact = pc->pmat;
    196:     /* must update the pc record of the matrix state or the PC will attempt to run PCSetUp() yet again */
    197:     PetscObjectStateGet((PetscObject)pc->pmat,&pc->matstate);
    198:   } else {
    199:     Mat F;
    200:     if (!pc->setupcalled) {
    201:       /* first time in so compute reordering and symbolic factorization */
    202:       MatGetOrdering(pc->pmat,((PC_Factor*)ilu)->ordering,&ilu->row,&ilu->col);
    203:       if (ilu->row) {PetscLogObjectParent((PetscObject)pc,(PetscObject)ilu->row);}
    204:       if (ilu->col) {PetscLogObjectParent((PetscObject)pc,(PetscObject)ilu->col);}
    205:       /*  Remove zeros along diagonal?     */
    206:       if (ilu->nonzerosalongdiagonal) {
    207:         MatReorderForNonzeroDiagonal(pc->pmat,ilu->nonzerosalongdiagonaltol,ilu->row,ilu->col);
    208:       }
    209:       if (!((PC_Factor*)ilu)->fact) {
    210:         MatGetFactor(pc->pmat,((PC_Factor*)ilu)->solvertype,MAT_FACTOR_ILU,&((PC_Factor*)ilu)->fact);
    211:       }
    212:       MatILUFactorSymbolic(((PC_Factor*)ilu)->fact,pc->pmat,ilu->row,ilu->col,&((PC_Factor*)ilu)->info);
    213:       MatGetInfo(((PC_Factor*)ilu)->fact,MAT_LOCAL,&info);

    215:       ilu->actualfill = info.fill_ratio_needed;

    217:       PetscLogObjectParent((PetscObject)pc,(PetscObject)((PC_Factor*)ilu)->fact);
    218:     } else if (pc->flag != SAME_NONZERO_PATTERN) {
    219:       if (!ilu->reuseordering) {
    220:         /* compute a new ordering for the ILU */
    221:         ISDestroy(&ilu->row);
    222:         ISDestroy(&ilu->col);
    223:         MatGetOrdering(pc->pmat,((PC_Factor*)ilu)->ordering,&ilu->row,&ilu->col);
    224:         if (ilu->row) {PetscLogObjectParent((PetscObject)pc,(PetscObject)ilu->row);}
    225:         if (ilu->col) {PetscLogObjectParent((PetscObject)pc,(PetscObject)ilu->col);}
    226:         /*  Remove zeros along diagonal?     */
    227:         if (ilu->nonzerosalongdiagonal) {
    228:           MatReorderForNonzeroDiagonal(pc->pmat,ilu->nonzerosalongdiagonaltol,ilu->row,ilu->col);
    229:         }
    230:       }
    231:       MatDestroy(&((PC_Factor*)ilu)->fact);
    232:       MatGetFactor(pc->pmat,((PC_Factor*)ilu)->solvertype,MAT_FACTOR_ILU,&((PC_Factor*)ilu)->fact);
    233:       MatILUFactorSymbolic(((PC_Factor*)ilu)->fact,pc->pmat,ilu->row,ilu->col,&((PC_Factor*)ilu)->info);
    234:       MatGetInfo(((PC_Factor*)ilu)->fact,MAT_LOCAL,&info);

    236:       ilu->actualfill = info.fill_ratio_needed;

    238:       PetscLogObjectParent((PetscObject)pc,(PetscObject)((PC_Factor*)ilu)->fact);
    239:     }
    240:     F = ((PC_Factor*)ilu)->fact;
    241:     if (F->errortype) { /* FactorSymbolic() fails */
    242:       pc->failedreason = (PCFailedReason)F->errortype;
    243:       return(0);
    244:     }

    246:     MatLUFactorNumeric(((PC_Factor*)ilu)->fact,pc->pmat,&((PC_Factor*)ilu)->info);
    247:     if (F->errortype) { /* FactorNumeric() fails */
    248:       pc->failedreason = (PCFailedReason)F->errortype;
    249:     }
    250:   }

    252:   PCFactorGetMatSolverPackage(pc,&stype);
    253:   if (!stype) {
    254:     PCFactorSetMatSolverPackage(pc,((PC_Factor*)ilu)->fact->solvertype);
    255:   }
    256:   return(0);
    257: }

    261: static PetscErrorCode PCDestroy_ILU(PC pc)
    262: {
    263:   PC_ILU         *ilu = (PC_ILU*)pc->data;

    267:   PCReset_ILU(pc);
    268:   PetscFree(((PC_Factor*)ilu)->solvertype);
    269:   PetscFree(((PC_Factor*)ilu)->ordering);
    270:   PetscFree(pc->data);
    271:   return(0);
    272: }

    276: static PetscErrorCode PCApply_ILU(PC pc,Vec x,Vec y)
    277: {
    278:   PC_ILU         *ilu = (PC_ILU*)pc->data;

    282:   MatSolve(((PC_Factor*)ilu)->fact,x,y);
    283:   return(0);
    284: }

    288: static PetscErrorCode PCApplyTranspose_ILU(PC pc,Vec x,Vec y)
    289: {
    290:   PC_ILU         *ilu = (PC_ILU*)pc->data;

    294:   MatSolveTranspose(((PC_Factor*)ilu)->fact,x,y);
    295:   return(0);
    296: }

    300: static PetscErrorCode PCApplySymmetricLeft_ILU(PC pc,Vec x,Vec y)
    301: {
    303:   PC_ILU         *icc = (PC_ILU*)pc->data;

    306:   MatForwardSolve(((PC_Factor*)icc)->fact,x,y);
    307:   return(0);
    308: }

    312: static PetscErrorCode PCApplySymmetricRight_ILU(PC pc,Vec x,Vec y)
    313: {
    315:   PC_ILU         *icc = (PC_ILU*)pc->data;

    318:   MatBackwardSolve(((PC_Factor*)icc)->fact,x,y);
    319:   return(0);
    320: }

    322: /*MC
    323:      PCILU - Incomplete factorization preconditioners.

    325:    Options Database Keys:
    326: +  -pc_factor_levels <k> - number of levels of fill for ILU(k)
    327: .  -pc_factor_in_place - only for ILU(0) with natural ordering, reuses the space of the matrix for
    328:                       its factorization (overwrites original matrix)
    329: .  -pc_factor_diagonal_fill - fill in a zero diagonal even if levels of fill indicate it wouldn't be fill
    330: .  -pc_factor_reuse_ordering - reuse ordering of factorized matrix from previous factorization
    331: .  -pc_factor_fill <nfill> - expected amount of fill in factored matrix compared to original matrix, nfill > 1
    332: .  -pc_factor_nonzeros_along_diagonal - reorder the matrix before factorization to remove zeros from the diagonal,
    333:                                    this decreases the chance of getting a zero pivot
    334: .  -pc_factor_mat_ordering_type <natural,nd,1wd,rcm,qmd> - set the row/column ordering of the factored matrix
    335: -  -pc_factor_pivot_in_blocks - for block ILU(k) factorization, i.e. with BAIJ matrices with block size larger
    336:                              than 1 the diagonal blocks are factored with partial pivoting (this increases the
    337:                              stability of the ILU factorization

    339:    Level: beginner

    341:   Concepts: incomplete factorization

    343:    Notes: Only implemented for some matrix formats. (for parallel see PCHYPRE for hypre's ILU)

    345:           For BAIJ matrices this implements a point block ILU

    347:           The "symmetric" application of this preconditioner is not actually symmetric since L is not transpose(U)
    348:           even when the matrix is not symmetric since the U stores the diagonals of the factorization.

    350:           If you are using MATSEQAIJCUSPARSE matrices (or MATMPIAIJCUSPARESE matrices with block Jacobi), factorization
    351:           is never done on the GPU).

    353:    References:
    354: +  1. - T. Dupont, R. Kendall, and H. Rachford. An approximate factorization procedure for solving
    355:    self adjoint elliptic difference equations. SIAM J. Numer. Anal., 5, 1968.
    356: .  2. -  T.A. Oliphant. An implicit numerical method for solving two dimensional timedependent diffusion problems. Quart. Appl. Math., 19, 1961.
    357: -  3. -  TONY F. CHAN AND HENK A. VAN DER VORST, APPROXIMATE AND INCOMPLETE FACTORIZATIONS,
    358:       Chapter in Parallel Numerical
    359:       Algorithms, edited by D. Keyes, A. Semah, V. Venkatakrishnan, ICASE/LaRC Interdisciplinary Series in
    360:       Science and Engineering, Kluwer.

    363: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCSOR, MatOrderingType,
    364:            PCFactorSetZeroPivot(), PCFactorSetShiftSetType(), PCFactorSetAmount(),
    365:            PCFactorSetDropTolerance(),PCFactorSetFill(), PCFactorSetMatOrderingType(), PCFactorSetReuseOrdering(),
    366:            PCFactorSetLevels(), PCFactorSetUseInPlace(), PCFactorSetAllowDiagonalFill(), PCFactorSetPivotInBlocks(),
    367:            PCFactorGetAllowDiagonalFill(), PCFactorGetUseInPlace()

    369: M*/

    373: PETSC_EXTERN PetscErrorCode PCCreate_ILU(PC pc)
    374: {
    376:   PC_ILU         *ilu;

    379:   PetscNewLog(pc,&ilu);

    381:   ((PC_Factor*)ilu)->fact               = 0;
    382:   MatFactorInfoInitialize(&((PC_Factor*)ilu)->info);
    383:   ((PC_Factor*)ilu)->factortype         = MAT_FACTOR_ILU;
    384:   ((PC_Factor*)ilu)->info.levels        = 0.;
    385:   ((PC_Factor*)ilu)->info.fill          = 1.0;
    386:   ilu->col                              = 0;
    387:   ilu->row                              = 0;
    388:   ilu->inplace                          = PETSC_FALSE;
    389:   PetscStrallocpy(MATORDERINGNATURAL,(char**)&((PC_Factor*)ilu)->ordering);
    390:   ilu->reuseordering                    = PETSC_FALSE;
    391:   ((PC_Factor*)ilu)->info.dt            = PETSC_DEFAULT;
    392:   ((PC_Factor*)ilu)->info.dtcount       = PETSC_DEFAULT;
    393:   ((PC_Factor*)ilu)->info.dtcol         = PETSC_DEFAULT;
    394:   ((PC_Factor*)ilu)->info.shifttype     = (PetscReal)MAT_SHIFT_NONE;
    395:   ((PC_Factor*)ilu)->info.shiftamount   = 100.0*PETSC_MACHINE_EPSILON;
    396:   ((PC_Factor*)ilu)->info.zeropivot     = 100.0*PETSC_MACHINE_EPSILON;
    397:   ((PC_Factor*)ilu)->info.pivotinblocks = 1.0;
    398:   ilu->reusefill                        = PETSC_FALSE;
    399:   ((PC_Factor*)ilu)->info.diagonal_fill = 0.0;
    400:   pc->data                              = (void*)ilu;

    402:   pc->ops->reset               = PCReset_ILU;
    403:   pc->ops->destroy             = PCDestroy_ILU;
    404:   pc->ops->apply               = PCApply_ILU;
    405:   pc->ops->applytranspose      = PCApplyTranspose_ILU;
    406:   pc->ops->setup               = PCSetUp_ILU;
    407:   pc->ops->setfromoptions      = PCSetFromOptions_ILU;
    408:   pc->ops->getfactoredmatrix   = PCFactorGetMatrix_Factor;
    409:   pc->ops->view                = PCView_ILU;
    410:   pc->ops->applysymmetricleft  = PCApplySymmetricLeft_ILU;
    411:   pc->ops->applysymmetricright = PCApplySymmetricRight_ILU;
    412:   pc->ops->applyrichardson     = 0;

    414:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetZeroPivot_C",PCFactorSetZeroPivot_Factor);
    415:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetShiftType_C",PCFactorSetShiftType_Factor);
    416:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetShiftAmount_C",PCFactorSetShiftAmount_Factor);
    417:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetMatSolverPackage_C",PCFactorGetMatSolverPackage_Factor);
    418:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetMatSolverPackage_C",PCFactorSetMatSolverPackage_Factor);
    419:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetUpMatSolverPackage_C",PCFactorSetUpMatSolverPackage_Factor);
    420:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetDropTolerance_C",PCFactorSetDropTolerance_ILU);
    421:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetFill_C",PCFactorSetFill_Factor);
    422:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetMatOrderingType_C",PCFactorSetMatOrderingType_Factor);
    423:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetReuseOrdering_C",PCFactorSetReuseOrdering_ILU);
    424:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetReuseFill_C",PCFactorSetReuseFill_ILU);
    425:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetLevels_C",PCFactorSetLevels_Factor);
    426:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetLevels_C",PCFactorGetLevels_Factor);
    427:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetUseInPlace_C",PCFactorSetUseInPlace_ILU);
    428:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetUseInPlace_C",PCFactorGetUseInPlace_ILU);
    429:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetAllowDiagonalFill_C",PCFactorSetAllowDiagonalFill_Factor);
    430:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorGetAllowDiagonalFill_C",PCFactorGetAllowDiagonalFill_Factor);
    431:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorSetPivotInBlocks_C",PCFactorSetPivotInBlocks_Factor);
    432:   PetscObjectComposeFunction((PetscObject)pc,"PCFactorReorderForNonzeroDiagonal_C",PCFactorReorderForNonzeroDiagonal_ILU);
    433:   return(0);
    434: }