Actual source code: pipefgmres.c
1: /*
2: Contributed by Patrick Sanan and Sascha M. Schnepp
3: */
5: #include <../src/ksp/ksp/impls/gmres/pipefgmres/pipefgmresimpl.h>
7: static PetscBool cited = PETSC_FALSE;
8: static const char citation[] =
9: "@article{SSM2016,\n"
10: " author = {P. Sanan and S.M. Schnepp and D.A. May},\n"
11: " title = {Pipelined, Flexible Krylov Subspace Methods},\n"
12: " journal = {SIAM Journal on Scientific Computing},\n"
13: " volume = {38},\n"
14: " number = {5},\n"
15: " pages = {C441-C470},\n"
16: " year = {2016},\n"
17: " doi = {10.1137/15M1049130},\n"
18: " URL = {http://dx.doi.org/10.1137/15M1049130},\n"
19: " eprint = {http://dx.doi.org/10.1137/15M1049130}\n"
20: "}\n";
22: #define PIPEFGMRES_DELTA_DIRECTIONS 10
23: #define PIPEFGMRES_DEFAULT_MAXK 30
25: static PetscErrorCode KSPPIPEFGMRESGetNewVectors(KSP,PetscInt);
26: static PetscErrorCode KSPPIPEFGMRESUpdateHessenberg(KSP,PetscInt,PetscBool*,PetscReal*);
27: static PetscErrorCode KSPPIPEFGMRESBuildSoln(PetscScalar*,Vec,Vec,KSP,PetscInt);
28: extern PetscErrorCode KSPReset_PIPEFGMRES(KSP);
30: /*
32: KSPSetUp_PIPEFGMRES - Sets up the workspace needed by pipefgmres.
34: This is called once, usually automatically by KSPSolve() or KSPSetUp(),
35: but can be called directly by KSPSetUp().
37: */
38: static PetscErrorCode KSPSetUp_PIPEFGMRES(KSP ksp)
39: {
40: PetscInt k;
41: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES*)ksp->data;
42: const PetscInt max_k = pipefgmres->max_k;
44: KSPSetUp_GMRES(ksp);
46: PetscMalloc1((VEC_OFFSET+max_k),&pipefgmres->prevecs);
47: PetscMalloc1((VEC_OFFSET+max_k),&pipefgmres->prevecs_user_work);
48: PetscLogObjectMemory((PetscObject)ksp,(VEC_OFFSET+max_k)*(2*sizeof(void*)));
50: KSPCreateVecs(ksp,pipefgmres->vv_allocated,&pipefgmres->prevecs_user_work[0],0,NULL);
51: PetscLogObjectParents(ksp,pipefgmres->vv_allocated,pipefgmres->prevecs_user_work[0]);
52: for (k=0; k < pipefgmres->vv_allocated; k++) {
53: pipefgmres->prevecs[k] = pipefgmres->prevecs_user_work[0][k];
54: }
56: PetscMalloc1((VEC_OFFSET+max_k),&pipefgmres->zvecs);
57: PetscMalloc1((VEC_OFFSET+max_k),&pipefgmres->zvecs_user_work);
58: PetscLogObjectMemory((PetscObject)ksp,(VEC_OFFSET+max_k)*(2*sizeof(void*)));
60: PetscMalloc1((VEC_OFFSET+max_k),&pipefgmres->redux);
61: PetscLogObjectMemory((PetscObject)ksp,(VEC_OFFSET+max_k)*(sizeof(void*)));
63: KSPCreateVecs(ksp,pipefgmres->vv_allocated,&pipefgmres->zvecs_user_work[0],0,NULL);
64: PetscLogObjectParents(ksp,pipefgmres->vv_allocated,pipefgmres->zvecs_user_work[0]);
65: for (k=0; k < pipefgmres->vv_allocated; k++) {
66: pipefgmres->zvecs[k] = pipefgmres->zvecs_user_work[0][k];
67: }
69: return 0;
70: }
72: /*
74: KSPPIPEFGMRESCycle - Run pipefgmres, possibly with restart. Return residual
75: history if requested.
77: input parameters:
78: . pipefgmres - structure containing parameters and work areas
80: output parameters:
81: . itcount - number of iterations used. If null, ignored.
82: . converged - 0 if not converged
84: Notes:
85: On entry, the value in vector VEC_VV(0) should be
86: the initial residual.
88: */
89: static PetscErrorCode KSPPIPEFGMRESCycle(PetscInt *itcount,KSP ksp)
90: {
91: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES*)(ksp->data);
92: PetscReal res_norm;
93: PetscReal hapbnd,tt;
94: PetscScalar *hh,*hes,*lhh,shift = pipefgmres->shift;
95: PetscBool hapend = PETSC_FALSE; /* indicates happy breakdown ending */
96: PetscInt loc_it; /* local count of # of dir. in Krylov space */
97: PetscInt max_k = pipefgmres->max_k; /* max # of directions Krylov space */
98: PetscInt i,j,k;
99: Mat Amat,Pmat;
100: Vec Q,W; /* Pipelining vectors */
101: Vec *redux = pipefgmres->redux; /* workspace for single reduction */
103: if (itcount) *itcount = 0;
105: /* Assign simpler names to these vectors, allocated as pipelining workspace */
106: Q = VEC_Q;
107: W = VEC_W;
109: /* Allocate memory for orthogonalization work (freed in the GMRES Destroy routine)*/
110: /* Note that we add an extra value here to allow for a single reduction */
111: if (!pipefgmres->orthogwork) { PetscMalloc1(pipefgmres->max_k + 2 ,&pipefgmres->orthogwork);
112: }
113: lhh = pipefgmres->orthogwork;
115: /* Number of pseudo iterations since last restart is the number
116: of prestart directions */
117: loc_it = 0;
119: /* note: (pipefgmres->it) is always set one less than (loc_it) It is used in
120: KSPBUILDSolution_PIPEFGMRES, where it is passed to KSPPIPEFGMRESBuildSoln.
121: Note that when KSPPIPEFGMRESBuildSoln is called from this function,
122: (loc_it -1) is passed, so the two are equivalent */
123: pipefgmres->it = (loc_it - 1);
125: /* initial residual is in VEC_VV(0) - compute its norm*/
126: VecNorm(VEC_VV(0),NORM_2,&res_norm);
128: /* first entry in right-hand-side of hessenberg system is just
129: the initial residual norm */
130: *RS(0) = res_norm;
132: PetscObjectSAWsTakeAccess((PetscObject)ksp);
133: if (ksp->normtype != KSP_NORM_NONE) ksp->rnorm = res_norm;
134: else ksp->rnorm = 0;
135: PetscObjectSAWsGrantAccess((PetscObject)ksp);
136: KSPLogResidualHistory(ksp,ksp->rnorm);
137: KSPMonitor(ksp,ksp->its,ksp->rnorm);
139: /* check for the convergence - maybe the current guess is good enough */
140: (*ksp->converged)(ksp,ksp->its,ksp->rnorm,&ksp->reason,ksp->cnvP);
141: if (ksp->reason) {
142: if (itcount) *itcount = 0;
143: return 0;
144: }
146: /* scale VEC_VV (the initial residual) */
147: VecScale(VEC_VV(0),1.0/res_norm);
149: /* Fill the pipeline */
150: KSP_PCApply(ksp,VEC_VV(loc_it),PREVEC(loc_it));
151: PCGetOperators(ksp->pc,&Amat,&Pmat);
152: KSP_MatMult(ksp,Amat,PREVEC(loc_it),ZVEC(loc_it));
153: VecAXPY(ZVEC(loc_it),-shift,VEC_VV(loc_it)); /* Note shift */
155: /* MAIN ITERATION LOOP BEGINNING*/
156: /* keep iterating until we have converged OR generated the max number
157: of directions OR reached the max number of iterations for the method */
158: while (!ksp->reason && loc_it < max_k && ksp->its < ksp->max_it) {
159: if (loc_it) {
160: KSPLogResidualHistory(ksp,res_norm);
161: KSPMonitor(ksp,ksp->its,res_norm);
162: }
163: pipefgmres->it = (loc_it - 1);
165: /* see if more space is needed for work vectors */
166: if (pipefgmres->vv_allocated <= loc_it + VEC_OFFSET + 1) {
167: KSPPIPEFGMRESGetNewVectors(ksp,loc_it+1);
168: /* (loc_it+1) is passed in as number of the first vector that should
169: be allocated */
170: }
172: /* Note that these inner products are with "Z" now, so
173: in particular, lhh[loc_it] is the 'barred' or 'shifted' value,
174: not the value from the equivalent FGMRES run (even in exact arithmetic)
175: That is, the H we need for the Arnoldi relation is different from the
176: coefficients we use in the orthogonalization process,because of the shift */
178: /* Do some local twiddling to allow for a single reduction */
179: for (i=0;i<loc_it+1;i++) {
180: redux[i] = VEC_VV(i);
181: }
182: redux[loc_it+1] = ZVEC(loc_it);
184: /* note the extra dot product which ends up in lh[loc_it+1], which computes ||z||^2 */
185: VecMDotBegin(ZVEC(loc_it),loc_it+2,redux,lhh);
187: /* Start the split reduction (This actually calls the MPI_Iallreduce, otherwise, the reduction is simply delayed until the "end" call)*/
188: PetscCommSplitReductionBegin(PetscObjectComm((PetscObject)ZVEC(loc_it)));
190: /* The work to be overlapped with the inner products follows.
191: This is application of the preconditioner and the operator
192: to compute intermediate quantites which will be combined (locally)
193: with the results of the inner products.
194: */
195: KSP_PCApply(ksp,ZVEC(loc_it),Q);
196: PCGetOperators(ksp->pc,&Amat,&Pmat);
197: KSP_MatMult(ksp,Amat,Q,W);
199: /* Compute inner products of the new direction with previous directions,
200: and the norm of the to-be-orthogonalized direction "Z".
201: This information is enough to build the required entries
202: of H. The inner product with VEC_VV(it_loc) is
203: *different* than in the standard FGMRES and need to be dealt with specially.
204: That is, for standard FGMRES the orthogonalization coefficients are the same
205: as the coefficients used in the Arnoldi relation to reconstruct, but here this
206: is not true (albeit only for the one entry of H which we "unshift" below. */
208: /* Finish the dot product, retrieving the extra entry */
209: VecMDotEnd(ZVEC(loc_it),loc_it+2,redux,lhh);
210: tt = PetscRealPart(lhh[loc_it+1]);
212: /* Hessenberg entries, and entries for (naive) classical Graham-Schmidt
213: Note that the Hessenberg entries require a shift, as these are for the
214: relation AU = VH, which is wrt unshifted basis vectors */
215: hh = HH(0,loc_it); hes=HES(0,loc_it);
216: for (j=0; j<loc_it; j++) {
217: hh[j] = lhh[j];
218: hes[j] = lhh[j];
219: }
220: hh[loc_it] = lhh[loc_it] + shift;
221: hes[loc_it] = lhh[loc_it] + shift;
223: /* we delay applying the shift here */
224: for (j=0; j<=loc_it; j++) {
225: lhh[j] = -lhh[j]; /* flip sign */
226: }
228: /* Compute the norm of the un-normalized new direction using the rearranged formula
229: Note that these are shifted ("barred") quantities */
230: for (k=0;k<=loc_it;k++) tt -= ((PetscReal)(PetscAbsScalar(lhh[k]) * PetscAbsScalar(lhh[k])));
231: /* On AVX512 this is accumulating roundoff errors for eg: tt=-2.22045e-16 */
232: if ((tt < 0.0) && tt > -PETSC_SMALL) tt = 0.0 ;
233: if (tt < 0.0) {
234: /* If we detect square root breakdown in the norm, we must restart the algorithm.
235: Here this means we simply break the current loop and reconstruct the solution
236: using the basis we have computed thus far. Note that by breaking immediately,
237: we do not update the iteration count, so computation done in this iteration
238: should be disregarded.
239: */
240: PetscInfo(ksp,"Restart due to square root breakdown at it = %D, tt=%g\n",ksp->its,(double)tt);
241: break;
242: } else {
243: tt = PetscSqrtReal(tt);
244: }
246: /* new entry in hessenburg is the 2-norm of our new direction */
247: hh[loc_it+1] = tt;
248: hes[loc_it+1] = tt;
250: /* The recurred computation for the new direction
251: The division by tt is delayed to the happy breakdown check later
252: Note placement BEFORE the unshift
253: */
254: VecCopy(ZVEC(loc_it),VEC_VV(loc_it+1));
255: VecMAXPY(VEC_VV(loc_it+1),loc_it+1,lhh,&VEC_VV(0));
256: /* (VEC_VV(loc_it+1) is not normalized yet) */
258: /* The recurred computation for the preconditioned vector (u) */
259: VecCopy(Q,PREVEC(loc_it+1));
260: VecMAXPY(PREVEC(loc_it+1),loc_it+1,lhh,&PREVEC(0));
261: VecScale(PREVEC(loc_it+1),1.0/tt);
263: /* Unshift an entry in the GS coefficients ("removing the bar") */
264: lhh[loc_it] -= shift;
266: /* The recurred computation for z (Au)
267: Note placement AFTER the "unshift" */
268: VecCopy(W,ZVEC(loc_it+1));
269: VecMAXPY(ZVEC(loc_it+1),loc_it+1,lhh,&ZVEC(0));
270: VecScale(ZVEC(loc_it+1),1.0/tt);
272: /* Happy Breakdown Check */
273: hapbnd = PetscAbsScalar((tt) / *RS(loc_it));
274: /* RS(loc_it) contains the res_norm from the last iteration */
275: hapbnd = PetscMin(pipefgmres->haptol,hapbnd);
276: if (tt > hapbnd) {
277: /* scale new direction by its norm */
278: VecScale(VEC_VV(loc_it+1),1.0/tt);
279: } else {
280: /* This happens when the solution is exactly reached. */
281: /* So there is no new direction... */
282: VecSet(VEC_TEMP,0.0); /* set VEC_TEMP to 0 */
283: hapend = PETSC_TRUE;
284: }
285: /* note that for pipefgmres we could get HES(loc_it+1, loc_it) = 0 and the
286: current solution would not be exact if HES was singular. Note that
287: HH non-singular implies that HES is not singular, and HES is guaranteed
288: to be nonsingular when PREVECS are linearly independent and A is
289: nonsingular (in GMRES, the nonsingularity of A implies the nonsingularity
290: of HES). So we should really add a check to verify that HES is nonsingular.*/
292: /* Note that to be thorough, in debug mode, one could call a LAPACK routine
293: here to check that the hessenberg matrix is indeed non-singular (since
294: FGMRES does not guarantee this) */
296: /* Now apply rotations to new col of hessenberg (and right side of system),
297: calculate new rotation, and get new residual norm at the same time*/
298: KSPPIPEFGMRESUpdateHessenberg(ksp,loc_it,&hapend,&res_norm);
299: if (ksp->reason) break;
301: loc_it++;
302: pipefgmres->it = (loc_it-1); /* Add this here in case it has converged */
304: PetscObjectSAWsTakeAccess((PetscObject)ksp);
305: ksp->its++;
306: if (ksp->normtype != KSP_NORM_NONE) ksp->rnorm = res_norm;
307: else ksp->rnorm = 0;
308: PetscObjectSAWsGrantAccess((PetscObject)ksp);
310: (*ksp->converged)(ksp,ksp->its,ksp->rnorm,&ksp->reason,ksp->cnvP);
312: /* Catch error in happy breakdown and signal convergence and break from loop */
313: if (hapend) {
314: if (!ksp->reason) {
316: else {
317: ksp->reason = KSP_DIVERGED_BREAKDOWN;
318: break;
319: }
320: }
321: }
322: }
323: /* END OF ITERATION LOOP */
324: KSPLogResidualHistory(ksp,ksp->rnorm);
326: /*
327: Monitor if we know that we will not return for a restart */
328: if (loc_it && (ksp->reason || ksp->its >= ksp->max_it)) {
329: KSPMonitor(ksp,ksp->its,ksp->rnorm);
330: }
332: if (itcount) *itcount = loc_it;
334: /*
335: Down here we have to solve for the "best" coefficients of the Krylov
336: columns, add the solution values together, and possibly unwind the
337: preconditioning from the solution
338: */
340: /* Form the solution (or the solution so far) */
341: /* Note: must pass in (loc_it-1) for iteration count so that KSPPIPEGMRESIIBuildSoln
342: properly navigates */
344: KSPPIPEFGMRESBuildSoln(RS(0),ksp->vec_sol,ksp->vec_sol,ksp,loc_it-1);
346: return 0;
347: }
349: /*
350: KSPSolve_PIPEFGMRES - This routine applies the PIPEFGMRES method.
352: Input Parameter:
353: . ksp - the Krylov space object that was set to use pipefgmres
355: Output Parameter:
356: . outits - number of iterations used
358: */
359: static PetscErrorCode KSPSolve_PIPEFGMRES(KSP ksp)
360: {
361: PetscInt its,itcount;
362: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES*)ksp->data;
363: PetscBool guess_zero = ksp->guess_zero;
365: /* We have not checked these routines for use with complex numbers. The inner products
366: are likely not defined correctly for that case */
369: PetscCitationsRegister(citation,&cited);
372: PetscObjectSAWsTakeAccess((PetscObject)ksp);
373: ksp->its = 0;
374: PetscObjectSAWsGrantAccess((PetscObject)ksp);
376: itcount = 0;
377: ksp->reason = KSP_CONVERGED_ITERATING;
378: while (!ksp->reason) {
379: KSPInitialResidual(ksp,ksp->vec_sol,VEC_TEMP,VEC_TEMP_MATOP,VEC_VV(0),ksp->vec_rhs);
380: KSPPIPEFGMRESCycle(&its,ksp);
381: itcount += its;
382: if (itcount >= ksp->max_it) {
383: if (!ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
384: break;
385: }
386: ksp->guess_zero = PETSC_FALSE; /* every future call to KSPInitialResidual() will have nonzero guess */
387: }
388: ksp->guess_zero = guess_zero; /* restore if user provided nonzero initial guess */
389: return 0;
390: }
392: static PetscErrorCode KSPDestroy_PIPEFGMRES(KSP ksp)
393: {
394: KSPReset_PIPEFGMRES(ksp);
395: KSPDestroy_GMRES(ksp);
396: return 0;
397: }
399: /*
400: KSPPIPEFGMRESBuildSoln - create the solution from the starting vector and the
401: current iterates.
403: Input parameters:
404: nrs - work area of size it + 1.
405: vguess - index of initial guess
406: vdest - index of result. Note that vguess may == vdest (replace
407: guess with the solution).
408: it - HH upper triangular part is a block of size (it+1) x (it+1)
410: This is an internal routine that knows about the PIPEFGMRES internals.
411: */
412: static PetscErrorCode KSPPIPEFGMRESBuildSoln(PetscScalar *nrs,Vec vguess,Vec vdest,KSP ksp,PetscInt it)
413: {
414: PetscScalar tt;
415: PetscInt k,j;
416: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES*)(ksp->data);
418: /* Solve for solution vector that minimizes the residual */
420: if (it < 0) { /* no pipefgmres steps have been performed */
421: VecCopy(vguess,vdest); /* VecCopy() is smart, exits immediately if vguess == vdest */
422: return 0;
423: }
425: /* solve the upper triangular system - RS is the right side and HH is
426: the upper triangular matrix - put soln in nrs */
427: if (*HH(it,it) != 0.0) nrs[it] = *RS(it) / *HH(it,it);
428: else nrs[it] = 0.0;
430: for (k=it-1; k>=0; k--) {
431: tt = *RS(k);
432: for (j=k+1; j<=it; j++) tt -= *HH(k,j) * nrs[j];
433: nrs[k] = tt / *HH(k,k);
434: }
436: /* Accumulate the correction to the solution of the preconditioned problem in VEC_TEMP */
437: VecZeroEntries(VEC_TEMP);
438: VecMAXPY(VEC_TEMP,it+1,nrs,&PREVEC(0));
440: /* add solution to previous solution */
441: if (vdest == vguess) {
442: VecAXPY(vdest,1.0,VEC_TEMP);
443: } else {
444: VecWAXPY(vdest,1.0,VEC_TEMP,vguess);
445: }
446: return 0;
447: }
449: /*
451: KSPPIPEFGMRESUpdateHessenberg - Do the scalar work for the orthogonalization.
452: Return new residual.
454: input parameters:
456: . ksp - Krylov space object
457: . it - plane rotations are applied to the (it+1)th column of the
458: modified hessenberg (i.e. HH(:,it))
459: . hapend - PETSC_FALSE not happy breakdown ending.
461: output parameters:
462: . res - the new residual
464: */
465: /*
466: . it - column of the Hessenberg that is complete, PIPEFGMRES is actually computing two columns ahead of this
467: */
468: static PetscErrorCode KSPPIPEFGMRESUpdateHessenberg(KSP ksp,PetscInt it,PetscBool *hapend,PetscReal *res)
469: {
470: PetscScalar *hh,*cc,*ss,*rs;
471: PetscInt j;
472: PetscReal hapbnd;
473: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES*)(ksp->data);
475: hh = HH(0,it); /* pointer to beginning of column to update */
476: cc = CC(0); /* beginning of cosine rotations */
477: ss = SS(0); /* beginning of sine rotations */
478: rs = RS(0); /* right hand side of least squares system */
480: /* The Hessenberg matrix is now correct through column it, save that form for possible spectral analysis */
481: for (j=0; j<=it+1; j++) *HES(j,it) = hh[j];
483: /* check for the happy breakdown */
484: hapbnd = PetscMin(PetscAbsScalar(hh[it+1] / rs[it]),pipefgmres->haptol);
485: if (PetscAbsScalar(hh[it+1]) < hapbnd) {
486: PetscInfo(ksp,"Detected happy breakdown, current hapbnd = %14.12e H(%D,%D) = %14.12e\n",(double)hapbnd,it+1,it,(double)PetscAbsScalar(*HH(it+1,it)));
487: *hapend = PETSC_TRUE;
488: }
490: /* Apply all the previously computed plane rotations to the new column
491: of the Hessenberg matrix */
492: /* Note: this uses the rotation [conj(c) s ; -s c], c= cos(theta), s= sin(theta),
493: and some refs have [c s ; -conj(s) c] (don't be confused!) */
495: for (j=0; j<it; j++) {
496: PetscScalar hhj = hh[j];
497: hh[j] = PetscConj(cc[j])*hhj + ss[j]*hh[j+1];
498: hh[j+1] = -ss[j] *hhj + cc[j]*hh[j+1];
499: }
501: /*
502: compute the new plane rotation, and apply it to:
503: 1) the right-hand-side of the Hessenberg system (RS)
504: note: it affects RS(it) and RS(it+1)
505: 2) the new column of the Hessenberg matrix
506: note: it affects HH(it,it) which is currently pointed to
507: by hh and HH(it+1, it) (*(hh+1))
508: thus obtaining the updated value of the residual...
509: */
511: /* compute new plane rotation */
513: if (!*hapend) {
514: PetscReal delta = PetscSqrtReal(PetscSqr(PetscAbsScalar(hh[it])) + PetscSqr(PetscAbsScalar(hh[it+1])));
515: if (delta == 0.0) {
516: ksp->reason = KSP_DIVERGED_NULL;
517: return 0;
518: }
520: cc[it] = hh[it] / delta; /* new cosine value */
521: ss[it] = hh[it+1] / delta; /* new sine value */
523: hh[it] = PetscConj(cc[it])*hh[it] + ss[it]*hh[it+1];
524: rs[it+1] = -ss[it]*rs[it];
525: rs[it] = PetscConj(cc[it])*rs[it];
526: *res = PetscAbsScalar(rs[it+1]);
527: } else { /* happy breakdown: HH(it+1, it) = 0, therefore we don't need to apply
528: another rotation matrix (so RH doesn't change). The new residual is
529: always the new sine term times the residual from last time (RS(it)),
530: but now the new sine rotation would be zero...so the residual should
531: be zero...so we will multiply "zero" by the last residual. This might
532: not be exactly what we want to do here -could just return "zero". */
534: *res = 0.0;
535: }
536: return 0;
537: }
539: /*
540: KSPBuildSolution_PIPEFGMRES
542: Input Parameter:
543: . ksp - the Krylov space object
544: . ptr-
546: Output Parameter:
547: . result - the solution
549: Note: this calls KSPPIPEFGMRESBuildSoln - the same function that KSPPIPEFGMRESCycle
550: calls directly.
552: */
553: PetscErrorCode KSPBuildSolution_PIPEFGMRES(KSP ksp,Vec ptr,Vec *result)
554: {
555: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES*)ksp->data;
557: if (!ptr) {
558: if (!pipefgmres->sol_temp) {
559: VecDuplicate(ksp->vec_sol,&pipefgmres->sol_temp);
560: PetscLogObjectParent((PetscObject)ksp,(PetscObject)pipefgmres->sol_temp);
561: }
562: ptr = pipefgmres->sol_temp;
563: }
564: if (!pipefgmres->nrs) {
565: /* allocate the work area */
566: PetscMalloc1(pipefgmres->max_k,&pipefgmres->nrs);
567: PetscLogObjectMemory((PetscObject)ksp,pipefgmres->max_k*sizeof(PetscScalar));
568: }
570: KSPPIPEFGMRESBuildSoln(pipefgmres->nrs,ksp->vec_sol,ptr,ksp,pipefgmres->it);
571: if (result) *result = ptr;
572: return 0;
573: }
575: PetscErrorCode KSPSetFromOptions_PIPEFGMRES(PetscOptionItems *PetscOptionsObject,KSP ksp)
576: {
577: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES*)ksp->data;
578: PetscBool flg;
579: PetscScalar shift;
581: KSPSetFromOptions_GMRES(PetscOptionsObject,ksp);
582: PetscOptionsHead(PetscOptionsObject,"KSP pipelined FGMRES Options");
583: PetscOptionsScalar("-ksp_pipefgmres_shift","shift parameter","KSPPIPEFGMRESSetShift",pipefgmres->shift,&shift,&flg);
584: if (flg) KSPPIPEFGMRESSetShift(ksp,shift);
585: PetscOptionsTail();
586: return 0;
587: }
589: PetscErrorCode KSPView_PIPEFGMRES(KSP ksp,PetscViewer viewer)
590: {
591: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES*)ksp->data;
592: PetscBool iascii,isstring;
594: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
595: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
597: if (iascii) {
598: PetscViewerASCIIPrintf(viewer," restart=%D\n",pipefgmres->max_k);
599: PetscViewerASCIIPrintf(viewer," happy breakdown tolerance %g\n",(double)pipefgmres->haptol);
600: #if defined(PETSC_USE_COMPLEX)
601: PetscViewerASCIIPrintf(viewer," shift=%g+%gi\n",PetscRealPart(pipefgmres->shift),PetscImaginaryPart(pipefgmres->shift));
602: #else
603: PetscViewerASCIIPrintf(viewer," shift=%g\n",pipefgmres->shift);
604: #endif
605: } else if (isstring) {
606: PetscViewerStringSPrintf(viewer,"restart %D",pipefgmres->max_k);
607: #if defined(PETSC_USE_COMPLEX)
608: PetscViewerStringSPrintf(viewer," shift=%g+%gi\n",PetscRealPart(pipefgmres->shift),PetscImaginaryPart(pipefgmres->shift));
609: #else
610: PetscViewerStringSPrintf(viewer," shift=%g\n",pipefgmres->shift);
611: #endif
612: }
613: return 0;
614: }
616: PetscErrorCode KSPReset_PIPEFGMRES(KSP ksp)
617: {
618: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES*)ksp->data;
619: PetscInt i;
621: PetscFree(pipefgmres->prevecs);
622: PetscFree(pipefgmres->zvecs);
623: for (i=0; i<pipefgmres->nwork_alloc; i++) {
624: VecDestroyVecs(pipefgmres->mwork_alloc[i],&pipefgmres->prevecs_user_work[i]);
625: VecDestroyVecs(pipefgmres->mwork_alloc[i],&pipefgmres->zvecs_user_work[i]);
626: }
627: PetscFree(pipefgmres->prevecs_user_work);
628: PetscFree(pipefgmres->zvecs_user_work);
629: PetscFree(pipefgmres->redux);
630: KSPReset_GMRES(ksp);
631: return 0;
632: }
634: /*MC
635: KSPPIPEFGMRES - Implements the Pipelined Generalized Minimal Residual method.
637: A flexible, 1-stage pipelined variant of GMRES.
639: Options Database Keys:
640: + -ksp_gmres_restart <restart> - the number of Krylov directions to orthogonalize against
641: . -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
642: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
643: . -ksp_pipefgmres_shift - the shift to use (defaults to 1. See KSPPIPEFGMRESSetShift()
644: vectors are allocated as needed)
645: - -ksp_gmres_krylov_monitor - plot the Krylov space generated
647: Level: intermediate
649: Notes:
651: This variant is not "explicitly normalized" like KSPPGMRES, and requires a shift parameter.
653: A heuristic for choosing the shift parameter is the largest eigenvalue of the preconditioned operator.
655: Only right preconditioning is supported (but this preconditioner may be nonlinear/variable/inexact, as with KSPFGMRES).
656: MPI configuration may be necessary for reductions to make asynchronous progress, which is important for performance of pipelined methods.
657: See the FAQ on the PETSc website for details.
659: Developer Notes:
660: This class is subclassed off of KSPGMRES.
662: Reference:
663: P. Sanan, S.M. Schnepp, and D.A. May,
664: "Pipelined, Flexible Krylov Subspace Methods,"
665: SIAM Journal on Scientific Computing 2016 38:5, C441-C470,
666: DOI: 10.1137/15M1049130
668: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPLGMRES, KSPPIPECG, KSPPIPECR, KSPPGMRES, KSPFGMRES
669: KSPGMRESSetRestart(), KSPGMRESSetHapTol(), KSPGMRESSetPreAllocateVectors(), KSPGMRESMonitorKrylov(), KSPPIPEFGMRESSetShift()
670: M*/
672: PETSC_EXTERN PetscErrorCode KSPCreate_PIPEFGMRES(KSP ksp)
673: {
674: KSP_PIPEFGMRES *pipefgmres;
676: PetscNewLog(ksp,&pipefgmres);
678: ksp->data = (void*)pipefgmres;
679: ksp->ops->buildsolution = KSPBuildSolution_PIPEFGMRES;
680: ksp->ops->setup = KSPSetUp_PIPEFGMRES;
681: ksp->ops->solve = KSPSolve_PIPEFGMRES;
682: ksp->ops->reset = KSPReset_PIPEFGMRES;
683: ksp->ops->destroy = KSPDestroy_PIPEFGMRES;
684: ksp->ops->view = KSPView_PIPEFGMRES;
685: ksp->ops->setfromoptions = KSPSetFromOptions_PIPEFGMRES;
686: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
687: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
689: KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_RIGHT,3);
690: KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_RIGHT,1);
692: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",KSPGMRESSetPreAllocateVectors_GMRES);
693: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESSetRestart_C",KSPGMRESSetRestart_GMRES);
694: PetscObjectComposeFunction((PetscObject)ksp,"KSPGMRESGetRestart_C",KSPGMRESGetRestart_GMRES);
696: pipefgmres->nextra_vecs = 1;
697: pipefgmres->haptol = 1.0e-30;
698: pipefgmres->q_preallocate = 0;
699: pipefgmres->delta_allocate = PIPEFGMRES_DELTA_DIRECTIONS;
700: pipefgmres->orthog = NULL;
701: pipefgmres->nrs = NULL;
702: pipefgmres->sol_temp = NULL;
703: pipefgmres->max_k = PIPEFGMRES_DEFAULT_MAXK;
704: pipefgmres->Rsvd = NULL;
705: pipefgmres->orthogwork = NULL;
706: pipefgmres->cgstype = KSP_GMRES_CGS_REFINE_NEVER;
707: pipefgmres->shift = 1.0;
708: return 0;
709: }
711: static PetscErrorCode KSPPIPEFGMRESGetNewVectors(KSP ksp,PetscInt it)
712: {
713: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES*)ksp->data;
714: PetscInt nwork = pipefgmres->nwork_alloc; /* number of work vector chunks allocated */
715: PetscInt nalloc; /* number to allocate */
716: PetscInt k;
718: nalloc = pipefgmres->delta_allocate; /* number of vectors to allocate
719: in a single chunk */
721: /* Adjust the number to allocate to make sure that we don't exceed the
722: number of available slots (pipefgmres->vecs_allocated)*/
723: if (it + VEC_OFFSET + nalloc >= pipefgmres->vecs_allocated) {
724: nalloc = pipefgmres->vecs_allocated - it - VEC_OFFSET;
725: }
726: if (!nalloc) return 0;
728: pipefgmres->vv_allocated += nalloc; /* vv_allocated is the number of vectors allocated */
730: /* work vectors */
731: KSPCreateVecs(ksp,nalloc,&pipefgmres->user_work[nwork],0,NULL);
732: PetscLogObjectParents(ksp,nalloc,pipefgmres->user_work[nwork]);
733: for (k=0; k < nalloc; k++) {
734: pipefgmres->vecs[it+VEC_OFFSET+k] = pipefgmres->user_work[nwork][k];
735: }
736: /* specify size of chunk allocated */
737: pipefgmres->mwork_alloc[nwork] = nalloc;
739: /* preconditioned vectors (note we don't use VEC_OFFSET) */
740: KSPCreateVecs(ksp,nalloc,&pipefgmres->prevecs_user_work[nwork],0,NULL);
741: PetscLogObjectParents(ksp,nalloc,pipefgmres->prevecs_user_work[nwork]);
742: for (k=0; k < nalloc; k++) {
743: pipefgmres->prevecs[it+k] = pipefgmres->prevecs_user_work[nwork][k];
744: }
746: KSPCreateVecs(ksp,nalloc,&pipefgmres->zvecs_user_work[nwork],0,NULL);
747: PetscLogObjectParents(ksp,nalloc,pipefgmres->zvecs_user_work[nwork]);
748: for (k=0; k < nalloc; k++) {
749: pipefgmres->zvecs[it+k] = pipefgmres->zvecs_user_work[nwork][k];
750: }
752: /* increment the number of work vector chunks */
753: pipefgmres->nwork_alloc++;
754: return 0;
755: }
757: /*@
758: KSPPIPEFGMRESSetShift - Set the shift parameter for the flexible, pipelined GMRES solver.
760: A heuristic is to set this to be comparable to the largest eigenvalue of the preconditioned operator. This can be acheived with PETSc itself by using a few iterations of a Krylov method. See KSPComputeEigenvalues (and note the caveats there).
762: Logically Collective on ksp
764: Input Parameters:
765: + ksp - the Krylov space context
766: - shift - the shift
768: Level: intermediate
770: Options Database:
771: . -ksp_pipefgmres_shift <shift> - set the shift parameter
773: .seealso: KSPComputeEigenvalues()
774: @*/
775: PetscErrorCode KSPPIPEFGMRESSetShift(KSP ksp,PetscScalar shift)
776: {
777: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES*)ksp->data;
781: pipefgmres->shift = shift;
782: return 0;
783: }