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/sles/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: /*
25: KSPSetUp_FGMRES - Sets up the workspace needed by fgmres.
27: This is called once, usually automatically by SLESSolve() or SLESSetUp(),
28: but can be called directly by KSPSetUp().
30: */
31: int KSPSetUp_FGMRES(KSP ksp)
32: {
33: unsigned int size,hh,hes,rs,cc;
34: int ierr,max_k,k;
35: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
38: if (ksp->pc_side == PC_SYMMETRIC) {
39: SETERRQ(2,"no symmetric preconditioning for KSPFGMRES");
40: }
41: max_k = fgmres->max_k;
42: hh = (max_k + 2) * (max_k + 1);
43: hes = (max_k + 1) * (max_k + 1);
44: rs = (max_k + 2);
45: cc = (max_k + 1); /* SS and CC are the same size */
46: size = (hh + hes + rs + 2*cc) * sizeof(PetscScalar);
48: /* Allocate space and set pointers to beginning */
49: PetscMalloc(size,&fgmres->hh_origin);
50: PetscLogObjectMemory(ksp,size); /* HH - modified (by plane
51: rotations) hessenburg */
52: fgmres->hes_origin = fgmres->hh_origin + hh; /* HES - unmodified hessenburg */
53: fgmres->rs_origin = fgmres->hes_origin + hes; /* RS - the right-hand-side of the
54: Hessenberg system */
55: fgmres->cc_origin = fgmres->rs_origin + rs; /* CC - cosines for rotations */
56: fgmres->ss_origin = fgmres->cc_origin + cc; /* SS - sines for rotations */
58: if (ksp->calc_sings) {
59: /* Allocate workspace to hold Hessenberg matrix needed by Eispack */
60: size = (max_k + 3)*(max_k + 9)*sizeof(PetscScalar);
61: PetscMalloc(size,&fgmres->Rsvd);
62: PetscMalloc(5*(max_k+2)*sizeof(PetscReal),&fgmres->Dsvd);
63: PetscLogObjectMemory(ksp,size+5*(max_k+2)*sizeof(PetscReal));
64: }
66: /* Allocate array to hold pointers to user vectors. Note that we need
67: 4 + max_k + 1 (since we need it+1 vectors, and it <= max_k) */
68: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void *),&fgmres->vecs);
69: fgmres->vecs_allocated = VEC_OFFSET + 2 + max_k;
70: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void *),&fgmres->user_work);
71: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(int),&fgmres->mwork_alloc);
72: PetscLogObjectMemory(ksp,(VEC_OFFSET+2+max_k)*(2*sizeof(void *)+sizeof(int)));
74: /* New for FGMRES - Allocate array to hold pointers to preconditioned
75: vectors - same sizes as user vectors above */
76: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void *),&fgmres->prevecs);
77: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void *),&fgmres->prevecs_user_work);
78: PetscLogObjectMemory(ksp,(VEC_OFFSET+2+max_k)*(2*sizeof(void *)));
81: /* if q_preallocate = 0 then only allocate one "chunck" of space (for
82: 5 vectors) - additional will then be allocated from FGMREScycle()
83: as needed. Otherwise, allocate all of the space that could be needed */
84: if (fgmres->q_preallocate) {
85: fgmres->vv_allocated = VEC_OFFSET + 2 + max_k;
86: } else {
87: fgmres->vv_allocated = 5;
88: }
90: /* space for work vectors */
91: VecDuplicateVecs(VEC_RHS,fgmres->vv_allocated,&fgmres->user_work[0]);
92: PetscLogObjectParents(ksp,fgmres->vv_allocated,fgmres->user_work[0]);
93: for (k=0; k < fgmres->vv_allocated; k++) {
94: fgmres->vecs[k] = fgmres->user_work[0][k];
95: }
97: /* space for preconditioned vectors */
98: VecDuplicateVecs(VEC_RHS,fgmres->vv_allocated,&fgmres->prevecs_user_work[0]);
99: PetscLogObjectParents(ksp,fgmres->vv_allocated,fgmres->prevecs_user_work[0]);
100: for (k=0; k < fgmres->vv_allocated; k++) {
101: fgmres->prevecs[k] = fgmres->prevecs_user_work[0][k];
102: }
104: /* specify how many work vectors have been allocated in this
105: chunck" (the first one) */
106: fgmres->mwork_alloc[0] = fgmres->vv_allocated;
107: fgmres->nwork_alloc = 1;
109: return(0);
110: }
112: /*
113: FGMRESResidual - This routine computes the initial residual (NOT PRECONDITIONED)
114: */
115: static int FGMRESResidual(KSP ksp)
116: {
117: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
118: PetscScalar mone = -1.0;
119: Mat Amat,Pmat;
120: MatStructure pflag;
121: int ierr;
124: PCGetOperators(ksp->B,&Amat,&Pmat,&pflag);
126: /* put A*x into VEC_TEMP */
127: MatMult(Amat,VEC_SOLN,VEC_TEMP);
128: /* now put residual (-A*x + f) into vec_vv(0) */
129: VecWAXPY(&mone,VEC_TEMP,VEC_RHS,VEC_VV(0));
130: return(0);
131: }
133: /*
135: FGMRESCycle - Run fgmres, possibly with restart. Return residual
136: history if requested.
138: input parameters:
139: . fgmres - structure containing parameters and work areas
141: output parameters:
142: . itcount - number of iterations used. If null, ignored.
143: . converged - 0 if not converged
145:
146: Notes:
147: On entry, the value in vector VEC_VV(0) should be
148: the initial residual.
151: */
152: int FGMREScycle(int *itcount,KSP ksp)
153: {
155: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
156: PetscReal res_norm;
157: PetscReal hapbnd,tt;
158: PetscScalar zero = 0.0;
159: PetscScalar tmp;
160: PetscTruth hapend = PETSC_FALSE; /* indicates happy breakdown ending */
161: int ierr;
162: int loc_it; /* local count of # of dir. in Krylov space */
163: int max_k = fgmres->max_k; /* max # of directions Krylov space */
164: int max_it = ksp->max_it; /* max # of overall iterations for the method */
165: Mat Amat,Pmat;
166: MatStructure pflag;
170: /* Number of pseudo iterations since last restart is the number
171: of prestart directions */
172: loc_it = 0;
174: /* initial residual is in VEC_VV(0) - compute its norm*/
175: ierr = VecNorm(VEC_VV(0),NORM_2,&res_norm);
177: /* first entry in right-hand-side of hessenberg system is just
178: the initial residual norm */
179: *RS(0) = res_norm;
181: /* check for the convergence - maybe the current guess is good enough */
182: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
183: if (ksp->reason) {
184: if (itcount) *itcount = 0;
185: return(0);
186: }
188: /* scale VEC_VV (the initial residual) */
189: tmp = 1.0/res_norm; VecScale(&tmp,VEC_VV(0));
191: /* FYI: AMS calls are for memory snooper */
192: PetscObjectTakeAccess(ksp);
193: ksp->rnorm = res_norm;
194: PetscObjectGrantAccess(ksp);
197: /* note: (fgmres->it) is always set one less than (loc_it) It is used in
198: KSPBUILDSolution_FGMRES, where it is passed to BuildFGmresSoln.
199: Note that when BuildFGmresSoln is called from this function,
200: (loc_it -1) is passed, so the two are equivalent */
201: fgmres->it = (loc_it - 1);
202:
203: /* MAIN ITERATION LOOP BEGINNING*/
204: /* keep iterating until we have converged OR generated the max number
205: of directions OR reached the max number of iterations for the method */
206: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
207: while (!ksp->reason && loc_it < max_k && ksp->its < max_it) {
208: KSPLogResidualHistory(ksp,res_norm);
209: fgmres->it = (loc_it - 1);
210: KSPMonitor(ksp,ksp->its,res_norm);
212: /* see if more space is needed for work vectors */
213: if (fgmres->vv_allocated <= loc_it + VEC_OFFSET + 1) {
214: FGMRESGetNewVectors(ksp,loc_it+1);
215: /* (loc_it+1) is passed in as number of the first vector that should
216: be allocated */
217: }
219: /* CHANGE THE PRECONDITIONER? */
220: /* ModifyPC is the callback function that can be used to
221: change the PC or its attributes before its applied */
222: (*fgmres->modifypc)(ksp,ksp->its,loc_it,res_norm,fgmres->modifyctx);
223:
224:
225: /* apply PRECONDITIONER to direction vector and store with
226: preconditioned vectors in prevec */
227: PCApply(ksp->B,VEC_VV(loc_it),PREVEC(loc_it));
228:
229: PCGetOperators(ksp->B,&Amat,&Pmat,&pflag);
230: /* Multiply preconditioned vector by operator - put in VEC_VV(loc_it+1) */
231: MatMult(Amat,PREVEC(loc_it),VEC_VV(1+loc_it));
233:
234: /* update hessenberg matrix and do Gram-Schmidt - new direction is in
235: VEC_VV(1+loc_it)*/
236: (*fgmres->orthog)(ksp,loc_it);
238: /* new entry in hessenburg is the 2-norm of our new direction */
239: VecNorm(VEC_VV(loc_it+1),NORM_2,&tt);
240: *HH(loc_it+1,loc_it) = tt;
241: *HES(loc_it+1,loc_it) = tt;
243: /* Happy Breakdown Check */
244: hapbnd = PetscAbsScalar((tt) / *RS(loc_it));
245: /* RS(loc_it) contains the res_norm from the last iteration */
246: hapbnd = PetscMin(fgmres->haptol,hapbnd);
247: if (tt > hapbnd) {
248: tmp = 1.0/tt;
249: /* scale new direction by its norm */
250: VecScale(&tmp,VEC_VV(loc_it+1));
251: } else {
252: /* This happens when the solution is exactly reached. */
253: /* So there is no new direction... */
254: ierr = VecSet(&zero,VEC_TEMP); /* set VEC_TEMP to 0 */
255: hapend = PETSC_TRUE;
256: }
257: /* note that for FGMRES we could get HES(loc_it+1, loc_it) = 0 and the
258: current solution would not be exact if HES was singular. Note that
259: HH non-singular implies that HES is no singular, and HES is guaranteed
260: to be nonsingular when PREVECS are linearly independent and A is
261: nonsingular (in GMRES, the nonsingularity of A implies the nonsingularity
262: of HES). So we should really add a check to verify that HES is nonsingular.*/
264:
265: /* Now apply rotations to new col of hessenberg (and right side of system),
266: calculate new rotation, and get new residual norm at the same time*/
267: FGMRESUpdateHessenberg(ksp,loc_it,hapend,&res_norm);
268: loc_it++;
269: fgmres->it = (loc_it-1); /* Add this here in case it has converged */
270:
271: PetscObjectTakeAccess(ksp);
272: ksp->its++;
273: ksp->rnorm = res_norm;
274: PetscObjectGrantAccess(ksp);
276: (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);
278: /* Catch error in happy breakdown and signal convergence and break from loop */
279: if (hapend) {
280: if (!ksp->reason) {
281: SETERRQ(0,"You reached the happy break down,but convergence was not indicated.");
282: }
283: break;
284: }
285: }
286: /* END OF ITERATION LOOP */
288: KSPLogResidualHistory(ksp,res_norm);
290: /*
291: Monitor if we know that we will not return for a restart */
292: if (ksp->reason || ksp->its >= max_it) {
293: KSPMonitor(ksp,ksp->its,res_norm);
294: }
296: if (itcount) *itcount = loc_it;
298: /*
299: Down here we have to solve for the "best" coefficients of the Krylov
300: columns, add the solution values together, and possibly unwind the
301: preconditioning from the solution
302: */
303:
304: /* Form the solution (or the solution so far) */
305: /* Note: must pass in (loc_it-1) for iteration count so that BuildFgmresSoln
306: properly navigates */
308: BuildFgmresSoln(RS(0),VEC_SOLN,VEC_SOLN,ksp,loc_it-1);
310: return(0);
311: }
313: /*
314: KSPSolve_FGMRES - This routine applies the FGMRES method.
317: Input Parameter:
318: . ksp - the Krylov space object that was set to use fgmres
320: Output Parameter:
321: . outits - number of iterations used
323: */
325: int KSPSolve_FGMRES(KSP ksp,int *outits)
326: {
327: int ierr;
328: int cycle_its; /* iterations done in a call to FGMREScycle */
329: int itcount; /* running total of iterations, incl. those in restarts */
330: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
331: PetscTruth diagonalscale;
334: ierr = PCDiagonalScale(ksp->B,&diagonalscale);
335: if (diagonalscale) SETERRQ1(1,"Krylov method %s does not support diagonal scaling",ksp->type_name);
337: PetscObjectTakeAccess(ksp);
338: ksp->its = 0;
339: PetscObjectGrantAccess(ksp);
341: /* initialize */
342: itcount = 0;
344: /* Compute the initial (NOT preconditioned) residual */
345: if (!ksp->guess_zero) {
346: FGMRESResidual(ksp);
347: } else { /* guess is 0 so residual is F (which is in VEC_RHS) */
348: VecCopy(VEC_RHS,VEC_VV(0));
349: }
350: /* now the residual is in VEC_VV(0) - which is what
351: FGMREScycle expects... */
352:
353: ierr = FGMREScycle(&cycle_its,ksp);
354: itcount += cycle_its;
355: while (!ksp->reason) {
356: ierr = FGMRESResidual(ksp);
357: if (itcount >= ksp->max_it) break;
358: ierr = FGMREScycle(&cycle_its,ksp);
359: itcount += cycle_its;
360: }
361: /* mark lack of convergence */
362: if (itcount >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
364: *outits = itcount;
365: return(0);
366: }
368: /*
370: KSPDestroy_FGMRES - Frees all memory space used by the Krylov method.
372: */
373: int KSPDestroy_FGMRES(KSP ksp)
374: {
375: KSP_FGMRES *fgmres = (KSP_FGMRES*)ksp->data;
376: int i,ierr;
379: /* Free the Hessenberg matrices */
380: if (fgmres->hh_origin) {PetscFree(fgmres->hh_origin);}
382: /* Free pointers to user variables */
383: if (fgmres->vecs) {PetscFree(fgmres->vecs);}
384: if (fgmres->prevecs) {PetscFree (fgmres->prevecs);}
386: /* free work vectors */
387: for (i=0; i < fgmres->nwork_alloc; i++) {
388: VecDestroyVecs(fgmres->user_work[i],fgmres->mwork_alloc[i]);
389: }
390: if (fgmres->user_work) {PetscFree(fgmres->user_work);}
392: for (i=0; i < fgmres->nwork_alloc; i++) {
393: VecDestroyVecs(fgmres->prevecs_user_work[i],fgmres->mwork_alloc[i]);
394: }
395: if (fgmres->prevecs_user_work) {PetscFree(fgmres->prevecs_user_work);}
397: if (fgmres->mwork_alloc) {PetscFree(fgmres->mwork_alloc);}
398: if (fgmres->nrs) {PetscFree(fgmres->nrs);}
399: if (fgmres->sol_temp) {VecDestroy(fgmres->sol_temp);}
400: if (fgmres->Rsvd) {PetscFree(fgmres->Rsvd);}
401: if (fgmres->Dsvd) {PetscFree(fgmres->Dsvd);}
402: if (fgmres->modifydestroy) {
403: (*fgmres->modifydestroy)(fgmres->modifyctx);
404: }
405: PetscFree(fgmres);
406: return(0);
407: }
409: /*
410: BuildFgmresSoln - create the solution from the starting vector and the
411: current iterates.
413: Input parameters:
414: nrs - work area of size it + 1.
415: vguess - index of initial guess
416: vdest - index of result. Note that vguess may == vdest (replace
417: guess with the solution).
418: it - HH upper triangular part is a block of size (it+1) x (it+1)
420: This is an internal routine that knows about the FGMRES internals.
421: */
422: static int BuildFgmresSoln(PetscScalar* nrs,Vec vguess,Vec vdest,KSP ksp,int it)
423: {
424: PetscScalar tt,zero = 0.0,one = 1.0;
425: int ierr,ii,k,j;
426: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
429: /* Solve for solution vector that minimizes the residual */
431: /* If it is < 0, no fgmres steps have been performed */
432: if (it < 0) {
433: if (vdest != vguess) {
434: VecCopy(vguess,vdest);
435: }
436: return(0);
437: }
439: /* so fgmres steps HAVE been performed */
441: /* solve the upper triangular system - RS is the right side and HH is
442: the upper triangular matrix - put soln in nrs */
443: nrs[it] = *RS(it) / *HH(it,it);
444: for (ii=1; ii<=it; ii++) {
445: k = it - ii;
446: tt = *RS(k);
447: for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
448: nrs[k] = tt / *HH(k,k);
449: }
451: /* Accumulate the correction to the soln of the preconditioned prob. in
452: VEC_TEMP - note that we use the preconditioned vectors */
453: VecSet(&zero,VEC_TEMP); /* set VEC_TEMP components to 0 */
454: VecMAXPY(it+1,nrs,VEC_TEMP,&PREVEC(0));
456: /* put updated solution into vdest.*/
457: if (vdest != vguess) {
458: VecCopy(VEC_TEMP,vdest);
459: VecAXPY(&one,vguess,vdest);
460: } else {/* replace guess with solution */
461: VecAXPY(&one,VEC_TEMP,vdest);
462: }
463: return(0);
464: }
466: /*
468: FGMRESUpdateHessenberg - Do the scalar work for the orthogonalization.
469: Return new residual.
471: input parameters:
473: . ksp - Krylov space object
474: . it - plane rotations are applied to the (it+1)th column of the
475: modified hessenberg (i.e. HH(:,it))
476: . hapend - PETSC_FALSE not happy breakdown ending.
478: output parameters:
479: . res - the new residual
480:
481: */
482: static int FGMRESUpdateHessenberg(KSP ksp,int it,PetscTruth hapend,PetscReal *res)
483: {
484: PetscScalar *hh,*cc,*ss,tt;
485: int j;
486: KSP_FGMRES *fgmres = (KSP_FGMRES *)(ksp->data);
489: hh = HH(0,it); /* pointer to beginning of column to update - so
490: incrementing hh "steps down" the (it+1)th col of HH*/
491: cc = CC(0); /* beginning of cosine rotations */
492: ss = SS(0); /* beginning of sine rotations */
494: /* Apply all the previously computed plane rotations to the new column
495: of the Hessenberg matrix */
496: /* Note: this uses the rotation [conj(c) s ; -s c], c= cos(theta), s= sin(theta),
497: and some refs have [c s ; -conj(s) c] (don't be confused!) */
499: for (j=1; j<=it; j++) {
500: tt = *hh;
501: #if defined(PETSC_USE_COMPLEX)
502: *hh = PetscConj(*cc) * tt + *ss * *(hh+1);
503: #else
504: *hh = *cc * tt + *ss * *(hh+1);
505: #endif
506: hh++;
507: *hh = *cc++ * *hh - (*ss++ * tt);
508: /* hh, cc, and ss have all been incremented one by end of loop */
509: }
511: /*
512: compute the new plane rotation, and apply it to:
513: 1) the right-hand-side of the Hessenberg system (RS)
514: note: it affects RS(it) and RS(it+1)
515: 2) the new column of the Hessenberg matrix
516: note: it affects HH(it,it) which is currently pointed to
517: by hh and HH(it+1, it) (*(hh+1))
518: thus obtaining the updated value of the residual...
519: */
521: /* compute new plane rotation */
523: if (!hapend) {
524: #if defined(PETSC_USE_COMPLEX)
525: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh+1)) * *(hh+1));
526: #else
527: tt = PetscSqrtScalar(*hh * *hh + *(hh+1) * *(hh+1));
528: #endif
529: if (tt == 0.0) {SETERRQ(PETSC_ERR_KSP_BRKDWN,"Your matrix or preconditioner is the null operator");}
530: *cc = *hh / tt; /* new cosine value */
531: *ss = *(hh+1) / tt; /* new sine value */
533: /* apply to 1) and 2) */
534: *RS(it+1) = - (*ss * *RS(it));
535: #if defined(PETSC_USE_COMPLEX)
536: *RS(it) = PetscConj(*cc) * *RS(it);
537: *hh = PetscConj(*cc) * *hh + *ss * *(hh+1);
538: #else
539: *RS(it) = *cc * *RS(it);
540: *hh = *cc * *hh + *ss * *(hh+1);
541: #endif
543: /* residual is the last element (it+1) of right-hand side! */
544: *res = PetscAbsScalar(*RS(it+1));
546: } else { /* happy breakdown: HH(it+1, it) = 0, therfore we don't need to apply
547: another rotation matrix (so RH doesn't change). The new residual is
548: always the new sine term times the residual from last time (RS(it)),
549: but now the new sine rotation would be zero...so the residual should
550: be zero...so we will multiply "zero" by the last residual. This might
551: not be exactly what we want to do here -could just return "zero". */
552:
553: *res = 0.0;
554: }
555: return(0);
556: }
558: /*
560: FGMRESGetNewVectors - This routine allocates more work vectors, starting from
561: VEC_VV(it), and more preconditioned work vectors, starting
562: from PREVEC(i).
564: */
565: static int FGMRESGetNewVectors(KSP ksp,int it)
566: {
567: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
568: int nwork = fgmres->nwork_alloc; /* number of work vector chunks allocated */
569: int nalloc; /* number to allocate */
570: int k,ierr;
571:
573: nalloc = fgmres->delta_allocate; /* number of vectors to allocate
574: in a single chunk */
576: /* Adjust the number to allocate to make sure that we don't exceed the
577: number of available slots (fgmres->vecs_allocated)*/
578: if (it + VEC_OFFSET + nalloc >= fgmres->vecs_allocated){
579: nalloc = fgmres->vecs_allocated - it - VEC_OFFSET;
580: }
581: if (!nalloc) return(0);
583: fgmres->vv_allocated += nalloc; /* vv_allocated is the number of vectors allocated */
585: /* work vectors */
586: VecDuplicateVecs(VEC_RHS,nalloc,&fgmres->user_work[nwork]);
587: PetscLogObjectParents(ksp,nalloc,fgmres->user_work[nwork]);
588: for (k=0; k < nalloc; k++) {
589: fgmres->vecs[it+VEC_OFFSET+k] = fgmres->user_work[nwork][k];
590: }
591: /* specify size of chunk allocated */
592: fgmres->mwork_alloc[nwork] = nalloc;
594: /* preconditioned vectors */
595: VecDuplicateVecs(VEC_RHS,nalloc,&fgmres->prevecs_user_work[nwork]);
596: PetscLogObjectParents(ksp,nalloc,fgmres->prevecs_user_work[nwork]);
597: for (k=0; k < nalloc; k++) {
598: fgmres->prevecs[it+VEC_OFFSET+k] = fgmres->prevecs_user_work[nwork][k];
599: }
601: /* increment the number of work vector chunks */
602: fgmres->nwork_alloc++;
603: return(0);
604: }
606: /*
608: KSPBuildSolution_FGMRES
610: Input Parameter:
611: . ksp - the Krylov space object
612: . ptr-
614: Output Parameter:
615: . result - the solution
617: Note: this calls BuildFgmresSoln - the same function that FGMREScycle
618: calls directly.
620: */
621: int KSPBuildSolution_FGMRES(KSP ksp,Vec ptr,Vec *result)
622: {
623: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
624: int ierr;
627: if (!ptr) {
628: if (!fgmres->sol_temp) {
629: VecDuplicate(ksp->vec_sol,&fgmres->sol_temp);
630: PetscLogObjectParent(ksp,fgmres->sol_temp);
631: }
632: ptr = fgmres->sol_temp;
633: }
634: if (!fgmres->nrs) {
635: /* allocate the work area */
636: PetscMalloc(fgmres->max_k*sizeof(PetscScalar),&fgmres->nrs);
637: PetscLogObjectMemory(ksp,fgmres->max_k*sizeof(PetscScalar));
638: }
639:
640: BuildFgmresSoln(fgmres->nrs,VEC_SOLN,ptr,ksp,fgmres->it);
641: *result = ptr;
642:
643: return(0);
644: }
646: /*
648: KSPView_FGMRES -Prints information about the current Krylov method
649: being used.
651: */
652: int KSPView_FGMRES(KSP ksp,PetscViewer viewer)
653: {
654: KSP_FGMRES *fgmres = (KSP_FGMRES *)ksp->data;
655: char *cstr;
656: int ierr;
657: PetscTruth isascii;
660: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
661: if (isascii) {
662: if (fgmres->orthog == KSPGMRESUnmodifiedGramSchmidtOrthogonalization) {
663: cstr = "Unmodified Gram-Schmidt Orthogonalization";
664: } else if (fgmres->orthog == KSPGMRESModifiedGramSchmidtOrthogonalization) {
665: cstr = "Modified Gram-Schmidt Orthogonalization";
666: } else if (fgmres->orthog == KSPGMRESIROrthogonalization) {
667: cstr = "Unmodified Gram-Schmidt + 1 step Iterative Refinement Orthogonalization";
668: } else {
669: cstr = "unknown orthogonalization";
670: }
671: PetscViewerASCIIPrintf(viewer," FGMRES: restart=%d, using %sn",fgmres->max_k,cstr);
672: } else {
673: SETERRQ(1,"Viewer type not supported for this object");
674: }
675: return(0);
676: }
678: int KSPSetFromOptions_FGMRES(KSP ksp)
679: {
680: int ierr,restart;
681: PetscReal haptol;
682: KSP_FGMRES *gmres = (KSP_FGMRES*)ksp->data;
683: PetscTruth flg;
686: PetscOptionsHead("KSP flexible GMRES Options");
687: PetscOptionsInt("-ksp_gmres_restart","Number of Krylov search directions","KSPGMRESSetRestart",gmres->max_k,&restart,&flg);
688: if (flg) { KSPGMRESSetRestart(ksp,restart); }
689: PetscOptionsReal("-ksp_gmres_haptol","Tolerance for declaring exact convergence (happy ending)","KSPGMRESSetHapTol",gmres->haptol,&haptol,&flg);
690: if (flg) { KSPGMRESSetHapTol(ksp,haptol); }
691: PetscOptionsName("-ksp_gmres_preallocate","Preallocate all Krylov vectors","KSPGMRESSetPreAllocateVectors",&flg);
692: if (flg) {KSPGMRESSetPreAllocateVectors(ksp);}
693: PetscOptionsLogicalGroupBegin("-ksp_gmres_unmodifiedgramschmidt","Use classical (unmodified) Gram-Schmidt (fast)","KSPGMRESSetOrthogonalization",&flg);
694: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESUnmodifiedGramSchmidtOrthogonalization);}
695: PetscOptionsLogicalGroup("-ksp_gmres_modifiedgramschmidt","Use modified Gram-Schmidt (slow but more stable)","KSPGMRESSetOrthogonalization",&flg);
696: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESModifiedGramSchmidtOrthogonalization);}
697: PetscOptionsLogicalGroupEnd("-ksp_gmres_irorthog","Use classical Gram-Schmidt with iterative refinement","KSPGMRESSetOrthogonalization",&flg);
698: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESIROrthogonalization);}
699: PetscOptionsName("-ksp_gmres_krylov_monitor","Graphically plot the Krylov directions","KSPSetMonitor",&flg);
700: if (flg) {
701: PetscViewers viewers;
702: PetscViewersCreate(ksp->comm,&viewers);
703: KSPSetMonitor(ksp,KSPGMRESKrylovMonitor,viewers,(int (*)(void*))PetscViewersDestroy);
704: }
705: PetscOptionsLogicalGroupBegin("-ksp_fgmres_modifypcnochange","do not vary the preconditioner","KSPFGMRESSetModifyPC",&flg);
706: if (flg) {KSPFGMRESSetModifyPC(ksp,KSPFGMRESModifyPCNoChange,0,0);}
707: PetscOptionsLogicalGroupEnd("-ksp_fgmres_modifypcsles","vary the SLES based preconditioner","KSPFGMRESSetModifyPC",&flg);
708: if (flg) {KSPFGMRESSetModifyPC(ksp,KSPFGMRESModifyPCSLES,0,0);}
709: PetscOptionsTail();
710: return(0);
711: }
713: EXTERN_C_BEGIN
714: int KSPFGMRESSetModifyPC_FGMRES(KSP ksp,int (*fcn)(KSP,int,int,PetscReal,void*),void *ctx,int (*d)(void*))
715: {
718: ((KSP_FGMRES *)ksp->data)->modifypc = fcn;
719: ((KSP_FGMRES *)ksp->data)->modifydestroy = d;
720: ((KSP_FGMRES *)ksp->data)->modifyctx = ctx;
721: return(0);
722: }
723: EXTERN_C_END
725: EXTERN_C_BEGIN
726: EXTERN int KSPGMRESSetPreAllocateVectors_GMRES(KSP);
727: EXTERN int KSPGMRESSetRestart_GMRES(KSP,int);
728: EXTERN int KSPGMRESSetOrthogonalization_GMRES(KSP,int (*)(KSP,int));
729: EXTERN_C_END
731: EXTERN_C_BEGIN
732: int KSPCreate_FGMRES(KSP ksp)
733: {
734: KSP_FGMRES *fgmres;
735: int ierr;
738: PetscNew(KSP_FGMRES,&fgmres);
739: PetscMemzero(fgmres,sizeof(KSP_FGMRES));
740: PetscLogObjectMemory(ksp,sizeof(KSP_FGMRES));
741: ksp->data = (void*)fgmres;
742: ksp->ops->buildsolution = KSPBuildSolution_FGMRES;
744: ksp->ops->setup = KSPSetUp_FGMRES;
745: ksp->ops->solve = KSPSolve_FGMRES;
746: ksp->ops->destroy = KSPDestroy_FGMRES;
747: ksp->ops->view = KSPView_FGMRES;
748: ksp->ops->setfromoptions = KSPSetFromOptions_FGMRES;
749: ksp->ops->computeextremesingularvalues = 0;
750: ksp->ops->computeeigenvalues = 0;
752: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",
753: "KSPGMRESSetPreAllocateVectors_GMRES",
754: KSPGMRESSetPreAllocateVectors_GMRES);
755: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C",
756: "KSPGMRESSetOrthogonalization_GMRES",
757: KSPGMRESSetOrthogonalization_GMRES);
758: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetRestart_C",
759: "KSPGMRESSetRestart_GMRES",
760: KSPGMRESSetRestart_GMRES);
761: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPFGMRESSetModifyPC_C",
762: "KSPFGMRESSetModifyPC_FGMRES",
763: KSPFGMRESSetModifyPC_FGMRES);
766: fgmres->haptol = 1.0e-30;
767: fgmres->q_preallocate = 0;
768: fgmres->delta_allocate = FGMRES_DELTA_DIRECTIONS;
769: fgmres->orthog = KSPGMRESIROrthogonalization;
770: fgmres->nrs = 0;
771: fgmres->sol_temp = 0;
772: fgmres->max_k = FGMRES_DEFAULT_MAXK;
773: fgmres->Rsvd = 0;
774: fgmres->modifypc = KSPFGMRESModifyPCNoChange;
775: fgmres->modifyctx = PETSC_NULL;
776: fgmres->modifydestroy = PETSC_NULL;
778: return(0);
779: }
780: EXTERN_C_END