Actual source code: pipefcg.c

  1: /*
  2:     Contributed by Patrick Sanan and Sascha M. Schnepp
  3: */

  5: #include <../src/ksp/ksp/impls/fcg/pipefcg/pipefcgimpl.h>

  7: static PetscBool  cited      = PETSC_FALSE;
  8: static const char citation[] = "@article{SSM2016,\n"
  9:                                "  author = {P. Sanan and S.M. Schnepp and D.A. May},\n"
 10:                                "  title = {Pipelined, Flexible Krylov Subspace Methods},\n"
 11:                                "  journal = {SIAM Journal on Scientific Computing},\n"
 12:                                "  volume = {38},\n"
 13:                                "  number = {5},\n"
 14:                                "  pages = {C441-C470},\n"
 15:                                "  year = {2016},\n"
 16:                                "  doi = {10.1137/15M1049130},\n"
 17:                                "  URL = {http://dx.doi.org/10.1137/15M1049130},\n"
 18:                                "  eprint = {http://dx.doi.org/10.1137/15M1049130}\n"
 19:                                "}\n";

 21: #define KSPPIPEFCG_DEFAULT_MMAX       15
 22: #define KSPPIPEFCG_DEFAULT_NPREALLOC  5
 23: #define KSPPIPEFCG_DEFAULT_VECB       5
 24: #define KSPPIPEFCG_DEFAULT_TRUNCSTRAT KSP_FCD_TRUNC_TYPE_NOTAY

 26: static PetscErrorCode KSPAllocateVectors_PIPEFCG(KSP ksp, PetscInt nvecsneeded, PetscInt chunksize)
 27: {
 28:   PetscInt     i;
 29:   KSP_PIPEFCG *pipefcg;
 30:   PetscInt     nnewvecs, nvecsprev;

 32:   pipefcg = (KSP_PIPEFCG *)ksp->data;

 34:   /* Allocate enough new vectors to add chunksize new vectors, reach nvecsneedtotal, or to reach mmax+1, whichever is smallest */
 35:   if (pipefcg->nvecs < PetscMin(pipefcg->mmax + 1, nvecsneeded)) {
 36:     nvecsprev = pipefcg->nvecs;
 37:     nnewvecs  = PetscMin(PetscMax(nvecsneeded - pipefcg->nvecs, chunksize), pipefcg->mmax + 1 - pipefcg->nvecs);
 38:     KSPCreateVecs(ksp, nnewvecs, &pipefcg->pQvecs[pipefcg->nchunks], 0, NULL);
 39:     KSPCreateVecs(ksp, nnewvecs, &pipefcg->pZETAvecs[pipefcg->nchunks], 0, NULL);
 40:     KSPCreateVecs(ksp, nnewvecs, &pipefcg->pPvecs[pipefcg->nchunks], 0, NULL);
 41:     KSPCreateVecs(ksp, nnewvecs, &pipefcg->pSvecs[pipefcg->nchunks], 0, NULL);
 42:     pipefcg->nvecs += nnewvecs;
 43:     for (i = 0; i < nnewvecs; ++i) {
 44:       pipefcg->Qvecs[nvecsprev + i]    = pipefcg->pQvecs[pipefcg->nchunks][i];
 45:       pipefcg->ZETAvecs[nvecsprev + i] = pipefcg->pZETAvecs[pipefcg->nchunks][i];
 46:       pipefcg->Pvecs[nvecsprev + i]    = pipefcg->pPvecs[pipefcg->nchunks][i];
 47:       pipefcg->Svecs[nvecsprev + i]    = pipefcg->pSvecs[pipefcg->nchunks][i];
 48:     }
 49:     pipefcg->chunksizes[pipefcg->nchunks] = nnewvecs;
 50:     ++pipefcg->nchunks;
 51:   }
 52:   return 0;
 53: }

 55: static PetscErrorCode KSPSetUp_PIPEFCG(KSP ksp)
 56: {
 57:   KSP_PIPEFCG   *pipefcg;
 58:   const PetscInt nworkstd = 5;

 60:   pipefcg = (KSP_PIPEFCG *)ksp->data;

 62:   /* Allocate "standard" work vectors (not including the basis and transformed basis vectors) */
 63:   KSPSetWorkVecs(ksp, nworkstd);

 65:   /* Allocated space for pointers to additional work vectors
 66:    note that mmax is the number of previous directions, so we add 1 for the current direction,
 67:    and an extra 1 for the prealloc (which might be empty) */
 68:   PetscMalloc4(pipefcg->mmax + 1, &(pipefcg->Pvecs), pipefcg->mmax + 1, &(pipefcg->pPvecs), pipefcg->mmax + 1, &(pipefcg->Svecs), pipefcg->mmax + 1, &(pipefcg->pSvecs));
 69:   PetscMalloc4(pipefcg->mmax + 1, &(pipefcg->Qvecs), pipefcg->mmax + 1, &(pipefcg->pQvecs), pipefcg->mmax + 1, &(pipefcg->ZETAvecs), pipefcg->mmax + 1, &(pipefcg->pZETAvecs));
 70:   PetscMalloc4(pipefcg->mmax + 1, &(pipefcg->Pold), pipefcg->mmax + 1, &(pipefcg->Sold), pipefcg->mmax + 1, &(pipefcg->Qold), pipefcg->mmax + 1, &(pipefcg->ZETAold));
 71:   PetscMalloc1(pipefcg->mmax + 1, &(pipefcg->chunksizes));
 72:   PetscMalloc3(pipefcg->mmax + 2, &(pipefcg->dots), pipefcg->mmax + 1, &(pipefcg->etas), pipefcg->mmax + 2, &(pipefcg->redux));

 74:   /* If the requested number of preallocated vectors is greater than mmax reduce nprealloc */
 75:   if (pipefcg->nprealloc > pipefcg->mmax + 1) PetscInfo(NULL, "Requested nprealloc=%" PetscInt_FMT " is greater than m_max+1=%" PetscInt_FMT ". Resetting nprealloc = m_max+1.\n", pipefcg->nprealloc, pipefcg->mmax + 1);

 77:   /* Preallocate additional work vectors */
 78:   KSPAllocateVectors_PIPEFCG(ksp, pipefcg->nprealloc, pipefcg->nprealloc);
 79:   return 0;
 80: }

 82: static PetscErrorCode KSPSolve_PIPEFCG_cycle(KSP ksp)
 83: {
 84:   PetscInt     i, j, k, idx, kdx, mi;
 85:   KSP_PIPEFCG *pipefcg;
 86:   PetscScalar  alpha = 0.0, gamma, *betas, *dots;
 87:   PetscReal    dp    = 0.0, delta, *eta, *etas;
 88:   Vec          B, R, Z, X, Qcurr, W, ZETAcurr, M, N, Pcurr, Scurr, *redux;
 89:   Mat          Amat, Pmat;

 91:   /* We have not checked these routines for use with complex numbers. The inner products
 92:      are likely not defined correctly for that case */

 95: #define VecXDot(x, y, a)          (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecDot(x, y, a) : VecTDot(x, y, a))
 96: #define VecXDotBegin(x, y, a)     (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecDotBegin(x, y, a) : VecTDotBegin(x, y, a))
 97: #define VecXDotEnd(x, y, a)       (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecDotEnd(x, y, a) : VecTDotEnd(x, y, a))
 98: #define VecMXDot(x, n, y, a)      (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecMDot(x, n, y, a) : VecMTDot(x, n, y, a))
 99: #define VecMXDotBegin(x, n, y, a) (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecMDotBegin(x, n, y, a) : VecMTDotBegin(x, n, y, a))
100: #define VecMXDotEnd(x, n, y, a)   (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecMDotEnd(x, n, y, a) : VecMTDotEnd(x, n, y, a))

102:   pipefcg = (KSP_PIPEFCG *)ksp->data;
103:   X       = ksp->vec_sol;
104:   B       = ksp->vec_rhs;
105:   R       = ksp->work[0];
106:   Z       = ksp->work[1];
107:   W       = ksp->work[2];
108:   M       = ksp->work[3];
109:   N       = ksp->work[4];

111:   redux = pipefcg->redux;
112:   dots  = pipefcg->dots;
113:   etas  = pipefcg->etas;
114:   betas = dots; /* dots takes the result of all dot products of which the betas are a subset */

116:   PCGetOperators(ksp->pc, &Amat, &Pmat);

118:   /* Compute cycle initial residual */
119:   KSP_MatMult(ksp, Amat, X, R);
120:   VecAYPX(R, -1.0, B);    /* r <- b - Ax */
121:   KSP_PCApply(ksp, R, Z); /* z <- Br     */

123:   Pcurr    = pipefcg->Pvecs[0];
124:   Scurr    = pipefcg->Svecs[0];
125:   Qcurr    = pipefcg->Qvecs[0];
126:   ZETAcurr = pipefcg->ZETAvecs[0];
127:   VecCopy(Z, Pcurr);
128:   KSP_MatMult(ksp, Amat, Pcurr, Scurr); /* S = Ap     */
129:   VecCopy(Scurr, W);                    /* w = s = Az */

131:   /* Initial state of pipelining intermediates */
132:   redux[0] = R;
133:   redux[1] = W;
134:   VecMXDotBegin(Z, 2, redux, dots);
135:   PetscCommSplitReductionBegin(PetscObjectComm((PetscObject)Z)); /* perform asynchronous reduction */
136:   KSP_PCApply(ksp, W, M);                                        /* m = B(w) */
137:   KSP_MatMult(ksp, Amat, M, N);                                  /* n = Am   */
138:   VecCopy(M, Qcurr);                                             /* q = m    */
139:   VecCopy(N, ZETAcurr);                                          /* zeta = n */
140:   VecMXDotEnd(Z, 2, redux, dots);
141:   gamma   = dots[0];
142:   delta   = PetscRealPart(dots[1]);
143:   etas[0] = delta;
144:   alpha   = gamma / delta;

146:   i = 0;
147:   do {
148:     ksp->its++;

150:     /* Update X, R, Z, W */
151:     VecAXPY(X, +alpha, Pcurr);    /* x <- x + alpha * pi    */
152:     VecAXPY(R, -alpha, Scurr);    /* r <- r - alpha * si    */
153:     VecAXPY(Z, -alpha, Qcurr);    /* z <- z - alpha * qi    */
154:     VecAXPY(W, -alpha, ZETAcurr); /* w <- w - alpha * zetai */

156:     /* Compute norm for convergence check */
157:     switch (ksp->normtype) {
158:     case KSP_NORM_PRECONDITIONED:
159:       VecNorm(Z, NORM_2, &dp); /* dp <- sqrt(z'*z) = sqrt(e'*A'*B'*B*A*e) */
160:       break;
161:     case KSP_NORM_UNPRECONDITIONED:
162:       VecNorm(R, NORM_2, &dp); /* dp <- sqrt(r'*r) = sqrt(e'*A'*A*e)      */
163:       break;
164:     case KSP_NORM_NATURAL:
165:       dp = PetscSqrtReal(PetscAbsScalar(gamma)); /* dp <- sqrt(r'*z) = sqrt(e'*A'*B*A*e)    */
166:       break;
167:     case KSP_NORM_NONE:
168:       dp = 0.0;
169:       break;
170:     default:
171:       SETERRQ(PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "%s", KSPNormTypes[ksp->normtype]);
172:     }

174:     /* Check for convergence */
175:     ksp->rnorm = dp;
176:     KSPLogResidualHistory(ksp, dp);
177:     KSPMonitor(ksp, ksp->its, dp);
178:     (*ksp->converged)(ksp, ksp->its, dp, &ksp->reason, ksp->cnvP);
179:     if (ksp->reason) return 0;

181:     /* Computations of current iteration done */
182:     ++i;

184:     /* If needbe, allocate a new chunk of vectors in P and C */
185:     KSPAllocateVectors_PIPEFCG(ksp, i + 1, pipefcg->vecb);

187:     /* Note that we wrap around and start clobbering old vectors */
188:     idx      = i % (pipefcg->mmax + 1);
189:     Pcurr    = pipefcg->Pvecs[idx];
190:     Scurr    = pipefcg->Svecs[idx];
191:     Qcurr    = pipefcg->Qvecs[idx];
192:     ZETAcurr = pipefcg->ZETAvecs[idx];
193:     eta      = pipefcg->etas + idx;

195:     /* number of old directions to orthogonalize against */
196:     switch (pipefcg->truncstrat) {
197:     case KSP_FCD_TRUNC_TYPE_STANDARD:
198:       mi = pipefcg->mmax;
199:       break;
200:     case KSP_FCD_TRUNC_TYPE_NOTAY:
201:       mi = ((i - 1) % pipefcg->mmax) + 1;
202:       break;
203:     default:
204:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unrecognized Truncation Strategy");
205:     }

207:     /* Pick old p,s,q,zeta in a way suitable for VecMDot */
208:     VecCopy(Z, Pcurr);
209:     for (k = PetscMax(0, i - mi), j = 0; k < i; ++j, ++k) {
210:       kdx                 = k % (pipefcg->mmax + 1);
211:       pipefcg->Pold[j]    = pipefcg->Pvecs[kdx];
212:       pipefcg->Sold[j]    = pipefcg->Svecs[kdx];
213:       pipefcg->Qold[j]    = pipefcg->Qvecs[kdx];
214:       pipefcg->ZETAold[j] = pipefcg->ZETAvecs[kdx];
215:       redux[j]            = pipefcg->Svecs[kdx];
216:     }
217:     redux[j]     = R; /* If the above loop is not executed redux contains only R => all beta_k = 0, only gamma, delta != 0 */
218:     redux[j + 1] = W;

220:     VecMXDotBegin(Z, j + 2, redux, betas);                         /* Start split reductions for beta_k = (z,s_k), gamma = (z,r), delta = (z,w) */
221:     PetscCommSplitReductionBegin(PetscObjectComm((PetscObject)Z)); /* perform asynchronous reduction */
222:     VecWAXPY(N, -1.0, R, W);                                       /* m = u + B(w-r): (a) ntmp = w-r              */
223:     KSP_PCApply(ksp, N, M);                                        /* m = u + B(w-r): (b) mtmp = B(ntmp) = B(w-r) */
224:     VecAXPY(M, 1.0, Z);                                            /* m = u + B(w-r): (c) m = z + mtmp            */
225:     KSP_MatMult(ksp, Amat, M, N);                                  /* n = Am                                      */
226:     VecMXDotEnd(Z, j + 2, redux, betas);                           /* Finish split reductions */
227:     gamma = betas[j];
228:     delta = PetscRealPart(betas[j + 1]);

230:     *eta = 0.;
231:     for (k = PetscMax(0, i - mi), j = 0; k < i; ++j, ++k) {
232:       kdx = k % (pipefcg->mmax + 1);
233:       betas[j] /= -etas[kdx]; /* betak  /= etak */
234:       *eta -= ((PetscReal)(PetscAbsScalar(betas[j]) * PetscAbsScalar(betas[j]))) * etas[kdx];
235:       /* etaitmp = -betaik^2 * etak */
236:     }
237:     *eta += delta; /* etai    = delta -betaik^2 * etak */
238:     if (*eta < 0.) {
239:       pipefcg->norm_breakdown = PETSC_TRUE;
240:       PetscInfo(ksp, "Restart due to square root breakdown at it = %" PetscInt_FMT "\n", ksp->its);
241:       break;
242:     } else {
243:       alpha = gamma / (*eta); /* alpha = gamma/etai */
244:     }

246:     /* project out stored search directions using classical G-S */
247:     VecCopy(Z, Pcurr);
248:     VecCopy(W, Scurr);
249:     VecCopy(M, Qcurr);
250:     VecCopy(N, ZETAcurr);
251:     VecMAXPY(Pcurr, j, betas, pipefcg->Pold);       /* pi    <- ui - sum_k beta_k p_k    */
252:     VecMAXPY(Scurr, j, betas, pipefcg->Sold);       /* si    <- wi - sum_k beta_k s_k    */
253:     VecMAXPY(Qcurr, j, betas, pipefcg->Qold);       /* qi    <- m  - sum_k beta_k q_k    */
254:     VecMAXPY(ZETAcurr, j, betas, pipefcg->ZETAold); /* zetai <- n  - sum_k beta_k zeta_k */

256:   } while (ksp->its < ksp->max_it);
257:   if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
258:   return 0;
259: }

261: static PetscErrorCode KSPSolve_PIPEFCG(KSP ksp)
262: {
263:   KSP_PIPEFCG *pipefcg;
264:   PetscScalar  gamma;
265:   PetscReal    dp = 0.0;
266:   Vec          B, R, Z, X;
267:   Mat          Amat, Pmat;

269: #define VecXDot(x, y, a) (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecDot(x, y, a) : VecTDot(x, y, a))

271:   PetscCitationsRegister(citation, &cited);

273:   pipefcg = (KSP_PIPEFCG *)ksp->data;
274:   X       = ksp->vec_sol;
275:   B       = ksp->vec_rhs;
276:   R       = ksp->work[0];
277:   Z       = ksp->work[1];

279:   PCGetOperators(ksp->pc, &Amat, &Pmat);

281:   /* Compute initial residual needed for convergence check*/
282:   ksp->its = 0;
283:   if (!ksp->guess_zero) {
284:     KSP_MatMult(ksp, Amat, X, R);
285:     VecAYPX(R, -1.0, B); /* r <- b - Ax                             */
286:   } else {
287:     VecCopy(B, R); /* r <- b (x is 0)                         */
288:   }
289:   switch (ksp->normtype) {
290:   case KSP_NORM_PRECONDITIONED:
291:     KSP_PCApply(ksp, R, Z);  /* z <- Br                                 */
292:     VecNorm(Z, NORM_2, &dp); /* dp <- dqrt(z'*z) = sqrt(e'*A'*B'*B*A*e) */
293:     break;
294:   case KSP_NORM_UNPRECONDITIONED:
295:     VecNorm(R, NORM_2, &dp); /* dp <- sqrt(r'*r) = sqrt(e'*A'*A*e)      */
296:     break;
297:   case KSP_NORM_NATURAL:
298:     KSP_PCApply(ksp, R, Z); /* z <- Br                                 */
299:     VecXDot(Z, R, &gamma);
300:     dp = PetscSqrtReal(PetscAbsScalar(gamma)); /* dp <- sqrt(r'*z) = sqrt(e'*A'*B*A*e)    */
301:     break;
302:   case KSP_NORM_NONE:
303:     dp = 0.0;
304:     break;
305:   default:
306:     SETERRQ(PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "%s", KSPNormTypes[ksp->normtype]);
307:   }

309:   /* Initial Convergence Check */
310:   KSPLogResidualHistory(ksp, dp);
311:   KSPMonitor(ksp, 0, dp);
312:   ksp->rnorm = dp;
313:   (*ksp->converged)(ksp, 0, dp, &ksp->reason, ksp->cnvP);
314:   if (ksp->reason) return 0;

316:   do {
317:     /* A cycle is broken only if a norm breakdown occurs. If not the entire solve happens in a single cycle.
318:        This is coded this way to allow both truncation and truncation-restart strategy
319:        (see KSPFCDGetNumOldDirections()) */
320:     KSPSolve_PIPEFCG_cycle(ksp);
321:     if (ksp->reason) return 0;
322:     if (pipefcg->norm_breakdown) {
323:       pipefcg->n_restarts++;
324:       pipefcg->norm_breakdown = PETSC_FALSE;
325:     }
326:   } while (ksp->its < ksp->max_it);

328:   if (ksp->its >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
329:   return 0;
330: }

332: static PetscErrorCode KSPDestroy_PIPEFCG(KSP ksp)
333: {
334:   PetscInt     i;
335:   KSP_PIPEFCG *pipefcg;

337:   pipefcg = (KSP_PIPEFCG *)ksp->data;

339:   /* Destroy "standard" work vecs */
340:   VecDestroyVecs(ksp->nwork, &ksp->work);

342:   /* Destroy vectors of old directions and the arrays that manage pointers to them */
343:   if (pipefcg->nvecs) {
344:     for (i = 0; i < pipefcg->nchunks; ++i) {
345:       VecDestroyVecs(pipefcg->chunksizes[i], &pipefcg->pPvecs[i]);
346:       VecDestroyVecs(pipefcg->chunksizes[i], &pipefcg->pSvecs[i]);
347:       VecDestroyVecs(pipefcg->chunksizes[i], &pipefcg->pQvecs[i]);
348:       VecDestroyVecs(pipefcg->chunksizes[i], &pipefcg->pZETAvecs[i]);
349:     }
350:   }
351:   PetscFree4(pipefcg->Pvecs, pipefcg->Svecs, pipefcg->pPvecs, pipefcg->pSvecs);
352:   PetscFree4(pipefcg->Qvecs, pipefcg->ZETAvecs, pipefcg->pQvecs, pipefcg->pZETAvecs);
353:   PetscFree4(pipefcg->Pold, pipefcg->Sold, pipefcg->Qold, pipefcg->ZETAold);
354:   PetscFree(pipefcg->chunksizes);
355:   PetscFree3(pipefcg->dots, pipefcg->etas, pipefcg->redux);
356:   KSPDestroyDefault(ksp);
357:   return 0;
358: }

360: static PetscErrorCode KSPView_PIPEFCG(KSP ksp, PetscViewer viewer)
361: {
362:   KSP_PIPEFCG *pipefcg = (KSP_PIPEFCG *)ksp->data;
363:   PetscBool    iascii, isstring;
364:   const char  *truncstr;

366:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii);
367:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring);

369:   if (pipefcg->truncstrat == KSP_FCD_TRUNC_TYPE_STANDARD) {
370:     truncstr = "Using standard truncation strategy";
371:   } else if (pipefcg->truncstrat == KSP_FCD_TRUNC_TYPE_NOTAY) {
372:     truncstr = "Using Notay's truncation strategy";
373:   } else {
374:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Undefined FCD truncation strategy");
375:   }

377:   if (iascii) {
378:     PetscViewerASCIIPrintf(viewer, "  max previous directions = %" PetscInt_FMT "\n", pipefcg->mmax);
379:     PetscViewerASCIIPrintf(viewer, "  preallocated %" PetscInt_FMT " directions\n", PetscMin(pipefcg->nprealloc, pipefcg->mmax + 1));
380:     PetscViewerASCIIPrintf(viewer, "  %s\n", truncstr);
381:     PetscViewerASCIIPrintf(viewer, "  restarts performed = %" PetscInt_FMT " \n", pipefcg->n_restarts);
382:   } else if (isstring) {
383:     PetscViewerStringSPrintf(viewer, "max previous directions = %" PetscInt_FMT ", preallocated %" PetscInt_FMT " directions, %s truncation strategy", pipefcg->mmax, pipefcg->nprealloc, truncstr);
384:   }
385:   return 0;
386: }

388: /*@
389:   KSPPIPEFCGSetMmax - set the maximum number of previous directions PIPEFCG will store for orthogonalization

391:   Note: mmax + 1 directions are stored (mmax previous ones along with the current one)
392:   and whether all are used in each iteration also depends on the truncation strategy
393:   (see KSPPIPEFCGSetTruncationType)

395:   Logically Collective on ksp

397:   Input Parameters:
398: +  ksp - the Krylov space context
399: -  mmax - the maximum number of previous directions to orthogonalize against

401:   Level: intermediate

403:   Options Database:
404: . -ksp_pipefcg_mmax <N> - maximum number of previous directions

406: .seealso: `KSPPIPEFCG`, `KSPPIPEFCGSetTruncationType()`, `KSPPIPEFCGSetNprealloc()`
407: @*/
408: PetscErrorCode KSPPIPEFCGSetMmax(KSP ksp, PetscInt mmax)
409: {
410:   KSP_PIPEFCG *pipefcg = (KSP_PIPEFCG *)ksp->data;

414:   pipefcg->mmax = mmax;
415:   return 0;
416: }

418: /*@
419:   KSPPIPEFCGGetMmax - get the maximum number of previous directions PIPEFCG will store

421:   Note: PIPEFCG stores mmax+1 directions at most (mmax previous ones, and the current one)

423:    Not Collective

425:    Input Parameter:
426: .  ksp - the Krylov space context

428:    Output Parameter:
429: .  mmax - the maximum number of previous directions allowed for orthogonalization

431:   Options Database:
432: . -ksp_pipefcg_mmax <N> - maximum number of previous directions

434:    Level: intermediate

436: .seealso: `KSPPIPEFCG`, `KSPPIPEFCGGetTruncationType()`, `KSPPIPEFCGGetNprealloc()`, `KSPPIPEFCGSetMmax()`
437: @*/
438: PetscErrorCode KSPPIPEFCGGetMmax(KSP ksp, PetscInt *mmax)
439: {
440:   KSP_PIPEFCG *pipefcg = (KSP_PIPEFCG *)ksp->data;

443:   *mmax = pipefcg->mmax;
444:   return 0;
445: }

447: /*@
448:   KSPPIPEFCGSetNprealloc - set the number of directions to preallocate with PIPEFCG

450:   Logically Collective on ksp

452:   Input Parameters:
453: +  ksp - the Krylov space context
454: -  nprealloc - the number of vectors to preallocate

456:   Level: advanced

458:   Options Database:
459: . -ksp_pipefcg_nprealloc <N> - the number of vectors to preallocate

461: .seealso: `KSPPIPEFCG`, `KSPPIPEFCGSetTruncationType()`, `KSPPIPEFCGGetNprealloc()`
462: @*/
463: PetscErrorCode KSPPIPEFCGSetNprealloc(KSP ksp, PetscInt nprealloc)
464: {
465:   KSP_PIPEFCG *pipefcg = (KSP_PIPEFCG *)ksp->data;

469:   pipefcg->nprealloc = nprealloc;
470:   return 0;
471: }

473: /*@
474:   KSPPIPEFCGGetNprealloc - get the number of directions to preallocate by PIPEFCG

476:    Not Collective

478:    Input Parameter:
479: .  ksp - the Krylov space context

481:    Output Parameter:
482: .  nprealloc - the number of directions preallocated

484:    Level: advanced

486: .seealso: `KSPPIPEFCG`, `KSPPIPEFCGGetTruncationType()`, `KSPPIPEFCGSetNprealloc()`
487: @*/
488: PetscErrorCode KSPPIPEFCGGetNprealloc(KSP ksp, PetscInt *nprealloc)
489: {
490:   KSP_PIPEFCG *pipefcg = (KSP_PIPEFCG *)ksp->data;

493:   *nprealloc = pipefcg->nprealloc;
494:   return 0;
495: }

497: /*@
498:   KSPPIPEFCGSetTruncationType - specify how many of its stored previous directions PIPEFCG uses during orthoganalization

500:   Logically Collective on ksp

502:   KSP_FCD_TRUNC_TYPE_STANDARD uses all (up to mmax) stored directions
503:   KSP_FCD_TRUNC_TYPE_NOTAY uses max(1,mod(i,mmax)) stored directions at iteration i=0,1,..

505:   Input Parameters:
506: +  ksp - the Krylov space context
507: -  truncstrat - the choice of strategy

509:   Level: intermediate

511:   Options Database:
512: .  -ksp_pipefcg_truncation_type <standard,notay> - which stored search directions to orthogonalize against

514: .seealso: `KSPPIPEFCG`, `KSPPIPEFCGGetTruncationType`, `KSPFCDTruncationType`
515: @*/
516: PetscErrorCode KSPPIPEFCGSetTruncationType(KSP ksp, KSPFCDTruncationType truncstrat)
517: {
518:   KSP_PIPEFCG *pipefcg = (KSP_PIPEFCG *)ksp->data;

522:   pipefcg->truncstrat = truncstrat;
523:   return 0;
524: }

526: /*@
527:   KSPPIPEFCGGetTruncationType - get the truncation strategy employed by PIPEFCG

529:    Not Collective

531:    Input Parameter:
532: .  ksp - the Krylov space context

534:    Output Parameter:
535: .  truncstrat - the strategy type

537:   Options Database:
538: . -ksp_pipefcg_truncation_type <standard,notay> - which stored basis vectors to orthogonalize against

540:    Level: intermediate

542: .seealso: `KSPPIPEFCG`, `KSPPIPEFCGSetTruncationType`, `KSPFCDTruncationType`
543: @*/
544: PetscErrorCode KSPPIPEFCGGetTruncationType(KSP ksp, KSPFCDTruncationType *truncstrat)
545: {
546:   KSP_PIPEFCG *pipefcg = (KSP_PIPEFCG *)ksp->data;

549:   *truncstrat = pipefcg->truncstrat;
550:   return 0;
551: }

553: static PetscErrorCode KSPSetFromOptions_PIPEFCG(KSP ksp, PetscOptionItems *PetscOptionsObject)
554: {
555:   KSP_PIPEFCG *pipefcg = (KSP_PIPEFCG *)ksp->data;
556:   PetscInt     mmax, nprealloc;
557:   PetscBool    flg;

559:   PetscOptionsHeadBegin(PetscOptionsObject, "KSP PIPEFCG options");
560:   PetscOptionsInt("-ksp_pipefcg_mmax", "Number of search directions to storue", "KSPPIPEFCGSetMmax", pipefcg->mmax, &mmax, &flg);
561:   if (flg) KSPPIPEFCGSetMmax(ksp, mmax);
562:   PetscOptionsInt("-ksp_pipefcg_nprealloc", "Number of directions to preallocate", "KSPPIPEFCGSetNprealloc", pipefcg->nprealloc, &nprealloc, &flg);
563:   if (flg) KSPPIPEFCGSetNprealloc(ksp, nprealloc);
564:   PetscOptionsEnum("-ksp_pipefcg_truncation_type", "Truncation approach for directions", "KSPFCGSetTruncationType", KSPFCDTruncationTypes, (PetscEnum)pipefcg->truncstrat, (PetscEnum *)&pipefcg->truncstrat, NULL);
565:   PetscOptionsHeadEnd();
566:   return 0;
567: }

569: /*MC

571:   KSPPIPEFCG - Implements a Pipelined, Flexible Conjugate Gradient method.

573:   Options Database Keys:
574: +   -ksp_pipefcg_mmax <N> - The number of previous search directions to store
575: .   -ksp_pipefcg_nprealloc <N> - The number of previous search directions to preallocate
576: -   -ksp_pipefcg_truncation_type <standard,notay> - which stored search directions to orthogonalize against

578:   Notes:
579:    Supports left preconditioning only.

581:    The natural "norm" for this method is (u,Au), where u is the preconditioned residual. As with standard CG, this norm is available at no additional computational cost. Choosing preconditioned or unpreconditioned norms involve an extra blocking global reduction, thus removing any benefit from pipelining.

583:    MPI configuration may be necessary for reductions to make asynchronous progress, which is important for performance of pipelined methods.
584:    See the FAQ on the PETSc website for details.

586:   Reference:
587:     P. Sanan, S.M. Schnepp, and D.A. May,
588:     "Pipelined, Flexible Krylov Subspace Methods,"
589:     SIAM Journal on Scientific Computing 2016 38:5, C441-C470,
590:     DOI: 10.1137/15M1049130

592:   Level: intermediate

594: .seealso: `KSPFCG`, `KSPPIPECG`, `KSPPIPECR`, `KSPGCR`, `KSPPIPEGCR`, `KSPFGMRES`, `KSPCG`, `KSPPIPEFCGSetMmax()`, `KSPPIPEFCGGetMmax()`, `KSPPIPEFCGSetNprealloc()`, `KSPPIPEFCGGetNprealloc()`, `KSPPIPEFCGSetTruncationType()`, `KSPPIPEFCGGetTruncationType()`

596: M*/
597: PETSC_EXTERN PetscErrorCode KSPCreate_PIPEFCG(KSP ksp)
598: {
599:   KSP_PIPEFCG *pipefcg;

601:   PetscNew(&pipefcg);
602: #if !defined(PETSC_USE_COMPLEX)
603:   pipefcg->type = KSP_CG_SYMMETRIC;
604: #else
605:   pipefcg->type = KSP_CG_HERMITIAN;
606: #endif
607:   pipefcg->mmax       = KSPPIPEFCG_DEFAULT_MMAX;
608:   pipefcg->nprealloc  = KSPPIPEFCG_DEFAULT_NPREALLOC;
609:   pipefcg->nvecs      = 0;
610:   pipefcg->vecb       = KSPPIPEFCG_DEFAULT_VECB;
611:   pipefcg->nchunks    = 0;
612:   pipefcg->truncstrat = KSPPIPEFCG_DEFAULT_TRUNCSTRAT;
613:   pipefcg->n_restarts = 0;

615:   ksp->data = (void *)pipefcg;

617:   KSPSetSupportedNorm(ksp, KSP_NORM_PRECONDITIONED, PC_LEFT, 2);
618:   KSPSetSupportedNorm(ksp, KSP_NORM_NATURAL, PC_LEFT, 1);
619:   KSPSetSupportedNorm(ksp, KSP_NORM_UNPRECONDITIONED, PC_LEFT, 1);
620:   KSPSetSupportedNorm(ksp, KSP_NORM_NONE, PC_LEFT, 1);

622:   ksp->ops->setup          = KSPSetUp_PIPEFCG;
623:   ksp->ops->solve          = KSPSolve_PIPEFCG;
624:   ksp->ops->destroy        = KSPDestroy_PIPEFCG;
625:   ksp->ops->view           = KSPView_PIPEFCG;
626:   ksp->ops->setfromoptions = KSPSetFromOptions_PIPEFCG;
627:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
628:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
629:   return 0;
630: }