Actual source code: fgmres.c
1: /* $Id: fgmres.c,v 1.29 2001/08/07 21:30:49 bsmith Exp $ */
3: /*
4: This file implements FGMRES (a Generalized Minimal Residual) method.
5: Reference: Saad, 1993.
7: Preconditioning: It the preconditioner is constant then this fgmres
8: code is equivalent to RIGHT-PRECONDITIONED GMRES.
10: Restarts: Restarts are basically solves with x0 not equal to zero.
11:
12: Contributed by Allison Baker
14: */
16: #include src/ksp/ksp/impls/fgmres/fgmresp.h
17: #define FGMRES_DELTA_DIRECTIONS 10
18: #define FGMRES_DEFAULT_MAXK 30
19: static int FGMRESGetNewVectors(KSP,int);
20: static int FGMRESUpdateHessenberg(KSP,int,PetscTruth,PetscReal *);
21: static int BuildFgmresSoln(PetscScalar*,Vec,Vec,KSP,int);
23: extern int KSPView_GMRES(KSP,PetscViewer);
24: /*
26: KSPSetUp_FGMRES - Sets up the workspace needed by fgmres.
28: This is called once, usually automatically by KSPSolveQ() or KSPSetUp(),
29: but can be called directly by KSPSetUp().
31: */
34: int KSPSetUp_FGMRES(KSP ksp)
35: {
36: unsigned int size,hh,hes,rs,cc;
37: int ierr,max_k,k;
38: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
41: if (ksp->pc_side == PC_SYMMETRIC) {
42: SETERRQ(2,"no symmetric preconditioning for KSPFGMRES");
43: } else if (ksp->pc_side == PC_LEFT) {
44: SETERRQ(2,"no left preconditioning for KSPFGMRES");
45: }
46: max_k = fgmres->max_k;
47: hh = (max_k + 2) * (max_k + 1);
48: hes = (max_k + 1) * (max_k + 1);
49: rs = (max_k + 2);
50: cc = (max_k + 1); /* SS and CC are the same size */
51: size = (hh + hes + rs + 2*cc) * sizeof(PetscScalar);
53: /* Allocate space and set pointers to beginning */
54: PetscMalloc(size,&fgmres->hh_origin);
55: PetscMemzero(fgmres->hh_origin,size);
56: PetscLogObjectMemory(ksp,size); /* HH - modified (by plane
57: rotations) hessenburg */
58: fgmres->hes_origin = fgmres->hh_origin + hh; /* HES - unmodified hessenburg */
59: fgmres->rs_origin = fgmres->hes_origin + hes; /* RS - the right-hand-side of the
60: Hessenberg system */
61: fgmres->cc_origin = fgmres->rs_origin + rs; /* CC - cosines for rotations */
62: fgmres->ss_origin = fgmres->cc_origin + cc; /* SS - sines for rotations */
64: if (ksp->calc_sings) {
65: /* Allocate workspace to hold Hessenberg matrix needed by Eispack */
66: size = (max_k + 3)*(max_k + 9)*sizeof(PetscScalar);
67: PetscMalloc(size,&fgmres->Rsvd);
68: PetscMalloc(5*(max_k+2)*sizeof(PetscReal),&fgmres->Dsvd);
69: PetscLogObjectMemory(ksp,size+5*(max_k+2)*sizeof(PetscReal));
70: }
72: /* Allocate array to hold pointers to user vectors. Note that we need
73: 4 + max_k + 1 (since we need it+1 vectors, and it <= max_k) */
74: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void *),&fgmres->vecs);
75: fgmres->vecs_allocated = VEC_OFFSET + 2 + max_k;
76: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void *),&fgmres->user_work);
77: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(int),&fgmres->mwork_alloc);
78: PetscLogObjectMemory(ksp,(VEC_OFFSET+2+max_k)*(2*sizeof(void *)+sizeof(int)));
80: /* New for FGMRES - Allocate array to hold pointers to preconditioned
81: vectors - same sizes as user vectors above */
82: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void *),&fgmres->prevecs);
83: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void *),&fgmres->prevecs_user_work);
84: PetscLogObjectMemory(ksp,(VEC_OFFSET+2+max_k)*(2*sizeof(void *)));
87: /* if q_preallocate = 0 then only allocate one "chunck" of space (for
88: 5 vectors) - additional will then be allocated from FGMREScycle()
89: as needed. Otherwise, allocate all of the space that could be needed */
90: if (fgmres->q_preallocate) {
91: fgmres->vv_allocated = VEC_OFFSET + 2 + max_k;
92: } else {
93: fgmres->vv_allocated = 5;
94: }
96: /* space for work vectors */
97: VecDuplicateVecs(VEC_RHS,fgmres->vv_allocated,&fgmres->user_work[0]);
98: PetscLogObjectParents(ksp,fgmres->vv_allocated,fgmres->user_work[0]);
99: for (k=0; k < fgmres->vv_allocated; k++) {
100: fgmres->vecs[k] = fgmres->user_work[0][k];
101: }
103: /* space for preconditioned vectors */
104: VecDuplicateVecs(VEC_RHS,fgmres->vv_allocated,&fgmres->prevecs_user_work[0]);
105: PetscLogObjectParents(ksp,fgmres->vv_allocated,fgmres->prevecs_user_work[0]);
106: for (k=0; k < fgmres->vv_allocated; k++) {
107: fgmres->prevecs[k] = fgmres->prevecs_user_work[0][k];
108: }
110: /* specify how many work vectors have been allocated in this
111: chunck" (the first one) */
112: fgmres->mwork_alloc[0] = fgmres->vv_allocated;
113: fgmres->nwork_alloc = 1;
115: return(0);
116: }
118: /*
119: FGMRESResidual - This routine computes the initial residual (NOT PRECONDITIONED)
120: */
123: static int FGMRESResidual(KSP ksp)
124: {
125: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
126: PetscScalar mone = -1.0;
127: Mat Amat,Pmat;
128: MatStructure pflag;
129: int ierr;
132: PCGetOperators(ksp->B,&Amat,&Pmat,&pflag);
134: /* put A*x into VEC_TEMP */
135: MatMult(Amat,VEC_SOLN,VEC_TEMP);
136: /* now put residual (-A*x + f) into vec_vv(0) */
137: VecWAXPY(&mone,VEC_TEMP,VEC_RHS,VEC_VV(0));
138: return(0);
139: }
141: /*
143: FGMRESCycle - Run fgmres, possibly with restart. Return residual
144: history if requested.
146: input parameters:
147: . fgmres - structure containing parameters and work areas
149: output parameters:
150: . itcount - number of iterations used. If null, ignored.
151: . converged - 0 if not converged
153:
154: Notes:
155: On entry, the value in vector VEC_VV(0) should be
156: the initial residual.
159: */
162: int FGMREScycle(int *itcount,KSP ksp)
163: {
165: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
166: PetscReal res_norm;
167: PetscReal hapbnd,tt;
168: PetscScalar zero = 0.0;
169: PetscScalar tmp;
170: PetscTruth hapend = PETSC_FALSE; /* indicates happy breakdown ending */
171: int ierr;
172: int loc_it; /* local count of # of dir. in Krylov space */
173: int max_k = fgmres->max_k; /* max # of directions Krylov space */
174: Mat Amat,Pmat;
175: MatStructure pflag;
179: /* Number of pseudo iterations since last restart is the number
180: of prestart directions */
181: loc_it = 0;
183: /* initial residual is in VEC_VV(0) - compute its norm*/
184: VecNorm(VEC_VV(0),NORM_2,&res_norm);
186: /* first entry in right-hand-side of hessenberg system is just
187: the initial residual norm */
188: *RS(0) = res_norm;
190: /* FYI: AMS calls are for memory snooper */
191: PetscObjectTakeAccess(ksp);
192: ksp->rnorm = res_norm;
193: PetscObjectGrantAccess(ksp);
194: KSPLogResidualHistory(ksp,res_norm);
196: /* check for the convergence - maybe the current guess is good enough */
197: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
198: if (ksp->reason) {
199: if (itcount) *itcount = 0;
200: return(0);
201: }
203: /* scale VEC_VV (the initial residual) */
204: tmp = 1.0/res_norm; VecScale(&tmp,VEC_VV(0));
208: /* note: (fgmres->it) is always set one less than (loc_it) It is used in
209: KSPBUILDSolution_FGMRES, where it is passed to BuildFGmresSoln.
210: Note that when BuildFGmresSoln is called from this function,
211: (loc_it -1) is passed, so the two are equivalent */
212: fgmres->it = (loc_it - 1);
213:
214: /* MAIN ITERATION LOOP BEGINNING*/
215: /* keep iterating until we have converged OR generated the max number
216: of directions OR reached the max number of iterations for the method */
217: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
218: while (!ksp->reason && loc_it < max_k && ksp->its < ksp->max_it) {
219: KSPLogResidualHistory(ksp,res_norm);
220: fgmres->it = (loc_it - 1);
221: KSPMonitor(ksp,ksp->its,res_norm);
223: /* see if more space is needed for work vectors */
224: if (fgmres->vv_allocated <= loc_it + VEC_OFFSET + 1) {
225: FGMRESGetNewVectors(ksp,loc_it+1);
226: /* (loc_it+1) is passed in as number of the first vector that should
227: be allocated */
228: }
230: /* CHANGE THE PRECONDITIONER? */
231: /* ModifyPC is the callback function that can be used to
232: change the PC or its attributes before its applied */
233: (*fgmres->modifypc)(ksp,ksp->its,loc_it,res_norm,fgmres->modifyctx);
234:
235:
236: /* apply PRECONDITIONER to direction vector and store with
237: preconditioned vectors in prevec */
238: PCApply(ksp->B,VEC_VV(loc_it),PREVEC(loc_it),PC_RIGHT);
239:
240: PCGetOperators(ksp->B,&Amat,&Pmat,&pflag);
241: /* Multiply preconditioned vector by operator - put in VEC_VV(loc_it+1) */
242: MatMult(Amat,PREVEC(loc_it),VEC_VV(1+loc_it));
244:
245: /* update hessenberg matrix and do Gram-Schmidt - new direction is in
246: VEC_VV(1+loc_it)*/
247: (*fgmres->orthog)(ksp,loc_it);
249: /* new entry in hessenburg is the 2-norm of our new direction */
250: VecNorm(VEC_VV(loc_it+1),NORM_2,&tt);
251: *HH(loc_it+1,loc_it) = tt;
252: *HES(loc_it+1,loc_it) = tt;
254: /* Happy Breakdown Check */
255: hapbnd = PetscAbsScalar((tt) / *RS(loc_it));
256: /* RS(loc_it) contains the res_norm from the last iteration */
257: hapbnd = PetscMin(fgmres->haptol,hapbnd);
258: if (tt > hapbnd) {
259: tmp = 1.0/tt;
260: /* scale new direction by its norm */
261: VecScale(&tmp,VEC_VV(loc_it+1));
262: } else {
263: /* This happens when the solution is exactly reached. */
264: /* So there is no new direction... */
265: VecSet(&zero,VEC_TEMP); /* set VEC_TEMP to 0 */
266: hapend = PETSC_TRUE;
267: }
268: /* note that for FGMRES we could get HES(loc_it+1, loc_it) = 0 and the
269: current solution would not be exact if HES was singular. Note that
270: HH non-singular implies that HES is no singular, and HES is guaranteed
271: to be nonsingular when PREVECS are linearly independent and A is
272: nonsingular (in GMRES, the nonsingularity of A implies the nonsingularity
273: of HES). So we should really add a check to verify that HES is nonsingular.*/
275:
276: /* Now apply rotations to new col of hessenberg (and right side of system),
277: calculate new rotation, and get new residual norm at the same time*/
278: FGMRESUpdateHessenberg(ksp,loc_it,hapend,&res_norm);
279: loc_it++;
280: fgmres->it = (loc_it-1); /* Add this here in case it has converged */
281:
282: PetscObjectTakeAccess(ksp);
283: ksp->its++;
284: ksp->rnorm = res_norm;
285: PetscObjectGrantAccess(ksp);
287: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
289: /* Catch error in happy breakdown and signal convergence and break from loop */
290: if (hapend) {
291: if (!ksp->reason) {
292: SETERRQ(0,"You reached the happy break down,but convergence was not indicated.");
293: }
294: break;
295: }
296: }
297: /* END OF ITERATION LOOP */
299: KSPLogResidualHistory(ksp,res_norm);
301: /*
302: Monitor if we know that we will not return for a restart */
303: if (ksp->reason || ksp->its >= ksp->max_it) {
304: KSPMonitor(ksp,ksp->its,res_norm);
305: }
307: if (itcount) *itcount = loc_it;
309: /*
310: Down here we have to solve for the "best" coefficients of the Krylov
311: columns, add the solution values together, and possibly unwind the
312: preconditioning from the solution
313: */
314:
315: /* Form the solution (or the solution so far) */
316: /* Note: must pass in (loc_it-1) for iteration count so that BuildFgmresSoln
317: properly navigates */
319: BuildFgmresSoln(RS(0),VEC_SOLN,VEC_SOLN,ksp,loc_it-1);
321: return(0);
322: }
324: /*
325: KSPSolve_FGMRES - This routine applies the FGMRES method.
328: Input Parameter:
329: . ksp - the Krylov space object that was set to use fgmres
331: Output Parameter:
332: . outits - number of iterations used
334: */
338: int KSPSolve_FGMRES(KSP ksp)
339: {
340: int ierr;
341: int cycle_its; /* iterations done in a call to FGMREScycle */
342: int itcount; /* running total of iterations, incl. those in restarts */
343: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
344: PetscTruth diagonalscale;
347: PCDiagonalScale(ksp->B,&diagonalscale);
348: if (diagonalscale) SETERRQ1(1,"Krylov method %s does not support diagonal scaling",ksp->type_name);
350: PetscObjectTakeAccess(ksp);
351: ksp->its = 0;
352: PetscObjectGrantAccess(ksp);
354: /* initialize */
355: itcount = 0;
357: /* Compute the initial (NOT preconditioned) residual */
358: if (!ksp->guess_zero) {
359: FGMRESResidual(ksp);
360: } else { /* guess is 0 so residual is F (which is in VEC_RHS) */
361: VecCopy(VEC_RHS,VEC_VV(0));
362: }
363: /* now the residual is in VEC_VV(0) - which is what
364: FGMREScycle expects... */
365:
366: FGMREScycle(&cycle_its,ksp);
367: itcount += cycle_its;
368: while (!ksp->reason) {
369: FGMRESResidual(ksp);
370: if (itcount >= ksp->max_it) break;
371: FGMREScycle(&cycle_its,ksp);
372: itcount += cycle_its;
373: }
374: /* mark lack of convergence */
375: if (itcount >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
377: return(0);
378: }
380: /*
382: KSPDestroy_FGMRES - Frees all memory space used by the Krylov method.
384: */
387: int KSPDestroy_FGMRES(KSP ksp)
388: {
389: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
390: int i,ierr;
393: /* Free the Hessenberg matrices */
394: if (fgmres->hh_origin) {PetscFree(fgmres->hh_origin);}
396: /* Free pointers to user variables */
397: if (fgmres->vecs) {PetscFree(fgmres->vecs);}
398: if (fgmres->prevecs) {PetscFree (fgmres->prevecs);}
400: /* free work vectors */
401: for (i=0; i < fgmres->nwork_alloc; i++) {
402: VecDestroyVecs(fgmres->user_work[i],fgmres->mwork_alloc[i]);
403: }
404: if (fgmres->user_work) {PetscFree(fgmres->user_work);}
406: for (i=0; i < fgmres->nwork_alloc; i++) {
407: VecDestroyVecs(fgmres->prevecs_user_work[i],fgmres->mwork_alloc[i]);
408: }
409: if (fgmres->prevecs_user_work) {PetscFree(fgmres->prevecs_user_work);}
411: if (fgmres->mwork_alloc) {PetscFree(fgmres->mwork_alloc);}
412: if (fgmres->nrs) {PetscFree(fgmres->nrs);}
413: if (fgmres->sol_temp) {VecDestroy(fgmres->sol_temp);}
414: if (fgmres->Rsvd) {PetscFree(fgmres->Rsvd);}
415: if (fgmres->Dsvd) {PetscFree(fgmres->Dsvd);}
416: if (fgmres->modifydestroy) {
417: (*fgmres->modifydestroy)(fgmres->modifyctx);
418: }
419: PetscFree(fgmres);
420: return(0);
421: }
423: /*
424: BuildFgmresSoln - create the solution from the starting vector and the
425: current iterates.
427: Input parameters:
428: nrs - work area of size it + 1.
429: vguess - index of initial guess
430: vdest - index of result. Note that vguess may == vdest (replace
431: guess with the solution).
432: it - HH upper triangular part is a block of size (it+1) x (it+1)
434: This is an internal routine that knows about the FGMRES internals.
435: */
438: static int BuildFgmresSoln(PetscScalar* nrs,Vec vguess,Vec vdest,KSP ksp,int it)
439: {
440: PetscScalar tt,zero = 0.0,one = 1.0;
441: int ierr,ii,k,j;
442: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
445: /* Solve for solution vector that minimizes the residual */
447: /* If it is < 0, no fgmres steps have been performed */
448: if (it < 0) {
449: if (vdest != vguess) {
450: VecCopy(vguess,vdest);
451: }
452: return(0);
453: }
455: /* so fgmres steps HAVE been performed */
457: /* solve the upper triangular system - RS is the right side and HH is
458: the upper triangular matrix - put soln in nrs */
459: nrs[it] = *RS(it) / *HH(it,it);
460: for (ii=1; ii<=it; ii++) {
461: k = it - ii;
462: tt = *RS(k);
463: for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
464: nrs[k] = tt / *HH(k,k);
465: }
467: /* Accumulate the correction to the soln of the preconditioned prob. in
468: VEC_TEMP - note that we use the preconditioned vectors */
469: VecSet(&zero,VEC_TEMP); /* set VEC_TEMP components to 0 */
470: VecMAXPY(it+1,nrs,VEC_TEMP,&PREVEC(0));
472: /* put updated solution into vdest.*/
473: if (vdest != vguess) {
474: VecCopy(VEC_TEMP,vdest);
475: VecAXPY(&one,vguess,vdest);
476: } else {/* replace guess with solution */
477: VecAXPY(&one,VEC_TEMP,vdest);
478: }
479: return(0);
480: }
482: /*
484: FGMRESUpdateHessenberg - Do the scalar work for the orthogonalization.
485: Return new residual.
487: input parameters:
489: . ksp - Krylov space object
490: . it - plane rotations are applied to the (it+1)th column of the
491: modified hessenberg (i.e. HH(:,it))
492: . hapend - PETSC_FALSE not happy breakdown ending.
494: output parameters:
495: . res - the new residual
496:
497: */
500: static int FGMRESUpdateHessenberg(KSP ksp,int it,PetscTruth hapend,PetscReal *res)
501: {
502: PetscScalar *hh,*cc,*ss,tt;
503: int j;
504: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
507: hh = HH(0,it); /* pointer to beginning of column to update - so
508: incrementing hh "steps down" the (it+1)th col of HH*/
509: cc = CC(0); /* beginning of cosine rotations */
510: ss = SS(0); /* beginning of sine rotations */
512: /* Apply all the previously computed plane rotations to the new column
513: of the Hessenberg matrix */
514: /* Note: this uses the rotation [conj(c) s ; -s c], c= cos(theta), s= sin(theta),
515: and some refs have [c s ; -conj(s) c] (don't be confused!) */
517: for (j=1; j<=it; j++) {
518: tt = *hh;
519: #if defined(PETSC_USE_COMPLEX)
520: *hh = PetscConj(*cc) * tt + *ss * *(hh+1);
521: #else
522: *hh = *cc * tt + *ss * *(hh+1);
523: #endif
524: hh++;
525: *hh = *cc++ * *hh - (*ss++ * tt);
526: /* hh, cc, and ss have all been incremented one by end of loop */
527: }
529: /*
530: compute the new plane rotation, and apply it to:
531: 1) the right-hand-side of the Hessenberg system (RS)
532: note: it affects RS(it) and RS(it+1)
533: 2) the new column of the Hessenberg matrix
534: note: it affects HH(it,it) which is currently pointed to
535: by hh and HH(it+1, it) (*(hh+1))
536: thus obtaining the updated value of the residual...
537: */
539: /* compute new plane rotation */
541: if (!hapend) {
542: #if defined(PETSC_USE_COMPLEX)
543: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh+1)) * *(hh+1));
544: #else
545: tt = PetscSqrtScalar(*hh * *hh + *(hh+1) * *(hh+1));
546: #endif
547: if (tt == 0.0) {SETERRQ(PETSC_ERR_KSP_BRKDWN,"Your matrix or preconditioner is the null operator");}
548: *cc = *hh / tt; /* new cosine value */
549: *ss = *(hh+1) / tt; /* new sine value */
551: /* apply to 1) and 2) */
552: *RS(it+1) = - (*ss * *RS(it));
553: #if defined(PETSC_USE_COMPLEX)
554: *RS(it) = PetscConj(*cc) * *RS(it);
555: *hh = PetscConj(*cc) * *hh + *ss * *(hh+1);
556: #else
557: *RS(it) = *cc * *RS(it);
558: *hh = *cc * *hh + *ss * *(hh+1);
559: #endif
561: /* residual is the last element (it+1) of right-hand side! */
562: *res = PetscAbsScalar(*RS(it+1));
564: } else { /* happy breakdown: HH(it+1, it) = 0, therfore we don't need to apply
565: another rotation matrix (so RH doesn't change). The new residual is
566: always the new sine term times the residual from last time (RS(it)),
567: but now the new sine rotation would be zero...so the residual should
568: be zero...so we will multiply "zero" by the last residual. This might
569: not be exactly what we want to do here -could just return "zero". */
570:
571: *res = 0.0;
572: }
573: return(0);
574: }
576: /*
578: FGMRESGetNewVectors - This routine allocates more work vectors, starting from
579: VEC_VV(it), and more preconditioned work vectors, starting
580: from PREVEC(i).
582: */
585: static int FGMRESGetNewVectors(KSP ksp,int it)
586: {
587: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
588: int nwork = fgmres->nwork_alloc; /* number of work vector chunks allocated */
589: int nalloc; /* number to allocate */
590: int k,ierr;
591:
593: nalloc = fgmres->delta_allocate; /* number of vectors to allocate
594: in a single chunk */
596: /* Adjust the number to allocate to make sure that we don't exceed the
597: number of available slots (fgmres->vecs_allocated)*/
598: if (it + VEC_OFFSET + nalloc >= fgmres->vecs_allocated){
599: nalloc = fgmres->vecs_allocated - it - VEC_OFFSET;
600: }
601: if (!nalloc) return(0);
603: fgmres->vv_allocated += nalloc; /* vv_allocated is the number of vectors allocated */
605: /* work vectors */
606: VecDuplicateVecs(VEC_RHS,nalloc,&fgmres->user_work[nwork]);
607: PetscLogObjectParents(ksp,nalloc,fgmres->user_work[nwork]);
608: for (k=0; k < nalloc; k++) {
609: fgmres->vecs[it+VEC_OFFSET+k] = fgmres->user_work[nwork][k];
610: }
611: /* specify size of chunk allocated */
612: fgmres->mwork_alloc[nwork] = nalloc;
614: /* preconditioned vectors */
615: VecDuplicateVecs(VEC_RHS,nalloc,&fgmres->prevecs_user_work[nwork]);
616: PetscLogObjectParents(ksp,nalloc,fgmres->prevecs_user_work[nwork]);
617: for (k=0; k < nalloc; k++) {
618: fgmres->prevecs[it+VEC_OFFSET+k] = fgmres->prevecs_user_work[nwork][k];
619: }
621: /* increment the number of work vector chunks */
622: fgmres->nwork_alloc++;
623: return(0);
624: }
626: /*
628: KSPBuildSolution_FGMRES
630: Input Parameter:
631: . ksp - the Krylov space object
632: . ptr-
634: Output Parameter:
635: . result - the solution
637: Note: this calls BuildFgmresSoln - the same function that FGMREScycle
638: calls directly.
640: */
643: int KSPBuildSolution_FGMRES(KSP ksp,Vec ptr,Vec *result)
644: {
645: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
646: int ierr;
649: if (!ptr) {
650: if (!fgmres->sol_temp) {
651: VecDuplicate(ksp->vec_sol,&fgmres->sol_temp);
652: PetscLogObjectParent(ksp,fgmres->sol_temp);
653: }
654: ptr = fgmres->sol_temp;
655: }
656: if (!fgmres->nrs) {
657: /* allocate the work area */
658: PetscMalloc(fgmres->max_k*sizeof(PetscScalar),&fgmres->nrs);
659: PetscLogObjectMemory(ksp,fgmres->max_k*sizeof(PetscScalar));
660: }
661:
662: BuildFgmresSoln(fgmres->nrs,VEC_SOLN,ptr,ksp,fgmres->it);
663: *result = ptr;
664:
665: return(0);
666: }
671: int KSPSetFromOptions_FGMRES(KSP ksp)
672: {
673: int ierr,restart,indx;
674: PetscReal haptol;
675: KSP_FGMRES *gmres = (KSP_FGMRES*)ksp->data;
676: PetscTruth flg;
677: const char *types[] = {"never","ifneeded","always"};
680: PetscOptionsHead("KSP flexible GMRES Options");
681: PetscOptionsInt("-ksp_gmres_restart","Number of Krylov search directions","KSPGMRESSetRestart",gmres->max_k,&restart,&flg);
682: if (flg) { KSPGMRESSetRestart(ksp,restart); }
683: PetscOptionsReal("-ksp_gmres_haptol","Tolerance for declaring exact convergence (happy ending)","KSPGMRESSetHapTol",gmres->haptol,&haptol,&flg);
684: if (flg) { KSPGMRESSetHapTol(ksp,haptol); }
685: PetscOptionsName("-ksp_gmres_preallocate","Preallocate all Krylov vectors","KSPGMRESSetPreAllocateVectors",&flg);
686: if (flg) {KSPGMRESSetPreAllocateVectors(ksp);}
687: PetscOptionsLogicalGroupBegin("-ksp_gmres_classicalgramschmidt","Use classical (unmodified) Gram-Schmidt (fast)","KSPGMRESSetOrthogonalization",&flg);
688: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESClassicalGramSchmidtOrthogonalization);}
689: PetscOptionsLogicalGroup("-ksp_gmres_modifiedgramschmidt","Use modified Gram-Schmidt (slow but more stable)","KSPGMRESSetOrthogonalization",&flg);
690: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESModifiedGramSchmidtOrthogonalization);}
691: PetscOptionsEList("-ksp_gmres_cgs_refinement_type","Type of iterative refinement for classical (unmodified) Gram-Schmidt","KSPGMRESSetCGSRefinementType()",types,3,types[gmres->cgstype],&indx,&flg);
692: if (flg) {
693: KSPGMRESSetCGSRefinementType(ksp,(KSPGMRESCGSRefinementType)indx);
694: }
695: PetscOptionsName("-ksp_gmres_krylov_monitor","Graphically plot the Krylov directions","KSPSetMonitor",&flg);
696: if (flg) {
697: PetscViewers viewers;
698: PetscViewersCreate(ksp->comm,&viewers);
699: KSPSetMonitor(ksp,KSPGMRESKrylovMonitor,viewers,(int (*)(void*))PetscViewersDestroy);
700: }
701: PetscOptionsLogicalGroupBegin("-ksp_fgmres_modifypcnochange","do not vary the preconditioner","KSPFGMRESSetModifyPC",&flg);
702: if (flg) {KSPFGMRESSetModifyPC(ksp,KSPFGMRESModifyPCNoChange,0,0);}
703: PetscOptionsLogicalGroupEnd("-ksp_fgmres_modifypcksp","vary the KSP based preconditioner","KSPFGMRESSetModifyPC",&flg);
704: if (flg) {KSPFGMRESSetModifyPC(ksp,KSPFGMRESModifyPCKSP,0,0);}
705: PetscOptionsTail();
706: return(0);
707: }
709: EXTERN int KSPComputeExtremeSingularValues_GMRES(KSP,PetscReal *,PetscReal *);
710: EXTERN int KSPComputeEigenvalues_GMRES(KSP,int,PetscReal *,PetscReal *,int *);
712: typedef int (*FCN1)(KSP,int,int,PetscReal,void*); /* force argument to next function to not be extern C*/
713: typedef int (*FCN2)(void*);
714: EXTERN_C_BEGIN
717: int KSPFGMRESSetModifyPC_FGMRES(KSP ksp,FCN1 fcn,void *ctx,FCN2 d)
718: {
721: ((KSP_FGMRES *)ksp->data)->modifypc = fcn;
722: ((KSP_FGMRES *)ksp->data)->modifydestroy = d;
723: ((KSP_FGMRES *)ksp->data)->modifyctx = ctx;
724: return(0);
725: }
726: EXTERN_C_END
728: EXTERN_C_BEGIN
729: EXTERN int KSPGMRESSetPreAllocateVectors_GMRES(KSP);
730: EXTERN int KSPGMRESSetRestart_GMRES(KSP,int);
731: EXTERN int KSPGMRESSetOrthogonalization_GMRES(KSP,int (*)(KSP,int));
732: EXTERN_C_END
736: int KSPDestroy_FGMRES_Internal(KSP ksp)
737: {
738: KSP_FGMRES *gmres = (KSP_FGMRES*)ksp->data;
739: int i,ierr;
742: /* Free the Hessenberg matrix */
743: if (gmres->hh_origin) {PetscFree(gmres->hh_origin);}
745: /* Free the pointer to user variables */
746: if (gmres->vecs) {PetscFree(gmres->vecs);}
748: /* free work vectors */
749: for (i=0; i<gmres->nwork_alloc; i++) {
750: VecDestroyVecs(gmres->user_work[i],gmres->mwork_alloc[i]);
751: }
752: if (gmres->user_work) {PetscFree(gmres->user_work);}
753: if (gmres->mwork_alloc) {PetscFree(gmres->mwork_alloc);}
754: if (gmres->nrs) {PetscFree(gmres->nrs);}
755: if (gmres->sol_temp) {VecDestroy(gmres->sol_temp);}
756: if (gmres->Rsvd) {PetscFree(gmres->Rsvd);}
757: if (gmres->Dsvd) {PetscFree(gmres->Dsvd);}
759: return(0);
760: }
762: EXTERN_C_BEGIN
765: int KSPGMRESSetRestart_FGMRES(KSP ksp,int max_k)
766: {
767: KSP_FGMRES *gmres = (KSP_FGMRES *)ksp->data;
768: int ierr;
771: if (max_k < 1) SETERRQ(1,"Restart must be positive");
772: if (!ksp->setupcalled) {
773: gmres->max_k = max_k;
774: } else if (gmres->max_k != max_k) {
775: gmres->max_k = max_k;
776: ksp->setupcalled = 0;
777: /* free the data structures, then create them again */
778: KSPDestroy_FGMRES_Internal(ksp);
779: }
780: return(0);
781: }
782: EXTERN_C_END
784: EXTERN_C_BEGIN
785: EXTERN int KSPGMRESSetCGSRefinementType_GMRES(KSP,KSPGMRESCGSRefinementType);
786: EXTERN_C_END
788: /*MC
789: KSPFGMRES - Implements the Flexible Generalized Minimal Residual method.
790: developed by Saad with restart
793: Options Database Keys:
794: + -ksp_gmres_restart <restart> - the number of Krylov directions to orthogonalize against
795: . -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
796: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
797: vectors are allocated as needed)
798: . -ksp_gmres_classicalgramschmidt - use classical (unmodified) Gram-Schmidt to orthogonalize against the Krylov space (fast) (the default)
799: . -ksp_gmres_modifiedgramschmidt - use modified Gram-Schmidt in the orthogonalization (more stable, but slower)
800: . -ksp_gmres_cgs_refinement_type <never,ifneeded,always> - determine if iterative refinement is used to increase the
801: stability of the classical Gram-Schmidt orthogonalization.
802: . -ksp_gmres_krylov_monitor - plot the Krylov space generated
803: . -ksp_fgmres_modifypcnochange - do not change the preconditioner between iterations
804: - -ksp_fgmres_modifypcksp - modify the preconditioner using KSPFGMRESModifyPCKSP()
806: Level: beginner
808: Notes: See KSPFGMRESSetModifyPC() for how to vary the preconditioner between iterations
810: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPGMRES, KSPLGMRES,
811: KSPGMRESSetRestart(), KSPGMRESSetHapTol(), KSPGMRESSetPreAllocateVectors(), KSPGMRESSetOrthogonalization()
812: KSPGMRESClassicalGramSchmidtOrthogonalization(), KSPGMRESModifiedGramSchmidtOrthogonalization(),
813: KSPGMRESCGSRefinementType, KSPGMRESSetCGSRefinementType(), KSPGMRESKrylovMonitor(), KSPFGMRESSetModifyPC(),
814: KSPFGMRESModifyPCKSP()
816: M*/
818: EXTERN_C_BEGIN
821: int KSPCreate_FGMRES(KSP ksp)
822: {
823: KSP_FGMRES *fgmres;
824: int ierr;
827: PetscNew(KSP_FGMRES,&fgmres);
828: PetscMemzero(fgmres,sizeof(KSP_FGMRES));
829: PetscLogObjectMemory(ksp,sizeof(KSP_FGMRES));
830: ksp->data = (void*)fgmres;
831: ksp->ops->buildsolution = KSPBuildSolution_FGMRES;
833: ksp->ops->setup = KSPSetUp_FGMRES;
834: ksp->ops->solve = KSPSolve_FGMRES;
835: ksp->ops->destroy = KSPDestroy_FGMRES;
836: ksp->ops->view = KSPView_GMRES;
837: ksp->ops->setfromoptions = KSPSetFromOptions_FGMRES;
838: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
839: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
841: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",
842: "KSPGMRESSetPreAllocateVectors_GMRES",
843: KSPGMRESSetPreAllocateVectors_GMRES);
844: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C",
845: "KSPGMRESSetOrthogonalization_GMRES",
846: KSPGMRESSetOrthogonalization_GMRES);
847: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetRestart_C",
848: "KSPGMRESSetRestart_FGMRES",
849: KSPGMRESSetRestart_FGMRES);
850: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPFGMRESSetModifyPC_C",
851: "KSPFGMRESSetModifyPC_FGMRES",
852: KSPFGMRESSetModifyPC_FGMRES);
853: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",
854: "KSPGMRESSetCGSRefinementType_GMRES",
855: KSPGMRESSetCGSRefinementType_GMRES);
858: fgmres->haptol = 1.0e-30;
859: fgmres->q_preallocate = 0;
860: fgmres->delta_allocate = FGMRES_DELTA_DIRECTIONS;
861: fgmres->orthog = KSPGMRESClassicalGramSchmidtOrthogonalization;
862: fgmres->nrs = 0;
863: fgmres->sol_temp = 0;
864: fgmres->max_k = FGMRES_DEFAULT_MAXK;
865: fgmres->Rsvd = 0;
866: fgmres->modifypc = KSPFGMRESModifyPCNoChange;
867: fgmres->modifyctx = PETSC_NULL;
868: fgmres->modifydestroy = PETSC_NULL;
869: fgmres->cgstype = KSP_GMRES_CGS_REFINE_NEVER;
870: /*
871: This is not great since it changes this without explicit request from the user
872: but there is no left preconditioning in the FGMRES
873: */
874: PetscLogInfo(ksp,"Warning: Setting PC_SIDE for FGMRES to right!\n");
875: ksp->pc_side = PC_RIGHT;
877: return(0);
878: }
879: EXTERN_C_END