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>       /*I "petscksp.h" I*/


 12: /*
 13:      KSPSetUp_CGNE - Sets up the workspace needed by the CGNE method. 

 15:      IDENTICAL TO THE CG ONE EXCEPT for one extra work vector!
 16: */
 19: PetscErrorCode KSPSetUp_CGNE(KSP ksp)
 20: {
 21:   KSP_CG         *cgP = (KSP_CG*)ksp->data;
 23:   PetscInt       maxit = ksp->max_it;

 26:   /* get work vectors needed by CGNE */
 27:   KSPDefaultGetWork(ksp,4);

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

 41: /*
 42:        KSPSolve_CGNE - This routine actually applies the conjugate gradient 
 43:     method

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


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

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

 68:   PCGetDiagonalScale(ksp->pc,&diagonalscale);
 69:   if (diagonalscale) SETERRQ1(((PetscObject)ksp)->comm,PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",((PetscObject)ksp)->type_name);
 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: #if !defined(PETSC_USE_COMPLEX)
 83: #define VecXDot(x,y,a) VecDot(x,y,a)
 84: #else
 85: #define VecXDot(x,y,a) (((cg->type) == (KSP_CG_HERMITIAN)) ? VecDot(x,y,a) : VecTDot(x,y,a))
 86: #endif

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

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

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

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

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

199: */

201: /*MC
202:      KSPCGNE - Applies the preconditioned conjugate gradient method to the normal equations
203:           without explicitly forming A^t*A

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


209:    Level: beginner

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

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

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

221:    This only supports left preconditioning.

223:    Developer Notes: How is this related to the preconditioned LSQR implementation?

225:    This object is subclassed off of KSPCG

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

230: M*/


243: PetscErrorCode  KSPCreate_CGNE(KSP ksp)
244: {
246:   KSP_CG         *cg;

249:   PetscNewLog(ksp,KSP_CG,&cg);
250: #if !defined(PETSC_USE_COMPLEX)
251:   cg->type                       = KSP_CG_SYMMETRIC;
252: #else
253:   cg->type                       = KSP_CG_HERMITIAN;
254: #endif
255:   ksp->data                      = (void*)cg;
256:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,2);
257:   KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_LEFT,1);
258:   KSPSetSupportedNorm(ksp,KSP_NORM_NATURAL,PC_LEFT,1);

260:   /*
261:        Sets the functions that are associated with this data structure 
262:        (in C++ this is the same as defining virtual functions)
263:   */
264:   ksp->ops->setup                = KSPSetUp_CGNE;
265:   ksp->ops->solve                = KSPSolve_CGNE;
266:   ksp->ops->destroy              = KSPDestroy_CG;
267:   ksp->ops->view                 = KSPView_CG;
268:   ksp->ops->setfromoptions       = KSPSetFromOptions_CG;
269:   ksp->ops->buildsolution        = KSPDefaultBuildSolution;
270:   ksp->ops->buildresidual        = KSPDefaultBuildResidual;

272:   /*
273:       Attach the function KSPCGSetType_CGNE() to this object. The routine 
274:       KSPCGSetType() checks for this attached function and calls it if it finds
275:       it. (Sort of like a dynamic member function that can be added at run time
276:   */
277:   PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPCGSetType_C","KSPCGSetType_CG",KSPCGSetType_CG);
278:   return(0);
279: }