Date: 2022-03-04T17:39:06+00:00

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[] =
      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 KSPPIPEFCG_DEFAULT_MMAX 15
     23: #define KSPPIPEFCG_DEFAULT_NPREALLOC 5
     24: #define KSPPIPEFCG_DEFAULT_VECB 5
     25: #define KSPPIPEFCG_DEFAULT_TRUNCSTRAT KSP_FCD_TRUNC_TYPE_NOTAY

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

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

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

     62: static PetscErrorCode KSPSetUp_PIPEFCG(KSP ksp)
     63: {
     65:   KSP_PIPEFCG    *pipefcg;
     66:   const PetscInt nworkstd = 5;

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

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

     74:   /* Allocated space for pointers to additional work vectors
     75:    note that mmax is the number of previous directions, so we add 1 for the current direction,
     76:    and an extra 1 for the prealloc (which might be empty) */
     77:   PetscMalloc4(pipefcg->mmax+1,&(pipefcg->Pvecs),pipefcg->mmax+1,&(pipefcg->pPvecs),pipefcg->mmax+1,&(pipefcg->Svecs),pipefcg->mmax+1,&(pipefcg->pSvecs));
     78:   PetscMalloc4(pipefcg->mmax+1,&(pipefcg->Qvecs),pipefcg->mmax+1,&(pipefcg->pQvecs),pipefcg->mmax+1,&(pipefcg->ZETAvecs),pipefcg->mmax+1,&(pipefcg->pZETAvecs));
     79:   PetscMalloc4(pipefcg->mmax+1,&(pipefcg->Pold),pipefcg->mmax+1,&(pipefcg->Sold),pipefcg->mmax+1,&(pipefcg->Qold),pipefcg->mmax+1,&(pipefcg->ZETAold));
     80:   PetscMalloc1(pipefcg->mmax+1,&(pipefcg->chunksizes));
     81:   PetscMalloc3(pipefcg->mmax+2,&(pipefcg->dots),pipefcg->mmax+1,&(pipefcg->etas),pipefcg->mmax+2,&(pipefcg->redux));

     83:   /* If the requested number of preallocated vectors is greater than mmax reduce nprealloc */
     84:   if (pipefcg->nprealloc > pipefcg->mmax+1) {
     85:     PetscInfo2(NULL,"Requested nprealloc=%d is greater than m_max+1=%d. Resetting nprealloc = m_max+1.\n",pipefcg->nprealloc, pipefcg->mmax+1);
     86:   }

     88:   /* Preallocate additional work vectors */
     89:   KSPAllocateVectors_PIPEFCG(ksp,pipefcg->nprealloc,pipefcg->nprealloc);

     91:   PetscLogObjectMemory((PetscObject)ksp,(pipefcg->mmax+1)*4*sizeof(Vec*)+(pipefcg->mmax+1)*4*sizeof(Vec**)+(pipefcg->mmax+1)*4*sizeof(Vec*)+
     92:     (pipefcg->mmax+1)*sizeof(PetscInt)+(pipefcg->mmax+2)*sizeof(Vec*)+(pipefcg->mmax+2)*sizeof(PetscScalar)+(pipefcg->mmax+1)*sizeof(PetscReal));
     93:   return(0);
     94: }

     96: static PetscErrorCode KSPSolve_PIPEFCG_cycle(KSP ksp)
     97: {
     99:   PetscInt       i,j,k,idx,kdx,mi;
    100:   KSP_PIPEFCG    *pipefcg;
    101:   PetscScalar    alpha=0.0,gamma,*betas,*dots;
    102:   PetscReal      dp=0.0, delta,*eta,*etas;
    103:   Vec            B,R,Z,X,Qcurr,W,ZETAcurr,M,N,Pcurr,Scurr,*redux;
    104:   Mat            Amat,Pmat;

    107:   /* We have not checked these routines for use with complex numbers. The inner products
    108:      are likely not defined correctly for that case */
    109:   if (PetscDefined(USE_COMPLEX) && !PetscDefined(SKIP_COMPLEX)) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"PIPEFGMRES has not been implemented for use with complex scalars");

    111: #define VecXDot(x,y,a)         (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecDot       (x,y,a)   : VecTDot       (x,y,a))
    112: #define VecXDotBegin(x,y,a)    (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecDotBegin  (x,y,a)   : VecTDotBegin  (x,y,a))
    113: #define VecXDotEnd(x,y,a)      (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecDotEnd    (x,y,a)   : VecTDotEnd    (x,y,a))
    114: #define VecMXDot(x,n,y,a)      (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecMDot      (x,n,y,a) : VecMTDot      (x,n,y,a))
    115: #define VecMXDotBegin(x,n,y,a) (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecMDotBegin (x,n,y,a) : VecMTDotBegin (x,n,y,a))
    116: #define VecMXDotEnd(x,n,y,a)   (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecMDotEnd   (x,n,y,a) : VecMTDotEnd   (x,n,y,a))

    118:   pipefcg       = (KSP_PIPEFCG*)ksp->data;
    119:   X             = ksp->vec_sol;
    120:   B             = ksp->vec_rhs;
    121:   R             = ksp->work[0];
    122:   Z             = ksp->work[1];
    123:   W             = ksp->work[2];
    124:   M             = ksp->work[3];
    125:   N             = ksp->work[4];

    127:   redux = pipefcg->redux;
    128:   dots  = pipefcg->dots;
    129:   etas  = pipefcg->etas;
    130:   betas = dots;        /* dots takes the result of all dot products of which the betas are a subset */

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

    134:   /* Compute cycle initial residual */
    135:   KSP_MatMult(ksp,Amat,X,R);
    136:   VecAYPX(R,-1.0,B);                   /* r <- b - Ax */
    137:   KSP_PCApply(ksp,R,Z);                /* z <- Br     */

    139:   Pcurr = pipefcg->Pvecs[0];
    140:   Scurr = pipefcg->Svecs[0];
    141:   Qcurr = pipefcg->Qvecs[0];
    142:   ZETAcurr = pipefcg->ZETAvecs[0];
    143:   VecCopy(Z,Pcurr);
    144:   KSP_MatMult(ksp,Amat,Pcurr,Scurr);  /* S = Ap     */
    145:   VecCopy(Scurr,W);                   /* w = s = Az */

    147:   /* Initial state of pipelining intermediates */
    148:   redux[0] = R;
    149:   redux[1] = W;
    150:   VecMXDotBegin(Z,2,redux,dots);
    151:   PetscCommSplitReductionBegin(PetscObjectComm((PetscObject)Z)); /* perform asynchronous reduction */
    152:   KSP_PCApply(ksp,W,M);            /* m = B(w) */
    153:   KSP_MatMult(ksp,Amat,M,N);       /* n = Am   */
    154:   VecCopy(M,Qcurr);                /* q = m    */
    155:   VecCopy(N,ZETAcurr);             /* zeta = n */
    156:   VecMXDotEnd(Z,2,redux,dots);
    157:   gamma    = dots[0];
    158:   delta    = PetscRealPart(dots[1]);
    159:   etas[0]  = delta;
    160:   alpha    = gamma/delta;

    162:   i = 0;
    163:   do {
    164:     ksp->its++;

    166:     /* Update X, R, Z, W */
    167:     VecAXPY(X,+alpha,Pcurr);           /* x <- x + alpha * pi    */
    168:     VecAXPY(R,-alpha,Scurr);           /* r <- r - alpha * si    */
    169:     VecAXPY(Z,-alpha,Qcurr);           /* z <- z - alpha * qi    */
    170:     VecAXPY(W,-alpha,ZETAcurr);        /* w <- w - alpha * zetai */

    172:     /* Compute norm for convergence check */
    173:     switch (ksp->normtype) {
    174:       case KSP_NORM_PRECONDITIONED:
    175:         VecNorm(Z,NORM_2,&dp);         /* dp <- sqrt(z'*z) = sqrt(e'*A'*B'*B*A*e) */
    176:         break;
    177:       case KSP_NORM_UNPRECONDITIONED:
    178:         VecNorm(R,NORM_2,&dp);         /* dp <- sqrt(r'*r) = sqrt(e'*A'*A*e)      */
    179:         break;
    180:       case KSP_NORM_NATURAL:
    181:         dp = PetscSqrtReal(PetscAbsScalar(gamma));          /* dp <- sqrt(r'*z) = sqrt(e'*A'*B*A*e)    */
    182:         break;
    183:       case KSP_NORM_NONE:
    184:         dp = 0.0;
    185:         break;
    186:       default: SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"%s",KSPNormTypes[ksp->normtype]);
    187:     }

    189:     /* Check for convergence */
    190:     ksp->rnorm = dp;
    191:     KSPLogResidualHistory(ksp,dp);
    192:     KSPMonitor(ksp,ksp->its,dp);
    193:     (*ksp->converged)(ksp,ksp->its,dp,&ksp->reason,ksp->cnvP);
    194:     if (ksp->reason) return(0);

    196:     /* Computations of current iteration done */
    197:     ++i;

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

    202:     /* Note that we wrap around and start clobbering old vectors */
    203:     idx = i % (pipefcg->mmax+1);
    204:     Pcurr    = pipefcg->Pvecs[idx];
    205:     Scurr    = pipefcg->Svecs[idx];
    206:     Qcurr    = pipefcg->Qvecs[idx];
    207:     ZETAcurr = pipefcg->ZETAvecs[idx];
    208:     eta      = pipefcg->etas+idx;

    210:     /* number of old directions to orthogonalize against */
    211:     switch(pipefcg->truncstrat) {
    212:       case KSP_FCD_TRUNC_TYPE_STANDARD:
    213:         mi = pipefcg->mmax;
    214:         break;
    215:       case KSP_FCD_TRUNC_TYPE_NOTAY:
    216:         mi = ((i-1) % pipefcg->mmax)+1;
    217:         break;
    218:       default:
    219:         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unrecognized Truncation Strategy");
    220:     }

    222:     /* Pick old p,s,q,zeta in a way suitable for VecMDot */
    223:     VecCopy(Z,Pcurr);
    224:     for (k=PetscMax(0,i-mi),j=0;k<i;++j,++k) {
    225:       kdx = k % (pipefcg->mmax+1);
    226:       pipefcg->Pold[j]    = pipefcg->Pvecs[kdx];
    227:       pipefcg->Sold[j]    = pipefcg->Svecs[kdx];
    228:       pipefcg->Qold[j]    = pipefcg->Qvecs[kdx];
    229:       pipefcg->ZETAold[j] = pipefcg->ZETAvecs[kdx];
    230:       redux[j]            = pipefcg->Svecs[kdx];
    231:     }
    232:     redux[j]   = R;   /* If the above loop is not executed redux contains only R => all beta_k = 0, only gamma, delta != 0 */
    233:     redux[j+1] = W;

    235:     VecMXDotBegin(Z,j+2,redux,betas);  /* Start split reductions for beta_k = (z,s_k), gamma = (z,r), delta = (z,w) */
    236:     PetscCommSplitReductionBegin(PetscObjectComm((PetscObject)Z)); /* perform asynchronous reduction */
    237:     VecWAXPY(N,-1.0,R,W);              /* m = u + B(w-r): (a) ntmp = w-r              */
    238:     KSP_PCApply(ksp,N,M);              /* m = u + B(w-r): (b) mtmp = B(ntmp) = B(w-r) */
    239:     VecAXPY(M,1.0,Z);                  /* m = u + B(w-r): (c) m = z + mtmp            */
    240:     KSP_MatMult(ksp,Amat,M,N);         /* n = Am                                      */
    241:     VecMXDotEnd(Z,j+2,redux,betas);    /* Finish split reductions */
    242:     gamma = betas[j];
    243:     delta = PetscRealPart(betas[j+1]);

    245:     *eta = 0.;
    246:     for (k=PetscMax(0,i-mi),j=0;k<i;++j,++k) {
    247:       kdx = k % (pipefcg->mmax+1);
    248:       betas[j] /= -etas[kdx];                               /* betak  /= etak */
    249:       *eta -= ((PetscReal)(PetscAbsScalar(betas[j])*PetscAbsScalar(betas[j]))) * etas[kdx];
    250:                                                             /* etaitmp = -betaik^2 * etak */
    251:     }
    252:     *eta += delta;                                          /* etai    = delta -betaik^2 * etak */
    253:     if (*eta < 0.) {
    254:       pipefcg->norm_breakdown = PETSC_TRUE;
    255:       PetscInfo1(ksp,"Restart due to square root breakdown at it = \n",ksp->its);
    256:       break;
    257:     } else {
    258:       alpha= gamma/(*eta);                                  /* alpha = gamma/etai */
    259:     }

    261:     /* project out stored search directions using classical G-S */
    262:     VecCopy(Z,Pcurr);
    263:     VecCopy(W,Scurr);
    264:     VecCopy(M,Qcurr);
    265:     VecCopy(N,ZETAcurr);
    266:     VecMAXPY(Pcurr   ,j,betas,pipefcg->Pold);    /* pi    <- ui - sum_k beta_k p_k    */
    267:     VecMAXPY(Scurr   ,j,betas,pipefcg->Sold);    /* si    <- wi - sum_k beta_k s_k    */
    268:     VecMAXPY(Qcurr   ,j,betas,pipefcg->Qold);    /* qi    <- m  - sum_k beta_k q_k    */
    269:     VecMAXPY(ZETAcurr,j,betas,pipefcg->ZETAold); /* zetai <- n  - sum_k beta_k zeta_k */

    271:   } while (ksp->its < ksp->max_it);
    272:   if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
    273:   return(0);
    274: }

    276: static PetscErrorCode KSPSolve_PIPEFCG(KSP ksp)
    277: {
    279:   KSP_PIPEFCG    *pipefcg;
    280:   PetscScalar    gamma;
    281:   PetscReal      dp=0.0;
    282:   Vec            B,R,Z,X;
    283:   Mat            Amat,Pmat;

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

    288:   PetscCitationsRegister(citation,&cited);

    290:   pipefcg       = (KSP_PIPEFCG*)ksp->data;
    291:   X             = ksp->vec_sol;
    292:   B             = ksp->vec_rhs;
    293:   R             = ksp->work[0];
    294:   Z             = ksp->work[1];

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

    298:   /* Compute initial residual needed for convergence check*/
    299:   ksp->its = 0;
    300:   if (!ksp->guess_zero) {
    301:     KSP_MatMult(ksp,Amat,X,R);
    302:     VecAYPX(R,-1.0,B);                 /* r <- b - Ax                             */
    303:   } else {
    304:     VecCopy(B,R);                      /* r <- b (x is 0)                         */
    305:   }
    306:   switch (ksp->normtype) {
    307:     case KSP_NORM_PRECONDITIONED:
    308:       KSP_PCApply(ksp,R,Z);            /* z <- Br                                 */
    309:       VecNorm(Z,NORM_2,&dp);           /* dp <- dqrt(z'*z) = sqrt(e'*A'*B'*B*A*e) */
    310:       break;
    311:     case KSP_NORM_UNPRECONDITIONED:
    312:       VecNorm(R,NORM_2,&dp);           /* dp <- sqrt(r'*r) = sqrt(e'*A'*A*e)      */
    313:       break;
    314:     case KSP_NORM_NATURAL:
    315:       KSP_PCApply(ksp,R,Z);            /* z <- Br                                 */
    316:       VecXDot(Z,R,&gamma);
    317:       dp = PetscSqrtReal(PetscAbsScalar(gamma));            /* dp <- sqrt(r'*z) = sqrt(e'*A'*B*A*e)    */
    318:       break;
    319:     case KSP_NORM_NONE:
    320:       dp = 0.0;
    321:       break;
    322:     default: SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"%s",KSPNormTypes[ksp->normtype]);
    323:   }

    325:   /* Initial Convergence Check */
    326:   KSPLogResidualHistory(ksp,dp);
    327:   KSPMonitor(ksp,0,dp);
    328:   ksp->rnorm = dp;
    329:   (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);
    330:   if (ksp->reason) return(0);

    332:   do {
    333:     /* A cycle is broken only if a norm breakdown occurs. If not the entire solve happens in a single cycle.
    334:        This is coded this way to allow both truncation and truncation-restart strategy
    335:        (see KSPFCDGetNumOldDirections()) */
    336:     KSPSolve_PIPEFCG_cycle(ksp);
    337:     if (ksp->reason) return(0);
    338:     if (pipefcg->norm_breakdown) {
    339:       pipefcg->n_restarts++;
    340:       pipefcg->norm_breakdown = PETSC_FALSE;
    341:     }
    342:   } while (ksp->its < ksp->max_it);

    344:   if (ksp->its >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
    345:   return(0);
    346: }

    348: static PetscErrorCode KSPDestroy_PIPEFCG(KSP ksp)
    349: {
    351:   PetscInt       i;
    352:   KSP_PIPEFCG    *pipefcg;

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

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

    360:   /* Destroy vectors of old directions and the arrays that manage pointers to them */
    361:   if (pipefcg->nvecs) {
    362:     for (i=0;i<pipefcg->nchunks;++i) {
    363:       VecDestroyVecs(pipefcg->chunksizes[i],&pipefcg->pPvecs[i]);
    364:       VecDestroyVecs(pipefcg->chunksizes[i],&pipefcg->pSvecs[i]);
    365:       VecDestroyVecs(pipefcg->chunksizes[i],&pipefcg->pQvecs[i]);
    366:       VecDestroyVecs(pipefcg->chunksizes[i],&pipefcg->pZETAvecs[i]);
    367:     }
    368:   }
    369:   PetscFree4(pipefcg->Pvecs,pipefcg->Svecs,pipefcg->pPvecs,pipefcg->pSvecs);
    370:   PetscFree4(pipefcg->Qvecs,pipefcg->ZETAvecs,pipefcg->pQvecs,pipefcg->pZETAvecs);
    371:   PetscFree4(pipefcg->Pold,pipefcg->Sold,pipefcg->Qold,pipefcg->ZETAold);
    372:   PetscFree(pipefcg->chunksizes);
    373:   PetscFree3(pipefcg->dots,pipefcg->etas,pipefcg->redux);
    374:   KSPDestroyDefault(ksp);
    375:   return(0);
    376: }

    378: static PetscErrorCode KSPView_PIPEFCG(KSP ksp,PetscViewer viewer)
    379: {
    380:   KSP_PIPEFCG    *pipefcg = (KSP_PIPEFCG*)ksp->data;
    382:   PetscBool      iascii,isstring;
    383:   const char     *truncstr;

    386:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    387:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);

    389:   if (pipefcg->truncstrat == KSP_FCD_TRUNC_TYPE_STANDARD) {
    390:     truncstr = "Using standard truncation strategy";
    391:   } else if (pipefcg->truncstrat == KSP_FCD_TRUNC_TYPE_NOTAY) {
    392:     truncstr = "Using Notay's truncation strategy";
    393:   } else {
    394:     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Undefined FCD truncation strategy");
    395:   }

    397:   if (iascii) {
    398:     PetscViewerASCIIPrintf(viewer,"  max previous directions = %D\n",pipefcg->mmax);
    399:     PetscViewerASCIIPrintf(viewer,"  preallocated %D directions\n",PetscMin(pipefcg->nprealloc,pipefcg->mmax+1));
    400:     PetscViewerASCIIPrintf(viewer,"  %s\n",truncstr);
    401:     PetscViewerASCIIPrintf(viewer,"  restarts performed = %D \n", pipefcg->n_restarts);
    402:   } else if (isstring) {
    403:     PetscViewerStringSPrintf(viewer,
    404:       "max previous directions = %D, preallocated %D directions, %s truncation strategy",
    405:       pipefcg->mmax,pipefcg->nprealloc,truncstr);
    406:   }
    407:   return(0);
    408: }

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

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

    417:   Logically Collective on ksp

    419:   Input Parameters:
    420: +  ksp - the Krylov space context
    421: -  mmax - the maximum number of previous directions to orthogonalize against

    423:   Level: intermediate

    425:   Options Database:
    426: . -ksp_pipefcg_mmax <N>

    428: .seealso: KSPPIPEFCG, KSPPIPEFCGSetTruncationType(), KSPPIPEFCGSetNprealloc()
    429: @*/
    430: PetscErrorCode KSPPIPEFCGSetMmax(KSP ksp,PetscInt mmax)
    431: {
    432:   KSP_PIPEFCG *pipefcg=(KSP_PIPEFCG*)ksp->data;

    437:   pipefcg->mmax=mmax;
    438:   return(0);
    439: }

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

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

    446:    Not Collective

    448:    Input Parameter:
    449: .  ksp - the Krylov space context

    451:    Output Parameter:
    452: .  mmax - the maximum number of previous directons allowed for orthogonalization

    454:   Options Database:
    455: . -ksp_pipefcg_mmax <N>

    457:    Level: intermediate

    459: .seealso: KSPPIPEFCG, KSPPIPEFCGGetTruncationType(), KSPPIPEFCGGetNprealloc(), KSPPIPEFCGSetMmax()
    460: @*/
    461: PetscErrorCode KSPPIPEFCGGetMmax(KSP ksp,PetscInt *mmax)
    462: {
    463:   KSP_PIPEFCG *pipefcg=(KSP_PIPEFCG*)ksp->data;

    467:   *mmax=pipefcg->mmax;
    468:   return(0);
    469: }

    471: /*@
    472:   KSPPIPEFCGSetNprealloc - set the number of directions to preallocate with PIPEFCG

    474:   Logically Collective on ksp

    476:   Input Parameters:
    477: +  ksp - the Krylov space context
    478: -  nprealloc - the number of vectors to preallocate

    480:   Level: advanced

    482:   Options Database:
    483: . -ksp_pipefcg_nprealloc <N>

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

    494:   pipefcg->nprealloc = nprealloc;
    495:   return(0);
    496: }

    498: /*@
    499:   KSPPIPEFCGGetNprealloc - get the number of directions to preallocate by PIPEFCG

    501:    Not Collective

    503:    Input Parameter:
    504: .  ksp - the Krylov space context

    506:    Output Parameter:
    507: .  nprealloc - the number of directions preallocated

    509:   Options Database:
    510: . -ksp_pipefcg_nprealloc <N>

    512:    Level: advanced

    514: .seealso: KSPPIPEFCG, KSPPIPEFCGGetTruncationType(), KSPPIPEFCGSetNprealloc()
    515: @*/
    516: PetscErrorCode KSPPIPEFCGGetNprealloc(KSP ksp,PetscInt *nprealloc)
    517: {
    518:   KSP_PIPEFCG *pipefcg=(KSP_PIPEFCG*)ksp->data;

    522:   *nprealloc = pipefcg->nprealloc;
    523:   return(0);
    524: }

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

    529:   Logically Collective on ksp

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

    534:   Input Parameters:
    535: +  ksp - the Krylov space context
    536: -  truncstrat - the choice of strategy

    538:   Level: intermediate

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

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

    552:   pipefcg->truncstrat=truncstrat;
    553:   return(0);
    554: }

    556: /*@
    557:   KSPPIPEFCGGetTruncationType - get the truncation strategy employed by PIPEFCG

    559:    Not Collective

    561:    Input Parameter:
    562: .  ksp - the Krylov space context

    564:    Output Parameter:
    565: .  truncstrat - the strategy type

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

    570:    Level: intermediate

    572: .seealso: KSPPIPEFCG, KSPPIPEFCGSetTruncationType, KSPFCDTruncationType
    573: @*/
    574: PetscErrorCode KSPPIPEFCGGetTruncationType(KSP ksp,KSPFCDTruncationType *truncstrat)
    575: {
    576:   KSP_PIPEFCG *pipefcg=(KSP_PIPEFCG*)ksp->data;

    580:   *truncstrat=pipefcg->truncstrat;
    581:   return(0);
    582: }

    584: static PetscErrorCode KSPSetFromOptions_PIPEFCG(PetscOptionItems *PetscOptionsObject,KSP ksp)
    585: {
    587:   KSP_PIPEFCG    *pipefcg=(KSP_PIPEFCG*)ksp->data;
    588:   PetscInt       mmax,nprealloc;
    589:   PetscBool      flg;

    592:   PetscOptionsHead(PetscOptionsObject,"KSP PIPEFCG options");
    593:   PetscOptionsInt("-ksp_pipefcg_mmax","Number of search directions to storue","KSPPIPEFCGSetMmax",pipefcg->mmax,&mmax,&flg);
    594:   if (flg) {KSPPIPEFCGSetMmax(ksp,mmax);}
    595:   PetscOptionsInt("-ksp_pipefcg_nprealloc","Number of directions to preallocate","KSPPIPEFCGSetNprealloc",pipefcg->nprealloc,&nprealloc,&flg);
    596:   if (flg) {KSPPIPEFCGSetNprealloc(ksp,nprealloc);}
    597:   PetscOptionsEnum("-ksp_pipefcg_truncation_type","Truncation approach for directions","KSPFCGSetTruncationType",KSPFCDTruncationTypes,(PetscEnum)pipefcg->truncstrat,(PetscEnum*)&pipefcg->truncstrat,NULL);
    598:   PetscOptionsTail();
    599:   return(0);
    600: }

    602: /*MC

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

    606:   Options Database Keys:
    607: +   -ksp_pipefcg_mmax <N> - The number of previous search directions to store
    608: .   -ksp_pipefcg_nprealloc <N> - The number of previous search directions to preallocate
    609: -   -ksp_pipefcg_truncation_type <standard,notay> - which stored search directions to orthogonalize against

    611:   Notes:
    612:    Supports left preconditioning only.

    614:    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.

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

    619:   Reference:
    620:     P. Sanan, S.M. Schnepp, and D.A. May,
    621:     "Pipelined, Flexible Krylov Subspace Methods,"
    622:     SIAM Journal on Scientific Computing 2016 38:5, C441-C470,
    623:     DOI: 10.1137/15M1049130

    625:   Level: intermediate

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

    629: M*/
    630: PETSC_EXTERN PetscErrorCode KSPCreate_PIPEFCG(KSP ksp)
    631: {
    633:   KSP_PIPEFCG    *pipefcg;

    636:   PetscNewLog(ksp,&pipefcg);
    637: #if !defined(PETSC_USE_COMPLEX)
    638:   pipefcg->type       = KSP_CG_SYMMETRIC;
    639: #else
    640:   pipefcg->type       = KSP_CG_HERMITIAN;
    641: #endif
    642:   pipefcg->mmax       = KSPPIPEFCG_DEFAULT_MMAX;
    643:   pipefcg->nprealloc  = KSPPIPEFCG_DEFAULT_NPREALLOC;
    644:   pipefcg->nvecs      = 0;
    645:   pipefcg->vecb       = KSPPIPEFCG_DEFAULT_VECB;
    646:   pipefcg->nchunks    = 0;
    647:   pipefcg->truncstrat = KSPPIPEFCG_DEFAULT_TRUNCSTRAT;
    648:   pipefcg->n_restarts = 0;

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

    652:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,2);
    653:   KSPSetSupportedNorm(ksp,KSP_NORM_NATURAL,PC_LEFT,1);
    654:   KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_LEFT,1);
    655:   KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_LEFT,1);

    657:   ksp->ops->setup          = KSPSetUp_PIPEFCG;
    658:   ksp->ops->solve          = KSPSolve_PIPEFCG;
    659:   ksp->ops->destroy        = KSPDestroy_PIPEFCG;
    660:   ksp->ops->view           = KSPView_PIPEFCG;
    661:   ksp->ops->setfromoptions = KSPSetFromOptions_PIPEFCG;
    662:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
    663:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
    664:   return(0);
    665: }