1 2 /* 3 The basic KSP routines, Create, View etc. are here. 4 */ 5 #include <petsc-private/kspimpl.h> /*I "petscksp.h" I*/ 6 7 /* Logging support */ 8 PetscClassId KSP_CLASSID; 9 PetscClassId DMKSP_CLASSID; 10 PetscLogEvent KSP_GMRESOrthogonalization, KSP_SetUp, KSP_Solve; 11 12 /* 13 Contains the list of registered KSP routines 14 */ 15 PetscFunctionList KSPList = 0; 16 PetscBool KSPRegisterAllCalled = PETSC_FALSE; 17 18 #undef __FUNCT__ 19 #define __FUNCT__ "KSPLoad" 20 /*@C 21 KSPLoad - Loads a KSP that has been stored in binary with KSPView(). 22 23 Collective on PetscViewer 24 25 Input Parameters: 26 + newdm - the newly loaded KSP, this needs to have been created with KSPCreate() or 27 some related function before a call to KSPLoad(). 28 - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 29 30 Level: intermediate 31 32 Notes: 33 The type is determined by the data in the file, any type set into the KSP before this call is ignored. 34 35 Notes for advanced users: 36 Most users should not need to know the details of the binary storage 37 format, since KSPLoad() and KSPView() completely hide these details. 38 But for anyone who's interested, the standard binary matrix storage 39 format is 40 .vb 41 has not yet been determined 42 .ve 43 44 .seealso: PetscViewerBinaryOpen(), KSPView(), MatLoad(), VecLoad() 45 @*/ 46 PetscErrorCode KSPLoad(KSP newdm, PetscViewer viewer) 47 { 48 PetscErrorCode ierr; 49 PetscBool isbinary; 50 PetscInt classid; 51 char type[256]; 52 PC pc; 53 54 PetscFunctionBegin; 55 PetscValidHeaderSpecific(newdm,KSP_CLASSID,1); 56 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 57 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 58 if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 59 60 ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 61 if (classid != KSP_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not KSP next in file"); 62 ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 63 ierr = KSPSetType(newdm, type);CHKERRQ(ierr); 64 if (newdm->ops->load) { 65 ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr); 66 } 67 ierr = KSPGetPC(newdm,&pc);CHKERRQ(ierr); 68 ierr = PCLoad(pc,viewer);CHKERRQ(ierr); 69 PetscFunctionReturn(0); 70 } 71 72 #include <petscdraw.h> 73 #undef __FUNCT__ 74 #define __FUNCT__ "KSPView" 75 /*@C 76 KSPView - Prints the KSP data structure. 77 78 Collective on KSP 79 80 Input Parameters: 81 + ksp - the Krylov space context 82 - viewer - visualization context 83 84 Options Database Keys: 85 . -ksp_view - print the ksp data structure at the end of a KSPSolve call 86 87 Note: 88 The available visualization contexts include 89 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 90 - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 91 output where only the first processor opens 92 the file. All other processors send their 93 data to the first processor to print. 94 95 The user can open an alternative visualization context with 96 PetscViewerASCIIOpen() - output to a specified file. 97 98 Level: beginner 99 100 .keywords: KSP, view 101 102 .seealso: PCView(), PetscViewerASCIIOpen() 103 @*/ 104 PetscErrorCode KSPView(KSP ksp,PetscViewer viewer) 105 { 106 PetscErrorCode ierr; 107 PetscBool iascii,isbinary,isdraw; 108 109 PetscFunctionBegin; 110 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 111 if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp)); 112 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 113 PetscCheckSameComm(ksp,1,viewer,2); 114 115 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 116 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 117 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 118 if (iascii) { 119 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ksp,viewer,"KSP Object");CHKERRQ(ierr); 120 if (ksp->ops->view) { 121 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 122 ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr); 123 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 124 } 125 if (ksp->guess_zero) { 126 ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D, initial guess is zero\n",ksp->max_it);CHKERRQ(ierr); 127 } else { 128 ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D\n", ksp->max_it);CHKERRQ(ierr); 129 } 130 if (ksp->guess_knoll) {ierr = PetscViewerASCIIPrintf(viewer," using preconditioner applied to right hand side for initial guess\n");CHKERRQ(ierr);} 131 ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%G, absolute=%G, divergence=%G\n",ksp->rtol,ksp->abstol,ksp->divtol);CHKERRQ(ierr); 132 if (ksp->pc_side == PC_RIGHT) { 133 ierr = PetscViewerASCIIPrintf(viewer," right preconditioning\n");CHKERRQ(ierr); 134 } else if (ksp->pc_side == PC_SYMMETRIC) { 135 ierr = PetscViewerASCIIPrintf(viewer," symmetric preconditioning\n");CHKERRQ(ierr); 136 } else { 137 ierr = PetscViewerASCIIPrintf(viewer," left preconditioning\n");CHKERRQ(ierr); 138 } 139 if (ksp->guess) {ierr = PetscViewerASCIIPrintf(viewer," using Fischers initial guess method %D with size %D\n",ksp->guess->method,ksp->guess->maxl);CHKERRQ(ierr);} 140 if (ksp->dscale) {ierr = PetscViewerASCIIPrintf(viewer," diagonally scaled system\n");CHKERRQ(ierr);} 141 if (ksp->nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached null space\n");CHKERRQ(ierr);} 142 if (!ksp->guess_zero) {ierr = PetscViewerASCIIPrintf(viewer," using nonzero initial guess\n");CHKERRQ(ierr);} 143 ierr = PetscViewerASCIIPrintf(viewer," using %s norm type for convergence test\n",KSPNormTypes[ksp->normtype]);CHKERRQ(ierr); 144 } else if (isbinary) { 145 PetscInt classid = KSP_FILE_CLASSID; 146 MPI_Comm comm; 147 PetscMPIInt rank; 148 char type[256]; 149 150 ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr); 151 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 152 if (!rank) { 153 ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 154 ierr = PetscStrncpy(type,((PetscObject)ksp)->type_name,256);CHKERRQ(ierr); 155 ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 156 } 157 if (ksp->ops->view) { 158 ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr); 159 } 160 } else if (isdraw) { 161 PetscDraw draw; 162 char str[36]; 163 PetscReal x,y,bottom,h; 164 PetscBool flg; 165 166 ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 167 ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 168 ierr = PetscObjectTypeCompare((PetscObject)ksp,KSPPREONLY,&flg);CHKERRQ(ierr); 169 if (!flg) { 170 ierr = PetscStrcpy(str,"KSP: ");CHKERRQ(ierr); 171 ierr = PetscStrcat(str,((PetscObject)ksp)->type_name);CHKERRQ(ierr); 172 ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 173 bottom = y - h; 174 } else { 175 bottom = y; 176 } 177 ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 178 } else if (ksp->ops->view) { 179 ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr); 180 } 181 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 182 ierr = PCView(ksp->pc,viewer);CHKERRQ(ierr); 183 if (isdraw) { 184 PetscDraw draw; 185 ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 186 ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 187 } 188 PetscFunctionReturn(0); 189 } 190 191 192 #undef __FUNCT__ 193 #define __FUNCT__ "KSPSetNormType" 194 /*@ 195 KSPSetNormType - Sets the norm that is used for convergence testing. 196 197 Logically Collective on KSP 198 199 Input Parameter: 200 + ksp - Krylov solver context 201 - normtype - one of 202 $ KSP_NORM_NONE - skips computing the norm, this should only be used if you are using 203 $ the Krylov method as a smoother with a fixed small number of iterations. 204 $ Implicitly sets KSPSkipConverged as KSP convergence test. 205 $ Supported only by CG, Richardson, Bi-CG-stab, CR, and CGS methods. 206 $ KSP_NORM_PRECONDITIONED - the default for left preconditioned solves, uses the l2 norm 207 $ of the preconditioned residual 208 $ KSP_NORM_UNPRECONDITIONED - uses the l2 norm of the true b - Ax residual, supported only by 209 $ CG, CHEBYSHEV, and RICHARDSON, automatically true for right (see KSPSetPCSide()) 210 $ preconditioning.. 211 $ KSP_NORM_NATURAL - supported by KSPCG, KSPCR, KSPCGNE, KSPCGS 212 213 214 Options Database Key: 215 . -ksp_norm_type <none,preconditioned,unpreconditioned,natural> 216 217 Notes: 218 Currently only works with the CG, Richardson, Bi-CG-stab, CR, and CGS methods. 219 220 Level: advanced 221 222 .keywords: KSP, create, context, norms 223 224 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPSkipConverged(), KSPSetCheckNormIteration() 225 @*/ 226 PetscErrorCode KSPSetNormType(KSP ksp,KSPNormType normtype) 227 { 228 PetscErrorCode ierr; 229 230 PetscFunctionBegin; 231 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 232 PetscValidLogicalCollectiveEnum(ksp,normtype,2); 233 ksp->normtype = normtype; 234 if (normtype == KSP_NORM_NONE) { 235 ierr = KSPSetConvergenceTest(ksp,KSPSkipConverged,0,0);CHKERRQ(ierr); 236 ierr = PetscInfo(ksp,"Warning: setting KSPNormType to skip computing the norm\n\ 237 KSP convergence test is implicitly set to KSPSkipConverged\n");CHKERRQ(ierr); 238 } 239 PetscFunctionReturn(0); 240 } 241 242 #undef __FUNCT__ 243 #define __FUNCT__ "KSPSetCheckNormIteration" 244 /*@ 245 KSPSetCheckNormIteration - Sets the first iteration at which the norm of the residual will be 246 computed and used in the convergence test. 247 248 Logically Collective on KSP 249 250 Input Parameter: 251 + ksp - Krylov solver context 252 - it - use -1 to check at all iterations 253 254 Notes: 255 Currently only works with KSPCG, KSPBCGS and KSPIBCGS 256 257 Use KSPSetNormType(ksp,KSP_NORM_NONE) to never check the norm 258 259 On steps where the norm is not computed, the previous norm is still in the variable, so if you run with, for example, 260 -ksp_monitor the residual norm will appear to be unchanged for several iterations (though it is not really unchanged). 261 Level: advanced 262 263 .keywords: KSP, create, context, norms 264 265 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPSkipConverged(), KSPSetNormType() 266 @*/ 267 PetscErrorCode KSPSetCheckNormIteration(KSP ksp,PetscInt it) 268 { 269 PetscFunctionBegin; 270 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 271 PetscValidLogicalCollectiveInt(ksp,it,2); 272 ksp->chknorm = it; 273 PetscFunctionReturn(0); 274 } 275 276 #undef __FUNCT__ 277 #define __FUNCT__ "KSPSetLagNorm" 278 /*@ 279 KSPSetLagNorm - Lags the residual norm calculation so that it is computed as part of the MPI_Allreduce() for 280 computing the inner products for the next iteration. This can reduce communication costs at the expense of doing 281 one additional iteration. 282 283 284 Logically Collective on KSP 285 286 Input Parameter: 287 + ksp - Krylov solver context 288 - flg - PETSC_TRUE or PETSC_FALSE 289 290 Options Database Keys: 291 . -ksp_lag_norm - lag the calculated residual norm 292 293 Notes: 294 Currently only works with KSPIBCGS. 295 296 Use KSPSetNormType(ksp,KSP_NORM_NONE) to never check the norm 297 298 If you lag the norm and run with, for example, -ksp_monitor, the residual norm reported will be the lagged one. 299 Level: advanced 300 301 .keywords: KSP, create, context, norms 302 303 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPSkipConverged(), KSPSetNormType(), KSPSetCheckNormIteration() 304 @*/ 305 PetscErrorCode KSPSetLagNorm(KSP ksp,PetscBool flg) 306 { 307 PetscFunctionBegin; 308 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 309 PetscValidLogicalCollectiveBool(ksp,flg,2); 310 ksp->lagnorm = flg; 311 PetscFunctionReturn(0); 312 } 313 314 #undef __FUNCT__ 315 #define __FUNCT__ "KSPSetSupportedNorm" 316 /*@ 317 KSPSetSupportedNorm - Sets a norm and preconditioner side supported by a KSP 318 319 Logically Collective 320 321 Input Arguments: 322 + ksp - Krylov method 323 . normtype - supported norm type 324 . pcside - preconditioner side that can be used with this norm 325 - preference - integer preference for this combination, larger values have higher priority 326 327 Level: developer 328 329 Notes: 330 This function should be called from the implementation files KSPCreate_XXX() to declare 331 which norms and preconditioner sides are supported. Users should not need to call this 332 function. 333 334 KSP_NORM_NONE is supported by default with all KSP methods and any PC side. If a KSP explicitly does not support 335 KSP_NORM_NONE, it should set this by setting priority=0. 336 337 .seealso: KSPSetNormType(), KSPSetPCSide() 338 @*/ 339 PetscErrorCode KSPSetSupportedNorm(KSP ksp,KSPNormType normtype,PCSide pcside,PetscInt priority) 340 { 341 342 PetscFunctionBegin; 343 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 344 ksp->normsupporttable[normtype][pcside] = priority; 345 PetscFunctionReturn(0); 346 } 347 348 #undef __FUNCT__ 349 #define __FUNCT__ "KSPNormSupportTableReset_Private" 350 PetscErrorCode KSPNormSupportTableReset_Private(KSP ksp) 351 { 352 PetscErrorCode ierr; 353 354 PetscFunctionBegin; 355 ierr = PetscMemzero(ksp->normsupporttable,sizeof(ksp->normsupporttable));CHKERRQ(ierr); 356 ierr = KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_LEFT,1);CHKERRQ(ierr); 357 ierr = KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_RIGHT,1);CHKERRQ(ierr); 358 PetscFunctionReturn(0); 359 } 360 361 #undef __FUNCT__ 362 #define __FUNCT__ "KSPSetUpNorms_Private" 363 PetscErrorCode KSPSetUpNorms_Private(KSP ksp,KSPNormType *normtype,PCSide *pcside) 364 { 365 PetscInt i,j,best,ibest = 0,jbest = 0; 366 367 PetscFunctionBegin; 368 best = 0; 369 for (i=0; i<KSP_NORM_MAX; i++) { 370 for (j=0; j<PC_SIDE_MAX; j++) { 371 if ((ksp->normtype == KSP_NORM_DEFAULT || ksp->normtype == i) 372 && (ksp->pc_side == PC_SIDE_DEFAULT || ksp->pc_side == j) 373 && (ksp->normsupporttable[i][j] > best)) { 374 if (ksp->normtype == KSP_NORM_DEFAULT && i == KSP_NORM_NONE && ksp->normsupporttable[i][j] <= 1) { 375 continue; /* Skip because we don't want to default to no norms unless set by the KSP (preonly). */ 376 } 377 best = ksp->normsupporttable[i][j]; 378 ibest = i; 379 jbest = j; 380 } 381 } 382 } 383 if (best < 1) { 384 if (ksp->normtype == KSP_NORM_DEFAULT && ksp->pc_side == PC_SIDE_DEFAULT) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_PLIB,"The %s KSP implementation did not call KSPSetSupportedNorm()",((PetscObject)ksp)->type_name); 385 if (ksp->normtype == KSP_NORM_DEFAULT) SETERRQ2(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"KSP %s does not support %s",((PetscObject)ksp)->type_name,PCSides[ksp->pc_side]); 386 if (ksp->pc_side == PC_SIDE_DEFAULT) SETERRQ2(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"KSP %s does not support %s",((PetscObject)ksp)->type_name,KSPNormTypes[ksp->normtype]); 387 SETERRQ3(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"KSP %s does not support %s with %s",((PetscObject)ksp)->type_name,KSPNormTypes[ksp->normtype],PCSides[ksp->pc_side]); 388 } 389 *normtype = (KSPNormType)ibest; 390 *pcside = (PCSide)jbest; 391 PetscFunctionReturn(0); 392 } 393 394 #undef __FUNCT__ 395 #define __FUNCT__ "KSPGetNormType" 396 /*@ 397 KSPGetNormType - Gets the norm that is used for convergence testing. 398 399 Not Collective 400 401 Input Parameter: 402 . ksp - Krylov solver context 403 404 Output Parameter: 405 . normtype - norm that is used for convergence testing 406 407 Level: advanced 408 409 .keywords: KSP, create, context, norms 410 411 .seealso: KSPNormType, KSPSetNormType(), KSPSkipConverged() 412 @*/ 413 PetscErrorCode KSPGetNormType(KSP ksp, KSPNormType *normtype) 414 { 415 PetscErrorCode ierr; 416 417 PetscFunctionBegin; 418 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 419 PetscValidPointer(normtype,2); 420 ierr = KSPSetUpNorms_Private(ksp,&ksp->normtype,&ksp->pc_side);CHKERRQ(ierr); 421 *normtype = ksp->normtype; 422 PetscFunctionReturn(0); 423 } 424 425 #if defined(PETSC_HAVE_AMS) 426 #undef __FUNCT__ 427 #define __FUNCT__ "KSPPublish_Petsc" 428 static PetscErrorCode KSPPublish_Petsc(PetscObject obj) 429 { 430 KSP ksp = (KSP) obj; 431 PetscErrorCode ierr; 432 433 PetscFunctionBegin; 434 ierr = AMS_Memory_add_field(obj->amsmem,"its",&ksp->its,1,AMS_INT,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 435 436 if (!ksp->res_hist) { 437 ierr = KSPSetResidualHistory((KSP)obj,NULL,PETSC_DECIDE,PETSC_FALSE);CHKERRQ(ierr); 438 } 439 ierr = AMS_Memory_add_field(obj->amsmem,"res_hist",ksp->res_hist,10,AMS_DOUBLE,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 440 PetscFunctionReturn(0); 441 } 442 #endif 443 444 #undef __FUNCT__ 445 #define __FUNCT__ "KSPSetOperators" 446 /*@ 447 KSPSetOperators - Sets the matrix associated with the linear system 448 and a (possibly) different one associated with the preconditioner. 449 450 Collective on KSP and Mat 451 452 Input Parameters: 453 + ksp - the KSP context 454 . Amat - the matrix that defines the linear system 455 . Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat. 456 - flag - flag indicating information about the preconditioner matrix structure 457 during successive linear solves. This flag is ignored the first time a 458 linear system is solved, and thus is irrelevant when solving just one linear 459 system. 460 461 Notes: 462 The flag can be used to eliminate unnecessary work in the preconditioner 463 during the repeated solution of linear systems of the same size. The 464 available options are 465 $ SAME_PRECONDITIONER - 466 $ Pmat is identical during successive linear solves. 467 $ This option is intended for folks who are using 468 $ different Amat and Pmat matrices and want to reuse the 469 $ same preconditioner matrix. For example, this option 470 $ saves work by not recomputing incomplete factorization 471 $ for ILU/ICC preconditioners. 472 $ SAME_NONZERO_PATTERN - 473 $ Pmat has the same nonzero structure during 474 $ successive linear solves. 475 $ DIFFERENT_NONZERO_PATTERN - 476 $ Pmat does not have the same nonzero structure. 477 478 All future calls to KSPSetOperators() must use the same size matrices! 479 480 Passing a NULL for Amat or Pmat removes the matrix that is currently used. 481 482 If you wish to replace either Amat or Pmat but leave the other one untouched then 483 first call KSPGetOperators() to get the one you wish to keep, call PetscObjectReference() 484 on it and then pass it back in in your call to KSPSetOperators(). 485 486 Caution: 487 If you specify SAME_NONZERO_PATTERN, PETSc believes your assertion 488 and does not check the structure of the matrix. If you erroneously 489 claim that the structure is the same when it actually is not, the new 490 preconditioner will not function correctly. Thus, use this optimization 491 feature carefully! 492 493 If in doubt about whether your preconditioner matrix has changed 494 structure or not, use the flag DIFFERENT_NONZERO_PATTERN. 495 496 Level: beginner 497 498 Alternative usage: If the operators have NOT been set with KSP/PCSetOperators() then the operators 499 are created in PC and returned to the user. In this case, if both operators 500 mat and pmat are requested, two DIFFERENT operators will be returned. If 501 only one is requested both operators in the PC will be the same (i.e. as 502 if one had called KSP/PCSetOperators() with the same argument for both Mats). 503 The user must set the sizes of the returned matrices and their type etc just 504 as if the user created them with MatCreate(). For example, 505 506 $ KSP/PCGetOperators(ksp/pc,&mat,NULL,NULL); is equivalent to 507 $ set size, type, etc of mat 508 509 $ MatCreate(comm,&mat); 510 $ KSP/PCSetOperators(ksp/pc,mat,mat,SAME_NONZERO_PATTERN); 511 $ PetscObjectDereference((PetscObject)mat); 512 $ set size, type, etc of mat 513 514 and 515 516 $ KSP/PCGetOperators(ksp/pc,&mat,&pmat,NULL); is equivalent to 517 $ set size, type, etc of mat and pmat 518 519 $ MatCreate(comm,&mat); 520 $ MatCreate(comm,&pmat); 521 $ KSP/PCSetOperators(ksp/pc,mat,pmat,SAME_NONZERO_PATTERN); 522 $ PetscObjectDereference((PetscObject)mat); 523 $ PetscObjectDereference((PetscObject)pmat); 524 $ set size, type, etc of mat and pmat 525 526 The rational for this support is so that when creating a TS, SNES, or KSP the hierarchy 527 of underlying objects (i.e. SNES, KSP, PC, Mat) and their livespans can be completely 528 managed by the top most level object (i.e. the TS, SNES, or KSP). Another way to look 529 at this is when you create a SNES you do not NEED to create a KSP and attach it to 530 the SNES object (the SNES object manages it for you). Similarly when you create a KSP 531 you do not need to attach a PC to it (the KSP object manages the PC object for you). 532 Thus, why should YOU have to create the Mat and attach it to the SNES/KSP/PC, when 533 it can be created for you? 534 535 .keywords: KSP, set, operators, matrix, preconditioner, linear system 536 537 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPGetOperators() 538 @*/ 539 PetscErrorCode KSPSetOperators(KSP ksp,Mat Amat,Mat Pmat,MatStructure flag) 540 { 541 MatNullSpace nullsp; 542 PetscErrorCode ierr; 543 544 PetscFunctionBegin; 545 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 546 if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 547 if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 548 if (Amat) PetscCheckSameComm(ksp,1,Amat,2); 549 if (Pmat) PetscCheckSameComm(ksp,1,Pmat,3); 550 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 551 ierr = PCSetOperators(ksp->pc,Amat,Pmat,flag);CHKERRQ(ierr); 552 if (ksp->setupstage == KSP_SETUP_NEWRHS) ksp->setupstage = KSP_SETUP_NEWMATRIX; /* so that next solve call will call PCSetUp() on new matrix */ 553 if (ksp->guess) { 554 ierr = KSPFischerGuessReset(ksp->guess);CHKERRQ(ierr); 555 } 556 if (Pmat) { 557 ierr = MatGetNullSpace(Pmat, &nullsp);CHKERRQ(ierr); 558 if (nullsp) { 559 ierr = KSPSetNullSpace(ksp, nullsp);CHKERRQ(ierr); 560 } 561 } 562 PetscFunctionReturn(0); 563 } 564 565 #undef __FUNCT__ 566 #define __FUNCT__ "KSPGetOperators" 567 /*@ 568 KSPGetOperators - Gets the matrix associated with the linear system 569 and a (possibly) different one associated with the preconditioner. 570 571 Collective on KSP and Mat 572 573 Input Parameter: 574 . ksp - the KSP context 575 576 Output Parameters: 577 + Amat - the matrix that defines the linear system 578 . Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat. 579 - flag - flag indicating information about the preconditioner matrix structure 580 during successive linear solves. This flag is ignored the first time a 581 linear system is solved, and thus is irrelevant when solving just one linear 582 system. 583 584 Level: intermediate 585 586 Notes: DOES NOT increase the reference counts of the matrix, so you should NOT destroy them. 587 588 .keywords: KSP, set, get, operators, matrix, preconditioner, linear system 589 590 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPSetOperators(), KSPGetOperatorsSet() 591 @*/ 592 PetscErrorCode KSPGetOperators(KSP ksp,Mat *Amat,Mat *Pmat,MatStructure *flag) 593 { 594 PetscErrorCode ierr; 595 596 PetscFunctionBegin; 597 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 598 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 599 ierr = PCGetOperators(ksp->pc,Amat,Pmat,flag);CHKERRQ(ierr); 600 PetscFunctionReturn(0); 601 } 602 603 #undef __FUNCT__ 604 #define __FUNCT__ "KSPGetOperatorsSet" 605 /*@C 606 KSPGetOperatorsSet - Determines if the matrix associated with the linear system and 607 possibly a different one associated with the preconditioner have been set in the KSP. 608 609 Not collective, though the results on all processes should be the same 610 611 Input Parameter: 612 . pc - the KSP context 613 614 Output Parameters: 615 + mat - the matrix associated with the linear system was set 616 - pmat - matrix associated with the preconditioner was set, usually the same 617 618 Level: intermediate 619 620 .keywords: KSP, get, operators, matrix, linear system 621 622 .seealso: PCSetOperators(), KSPGetOperators(), KSPSetOperators(), PCGetOperators(), PCGetOperatorsSet() 623 @*/ 624 PetscErrorCode KSPGetOperatorsSet(KSP ksp,PetscBool *mat,PetscBool *pmat) 625 { 626 PetscErrorCode ierr; 627 628 PetscFunctionBegin; 629 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 630 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 631 ierr = PCGetOperatorsSet(ksp->pc,mat,pmat);CHKERRQ(ierr); 632 PetscFunctionReturn(0); 633 } 634 635 #undef __FUNCT__ 636 #define __FUNCT__ "KSPSetPreSolve" 637 /*@C 638 KSPSetPreSolve - Sets a function that is called before every KSPSolve() is started 639 640 Logically Collective on KSP 641 642 Input Parameters: 643 + ksp - the solver object 644 . presolve - the function to call before the solve 645 - prectx - any context needed by the function 646 647 Level: developer 648 649 .keywords: KSP, create, context 650 651 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP, KSPSetPostSolve() 652 @*/ 653 PetscErrorCode KSPSetPreSolve(KSP ksp,PetscErrorCode (*presolve)(KSP,Vec,Vec,void*),void *prectx) 654 { 655 PetscFunctionBegin; 656 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 657 ksp->presolve = presolve; 658 ksp->prectx = prectx; 659 PetscFunctionReturn(0); 660 } 661 662 #undef __FUNCT__ 663 #define __FUNCT__ "KSPSetPostSolve" 664 /*@C 665 KSPSetPostSolve - Sets a function that is called before every KSPSolve() is started 666 667 Logically Collective on KSP 668 669 Input Parameters: 670 + ksp - the solver object 671 . postsolve - the function to call before the solve 672 - postctx - any context needed by the function 673 674 Level: developer 675 676 .keywords: KSP, create, context 677 678 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP, KSPSetPreSolve() 679 @*/ 680 PetscErrorCode KSPSetPostSolve(KSP ksp,PetscErrorCode (*postsolve)(KSP,Vec,Vec,void*),void *postctx) 681 { 682 PetscFunctionBegin; 683 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 684 ksp->postsolve = postsolve; 685 ksp->postctx = postctx; 686 PetscFunctionReturn(0); 687 } 688 689 #undef __FUNCT__ 690 #define __FUNCT__ "KSPCreate" 691 /*@ 692 KSPCreate - Creates the default KSP context. 693 694 Collective on MPI_Comm 695 696 Input Parameter: 697 . comm - MPI communicator 698 699 Output Parameter: 700 . ksp - location to put the KSP context 701 702 Notes: 703 The default KSP type is GMRES with a restart of 30, using modified Gram-Schmidt 704 orthogonalization. 705 706 Level: beginner 707 708 .keywords: KSP, create, context 709 710 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP 711 @*/ 712 PetscErrorCode KSPCreate(MPI_Comm comm,KSP *inksp) 713 { 714 KSP ksp; 715 PetscErrorCode ierr; 716 void *ctx; 717 718 PetscFunctionBegin; 719 PetscValidPointer(inksp,2); 720 *inksp = 0; 721 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 722 ierr = KSPInitializePackage(NULL);CHKERRQ(ierr); 723 #endif 724 725 ierr = PetscHeaderCreate(ksp,_p_KSP,struct _KSPOps,KSP_CLASSID,"KSP","Krylov Method","KSP",comm,KSPDestroy,KSPView);CHKERRQ(ierr); 726 727 ksp->max_it = 10000; 728 ksp->pc_side = PC_SIDE_DEFAULT; 729 ksp->rtol = 1.e-5; 730 ksp->abstol = 1.e-50; 731 ksp->divtol = 1.e4; 732 733 ksp->chknorm = -1; 734 ksp->normtype = KSP_NORM_DEFAULT; 735 ksp->rnorm = 0.0; 736 ksp->its = 0; 737 ksp->guess_zero = PETSC_TRUE; 738 ksp->calc_sings = PETSC_FALSE; 739 ksp->res_hist = NULL; 740 ksp->res_hist_alloc = NULL; 741 ksp->res_hist_len = 0; 742 ksp->res_hist_max = 0; 743 ksp->res_hist_reset = PETSC_TRUE; 744 ksp->numbermonitors = 0; 745 746 ierr = KSPDefaultConvergedCreate(&ctx);CHKERRQ(ierr); 747 ierr = KSPSetConvergenceTest(ksp,KSPDefaultConverged,ctx,KSPDefaultConvergedDestroy);CHKERRQ(ierr); 748 ksp->ops->buildsolution = KSPBuildSolutionDefault; 749 ksp->ops->buildresidual = KSPBuildResidualDefault; 750 #if defined(PETSC_HAVE_AMS) 751 ((PetscObject)ksp)->bops->publish = KSPPublish_Petsc; 752 #endif 753 754 ksp->vec_sol = 0; 755 ksp->vec_rhs = 0; 756 ksp->pc = 0; 757 ksp->data = 0; 758 ksp->nwork = 0; 759 ksp->work = 0; 760 ksp->reason = KSP_CONVERGED_ITERATING; 761 ksp->setupstage = KSP_SETUP_NEW; 762 763 ierr = KSPNormSupportTableReset_Private(ksp);CHKERRQ(ierr); 764 765 *inksp = ksp; 766 PetscFunctionReturn(0); 767 } 768 769 #undef __FUNCT__ 770 #define __FUNCT__ "KSPSetType" 771 /*@C 772 KSPSetType - Builds KSP for a particular solver. 773 774 Logically Collective on KSP 775 776 Input Parameters: 777 + ksp - the Krylov space context 778 - type - a known method 779 780 Options Database Key: 781 . -ksp_type <method> - Sets the method; use -help for a list 782 of available methods (for instance, cg or gmres) 783 784 Notes: 785 See "petsc/include/petscksp.h" for available methods (for instance, 786 KSPCG or KSPGMRES). 787 788 Normally, it is best to use the KSPSetFromOptions() command and 789 then set the KSP type from the options database rather than by using 790 this routine. Using the options database provides the user with 791 maximum flexibility in evaluating the many different Krylov methods. 792 The KSPSetType() routine is provided for those situations where it 793 is necessary to set the iterative solver independently of the command 794 line or options database. This might be the case, for example, when 795 the choice of iterative solver changes during the execution of the 796 program, and the user's application is taking responsibility for 797 choosing the appropriate method. In other words, this routine is 798 not for beginners. 799 800 Level: intermediate 801 802 .keywords: KSP, set, method 803 804 .seealso: PCSetType(), KSPType 805 806 @*/ 807 PetscErrorCode KSPSetType(KSP ksp, KSPType type) 808 { 809 PetscErrorCode ierr,(*r)(KSP); 810 PetscBool match; 811 812 PetscFunctionBegin; 813 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 814 PetscValidCharPointer(type,2); 815 816 ierr = PetscObjectTypeCompare((PetscObject)ksp,type,&match);CHKERRQ(ierr); 817 if (match) PetscFunctionReturn(0); 818 819 ierr = PetscFunctionListFind(PetscObjectComm((PetscObject)ksp),KSPList,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 820 if (!r) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSP type %s",type); 821 /* Destroy the previous private KSP context */ 822 if (ksp->ops->destroy) { 823 ierr = (*ksp->ops->destroy)(ksp);CHKERRQ(ierr); 824 ksp->ops->destroy = NULL; 825 } 826 /* Reinitialize function pointers in KSPOps structure */ 827 ierr = PetscMemzero(ksp->ops,sizeof(struct _KSPOps));CHKERRQ(ierr); 828 ksp->ops->buildsolution = KSPBuildSolutionDefault; 829 ksp->ops->buildresidual = KSPBuildResidualDefault; 830 ierr = KSPNormSupportTableReset_Private(ksp);CHKERRQ(ierr); 831 /* Call the KSPCreate_XXX routine for this particular Krylov solver */ 832 ksp->setupstage = KSP_SETUP_NEW; 833 ierr = PetscObjectChangeTypeName((PetscObject)ksp,type);CHKERRQ(ierr); 834 ierr = (*r)(ksp);CHKERRQ(ierr); 835 #if defined(PETSC_HAVE_AMS) 836 if (PetscAMSPublishAll) { 837 ierr = PetscObjectAMSPublish((PetscObject)ksp);CHKERRQ(ierr); 838 } 839 #endif 840 PetscFunctionReturn(0); 841 } 842 843 #undef __FUNCT__ 844 #define __FUNCT__ "KSPRegisterDestroy" 845 /*@ 846 KSPRegisterDestroy - Frees the list of KSP methods that were 847 registered by KSPRegisterDynamic(). 848 849 Not Collective 850 851 Level: advanced 852 853 .keywords: KSP, register, destroy 854 855 .seealso: KSPRegisterDynamic(), KSPRegisterAll() 856 @*/ 857 PetscErrorCode KSPRegisterDestroy(void) 858 { 859 PetscErrorCode ierr; 860 861 PetscFunctionBegin; 862 ierr = PetscFunctionListDestroy(&KSPList);CHKERRQ(ierr); 863 KSPRegisterAllCalled = PETSC_FALSE; 864 PetscFunctionReturn(0); 865 } 866 867 #undef __FUNCT__ 868 #define __FUNCT__ "KSPGetType" 869 /*@C 870 KSPGetType - Gets the KSP type as a string from the KSP object. 871 872 Not Collective 873 874 Input Parameter: 875 . ksp - Krylov context 876 877 Output Parameter: 878 . name - name of KSP method 879 880 Level: intermediate 881 882 .keywords: KSP, get, method, name 883 884 .seealso: KSPSetType() 885 @*/ 886 PetscErrorCode KSPGetType(KSP ksp,KSPType *type) 887 { 888 PetscFunctionBegin; 889 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 890 PetscValidPointer(type,2); 891 *type = ((PetscObject)ksp)->type_name; 892 PetscFunctionReturn(0); 893 } 894 895 #undef __FUNCT__ 896 #define __FUNCT__ "KSPRegister" 897 /*@C 898 KSPRegister - See KSPRegisterDynamic() 899 900 Level: advanced 901 @*/ 902 PetscErrorCode KSPRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(KSP)) 903 { 904 PetscErrorCode ierr; 905 char fullname[PETSC_MAX_PATH_LEN]; 906 907 PetscFunctionBegin; 908 ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr); 909 ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&KSPList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 910 PetscFunctionReturn(0); 911 } 912 913 #undef __FUNCT__ 914 #define __FUNCT__ "KSPSetNullSpace" 915 /*@ 916 KSPSetNullSpace - Sets the null space of the operator 917 918 Logically Collective on KSP 919 920 Input Parameters: 921 + ksp - the Krylov space object 922 - nullsp - the null space of the operator 923 924 Level: advanced 925 926 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPGetNullSpace(), MatSetNullSpace() 927 @*/ 928 PetscErrorCode KSPSetNullSpace(KSP ksp,MatNullSpace nullsp) 929 { 930 PetscErrorCode ierr; 931 932 PetscFunctionBegin; 933 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 934 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 935 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 936 if (ksp->nullsp) { ierr = MatNullSpaceDestroy(&ksp->nullsp);CHKERRQ(ierr); } 937 ksp->nullsp = nullsp; 938 PetscFunctionReturn(0); 939 } 940 941 #undef __FUNCT__ 942 #define __FUNCT__ "KSPGetNullSpace" 943 /*@ 944 KSPGetNullSpace - Gets the null space of the operator 945 946 Not Collective 947 948 Input Parameters: 949 + ksp - the Krylov space object 950 - nullsp - the null space of the operator 951 952 Level: advanced 953 954 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPSetNullSpace() 955 @*/ 956 PetscErrorCode KSPGetNullSpace(KSP ksp,MatNullSpace *nullsp) 957 { 958 PetscFunctionBegin; 959 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 960 PetscValidPointer(nullsp,2); 961 *nullsp = ksp->nullsp; 962 PetscFunctionReturn(0); 963 } 964 965