Actual source code: fbcgsr.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: /*
  3:     This file implements FBiCGStab-R.
  4:     Only allow right preconditioning.
  5:     FBiCGStab-R is a mathematically equivalent variant of FBiCGStab. Differences are:
  6:       (1) There are fewer MPI_Allreduce calls.
  7:       (2) The convergence occasionally is much faster than that of FBiCGStab.
  8: */
  9: #include <../src/ksp/ksp/impls/bcgs/bcgsimpl.h>       /*I  "petscksp.h"  I*/
 10: #include <petsc/private/vecimpl.h>

 14: static PetscErrorCode KSPSetUp_FBCGSR(KSP ksp)
 15: {

 19:   KSPSetWorkVecs(ksp,8);
 20:   return(0);
 21: }

 23: #include <petsc/private/pcimpl.h>            /*I "petscksp.h" I*/
 26: static PetscErrorCode  KSPSolve_FBCGSR(KSP ksp)
 27: {
 28:   PetscErrorCode    ierr;
 29:   PetscInt          i,j,N;
 30:   PetscScalar       tau,sigma,alpha,omega,beta;
 31:   PetscReal         rho;
 32:   PetscScalar       xi1,xi2,xi3,xi4;
 33:   Vec               X,B,P,P2,RP,R,V,S,T,S2;
 34:   PetscScalar       *PETSC_RESTRICT rp, *PETSC_RESTRICT r, *PETSC_RESTRICT p;
 35:   PetscScalar       *PETSC_RESTRICT v, *PETSC_RESTRICT s, *PETSC_RESTRICT t, *PETSC_RESTRICT s2;
 36:   PetscScalar       insums[4],outsums[4];
 37:   KSP_BCGS          *bcgs = (KSP_BCGS*)ksp->data;
 38:   PC                pc;

 41:   if (!ksp->vec_rhs->petscnative) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"Only coded for PETSc vectors");
 42:   VecGetLocalSize(ksp->vec_sol,&N);

 44:   X  = ksp->vec_sol;
 45:   B  = ksp->vec_rhs;
 46:   P2 = ksp->work[0];

 48:   /* The followings are involved in modified inner product calculations and vector updates */
 49:   RP = ksp->work[1]; VecGetArray(RP,(PetscScalar**)&rp); VecRestoreArray(RP,NULL);
 50:   R  = ksp->work[2]; VecGetArray(R,(PetscScalar**)&r);   VecRestoreArray(R,NULL);
 51:   P  = ksp->work[3]; VecGetArray(P,(PetscScalar**)&p);   VecRestoreArray(P,NULL);
 52:   V  = ksp->work[4]; VecGetArray(V,(PetscScalar**)&v);   VecRestoreArray(V,NULL);
 53:   S  = ksp->work[5]; VecGetArray(S,(PetscScalar**)&s);   VecRestoreArray(S,NULL);
 54:   T  = ksp->work[6]; VecGetArray(T,(PetscScalar**)&t);   VecRestoreArray(T,NULL);
 55:   S2 = ksp->work[7]; VecGetArray(S2,(PetscScalar**)&s2); VecRestoreArray(S2,NULL);

 57:   /* Only supports right preconditioning */
 58:   if (ksp->pc_side != PC_RIGHT) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"KSP fbcgsr does not support %s",PCSides[ksp->pc_side]);
 59:   if (!ksp->guess_zero) {
 60:     if (!bcgs->guess) {
 61:       VecDuplicate(X,&bcgs->guess);
 62:     }
 63:     VecCopy(X,bcgs->guess);
 64:   } else {
 65:     VecSet(X,0.0);
 66:   }

 68:   /* Compute initial residual */
 69:   KSPGetPC(ksp,&pc);
 70:   PCSetUp(pc);
 71:   if (!ksp->guess_zero) {
 72:     KSP_MatMult(ksp,pc->mat,X,P2); /* P2 is used as temporary storage */
 73:     VecCopy(B,R);
 74:     VecAXPY(R,-1.0,P2);
 75:   } else {
 76:     VecCopy(B,R);
 77:   }

 79:   /* Test for nothing to do */
 80:   VecNorm(R,NORM_2,&rho);
 81:   PetscObjectSAWsTakeAccess((PetscObject)ksp);
 82:   ksp->its   = 0;
 83:   ksp->rnorm = rho;
 84:   PetscObjectSAWsGrantAccess((PetscObject)ksp);
 85:   KSPLogResidualHistory(ksp,rho);
 86:   KSPMonitor(ksp,0,rho);
 87:   (*ksp->converged)(ksp,0,rho,&ksp->reason,ksp->cnvP);
 88:   if (ksp->reason) return(0);

 90:   /* Initialize iterates */
 91:   VecCopy(R,RP); /* rp <- r */
 92:   VecCopy(R,P); /* p <- r */

 94:   /* Big loop */
 95:   for (i=0; i<ksp->max_it; i++) {

 97:     /* matmult and pc */
 98:     KSP_PCApply(ksp,P,P2); /* p2 <- K p */
 99:     KSP_MatMult(ksp,pc->mat,P2,V); /* v <- A p2 */

101:     /* inner prodcuts */
102:     if (i==0) {
103:       tau  = rho*rho;
104:       VecDot(V,RP,&sigma); /* sigma <- (v,rp) */
105:     } else {
106:       PetscLogEventBegin(VEC_ReduceArithmetic,0,0,0,0);
107:       tau  = sigma = 0.0;
108:       for (j=0; j<N; j++) {
109:         tau   += r[j]*rp[j]; /* tau <- (r,rp) */
110:         sigma += v[j]*rp[j]; /* sigma <- (v,rp) */
111:       }
112:       PetscLogFlops(4.0*N);
113:       PetscLogEventEnd(VEC_ReduceArithmetic,0,0,0,0);
114:       insums[0] = tau;
115:       insums[1] = sigma;
116:       PetscLogEventBarrierBegin(VEC_ReduceBarrier,0,0,0,0,PetscObjectComm((PetscObject)ksp));
117:       MPIU_Allreduce(insums,outsums,2,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)ksp));
118:       PetscLogEventBarrierEnd(VEC_ReduceBarrier,0,0,0,0,PetscObjectComm((PetscObject)ksp));
119:       tau       = outsums[0];
120:       sigma     = outsums[1];
121:     }

123:     /* scalar update */
124:     alpha = tau / sigma;

126:     /* vector update */
127:     VecWAXPY(S,-alpha,V,R);  /* s <- r - alpha v */

129:     /* matmult and pc */
130:     KSP_PCApply(ksp,S,S2); /* s2 <- K s */
131:     KSP_MatMult(ksp,pc->mat,S2,T); /* t <- A s2 */

133:     /* inner prodcuts */
134:     PetscLogEventBegin(VEC_ReduceArithmetic,0,0,0,0);
135:     xi1  = xi2 = xi3 = xi4 = 0.0;
136:     for (j=0; j<N; j++) {
137:       xi1 += s[j]*s[j]; /* xi1 <- (s,s) */
138:       xi2 += t[j]*s[j]; /* xi2 <- (t,s) */
139:       xi3 += t[j]*t[j]; /* xi3 <- (t,t) */
140:       xi4 += t[j]*rp[j]; /* xi4 <- (t,rp) */
141:     }
142:     PetscLogFlops(8.0*N);
143:     PetscLogEventEnd(VEC_ReduceArithmetic,0,0,0,0);

145:     insums[0] = xi1;
146:     insums[1] = xi2;
147:     insums[2] = xi3;
148:     insums[3] = xi4;

150:     PetscLogEventBarrierBegin(VEC_ReduceBarrier,0,0,0,0,PetscObjectComm((PetscObject)ksp));
151:     MPIU_Allreduce(insums,outsums,4,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)ksp));
152:     PetscLogEventBarrierEnd(VEC_ReduceBarrier,0,0,0,0,PetscObjectComm((PetscObject)ksp));
153:     xi1  = outsums[0];
154:     xi2  = outsums[1];
155:     xi3  = outsums[2];
156:     xi4  = outsums[3];

158:     /* test denominator */
159:     if (xi3 == 0.0) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_PLIB,"Divide by zero");
160:     if (sigma == 0.0) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_PLIB,"Divide by zero");

162:     /* scalar updates */
163:     omega = xi2 / xi3;
164:     beta  = -xi4 / sigma;
165:     rho   = PetscSqrtReal(PetscAbsScalar(xi1 - omega * xi2)); /* residual norm */

167:     /* vector updates */
168:     VecAXPBYPCZ(X,alpha,omega,1.0,P2,S2); /* x <- alpha * p2 + omega * s2 + x */

170:     /* convergence test */
171:     PetscObjectSAWsTakeAccess((PetscObject)ksp);
172:     ksp->its++;
173:     ksp->rnorm = rho;
174:     PetscObjectSAWsGrantAccess((PetscObject)ksp);
175:     KSPLogResidualHistory(ksp,rho);
176:     KSPMonitor(ksp,i+1,rho);
177:     (*ksp->converged)(ksp,i+1,rho,&ksp->reason,ksp->cnvP);
178:     if (ksp->reason) break;

180:     /* vector updates */
181:     PetscLogEventBegin(VEC_Ops,0,0,0,0);
182:     for (j=0; j<N; j++) {
183:       r[j] = s[j] - omega * t[j]; /* r <- s - omega t */
184:       p[j] = r[j] + beta * (p[j] - omega * v[j]); /* p <- r + beta * (p - omega v) */
185:     }
186:     PetscLogFlops(6.0*N);
187:     PetscLogEventEnd(VEC_Ops,0,0,0,0);

189:   }

191:   if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
192:   return(0);
193: }

195: /*MC
196:      KSPFBCGSR - Implements a mathematically equivalent variant of FBiCGSTab.

198:    Options Database Keys:
199: .   see KSPSolve()

201:    Level: beginner

203:    Notes: Only allow right preconditioning

205: .seealso:  KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPBICG, KSPFBCGSL, KSPSetPCSide()
206: M*/
209: PETSC_EXTERN PetscErrorCode KSPCreate_FBCGSR(KSP ksp)
210: {
212:   KSP_BCGS       *bcgs;

215:   PetscNewLog(ksp,&bcgs);

217:   ksp->data                = bcgs;
218:   ksp->ops->setup          = KSPSetUp_FBCGSR;
219:   ksp->ops->solve          = KSPSolve_FBCGSR;
220:   ksp->ops->destroy        = KSPDestroy_BCGS;
221:   ksp->ops->reset          = KSPReset_BCGS;
222:   ksp->ops->buildsolution  = KSPBuildSolution_BCGS;
223:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
224:   ksp->ops->setfromoptions = KSPSetFromOptions_BCGS;
225:   ksp->pc_side             = PC_RIGHT; /* set default PC side */

227:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,3);
228:   KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_RIGHT,2);
229:   return(0);
230: }