Actual source code: fgmres.c
1: #define PETSCKSP_DLL
3: /*
4: This file implements FGMRES (a Generalized Minimal Residual) method.
5: Reference: Saad, 1993.
7: Preconditioning: If the preconditioner is constant then this fgmres
8: code is equivalent to RIGHT-PRECONDITIONED GMRES.
9: FGMRES is a modification of gmres that allows the preconditioner to change
10: at each iteration.
12: Restarts: Restarts are basically solves with x0 not equal to zero.
13:
14: Contributed by Allison Baker
16: */
18: #include ../src/ksp/ksp/impls/gmres/fgmres/fgmresp.h
19: #define FGMRES_DELTA_DIRECTIONS 10
20: #define FGMRES_DEFAULT_MAXK 30
21: static PetscErrorCode FGMRESGetNewVectors(KSP,PetscInt);
22: static PetscErrorCode FGMRESUpdateHessenberg(KSP,PetscInt,PetscTruth,PetscReal *);
23: static PetscErrorCode BuildFgmresSoln(PetscScalar*,Vec,Vec,KSP,PetscInt);
25: EXTERN PetscErrorCode KSPView_GMRES(KSP,PetscViewer);
26: /*
28: KSPSetUp_FGMRES - Sets up the workspace needed by fgmres.
30: This is called once, usually automatically by KSPSolveQ() or KSPSetUp(),
31: but can be called directly by KSPSetUp().
33: */
36: PetscErrorCode KSPSetUp_FGMRES(KSP ksp)
37: {
38: PetscInt size,hh,hes,rs,cc;
40: PetscInt max_k,k;
41: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
44: if (ksp->pc_side == PC_SYMMETRIC) {
45: SETERRQ(PETSC_ERR_SUP,"no symmetric preconditioning for KSPFGMRES");
46: } else if (ksp->pc_side == PC_LEFT) {
47: SETERRQ(PETSC_ERR_SUP,"no left preconditioning for KSPFGMRES");
48: }
49: max_k = fgmres->max_k;
50: hh = (max_k + 2) * (max_k + 1);
51: hes = (max_k + 1) * (max_k + 1);
52: rs = (max_k + 2);
53: cc = (max_k + 1); /* SS and CC are the same size */
54: size = (hh + hes + rs + 2*cc) * sizeof(PetscScalar);
56: /* Allocate space and set pointers to beginning */
57: PetscMalloc(size,&fgmres->hh_origin);
58: PetscMemzero(fgmres->hh_origin,size);
59: PetscLogObjectMemory(ksp,size); /* HH - modified (by plane rotations) hessenburg */
60: fgmres->hes_origin = fgmres->hh_origin + hh; /* HES - unmodified hessenburg */
61: fgmres->rs_origin = fgmres->hes_origin + hes; /* RS - the right-hand-side of the
62: Hessenberg system */
63: fgmres->cc_origin = fgmres->rs_origin + rs; /* CC - cosines for rotations */
64: fgmres->ss_origin = fgmres->cc_origin + cc; /* SS - sines for rotations */
66: if (ksp->calc_sings) {
67: /* Allocate workspace to hold Hessenberg matrix needed by Eispack */
68: size = (max_k + 3)*(max_k + 9)*sizeof(PetscScalar);
69: PetscMalloc(size,&fgmres->Rsvd);
70: PetscMalloc(5*(max_k+2)*sizeof(PetscReal),&fgmres->Dsvd);
71: PetscLogObjectMemory(ksp,size+5*(max_k+2)*sizeof(PetscReal));
72: }
74: /* Allocate array to hold pointers to user vectors. Note that we need
75: 4 + max_k + 1 (since we need it+1 vectors, and it <= max_k) */
76: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void*),&fgmres->vecs);
77: fgmres->vecs_allocated = VEC_OFFSET + 2 + max_k;
78: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void*),&fgmres->user_work);
79: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(PetscInt),&fgmres->mwork_alloc);
80: PetscLogObjectMemory(ksp,(VEC_OFFSET+2+max_k)*(2*sizeof(void*)+sizeof(PetscInt)));
82: /* New for FGMRES - Allocate array to hold pointers to preconditioned
83: vectors - same sizes as user vectors above */
84: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void*),&fgmres->prevecs);
85: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void*),&fgmres->prevecs_user_work);
86: PetscLogObjectMemory(ksp,(VEC_OFFSET+2+max_k)*(2*sizeof(void*)));
89: /* if q_preallocate = 0 then only allocate one "chunck" of space (for
90: 5 vectors) - additional will then be allocated from FGMREScycle()
91: as needed. Otherwise, allocate all of the space that could be needed */
92: if (fgmres->q_preallocate) {
93: fgmres->vv_allocated = VEC_OFFSET + 2 + max_k;
94: } else {
95: fgmres->vv_allocated = 5;
96: }
98: /* space for work vectors */
99: KSPGetVecs(ksp,fgmres->vv_allocated,&fgmres->user_work[0],0,PETSC_NULL);
100: PetscLogObjectParents(ksp,fgmres->vv_allocated,fgmres->user_work[0]);
101: for (k=0; k < fgmres->vv_allocated; k++) {
102: fgmres->vecs[k] = fgmres->user_work[0][k];
103: }
105: /* space for preconditioned vectors */
106: KSPGetVecs(ksp,fgmres->vv_allocated,&fgmres->prevecs_user_work[0],0,PETSC_NULL);
107: PetscLogObjectParents(ksp,fgmres->vv_allocated,fgmres->prevecs_user_work[0]);
108: for (k=0; k < fgmres->vv_allocated; k++) {
109: fgmres->prevecs[k] = fgmres->prevecs_user_work[0][k];
110: }
112: /* specify how many work vectors have been allocated in this
113: chunck" (the first one) */
114: fgmres->mwork_alloc[0] = fgmres->vv_allocated;
115: fgmres->nwork_alloc = 1;
117: return(0);
118: }
120: /*
121: FGMRESResidual - This routine computes the initial residual (NOT PRECONDITIONED)
122: */
125: static PetscErrorCode FGMRESResidual(KSP ksp)
126: {
127: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
128: Mat Amat,Pmat;
129: MatStructure pflag;
133: PCGetOperators(ksp->pc,&Amat,&Pmat,&pflag);
135: /* put A*x into VEC_TEMP */
136: MatMult(Amat,ksp->vec_sol,VEC_TEMP);
137: /* now put residual (-A*x + f) into vec_vv(0) */
138: VecWAXPY(VEC_VV(0),-1.0,VEC_TEMP,ksp->vec_rhs);
139: return(0);
140: }
142: /*
144: FGMRESCycle - Run fgmres, possibly with restart. Return residual
145: history if requested.
147: input parameters:
148: . fgmres - structure containing parameters and work areas
150: output parameters:
151: . itcount - number of iterations used. If null, ignored.
152: . converged - 0 if not converged
154:
155: Notes:
156: On entry, the value in vector VEC_VV(0) should be
157: the initial residual.
160: */
163: PetscErrorCode FGMREScycle(PetscInt *itcount,KSP ksp)
164: {
166: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
167: PetscReal res_norm;
168: PetscReal hapbnd,tt;
169: PetscTruth hapend = PETSC_FALSE; /* indicates happy breakdown ending */
171: PetscInt loc_it; /* local count of # of dir. in Krylov space */
172: PetscInt max_k = fgmres->max_k; /* max # of directions Krylov space */
173: Mat Amat,Pmat;
174: MatStructure pflag;
178: /* Number of pseudo iterations since last restart is the number
179: of prestart directions */
180: loc_it = 0;
182: /* note: (fgmres->it) is always set one less than (loc_it) It is used in
183: KSPBUILDSolution_FGMRES, where it is passed to BuildFGmresSoln.
184: Note that when BuildFGmresSoln is called from this function,
185: (loc_it -1) is passed, so the two are equivalent */
186: fgmres->it = (loc_it - 1);
188: /* initial residual is in VEC_VV(0) - compute its norm*/
189: VecNorm(VEC_VV(0),NORM_2,&res_norm);
191: /* first entry in right-hand-side of hessenberg system is just
192: the initial residual norm */
193: *RS(0) = res_norm;
195: ksp->rnorm = res_norm;
196: KSPLogResidualHistory(ksp,res_norm);
198: /* check for the convergence - maybe the current guess is good enough */
199: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
200: if (ksp->reason) {
201: if (itcount) *itcount = 0;
202: return(0);
203: }
205: /* scale VEC_VV (the initial residual) */
206: VecScale(VEC_VV(0),1.0/res_norm);
207:
208: /* MAIN ITERATION LOOP BEGINNING*/
209: /* keep iterating until we have converged OR generated the max number
210: of directions OR reached the max number of iterations for the method */
211: while (!ksp->reason && loc_it < max_k && ksp->its < ksp->max_it) {
212: if (loc_it) KSPLogResidualHistory(ksp,res_norm);
213: fgmres->it = (loc_it - 1);
214: KSPMonitor(ksp,ksp->its,res_norm);
216: /* see if more space is needed for work vectors */
217: if (fgmres->vv_allocated <= loc_it + VEC_OFFSET + 1) {
218: FGMRESGetNewVectors(ksp,loc_it+1);
219: /* (loc_it+1) is passed in as number of the first vector that should
220: be allocated */
221: }
223: /* CHANGE THE PRECONDITIONER? */
224: /* ModifyPC is the callback function that can be used to
225: change the PC or its attributes before its applied */
226: (*fgmres->modifypc)(ksp,ksp->its,loc_it,res_norm,fgmres->modifyctx);
227:
228:
229: /* apply PRECONDITIONER to direction vector and store with
230: preconditioned vectors in prevec */
231: KSP_PCApply(ksp,VEC_VV(loc_it),PREVEC(loc_it));
232:
233: PCGetOperators(ksp->pc,&Amat,&Pmat,&pflag);
234: /* Multiply preconditioned vector by operator - put in VEC_VV(loc_it+1) */
235: MatMult(Amat,PREVEC(loc_it),VEC_VV(1+loc_it));
237:
238: /* update hessenberg matrix and do Gram-Schmidt - new direction is in
239: VEC_VV(1+loc_it)*/
240: (*fgmres->orthog)(ksp,loc_it);
242: /* new entry in hessenburg is the 2-norm of our new direction */
243: VecNorm(VEC_VV(loc_it+1),NORM_2,&tt);
244: *HH(loc_it+1,loc_it) = tt;
245: *HES(loc_it+1,loc_it) = tt;
247: /* Happy Breakdown Check */
248: hapbnd = PetscAbsScalar((tt) / *RS(loc_it));
249: /* RS(loc_it) contains the res_norm from the last iteration */
250: hapbnd = PetscMin(fgmres->haptol,hapbnd);
251: if (tt > hapbnd) {
252: /* scale new direction by its norm */
253: VecScale(VEC_VV(loc_it+1),1.0/tt);
254: } else {
255: /* This happens when the solution is exactly reached. */
256: /* So there is no new direction... */
257: VecSet(VEC_TEMP,0.0); /* set VEC_TEMP to 0 */
258: hapend = PETSC_TRUE;
259: }
260: /* note that for FGMRES we could get HES(loc_it+1, loc_it) = 0 and the
261: current solution would not be exact if HES was singular. Note that
262: HH non-singular implies that HES is no singular, and HES is guaranteed
263: to be nonsingular when PREVECS are linearly independent and A is
264: nonsingular (in GMRES, the nonsingularity of A implies the nonsingularity
265: of HES). So we should really add a check to verify that HES is nonsingular.*/
267:
268: /* Now apply rotations to new col of hessenberg (and right side of system),
269: calculate new rotation, and get new residual norm at the same time*/
270: FGMRESUpdateHessenberg(ksp,loc_it,hapend,&res_norm);
271: if (ksp->reason) break;
273: loc_it++;
274: fgmres->it = (loc_it-1); /* Add this here in case it has converged */
275:
276: PetscObjectTakeAccess(ksp);
277: ksp->its++;
278: ksp->rnorm = res_norm;
279: PetscObjectGrantAccess(ksp);
281: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
283: /* Catch error in happy breakdown and signal convergence and break from loop */
284: if (hapend) {
285: if (!ksp->reason) {
286: SETERRQ(0,"You reached the happy break down,but convergence was not indicated.");
287: }
288: break;
289: }
290: }
291: /* END OF ITERATION LOOP */
293: KSPLogResidualHistory(ksp,res_norm);
295: /*
296: Monitor if we know that we will not return for a restart */
297: if (ksp->reason || ksp->its >= ksp->max_it) {
298: KSPMonitor(ksp,ksp->its,res_norm);
299: }
301: if (itcount) *itcount = loc_it;
303: /*
304: Down here we have to solve for the "best" coefficients of the Krylov
305: columns, add the solution values together, and possibly unwind the
306: preconditioning from the solution
307: */
308:
309: /* Form the solution (or the solution so far) */
310: /* Note: must pass in (loc_it-1) for iteration count so that BuildFgmresSoln
311: properly navigates */
313: BuildFgmresSoln(RS(0),ksp->vec_sol,ksp->vec_sol,ksp,loc_it-1);
315: return(0);
316: }
318: /*
319: KSPSolve_FGMRES - This routine applies the FGMRES method.
322: Input Parameter:
323: . ksp - the Krylov space object that was set to use fgmres
325: Output Parameter:
326: . outits - number of iterations used
328: */
332: PetscErrorCode KSPSolve_FGMRES(KSP ksp)
333: {
335: PetscInt cycle_its = 0; /* iterations done in a call to FGMREScycle */
336: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
337: PetscTruth diagonalscale;
340: PCDiagonalScale(ksp->pc,&diagonalscale);
341: if (diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",((PetscObject)ksp)->type_name);
342: if (ksp->normtype != KSP_NORM_UNPRECONDITIONED) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Can only use FGMRES with unpreconditioned residual (it is coded with right preconditioning)");
344: PetscObjectTakeAccess(ksp);
345: ksp->its = 0;
346: PetscObjectGrantAccess(ksp);
348: /* Compute the initial (NOT preconditioned) residual */
349: if (!ksp->guess_zero) {
350: FGMRESResidual(ksp);
351: } else { /* guess is 0 so residual is F (which is in ksp->vec_rhs) */
352: VecCopy(ksp->vec_rhs,VEC_VV(0));
353: }
354: /* now the residual is in VEC_VV(0) - which is what
355: FGMREScycle expects... */
356:
357: FGMREScycle(&cycle_its,ksp);
358: while (!ksp->reason) {
359: FGMRESResidual(ksp);
360: if (ksp->its >= ksp->max_it) break;
361: FGMREScycle(&cycle_its,ksp);
362: }
363: /* mark lack of convergence */
364: if (ksp->its >= ksp->max_it && !ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
366: return(0);
367: }
369: /*
371: KSPDestroy_FGMRES - Frees all memory space used by the Krylov method.
373: */
376: PetscErrorCode KSPDestroy_FGMRES(KSP ksp)
377: {
378: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
380: PetscInt i;
383: /* Free the Hessenberg matrices */
384: PetscFree(fgmres->hh_origin);
386: /* Free pointers to user variables */
387: PetscFree(fgmres->vecs);
388: PetscFree (fgmres->prevecs);
390: /* free work vectors */
391: for (i=0; i < fgmres->nwork_alloc; i++) {
392: VecDestroyVecs(fgmres->user_work[i],fgmres->mwork_alloc[i]);
393: }
394: PetscFree(fgmres->user_work);
396: for (i=0; i < fgmres->nwork_alloc; i++) {
397: VecDestroyVecs(fgmres->prevecs_user_work[i],fgmres->mwork_alloc[i]);
398: }
399: PetscFree(fgmres->prevecs_user_work);
401: PetscFree(fgmres->mwork_alloc);
402: PetscFree(fgmres->nrs);
403: if (fgmres->sol_temp) {VecDestroy(fgmres->sol_temp);}
404: PetscFree(fgmres->Rsvd);
405: PetscFree(fgmres->Dsvd);
406: PetscFree(fgmres->orthogwork);
407: if (fgmres->modifydestroy) {
408: (*fgmres->modifydestroy)(fgmres->modifyctx);
409: }
410: PetscFree(ksp->data);
412: /* clear composed functions */
413: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C","",PETSC_NULL);
414: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C","",PETSC_NULL);
415: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetRestart_C","",PETSC_NULL);
416: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPFGMRESSetModifyPC_C","",PETSC_NULL);
417: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C","",PETSC_NULL);
418: return(0);
419: }
421: /*
422: BuildFgmresSoln - create the solution from the starting vector and the
423: current iterates.
425: Input parameters:
426: nrs - work area of size it + 1.
427: vguess - index of initial guess
428: vdest - index of result. Note that vguess may == vdest (replace
429: guess with the solution).
430: it - HH upper triangular part is a block of size (it+1) x (it+1)
432: This is an internal routine that knows about the FGMRES internals.
433: */
436: static PetscErrorCode BuildFgmresSoln(PetscScalar* nrs,Vec vguess,Vec vdest,KSP ksp,PetscInt it)
437: {
438: PetscScalar tt;
440: PetscInt ii,k,j;
441: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
444: /* Solve for solution vector that minimizes the residual */
446: /* If it is < 0, no fgmres steps have been performed */
447: if (it < 0) {
448: VecCopy(vguess,vdest); /* VecCopy() is smart, exists immediately if vguess == vdest */
449: return(0);
450: }
452: /* so fgmres steps HAVE been performed */
454: /* solve the upper triangular system - RS is the right side and HH is
455: the upper triangular matrix - put soln in nrs */
456: if (*HH(it,it) != 0.0) {
457: nrs[it] = *RS(it) / *HH(it,it);
458: } else {
459: nrs[it] = 0.0;
460: }
461: for (ii=1; ii<=it; ii++) {
462: k = it - ii;
463: tt = *RS(k);
464: for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
465: nrs[k] = tt / *HH(k,k);
466: }
468: /* Accumulate the correction to the soln of the preconditioned prob. in
469: VEC_TEMP - note that we use the preconditioned vectors */
470: VecSet(VEC_TEMP,0.0); /* set VEC_TEMP components to 0 */
471: VecMAXPY(VEC_TEMP,it+1,nrs,&PREVEC(0));
473: /* put updated solution into vdest.*/
474: if (vdest != vguess) {
475: VecCopy(VEC_TEMP,vdest);
476: VecAXPY(vdest,1.0,vguess);
477: } else {/* replace guess with solution */
478: VecAXPY(vdest,1.0,VEC_TEMP);
479: }
480: return(0);
481: }
483: /*
485: FGMRESUpdateHessenberg - Do the scalar work for the orthogonalization.
486: Return new residual.
488: input parameters:
490: . ksp - Krylov space object
491: . it - plane rotations are applied to the (it+1)th column of the
492: modified hessenberg (i.e. HH(:,it))
493: . hapend - PETSC_FALSE not happy breakdown ending.
495: output parameters:
496: . res - the new residual
497:
498: */
501: static PetscErrorCode FGMRESUpdateHessenberg(KSP ksp,PetscInt it,PetscTruth hapend,PetscReal *res)
502: {
503: PetscScalar *hh,*cc,*ss,tt;
504: PetscInt j;
505: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
508: hh = HH(0,it); /* pointer to beginning of column to update - so
509: incrementing hh "steps down" the (it+1)th col of HH*/
510: cc = CC(0); /* beginning of cosine rotations */
511: ss = SS(0); /* beginning of sine rotations */
513: /* Apply all the previously computed plane rotations to the new column
514: of the Hessenberg matrix */
515: /* Note: this uses the rotation [conj(c) s ; -s c], c= cos(theta), s= sin(theta),
516: and some refs have [c s ; -conj(s) c] (don't be confused!) */
518: for (j=1; j<=it; j++) {
519: tt = *hh;
520: #if defined(PETSC_USE_COMPLEX)
521: *hh = PetscConj(*cc) * tt + *ss * *(hh+1);
522: #else
523: *hh = *cc * tt + *ss * *(hh+1);
524: #endif
525: hh++;
526: *hh = *cc++ * *hh - (*ss++ * tt);
527: /* hh, cc, and ss have all been incremented one by end of loop */
528: }
530: /*
531: compute the new plane rotation, and apply it to:
532: 1) the right-hand-side of the Hessenberg system (RS)
533: note: it affects RS(it) and RS(it+1)
534: 2) the new column of the Hessenberg matrix
535: note: it affects HH(it,it) which is currently pointed to
536: by hh and HH(it+1, it) (*(hh+1))
537: thus obtaining the updated value of the residual...
538: */
540: /* compute new plane rotation */
542: if (!hapend) {
543: #if defined(PETSC_USE_COMPLEX)
544: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh+1)) * *(hh+1));
545: #else
546: tt = PetscSqrtScalar(*hh * *hh + *(hh+1) * *(hh+1));
547: #endif
548: if (tt == 0.0) {
549: ksp->reason = KSP_DIVERGED_NULL;
550: return(0);
551: }
553: *cc = *hh / tt; /* new cosine value */
554: *ss = *(hh+1) / tt; /* new sine value */
556: /* apply to 1) and 2) */
557: *RS(it+1) = - (*ss * *RS(it));
558: #if defined(PETSC_USE_COMPLEX)
559: *RS(it) = PetscConj(*cc) * *RS(it);
560: *hh = PetscConj(*cc) * *hh + *ss * *(hh+1);
561: #else
562: *RS(it) = *cc * *RS(it);
563: *hh = *cc * *hh + *ss * *(hh+1);
564: #endif
566: /* residual is the last element (it+1) of right-hand side! */
567: *res = PetscAbsScalar(*RS(it+1));
569: } else { /* happy breakdown: HH(it+1, it) = 0, therfore we don't need to apply
570: another rotation matrix (so RH doesn't change). The new residual is
571: always the new sine term times the residual from last time (RS(it)),
572: but now the new sine rotation would be zero...so the residual should
573: be zero...so we will multiply "zero" by the last residual. This might
574: not be exactly what we want to do here -could just return "zero". */
575:
576: *res = 0.0;
577: }
578: return(0);
579: }
581: /*
583: FGMRESGetNewVectors - This routine allocates more work vectors, starting from
584: VEC_VV(it), and more preconditioned work vectors, starting
585: from PREVEC(i).
587: */
590: static PetscErrorCode FGMRESGetNewVectors(KSP ksp,PetscInt it)
591: {
592: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
593: PetscInt nwork = fgmres->nwork_alloc; /* number of work vector chunks allocated */
594: PetscInt nalloc; /* number to allocate */
596: PetscInt k;
597:
599: nalloc = fgmres->delta_allocate; /* number of vectors to allocate
600: in a single chunk */
602: /* Adjust the number to allocate to make sure that we don't exceed the
603: number of available slots (fgmres->vecs_allocated)*/
604: if (it + VEC_OFFSET + nalloc >= fgmres->vecs_allocated){
605: nalloc = fgmres->vecs_allocated - it - VEC_OFFSET;
606: }
607: if (!nalloc) return(0);
609: fgmres->vv_allocated += nalloc; /* vv_allocated is the number of vectors allocated */
611: /* work vectors */
612: KSPGetVecs(ksp,nalloc,&fgmres->user_work[nwork],0,PETSC_NULL);
613: PetscLogObjectParents(ksp,nalloc,fgmres->user_work[nwork]);
614: for (k=0; k < nalloc; k++) {
615: fgmres->vecs[it+VEC_OFFSET+k] = fgmres->user_work[nwork][k];
616: }
617: /* specify size of chunk allocated */
618: fgmres->mwork_alloc[nwork] = nalloc;
620: /* preconditioned vectors */
621: KSPGetVecs(ksp,nalloc,&fgmres->prevecs_user_work[nwork],0,PETSC_NULL);
622: PetscLogObjectParents(ksp,nalloc,fgmres->prevecs_user_work[nwork]);
623: for (k=0; k < nalloc; k++) {
624: fgmres->prevecs[it+VEC_OFFSET+k] = fgmres->prevecs_user_work[nwork][k];
625: }
627: /* increment the number of work vector chunks */
628: fgmres->nwork_alloc++;
629: return(0);
630: }
632: /*
634: KSPBuildSolution_FGMRES
636: Input Parameter:
637: . ksp - the Krylov space object
638: . ptr-
640: Output Parameter:
641: . result - the solution
643: Note: this calls BuildFgmresSoln - the same function that FGMREScycle
644: calls directly.
646: */
649: PetscErrorCode KSPBuildSolution_FGMRES(KSP ksp,Vec ptr,Vec *result)
650: {
651: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
655: if (!ptr) {
656: if (!fgmres->sol_temp) {
657: VecDuplicate(ksp->vec_sol,&fgmres->sol_temp);
658: PetscLogObjectParent(ksp,fgmres->sol_temp);
659: }
660: ptr = fgmres->sol_temp;
661: }
662: if (!fgmres->nrs) {
663: /* allocate the work area */
664: PetscMalloc(fgmres->max_k*sizeof(PetscScalar),&fgmres->nrs);
665: PetscLogObjectMemory(ksp,fgmres->max_k*sizeof(PetscScalar));
666: }
667:
668: BuildFgmresSoln(fgmres->nrs,ksp->vec_sol,ptr,ksp,fgmres->it);
669: if (result) *result = ptr;
670:
671: return(0);
672: }
678: PetscErrorCode KSPSetFromOptions_FGMRES(KSP ksp)
679: {
681: PetscTruth flg;
684: KSPSetFromOptions_GMRES(ksp);
685: PetscOptionsHead("KSP flexible GMRES Options");
686: PetscOptionsTruthGroupBegin("-ksp_fgmres_modifypcnochange","do not vary the preconditioner","KSPFGMRESSetModifyPC",&flg);
687: if (flg) {KSPFGMRESSetModifyPC(ksp,KSPFGMRESModifyPCNoChange,0,0);}
688: PetscOptionsTruthGroupEnd("-ksp_fgmres_modifypcksp","vary the KSP based preconditioner","KSPFGMRESSetModifyPC",&flg);
689: if (flg) {KSPFGMRESSetModifyPC(ksp,KSPFGMRESModifyPCKSP,0,0);}
690: PetscOptionsTail();
691: return(0);
692: }
694: EXTERN PetscErrorCode KSPComputeExtremeSingularValues_GMRES(KSP,PetscReal *,PetscReal *);
695: EXTERN PetscErrorCode KSPComputeEigenvalues_GMRES(KSP,PetscInt,PetscReal *,PetscReal *,PetscInt *);
698: typedef PetscErrorCode (*FCN2)(void*);
702: PetscErrorCode KSPFGMRESSetModifyPC_FGMRES(KSP ksp,FCN1 fcn,void *ctx,FCN2 d)
703: {
706: ((KSP_FGMRES *)ksp->data)->modifypc = fcn;
707: ((KSP_FGMRES *)ksp->data)->modifydestroy = d;
708: ((KSP_FGMRES *)ksp->data)->modifyctx = ctx;
709: return(0);
710: }
714: EXTERN PetscErrorCode KSPGMRESSetPreAllocateVectors_GMRES(KSP);
715: EXTERN PetscErrorCode KSPGMRESSetRestart_GMRES(KSP,PetscInt);
716: EXTERN PetscErrorCode KSPGMRESSetOrthogonalization_GMRES(KSP,PetscErrorCode (*)(KSP,PetscInt));
719: EXTERN PetscErrorCode KSPDestroy_GMRES_Internal(KSP);
723: PetscErrorCode KSPDestroy_FGMRES_Internal(KSP ksp)
724: {
725: KSP_FGMRES *gmres = (KSP_FGMRES*)ksp->data;
729: KSPDestroy_GMRES_Internal(ksp);
730: PetscFree (gmres->prevecs);
731: PetscFree(gmres->prevecs_user_work);
732: if (gmres->modifydestroy) {
733: (*gmres->modifydestroy)(gmres->modifyctx);
734: }
735: return(0);
736: }
741: PetscErrorCode KSPGMRESSetRestart_FGMRES(KSP ksp,PetscInt max_k)
742: {
743: KSP_FGMRES *gmres = (KSP_FGMRES *)ksp->data;
747: if (max_k < 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Restart must be positive");
748: if (!ksp->setupcalled) {
749: gmres->max_k = max_k;
750: } else if (gmres->max_k != max_k) {
751: gmres->max_k = max_k;
752: ksp->setupcalled = 0;
753: /* free the data structures, then create them again */
754: KSPDestroy_FGMRES_Internal(ksp);
755: }
756: return(0);
757: }
761: EXTERN PetscErrorCode KSPGMRESSetCGSRefinementType_GMRES(KSP,KSPGMRESCGSRefinementType);
764: /*MC
765: KSPFGMRES - Implements the Flexible Generalized Minimal Residual method.
766: developed by Saad with restart
769: Options Database Keys:
770: + -ksp_gmres_restart <restart> - the number of Krylov directions to orthogonalize against
771: . -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
772: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
773: vectors are allocated as needed)
774: . -ksp_gmres_classicalgramschmidt - use classical (unmodified) Gram-Schmidt to orthogonalize against the Krylov space (fast) (the default)
775: . -ksp_gmres_modifiedgramschmidt - use modified Gram-Schmidt in the orthogonalization (more stable, but slower)
776: . -ksp_gmres_cgs_refinement_type <never,ifneeded,always> - determine if iterative refinement is used to increase the
777: stability of the classical Gram-Schmidt orthogonalization.
778: . -ksp_gmres_krylov_monitor - plot the Krylov space generated
779: . -ksp_fgmres_modifypcnochange - do not change the preconditioner between iterations
780: - -ksp_fgmres_modifypcksp - modify the preconditioner using KSPFGMRESModifyPCKSP()
782: Level: beginner
784: Notes: See KSPFGMRESSetModifyPC() for how to vary the preconditioner between iterations
785: This object is subclassed off of KSPGMRES
787: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPGMRES, KSPLGMRES,
788: KSPGMRESSetRestart(), KSPGMRESSetHapTol(), KSPGMRESSetPreAllocateVectors(), KSPGMRESSetOrthogonalization()
789: KSPGMRESClassicalGramSchmidtOrthogonalization(), KSPGMRESModifiedGramSchmidtOrthogonalization(),
790: KSPGMRESCGSRefinementType, KSPGMRESSetCGSRefinementType(), KSPGMRESMonitorKrylov(), KSPFGMRESSetModifyPC(),
791: KSPFGMRESModifyPCKSP()
793: M*/
798: PetscErrorCode KSPCreate_FGMRES(KSP ksp)
799: {
800: KSP_FGMRES *fgmres;
804: PetscNewLog(ksp,KSP_FGMRES,&fgmres);
805: ksp->data = (void*)fgmres;
806: ksp->ops->buildsolution = KSPBuildSolution_FGMRES;
807: ksp->ops->setup = KSPSetUp_FGMRES;
808: ksp->ops->solve = KSPSolve_FGMRES;
809: ksp->ops->destroy = KSPDestroy_FGMRES;
810: ksp->ops->view = KSPView_GMRES;
811: ksp->ops->setfromoptions = KSPSetFromOptions_FGMRES;
812: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
813: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
815: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",
816: "KSPGMRESSetPreAllocateVectors_GMRES",
817: KSPGMRESSetPreAllocateVectors_GMRES);
818: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C",
819: "KSPGMRESSetOrthogonalization_GMRES",
820: KSPGMRESSetOrthogonalization_GMRES);
821: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetRestart_C",
822: "KSPGMRESSetRestart_FGMRES",
823: KSPGMRESSetRestart_FGMRES);
824: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPFGMRESSetModifyPC_C",
825: "KSPFGMRESSetModifyPC_FGMRES",
826: KSPFGMRESSetModifyPC_FGMRES);
827: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",
828: "KSPGMRESSetCGSRefinementType_GMRES",
829: KSPGMRESSetCGSRefinementType_GMRES);
832: fgmres->haptol = 1.0e-30;
833: fgmres->q_preallocate = 0;
834: fgmres->delta_allocate = FGMRES_DELTA_DIRECTIONS;
835: fgmres->orthog = KSPGMRESClassicalGramSchmidtOrthogonalization;
836: fgmres->nrs = 0;
837: fgmres->sol_temp = 0;
838: fgmres->max_k = FGMRES_DEFAULT_MAXK;
839: fgmres->Rsvd = 0;
840: fgmres->orthogwork = 0;
841: fgmres->modifypc = KSPFGMRESModifyPCNoChange;
842: fgmres->modifyctx = PETSC_NULL;
843: fgmres->modifydestroy = PETSC_NULL;
844: fgmres->cgstype = KSP_GMRES_CGS_REFINE_NEVER;
845: /*
846: This is not great since it changes this without explicit request from the user
847: but there is no left preconditioning in the FGMRES
848: */
849: PetscInfo(ksp,"WARNING! Setting PC_SIDE for FGMRES to right!\n");
850: ksp->pc_side = PC_RIGHT;
851: ksp->normtype = KSP_NORM_UNPRECONDITIONED;
852: return(0);
853: }