Actual source code: cgne.c
1: #define PETSCKSP_DLL
3: /*
4: cgctx.h defines the simple data structured used to store information
5: related to the type of matrix (e.g. complex symmetric) being solved and
6: data used during the optional Lanczo process used to compute eigenvalues
7: */
8: #include ../src/ksp/ksp/impls/cg/cgctx.h
9: EXTERN PetscErrorCode KSPComputeExtremeSingularValues_CG(KSP,PetscReal *,PetscReal *);
10: EXTERN PetscErrorCode KSPComputeEigenvalues_CG(KSP,PetscInt,PetscReal *,PetscReal *,PetscInt *);
13: /*
14: KSPSetUp_CGNE - Sets up the workspace needed by the CGNE method.
16: IDENTICAL TO THE CG ONE EXCEPT for one extra work vector!
17: */
20: PetscErrorCode KSPSetUp_CGNE(KSP ksp)
21: {
22: KSP_CG *cgP = (KSP_CG*)ksp->data;
24: PetscInt maxit = ksp->max_it;
27: /*
28: This implementation of CGNE only handles left preconditioning
29: so generate an error otherwise.
30: */
31: if (ksp->pc_side == PC_RIGHT) {
32: SETERRQ(PETSC_ERR_SUP,"No right preconditioning for KSPCGNE");
33: } else if (ksp->pc_side == PC_SYMMETRIC) {
34: SETERRQ(PETSC_ERR_SUP,"No symmetric preconditioning for KSPCGNE");
35: }
37: /* get work vectors needed by CGNE */
38: KSPDefaultGetWork(ksp,4);
40: /*
41: If user requested computations of eigenvalues then allocate work
42: work space needed
43: */
44: if (ksp->calc_sings) {
45: /* get space to store tridiagonal matrix for Lanczos */
46: PetscMalloc(2*(maxit+1)*sizeof(PetscScalar),&cgP->e);
47: PetscLogObjectMemory(ksp,2*(maxit+1)*sizeof(PetscScalar));
48: cgP->d = cgP->e + maxit + 1;
49: PetscMalloc(2*(maxit+1)*sizeof(PetscReal),&cgP->ee);
50: PetscLogObjectMemory(ksp,2*(maxit+1)*sizeof(PetscScalar));
51: cgP->dd = cgP->ee + maxit + 1;
52: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_CG;
53: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_CG;
54: }
55: return(0);
56: }
58: /*
59: KSPSolve_CGNE - This routine actually applies the conjugate gradient
60: method
62: Input Parameter:
63: . ksp - the Krylov space object that was set to use conjugate gradient, by, for
64: example, KSPCreate(MPI_Comm,KSP *ksp); KSPSetType(ksp,KSPCG);
67: Virtually identical to the KSPSolve_CG, it should definitely reuse the same code.
69: */
72: PetscErrorCode KSPSolve_CGNE(KSP ksp)
73: {
75: PetscInt i,stored_max_it,eigs;
76: PetscScalar dpi,a = 1.0,beta,betaold = 1.0,b = 0,*e = 0,*d = 0;
77: PetscReal dp = 0.0;
78: Vec X,B,Z,R,P,T;
79: KSP_CG *cg;
80: Mat Amat,Pmat;
81: MatStructure pflag;
82: PetscTruth diagonalscale,transpose_pc;
85: PCDiagonalScale(ksp->pc,&diagonalscale);
86: if (diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",((PetscObject)ksp)->type_name);
87: PCApplyTransposeExists(ksp->pc,&transpose_pc);
89: cg = (KSP_CG*)ksp->data;
90: eigs = ksp->calc_sings;
91: stored_max_it = ksp->max_it;
92: X = ksp->vec_sol;
93: B = ksp->vec_rhs;
94: R = ksp->work[0];
95: Z = ksp->work[1];
96: P = ksp->work[2];
97: T = ksp->work[3];
99: #if !defined(PETSC_USE_COMPLEX)
100: #define VecXDot(x,y,a) VecDot(x,y,a)
101: #else
102: #define VecXDot(x,y,a) (((cg->type) == (KSP_CG_HERMITIAN)) ? VecDot(x,y,a) : VecTDot(x,y,a))
103: #endif
105: if (eigs) {e = cg->e; d = cg->d; e[0] = 0.0; }
106: PCGetOperators(ksp->pc,&Amat,&Pmat,&pflag);
108: ksp->its = 0;
109: MatMultTranspose(Amat,B,T);
110: if (!ksp->guess_zero) {
111: KSP_MatMult(ksp,Amat,X,P);
112: KSP_MatMultTranspose(ksp,Amat,P,R);
113: VecAYPX(R,-1.0,T);
114: } else {
115: VecCopy(T,R); /* r <- b (x is 0) */
116: }
117: KSP_PCApply(ksp,R,T);
118: if (transpose_pc) {
119: KSP_PCApplyTranspose(ksp,T,Z);
120: } else {
121: KSP_PCApply(ksp,T,Z);
122: }
124: if (ksp->normtype == KSP_NORM_PRECONDITIONED) {
125: VecNorm(Z,NORM_2,&dp); /* dp <- z'*z */
126: } else if (ksp->normtype == KSP_NORM_UNPRECONDITIONED) {
127: VecNorm(R,NORM_2,&dp); /* dp <- r'*r */
128: } else if (ksp->normtype == KSP_NORM_NATURAL) {
129: VecXDot(Z,R,&beta);
130: dp = sqrt(PetscAbsScalar(beta));
131: } else dp = 0.0;
132: KSPLogResidualHistory(ksp,dp);
133: KSPMonitor(ksp,0,dp); /* call any registered monitor routines */
134: ksp->rnorm = dp;
135: (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP); /* test for convergence */
136: if (ksp->reason) return(0);
138: i = 0;
139: do {
140: ksp->its = i+1;
141: VecXDot(Z,R,&beta); /* beta <- r'z */
142: if (beta == 0.0) {
143: ksp->reason = KSP_CONVERGED_ATOL;
144: PetscInfo(ksp,"converged due to beta = 0\n");
145: break;
146: #if !defined(PETSC_USE_COMPLEX)
147: } else if (beta < 0.0) {
148: ksp->reason = KSP_DIVERGED_INDEFINITE_PC;
149: PetscInfo(ksp,"diverging due to indefinite preconditioner\n");
150: break;
151: #endif
152: }
153: if (!i) {
154: VecCopy(Z,P); /* p <- z */
155: b = 0.0;
156: } else {
157: b = beta/betaold;
158: if (eigs) {
159: if (ksp->max_it != stored_max_it) {
160: SETERRQ(PETSC_ERR_SUP,"Can not change maxit AND calculate eigenvalues");
161: }
162: e[i] = sqrt(PetscAbsScalar(b))/a;
163: }
164: VecAYPX(P,b,Z); /* p <- z + b* p */
165: }
166: betaold = beta;
167: MatMult(Amat,P,T);
168: MatMultTranspose(Amat,T,Z);
169: VecXDot(P,Z,&dpi); /* dpi <- z'p */
170: a = beta/dpi; /* a = beta/p'z */
171: if (eigs) {
172: d[i] = sqrt(PetscAbsScalar(b))*e[i] + 1.0/a;
173: }
174: VecAXPY(X,a,P); /* x <- x + ap */
175: VecAXPY(R,-a,Z); /* r <- r - az */
176: if (ksp->normtype == KSP_NORM_PRECONDITIONED) {
177: KSP_PCApply(ksp,R,T);
178: if (transpose_pc) {
179: KSP_PCApplyTranspose(ksp,T,Z);
180: } else {
181: KSP_PCApply(ksp,T,Z);
182: }
183: VecNorm(Z,NORM_2,&dp); /* dp <- z'*z */
184: } else if (ksp->normtype == KSP_NORM_UNPRECONDITIONED) {
185: VecNorm(R,NORM_2,&dp);
186: } else if (ksp->normtype == KSP_NORM_NATURAL) {
187: dp = sqrt(PetscAbsScalar(beta));
188: } else {
189: dp = 0.0;
190: }
191: ksp->rnorm = dp;
192: KSPLogResidualHistory(ksp,dp);
193: KSPMonitor(ksp,i+1,dp);
194: (*ksp->converged)(ksp,i+1,dp,&ksp->reason,ksp->cnvP);
195: if (ksp->reason) break;
196: if (ksp->normtype != KSP_NORM_PRECONDITIONED) {
197: if (transpose_pc) {
198: KSP_PCApplyTranspose(ksp,T,Z);
199: } else {
200: KSP_PCApply(ksp,T,Z);
201: }
202: }
203: i++;
204: } while (i<ksp->max_it);
205: if (i >= ksp->max_it) {
206: ksp->reason = KSP_DIVERGED_ITS;
207: }
208: return(0);
209: }
211: /*
212: KSPCreate_CGNE - Creates the data structure for the Krylov method CGNE and sets the
213: function pointers for all the routines it needs to call (KSPSolve_CGNE() etc)
216: */
218: /*MC
219: KSPCGNE - Applies the preconditioned conjugate gradient method to the normal equations
220: without explicitly forming A^t*A
222: Options Database Keys:
223: . -ksp_cg_type <Hermitian or symmetric - (for complex matrices only) indicates the matrix is Hermitian or symmetric
226: Level: beginner
228: Notes: eigenvalue computation routines will return information about the
229: spectrum of A^t*A, rather than A.
231: This is NOT a different algorithm then used with KSPCG, it merely uses that algorithm with the
232: matrix defined by A^t*A and preconditioner defined by B^t*B where B is the preconditioner for A.
234: This method requires that one be apply to apply the transpose of the preconditioner and operator
235: as well as the operator and preconditioner. If the transpose of the preconditioner is not available then
236: the preconditioner is used in its place so one ends up preconditioning A'A with B B. Seems odd?
238: This object is subclassed off of KSPCG
240: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP,
241: KSPCGSetType(), KSPBICG
243: M*/
255: PetscErrorCode KSPCreate_CGNE(KSP ksp)
256: {
258: KSP_CG *cg;
261: PetscNewLog(ksp,KSP_CG,&cg);
262: #if !defined(PETSC_USE_COMPLEX)
263: cg->type = KSP_CG_SYMMETRIC;
264: #else
265: cg->type = KSP_CG_HERMITIAN;
266: #endif
267: ksp->data = (void*)cg;
268: ksp->pc_side = PC_LEFT;
270: /*
271: Sets the functions that are associated with this data structure
272: (in C++ this is the same as defining virtual functions)
273: */
274: ksp->ops->setup = KSPSetUp_CGNE;
275: ksp->ops->solve = KSPSolve_CGNE;
276: ksp->ops->destroy = KSPDestroy_CG;
277: ksp->ops->view = KSPView_CG;
278: ksp->ops->setfromoptions = KSPSetFromOptions_CG;
279: ksp->ops->buildsolution = KSPDefaultBuildSolution;
280: ksp->ops->buildresidual = KSPDefaultBuildResidual;
282: /*
283: Attach the function KSPCGSetType_CGNE() to this object. The routine
284: KSPCGSetType() checks for this attached function and calls it if it finds
285: it. (Sort of like a dynamic member function that can be added at run time
286: */
287: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPCGSetType_C","KSPCGSetType_CG",KSPCGSetType_CG);
288: return(0);
289: }