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:   PetscInt        i;
     30:   KSP_PIPEFCG     *pipefcg;
     31:   PetscInt        nnewvecs, nvecsprev;

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

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

     60: static PetscErrorCode KSPSetUp_PIPEFCG(KSP ksp)
     61: {
     63:   KSP_PIPEFCG    *pipefcg;
     64:   const PetscInt nworkstd = 5;

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

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

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

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

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

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

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

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

    106: #define VecXDot(x,y,a)         (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecDot       (x,y,a)   : VecTDot       (x,y,a))
    107: #define VecXDotBegin(x,y,a)    (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecDotBegin  (x,y,a)   : VecTDotBegin  (x,y,a))
    108: #define VecXDotEnd(x,y,a)      (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecDotEnd    (x,y,a)   : VecTDotEnd    (x,y,a))
    109: #define VecMXDot(x,n,y,a)      (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecMDot      (x,n,y,a) : VecMTDot      (x,n,y,a))
    110: #define VecMXDotBegin(x,n,y,a) (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecMDotBegin (x,n,y,a) : VecMTDotBegin (x,n,y,a))
    111: #define VecMXDotEnd(x,n,y,a)   (((pipefcg->type) == (KSP_CG_HERMITIAN)) ? VecMDotEnd   (x,n,y,a) : VecMTDotEnd   (x,n,y,a))

    113:   pipefcg       = (KSP_PIPEFCG*)ksp->data;
    114:   X             = ksp->vec_sol;
    115:   B             = ksp->vec_rhs;
    116:   R             = ksp->work[0];
    117:   Z             = ksp->work[1];
    118:   W             = ksp->work[2];
    119:   M             = ksp->work[3];
    120:   N             = ksp->work[4];

    122:   redux = pipefcg->redux;
    123:   dots  = pipefcg->dots;
    124:   etas  = pipefcg->etas;
    125:   betas = dots;        /* dots takes the result of all dot products of which the betas are a subset */

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

    129:   /* Compute cycle initial residual */
    130:   KSP_MatMult(ksp,Amat,X,R);
    131:   VecAYPX(R,-1.0,B);                   /* r <- b - Ax */
    132:   KSP_PCApply(ksp,R,Z);                /* z <- Br     */

    134:   Pcurr = pipefcg->Pvecs[0];
    135:   Scurr = pipefcg->Svecs[0];
    136:   Qcurr = pipefcg->Qvecs[0];
    137:   ZETAcurr = pipefcg->ZETAvecs[0];
    138:   VecCopy(Z,Pcurr);
    139:   KSP_MatMult(ksp,Amat,Pcurr,Scurr);  /* S = Ap     */
    140:   VecCopy(Scurr,W);                   /* w = s = Az */

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

    157:   i = 0;
    158:   do {
    159:     ksp->its++;

    161:     /* Update X, R, Z, W */
    162:     VecAXPY(X,+alpha,Pcurr);           /* x <- x + alpha * pi    */
    163:     VecAXPY(R,-alpha,Scurr);           /* r <- r - alpha * si    */
    164:     VecAXPY(Z,-alpha,Qcurr);           /* z <- z - alpha * qi    */
    165:     VecAXPY(W,-alpha,ZETAcurr);        /* w <- w - alpha * zetai */

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

    184:     /* Check for convergence */
    185:     ksp->rnorm = dp;
    186:     KSPLogResidualHistory(ksp,dp);
    187:     KSPMonitor(ksp,ksp->its,dp);
    188:     (*ksp->converged)(ksp,ksp->its,dp,&ksp->reason,ksp->cnvP);
    189:     if (ksp->reason) return 0;

    191:     /* Computations of current iteration done */
    192:     ++i;

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

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

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

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

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

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

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

    266:   } while (ksp->its < ksp->max_it);
    267:   if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
    268:   return 0;
    269: }

    271: static PetscErrorCode KSPSolve_PIPEFCG(KSP ksp)
    272: {
    273:   KSP_PIPEFCG    *pipefcg;
    274:   PetscScalar    gamma;
    275:   PetscReal      dp=0.0;
    276:   Vec            B,R,Z,X;
    277:   Mat            Amat,Pmat;

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

    281:   PetscCitationsRegister(citation,&cited);

    283:   pipefcg       = (KSP_PIPEFCG*)ksp->data;
    284:   X             = ksp->vec_sol;
    285:   B             = ksp->vec_rhs;
    286:   R             = ksp->work[0];
    287:   Z             = ksp->work[1];

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

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

    318:   /* Initial Convergence Check */
    319:   KSPLogResidualHistory(ksp,dp);
    320:   KSPMonitor(ksp,0,dp);
    321:   ksp->rnorm = dp;
    322:   (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);
    323:   if (ksp->reason) return 0;

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

    337:   if (ksp->its >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
    338:   return 0;
    339: }

    341: static PetscErrorCode KSPDestroy_PIPEFCG(KSP ksp)
    342: {
    343:   PetscInt       i;
    344:   KSP_PIPEFCG    *pipefcg;

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

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

    351:   /* Destroy vectors of old directions and the arrays that manage pointers to them */
    352:   if (pipefcg->nvecs) {
    353:     for (i=0;i<pipefcg->nchunks;++i) {
    354:       VecDestroyVecs(pipefcg->chunksizes[i],&pipefcg->pPvecs[i]);
    355:       VecDestroyVecs(pipefcg->chunksizes[i],&pipefcg->pSvecs[i]);
    356:       VecDestroyVecs(pipefcg->chunksizes[i],&pipefcg->pQvecs[i]);
    357:       VecDestroyVecs(pipefcg->chunksizes[i],&pipefcg->pZETAvecs[i]);
    358:     }
    359:   }
    360:   PetscFree4(pipefcg->Pvecs,pipefcg->Svecs,pipefcg->pPvecs,pipefcg->pSvecs);
    361:   PetscFree4(pipefcg->Qvecs,pipefcg->ZETAvecs,pipefcg->pQvecs,pipefcg->pZETAvecs);
    362:   PetscFree4(pipefcg->Pold,pipefcg->Sold,pipefcg->Qold,pipefcg->ZETAold);
    363:   PetscFree(pipefcg->chunksizes);
    364:   PetscFree3(pipefcg->dots,pipefcg->etas,pipefcg->redux);
    365:   KSPDestroyDefault(ksp);
    366:   return 0;
    367: }

    369: static PetscErrorCode KSPView_PIPEFCG(KSP ksp,PetscViewer viewer)
    370: {
    371:   KSP_PIPEFCG    *pipefcg = (KSP_PIPEFCG*)ksp->data;
    373:   PetscBool      iascii,isstring;
    374:   const char     *truncstr;

    376:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    377:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);

    379:   if (pipefcg->truncstrat == KSP_FCD_TRUNC_TYPE_STANDARD) {
    380:     truncstr = "Using standard truncation strategy";
    381:   } else if (pipefcg->truncstrat == KSP_FCD_TRUNC_TYPE_NOTAY) {
    382:     truncstr = "Using Notay's truncation strategy";
    383:   } else {
    384:     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Undefined FCD truncation strategy");
    385:   }

    387:   if (iascii) {
    388:     PetscViewerASCIIPrintf(viewer,"  max previous directions = %D\n",pipefcg->mmax);
    389:     PetscViewerASCIIPrintf(viewer,"  preallocated %D directions\n",PetscMin(pipefcg->nprealloc,pipefcg->mmax+1));
    390:     PetscViewerASCIIPrintf(viewer,"  %s\n",truncstr);
    391:     PetscViewerASCIIPrintf(viewer,"  restarts performed = %D \n", pipefcg->n_restarts);
    392:   } else if (isstring) {
    393:     PetscViewerStringSPrintf(viewer,
    394:       "max previous directions = %D, preallocated %D directions, %s truncation strategy",
    395:       pipefcg->mmax,pipefcg->nprealloc,truncstr);
    396:   }
    397:   return 0;
    398: }

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

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

    407:   Logically Collective on ksp

    409:   Input Parameters:
    410: +  ksp - the Krylov space context
    411: -  mmax - the maximum number of previous directions to orthogonalize against

    413:   Level: intermediate

    415:   Options Database:
    416: . -ksp_pipefcg_mmax <N> - maximum number of previous directions

    418: .seealso: KSPPIPEFCG, KSPPIPEFCGSetTruncationType(), KSPPIPEFCGSetNprealloc()
    419: @*/
    420: PetscErrorCode KSPPIPEFCGSetMmax(KSP ksp,PetscInt mmax)
    421: {
    422:   KSP_PIPEFCG *pipefcg=(KSP_PIPEFCG*)ksp->data;

    426:   pipefcg->mmax=mmax;
    427:   return 0;
    428: }

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

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

    435:    Not Collective

    437:    Input Parameter:
    438: .  ksp - the Krylov space context

    440:    Output Parameter:
    441: .  mmax - the maximum number of previous directions allowed for orthogonalization

    443:   Options Database:
    444: . -ksp_pipefcg_mmax <N> - maximum number of previous directions

    446:    Level: intermediate

    448: .seealso: KSPPIPEFCG, KSPPIPEFCGGetTruncationType(), KSPPIPEFCGGetNprealloc(), KSPPIPEFCGSetMmax()
    449: @*/
    450: PetscErrorCode KSPPIPEFCGGetMmax(KSP ksp,PetscInt *mmax)
    451: {
    452:   KSP_PIPEFCG *pipefcg=(KSP_PIPEFCG*)ksp->data;

    455:   *mmax=pipefcg->mmax;
    456:   return 0;
    457: }

    459: /*@
    460:   KSPPIPEFCGSetNprealloc - set the number of directions to preallocate with PIPEFCG

    462:   Logically Collective on ksp

    464:   Input Parameters:
    465: +  ksp - the Krylov space context
    466: -  nprealloc - the number of vectors to preallocate

    468:   Level: advanced

    470:   Options Database:
    471: . -ksp_pipefcg_nprealloc <N> - the number of vectors to preallocate

    473: .seealso: KSPPIPEFCG, KSPPIPEFCGSetTruncationType(), KSPPIPEFCGGetNprealloc()
    474: @*/
    475: PetscErrorCode KSPPIPEFCGSetNprealloc(KSP ksp,PetscInt nprealloc)
    476: {
    477:   KSP_PIPEFCG *pipefcg=(KSP_PIPEFCG*)ksp->data;

    481:   pipefcg->nprealloc = nprealloc;
    482:   return 0;
    483: }

    485: /*@
    486:   KSPPIPEFCGGetNprealloc - get the number of directions to preallocate by PIPEFCG

    488:    Not Collective

    490:    Input Parameter:
    491: .  ksp - the Krylov space context

    493:    Output Parameter:
    494: .  nprealloc - the number of directions preallocated

    496:    Level: advanced

    498: .seealso: KSPPIPEFCG, KSPPIPEFCGGetTruncationType(), KSPPIPEFCGSetNprealloc()
    499: @*/
    500: PetscErrorCode KSPPIPEFCGGetNprealloc(KSP ksp,PetscInt *nprealloc)
    501: {
    502:   KSP_PIPEFCG *pipefcg=(KSP_PIPEFCG*)ksp->data;

    505:   *nprealloc = pipefcg->nprealloc;
    506:   return 0;
    507: }

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

    512:   Logically Collective on ksp

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

    517:   Input Parameters:
    518: +  ksp - the Krylov space context
    519: -  truncstrat - the choice of strategy

    521:   Level: intermediate

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

    526: .seealso: KSPPIPEFCG, KSPPIPEFCGGetTruncationType, KSPFCDTruncationType
    527: @*/
    528: PetscErrorCode KSPPIPEFCGSetTruncationType(KSP ksp,KSPFCDTruncationType truncstrat)
    529: {
    530:   KSP_PIPEFCG *pipefcg=(KSP_PIPEFCG*)ksp->data;

    534:   pipefcg->truncstrat=truncstrat;
    535:   return 0;
    536: }

    538: /*@
    539:   KSPPIPEFCGGetTruncationType - get the truncation strategy employed by PIPEFCG

    541:    Not Collective

    543:    Input Parameter:
    544: .  ksp - the Krylov space context

    546:    Output Parameter:
    547: .  truncstrat - the strategy type

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

    552:    Level: intermediate

    554: .seealso: KSPPIPEFCG, KSPPIPEFCGSetTruncationType, KSPFCDTruncationType
    555: @*/
    556: PetscErrorCode KSPPIPEFCGGetTruncationType(KSP ksp,KSPFCDTruncationType *truncstrat)
    557: {
    558:   KSP_PIPEFCG *pipefcg=(KSP_PIPEFCG*)ksp->data;

    561:   *truncstrat=pipefcg->truncstrat;
    562:   return 0;
    563: }

    565: static PetscErrorCode KSPSetFromOptions_PIPEFCG(PetscOptionItems *PetscOptionsObject,KSP ksp)
    566: {
    567:   KSP_PIPEFCG    *pipefcg=(KSP_PIPEFCG*)ksp->data;
    568:   PetscInt       mmax,nprealloc;
    569:   PetscBool      flg;

    571:   PetscOptionsHead(PetscOptionsObject,"KSP PIPEFCG options");
    572:   PetscOptionsInt("-ksp_pipefcg_mmax","Number of search directions to storue","KSPPIPEFCGSetMmax",pipefcg->mmax,&mmax,&flg);
    573:   if (flg) KSPPIPEFCGSetMmax(ksp,mmax);
    574:   PetscOptionsInt("-ksp_pipefcg_nprealloc","Number of directions to preallocate","KSPPIPEFCGSetNprealloc",pipefcg->nprealloc,&nprealloc,&flg);
    575:   if (flg) KSPPIPEFCGSetNprealloc(ksp,nprealloc);
    576:   PetscOptionsEnum("-ksp_pipefcg_truncation_type","Truncation approach for directions","KSPFCGSetTruncationType",KSPFCDTruncationTypes,(PetscEnum)pipefcg->truncstrat,(PetscEnum*)&pipefcg->truncstrat,NULL);
    577:   PetscOptionsTail();
    578:   return 0;
    579: }

    581: /*MC

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

    585:   Options Database Keys:
    586: +   -ksp_pipefcg_mmax <N> - The number of previous search directions to store
    587: .   -ksp_pipefcg_nprealloc <N> - The number of previous search directions to preallocate
    588: -   -ksp_pipefcg_truncation_type <standard,notay> - which stored search directions to orthogonalize against

    590:   Notes:
    591:    Supports left preconditioning only.

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

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

    598:   Reference:
    599:     P. Sanan, S.M. Schnepp, and D.A. May,
    600:     "Pipelined, Flexible Krylov Subspace Methods,"
    601:     SIAM Journal on Scientific Computing 2016 38:5, C441-C470,
    602:     DOI: 10.1137/15M1049130

    604:   Level: intermediate

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

    608: M*/
    609: PETSC_EXTERN PetscErrorCode KSPCreate_PIPEFCG(KSP ksp)
    610: {
    611:   KSP_PIPEFCG    *pipefcg;

    613:   PetscNewLog(ksp,&pipefcg);
    614: #if !defined(PETSC_USE_COMPLEX)
    615:   pipefcg->type       = KSP_CG_SYMMETRIC;
    616: #else
    617:   pipefcg->type       = KSP_CG_HERMITIAN;
    618: #endif
    619:   pipefcg->mmax       = KSPPIPEFCG_DEFAULT_MMAX;
    620:   pipefcg->nprealloc  = KSPPIPEFCG_DEFAULT_NPREALLOC;
    621:   pipefcg->nvecs      = 0;
    622:   pipefcg->vecb       = KSPPIPEFCG_DEFAULT_VECB;
    623:   pipefcg->nchunks    = 0;
    624:   pipefcg->truncstrat = KSPPIPEFCG_DEFAULT_TRUNCSTRAT;
    625:   pipefcg->n_restarts = 0;

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

    629:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,2);
    630:   KSPSetSupportedNorm(ksp,KSP_NORM_NATURAL,PC_LEFT,1);
    631:   KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_LEFT,1);
    632:   KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_LEFT,1);

    634:   ksp->ops->setup          = KSPSetUp_PIPEFCG;
    635:   ksp->ops->solve          = KSPSolve_PIPEFCG;
    636:   ksp->ops->destroy        = KSPDestroy_PIPEFCG;
    637:   ksp->ops->view           = KSPView_PIPEFCG;
    638:   ksp->ops->setfromoptions = KSPSetFromOptions_PIPEFCG;
    639:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
    640:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
    641:   return 0;
    642: }