Actual source code: fgmres.c
2: /*
3: This file implements FGMRES (a Generalized Minimal Residual) method.
4: Reference: Saad, 1993.
6: Preconditioning: If the preconditioner is constant then this fgmres
7: code is equivalent to RIGHT-PRECONDITIONED GMRES.
8: FGMRES is a modification of gmres that allows the preconditioner to change
9: at each iteration.
11: Restarts: Restarts are basically solves with x0 not equal to zero.
13: Contributed by Allison Baker
15: */
17: #include <../src/ksp/ksp/impls/gmres/fgmres/fgmresimpl.h>
18: #define FGMRES_DELTA_DIRECTIONS 10
19: #define FGMRES_DEFAULT_MAXK 30
20: static PetscErrorCode KSPFGMRESGetNewVectors(KSP,PetscInt);
21: static PetscErrorCode KSPFGMRESUpdateHessenberg(KSP,PetscInt,PetscBool,PetscReal*);
22: static PetscErrorCode KSPFGMRESBuildSoln(PetscScalar*,Vec,Vec,KSP,PetscInt);
24: /*
26: KSPSetUp_FGMRES - Sets up the workspace needed by fgmres.
28: This is called once, usually automatically by KSPSolve() or KSPSetUp(),
29: but can be called directly by KSPSetUp().
31: */
32: PetscErrorCode KSPSetUp_FGMRES(KSP ksp)
33: {
34: PetscInt max_k,k;
35: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
37: max_k = fgmres->max_k;
39: KSPSetUp_GMRES(ksp);
41: PetscMalloc1(max_k+2,&fgmres->prevecs);
42: PetscMalloc1(max_k+2,&fgmres->prevecs_user_work);
43: PetscLogObjectMemory((PetscObject)ksp,(max_k+2)*(2*sizeof(void*)));
45: /* fgmres->vv_allocated includes extra work vectors, which are not used in the additional
46: block of vectors used to store the preconditioned directions, hence the -VEC_OFFSET
47: term for this first allocation of vectors holding preconditioned directions */
48: KSPCreateVecs(ksp,fgmres->vv_allocated-VEC_OFFSET,&fgmres->prevecs_user_work[0],0,NULL);
49: PetscLogObjectParents(ksp,fgmres->vv_allocated-VEC_OFFSET,fgmres->prevecs_user_work[0]);
50: for (k=0; k < fgmres->vv_allocated - VEC_OFFSET ; k++) {
51: fgmres->prevecs[k] = fgmres->prevecs_user_work[0][k];
52: }
53: return 0;
54: }
56: /*
57: KSPFGMRESResidual - This routine computes the initial residual (NOT PRECONDITIONED)
58: */
59: static PetscErrorCode KSPFGMRESResidual(KSP ksp)
60: {
61: KSP_FGMRES *fgmres = (KSP_FGMRES*)(ksp->data);
62: Mat Amat,Pmat;
64: PCGetOperators(ksp->pc,&Amat,&Pmat);
66: /* put A*x into VEC_TEMP */
67: KSP_MatMult(ksp,Amat,ksp->vec_sol,VEC_TEMP);
68: /* now put residual (-A*x + f) into vec_vv(0) */
69: VecWAXPY(VEC_VV(0),-1.0,VEC_TEMP,ksp->vec_rhs);
70: return 0;
71: }
73: /*
75: KSPFGMRESCycle - Run fgmres, possibly with restart. Return residual
76: history if requested.
78: input parameters:
79: . fgmres - structure containing parameters and work areas
81: output parameters:
82: . itcount - number of iterations used. If null, ignored.
83: . converged - 0 if not converged
85: Notes:
86: On entry, the value in vector VEC_VV(0) should be
87: the initial residual.
89: */
90: PetscErrorCode KSPFGMRESCycle(PetscInt *itcount,KSP ksp)
91: {
93: KSP_FGMRES *fgmres = (KSP_FGMRES*)(ksp->data);
94: PetscReal res_norm;
95: PetscReal hapbnd,tt;
96: PetscBool hapend = PETSC_FALSE; /* indicates happy breakdown ending */
97: PetscInt loc_it; /* local count of # of dir. in Krylov space */
98: PetscInt max_k = fgmres->max_k; /* max # of directions Krylov space */
99: Mat Amat,Pmat;
101: /* Number of pseudo iterations since last restart is the number
102: of prestart directions */
103: loc_it = 0;
105: /* note: (fgmres->it) is always set one less than (loc_it) It is used in
106: KSPBUILDSolution_FGMRES, where it is passed to KSPFGMRESBuildSoln.
107: Note that when KSPFGMRESBuildSoln is called from this function,
108: (loc_it -1) is passed, so the two are equivalent */
109: fgmres->it = (loc_it - 1);
111: /* initial residual is in VEC_VV(0) - compute its norm*/
112: VecNorm(VEC_VV(0),NORM_2,&res_norm);
113: KSPCheckNorm(ksp,res_norm);
115: /* first entry in right-hand-side of hessenberg system is just
116: the initial residual norm */
117: *RS(0) = res_norm;
119: ksp->rnorm = res_norm;
120: KSPLogResidualHistory(ksp,res_norm);
121: KSPMonitor(ksp,ksp->its,res_norm);
123: /* check for the convergence - maybe the current guess is good enough */
124: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
125: if (ksp->reason) {
126: if (itcount) *itcount = 0;
127: return 0;
128: }
130: /* scale VEC_VV (the initial residual) */
131: VecScale(VEC_VV(0),1.0/res_norm);
133: /* MAIN ITERATION LOOP BEGINNING*/
134: /* keep iterating until we have converged OR generated the max number
135: of directions OR reached the max number of iterations for the method */
136: while (!ksp->reason && loc_it < max_k && ksp->its < ksp->max_it) {
137: if (loc_it) {
138: KSPLogResidualHistory(ksp,res_norm);
139: KSPMonitor(ksp,ksp->its,res_norm);
140: }
141: fgmres->it = (loc_it - 1);
143: /* see if more space is needed for work vectors */
144: if (fgmres->vv_allocated <= loc_it + VEC_OFFSET + 1) {
145: KSPFGMRESGetNewVectors(ksp,loc_it+1);
146: /* (loc_it+1) is passed in as number of the first vector that should
147: be allocated */
148: }
150: /* CHANGE THE PRECONDITIONER? */
151: /* ModifyPC is the callback function that can be used to
152: change the PC or its attributes before its applied */
153: (*fgmres->modifypc)(ksp,ksp->its,loc_it,res_norm,fgmres->modifyctx);
155: /* apply PRECONDITIONER to direction vector and store with
156: preconditioned vectors in prevec */
157: KSP_PCApply(ksp,VEC_VV(loc_it),PREVEC(loc_it));
159: PCGetOperators(ksp->pc,&Amat,&Pmat);
160: /* Multiply preconditioned vector by operator - put in VEC_VV(loc_it+1) */
161: KSP_MatMult(ksp,Amat,PREVEC(loc_it),VEC_VV(1+loc_it));
163: /* update hessenberg matrix and do Gram-Schmidt - new direction is in
164: VEC_VV(1+loc_it)*/
165: (*fgmres->orthog)(ksp,loc_it);
167: /* new entry in hessenburg is the 2-norm of our new direction */
168: VecNorm(VEC_VV(loc_it+1),NORM_2,&tt);
169: KSPCheckNorm(ksp,tt);
171: *HH(loc_it+1,loc_it) = tt;
172: *HES(loc_it+1,loc_it) = tt;
174: /* Happy Breakdown Check */
175: hapbnd = PetscAbsScalar((tt) / *RS(loc_it));
176: /* RS(loc_it) contains the res_norm from the last iteration */
177: hapbnd = PetscMin(fgmres->haptol,hapbnd);
178: if (tt > hapbnd) {
179: /* scale new direction by its norm */
180: VecScale(VEC_VV(loc_it+1),1.0/tt);
181: } else {
182: /* This happens when the solution is exactly reached. */
183: /* So there is no new direction... */
184: VecSet(VEC_TEMP,0.0); /* set VEC_TEMP to 0 */
185: hapend = PETSC_TRUE;
186: }
187: /* note that for FGMRES we could get HES(loc_it+1, loc_it) = 0 and the
188: current solution would not be exact if HES was singular. Note that
189: HH non-singular implies that HES is no singular, and HES is guaranteed
190: to be nonsingular when PREVECS are linearly independent and A is
191: nonsingular (in GMRES, the nonsingularity of A implies the nonsingularity
192: of HES). So we should really add a check to verify that HES is nonsingular.*/
194: /* Now apply rotations to new col of hessenberg (and right side of system),
195: calculate new rotation, and get new residual norm at the same time*/
196: KSPFGMRESUpdateHessenberg(ksp,loc_it,hapend,&res_norm);
197: if (ksp->reason) break;
199: loc_it++;
200: fgmres->it = (loc_it-1); /* Add this here in case it has converged */
202: PetscObjectSAWsTakeAccess((PetscObject)ksp);
203: ksp->its++;
204: ksp->rnorm = res_norm;
205: PetscObjectSAWsGrantAccess((PetscObject)ksp);
207: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
209: /* Catch error in happy breakdown and signal convergence and break from loop */
210: if (hapend) {
211: if (!ksp->reason) {
213: else {
214: ksp->reason = KSP_DIVERGED_BREAKDOWN;
215: break;
216: }
217: }
218: }
219: }
220: /* END OF ITERATION LOOP */
221: KSPLogResidualHistory(ksp,res_norm);
223: /*
224: Monitor if we know that we will not return for a restart */
225: if (loc_it && (ksp->reason || ksp->its >= ksp->max_it)) {
226: KSPMonitor(ksp,ksp->its,res_norm);
227: }
229: if (itcount) *itcount = loc_it;
231: /*
232: Down here we have to solve for the "best" coefficients of the Krylov
233: columns, add the solution values together, and possibly unwind the
234: preconditioning from the solution
235: */
237: /* Form the solution (or the solution so far) */
238: /* Note: must pass in (loc_it-1) for iteration count so that KSPFGMRESBuildSoln
239: properly navigates */
241: KSPFGMRESBuildSoln(RS(0),ksp->vec_sol,ksp->vec_sol,ksp,loc_it-1);
242: return 0;
243: }
245: /*
246: KSPSolve_FGMRES - This routine applies the FGMRES method.
248: Input Parameter:
249: . ksp - the Krylov space object that was set to use fgmres
251: Output Parameter:
252: . outits - number of iterations used
254: */
256: PetscErrorCode KSPSolve_FGMRES(KSP ksp)
257: {
258: PetscInt cycle_its = 0; /* iterations done in a call to KSPFGMRESCycle */
259: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
260: PetscBool diagonalscale;
262: PCGetDiagonalScale(ksp->pc,&diagonalscale);
265: PetscObjectSAWsTakeAccess((PetscObject)ksp);
266: ksp->its = 0;
267: PetscObjectSAWsGrantAccess((PetscObject)ksp);
269: /* Compute the initial (NOT preconditioned) residual */
270: if (!ksp->guess_zero) {
271: KSPFGMRESResidual(ksp);
272: } else { /* guess is 0 so residual is F (which is in ksp->vec_rhs) */
273: VecCopy(ksp->vec_rhs,VEC_VV(0));
274: }
275: /* This may be true only on a subset of MPI ranks; setting it here so it will be detected by the first norm computaion in the Krylov method */
276: if (ksp->reason == KSP_DIVERGED_PC_FAILED) {
277: VecSetInf(VEC_VV(0));
278: }
280: /* now the residual is in VEC_VV(0) - which is what
281: KSPFGMRESCycle expects... */
283: KSPFGMRESCycle(&cycle_its,ksp);
284: while (!ksp->reason) {
285: KSPFGMRESResidual(ksp);
286: if (ksp->its >= ksp->max_it) break;
287: KSPFGMRESCycle(&cycle_its,ksp);
288: }
289: /* mark lack of convergence */
290: if (ksp->its >= ksp->max_it && !ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
291: return 0;
292: }
294: extern PetscErrorCode KSPReset_FGMRES(KSP);
295: /*
297: KSPDestroy_FGMRES - Frees all memory space used by the Krylov method.
299: */
300: PetscErrorCode KSPDestroy_FGMRES(KSP ksp)
301: {
302: KSPReset_FGMRES(ksp);
303: PetscObjectComposeFunction((PetscObject)ksp,"KSPFGMRESSetModifyPC_C",NULL);
304: KSPDestroy_GMRES(ksp);
305: return 0;
306: }
308: /*
309: KSPFGMRESBuildSoln - create the solution from the starting vector and the
310: current iterates.
312: Input parameters:
313: nrs - work area of size it + 1.
314: vguess - index of initial guess
315: vdest - index of result. Note that vguess may == vdest (replace
316: guess with the solution).
317: it - HH upper triangular part is a block of size (it+1) x (it+1)
319: This is an internal routine that knows about the FGMRES internals.
320: */
321: static PetscErrorCode KSPFGMRESBuildSoln(PetscScalar *nrs,Vec vguess,Vec vdest,KSP ksp,PetscInt it)
322: {
323: PetscScalar tt;
324: PetscInt ii,k,j;
325: KSP_FGMRES *fgmres = (KSP_FGMRES*)(ksp->data);
327: /* Solve for solution vector that minimizes the residual */
329: /* If it is < 0, no fgmres steps have been performed */
330: if (it < 0) {
331: VecCopy(vguess,vdest); /* VecCopy() is smart, exists immediately if vguess == vdest */
332: return 0;
333: }
335: /* so fgmres steps HAVE been performed */
337: /* solve the upper triangular system - RS is the right side and HH is
338: the upper triangular matrix - put soln in nrs */
339: if (*HH(it,it) != 0.0) {
340: nrs[it] = *RS(it) / *HH(it,it);
341: } else {
342: nrs[it] = 0.0;
343: }
344: for (ii=1; ii<=it; ii++) {
345: k = it - ii;
346: tt = *RS(k);
347: for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
348: nrs[k] = tt / *HH(k,k);
349: }
351: /* Accumulate the correction to the soln of the preconditioned prob. in
352: VEC_TEMP - note that we use the preconditioned vectors */
353: VecSet(VEC_TEMP,0.0); /* set VEC_TEMP components to 0 */
354: VecMAXPY(VEC_TEMP,it+1,nrs,&PREVEC(0));
356: /* put updated solution into vdest.*/
357: if (vdest != vguess) {
358: VecCopy(VEC_TEMP,vdest);
359: VecAXPY(vdest,1.0,vguess);
360: } else { /* replace guess with solution */
361: VecAXPY(vdest,1.0,VEC_TEMP);
362: }
363: return 0;
364: }
366: /*
368: KSPFGMRESUpdateHessenberg - Do the scalar work for the orthogonalization.
369: Return new residual.
371: input parameters:
373: . ksp - Krylov space object
374: . it - plane rotations are applied to the (it+1)th column of the
375: modified hessenberg (i.e. HH(:,it))
376: . hapend - PETSC_FALSE not happy breakdown ending.
378: output parameters:
379: . res - the new residual
381: */
382: static PetscErrorCode KSPFGMRESUpdateHessenberg(KSP ksp,PetscInt it,PetscBool hapend,PetscReal *res)
383: {
384: PetscScalar *hh,*cc,*ss,tt;
385: PetscInt j;
386: KSP_FGMRES *fgmres = (KSP_FGMRES*)(ksp->data);
388: hh = HH(0,it); /* pointer to beginning of column to update - so
389: incrementing hh "steps down" the (it+1)th col of HH*/
390: cc = CC(0); /* beginning of cosine rotations */
391: ss = SS(0); /* beginning of sine rotations */
393: /* Apply all the previously computed plane rotations to the new column
394: of the Hessenberg matrix */
395: /* Note: this uses the rotation [conj(c) s ; -s c], c= cos(theta), s= sin(theta),
396: and some refs have [c s ; -conj(s) c] (don't be confused!) */
398: for (j=1; j<=it; j++) {
399: tt = *hh;
400: *hh = PetscConj(*cc) * tt + *ss * *(hh+1);
401: hh++;
402: *hh = *cc++ * *hh - (*ss++ * tt);
403: /* hh, cc, and ss have all been incremented one by end of loop */
404: }
406: /*
407: compute the new plane rotation, and apply it to:
408: 1) the right-hand-side of the Hessenberg system (RS)
409: note: it affects RS(it) and RS(it+1)
410: 2) the new column of the Hessenberg matrix
411: note: it affects HH(it,it) which is currently pointed to
412: by hh and HH(it+1, it) (*(hh+1))
413: thus obtaining the updated value of the residual...
414: */
416: /* compute new plane rotation */
418: if (!hapend) {
419: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh+1)) * *(hh+1));
420: if (tt == 0.0) {
421: ksp->reason = KSP_DIVERGED_NULL;
422: return 0;
423: }
425: *cc = *hh / tt; /* new cosine value */
426: *ss = *(hh+1) / tt; /* new sine value */
428: /* apply to 1) and 2) */
429: *RS(it+1) = -(*ss * *RS(it));
430: *RS(it) = PetscConj(*cc) * *RS(it);
431: *hh = PetscConj(*cc) * *hh + *ss * *(hh+1);
433: /* residual is the last element (it+1) of right-hand side! */
434: *res = PetscAbsScalar(*RS(it+1));
436: } else { /* happy breakdown: HH(it+1, it) = 0, therefore we don't need to apply
437: another rotation matrix (so RH doesn't change). The new residual is
438: always the new sine term times the residual from last time (RS(it)),
439: but now the new sine rotation would be zero...so the residual should
440: be zero...so we will multiply "zero" by the last residual. This might
441: not be exactly what we want to do here -could just return "zero". */
443: *res = 0.0;
444: }
445: return 0;
446: }
448: /*
450: KSPFGMRESGetNewVectors - This routine allocates more work vectors, starting from
451: VEC_VV(it), and more preconditioned work vectors, starting
452: from PREVEC(i).
454: */
455: static PetscErrorCode KSPFGMRESGetNewVectors(KSP ksp,PetscInt it)
456: {
457: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
458: PetscInt nwork = fgmres->nwork_alloc; /* number of work vector chunks allocated */
459: PetscInt nalloc; /* number to allocate */
460: PetscInt k;
462: nalloc = fgmres->delta_allocate; /* number of vectors to allocate
463: in a single chunk */
465: /* Adjust the number to allocate to make sure that we don't exceed the
466: number of available slots (fgmres->vecs_allocated)*/
467: if (it + VEC_OFFSET + nalloc >= fgmres->vecs_allocated) {
468: nalloc = fgmres->vecs_allocated - it - VEC_OFFSET;
469: }
470: if (!nalloc) return 0;
472: fgmres->vv_allocated += nalloc; /* vv_allocated is the number of vectors allocated */
474: /* work vectors */
475: KSPCreateVecs(ksp,nalloc,&fgmres->user_work[nwork],0,NULL);
476: PetscLogObjectParents(ksp,nalloc,fgmres->user_work[nwork]);
477: for (k=0; k < nalloc; k++) {
478: fgmres->vecs[it+VEC_OFFSET+k] = fgmres->user_work[nwork][k];
479: }
480: /* specify size of chunk allocated */
481: fgmres->mwork_alloc[nwork] = nalloc;
483: /* preconditioned vectors */
484: KSPCreateVecs(ksp,nalloc,&fgmres->prevecs_user_work[nwork],0,NULL);
485: PetscLogObjectParents(ksp,nalloc,fgmres->prevecs_user_work[nwork]);
486: for (k=0; k < nalloc; k++) {
487: fgmres->prevecs[it+k] = fgmres->prevecs_user_work[nwork][k];
488: }
490: /* increment the number of work vector chunks */
491: fgmres->nwork_alloc++;
492: return 0;
493: }
495: /*
497: KSPBuildSolution_FGMRES
499: Input Parameter:
500: . ksp - the Krylov space object
501: . ptr-
503: Output Parameter:
504: . result - the solution
506: Note: this calls KSPFGMRESBuildSoln - the same function that KSPFGMRESCycle
507: calls directly.
509: */
510: PetscErrorCode KSPBuildSolution_FGMRES(KSP ksp,Vec ptr,Vec *result)
511: {
512: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
514: if (!ptr) {
515: if (!fgmres->sol_temp) {
516: VecDuplicate(ksp->vec_sol,&fgmres->sol_temp);
517: PetscLogObjectParent((PetscObject)ksp,(PetscObject)fgmres->sol_temp);
518: }
519: ptr = fgmres->sol_temp;
520: }
521: if (!fgmres->nrs) {
522: /* allocate the work area */
523: PetscMalloc1(fgmres->max_k,&fgmres->nrs);
524: PetscLogObjectMemory((PetscObject)ksp,fgmres->max_k*sizeof(PetscScalar));
525: }
527: KSPFGMRESBuildSoln(fgmres->nrs,ksp->vec_sol,ptr,ksp,fgmres->it);
528: if (result) *result = ptr;
529: return 0;
530: }
532: PetscErrorCode KSPSetFromOptions_FGMRES(PetscOptionItems *PetscOptionsObject,KSP ksp)
533: {
534: PetscBool flg;
536: KSPSetFromOptions_GMRES(PetscOptionsObject,ksp);
537: PetscOptionsHead(PetscOptionsObject,"KSP flexible GMRES Options");
538: PetscOptionsBoolGroupBegin("-ksp_fgmres_modifypcnochange","do not vary the preconditioner","KSPFGMRESSetModifyPC",&flg);
539: if (flg) KSPFGMRESSetModifyPC(ksp,KSPFGMRESModifyPCNoChange,NULL,NULL);
540: PetscOptionsBoolGroupEnd("-ksp_fgmres_modifypcksp","vary the KSP based preconditioner","KSPFGMRESSetModifyPC",&flg);
541: if (flg) KSPFGMRESSetModifyPC(ksp,KSPFGMRESModifyPCKSP,NULL,NULL);
542: PetscOptionsTail();
543: return 0;
544: }
546: typedef PetscErrorCode (*FCN1)(KSP,PetscInt,PetscInt,PetscReal,void*); /* force argument to next function to not be extern C*/
547: typedef PetscErrorCode (*FCN2)(void*);
549: static PetscErrorCode KSPFGMRESSetModifyPC_FGMRES(KSP ksp,FCN1 fcn,void *ctx,FCN2 d)
550: {
552: ((KSP_FGMRES*)ksp->data)->modifypc = fcn;
553: ((KSP_FGMRES*)ksp->data)->modifydestroy = d;
554: ((KSP_FGMRES*)ksp->data)->modifyctx = ctx;
555: return 0;
556: }
558: PetscErrorCode KSPReset_FGMRES(KSP ksp)
559: {
560: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
561: PetscInt i;
563: PetscFree (fgmres->prevecs);
564: if (fgmres->nwork_alloc>0) {
565: i=0;
566: /* In the first allocation we allocated VEC_OFFSET fewer vectors in prevecs */
567: VecDestroyVecs(fgmres->mwork_alloc[i]-VEC_OFFSET,&fgmres->prevecs_user_work[i]);
568: for (i=1; i<fgmres->nwork_alloc; i++) {
569: VecDestroyVecs(fgmres->mwork_alloc[i],&fgmres->prevecs_user_work[i]);
570: }
571: }
572: PetscFree(fgmres->prevecs_user_work);
573: if (fgmres->modifydestroy) {
574: (*fgmres->modifydestroy)(fgmres->modifyctx);
575: }
576: KSPReset_GMRES(ksp);
577: return 0;
578: }
580: PetscErrorCode KSPGMRESSetRestart_FGMRES(KSP ksp,PetscInt max_k)
581: {
582: KSP_FGMRES *gmres = (KSP_FGMRES*)ksp->data;
585: if (!ksp->setupstage) {
586: gmres->max_k = max_k;
587: } else if (gmres->max_k != max_k) {
588: gmres->max_k = max_k;
589: ksp->setupstage = KSP_SETUP_NEW;
590: /* free the data structures, then create them again */
591: KSPReset_FGMRES(ksp);
592: }
593: return 0;
594: }
596: PetscErrorCode KSPGMRESGetRestart_FGMRES(KSP ksp,PetscInt *max_k)
597: {
598: KSP_FGMRES *gmres = (KSP_FGMRES*)ksp->data;
600: *max_k = gmres->max_k;
601: return 0;
602: }
604: /*MC
605: KSPFGMRES - Implements the Flexible Generalized Minimal Residual method.
606: developed by Saad with restart
608: Options Database Keys:
609: + -ksp_gmres_restart <restart> - the number of Krylov directions to orthogonalize against
610: . -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
611: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
612: vectors are allocated as needed)
613: . -ksp_gmres_classicalgramschmidt - use classical (unmodified) Gram-Schmidt to orthogonalize against the Krylov space (fast) (the default)
614: . -ksp_gmres_modifiedgramschmidt - use modified Gram-Schmidt in the orthogonalization (more stable, but slower)
615: . -ksp_gmres_cgs_refinement_type <refine_never,refine_ifneeded,refine_always> - determine if iterative refinement is used to increase the
616: stability of the classical Gram-Schmidt orthogonalization.
617: . -ksp_gmres_krylov_monitor - plot the Krylov space generated
618: . -ksp_fgmres_modifypcnochange - do not change the preconditioner between iterations
619: - -ksp_fgmres_modifypcksp - modify the preconditioner using KSPFGMRESModifyPCKSP()
621: Level: beginner
623: Notes:
624: See KSPFGMRESSetModifyPC() for how to vary the preconditioner between iterations
625: Only right preconditioning is supported.
627: Notes:
628: The following options -ksp_type fgmres -pc_type ksp -ksp_ksp_type bcgs -ksp_view -ksp_pc_type jacobi make the preconditioner (or inner solver)
629: be bi-CG-stab with a preconditioner of Jacobi.
631: Developer Notes:
632: This object is subclassed off of KSPGMRES
634: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPGMRES, KSPLGMRES,
635: KSPGMRESSetRestart(), KSPGMRESSetHapTol(), KSPGMRESSetPreAllocateVectors(), KSPGMRESSetOrthogonalization(), KSPGMRESGetOrthogonalization(),
636: KSPGMRESClassicalGramSchmidtOrthogonalization(), KSPGMRESModifiedGramSchmidtOrthogonalization(),
637: KSPGMRESCGSRefinementType, KSPGMRESSetCGSRefinementType(), KSPGMRESGetCGSRefinementType(), KSPGMRESMonitorKrylov(), KSPFGMRESSetModifyPC(),
638: KSPFGMRESModifyPCKSP()
640: M*/
642: PETSC_EXTERN PetscErrorCode KSPCreate_FGMRES(KSP ksp)
643: {
644: KSP_FGMRES *fgmres;
646: PetscNewLog(ksp,&fgmres);
648: ksp->data = (void*)fgmres;
649: ksp->ops->buildsolution = KSPBuildSolution_FGMRES;
650: ksp->ops->setup = KSPSetUp_FGMRES;
651: ksp->ops->solve = KSPSolve_FGMRES;
652: ksp->ops->reset = KSPReset_FGMRES;
653: ksp->ops->destroy = KSPDestroy_FGMRES;
654: ksp->ops->view = KSPView_GMRES;
655: ksp->ops->setfromoptions = KSPSetFromOptions_FGMRES;
656: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
657: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
659: KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_RIGHT,3);
660: KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_RIGHT,1);
662: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",KSPGMRESSetPreAllocateVectors_GMRES);
663: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C",KSPGMRESSetOrthogonalization_GMRES);
664: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetOrthogonalization_C",KSPGMRESGetOrthogonalization_GMRES);
665: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetRestart_C",KSPGMRESSetRestart_FGMRES);
666: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetRestart_C",KSPGMRESGetRestart_FGMRES);
667: PetscObjectComposeFunction((PetscObject)ksp,"KSPFGMRESSetModifyPC_C",KSPFGMRESSetModifyPC_FGMRES);
668: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",KSPGMRESSetCGSRefinementType_GMRES);
669: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetCGSRefinementType_C",KSPGMRESGetCGSRefinementType_GMRES);
671: fgmres->haptol = 1.0e-30;
672: fgmres->q_preallocate = 0;
673: fgmres->delta_allocate = FGMRES_DELTA_DIRECTIONS;
674: fgmres->orthog = KSPGMRESClassicalGramSchmidtOrthogonalization;
675: fgmres->nrs = NULL;
676: fgmres->sol_temp = NULL;
677: fgmres->max_k = FGMRES_DEFAULT_MAXK;
678: fgmres->Rsvd = NULL;
679: fgmres->orthogwork = NULL;
680: fgmres->modifypc = KSPFGMRESModifyPCNoChange;
681: fgmres->modifyctx = NULL;
682: fgmres->modifydestroy = NULL;
683: fgmres->cgstype = KSP_GMRES_CGS_REFINE_NEVER;
684: return 0;
685: }