Actual source code: gmres.c

petsc-master 2016-09-24
Report Typos and Errors
  2: /*
  3:     This file implements GMRES (a Generalized Minimal Residual) method.
  4:     Reference:  Saad and Schultz, 1986.


  7:     Some comments on left vs. right preconditioning, and restarts.
  8:     Left and right preconditioning.
  9:     If right preconditioning is chosen, then the problem being solved
 10:     by gmres is actually
 11:        My =  AB^-1 y = f
 12:     so the initial residual is
 13:           r = f - Mx
 14:     Note that B^-1 y = x or y = B x, and if x is non-zero, the initial
 15:     residual is
 16:           r = f - A x
 17:     The final solution is then
 18:           x = B^-1 y

 20:     If left preconditioning is chosen, then the problem being solved is
 21:        My = B^-1 A x = B^-1 f,
 22:     and the initial residual is
 23:        r  = B^-1(f - Ax)

 25:     Restarts:  Restarts are basically solves with x0 not equal to zero.
 26:     Note that we can eliminate an extra application of B^-1 between
 27:     restarts as long as we don't require that the solution at the end
 28:     of an unsuccessful gmres iteration always be the solution x.
 29:  */

 31:  #include <../src/ksp/ksp/impls/gmres/gmresimpl.h>
 32: #define GMRES_DELTA_DIRECTIONS 10
 33: #define GMRES_DEFAULT_MAXK     30
 34: static PetscErrorCode KSPGMRESUpdateHessenberg(KSP,PetscInt,PetscBool,PetscReal*);
 35: static PetscErrorCode KSPGMRESBuildSoln(PetscScalar*,Vec,Vec,KSP,PetscInt);

 39: PetscErrorCode    KSPSetUp_GMRES(KSP ksp)
 40: {
 41:   PetscInt       hh,hes,rs,cc;
 43:   PetscInt       max_k,k;
 44:   KSP_GMRES      *gmres = (KSP_GMRES*)ksp->data;

 47:   max_k = gmres->max_k;          /* restart size */
 48:   hh    = (max_k + 2) * (max_k + 1);
 49:   hes   = (max_k + 1) * (max_k + 1);
 50:   rs    = (max_k + 2);
 51:   cc    = (max_k + 1);

 53:   PetscCalloc5(hh,&gmres->hh_origin,hes,&gmres->hes_origin,rs,&gmres->rs_origin,cc,&gmres->cc_origin,cc,&gmres->ss_origin);
 54:   PetscLogObjectMemory((PetscObject)ksp,(hh + hes + rs + 2*cc)*sizeof(PetscScalar));

 56:   if (ksp->calc_sings) {
 57:     /* Allocate workspace to hold Hessenberg matrix needed by lapack */
 58:     PetscMalloc1((max_k + 3)*(max_k + 9),&gmres->Rsvd);
 59:     PetscLogObjectMemory((PetscObject)ksp,(max_k + 3)*(max_k + 9)*sizeof(PetscScalar));
 60:     PetscMalloc1(6*(max_k+2),&gmres->Dsvd);
 61:     PetscLogObjectMemory((PetscObject)ksp,6*(max_k+2)*sizeof(PetscReal));
 62:   }

 64:   /* Allocate array to hold pointers to user vectors.  Note that we need
 65:    4 + max_k + 1 (since we need it+1 vectors, and it <= max_k) */
 66:   gmres->vecs_allocated = VEC_OFFSET + 2 + max_k + gmres->nextra_vecs;

 68:   PetscMalloc1(gmres->vecs_allocated,&gmres->vecs);
 69:   PetscMalloc1(VEC_OFFSET+2+max_k,&gmres->user_work);
 70:   PetscMalloc1(VEC_OFFSET+2+max_k,&gmres->mwork_alloc);
 71:   PetscLogObjectMemory((PetscObject)ksp,(VEC_OFFSET+2+max_k)*(sizeof(Vec*)+sizeof(PetscInt)) + gmres->vecs_allocated*sizeof(Vec));

 73:   if (gmres->q_preallocate) {
 74:     gmres->vv_allocated = VEC_OFFSET + 2 + max_k;

 76:     KSPCreateVecs(ksp,gmres->vv_allocated,&gmres->user_work[0],0,NULL);
 77:     PetscLogObjectParents(ksp,gmres->vv_allocated,gmres->user_work[0]);

 79:     gmres->mwork_alloc[0] = gmres->vv_allocated;
 80:     gmres->nwork_alloc    = 1;
 81:     for (k=0; k<gmres->vv_allocated; k++) {
 82:       gmres->vecs[k] = gmres->user_work[0][k];
 83:     }
 84:   } else {
 85:     gmres->vv_allocated = 5;

 87:     KSPCreateVecs(ksp,5,&gmres->user_work[0],0,NULL);
 88:     PetscLogObjectParents(ksp,5,gmres->user_work[0]);

 90:     gmres->mwork_alloc[0] = 5;
 91:     gmres->nwork_alloc    = 1;
 92:     for (k=0; k<gmres->vv_allocated; k++) {
 93:       gmres->vecs[k] = gmres->user_work[0][k];
 94:     }
 95:   }
 96:   return(0);
 97: }

 99: /*
100:     Run gmres, possibly with restart.  Return residual history if requested.
101:     input parameters:

103: .        gmres  - structure containing parameters and work areas

105:     output parameters:
106: .        nres    - residuals (from preconditioned system) at each step.
107:                   If restarting, consider passing nres+it.  If null,
108:                   ignored
109: .        itcount - number of iterations used.  nres[0] to nres[itcount]
110:                   are defined.  If null, ignored.

112:     Notes:
113:     On entry, the value in vector VEC_VV(0) should be the initial residual
114:     (this allows shortcuts where the initial preconditioned residual is 0).
115:  */
118: PetscErrorCode KSPGMRESCycle(PetscInt *itcount,KSP ksp)
119: {
120:   KSP_GMRES      *gmres = (KSP_GMRES*)(ksp->data);
121:   PetscReal      res_norm,res,hapbnd,tt;
123:   PetscInt       it     = 0, max_k = gmres->max_k;
124:   PetscBool      hapend = PETSC_FALSE;

127:   if (itcount) *itcount = 0;
128:   VecNormalize(VEC_VV(0),&res_norm);
129:   KSPCheckNorm(ksp,res_norm);
130:   res     = res_norm;
131:   *GRS(0) = res_norm;

133:   /* check for the convergence */
134:   PetscObjectSAWsTakeAccess((PetscObject)ksp);
135:   ksp->rnorm = res;
136:   PetscObjectSAWsGrantAccess((PetscObject)ksp);
137:   gmres->it  = (it - 1);
138:   KSPLogResidualHistory(ksp,res);
139:   KSPMonitor(ksp,ksp->its,res);
140:   if (!res) {
141:     ksp->reason = KSP_CONVERGED_ATOL;
142:     PetscInfo(ksp,"Converged due to zero residual norm on entry\n");
143:     return(0);
144:   }

146:   (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);
147:   while (!ksp->reason && it < max_k && ksp->its < ksp->max_it) {
148:     if (it) {
149:       KSPLogResidualHistory(ksp,res);
150:       KSPMonitor(ksp,ksp->its,res);
151:     }
152:     gmres->it = (it - 1);
153:     if (gmres->vv_allocated <= it + VEC_OFFSET + 1) {
154:       KSPGMRESGetNewVectors(ksp,it+1);
155:     }
156:     KSP_PCApplyBAorAB(ksp,VEC_VV(it),VEC_VV(1+it),VEC_TEMP_MATOP);

158:     /* update hessenberg matrix and do Gram-Schmidt */
159:     (*gmres->orthog)(ksp,it);
160:     if (ksp->reason) break;

162:     /* vv(i+1) . vv(i+1) */
163:     VecNormalize(VEC_VV(it+1),&tt);

165:     /* save the magnitude */
166:     *HH(it+1,it)  = tt;
167:     *HES(it+1,it) = tt;

169:     /* check for the happy breakdown */
170:     hapbnd = PetscAbsScalar(tt / *GRS(it));
171:     if (hapbnd > gmres->haptol) hapbnd = gmres->haptol;
172:     if (tt < hapbnd) {
173:       PetscInfo2(ksp,"Detected happy breakdown, current hapbnd = %14.12e tt = %14.12e\n",(double)hapbnd,(double)tt);
174:       hapend = PETSC_TRUE;
175:     }
176:     KSPGMRESUpdateHessenberg(ksp,it,hapend,&res);

178:     it++;
179:     gmres->it = (it-1);   /* For converged */
180:     ksp->its++;
181:     ksp->rnorm = res;
182:     if (ksp->reason) break;

184:     (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);

186:     /* Catch error in happy breakdown and signal convergence and break from loop */
187:     if (hapend) {
188:       if (!ksp->reason) {
189:         if (ksp->errorifnotconverged) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_NOT_CONVERGED,"You reached the happy break down, but convergence was not indicated. Residual norm = %g",(double)res);
190:         else {
191:           ksp->reason = KSP_DIVERGED_BREAKDOWN;
192:           break;
193:         }
194:       }
195:     }
196:   }

198:   /* Monitor if we know that we will not return for a restart */
199:   if (it && (ksp->reason || ksp->its >= ksp->max_it)) {
200:     KSPLogResidualHistory(ksp,res);
201:     KSPMonitor(ksp,ksp->its,res);
202:   }

204:   if (itcount) *itcount = it;


207:   /*
208:     Down here we have to solve for the "best" coefficients of the Krylov
209:     columns, add the solution values together, and possibly unwind the
210:     preconditioning from the solution
211:    */
212:   /* Form the solution (or the solution so far) */
213:   KSPGMRESBuildSoln(GRS(0),ksp->vec_sol,ksp->vec_sol,ksp,it-1);
214:   return(0);
215: }

219: PetscErrorCode KSPSolve_GMRES(KSP ksp)
220: {
222:   PetscInt       its,itcount,i;
223:   KSP_GMRES      *gmres     = (KSP_GMRES*)ksp->data;
224:   PetscBool      guess_zero = ksp->guess_zero;
225:   PetscInt       N = gmres->max_k + 1;
226:   PetscBLASInt   bN;

229:   if (ksp->calc_sings && !gmres->Rsvd) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ORDER,"Must call KSPSetComputeSingularValues() before KSPSetUp() is called");

231:   PetscObjectSAWsTakeAccess((PetscObject)ksp);
232:   ksp->its = 0;
233:   PetscObjectSAWsGrantAccess((PetscObject)ksp);

235:   itcount     = 0;
236:   gmres->fullcycle = 0;
237:   ksp->reason = KSP_CONVERGED_ITERATING;
238:   while (!ksp->reason) {
239:     KSPInitialResidual(ksp,ksp->vec_sol,VEC_TEMP,VEC_TEMP_MATOP,VEC_VV(0),ksp->vec_rhs);
240:     KSPGMRESCycle(&its,ksp);
241:     /* Store the Hessenberg matrix and the basis vectors of the Krylov subspace
242:     if the cycle is complete for the computation of the Ritz pairs */
243:     if (its == gmres->max_k) {
244:       gmres->fullcycle++;
245:       if (ksp->calc_ritz) {
246:         if (!gmres->hes_ritz) {
247:           PetscMalloc1(N*N,&gmres->hes_ritz);
248:           PetscLogObjectMemory((PetscObject)ksp,N*N*sizeof(PetscScalar));
249:           VecDuplicateVecs(VEC_VV(0),N,&gmres->vecb);
250:         }
251:         PetscBLASIntCast(N,&bN);
252:         PetscMemcpy(gmres->hes_ritz,gmres->hes_origin,bN*bN*sizeof(PetscReal));
253:         for (i=0; i<gmres->max_k+1; i++) {
254:           VecCopy(VEC_VV(i),gmres->vecb[i]);
255:         }
256:       }
257:     }
258:     itcount += its;
259:     if (itcount >= ksp->max_it) {
260:       if (!ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
261:       break;
262:     }
263:     ksp->guess_zero = PETSC_FALSE; /* every future call to KSPInitialResidual() will have nonzero guess */
264:   }
265:   ksp->guess_zero = guess_zero; /* restore if user provided nonzero initial guess */
266:   return(0);
267: }

271: PetscErrorCode KSPReset_GMRES(KSP ksp)
272: {
273:   KSP_GMRES      *gmres = (KSP_GMRES*)ksp->data;
275:   PetscInt       i;

278:   /* Free the Hessenberg matrices */
279:   PetscFree6(gmres->hh_origin,gmres->hes_origin,gmres->rs_origin,gmres->cc_origin,gmres->ss_origin,gmres->hes_ritz);

281:   /* free work vectors */
282:   PetscFree(gmres->vecs);
283:   for (i=0; i<gmres->nwork_alloc; i++) {
284:     VecDestroyVecs(gmres->mwork_alloc[i],&gmres->user_work[i]);
285:   }
286:   gmres->nwork_alloc = 0;
287:   if (gmres->vecb)  {
288:     VecDestroyVecs(gmres->max_k+1,&gmres->vecb);
289:   }

291:   PetscFree(gmres->user_work);
292:   PetscFree(gmres->mwork_alloc);
293:   PetscFree(gmres->nrs);
294:   VecDestroy(&gmres->sol_temp);
295:   PetscFree(gmres->Rsvd);
296:   PetscFree(gmres->Dsvd);
297:   PetscFree(gmres->orthogwork);

299:   gmres->sol_temp       = 0;
300:   gmres->vv_allocated   = 0;
301:   gmres->vecs_allocated = 0;
302:   gmres->sol_temp       = 0;
303:   return(0);
304: }

308: PetscErrorCode KSPDestroy_GMRES(KSP ksp)
309: {

313:   KSPReset_GMRES(ksp);
314:   PetscFree(ksp->data);
315:   /* clear composed functions */
316:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",NULL);
317:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C",NULL);
318:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetOrthogonalization_C",NULL);
319:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetRestart_C",NULL);
320:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetRestart_C",NULL);
321:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetHapTol_C",NULL);
322:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",NULL);
323:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetCGSRefinementType_C",NULL);
324:   return(0);
325: }
326: /*
327:     KSPGMRESBuildSoln - create the solution from the starting vector and the
328:     current iterates.

330:     Input parameters:
331:         nrs - work area of size it + 1.
332:         vs  - index of initial guess
333:         vdest - index of result.  Note that vs may == vdest (replace
334:                 guess with the solution).

336:      This is an internal routine that knows about the GMRES internals.
337:  */
340: static PetscErrorCode KSPGMRESBuildSoln(PetscScalar *nrs,Vec vs,Vec vdest,KSP ksp,PetscInt it)
341: {
342:   PetscScalar    tt;
344:   PetscInt       ii,k,j;
345:   KSP_GMRES      *gmres = (KSP_GMRES*)(ksp->data);

348:   /* Solve for solution vector that minimizes the residual */

350:   /* If it is < 0, no gmres steps have been performed */
351:   if (it < 0) {
352:     VecCopy(vs,vdest); /* VecCopy() is smart, exists immediately if vguess == vdest */
353:     return(0);
354:   }
355:   if (*HH(it,it) != 0.0) {
356:     nrs[it] = *GRS(it) / *HH(it,it);
357:   } else {
358:     ksp->reason = KSP_DIVERGED_BREAKDOWN;

360:     PetscInfo2(ksp,"Likely your matrix or preconditioner is singular. HH(it,it) is identically zero; it = %D GRS(it) = %g\n",it,(double)PetscAbsScalar(*GRS(it)));
361:     return(0);
362:   }
363:   for (ii=1; ii<=it; ii++) {
364:     k  = it - ii;
365:     tt = *GRS(k);
366:     for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
367:     if (*HH(k,k) == 0.0) {
368:       ksp->reason = KSP_DIVERGED_BREAKDOWN;

370:       PetscInfo1(ksp,"Likely your matrix or preconditioner is singular. HH(k,k) is identically zero; k = %D\n",k);
371:       return(0);
372:     }
373:     nrs[k] = tt / *HH(k,k);
374:   }

376:   /* Accumulate the correction to the solution of the preconditioned problem in TEMP */
377:   VecSet(VEC_TEMP,0.0);
378:   VecMAXPY(VEC_TEMP,it+1,nrs,&VEC_VV(0));

380:   KSPUnwindPreconditioner(ksp,VEC_TEMP,VEC_TEMP_MATOP);
381:   /* add solution to previous solution */
382:   if (vdest != vs) {
383:     VecCopy(vs,vdest);
384:   }
385:   VecAXPY(vdest,1.0,VEC_TEMP);
386:   return(0);
387: }
388: /*
389:    Do the scalar work for the orthogonalization.  Return new residual norm.
390:  */
393: static PetscErrorCode KSPGMRESUpdateHessenberg(KSP ksp,PetscInt it,PetscBool hapend,PetscReal *res)
394: {
395:   PetscScalar *hh,*cc,*ss,tt;
396:   PetscInt    j;
397:   KSP_GMRES   *gmres = (KSP_GMRES*)(ksp->data);

400:   hh = HH(0,it);
401:   cc = CC(0);
402:   ss = SS(0);

404:   /* Apply all the previously computed plane rotations to the new column
405:      of the Hessenberg matrix */
406:   for (j=1; j<=it; j++) {
407:     tt  = *hh;
408:     *hh = PetscConj(*cc) * tt + *ss * *(hh+1);
409:     hh++;
410:     *hh = *cc++ * *hh - (*ss++ * tt);
411:   }

413:   /*
414:     compute the new plane rotation, and apply it to:
415:      1) the right-hand-side of the Hessenberg system
416:      2) the new column of the Hessenberg matrix
417:     thus obtaining the updated value of the residual
418:   */
419:   if (!hapend) {
420:     tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh+1)) * *(hh+1));
421:     if (tt == 0.0) {
422:       ksp->reason = KSP_DIVERGED_NULL;
423:       return(0);
424:     }
425:     *cc        = *hh / tt;
426:     *ss        = *(hh+1) / tt;
427:     *GRS(it+1) = -(*ss * *GRS(it));
428:     *GRS(it)   = PetscConj(*cc) * *GRS(it);
429:     *hh        = PetscConj(*cc) * *hh + *ss * *(hh+1);
430:     *res       = PetscAbsScalar(*GRS(it+1));
431:   } else {
432:     /* happy breakdown: HH(it+1, it) = 0, therfore we don't need to apply
433:             another rotation matrix (so RH doesn't change).  The new residual is
434:             always the new sine term times the residual from last time (GRS(it)),
435:             but now the new sine rotation would be zero...so the residual should
436:             be zero...so we will multiply "zero" by the last residual.  This might
437:             not be exactly what we want to do here -could just return "zero". */

439:     *res = 0.0;
440:   }
441:   return(0);
442: }
443: /*
444:    This routine allocates more work vectors, starting from VEC_VV(it).
445:  */
448: PetscErrorCode KSPGMRESGetNewVectors(KSP ksp,PetscInt it)
449: {
450:   KSP_GMRES      *gmres = (KSP_GMRES*)ksp->data;
452:   PetscInt       nwork = gmres->nwork_alloc,k,nalloc;

455:   nalloc = PetscMin(ksp->max_it,gmres->delta_allocate);
456:   /* Adjust the number to allocate to make sure that we don't exceed the
457:     number of available slots */
458:   if (it + VEC_OFFSET + nalloc >= gmres->vecs_allocated) {
459:     nalloc = gmres->vecs_allocated - it - VEC_OFFSET;
460:   }
461:   if (!nalloc) return(0);

463:   gmres->vv_allocated += nalloc;

465:   KSPCreateVecs(ksp,nalloc,&gmres->user_work[nwork],0,NULL);
466:   PetscLogObjectParents(ksp,nalloc,gmres->user_work[nwork]);

468:   gmres->mwork_alloc[nwork] = nalloc;
469:   for (k=0; k<nalloc; k++) {
470:     gmres->vecs[it+VEC_OFFSET+k] = gmres->user_work[nwork][k];
471:   }
472:   gmres->nwork_alloc++;
473:   return(0);
474: }

478: PetscErrorCode KSPBuildSolution_GMRES(KSP ksp,Vec ptr,Vec *result)
479: {
480:   KSP_GMRES      *gmres = (KSP_GMRES*)ksp->data;

484:   if (!ptr) {
485:     if (!gmres->sol_temp) {
486:       VecDuplicate(ksp->vec_sol,&gmres->sol_temp);
487:       PetscLogObjectParent((PetscObject)ksp,(PetscObject)gmres->sol_temp);
488:     }
489:     ptr = gmres->sol_temp;
490:   }
491:   if (!gmres->nrs) {
492:     /* allocate the work area */
493:     PetscMalloc1(gmres->max_k,&gmres->nrs);
494:     PetscLogObjectMemory((PetscObject)ksp,gmres->max_k*sizeof(PetscScalar));
495:   }

497:   KSPGMRESBuildSoln(gmres->nrs,ksp->vec_sol,ptr,ksp,gmres->it);
498:   if (result) *result = ptr;
499:   return(0);
500: }

504: PetscErrorCode KSPView_GMRES(KSP ksp,PetscViewer viewer)
505: {
506:   KSP_GMRES      *gmres = (KSP_GMRES*)ksp->data;
507:   const char     *cstr;
509:   PetscBool      iascii,isstring;

512:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
513:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
514:   if (gmres->orthog == KSPGMRESClassicalGramSchmidtOrthogonalization) {
515:     switch (gmres->cgstype) {
516:     case (KSP_GMRES_CGS_REFINE_NEVER):
517:       cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with no iterative refinement";
518:       break;
519:     case (KSP_GMRES_CGS_REFINE_ALWAYS):
520:       cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with one step of iterative refinement";
521:       break;
522:     case (KSP_GMRES_CGS_REFINE_IFNEEDED):
523:       cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with one step of iterative refinement when needed";
524:       break;
525:     default:
526:       SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Unknown orthogonalization");
527:     }
528:   } else if (gmres->orthog == KSPGMRESModifiedGramSchmidtOrthogonalization) {
529:     cstr = "Modified Gram-Schmidt Orthogonalization";
530:   } else {
531:     cstr = "unknown orthogonalization";
532:   }
533:   if (iascii) {
534:     PetscViewerASCIIPrintf(viewer,"  GMRES: restart=%D, using %s\n",gmres->max_k,cstr);
535:     PetscViewerASCIIPrintf(viewer,"  GMRES: happy breakdown tolerance %g\n",(double)gmres->haptol);
536:   } else if (isstring) {
537:     PetscViewerStringSPrintf(viewer,"%s restart %D",cstr,gmres->max_k);
538:   }
539:   return(0);
540: }

544: /*@C
545:    KSPGMRESMonitorKrylov - Calls VecView() for each new direction in the GMRES accumulated Krylov space.

547:    Collective on KSP

549:    Input Parameters:
550: +  ksp - the KSP context
551: .  its - iteration number
552: .  fgnorm - 2-norm of residual (or gradient)
553: -  dummy - an collection of viewers created with KSPViewerCreate()

555:    Options Database Keys:
556: .   -ksp_gmres_kyrlov_monitor

558:    Notes: A new PETSCVIEWERDRAW is created for each Krylov vector so they can all be simultaneously viewed
559:    Level: intermediate

561: .keywords: KSP, nonlinear, vector, monitor, view, Krylov space

563: .seealso: KSPMonitorSet(), KSPMonitorDefault(), VecView(), KSPViewersCreate(), KSPViewersDestroy()
564: @*/
565: PetscErrorCode  KSPGMRESMonitorKrylov(KSP ksp,PetscInt its,PetscReal fgnorm,void *dummy)
566: {
567:   PetscViewers   viewers = (PetscViewers)dummy;
568:   KSP_GMRES      *gmres  = (KSP_GMRES*)ksp->data;
570:   Vec            x;
571:   PetscViewer    viewer;
572:   PetscBool      flg;

575:   PetscViewersGetViewer(viewers,gmres->it+1,&viewer);
576:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&flg);
577:   if (!flg) {
578:     PetscViewerSetType(viewer,PETSCVIEWERDRAW);
579:     PetscViewerDrawSetInfo(viewer,NULL,"Krylov GMRES Monitor",PETSC_DECIDE,PETSC_DECIDE,300,300);
580:   }
581:   x    = VEC_VV(gmres->it+1);
582:   VecView(x,viewer);
583:   return(0);
584: }

588: PetscErrorCode KSPSetFromOptions_GMRES(PetscOptionItems *PetscOptionsObject,KSP ksp)
589: {
591:   PetscInt       restart;
592:   PetscReal      haptol;
593:   KSP_GMRES      *gmres = (KSP_GMRES*)ksp->data;
594:   PetscBool      flg;

597:   PetscOptionsHead(PetscOptionsObject,"KSP GMRES Options");
598:   PetscOptionsInt("-ksp_gmres_restart","Number of Krylov search directions","KSPGMRESSetRestart",gmres->max_k,&restart,&flg);
599:   if (flg) { KSPGMRESSetRestart(ksp,restart); }
600:   PetscOptionsReal("-ksp_gmres_haptol","Tolerance for exact convergence (happy ending)","KSPGMRESSetHapTol",gmres->haptol,&haptol,&flg);
601:   if (flg) { KSPGMRESSetHapTol(ksp,haptol); }
602:   flg  = PETSC_FALSE;
603:   PetscOptionsBool("-ksp_gmres_preallocate","Preallocate Krylov vectors","KSPGMRESSetPreAllocateVectors",flg,&flg,NULL);
604:   if (flg) {KSPGMRESSetPreAllocateVectors(ksp);}
605:   PetscOptionsBoolGroupBegin("-ksp_gmres_classicalgramschmidt","Classical (unmodified) Gram-Schmidt (fast)","KSPGMRESSetOrthogonalization",&flg);
606:   if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESClassicalGramSchmidtOrthogonalization);}
607:   PetscOptionsBoolGroupEnd("-ksp_gmres_modifiedgramschmidt","Modified Gram-Schmidt (slow,more stable)","KSPGMRESSetOrthogonalization",&flg);
608:   if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESModifiedGramSchmidtOrthogonalization);}
609:   PetscOptionsEnum("-ksp_gmres_cgs_refinement_type","Type of iterative refinement for classical (unmodified) Gram-Schmidt","KSPGMRESSetCGSRefinementType",
610:                           KSPGMRESCGSRefinementTypes,(PetscEnum)gmres->cgstype,(PetscEnum*)&gmres->cgstype,&flg);
611:   flg  = PETSC_FALSE;
612:   PetscOptionsBool("-ksp_gmres_krylov_monitor","Plot the Krylov directions","KSPMonitorSet",flg,&flg,NULL);
613:   if (flg) {
614:     PetscViewers viewers;
615:     PetscViewersCreate(PetscObjectComm((PetscObject)ksp),&viewers);
616:     KSPMonitorSet(ksp,KSPGMRESMonitorKrylov,viewers,(PetscErrorCode (*)(void**))PetscViewersDestroy);
617:   }
618:   PetscOptionsTail();
619:   return(0);
620: }

622: extern PetscErrorCode KSPComputeExtremeSingularValues_GMRES(KSP,PetscReal*,PetscReal*);
623: extern PetscErrorCode KSPComputeEigenvalues_GMRES(KSP,PetscInt,PetscReal*,PetscReal*,PetscInt*);

627: PetscErrorCode  KSPGMRESSetHapTol_GMRES(KSP ksp,PetscReal tol)
628: {
629:   KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;

632:   if (tol < 0.0) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Tolerance must be non-negative");
633:   gmres->haptol = tol;
634:   return(0);
635: }

639: PetscErrorCode  KSPGMRESGetRestart_GMRES(KSP ksp,PetscInt *max_k)
640: {
641:   KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;

644:   *max_k = gmres->max_k;
645:   return(0);
646: }

650: PetscErrorCode  KSPGMRESSetRestart_GMRES(KSP ksp,PetscInt max_k)
651: {
652:   KSP_GMRES      *gmres = (KSP_GMRES*)ksp->data;

656:   if (max_k < 1) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Restart must be positive");
657:   if (!ksp->setupstage) {
658:     gmres->max_k = max_k;
659:   } else if (gmres->max_k != max_k) {
660:     gmres->max_k    = max_k;
661:     ksp->setupstage = KSP_SETUP_NEW;
662:     /* free the data structures, then create them again */
663:     KSPReset_GMRES(ksp);
664:   }
665:   return(0);
666: }

670: PetscErrorCode  KSPGMRESSetOrthogonalization_GMRES(KSP ksp,FCN fcn)
671: {
673:   ((KSP_GMRES*)ksp->data)->orthog = fcn;
674:   return(0);
675: }

679: PetscErrorCode  KSPGMRESGetOrthogonalization_GMRES(KSP ksp,FCN *fcn)
680: {
682:   *fcn = ((KSP_GMRES*)ksp->data)->orthog;
683:   return(0);
684: }

688: PetscErrorCode  KSPGMRESSetPreAllocateVectors_GMRES(KSP ksp)
689: {
690:   KSP_GMRES *gmres;

693:   gmres = (KSP_GMRES*)ksp->data;
694:   gmres->q_preallocate = 1;
695:   return(0);
696: }

700: PetscErrorCode  KSPGMRESSetCGSRefinementType_GMRES(KSP ksp,KSPGMRESCGSRefinementType type)
701: {
702:   KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;

705:   gmres->cgstype = type;
706:   return(0);
707: }

711: PetscErrorCode  KSPGMRESGetCGSRefinementType_GMRES(KSP ksp,KSPGMRESCGSRefinementType *type)
712: {
713:   KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;

716:   *type = gmres->cgstype;
717:   return(0);
718: }

722: /*@
723:    KSPGMRESSetCGSRefinementType - Sets the type of iterative refinement to use
724:          in the classical Gram Schmidt orthogonalization.

726:    Logically Collective on KSP

728:    Input Parameters:
729: +  ksp - the Krylov space context
730: -  type - the type of refinement

732:   Options Database:
733: .  -ksp_gmres_cgs_refinement_type <refine_never,refine_ifneeded,refine_always>

735:    Level: intermediate

737: .keywords: KSP, GMRES, iterative refinement

739: .seealso: KSPGMRESSetOrthogonalization(), KSPGMRESCGSRefinementType, KSPGMRESClassicalGramSchmidtOrthogonalization(), KSPGMRESGetCGSRefinementType(),
740:           KSPGMRESGetOrthogonalization()
741: @*/
742: PetscErrorCode  KSPGMRESSetCGSRefinementType(KSP ksp,KSPGMRESCGSRefinementType type)
743: {

749:   PetscTryMethod(ksp,"KSPGMRESSetCGSRefinementType_C",(KSP,KSPGMRESCGSRefinementType),(ksp,type));
750:   return(0);
751: }

755: /*@
756:    KSPGMRESGetCGSRefinementType - Gets the type of iterative refinement to use
757:          in the classical Gram Schmidt orthogonalization.

759:    Not Collective

761:    Input Parameter:
762: .  ksp - the Krylov space context

764:    Output Parameter:
765: .  type - the type of refinement

767:   Options Database:
768: .  -ksp_gmres_cgs_refinement_type <never,ifneeded,always>

770:    Level: intermediate

772: .keywords: KSP, GMRES, iterative refinement

774: .seealso: KSPGMRESSetOrthogonalization(), KSPGMRESCGSRefinementType, KSPGMRESClassicalGramSchmidtOrthogonalization(), KSPGMRESSetCGSRefinementType(),
775:           KSPGMRESGetOrthogonalization()
776: @*/
777: PetscErrorCode  KSPGMRESGetCGSRefinementType(KSP ksp,KSPGMRESCGSRefinementType *type)
778: {

783:   PetscUseMethod(ksp,"KSPGMRESGetCGSRefinementType_C",(KSP,KSPGMRESCGSRefinementType*),(ksp,type));
784:   return(0);
785: }


790: /*@
791:    KSPGMRESSetRestart - Sets number of iterations at which GMRES, FGMRES and LGMRES restarts.

793:    Logically Collective on KSP

795:    Input Parameters:
796: +  ksp - the Krylov space context
797: -  restart - integer restart value

799:   Options Database:
800: .  -ksp_gmres_restart <positive integer>

802:     Note: The default value is 30.

804:    Level: intermediate

806: .keywords: KSP, GMRES, restart, iterations

808: .seealso: KSPSetTolerances(), KSPGMRESSetOrthogonalization(), KSPGMRESSetPreAllocateVectors(), KSPGMRESGetRestart()
809: @*/
810: PetscErrorCode  KSPGMRESSetRestart(KSP ksp, PetscInt restart)
811: {


817:   PetscTryMethod(ksp,"KSPGMRESSetRestart_C",(KSP,PetscInt),(ksp,restart));
818:   return(0);
819: }

823: /*@
824:    KSPGMRESGetRestart - Gets number of iterations at which GMRES, FGMRES and LGMRES restarts.

826:    Not Collective

828:    Input Parameter:
829: .  ksp - the Krylov space context

831:    Output Parameter:
832: .   restart - integer restart value

834:     Note: The default value is 30.

836:    Level: intermediate

838: .keywords: KSP, GMRES, restart, iterations

840: .seealso: KSPSetTolerances(), KSPGMRESSetOrthogonalization(), KSPGMRESSetPreAllocateVectors(), KSPGMRESSetRestart()
841: @*/
842: PetscErrorCode  KSPGMRESGetRestart(KSP ksp, PetscInt *restart)
843: {

847:   PetscUseMethod(ksp,"KSPGMRESGetRestart_C",(KSP,PetscInt*),(ksp,restart));
848:   return(0);
849: }

853: /*@
854:    KSPGMRESSetHapTol - Sets tolerance for determining happy breakdown in GMRES, FGMRES and LGMRES.

856:    Logically Collective on KSP

858:    Input Parameters:
859: +  ksp - the Krylov space context
860: -  tol - the tolerance

862:   Options Database:
863: .  -ksp_gmres_haptol <positive real value>

865:    Note: Happy breakdown is the rare case in GMRES where an 'exact' solution is obtained after
866:          a certain number of iterations. If you attempt more iterations after this point unstable
867:          things can happen hence very occasionally you may need to set this value to detect this condition

869:    Level: intermediate

871: .keywords: KSP, GMRES, tolerance

873: .seealso: KSPSetTolerances()
874: @*/
875: PetscErrorCode  KSPGMRESSetHapTol(KSP ksp,PetscReal tol)
876: {

881:   PetscTryMethod((ksp),"KSPGMRESSetHapTol_C",(KSP,PetscReal),((ksp),(tol)));
882:   return(0);
883: }

885: /*MC
886:      KSPGMRES - Implements the Generalized Minimal Residual method.
887:                 (Saad and Schultz, 1986) with restart


890:    Options Database Keys:
891: +   -ksp_gmres_restart <restart> - the number of Krylov directions to orthogonalize against
892: .   -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
893: .   -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
894:                              vectors are allocated as needed)
895: .   -ksp_gmres_classicalgramschmidt - use classical (unmodified) Gram-Schmidt to orthogonalize against the Krylov space (fast) (the default)
896: .   -ksp_gmres_modifiedgramschmidt - use modified Gram-Schmidt in the orthogonalization (more stable, but slower)
897: .   -ksp_gmres_cgs_refinement_type <never,ifneeded,always> - determine if iterative refinement is used to increase the
898:                                    stability of the classical Gram-Schmidt  orthogonalization.
899: -   -ksp_gmres_krylov_monitor - plot the Krylov space generated

901:    Level: beginner

903:    Notes: Left and right preconditioning are supported, but not symmetric preconditioning.

905:    References:
906: .     1. - YOUCEF SAAD AND MARTIN H. SCHULTZ, GMRES: A GENERALIZED MINIMAL RESIDUAL ALGORITHM FOR SOLVING NONSYMMETRIC LINEAR SYSTEMS.
907:           SIAM J. ScI. STAT. COMPUT. Vo|. 7, No. 3, July 1986.

909: .seealso:  KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPFGMRES, KSPLGMRES,
910:            KSPGMRESSetRestart(), KSPGMRESSetHapTol(), KSPGMRESSetPreAllocateVectors(), KSPGMRESSetOrthogonalization(), KSPGMRESGetOrthogonalization(),
911:            KSPGMRESClassicalGramSchmidtOrthogonalization(), KSPGMRESModifiedGramSchmidtOrthogonalization(),
912:            KSPGMRESCGSRefinementType, KSPGMRESSetCGSRefinementType(), KSPGMRESGetCGSRefinementType(), KSPGMRESMonitorKrylov(), KSPSetPCSide()

914: M*/

918: PETSC_EXTERN PetscErrorCode KSPCreate_GMRES(KSP ksp)
919: {
920:   KSP_GMRES      *gmres;

924:   PetscNewLog(ksp,&gmres);
925:   ksp->data = (void*)gmres;

927:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,4);
928:   KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_RIGHT,3);
929:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_SYMMETRIC,2);

931:   ksp->ops->buildsolution                = KSPBuildSolution_GMRES;
932:   ksp->ops->setup                        = KSPSetUp_GMRES;
933:   ksp->ops->solve                        = KSPSolve_GMRES;
934:   ksp->ops->reset                        = KSPReset_GMRES;
935:   ksp->ops->destroy                      = KSPDestroy_GMRES;
936:   ksp->ops->view                         = KSPView_GMRES;
937:   ksp->ops->setfromoptions               = KSPSetFromOptions_GMRES;
938:   ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
939:   ksp->ops->computeeigenvalues           = KSPComputeEigenvalues_GMRES;
940: #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_HAVE_ESSL)
941:   ksp->ops->computeritz                  = KSPComputeRitz_GMRES;
942: #endif
943:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",KSPGMRESSetPreAllocateVectors_GMRES);
944:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C",KSPGMRESSetOrthogonalization_GMRES);
945:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetOrthogonalization_C",KSPGMRESGetOrthogonalization_GMRES);
946:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetRestart_C",KSPGMRESSetRestart_GMRES);
947:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetRestart_C",KSPGMRESGetRestart_GMRES);
948:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetHapTol_C",KSPGMRESSetHapTol_GMRES);
949:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",KSPGMRESSetCGSRefinementType_GMRES);
950:   PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetCGSRefinementType_C",KSPGMRESGetCGSRefinementType_GMRES);

952:   gmres->haptol         = 1.0e-30;
953:   gmres->q_preallocate  = 0;
954:   gmres->delta_allocate = GMRES_DELTA_DIRECTIONS;
955:   gmres->orthog         = KSPGMRESClassicalGramSchmidtOrthogonalization;
956:   gmres->nrs            = 0;
957:   gmres->sol_temp       = 0;
958:   gmres->max_k          = GMRES_DEFAULT_MAXK;
959:   gmres->Rsvd           = 0;
960:   gmres->cgstype        = KSP_GMRES_CGS_REFINE_NEVER;
961:   gmres->orthogwork     = 0;
962:   return(0);
963: }