Actual source code: trlan.c

slepc-3.7.4 2017-05-17
Report Typos and Errors
  1: /*
  2:    This file implements a wrapper to the TRLAN package

  4:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  6:    Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain

  8:    This file is part of SLEPc.

 10:    SLEPc is free software: you can redistribute it and/or modify it under  the
 11:    terms of version 3 of the GNU Lesser General Public License as published by
 12:    the Free Software Foundation.

 14:    SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY
 15:    WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS
 16:    FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for
 17:    more details.

 19:    You  should have received a copy of the GNU Lesser General  Public  License
 20:    along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
 21:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 22: */

 24: #include <slepc/private/epsimpl.h>
 25: #include <../src/eps/impls/external/trlan/trlanp.h>

 27: PetscErrorCode EPSSolve_TRLAN(EPS);

 29: /* Nasty global variable to access EPS data from TRLan_ */
 30: static EPS globaleps;

 34: PetscErrorCode EPSSetUp_TRLAN(EPS eps)
 35: {
 37:   PetscBool      istrivial;
 38:   EPS_TRLAN      *tr = (EPS_TRLAN*)eps->data;

 41:   PetscBLASIntCast(PetscMax(7,eps->nev+PetscMin(eps->nev,6)),&tr->maxlan);
 42:   if (eps->ncv) {
 43:     if (eps->ncv<eps->nev) SETERRQ(PetscObjectComm((PetscObject)eps),1,"The value of ncv must be at least nev");
 44:   } else eps->ncv = tr->maxlan;
 45:   if (eps->mpd) { PetscInfo(eps,"Warning: parameter mpd ignored\n"); }
 46:   if (!eps->max_it) eps->max_it = PetscMax(1000,eps->n);

 48:   if (!eps->ishermitian) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Requested method is only available for Hermitian problems");

 50:   if (eps->isgeneralized) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Requested method is not available for generalized problems");

 52:   if (!eps->which) eps->which = EPS_LARGEST_REAL;
 53:   if (eps->which!=EPS_LARGEST_REAL && eps->which!=EPS_SMALLEST_REAL && eps->which!=EPS_TARGET_REAL) SETERRQ(PetscObjectComm((PetscObject)eps),1,"Wrong value of eps->which");
 54:   if (eps->arbitrary) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Arbitrary selection of eigenpairs not supported in this solver");
 55:   if (eps->stopping!=EPSStoppingBasic) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"External packages do not support user-defined stopping test");

 57:   tr->restart = 0;
 58:   if (tr->maxlan+1-eps->ncv<=0) {
 59:     PetscBLASIntCast(tr->maxlan*(tr->maxlan+10),&tr->lwork);
 60:   } else {
 61:     PetscBLASIntCast(eps->nloc*(tr->maxlan+1-eps->ncv) + tr->maxlan*(tr->maxlan+10),&tr->lwork);
 62:   }
 63:   PetscMalloc1(tr->lwork,&tr->work);
 64:   PetscLogObjectMemory((PetscObject)eps,tr->lwork*sizeof(PetscReal));

 66:   if (eps->extraction) { PetscInfo(eps,"Warning: extraction type ignored\n"); }
 67:   RGIsTrivial(eps->rg,&istrivial);
 68:   if (!istrivial) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support region filtering");

 70:   EPSAllocateSolution(eps,0);

 72:   /* dispatch solve method */
 73:   eps->ops->solve = EPSSolve_TRLAN;
 74:   return(0);
 75: }

 79: static PetscBLASInt MatMult_TRLAN(PetscBLASInt *n,PetscBLASInt *m,PetscReal *xin,PetscBLASInt *ldx,PetscReal *yout,PetscBLASInt *ldy)
 80: {
 82:   Vec            x,y;
 83:   PetscBLASInt   i;

 86:   VecCreateMPIWithArray(PetscObjectComm((PetscObject)globaleps),1,*n,PETSC_DECIDE,NULL,&x);
 87:   VecCreateMPIWithArray(PetscObjectComm((PetscObject)globaleps),1,*n,PETSC_DECIDE,NULL,&y);
 88:   for (i=0;i<*m;i++) {
 89:     VecPlaceArray(x,(PetscScalar*)xin+i*(*ldx));
 90:     VecPlaceArray(y,(PetscScalar*)yout+i*(*ldy));
 91:     STApply(globaleps->st,x,y);
 92:     BVOrthogonalizeVec(globaleps->V,y,NULL,NULL,NULL);
 93:     VecResetArray(x);
 94:     VecResetArray(y);
 95:   }
 96:   VecDestroy(&x);
 97:   VecDestroy(&y);
 98:   return(0);
 99: }

103: PetscErrorCode EPSSolve_TRLAN(EPS eps)
104: {
106:   PetscInt       i;
107:   PetscBLASInt   ipar[32],n,lohi,stat,ncv;
108:   EPS_TRLAN      *tr = (EPS_TRLAN*)eps->data;
109:   PetscScalar    *pV;
110:   Vec            v0;

113:   PetscBLASIntCast(eps->ncv,&ncv);
114:   PetscBLASIntCast(eps->nloc,&n);

116:   if (eps->which==EPS_LARGEST_REAL || eps->which==EPS_TARGET_REAL) lohi = 1;
117:   else if (eps->which==EPS_SMALLEST_REAL) lohi = -1;
118:   else SETERRQ(PetscObjectComm((PetscObject)eps),1,"Wrong value of eps->which");

120:   globaleps = eps;

122:   ipar[0]  = 0;            /* stat: error flag */
123:   ipar[1]  = lohi;         /* smallest (lohi<0) or largest eigenvalues (lohi>0) */
124:   PetscBLASIntCast(eps->nev,&ipar[2]); /* number of desired eigenpairs */
125:   ipar[3]  = 0;            /* number of eigenpairs already converged */
126:   ipar[4]  = tr->maxlan;   /* maximum Lanczos basis size */
127:   ipar[5]  = tr->restart;  /* restarting scheme */
128:   PetscBLASIntCast(eps->max_it,&ipar[6]); /* maximum number of MATVECs */
129: #if !defined(PETSC_HAVE_MPIUNI)
130:   PetscBLASIntCast(MPI_Comm_c2f(PetscObjectComm((PetscObject)eps)),&ipar[7]);
131: #endif
132:   ipar[8]  = 0;            /* verboseness */
133:   ipar[9]  = 99;           /* Fortran IO unit number used to write log messages */
134:   ipar[10] = 1;            /* use supplied starting vector */
135:   ipar[11] = 0;            /* checkpointing flag */
136:   ipar[12] = 98;           /* Fortran IO unit number used to write checkpoint files */
137:   ipar[13] = 0;            /* number of flops per matvec per PE (not used) */
138:   tr->work[0] = eps->tol;  /* relative tolerance on residual norms */

140:   for (i=0;i<eps->ncv;i++) eps->eigr[i]=0.0;
141:   EPSGetStartVector(eps,0,NULL);
142:   BVSetActiveColumns(eps->V,0,0);  /* just for deflation space */
143:   BVGetColumn(eps->V,0,&v0);
144:   VecGetArray(v0,&pV);

146:   PetscStackCall("TRLan",TRLan_(MatMult_TRLAN,ipar,&n,&ncv,eps->eigr,pV,&n,tr->work,&tr->lwork));

148:   VecRestoreArray(v0,&pV);
149:   BVRestoreColumn(eps->V,0,&v0);

151:   stat        = ipar[0];
152:   eps->nconv  = ipar[3];
153:   eps->its    = ipar[25];
154:   eps->reason = EPS_CONVERGED_TOL;

156:   if (stat!=0) SETERRQ1(PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"Error in TRLAN (code=%d)",stat);
157:   return(0);
158: }

162: PetscErrorCode EPSReset_TRLAN(EPS eps)
163: {
165:   EPS_TRLAN      *tr = (EPS_TRLAN*)eps->data;

168:   PetscFree(tr->work);
169:   return(0);
170: }

174: PetscErrorCode EPSDestroy_TRLAN(EPS eps)
175: {

179:   PetscFree(eps->data);
180:   return(0);
181: }

185: PETSC_EXTERN PetscErrorCode EPSCreate_TRLAN(EPS eps)
186: {
187:   EPS_TRLAN      *ctx;

191:   PetscNewLog(eps,&ctx);
192:   eps->data = (void*)ctx;

194:   eps->ops->setup                = EPSSetUp_TRLAN;
195:   eps->ops->destroy              = EPSDestroy_TRLAN;
196:   eps->ops->reset                = EPSReset_TRLAN;
197:   eps->ops->backtransform        = EPSBackTransform_Default;
198:   return(0);
199: }