Actual source code: scalapack.c
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 eigensolvers in ScaLAPACK.
12: */
14: #include <slepc/private/epsimpl.h>
15: #include <slepc/private/slepcscalapack.h>
17: typedef struct {
18: Mat As,Bs; /* converted matrices */
19: } EPS_ScaLAPACK;
21: static PetscErrorCode EPSSetUp_ScaLAPACK(EPS eps)
22: {
23: EPS_ScaLAPACK *ctx = (EPS_ScaLAPACK*)eps->data;
24: Mat A,B;
25: PetscInt nmat;
26: PetscBool isshift;
27: PetscScalar shift;
29: PetscFunctionBegin;
30: EPSCheckHermitianDefinite(eps);
31: EPSCheckNotStructured(eps);
32: PetscCall(PetscObjectTypeCompare((PetscObject)eps->st,STSHIFT,&isshift));
33: PetscCheck(isshift,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support spectral transformations");
34: if (eps->nev==0) eps->nev = 1;
35: eps->ncv = eps->n;
36: if (eps->mpd!=PETSC_DETERMINE) PetscCall(PetscInfo(eps,"Warning: parameter mpd ignored\n"));
37: if (eps->max_it==PETSC_DETERMINE) eps->max_it = 1;
38: if (!eps->which) PetscCall(EPSSetWhichEigenpairs_Default(eps));
39: PetscCheck(eps->which!=EPS_ALL || eps->inta==eps->intb,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support interval computation");
40: EPSCheckUnsupported(eps,EPS_FEATURE_BALANCE | EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION);
41: EPSCheckIgnored(eps,EPS_FEATURE_EXTRACTION | EPS_FEATURE_CONVERGENCE | EPS_FEATURE_STOPPING);
42: PetscCall(EPSAllocateSolution(eps,0));
44: /* convert matrices */
45: PetscCall(MatDestroy(&ctx->As));
46: PetscCall(MatDestroy(&ctx->Bs));
47: PetscCall(STGetNumMatrices(eps->st,&nmat));
48: PetscCall(STGetMatrix(eps->st,0,&A));
49: PetscCall(MatConvert(A,MATSCALAPACK,MAT_INITIAL_MATRIX,&ctx->As));
50: if (nmat>1) {
51: PetscCall(STGetMatrix(eps->st,1,&B));
52: PetscCall(MatConvert(B,MATSCALAPACK,MAT_INITIAL_MATRIX,&ctx->Bs));
53: }
54: PetscCall(STGetShift(eps->st,&shift));
55: if (shift != 0.0) {
56: if (nmat>1) PetscCall(MatAXPY(ctx->As,-shift,ctx->Bs,SAME_NONZERO_PATTERN));
57: else PetscCall(MatShift(ctx->As,-shift));
58: }
59: PetscFunctionReturn(PETSC_SUCCESS);
60: }
62: static PetscErrorCode EPSSolve_ScaLAPACK(EPS eps)
63: {
64: EPS_ScaLAPACK *ctx = (EPS_ScaLAPACK*)eps->data;
65: Mat A = ctx->As,B = ctx->Bs,Q,V;
66: Mat_ScaLAPACK *a = (Mat_ScaLAPACK*)A->data,*b,*q;
67: PetscReal rdummy=0.0,abstol=0.0,*gap=NULL,orfac=-1.0,*w = eps->errest; /* used to store real eigenvalues */
68: PetscScalar *work,minlwork[3];
69: PetscBLASInt i,m,idummy=0,lwork=-1,liwork=-1,minliwork,*iwork,*ifail=NULL,*iclustr=NULL,one=1;
70: #if PetscDefined(USE_COMPLEX)
71: PetscReal *rwork,minlrwork[3];
72: PetscBLASInt lrwork=-1;
73: #endif
75: PetscFunctionBegin;
76: PetscCall(MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&Q));
77: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
78: q = (Mat_ScaLAPACK*)Q->data;
80: if (B) {
82: b = (Mat_ScaLAPACK*)B->data;
83: PetscCall(PetscMalloc3(a->grid->nprow*a->grid->npcol,&gap,a->N,&ifail,2*a->grid->nprow*a->grid->npcol,&iclustr));
84: #if !PetscDefined(USE_COMPLEX)
85: /* allocate workspace */
86: PetscCallScaLAPACKInfo("sygvx",SCALAPACKsygvx_(&one,"V","A","L",&a->N,a->loc,&one,&one,a->desc,b->loc,&one,&one,b->desc,&rdummy,&rdummy,&idummy,&idummy,&abstol,&m,&idummy,w,&orfac,q->loc,&one,&one,q->desc,minlwork,&lwork,&minliwork,&liwork,ifail,iclustr,gap,&info));
87: PetscCall(PetscBLASIntCast((PetscInt)minlwork[0],&lwork));
88: liwork = minliwork;
89: /* call computational routine */
90: PetscCall(PetscMalloc2(lwork,&work,liwork,&iwork));
91: PetscCallScaLAPACKInfo("sygvx",SCALAPACKsygvx_(&one,"V","A","L",&a->N,a->loc,&one,&one,a->desc,b->loc,&one,&one,b->desc,&rdummy,&rdummy,&idummy,&idummy,&abstol,&m,&idummy,w,&orfac,q->loc,&one,&one,q->desc,work,&lwork,iwork,&liwork,ifail,iclustr,gap,&info));
92: PetscCall(PetscFree2(work,iwork));
93: #else
94: /* allocate workspace */
95: PetscCallScaLAPACKInfo("sygvx",SCALAPACKsygvx_(&one,"V","A","L",&a->N,a->loc,&one,&one,a->desc,b->loc,&one,&one,b->desc,&rdummy,&rdummy,&idummy,&idummy,&abstol,&m,&idummy,w,&orfac,q->loc,&one,&one,q->desc,minlwork,&lwork,minlrwork,&lrwork,&minliwork,&liwork,ifail,iclustr,gap,&info));
96: PetscCall(PetscBLASIntCast((PetscInt)PetscRealPart(minlwork[0]),&lwork));
97: PetscCall(PetscBLASIntCast((PetscInt)minlrwork[0],&lrwork));
98: lrwork += a->N*a->N;
99: liwork = minliwork;
100: /* call computational routine */
101: PetscCall(PetscMalloc3(lwork,&work,lrwork,&rwork,liwork,&iwork));
102: PetscCallScaLAPACKInfo("sygvx",SCALAPACKsygvx_(&one,"V","A","L",&a->N,a->loc,&one,&one,a->desc,b->loc,&one,&one,b->desc,&rdummy,&rdummy,&idummy,&idummy,&abstol,&m,&idummy,w,&orfac,q->loc,&one,&one,q->desc,work,&lwork,rwork,&lrwork,iwork,&liwork,ifail,iclustr,gap,&info));
103: PetscCall(PetscFree3(work,rwork,iwork));
104: #endif
105: PetscCall(PetscFree3(gap,ifail,iclustr));
107: } else {
109: #if !PetscDefined(USE_COMPLEX)
110: /* allocate workspace */
111: PetscCallScaLAPACKInfo("syev",SCALAPACKsyev_("V","L",&a->N,a->loc,&one,&one,a->desc,w,q->loc,&one,&one,q->desc,minlwork,&lwork,&info));
112: PetscCall(PetscBLASIntCast((PetscInt)minlwork[0],&lwork));
113: PetscCall(PetscMalloc1(lwork,&work));
114: /* call computational routine */
115: PetscCallScaLAPACKInfo("syev",SCALAPACKsyev_("V","L",&a->N,a->loc,&one,&one,a->desc,w,q->loc,&one,&one,q->desc,work,&lwork,&info));
116: PetscCall(PetscFree(work));
117: #else
118: /* allocate workspace */
119: PetscCallScaLAPACKInfo("syev",SCALAPACKsyev_("V","L",&a->N,a->loc,&one,&one,a->desc,w,q->loc,&one,&one,q->desc,minlwork,&lwork,minlrwork,&lrwork,&info));
120: PetscCall(PetscBLASIntCast((PetscInt)PetscRealPart(minlwork[0]),&lwork));
121: lrwork = 4*a->N; /* PetscCall(PetscBLASIntCast((PetscInt)minlrwork[0],&lrwork)); */
122: PetscCall(PetscMalloc2(lwork,&work,lrwork,&rwork));
123: /* call computational routine */
124: PetscCallScaLAPACKInfo("syev",SCALAPACKsyev_("V","L",&a->N,a->loc,&one,&one,a->desc,w,q->loc,&one,&one,q->desc,work,&lwork,rwork,&lrwork,&info));
125: PetscCall(PetscFree2(work,rwork));
126: #endif
128: }
129: PetscCall(PetscFPTrapPop());
131: for (i=0;i<eps->ncv;i++) {
132: eps->eigr[i] = eps->errest[i];
133: eps->errest[i] = PETSC_MACHINE_EPSILON;
134: }
136: PetscCall(BVGetMat(eps->V,&V));
137: PetscCall(MatConvert(Q,MATDENSE,MAT_REUSE_MATRIX,&V));
138: PetscCall(BVRestoreMat(eps->V,&V));
139: PetscCall(MatDestroy(&Q));
141: eps->nconv = eps->ncv;
142: eps->its = 1;
143: eps->reason = EPS_CONVERGED_TOL;
144: PetscFunctionReturn(PETSC_SUCCESS);
145: }
147: static PetscErrorCode EPSDestroy_ScaLAPACK(EPS eps)
148: {
149: PetscFunctionBegin;
150: PetscCall(PetscFree(eps->data));
151: PetscFunctionReturn(PETSC_SUCCESS);
152: }
154: static PetscErrorCode EPSReset_ScaLAPACK(EPS eps)
155: {
156: EPS_ScaLAPACK *ctx = (EPS_ScaLAPACK*)eps->data;
158: PetscFunctionBegin;
159: PetscCall(MatDestroy(&ctx->As));
160: PetscCall(MatDestroy(&ctx->Bs));
161: PetscFunctionReturn(PETSC_SUCCESS);
162: }
164: /*MC
165: EPSSCALAPACK - EPSSCALAPACK = "scalapack" - A wrapper to ScaLAPACK
166: eigensolvers {cite:p}`Bla97`.
168: Notes:
169: Only available for Hermitian problems, using subroutines `pdsyev` and
170: `pdsygvx` and the analogs for other precisions.
172: This is a direct eigensolver, that is, the full spectrum is computed.
173: The computation involves redistributing the matrices from PETSc storage
174: to ScaLAPACK distribution, and vice versa (this is done automatically
175: by SLEPc). Alternatively, the user may create the problem matrices
176: already with type `MATSCALAPACK`.
178: Level: beginner
180: .seealso: [](ch:eps), `EPS`, `EPSType`, `EPSSetType()`
181: M*/
182: SLEPC_EXTERN PetscErrorCode EPSCreate_ScaLAPACK(EPS eps)
183: {
184: EPS_ScaLAPACK *ctx;
186: PetscFunctionBegin;
187: PetscCall(PetscNew(&ctx));
188: eps->data = (void*)ctx;
190: eps->categ = EPS_CATEGORY_OTHER;
192: eps->ops->solve = EPSSolve_ScaLAPACK;
193: eps->ops->setup = EPSSetUp_ScaLAPACK;
194: eps->ops->setupsort = EPSSetUpSort_Basic;
195: eps->ops->destroy = EPSDestroy_ScaLAPACK;
196: eps->ops->reset = EPSReset_ScaLAPACK;
197: eps->ops->backtransform = EPSBackTransform_Default;
198: eps->ops->setdefaultst = EPSSetDefaultST_NoFactor;
199: PetscFunctionReturn(PETSC_SUCCESS);
200: }