Actual source code: trlan.c
slepc-3.23.0 2025-03-29
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: static PetscErrorCode EPSSetUp_TRLAN(EPS eps)
24: {
25: EPS_TRLAN *tr = (EPS_TRLAN*)eps->data;
27: PetscFunctionBegin;
28: EPSCheckHermitian(eps);
29: EPSCheckStandard(eps);
30: EPSCheckNotStructured(eps);
31: if (eps->nev==0) eps->nev = 1;
32: PetscCall(PetscBLASIntCast(PetscMax(7,eps->nev+PetscMin(eps->nev,6)),&tr->maxlan));
33: if (eps->ncv!=PETSC_DETERMINE) {
34: PetscCheck(eps->ncv>=eps->nev,PetscObjectComm((PetscObject)eps),PETSC_ERR_USER_INPUT,"The value of ncv must be at least nev");
35: } else eps->ncv = tr->maxlan;
36: if (eps->mpd!=PETSC_DETERMINE) PetscCall(PetscInfo(eps,"Warning: parameter mpd ignored\n"));
37: if (eps->max_it==PETSC_DETERMINE) eps->max_it = PetscMax(1000,eps->n);
39: if (!eps->which) eps->which = EPS_LARGEST_REAL;
40: PetscCheck(eps->which==EPS_SMALLEST_REAL || eps->which==EPS_LARGEST_REAL || eps->which==EPS_TARGET_REAL,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver supports only smallest, largest or target real eigenvalues");
41: EPSCheckUnsupported(eps,EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION | EPS_FEATURE_CONVERGENCE | EPS_FEATURE_STOPPING);
42: EPSCheckIgnored(eps,EPS_FEATURE_BALANCE | EPS_FEATURE_EXTRACTION);
44: tr->restart = 0;
45: if (tr->maxlan+1-eps->ncv<=0) PetscCall(PetscBLASIntCast(tr->maxlan*(tr->maxlan+10),&tr->lwork));
46: else PetscCall(PetscBLASIntCast(eps->nloc*(tr->maxlan+1-eps->ncv) + tr->maxlan*(tr->maxlan+10),&tr->lwork));
47: if (tr->work) PetscCall(PetscFree(tr->work));
48: PetscCall(PetscMalloc1(tr->lwork,&tr->work));
50: PetscCall(EPSAllocateSolution(eps,0));
51: PetscFunctionReturn(PETSC_SUCCESS);
52: }
54: static PetscBLASInt MatMult_TRLAN(PetscBLASInt *n,PetscBLASInt *m,PetscReal *xin,PetscBLASInt *ldx,PetscReal *yout,PetscBLASInt *ldy)
55: {
56: Vec x=globaldata.x,y=globaldata.y;
57: EPS eps=globaldata.eps;
58: PetscBLASInt i;
60: PetscFunctionBegin;
61: for (i=0;i<*m;i++) {
62: PetscCall(VecPlaceArray(x,(PetscScalar*)xin+i*(*ldx)));
63: PetscCall(VecPlaceArray(y,(PetscScalar*)yout+i*(*ldy)));
64: PetscCall(STApply(eps->st,x,y));
65: PetscCall(BVOrthogonalizeVec(eps->V,y,NULL,NULL,NULL));
66: PetscCall(VecResetArray(x));
67: PetscCall(VecResetArray(y));
68: }
69: PetscFunctionReturn(PETSC_SUCCESS);
70: }
72: static PetscErrorCode EPSSolve_TRLAN(EPS eps)
73: {
74: PetscInt i;
75: PetscBLASInt ipar[32],n,lohi,stat,ncv;
76: EPS_TRLAN *tr = (EPS_TRLAN*)eps->data;
77: PetscScalar *pV;
78: Vec v0;
79: Mat A;
80: #if !defined(PETSC_HAVE_MPIUNI)
81: MPI_Fint fcomm;
82: #endif
84: PetscFunctionBegin;
85: PetscCall(PetscBLASIntCast(eps->ncv,&ncv));
86: PetscCall(PetscBLASIntCast(eps->nloc,&n));
88: PetscCheck(eps->which==EPS_LARGEST_REAL || eps->which==EPS_TARGET_REAL || eps->which==EPS_SMALLEST_REAL,PetscObjectComm((PetscObject)eps),PETSC_ERR_USER_INPUT,"Wrong value of eps->which");
89: lohi = (eps->which==EPS_SMALLEST_REAL)? -1: 1;
91: globaldata.eps = eps;
92: PetscCall(STGetMatrix(eps->st,0,&A));
93: PetscCall(MatCreateVecsEmpty(A,&globaldata.x,&globaldata.y));
95: ipar[0] = 0; /* stat: error flag */
96: ipar[1] = lohi; /* smallest (lohi<0) or largest eigenvalues (lohi>0) */
97: PetscCall(PetscBLASIntCast(eps->nev,&ipar[2])); /* number of desired eigenpairs */
98: ipar[3] = 0; /* number of eigenpairs already converged */
99: ipar[4] = tr->maxlan; /* maximum Lanczos basis size */
100: ipar[5] = tr->restart; /* restarting scheme */
101: PetscCall(PetscBLASIntCast(eps->max_it,&ipar[6])); /* maximum number of MATVECs */
102: #if !defined(PETSC_HAVE_MPIUNI)
103: fcomm = MPI_Comm_c2f(PetscObjectComm((PetscObject)eps));
104: ipar[7] = fcomm;
105: #endif
106: ipar[8] = 0; /* verboseness */
107: ipar[9] = 99; /* Fortran IO unit number used to write log messages */
108: ipar[10] = 1; /* use supplied starting vector */
109: ipar[11] = 0; /* checkpointing flag */
110: ipar[12] = 98; /* Fortran IO unit number used to write checkpoint files */
111: ipar[13] = 0; /* number of flops per matvec per PE (not used) */
112: tr->work[0] = eps->tol; /* relative tolerance on residual norms */
114: for (i=0;i<eps->ncv;i++) eps->eigr[i]=0.0;
115: PetscCall(EPSGetStartVector(eps,0,NULL));
116: PetscCall(BVSetActiveColumns(eps->V,0,0)); /* just for deflation space */
117: PetscCall(BVGetColumn(eps->V,0,&v0));
118: PetscCall(VecGetArray(v0,&pV));
120: PetscStackCallExternalVoid("TRLan",TRLan_(MatMult_TRLAN,ipar,&n,&ncv,eps->eigr,pV,&n,tr->work,&tr->lwork));
122: PetscCall(VecRestoreArray(v0,&pV));
123: PetscCall(BVRestoreColumn(eps->V,0,&v0));
125: stat = ipar[0];
126: eps->nconv = ipar[3];
127: eps->its = ipar[25];
128: eps->reason = EPS_CONVERGED_TOL;
130: PetscCall(VecDestroy(&globaldata.x));
131: PetscCall(VecDestroy(&globaldata.y));
132: PetscCheck(stat==0,PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"Error in TRLAN (code=%" PetscBLASInt_FMT ")",stat);
133: PetscFunctionReturn(PETSC_SUCCESS);
134: }
136: static PetscErrorCode EPSReset_TRLAN(EPS eps)
137: {
138: EPS_TRLAN *tr = (EPS_TRLAN*)eps->data;
140: PetscFunctionBegin;
141: PetscCall(PetscFree(tr->work));
142: PetscFunctionReturn(PETSC_SUCCESS);
143: }
145: static PetscErrorCode EPSDestroy_TRLAN(EPS eps)
146: {
147: PetscFunctionBegin;
148: PetscCall(PetscFree(eps->data));
149: PetscFunctionReturn(PETSC_SUCCESS);
150: }
152: SLEPC_EXTERN PetscErrorCode EPSCreate_TRLAN(EPS eps)
153: {
154: EPS_TRLAN *ctx;
156: PetscFunctionBegin;
157: PetscCall(PetscNew(&ctx));
158: eps->data = (void*)ctx;
160: eps->ops->solve = EPSSolve_TRLAN;
161: eps->ops->setup = EPSSetUp_TRLAN;
162: eps->ops->setupsort = EPSSetUpSort_Basic;
163: eps->ops->destroy = EPSDestroy_TRLAN;
164: eps->ops->reset = EPSReset_TRLAN;
165: eps->ops->backtransform = EPSBackTransform_Default;
166: PetscFunctionReturn(PETSC_SUCCESS);
167: }