Actual source code: precon.c
1: /*
2: The PC (preconditioner) interface routines, callable by users.
3: */
4: #include <petsc/private/pcimpl.h>
5: #include <petscdm.h>
7: /* Logging support */
8: PetscClassId PC_CLASSID;
9: PetscLogEvent PC_SetUp, PC_SetUpOnBlocks, PC_Apply, PC_MatApply, PC_ApplyCoarse, PC_ApplySymmetricLeft, PC_ApplySymmetricRight, PC_ModifySubMatrices, PC_ApplyOnBlocks, PC_ApplyTransposeOnBlocks;
10: PetscInt PetscMGLevelId;
11: PetscLogStage PCMPIStage;
13: PETSC_INTERN PetscErrorCode PCGetDefaultType_Private(PC pc, const char *type[])
14: {
15: PetscMPIInt size;
16: PetscBool hasopblock, hasopsolve, flg1, flg2, set, flg3, isnormal;
18: PetscFunctionBegin;
19: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
20: if (pc->pmat) {
21: PetscCall(MatHasOperation(pc->pmat, MATOP_GET_DIAGONAL_BLOCK, &hasopblock));
22: PetscCall(MatHasOperation(pc->pmat, MATOP_SOLVE, &hasopsolve));
23: if (size == 1) {
24: PetscCall(MatGetFactorAvailable(pc->pmat, "petsc", MAT_FACTOR_ICC, &flg1));
25: PetscCall(MatGetFactorAvailable(pc->pmat, "petsc", MAT_FACTOR_ILU, &flg2));
26: PetscCall(MatIsSymmetricKnown(pc->pmat, &set, &flg3));
27: PetscCall(PetscObjectTypeCompareAny((PetscObject)pc->pmat, &isnormal, MATNORMAL, MATNORMALHERMITIAN, NULL));
28: if (flg1 && (!flg2 || (set && flg3))) {
29: *type = PCICC;
30: } else if (flg2) {
31: *type = PCILU;
32: } else if (isnormal) {
33: *type = PCNONE;
34: } else if (hasopblock) { /* likely is a parallel matrix run on one processor */
35: if (pc->kspnestlevel > 0) {
36: Mat D;
38: PetscCall(MatGetDiagonalBlock(pc->pmat, &D));
39: PetscCall(PetscObjectTypeCompare((PetscObject)D, ((PetscObject)pc->pmat)->type_name, &flg1)); /* make sure there is no recursive call to PCGetDefaultType_Private() */
40: } else flg1 = PETSC_FALSE;
41: if (!flg1) *type = PCBJACOBI;
42: else *type = PCNONE;
43: } else if (hasopsolve) {
44: *type = PCMAT;
45: } else {
46: *type = PCNONE;
47: }
48: } else {
49: if (hasopblock) {
50: *type = PCBJACOBI;
51: } else if (hasopsolve) {
52: *type = PCMAT;
53: } else {
54: *type = PCNONE;
55: }
56: }
57: } else *type = NULL;
58: PetscFunctionReturn(PETSC_SUCCESS);
59: }
61: /* do not log solves, setup, and applications of preconditioners while constructing preconditioners; perhaps they should be logged separately from the regular solves */
62: PETSC_EXTERN PetscLogEvent KSP_Solve, KSP_SetUp, KSP_MatSolve, KSP_SolveTranspose, KSP_MatSolveTranspose;
64: static PetscErrorCode PCLogEventsDeactivatePush(void)
65: {
66: PetscFunctionBegin;
67: PetscCall(KSPInitializePackage());
68: PetscCall(PetscLogEventDeactivatePush(KSP_Solve));
69: PetscCall(PetscLogEventDeactivatePush(KSP_SetUp));
70: PetscCall(PetscLogEventDeactivatePush(KSP_MatSolve));
71: PetscCall(PetscLogEventDeactivatePush(KSP_SolveTranspose));
72: PetscCall(PetscLogEventDeactivatePush(KSP_MatSolveTranspose));
73: PetscCall(PetscLogEventDeactivatePush(PC_SetUp));
74: PetscCall(PetscLogEventDeactivatePush(PC_SetUpOnBlocks));
75: PetscCall(PetscLogEventDeactivatePush(PC_Apply));
76: PetscCall(PetscLogEventDeactivatePush(PC_MatApply));
77: PetscFunctionReturn(PETSC_SUCCESS);
78: }
80: static PetscErrorCode PCLogEventsDeactivatePop(void)
81: {
82: PetscFunctionBegin;
83: PetscCall(KSPInitializePackage());
84: PetscCall(PetscLogEventDeactivatePop(KSP_Solve));
85: PetscCall(PetscLogEventDeactivatePop(KSP_SetUp));
86: PetscCall(PetscLogEventDeactivatePop(KSP_MatSolve));
87: PetscCall(PetscLogEventDeactivatePop(KSP_SolveTranspose));
88: PetscCall(PetscLogEventDeactivatePop(KSP_MatSolveTranspose));
89: PetscCall(PetscLogEventDeactivatePop(PC_SetUp));
90: PetscCall(PetscLogEventDeactivatePop(PC_SetUpOnBlocks));
91: PetscCall(PetscLogEventDeactivatePop(PC_Apply));
92: PetscCall(PetscLogEventDeactivatePop(PC_MatApply));
93: PetscFunctionReturn(PETSC_SUCCESS);
94: }
96: /*
97: PCMatWorkCreate - Creates a dense work matrix in an empty slot using a vector's row layout and backend.
99: Collective on `v`
101: Input Parameters:
102: + v - vector that supplies the communicator, local and global row counts, and `VecType`
103: . n - local column count, or `PETSC_DECIDE`
104: - N - global column count
106: Input/Output Parameter:
107: . work - matrix slot. If `*work` is `NULL`, the caller receives an owned matrix. An existing matrix is reused as supplied.
109: Level: developer
111: .seealso: `PCMatWorkSetUp()`, `MatCreateDenseFromVecType()`
112: */
113: PetscErrorCode PCMatWorkCreate(Vec v, PetscInt n, PetscInt N, Mat *work)
114: {
115: VecType type;
116: PetscInt m, M;
118: PetscFunctionBegin;
119: if (*work == NULL) {
120: PetscCall(VecGetType(v, &type));
121: PetscCall(VecGetLocalSize(v, &m));
122: PetscCall(VecGetSize(v, &M));
123: PetscCall(MatCreateDenseFromVecType(PetscObjectComm((PetscObject)v), type, m, n, M, N, PETSC_DECIDE, NULL, work));
124: }
125: PetscFunctionReturn(PETSC_SUCCESS);
126: }
128: /*@
129: PCReset - Resets a `PC` context to the state it was in before `PCSetUp()` was called, and removes any allocated `Vec` and `Mat` from its data structure
131: Collective
133: Input Parameter:
134: . pc - the `PC` preconditioner context
136: Level: developer
138: Notes:
139: Any options set, including those set with `KSPSetFromOptions()` remain.
141: This allows a `PC` to be reused for a different sized linear system but using the same options that have been previously set in `pc`
143: .seealso: [](ch_ksp), `PC`, `PCCreate()`, `PCSetUp()`
144: @*/
145: PetscErrorCode PCReset(PC pc)
146: {
147: PetscFunctionBegin;
149: PetscTryTypeMethod(pc, reset);
150: PetscCall(PCMatWorkReset(pc));
151: PetscCall(MatDestroy(&pc->pmat));
152: PetscCall(MatDestroy(&pc->mat));
154: pc->setupcalled = PETSC_FALSE;
155: PetscFunctionReturn(PETSC_SUCCESS);
156: }
158: /*@
159: PCDestroy - Destroys `PC` context that was created with `PCCreate()`.
161: Collective
163: Input Parameter:
164: . pc - the `PC` preconditioner context
166: Level: developer
168: .seealso: [](ch_ksp), `PC`, `PCCreate()`, `PCSetUp()`
169: @*/
170: PetscErrorCode PCDestroy(PC *pc)
171: {
172: PetscFunctionBegin;
173: if (!*pc) PetscFunctionReturn(PETSC_SUCCESS);
175: if (--((PetscObject)*pc)->refct > 0) {
176: *pc = NULL;
177: PetscFunctionReturn(PETSC_SUCCESS);
178: }
180: PetscCall(PCReset(*pc));
182: /* if memory was published with SAWs then destroy it */
183: PetscCall(PetscObjectSAWsViewOff((PetscObject)*pc));
184: PetscTryTypeMethod(*pc, destroy);
185: PetscCall(DMDestroy(&(*pc)->dm));
186: PetscCall(PetscHeaderDestroy(pc));
187: PetscFunctionReturn(PETSC_SUCCESS);
188: }
190: /*@
191: PCSetUseAmat - Sets a flag to indicate that when the preconditioner needs to apply (part of) the
192: operator during the preconditioning process it applies the `Amat` provided to `TSSetRHSJacobian()`,
193: `TSSetIJacobian()`, `SNESSetJacobian()`, `KSPSetOperators()` or `PCSetOperators()` not the `Pmat`.
195: Logically Collective
197: Input Parameters:
198: + pc - the `PC` preconditioner context
199: - flg - `PETSC_TRUE` to use the `Amat`, `PETSC_FALSE` to use the `Pmat` (default is `PETSC_FALSE`)
201: Options Database Key:
202: . -pc_use_amat (true|false) - use the `Amat` argument to `KSPSetOperators()` or `PCSetOperators()` to apply the operator
204: Level: intermediate
206: Note:
207: For the common case in which the linear system matrix and the matrix used to construct the
208: preconditioner are identical, this routine has no effect.
210: .seealso: [](ch_ksp), `PC`, `PCGetUseAmat()`, `PCBJACOBI`, `PCMG`, `PCFIELDSPLIT`, `PCCOMPOSITE`,
211: `KSPSetOperators()`, `PCSetOperators()`
212: @*/
213: PetscErrorCode PCSetUseAmat(PC pc, PetscBool flg)
214: {
215: PetscFunctionBegin;
217: pc->useAmat = flg;
218: PetscFunctionReturn(PETSC_SUCCESS);
219: }
221: /*@
222: PCSetErrorIfFailure - Causes `PC` to generate an error if a floating point exception, for example a zero pivot, is detected.
224: Logically Collective
226: Input Parameters:
227: + pc - iterative context obtained from `PCCreate()`
228: - flg - `PETSC_TRUE` indicates you want the error generated
230: Level: advanced
232: Notes:
233: Normally PETSc continues if a linear solver fails due to a failed setup of a preconditioner, you can call `KSPGetConvergedReason()` after a `KSPSolve()`
234: to determine if it has converged or failed. Or use -ksp_error_if_not_converged to cause the program to terminate as soon as lack of convergence is
235: detected.
237: This is propagated into `KSP`s used by this `PC`, which then propagate it into `PC`s used by those `KSP`s
239: .seealso: [](ch_ksp), `PC`, `KSPSetErrorIfNotConverged()`, `PCGetInitialGuessNonzero()`, `PCSetInitialGuessKnoll()`, `PCGetInitialGuessKnoll()`
240: @*/
241: PetscErrorCode PCSetErrorIfFailure(PC pc, PetscBool flg)
242: {
243: PetscFunctionBegin;
246: pc->erroriffailure = flg;
247: PetscFunctionReturn(PETSC_SUCCESS);
248: }
250: /*@
251: PCGetUseAmat - Gets the flag that indicates that when the preconditioner needs to apply (part of) the
252: operator during the preconditioning process it applies the `Amat` provided to `TSSetRHSJacobian()`,
253: `TSSetIJacobian()`, `SNESSetJacobian()`, `KSPSetOperators()` or `PCSetOperators()` not the `Pmat`.
255: Logically Collective
257: Input Parameter:
258: . pc - the `PC` preconditioner context
260: Output Parameter:
261: . flg - `PETSC_TRUE` to use the `Amat`, `PETSC_FALSE` to use the `Pmat`
263: Level: intermediate
265: .seealso: [](ch_ksp), `PC`, `PCSetUseAmat()`, `PCBJACOBI`, `PCMG`, `PCFIELDSPLIT`, `PCCOMPOSITE`
266: @*/
267: PetscErrorCode PCGetUseAmat(PC pc, PetscBool *flg)
268: {
269: PetscFunctionBegin;
271: *flg = pc->useAmat;
272: PetscFunctionReturn(PETSC_SUCCESS);
273: }
275: /*@
276: PCSetKSPNestLevel - sets the amount of nesting the `KSP` that contains this `PC` has
278: Collective
280: Input Parameters:
281: + pc - the `PC`
282: - level - the nest level
284: Level: developer
286: .seealso: [](ch_ksp), `KSPSetUp()`, `KSPSolve()`, `KSPDestroy()`, `KSP`, `KSPGMRES`, `KSPType`, `KSPGetNestLevel()`, `PCGetKSPNestLevel()`, `KSPSetNestLevel()`
287: @*/
288: PetscErrorCode PCSetKSPNestLevel(PC pc, PetscInt level)
289: {
290: PetscFunctionBegin;
293: pc->kspnestlevel = level;
294: PetscFunctionReturn(PETSC_SUCCESS);
295: }
297: /*@
298: PCGetKSPNestLevel - gets the amount of nesting the `KSP` that contains this `PC` has
300: Not Collective
302: Input Parameter:
303: . pc - the `PC`
305: Output Parameter:
306: . level - the nest level
308: Level: developer
310: .seealso: [](ch_ksp), `KSPSetUp()`, `KSPSolve()`, `KSPDestroy()`, `KSP`, `KSPGMRES`, `KSPType`, `KSPSetNestLevel()`, `PCSetKSPNestLevel()`, `KSPGetNestLevel()`
311: @*/
312: PetscErrorCode PCGetKSPNestLevel(PC pc, PetscInt *level)
313: {
314: PetscFunctionBegin;
316: PetscAssertPointer(level, 2);
317: *level = pc->kspnestlevel;
318: PetscFunctionReturn(PETSC_SUCCESS);
319: }
321: /*@
322: PCCreate - Creates a preconditioner context, `PC`
324: Collective
326: Input Parameter:
327: . comm - MPI communicator
329: Output Parameter:
330: . newpc - location to put the `PC` preconditioner context
332: Level: developer
334: Notes:
335: This is rarely called directly by users since `KSP` manages the `PC` objects it uses. Use `KSPGetPC()` to access the `PC` used by a `KSP`.
337: Use `PCSetType()` or `PCSetFromOptions()` with the option `-pc_type pctype` to set the `PCType` for this `PC`
339: The default preconditioner type `PCType` for sparse matrices is `PCILU` or `PCICC` with 0 fill on one process and block Jacobi (`PCBJACOBI`) with `PCILU` or `PCICC`
340: in parallel. For dense matrices it is always `PCNONE`.
342: .seealso: [](ch_ksp), `PC`, `PCType`, `PCSetType`, `PCSetUp()`, `PCApply()`, `PCDestroy()`, `KSP`, `KSPGetPC()`
343: @*/
344: PetscErrorCode PCCreate(MPI_Comm comm, PC *newpc)
345: {
346: PC pc;
348: PetscFunctionBegin;
349: PetscAssertPointer(newpc, 2);
350: PetscCall(PCInitializePackage());
352: PetscCall(PetscHeaderCreate(pc, PC_CLASSID, "PC", "Preconditioner", "PC", comm, PCDestroy, PCView));
353: PetscCall(PCParametersInitialize(pc));
354: *newpc = pc;
355: PetscFunctionReturn(PETSC_SUCCESS);
356: }
358: /*@
359: PCParametersInitialize - Sets the base defaults for parameters in `pc`, updating a parameter's current value when it matches its previously recorded default.
361: Logically collective
363: Input Parameter:
364: . pc - the `PC` object
366: Level: developer
368: Notes:
370: The base defaults are the non-type-specific values established when the `PC` is created. A `PCType` constructor may subsequently replace them with type-specific defaults.
372: Developer Notes:
374: `PCCreate()` calls this routine to establish the base defaults. `PCSetType()` calls it before constructing a new `PCType`, so the recorded defaults associated with the previous type are replaced before the new type installs its own defaults.
376: Default tracking is based on value equality, not on whether a setter was called. Consequently, an explicitly assigned value that equals the recorded default may be updated when the type changes.
378: .seealso: [](ch_ksp), `PC`, `PCApply()`, `PCDestroy()`, `PetscObjectParameterSetDefault()`
379: @*/
380: PetscErrorCode PCParametersInitialize(PC pc)
381: {
382: PetscObjectParameterSetDefault(pc, useAmat, PETSC_FALSE);
383: return PETSC_SUCCESS;
384: }
386: /*@
387: PCApply - Applies the preconditioner to a vector.
389: Collective
391: Input Parameters:
392: + pc - the `PC` preconditioner context
393: - x - input vector
395: Output Parameter:
396: . y - output vector
398: Level: developer
400: .seealso: [](ch_ksp), `PC`, `PCApplyTranspose()`, `PCApplyBAorAB()`
401: @*/
402: PetscErrorCode PCApply(PC pc, Vec x, Vec y)
403: {
404: PetscInt m, n, mv, nv;
406: PetscFunctionBegin;
410: PetscCheck(x != y, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_IDN, "x and y must be different vectors");
411: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(x, 2, PETSC_TRUE));
412: /* use pmat to check vector sizes since for KSPLSQR the pmat may be of a different size than mat */
413: PetscCall(MatGetLocalSize(pc->pmat, &m, &n));
414: PetscCall(VecGetLocalSize(x, &mv));
415: PetscCall(VecGetLocalSize(y, &nv));
416: /* check pmat * y = x is feasible */
417: PetscCheck(mv == m, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Preconditioner number of local rows %" PetscInt_FMT " does not equal input vector size %" PetscInt_FMT, m, mv);
418: PetscCheck(nv == n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Preconditioner number of local columns %" PetscInt_FMT " does not equal output vector size %" PetscInt_FMT, n, nv);
419: PetscCall(VecSetErrorIfLocked(y, 3));
421: PetscCall(PCSetUp(pc));
422: PetscCall(VecLockReadPush(x));
423: PetscCall(PetscLogEventBegin(PC_Apply, pc, x, y, 0));
424: PetscUseTypeMethod(pc, apply, x, y);
425: PetscCall(PetscLogEventEnd(PC_Apply, pc, x, y, 0));
426: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(y, 3, PETSC_FALSE));
427: PetscCall(VecLockReadPop(x));
428: PetscFunctionReturn(PETSC_SUCCESS);
429: }
431: /*
432: Checks that the blocks of vectors In and Out are dense and have layouts compatible with each other and with Pmat, inname and outname naming them in the error messages
433: */
434: static PetscErrorCode PCMatCheckBlocks_Private(PC pc, Mat In, Mat Out, const char *inname, const char *outname)
435: {
436: Mat A;
437: PetscInt m1, M1, m2, M2, n1, N1, n2, N2, m3, M3, n3, N3;
438: PetscBool match;
440: PetscFunctionBegin;
441: PetscCall(PCGetOperators(pc, NULL, &A));
442: PetscCall(MatGetLocalSize(A, &m3, &n3));
443: PetscCall(MatGetLocalSize(In, &m2, &n2));
444: PetscCall(MatGetLocalSize(Out, &m1, &n1));
445: PetscCall(MatGetSize(A, &M3, &N3));
446: PetscCall(MatGetSize(In, &M2, &N2));
447: PetscCall(MatGetSize(Out, &M1, &N1));
448: PetscCheck(n1 == n2 && N1 == N2, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Incompatible number of columns between block of %s (n,N) = (%" PetscInt_FMT ",%" PetscInt_FMT ") and block of %s (n,N) = (%" PetscInt_FMT ",%" PetscInt_FMT ")", inname, n2, N2, outname, n1, N1);
449: PetscCheck(m2 == m3 && M2 == M3, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Incompatible layout between block of %s (m,M) = (%" PetscInt_FMT ",%" PetscInt_FMT ") and Pmat (m,M)x(n,N) = (%" PetscInt_FMT ",%" PetscInt_FMT ")x(%" PetscInt_FMT ",%" PetscInt_FMT ")", inname, m2, M2, m3, M3, n3, N3);
450: PetscCheck(m1 == n3 && M1 == N3, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Incompatible layout between block of %s (m,M) = (%" PetscInt_FMT ",%" PetscInt_FMT ") and Pmat (m,M)x(n,N) = (%" PetscInt_FMT ",%" PetscInt_FMT ")x(%" PetscInt_FMT ",%" PetscInt_FMT ")", outname, m1, M1, m3, M3, n3, N3);
451: PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)Out, &match, MATSEQDENSE, MATMPIDENSE, ""));
452: PetscCheck(match, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Provided block of %s not stored in a dense Mat", outname);
453: PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)In, &match, MATSEQDENSE, MATMPIDENSE, ""));
454: PetscCheck(match, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Provided block of %s not stored in a dense Mat", inname);
455: PetscFunctionReturn(PETSC_SUCCESS);
456: }
458: static PetscErrorCode PCMatApplyTranspose_Private(PC pc, Mat X, Mat Y, PetscBool transpose)
459: {
460: Vec cy, cx;
461: PetscInt n1, N1;
463: PetscFunctionBegin;
467: PetscCheckSameComm(pc, 1, X, 2);
468: PetscCheckSameComm(pc, 1, Y, 3);
469: PetscCheck(Y != X, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_IDN, "Y and X must be different matrices");
470: PetscCall(PCMatCheckBlocks_Private(pc, X, Y, "input vectors", "output vectors"));
471: PetscCall(MatGetSize(Y, NULL, &N1));
472: PetscCall(PCSetUp(pc));
473: if (!transpose && pc->ops->matapply) {
474: PetscCall(PetscLogEventBegin(PC_MatApply, pc, X, Y, 0));
475: PetscUseTypeMethod(pc, matapply, X, Y);
476: PetscCall(PetscLogEventEnd(PC_MatApply, pc, X, Y, 0));
477: } else if (transpose && pc->ops->matapplytranspose) {
478: PetscCall(PetscLogEventBegin(PC_MatApply, pc, X, Y, 0));
479: PetscUseTypeMethod(pc, matapplytranspose, X, Y);
480: PetscCall(PetscLogEventEnd(PC_MatApply, pc, X, Y, 0));
481: } else {
482: PetscCall(PetscInfo(pc, "PC type %s applying column by column\n", ((PetscObject)pc)->type_name));
483: for (n1 = 0; n1 < N1; ++n1) {
484: PetscCall(MatDenseGetColumnVecRead(X, n1, &cx));
485: PetscCall(MatDenseGetColumnVecWrite(Y, n1, &cy));
486: if (!transpose) PetscCall(PCApply(pc, cx, cy));
487: else PetscCall(PCApplyTranspose(pc, cx, cy));
488: PetscCall(MatDenseRestoreColumnVecWrite(Y, n1, &cy));
489: PetscCall(MatDenseRestoreColumnVecRead(X, n1, &cx));
490: }
491: }
492: PetscFunctionReturn(PETSC_SUCCESS);
493: }
495: /*@
496: PCMatApply - Applies the preconditioner to multiple vectors stored as a `MATDENSE`. Like `PCApply()`, `Y` and `X` must be different matrices.
498: Collective
500: Input Parameters:
501: + pc - the `PC` preconditioner context
502: - X - block of input vectors
504: Output Parameter:
505: . Y - block of output vectors
507: Level: developer
509: .seealso: [](ch_ksp), `PC`, `PCApply()`, `KSPMatSolve()`
510: @*/
511: PetscErrorCode PCMatApply(PC pc, Mat X, Mat Y)
512: {
513: PetscFunctionBegin;
514: PetscCall(PCMatApplyTranspose_Private(pc, X, Y, PETSC_FALSE));
515: PetscFunctionReturn(PETSC_SUCCESS);
516: }
518: /*@
519: PCMatApplyTranspose - Applies the transpose of preconditioner to multiple vectors stored as a `MATDENSE`. Like `PCApplyTranspose()`, `Y` and `X` must be different matrices.
521: Collective
523: Input Parameters:
524: + pc - the `PC` preconditioner context
525: - X - block of input vectors
527: Output Parameter:
528: . Y - block of output vectors
530: Level: developer
532: .seealso: [](ch_ksp), `PC`, `PCApplyTranspose()`, `KSPMatSolveTranspose()`
533: @*/
534: PetscErrorCode PCMatApplyTranspose(PC pc, Mat X, Mat Y)
535: {
536: PetscFunctionBegin;
537: PetscCall(PCMatApplyTranspose_Private(pc, X, Y, PETSC_TRUE));
538: PetscFunctionReturn(PETSC_SUCCESS);
539: }
541: /*@
542: PCApplySymmetricLeft - Applies the left part of a symmetric preconditioner to a vector.
544: Collective
546: Input Parameters:
547: + pc - the `PC` preconditioner context
548: - x - input vector
550: Output Parameter:
551: . y - output vector
553: Level: developer
555: Note:
556: Currently, this routine is implemented only for `PCICC` and `PCJACOBI` preconditioners.
558: .seealso: [](ch_ksp), `PC`, `PCApply()`, `PCApplySymmetricRight()`
559: @*/
560: PetscErrorCode PCApplySymmetricLeft(PC pc, Vec x, Vec y)
561: {
562: PetscFunctionBegin;
566: PetscCheck(x != y, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_IDN, "x and y must be different vectors");
567: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(x, 2, PETSC_TRUE));
568: PetscCall(PCSetUp(pc));
569: PetscCall(VecLockReadPush(x));
570: PetscCall(PetscLogEventBegin(PC_ApplySymmetricLeft, pc, x, y, 0));
571: PetscUseTypeMethod(pc, applysymmetricleft, x, y);
572: PetscCall(PetscLogEventEnd(PC_ApplySymmetricLeft, pc, x, y, 0));
573: PetscCall(VecLockReadPop(x));
574: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(y, 3, PETSC_FALSE));
575: PetscFunctionReturn(PETSC_SUCCESS);
576: }
578: /*@
579: PCApplySymmetricRight - Applies the right part of a symmetric preconditioner to a vector.
581: Collective
583: Input Parameters:
584: + pc - the `PC` preconditioner context
585: - x - input vector
587: Output Parameter:
588: . y - output vector
590: Level: developer
592: Note:
593: Currently, this routine is implemented only for `PCICC` and `PCJACOBI` preconditioners.
595: .seealso: [](ch_ksp), `PC`, `PCApply()`, `PCApplySymmetricLeft()`
596: @*/
597: PetscErrorCode PCApplySymmetricRight(PC pc, Vec x, Vec y)
598: {
599: PetscFunctionBegin;
603: PetscCheck(x != y, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_IDN, "x and y must be different vectors");
604: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(x, 2, PETSC_TRUE));
605: PetscCall(PCSetUp(pc));
606: PetscCall(VecLockReadPush(x));
607: PetscCall(PetscLogEventBegin(PC_ApplySymmetricRight, pc, x, y, 0));
608: PetscUseTypeMethod(pc, applysymmetricright, x, y);
609: PetscCall(PetscLogEventEnd(PC_ApplySymmetricRight, pc, x, y, 0));
610: PetscCall(VecLockReadPop(x));
611: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(y, 3, PETSC_FALSE));
612: PetscFunctionReturn(PETSC_SUCCESS);
613: }
615: /*@
616: PCApplyTranspose - Applies the transpose of preconditioner to a vector.
618: Collective
620: Input Parameters:
621: + pc - the `PC` preconditioner context
622: - x - input vector
624: Output Parameter:
625: . y - output vector
627: Level: developer
629: Note:
630: For complex numbers this applies the non-Hermitian transpose.
632: Developer Note:
633: We need to implement a `PCApplyHermitianTranspose()`
635: .seealso: [](ch_ksp), `PC`, `PCApply()`, `PCApplyBAorAB()`, `PCApplyBAorABTranspose()`, `PCApplyTransposeExists()`
636: @*/
637: PetscErrorCode PCApplyTranspose(PC pc, Vec x, Vec y)
638: {
639: PetscFunctionBegin;
643: PetscCheck(x != y, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_IDN, "x and y must be different vectors");
644: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(x, 2, PETSC_TRUE));
645: PetscCall(PCSetUp(pc));
646: PetscCall(VecLockReadPush(x));
647: PetscCall(PetscLogEventBegin(PC_Apply, pc, x, y, 0));
648: PetscUseTypeMethod(pc, applytranspose, x, y);
649: PetscCall(PetscLogEventEnd(PC_Apply, pc, x, y, 0));
650: PetscCall(VecLockReadPop(x));
651: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(y, 3, PETSC_FALSE));
652: PetscFunctionReturn(PETSC_SUCCESS);
653: }
655: /*@
656: PCApplyTransposeExists - Test whether the preconditioner has a transpose apply operation
658: Collective
660: Input Parameter:
661: . pc - the `PC` preconditioner context
663: Output Parameter:
664: . flg - `PETSC_TRUE` if a transpose operation is defined
666: Level: developer
668: .seealso: [](ch_ksp), `PC`, `PCApplyTranspose()`
669: @*/
670: PetscErrorCode PCApplyTransposeExists(PC pc, PetscBool *flg)
671: {
672: PetscFunctionBegin;
674: PetscAssertPointer(flg, 2);
675: if (pc->ops->applytranspose) *flg = PETSC_TRUE;
676: else *flg = PETSC_FALSE;
677: PetscFunctionReturn(PETSC_SUCCESS);
678: }
680: /*@
681: PCApplyBAorAB - Applies the preconditioner and operator to a vector. $y = B A x$ or $y = A B x$.
683: Collective
685: Input Parameters:
686: + pc - the `PC` preconditioner context
687: . side - indicates the preconditioner side, one of `PC_LEFT`, `PC_RIGHT`, or `PC_SYMMETRIC`
688: . x - input vector
689: - work - work vector
691: Output Parameter:
692: . y - output vector
694: Level: developer
696: .seealso: [](ch_ksp), `PC`, `PCApply()`, `PCApplyTranspose()`, `PCApplyBAorABTranspose()`
697: @*/
698: PetscErrorCode PCApplyBAorAB(PC pc, PCSide side, Vec x, Vec y, Vec work)
699: {
700: PetscFunctionBegin;
706: PetscCheckSameComm(pc, 1, x, 3);
707: PetscCheckSameComm(pc, 1, y, 4);
708: PetscCheckSameComm(pc, 1, work, 5);
709: PetscCheck(x != y, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_IDN, "x and y must be different vectors");
710: PetscCheck(side == PC_LEFT || side == PC_SYMMETRIC || side == PC_RIGHT, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Side must be right, left, or symmetric");
711: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(x, 3, PETSC_TRUE));
713: PetscCall(PCSetUp(pc));
714: if (pc->ops->applyBA) {
715: PetscUseTypeMethod(pc, applyBA, side, x, y, work);
716: } else if (side == PC_RIGHT) {
717: PetscCall(PCApply(pc, x, work));
718: PetscCall(MatMult(pc->mat, work, y));
719: } else if (side == PC_LEFT) {
720: PetscCall(MatMult(pc->mat, x, work));
721: PetscCall(PCApply(pc, work, y));
722: } else if (side == PC_SYMMETRIC) {
723: /* There's an extra copy here; maybe should provide 2 work vectors instead? */
724: PetscCall(PCApplySymmetricRight(pc, x, work));
725: PetscCall(MatMult(pc->mat, work, y));
726: PetscCall(VecCopy(y, work));
727: PetscCall(PCApplySymmetricLeft(pc, work, y));
728: }
729: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(y, 4, PETSC_FALSE));
730: PetscFunctionReturn(PETSC_SUCCESS);
731: }
733: /*@
734: PCApplyBAorABTranspose - Applies the transpose of the preconditioner
735: and operator to a vector. That is, applies $B^T A^T$ with left preconditioning,
736: NOT $(B A)^T = A^T B^T$.
738: Collective
740: Input Parameters:
741: + pc - the `PC` preconditioner context
742: . side - indicates the preconditioner side, one of `PC_LEFT`, `PC_RIGHT`, or `PC_SYMMETRIC`
743: . x - input vector
744: - work - work vector
746: Output Parameter:
747: . y - output vector
749: Level: developer
751: Note:
752: This routine is used internally so that the same Krylov code can be used to solve $A x = b$ and $A^T x = b$, with a preconditioner
753: defined by $B^T$. This is why this has the funny form that it computes $B^T A^T$
755: .seealso: [](ch_ksp), `PC`, `PCApply()`, `PCApplyTranspose()`, `PCApplyBAorAB()`
756: @*/
757: PetscErrorCode PCApplyBAorABTranspose(PC pc, PCSide side, Vec x, Vec y, Vec work)
758: {
759: PetscFunctionBegin;
764: PetscCheck(x != y, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_IDN, "x and y must be different vectors");
765: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(x, 3, PETSC_TRUE));
766: if (pc->ops->applyBAtranspose) {
767: PetscUseTypeMethod(pc, applyBAtranspose, side, x, y, work);
768: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(y, 4, PETSC_FALSE));
769: PetscFunctionReturn(PETSC_SUCCESS);
770: }
771: PetscCheck(side == PC_LEFT || side == PC_RIGHT, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Side must be right or left");
773: PetscCall(PCSetUp(pc));
774: if (side == PC_RIGHT) {
775: PetscCall(PCApplyTranspose(pc, x, work));
776: PetscCall(MatMultTranspose(pc->mat, work, y));
777: } else if (side == PC_LEFT) {
778: PetscCall(MatMultTranspose(pc->mat, x, work));
779: PetscCall(PCApplyTranspose(pc, work, y));
780: }
781: /* add support for PC_SYMMETRIC */
782: if (pc->erroriffailure) PetscCall(VecValidValues_Internal(y, 4, PETSC_FALSE));
783: PetscFunctionReturn(PETSC_SUCCESS);
784: }
786: /*@
787: PCApplyRichardsonExists - Determines whether a particular preconditioner has a
788: built-in fast application of Richardson's method.
790: Not Collective
792: Input Parameter:
793: . pc - the preconditioner
795: Output Parameter:
796: . exists - `PETSC_TRUE` or `PETSC_FALSE`
798: Level: developer
800: .seealso: [](ch_ksp), `PC`, `KSPRICHARDSON`, `PCApplyRichardson()`, `PCMatApplyRichardsonExists()`
801: @*/
802: PetscErrorCode PCApplyRichardsonExists(PC pc, PetscBool *exists)
803: {
804: PetscFunctionBegin;
806: PetscAssertPointer(exists, 2);
807: if (pc->ops->applyrichardson) *exists = PETSC_TRUE;
808: else *exists = PETSC_FALSE;
809: PetscFunctionReturn(PETSC_SUCCESS);
810: }
812: /*@
813: PCApplyRichardson - Applies several steps of Richardson iteration with
814: the particular preconditioner. This routine is usually used by the
815: Krylov solvers and not the application code directly.
817: Collective
819: Input Parameters:
820: + pc - the `PC` preconditioner context
821: . b - the right-hand side
822: . w - one work vector
823: . rtol - relative decrease in residual norm convergence criteria
824: . abstol - absolute residual norm convergence criteria
825: . dtol - divergence residual norm increase criteria
826: . its - the number of iterations to apply.
827: - guesszero - if the input x contains nonzero initial guess
829: Output Parameters:
830: + outits - number of iterations actually used (for SOR this always equals its)
831: . reason - the reason the apply terminated
832: - y - the solution (also contains initial guess if guesszero is `PETSC_FALSE`
834: Level: developer
836: Notes:
837: Most preconditioners do not support this function. Use the command
838: `PCApplyRichardsonExists()` to determine if one does.
840: Except for the `PCMG` this routine ignores the convergence tolerances
841: and always runs for the number of iterations
843: .seealso: [](ch_ksp), `PC`, `PCApplyRichardsonExists()`, `PCMatApplyRichardson()`
844: @*/
845: PetscErrorCode PCApplyRichardson(PC pc, Vec b, Vec y, Vec w, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool guesszero, PetscInt *outits, PCRichardsonConvergedReason *reason)
846: {
847: PetscFunctionBegin;
852: PetscCheck(b != y, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_IDN, "b and y must be different vectors");
853: PetscCall(PCSetUp(pc));
854: PetscUseTypeMethod(pc, applyrichardson, b, y, w, rtol, abstol, dtol, its, guesszero, outits, reason);
855: PetscFunctionReturn(PETSC_SUCCESS);
856: }
858: /*@
859: PCMatApplyRichardsonExists - Determines whether a particular preconditioner has a
860: built-in fast application of Richardson's method on a block of vectors stored as a `MATDENSE`.
862: Not Collective
864: Input Parameter:
865: . pc - the preconditioner
867: Output Parameter:
868: . exists - `PETSC_TRUE` or `PETSC_FALSE`
870: Level: developer
872: .seealso: [](ch_ksp), `PC`, `KSPRICHARDSON`, `PCMatApplyRichardson()`, `PCApplyRichardsonExists()`
873: @*/
874: PetscErrorCode PCMatApplyRichardsonExists(PC pc, PetscBool *exists)
875: {
876: PetscFunctionBegin;
878: PetscAssertPointer(exists, 2);
879: if (pc->ops->matapplyrichardson) *exists = PETSC_TRUE;
880: else *exists = PETSC_FALSE;
881: PetscFunctionReturn(PETSC_SUCCESS);
882: }
884: /*@
885: PCMatApplyRichardson - Applies several steps of Richardson iteration with the particular preconditioner
886: to multiple vectors stored as a `MATDENSE`. This routine is usually used by the Krylov solvers and not
887: the application code directly.
889: Collective
891: Input Parameters:
892: + pc - the `PC` preconditioner context
893: . B - the block of right-hand sides
894: . W - one work block of vectors, may be `NULL`
895: . rtol - relative decrease in residual norm convergence criteria
896: . abstol - absolute residual norm convergence criteria
897: . dtol - divergence residual norm increase criteria
898: . its - the number of iterations to apply
899: - guesszero - `PETSC_TRUE` if `Y` is known to be initially zero
901: Output Parameters:
902: + outits - number of iterations actually used
903: . reason - the reason the apply terminated
904: - Y - the block of solutions (also contains the initial guess if guesszero is `PETSC_FALSE`)
906: Level: developer
908: Notes:
909: Most preconditioners do not support this function. Use the command
910: `PCMatApplyRichardsonExists()` to determine if one does.
912: `W` may be `NULL`. For example `KSPRICHARDSON` does not provide one, so implementations that need scratch space must allocate it themselves.
914: Like `PCMatApply()`, `B` and `Y` must be different matrices.
916: .seealso: [](ch_ksp), `PC`, `PCMatApplyRichardsonExists()`, `PCApplyRichardson()`, `PCMatApply()`, `KSPMatSolve()`
917: @*/
918: PetscErrorCode PCMatApplyRichardson(PC pc, Mat B, Mat Y, Mat W, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool guesszero, PetscInt *outits, PCRichardsonConvergedReason *reason)
919: {
920: PetscFunctionBegin;
925: PetscCheckSameComm(pc, 1, B, 2);
926: PetscCheckSameComm(pc, 1, Y, 3);
927: if (W) PetscCheckSameComm(pc, 1, W, 4);
928: PetscCheck(Y != B, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_IDN, "Y and B must be different matrices");
929: PetscCheck(!W || (W != Y && W != B), PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_IDN, "W must be different from B and Y");
930: PetscCall(PCMatCheckBlocks_Private(pc, B, Y, "right-hand sides", "solutions"));
931: /* W has the same layout as B, so it is checked against Pmat and against the number of columns of Y in the same way */
932: if (W) PetscCall(PCMatCheckBlocks_Private(pc, W, Y, "work vectors", "solutions"));
933: PetscCall(PCSetUp(pc));
934: PetscUseTypeMethod(pc, matapplyrichardson, B, Y, W, rtol, abstol, dtol, its, guesszero, outits, reason);
935: PetscFunctionReturn(PETSC_SUCCESS);
936: }
938: /*@
939: PCSetFailedReason - Sets the reason a `PCSetUp()` failed or `PC_NOERROR` if it did not fail
941: Logically Collective
943: Input Parameters:
944: + pc - the `PC` preconditioner context
945: - reason - the reason it failed
947: Level: advanced
949: .seealso: [](ch_ksp), `PC`, `PCCreate()`, `PCApply()`, `PCDestroy()`, `PCFailedReason`
950: @*/
951: PetscErrorCode PCSetFailedReason(PC pc, PCFailedReason reason)
952: {
953: PetscFunctionBegin;
955: pc->failedreason = reason;
956: PetscFunctionReturn(PETSC_SUCCESS);
957: }
959: /*@
960: PCGetFailedReason - Gets the reason a `PCSetUp()` failed or `PC_NOERROR` if it did not fail
962: Not Collective
964: Input Parameter:
965: . pc - the `PC` preconditioner context
967: Output Parameter:
968: . reason - the reason it failed
970: Level: advanced
972: Note:
973: After a call to `KSPCheckDot()` or `KSPCheckNorm()` inside a `KSPSolve()` or a call to `PCReduceFailedReason()`
974: this is the maximum reason over all MPI processes in the `PC` communicator and hence logically collective.
975: Otherwise it returns the local value.
977: .seealso: [](ch_ksp), `PC`, `PCCreate()`, `PCApply()`, `PCDestroy()`, `PCSetFailedReason()`, `PCFailedReason`
978: @*/
979: PetscErrorCode PCGetFailedReason(PC pc, PCFailedReason *reason)
980: {
981: PetscFunctionBegin;
983: *reason = pc->failedreason;
984: PetscFunctionReturn(PETSC_SUCCESS);
985: }
987: /*@
988: PCReduceFailedReason - Reduce the failed reason among the MPI processes that share the `PC`
990: Collective
992: Input Parameter:
993: . pc - the `PC` preconditioner context
995: Level: advanced
997: Note:
998: Different MPI processes may have different reasons or no reason, see `PCGetFailedReason()`. This routine
999: makes them have a common value (failure if any MPI process had a failure).
1001: .seealso: [](ch_ksp), `PC`, `PCCreate()`, `PCApply()`, `PCDestroy()`, `PCGetFailedReason()`, `PCSetFailedReason()`, `PCFailedReason`
1002: @*/
1003: PetscErrorCode PCReduceFailedReason(PC pc)
1004: {
1005: PetscInt buf;
1007: PetscFunctionBegin;
1009: buf = (PetscInt)pc->failedreason;
1010: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &buf, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)pc)));
1011: pc->failedreason = (PCFailedReason)buf;
1012: PetscFunctionReturn(PETSC_SUCCESS);
1013: }
1015: /*
1016: a setupcall of 0 indicates never setup,
1017: 1 indicates has been previously setup
1018: -1 indicates a PCSetUp() was attempted and failed
1019: */
1020: /*@
1021: PCSetUp - Prepares for the use of a preconditioner. Performs all the one-time operations needed before the preconditioner
1022: can be used with `PCApply()`
1024: Collective
1026: Input Parameter:
1027: . pc - the `PC` preconditioner context
1029: Level: developer
1031: Notes:
1032: For example, for `PCLU` this will compute the factorization.
1034: This is called automatically by `KSPSetUp()` or `PCApply()` so rarely needs to be called directly.
1036: For nested preconditioners, such as `PCFIELDSPLIT` or `PCBJACOBI` this may not finish the construction of the preconditioner
1037: on the inner levels, the routine `PCSetUpOnBlocks()` may compute more of the preconditioner in those situations.
1039: .seealso: [](ch_ksp), `PC`, `PCCreate()`, `PCApply()`, `PCDestroy()`, `KSPSetUp()`, `PCSetUpOnBlocks()`
1040: @*/
1041: PetscErrorCode PCSetUp(PC pc)
1042: {
1043: const char *def;
1044: MatState matstate;
1046: PetscFunctionBegin;
1048: PetscCheck(pc->mat, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Missing A matrix. Use PCSetOperators()");
1049: PetscCheck(pc->pmat, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Missing P matrix. Use PCSetOperators()");
1051: if (pc->setupcalled && pc->reusepreconditioner) {
1052: PetscCall(PetscInfo(pc, "Leaving PC with identical preconditioner since reuse preconditioner is set\n"));
1053: PetscFunctionReturn(PETSC_SUCCESS);
1054: }
1056: PetscCall(MatGetState(pc->pmat, &matstate));
1057: if (!pc->setupcalled) pc->flag = DIFFERENT_NONZERO_PATTERN;
1058: else {
1059: PetscBool same;
1061: PetscCall(MatStateCompare(matstate, pc->matstate, &same));
1062: if (same) PetscFunctionReturn(PETSC_SUCCESS);
1063: pc->flag = SAME_NONZERO_PATTERN;
1064: if (matstate.nonzerostate != pc->matstate.nonzerostate) {
1065: PetscCall(PetscInfo(pc, "Setting up PC with different nonzero pattern\n"));
1066: pc->flag = DIFFERENT_NONZERO_PATTERN;
1067: }
1068: }
1069: pc->matstate = matstate;
1071: if (!((PetscObject)pc)->type_name) {
1072: PetscCall(PCGetDefaultType_Private(pc, &def));
1073: PetscCall(PCSetType(pc, def));
1074: }
1076: PetscCall(MatSetErrorIfFailure(pc->pmat, pc->erroriffailure));
1077: PetscCall(MatSetErrorIfFailure(pc->mat, pc->erroriffailure));
1078: PetscCall(PetscLogEventBegin(PC_SetUp, pc, 0, 0, 0));
1079: if (pc->ops->setup) {
1080: PetscCall(PCLogEventsDeactivatePush());
1081: PetscUseTypeMethod(pc, setup);
1082: PetscCall(PCLogEventsDeactivatePop());
1083: }
1084: PetscCall(PetscLogEventEnd(PC_SetUp, pc, 0, 0, 0));
1085: if (pc->postsetup) PetscCall((*pc->postsetup)(pc));
1086: pc->setupcalled = PETSC_TRUE;
1087: PetscFunctionReturn(PETSC_SUCCESS);
1088: }
1090: /*@
1091: PCSetUpOnBlocks - Sets up the preconditioner for each block in
1092: the block Jacobi, overlapping Schwarz, and fieldsplit methods.
1094: Collective
1096: Input Parameter:
1097: . pc - the `PC` preconditioner context
1099: Level: developer
1101: Notes:
1102: For nested preconditioners such as `PCBJACOBI`, `PCSetUp()` is not called on each sub-`KSP` when `PCSetUp()` is
1103: called on the outer `PC`, this routine ensures it is called.
1105: It calls `PCSetUp()` if not yet called.
1107: .seealso: [](ch_ksp), `PC`, `PCSetUp()`, `PCCreate()`, `PCApply()`, `PCDestroy()`
1108: @*/
1109: PetscErrorCode PCSetUpOnBlocks(PC pc)
1110: {
1111: PetscFunctionBegin;
1113: if (!pc->setupcalled) PetscCall(PCSetUp(pc)); /* "if" to prevent -info extra prints */
1114: if (!pc->ops->setuponblocks) PetscFunctionReturn(PETSC_SUCCESS);
1115: PetscCall(MatSetErrorIfFailure(pc->pmat, pc->erroriffailure));
1116: PetscCall(PetscLogEventBegin(PC_SetUpOnBlocks, pc, 0, 0, 0));
1117: PetscCall(PCLogEventsDeactivatePush());
1118: PetscUseTypeMethod(pc, setuponblocks);
1119: PetscCall(PCLogEventsDeactivatePop());
1120: PetscCall(PetscLogEventEnd(PC_SetUpOnBlocks, pc, 0, 0, 0));
1121: PetscFunctionReturn(PETSC_SUCCESS);
1122: }
1124: /*@
1125: PCSetModifySubMatrices - Sets a user-defined routine for modifying the
1126: submatrices that arise within certain subdomain-based preconditioners such as `PCASM`
1128: Logically Collective
1130: Input Parameters:
1131: + pc - the `PC` preconditioner context
1132: . func - routine for modifying the submatrices, see `PCModifySubMatricesFn`
1133: - ctx - optional user-defined context (may be `NULL`)
1135: Level: advanced
1137: Notes:
1138: The basic submatrices are extracted from the matrix used to construct the preconditioner as
1139: usual; the user can then alter these (for example, to set different boundary
1140: conditions for each submatrix) before they are used for the local solves.
1142: `PCSetModifySubMatrices()` MUST be called before `KSPSetUp()` and
1143: `KSPSolve()`.
1145: A routine set by `PCSetModifySubMatrices()` is currently called within
1146: `PCBJACOBI`, `PCASM`, `PCGASM`, and `PCHPDDM`.
1147: All other preconditioners ignore this routine.
1149: .seealso: [](ch_ksp), `PC`, `PCModifySubMatricesFn`, `PCBJACOBI`, `PCASM`, `PCModifySubMatrices()`
1150: @*/
1151: PetscErrorCode PCSetModifySubMatrices(PC pc, PCModifySubMatricesFn *func, PetscCtx ctx)
1152: {
1153: PetscFunctionBegin;
1155: pc->modifysubmatrices = func;
1156: pc->modifysubmatricesP = ctx;
1157: PetscFunctionReturn(PETSC_SUCCESS);
1158: }
1160: /*@
1161: PCModifySubMatrices - Calls an optional user-defined routine within
1162: certain preconditioners if one has been set with `PCSetModifySubMatrices()`.
1164: Collective
1166: Input Parameters:
1167: + pc - the `PC` preconditioner context
1168: . nsub - the number of local submatrices
1169: . row - an array of index sets that contain the global row numbers
1170: that comprise each local submatrix
1171: . col - an array of index sets that contain the global column numbers
1172: that comprise each local submatrix
1173: . submat - array of local submatrices
1174: - ctx - optional user-defined context for private data for the
1175: user-defined routine (may be `NULL`)
1177: Output Parameter:
1178: . submat - array of local submatrices (the entries of which may
1179: have been modified)
1181: Level: developer
1183: Note:
1184: The user should NOT generally call this routine, as it will
1185: automatically be called within certain preconditioners.
1187: .seealso: [](ch_ksp), `PC`, `PCModifySubMatricesFn`, `PCSetModifySubMatrices()`
1188: @*/
1189: PetscErrorCode PCModifySubMatrices(PC pc, PetscInt nsub, const IS row[], const IS col[], Mat submat[], PetscCtx ctx)
1190: {
1191: PetscFunctionBegin;
1193: if (!pc->modifysubmatrices) PetscFunctionReturn(PETSC_SUCCESS);
1194: PetscCall(PetscLogEventBegin(PC_ModifySubMatrices, pc, 0, 0, 0));
1195: PetscCall((*pc->modifysubmatrices)(pc, nsub, row, col, submat, ctx));
1196: PetscCall(PetscLogEventEnd(PC_ModifySubMatrices, pc, 0, 0, 0));
1197: PetscFunctionReturn(PETSC_SUCCESS);
1198: }
1200: /*@
1201: PCSetOperators - Sets the matrix associated with the linear system and
1202: a (possibly) different one from which the preconditioner will be constructed.
1204: Logically Collective
1206: Input Parameters:
1207: + pc - the `PC` preconditioner context
1208: . Amat - the matrix that defines the linear system
1209: - Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat.
1211: Level: advanced
1213: Notes:
1214: Using this routine directly is rarely needed, the preferred, and equivalent, usage is `KSPSetOperators()`.
1216: Passing a `NULL` for `Amat` or `Pmat` removes the matrix that is currently used.
1218: If you wish to replace either `Amat` or `Pmat` but leave the other one untouched then
1219: first call `KSPGetOperators()` to get the one you wish to keep, call `PetscObjectReference()`
1220: on it and then pass it back in in your call to `KSPSetOperators()`.
1222: More Notes about Repeated Solution of Linear Systems:
1223: PETSc does NOT reset the matrix entries of either `Amat` or `Pmat`
1224: to zero after a linear solve; the user is completely responsible for
1225: matrix assembly. See the routine `MatZeroEntries()` if desiring to
1226: zero all elements of a matrix.
1228: .seealso: [](ch_ksp), `PC`, `PCGetOperators()`, `MatZeroEntries()`
1229: @*/
1230: PetscErrorCode PCSetOperators(PC pc, Mat Amat, Mat Pmat)
1231: {
1232: PetscInt m1, n1, m2, n2;
1234: PetscFunctionBegin;
1238: if (Amat) PetscCheckSameComm(pc, 1, Amat, 2);
1239: if (Pmat) PetscCheckSameComm(pc, 1, Pmat, 3);
1240: if (pc->setupcalled && pc->mat && pc->pmat && Amat && Pmat) {
1241: PetscCall(MatGetLocalSize(Amat, &m1, &n1));
1242: PetscCall(MatGetLocalSize(pc->mat, &m2, &n2));
1243: PetscCheck(m1 == m2 && n1 == n2, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Cannot change local size of Amat after use old sizes %" PetscInt_FMT " %" PetscInt_FMT " new sizes %" PetscInt_FMT " %" PetscInt_FMT, m2, n2, m1, n1);
1244: PetscCall(MatGetLocalSize(Pmat, &m1, &n1));
1245: PetscCall(MatGetLocalSize(pc->pmat, &m2, &n2));
1246: PetscCheck(m1 == m2 && n1 == n2, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Cannot change local size of Pmat after use old sizes %" PetscInt_FMT " %" PetscInt_FMT " new sizes %" PetscInt_FMT " %" PetscInt_FMT, m2, n2, m1, n1);
1247: }
1249: if (Pmat != pc->pmat) {
1250: /* changing the operator that defines the preconditioner thus reneed to clear current states so new preconditioner is built */
1251: PetscCall(MatStateInvalidate(pc->matstate));
1252: }
1254: /* reference first in case the matrices are the same */
1255: PetscCall(PetscObjectReference((PetscObject)Amat));
1256: PetscCall(MatDestroy(&pc->mat));
1257: PetscCall(PetscObjectReference((PetscObject)Pmat));
1258: PetscCall(MatDestroy(&pc->pmat));
1259: pc->mat = Amat;
1260: pc->pmat = Pmat;
1261: PetscFunctionReturn(PETSC_SUCCESS);
1262: }
1264: /*@
1265: PCSetReusePreconditioner - reuse the current preconditioner even if the operator in the preconditioner `PC` has changed.
1267: Logically Collective
1269: Input Parameters:
1270: + pc - the `PC` preconditioner context
1271: - flag - `PETSC_TRUE` do not compute a new preconditioner, `PETSC_FALSE` do compute a new preconditioner
1273: Level: intermediate
1275: Note:
1276: Normally if a matrix inside a `PC` changes the `PC` automatically updates itself using information from the changed matrix. This option
1277: prevents this.
1279: .seealso: [](ch_ksp), `PC`, `PCGetOperators()`, `MatZeroEntries()`, `PCGetReusePreconditioner()`, `KSPSetReusePreconditioner()`
1280: @*/
1281: PetscErrorCode PCSetReusePreconditioner(PC pc, PetscBool flag)
1282: {
1283: PetscFunctionBegin;
1286: pc->reusepreconditioner = flag;
1287: PetscTryMethod(pc, "PCSetReusePreconditioner_C", (PC, PetscBool), (pc, flag));
1288: PetscFunctionReturn(PETSC_SUCCESS);
1289: }
1291: /*@
1292: PCGetReusePreconditioner - Determines if the `PC` reuses the current preconditioner even if the operator in the preconditioner has changed.
1294: Not Collective
1296: Input Parameter:
1297: . pc - the `PC` preconditioner context
1299: Output Parameter:
1300: . flag - `PETSC_TRUE` do not compute a new preconditioner, `PETSC_FALSE` do compute a new preconditioner
1302: Level: intermediate
1304: .seealso: [](ch_ksp), `PC`, `PCGetOperators()`, `MatZeroEntries()`, `PCSetReusePreconditioner()`
1305: @*/
1306: PetscErrorCode PCGetReusePreconditioner(PC pc, PetscBool *flag)
1307: {
1308: PetscFunctionBegin;
1310: PetscAssertPointer(flag, 2);
1311: *flag = pc->reusepreconditioner;
1312: PetscFunctionReturn(PETSC_SUCCESS);
1313: }
1315: /*@
1316: PCGetOperators - Gets the matrix associated with the linear system and
1317: possibly a different one which is used to construct the preconditioner.
1319: Not Collective, though parallel `Mat`s are returned if `pc` is parallel
1321: Input Parameter:
1322: . pc - the `PC` preconditioner context
1324: Output Parameters:
1325: + Amat - the matrix defining the linear system
1326: - Pmat - the matrix from which the preconditioner is constructed, usually the same as Amat.
1328: Level: intermediate
1330: Note:
1331: Does not increase the reference count of the matrices, so you should not destroy them
1333: Alternative usage: If the operators have NOT been set with `KSPSetOperators()`/`PCSetOperators()` then the operators
1334: are created in `PC` and returned to the user. In this case, if both operators
1335: mat and pmat are requested, two DIFFERENT operators will be returned. If
1336: only one is requested both operators in the PC will be the same (i.e. as
1337: if one had called `KSPSetOperators()`/`PCSetOperators()` with the same argument for both Mats).
1338: The user must set the sizes of the returned matrices and their type etc just
1339: as if the user created them with `MatCreate()`. For example,
1341: .vb
1342: KSP/PCGetOperators(ksp/pc,&Amat,NULL); is equivalent to
1343: set size, type, etc of Amat
1345: MatCreate(comm,&mat);
1346: KSP/PCSetOperators(ksp/pc,Amat,Amat);
1347: PetscObjectDereference((PetscObject)mat);
1348: set size, type, etc of Amat
1349: .ve
1351: and
1353: .vb
1354: KSP/PCGetOperators(ksp/pc,&Amat,&Pmat); is equivalent to
1355: set size, type, etc of Amat and Pmat
1357: MatCreate(comm,&Amat);
1358: MatCreate(comm,&Pmat);
1359: KSP/PCSetOperators(ksp/pc,Amat,Pmat);
1360: PetscObjectDereference((PetscObject)Amat);
1361: PetscObjectDereference((PetscObject)Pmat);
1362: set size, type, etc of Amat and Pmat
1363: .ve
1365: The rationale for this support is so that when creating a `TS`, `SNES`, or `KSP` the hierarchy
1366: of underlying objects (i.e. `SNES`, `KSP`, `PC`, `Mat`) and their lifespans can be completely
1367: managed by the top most level object (i.e. the `TS`, `SNES`, or `KSP`). Another way to look
1368: at this is when you create a `SNES` you do not NEED to create a `KSP` and attach it to
1369: the `SNES` object (the `SNES` object manages it for you). Similarly when you create a KSP
1370: you do not need to attach a `PC` to it (the `KSP` object manages the `PC` object for you).
1371: Thus, why should YOU have to create the `Mat` and attach it to the `SNES`/`KSP`/`PC`, when
1372: it can be created for you?
1374: .seealso: [](ch_ksp), `PC`, `PCSetOperators()`, `KSPGetOperators()`, `KSPSetOperators()`, `PCGetOperatorsSet()`
1375: @*/
1376: PetscErrorCode PCGetOperators(PC pc, Mat *Amat, Mat *Pmat)
1377: {
1378: PetscFunctionBegin;
1380: if (Amat) {
1381: if (!pc->mat) {
1382: if (pc->pmat && !Pmat) { /* Pmat has been set, but user did not request it, so use for Amat */
1383: pc->mat = pc->pmat;
1384: PetscCall(PetscObjectReference((PetscObject)pc->mat));
1385: } else { /* both Amat and Pmat are empty */
1386: PetscCall(MatCreate(PetscObjectComm((PetscObject)pc), &pc->mat));
1387: if (!Pmat) { /* user did NOT request Pmat, so make same as Amat */
1388: pc->pmat = pc->mat;
1389: PetscCall(PetscObjectReference((PetscObject)pc->pmat));
1390: }
1391: }
1392: }
1393: *Amat = pc->mat;
1394: }
1395: if (Pmat) {
1396: if (!pc->pmat) {
1397: if (pc->mat && !Amat) { /* Amat has been set but was not requested, so use for pmat */
1398: pc->pmat = pc->mat;
1399: PetscCall(PetscObjectReference((PetscObject)pc->pmat));
1400: } else {
1401: PetscCall(MatCreate(PetscObjectComm((PetscObject)pc), &pc->pmat));
1402: if (!Amat) { /* user did NOT request Amat, so make same as Pmat */
1403: pc->mat = pc->pmat;
1404: PetscCall(PetscObjectReference((PetscObject)pc->mat));
1405: }
1406: }
1407: }
1408: *Pmat = pc->pmat;
1409: }
1410: PetscFunctionReturn(PETSC_SUCCESS);
1411: }
1413: /*@
1414: PCGetOperatorsSet - Determines if the matrix associated with the linear system and
1415: possibly a different one associated with the preconditioner have been set in the `PC`.
1417: Not Collective, though the results on all processes should be the same
1419: Input Parameter:
1420: . pc - the `PC` preconditioner context
1422: Output Parameters:
1423: + mat - the matrix associated with the linear system was set
1424: - pmat - matrix associated with the preconditioner was set, usually the same
1426: Level: intermediate
1428: .seealso: [](ch_ksp), `PC`, `PCSetOperators()`, `KSPGetOperators()`, `KSPSetOperators()`, `PCGetOperators()`
1429: @*/
1430: PetscErrorCode PCGetOperatorsSet(PC pc, PetscBool *mat, PetscBool *pmat)
1431: {
1432: PetscFunctionBegin;
1434: if (mat) *mat = pc->mat ? PETSC_TRUE : PETSC_FALSE;
1435: if (pmat) *pmat = pc->pmat ? PETSC_TRUE : PETSC_FALSE;
1436: PetscFunctionReturn(PETSC_SUCCESS);
1437: }
1439: /*@
1440: PCFactorGetMatrix - Gets the factored matrix from the
1441: preconditioner context. This routine is valid only for the `PCLU`,
1442: `PCILU`, `PCCHOLESKY`, and `PCICC` methods.
1444: Not Collective though `mat` is parallel if `pc` is parallel
1446: Input Parameter:
1447: . pc - the `PC` preconditioner context
1449: Output Parameters:
1450: . mat - the factored matrix
1452: Level: advanced
1454: Note:
1455: Does not increase the reference count for `mat` so DO NOT destroy it
1457: .seealso: [](ch_ksp), `PC`, `PCLU`, `PCILU`, `PCCHOLESKY`, `PCICC`
1458: @*/
1459: PetscErrorCode PCFactorGetMatrix(PC pc, Mat *mat)
1460: {
1461: PetscFunctionBegin;
1463: PetscAssertPointer(mat, 2);
1464: PetscCall(PCFactorSetUpMatSolverType(pc));
1465: PetscUseTypeMethod(pc, getfactoredmatrix, mat);
1466: PetscFunctionReturn(PETSC_SUCCESS);
1467: }
1469: /*@
1470: PCSetOptionsPrefix - Sets the prefix used for searching for all
1471: `PC` options in the database.
1473: Logically Collective
1475: Input Parameters:
1476: + pc - the `PC` preconditioner context
1477: - prefix - the prefix string to prepend to all `PC` option requests
1479: Level: advanced
1481: Note:
1482: A hyphen (-) must NOT be given at the beginning of the prefix name.
1483: The first character of all runtime options is AUTOMATICALLY the
1484: hyphen.
1486: .seealso: [](ch_ksp), `PC`, `PCSetFromOptions()`, `PCAppendOptionsPrefix()`, `PCGetOptionsPrefix()`
1487: @*/
1488: PetscErrorCode PCSetOptionsPrefix(PC pc, const char prefix[])
1489: {
1490: PetscFunctionBegin;
1492: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)pc, prefix));
1493: PetscFunctionReturn(PETSC_SUCCESS);
1494: }
1496: /*@
1497: PCAppendOptionsPrefix - Appends to the prefix used for searching for all
1498: `PC` options in the database.
1500: Logically Collective
1502: Input Parameters:
1503: + pc - the `PC` preconditioner context
1504: - prefix - the prefix string to prepend to all `PC` option requests
1506: Level: advanced
1508: Note:
1509: A hyphen (-) must NOT be given at the beginning of the prefix name.
1510: The first character of all runtime options is AUTOMATICALLY the
1511: hyphen.
1513: .seealso: [](ch_ksp), `PC`, `PCSetFromOptions()`, `PCSetOptionsPrefix()`, `PCGetOptionsPrefix()`
1514: @*/
1515: PetscErrorCode PCAppendOptionsPrefix(PC pc, const char prefix[])
1516: {
1517: PetscFunctionBegin;
1519: PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)pc, prefix));
1520: PetscFunctionReturn(PETSC_SUCCESS);
1521: }
1523: /*@
1524: PCGetOptionsPrefix - Gets the prefix used for searching for all
1525: `PC` options in the database.
1527: Not Collective
1529: Input Parameter:
1530: . pc - the `PC` preconditioner context
1532: Output Parameter:
1533: . prefix - pointer to the prefix string used, is returned
1535: Level: advanced
1537: .seealso: [](ch_ksp), `PC`, `PCSetFromOptions()`, `PCSetOptionsPrefix()`, `PCAppendOptionsPrefix()`
1538: @*/
1539: PetscErrorCode PCGetOptionsPrefix(PC pc, const char *prefix[])
1540: {
1541: PetscFunctionBegin;
1543: PetscAssertPointer(prefix, 2);
1544: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)pc, prefix));
1545: PetscFunctionReturn(PETSC_SUCCESS);
1546: }
1548: /*
1549: Indicates the right-hand side will be changed by KSPSolve(), this occurs for a few
1550: preconditioners including BDDC and Eisentat that transform the equations before applying
1551: the Krylov methods
1552: */
1553: PETSC_INTERN PetscErrorCode PCPreSolveChangeRHS(PC pc, PetscBool *change)
1554: {
1555: PetscFunctionBegin;
1557: PetscAssertPointer(change, 2);
1558: *change = PETSC_FALSE;
1559: PetscTryMethod(pc, "PCPreSolveChangeRHS_C", (PC, PetscBool *), (pc, change));
1560: PetscFunctionReturn(PETSC_SUCCESS);
1561: }
1563: /*@
1564: PCPreSolve - Optional pre-solve phase, intended for any preconditioner-specific actions that must be performed before
1565: the iterative solve itself. Used in conjunction with `PCPostSolve()`
1567: Collective
1569: Input Parameters:
1570: + pc - the `PC` preconditioner context
1571: - ksp - the Krylov subspace context
1573: Level: developer
1575: Notes:
1576: `KSPSolve()` calls this directly, so is rarely called by the user.
1578: Certain preconditioners, such as the `PCType` of `PCEISENSTAT`, change the formulation of the linear system to be solved iteratively.
1579: This function performs that transformation. `PCPostSolve()` then transforms the system back to its original form after the solve.
1580: `PCPostSolve()` also transforms the resulting solution of the transformed system to the solution of the original problem.
1582: `KSPSetPostSolve()` provides an alternative way to provide such transformations.
1584: .seealso: [](ch_ksp), `PC`, `PCPostSolve()`, `KSP`, `PCSetPostSetUp()`, `KSPSetPreSolve()`, `KSPSetPostSolve()`
1585: @*/
1586: PetscErrorCode PCPreSolve(PC pc, KSP ksp)
1587: {
1588: Vec x, rhs;
1590: PetscFunctionBegin;
1593: pc->presolvedone++;
1594: PetscCheck(pc->presolvedone <= 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Cannot embed PCPreSolve() more than twice");
1595: PetscCall(KSPGetSolution(ksp, &x));
1596: PetscCall(KSPGetRhs(ksp, &rhs));
1597: PetscTryTypeMethod(pc, presolve, ksp, rhs, x);
1598: PetscFunctionReturn(PETSC_SUCCESS);
1599: }
1601: /*@
1602: PCSetPostSetUp - Sets function called at the end of `PCSetUp()` to adjust the computed preconditioner
1604: Logically Collective
1606: Input Parameters:
1607: + pc - the preconditioner object
1608: - postsetup - the function to call after `PCSetUp()`
1610: Calling sequence of `postsetup`:
1611: . pc - the `PC` context
1613: Level: developer
1615: .seealso: [](ch_ksp), `PC`, `PCSetUp()`
1616: @*/
1617: PetscErrorCode PCSetPostSetUp(PC pc, PetscErrorCode (*postsetup)(PC pc))
1618: {
1619: PetscFunctionBegin;
1621: pc->postsetup = postsetup;
1622: PetscFunctionReturn(PETSC_SUCCESS);
1623: }
1625: /*@
1626: PCPostSolve - Optional post-solve phase, intended for any
1627: preconditioner-specific actions that must be performed after
1628: the iterative solve itself.
1630: Collective
1632: Input Parameters:
1633: + pc - the `PC` preconditioner context
1634: - ksp - the `KSP` Krylov subspace context
1636: Example Usage:
1637: .vb
1638: PCPreSolve(pc,ksp);
1639: KSPSolve(ksp,b,x);
1640: PCPostSolve(pc,ksp);
1641: .ve
1643: Level: developer
1645: Note:
1646: `KSPSolve()` calls this routine directly, so it is rarely called by the user.
1648: .seealso: [](ch_ksp), `PC`, `KSPSetPostSolve()`, `KSPSetPreSolve()`, `PCPreSolve()`, `KSPSolve()`
1649: @*/
1650: PetscErrorCode PCPostSolve(PC pc, KSP ksp)
1651: {
1652: Vec x, rhs;
1654: PetscFunctionBegin;
1657: pc->presolvedone--;
1658: PetscCall(KSPGetSolution(ksp, &x));
1659: PetscCall(KSPGetRhs(ksp, &rhs));
1660: PetscTryTypeMethod(pc, postsolve, ksp, rhs, x);
1661: PetscFunctionReturn(PETSC_SUCCESS);
1662: }
1664: /*@
1665: PCLoad - Loads a `PC` that has been stored in binary with `PCView()`.
1667: Collective
1669: Input Parameters:
1670: + newdm - the newly loaded `PC`, this needs to have been created with `PCCreate()` or
1671: some related function before a call to `PCLoad()`.
1672: - viewer - binary file viewer `PETSCVIEWERBINARY`, obtained from `PetscViewerBinaryOpen()`
1674: Level: intermediate
1676: Note:
1677: The type is determined by the data in the file, any `PCType` set into the `PC` before this call is ignored.
1679: .seealso: [](ch_ksp), `PC`, `PetscViewerBinaryOpen()`, `PCView()`, `MatLoad()`, `VecLoad()`, `PETSCVIEWERBINARY`
1680: @*/
1681: PetscErrorCode PCLoad(PC newdm, PetscViewer viewer)
1682: {
1683: PetscBool isbinary;
1684: PetscInt classid;
1685: char type[256];
1687: PetscFunctionBegin;
1690: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
1691: PetscCheck(isbinary, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen()");
1693: PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
1694: PetscCheck(classid == PC_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not PC next in file");
1695: PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
1696: PetscCall(PCSetType(newdm, type));
1697: PetscTryTypeMethod(newdm, load, viewer);
1698: PetscFunctionReturn(PETSC_SUCCESS);
1699: }
1701: #include <petscdraw.h>
1702: #if PetscDefined(HAVE_SAWS)
1703: #include <petscviewersaws.h>
1704: #endif
1706: /*@
1707: PCViewFromOptions - View (print or provide information about) the `PC`, based on options in the options database
1709: Collective
1711: Input Parameters:
1712: + A - the `PC` context
1713: . obj - optional object that provides the options prefix, pass `NULL` to use the options prefix of `A`
1714: - name - command line option name
1716: Options Database Key:
1717: . -name viewer_specification - See `PetscOptionsCreateViewer()` for the values of `viewer_specification`
1719: Level: developer
1721: Note:
1722: This checks the options database, creates the viewer on-the-fly, uses it and then destroys it. Hence it should not be called in heavily used routines,
1723: rather `PetscOptionsCreateViewer()` should be used to construct the viewer once which can then be utilized in the heavily used routine.
1725: .seealso: [](ch_ksp), `PC`, `PCView()`, `PetscObjectViewFromOptions()`, `PCCreate()`, `PetscOptionsCreateViewer()`
1726: @*/
1727: PetscErrorCode PCViewFromOptions(PC A, PetscObject obj, const char name[])
1728: {
1729: PetscFunctionBegin;
1731: PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
1732: PetscFunctionReturn(PETSC_SUCCESS);
1733: }
1735: /*@
1736: PCView - Prints information about the `PC`
1738: Collective
1740: Input Parameters:
1741: + pc - the `PC` preconditioner context
1742: - viewer - optional `PetscViewer` visualization context
1744: Level: intermediate
1746: Notes:
1747: The available visualization contexts include
1748: + `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
1749: - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
1750: output where only the first processor opens
1751: the file. All other processors send their
1752: data to the first processor to print.
1754: The user can open an alternative visualization contexts with
1755: `PetscViewerASCIIOpen()` (output to a specified file).
1757: .seealso: [](ch_ksp), `PC`, `PetscViewer`, `PetscViewerType`, `KSPView()`, `PetscViewerASCIIOpen()`
1758: @*/
1759: PetscErrorCode PCView(PC pc, PetscViewer viewer)
1760: {
1761: PCType cstr;
1762: PetscViewerFormat format;
1763: PetscBool isascii, isstring, isbinary, isdraw, pop = PETSC_FALSE;
1764: #if PetscDefined(HAVE_SAWS)
1765: PetscBool issaws;
1766: #endif
1768: PetscFunctionBegin;
1770: if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)pc), &viewer));
1772: PetscCheckSameComm(pc, 1, viewer, 2);
1774: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1775: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
1776: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
1777: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
1778: #if PetscDefined(HAVE_SAWS)
1779: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws));
1780: #endif
1782: if (isascii) {
1783: PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)pc, viewer));
1784: if (!pc->setupcalled) PetscCall(PetscViewerASCIIPrintf(viewer, " PC has not been set up so information may be incomplete\n"));
1785: PetscCall(PetscViewerASCIIPushTab(viewer));
1786: PetscTryTypeMethod(pc, view, viewer);
1787: PetscCall(PetscViewerASCIIPopTab(viewer));
1788: if (pc->mat) {
1789: PetscCall(PetscViewerGetFormat(viewer, &format));
1790: if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) {
1791: PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_INFO));
1792: pop = PETSC_TRUE;
1793: }
1794: if (pc->pmat == pc->mat) {
1795: PetscCall(PetscViewerASCIIPrintf(viewer, " linear system matrix, which is also used to construct the preconditioner:\n"));
1796: PetscCall(PetscViewerASCIIPushTab(viewer));
1797: PetscCall(MatView(pc->mat, viewer));
1798: PetscCall(PetscViewerASCIIPopTab(viewer));
1799: } else {
1800: if (pc->pmat) {
1801: PetscCall(PetscViewerASCIIPrintf(viewer, " linear system matrix, followed by the matrix used to construct the preconditioner:\n"));
1802: } else {
1803: PetscCall(PetscViewerASCIIPrintf(viewer, " linear system matrix:\n"));
1804: }
1805: PetscCall(PetscViewerASCIIPushTab(viewer));
1806: PetscCall(MatView(pc->mat, viewer));
1807: if (pc->pmat) PetscCall(MatView(pc->pmat, viewer));
1808: PetscCall(PetscViewerASCIIPopTab(viewer));
1809: }
1810: if (pop) PetscCall(PetscViewerPopFormat(viewer));
1811: }
1812: } else if (isstring) {
1813: PetscCall(PCGetType(pc, &cstr));
1814: PetscCall(PetscViewerStringSPrintf(viewer, " PCType: %-7.7s", cstr));
1815: PetscTryTypeMethod(pc, view, viewer);
1816: if (pc->mat) PetscCall(MatView(pc->mat, viewer));
1817: if (pc->pmat && pc->pmat != pc->mat) PetscCall(MatView(pc->pmat, viewer));
1818: } else if (isbinary) {
1819: PetscInt classid = PC_FILE_CLASSID;
1820: MPI_Comm comm;
1821: PetscMPIInt rank;
1822: char type[256];
1824: PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
1825: PetscCallMPI(MPI_Comm_rank(comm, &rank));
1826: if (rank == 0) {
1827: PetscCall(PetscViewerBinaryWrite(viewer, &classid, 1, PETSC_INT));
1828: PetscCall(PetscStrncpy(type, ((PetscObject)pc)->type_name, 256));
1829: PetscCall(PetscViewerBinaryWrite(viewer, type, 256, PETSC_CHAR));
1830: }
1831: PetscTryTypeMethod(pc, view, viewer);
1832: } else if (isdraw) {
1833: PetscDraw draw;
1834: char str[25];
1835: PetscReal x, y, bottom, h;
1836: PetscInt n;
1838: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
1839: PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y));
1840: if (pc->mat) {
1841: PetscCall(MatGetSize(pc->mat, &n, NULL));
1842: PetscCall(PetscSNPrintf(str, 25, "PC: %s (%" PetscInt_FMT ")", ((PetscObject)pc)->type_name, n));
1843: } else {
1844: PetscCall(PetscSNPrintf(str, 25, "PC: %s", ((PetscObject)pc)->type_name));
1845: }
1846: PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_RED, PETSC_DRAW_BLACK, str, NULL, &h));
1847: bottom = y - h;
1848: PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom));
1849: PetscTryTypeMethod(pc, view, viewer);
1850: PetscCall(PetscDrawPopCurrentPoint(draw));
1851: #if PetscDefined(HAVE_SAWS)
1852: } else if (issaws) {
1853: PetscMPIInt rank;
1855: PetscCall(PetscObjectName((PetscObject)pc));
1856: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
1857: if (!((PetscObject)pc)->amsmem && rank == 0) PetscCall(PetscObjectViewSAWs((PetscObject)pc, viewer));
1858: if (pc->mat) PetscCall(MatView(pc->mat, viewer));
1859: if (pc->pmat && pc->pmat != pc->mat) PetscCall(MatView(pc->pmat, viewer));
1860: #endif
1861: }
1862: PetscFunctionReturn(PETSC_SUCCESS);
1863: }
1865: /*@
1866: PCRegister - Adds a method (`PCType`) to the PETSc preconditioner package.
1868: Not collective. No Fortran Support
1870: Input Parameters:
1871: + sname - name of a new user-defined solver
1872: - function - routine to create the method context which will be stored in a `PC` when `PCSetType()` is called
1874: Example Usage:
1875: .vb
1876: PCRegister("my_solver", MySolverCreate);
1877: .ve
1879: Then, your solver can be chosen with the procedural interface via
1880: .vb
1881: PCSetType(pc, "my_solver")
1882: .ve
1883: or at runtime via the option
1884: .vb
1885: -pc_type my_solver
1886: .ve
1888: Level: advanced
1890: Note:
1891: A simpler alternative to using `PCRegister()` for an application specific preconditioner is to use a `PC` of `PCType` `PCSHELL` and
1892: provide your customizations with `PCShellSetContext()` and `PCShellSetApply()`
1894: `PCRegister()` may be called multiple times to add several user-defined preconditioners.
1896: .seealso: [](ch_ksp), `PC`, `PCType`, `PCRegisterAll()`, `PCSetType()`, `PCShellSetContext()`, `PCShellSetApply()`, `PCSHELL`
1897: @*/
1898: PetscErrorCode PCRegister(const char sname[], PetscErrorCode (*function)(PC))
1899: {
1900: PetscFunctionBegin;
1901: PetscCall(PCInitializePackage());
1902: PetscCall(PetscFunctionListAdd(&PCList, sname, function));
1903: PetscFunctionReturn(PETSC_SUCCESS);
1904: }
1906: static PetscErrorCode MatMult_PC(Mat A, Vec X, Vec Y)
1907: {
1908: PC pc;
1910: PetscFunctionBegin;
1911: PetscCall(MatShellGetContext(A, &pc));
1912: PetscCall(PCApply(pc, X, Y));
1913: PetscFunctionReturn(PETSC_SUCCESS);
1914: }
1916: /*@
1917: PCComputeOperator - Computes the explicit preconditioned operator as a matrix `Mat`.
1919: Collective
1921: Input Parameters:
1922: + pc - the `PC` preconditioner object
1923: - mattype - the `MatType` to be used for the operator
1925: Output Parameter:
1926: . mat - the explicit preconditioned operator
1928: Level: advanced
1930: Note:
1931: This computation is done by applying the operators to columns of the identity matrix.
1932: This routine is costly in general, and is recommended for use only with relatively small systems.
1933: Currently, this routine uses a dense matrix format when `mattype` == `NULL`
1935: Developer Note:
1936: This should be called `PCCreateExplicitOperator()`
1938: .seealso: [](ch_ksp), `PC`, `KSPComputeOperator()`, `MatType`
1939: @*/
1940: PetscErrorCode PCComputeOperator(PC pc, MatType mattype, Mat *mat)
1941: {
1942: PetscInt N, M, m, n;
1943: Mat A, Apc;
1945: PetscFunctionBegin;
1947: PetscAssertPointer(mat, 3);
1948: PetscCall(PCGetOperators(pc, &A, NULL));
1949: PetscCall(MatGetLocalSize(A, &m, &n));
1950: PetscCall(MatGetSize(A, &M, &N));
1951: PetscCall(MatCreateShell(PetscObjectComm((PetscObject)pc), m, n, M, N, pc, &Apc));
1952: PetscCall(MatShellSetOperation(Apc, MATOP_MULT, (PetscErrorCodeFn *)MatMult_PC));
1953: PetscCall(MatComputeOperator(Apc, mattype, mat));
1954: PetscCall(MatDestroy(&Apc));
1955: PetscFunctionReturn(PETSC_SUCCESS);
1956: }
1958: /*@
1959: PCSetCoordinates - sets the coordinates of all the nodes (degrees of freedom in the vector) on the local process
1961: Collective
1963: Input Parameters:
1964: + pc - the `PC` preconditioner context
1965: . dim - the dimension of the coordinates 1, 2, or 3
1966: . nloc - the blocked size of the coordinates array
1967: - coords - the coordinates array
1969: Level: intermediate
1971: Notes:
1972: `coords` is an array of the dim coordinates for the nodes on
1973: the local processor, of size `dim`*`nloc`.
1974: If there are 108 equations (dofs) on a processor
1975: for a 3d displacement finite element discretization of elasticity (so
1976: that there are nloc = 36 = 108/3 nodes) then the array must have 108
1977: double precision values (ie, 3 * 36). These x y z coordinates
1978: should be ordered for nodes 0 to N-1 like so: [ 0.x, 0.y, 0.z, 1.x,
1979: ... , N-1.z ].
1981: The information provided here can be used by some preconditioners, such as `PCGAMG`, to produce a better preconditioner.
1982: See also `MatSetNearNullSpace()`.
1984: .seealso: [](ch_ksp), `PC`, `MatSetNearNullSpace()`
1985: @*/
1986: PetscErrorCode PCSetCoordinates(PC pc, PetscInt dim, PetscInt nloc, PetscReal coords[])
1987: {
1988: PetscFunctionBegin;
1991: PetscTryMethod(pc, "PCSetCoordinates_C", (PC, PetscInt, PetscInt, PetscReal[]), (pc, dim, nloc, coords));
1992: PetscFunctionReturn(PETSC_SUCCESS);
1993: }
1995: /*@
1996: PCGetInterpolations - Gets interpolation matrices for all levels (except level 0)
1998: Logically Collective
2000: Input Parameter:
2001: . pc - the precondition context
2003: Output Parameters:
2004: + num_levels - the number of levels
2005: - interpolations - the interpolation matrices (size of `num_levels`-1)
2007: Level: advanced
2009: Developer Note:
2010: Why is this here instead of in `PCMG` etc?
2012: .seealso: [](ch_ksp), `PC`, `PCMG`, `PCMGGetRestriction()`, `PCMGSetInterpolation()`, `PCMGGetInterpolation()`, `PCGetCoarseOperators()`
2013: @*/
2014: PetscErrorCode PCGetInterpolations(PC pc, PetscInt *num_levels, Mat *interpolations[])
2015: {
2016: PetscFunctionBegin;
2018: PetscAssertPointer(num_levels, 2);
2019: PetscAssertPointer(interpolations, 3);
2020: PetscUseMethod(pc, "PCGetInterpolations_C", (PC, PetscInt *, Mat *[]), (pc, num_levels, interpolations));
2021: PetscFunctionReturn(PETSC_SUCCESS);
2022: }
2024: /*@
2025: PCGetCoarseOperators - Gets coarse operator matrices for all levels (except the finest level)
2027: Logically Collective
2029: Input Parameter:
2030: . pc - the precondition context
2032: Output Parameters:
2033: + num_levels - the number of levels
2034: - coarseOperators - the coarse operator matrices (size of `num_levels`-1)
2036: Level: advanced
2038: Developer Note:
2039: Why is this here instead of in `PCMG` etc?
2041: .seealso: [](ch_ksp), `PC`, `PCMG`, `PCMGGetRestriction()`, `PCMGSetInterpolation()`, `PCMGGetRScale()`, `PCMGGetInterpolation()`, `PCGetInterpolations()`
2042: @*/
2043: PetscErrorCode PCGetCoarseOperators(PC pc, PetscInt *num_levels, Mat *coarseOperators[])
2044: {
2045: PetscFunctionBegin;
2047: PetscAssertPointer(num_levels, 2);
2048: PetscAssertPointer(coarseOperators, 3);
2049: PetscUseMethod(pc, "PCGetCoarseOperators_C", (PC, PetscInt *, Mat *[]), (pc, num_levels, coarseOperators));
2050: PetscFunctionReturn(PETSC_SUCCESS);
2051: }