1 2 #include <../src/ksp/pc/impls/factor/factor.h> /*I "petscpc.h" I*/ 3 4 /* ------------------------------------------------------------------------------------------*/ 5 6 7 #undef __FUNCT__ 8 #define __FUNCT__ "PCFactorSetUpMatSolverPackage_Factor" 9 PetscErrorCode PCFactorSetUpMatSolverPackage_Factor(PC pc) 10 { 11 PC_Factor *icc = (PC_Factor*)pc->data; 12 PetscErrorCode ierr; 13 14 PetscFunctionBegin; 15 if (!pc->pmat) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"You can only call this routine after the matrix object has been provided to the solver, for example with KSPSetOperators() or SNESSetJacobian()"); 16 if (!pc->setupcalled && !((PC_Factor*)icc)->fact) { 17 ierr = MatGetFactor(pc->pmat,((PC_Factor*)icc)->solvertype,((PC_Factor*)icc)->factortype,&((PC_Factor*)icc)->fact);CHKERRQ(ierr); 18 } 19 PetscFunctionReturn(0); 20 } 21 22 #undef __FUNCT__ 23 #define __FUNCT__ "PCFactorSetZeroPivot_Factor" 24 PetscErrorCode PCFactorSetZeroPivot_Factor(PC pc,PetscReal z) 25 { 26 PC_Factor *ilu = (PC_Factor*)pc->data; 27 28 PetscFunctionBegin; 29 ilu->info.zeropivot = z; 30 PetscFunctionReturn(0); 31 } 32 33 #undef __FUNCT__ 34 #define __FUNCT__ "PCFactorSetShiftType_Factor" 35 PetscErrorCode PCFactorSetShiftType_Factor(PC pc,MatFactorShiftType shifttype) 36 { 37 PC_Factor *dir = (PC_Factor*)pc->data; 38 39 PetscFunctionBegin; 40 if (shifttype == (MatFactorShiftType)PETSC_DECIDE) dir->info.shifttype = (PetscReal) MAT_SHIFT_NONE; 41 else { 42 dir->info.shifttype = (PetscReal) shifttype; 43 if ((shifttype == MAT_SHIFT_NONZERO || shifttype == MAT_SHIFT_INBLOCKS) && dir->info.shiftamount == 0.0) { 44 dir->info.shiftamount = 100.0*PETSC_MACHINE_EPSILON; /* set default amount if user has not called PCFactorSetShiftAmount() yet */ 45 } 46 } 47 PetscFunctionReturn(0); 48 } 49 50 #undef __FUNCT__ 51 #define __FUNCT__ "PCFactorSetShiftAmount_Factor" 52 PetscErrorCode PCFactorSetShiftAmount_Factor(PC pc,PetscReal shiftamount) 53 { 54 PC_Factor *dir = (PC_Factor*)pc->data; 55 56 PetscFunctionBegin; 57 if (shiftamount == (PetscReal) PETSC_DECIDE) dir->info.shiftamount = 100.0*PETSC_MACHINE_EPSILON; 58 else dir->info.shiftamount = shiftamount; 59 PetscFunctionReturn(0); 60 } 61 62 #undef __FUNCT__ 63 #define __FUNCT__ "PCFactorSetDropTolerance_Factor" 64 PetscErrorCode PCFactorSetDropTolerance_Factor(PC pc,PetscReal dt,PetscReal dtcol,PetscInt dtcount) 65 { 66 PC_Factor *ilu = (PC_Factor*)pc->data; 67 68 PetscFunctionBegin; 69 if (pc->setupcalled && (!ilu->info.usedt || ((PC_Factor*)ilu)->info.dt != dt || ((PC_Factor*)ilu)->info.dtcol != dtcol || ((PC_Factor*)ilu)->info.dtcount != dtcount)) { 70 SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Cannot change tolerance after use"); 71 } 72 ilu->info.usedt = PETSC_TRUE; 73 ilu->info.dt = dt; 74 ilu->info.dtcol = dtcol; 75 ilu->info.dtcount = dtcount; 76 ilu->info.fill = PETSC_DEFAULT; 77 PetscFunctionReturn(0); 78 } 79 80 #undef __FUNCT__ 81 #define __FUNCT__ "PCFactorSetFill_Factor" 82 PetscErrorCode PCFactorSetFill_Factor(PC pc,PetscReal fill) 83 { 84 PC_Factor *dir = (PC_Factor*)pc->data; 85 86 PetscFunctionBegin; 87 dir->info.fill = fill; 88 PetscFunctionReturn(0); 89 } 90 91 #undef __FUNCT__ 92 #define __FUNCT__ "PCFactorSetMatOrderingType_Factor" 93 PetscErrorCode PCFactorSetMatOrderingType_Factor(PC pc,MatOrderingType ordering) 94 { 95 PC_Factor *dir = (PC_Factor*)pc->data; 96 PetscErrorCode ierr; 97 PetscBool flg; 98 99 PetscFunctionBegin; 100 if (!pc->setupcalled) { 101 ierr = PetscFree(dir->ordering);CHKERRQ(ierr); 102 ierr = PetscStrallocpy(ordering,(char**)&dir->ordering);CHKERRQ(ierr); 103 } else { 104 ierr = PetscStrcmp(dir->ordering,ordering,&flg);CHKERRQ(ierr); 105 if (!flg) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Cannot change ordering after use"); 106 } 107 PetscFunctionReturn(0); 108 } 109 110 #undef __FUNCT__ 111 #define __FUNCT__ "PCFactorGetLevels_Factor" 112 PetscErrorCode PCFactorGetLevels_Factor(PC pc,PetscInt *levels) 113 { 114 PC_Factor *ilu = (PC_Factor*)pc->data; 115 116 PetscFunctionBegin; 117 *levels = ilu->info.levels; 118 PetscFunctionReturn(0); 119 } 120 121 #undef __FUNCT__ 122 #define __FUNCT__ "PCFactorSetLevels_Factor" 123 PetscErrorCode PCFactorSetLevels_Factor(PC pc,PetscInt levels) 124 { 125 PC_Factor *ilu = (PC_Factor*)pc->data; 126 PetscErrorCode ierr; 127 128 PetscFunctionBegin; 129 if (!pc->setupcalled) ilu->info.levels = levels; 130 else if (ilu->info.levels != levels) { 131 ierr = (*pc->ops->reset)(pc);CHKERRQ(ierr); /* remove previous factored matrices */ 132 pc->setupcalled = 0; /* force a complete rebuild of preconditioner factored matrices */ 133 ilu->info.levels = levels; 134 } else if (ilu->info.usedt) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Cannot change levels after use with ILUdt"); 135 PetscFunctionReturn(0); 136 } 137 138 #undef __FUNCT__ 139 #define __FUNCT__ "PCFactorSetAllowDiagonalFill_Factor" 140 PetscErrorCode PCFactorSetAllowDiagonalFill_Factor(PC pc,PetscBool flg) 141 { 142 PC_Factor *dir = (PC_Factor*)pc->data; 143 144 PetscFunctionBegin; 145 dir->info.diagonal_fill = (PetscReal) flg; 146 PetscFunctionReturn(0); 147 } 148 149 #undef __FUNCT__ 150 #define __FUNCT__ "PCFactorGetAllowDiagonalFill_Factor" 151 PetscErrorCode PCFactorGetAllowDiagonalFill_Factor(PC pc,PetscBool *flg) 152 { 153 PC_Factor *dir = (PC_Factor*)pc->data; 154 155 PetscFunctionBegin; 156 *flg = dir->info.diagonal_fill ? PETSC_TRUE : PETSC_FALSE; 157 PetscFunctionReturn(0); 158 } 159 160 /* ------------------------------------------------------------------------------------------*/ 161 162 #undef __FUNCT__ 163 #define __FUNCT__ "PCFactorSetPivotInBlocks_Factor" 164 PetscErrorCode PCFactorSetPivotInBlocks_Factor(PC pc,PetscBool pivot) 165 { 166 PC_Factor *dir = (PC_Factor*)pc->data; 167 168 PetscFunctionBegin; 169 dir->info.pivotinblocks = pivot ? 1.0 : 0.0; 170 PetscFunctionReturn(0); 171 } 172 173 #undef __FUNCT__ 174 #define __FUNCT__ "PCFactorGetMatrix_Factor" 175 PetscErrorCode PCFactorGetMatrix_Factor(PC pc,Mat *mat) 176 { 177 PC_Factor *ilu = (PC_Factor*)pc->data; 178 179 PetscFunctionBegin; 180 if (!ilu->fact) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ORDER,"Matrix not yet factored; call after KSPSetUp() or PCSetUp()"); 181 *mat = ilu->fact; 182 PetscFunctionReturn(0); 183 } 184 185 #undef __FUNCT__ 186 #define __FUNCT__ "PCFactorSetMatSolverPackage_Factor" 187 PetscErrorCode PCFactorSetMatSolverPackage_Factor(PC pc,const MatSolverPackage stype) 188 { 189 PetscErrorCode ierr; 190 PC_Factor *lu = (PC_Factor*)pc->data; 191 192 PetscFunctionBegin; 193 if (lu->fact) { 194 const MatSolverPackage ltype; 195 PetscBool flg; 196 ierr = MatFactorGetSolverPackage(lu->fact,<ype);CHKERRQ(ierr); 197 ierr = PetscStrcmp(stype,ltype,&flg);CHKERRQ(ierr); 198 if (!flg) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Cannot change solver matrix package after PC has been setup or used"); 199 } else { 200 ierr = PetscFree(lu->solvertype);CHKERRQ(ierr); 201 ierr = PetscStrallocpy(stype,&lu->solvertype);CHKERRQ(ierr); 202 } 203 PetscFunctionReturn(0); 204 } 205 206 #undef __FUNCT__ 207 #define __FUNCT__ "PCFactorGetMatSolverPackage_Factor" 208 PetscErrorCode PCFactorGetMatSolverPackage_Factor(PC pc,const MatSolverPackage *stype) 209 { 210 PC_Factor *lu = (PC_Factor*)pc->data; 211 212 PetscFunctionBegin; 213 *stype = lu->solvertype; 214 PetscFunctionReturn(0); 215 } 216 217 #undef __FUNCT__ 218 #define __FUNCT__ "PCFactorSetColumnPivot_Factor" 219 PetscErrorCode PCFactorSetColumnPivot_Factor(PC pc,PetscReal dtcol) 220 { 221 PC_Factor *dir = (PC_Factor*)pc->data; 222 223 PetscFunctionBegin; 224 if (dtcol < 0.0 || dtcol > 1.0) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Column pivot tolerance is %g must be between 0 and 1",(double)dtcol); 225 dir->info.dtcol = dtcol; 226 PetscFunctionReturn(0); 227 } 228 229 #undef __FUNCT__ 230 #define __FUNCT__ "PCSetFromOptions_Factor" 231 PetscErrorCode PCSetFromOptions_Factor(PetscOptionItems *PetscOptionsObject,PC pc) 232 { 233 PC_Factor *factor = (PC_Factor*)pc->data; 234 PetscErrorCode ierr; 235 PetscBool flg,set; 236 char tname[256], solvertype[64]; 237 PetscFunctionList ordlist; 238 PetscEnum etmp; 239 PetscBool inplace; 240 241 PetscFunctionBegin; 242 ierr = PCFactorGetUseInPlace(pc,&inplace);CHKERRQ(ierr); 243 ierr = PetscOptionsBool("-pc_factor_in_place","Form factored matrix in the same memory as the matrix","PCFactorSetUseInPlace",inplace,&flg,&set);CHKERRQ(ierr); 244 if (set) { 245 ierr = PCFactorSetUseInPlace(pc,flg);CHKERRQ(ierr); 246 } 247 ierr = PetscOptionsReal("-pc_factor_fill","Expected non-zeros in factored matrix","PCFactorSetFill",((PC_Factor*)factor)->info.fill,&((PC_Factor*)factor)->info.fill,NULL);CHKERRQ(ierr); 248 249 ierr = PetscOptionsEnum("-pc_factor_shift_type","Type of shift to add to diagonal","PCFactorSetShiftType",MatFactorShiftTypes,(PetscEnum)(int)((PC_Factor*)factor)->info.shifttype,&etmp,&flg);CHKERRQ(ierr); 250 if (flg) { 251 ierr = PCFactorSetShiftType(pc,(MatFactorShiftType)etmp);CHKERRQ(ierr); 252 } 253 ierr = PetscOptionsReal("-pc_factor_shift_amount","Shift added to diagonal","PCFactorSetShiftAmount",((PC_Factor*)factor)->info.shiftamount,&((PC_Factor*)factor)->info.shiftamount,0);CHKERRQ(ierr); 254 255 ierr = PetscOptionsReal("-pc_factor_zeropivot","Pivot is considered zero if less than","PCFactorSetZeroPivot",((PC_Factor*)factor)->info.zeropivot,&((PC_Factor*)factor)->info.zeropivot,0);CHKERRQ(ierr); 256 ierr = PetscOptionsReal("-pc_factor_column_pivot","Column pivot tolerance (used only for some factorization)","PCFactorSetColumnPivot",((PC_Factor*)factor)->info.dtcol,&((PC_Factor*)factor)->info.dtcol,&flg);CHKERRQ(ierr); 257 258 ierr = PetscOptionsBool("-pc_factor_pivot_in_blocks","Pivot inside matrix dense blocks for BAIJ and SBAIJ","PCFactorSetPivotInBlocks",((PC_Factor*)factor)->info.pivotinblocks ? PETSC_TRUE : PETSC_FALSE,&flg,&set);CHKERRQ(ierr); 259 if (set) { 260 ierr = PCFactorSetPivotInBlocks(pc,flg);CHKERRQ(ierr); 261 } 262 263 ierr = PetscOptionsBool("-pc_factor_reuse_fill","Use fill from previous factorization","PCFactorSetReuseFill",PETSC_FALSE,&flg,&set);CHKERRQ(ierr); 264 if (set) { 265 ierr = PCFactorSetReuseFill(pc,flg);CHKERRQ(ierr); 266 } 267 ierr = PetscOptionsBool("-pc_factor_reuse_ordering","Reuse ordering from previous factorization","PCFactorSetReuseOrdering",PETSC_FALSE,&flg,&set);CHKERRQ(ierr); 268 if (set) { 269 ierr = PCFactorSetReuseOrdering(pc,flg);CHKERRQ(ierr); 270 } 271 272 ierr = MatGetOrderingList(&ordlist);CHKERRQ(ierr); 273 ierr = PetscOptionsFList("-pc_factor_mat_ordering_type","Reordering to reduce nonzeros in factored matrix","PCFactorSetMatOrderingType",ordlist,((PC_Factor*)factor)->ordering,tname,256,&flg);CHKERRQ(ierr); 274 if (flg) { 275 ierr = PCFactorSetMatOrderingType(pc,tname);CHKERRQ(ierr); 276 } 277 278 /* maybe should have MatGetSolverTypes(Mat,&list) like the ordering list */ 279 ierr = PetscOptionsString("-pc_factor_mat_solver_package","Specific direct solver to use","MatGetFactor",((PC_Factor*)factor)->solvertype,solvertype,64,&flg);CHKERRQ(ierr); 280 if (flg) { 281 ierr = PCFactorSetMatSolverPackage(pc,solvertype);CHKERRQ(ierr); 282 } 283 PetscFunctionReturn(0); 284 } 285 286 #undef __FUNCT__ 287 #define __FUNCT__ "PCView_Factor" 288 PetscErrorCode PCView_Factor(PC pc,PetscViewer viewer) 289 { 290 PC_Factor *factor = (PC_Factor*)pc->data; 291 PetscErrorCode ierr; 292 PetscBool isstring,iascii; 293 294 PetscFunctionBegin; 295 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 296 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 297 if (iascii) { 298 if (factor->factortype == MAT_FACTOR_ILU || factor->factortype == MAT_FACTOR_ICC) { 299 if (factor->info.dt > 0) { 300 ierr = PetscViewerASCIIPrintf(viewer," drop tolerance %g\n",(double)factor->info.dt);CHKERRQ(ierr); 301 ierr = PetscViewerASCIIPrintf(viewer," max nonzeros per row %D\n",factor->info.dtcount);CHKERRQ(ierr); 302 ierr = PetscViewerASCIIPrintf(viewer," column permutation tolerance %g\n",(double)factor->info.dtcol);CHKERRQ(ierr); 303 } else if (factor->info.levels == 1) { 304 ierr = PetscViewerASCIIPrintf(viewer," %D level of fill\n",(PetscInt)factor->info.levels);CHKERRQ(ierr); 305 } else { 306 ierr = PetscViewerASCIIPrintf(viewer," %D levels of fill\n",(PetscInt)factor->info.levels);CHKERRQ(ierr); 307 } 308 } 309 310 ierr = PetscViewerASCIIPrintf(viewer," tolerance for zero pivot %g\n",(double)factor->info.zeropivot);CHKERRQ(ierr); 311 if (MatFactorShiftTypesDetail[(int)factor->info.shifttype]) { /* Only print when using a nontrivial shift */ 312 ierr = PetscViewerASCIIPrintf(viewer," using %s [%s]\n",MatFactorShiftTypesDetail[(int)factor->info.shifttype],MatFactorShiftTypes[(int)factor->info.shifttype]);CHKERRQ(ierr); 313 } 314 315 ierr = PetscViewerASCIIPrintf(viewer," matrix ordering: %s\n",factor->ordering);CHKERRQ(ierr); 316 317 if (factor->fact) { 318 MatInfo info; 319 ierr = MatGetInfo(factor->fact,MAT_LOCAL,&info);CHKERRQ(ierr); 320 ierr = PetscViewerASCIIPrintf(viewer," factor fill ratio given %g, needed %g\n",(double)info.fill_ratio_given,(double)info.fill_ratio_needed);CHKERRQ(ierr); 321 ierr = PetscViewerASCIIPrintf(viewer," Factored matrix follows:\n");CHKERRQ(ierr); 322 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 323 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 324 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 325 ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr); 326 ierr = MatView(factor->fact,viewer);CHKERRQ(ierr); 327 ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 328 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 329 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 330 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 331 } 332 333 } else if (isstring) { 334 MatFactorType t; 335 ierr = MatGetFactorType(factor->fact,&t);CHKERRQ(ierr); 336 if (t == MAT_FACTOR_ILU || t == MAT_FACTOR_ICC) { 337 ierr = PetscViewerStringSPrintf(viewer," lvls=%D,order=%s",(PetscInt)factor->info.levels,factor->ordering);CHKERRQ(ierr);CHKERRQ(ierr); 338 } 339 } 340 PetscFunctionReturn(0); 341 } 342