Actual source code: gmres.c
1: #define PETSCKSP_DLL
3: /*
4: This file implements GMRES (a Generalized Minimal Residual) method.
5: Reference: Saad and Schultz, 1986.
8: Some comments on left vs. right preconditioning, and restarts.
9: Left and right preconditioning.
10: If right preconditioning is chosen, then the problem being solved
11: by gmres is actually
12: My = AB^-1 y = f
13: so the initial residual is
14: r = f - Mx
15: Note that B^-1 y = x or y = B x, and if x is non-zero, the initial
16: residual is
17: r = f - A x
18: The final solution is then
19: x = B^-1 y
21: If left preconditioning is chosen, then the problem being solved is
22: My = B^-1 A x = B^-1 f,
23: and the initial residual is
24: r = B^-1(f - Ax)
26: Restarts: Restarts are basically solves with x0 not equal to zero.
27: Note that we can eliminate an extra application of B^-1 between
28: restarts as long as we don't require that the solution at the end
29: of an unsuccessful gmres iteration always be the solution x.
30: */
32: #include src/ksp/ksp/impls/gmres/gmresp.h
33: #define GMRES_DELTA_DIRECTIONS 10
34: #define GMRES_DEFAULT_MAXK 30
35: static PetscErrorCode GMRESGetNewVectors(KSP,PetscInt);
36: static PetscErrorCode GMRESUpdateHessenberg(KSP,PetscInt,PetscTruth,PetscReal*);
37: static PetscErrorCode BuildGmresSoln(PetscScalar*,Vec,Vec,KSP,PetscInt);
41: PetscErrorCode KSPSetUp_GMRES(KSP ksp)
42: {
43: PetscInt size,hh,hes,rs,cc;
45: PetscInt max_k,k;
46: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
47: Vec vec;
48: Mat pmat;
51: if (ksp->pc_side == PC_SYMMETRIC) {
52: SETERRQ(PETSC_ERR_SUP,"no symmetric preconditioning for KSPGMRES");
53: }
55: max_k = gmres->max_k; /* restart size */
56: hh = (max_k + 2) * (max_k + 1);
57: hes = (max_k + 1) * (max_k + 1);
58: rs = (max_k + 2);
59: cc = (max_k + 1);
60: size = (hh + hes + rs + 2*cc) * sizeof(PetscScalar);
62: PetscMalloc(size,&gmres->hh_origin);
63: PetscMemzero(gmres->hh_origin,size);
64: PetscLogObjectMemory(ksp,size);
65: gmres->hes_origin = gmres->hh_origin + hh;
66: gmres->rs_origin = gmres->hes_origin + hes;
67: gmres->cc_origin = gmres->rs_origin + rs;
68: gmres->ss_origin = gmres->cc_origin + cc;
70: if (ksp->calc_sings) {
71: /* Allocate workspace to hold Hessenberg matrix needed by lapack */
72: size = (max_k + 3)*(max_k + 9)*sizeof(PetscScalar);
73: PetscMalloc(size,&gmres->Rsvd);
74: PetscMalloc(5*(max_k+2)*sizeof(PetscReal),&gmres->Dsvd);
75: PetscLogObjectMemory(ksp,size+5*(max_k+2)*sizeof(PetscReal));
76: }
78: /* Allocate array to hold pointers to user vectors. Note that we need
79: 4 + max_k + 1 (since we need it+1 vectors, and it <= max_k) */
80: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void*),&gmres->vecs);
81: gmres->vecs_allocated = VEC_OFFSET + 2 + max_k;
82: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void*),&gmres->user_work);
83: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(PetscInt),&gmres->mwork_alloc);
84: PetscLogObjectMemory(ksp,(VEC_OFFSET+2+max_k)*(2*sizeof(void*)+sizeof(PetscInt)));
86: PCGetOperators(ksp->pc,0,&pmat,0);
87: if (!pmat) SETERRQ(PETSC_ERR_ORDER,"You must call KSPSetOperators() or PCSetOperators() before this call");
88: MatGetVecs(pmat,&vec,0);
89: if (gmres->q_preallocate) {
90: gmres->vv_allocated = VEC_OFFSET + 2 + max_k;
91: KSPGetVecs(ksp,gmres->vv_allocated,&gmres->user_work[0],0,PETSC_NULL);
92: PetscLogObjectParents(ksp,gmres->vv_allocated,gmres->user_work[0]);
93: gmres->mwork_alloc[0] = gmres->vv_allocated;
94: gmres->nwork_alloc = 1;
95: for (k=0; k<gmres->vv_allocated; k++) {
96: gmres->vecs[k] = gmres->user_work[0][k];
97: }
98: } else {
99: gmres->vv_allocated = 5;
100: KSPGetVecs(ksp,5,&gmres->user_work[0],0,PETSC_NULL);
101: PetscLogObjectParents(ksp,5,gmres->user_work[0]);
102: gmres->mwork_alloc[0] = 5;
103: gmres->nwork_alloc = 1;
104: for (k=0; k<gmres->vv_allocated; k++) {
105: gmres->vecs[k] = gmres->user_work[0][k];
106: }
107: }
108: VecDestroy(vec);
109: return(0);
110: }
112: /*
113: Run gmres, possibly with restart. Return residual history if requested.
114: input parameters:
116: . gmres - structure containing parameters and work areas
118: output parameters:
119: . nres - residuals (from preconditioned system) at each step.
120: If restarting, consider passing nres+it. If null,
121: ignored
122: . itcount - number of iterations used. nres[0] to nres[itcount]
123: are defined. If null, ignored.
124:
125: Notes:
126: On entry, the value in vector VEC_VV(0) should be the initial residual
127: (this allows shortcuts where the initial preconditioned residual is 0).
128: */
131: PetscErrorCode GMREScycle(PetscInt *itcount,KSP ksp)
132: {
133: KSP_GMRES *gmres = (KSP_GMRES *)(ksp->data);
134: PetscReal res_norm,res,hapbnd,tt;
136: PetscInt it = 0, max_k = gmres->max_k;
137: PetscTruth hapend = PETSC_FALSE;
140: VecNormalize(VEC_VV(0),&res_norm);
141: res = res_norm;
142: *GRS(0) = res_norm;
144: /* check for the convergence */
145: PetscObjectTakeAccess(ksp);
146: ksp->rnorm = res;
147: PetscObjectGrantAccess(ksp);
148: gmres->it = (it - 1);
149: KSPLogResidualHistory(ksp,res);
150: if (!res) {
151: if (itcount) *itcount = 0;
152: ksp->reason = KSP_CONVERGED_ATOL;
153: PetscInfo(ksp,"Converged due to zero residual norm on entry\n");
154: return(0);
155: }
157: (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);
158: while (!ksp->reason && it < max_k && ksp->its < ksp->max_it) {
159: KSPLogResidualHistory(ksp,res);
160: gmres->it = (it - 1);
161: KSPMonitor(ksp,ksp->its,res);
162: if (gmres->vv_allocated <= it + VEC_OFFSET + 1) {
163: GMRESGetNewVectors(ksp,it+1);
164: }
165: KSP_PCApplyBAorAB(ksp,VEC_VV(it),VEC_VV(1+it),VEC_TEMP_MATOP);
167: /* update hessenberg matrix and do Gram-Schmidt */
168: (*gmres->orthog)(ksp,it);
170: /* vv(i+1) . vv(i+1) */
171: VecNormalize(VEC_VV(it+1),&tt);
172: /* save the magnitude */
173: *HH(it+1,it) = tt;
174: *HES(it+1,it) = tt;
176: /* check for the happy breakdown */
177: hapbnd = PetscAbsScalar(tt / *GRS(it));
178: if (hapbnd > gmres->haptol) hapbnd = gmres->haptol;
179: if (tt < hapbnd) {
180: PetscInfo2(ksp,"Detected happy breakdown, current hapbnd = %G tt = %G\n",hapbnd,tt);
181: hapend = PETSC_TRUE;
182: }
183: GMRESUpdateHessenberg(ksp,it,hapend,&res);
184: if (ksp->reason) break;
186: it++;
187: gmres->it = (it-1); /* For converged */
188: PetscObjectTakeAccess(ksp);
189: ksp->its++;
190: ksp->rnorm = res;
191: PetscObjectGrantAccess(ksp);
193: (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);
195: /* Catch error in happy breakdown and signal convergence and break from loop */
196: if (hapend) {
197: if (!ksp->reason) {
198: SETERRQ1(0,"You reached the happy break down, but convergence was not indicated. Residual norm = %G",res);
199: }
200: break;
201: }
202: }
204: /* Monitor if we know that we will not return for a restart */
205: if (ksp->reason || ksp->its >= ksp->max_it) {
206: KSPLogResidualHistory(ksp,res);
207: KSPMonitor(ksp,ksp->its,res);
208: }
210: if (itcount) *itcount = it;
213: /*
214: Down here we have to solve for the "best" coefficients of the Krylov
215: columns, add the solution values together, and possibly unwind the
216: preconditioning from the solution
217: */
218: /* Form the solution (or the solution so far) */
219: BuildGmresSoln(GRS(0),ksp->vec_sol,ksp->vec_sol,ksp,it-1);
221: return(0);
222: }
226: PetscErrorCode KSPSolve_GMRES(KSP ksp)
227: {
229: PetscInt its,itcount;
230: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
231: PetscTruth guess_zero = ksp->guess_zero;
234: if (ksp->calc_sings && !gmres->Rsvd) {
235: SETERRQ(PETSC_ERR_ORDER,"Must call KSPSetComputeSingularValues() before KSPSetUp() is called");
236: }
238: PetscObjectTakeAccess(ksp);
239: ksp->its = 0;
240: PetscObjectGrantAccess(ksp);
242: itcount = 0;
243: ksp->reason = KSP_CONVERGED_ITERATING;
244: while (!ksp->reason) {
245: KSPInitialResidual(ksp,ksp->vec_sol,VEC_TEMP,VEC_TEMP_MATOP,VEC_VV(0),ksp->vec_rhs);
246: GMREScycle(&its,ksp);
247: itcount += its;
248: if (itcount >= ksp->max_it) {
249: ksp->reason = KSP_DIVERGED_ITS;
250: break;
251: }
252: ksp->guess_zero = PETSC_FALSE; /* every future call to KSPInitialResidual() will have nonzero guess */
253: }
254: ksp->guess_zero = guess_zero; /* restore if user provided nonzero initial guess */
255: return(0);
256: }
260: PetscErrorCode KSPDestroy_GMRES_Internal(KSP ksp)
261: {
262: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
264: PetscInt i;
267: /* Free the Hessenberg matrix */
268: PetscFree(gmres->hh_origin);
270: /* Free the pointer to user variables */
271: PetscFree(gmres->vecs);
273: /* free work vectors */
274: for (i=0; i<gmres->nwork_alloc; i++) {
275: VecDestroyVecs(gmres->user_work[i],gmres->mwork_alloc[i]);
276: }
277: PetscFree(gmres->user_work);
278: PetscFree(gmres->mwork_alloc);
279: PetscFree(gmres->nrs);
280: if (gmres->sol_temp) {
281: VecDestroy(gmres->sol_temp);
282: }
283: PetscFree(gmres->Rsvd);
284: PetscFree(gmres->Dsvd);
285: PetscFree(gmres->orthogwork);
286: gmres->orthogwork = 0;
287: gmres->Dsvd = 0;
288: gmres->hh_origin = 0;
289: gmres->vecs = 0;
290: gmres->user_work = 0;
291: gmres->mwork_alloc = 0;
292: gmres->nrs = 0;
293: gmres->sol_temp = 0;
294: gmres->nwork_alloc = 0;
295: gmres->vv_allocated = 0;
296: gmres->vecs_allocated = 0;
297: gmres->nrs = 0;
298: gmres->sol_temp = 0;
299: gmres->Rsvd = 0;
300: return(0);
301: }
305: PetscErrorCode KSPDestroy_GMRES(KSP ksp)
306: {
307: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
311: KSPDestroy_GMRES_Internal(ksp);
312: PetscFree(gmres);
313: return(0);
314: }
315: /*
316: BuildGmresSoln - create the solution from the starting vector and the
317: current iterates.
319: Input parameters:
320: nrs - work area of size it + 1.
321: vs - index of initial guess
322: vdest - index of result. Note that vs may == vdest (replace
323: guess with the solution).
325: This is an internal routine that knows about the GMRES internals.
326: */
329: static PetscErrorCode BuildGmresSoln(PetscScalar* nrs,Vec vs,Vec vdest,KSP ksp,PetscInt it)
330: {
331: PetscScalar tt;
333: PetscInt ii,k,j;
334: KSP_GMRES *gmres = (KSP_GMRES *)(ksp->data);
337: /* Solve for solution vector that minimizes the residual */
339: /* If it is < 0, no gmres steps have been performed */
340: if (it < 0) {
341: if (vdest != vs) {
342: VecCopy(vs,vdest);
343: }
344: return(0);
345: }
346: if (*HH(it,it) == 0.0) SETERRQ2(PETSC_ERR_CONV_FAILED,"HH(it,it) is identically zero; it = %D GRS(it) = %G",it,PetscAbsScalar(*GRS(it)));
347: if (*HH(it,it) != 0.0) {
348: nrs[it] = *GRS(it) / *HH(it,it);
349: } else {
350: nrs[it] = 0.0;
351: }
352: for (ii=1; ii<=it; ii++) {
353: k = it - ii;
354: tt = *GRS(k);
355: for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
356: nrs[k] = tt / *HH(k,k);
357: }
359: /* Accumulate the correction to the solution of the preconditioned problem in TEMP */
360: VecSet(VEC_TEMP,0.0);
361: VecMAXPY(VEC_TEMP,it+1,nrs,&VEC_VV(0));
363: KSPUnwindPreconditioner(ksp,VEC_TEMP,VEC_TEMP_MATOP);
364: /* add solution to previous solution */
365: if (vdest != vs) {
366: VecCopy(vs,vdest);
367: }
368: VecAXPY(vdest,1.0,VEC_TEMP);
369: return(0);
370: }
371: /*
372: Do the scalar work for the orthogonalization. Return new residual.
373: */
376: static PetscErrorCode GMRESUpdateHessenberg(KSP ksp,PetscInt it,PetscTruth hapend,PetscReal *res)
377: {
378: PetscScalar *hh,*cc,*ss,tt;
379: PetscInt j;
380: KSP_GMRES *gmres = (KSP_GMRES *)(ksp->data);
383: hh = HH(0,it);
384: cc = CC(0);
385: ss = SS(0);
387: /* Apply all the previously computed plane rotations to the new column
388: of the Hessenberg matrix */
389: for (j=1; j<=it; j++) {
390: tt = *hh;
391: #if defined(PETSC_USE_COMPLEX)
392: *hh = PetscConj(*cc) * tt + *ss * *(hh+1);
393: #else
394: *hh = *cc * tt + *ss * *(hh+1);
395: #endif
396: hh++;
397: *hh = *cc++ * *hh - (*ss++ * tt);
398: }
400: /*
401: compute the new plane rotation, and apply it to:
402: 1) the right-hand-side of the Hessenberg system
403: 2) the new column of the Hessenberg matrix
404: thus obtaining the updated value of the residual
405: */
406: if (!hapend) {
407: #if defined(PETSC_USE_COMPLEX)
408: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh+1)) * *(hh+1));
409: #else
410: tt = PetscSqrtScalar(*hh * *hh + *(hh+1) * *(hh+1));
411: #endif
412: if (tt == 0.0) {
413: ksp->reason = KSP_DIVERGED_NULL;
414: return(0);
415: }
416: *cc = *hh / tt;
417: *ss = *(hh+1) / tt;
418: *GRS(it+1) = - (*ss * *GRS(it));
419: #if defined(PETSC_USE_COMPLEX)
420: *GRS(it) = PetscConj(*cc) * *GRS(it);
421: *hh = PetscConj(*cc) * *hh + *ss * *(hh+1);
422: #else
423: *GRS(it) = *cc * *GRS(it);
424: *hh = *cc * *hh + *ss * *(hh+1);
425: #endif
426: *res = PetscAbsScalar(*GRS(it+1));
427: } else {
428: /* happy breakdown: HH(it+1, it) = 0, therfore we don't need to apply
429: another rotation matrix (so RH doesn't change). The new residual is
430: always the new sine term times the residual from last time (GRS(it)),
431: but now the new sine rotation would be zero...so the residual should
432: be zero...so we will multiply "zero" by the last residual. This might
433: not be exactly what we want to do here -could just return "zero". */
434:
435: *res = 0.0;
436: }
437: return(0);
438: }
439: /*
440: This routine allocates more work vectors, starting from VEC_VV(it).
441: */
444: static PetscErrorCode GMRESGetNewVectors(KSP ksp,PetscInt it)
445: {
446: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
448: PetscInt nwork = gmres->nwork_alloc,k,nalloc;
451: nalloc = PetscMin(ksp->max_it,gmres->delta_allocate);
452: /* Adjust the number to allocate to make sure that we don't exceed the
453: number of available slots */
454: if (it + VEC_OFFSET + nalloc >= gmres->vecs_allocated){
455: nalloc = gmres->vecs_allocated - it - VEC_OFFSET;
456: }
457: if (!nalloc) return(0);
459: gmres->vv_allocated += nalloc;
460: KSPGetVecs(ksp,nalloc,&gmres->user_work[nwork],0,PETSC_NULL);
461: PetscLogObjectParents(ksp,nalloc,gmres->user_work[nwork]);
462: gmres->mwork_alloc[nwork] = nalloc;
463: for (k=0; k<nalloc; k++) {
464: gmres->vecs[it+VEC_OFFSET+k] = gmres->user_work[nwork][k];
465: }
466: gmres->nwork_alloc++;
467: return(0);
468: }
472: PetscErrorCode KSPBuildSolution_GMRES(KSP ksp,Vec ptr,Vec *result)
473: {
474: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
478: if (!ptr) {
479: if (!gmres->sol_temp) {
480: VecDuplicate(ksp->vec_sol,&gmres->sol_temp);
481: PetscLogObjectParent(ksp,gmres->sol_temp);
482: }
483: ptr = gmres->sol_temp;
484: }
485: if (!gmres->nrs) {
486: /* allocate the work area */
487: PetscMalloc(gmres->max_k*sizeof(PetscScalar),&gmres->nrs);
488: PetscLogObjectMemory(ksp,gmres->max_k*sizeof(PetscScalar));
489: }
491: BuildGmresSoln(gmres->nrs,ksp->vec_sol,ptr,ksp,gmres->it);
492: *result = ptr;
493: return(0);
494: }
498: PetscErrorCode KSPView_GMRES(KSP ksp,PetscViewer viewer)
499: {
500: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
501: const char *cstr;
503: PetscTruth iascii,isstring;
506: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
507: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
508: if (gmres->orthog == KSPGMRESClassicalGramSchmidtOrthogonalization) {
509: switch (gmres->cgstype) {
510: case (KSP_GMRES_CGS_REFINE_NEVER):
511: cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with no iterative refinement";
512: break;
513: case (KSP_GMRES_CGS_REFINE_ALWAYS):
514: cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with one step of iterative refinement";
515: break;
516: case (KSP_GMRES_CGS_REFINE_IFNEEDED):
517: cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with one step of iterative refinement when needed";
518: break;
519: default:
520: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown orthogonalization");
521: }
522: } else if (gmres->orthog == KSPGMRESModifiedGramSchmidtOrthogonalization) {
523: cstr = "Modified Gram-Schmidt Orthogonalization";
524: } else {
525: cstr = "unknown orthogonalization";
526: }
527: if (iascii) {
528: PetscViewerASCIIPrintf(viewer," GMRES: restart=%D, using %s\n",gmres->max_k,cstr);
529: PetscViewerASCIIPrintf(viewer," GMRES: happy breakdown tolerance %G\n",gmres->haptol);
530: } else if (isstring) {
531: PetscViewerStringSPrintf(viewer,"%s restart %D",cstr,gmres->max_k);
532: } else {
533: SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for KSP GMRES",((PetscObject)viewer)->type_name);
534: }
535: return(0);
536: }
540: /*@C
541: KSPGMRESKrylovMonitor - Calls VecView() for each direction in the
542: GMRES accumulated Krylov space.
544: Collective on KSP
546: Input Parameters:
547: + ksp - the KSP context
548: . its - iteration number
549: . fgnorm - 2-norm of residual (or gradient)
550: - a viewers object created with PetscViewersCreate()
552: Level: intermediate
554: .keywords: KSP, nonlinear, vector, monitor, view, Krylov space
556: .seealso: KSPSetMonitor(), KSPDefaultMonitor(), VecView(), PetscViewersCreate(), PetscViewersDestroy()
557: @*/
558: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESKrylovMonitor(KSP ksp,PetscInt its,PetscReal fgnorm,void *dummy)
559: {
560: PetscViewers viewers = (PetscViewers)dummy;
561: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
563: Vec x;
564: PetscViewer viewer;
567: PetscViewersGetViewer(viewers,gmres->it+1,&viewer);
568: PetscViewerSetType(viewer,PETSC_VIEWER_DRAW);
570: x = VEC_VV(gmres->it+1);
571: VecView(x,viewer);
573: return(0);
574: }
578: PetscErrorCode KSPSetFromOptions_GMRES(KSP ksp)
579: {
581: PetscInt restart;
582: PetscReal haptol;
583: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
584: PetscTruth flg;
587: PetscOptionsHead("KSP GMRES Options");
588: PetscOptionsInt("-ksp_gmres_restart","Number of Krylov search directions","KSPGMRESSetRestart",gmres->max_k,&restart,&flg);
589: if (flg) { KSPGMRESSetRestart(ksp,restart); }
590: PetscOptionsReal("-ksp_gmres_haptol","Tolerance for exact convergence (happy ending)","KSPGMRESSetHapTol",gmres->haptol,&haptol,&flg);
591: if (flg) { KSPGMRESSetHapTol(ksp,haptol); }
592: PetscOptionsName("-ksp_gmres_preallocate","Preallocate Krylov vectors","KSPGMRESSetPreAllocateVectors",&flg);
593: if (flg) {KSPGMRESSetPreAllocateVectors(ksp);}
594: PetscOptionsTruthGroupBegin("-ksp_gmres_classicalgramschmidt","Classical (unmodified) Gram-Schmidt (fast)","KSPGMRESSetOrthogonalization",&flg);
595: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESClassicalGramSchmidtOrthogonalization);}
596: PetscOptionsTruthGroupEnd("-ksp_gmres_modifiedgramschmidt","Modified Gram-Schmidt (slow,more stable)","KSPGMRESSetOrthogonalization",&flg);
597: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESModifiedGramSchmidtOrthogonalization);}
598: PetscOptionsEnum("-ksp_gmres_cgs_refinement_type","Type of iterative refinement for classical (unmodified) Gram-Schmidt","KSPGMRESSetCGSRefinementType",
599: KSPGMRESCGSRefinementTypes,(PetscEnum)gmres->cgstype,(PetscEnum*)&gmres->cgstype,&flg);
600: PetscOptionsName("-ksp_gmres_krylov_monitor","Plot the Krylov directions","KSPSetMonitor",&flg);
601: if (flg) {
602: PetscViewers viewers;
603: PetscViewersCreate(ksp->comm,&viewers);
604: KSPSetMonitor(ksp,KSPGMRESKrylovMonitor,viewers,(PetscErrorCode (*)(void*))PetscViewersDestroy);
605: }
606: PetscOptionsTail();
607: return(0);
608: }
610: EXTERN PetscErrorCode KSPComputeExtremeSingularValues_GMRES(KSP,PetscReal *,PetscReal *);
611: EXTERN PetscErrorCode KSPComputeEigenvalues_GMRES(KSP,PetscInt,PetscReal *,PetscReal *,PetscInt *);
617: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetHapTol_GMRES(KSP ksp,PetscReal tol)
618: {
619: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
622: if (tol < 0.0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Tolerance must be non-negative");
623: gmres->haptol = tol;
624: return(0);
625: }
631: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetRestart_GMRES(KSP ksp,PetscInt max_k)
632: {
633: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
637: if (max_k < 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Restart must be positive");
638: if (!ksp->setupcalled) {
639: gmres->max_k = max_k;
640: } else if (gmres->max_k != max_k) {
641: gmres->max_k = max_k;
642: ksp->setupcalled = 0;
643: /* free the data structures, then create them again */
644: KSPDestroy_GMRES_Internal(ksp);
645: }
646: return(0);
647: }
654: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetOrthogonalization_GMRES(KSP ksp,FCN fcn)
655: {
658: ((KSP_GMRES *)ksp->data)->orthog = fcn;
659: return(0);
660: }
666: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetPreAllocateVectors_GMRES(KSP ksp)
667: {
668: KSP_GMRES *gmres;
671: gmres = (KSP_GMRES *)ksp->data;
672: gmres->q_preallocate = 1;
673: return(0);
674: }
680: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetCGSRefinementType_GMRES(KSP ksp,KSPGMRESCGSRefinementType type)
681: {
682: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
685: gmres->cgstype = type;
686: return(0);
687: }
692: /*@
693: KSPGMRESSetCGSRefinementType - Sets the type of iterative refinement to use
694: in the classical Gram Schmidt orthogonalization.
695: of the preconditioned problem.
697: Collective on KSP
699: Input Parameters:
700: + ksp - the Krylov space context
701: - type - the type of refinement
703: Options Database:
704: . -ksp_gmres_cgs_refinement_type <never,ifneeded,always>
706: Level: intermediate
708: .keywords: KSP, GMRES, iterative refinement
710: .seealso: KSPGMRESSetOrthogonalization(), KSPGMRESCGSRefinementType, KSPGMRESClassicalGramSchmidtOrthogonalization()
711: @*/
712: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetCGSRefinementType(KSP ksp,KSPGMRESCGSRefinementType type)
713: {
714: PetscErrorCode ierr,(*f)(KSP,KSPGMRESCGSRefinementType);
718: PetscObjectQueryFunction((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",(void (**)(void))&f);
719: if (f) {
720: (*f)(ksp,type);
721: }
722: return(0);
723: }
727: /*@
728: KSPGMRESSetRestart - Sets number of iterations at which GMRES, FGMRES and LGMRES restarts.
730: Collective on KSP
732: Input Parameters:
733: + ksp - the Krylov space context
734: - restart - integer restart value
736: Options Database:
737: . -ksp_gmres_restart <positive integer>
739: Note: The default value is 30.
741: Level: intermediate
743: .keywords: KSP, GMRES, restart, iterations
745: .seealso: KSPSetTolerances(), KSPGMRESSetOrthogonalization(), KSPGMRESSetPreAllocateVectors()
746: @*/
747: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetRestart(KSP ksp, PetscInt restart)
748: {
752: PetscTryMethod(ksp,KSPGMRESSetRestart_C,(KSP,PetscInt),(ksp,restart));
753: return(0);
754: }
758: /*@
759: KSPGMRESSetHapTol - Sets tolerance for determining happy breakdown in GMRES, FGMRES and LGMRES.
761: Collective on KSP
763: Input Parameters:
764: + ksp - the Krylov space context
765: - tol - the tolerance
767: Options Database:
768: . -ksp_gmres_haptol <positive real value>
770: Note: Happy breakdown is the rare case in GMRES where an 'exact' solution is obtained after
771: a certain number of iterations. If you attempt more iterations after this point unstable
772: things can happen hence very occasionally you may need to set this value to detect this condition
774: Level: intermediate
776: .keywords: KSP, GMRES, tolerance
778: .seealso: KSPSetTolerances()
779: @*/
780: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetHapTol(KSP ksp,PetscReal tol)
781: {
785: PetscTryMethod((ksp),KSPGMRESSetHapTol_C,(KSP,PetscReal),((ksp),(tol)));
786: return(0);
787: }
789: /*MC
790: KSPGMRES - Implements the Generalized Minimal Residual method.
791: (Saad and Schultz, 1986) with restart
794: Options Database Keys:
795: + -ksp_gmres_restart <restart> - the number of Krylov directions to orthogonalize against
796: . -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
797: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
798: vectors are allocated as needed)
799: . -ksp_gmres_classicalgramschmidt - use classical (unmodified) Gram-Schmidt to orthogonalize against the Krylov space (fast) (the default)
800: . -ksp_gmres_modifiedgramschmidt - use modified Gram-Schmidt in the orthogonalization (more stable, but slower)
801: . -ksp_gmres_cgs_refinement_type <never,ifneeded,always> - determine if iterative refinement is used to increase the
802: stability of the classical Gram-Schmidt orthogonalization.
803: - -ksp_gmres_krylov_monitor - plot the Krylov space generated
805: Level: beginner
808: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPFGMRES, KSPLGMRES,
809: KSPGMRESSetRestart(), KSPGMRESSetHapTol(), KSPGMRESSetPreAllocateVectors(), KSPGMRESSetOrthogonalization()
810: KSPGMRESClassicalGramSchmidtOrthogonalization(), KSPGMRESModifiedGramSchmidtOrthogonalization(),
811: KSPGMRESCGSRefinementType, KSPGMRESSetCGSRefinementType(), KSPGMRESKrylovMonitor()
813: M*/
818: PetscErrorCode PETSCKSP_DLLEXPORT KSPCreate_GMRES(KSP ksp)
819: {
820: KSP_GMRES *gmres;
824: PetscNew(KSP_GMRES,&gmres);
825: PetscLogObjectMemory(ksp,sizeof(KSP_GMRES));
826: ksp->data = (void*)gmres;
827: ksp->ops->buildsolution = KSPBuildSolution_GMRES;
829: ksp->ops->setup = KSPSetUp_GMRES;
830: ksp->ops->solve = KSPSolve_GMRES;
831: ksp->ops->destroy = KSPDestroy_GMRES;
832: ksp->ops->view = KSPView_GMRES;
833: ksp->ops->setfromoptions = KSPSetFromOptions_GMRES;
834: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
835: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
837: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",
838: "KSPGMRESSetPreAllocateVectors_GMRES",
839: KSPGMRESSetPreAllocateVectors_GMRES);
840: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C",
841: "KSPGMRESSetOrthogonalization_GMRES",
842: KSPGMRESSetOrthogonalization_GMRES);
843: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetRestart_C",
844: "KSPGMRESSetRestart_GMRES",
845: KSPGMRESSetRestart_GMRES);
846: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetHapTol_C",
847: "KSPGMRESSetHapTol_GMRES",
848: KSPGMRESSetHapTol_GMRES);
849: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",
850: "KSPGMRESSetCGSRefinementType_GMRES",
851: KSPGMRESSetCGSRefinementType_GMRES);
853: gmres->haptol = 1.0e-30;
854: gmres->q_preallocate = 0;
855: gmres->delta_allocate = GMRES_DELTA_DIRECTIONS;
856: gmres->orthog = KSPGMRESClassicalGramSchmidtOrthogonalization;
857: gmres->nrs = 0;
858: gmres->sol_temp = 0;
859: gmres->max_k = GMRES_DEFAULT_MAXK;
860: gmres->Rsvd = 0;
861: gmres->cgstype = KSP_GMRES_CGS_REFINE_NEVER;
862: gmres->orthogwork = 0;
863: return(0);
864: }