1 2 /* 3 Defines a direct factorization preconditioner for any Mat implementation 4 Note: this need not be consided a preconditioner since it supplies 5 a direct solver. 6 */ 7 8 #include <../src/ksp/pc/impls/factor/lu/lu.h> /*I "petscpc.h" I*/ 9 10 11 PetscErrorCode PCFactorReorderForNonzeroDiagonal_LU(PC pc,PetscReal z) 12 { 13 PC_LU *lu = (PC_LU*)pc->data; 14 15 PetscFunctionBegin; 16 lu->nonzerosalongdiagonal = PETSC_TRUE; 17 if (z == PETSC_DECIDE) lu->nonzerosalongdiagonaltol = 1.e-10; 18 else lu->nonzerosalongdiagonaltol = z; 19 PetscFunctionReturn(0); 20 } 21 22 static PetscErrorCode PCSetFromOptions_LU(PetscOptionItems *PetscOptionsObject,PC pc) 23 { 24 PC_LU *lu = (PC_LU*)pc->data; 25 PetscErrorCode ierr; 26 PetscBool flg = PETSC_FALSE; 27 PetscReal tol; 28 29 PetscFunctionBegin; 30 ierr = PetscOptionsHead(PetscOptionsObject,"LU options");CHKERRQ(ierr); 31 ierr = PCSetFromOptions_Factor(PetscOptionsObject,pc);CHKERRQ(ierr); 32 33 ierr = PetscOptionsName("-pc_factor_nonzeros_along_diagonal","Reorder to remove zeros from diagonal","PCFactorReorderForNonzeroDiagonal",&flg);CHKERRQ(ierr); 34 if (flg) { 35 tol = PETSC_DECIDE; 36 ierr = PetscOptionsReal("-pc_factor_nonzeros_along_diagonal","Reorder to remove zeros from diagonal","PCFactorReorderForNonzeroDiagonal",lu->nonzerosalongdiagonaltol,&tol,0);CHKERRQ(ierr); 37 ierr = PCFactorReorderForNonzeroDiagonal(pc,tol);CHKERRQ(ierr); 38 } 39 ierr = PetscOptionsTail();CHKERRQ(ierr); 40 PetscFunctionReturn(0); 41 } 42 43 static PetscErrorCode PCView_LU(PC pc,PetscViewer viewer) 44 { 45 PetscErrorCode ierr; 46 47 PetscFunctionBegin; 48 ierr = PCView_Factor(pc,viewer);CHKERRQ(ierr); 49 PetscFunctionReturn(0); 50 } 51 52 static PetscErrorCode PCSetUp_LU(PC pc) 53 { 54 PetscErrorCode ierr; 55 PC_LU *dir = (PC_LU*)pc->data; 56 MatSolverType stype; 57 MatFactorError err; 58 59 PetscFunctionBegin; 60 pc->failedreason = PC_NOERROR; 61 if (dir->hdr.reusefill && pc->setupcalled) ((PC_Factor*)dir)->info.fill = dir->hdr.actualfill; 62 63 ierr = MatSetErrorIfFailure(pc->pmat,pc->erroriffailure);CHKERRQ(ierr); 64 if (dir->hdr.inplace) { 65 if (dir->row && dir->col && dir->row != dir->col) {ierr = ISDestroy(&dir->row);CHKERRQ(ierr);} 66 ierr = ISDestroy(&dir->col);CHKERRQ(ierr); 67 ierr = MatGetOrdering(pc->pmat,((PC_Factor*)dir)->ordering,&dir->row,&dir->col);CHKERRQ(ierr); 68 if (dir->row) { 69 ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->row);CHKERRQ(ierr); 70 ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->col);CHKERRQ(ierr); 71 } 72 ierr = MatLUFactor(pc->pmat,dir->row,dir->col,&((PC_Factor*)dir)->info);CHKERRQ(ierr); 73 ierr = MatFactorGetError(pc->pmat,&err);CHKERRQ(ierr); 74 if (err) { /* Factor() fails */ 75 pc->failedreason = (PCFailedReason)err; 76 PetscFunctionReturn(0); 77 } 78 79 ((PC_Factor*)dir)->fact = pc->pmat; 80 } else { 81 MatInfo info; 82 83 if (!pc->setupcalled) { 84 ierr = MatGetOrdering(pc->pmat,((PC_Factor*)dir)->ordering,&dir->row,&dir->col);CHKERRQ(ierr); 85 if (dir->nonzerosalongdiagonal) { 86 ierr = MatReorderForNonzeroDiagonal(pc->pmat,dir->nonzerosalongdiagonaltol,dir->row,dir->col);CHKERRQ(ierr); 87 } 88 if (dir->row) { 89 ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->row);CHKERRQ(ierr); 90 ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->col);CHKERRQ(ierr); 91 } 92 if (!((PC_Factor*)dir)->fact) { 93 ierr = MatGetFactor(pc->pmat,((PC_Factor*)dir)->solvertype,MAT_FACTOR_LU,&((PC_Factor*)dir)->fact);CHKERRQ(ierr); 94 } 95 ierr = MatLUFactorSymbolic(((PC_Factor*)dir)->fact,pc->pmat,dir->row,dir->col,&((PC_Factor*)dir)->info);CHKERRQ(ierr); 96 ierr = MatGetInfo(((PC_Factor*)dir)->fact,MAT_LOCAL,&info);CHKERRQ(ierr); 97 dir->hdr.actualfill = info.fill_ratio_needed; 98 ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)((PC_Factor*)dir)->fact);CHKERRQ(ierr); 99 } else if (pc->flag != SAME_NONZERO_PATTERN) { 100 if (!dir->hdr.reuseordering) { 101 if (dir->row && dir->col && dir->row != dir->col) {ierr = ISDestroy(&dir->row);CHKERRQ(ierr);} 102 ierr = ISDestroy(&dir->col);CHKERRQ(ierr); 103 ierr = MatGetOrdering(pc->pmat,((PC_Factor*)dir)->ordering,&dir->row,&dir->col);CHKERRQ(ierr); 104 if (dir->nonzerosalongdiagonal) { 105 ierr = MatReorderForNonzeroDiagonal(pc->pmat,dir->nonzerosalongdiagonaltol,dir->row,dir->col);CHKERRQ(ierr); 106 } 107 if (dir->row) { 108 ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->row);CHKERRQ(ierr); 109 ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)dir->col);CHKERRQ(ierr); 110 } 111 } 112 ierr = MatDestroy(&((PC_Factor*)dir)->fact);CHKERRQ(ierr); 113 ierr = MatGetFactor(pc->pmat,((PC_Factor*)dir)->solvertype,MAT_FACTOR_LU,&((PC_Factor*)dir)->fact);CHKERRQ(ierr); 114 ierr = MatLUFactorSymbolic(((PC_Factor*)dir)->fact,pc->pmat,dir->row,dir->col,&((PC_Factor*)dir)->info);CHKERRQ(ierr); 115 ierr = MatGetInfo(((PC_Factor*)dir)->fact,MAT_LOCAL,&info);CHKERRQ(ierr); 116 dir->hdr.actualfill = info.fill_ratio_needed; 117 ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)((PC_Factor*)dir)->fact);CHKERRQ(ierr); 118 } else { 119 ierr = MatFactorGetError(((PC_Factor*)dir)->fact,&err);CHKERRQ(ierr); 120 if (err == MAT_FACTOR_NUMERIC_ZEROPIVOT) { 121 ierr = MatFactorClearError(((PC_Factor*)dir)->fact);CHKERRQ(ierr); 122 pc->failedreason = PC_NOERROR; 123 } 124 } 125 ierr = MatFactorGetError(((PC_Factor*)dir)->fact,&err);CHKERRQ(ierr); 126 if (err) { /* FactorSymbolic() fails */ 127 pc->failedreason = (PCFailedReason)err; 128 PetscFunctionReturn(0); 129 } 130 131 ierr = MatLUFactorNumeric(((PC_Factor*)dir)->fact,pc->pmat,&((PC_Factor*)dir)->info);CHKERRQ(ierr); 132 ierr = MatFactorGetError(((PC_Factor*)dir)->fact,&err);CHKERRQ(ierr); 133 if (err) { /* FactorNumeric() fails */ 134 pc->failedreason = (PCFailedReason)err; 135 } 136 137 } 138 139 ierr = PCFactorGetMatSolverType(pc,&stype);CHKERRQ(ierr); 140 if (!stype) { 141 MatSolverType solverpackage; 142 ierr = MatFactorGetSolverType(((PC_Factor*)dir)->fact,&solverpackage);CHKERRQ(ierr); 143 ierr = PCFactorSetMatSolverType(pc,solverpackage);CHKERRQ(ierr); 144 } 145 PetscFunctionReturn(0); 146 } 147 148 static PetscErrorCode PCReset_LU(PC pc) 149 { 150 PC_LU *dir = (PC_LU*)pc->data; 151 PetscErrorCode ierr; 152 153 PetscFunctionBegin; 154 if (!dir->hdr.inplace && ((PC_Factor*)dir)->fact) {ierr = MatDestroy(&((PC_Factor*)dir)->fact);CHKERRQ(ierr);} 155 if (dir->row && dir->col && dir->row != dir->col) {ierr = ISDestroy(&dir->row);CHKERRQ(ierr);} 156 ierr = ISDestroy(&dir->col);CHKERRQ(ierr); 157 PetscFunctionReturn(0); 158 } 159 160 static PetscErrorCode PCDestroy_LU(PC pc) 161 { 162 PC_LU *dir = (PC_LU*)pc->data; 163 PetscErrorCode ierr; 164 165 PetscFunctionBegin; 166 ierr = PCReset_LU(pc);CHKERRQ(ierr); 167 ierr = PetscFree(((PC_Factor*)dir)->ordering);CHKERRQ(ierr); 168 ierr = PetscFree(((PC_Factor*)dir)->solvertype);CHKERRQ(ierr); 169 ierr = PetscFree(pc->data);CHKERRQ(ierr); 170 PetscFunctionReturn(0); 171 } 172 173 static PetscErrorCode PCApply_LU(PC pc,Vec x,Vec y) 174 { 175 PC_LU *dir = (PC_LU*)pc->data; 176 PetscErrorCode ierr; 177 178 PetscFunctionBegin; 179 if (dir->hdr.inplace) { 180 ierr = MatSolve(pc->pmat,x,y);CHKERRQ(ierr); 181 } else { 182 ierr = MatSolve(((PC_Factor*)dir)->fact,x,y);CHKERRQ(ierr); 183 } 184 PetscFunctionReturn(0); 185 } 186 187 static PetscErrorCode PCApplyTranspose_LU(PC pc,Vec x,Vec y) 188 { 189 PC_LU *dir = (PC_LU*)pc->data; 190 PetscErrorCode ierr; 191 192 PetscFunctionBegin; 193 if (dir->hdr.inplace) { 194 ierr = MatSolveTranspose(pc->pmat,x,y);CHKERRQ(ierr); 195 } else { 196 ierr = MatSolveTranspose(((PC_Factor*)dir)->fact,x,y);CHKERRQ(ierr); 197 } 198 PetscFunctionReturn(0); 199 } 200 201 /* -----------------------------------------------------------------------------------*/ 202 203 /*MC 204 PCLU - Uses a direct solver, based on LU factorization, as a preconditioner 205 206 Options Database Keys: 207 + -pc_factor_reuse_ordering - Activate PCFactorSetReuseOrdering() 208 . -pc_factor_mat_solver_type - Actives PCFactorSetMatSolverType() to choose the direct solver, like superlu 209 . -pc_factor_reuse_fill - Activates PCFactorSetReuseFill() 210 . -pc_factor_fill <fill> - Sets fill amount 211 . -pc_factor_in_place - Activates in-place factorization 212 . -pc_factor_mat_ordering_type <nd,rcm,...> - Sets ordering routine 213 . -pc_factor_pivot_in_blocks <true,false> - allow pivoting within the small blocks during factorization (may increase 214 stability of factorization. 215 . -pc_factor_shift_type <shifttype> - Sets shift type or PETSC_DECIDE for the default; use '-help' for a list of available types 216 . -pc_factor_shift_amount <shiftamount> - Sets shift amount or PETSC_DECIDE for the default 217 - -pc_factor_nonzeros_along_diagonal - permutes the rows and columns to try to put nonzero value along the 218 diagonal. 219 220 Notes: Not all options work for all matrix formats 221 Run with -help to see additional options for particular matrix formats or factorization 222 algorithms 223 224 Level: beginner 225 226 Concepts: LU factorization, direct solver 227 228 Notes: Usually this will compute an "exact" solution in one iteration and does 229 not need a Krylov method (i.e. you can use -ksp_type preonly, or 230 KSPSetType(ksp,KSPPREONLY) for the Krylov method 231 232 .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, 233 PCILU, PCCHOLESKY, PCICC, PCFactorSetReuseOrdering(), PCFactorSetReuseFill(), PCFactorGetMatrix(), 234 PCFactorSetFill(), PCFactorSetUseInPlace(), PCFactorSetMatOrderingType(), PCFactorSetColumnPivot(), 235 PCFactorSetPivotingInBlocks(),PCFactorSetShiftType(),PCFactorSetShiftAmount() 236 PCFactorReorderForNonzeroDiagonal() 237 M*/ 238 239 PETSC_EXTERN PetscErrorCode PCCreate_LU(PC pc) 240 { 241 PetscErrorCode ierr; 242 PetscMPIInt size; 243 PC_LU *dir; 244 245 PetscFunctionBegin; 246 ierr = PetscNewLog(pc,&dir);CHKERRQ(ierr); 247 pc->data = (void*)dir; 248 ierr = PCFactorInitialize(pc);CHKERRQ(ierr); 249 ((PC_Factor*)dir)->factortype = MAT_FACTOR_LU; 250 dir->nonzerosalongdiagonal = PETSC_FALSE; 251 252 ((PC_Factor*)dir)->info.fill = 5.0; 253 ((PC_Factor*)dir)->info.dtcol = 1.e-6; /* default to pivoting; this is only thing PETSc LU supports */ 254 ((PC_Factor*)dir)->info.shifttype = (PetscReal)MAT_SHIFT_NONE; 255 dir->col = 0; 256 dir->row = 0; 257 258 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);CHKERRQ(ierr); 259 if (size == 1) { 260 ierr = PetscStrallocpy(MATORDERINGND,(char**)&((PC_Factor*)dir)->ordering);CHKERRQ(ierr); 261 } else { 262 ierr = PetscStrallocpy(MATORDERINGNATURAL,(char**)&((PC_Factor*)dir)->ordering);CHKERRQ(ierr); 263 } 264 265 pc->ops->reset = PCReset_LU; 266 pc->ops->destroy = PCDestroy_LU; 267 pc->ops->apply = PCApply_LU; 268 pc->ops->applytranspose = PCApplyTranspose_LU; 269 pc->ops->setup = PCSetUp_LU; 270 pc->ops->setfromoptions = PCSetFromOptions_LU; 271 pc->ops->view = PCView_LU; 272 pc->ops->applyrichardson = 0; 273 ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFactorReorderForNonzeroDiagonal_C",PCFactorReorderForNonzeroDiagonal_LU);CHKERRQ(ierr); 274 PetscFunctionReturn(0); 275 } 276