Actual source code: pcimpl.h
1: #pragma once
3: #include <petscksp.h>
4: #include <petscpc.h>
5: #include <petsc/private/petscimpl.h>
7: PETSC_EXTERN PetscBool PCRegisterAllCalled;
8: PETSC_EXTERN PetscErrorCode PCRegisterAll(void);
10: typedef struct _PCOps *PCOps;
11: struct _PCOps {
12: PetscErrorCode (*setup)(PC);
13: PetscErrorCode (*apply)(PC, Vec, Vec);
14: PetscErrorCode (*matapply)(PC, Mat, Mat);
15: PetscErrorCode (*applyrichardson)(PC, Vec, Vec, Vec, PetscReal, PetscReal, PetscReal, PetscInt, PetscBool, PetscInt *, PCRichardsonConvergedReason *);
16: PetscErrorCode (*matapplyrichardson)(PC, Mat, Mat, Mat, PetscReal, PetscReal, PetscReal, PetscInt, PetscBool, PetscInt *, PCRichardsonConvergedReason *);
17: PetscErrorCode (*applyBA)(PC, PCSide, Vec, Vec, Vec);
18: PetscErrorCode (*applytranspose)(PC, Vec, Vec);
19: PetscErrorCode (*matapplytranspose)(PC, Mat, Mat);
20: PetscErrorCode (*applyBAtranspose)(PC, PetscInt, Vec, Vec, Vec);
21: PetscErrorCode (*setfromoptions)(PC, PetscOptionItems);
22: PetscErrorCode (*presolve)(PC, KSP, Vec, Vec);
23: PetscErrorCode (*postsolve)(PC, KSP, Vec, Vec);
24: PetscErrorCode (*getfactoredmatrix)(PC, Mat *);
25: PetscErrorCode (*applysymmetricleft)(PC, Vec, Vec);
26: PetscErrorCode (*applysymmetricright)(PC, Vec, Vec);
27: PetscErrorCode (*setuponblocks)(PC);
28: PetscErrorCode (*destroy)(PC);
29: PetscErrorCode (*view)(PC, PetscViewer);
30: PetscErrorCode (*reset)(PC);
31: PetscErrorCode (*load)(PC, PetscViewer);
32: };
34: /*
35: Preconditioner context
36: */
37: struct _p_PC {
38: PETSCHEADER(struct _PCOps);
39: DM dm;
40: PetscBool setupcalled;
41: MatState matstate; /* last known state of the pmat associated with this PC */
42: PetscBool reusepreconditioner;
43: MatStructure flag; /* reset each PCSetUp() to indicate to PC implementations if nonzero structure has changed */
45: PetscInt setfromoptionscalled;
46: PetscBool erroriffailure; /* generate an error if FPE detected (for example a zero pivot) instead of returning */
47: Mat mat, pmat;
48: Mat *matwork; /* owned work matrices for block applications */
49: PetscInt nmatwork; /* number of allocated Mat slots */
51: PetscObjectParameterDeclare(PetscBool, useAmat); /* used by several PC that including applying the operator inside the preconditioner */
53: PetscErrorCode (*modifysubmatrices)(PC, PetscInt, const IS[], const IS[], Mat[], void *); /* user provided routine */
54: void *modifysubmatricesP; /* context for user routine */
55: void *data;
56: void *ctx; /* optional user-defined context */
57: PCFailedReason failedreason; /* after VecNorm or VecDot contains maximum of all rank failed reasons */
58: PCFailedReason failedreasonrank; /* failed reason on this rank */
59: PetscInt presolvedone;
60: PetscErrorCode (*postsetup)(PC);
61: PetscInt kspnestlevel; /* how many levels of nesting does the KSP have that contains the PC */
62: };
64: PETSC_EXTERN PetscLogEvent PC_SetUp;
65: PETSC_EXTERN PetscLogEvent PC_SetUpOnBlocks;
66: PETSC_EXTERN PetscLogEvent PC_Apply;
67: PETSC_EXTERN PetscLogEvent PC_MatApply;
68: PETSC_EXTERN PetscLogEvent PC_ApplyCoarse;
69: PETSC_EXTERN PetscLogEvent PC_ApplySymmetricLeft;
70: PETSC_EXTERN PetscLogEvent PC_ApplySymmetricRight;
71: PETSC_EXTERN PetscLogEvent PC_ModifySubMatrices;
72: PETSC_EXTERN PetscLogEvent PC_ApplyOnBlocks;
73: PETSC_EXTERN PetscLogEvent PC_ApplyTransposeOnBlocks;
74: PETSC_EXTERN PetscLogStage PCMPIStage;
76: PETSC_INTERN PetscErrorCode PCMatWorkCreate(Vec, PetscInt, PetscInt, Mat *);
77: PETSC_INTERN PetscErrorCode PCMatWorkGet_ASM(PC, PetscInt, Mat *, Mat *);
78: PETSC_INTERN PetscErrorCode PCMatWorkGet_GASM(PC, PetscInt, Mat *, Mat *);
80: // SLEPc builds PCHPDDM in a separate library and needs the three following workspace operations inline
82: /*
83: PCMatWorkClear - Releases the work matrices and sets their slots to `NULL` while retaining the slot array.
85: Collective on `pc`
87: Input Parameter:
88: . pc - preconditioner that owns the workspace
90: Level: developer
92: .seealso: `PCMatWorkSetUp()`, `PCMatWorkReset()`
93: */
94: static inline PetscErrorCode PCMatWorkClear(PC pc)
95: {
96: PetscFunctionBegin;
98: for (PetscInt i = 0; i < pc->nmatwork; ++i) PetscCall(MatDestroy(&pc->matwork[i]));
99: PetscFunctionReturn(PETSC_SUCCESS);
100: }
102: /*
103: PCMatWorkReset - Releases the work matrices, frees the slot array, and resets its capacity to zero.
105: Collective on `pc`
107: Input Parameter:
108: . pc - preconditioner that owns the workspace
110: Level: developer
112: .seealso: `PCMatWorkClear()`, `PCReset()`
113: */
114: static inline PetscErrorCode PCMatWorkReset(PC pc)
115: {
116: PetscFunctionBegin;
117: PetscCall(PCMatWorkClear(pc));
118: PetscCall(PetscFree(pc->matwork));
119: pc->nmatwork = 0;
120: PetscFunctionReturn(PETSC_SUCCESS);
121: }
123: /*
124: PCMatWorkSetUp - Reserves at least `n` work matrix slots in a preconditioner.
126: Collective on `pc`
128: Input Parameters:
129: + pc - preconditioner that owns the workspace
130: . n - minimum number of slots
131: - cols - expected global column count of the first matrix, or `PETSC_DECIDE` to skip the check
133: Level: developer
135: Notes:
136: If the first slot contains a matrix with a different column count, the routine clears all slots.
137: Increasing the capacity can move the array. New slots are initialized to `NULL`.
139: .seealso: `PCMatWorkCreate()`, `PCMatWorkClear()`, `PCMatWorkReset()`
140: */
141: static inline PetscErrorCode PCMatWorkSetUp(PC pc, PetscInt n, PetscInt cols)
142: {
143: PetscInt oldcols = cols;
145: PetscFunctionBegin;
149: PetscCheck(n >= 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "n must be non-negative, got %" PetscInt_FMT, n);
150: if (pc->matwork != NULL && cols >= 0 && pc->matwork[0] != NULL) PetscCall(MatGetSize(pc->matwork[0], NULL, &oldcols));
151: if (cols != oldcols) PetscCall(PCMatWorkClear(pc));
152: if (n > pc->nmatwork) {
153: PetscCall(PetscRealloc(n * sizeof(*pc->matwork), &pc->matwork));
154: PetscCall(PetscArrayzero(pc->matwork + pc->nmatwork, n - pc->nmatwork));
155: pc->nmatwork = n;
156: }
157: PetscFunctionReturn(PETSC_SUCCESS);
158: }