Actual source code: cgne.c
1: /*
2: cgimpl.h defines the simple data structured used to store information
3: related to the type of matrix (e.g. complex symmetric) being solved and
4: data used during the optional Lanczos process used to compute eigenvalues
5: */
6: #include <../src/ksp/ksp/impls/cg/cgimpl.h>
7: extern PetscErrorCode KSPComputeExtremeSingularValues_CG(KSP, PetscReal *, PetscReal *);
8: extern PetscErrorCode KSPComputeEigenvalues_CG(KSP, PetscInt, PetscReal *, PetscReal *, PetscInt *);
10: static PetscErrorCode KSPCGSetType_CGNE(KSP ksp, KSPCGType type)
11: {
12: KSP_CG *cg = (KSP_CG *)ksp->data;
14: PetscFunctionBegin;
15: cg->type = type;
16: PetscFunctionReturn(PETSC_SUCCESS);
17: }
19: static PetscErrorCode KSPSetUp_CGNE(KSP ksp)
20: {
21: KSP_CG *cgP = (KSP_CG *)ksp->data;
22: PetscInt maxit = ksp->max_it;
24: PetscFunctionBegin;
25: /* get work vectors needed by CGNE */
26: PetscCall(KSPSetWorkVecs(ksp, 4));
28: /*
29: If user requested computations of eigenvalues then allocate work space needed
30: */
31: if (ksp->calc_sings) {
32: /* get space to store tridiagonal matrix for Lanczos */
33: PetscCall(PetscMalloc4(maxit, &cgP->e, maxit, &cgP->d, maxit, &cgP->ee, maxit, &cgP->dd));
35: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_CG;
36: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_CG;
37: }
38: PetscFunctionReturn(PETSC_SUCCESS);
39: }
41: static PetscErrorCode KSPSolve_CGNE(KSP ksp)
42: {
43: PetscInt i, stored_max_it, eigs;
44: PetscScalar dpi, a = 1.0, beta, betaold = 1.0, b = 0, *e = NULL, *d = NULL;
45: PetscReal dp = 0.0;
46: Vec X, B, Z, R, P, T;
47: KSP_CG *cg;
48: Mat Amat, Pmat;
49: PetscBool transpose_pc;
51: PetscFunctionBegin;
52: PetscCall(PCApplyTransposeExists(ksp->pc, &transpose_pc));
54: cg = (KSP_CG *)ksp->data;
55: eigs = ksp->calc_sings;
56: stored_max_it = ksp->max_it;
57: X = ksp->vec_sol;
58: B = ksp->vec_rhs;
59: R = ksp->work[0];
60: Z = ksp->work[1];
61: P = ksp->work[2];
62: T = ksp->work[3];
64: #define VecXDot(x, y, a) (cg->type == KSP_CG_HERMITIAN ? VecDot(x, y, a) : VecTDot(x, y, a))
66: if (eigs) {
67: e = cg->e;
68: d = cg->d;
69: e[0] = 0.0;
70: }
71: PetscCall(PCGetOperators(ksp->pc, &Amat, &Pmat));
73: ksp->its = 0;
74: PetscCall(KSP_MatMultTranspose(ksp, Amat, B, T));
75: if (!ksp->guess_zero) {
76: PetscCall(KSP_MatMult(ksp, Amat, X, P));
77: PetscCall(KSP_MatMultTranspose(ksp, Amat, P, R));
78: PetscCall(VecAYPX(R, -1.0, T));
79: } else {
80: PetscCall(VecCopy(T, R)); /* r <- b (x is 0) */
81: }
82: if (transpose_pc) {
83: PetscCall(KSP_PCApplyTranspose(ksp, R, T));
84: } else {
85: PetscCall(KSP_PCApply(ksp, R, T));
86: }
87: PetscCall(KSP_PCApply(ksp, T, Z));
89: if (ksp->normtype == KSP_NORM_PRECONDITIONED) {
90: PetscCall(VecNorm(Z, NORM_2, &dp)); /* dp <- z'*z */
91: } else if (ksp->normtype == KSP_NORM_UNPRECONDITIONED) {
92: PetscCall(VecNorm(R, NORM_2, &dp)); /* dp <- r'*r */
93: } else if (ksp->normtype == KSP_NORM_NATURAL) {
94: PetscCall(VecXDot(Z, R, &beta));
95: KSPCheckDot(ksp, beta);
96: dp = PetscSqrtReal(PetscAbsScalar(beta));
97: } else dp = 0.0;
98: PetscCall(KSPLogResidualHistory(ksp, dp));
99: PetscCall(KSPMonitor(ksp, 0, dp));
100: ksp->rnorm = dp;
101: PetscCall((*ksp->converged)(ksp, 0, dp, &ksp->reason, ksp->cnvP)); /* test for convergence */
102: if (ksp->reason) PetscFunctionReturn(PETSC_SUCCESS);
104: i = 0;
105: do {
106: ksp->its = i + 1;
107: PetscCall(VecXDot(Z, R, &beta)); /* beta <- r'z */
108: KSPCheckDot(ksp, beta);
109: if (beta == 0.0) {
110: ksp->reason = KSP_CONVERGED_ATOL;
111: PetscCall(PetscInfo(ksp, "converged due to beta = 0\n"));
112: break;
113: #if !PetscDefined(USE_COMPLEX)
114: } else if (beta < 0.0) {
115: ksp->reason = KSP_DIVERGED_INDEFINITE_PC;
116: PetscCall(PetscInfo(ksp, "diverging due to indefinite preconditioner\n"));
117: break;
118: #endif
119: }
120: if (!i) {
121: PetscCall(VecCopy(Z, P)); /* p <- z */
122: b = 0.0;
123: } else {
124: b = beta / betaold;
125: if (eigs) {
126: PetscCheck(ksp->max_it == stored_max_it, PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "Can not change maxit AND calculate eigenvalues");
127: e[i] = PetscSqrtReal(PetscAbsScalar(b)) / a;
128: }
129: PetscCall(VecAYPX(P, b, Z)); /* p <- z + b* p */
130: }
131: betaold = beta;
132: PetscCall(KSP_MatMult(ksp, Amat, P, T));
133: PetscCall(KSP_MatMultTranspose(ksp, Amat, T, Z));
134: PetscCall(VecXDot(P, Z, &dpi)); /* dpi <- z'p */
135: KSPCheckDot(ksp, dpi);
136: a = beta / dpi; /* a = beta/p'z */
137: if (eigs) d[i] = PetscSqrtReal(PetscAbsScalar(b)) * e[i] + 1.0 / a;
138: PetscCall(VecAXPY(X, a, P)); /* x <- x + ap */
139: PetscCall(VecAXPY(R, -a, Z)); /* r <- r - az */
140: if (ksp->normtype == KSP_NORM_PRECONDITIONED) {
141: if (transpose_pc) {
142: PetscCall(KSP_PCApplyTranspose(ksp, R, T));
143: } else {
144: PetscCall(KSP_PCApply(ksp, R, T));
145: }
146: PetscCall(KSP_PCApply(ksp, T, Z));
147: PetscCall(VecNorm(Z, NORM_2, &dp)); /* dp <- z'*z */
148: } else if (ksp->normtype == KSP_NORM_UNPRECONDITIONED) {
149: PetscCall(VecNorm(R, NORM_2, &dp));
150: } else if (ksp->normtype == KSP_NORM_NATURAL) {
151: dp = PetscSqrtReal(PetscAbsScalar(beta));
152: } else dp = 0.0;
153: ksp->rnorm = dp;
154: PetscCall(KSPLogResidualHistory(ksp, dp));
155: PetscCall(KSPMonitor(ksp, i + 1, dp));
156: PetscCall((*ksp->converged)(ksp, i + 1, dp, &ksp->reason, ksp->cnvP));
157: if (ksp->reason) break;
158: if (ksp->normtype != KSP_NORM_PRECONDITIONED) {
159: if (transpose_pc) {
160: PetscCall(KSP_PCApplyTranspose(ksp, R, T));
161: } else {
162: PetscCall(KSP_PCApply(ksp, R, T));
163: }
164: PetscCall(KSP_PCApply(ksp, T, Z));
165: }
166: i++;
167: } while (i < ksp->max_it);
168: if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
169: PetscFunctionReturn(PETSC_SUCCESS);
170: }
172: /*MC
173: KSPCGNE - Applies the preconditioned conjugate gradient method to the normal equations
174: without explicitly forming $A^T A$.
176: Options Database Key:
177: . -ksp_cg_type <Hermitian or symmetric - (for complex matrices only) indicates the matrix is Hermitian or symmetric
179: Level: beginner
181: Notes:
182: Eigenvalue computation routines including `KSPSetComputeEigenvalues()` and `KSPComputeEigenvalues()` will return information about the
183: spectrum of $A^T*A$, rather than $A$.
185: `KSPCGNE` is a general-purpose non-symmetric method. It works well when the singular values are much better behaved than
186: eigenvalues. A unitary matrix is a classic example where `KSPCGNE` converges in one iteration, but `KSPGMRES` and `KSPCGS` need N
187: iterations, see {cite}`nachtigal90`. If you intend to solve least squares problems, use `KSPLSQR`.
189: This is NOT a different algorithm than used with `KSPCG`, it merely uses that algorithm with the
190: matrix defined by $A^T A$ and preconditioner defined by $B^T B$ where $B$ is the preconditioner for $A$.
192: See `PETSCREGRESSORLINEAR` for the PETSc toolkit for solving linear regression problems including least squares.
194: This method requires that one be able to apply the transpose of the preconditioner and operator
195: as well as the operator and preconditioner. If the transpose of the preconditioner is not available then
196: the preconditioner is used in its place so one ends up preconditioning $A^T A$ with $B B$. Seems odd?
198: This only supports left preconditioning.
200: Developer Note:
201: This object is subclassed off of `KSPCG`, see the source code in src/ksp/ksp/impls/cg for comments on the structure of the code
203: .seealso: [](ch_ksp), `KSPCreate()`, `KSPSetType()`, `KSPType`, `KSP`, `KSPCG`, `KSPLSQR`, `KSPCGLS`,
204: `KSPCGSetType()`, `KSPBICG`, `KSPSetComputeEigenvalues()`, `KSPComputeEigenvalues()`, `PETSCREGRESSORLINEAR`
205: M*/
207: PETSC_EXTERN PetscErrorCode KSPCreate_CGNE(KSP ksp)
208: {
209: KSP_CG *cg;
211: PetscFunctionBegin;
212: PetscCall(PetscNew(&cg));
213: cg->type = !PetscDefined(USE_COMPLEX) ? KSP_CG_SYMMETRIC : KSP_CG_HERMITIAN;
214: ksp->data = (void *)cg;
215: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_PRECONDITIONED, PC_LEFT, 3));
216: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_UNPRECONDITIONED, PC_LEFT, 2));
217: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_NATURAL, PC_LEFT, 2));
218: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_NONE, PC_LEFT, 1));
220: /*
221: Sets the functions that are associated with this data structure
222: (in C++ this is the same as defining virtual functions)
223: */
224: ksp->ops->setup = KSPSetUp_CGNE;
225: ksp->ops->solve = KSPSolve_CGNE;
226: ksp->ops->destroy = KSPDestroy_CG;
227: ksp->ops->view = KSPView_CG;
228: ksp->ops->setfromoptions = KSPSetFromOptions_CG;
229: ksp->ops->buildsolution = KSPBuildSolutionDefault;
230: ksp->ops->buildresidual = KSPBuildResidualDefault;
232: /*
233: Attach the function KSPCGSetType_CGNE() to this object. The routine
234: KSPCGSetType() checks for this attached function and calls it if it finds
235: it. (Sort of like a dynamic member function that can be added at run time
236: */
237: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPCGSetType_C", KSPCGSetType_CGNE));
238: PetscFunctionReturn(PETSC_SUCCESS);
239: }