2: /*
3: This file implements the conjugate gradient method in PETSc as part of
4: KSP. You can use this as a starting point for implementing your own
5: Krylov method that is not provided with PETSc.
7: The following basic routines are required for each Krylov method.
8: KSPCreate_XXX() - Creates the Krylov context
9: KSPSetFromOptions_XXX() - Sets runtime options
10: KSPSolve_XXX() - Runs the Krylov method
11: KSPDestroy_XXX() - Destroys the Krylov context, freeing all
12: memory it needed
13: Here the "_XXX" denotes a particular implementation, in this case
14: we use _CG (e.g. KSPCreate_CG, KSPDestroy_CG). These routines are
15: are actually called vai the common user interface routines
16: KSPSetType(), KSPSetFromOptions(), KSPSolve(), and KSPDestroy() so the
17: application code interface remains identical for all preconditioners.
19: Other basic routines for the KSP objects include
20: KSPSetUp_XXX()
21: KSPView_XXX() - Prints details of solver being used.
23: Detailed notes:
24: By default, this code implements the CG (Conjugate Gradient) method,
25: which is valid for real symmetric (and complex Hermitian) positive
26: definite matrices. Note that for the complex Hermitian case, the
27: VecDot() arguments within the code MUST remain in the order given
28: for correct computation of inner products.
30: Reference: Hestenes and Steifel, 1952.
32: By switching to the indefinite vector inner product, VecTDot(), the
33: same code is used for the complex symmetric case as well. The user
34: must call KSPCGSetType(ksp,KSP_CG_SYMMETRIC) or use the option
35: -ksp_cg_type symmetric to invoke this variant for the complex case.
36: Note, however, that the complex symmetric code is NOT valid for
37: all such matrices ... and thus we don't recommend using this method.
38: */
39: /*
40: cgimpl.h defines the simple data structured used to store information
41: related to the type of matrix (e.g. complex symmetric) being solved and
42: data used during the optional Lanczo process used to compute eigenvalues
43: */
44: #include <../src/ksp/ksp/impls/cg/cgimpl.h> /*I "petscksp.h" I*/
45: extern PetscErrorCode KSPComputeExtremeSingularValues_CG(KSP,PetscReal*,PetscReal*);
46: extern PetscErrorCode KSPComputeEigenvalues_CG(KSP,PetscInt,PetscReal*,PetscReal*,PetscInt*);
48: /*
49: KSPSetUp_CG - Sets up the workspace needed by the CG method.
51: This is called once, usually automatically by KSPSolve() or KSPSetUp()
52: but can be called directly by KSPSetUp()
53: */
56: PetscErrorCode KSPSetUp_CG(KSP ksp) 57: {
58: KSP_CG *cgP = (KSP_CG*)ksp->data;
60: PetscInt maxit = ksp->max_it,nwork = 3;
63: /* get work vectors needed by CG */
64: if (cgP->singlereduction) nwork += 2;
65: KSPSetWorkVecs(ksp,nwork);
67: /*
68: If user requested computations of eigenvalues then allocate work
69: work space needed
70: */
71: if (ksp->calc_sings) {
72: /* get space to store tridiagonal matrix for Lanczos */
73: PetscMalloc4(maxit+1,&cgP->e,maxit+1,&cgP->d,maxit+1,&cgP->ee,maxit+1,&cgP->dd);
74: PetscLogObjectMemory((PetscObject)ksp,2*(maxit+1)*(sizeof(PetscScalar)+sizeof(PetscReal)));
76: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_CG;
77: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_CG;
78: }
79: return(0);
80: }
82: /*
83: KSPSolve_CG - This routine actually applies the conjugate gradient method
85: This routine is MUCH too messy. I has too many options (norm type and single reduction) embedded making the code confusing and likely to be buggy.
87: Input Parameter:
88: . ksp - the Krylov space object that was set to use conjugate gradient, by, for
89: example, KSPCreate(MPI_Comm,KSP *ksp); KSPSetType(ksp,KSPCG);
90: */
93: PetscErrorCode KSPSolve_CG(KSP ksp) 94: {
96: PetscInt i,stored_max_it,eigs;
97: PetscScalar dpi = 0.0,a = 1.0,beta,betaold = 1.0,b = 0,*e = 0,*d = 0,delta,dpiold;
98: PetscReal dp = 0.0;
99: Vec X,B,Z,R,P,S,W;
100: KSP_CG *cg;
101: Mat Amat,Pmat;
102: PetscBool diagonalscale;
105: PCGetDiagonalScale(ksp->pc,&diagonalscale);
106: if (diagonalscale) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",((PetscObject)ksp)->type_name);
108: cg = (KSP_CG*)ksp->data;
109: eigs = ksp->calc_sings;
110: stored_max_it = ksp->max_it;
111: X = ksp->vec_sol;
112: B = ksp->vec_rhs;
113: R = ksp->work[0];
114: Z = ksp->work[1];
115: P = ksp->work[2];
116: if (cg->singlereduction) {
117: S = ksp->work[3];
118: W = ksp->work[4];
119: } else {
120: S = 0; /* unused */
121: W = Z;
122: }
124: #define VecXDot(x,y,a) (((cg->type) == (KSP_CG_HERMITIAN)) ? VecDot(x,y,a) : VecTDot(x,y,a))126: if (eigs) {e = cg->e; d = cg->d; e[0] = 0.0; }
127: PCGetOperators(ksp->pc,&Amat,&Pmat);
129: ksp->its = 0;
130: if (!ksp->guess_zero) {
131: KSP_MatMult(ksp,Amat,X,R); /* r <- b - Ax */
132: VecAYPX(R,-1.0,B);
133: } else {
134: VecCopy(B,R); /* r <- b (x is 0) */
135: }
137: switch (ksp->normtype) {
138: case KSP_NORM_PRECONDITIONED:
139: KSP_PCApply(ksp,R,Z); /* z <- Br */
140: VecNorm(Z,NORM_2,&dp); /* dp <- z'*z = e'*A'*B'*B*A'*e' */
141: break;
142: case KSP_NORM_UNPRECONDITIONED:
143: VecNorm(R,NORM_2,&dp); /* dp <- r'*r = e'*A'*A*e */
144: break;
145: case KSP_NORM_NATURAL:
146: KSP_PCApply(ksp,R,Z); /* z <- Br */
147: if (cg->singlereduction) {
148: KSP_MatMult(ksp,Amat,Z,S);
149: VecXDot(Z,S,&delta);
150: }
151: VecXDot(Z,R,&beta); /* beta <- z'*r */
152: if (PetscIsInfOrNanScalar(beta)) {
153: if (ksp->errorifnotconverged) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_NOT_CONVERGED,"KSPSolve has not converged due to Nan or Inf inner product");
154: else {
155: ksp->reason = KSP_DIVERGED_NANORINF;
156: return(0);
157: }
158: }
159: dp = PetscSqrtReal(PetscAbsScalar(beta)); /* dp <- r'*z = r'*B*r = e'*A'*B*A*e */
160: break;
161: case KSP_NORM_NONE:
162: dp = 0.0;
163: break;
164: default:SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"%s",KSPNormTypes[ksp->normtype]);
165: }
166: KSPLogResidualHistory(ksp,dp);
167: KSPMonitor(ksp,0,dp);
168: ksp->rnorm = dp;
170: (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP); /* test for convergence */
171: if (ksp->reason) return(0);
173: if (ksp->normtype != KSP_NORM_PRECONDITIONED && (ksp->normtype != KSP_NORM_NATURAL)) {
174: KSP_PCApply(ksp,R,Z); /* z <- Br */
175: }
176: if (ksp->normtype != KSP_NORM_NATURAL) {
177: if (cg->singlereduction) {
178: KSP_MatMult(ksp,Amat,Z,S);
179: VecXDot(Z,S,&delta);
180: }
181: VecXDot(Z,R,&beta); /* beta <- z'*r */
182: if (PetscIsInfOrNanScalar(beta)) {
183: if (ksp->errorifnotconverged) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_NOT_CONVERGED,"KSPSolve has not converged due to Nan or Inf inner product");
184: else {
185: ksp->reason = KSP_DIVERGED_NANORINF;
186: return(0);
187: }
188: }
189: }
191: i = 0;
192: do {
193: ksp->its = i+1;
194: if (beta == 0.0) {
195: ksp->reason = KSP_CONVERGED_ATOL;
196: PetscInfo(ksp,"converged due to beta = 0\n");
197: break;
198: #if !defined(PETSC_USE_COMPLEX)
199: } else if ((i > 0) && (beta*betaold < 0.0)) {
200: ksp->reason = KSP_DIVERGED_INDEFINITE_PC;
201: PetscInfo(ksp,"diverging due to indefinite preconditioner\n");
202: break;
203: #endif
204: }
205: if (!i) {
206: VecCopy(Z,P); /* p <- z */
207: b = 0.0;
208: } else {
209: b = beta/betaold;
210: if (eigs) {
211: if (ksp->max_it != stored_max_it) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"Can not change maxit AND calculate eigenvalues");
212: e[i] = PetscSqrtReal(PetscAbsScalar(b))/a;
213: }
214: VecAYPX(P,b,Z); /* p <- z + b* p */
215: }
216: dpiold = dpi;
217: if (!cg->singlereduction || !i) {
218: KSP_MatMult(ksp,Amat,P,W); /* w <- Ap */
219: VecXDot(P,W,&dpi); /* dpi <- p'w */
220: } else {
221: VecAYPX(W,beta/betaold,S); /* w <- Ap */
222: dpi = delta - beta*beta*dpiold/(betaold*betaold); /* dpi <- p'w */
223: }
224: betaold = beta;
225: if (PetscIsInfOrNanScalar(dpi)) {
226: if (ksp->errorifnotconverged) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_NOT_CONVERGED,"KSPSolve has not converged due to Nan or Inf inner product");
227: else {
228: ksp->reason = KSP_DIVERGED_NANORINF;
229: return(0);
230: }
231: }
233: if ((dpi == 0.0) || ((i > 0) && (PetscRealPart(dpi*dpiold) <= 0.0))) {
234: ksp->reason = KSP_DIVERGED_INDEFINITE_MAT;
235: PetscInfo(ksp,"diverging due to indefinite or negative definite matrix\n");
236: break;
237: }
238: a = beta/dpi; /* a = beta/p'w */
239: if (eigs) d[i] = PetscSqrtReal(PetscAbsScalar(b))*e[i] + 1.0/a;
240: VecAXPY(X,a,P); /* x <- x + ap */
241: VecAXPY(R,-a,W); /* r <- r - aw */
242: if (ksp->normtype == KSP_NORM_PRECONDITIONED && ksp->chknorm < i+2) {
243: KSP_PCApply(ksp,R,Z); /* z <- Br */
244: if (cg->singlereduction) {
245: KSP_MatMult(ksp,Amat,Z,S);
246: }
247: VecNorm(Z,NORM_2,&dp); /* dp <- z'*z */
248: } else if (ksp->normtype == KSP_NORM_UNPRECONDITIONED && ksp->chknorm < i+2) {
249: VecNorm(R,NORM_2,&dp); /* dp <- r'*r */
250: } else if (ksp->normtype == KSP_NORM_NATURAL) {
251: KSP_PCApply(ksp,R,Z); /* z <- Br */
252: if (cg->singlereduction) {
253: PetscScalar tmp[2];
254: Vec vecs[2];
255: vecs[0] = S; vecs[1] = R;
256: KSP_MatMult(ksp,Amat,Z,S);
257: VecMDot(Z,2,vecs,tmp);
258: delta = tmp[0]; beta = tmp[1];
259: } else {
260: VecXDot(Z,R,&beta); /* beta <- r'*z */
261: }
262: if (PetscIsInfOrNanScalar(beta)) {
263: if (ksp->errorifnotconverged) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_NOT_CONVERGED,"KSPSolve has not converged due to Nan or Inf inner product");
264: else {
265: ksp->reason = KSP_DIVERGED_NANORINF;
266: return(0);
267: }
268: }
269: dp = PetscSqrtReal(PetscAbsScalar(beta));
270: } else {
271: dp = 0.0;
272: }
273: ksp->rnorm = dp;
274: KSPLogResidualHistory(ksp,dp);
275: KSPMonitor(ksp,i+1,dp);
276: (*ksp->converged)(ksp,i+1,dp,&ksp->reason,ksp->cnvP);
277: if (ksp->reason) break;
279: if ((ksp->normtype != KSP_NORM_PRECONDITIONED && (ksp->normtype != KSP_NORM_NATURAL)) || (ksp->chknorm >= i+2)) {
280: KSP_PCApply(ksp,R,Z); /* z <- Br */
281: if (cg->singlereduction) {
282: KSP_MatMult(ksp,Amat,Z,S);
283: }
284: }
285: if ((ksp->normtype != KSP_NORM_NATURAL) || (ksp->chknorm >= i+2)) {
286: if (cg->singlereduction) {
287: PetscScalar tmp[2];
288: Vec vecs[2];
289: vecs[0] = S; vecs[1] = R;
290: VecMDot(Z,2,vecs,tmp);
291: delta = tmp[0]; beta = tmp[1];
292: } else {
293: VecXDot(Z,R,&beta); /* beta <- z'*r */
294: }
295: if (PetscIsInfOrNanScalar(beta)) {
296: if (ksp->errorifnotconverged) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_NOT_CONVERGED,"KSPSolve has not converged due to Nan or Inf inner product");
297: else {
298: ksp->reason = KSP_DIVERGED_NANORINF;
299: return(0);
300: }
301: }
302: }
304: i++;
305: } while (i<ksp->max_it);
306: if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
307: return(0);
308: }
312: PetscErrorCode KSPDestroy_CG(KSP ksp)313: {
314: KSP_CG *cg = (KSP_CG*)ksp->data;
318: /* free space used for singular value calculations */
319: if (ksp->calc_sings) {
320: PetscFree4(cg->e,cg->d,cg->ee,cg->dd);
321: }
322: KSPDestroyDefault(ksp);
323: PetscObjectComposeFunction((PetscObject)ksp,"KSPCGSetType_C",NULL);
324: PetscObjectComposeFunction((PetscObject)ksp,"KSPCGUseSingleReduction_C",NULL);
325: return(0);
326: }
328: /*
329: KSPView_CG - Prints information about the current Krylov method being used
331: Currently this only prints information to a file (or stdout) about the
332: symmetry of the problem. If your Krylov method has special options or
333: flags that information should be printed here.
335: */
338: PetscErrorCode KSPView_CG(KSP ksp,PetscViewer viewer)339: {
340: #if defined(PETSC_USE_COMPLEX)
341: KSP_CG *cg = (KSP_CG*)ksp->data;
343: PetscBool iascii;
346: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
347: if (iascii) {
348: PetscViewerASCIIPrintf(viewer," CG or CGNE: variant %s\n",KSPCGTypes[cg->type]);
349: }
350: #endif
351: return(0);
352: }
354: /*
355: KSPSetFromOptions_CG - Checks the options database for options related to the
356: conjugate gradient method.
357: */
360: PetscErrorCode KSPSetFromOptions_CG(KSP ksp)361: {
363: KSP_CG *cg = (KSP_CG*)ksp->data;
366: PetscOptionsHead("KSP CG and CGNE options");
367: #if defined(PETSC_USE_COMPLEX)
368: PetscOptionsEnum("-ksp_cg_type","Matrix is Hermitian or complex symmetric","KSPCGSetType",KSPCGTypes,(PetscEnum)cg->type,
369: (PetscEnum*)&cg->type,NULL);
370: #endif
371: PetscOptionsBool("-ksp_cg_single_reduction","Merge inner products into single MPI_Allreduce()",
372: "KSPCGUseSingleReduction",cg->singlereduction,&cg->singlereduction,NULL);
373: PetscOptionsTail();
374: return(0);
375: }
377: /*
378: KSPCGSetType_CG - This is an option that is SPECIFIC to this particular Krylov method.
379: This routine is registered below in KSPCreate_CG() and called from the
380: routine KSPCGSetType() (see the file cgtype.c).
381: */
384: static PetscErrorCode KSPCGSetType_CG(KSP ksp,KSPCGType type)385: {
386: KSP_CG *cg = (KSP_CG*)ksp->data;
389: cg->type = type;
390: return(0);
391: }
395: static PetscErrorCode KSPCGUseSingleReduction_CG(KSP ksp,PetscBool flg)396: {
397: KSP_CG *cg = (KSP_CG*)ksp->data;
400: cg->singlereduction = flg;
401: return(0);
402: }
404: /*
405: KSPCreate_CG - Creates the data structure for the Krylov method CG and sets the
406: function pointers for all the routines it needs to call (KSPSolve_CG() etc)
408: It must be wrapped in EXTERN_C_BEGIN to be dynamically linkable in C++
409: */
410: /*MC
411: KSPCG - The preconditioned conjugate gradient (PCG) iterative method
413: Options Database Keys:
414: + -ksp_cg_type Hermitian - (for complex matrices only) indicates the matrix is Hermitian, see KSPCGSetType()
415: . -ksp_cg_type symmetric - (for complex matrices only) indicates the matrix is symmetric
416: - -ksp_cg_single_reduction - performs both inner products needed in the algorithm with a single MPI_Allreduce() call, see KSPCGUseSingleReduction()
418: Level: beginner
420: Notes: The PCG method requires both the matrix and preconditioner to be symmetric positive (or negative) (semi) definite
421: Only left preconditioning is supported.
423: For complex numbers there are two different CG methods. One for Hermitian symmetric matrices and one for non-Hermitian symmetric matrices. Use
424: KSPCGSetType() to indicate which type you are using.
426: Developer Notes: KSPSolve_CG() should actually query the matrix to determine if it is Hermitian symmetric or not and NOT require the user to
427: indicate it to the KSP object.
429: References:
430: Methods of Conjugate Gradients for Solving Linear Systems, Magnus R. Hestenes and Eduard Stiefel,
431: Journal of Research of the National Bureau of Standards Vol. 49, No. 6, December 1952 Research Paper 2379
432: pp. 409--436.
434: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP,
435: KSPCGSetType(), KSPCGUseSingleReduction(), KSPPIPECG, KSPGROPPCG437: M*/
440: PETSC_EXTERN PetscErrorCode KSPCreate_CG(KSP ksp)441: {
443: KSP_CG *cg;
446: PetscNewLog(ksp,&cg);
447: #if !defined(PETSC_USE_COMPLEX)
448: cg->type = KSP_CG_SYMMETRIC;
449: #else
450: cg->type = KSP_CG_HERMITIAN;
451: #endif
452: ksp->data = (void*)cg;
454: KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,3);
455: KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_LEFT,2);
456: KSPSetSupportedNorm(ksp,KSP_NORM_NATURAL,PC_LEFT,2);
457: KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_LEFT,2);
459: /*
460: Sets the functions that are associated with this data structure
461: (in C++ this is the same as defining virtual functions)
462: */
463: ksp->ops->setup = KSPSetUp_CG;
464: ksp->ops->solve = KSPSolve_CG;
465: ksp->ops->destroy = KSPDestroy_CG;
466: ksp->ops->view = KSPView_CG;
467: ksp->ops->setfromoptions = KSPSetFromOptions_CG;
468: ksp->ops->buildsolution = KSPBuildSolutionDefault;
469: ksp->ops->buildresidual = KSPBuildResidualDefault;
471: /*
472: Attach the function KSPCGSetType_CG() to this object. The routine
473: KSPCGSetType() checks for this attached function and calls it if it finds
474: it. (Sort of like a dynamic member function that can be added at run time
475: */
476: PetscObjectComposeFunction((PetscObject)ksp,"KSPCGSetType_C",KSPCGSetType_CG);
477: PetscObjectComposeFunction((PetscObject)ksp,"KSPCGUseSingleReduction_C",KSPCGUseSingleReduction_CG);
478: return(0);
479: }