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;
49: if (ksp->pc_side == PC_SYMMETRIC) {
50: SETERRQ(PETSC_ERR_SUP,"no symmetric preconditioning for KSPGMRES");
51: } else if (ksp->pc_side == PC_RIGHT) {
52: SETERRQ(PETSC_ERR_SUP, "no right preconditioning for KSPGMRES use KSPFGMRES");
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: if (gmres->q_preallocate) {
87: gmres->vv_allocated = VEC_OFFSET + 2 + max_k;
88: KSPGetVecs(ksp,gmres->vv_allocated,&gmres->user_work[0],0,PETSC_NULL);
89: PetscLogObjectParents(ksp,gmres->vv_allocated,gmres->user_work[0]);
90: gmres->mwork_alloc[0] = gmres->vv_allocated;
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: } else {
96: gmres->vv_allocated = 5;
97: KSPGetVecs(ksp,5,&gmres->user_work[0],0,PETSC_NULL);
98: PetscLogObjectParents(ksp,5,gmres->user_work[0]);
99: gmres->mwork_alloc[0] = 5;
100: gmres->nwork_alloc = 1;
101: for (k=0; k<gmres->vv_allocated; k++) {
102: gmres->vecs[k] = gmres->user_work[0][k];
103: }
104: }
105: return(0);
106: }
108: /*
109: Run gmres, possibly with restart. Return residual history if requested.
110: input parameters:
112: . gmres - structure containing parameters and work areas
114: output parameters:
115: . nres - residuals (from preconditioned system) at each step.
116: If restarting, consider passing nres+it. If null,
117: ignored
118: . itcount - number of iterations used. nres[0] to nres[itcount]
119: are defined. If null, ignored.
120:
121: Notes:
122: On entry, the value in vector VEC_VV(0) should be the initial residual
123: (this allows shortcuts where the initial preconditioned residual is 0).
124: */
127: PetscErrorCode GMREScycle(PetscInt *itcount,KSP ksp)
128: {
129: KSP_GMRES *gmres = (KSP_GMRES *)(ksp->data);
130: PetscReal res_norm,res,hapbnd,tt;
132: PetscInt it = 0, max_k = gmres->max_k;
133: PetscTruth hapend = PETSC_FALSE;
136: VecNormalize(VEC_VV(0),&res_norm);
137: res = res_norm;
138: *GRS(0) = res_norm;
140: /* check for the convergence */
141: PetscObjectTakeAccess(ksp);
142: ksp->rnorm = res;
143: PetscObjectGrantAccess(ksp);
144: gmres->it = (it - 1);
145: KSPLogResidualHistory(ksp,res);
146: KSPMonitor(ksp,ksp->its,res);
147: if (!res) {
148: if (itcount) *itcount = 0;
149: ksp->reason = KSP_CONVERGED_ATOL;
150: PetscInfo(ksp,"Converged due to zero residual norm on entry\n");
151: return(0);
152: }
154: (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);
155: while (!ksp->reason && it < max_k && ksp->its < ksp->max_it) {
156: if (it) {
157: KSPLogResidualHistory(ksp,res);
158: KSPMonitor(ksp,ksp->its,res);
159: }
160: gmres->it = (it - 1);
161: if (gmres->vv_allocated <= it + VEC_OFFSET + 1) {
162: GMRESGetNewVectors(ksp,it+1);
163: }
164: KSP_PCApplyBAorAB(ksp,VEC_VV(it),VEC_VV(1+it),VEC_TEMP_MATOP);
166: /* update hessenberg matrix and do Gram-Schmidt */
167: (*gmres->orthog)(ksp,it);
169: /* vv(i+1) . vv(i+1) */
170: VecNormalize(VEC_VV(it+1),&tt);
171: /* save the magnitude */
172: *HH(it+1,it) = tt;
173: *HES(it+1,it) = tt;
175: /* check for the happy breakdown */
176: hapbnd = PetscAbsScalar(tt / *GRS(it));
177: if (hapbnd > gmres->haptol) hapbnd = gmres->haptol;
178: if (tt < hapbnd) {
179: PetscInfo2(ksp,"Detected happy breakdown, current hapbnd = %G tt = %G\n",hapbnd,tt);
180: hapend = PETSC_TRUE;
181: }
182: GMRESUpdateHessenberg(ksp,it,hapend,&res);
184: it++;
185: gmres->it = (it-1); /* For converged */
186: ksp->its++;
187: ksp->rnorm = res;
188: if (ksp->reason) break;
190: (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);
192: /* Catch error in happy breakdown and signal convergence and break from loop */
193: if (hapend) {
194: if (!ksp->reason) {
195: SETERRQ1(0,"You reached the happy break down, but convergence was not indicated. Residual norm = %G",res);
196: }
197: break;
198: }
199: }
201: /* Monitor if we know that we will not return for a restart */
202: if (it && (ksp->reason || ksp->its >= ksp->max_it)) {
203: KSPLogResidualHistory(ksp,res);
204: KSPMonitor(ksp,ksp->its,res);
205: }
207: if (itcount) *itcount = it;
210: /*
211: Down here we have to solve for the "best" coefficients of the Krylov
212: columns, add the solution values together, and possibly unwind the
213: preconditioning from the solution
214: */
215: /* Form the solution (or the solution so far) */
216: BuildGmresSoln(GRS(0),ksp->vec_sol,ksp->vec_sol,ksp,it-1);
218: return(0);
219: }
223: PetscErrorCode KSPSolve_GMRES(KSP ksp)
224: {
226: PetscInt its,itcount;
227: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
228: PetscTruth guess_zero = ksp->guess_zero;
231: if (ksp->calc_sings && !gmres->Rsvd) {
232: SETERRQ(PETSC_ERR_ORDER,"Must call KSPSetComputeSingularValues() before KSPSetUp() is called");
233: }
234: if (ksp->normtype != KSP_NORM_PRECONDITIONED) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Currently can use GMRES with only preconditioned residual (right preconditioning not coded)");
236: PetscObjectTakeAccess(ksp);
237: ksp->its = 0;
238: PetscObjectGrantAccess(ksp);
240: itcount = 0;
241: ksp->reason = KSP_CONVERGED_ITERATING;
242: while (!ksp->reason) {
243: KSPInitialResidual(ksp,ksp->vec_sol,VEC_TEMP,VEC_TEMP_MATOP,VEC_VV(0),ksp->vec_rhs);
244: GMREScycle(&its,ksp);
245: itcount += its;
246: if (itcount >= ksp->max_it) {
247: if (!ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
248: break;
249: }
250: ksp->guess_zero = PETSC_FALSE; /* every future call to KSPInitialResidual() will have nonzero guess */
251: }
252: ksp->guess_zero = guess_zero; /* restore if user provided nonzero initial guess */
253: return(0);
254: }
258: PetscErrorCode KSPDestroy_GMRES_Internal(KSP ksp)
259: {
260: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
262: PetscInt i;
265: /* Free the Hessenberg matrix */
266: PetscFree(gmres->hh_origin);
268: /* Free the pointer to user variables */
269: PetscFree(gmres->vecs);
271: /* free work vectors */
272: for (i=0; i<gmres->nwork_alloc; i++) {
273: VecDestroyVecs(gmres->user_work[i],gmres->mwork_alloc[i]);
274: }
275: PetscFree(gmres->user_work);
276: PetscFree(gmres->mwork_alloc);
277: PetscFree(gmres->nrs);
278: if (gmres->sol_temp) {
279: VecDestroy(gmres->sol_temp);
280: }
281: PetscFree(gmres->Rsvd);
282: PetscFree(gmres->Dsvd);
283: PetscFree(gmres->orthogwork);
284: gmres->sol_temp = 0;
285: gmres->vv_allocated = 0;
286: gmres->vecs_allocated = 0;
287: gmres->sol_temp = 0;
288: return(0);
289: }
293: PetscErrorCode KSPDestroy_GMRES(KSP ksp)
294: {
298: KSPDestroy_GMRES_Internal(ksp);
299: PetscFree(ksp->data);
300: /* clear composed functions */
301: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C","",PETSC_NULL);
302: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C","",PETSC_NULL);
303: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetRestart_C","",PETSC_NULL);
304: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetHapTol_C","",PETSC_NULL);
305: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C","",PETSC_NULL);
306: return(0);
307: }
308: /*
309: BuildGmresSoln - create the solution from the starting vector and the
310: current iterates.
312: Input parameters:
313: nrs - work area of size it + 1.
314: vs - index of initial guess
315: vdest - index of result. Note that vs may == vdest (replace
316: guess with the solution).
318: This is an internal routine that knows about the GMRES internals.
319: */
322: static PetscErrorCode BuildGmresSoln(PetscScalar* nrs,Vec vs,Vec vdest,KSP ksp,PetscInt it)
323: {
324: PetscScalar tt;
326: PetscInt ii,k,j;
327: KSP_GMRES *gmres = (KSP_GMRES *)(ksp->data);
330: /* Solve for solution vector that minimizes the residual */
332: /* If it is < 0, no gmres steps have been performed */
333: if (it < 0) {
334: VecCopy(vs,vdest); /* VecCopy() is smart, exists immediately if vguess == vdest */
335: return(0);
336: }
337: 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)));
338: if (*HH(it,it) != 0.0) {
339: nrs[it] = *GRS(it) / *HH(it,it);
340: } else {
341: nrs[it] = 0.0;
342: }
343: for (ii=1; ii<=it; ii++) {
344: k = it - ii;
345: tt = *GRS(k);
346: for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
347: if (*HH(k,k) == 0.0) SETERRQ2(PETSC_ERR_CONV_FAILED,"HH(k,k) is identically zero; it = %D k = %D",it,k);
348: nrs[k] = tt / *HH(k,k);
349: }
351: /* Accumulate the correction to the solution of the preconditioned problem in TEMP */
352: VecSet(VEC_TEMP,0.0);
353: VecMAXPY(VEC_TEMP,it+1,nrs,&VEC_VV(0));
355: KSPUnwindPreconditioner(ksp,VEC_TEMP,VEC_TEMP_MATOP);
356: /* add solution to previous solution */
357: if (vdest != vs) {
358: VecCopy(vs,vdest);
359: }
360: VecAXPY(vdest,1.0,VEC_TEMP);
361: return(0);
362: }
363: /*
364: Do the scalar work for the orthogonalization. Return new residual.
365: */
368: static PetscErrorCode GMRESUpdateHessenberg(KSP ksp,PetscInt it,PetscTruth hapend,PetscReal *res)
369: {
370: PetscScalar *hh,*cc,*ss,tt;
371: PetscInt j;
372: KSP_GMRES *gmres = (KSP_GMRES *)(ksp->data);
375: hh = HH(0,it);
376: cc = CC(0);
377: ss = SS(0);
379: /* Apply all the previously computed plane rotations to the new column
380: of the Hessenberg matrix */
381: for (j=1; j<=it; j++) {
382: tt = *hh;
383: #if defined(PETSC_USE_COMPLEX)
384: *hh = PetscConj(*cc) * tt + *ss * *(hh+1);
385: #else
386: *hh = *cc * tt + *ss * *(hh+1);
387: #endif
388: hh++;
389: *hh = *cc++ * *hh - (*ss++ * tt);
390: }
392: /*
393: compute the new plane rotation, and apply it to:
394: 1) the right-hand-side of the Hessenberg system
395: 2) the new column of the Hessenberg matrix
396: thus obtaining the updated value of the residual
397: */
398: if (!hapend) {
399: #if defined(PETSC_USE_COMPLEX)
400: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh+1)) * *(hh+1));
401: #else
402: tt = PetscSqrtScalar(*hh * *hh + *(hh+1) * *(hh+1));
403: #endif
404: if (tt == 0.0) {
405: ksp->reason = KSP_DIVERGED_NULL;
406: return(0);
407: }
408: *cc = *hh / tt;
409: *ss = *(hh+1) / tt;
410: *GRS(it+1) = - (*ss * *GRS(it));
411: #if defined(PETSC_USE_COMPLEX)
412: *GRS(it) = PetscConj(*cc) * *GRS(it);
413: *hh = PetscConj(*cc) * *hh + *ss * *(hh+1);
414: #else
415: *GRS(it) = *cc * *GRS(it);
416: *hh = *cc * *hh + *ss * *(hh+1);
417: #endif
418: *res = PetscAbsScalar(*GRS(it+1));
419: } else {
420: /* happy breakdown: HH(it+1, it) = 0, therfore we don't need to apply
421: another rotation matrix (so RH doesn't change). The new residual is
422: always the new sine term times the residual from last time (GRS(it)),
423: but now the new sine rotation would be zero...so the residual should
424: be zero...so we will multiply "zero" by the last residual. This might
425: not be exactly what we want to do here -could just return "zero". */
426:
427: *res = 0.0;
428: }
429: return(0);
430: }
431: /*
432: This routine allocates more work vectors, starting from VEC_VV(it).
433: */
436: static PetscErrorCode GMRESGetNewVectors(KSP ksp,PetscInt it)
437: {
438: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
440: PetscInt nwork = gmres->nwork_alloc,k,nalloc;
443: nalloc = PetscMin(ksp->max_it,gmres->delta_allocate);
444: /* Adjust the number to allocate to make sure that we don't exceed the
445: number of available slots */
446: if (it + VEC_OFFSET + nalloc >= gmres->vecs_allocated){
447: nalloc = gmres->vecs_allocated - it - VEC_OFFSET;
448: }
449: if (!nalloc) return(0);
451: gmres->vv_allocated += nalloc;
452: KSPGetVecs(ksp,nalloc,&gmres->user_work[nwork],0,PETSC_NULL);
453: PetscLogObjectParents(ksp,nalloc,gmres->user_work[nwork]);
454: gmres->mwork_alloc[nwork] = nalloc;
455: for (k=0; k<nalloc; k++) {
456: gmres->vecs[it+VEC_OFFSET+k] = gmres->user_work[nwork][k];
457: }
458: gmres->nwork_alloc++;
459: return(0);
460: }
464: PetscErrorCode KSPBuildSolution_GMRES(KSP ksp,Vec ptr,Vec *result)
465: {
466: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
470: if (!ptr) {
471: if (!gmres->sol_temp) {
472: VecDuplicate(ksp->vec_sol,&gmres->sol_temp);
473: PetscLogObjectParent(ksp,gmres->sol_temp);
474: }
475: ptr = gmres->sol_temp;
476: }
477: if (!gmres->nrs) {
478: /* allocate the work area */
479: PetscMalloc(gmres->max_k*sizeof(PetscScalar),&gmres->nrs);
480: PetscLogObjectMemory(ksp,gmres->max_k*sizeof(PetscScalar));
481: }
483: BuildGmresSoln(gmres->nrs,ksp->vec_sol,ptr,ksp,gmres->it);
484: if (result) *result = ptr;
485: return(0);
486: }
490: PetscErrorCode KSPView_GMRES(KSP ksp,PetscViewer viewer)
491: {
492: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
493: const char *cstr;
495: PetscTruth iascii,isstring;
498: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
499: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
500: if (gmres->orthog == KSPGMRESClassicalGramSchmidtOrthogonalization) {
501: switch (gmres->cgstype) {
502: case (KSP_GMRES_CGS_REFINE_NEVER):
503: cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with no iterative refinement";
504: break;
505: case (KSP_GMRES_CGS_REFINE_ALWAYS):
506: cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with one step of iterative refinement";
507: break;
508: case (KSP_GMRES_CGS_REFINE_IFNEEDED):
509: cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with one step of iterative refinement when needed";
510: break;
511: default:
512: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown orthogonalization");
513: }
514: } else if (gmres->orthog == KSPGMRESModifiedGramSchmidtOrthogonalization) {
515: cstr = "Modified Gram-Schmidt Orthogonalization";
516: } else {
517: cstr = "unknown orthogonalization";
518: }
519: if (iascii) {
520: PetscViewerASCIIPrintf(viewer," GMRES: restart=%D, using %s\n",gmres->max_k,cstr);
521: PetscViewerASCIIPrintf(viewer," GMRES: happy breakdown tolerance %G\n",gmres->haptol);
522: } else if (isstring) {
523: PetscViewerStringSPrintf(viewer,"%s restart %D",cstr,gmres->max_k);
524: } else {
525: SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for KSP GMRES",((PetscObject)viewer)->type_name);
526: }
527: return(0);
528: }
532: /*@C
533: KSPGMRESMonitorKrylov - Calls VecView() for each direction in the
534: GMRES accumulated Krylov space.
536: Collective on KSP
538: Input Parameters:
539: + ksp - the KSP context
540: . its - iteration number
541: . fgnorm - 2-norm of residual (or gradient)
542: - a viewers object created with PetscViewersCreate()
544: Level: intermediate
546: .keywords: KSP, nonlinear, vector, monitor, view, Krylov space
548: .seealso: KSPMonitorSet(), KSPMonitorDefault(), VecView(), PetscViewersCreate(), PetscViewersDestroy()
549: @*/
550: PetscErrorCode KSPGMRESMonitorKrylov(KSP ksp,PetscInt its,PetscReal fgnorm,void *dummy)
551: {
552: PetscViewers viewers = (PetscViewers)dummy;
553: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
555: Vec x;
556: PetscViewer viewer;
559: PetscViewersGetViewer(viewers,gmres->it+1,&viewer);
560: PetscViewerSetType(viewer,PETSC_VIEWER_DRAW);
562: x = VEC_VV(gmres->it+1);
563: VecView(x,viewer);
565: return(0);
566: }
570: PetscErrorCode KSPSetFromOptions_GMRES(KSP ksp)
571: {
573: PetscInt restart;
574: PetscReal haptol;
575: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
576: PetscTruth flg;
579: PetscOptionsHead("KSP GMRES Options");
580: PetscOptionsInt("-ksp_gmres_restart","Number of Krylov search directions","KSPGMRESSetRestart",gmres->max_k,&restart,&flg);
581: if (flg) { KSPGMRESSetRestart(ksp,restart); }
582: PetscOptionsReal("-ksp_gmres_haptol","Tolerance for exact convergence (happy ending)","KSPGMRESSetHapTol",gmres->haptol,&haptol,&flg);
583: if (flg) { KSPGMRESSetHapTol(ksp,haptol); }
584: PetscOptionsName("-ksp_gmres_preallocate","Preallocate Krylov vectors","KSPGMRESSetPreAllocateVectors",&flg);
585: if (flg) {KSPGMRESSetPreAllocateVectors(ksp);}
586: PetscOptionsTruthGroupBegin("-ksp_gmres_classicalgramschmidt","Classical (unmodified) Gram-Schmidt (fast)","KSPGMRESSetOrthogonalization",&flg);
587: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESClassicalGramSchmidtOrthogonalization);}
588: PetscOptionsTruthGroupEnd("-ksp_gmres_modifiedgramschmidt","Modified Gram-Schmidt (slow,more stable)","KSPGMRESSetOrthogonalization",&flg);
589: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESModifiedGramSchmidtOrthogonalization);}
590: PetscOptionsEnum("-ksp_gmres_cgs_refinement_type","Type of iterative refinement for classical (unmodified) Gram-Schmidt","KSPGMRESSetCGSRefinementType",
591: KSPGMRESCGSRefinementTypes,(PetscEnum)gmres->cgstype,(PetscEnum*)&gmres->cgstype,&flg);
592: PetscOptionsName("-ksp_gmres_krylov_monitor","Plot the Krylov directions","KSPMonitorSet",&flg);
593: if (flg) {
594: PetscViewers viewers;
595: PetscViewersCreate(((PetscObject)ksp)->comm,&viewers);
596: KSPMonitorSet(ksp,KSPGMRESMonitorKrylov,viewers,(PetscErrorCode (*)(void*))PetscViewersDestroy);
597: }
598: PetscOptionsTail();
599: return(0);
600: }
602: EXTERN PetscErrorCode KSPComputeExtremeSingularValues_GMRES(KSP,PetscReal *,PetscReal *);
603: EXTERN PetscErrorCode KSPComputeEigenvalues_GMRES(KSP,PetscInt,PetscReal *,PetscReal *,PetscInt *);
609: PetscErrorCode KSPGMRESSetHapTol_GMRES(KSP ksp,PetscReal tol)
610: {
611: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
614: if (tol < 0.0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Tolerance must be non-negative");
615: gmres->haptol = tol;
616: return(0);
617: }
623: PetscErrorCode KSPGMRESSetRestart_GMRES(KSP ksp,PetscInt max_k)
624: {
625: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
629: if (max_k < 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Restart must be positive");
630: if (!ksp->setupcalled) {
631: gmres->max_k = max_k;
632: } else if (gmres->max_k != max_k) {
633: gmres->max_k = max_k;
634: ksp->setupcalled = 0;
635: /* free the data structures, then create them again */
636: KSPDestroy_GMRES_Internal(ksp);
637: }
638: return(0);
639: }
646: PetscErrorCode KSPGMRESSetOrthogonalization_GMRES(KSP ksp,FCN fcn)
647: {
650: ((KSP_GMRES *)ksp->data)->orthog = fcn;
651: return(0);
652: }
658: PetscErrorCode KSPGMRESSetPreAllocateVectors_GMRES(KSP ksp)
659: {
660: KSP_GMRES *gmres;
663: gmres = (KSP_GMRES *)ksp->data;
664: gmres->q_preallocate = 1;
665: return(0);
666: }
672: PetscErrorCode KSPGMRESSetCGSRefinementType_GMRES(KSP ksp,KSPGMRESCGSRefinementType type)
673: {
674: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
677: gmres->cgstype = type;
678: return(0);
679: }
684: /*@
685: KSPGMRESSetCGSRefinementType - Sets the type of iterative refinement to use
686: in the classical Gram Schmidt orthogonalization.
687: of the preconditioned problem.
689: Collective on KSP
691: Input Parameters:
692: + ksp - the Krylov space context
693: - type - the type of refinement
695: Options Database:
696: . -ksp_gmres_cgs_refinement_type <never,ifneeded,always>
698: Level: intermediate
700: .keywords: KSP, GMRES, iterative refinement
702: .seealso: KSPGMRESSetOrthogonalization(), KSPGMRESCGSRefinementType, KSPGMRESClassicalGramSchmidtOrthogonalization()
703: @*/
704: PetscErrorCode KSPGMRESSetCGSRefinementType(KSP ksp,KSPGMRESCGSRefinementType type)
705: {
706: PetscErrorCode ierr,(*f)(KSP,KSPGMRESCGSRefinementType);
710: PetscObjectQueryFunction((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",(void (**)(void))&f);
711: if (f) {
712: (*f)(ksp,type);
713: }
714: return(0);
715: }
719: /*@
720: KSPGMRESSetRestart - Sets number of iterations at which GMRES, FGMRES and LGMRES restarts.
722: Collective on KSP
724: Input Parameters:
725: + ksp - the Krylov space context
726: - restart - integer restart value
728: Options Database:
729: . -ksp_gmres_restart <positive integer>
731: Note: The default value is 30.
733: Level: intermediate
735: .keywords: KSP, GMRES, restart, iterations
737: .seealso: KSPSetTolerances(), KSPGMRESSetOrthogonalization(), KSPGMRESSetPreAllocateVectors()
738: @*/
739: PetscErrorCode KSPGMRESSetRestart(KSP ksp, PetscInt restart)
740: {
744: PetscTryMethod(ksp,"KSPGMRESSetRestart_C",(KSP,PetscInt),(ksp,restart));
745: return(0);
746: }
750: /*@
751: KSPGMRESSetHapTol - Sets tolerance for determining happy breakdown in GMRES, FGMRES and LGMRES.
753: Collective on KSP
755: Input Parameters:
756: + ksp - the Krylov space context
757: - tol - the tolerance
759: Options Database:
760: . -ksp_gmres_haptol <positive real value>
762: Note: Happy breakdown is the rare case in GMRES where an 'exact' solution is obtained after
763: a certain number of iterations. If you attempt more iterations after this point unstable
764: things can happen hence very occasionally you may need to set this value to detect this condition
766: Level: intermediate
768: .keywords: KSP, GMRES, tolerance
770: .seealso: KSPSetTolerances()
771: @*/
772: PetscErrorCode KSPGMRESSetHapTol(KSP ksp,PetscReal tol)
773: {
777: PetscTryMethod((ksp),"KSPGMRESSetHapTol_C",(KSP,PetscReal),((ksp),(tol)));
778: return(0);
779: }
781: /*MC
782: KSPGMRES - Implements the Generalized Minimal Residual method.
783: (Saad and Schultz, 1986) with restart
786: Options Database Keys:
787: + -ksp_gmres_restart <restart> - the number of Krylov directions to orthogonalize against
788: . -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
789: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
790: vectors are allocated as needed)
791: . -ksp_gmres_classicalgramschmidt - use classical (unmodified) Gram-Schmidt to orthogonalize against the Krylov space (fast) (the default)
792: . -ksp_gmres_modifiedgramschmidt - use modified Gram-Schmidt in the orthogonalization (more stable, but slower)
793: . -ksp_gmres_cgs_refinement_type <never,ifneeded,always> - determine if iterative refinement is used to increase the
794: stability of the classical Gram-Schmidt orthogonalization.
795: - -ksp_gmres_krylov_monitor - plot the Krylov space generated
797: Level: beginner
799: References:
800: GMRES: A GENERALIZED MINIMAL RESIDUAL ALGORITHM FOR SOLVING NONSYMMETRIC LINEAR SYSTEMS. YOUCEF SAAD AND MARTIN H. SCHULTZ,
801: SIAM J. ScI. STAT. COMPUT. Vo|. 7, No. 3, July 1986, pp. 856--869.
803: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPFGMRES, KSPLGMRES,
804: KSPGMRESSetRestart(), KSPGMRESSetHapTol(), KSPGMRESSetPreAllocateVectors(), KSPGMRESSetOrthogonalization()
805: KSPGMRESClassicalGramSchmidtOrthogonalization(), KSPGMRESModifiedGramSchmidtOrthogonalization(),
806: KSPGMRESCGSRefinementType, KSPGMRESSetCGSRefinementType(), KSPGMRESMonitorKrylov()
808: M*/
813: PetscErrorCode KSPCreate_GMRES(KSP ksp)
814: {
815: KSP_GMRES *gmres;
819: PetscNewLog(ksp,KSP_GMRES,&gmres);
820: ksp->data = (void*)gmres;
823: ksp->normtype = KSP_NORM_PRECONDITIONED;
824: ksp->pc_side = PC_LEFT;
826: ksp->ops->buildsolution = KSPBuildSolution_GMRES;
827: ksp->ops->setup = KSPSetUp_GMRES;
828: ksp->ops->solve = KSPSolve_GMRES;
829: ksp->ops->destroy = KSPDestroy_GMRES;
830: ksp->ops->view = KSPView_GMRES;
831: ksp->ops->setfromoptions = KSPSetFromOptions_GMRES;
832: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
833: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
835: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",
836: "KSPGMRESSetPreAllocateVectors_GMRES",
837: KSPGMRESSetPreAllocateVectors_GMRES);
838: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C",
839: "KSPGMRESSetOrthogonalization_GMRES",
840: KSPGMRESSetOrthogonalization_GMRES);
841: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetRestart_C",
842: "KSPGMRESSetRestart_GMRES",
843: KSPGMRESSetRestart_GMRES);
844: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetHapTol_C",
845: "KSPGMRESSetHapTol_GMRES",
846: KSPGMRESSetHapTol_GMRES);
847: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",
848: "KSPGMRESSetCGSRefinementType_GMRES",
849: KSPGMRESSetCGSRefinementType_GMRES);
851: gmres->haptol = 1.0e-30;
852: gmres->q_preallocate = 0;
853: gmres->delta_allocate = GMRES_DELTA_DIRECTIONS;
854: gmres->orthog = KSPGMRESClassicalGramSchmidtOrthogonalization;
855: gmres->nrs = 0;
856: gmres->sol_temp = 0;
857: gmres->max_k = GMRES_DEFAULT_MAXK;
858: gmres->Rsvd = 0;
859: gmres->cgstype = KSP_GMRES_CGS_REFINE_NEVER;
860: gmres->orthogwork = 0;
861: return(0);
862: }