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__ "KSPCreate" 637 /*@ 638 KSPCreate - Creates the default KSP context. 639 640 Collective on MPI_Comm 641 642 Input Parameter: 643 . comm - MPI communicator 644 645 Output Parameter: 646 . ksp - location to put the KSP context 647 648 Notes: 649 The default KSP type is GMRES with a restart of 30, using modified Gram-Schmidt 650 orthogonalization. 651 652 Level: beginner 653 654 .keywords: KSP, create, context 655 656 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP 657 @*/ 658 PetscErrorCode KSPCreate(MPI_Comm comm,KSP *inksp) 659 { 660 KSP ksp; 661 PetscErrorCode ierr; 662 void *ctx; 663 664 PetscFunctionBegin; 665 PetscValidPointer(inksp,2); 666 *inksp = 0; 667 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 668 ierr = KSPInitializePackage(NULL);CHKERRQ(ierr); 669 #endif 670 671 ierr = PetscHeaderCreate(ksp,_p_KSP,struct _KSPOps,KSP_CLASSID,"KSP","Krylov Method","KSP",comm,KSPDestroy,KSPView);CHKERRQ(ierr); 672 673 ksp->max_it = 10000; 674 ksp->pc_side = PC_SIDE_DEFAULT; 675 ksp->rtol = 1.e-5; 676 ksp->abstol = 1.e-50; 677 ksp->divtol = 1.e4; 678 679 ksp->chknorm = -1; 680 ksp->normtype = KSP_NORM_DEFAULT; 681 ksp->rnorm = 0.0; 682 ksp->its = 0; 683 ksp->guess_zero = PETSC_TRUE; 684 ksp->calc_sings = PETSC_FALSE; 685 ksp->res_hist = NULL; 686 ksp->res_hist_alloc = NULL; 687 ksp->res_hist_len = 0; 688 ksp->res_hist_max = 0; 689 ksp->res_hist_reset = PETSC_TRUE; 690 ksp->numbermonitors = 0; 691 692 ierr = KSPDefaultConvergedCreate(&ctx);CHKERRQ(ierr); 693 ierr = KSPSetConvergenceTest(ksp,KSPDefaultConverged,ctx,KSPDefaultConvergedDestroy);CHKERRQ(ierr); 694 ksp->ops->buildsolution = KSPBuildSolutionDefault; 695 ksp->ops->buildresidual = KSPBuildResidualDefault; 696 #if defined(PETSC_HAVE_AMS) 697 ((PetscObject)ksp)->bops->publish = KSPPublish_Petsc; 698 #endif 699 700 ksp->vec_sol = 0; 701 ksp->vec_rhs = 0; 702 ksp->pc = 0; 703 ksp->data = 0; 704 ksp->nwork = 0; 705 ksp->work = 0; 706 ksp->reason = KSP_CONVERGED_ITERATING; 707 ksp->setupstage = KSP_SETUP_NEW; 708 709 ierr = KSPNormSupportTableReset_Private(ksp);CHKERRQ(ierr); 710 711 *inksp = ksp; 712 PetscFunctionReturn(0); 713 } 714 715 #undef __FUNCT__ 716 #define __FUNCT__ "KSPSetType" 717 /*@C 718 KSPSetType - Builds KSP for a particular solver. 719 720 Logically Collective on KSP 721 722 Input Parameters: 723 + ksp - the Krylov space context 724 - type - a known method 725 726 Options Database Key: 727 . -ksp_type <method> - Sets the method; use -help for a list 728 of available methods (for instance, cg or gmres) 729 730 Notes: 731 See "petsc/include/petscksp.h" for available methods (for instance, 732 KSPCG or KSPGMRES). 733 734 Normally, it is best to use the KSPSetFromOptions() command and 735 then set the KSP type from the options database rather than by using 736 this routine. Using the options database provides the user with 737 maximum flexibility in evaluating the many different Krylov methods. 738 The KSPSetType() routine is provided for those situations where it 739 is necessary to set the iterative solver independently of the command 740 line or options database. This might be the case, for example, when 741 the choice of iterative solver changes during the execution of the 742 program, and the user's application is taking responsibility for 743 choosing the appropriate method. In other words, this routine is 744 not for beginners. 745 746 Level: intermediate 747 748 .keywords: KSP, set, method 749 750 .seealso: PCSetType(), KSPType 751 752 @*/ 753 PetscErrorCode KSPSetType(KSP ksp, KSPType type) 754 { 755 PetscErrorCode ierr,(*r)(KSP); 756 PetscBool match; 757 758 PetscFunctionBegin; 759 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 760 PetscValidCharPointer(type,2); 761 762 ierr = PetscObjectTypeCompare((PetscObject)ksp,type,&match);CHKERRQ(ierr); 763 if (match) PetscFunctionReturn(0); 764 765 ierr = PetscFunctionListFind(PetscObjectComm((PetscObject)ksp),KSPList,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 766 if (!r) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSP type %s",type); 767 /* Destroy the previous private KSP context */ 768 if (ksp->ops->destroy) { 769 ierr = (*ksp->ops->destroy)(ksp);CHKERRQ(ierr); 770 ksp->ops->destroy = NULL; 771 } 772 /* Reinitialize function pointers in KSPOps structure */ 773 ierr = PetscMemzero(ksp->ops,sizeof(struct _KSPOps));CHKERRQ(ierr); 774 ksp->ops->buildsolution = KSPBuildSolutionDefault; 775 ksp->ops->buildresidual = KSPBuildResidualDefault; 776 ierr = KSPNormSupportTableReset_Private(ksp);CHKERRQ(ierr); 777 /* Call the KSPCreate_XXX routine for this particular Krylov solver */ 778 ksp->setupstage = KSP_SETUP_NEW; 779 ierr = PetscObjectChangeTypeName((PetscObject)ksp,type);CHKERRQ(ierr); 780 ierr = (*r)(ksp);CHKERRQ(ierr); 781 #if defined(PETSC_HAVE_AMS) 782 if (PetscAMSPublishAll) { 783 ierr = PetscObjectAMSPublish((PetscObject)ksp);CHKERRQ(ierr); 784 } 785 #endif 786 PetscFunctionReturn(0); 787 } 788 789 #undef __FUNCT__ 790 #define __FUNCT__ "KSPRegisterDestroy" 791 /*@ 792 KSPRegisterDestroy - Frees the list of KSP methods that were 793 registered by KSPRegisterDynamic(). 794 795 Not Collective 796 797 Level: advanced 798 799 .keywords: KSP, register, destroy 800 801 .seealso: KSPRegisterDynamic(), KSPRegisterAll() 802 @*/ 803 PetscErrorCode KSPRegisterDestroy(void) 804 { 805 PetscErrorCode ierr; 806 807 PetscFunctionBegin; 808 ierr = PetscFunctionListDestroy(&KSPList);CHKERRQ(ierr); 809 KSPRegisterAllCalled = PETSC_FALSE; 810 PetscFunctionReturn(0); 811 } 812 813 #undef __FUNCT__ 814 #define __FUNCT__ "KSPGetType" 815 /*@C 816 KSPGetType - Gets the KSP type as a string from the KSP object. 817 818 Not Collective 819 820 Input Parameter: 821 . ksp - Krylov context 822 823 Output Parameter: 824 . name - name of KSP method 825 826 Level: intermediate 827 828 .keywords: KSP, get, method, name 829 830 .seealso: KSPSetType() 831 @*/ 832 PetscErrorCode KSPGetType(KSP ksp,KSPType *type) 833 { 834 PetscFunctionBegin; 835 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 836 PetscValidPointer(type,2); 837 *type = ((PetscObject)ksp)->type_name; 838 PetscFunctionReturn(0); 839 } 840 841 #undef __FUNCT__ 842 #define __FUNCT__ "KSPRegister" 843 /*@C 844 KSPRegister - See KSPRegisterDynamic() 845 846 Level: advanced 847 @*/ 848 PetscErrorCode KSPRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(KSP)) 849 { 850 PetscErrorCode ierr; 851 char fullname[PETSC_MAX_PATH_LEN]; 852 853 PetscFunctionBegin; 854 ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr); 855 ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&KSPList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 856 PetscFunctionReturn(0); 857 } 858 859 #undef __FUNCT__ 860 #define __FUNCT__ "KSPSetNullSpace" 861 /*@ 862 KSPSetNullSpace - Sets the null space of the operator 863 864 Logically Collective on KSP 865 866 Input Parameters: 867 + ksp - the Krylov space object 868 - nullsp - the null space of the operator 869 870 Level: advanced 871 872 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPGetNullSpace(), MatSetNullSpace() 873 @*/ 874 PetscErrorCode KSPSetNullSpace(KSP ksp,MatNullSpace nullsp) 875 { 876 PetscErrorCode ierr; 877 878 PetscFunctionBegin; 879 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 880 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 881 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 882 if (ksp->nullsp) { ierr = MatNullSpaceDestroy(&ksp->nullsp);CHKERRQ(ierr); } 883 ksp->nullsp = nullsp; 884 PetscFunctionReturn(0); 885 } 886 887 #undef __FUNCT__ 888 #define __FUNCT__ "KSPGetNullSpace" 889 /*@ 890 KSPGetNullSpace - Gets the null space of the operator 891 892 Not Collective 893 894 Input Parameters: 895 + ksp - the Krylov space object 896 - nullsp - the null space of the operator 897 898 Level: advanced 899 900 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPSetNullSpace() 901 @*/ 902 PetscErrorCode KSPGetNullSpace(KSP ksp,MatNullSpace *nullsp) 903 { 904 PetscFunctionBegin; 905 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 906 PetscValidPointer(nullsp,2); 907 *nullsp = ksp->nullsp; 908 PetscFunctionReturn(0); 909 } 910 911