Actual source code: dsnhep.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: */
11: #include <slepc/private/dsimpl.h>
12: #include <slepcblaslapack.h>
14: static PetscErrorCode DSAllocate_NHEP(DS ds,PetscInt ld)
15: {
16: PetscFunctionBegin;
17: PetscCall(DSAllocateMat_Private(ds,DS_MAT_A));
18: PetscCall(DSAllocateMat_Private(ds,DS_MAT_Q));
19: PetscCall(PetscFree(ds->perm));
20: PetscCall(PetscMalloc1(ld,&ds->perm));
21: PetscFunctionReturn(PETSC_SUCCESS);
22: }
24: static PetscErrorCode DSView_NHEP(DS ds,PetscViewer viewer)
25: {
26: PetscViewerFormat format;
28: PetscFunctionBegin;
29: PetscCall(PetscViewerGetFormat(viewer,&format));
30: if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS);
31: PetscCall(DSViewMat(ds,viewer,DS_MAT_A));
32: if (ds->state>DS_STATE_INTERMEDIATE) PetscCall(DSViewMat(ds,viewer,DS_MAT_Q));
33: if (ds->omat[DS_MAT_X]) PetscCall(DSViewMat(ds,viewer,DS_MAT_X));
34: if (ds->omat[DS_MAT_Y]) PetscCall(DSViewMat(ds,viewer,DS_MAT_Y));
35: PetscFunctionReturn(PETSC_SUCCESS);
36: }
38: static PetscErrorCode DSVectors_NHEP_Refined_Some(DS ds,PetscInt *k,PetscReal *rnorm,PetscBool left)
39: {
40: PetscInt i,j;
41: PetscBLASInt ld,n,n1,lwork,inc=1;
42: PetscScalar sdummy,done=1.0,zero=0.0;
43: PetscReal *sigma;
44: PetscBool iscomplex = PETSC_FALSE;
45: PetscScalar *X,*W;
46: const PetscScalar *A,*Q;
48: PetscFunctionBegin;
49: PetscCheck(!left,PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Not implemented for left vectors");
50: PetscCall(PetscBLASIntCast(ds->n,&n));
51: PetscCall(PetscBLASIntCast(ds->ld,&ld));
52: n1 = n+1;
53: PetscCall(DSAllocateWork_Private(ds,5*ld,6*ld,0));
54: PetscCall(DSAllocateMat_Private(ds,DS_MAT_W));
55: lwork = 5*ld;
56: sigma = ds->rwork+5*ld;
58: /* build A-w*I in W */
59: PetscCall(MatDenseGetArrayRead(ds->omat[DS_MAT_A],&A));
60: PetscCall(MatDenseGetArrayWrite(ds->omat[DS_MAT_W],&W));
61: if ((*k)<n-1 && A[(*k)+1+(*k)*ld]!=0.0) iscomplex = PETSC_TRUE;
62: PetscCheck(!iscomplex,PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented for complex eigenvalues yet");
63: for (j=0;j<n;j++)
64: for (i=0;i<=n;i++)
65: W[i+j*ld] = A[i+j*ld];
66: for (i=0;i<n;i++)
67: W[i+i*ld] -= A[(*k)+(*k)*ld];
68: PetscCall(MatDenseRestoreArrayRead(ds->omat[DS_MAT_A],&A));
70: /* compute SVD of W */
71: #if !PetscDefined(USE_COMPLEX)
72: PetscCallLAPACKInfo("LAPACKgesvd",LAPACKgesvd_("N","O",&n1,&n,W,&ld,sigma,&sdummy,&ld,&sdummy,&ld,ds->work,&lwork,&info));
73: #else
74: PetscCallLAPACKInfo("LAPACKgesvd",LAPACKgesvd_("N","O",&n1,&n,W,&ld,sigma,&sdummy,&ld,&sdummy,&ld,ds->work,&lwork,ds->rwork,&info));
75: #endif
77: /* the smallest singular value is the new error estimate */
78: if (rnorm) *rnorm = sigma[n-1];
80: /* update vector with right singular vector associated to smallest singular value,
81: accumulating the transformation matrix Q */
82: PetscCall(MatDenseGetArrayRead(ds->omat[DS_MAT_Q],&Q));
83: PetscCall(MatDenseGetArray(ds->omat[left?DS_MAT_Y:DS_MAT_X],&X));
84: PetscCallBLAS("BLASgemv",BLASgemv_("N",&n,&n,&done,Q,&ld,W+n-1,&ld,&zero,X+(*k)*ld,&inc));
85: PetscCall(MatDenseRestoreArrayRead(ds->omat[DS_MAT_Q],&Q));
86: PetscCall(MatDenseRestoreArray(ds->omat[left?DS_MAT_Y:DS_MAT_X],&X));
87: PetscCall(MatDenseRestoreArrayWrite(ds->omat[DS_MAT_W],&W));
88: PetscFunctionReturn(PETSC_SUCCESS);
89: }
91: static PetscErrorCode DSVectors_NHEP_Refined_All(DS ds,PetscBool left)
92: {
93: PetscInt i;
95: PetscFunctionBegin;
96: for (i=0;i<ds->n;i++) PetscCall(DSVectors_NHEP_Refined_Some(ds,&i,NULL,left));
97: PetscFunctionReturn(PETSC_SUCCESS);
98: }
100: static PetscErrorCode DSVectors_NHEP_Eigen_Some(DS ds,PetscInt *k,PetscReal *rnorm,PetscBool left)
101: {
102: PetscInt i;
103: PetscBLASInt mm=1,mout,ld,n,*select,inc=1,cols=1,zero=0;
104: PetscScalar sone=1.0,szero=0.0;
105: PetscReal norm,done=1.0;
106: PetscBool iscomplex = PETSC_FALSE;
107: PetscScalar *X,*Y;
108: const PetscScalar *A,*Q;
110: PetscFunctionBegin;
111: PetscCall(PetscBLASIntCast(ds->n,&n));
112: PetscCall(PetscBLASIntCast(ds->ld,&ld));
113: PetscCall(DSAllocateWork_Private(ds,0,0,ld));
114: select = ds->iwork;
115: for (i=0;i<n;i++) select[i] = (PetscBLASInt)PETSC_FALSE;
117: /* compute k-th eigenvector Y of A */
118: PetscCall(MatDenseGetArrayRead(ds->omat[DS_MAT_A],&A));
119: PetscCall(MatDenseGetArray(ds->omat[left?DS_MAT_Y:DS_MAT_X],&X));
120: Y = X+(*k)*ld;
121: select[*k] = (PetscBLASInt)PETSC_TRUE;
122: #if !PetscDefined(USE_COMPLEX)
123: if ((*k)<n-1 && A[(*k)+1+(*k)*ld]!=0.0) iscomplex = PETSC_TRUE;
124: mm = iscomplex? 2: 1;
125: if (iscomplex) select[(*k)+1] = (PetscBLASInt)PETSC_TRUE;
126: PetscCall(DSAllocateWork_Private(ds,3*ld,0,0));
127: PetscCallLAPACKInfo("LAPACKtrevc",LAPACKtrevc_(left?"L":"R","S",select,&n,(PetscScalar*)A,&ld,Y,&ld,Y,&ld,&mm,&mout,ds->work,&info));
128: #else
129: PetscCall(DSAllocateWork_Private(ds,2*ld,ld,0));
130: PetscCallLAPACKInfo("LAPACKtrevc",LAPACKtrevc_(left?"L":"R","S",select,&n,(PetscScalar*)A,&ld,Y,&ld,Y,&ld,&mm,&mout,ds->work,ds->rwork,&info));
131: #endif
132: PetscCheck(mout==mm,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Inconsistent arguments");
133: PetscCall(MatDenseRestoreArrayRead(ds->omat[DS_MAT_A],&A));
135: /* accumulate and normalize eigenvectors */
136: if (ds->state>=DS_STATE_CONDENSED) {
137: PetscCall(MatDenseGetArrayRead(ds->omat[DS_MAT_Q],&Q));
138: PetscCall(PetscArraycpy(ds->work,Y,mout*ld));
139: PetscCallBLAS("BLASgemv",BLASgemv_("N",&n,&n,&sone,Q,&ld,ds->work,&inc,&szero,Y,&inc));
140: #if !PetscDefined(USE_COMPLEX)
141: if (iscomplex) PetscCallBLAS("BLASgemv",BLASgemv_("N",&n,&n,&sone,Q,&ld,ds->work+ld,&inc,&szero,Y+ld,&inc));
142: #endif
143: PetscCall(MatDenseRestoreArrayRead(ds->omat[DS_MAT_Q],&Q));
144: cols = 1;
145: norm = BLASnrm2_(&n,Y,&inc);
146: #if !PetscDefined(USE_COMPLEX)
147: if (iscomplex) {
148: norm = SlepcAbsEigenvalue(norm,BLASnrm2_(&n,Y+ld,&inc));
149: cols = 2;
150: }
151: #endif
152: PetscCallLAPACKInfo("LAPACKlascl",LAPACKlascl_("G",&zero,&zero,&norm,&done,&n,&cols,Y,&ld,&info));
153: }
155: /* set output arguments */
156: if (iscomplex) (*k)++;
157: if (rnorm) {
158: if (iscomplex) *rnorm = SlepcAbsEigenvalue(Y[n-1],Y[n-1+ld]);
159: else *rnorm = PetscAbsScalar(Y[n-1]);
160: }
161: PetscCall(MatDenseRestoreArray(ds->omat[left?DS_MAT_Y:DS_MAT_X],&X));
162: PetscFunctionReturn(PETSC_SUCCESS);
163: }
165: static PetscErrorCode DSVectors_NHEP_Eigen_All(DS ds,PetscBool left)
166: {
167: PetscInt i;
168: PetscBLASInt n,ld,mout,inc=1,cols,zero=0;
169: PetscBool iscomplex;
170: PetscScalar *X,*Y,*Z;
171: const PetscScalar *A,*Q;
172: PetscReal norm,done=1.0;
173: const char *side,*back;
175: PetscFunctionBegin;
176: PetscCall(MatDenseGetArrayRead(ds->omat[DS_MAT_A],&A));
177: PetscCall(PetscBLASIntCast(ds->n,&n));
178: PetscCall(PetscBLASIntCast(ds->ld,&ld));
179: if (left) {
180: X = NULL;
181: PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Y],&Y));
182: side = "L";
183: } else {
184: PetscCall(MatDenseGetArray(ds->omat[DS_MAT_X],&X));
185: Y = NULL;
186: side = "R";
187: }
188: Z = left? Y: X;
189: if (ds->state>=DS_STATE_CONDENSED) {
190: /* DSSolve() has been called, backtransform with matrix Q */
191: back = "B";
192: PetscCall(MatDenseGetArrayRead(ds->omat[DS_MAT_Q],&Q));
193: PetscCall(PetscArraycpy(Z,Q,ld*ld));
194: PetscCall(MatDenseRestoreArrayRead(ds->omat[DS_MAT_Q],&Q));
195: } else back = "A";
196: #if !PetscDefined(USE_COMPLEX)
197: PetscCall(DSAllocateWork_Private(ds,3*ld,0,0));
198: PetscCallLAPACKInfo("LAPACKtrevc",LAPACKtrevc_(side,back,NULL,&n,(PetscScalar*)A,&ld,Y,&ld,X,&ld,&n,&mout,ds->work,&info));
199: #else
200: PetscCall(DSAllocateWork_Private(ds,2*ld,ld,0));
201: PetscCallLAPACKInfo("LAPACKtrevc",LAPACKtrevc_(side,back,NULL,&n,(PetscScalar*)A,&ld,Y,&ld,X,&ld,&n,&mout,ds->work,ds->rwork,&info));
202: #endif
204: /* normalize eigenvectors */
205: for (i=0;i<n;i++) {
206: iscomplex = (i<n-1 && A[i+1+i*ld]!=0.0)? PETSC_TRUE: PETSC_FALSE;
207: cols = 1;
208: norm = BLASnrm2_(&n,Z+i*ld,&inc);
209: #if !PetscDefined(USE_COMPLEX)
210: if (iscomplex) {
211: norm = SlepcAbsEigenvalue(norm,BLASnrm2_(&n,Z+(i+1)*ld,&inc));
212: cols = 2;
213: }
214: #endif
215: PetscCallLAPACKInfo("LAPACKlascl",LAPACKlascl_("G",&zero,&zero,&norm,&done,&n,&cols,Z+i*ld,&ld,&info));
216: if (iscomplex) i++;
217: }
218: PetscCall(MatDenseRestoreArrayRead(ds->omat[DS_MAT_A],&A));
219: PetscCall(MatDenseRestoreArray(ds->omat[left?DS_MAT_Y:DS_MAT_X],&Z));
220: PetscFunctionReturn(PETSC_SUCCESS);
221: }
223: static PetscErrorCode DSVectors_NHEP(DS ds,DSMatType mat,PetscInt *j,PetscReal *rnorm)
224: {
225: PetscFunctionBegin;
226: switch (mat) {
227: case DS_MAT_X:
228: if (ds->refined) {
229: PetscCheck(ds->extrarow,PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Refined vectors require activating the extra row");
230: if (j) PetscCall(DSVectors_NHEP_Refined_Some(ds,j,rnorm,PETSC_FALSE));
231: else PetscCall(DSVectors_NHEP_Refined_All(ds,PETSC_FALSE));
232: } else {
233: if (j) PetscCall(DSVectors_NHEP_Eigen_Some(ds,j,rnorm,PETSC_FALSE));
234: else PetscCall(DSVectors_NHEP_Eigen_All(ds,PETSC_FALSE));
235: }
236: break;
237: case DS_MAT_Y:
238: PetscCheck(!ds->refined,PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Not implemented yet");
239: if (j) PetscCall(DSVectors_NHEP_Eigen_Some(ds,j,rnorm,PETSC_TRUE));
240: else PetscCall(DSVectors_NHEP_Eigen_All(ds,PETSC_TRUE));
241: break;
242: case DS_MAT_U:
243: case DS_MAT_V:
244: SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Not implemented yet");
245: default:
246: SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_OUTOFRANGE,"Invalid mat parameter");
247: }
248: PetscFunctionReturn(PETSC_SUCCESS);
249: }
251: static PetscErrorCode DSSort_NHEP_Arbitrary(DS ds,PetscScalar *wr,PetscScalar *wi,PetscScalar *rr,PetscScalar *ri,PetscInt *k)
252: {
253: PetscInt i;
254: PetscBLASInt n,ld,mout,lwork,*selection;
255: PetscScalar *T,*Q,*work;
256: PetscReal dummy;
257: #if !PetscDefined(USE_COMPLEX)
258: PetscBLASInt *iwork,liwork;
259: #endif
261: PetscFunctionBegin;
262: PetscCheck(k,PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_WRONG,"Must supply argument k");
263: PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&T));
264: PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Q],&Q));
265: PetscCall(PetscBLASIntCast(ds->n,&n));
266: PetscCall(PetscBLASIntCast(ds->ld,&ld));
267: #if !PetscDefined(USE_COMPLEX)
268: lwork = n;
269: liwork = 1;
270: PetscCall(DSAllocateWork_Private(ds,lwork,0,liwork+n));
271: work = ds->work;
272: PetscCall(PetscBLASIntCast(ds->lwork,&lwork));
273: selection = ds->iwork;
274: iwork = ds->iwork + n;
275: PetscCall(PetscBLASIntCast(ds->liwork-n,&liwork));
276: #else
277: lwork = 1;
278: PetscCall(DSAllocateWork_Private(ds,lwork,0,n));
279: work = ds->work;
280: selection = ds->iwork;
281: #endif
282: /* Compute the selected eigenvalue to be in the leading position */
283: PetscCall(DSSortEigenvalues_Private(ds,rr,ri,ds->perm,PETSC_FALSE));
284: PetscCall(PetscArrayzero(selection,n));
285: for (i=0;i<*k;i++) selection[ds->perm[i]] = 1;
286: #if !PetscDefined(USE_COMPLEX)
287: PetscCallLAPACKInfo("LAPACKtrsen",LAPACKtrsen_("N","V",selection,&n,T,&ld,Q,&ld,wr,wi,&mout,&dummy,&dummy,work,&lwork,iwork,&liwork,&info));
288: #else
289: PetscCallLAPACKInfo("LAPACKtrsen",LAPACKtrsen_("N","V",selection,&n,T,&ld,Q,&ld,wr,&mout,&dummy,&dummy,work,&lwork,&info));
290: #endif
291: *k = mout;
292: PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&T));
293: PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Q],&Q));
294: PetscFunctionReturn(PETSC_SUCCESS);
295: }
297: static PetscErrorCode DSSort_NHEP(DS ds,PetscScalar *wr,PetscScalar *wi,PetscScalar *rr,PetscScalar *ri,PetscInt *k)
298: {
299: PetscFunctionBegin;
300: if (!rr || wr == rr) PetscCall(DSSort_NHEP_Total(ds,DS_MAT_A,DS_MAT_Q,wr,wi));
301: else PetscCall(DSSort_NHEP_Arbitrary(ds,wr,wi,rr,ri,k));
302: PetscFunctionReturn(PETSC_SUCCESS);
303: }
305: static PetscErrorCode DSSortWithPermutation_NHEP(DS ds,PetscInt *perm,PetscScalar *wr,PetscScalar *wi)
306: {
307: PetscFunctionBegin;
308: PetscCall(DSSortWithPermutation_NHEP_Private(ds,perm,DS_MAT_A,DS_MAT_Q,wr,wi));
309: PetscFunctionReturn(PETSC_SUCCESS);
310: }
312: static PetscErrorCode DSUpdateExtraRow_NHEP(DS ds)
313: {
314: PetscInt i;
315: PetscBLASInt n,ld,incx=1;
316: PetscScalar *A,*x,*y,one=1.0,zero=0.0;
317: const PetscScalar *Q;
319: PetscFunctionBegin;
320: PetscCall(PetscBLASIntCast(ds->n,&n));
321: PetscCall(PetscBLASIntCast(ds->ld,&ld));
322: PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
323: PetscCall(MatDenseGetArrayRead(ds->omat[DS_MAT_Q],&Q));
324: PetscCall(DSAllocateWork_Private(ds,2*ld,0,0));
325: x = ds->work;
326: y = ds->work+ld;
327: for (i=0;i<n;i++) x[i] = PetscConj(A[n+i*ld]);
328: PetscCallBLAS("BLASgemv",BLASgemv_("C",&n,&n,&one,Q,&ld,x,&incx,&zero,y,&incx));
329: for (i=0;i<n;i++) A[n+i*ld] = PetscConj(y[i]);
330: PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
331: PetscCall(MatDenseRestoreArrayRead(ds->omat[DS_MAT_Q],&Q));
332: ds->k = n;
333: PetscFunctionReturn(PETSC_SUCCESS);
334: }
336: static PetscErrorCode DSSolve_NHEP(DS ds,PetscScalar *wr,PetscScalar *wi)
337: {
338: PetscFunctionBegin;
339: #if !PetscDefined(USE_COMPLEX)
340: PetscAssertPointer(wi,3);
341: #endif
342: PetscCall(DSSolve_NHEP_Private(ds,DS_MAT_A,DS_MAT_Q,wr,wi));
343: PetscFunctionReturn(PETSC_SUCCESS);
344: }
346: #if !PetscDefined(HAVE_MPIUNI)
347: static PetscErrorCode DSSynchronize_NHEP(DS ds,PetscScalar eigr[],PetscScalar eigi[])
348: {
349: PetscInt ld=ds->ld,l=ds->l,k;
350: PetscMPIInt n,rank,off=0,size,ldn;
351: PetscScalar *A,*Q;
353: PetscFunctionBegin;
354: k = (ds->n-l)*ld;
355: if (ds->state>DS_STATE_RAW) k += (ds->n-l)*ld;
356: if (eigr) k += ds->n-l;
357: if (eigi) k += ds->n-l;
358: PetscCall(DSAllocateWork_Private(ds,k,0,0));
359: PetscCall(PetscMPIIntCast(k*sizeof(PetscScalar),&size));
360: PetscCall(PetscMPIIntCast(ds->n-l,&n));
361: PetscCall(PetscMPIIntCast(ld*(ds->n-l),&ldn));
362: PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
363: if (ds->state>DS_STATE_RAW) PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Q],&Q));
364: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ds),&rank));
365: if (!rank) {
366: PetscCallMPI(MPI_Pack(A+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
367: if (ds->state>DS_STATE_RAW) PetscCallMPI(MPI_Pack(Q+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
368: if (eigr) PetscCallMPI(MPI_Pack(eigr+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
369: #if !PetscDefined(USE_COMPLEX)
370: if (eigi) PetscCallMPI(MPI_Pack(eigi+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
371: #endif
372: }
373: PetscCallMPI(MPI_Bcast(ds->work,size,MPI_BYTE,0,PetscObjectComm((PetscObject)ds)));
374: if (rank) {
375: PetscCallMPI(MPI_Unpack(ds->work,size,&off,A+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
376: if (ds->state>DS_STATE_RAW) PetscCallMPI(MPI_Unpack(ds->work,size,&off,Q+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
377: if (eigr) PetscCallMPI(MPI_Unpack(ds->work,size,&off,eigr+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
378: #if !PetscDefined(USE_COMPLEX)
379: if (eigi) PetscCallMPI(MPI_Unpack(ds->work,size,&off,eigi+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
380: #endif
381: }
382: PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
383: if (ds->state>DS_STATE_RAW) PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Q],&Q));
384: PetscFunctionReturn(PETSC_SUCCESS);
385: }
386: #endif
388: static PetscErrorCode DSTruncate_NHEP(DS ds,PetscInt n,PetscBool trim)
389: {
390: PetscInt i,ld=ds->ld,l=ds->l;
391: PetscScalar *A;
393: PetscFunctionBegin;
394: PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
395: #if PetscDefined(USE_DEBUG)
396: /* make sure diagonal 2x2 block is not broken */
397: PetscCheck(ds->state<DS_STATE_CONDENSED || n==0 || n==ds->n || A[n+(n-1)*ld]==0.0,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"The given size would break a 2x2 block, call DSGetTruncateSize() first");
398: #endif
399: if (trim) {
400: if (ds->extrarow) { /* clean extra row */
401: for (i=l;i<ds->n;i++) A[ds->n+i*ld] = 0.0;
402: }
403: ds->l = 0;
404: ds->k = 0;
405: ds->n = n;
406: ds->t = ds->n; /* truncated length equal to the new dimension */
407: } else {
408: if (ds->extrarow && ds->k==ds->n) {
409: /* copy entries of extra row to the new position, then clean last row */
410: for (i=l;i<n;i++) A[n+i*ld] = A[ds->n+i*ld];
411: for (i=l;i<ds->n;i++) A[ds->n+i*ld] = 0.0;
412: }
413: ds->k = ds->extrarow? n: 0;
414: ds->t = ds->n; /* truncated length equal to previous dimension */
415: ds->n = n;
416: }
417: PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
418: PetscFunctionReturn(PETSC_SUCCESS);
419: }
421: static PetscErrorCode DSCond_NHEP(DS ds,PetscReal *cond)
422: {
423: PetscScalar *work;
424: PetscReal *rwork;
425: PetscBLASInt *ipiv;
426: PetscBLASInt lwork,n,ld;
427: PetscReal hn,hin;
428: PetscScalar *A;
430: PetscFunctionBegin;
431: PetscCall(PetscBLASIntCast(ds->n,&n));
432: PetscCall(PetscBLASIntCast(ds->ld,&ld));
433: lwork = 8*ld;
434: PetscCall(DSAllocateWork_Private(ds,lwork,ld,ld));
435: work = ds->work;
436: rwork = ds->rwork;
437: ipiv = ds->iwork;
439: /* use workspace matrix W to avoid overwriting A */
440: PetscCall(DSAllocateMat_Private(ds,DS_MAT_W));
441: PetscCall(MatCopy(ds->omat[DS_MAT_A],ds->omat[DS_MAT_W],SAME_NONZERO_PATTERN));
442: PetscCall(MatDenseGetArray(ds->omat[DS_MAT_W],&A));
444: /* norm of A */
445: if (ds->state<DS_STATE_INTERMEDIATE) hn = LAPACKlange_("I",&n,&n,A,&ld,rwork);
446: else hn = LAPACKlanhs_("I",&n,A,&ld,rwork);
448: /* norm of inv(A) */
449: PetscCallLAPACKInfo("LAPACKgetrf",LAPACKgetrf_(&n,&n,A,&ld,ipiv,&info));
450: PetscCallLAPACKInfo("LAPACKgetri",LAPACKgetri_(&n,A,&ld,ipiv,work,&lwork,&info));
451: hin = LAPACKlange_("I",&n,&n,A,&ld,rwork);
452: PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_W],&A));
454: *cond = hn*hin;
455: PetscFunctionReturn(PETSC_SUCCESS);
456: }
458: static PetscErrorCode DSTranslateHarmonic_NHEP(DS ds,PetscScalar tau,PetscReal beta,PetscBool recover,PetscScalar *gin,PetscReal *gammaout)
459: {
460: PetscInt i,j;
461: PetscBLASInt *ipiv,n,ld,one=1,ncol;
462: PetscScalar *A,*B,*g=gin,*ghat,done=1.0,dmone=-1.0,dzero=0.0;
463: const PetscScalar *Q;
464: PetscReal gamma=1.0;
466: PetscFunctionBegin;
467: PetscCall(PetscBLASIntCast(ds->n,&n));
468: PetscCall(PetscBLASIntCast(ds->ld,&ld));
469: PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
471: if (!recover) {
473: PetscCall(DSAllocateWork_Private(ds,0,0,ld));
474: ipiv = ds->iwork;
475: if (!g) {
476: PetscCall(DSAllocateWork_Private(ds,ld,0,0));
477: g = ds->work;
478: }
479: /* use workspace matrix W to factor A-tau*eye(n) */
480: PetscCall(DSAllocateMat_Private(ds,DS_MAT_W));
481: PetscCall(MatCopy(ds->omat[DS_MAT_A],ds->omat[DS_MAT_W],SAME_NONZERO_PATTERN));
482: PetscCall(MatDenseGetArray(ds->omat[DS_MAT_W],&B));
484: /* Vector g initially stores b = beta*e_n^T */
485: PetscCall(PetscArrayzero(g,n));
486: g[n-1] = beta;
488: /* g = (A-tau*eye(n))'\b */
489: for (i=0;i<n;i++) B[i+i*ld] -= tau;
490: PetscCallLAPACKInfo("LAPACKgetrf",LAPACKgetrf_(&n,&n,B,&ld,ipiv,&info));
491: PetscCall(PetscLogFlops(2.0*n*n*n/3.0));
492: PetscCallLAPACKInfo("LAPACKgetrs",LAPACKgetrs_("C",&n,&one,B,&ld,ipiv,g,&ld,&info));
493: PetscCall(PetscLogFlops(2.0*n*n-n));
494: PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_W],&B));
496: /* A = A + g*b' */
497: for (i=0;i<n;i++) A[i+(n-1)*ld] += g[i]*beta;
499: } else { /* recover */
501: PetscCall(DSAllocateWork_Private(ds,ld,0,0));
502: ghat = ds->work;
503: PetscCall(MatDenseGetArrayRead(ds->omat[DS_MAT_Q],&Q));
505: /* g^ = -Q(:,idx)'*g */
506: PetscCall(PetscBLASIntCast(ds->l+ds->k,&ncol));
507: PetscCallBLAS("BLASgemv",BLASgemv_("C",&n,&ncol,&dmone,Q,&ld,g,&one,&dzero,ghat,&one));
509: /* A = A + g^*b' */
510: for (i=0;i<ds->l+ds->k;i++)
511: for (j=ds->l;j<ds->l+ds->k;j++)
512: A[i+j*ld] += ghat[i]*Q[n-1+j*ld]*beta;
514: /* g~ = (I-Q(:,idx)*Q(:,idx)')*g = g+Q(:,idx)*g^ */
515: PetscCallBLAS("BLASgemv",BLASgemv_("N",&n,&ncol,&done,Q,&ld,ghat,&one,&done,g,&one));
516: PetscCall(MatDenseRestoreArrayRead(ds->omat[DS_MAT_Q],&Q));
517: }
519: /* Compute gamma factor */
520: if (gammaout || (recover && ds->extrarow)) gamma = SlepcAbs(1.0,BLASnrm2_(&n,g,&one));
521: if (gammaout) *gammaout = gamma;
522: if (recover && ds->extrarow) {
523: for (j=ds->l;j<ds->l+ds->k;j++) A[ds->n+j*ld] *= gamma;
524: }
525: PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
526: PetscFunctionReturn(PETSC_SUCCESS);
527: }
529: static PetscErrorCode DSReallocate_NHEP(DS ds,PetscInt ld)
530: {
531: PetscInt i,*perm=ds->perm;
533: PetscFunctionBegin;
534: for (i=0;i<DS_NUM_MAT;i++) {
535: if (i!=DS_MAT_A && i!=DS_MAT_Q) PetscCall(MatDestroy(&ds->omat[i]));
536: }
538: PetscCall(DSReallocateMat_Private(ds,DS_MAT_A,ld));
539: PetscCall(DSReallocateMat_Private(ds,DS_MAT_Q,ld));
541: PetscCall(PetscMalloc1(ld,&ds->perm));
542: PetscCall(PetscArraycpy(ds->perm,perm,ds->ld));
543: PetscCall(PetscFree(perm));
544: PetscFunctionReturn(PETSC_SUCCESS);
545: }
547: /*MC
548: DSNHEP - Dense Non-Hermitian Eigenvalue Problem.
550: Notes:
551: The problem is expressed as $AX = X\Lambda$, where $A$ is the input matrix.
552: $\Lambda$ is a diagonal matrix whose diagonal elements are the arguments of
553: `DSSolve()`. After solve, $A$ is overwritten with the upper quasi-triangular
554: matrix $T$ of the (real) Schur form, $AQ = QT$.
556: In the intermediate state $A$ is reduced to upper Hessenberg form.
558: Computation of left eigenvectors is supported, but two-sided Krylov solvers
559: usually rely on the related `DSNHEPTS`.
561: Used DS matrices:
562: + `DS_MAT_A` - problem matrix
563: - `DS_MAT_Q` - orthogonal/unitary transformation that reduces to Hessenberg form
564: (intermediate step) or matrix of orthogonal Schur vectors
566: Implemented methods:
567: . 0 - Implicit QR (`_hseqr`)
569: Level: beginner
571: .seealso: [](sec:ds), `DSCreate()`, `DSSetType()`, `DSType`
572: M*/
573: SLEPC_EXTERN PetscErrorCode DSCreate_NHEP(DS ds)
574: {
575: PetscFunctionBegin;
576: ds->ops->allocate = DSAllocate_NHEP;
577: ds->ops->view = DSView_NHEP;
578: ds->ops->vectors = DSVectors_NHEP;
579: ds->ops->solve[0] = DSSolve_NHEP;
580: ds->ops->sort = DSSort_NHEP;
581: ds->ops->sortperm = DSSortWithPermutation_NHEP;
582: #if !PetscDefined(HAVE_MPIUNI)
583: ds->ops->synchronize = DSSynchronize_NHEP;
584: #endif
585: ds->ops->gettruncatesize = DSGetTruncateSize_Default;
586: ds->ops->truncate = DSTruncate_NHEP;
587: ds->ops->update = DSUpdateExtraRow_NHEP;
588: ds->ops->cond = DSCond_NHEP;
589: ds->ops->transharm = DSTranslateHarmonic_NHEP;
590: ds->ops->reallocate = DSReallocate_NHEP;
591: PetscFunctionReturn(PETSC_SUCCESS);
592: }