Actual source code: cgne.c

  1: /*
  2:        cgctx.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 Lanczo process used to compute eigenvalues
  5: */
 6:  #include src/ksp/ksp/impls/cg/cgctx.h
  7: EXTERN PetscErrorCode KSPComputeExtremeSingularValues_CG(KSP,PetscReal *,PetscReal *);
  8: EXTERN PetscErrorCode KSPComputeEigenvalues_CG(KSP,PetscInt,PetscReal *,PetscReal *,PetscInt *);


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

 14:       This is called once, usually automatically by KSPSolve() or KSPSetUp()
 15:      but can be called directly by KSPSetUp()
 16: */
 19: PetscErrorCode KSPSetUp_CGNE(KSP ksp)
 20: {
 21:   KSP_CG         *cgP = (KSP_CG*)ksp->data;
 23:   PetscInt       maxit = ksp->max_it;

 26:   /* 
 27:        This implementation of CGNE only handles left preconditioning
 28:      so generate an error otherwise.
 29:   */
 30:   if (ksp->pc_side == PC_RIGHT) {
 31:     SETERRQ(2,"No right preconditioning for KSPCGNE");
 32:   } else if (ksp->pc_side == PC_SYMMETRIC) {
 33:     SETERRQ(2,"No symmetric preconditioning for KSPCGNE");
 34:   }

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

 39:   /*
 40:      If user requested computations of eigenvalues then allocate work
 41:      work space needed
 42:   */
 43:   if (ksp->calc_sings) {
 44:     /* get space to store tridiagonal matrix for Lanczos */
 45:     PetscMalloc(2*(maxit+1)*sizeof(PetscScalar),&cgP->e);
 46:     PetscLogObjectMemory(ksp,2*(maxit+1)*sizeof(PetscScalar));
 47:     cgP->d                         = cgP->e + maxit + 1;
 48:     PetscMalloc(2*(maxit+1)*sizeof(PetscReal),&cgP->ee);
 49:     PetscLogObjectMemory(ksp,2*(maxit+1)*sizeof(PetscScalar));
 50:     cgP->dd                        = cgP->ee + maxit + 1;
 51:     ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_CG;
 52:     ksp->ops->computeeigenvalues           = KSPComputeEigenvalues_CG;
 53:   }
 54:   return(0);
 55: }

 57: /*
 58:        KSPSolve_CGNE - This routine actually applies the conjugate gradient 
 59:     method

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

 65:    Output Parameter:
 66: .     its - number of iterations used

 68: */
 71: PetscErrorCode  KSPSolve_CGNE(KSP ksp)
 72: {
 74:   PetscInt       i,stored_max_it,eigs;
 75:   PetscScalar    dpi,a = 1.0,beta,betaold = 1.0,b,*e = 0,*d = 0,mone = -1.0,ma;
 76:   PetscReal      dp = 0.0;
 77:   Vec            X,B,Z,R,P,T;
 78:   KSP_CG         *cg;
 79:   Mat            Amat,Pmat;
 80:   MatStructure   pflag;
 81:   PetscTruth     diagonalscale,transpose_pc;

 84:   PCDiagonalScale(ksp->pc,&diagonalscale);
 85:   if (diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",ksp->type_name);
 86:   PCHasApplyTranspose(ksp->pc,&transpose_pc);

 88:   cg            = (KSP_CG*)ksp->data;
 89:   eigs          = ksp->calc_sings;
 90:   stored_max_it = ksp->max_it;
 91:   X             = ksp->vec_sol;
 92:   B             = ksp->vec_rhs;
 93:   R             = ksp->work[0];
 94:   Z             = ksp->work[1];
 95:   P             = ksp->work[2];
 96:   T             = ksp->work[3];

 98: #if !defined(PETSC_USE_COMPLEX)
 99: #define VecXDot(x,y,a) VecDot(x,y,a)
100: #else
101: #define VecXDot(x,y,a) (((cg->type) == (KSP_CG_HERMITIAN)) ? VecDot(x,y,a) : VecTDot(x,y,a))
102: #endif

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

107:   ksp->its = 0;
108:   MatMultTranspose(Amat,B,T);
109:   if (!ksp->guess_zero) {
110:     KSP_MatMult(ksp,Amat,X,P);
111:     KSP_MatMultTranspose(ksp,Amat,P,R);
112:     VecAYPX(&mone,T,R);
113:   } else {
114:     VecCopy(T,R);              /*     r <- b (x is 0) */
115:   }
116:   KSP_PCApply(ksp,R,T);
117:   if (transpose_pc) {
118:     KSP_PCApplyTranspose(ksp,T,Z);
119:   } else {
120:     KSP_PCApply(ksp,T,Z);
121:   }

123:   VecXDot(Z,R,&beta);
124:   if (ksp->normtype == KSP_PRECONDITIONED_NORM) {
125:     VecNorm(Z,NORM_2,&dp); /*    dp <- z'*z       */
126:   } else if (ksp->normtype == KSP_UNPRECONDITIONED_NORM) {
127:     VecNorm(R,NORM_2,&dp); /*    dp <- r'*r       */
128:   } else if (ksp->normtype == KSP_NATURAL_NORM) {
129:     dp = sqrt(PetscAbsScalar(beta));
130:   } else dp = 0.0;
131:   KSPLogResidualHistory(ksp,dp);
132:   KSPMonitor(ksp,0,dp);                              /* call any registered monitor routines */
133:   ksp->rnorm = dp;
134:   (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);      /* test for convergence */
135:   if (ksp->reason) return(0);

137:   i = 0;
138:   do {
139:      ksp->its = i+1;
140:      VecXDot(Z,R,&beta);     /*     beta <- r'z     */
141:      if (beta == 0.0) {
142:        ksp->reason = KSP_CONVERGED_ATOL;
143:        PetscLogInfo(ksp,"KSPSolve_CGNE:converged due to beta = 0");
144:        break;
145: #if !defined(PETSC_USE_COMPLEX)
146:      } else if (beta < 0.0) {
147:        ksp->reason = KSP_DIVERGED_INDEFINITE_PC;
148:        PetscLogInfo(ksp,"KSPSolve_CGNE:diverging due to indefinite preconditioner");
149:        break;
150: #endif
151:      }
152:      if (!i) {
153:        VecCopy(Z,P);         /*     p <- z          */
154:      } else {
155:          b = beta/betaold;
156:          if (eigs) {
157:            if (ksp->max_it != stored_max_it) {
158:              SETERRQ(PETSC_ERR_SUP,"Can not change maxit AND calculate eigenvalues");
159:            }
160:            e[i] = sqrt(PetscAbsScalar(b))/a;
161:          }
162:          VecAYPX(&b,Z,P);    /*     p <- z + b* p   */
163:      }
164:      betaold = beta;
165:      MatMult(Amat,P,T);
166:      MatMultTranspose(Amat,T,Z);
167:      VecXDot(P,Z,&dpi);      /*     dpi <- z'p      */
168:      a = beta/dpi;                                 /*     a = beta/p'z    */
169:      if (eigs) {
170:        d[i] = sqrt(PetscAbsScalar(b))*e[i] + 1.0/a;
171:      }
172:      VecAXPY(&a,P,X);          /*     x <- x + ap     */
173:      ma = -a; VecAXPY(&ma,Z,R);                      /*     r <- r - az     */
174:      if (ksp->normtype == KSP_PRECONDITIONED_NORM) {
175:        KSP_PCApply(ksp,R,T);
176:        if (transpose_pc) {
177:          KSP_PCApplyTranspose(ksp,T,Z);
178:        } else {
179:          KSP_PCApply(ksp,T,Z);
180:        }
181:        VecNorm(Z,NORM_2,&dp);              /*    dp <- z'*z       */
182:      } else if (ksp->normtype == KSP_UNPRECONDITIONED_NORM) {
183:        VecNorm(R,NORM_2,&dp);
184:      } else if (ksp->normtype == KSP_NATURAL_NORM) {
185:        dp = sqrt(PetscAbsScalar(beta));
186:      } else {
187:        dp = 0.0;
188:      }
189:      ksp->rnorm = dp;
190:      KSPLogResidualHistory(ksp,dp);
191:      KSPMonitor(ksp,i+1,dp);
192:      (*ksp->converged)(ksp,i+1,dp,&ksp->reason,ksp->cnvP);
193:      if (ksp->reason) break;
194:      if (ksp->normtype != KSP_PRECONDITIONED_NORM) {
195:        KSP_PCApply(ksp,R,Z); /* z <- Br  */
196:      }
197:      i++;
198:   } while (i<ksp->max_it);
199:   if (i >= ksp->max_it) {
200:     ksp->reason = KSP_DIVERGED_ITS;
201:   }
202:   return(0);
203: }
204: /*
205:        KSPDestroy_CGNE - Frees all memory space used by the Krylov method

207: */
210: PetscErrorCode KSPDestroy_CGNE(KSP ksp)
211: {
212:   KSP_CG         *cg = (KSP_CG*)ksp->data;

216:   /* free space used for singular value calculations */
217:   if (ksp->calc_sings) {
218:     PetscFree(cg->e);
219:     PetscFree(cg->ee);
220:   }
221:   KSPDefaultFreeWork(ksp);
222:   /* free the context variable */
223:   PetscFree(cg);
224:   return(0);
225: }

227: /*
228:      KSPView_CGNE - Prints information about the current Krylov method being used

230:       Currently this only prints information to a file (or stdout) about the 
231:       symmetry of the problem. If your Krylov method has special options or 
232:       flags that information should be printed here.

234: */
237: PetscErrorCode KSPView_CGNE(KSP ksp,PetscViewer viewer)
238: {
239: #if defined(PETSC_USE_COMPLEX)
240:   KSP_CG         *cg = (KSP_CG *)ksp->data;
242:   PetscTruth     iascii;

245:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
246:   if (iascii) {
247:     if (cg->type == KSP_CG_HERMITIAN) {
248:       PetscViewerASCIIPrintf(viewer,"  CG: variant for complex, Hermitian system\n");
249:     } else if (cg->type == KSP_CG_SYMMETRIC) {
250:       PetscViewerASCIIPrintf(viewer,"  CG: variant for complex, symmetric system\n");
251:     } else {
252:       PetscViewerASCIIPrintf(viewer,"  CG: unknown variant\n");
253:     }
254:   } else {
255:     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for KSP cg",((PetscObject)viewer)->type_name);
256:   }
257: #endif
258:   return(0);
259: }

261: /*
262:     KSPSetFromOptions_CGNE - Checks the options database for options related to the 
263:                            conjugate gradient method.
264: */
267: PetscErrorCode KSPSetFromOptions_CGNE(KSP ksp)
268: {
269: #if defined(PETSC_USE_COMPLEX)
271:   PetscTruth     flg;
272: #endif

275: #if defined(PETSC_USE_COMPLEX)
276:   PetscOptionsHead("KSP CGNE options");
277:     PetscOptionsLogicalGroupBegin("-ksp_cgne_Hermitian","Matrix is Hermitian","KSPCGSetType",&flg);
278:     if (flg) { KSPCGSetType(ksp,KSP_CG_HERMITIAN); }
279:     PetscOptionsLogicalGroupEnd("-ksp_cgne_symmetric","Matrix is complex symmetric, not Hermitian","KSPCGSetType",&flg);
280:     if (flg) { KSPCGSetType(ksp,KSP_CG_SYMMETRIC); }
281:   PetscOptionsTail();
282: #endif
283:   return(0);
284: }

286: /*
287:     KSPCGSetType_CGNE - This is an option that is SPECIFIC to this particular Krylov method.
288:                       This routine is registered below in KSPCreate_CGNE() and called from the 
289:                       routine KSPCGSetType() (see the file cgtype.c).

292: */
296: PetscErrorCode KSPCGSetType_CGNE(KSP ksp,KSPCGType type)
297: {
298:   KSP_CG *cg;

301:   cg = (KSP_CG *)ksp->data;
302:   cg->type = type;
303:   return(0);
304: }

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

312: */

314: /*MC
315:      KSPCGNE - Applies the preconditioned conjugate gradient method to the normal equations
316:           without explicitly forming A^t*A

318:    Options Database Keys:
319: +   -ksp_cgne_Hermitian - (for complex matrices only) indicates the matrix is Hermitian
320: -   -ksp_cgne_symmetric - (for complex matrices only) indicates the matrix is symmetric

322:    Level: beginner

324:    Notes: eigenvalue computation routines will return information about the
325:    spectrum of A^tA, rather than A.

327: .seealso:  KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP,
328:            KSPCGSetType()

330: M*/

335: PetscErrorCode KSPCreate_CGNE(KSP ksp)
336: {
338:   KSP_CG         *cg;

341:   PetscNew(KSP_CG,&cg);
342:   PetscMemzero(cg,sizeof(KSP_CG));
343:   PetscLogObjectMemory(ksp,sizeof(KSP_CG));
344: #if !defined(PETSC_USE_COMPLEX)
345:   cg->type                       = KSP_CG_SYMMETRIC;
346: #else
347:   cg->type                       = KSP_CG_HERMITIAN;
348: #endif
349:   ksp->data                      = (void*)cg;
350:   ksp->pc_side                   = PC_LEFT;

352:   /*
353:        Sets the functions that are associated with this data structure 
354:        (in C++ this is the same as defining virtual functions)
355:   */
356:   ksp->ops->setup                = KSPSetUp_CGNE;
357:   ksp->ops->solve                = KSPSolve_CGNE;
358:   ksp->ops->destroy              = KSPDestroy_CGNE;
359:   ksp->ops->view                 = KSPView_CGNE;
360:   ksp->ops->setfromoptions       = KSPSetFromOptions_CGNE;
361:   ksp->ops->buildsolution        = KSPDefaultBuildSolution;
362:   ksp->ops->buildresidual        = KSPDefaultBuildResidual;

364:   /*
365:       Attach the function KSPCGSetType_CGNE() to this object. The routine 
366:       KSPCGSetType() checks for this attached function and calls it if it finds
367:       it. (Sort of like a dynamic member function that can be added at run time
368:   */
369:   PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPCGSetType_C","KSPCGSetType_CGNE",
370:                                      KSPCGSetType_CGNE);
371:   return(0);
372: }