Actual source code: trlan.c

slepc-3.17.0 2022-03-31
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain

  6:    This file is part of SLEPc.
  7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
  8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9: */
 10: /*
 11:    This file implements a wrapper to the TRLAN package
 12: */

 14: #include <slepc/private/epsimpl.h>
 15: #include "trlan.h"

 17: /* Nasty global variable to access EPS data from TRLan_ */
 18: static struct {
 19:   EPS eps;
 20:   Vec x,y;
 21: } globaldata;

 23: PetscErrorCode EPSSetUp_TRLAN(EPS eps)
 24: {
 25:   EPS_TRLAN      *tr = (EPS_TRLAN*)eps->data;

 27:   EPSCheckHermitian(eps);
 28:   EPSCheckStandard(eps);
 29:   PetscBLASIntCast(PetscMax(7,eps->nev+PetscMin(eps->nev,6)),&tr->maxlan);
 30:   if (eps->ncv!=PETSC_DEFAULT) {
 32:   } else eps->ncv = tr->maxlan;
 33:   if (eps->mpd!=PETSC_DEFAULT) PetscInfo(eps,"Warning: parameter mpd ignored\n");
 34:   if (eps->max_it==PETSC_DEFAULT) eps->max_it = PetscMax(1000,eps->n);

 36:   if (!eps->which) eps->which = EPS_LARGEST_REAL;
 38:   EPSCheckUnsupported(eps,EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION | EPS_FEATURE_CONVERGENCE | EPS_FEATURE_STOPPING);
 39:   EPSCheckIgnored(eps,EPS_FEATURE_BALANCE | EPS_FEATURE_EXTRACTION);

 41:   tr->restart = 0;
 42:   if (tr->maxlan+1-eps->ncv<=0) PetscBLASIntCast(tr->maxlan*(tr->maxlan+10),&tr->lwork);
 43:   else PetscBLASIntCast(eps->nloc*(tr->maxlan+1-eps->ncv) + tr->maxlan*(tr->maxlan+10),&tr->lwork);
 44:   if (tr->work) PetscFree(tr->work);
 45:   PetscMalloc1(tr->lwork,&tr->work);
 46:   PetscLogObjectMemory((PetscObject)eps,tr->lwork*sizeof(PetscReal));

 48:   EPSAllocateSolution(eps,0);
 49:   PetscFunctionReturn(0);
 50: }

 52: static PetscBLASInt MatMult_TRLAN(PetscBLASInt *n,PetscBLASInt *m,PetscReal *xin,PetscBLASInt *ldx,PetscReal *yout,PetscBLASInt *ldy)
 53: {
 54:   Vec            x=globaldata.x,y=globaldata.y;
 55:   EPS            eps=globaldata.eps;
 56:   PetscBLASInt   i;

 58:   for (i=0;i<*m;i++) {
 59:     VecPlaceArray(x,(PetscScalar*)xin+i*(*ldx));
 60:     VecPlaceArray(y,(PetscScalar*)yout+i*(*ldy));
 61:     STApply(eps->st,x,y);
 62:     BVOrthogonalizeVec(eps->V,y,NULL,NULL,NULL);
 63:     VecResetArray(x);
 64:     VecResetArray(y);
 65:   }
 66:   PetscFunctionReturn(0);
 67: }

 69: PetscErrorCode EPSSolve_TRLAN(EPS eps)
 70: {
 71:   PetscInt       i;
 72:   PetscBLASInt   ipar[32],n,lohi,stat,ncv;
 73:   EPS_TRLAN      *tr = (EPS_TRLAN*)eps->data;
 74:   PetscScalar    *pV;
 75:   Vec            v0;
 76:   Mat            A;
 77: #if !defined(PETSC_HAVE_MPIUNI)
 78:   MPI_Fint       fcomm;
 79: #endif

 81:   PetscBLASIntCast(eps->ncv,&ncv);
 82:   PetscBLASIntCast(eps->nloc,&n);

 85:   lohi = (eps->which==EPS_SMALLEST_REAL)? -1: 1;

 87:   globaldata.eps = eps;
 88:   STGetMatrix(eps->st,0,&A);
 89:   MatCreateVecsEmpty(A,&globaldata.x,&globaldata.y);

 91:   ipar[0]  = 0;            /* stat: error flag */
 92:   ipar[1]  = lohi;         /* smallest (lohi<0) or largest eigenvalues (lohi>0) */
 93:   PetscBLASIntCast(eps->nev,&ipar[2]); /* number of desired eigenpairs */
 94:   ipar[3]  = 0;            /* number of eigenpairs already converged */
 95:   ipar[4]  = tr->maxlan;   /* maximum Lanczos basis size */
 96:   ipar[5]  = tr->restart;  /* restarting scheme */
 97:   PetscBLASIntCast(eps->max_it,&ipar[6]); /* maximum number of MATVECs */
 98: #if !defined(PETSC_HAVE_MPIUNI)
 99:   fcomm    = MPI_Comm_c2f(PetscObjectComm((PetscObject)eps));
100:   ipar[7]  = fcomm;
101: #endif
102:   ipar[8]  = 0;            /* verboseness */
103:   ipar[9]  = 99;           /* Fortran IO unit number used to write log messages */
104:   ipar[10] = 1;            /* use supplied starting vector */
105:   ipar[11] = 0;            /* checkpointing flag */
106:   ipar[12] = 98;           /* Fortran IO unit number used to write checkpoint files */
107:   ipar[13] = 0;            /* number of flops per matvec per PE (not used) */
108:   tr->work[0] = eps->tol;  /* relative tolerance on residual norms */

110:   for (i=0;i<eps->ncv;i++) eps->eigr[i]=0.0;
111:   EPSGetStartVector(eps,0,NULL);
112:   BVSetActiveColumns(eps->V,0,0);  /* just for deflation space */
113:   BVGetColumn(eps->V,0,&v0);
114:   VecGetArray(v0,&pV);

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

118:   VecRestoreArray(v0,&pV);
119:   BVRestoreColumn(eps->V,0,&v0);

121:   stat        = ipar[0];
122:   eps->nconv  = ipar[3];
123:   eps->its    = ipar[25];
124:   eps->reason = EPS_CONVERGED_TOL;

126:   VecDestroy(&globaldata.x);
127:   VecDestroy(&globaldata.y);
129:   PetscFunctionReturn(0);
130: }

132: PetscErrorCode EPSReset_TRLAN(EPS eps)
133: {
134:   EPS_TRLAN      *tr = (EPS_TRLAN*)eps->data;

136:   PetscFree(tr->work);
137:   PetscFunctionReturn(0);
138: }

140: PetscErrorCode EPSDestroy_TRLAN(EPS eps)
141: {
142:   PetscFree(eps->data);
143:   PetscFunctionReturn(0);
144: }

146: SLEPC_EXTERN PetscErrorCode EPSCreate_TRLAN(EPS eps)
147: {
148:   EPS_TRLAN      *ctx;

150:   PetscNewLog(eps,&ctx);
151:   eps->data = (void*)ctx;

153:   eps->ops->solve          = EPSSolve_TRLAN;
154:   eps->ops->setup          = EPSSetUp_TRLAN;
155:   eps->ops->setupsort      = EPSSetUpSort_Basic;
156:   eps->ops->destroy        = EPSDestroy_TRLAN;
157:   eps->ops->reset          = EPSReset_TRLAN;
158:   eps->ops->backtransform  = EPSBackTransform_Default;
159:   PetscFunctionReturn(0);
160: }