Actual source code: cg.c

  1: #define PETSCKSP_DLL

  3: /*
  4:     This file implements the conjugate gradient method in PETSc as part of
  5:     KSP. You can use this as a starting point for implementing your own 
  6:     Krylov method that is not provided with PETSc.

  8:     The following basic routines are required for each Krylov method.
  9:         KSPCreate_XXX()          - Creates the Krylov context
 10:         KSPSetFromOptions_XXX()  - Sets runtime options
 11:         KSPSolve_XXX()           - Runs the Krylov method
 12:         KSPDestroy_XXX()         - Destroys the Krylov context, freeing all 
 13:                                    memory it needed
 14:     Here the "_XXX" denotes a particular implementation, in this case 
 15:     we use _CG (e.g. KSPCreate_CG, KSPDestroy_CG). These routines are 
 16:     are actually called vai the common user interface routines
 17:     KSPSetType(), KSPSetFromOptions(), KSPSolve(), and KSPDestroy() so the
 18:     application code interface remains identical for all preconditioners.

 20:     Other basic routines for the KSP objects include
 21:         KSPSetUp_XXX()
 22:         KSPView_XXX()             - Prints details of solver being used.

 24:     Detailed notes:                         
 25:     By default, this code implements the CG (Conjugate Gradient) method,
 26:     which is valid for real symmetric (and complex Hermitian) positive
 27:     definite matrices. Note that for the complex Hermitian case, the
 28:     VecDot() arguments within the code MUST remain in the order given
 29:     for correct computation of inner products.

 31:     Reference: Hestenes and Steifel, 1952.

 33:     By switching to the indefinite vector inner product, VecTDot(), the
 34:     same code is used for the complex symmetric case as well.  The user
 35:     must call KSPCGSetType(ksp,KSP_CG_SYMMETRIC) or use the option 
 36:     -ksp_cg_type symmetric to invoke this variant for the complex case.
 37:     Note, however, that the complex symmetric code is NOT valid for
 38:     all such matrices ... and thus we don't recommend using this method.
 39: */
 40: /*
 41:        cgctx.h defines the simple data structured used to store information
 42:     related to the type of matrix (e.g. complex symmetric) being solved and
 43:     data used during the optional Lanczo process used to compute eigenvalues
 44: */
 45:  #include ../src/ksp/ksp/impls/cg/cgctx.h
 46: EXTERN PetscErrorCode KSPComputeExtremeSingularValues_CG(KSP,PetscReal *,PetscReal *);
 47: EXTERN PetscErrorCode KSPComputeEigenvalues_CG(KSP,PetscInt,PetscReal *,PetscReal *,PetscInt *);

 49: /*
 50:      KSPSetUp_CG - Sets up the workspace needed by the CG method. 

 52:       This is called once, usually automatically by KSPSolve() or KSPSetUp()
 53:      but can be called directly by KSPSetUp()
 54: */
 57: PetscErrorCode KSPSetUp_CG(KSP ksp)
 58: {
 59:   KSP_CG         *cgP = (KSP_CG*)ksp->data;
 61:   PetscInt        maxit = ksp->max_it;

 64:   /* 
 65:        This implementation of CG only handles left preconditioning
 66:      so generate an error otherwise.
 67:   */
 68:   if (ksp->pc_side == PC_RIGHT) {
 69:     SETERRQ(PETSC_ERR_SUP,"No right preconditioning for KSPCG");
 70:   } else if (ksp->pc_side == PC_SYMMETRIC) {
 71:     SETERRQ(PETSC_ERR_SUP,"No symmetric preconditioning for KSPCG");
 72:   }

 74:   /* get work vectors needed by CG */
 75:   KSPDefaultGetWork(ksp,3);

 77:   /*
 78:      If user requested computations of eigenvalues then allocate work
 79:      work space needed
 80:   */
 81:   if (ksp->calc_sings) {
 82:     /* get space to store tridiagonal matrix for Lanczos */
 83:     PetscMalloc(2*(maxit+1)*sizeof(PetscScalar),&cgP->e);
 84:     PetscLogObjectMemory(ksp,2*(maxit+1)*sizeof(PetscScalar));
 85:     cgP->d                         = cgP->e + maxit + 1;
 86:     PetscMalloc(2*(maxit+1)*sizeof(PetscReal),&cgP->ee);
 87:     PetscLogObjectMemory(ksp,2*(maxit+1)*sizeof(PetscScalar));
 88:     cgP->dd                        = cgP->ee + maxit + 1;
 89:     ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_CG;
 90:     ksp->ops->computeeigenvalues           = KSPComputeEigenvalues_CG;
 91:   }
 92:   return(0);
 93: }

 95: /*
 96:        KSPSolve_CG - This routine actually applies the conjugate gradient 
 97:     method

 99:    Input Parameter:
100: .     ksp - the Krylov space object that was set to use conjugate gradient, by, for 
101:             example, KSPCreate(MPI_Comm,KSP *ksp); KSPSetType(ksp,KSPCG);
102: */
105: PetscErrorCode  KSPSolve_CG(KSP ksp)
106: {
108:   PetscInt       i,stored_max_it,eigs;
109:   PetscScalar    dpi,a = 1.0,beta,betaold = 1.0,b = 0,*e = 0,*d = 0;
110:   PetscReal      dp = 0.0;
111:   Vec            X,B,Z,R,P;
112:   KSP_CG         *cg;
113:   Mat            Amat,Pmat;
114:   MatStructure   pflag;
115:   PetscTruth     diagonalscale;

118:   PCDiagonalScale(ksp->pc,&diagonalscale);
119:   if (diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",((PetscObject)ksp)->type_name);

121:   cg            = (KSP_CG*)ksp->data;
122:   eigs          = ksp->calc_sings;
123:   stored_max_it = ksp->max_it;
124:   X             = ksp->vec_sol;
125:   B             = ksp->vec_rhs;
126:   R             = ksp->work[0];
127:   Z             = ksp->work[1];
128:   P             = ksp->work[2];

130: #if !defined(PETSC_USE_COMPLEX)
131: #define VecXDot(x,y,a) VecDot(x,y,a)
132: #else
133: #define VecXDot(x,y,a) (((cg->type) == (KSP_CG_HERMITIAN)) ? VecDot(x,y,a) : VecTDot(x,y,a))
134: #endif

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

139:   ksp->its = 0;
140:   if (!ksp->guess_zero) {
141:     KSP_MatMult(ksp,Amat,X,R);            /*     r <- b - Ax     */
142:     VecAYPX(R,-1.0,B);
143:   } else {
144:     VecCopy(B,R);                         /*     r <- b (x is 0) */
145:   }

147:   if (ksp->normtype == KSP_NORM_PRECONDITIONED) {
148:     KSP_PCApply(ksp,R,Z);                   /*     z <- Br         */
149:     VecNorm(Z,NORM_2,&dp);                /*    dp <- z'*z = e'*A'*B'*B*A'*e'     */
150:   } else if (ksp->normtype == KSP_NORM_UNPRECONDITIONED) {
151:     VecNorm(R,NORM_2,&dp);                /*    dp <- r'*r = e'*A'*A*e            */
152:   } else if (ksp->normtype == KSP_NORM_NATURAL) {
153:     KSP_PCApply(ksp,R,Z);                   /*     z <- Br         */
154:     VecXDot(Z,R,&beta);                     /*  beta <- z'*r       */
155:     if PetscIsInfOrNanScalar(beta) SETERRQ(PETSC_ERR_FP,"Infinite or not-a-number generated in dot product");
156:     dp = sqrt(PetscAbsScalar(beta));                           /*    dp <- r'*z = r'*B*r = e'*A'*B*A*e */
157:   } else dp = 0.0;
158:   KSPLogResidualHistory(ksp,dp);
159:   KSPMonitor(ksp,0,dp);                              /* call any registered monitor routines */
160:   ksp->rnorm = dp;

162:   (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);      /* test for convergence */
163:   if (ksp->reason) return(0);

165:   if (ksp->normtype != KSP_NORM_PRECONDITIONED && (ksp->normtype != KSP_NORM_NATURAL)){
166:     KSP_PCApply(ksp,R,Z);                   /*     z <- Br         */
167:   }
168:   if (ksp->normtype != KSP_NORM_NATURAL){
169:     VecXDot(Z,R,&beta);         /*  beta <- z'*r       */
170:     if PetscIsInfOrNanScalar(beta) SETERRQ(PETSC_ERR_FP,"Infinite or not-a-number generated in dot product");
171:   }

173:   i = 0;
174:   do {
175:      ksp->its = i+1;
176:      if (beta == 0.0) {
177:        ksp->reason = KSP_CONVERGED_ATOL;
178:        PetscInfo(ksp,"converged due to beta = 0\n");
179:        break;
180: #if !defined(PETSC_USE_COMPLEX)
181:      } else if (beta < 0.0) {
182:        ksp->reason = KSP_DIVERGED_INDEFINITE_PC;
183:        PetscInfo(ksp,"diverging due to indefinite preconditioner\n");
184:        break;
185: #endif
186:      }
187:      if (!i) {
188:        VecCopy(Z,P);         /*     p <- z          */
189:        b = 0.0;
190:      } else {
191:        b = beta/betaold;
192:        if (eigs) {
193:          if (ksp->max_it != stored_max_it) {
194:            SETERRQ(PETSC_ERR_SUP,"Can not change maxit AND calculate eigenvalues");
195:          }
196:          e[i] = sqrt(PetscAbsScalar(b))/a;
197:        }
198:        VecAYPX(P,b,Z);    /*     p <- z + b* p   */
199:      }
200:      betaold = beta;
201:      KSP_MatMult(ksp,Amat,P,Z);          /*     z <- Kp         */
202:      VecXDot(P,Z,&dpi);      /*     dpi <- z'p      */
203:      if PetscIsInfOrNanScalar(dpi) SETERRQ(PETSC_ERR_FP,"Infinite or not-a-number generated in dot product");

205:      if (PetscRealPart(dpi) <= 0.0) {
206:        ksp->reason = KSP_DIVERGED_INDEFINITE_MAT;
207:        PetscInfo(ksp,"diverging due to indefinite or negative definite matrix\n");
208:        break;
209:      }
210:      a = beta/dpi;                                 /*     a = beta/p'z    */
211:      if (eigs) {
212:        d[i] = sqrt(PetscAbsScalar(b))*e[i] + 1.0/a;
213:      }
214:      VecAXPY(X,a,P);          /*     x <- x + ap     */
215:      VecAXPY(R,-a,Z);                      /*     r <- r - az     */
216:      if (ksp->normtype == KSP_NORM_PRECONDITIONED && ksp->chknorm < i+2) {
217:        KSP_PCApply(ksp,R,Z);               /*     z <- Br         */
218:        VecNorm(Z,NORM_2,&dp);              /*    dp <- z'*z       */
219:      } else if (ksp->normtype == KSP_NORM_UNPRECONDITIONED && ksp->chknorm < i+2) {
220:        VecNorm(R,NORM_2,&dp);              /*    dp <- r'*r       */
221:      } else if (ksp->normtype == KSP_NORM_NATURAL) {
222:        KSP_PCApply(ksp,R,Z);               /*     z <- Br         */
223:        VecXDot(Z,R,&beta);     /*  beta <- r'*z       */
224:        if PetscIsInfOrNanScalar(beta) SETERRQ(PETSC_ERR_FP,"Infinite or not-a-number generated in dot product");
225:        dp = sqrt(PetscAbsScalar(beta));
226:      } else {
227:        dp = 0.0;
228:      }
229:      ksp->rnorm = dp;
230:      KSPLogResidualHistory(ksp,dp);
231:      KSPMonitor(ksp,i+1,dp);
232:      (*ksp->converged)(ksp,i+1,dp,&ksp->reason,ksp->cnvP);
233:      if (ksp->reason) break;

235:      if ((ksp->normtype != KSP_NORM_PRECONDITIONED && (ksp->normtype != KSP_NORM_NATURAL)) || (ksp->chknorm >= i+2)){
236:        KSP_PCApply(ksp,R,Z);                   /*     z <- Br         */
237:      }
238:      if ((ksp->normtype != KSP_NORM_NATURAL) || (ksp->chknorm >= i+2)){
239:        VecXDot(Z,R,&beta);        /*  beta <- z'*r       */
240:        if PetscIsInfOrNanScalar(beta) SETERRQ(PETSC_ERR_FP,"Infinite or not-a-number generated in dot product");
241:      }

243:      i++;
244:   } while (i<ksp->max_it);
245:   if (i >= ksp->max_it) {
246:     ksp->reason = KSP_DIVERGED_ITS;
247:   }
248:   return(0);
249: }
250: /*
251:        KSPDestroy_CG - Frees all memory space used by the Krylov method

253: */
256: PetscErrorCode KSPDestroy_CG(KSP ksp)
257: {
258:   KSP_CG         *cg = (KSP_CG*)ksp->data;

262:   /* free space used for singular value calculations */
263:   if (ksp->calc_sings) {
264:     PetscFree(cg->e);
265:     PetscFree(cg->ee);
266:   }
267:   KSPDefaultDestroy(ksp);
268:   PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPCGSetType_C","",PETSC_NULL);
269:   return(0);
270: }

272: /*
273:      KSPView_CG - Prints information about the current Krylov method being used

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

279: */
282: PetscErrorCode KSPView_CG(KSP ksp,PetscViewer viewer)
283: {
284: #if defined(PETSC_USE_COMPLEX)
285:   KSP_CG         *cg = (KSP_CG *)ksp->data;
287:   PetscTruth     iascii;

290:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
291:   if (iascii) {
292:     PetscViewerASCIIPrintf(viewer,"  CG or CGNE: variant %s\n",KSPCGTypes[cg->type]);
293:   } else {
294:     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for KSP cg",((PetscObject)viewer)->type_name);
295:   }
296: #endif
297:   return(0);
298: }

300: /*
301:     KSPSetFromOptions_CG - Checks the options database for options related to the 
302:                            conjugate gradient method.
303: */
306: PetscErrorCode KSPSetFromOptions_CG(KSP ksp)
307: {
308: #if defined(PETSC_USE_COMPLEX)
310:   KSP_CG         *cg = (KSP_CG *)ksp->data;
311: #endif

314: #if defined(PETSC_USE_COMPLEX)
315:   PetscOptionsHead("KSP CG and CGNE options");
316:   PetscOptionsEnum("-ksp_cg_type","Matrix is Hermitian or complex symmetric","KSPCGSetType",KSPCGTypes,(PetscEnum)cg->type,
317:                           (PetscEnum*)&cg->type,PETSC_NULL);
318:   PetscOptionsTail();
319: #endif
320:   return(0);
321: }

323: /*
324:     KSPCGSetType_CG - This is an option that is SPECIFIC to this particular Krylov method.
325:                       This routine is registered below in KSPCreate_CG() and called from the 
326:                       routine KSPCGSetType() (see the file cgtype.c).

329: */
333: PetscErrorCode  KSPCGSetType_CG(KSP ksp,KSPCGType type)
334: {
335:   KSP_CG *cg;

338:   cg = (KSP_CG *)ksp->data;
339:   cg->type = type;
340:   return(0);
341: }

344: /*
345:     KSPCreate_CG - Creates the data structure for the Krylov method CG and sets the 
346:        function pointers for all the routines it needs to call (KSPSolve_CG() etc)

349: */
350: /*MC
351:      KSPCG - The preconditioned conjugate gradient (PCG) iterative method

353:    Options Database Keys:
354: +   -ksp_cg_type Hermitian - (for complex matrices only) indicates the matrix is Hermitian
355: -   -ksp_cg_type symmetric - (for complex matrices only) indicates the matrix is symmetric

357:    Level: beginner

359:    Notes: The PCG method requires both the matrix and preconditioner to 
360:           be symmetric positive (semi) definite

362:    References:
363:    Methods of Conjugate Gradients for Solving Linear Systems, Magnus R. Hestenes and Eduard Stiefel,
364:    Journal of Research of the National Bureau of Standards Vol. 49, No. 6, December 1952 Research Paper 2379
365:    pp. 409--436.

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

370: M*/
374: PetscErrorCode  KSPCreate_CG(KSP ksp)
375: {
377:   KSP_CG         *cg;

380:   PetscNewLog(ksp,KSP_CG,&cg);
381: #if !defined(PETSC_USE_COMPLEX)
382:   cg->type                       = KSP_CG_SYMMETRIC;
383: #else
384:   cg->type                       = KSP_CG_HERMITIAN;
385: #endif
386:   ksp->data                      = (void*)cg;
387:   ksp->pc_side                   = PC_LEFT;

389:   /*
390:        Sets the functions that are associated with this data structure 
391:        (in C++ this is the same as defining virtual functions)
392:   */
393:   ksp->ops->setup                = KSPSetUp_CG;
394:   ksp->ops->solve                = KSPSolve_CG;
395:   ksp->ops->destroy              = KSPDestroy_CG;
396:   ksp->ops->view                 = KSPView_CG;
397:   ksp->ops->setfromoptions       = KSPSetFromOptions_CG;
398:   ksp->ops->buildsolution        = KSPDefaultBuildSolution;
399:   ksp->ops->buildresidual        = KSPDefaultBuildResidual;

401:   /*
402:       Attach the function KSPCGSetType_CG() to this object. The routine 
403:       KSPCGSetType() checks for this attached function and calls it if it finds
404:       it. (Sort of like a dynamic member function that can be added at run time
405:   */
406:   PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPCGSetType_C",
407:                                            "KSPCGSetType_CG",
408:                                            KSPCGSetType_CG);
409:   return(0);
410: }