Actual source code: gmres.c
1: /*$Id: gmres.c,v 1.176 2001/08/07 03:03:51 balay Exp $*/
3: /*
4: This file implements GMRES (a Generalized Minimal Residual) method.
5: Reference: Saad and Schultz, 1986.
8: Some comments on left vs. right preconditioning, and restarts.
9: Left and right preconditioning.
10: If right preconditioning is chosen, then the problem being solved
11: by gmres is actually
12: My = AB^-1 y = f
13: so the initial residual is
14: r = f - Mx
15: Note that B^-1 y = x or y = B x, and if x is non-zero, the initial
16: residual is
17: r = f - A x
18: The final solution is then
19: x = B^-1 y
21: If left preconditioning is chosen, then the problem being solved is
22: My = B^-1 A x = B^-1 f,
23: and the initial residual is
24: r = B^-1(f - Ax)
26: Restarts: Restarts are basically solves with x0 not equal to zero.
27: Note that we can eliminate an extra application of B^-1 between
28: restarts as long as we don't require that the solution at the end
29: of an unsuccessful gmres iteration always be the solution x.
30: */
32: #include src/sles/ksp/impls/gmres/gmresp.h
33: #define GMRES_DELTA_DIRECTIONS 10
34: #define GMRES_DEFAULT_MAXK 30
35: static int GMRESGetNewVectors(KSP,int);
36: static int GMRESUpdateHessenberg(KSP,int,PetscTruth,PetscReal*);
37: static int BuildGmresSoln(PetscScalar*,Vec,Vec,KSP,int);
41: int KSPSetUp_GMRES(KSP ksp)
42: {
43: unsigned int size,hh,hes,rs,cc;
44: int ierr,max_k,k;
45: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
48: if (ksp->pc_side == PC_SYMMETRIC) {
49: SETERRQ(2,"no symmetric preconditioning for KSPGMRES");
50: }
52: max_k = gmres->max_k;
53: hh = (max_k + 2) * (max_k + 1);
54: hes = (max_k + 1) * (max_k + 1);
55: rs = (max_k + 2);
56: cc = (max_k + 1);
57: size = (hh + hes + rs + 2*cc) * sizeof(PetscScalar);
59: PetscMalloc(size,&gmres->hh_origin);
60: PetscMemzero(gmres->hh_origin,size);
61: PetscLogObjectMemory(ksp,size);
62: gmres->hes_origin = gmres->hh_origin + hh;
63: gmres->rs_origin = gmres->hes_origin + hes;
64: gmres->cc_origin = gmres->rs_origin + rs;
65: gmres->ss_origin = gmres->cc_origin + cc;
67: if (ksp->calc_sings) {
68: /* Allocate workspace to hold Hessenberg matrix needed by Eispack */
69: size = (max_k + 3)*(max_k + 9)*sizeof(PetscScalar);
70: PetscMalloc(size,&gmres->Rsvd);
71: PetscMalloc(5*(max_k+2)*sizeof(PetscReal),&gmres->Dsvd);
72: PetscLogObjectMemory(ksp,size+5*(max_k+2)*sizeof(PetscReal));
73: }
75: /* Allocate array to hold pointers to user vectors. Note that we need
76: 4 + max_k + 1 (since we need it+1 vectors, and it <= max_k) */
77: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void *),&gmres->vecs);
78: gmres->vecs_allocated = VEC_OFFSET + 2 + max_k;
79: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void *),&gmres->user_work);
80: PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(int),&gmres->mwork_alloc);
81: PetscLogObjectMemory(ksp,(VEC_OFFSET+2+max_k)*(2*sizeof(void *)+sizeof(int)));
83: if (gmres->q_preallocate) {
84: gmres->vv_allocated = VEC_OFFSET + 2 + max_k;
85: VecDuplicateVecs(VEC_RHS,gmres->vv_allocated,&gmres->user_work[0]);
86: PetscLogObjectParents(ksp,gmres->vv_allocated,gmres->user_work[0]);
87: gmres->mwork_alloc[0] = gmres->vv_allocated;
88: gmres->nwork_alloc = 1;
89: for (k=0; k<gmres->vv_allocated; k++) {
90: gmres->vecs[k] = gmres->user_work[0][k];
91: }
92: } else {
93: gmres->vv_allocated = 5;
94: VecDuplicateVecs(ksp->vec_rhs,5,&gmres->user_work[0]);
95: PetscLogObjectParents(ksp,5,gmres->user_work[0]);
96: gmres->mwork_alloc[0] = 5;
97: gmres->nwork_alloc = 1;
98: for (k=0; k<gmres->vv_allocated; k++) {
99: gmres->vecs[k] = gmres->user_work[0][k];
100: }
101: }
102: return(0);
103: }
105: /*
106: Run gmres, possibly with restart. Return residual history if requested.
107: input parameters:
109: . gmres - structure containing parameters and work areas
111: output parameters:
112: . nres - residuals (from preconditioned system) at each step.
113: If restarting, consider passing nres+it. If null,
114: ignored
115: . itcount - number of iterations used. nres[0] to nres[itcount]
116: are defined. If null, ignored.
117:
118: Notes:
119: On entry, the value in vector VEC_VV(0) should be the initial residual
120: (this allows shortcuts where the initial preconditioned residual is 0).
121: */
124: int GMREScycle(int *itcount,KSP ksp)
125: {
126: KSP_GMRES *gmres = (KSP_GMRES *)(ksp->data);
127: PetscReal res_norm,res,hapbnd,tt;
128: int ierr,it = 0, max_k = gmres->max_k,max_it = ksp->max_it;
129: PetscTruth hapend = PETSC_FALSE;
132: VecNormalize(VEC_VV(0),&res_norm);
133: res = res_norm;
134: *GRS(0) = res_norm;
136: /* check for the convergence */
137: if (!res) {
138: if (itcount) *itcount = 0;
139: ksp->reason = KSP_CONVERGED_ATOL;
140: PetscLogInfo(ksp,"GMRESCycle: Converged due to zero residual norm on entry\n");
141: return(0);
142: }
144: PetscObjectTakeAccess(ksp);
145: ksp->rnorm = res;
146: PetscObjectGrantAccess(ksp);
147: gmres->it = (it - 1);
148: (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);
149: while (!ksp->reason && it < max_k && ksp->its < max_it) {
150: KSPLogResidualHistory(ksp,res);
151: gmres->it = (it - 1);
152: KSPMonitor(ksp,ksp->its,res);
153: if (gmres->vv_allocated <= it + VEC_OFFSET + 1) {
154: GMRESGetNewVectors(ksp,it+1);
155: }
156: KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,VEC_VV(it),VEC_VV(1+it),VEC_TEMP_MATOP);
158: /* update hessenberg matrix and do Gram-Schmidt */
159: (*gmres->orthog)(ksp,it);
161: /* vv(i+1) . vv(i+1) */
162: VecNormalize(VEC_VV(it+1),&tt);
163: /* save the magnitude */
164: *HH(it+1,it) = tt;
165: *HES(it+1,it) = tt;
167: /* check for the happy breakdown */
168: hapbnd = PetscAbsScalar(tt / *GRS(it));
169: if (hapbnd > gmres->haptol) hapbnd = gmres->haptol;
170: if (tt < hapbnd) {
171: PetscLogInfo(ksp,"Detected happy breakdown, current hapbnd = %g tt = %g\n",hapbnd,tt);
172: hapend = PETSC_TRUE;
173: }
174: GMRESUpdateHessenberg(ksp,it,hapend,&res);
175: it++;
176: gmres->it = (it-1); /* For converged */
177: PetscObjectTakeAccess(ksp);
178: ksp->its++;
179: ksp->rnorm = res;
180: PetscObjectGrantAccess(ksp);
182: (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);
184: /* Catch error in happy breakdown and signal convergence and break from loop */
185: if (hapend) {
186: if (!ksp->reason) {
187: SETERRQ1(0,"You reached the happy break down, but convergence was not indicated. Residual norm = %g",res);
188: }
189: break;
190: }
191: }
193: /* Monitor if we know that we will not return for a restart */
194: if (ksp->reason || ksp->its >= max_it) {
195: KSPLogResidualHistory(ksp,res);
196: KSPMonitor(ksp,ksp->its,res);
197: }
199: if (itcount) *itcount = it;
202: /*
203: Down here we have to solve for the "best" coefficients of the Krylov
204: columns, add the solution values together, and possibly unwind the
205: preconditioning from the solution
206: */
207: /* Form the solution (or the solution so far) */
208: BuildGmresSoln(GRS(0),VEC_SOLN,VEC_SOLN,ksp,it-1);
210: return(0);
211: }
215: int KSPSolve_GMRES(KSP ksp,int *outits)
216: {
217: int ierr,its,itcount;
218: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
219: PetscTruth guess_zero = ksp->guess_zero;
222: if (ksp->calc_sings && !gmres->Rsvd) {
223: SETERRQ(1,"Must call KSPSetComputeSingularValues() before KSPSetUp() is called");
224: }
226: PetscObjectTakeAccess(ksp);
227: ksp->its = 0;
228: PetscObjectGrantAccess(ksp);
230: itcount = 0;
231: ksp->reason = KSP_CONVERGED_ITERATING;
232: while (!ksp->reason) {
233: KSPInitialResidual(ksp,VEC_SOLN,VEC_TEMP,VEC_TEMP_MATOP,VEC_VV(0),VEC_RHS);
234: GMREScycle(&its,ksp);
235: itcount += its;
236: if (itcount >= ksp->max_it) {
237: ksp->reason = KSP_DIVERGED_ITS;
238: break;
239: }
240: ksp->guess_zero = PETSC_FALSE; /* every future call to KSPInitialResidual() will have nonzero guess */
241: }
242: ksp->guess_zero = guess_zero; /* restore if user provided nonzero initial guess */
243: if (outits) *outits = itcount;
244: return(0);
245: }
249: int KSPDestroy_GMRES_Internal(KSP ksp)
250: {
251: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
252: int i,ierr;
255: /* Free the Hessenberg matrix */
256: if (gmres->hh_origin) {PetscFree(gmres->hh_origin);}
258: /* Free the pointer to user variables */
259: if (gmres->vecs) {PetscFree(gmres->vecs);}
261: /* free work vectors */
262: for (i=0; i<gmres->nwork_alloc; i++) {
263: VecDestroyVecs(gmres->user_work[i],gmres->mwork_alloc[i]);
264: }
265: if (gmres->user_work) {PetscFree(gmres->user_work);}
266: if (gmres->mwork_alloc) {PetscFree(gmres->mwork_alloc);}
267: if (gmres->nrs) {PetscFree(gmres->nrs);}
268: if (gmres->sol_temp) {VecDestroy(gmres->sol_temp);}
269: if (gmres->Rsvd) {PetscFree(gmres->Rsvd);}
270: if (gmres->Dsvd) {PetscFree(gmres->Dsvd);}
272: return(0);
273: }
277: int KSPDestroy_GMRES(KSP ksp)
278: {
279: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
280: int ierr;
283: KSPDestroy_GMRES_Internal(ksp);
284: PetscFree(gmres);
285: return(0);
286: }
287: /*
288: BuildGmresSoln - create the solution from the starting vector and the
289: current iterates.
291: Input parameters:
292: nrs - work area of size it + 1.
293: vs - index of initial guess
294: vdest - index of result. Note that vs may == vdest (replace
295: guess with the solution).
297: This is an internal routine that knows about the GMRES internals.
298: */
301: static int BuildGmresSoln(PetscScalar* nrs,Vec vs,Vec vdest,KSP ksp,int it)
302: {
303: PetscScalar tt,zero = 0.0,one = 1.0;
304: int ierr,ii,k,j;
305: KSP_GMRES *gmres = (KSP_GMRES *)(ksp->data);
308: /* Solve for solution vector that minimizes the residual */
310: /* If it is < 0, no gmres steps have been performed */
311: if (it < 0) {
312: if (vdest != vs) {
313: VecCopy(vs,vdest);
314: }
315: return(0);
316: }
317: if (*HH(it,it) == 0.0) SETERRQ2(1,"HH(it,it) is identically zero; it = %d GRS(it) = %g",it,PetscAbsScalar(*GRS(it)));
318: if (*HH(it,it) != 0.0) {
319: nrs[it] = *GRS(it) / *HH(it,it);
320: } else {
321: nrs[it] = 0.0;
322: }
323: for (ii=1; ii<=it; ii++) {
324: k = it - ii;
325: tt = *GRS(k);
326: for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
327: nrs[k] = tt / *HH(k,k);
328: }
330: /* Accumulate the correction to the solution of the preconditioned problem in TEMP */
331: VecSet(&zero,VEC_TEMP);
332: VecMAXPY(it+1,nrs,VEC_TEMP,&VEC_VV(0));
334: KSPUnwindPreconditioner(ksp,VEC_TEMP,VEC_TEMP_MATOP);
335: /* add solution to previous solution */
336: if (vdest != vs) {
337: VecCopy(vs,vdest);
338: }
339: VecAXPY(&one,VEC_TEMP,vdest);
340: return(0);
341: }
342: /*
343: Do the scalar work for the orthogonalization. Return new residual.
344: */
347: static int GMRESUpdateHessenberg(KSP ksp,int it,PetscTruth hapend,PetscReal *res)
348: {
349: PetscScalar *hh,*cc,*ss,tt;
350: int j;
351: KSP_GMRES *gmres = (KSP_GMRES *)(ksp->data);
354: hh = HH(0,it);
355: cc = CC(0);
356: ss = SS(0);
358: /* Apply all the previously computed plane rotations to the new column
359: of the Hessenberg matrix */
360: for (j=1; j<=it; j++) {
361: tt = *hh;
362: #if defined(PETSC_USE_COMPLEX)
363: *hh = PetscConj(*cc) * tt + *ss * *(hh+1);
364: #else
365: *hh = *cc * tt + *ss * *(hh+1);
366: #endif
367: hh++;
368: *hh = *cc++ * *hh - (*ss++ * tt);
369: }
371: /*
372: compute the new plane rotation, and apply it to:
373: 1) the right-hand-side of the Hessenberg system
374: 2) the new column of the Hessenberg matrix
375: thus obtaining the updated value of the residual
376: */
377: if (!hapend) {
378: #if defined(PETSC_USE_COMPLEX)
379: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh+1)) * *(hh+1));
380: #else
381: tt = PetscSqrtScalar(*hh * *hh + *(hh+1) * *(hh+1));
382: #endif
383: if (tt == 0.0) {SETERRQ(PETSC_ERR_KSP_BRKDWN,"Your matrix or preconditioner is the null operator");}
384: *cc = *hh / tt;
385: *ss = *(hh+1) / tt;
386: *GRS(it+1) = - (*ss * *GRS(it));
387: #if defined(PETSC_USE_COMPLEX)
388: *GRS(it) = PetscConj(*cc) * *GRS(it);
389: *hh = PetscConj(*cc) * *hh + *ss * *(hh+1);
390: #else
391: *GRS(it) = *cc * *GRS(it);
392: *hh = *cc * *hh + *ss * *(hh+1);
393: #endif
394: *res = PetscAbsScalar(*GRS(it+1));
395: } else {
396: /* happy breakdown: HH(it+1, it) = 0, therfore we don't need to apply
397: another rotation matrix (so RH doesn't change). The new residual is
398: always the new sine term times the residual from last time (GRS(it)),
399: but now the new sine rotation would be zero...so the residual should
400: be zero...so we will multiply "zero" by the last residual. This might
401: not be exactly what we want to do here -could just return "zero". */
402:
403: *res = 0.0;
404: }
405: return(0);
406: }
407: /*
408: This routine allocates more work vectors, starting from VEC_VV(it).
409: */
412: static int GMRESGetNewVectors(KSP ksp,int it)
413: {
414: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
415: int nwork = gmres->nwork_alloc,k,nalloc,ierr;
418: nalloc = gmres->delta_allocate;
419: /* Adjust the number to allocate to make sure that we don't exceed the
420: number of available slots */
421: if (it + VEC_OFFSET + nalloc >= gmres->vecs_allocated){
422: nalloc = gmres->vecs_allocated - it - VEC_OFFSET;
423: }
424: if (!nalloc) return(0);
426: gmres->vv_allocated += nalloc;
427: VecDuplicateVecs(ksp->vec_rhs,nalloc,&gmres->user_work[nwork]);
428: PetscLogObjectParents(ksp,nalloc,gmres->user_work[nwork]);
429: gmres->mwork_alloc[nwork] = nalloc;
430: for (k=0; k<nalloc; k++) {
431: gmres->vecs[it+VEC_OFFSET+k] = gmres->user_work[nwork][k];
432: }
433: gmres->nwork_alloc++;
434: return(0);
435: }
439: int KSPBuildSolution_GMRES(KSP ksp,Vec ptr,Vec *result)
440: {
441: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
442: int ierr;
445: if (!ptr) {
446: if (!gmres->sol_temp) {
447: VecDuplicate(ksp->vec_sol,&gmres->sol_temp);
448: PetscLogObjectParent(ksp,gmres->sol_temp);
449: }
450: ptr = gmres->sol_temp;
451: }
452: if (!gmres->nrs) {
453: /* allocate the work area */
454: PetscMalloc(gmres->max_k*sizeof(PetscScalar),&gmres->nrs);
455: PetscLogObjectMemory(ksp,gmres->max_k*sizeof(PetscScalar));
456: }
458: BuildGmresSoln(gmres->nrs,VEC_SOLN,ptr,ksp,gmres->it);
459: *result = ptr;
460: return(0);
461: }
465: int KSPView_GMRES(KSP ksp,PetscViewer viewer)
466: {
467: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
468: char *cstr;
469: int ierr;
470: PetscTruth isascii,isstring;
473: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
474: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
475: if (gmres->orthog == KSPGMRESUnmodifiedGramSchmidtOrthogonalization) {
476: cstr = "Unmodified Gram-Schmidt Orthogonalization";
477: } else if (gmres->orthog == KSPGMRESModifiedGramSchmidtOrthogonalization) {
478: cstr = "Modified Gram-Schmidt Orthogonalization";
479: } else if (gmres->orthog == KSPGMRESIROrthogonalization) {
480: cstr = "Unmodified Gram-Schmidt + 1 step Iterative Refinement Orthogonalization";
481: } else {
482: cstr = "unknown orthogonalization";
483: }
484: if (isascii) {
485: PetscViewerASCIIPrintf(viewer," GMRES: restart=%d, using %s\n",gmres->max_k,cstr);
486: PetscViewerASCIIPrintf(viewer," GMRES: happy breakdown tolerance %g\n",gmres->haptol);
487: } else if (isstring) {
488: PetscViewerStringSPrintf(viewer,"%s restart %d",cstr,gmres->max_k);
489: } else {
490: SETERRQ1(1,"Viewer type %s not supported for KSP GMRES",((PetscObject)viewer)->type_name);
491: }
492: return(0);
493: }
497: /*@C
498: KSPGMRESKrylovMonitor - Calls VecView() for each direction in the
499: GMRES accumulated Krylov space.
501: Collective on KSP
503: Input Parameters:
504: + ksp - the KSP context
505: . its - iteration number
506: . fgnorm - 2-norm of residual (or gradient)
507: - a viewers object created with PetscViewersCreate()
509: Level: intermediate
511: .keywords: KSP, nonlinear, vector, monitor, view, Krylov space
513: .seealso: KSPSetMonitor(), KSPDefaultMonitor(), VecView(), PetscViewersCreate(), PetscViewersDestroy()
514: @*/
515: int KSPGMRESKrylovMonitor(KSP ksp,int its,PetscReal fgnorm,void *dummy)
516: {
517: PetscViewers viewers = (PetscViewers)dummy;
518: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
519: int ierr;
520: Vec x;
521: PetscViewer viewer;
524: PetscViewersGetViewer(viewers,gmres->it+1,&viewer);
525: PetscViewerSetType(viewer,PETSC_VIEWER_DRAW);
527: x = VEC_VV(gmres->it+1);
528: VecView(x,viewer);
530: return(0);
531: }
535: int KSPSetFromOptions_GMRES(KSP ksp)
536: {
537: int ierr,restart;
538: PetscReal haptol;
539: KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;
540: PetscTruth flg;
543: PetscOptionsHead("KSP GMRES Options");
544: PetscOptionsInt("-ksp_gmres_restart","Number of Krylov search directions","KSPGMRESSetRestart",gmres->max_k,&restart,&flg);
545: if (flg) { KSPGMRESSetRestart(ksp,restart); }
546: PetscOptionsReal("-ksp_gmres_haptol","Tolerance for exact convergence (happy ending)","KSPGMRESSetHapTol",gmres->haptol,&haptol,&flg);
547: if (flg) { KSPGMRESSetHapTol(ksp,haptol); }
548: PetscOptionsName("-ksp_gmres_preallocate","Preallocate Krylov vectors","KSPGMRESSetPreAllocateVectors",&flg);
549: if (flg) {KSPGMRESSetPreAllocateVectors(ksp);}
550: PetscOptionsLogicalGroupBegin("-ksp_gmres_unmodifiedgramschmidt","Classical (unmodified) Gram-Schmidt (fast)","KSPGMRESSetOrthogonalization",&flg);
551: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESUnmodifiedGramSchmidtOrthogonalization);}
552: PetscOptionsLogicalGroup("-ksp_gmres_modifiedgramschmidt","Modified Gram-Schmidt (slow,more stable)","KSPGMRESSetOrthogonalization",&flg);
553: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESModifiedGramSchmidtOrthogonalization);}
554: PetscOptionsLogicalGroupEnd("-ksp_gmres_irorthog","Classical Gram-Schmidt + iterative refinement","KSPGMRESSetOrthogonalization",&flg);
555: if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESIROrthogonalization);}
556: PetscOptionsName("-ksp_gmres_krylov_monitor","Plot the Krylov directions","KSPSetMonitor",&flg);
557: if (flg) {
558: PetscViewers viewers;
559: PetscViewersCreate(ksp->comm,&viewers);
560: KSPSetMonitor(ksp,KSPGMRESKrylovMonitor,viewers,(int (*)(void*))PetscViewersDestroy);
561: }
562: PetscOptionsTail();
563: return(0);
564: }
566: EXTERN int KSPComputeExtremeSingularValues_GMRES(KSP,PetscReal *,PetscReal *);
567: EXTERN int KSPComputeEigenvalues_GMRES(KSP,int,PetscReal *,PetscReal *,int *);
570: EXTERN_C_BEGIN
573: int KSPGMRESSetHapTol_GMRES(KSP ksp,double tol)
574: {
575: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
578: if (tol < 0.0) SETERRQ(1,"Tolerance must be non-negative");
579: gmres->haptol = tol;
580: return(0);
581: }
582: EXTERN_C_END
584: EXTERN_C_BEGIN
587: int KSPGMRESSetRestart_GMRES(KSP ksp,int max_k)
588: {
589: KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;
590: int ierr;
593: if (max_k < 1) SETERRQ(1,"Restart must be positive");
594: if (!ksp->setupcalled) {
595: gmres->max_k = max_k;
596: } else if (gmres->max_k != max_k) {
597: gmres->max_k = max_k;
598: ksp->setupcalled = 0;
599: /* free the data structures, then create them again */
600: KSPDestroy_GMRES_Internal(ksp);
601: }
603: return(0);
604: }
605: EXTERN_C_END
607: EXTERN_C_BEGIN
610: int KSPGMRESSetOrthogonalization_GMRES(KSP ksp,int (*fcn)(KSP,int))
611: {
614: ((KSP_GMRES *)ksp->data)->orthog = fcn;
615: return(0);
616: }
617: EXTERN_C_END
619: EXTERN_C_BEGIN
622: int KSPGMRESSetPreAllocateVectors_GMRES(KSP ksp)
623: {
624: KSP_GMRES *gmres;
627: gmres = (KSP_GMRES *)ksp->data;
628: gmres->q_preallocate = 1;
629: return(0);
630: }
631: EXTERN_C_END
633: EXTERN_C_BEGIN
636: int KSPCreate_GMRES(KSP ksp)
637: {
638: KSP_GMRES *gmres;
639: int ierr;
642: PetscNew(KSP_GMRES,&gmres);
643: PetscMemzero(gmres,sizeof(KSP_GMRES));
644: PetscLogObjectMemory(ksp,sizeof(KSP_GMRES));
645: ksp->data = (void*)gmres;
646: ksp->ops->buildsolution = KSPBuildSolution_GMRES;
648: ksp->ops->setup = KSPSetUp_GMRES;
649: ksp->ops->solve = KSPSolve_GMRES;
650: ksp->ops->destroy = KSPDestroy_GMRES;
651: ksp->ops->view = KSPView_GMRES;
652: ksp->ops->setfromoptions = KSPSetFromOptions_GMRES;
653: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
654: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
656: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",
657: "KSPGMRESSetPreAllocateVectors_GMRES",
658: KSPGMRESSetPreAllocateVectors_GMRES);
659: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C",
660: "KSPGMRESSetOrthogonalization_GMRES",
661: KSPGMRESSetOrthogonalization_GMRES);
662: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetRestart_C",
663: "KSPGMRESSetRestart_GMRES",
664: KSPGMRESSetRestart_GMRES);
665: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetHapTol_C",
666: "KSPGMRESSetHapTol_GMRES",
667: KSPGMRESSetHapTol_GMRES);
669: gmres->haptol = 1.0e-30;
670: gmres->q_preallocate = 0;
671: gmres->delta_allocate = GMRES_DELTA_DIRECTIONS;
672: gmres->orthog = KSPGMRESUnmodifiedGramSchmidtOrthogonalization;
673: gmres->nrs = 0;
674: gmres->sol_temp = 0;
675: gmres->max_k = GMRES_DEFAULT_MAXK;
676: gmres->Rsvd = 0;
677: return(0);
678: }
679: EXTERN_C_END