Actual source code: cgne.c


  2: /*
  3:        cgimpl.h defines the simple data structured used to store information
  4:     related to the type of matrix (e.g. complex symmetric) being solved and
  5:     data used during the optional Lanczo process used to compute eigenvalues
  6: */
  7: #include <../src/ksp/ksp/impls/cg/cgimpl.h>
  8: extern PetscErrorCode KSPComputeExtremeSingularValues_CG(KSP,PetscReal*,PetscReal*);
  9: extern PetscErrorCode KSPComputeEigenvalues_CG(KSP,PetscInt,PetscReal*,PetscReal*,PetscInt*);

 11: static PetscErrorCode  KSPCGSetType_CGNE(KSP ksp,KSPCGType type)
 12: {
 13:   KSP_CG *cg = (KSP_CG*)ksp->data;

 15:   cg->type = type;
 16:   return 0;
 17: }

 19: /*
 20:      KSPSetUp_CGNE - Sets up the workspace needed by the CGNE method.

 22:      IDENTICAL TO THE CG ONE EXCEPT for one extra work vector!
 23: */
 24: static PetscErrorCode KSPSetUp_CGNE(KSP ksp)
 25: {
 26:   KSP_CG         *cgP = (KSP_CG*)ksp->data;
 27:   PetscInt       maxit = ksp->max_it;

 29:   /* get work vectors needed by CGNE */
 30:   KSPSetWorkVecs(ksp,4);

 32:   /*
 33:      If user requested computations of eigenvalues then allocate work
 34:      work space needed
 35:   */
 36:   if (ksp->calc_sings) {
 37:     /* get space to store tridiagonal matrix for Lanczos */
 38:     PetscMalloc4(maxit,&cgP->e,maxit,&cgP->d,maxit,&cgP->ee,maxit,&cgP->dd);
 39:     PetscLogObjectMemory((PetscObject)ksp,2*maxit*(sizeof(PetscScalar)+sizeof(PetscReal)));

 41:     ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_CG;
 42:     ksp->ops->computeeigenvalues           = KSPComputeEigenvalues_CG;
 43:   }
 44:   return 0;
 45: }

 47: /*
 48:        KSPSolve_CGNE - This routine actually applies the conjugate gradient
 49:     method

 51:    Input Parameter:
 52: .     ksp - the Krylov space object that was set to use conjugate gradient, by, for
 53:             example, KSPCreate(MPI_Comm,KSP *ksp); KSPSetType(ksp,KSPCG);

 55:     Virtually identical to the KSPSolve_CG, it should definitely reuse the same code.

 57: */
 58: static PetscErrorCode  KSPSolve_CGNE(KSP ksp)
 59: {
 60:   PetscInt       i,stored_max_it,eigs;
 61:   PetscScalar    dpi,a = 1.0,beta,betaold = 1.0,b = 0,*e = NULL,*d = NULL;
 62:   PetscReal      dp = 0.0;
 63:   Vec            X,B,Z,R,P,T;
 64:   KSP_CG         *cg;
 65:   Mat            Amat,Pmat;
 66:   PetscBool      diagonalscale,transpose_pc;

 68:   PCGetDiagonalScale(ksp->pc,&diagonalscale);
 70:   PCApplyTransposeExists(ksp->pc,&transpose_pc);

 72:   cg            = (KSP_CG*)ksp->data;
 73:   eigs          = ksp->calc_sings;
 74:   stored_max_it = ksp->max_it;
 75:   X             = ksp->vec_sol;
 76:   B             = ksp->vec_rhs;
 77:   R             = ksp->work[0];
 78:   Z             = ksp->work[1];
 79:   P             = ksp->work[2];
 80:   T             = ksp->work[3];

 82: #define VecXDot(x,y,a) (((cg->type) == (KSP_CG_HERMITIAN)) ? VecDot(x,y,a) : VecTDot(x,y,a))

 84:   if (eigs) {e = cg->e; d = cg->d; e[0] = 0.0; }
 85:   PCGetOperators(ksp->pc,&Amat,&Pmat);

 87:   ksp->its = 0;
 88:   KSP_MatMultTranspose(ksp,Amat,B,T);
 89:   if (!ksp->guess_zero) {
 90:     KSP_MatMult(ksp,Amat,X,P);
 91:     KSP_MatMultTranspose(ksp,Amat,P,R);
 92:     VecAYPX(R,-1.0,T);
 93:   } else {
 94:     VecCopy(T,R);              /*     r <- b (x is 0) */
 95:   }
 96:   if (transpose_pc) {
 97:     KSP_PCApplyTranspose(ksp,R,T);
 98:   } else {
 99:     KSP_PCApply(ksp,R,T);
100:   }
101:   KSP_PCApply(ksp,T,Z);

103:   if (ksp->normtype == KSP_NORM_PRECONDITIONED) {
104:     VecNorm(Z,NORM_2,&dp); /*    dp <- z'*z       */
105:   } else if (ksp->normtype == KSP_NORM_UNPRECONDITIONED) {
106:     VecNorm(R,NORM_2,&dp); /*    dp <- r'*r       */
107:   } else if (ksp->normtype == KSP_NORM_NATURAL) {
108:     VecXDot(Z,R,&beta);
109:     KSPCheckDot(ksp,beta);
110:     dp   = PetscSqrtReal(PetscAbsScalar(beta));
111:   } else dp = 0.0;
112:   KSPLogResidualHistory(ksp,dp);
113:   KSPMonitor(ksp,0,dp);
114:   ksp->rnorm = dp;
115:   (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP); /* test for convergence */
116:   if (ksp->reason) return 0;

118:   i = 0;
119:   do {
120:     ksp->its = i+1;
121:     VecXDot(Z,R,&beta); /*     beta <- r'z     */
122:     KSPCheckDot(ksp,beta);
123:     if (beta == 0.0) {
124:       ksp->reason = KSP_CONVERGED_ATOL;
125:       PetscInfo(ksp,"converged due to beta = 0\n");
126:       break;
127: #if !defined(PETSC_USE_COMPLEX)
128:     } else if (beta < 0.0) {
129:       ksp->reason = KSP_DIVERGED_INDEFINITE_PC;
130:       PetscInfo(ksp,"diverging due to indefinite preconditioner\n");
131:       break;
132: #endif
133:     }
134:     if (!i) {
135:       VecCopy(Z,P);          /*     p <- z          */
136:       b    = 0.0;
137:     } else {
138:       b = beta/betaold;
139:       if (eigs) {
141:         e[i] = PetscSqrtReal(PetscAbsScalar(b))/a;
142:       }
143:       VecAYPX(P,b,Z);     /*     p <- z + b* p   */
144:     }
145:     betaold = beta;
146:     KSP_MatMult(ksp,Amat,P,T);
147:     KSP_MatMultTranspose(ksp,Amat,T,Z);
148:     VecXDot(P,Z,&dpi);    /*     dpi <- z'p      */
149:     KSPCheckDot(ksp,dpi);
150:     a       = beta/dpi;                            /*     a = beta/p'z    */
151:     if (eigs) d[i] = PetscSqrtReal(PetscAbsScalar(b))*e[i] + 1.0/a;
152:     VecAXPY(X,a,P);           /*     x <- x + ap     */
153:     VecAXPY(R,-a,Z);                       /*     r <- r - az     */
154:     if (ksp->normtype == KSP_NORM_PRECONDITIONED) {
155:       if (transpose_pc) {
156:         KSP_PCApplyTranspose(ksp,R,T);
157:       } else {
158:         KSP_PCApply(ksp,R,T);
159:       }
160:       KSP_PCApply(ksp,T,Z);
161:       VecNorm(Z,NORM_2,&dp);              /*    dp <- z'*z       */
162:     } else if (ksp->normtype == KSP_NORM_UNPRECONDITIONED) {
163:       VecNorm(R,NORM_2,&dp);
164:     } else if (ksp->normtype == KSP_NORM_NATURAL) {
165:       dp = PetscSqrtReal(PetscAbsScalar(beta));
166:     } else dp = 0.0;
167:     ksp->rnorm = dp;
168:     KSPLogResidualHistory(ksp,dp);
169:     KSPMonitor(ksp,i+1,dp);
170:     (*ksp->converged)(ksp,i+1,dp,&ksp->reason,ksp->cnvP);
171:     if (ksp->reason) break;
172:     if (ksp->normtype != KSP_NORM_PRECONDITIONED) {
173:       if (transpose_pc) {
174:         KSP_PCApplyTranspose(ksp,R,T);
175:       } else {
176:         KSP_PCApply(ksp,R,T);
177:       }
178:       KSP_PCApply(ksp,T,Z);
179:     }
180:     i++;
181:   } while (i<ksp->max_it);
182:   if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
183:   return 0;
184: }

186: /*
187:     KSPCreate_CGNE - Creates the data structure for the Krylov method CGNE and sets the
188:        function pointers for all the routines it needs to call (KSPSolve_CGNE() etc)

190:     It must be labeled as PETSC_EXTERN to be dynamically linkable in C++
191: */

193: /*MC
194:      KSPCGNE - Applies the preconditioned conjugate gradient method to the normal equations
195:           without explicitly forming A^t*A

197:    Options Database Keys:
198: .   -ksp_cg_type <Hermitian or symmetric - (for complex matrices only) indicates the matrix is Hermitian or symmetric

200:    Level: beginner

202:    Notes:
203:     eigenvalue computation routines will return information about the
204:           spectrum of A^t*A, rather than A.

206:    CGNE is a general-purpose non-symmetric method. It works well when the singular values are much better behaved than
207:    eigenvalues. A unitary matrix is a classic example where CGNE converges in one iteration, but GMRES and CGS need N
208:    iterations (see Nachtigal, Reddy, and Trefethen, "How fast are nonsymmetric matrix iterations", 1992). If you intend
209:    to solve least squares problems, use KSPLSQR.

211:    This is NOT a different algorithm than used with KSPCG, it merely uses that algorithm with the
212:    matrix defined by A^t*A and preconditioner defined by B^t*B where B is the preconditioner for A.

214:    This method requires that one be able to apply the transpose of the preconditioner and operator
215:    as well as the operator and preconditioner. If the transpose of the preconditioner is not available then
216:    the preconditioner is used in its place so one ends up preconditioning A'A with B B. Seems odd?

218:    This only supports left preconditioning.

220:    This object is subclassed off of KSPCG

222: .seealso:  KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP,
223:            KSPCGSetType(), KSPBICG

225: M*/

227: PETSC_EXTERN PetscErrorCode KSPCreate_CGNE(KSP ksp)
228: {
229:   KSP_CG         *cg;

231:   PetscNewLog(ksp,&cg);
232: #if !defined(PETSC_USE_COMPLEX)
233:   cg->type = KSP_CG_SYMMETRIC;
234: #else
235:   cg->type = KSP_CG_HERMITIAN;
236: #endif
237:   ksp->data = (void*)cg;
238:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,3);
239:   KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_LEFT,2);
240:   KSPSetSupportedNorm(ksp,KSP_NORM_NATURAL,PC_LEFT,2);
241:   KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_LEFT,1);

243:   /*
244:        Sets the functions that are associated with this data structure
245:        (in C++ this is the same as defining virtual functions)
246:   */
247:   ksp->ops->setup          = KSPSetUp_CGNE;
248:   ksp->ops->solve          = KSPSolve_CGNE;
249:   ksp->ops->destroy        = KSPDestroy_CG;
250:   ksp->ops->view           = KSPView_CG;
251:   ksp->ops->setfromoptions = KSPSetFromOptions_CG;
252:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
253:   ksp->ops->buildresidual  = KSPBuildResidualDefault;

255:   /*
256:       Attach the function KSPCGSetType_CGNE() to this object. The routine
257:       KSPCGSetType() checks for this attached function and calls it if it finds
258:       it. (Sort of like a dynamic member function that can be added at run time
259:   */
260:   PetscObjectComposeFunction((PetscObject)ksp,"KSPCGSetType_C",KSPCGSetType_CGNE);
261:   return 0;
262: }