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);
183: if (ksp->reason) break;
185: it++;
186: gmres->it = (it-1); /* For converged */
187: PetscObjectTakeAccess(ksp);
188: ksp->its++;
189: ksp->rnorm = res;
190: PetscObjectGrantAccess(ksp);
192: (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);
194: /* Catch error in happy breakdown and signal convergence and break from loop */
195: if (hapend) {
196: if (!ksp->reason) {
197: SETERRQ1(0,"You reached the happy break down, but convergence was not indicated. Residual norm = %G",res);
198: }
199: break;
200: }
201: }
203: /* Monitor if we know that we will not return for a restart */
204: if (ksp->reason || ksp->its >= ksp->max_it) {
205: KSPLogResidualHistory(ksp,res);
206: KSPMonitor(ksp,ksp->its,res);
207: }
209: if (itcount) *itcount = it;
212: /*
213: Down here we have to solve for the "best" coefficients of the Krylov
214: columns, add the solution values together, and possibly unwind the
215: preconditioning from the solution
216: */
217: /* Form the solution (or the solution so far) */
218: BuildGmresSoln(GRS(0),ksp->vec_sol,ksp->vec_sol,ksp,it-1);
220: return(0);
221: }
225: PetscErrorCode KSPSolve_GMRES(KSP ksp)
226: {
228: PetscInt its,itcount;
229: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
230: PetscTruth guess_zero = ksp->guess_zero;
233: if (ksp->calc_sings && !gmres->Rsvd) {
234: SETERRQ(PETSC_ERR_ORDER,"Must call KSPSetComputeSingularValues() before KSPSetUp() is called");
235: }
236: if (ksp->normtype != KSP_NORM_PRECONDITIONED) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Currently can use GMRES with only preconditioned residual (right preconditioning not coded)");
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: if (!ksp->reason) 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->sol_temp = 0;
287: gmres->vv_allocated = 0;
288: gmres->vecs_allocated = 0;
289: gmres->sol_temp = 0;
290: return(0);
291: }
295: PetscErrorCode KSPDestroy_GMRES(KSP ksp)
296: {
300: KSPDestroy_GMRES_Internal(ksp);
301: PetscFree(ksp->data);
302: /* clear composed functions */
303: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C","",PETSC_NULL);
304: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C","",PETSC_NULL);
305: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetRestart_C","",PETSC_NULL);
306: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetHapTol_C","",PETSC_NULL);
307: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C","",PETSC_NULL);
308: return(0);
309: }
310: /*
311: BuildGmresSoln - create the solution from the starting vector and the
312: current iterates.
314: Input parameters:
315: nrs - work area of size it + 1.
316: vs - index of initial guess
317: vdest - index of result. Note that vs may == vdest (replace
318: guess with the solution).
320: This is an internal routine that knows about the GMRES internals.
321: */
324: static PetscErrorCode BuildGmresSoln(PetscScalar* nrs,Vec vs,Vec vdest,KSP ksp,PetscInt it)
325: {
326: PetscScalar tt;
328: PetscInt ii,k,j;
329: KSP_GMRES *gmres = (KSP_GMRES *)(ksp->data);
332: /* Solve for solution vector that minimizes the residual */
334: /* If it is < 0, no gmres steps have been performed */
335: if (it < 0) {
336: if (vdest != vs) {
337: VecCopy(vs,vdest);
338: }
339: return(0);
340: }
341: 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)));
342: if (*HH(it,it) != 0.0) {
343: nrs[it] = *GRS(it) / *HH(it,it);
344: } else {
345: nrs[it] = 0.0;
346: }
347: for (ii=1; ii<=it; ii++) {
348: k = it - ii;
349: tt = *GRS(k);
350: for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
351: nrs[k] = tt / *HH(k,k);
352: }
354: /* Accumulate the correction to the solution of the preconditioned problem in TEMP */
355: VecSet(VEC_TEMP,0.0);
356: VecMAXPY(VEC_TEMP,it+1,nrs,&VEC_VV(0));
358: KSPUnwindPreconditioner(ksp,VEC_TEMP,VEC_TEMP_MATOP);
359: /* add solution to previous solution */
360: if (vdest != vs) {
361: VecCopy(vs,vdest);
362: }
363: VecAXPY(vdest,1.0,VEC_TEMP);
364: return(0);
365: }
366: /*
367: Do the scalar work for the orthogonalization. Return new residual.
368: */
371: static PetscErrorCode GMRESUpdateHessenberg(KSP ksp,PetscInt it,PetscTruth hapend,PetscReal *res)
372: {
373: PetscScalar *hh,*cc,*ss,tt;
374: PetscInt j;
375: KSP_GMRES *gmres = (KSP_GMRES *)(ksp->data);
378: hh = HH(0,it);
379: cc = CC(0);
380: ss = SS(0);
382: /* Apply all the previously computed plane rotations to the new column
383: of the Hessenberg matrix */
384: for (j=1; j<=it; j++) {
385: tt = *hh;
386: #if defined(PETSC_USE_COMPLEX)
387: *hh = PetscConj(*cc) * tt + *ss * *(hh+1);
388: #else
389: *hh = *cc * tt + *ss * *(hh+1);
390: #endif
391: hh++;
392: *hh = *cc++ * *hh - (*ss++ * tt);
393: }
395: /*
396: compute the new plane rotation, and apply it to:
397: 1) the right-hand-side of the Hessenberg system
398: 2) the new column of the Hessenberg matrix
399: thus obtaining the updated value of the residual
400: */
401: if (!hapend) {
402: #if defined(PETSC_USE_COMPLEX)
403: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh+1)) * *(hh+1));
404: #else
405: tt = PetscSqrtScalar(*hh * *hh + *(hh+1) * *(hh+1));
406: #endif
407: if (tt == 0.0) {
408: ksp->reason = KSP_DIVERGED_NULL;
409: return(0);
410: }
411: *cc = *hh / tt;
412: *ss = *(hh+1) / tt;
413: *GRS(it+1) = - (*ss * *GRS(it));
414: #if defined(PETSC_USE_COMPLEX)
415: *GRS(it) = PetscConj(*cc) * *GRS(it);
416: *hh = PetscConj(*cc) * *hh + *ss * *(hh+1);
417: #else
418: *GRS(it) = *cc * *GRS(it);
419: *hh = *cc * *hh + *ss * *(hh+1);
420: #endif
421: *res = PetscAbsScalar(*GRS(it+1));
422: } else {
423: /* happy breakdown: HH(it+1, it) = 0, therfore we don't need to apply
424: another rotation matrix (so RH doesn't change). The new residual is
425: always the new sine term times the residual from last time (GRS(it)),
426: but now the new sine rotation would be zero...so the residual should
427: be zero...so we will multiply "zero" by the last residual. This might
428: not be exactly what we want to do here -could just return "zero". */
429:
430: *res = 0.0;
431: }
432: return(0);
433: }
434: /*
435: This routine allocates more work vectors, starting from VEC_VV(it).
436: */
439: static PetscErrorCode GMRESGetNewVectors(KSP ksp,PetscInt it)
440: {
441: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
443: PetscInt nwork = gmres->nwork_alloc,k,nalloc;
446: nalloc = PetscMin(ksp->max_it,gmres->delta_allocate);
447: /* Adjust the number to allocate to make sure that we don't exceed the
448: number of available slots */
449: if (it + VEC_OFFSET + nalloc >= gmres->vecs_allocated){
450: nalloc = gmres->vecs_allocated - it - VEC_OFFSET;
451: }
452: if (!nalloc) return(0);
454: gmres->vv_allocated += nalloc;
455: KSPGetVecs(ksp,nalloc,&gmres->user_work[nwork],0,PETSC_NULL);
456: PetscLogObjectParents(ksp,nalloc,gmres->user_work[nwork]);
457: gmres->mwork_alloc[nwork] = nalloc;
458: for (k=0; k<nalloc; k++) {
459: gmres->vecs[it+VEC_OFFSET+k] = gmres->user_work[nwork][k];
460: }
461: gmres->nwork_alloc++;
462: return(0);
463: }
467: PetscErrorCode KSPBuildSolution_GMRES(KSP ksp,Vec ptr,Vec *result)
468: {
469: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
473: if (!ptr) {
474: if (!gmres->sol_temp) {
475: VecDuplicate(ksp->vec_sol,&gmres->sol_temp);
476: PetscLogObjectParent(ksp,gmres->sol_temp);
477: }
478: ptr = gmres->sol_temp;
479: }
480: if (!gmres->nrs) {
481: /* allocate the work area */
482: PetscMalloc(gmres->max_k*sizeof(PetscScalar),&gmres->nrs);
483: PetscLogObjectMemory(ksp,gmres->max_k*sizeof(PetscScalar));
484: }
486: BuildGmresSoln(gmres->nrs,ksp->vec_sol,ptr,ksp,gmres->it);
487: *result = ptr;
488: return(0);
489: }
493: PetscErrorCode KSPView_GMRES(KSP ksp,PetscViewer viewer)
494: {
495: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
496: const char *cstr;
498: PetscTruth iascii,isstring;
501: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
502: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
503: if (gmres->orthog == KSPGMRESClassicalGramSchmidtOrthogonalization) {
504: switch (gmres->cgstype) {
505: case (KSP_GMRES_CGS_REFINE_NEVER):
506: cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with no iterative refinement";
507: break;
508: case (KSP_GMRES_CGS_REFINE_ALWAYS):
509: cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with one step of iterative refinement";
510: break;
511: case (KSP_GMRES_CGS_REFINE_IFNEEDED):
512: cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with one step of iterative refinement when needed";
513: break;
514: default:
515: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown orthogonalization");
516: }
517: } else if (gmres->orthog == KSPGMRESModifiedGramSchmidtOrthogonalization) {
518: cstr = "Modified Gram-Schmidt Orthogonalization";
519: } else {
520: cstr = "unknown orthogonalization";
521: }
522: if (iascii) {
523: PetscViewerASCIIPrintf(viewer," GMRES: restart=%D, using %s\n",gmres->max_k,cstr);
524: PetscViewerASCIIPrintf(viewer," GMRES: happy breakdown tolerance %G\n",gmres->haptol);
525: } else if (isstring) {
526: PetscViewerStringSPrintf(viewer,"%s restart %D",cstr,gmres->max_k);
527: } else {
528: SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for KSP GMRES",((PetscObject)viewer)->type_name);
529: }
530: return(0);
531: }
535: /*@C
536: KSPGMRESMonitorKrylov - Calls VecView() for each direction in the
537: GMRES accumulated Krylov space.
539: Collective on KSP
541: Input Parameters:
542: + ksp - the KSP context
543: . its - iteration number
544: . fgnorm - 2-norm of residual (or gradient)
545: - a viewers object created with PetscViewersCreate()
547: Level: intermediate
549: .keywords: KSP, nonlinear, vector, monitor, view, Krylov space
551: .seealso: KSPMonitorSet(), KSPMonitorDefault(), VecView(), PetscViewersCreate(), PetscViewersDestroy()
552: @*/
553: PetscErrorCode KSPGMRESMonitorKrylov(KSP ksp,PetscInt its,PetscReal fgnorm,void *dummy)
554: {
555: PetscViewers viewers = (PetscViewers)dummy;
556: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
558: Vec x;
559: PetscViewer viewer;
562: PetscViewersGetViewer(viewers,gmres->it+1,&viewer);
563: PetscViewerSetType(viewer,PETSC_VIEWER_DRAW);
565: x = VEC_VV(gmres->it+1);
566: VecView(x,viewer);
568: return(0);
569: }
573: PetscErrorCode KSPSetFromOptions_GMRES(KSP ksp)
574: {
576: PetscInt restart;
577: PetscReal haptol;
578: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
579: PetscTruth flg;
582: PetscOptionsHead("KSP GMRES Options");
583: PetscOptionsInt("-ksp_gmres_restart","Number of Krylov search directions","KSPGMRESSetRestart",gmres->max_k,&restart,&flg);
584: if (flg) { KSPGMRESSetRestart(ksp,restart); }
585: PetscOptionsReal("-ksp_gmres_haptol","Tolerance for exact convergence (happy ending)","KSPGMRESSetHapTol",gmres->haptol,&haptol,&flg);
586: if (flg) { KSPGMRESSetHapTol(ksp,haptol); }
587: PetscOptionsName("-ksp_gmres_preallocate","Preallocate Krylov vectors","KSPGMRESSetPreAllocateVectors",&flg);
588: if (flg) {KSPGMRESSetPreAllocateVectors(ksp);}
589: PetscOptionsTruthGroupBegin("-ksp_gmres_classicalgramschmidt","Classical (unmodified) Gram-Schmidt (fast)","KSPGMRESSetOrthogonalization",&flg);
590: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESClassicalGramSchmidtOrthogonalization);}
591: PetscOptionsTruthGroupEnd("-ksp_gmres_modifiedgramschmidt","Modified Gram-Schmidt (slow,more stable)","KSPGMRESSetOrthogonalization",&flg);
592: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESModifiedGramSchmidtOrthogonalization);}
593: PetscOptionsEnum("-ksp_gmres_cgs_refinement_type","Type of iterative refinement for classical (unmodified) Gram-Schmidt","KSPGMRESSetCGSRefinementType",
594: KSPGMRESCGSRefinementTypes,(PetscEnum)gmres->cgstype,(PetscEnum*)&gmres->cgstype,&flg);
595: PetscOptionsName("-ksp_gmres_krylov_monitor","Plot the Krylov directions","KSPMonitorSet",&flg);
596: if (flg) {
597: PetscViewers viewers;
598: PetscViewersCreate(((PetscObject)ksp)->comm,&viewers);
599: KSPMonitorSet(ksp,KSPGMRESMonitorKrylov,viewers,(PetscErrorCode (*)(void*))PetscViewersDestroy);
600: }
601: PetscOptionsTail();
602: return(0);
603: }
605: EXTERN PetscErrorCode KSPComputeExtremeSingularValues_GMRES(KSP,PetscReal *,PetscReal *);
606: EXTERN PetscErrorCode KSPComputeEigenvalues_GMRES(KSP,PetscInt,PetscReal *,PetscReal *,PetscInt *);
612: PetscErrorCode KSPGMRESSetHapTol_GMRES(KSP ksp,PetscReal tol)
613: {
614: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
617: if (tol < 0.0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Tolerance must be non-negative");
618: gmres->haptol = tol;
619: return(0);
620: }
626: PetscErrorCode KSPGMRESSetRestart_GMRES(KSP ksp,PetscInt max_k)
627: {
628: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
632: if (max_k < 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Restart must be positive");
633: if (!ksp->setupcalled) {
634: gmres->max_k = max_k;
635: } else if (gmres->max_k != max_k) {
636: gmres->max_k = max_k;
637: ksp->setupcalled = 0;
638: /* free the data structures, then create them again */
639: KSPDestroy_GMRES_Internal(ksp);
640: }
641: return(0);
642: }
649: PetscErrorCode KSPGMRESSetOrthogonalization_GMRES(KSP ksp,FCN fcn)
650: {
653: ((KSP_GMRES *)ksp->data)->orthog = fcn;
654: return(0);
655: }
661: PetscErrorCode KSPGMRESSetPreAllocateVectors_GMRES(KSP ksp)
662: {
663: KSP_GMRES *gmres;
666: gmres = (KSP_GMRES *)ksp->data;
667: gmres->q_preallocate = 1;
668: return(0);
669: }
675: PetscErrorCode KSPGMRESSetCGSRefinementType_GMRES(KSP ksp,KSPGMRESCGSRefinementType type)
676: {
677: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
680: gmres->cgstype = type;
681: return(0);
682: }
687: /*@
688: KSPGMRESSetCGSRefinementType - Sets the type of iterative refinement to use
689: in the classical Gram Schmidt orthogonalization.
690: of the preconditioned problem.
692: Collective on KSP
694: Input Parameters:
695: + ksp - the Krylov space context
696: - type - the type of refinement
698: Options Database:
699: . -ksp_gmres_cgs_refinement_type <never,ifneeded,always>
701: Level: intermediate
703: .keywords: KSP, GMRES, iterative refinement
705: .seealso: KSPGMRESSetOrthogonalization(), KSPGMRESCGSRefinementType, KSPGMRESClassicalGramSchmidtOrthogonalization()
706: @*/
707: PetscErrorCode KSPGMRESSetCGSRefinementType(KSP ksp,KSPGMRESCGSRefinementType type)
708: {
709: PetscErrorCode ierr,(*f)(KSP,KSPGMRESCGSRefinementType);
713: PetscObjectQueryFunction((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",(void (**)(void))&f);
714: if (f) {
715: (*f)(ksp,type);
716: }
717: return(0);
718: }
722: /*@
723: KSPGMRESSetRestart - Sets number of iterations at which GMRES, FGMRES and LGMRES restarts.
725: Collective on KSP
727: Input Parameters:
728: + ksp - the Krylov space context
729: - restart - integer restart value
731: Options Database:
732: . -ksp_gmres_restart <positive integer>
734: Note: The default value is 30.
736: Level: intermediate
738: .keywords: KSP, GMRES, restart, iterations
740: .seealso: KSPSetTolerances(), KSPGMRESSetOrthogonalization(), KSPGMRESSetPreAllocateVectors()
741: @*/
742: PetscErrorCode KSPGMRESSetRestart(KSP ksp, PetscInt restart)
743: {
747: PetscTryMethod(ksp,"KSPGMRESSetRestart_C",(KSP,PetscInt),(ksp,restart));
748: return(0);
749: }
753: /*@
754: KSPGMRESSetHapTol - Sets tolerance for determining happy breakdown in GMRES, FGMRES and LGMRES.
756: Collective on KSP
758: Input Parameters:
759: + ksp - the Krylov space context
760: - tol - the tolerance
762: Options Database:
763: . -ksp_gmres_haptol <positive real value>
765: Note: Happy breakdown is the rare case in GMRES where an 'exact' solution is obtained after
766: a certain number of iterations. If you attempt more iterations after this point unstable
767: things can happen hence very occasionally you may need to set this value to detect this condition
769: Level: intermediate
771: .keywords: KSP, GMRES, tolerance
773: .seealso: KSPSetTolerances()
774: @*/
775: PetscErrorCode KSPGMRESSetHapTol(KSP ksp,PetscReal tol)
776: {
780: PetscTryMethod((ksp),"KSPGMRESSetHapTol_C",(KSP,PetscReal),((ksp),(tol)));
781: return(0);
782: }
784: /*MC
785: KSPGMRES - Implements the Generalized Minimal Residual method.
786: (Saad and Schultz, 1986) with restart
789: Options Database Keys:
790: + -ksp_gmres_restart <restart> - the number of Krylov directions to orthogonalize against
791: . -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
792: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
793: vectors are allocated as needed)
794: . -ksp_gmres_classicalgramschmidt - use classical (unmodified) Gram-Schmidt to orthogonalize against the Krylov space (fast) (the default)
795: . -ksp_gmres_modifiedgramschmidt - use modified Gram-Schmidt in the orthogonalization (more stable, but slower)
796: . -ksp_gmres_cgs_refinement_type <never,ifneeded,always> - determine if iterative refinement is used to increase the
797: stability of the classical Gram-Schmidt orthogonalization.
798: - -ksp_gmres_krylov_monitor - plot the Krylov space generated
800: Level: beginner
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: }