Actual source code: gmres.c

      1: #define PETSCKSP_DLL

      3: /*
      4:     This file implements GMRES (a Generalized Minimal Residual) method.
      5:     Reference:  Saad and Schultz, 1986.

      8:     Some comments on left vs. right preconditioning, and restarts.
      9:     Left and right preconditioning.
     10:     If right preconditioning is chosen, then the problem being solved
     11:     by gmres is actually
     12:        My =  AB^-1 y = f
     13:     so the initial residual is
     14:           r = f - Mx
     15:     Note that B^-1 y = x or y = B x, and if x is non-zero, the initial
     16:     residual is
     17:           r = f - A x
     18:     The final solution is then
     19:           x = B^-1 y

     21:     If left preconditioning is chosen, then the problem being solved is
     22:        My = B^-1 A x = B^-1 f,
     23:     and the initial residual is
     24:        r  = B^-1(f - Ax)

     26:     Restarts:  Restarts are basically solves with x0 not equal to zero.
     27:     Note that we can eliminate an extra application of B^-1 between
     28:     restarts as long as we don't require that the solution at the end
     29:     of an unsuccessful gmres iteration always be the solution x.
     30:  */

     32:  #include src/ksp/ksp/impls/gmres/gmresp.h
     33: #define GMRES_DELTA_DIRECTIONS 10
     34: #define GMRES_DEFAULT_MAXK     30
     35: static PetscErrorCode    GMRESGetNewVectors(KSP,PetscInt);
     36: static PetscErrorCode    GMRESUpdateHessenberg(KSP,PetscInt,PetscTruth,PetscReal*);
     37: static PetscErrorCode    BuildGmresSoln(PetscScalar*,Vec,Vec,KSP,PetscInt);

     41: PetscErrorCode    KSPSetUp_GMRES(KSP ksp)
     42: {
     43:   PetscInt       size,hh,hes,rs,cc;
     45:   PetscInt       max_k,k;
     46:   KSP_GMRES      *gmres = (KSP_GMRES *)ksp->data;
     47:   Vec            vec;
     48:   Mat            pmat;

     51:   if (ksp->pc_side == PC_SYMMETRIC) {
     52:     SETERRQ(PETSC_ERR_SUP,"no symmetric preconditioning for KSPGMRES");
     53:   }

     55:   max_k         = gmres->max_k;
     56:   hh            = (max_k + 2) * (max_k + 1);
     57:   hes           = (max_k + 1) * (max_k + 1);
     58:   rs            = (max_k + 2);
     59:   cc            = (max_k + 1);
     60:   size          = (hh + hes + rs + 2*cc) * sizeof(PetscScalar);

     62:   PetscMalloc(size,&gmres->hh_origin);
     63:   PetscMemzero(gmres->hh_origin,size);
     64:   PetscLogObjectMemory(ksp,size);
     65:   gmres->hes_origin = gmres->hh_origin + hh;
     66:   gmres->rs_origin  = gmres->hes_origin + hes;
     67:   gmres->cc_origin  = gmres->rs_origin + rs;
     68:   gmres->ss_origin  = gmres->cc_origin + cc;

     70:   if (ksp->calc_sings) {
     71:     /* Allocate workspace to hold Hessenberg matrix needed by lapack */
     72:     size = (max_k + 3)*(max_k + 9)*sizeof(PetscScalar);
     73:     PetscMalloc(size,&gmres->Rsvd);
     74:     PetscMalloc(5*(max_k+2)*sizeof(PetscReal),&gmres->Dsvd);
     75:     PetscLogObjectMemory(ksp,size+5*(max_k+2)*sizeof(PetscReal));
     76:   }

     78:   /* Allocate array to hold pointers to user vectors.  Note that we need
     79:    4 + max_k + 1 (since we need it+1 vectors, and it <= max_k) */
     80:   PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void*),&gmres->vecs);
     81:   gmres->vecs_allocated = VEC_OFFSET + 2 + max_k;
     82:   PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(void*),&gmres->user_work);
     83:   PetscMalloc((VEC_OFFSET+2+max_k)*sizeof(PetscInt),&gmres->mwork_alloc);
     84:   PetscLogObjectMemory(ksp,(VEC_OFFSET+2+max_k)*(2*sizeof(void*)+sizeof(PetscInt)));

     86:   PCGetOperators(ksp->pc,0,&pmat,0);
     87:   if (!pmat) SETERRQ(PETSC_ERR_ORDER,"You must call KSPSetOperators() or PCSetOperators() before this call");
     88:   MatGetVecs(pmat,&vec,0);
     89:   if (gmres->q_preallocate) {
     90:     gmres->vv_allocated   = VEC_OFFSET + 2 + max_k;
     91:     KSPGetVecs(ksp,gmres->vv_allocated,&gmres->user_work[0]);
     92:     PetscLogObjectParents(ksp,gmres->vv_allocated,gmres->user_work[0]);
     93:     gmres->mwork_alloc[0] = gmres->vv_allocated;
     94:     gmres->nwork_alloc    = 1;
     95:     for (k=0; k<gmres->vv_allocated; k++) {
     96:       gmres->vecs[k] = gmres->user_work[0][k];
     97:     }
     98:   } else {
     99:     gmres->vv_allocated    = 5;
    100:     KSPGetVecs(ksp,5,&gmres->user_work[0]);
    101:     PetscLogObjectParents(ksp,5,gmres->user_work[0]);
    102:     gmres->mwork_alloc[0]  = 5;
    103:     gmres->nwork_alloc     = 1;
    104:     for (k=0; k<gmres->vv_allocated; k++) {
    105:       gmres->vecs[k] = gmres->user_work[0][k];
    106:     }
    107:   }
    108:   VecDestroy(vec);
    109:   return(0);
    110: }

    112: /*
    113:     Run gmres, possibly with restart.  Return residual history if requested.
    114:     input parameters:

    116: .        gmres  - structure containing parameters and work areas

    118:     output parameters:
    119: .        nres    - residuals (from preconditioned system) at each step.
    120:                   If restarting, consider passing nres+it.  If null,
    121:                   ignored
    122: .        itcount - number of iterations used.  nres[0] to nres[itcount]
    123:                   are defined.  If null, ignored.
    124:
    125:     Notes:
    126:     On entry, the value in vector VEC_VV(0) should be the initial residual
    127:     (this allows shortcuts where the initial preconditioned residual is 0).
    128:  */
    131: PetscErrorCode GMREScycle(PetscInt *itcount,KSP ksp)
    132: {
    133:   KSP_GMRES      *gmres = (KSP_GMRES *)(ksp->data);
    134:   PetscReal      res_norm,res,hapbnd,tt;
    136:   PetscInt       it = 0, max_k = gmres->max_k;
    137:   PetscTruth     hapend = PETSC_FALSE;

    140:   VecNormalize(VEC_VV(0),&res_norm);
    141:   res     = res_norm;
    142:   *GRS(0) = res_norm;

    144:   /* check for the convergence */
    145:   PetscObjectTakeAccess(ksp);
    146:   ksp->rnorm = res;
    147:   PetscObjectGrantAccess(ksp);
    148:   gmres->it = (it - 1);
    149:   KSPLogResidualHistory(ksp,res);
    150:   if (!res) {
    151:     if (itcount) *itcount = 0;
    152:     ksp->reason = KSP_CONVERGED_ATOL;
    153:     PetscLogInfo((ksp,"GMRESCycle: Converged due to zero residual norm on entry\n"));
    154:     return(0);
    155:   }

    157:   (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);
    158:   while (!ksp->reason && it < max_k && ksp->its < ksp->max_it) {
    159:     KSPLogResidualHistory(ksp,res);
    160:     gmres->it = (it - 1);
    161:     KSPMonitor(ksp,ksp->its,res);
    162:     if (gmres->vv_allocated <= it + VEC_OFFSET + 1) {
    163:       GMRESGetNewVectors(ksp,it+1);
    164:     }
    165:     KSP_PCApplyBAorAB(ksp,VEC_VV(it),VEC_VV(1+it),VEC_TEMP_MATOP);

    167:     /* update hessenberg matrix and do Gram-Schmidt */
    168:     (*gmres->orthog)(ksp,it);

    170:     /* vv(i+1) . vv(i+1) */
    171:     VecNormalize(VEC_VV(it+1),&tt);
    172:     /* save the magnitude */
    173:     *HH(it+1,it)    = tt;
    174:     *HES(it+1,it)   = tt;

    176:     /* check for the happy breakdown */
    177:     hapbnd  = PetscAbsScalar(tt / *GRS(it));
    178:     if (hapbnd > gmres->haptol) hapbnd = gmres->haptol;
    179:     if (tt < hapbnd) {
    180:       PetscLogInfo((ksp,"GMREScycle:Detected happy breakdown, current hapbnd = %g tt = %g\n",hapbnd,tt));
    181:       hapend = PETSC_TRUE;
    182:     }
    183:     GMRESUpdateHessenberg(ksp,it,hapend,&res);
    184:     if (ksp->reason) break;

    186:     it++;
    187:     gmres->it  = (it-1);  /* For converged */
    188:     PetscObjectTakeAccess(ksp);
    189:     ksp->its++;
    190:     ksp->rnorm = res;
    191:     PetscObjectGrantAccess(ksp);

    193:     (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);

    195:     /* Catch error in happy breakdown and signal convergence and break from loop */
    196:     if (hapend) {
    197:       if (!ksp->reason) {
    198:         SETERRQ1(0,"You reached the happy break down, but convergence was not indicated. Residual norm = %g",res);
    199:       }
    200:       break;
    201:     }
    202:   }

    204:   /* Monitor if we know that we will not return for a restart */
    205:   if (ksp->reason || ksp->its >= ksp->max_it) {
    206:     KSPLogResidualHistory(ksp,res);
    207:     KSPMonitor(ksp,ksp->its,res);
    208:   }

    210:   if (itcount) *itcount    = it;

    213:   /*
    214:     Down here we have to solve for the "best" coefficients of the Krylov
    215:     columns, add the solution values together, and possibly unwind the
    216:     preconditioning from the solution
    217:    */
    218:   /* Form the solution (or the solution so far) */
    219:   BuildGmresSoln(GRS(0),ksp->vec_sol,ksp->vec_sol,ksp,it-1);

    221:   return(0);
    222: }

    226: PetscErrorCode KSPSolve_GMRES(KSP ksp)
    227: {
    229:   PetscInt       its,itcount;
    230:   KSP_GMRES      *gmres = (KSP_GMRES *)ksp->data;
    231:   PetscTruth     guess_zero = ksp->guess_zero;

    234:   if (ksp->calc_sings && !gmres->Rsvd) {
    235:     SETERRQ(PETSC_ERR_ORDER,"Must call KSPSetComputeSingularValues() before KSPSetUp() is called");
    236:   }

    238:   PetscObjectTakeAccess(ksp);
    239:   ksp->its = 0;
    240:   PetscObjectGrantAccess(ksp);

    242:   itcount     = 0;
    243:   ksp->reason = KSP_CONVERGED_ITERATING;
    244:   while (!ksp->reason) {
    245:     KSPInitialResidual(ksp,ksp->vec_sol,VEC_TEMP,VEC_TEMP_MATOP,VEC_VV(0),ksp->vec_rhs);
    246:     GMREScycle(&its,ksp);
    247:     itcount += its;
    248:     if (itcount >= ksp->max_it) {
    249:       ksp->reason = KSP_DIVERGED_ITS;
    250:       break;
    251:     }
    252:     ksp->guess_zero = PETSC_FALSE; /* every future call to KSPInitialResidual() will have nonzero guess */
    253:   }
    254:   ksp->guess_zero = guess_zero; /* restore if user provided nonzero initial guess */
    255:   return(0);
    256: }

    260: PetscErrorCode KSPDestroy_GMRES_Internal(KSP ksp)
    261: {
    262:   KSP_GMRES      *gmres = (KSP_GMRES*)ksp->data;
    264:   PetscInt       i;

    267:   /* Free the Hessenberg matrix */
    268:   if (gmres->hh_origin) {
    269:     PetscFree(gmres->hh_origin);
    270:     gmres->hh_origin = 0;
    271:   }

    273:   /* Free the pointer to user variables */
    274:   if (gmres->vecs) {
    275:     PetscFree(gmres->vecs);
    276:     gmres->vecs = 0;
    277:   }

    279:   /* free work vectors */
    280:   for (i=0; i<gmres->nwork_alloc; i++) {
    281:     VecDestroyVecs(gmres->user_work[i],gmres->mwork_alloc[i]);
    282:   }
    283:   if (gmres->user_work)  {
    284:     PetscFree(gmres->user_work);
    285:     gmres->user_work = 0;
    286:   }
    287:   if (gmres->mwork_alloc) {
    288:     PetscFree(gmres->mwork_alloc);
    289:     gmres->mwork_alloc = 0;
    290:   }
    291:   if (gmres->nrs) {
    292:     PetscFree(gmres->nrs);
    293:     gmres->nrs = 0;
    294:   }
    295:   if (gmres->sol_temp) {
    296:     VecDestroy(gmres->sol_temp);
    297:     gmres->sol_temp = 0;
    298:   }
    299:   if (gmres->Rsvd) {
    300:     PetscFree(gmres->Rsvd);
    301:     gmres->Rsvd = 0;
    302:   }
    303:   if (gmres->Dsvd) {
    304:     PetscFree(gmres->Dsvd);
    305:     gmres->Dsvd = 0;
    306:   }

    308:   gmres->nwork_alloc    = 0;
    309:   gmres->vv_allocated   = 0;
    310:   gmres->vecs_allocated = 0;
    311:   gmres->nrs            = 0;
    312:   gmres->sol_temp       = 0;
    313:   gmres->Rsvd           = 0;
    314:   return(0);
    315: }

    319: PetscErrorCode KSPDestroy_GMRES(KSP ksp)
    320: {
    321:   KSP_GMRES      *gmres = (KSP_GMRES*)ksp->data;

    325:   KSPDestroy_GMRES_Internal(ksp);
    326:   PetscFree(gmres);
    327:   return(0);
    328: }
    329: /*
    330:     BuildGmresSoln - create the solution from the starting vector and the
    331:     current iterates.

    333:     Input parameters:
    334:         nrs - work area of size it + 1.
    335:         vs  - index of initial guess
    336:         vdest - index of result.  Note that vs may == vdest (replace
    337:                 guess with the solution).

    339:      This is an internal routine that knows about the GMRES internals.
    340:  */
    343: static PetscErrorCode BuildGmresSoln(PetscScalar* nrs,Vec vs,Vec vdest,KSP ksp,PetscInt it)
    344: {
    345:   PetscScalar    tt,zero = 0.0,one = 1.0;
    347:   PetscInt       ii,k,j;
    348:   KSP_GMRES      *gmres = (KSP_GMRES *)(ksp->data);

    351:   /* Solve for solution vector that minimizes the residual */

    353:   /* If it is < 0, no gmres steps have been performed */
    354:   if (it < 0) {
    355:     if (vdest != vs) {
    356:       VecCopy(vs,vdest);
    357:     }
    358:     return(0);
    359:   }
    360:   if (*HH(it,it) == 0.0) SETERRQ2(PETSC_ERR_CONV_FAILED,"HH(it,it) is identically zero; it = %D GRS(it) = %g",it,PetscAbsScalar(*GRS(it)));
    361:   if (*HH(it,it) != 0.0) {
    362:     nrs[it] = *GRS(it) / *HH(it,it);
    363:   } else {
    364:     nrs[it] = 0.0;
    365:   }
    366:   for (ii=1; ii<=it; ii++) {
    367:     k   = it - ii;
    368:     tt  = *GRS(k);
    369:     for (j=k+1; j<=it; j++) tt  = tt - *HH(k,j) * nrs[j];
    370:     nrs[k]   = tt / *HH(k,k);
    371:   }

    373:   /* Accumulate the correction to the solution of the preconditioned problem in TEMP */
    374:   VecSet(VEC_TEMP,zero);
    375:   VecMAXPY(VEC_TEMP,it+1,nrs,&VEC_VV(0));

    377:   KSPUnwindPreconditioner(ksp,VEC_TEMP,VEC_TEMP_MATOP);
    378:   /* add solution to previous solution */
    379:   if (vdest != vs) {
    380:     VecCopy(vs,vdest);
    381:   }
    382:   VecAXPY(vdest,one,VEC_TEMP);
    383:   return(0);
    384: }
    385: /*
    386:    Do the scalar work for the orthogonalization.  Return new residual.
    387:  */
    390: static PetscErrorCode GMRESUpdateHessenberg(KSP ksp,PetscInt it,PetscTruth hapend,PetscReal *res)
    391: {
    392:   PetscScalar *hh,*cc,*ss,tt;
    393:   PetscInt    j;
    394:   KSP_GMRES   *gmres = (KSP_GMRES *)(ksp->data);

    397:   hh  = HH(0,it);
    398:   cc  = CC(0);
    399:   ss  = SS(0);

    401:   /* Apply all the previously computed plane rotations to the new column
    402:      of the Hessenberg matrix */
    403:   for (j=1; j<=it; j++) {
    404:     tt  = *hh;
    405: #if defined(PETSC_USE_COMPLEX)
    406:     *hh = PetscConj(*cc) * tt + *ss * *(hh+1);
    407: #else
    408:     *hh = *cc * tt + *ss * *(hh+1);
    409: #endif
    410:     hh++;
    411:     *hh = *cc++ * *hh - (*ss++ * tt);
    412:   }

    414:   /*
    415:     compute the new plane rotation, and apply it to:
    416:      1) the right-hand-side of the Hessenberg system
    417:      2) the new column of the Hessenberg matrix
    418:     thus obtaining the updated value of the residual
    419:   */
    420:   if (!hapend) {
    421: #if defined(PETSC_USE_COMPLEX)
    422:     tt        = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh+1)) * *(hh+1));
    423: #else
    424:     tt        = PetscSqrtScalar(*hh * *hh + *(hh+1) * *(hh+1));
    425: #endif
    426:     if (tt == 0.0) {
    427:       ksp->reason = KSP_DIVERGED_NULL;
    428:       return(0);
    429:     }
    430:     *cc       = *hh / tt;
    431:     *ss       = *(hh+1) / tt;
    432:     *GRS(it+1) = - (*ss * *GRS(it));
    433: #if defined(PETSC_USE_COMPLEX)
    434:     *GRS(it)   = PetscConj(*cc) * *GRS(it);
    435:     *hh       = PetscConj(*cc) * *hh + *ss * *(hh+1);
    436: #else
    437:     *GRS(it)   = *cc * *GRS(it);
    438:     *hh       = *cc * *hh + *ss * *(hh+1);
    439: #endif
    440:     *res      = PetscAbsScalar(*GRS(it+1));
    441:   } else {
    442:     /* happy breakdown: HH(it+1, it) = 0, therfore we don't need to apply
    443:             another rotation matrix (so RH doesn't change).  The new residual is
    444:             always the new sine term times the residual from last time (GRS(it)),
    445:             but now the new sine rotation would be zero...so the residual should
    446:             be zero...so we will multiply "zero" by the last residual.  This might
    447:             not be exactly what we want to do here -could just return "zero". */
    448:
    449:     *res = 0.0;
    450:   }
    451:   return(0);
    452: }
    453: /*
    454:    This routine allocates more work vectors, starting from VEC_VV(it).
    455:  */
    458: static PetscErrorCode GMRESGetNewVectors(KSP ksp,PetscInt it)
    459: {
    460:   KSP_GMRES      *gmres = (KSP_GMRES *)ksp->data;
    462:   PetscInt       nwork = gmres->nwork_alloc,k,nalloc;

    465:   nalloc = PetscMin(ksp->max_it,gmres->delta_allocate);
    466:   /* Adjust the number to allocate to make sure that we don't exceed the
    467:     number of available slots */
    468:   if (it + VEC_OFFSET + nalloc >= gmres->vecs_allocated){
    469:     nalloc = gmres->vecs_allocated - it - VEC_OFFSET;
    470:   }
    471:   if (!nalloc) return(0);

    473:   gmres->vv_allocated += nalloc;
    474:   KSPGetVecs(ksp,nalloc,&gmres->user_work[nwork]);
    475:   PetscLogObjectParents(ksp,nalloc,gmres->user_work[nwork]);
    476:   gmres->mwork_alloc[nwork] = nalloc;
    477:   for (k=0; k<nalloc; k++) {
    478:     gmres->vecs[it+VEC_OFFSET+k] = gmres->user_work[nwork][k];
    479:   }
    480:   gmres->nwork_alloc++;
    481:   return(0);
    482: }

    486: PetscErrorCode KSPBuildSolution_GMRES(KSP ksp,Vec  ptr,Vec *result)
    487: {
    488:   KSP_GMRES      *gmres = (KSP_GMRES *)ksp->data;

    492:   if (!ptr) {
    493:     if (!gmres->sol_temp) {
    494:       VecDuplicate(ksp->vec_sol,&gmres->sol_temp);
    495:       PetscLogObjectParent(ksp,gmres->sol_temp);
    496:     }
    497:     ptr = gmres->sol_temp;
    498:   }
    499:   if (!gmres->nrs) {
    500:     /* allocate the work area */
    501:     PetscMalloc(gmres->max_k*sizeof(PetscScalar),&gmres->nrs);
    502:     PetscLogObjectMemory(ksp,gmres->max_k*sizeof(PetscScalar));
    503:   }

    505:   BuildGmresSoln(gmres->nrs,ksp->vec_sol,ptr,ksp,gmres->it);
    506:   *result = ptr;
    507:   return(0);
    508: }

    512: PetscErrorCode KSPView_GMRES(KSP ksp,PetscViewer viewer)
    513: {
    514:   KSP_GMRES      *gmres = (KSP_GMRES *)ksp->data;
    515:   const char     *cstr;
    517:   PetscTruth     iascii,isstring;

    520:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
    521:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
    522:   if (gmres->orthog == KSPGMRESClassicalGramSchmidtOrthogonalization) {
    523:     switch (gmres->cgstype) {
    524:       case (KSP_GMRES_CGS_REFINE_NEVER):
    525:         cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with no iterative refinement";
    526:         break;
    527:       case (KSP_GMRES_CGS_REFINE_ALWAYS):
    528:         cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with one step of iterative refinement";
    529:         break;
    530:       case (KSP_GMRES_CGS_REFINE_IFNEEDED):
    531:         cstr = "Classical (unmodified) Gram-Schmidt Orthogonalization with one step of iterative refinement when needed";
    532:         break;
    533:       default:
    534:         SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown orthogonalization");
    535:     }
    536:   } else if (gmres->orthog == KSPGMRESModifiedGramSchmidtOrthogonalization) {
    537:     cstr = "Modified Gram-Schmidt Orthogonalization";
    538:   } else {
    539:     cstr = "unknown orthogonalization";
    540:   }
    541:   if (iascii) {
    542:     PetscViewerASCIIPrintf(viewer,"  GMRES: restart=%D, using %s\n",gmres->max_k,cstr);
    543:     PetscViewerASCIIPrintf(viewer,"  GMRES: happy breakdown tolerance %g\n",gmres->haptol);
    544:   } else if (isstring) {
    545:     PetscViewerStringSPrintf(viewer,"%s restart %D",cstr,gmres->max_k);
    546:   } else {
    547:     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for KSP GMRES",((PetscObject)viewer)->type_name);
    548:   }
    549:   return(0);
    550: }

    554: /*@C
    555:    KSPGMRESKrylovMonitor - Calls VecView() for each direction in the
    556:    GMRES accumulated Krylov space.

    558:    Collective on KSP

    560:    Input Parameters:
    561: +  ksp - the KSP context
    562: .  its - iteration number
    563: .  fgnorm - 2-norm of residual (or gradient)
    564: -  a viewers object created with PetscViewersCreate()

    566:    Level: intermediate

    568: .keywords: KSP, nonlinear, vector, monitor, view, Krylov space

    570: .seealso: KSPSetMonitor(), KSPDefaultMonitor(), VecView(), PetscViewersCreate(), PetscViewersDestroy()
    571: @*/
    572: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESKrylovMonitor(KSP ksp,PetscInt its,PetscReal fgnorm,void *dummy)
    573: {
    574:   PetscViewers   viewers = (PetscViewers)dummy;
    575:   KSP_GMRES      *gmres = (KSP_GMRES*)ksp->data;
    577:   Vec            x;
    578:   PetscViewer    viewer;

    581:   PetscViewersGetViewer(viewers,gmres->it+1,&viewer);
    582:   PetscViewerSetType(viewer,PETSC_VIEWER_DRAW);

    584:   x      = VEC_VV(gmres->it+1);
    585:   VecView(x,viewer);

    587:   return(0);
    588: }

    592: PetscErrorCode KSPSetFromOptions_GMRES(KSP ksp)
    593: {
    595:   PetscInt       restart;
    596:   PetscReal      haptol;
    597:   KSP_GMRES      *gmres = (KSP_GMRES*)ksp->data;
    598:   PetscTruth     flg;

    601:   PetscOptionsHead("KSP GMRES Options");
    602:     PetscOptionsInt("-ksp_gmres_restart","Number of Krylov search directions","KSPGMRESSetRestart",gmres->max_k,&restart,&flg);
    603:     if (flg) { KSPGMRESSetRestart(ksp,restart); }
    604:     PetscOptionsReal("-ksp_gmres_haptol","Tolerance for exact convergence (happy ending)","KSPGMRESSetHapTol",gmres->haptol,&haptol,&flg);
    605:     if (flg) { KSPGMRESSetHapTol(ksp,haptol); }
    606:     PetscOptionsName("-ksp_gmres_preallocate","Preallocate Krylov vectors","KSPGMRESSetPreAllocateVectors",&flg);
    607:     if (flg) {KSPGMRESSetPreAllocateVectors(ksp);}
    608:     PetscOptionsTruthGroupBegin("-ksp_gmres_classicalgramschmidt","Classical (unmodified) Gram-Schmidt (fast)","KSPGMRESSetOrthogonalization",&flg);
    609:     if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESClassicalGramSchmidtOrthogonalization);}
    610:     PetscOptionsTruthGroupEnd("-ksp_gmres_modifiedgramschmidt","Modified Gram-Schmidt (slow,more stable)","KSPGMRESSetOrthogonalization",&flg);
    611:     if (flg) {KSPGMRESSetOrthogonalization(ksp,KSPGMRESModifiedGramSchmidtOrthogonalization);}
    612:     PetscOptionsEnum("-ksp_gmres_cgs_refinement_type","Type of iterative refinement for classical (unmodified) Gram-Schmidt","KSPGMRESSetCGSRefinementType",
    613:                             KSPGMRESCGSRefinementTypes,(PetscEnum)gmres->cgstype,(PetscEnum*)&gmres->cgstype,&flg);
    614:     PetscOptionsName("-ksp_gmres_krylov_monitor","Plot the Krylov directions","KSPSetMonitor",&flg);
    615:     if (flg) {
    616:       PetscViewers viewers;
    617:       PetscViewersCreate(ksp->comm,&viewers);
    618:       KSPSetMonitor(ksp,KSPGMRESKrylovMonitor,viewers,(PetscErrorCode (*)(void*))PetscViewersDestroy);
    619:     }
    620:   PetscOptionsTail();
    621:   return(0);
    622: }

    624: EXTERN PetscErrorCode KSPComputeExtremeSingularValues_GMRES(KSP,PetscReal *,PetscReal *);
    625: EXTERN PetscErrorCode KSPComputeEigenvalues_GMRES(KSP,PetscInt,PetscReal *,PetscReal *,PetscInt *);

    631: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetHapTol_GMRES(KSP ksp,PetscReal tol)
    632: {
    633:   KSP_GMRES *gmres = (KSP_GMRES *)ksp->data;

    636:   if (tol < 0.0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Tolerance must be non-negative");
    637:   gmres->haptol = tol;
    638:   return(0);
    639: }

    645: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetRestart_GMRES(KSP ksp,PetscInt max_k)
    646: {
    647:   KSP_GMRES      *gmres = (KSP_GMRES *)ksp->data;

    651:   if (max_k < 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Restart must be positive");
    652:   if (!ksp->setupcalled) {
    653:     gmres->max_k = max_k;
    654:   } else if (gmres->max_k != max_k) {
    655:      gmres->max_k = max_k;
    656:      ksp->setupcalled = 0;
    657:      /* free the data structures, then create them again */
    658:      KSPDestroy_GMRES_Internal(ksp);
    659:   }

    661:   return(0);
    662: }

    669: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetOrthogonalization_GMRES(KSP ksp,FCN fcn)
    670: {
    673:   ((KSP_GMRES *)ksp->data)->orthog = fcn;
    674:   return(0);
    675: }

    681: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetPreAllocateVectors_GMRES(KSP ksp)
    682: {
    683:   KSP_GMRES *gmres;

    686:   gmres = (KSP_GMRES *)ksp->data;
    687:   gmres->q_preallocate = 1;
    688:   return(0);
    689: }

    695: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetCGSRefinementType_GMRES(KSP ksp,KSPGMRESCGSRefinementType type)
    696: {
    697:   KSP_GMRES *gmres = (KSP_GMRES*)ksp->data;

    700:   gmres->cgstype = type;
    701:   return(0);
    702: }

    707: /*@
    708:    KSPGMRESSetCGSRefinementType - Sets the type of iterative refinement to use
    709:          in the classical Gram Schmidt orthogonalization.
    710:    of the preconditioned problem.

    712:    Collective on KSP

    714:    Input Parameters:
    715: +  ksp - the Krylov space context
    716: -  type - the type of refinement

    718:   Options Database:
    719: .  -ksp_gmres_cgs_refinement_type <never,ifneeded,always>

    721:    Level: intermediate

    723: .keywords: KSP, GMRES, iterative refinement

    725: .seealso: KSPGMRESSetOrthogonalization(), KSPGMRESCGSRefinementType, KSPGMRESClassicalGramSchmidtOrthogonalization()
    726: @*/
    727: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetCGSRefinementType(KSP ksp,KSPGMRESCGSRefinementType type)
    728: {
    729:   PetscErrorCode ierr,(*f)(KSP,KSPGMRESCGSRefinementType);

    733:   PetscObjectQueryFunction((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",(void (**)(void))&f);
    734:   if (f) {
    735:     (*f)(ksp,type);
    736:   }
    737:   return(0);
    738: }

    742: /*@C
    743:    KSPGMRESSetRestart - Sets number of iterations at which GMRES, FGMRES and LGMRES restarts.

    745:    Collective on KSP

    747:    Input Parameters:
    748: +  ksp - the Krylov space context
    749: -  restart - integer restart value

    751:   Options Database:
    752: .  -ksp_gmres_restart <positive integer>

    754:     Note: The default value is 30.

    756:    Level: intermediate

    758: .keywords: KSP, GMRES, restart, iterations

    760: .seealso: KSPSetTolerances(), KSPGMRESSetOrthogonalization(), KSPGMRESSetPreAllocateVectors()
    761: @*/
    762: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetRestart(KSP ksp, PetscInt restart)
    763: {

    767:   PetscTryMethod((ksp),KSPGMRESSetRestart_C,(KSP,PetscInt),((ksp),(restart)));
    768:   return(0);
    769: }

    773: /*@
    774:    KSPGMRESSetHapTol - Sets tolerance for determining happy breakdown in GMRES, FGMRES and LGMRES.

    776:    Collective on KSP

    778:    Input Parameters:
    779: +  ksp - the Krylov space context
    780: -  tol - the tolerance

    782:   Options Database:
    783: .  -ksp_gmres_haptol <positive real value>

    785:    Note: Happy breakdown is the rare case in GMRES where an 'exact' solution is obtained after
    786:          a certain number of iterations. If you attempt more iterations after this point unstable
    787:          things can happen hence very occasionally you may need to set this value to detect this condition

    789:    Level: intermediate

    791: .keywords: KSP, GMRES, tolerance

    793: .seealso: KSPSetTolerances()
    794: @*/
    795: PetscErrorCode PETSCKSP_DLLEXPORT KSPGMRESSetHapTol(KSP ksp,PetscReal tol)
    796: {

    800:   PetscTryMethod((ksp),KSPGMRESSetHapTol_C,(KSP,PetscReal),((ksp),(tol)));
    801:   return(0);
    802: }

    804: /*MC
    805:      KSPGMRES - Implements the Generalized Minimal Residual method.
    806:                 (Saad and Schultz, 1986) with restart

    809:    Options Database Keys:
    810: +   -ksp_gmres_restart <restart> - the number of Krylov directions to orthogonalize against
    811: .   -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
    812: .   -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
    813:                              vectors are allocated as needed)
    814: .   -ksp_gmres_classicalgramschmidt - use classical (unmodified) Gram-Schmidt to orthogonalize against the Krylov space (fast) (the default)
    815: .   -ksp_gmres_modifiedgramschmidt - use modified Gram-Schmidt in the orthogonalization (more stable, but slower)
    816: .   -ksp_gmres_cgs_refinement_type <never,ifneeded,always> - determine if iterative refinement is used to increase the
    817:                                    stability of the classical Gram-Schmidt  orthogonalization.
    818: -   -ksp_gmres_krylov_monitor - plot the Krylov space generated

    820:    Level: beginner

    823: .seealso:  KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPFGMRES, KSPLGMRES,
    824:            KSPGMRESSetRestart(), KSPGMRESSetHapTol(), KSPGMRESSetPreAllocateVectors(), KSPGMRESSetOrthogonalization()
    825:            KSPGMRESClassicalGramSchmidtOrthogonalization(), KSPGMRESModifiedGramSchmidtOrthogonalization(),
    826:            KSPGMRESCGSRefinementType, KSPGMRESSetCGSRefinementType(), KSPGMRESKrylovMonitor()

    828: M*/

    833: PetscErrorCode PETSCKSP_DLLEXPORT KSPCreate_GMRES(KSP ksp)
    834: {
    835:   KSP_GMRES      *gmres;

    839:   PetscNew(KSP_GMRES,&gmres);
    840:   PetscLogObjectMemory(ksp,sizeof(KSP_GMRES));
    841:   ksp->data                              = (void*)gmres;
    842:   ksp->ops->buildsolution                = KSPBuildSolution_GMRES;

    844:   ksp->ops->setup                        = KSPSetUp_GMRES;
    845:   ksp->ops->solve                        = KSPSolve_GMRES;
    846:   ksp->ops->destroy                      = KSPDestroy_GMRES;
    847:   ksp->ops->view                         = KSPView_GMRES;
    848:   ksp->ops->setfromoptions               = KSPSetFromOptions_GMRES;
    849:   ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
    850:   ksp->ops->computeeigenvalues           = KSPComputeEigenvalues_GMRES;

    852:   PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetPreAllocateVectors_C",
    853:                                     "KSPGMRESSetPreAllocateVectors_GMRES",
    854:                                      KSPGMRESSetPreAllocateVectors_GMRES);
    855:   PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetOrthogonalization_C",
    856:                                     "KSPGMRESSetOrthogonalization_GMRES",
    857:                                      KSPGMRESSetOrthogonalization_GMRES);
    858:   PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetRestart_C",
    859:                                     "KSPGMRESSetRestart_GMRES",
    860:                                      KSPGMRESSetRestart_GMRES);
    861:   PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetHapTol_C",
    862:                                     "KSPGMRESSetHapTol_GMRES",
    863:                                      KSPGMRESSetHapTol_GMRES);
    864:   PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPGMRESSetCGSRefinementType_C",
    865:                                     "KSPGMRESSetCGSRefinementType_GMRES",
    866:                                      KSPGMRESSetCGSRefinementType_GMRES);

    868:   gmres->haptol              = 1.0e-30;
    869:   gmres->q_preallocate       = 0;
    870:   gmres->delta_allocate      = GMRES_DELTA_DIRECTIONS;
    871:   gmres->orthog              = KSPGMRESClassicalGramSchmidtOrthogonalization;
    872:   gmres->nrs                 = 0;
    873:   gmres->sol_temp            = 0;
    874:   gmres->max_k               = GMRES_DEFAULT_MAXK;
    875:   gmres->Rsvd                = 0;
    876:   gmres->cgstype             = KSP_GMRES_CGS_REFINE_NEVER;
    877:   return(0);
    878: }