Actual source code: dsgnhep.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: /*
 15:   1) Patterns of A and B
 16:       DS_STATE_RAW:       DS_STATE_INTERM/CONDENSED
 17:        0       n-1              0       n-1
 18:       -------------            -------------
 19:     0 |* * * * * *|          0 |* * * * * *|
 20:       |* * * * * *|            |  * * * * *|
 21:       |* * * * * *|            |    * * * *|
 22:       |* * * * * *|            |    * * * *|
 23:       |* * * * * *|            |        * *|
 24:   n-1 |* * * * * *|        n-1 |          *|
 25:       -------------            -------------

 27:   2) Moreover, P and Q are assumed to be the identity in DS_STATE_INTERMEDIATE.
 28: */

 30: static PetscErrorCode CleanDenseSchur(PetscInt n,PetscInt k,PetscScalar *S,PetscInt ldS,PetscScalar *T,PetscInt ldT,PetscScalar *X,PetscInt ldX,PetscScalar *Y,PetscInt ldY);

 32: static PetscErrorCode DSAllocate_GNHEP(DS ds,PetscInt ld)
 33: {
 34:   PetscFunctionBegin;
 35:   PetscCall(DSAllocateMat_Private(ds,DS_MAT_A));
 36:   PetscCall(DSAllocateMat_Private(ds,DS_MAT_B));
 37:   PetscCall(DSAllocateMat_Private(ds,DS_MAT_Z));
 38:   PetscCall(DSAllocateMat_Private(ds,DS_MAT_Q));
 39:   PetscCall(PetscFree(ds->perm));
 40:   PetscCall(PetscMalloc1(ld,&ds->perm));
 41:   PetscFunctionReturn(PETSC_SUCCESS);
 42: }

 44: static PetscErrorCode DSView_GNHEP(DS ds,PetscViewer viewer)
 45: {
 46:   PetscViewerFormat format;

 48:   PetscFunctionBegin;
 49:   PetscCall(PetscViewerGetFormat(viewer,&format));
 50:   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS);
 51:   PetscCall(DSViewMat(ds,viewer,DS_MAT_A));
 52:   PetscCall(DSViewMat(ds,viewer,DS_MAT_B));
 53:   if (ds->state>DS_STATE_INTERMEDIATE) {
 54:     PetscCall(DSViewMat(ds,viewer,DS_MAT_Z));
 55:     PetscCall(DSViewMat(ds,viewer,DS_MAT_Q));
 56:   }
 57:   if (ds->omat[DS_MAT_X]) PetscCall(DSViewMat(ds,viewer,DS_MAT_X));
 58:   if (ds->omat[DS_MAT_Y]) PetscCall(DSViewMat(ds,viewer,DS_MAT_Y));
 59:   PetscFunctionReturn(PETSC_SUCCESS);
 60: }

 62: static PetscErrorCode DSVectors_GNHEP_Eigen_Some(DS ds,PetscInt *k,PetscReal *rnorm,PetscBool left)
 63: {
 64:   PetscInt       i;
 65:   PetscBLASInt   n,ld,mout,*select,mm,inc=1,cols=1,zero=0;
 66:   PetscScalar    *X,*Y,*XY,*Z,*Q,*A,*B,fone=1.0,fzero=0.0;
 67:   PetscReal      norm,done=1.0;
 68:   PetscBool      iscomplex = PETSC_FALSE;
 69:   const char     *side;

 71:   PetscFunctionBegin;
 72:   PetscCall(PetscBLASIntCast(ds->n,&n));
 73:   PetscCall(PetscBLASIntCast(ds->ld,&ld));
 74:   if (left) {
 75:     X = NULL;
 76:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Y],&Y));
 77:     side = "L";
 78:   } else {
 79:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_X],&X));
 80:     Y = NULL;
 81:     side = "R";
 82:   }
 83:   XY = left? Y: X;
 84:   PetscCall(DSAllocateWork_Private(ds,0,0,ld));
 85:   select = ds->iwork;
 86:   for (i=0;i<n;i++) select[i] = (PetscBLASInt)PETSC_FALSE;
 87:   if (ds->state <= DS_STATE_INTERMEDIATE) {
 88:     PetscCall(DSSetIdentity(ds,DS_MAT_Q));
 89:     PetscCall(DSSetIdentity(ds,DS_MAT_Z));
 90:   }
 91:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
 92:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_B],&B));
 93:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Q],&Q));
 94:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Z],&Z));
 95:   PetscCall(CleanDenseSchur(n,0,A,ld,B,ld,Q,ld,Z,ld));
 96:   if (ds->state < DS_STATE_CONDENSED) PetscCall(DSSetState(ds,DS_STATE_CONDENSED));

 98:   /* compute k-th eigenvector */
 99:   select[*k] = (PetscBLASInt)PETSC_TRUE;
100: #if PetscDefined(USE_COMPLEX)
101:   mm = 1;
102:   PetscCall(DSAllocateWork_Private(ds,2*ld,2*ld,0));
103:   PetscCallLAPACKInfo("LAPACKtgevc",LAPACKtgevc_(side,"S",select,&n,A,&ld,B,&ld,PetscSafePointerPlusOffset(Y,(*k)*ld),&ld,PetscSafePointerPlusOffset(X,(*k)*ld),&ld,&mm,&mout,ds->work,ds->rwork,&info));
104: #else
105:   if ((*k)<n-1 && (A[ld*(*k)+(*k)+1] != 0.0 || B[ld*(*k)+(*k)+1] != 0.0)) iscomplex = PETSC_TRUE;
106:   mm = iscomplex? 2: 1;
107:   if (iscomplex) select[(*k)+1] = (PetscBLASInt)PETSC_TRUE;
108:   PetscCall(DSAllocateWork_Private(ds,6*ld,0,0));
109:   PetscCallLAPACKInfo("LAPACKtgevc",LAPACKtgevc_(side,"S",select,&n,A,&ld,B,&ld,PetscSafePointerPlusOffset(Y,(*k)*ld),&ld,PetscSafePointerPlusOffset(X,(*k)*ld),&ld,&mm,&mout,ds->work,&info));
110: #endif
111:   PetscCheck(select[*k] && mout==mm,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Wrong arguments in call to Lapack xTGEVC");
112:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
113:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_B],&B));

115:   /* accumulate and normalize eigenvectors */
116:   PetscCall(PetscArraycpy(ds->work,XY+(*k)*ld,mm*ld));
117:   PetscCallBLAS("BLASgemm",BLASgemm_("N","N",&n,&mm,&n,&fone,left?Z:Q,&ld,ds->work,&ld,&fzero,XY+(*k)*ld,&ld));
118:   norm = BLASnrm2_(&n,XY+(*k)*ld,&inc);
119: #if !PetscDefined(USE_COMPLEX)
120:   if (iscomplex) {
121:     norm = SlepcAbsEigenvalue(norm,BLASnrm2_(&n,XY+(*k+1)*ld,&inc));
122:     cols = 2;
123:   }
124: #endif
125:   PetscCallLAPACKInfo("LAPACKlascl",LAPACKlascl_("G",&zero,&zero,&norm,&done,&n,&cols,XY+(*k)*ld,&ld,&info));
126:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Q],&Q));
127:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Z],&Z));

129:   /* set output arguments */
130:   if (rnorm) {
131:     if (iscomplex) *rnorm = SlepcAbsEigenvalue(XY[n-1+(*k)*ld],XY[n-1+(*k+1)*ld]);
132:     else *rnorm = PetscAbsScalar(XY[n-1+(*k)*ld]);
133:   }
134:   if (iscomplex) (*k)++;
135:   PetscCall(MatDenseRestoreArray(ds->omat[left?DS_MAT_Y:DS_MAT_X],&XY));
136:   PetscFunctionReturn(PETSC_SUCCESS);
137: }

139: static PetscErrorCode DSVectors_GNHEP_Eigen_All(DS ds,PetscBool left)
140: {
141:   PetscInt       i;
142:   PetscBLASInt   n,ld,mout,inc = 1;
143:   PetscBool      iscomplex;
144:   PetscScalar    *X,*Y,*XY,*Q,*Z,*A,*B,tmp;
145:   PetscReal      norm;
146:   const char     *side,*back;

148:   PetscFunctionBegin;
149:   PetscCall(PetscBLASIntCast(ds->n,&n));
150:   PetscCall(PetscBLASIntCast(ds->ld,&ld));
151:   if (left) {
152:     X = NULL;
153:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Y],&Y));
154:     side = "L";
155:   } else {
156:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_X],&X));
157:     Y = NULL;
158:     side = "R";
159:   }
160:   XY = left? Y: X;
161:   if (ds->state <= DS_STATE_INTERMEDIATE) {
162:     PetscCall(DSSetIdentity(ds,DS_MAT_Q));
163:     PetscCall(DSSetIdentity(ds,DS_MAT_Z));
164:   }
165:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
166:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_B],&B));
167:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Q],&Q));
168:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Z],&Z));
169:   PetscCall(CleanDenseSchur(n,0,A,ld,B,ld,Q,ld,Z,ld));
170:   if (ds->state>=DS_STATE_CONDENSED) {
171:     /* DSSolve() has been called, backtransform with matrix Q */
172:     back = "B";
173:     PetscCall(PetscArraycpy(left?Y:X,left?Z:Q,ld*ld));
174:   } else {
175:     back = "A";
176:     PetscCall(DSSetState(ds,DS_STATE_CONDENSED));
177:   }
178: #if PetscDefined(USE_COMPLEX)
179:   PetscCall(DSAllocateWork_Private(ds,2*ld,2*ld,0));
180:   PetscCallLAPACKInfo("LAPACKtgevc",LAPACKtgevc_(side,back,NULL,&n,A,&ld,B,&ld,Y,&ld,X,&ld,&n,&mout,ds->work,ds->rwork,&info));
181: #else
182:   PetscCall(DSAllocateWork_Private(ds,6*ld,0,0));
183:   PetscCallLAPACKInfo("LAPACKtgevc",LAPACKtgevc_(side,back,NULL,&n,A,&ld,B,&ld,Y,&ld,X,&ld,&n,&mout,ds->work,&info));
184: #endif
185:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Q],&Q));
186:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Z],&Z));

188:   /* normalize eigenvectors */
189:   for (i=0;i<n;i++) {
190:     iscomplex = (i<n-1 && (A[i+1+i*ld]!=0.0 || B[i+1+i*ld]!=0.0))? PETSC_TRUE: PETSC_FALSE;
191:     norm = BLASnrm2_(&n,XY+i*ld,&inc);
192: #if !PetscDefined(USE_COMPLEX)
193:     if (iscomplex) {
194:       tmp = BLASnrm2_(&n,XY+(i+1)*ld,&inc);
195:       norm = SlepcAbsEigenvalue(norm,tmp);
196:     }
197: #endif
198:     tmp = 1.0 / norm;
199:     PetscCallBLAS("BLASscal",BLASscal_(&n,&tmp,XY+i*ld,&inc));
200: #if !PetscDefined(USE_COMPLEX)
201:     if (iscomplex) PetscCallBLAS("BLASscal",BLASscal_(&n,&tmp,XY+(i+1)*ld,&inc));
202: #endif
203:     if (iscomplex) i++;
204:   }
205:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
206:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_B],&B));
207:   PetscCall(MatDenseRestoreArray(ds->omat[left?DS_MAT_Y:DS_MAT_X],&XY));
208:   PetscFunctionReturn(PETSC_SUCCESS);
209: }

211: static PetscErrorCode DSVectors_GNHEP(DS ds,DSMatType mat,PetscInt *k,PetscReal *rnorm)
212: {
213:   PetscFunctionBegin;
214:   switch (mat) {
215:     case DS_MAT_X:
216:     case DS_MAT_Y:
217:       if (k) PetscCall(DSVectors_GNHEP_Eigen_Some(ds,k,rnorm,mat == DS_MAT_Y?PETSC_TRUE:PETSC_FALSE));
218:       else PetscCall(DSVectors_GNHEP_Eigen_All(ds,mat == DS_MAT_Y?PETSC_TRUE:PETSC_FALSE));
219:       break;
220:     default:
221:       SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_OUTOFRANGE,"Invalid mat parameter");
222:   }
223:   PetscFunctionReturn(PETSC_SUCCESS);
224: }

226: static PetscErrorCode DSSort_GNHEP_Arbitrary(DS ds,PetscScalar *wr,PetscScalar *wi,PetscScalar *rr,PetscScalar *ri,PetscInt *k)
227: {
228:   PetscInt       i;
229:   PetscBLASInt   n,ld,mout,lwork,liwork,*iwork,*selection,zero_=0,true_=1;
230:   PetscScalar    *S,*T,*Q,*Z,*work,*beta;

232:   PetscFunctionBegin;
233:   if (!ds->sc) PetscFunctionReturn(PETSC_SUCCESS);
234:   PetscCall(PetscBLASIntCast(ds->n,&n));
235:   PetscCall(PetscBLASIntCast(ds->ld,&ld));
236: #if !PetscDefined(USE_COMPLEX)
237:   lwork = 4*n+16;
238: #else
239:   lwork = 1;
240: #endif
241:   liwork = 1;
242:   PetscCall(DSAllocateWork_Private(ds,lwork+2*n,0,liwork+n));
243:   beta      = ds->work;
244:   work      = ds->work + n;
245:   PetscCall(PetscBLASIntCast(ds->lwork-n,&lwork));
246:   selection = ds->iwork;
247:   iwork     = ds->iwork + n;
248:   PetscCall(PetscBLASIntCast(ds->liwork-n,&liwork));
249:   /* Compute the selected eigenvalue to be in the leading position */
250:   PetscCall(DSSortEigenvalues_Private(ds,rr,ri,ds->perm,PETSC_FALSE));
251:   PetscCall(PetscArrayzero(selection,n));
252:   for (i=0; i<*k; i++) selection[ds->perm[i]] = 1;
253:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&S));
254:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_B],&T));
255:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Q],&Q));
256:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Z],&Z));
257: #if !PetscDefined(USE_COMPLEX)
258:   PetscCallLAPACKInfo("LAPACKtgsen",LAPACKtgsen_(&zero_,&true_,&true_,selection,&n,S,&ld,T,&ld,wr,wi,beta,Z,&ld,Q,&ld,&mout,NULL,NULL,NULL,work,&lwork,iwork,&liwork,&info));
259: #else
260:   PetscCallLAPACKInfo("LAPACKtgsen",LAPACKtgsen_(&zero_,&true_,&true_,selection,&n,S,&ld,T,&ld,wr,beta,Z,&ld,Q,&ld,&mout,NULL,NULL,NULL,work,&lwork,iwork,&liwork,&info));
261: #endif
262:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&S));
263:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_B],&T));
264:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Q],&Q));
265:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Z],&Z));
266:   *k = mout;
267:   for (i=0;i<n;i++) {
268:     if (beta[i]==0.0) wr[i] = (PetscRealPart(wr[i])>0.0)? PETSC_MAX_REAL: PETSC_MIN_REAL;
269:     else wr[i] /= beta[i];
270: #if !PetscDefined(USE_COMPLEX)
271:     if (beta[i]==0.0) wi[i] = (wi[i]>0.0)? PETSC_MAX_REAL: PETSC_MIN_REAL;
272:     else wi[i] /= beta[i];
273: #endif
274:   }
275:   PetscFunctionReturn(PETSC_SUCCESS);
276: }

278: static PetscErrorCode DSSort_GNHEP_Total(DS ds,PetscScalar *wr,PetscScalar *wi)
279: {
280:   PetscScalar    re;
281:   PetscInt       i,j,pos,result;
282:   PetscBLASInt   ifst,ilst,n,ld,one=1;
283:   PetscScalar    *S,*T,*Z,*Q;
284: #if !PetscDefined(USE_COMPLEX)
285:   PetscBLASInt   lwork;
286:   PetscScalar    *work,a,safmin,scale1,scale2,im;
287: #endif

289:   PetscFunctionBegin;
290:   if (!ds->sc) PetscFunctionReturn(PETSC_SUCCESS);
291:   PetscCall(PetscBLASIntCast(ds->n,&n));
292:   PetscCall(PetscBLASIntCast(ds->ld,&ld));
293:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&S));
294:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_B],&T));
295:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Q],&Q));
296:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Z],&Z));
297: #if !PetscDefined(USE_COMPLEX)
298:   lwork = -1;
299:   PetscCallLAPACKInfo("LAPACKtgexc",LAPACKtgexc_(&one,&one,&ld,NULL,&ld,NULL,&ld,NULL,&ld,NULL,&ld,&one,&one,&a,&lwork,&info));
300:   safmin = LAPACKlamch_("S");
301:   PetscCall(PetscBLASIntCast((PetscInt)a,&lwork));
302:   PetscCall(DSAllocateWork_Private(ds,lwork,0,0));
303:   work = ds->work;
304: #endif
305:   /* selection sort */
306:   for (i=ds->l;i<n-1;i++) {
307:     re = wr[i];
308: #if !PetscDefined(USE_COMPLEX)
309:     im = wi[i];
310: #endif
311:     pos = 0;
312:     j = i+1; /* j points to the next eigenvalue */
313: #if !PetscDefined(USE_COMPLEX)
314:     if (im != 0) j=i+2;
315: #endif
316:     /* find minimum eigenvalue */
317:     for (;j<n;j++) {
318: #if !PetscDefined(USE_COMPLEX)
319:       PetscCall(SlepcSCCompare(ds->sc,re,im,wr[j],wi[j],&result));
320: #else
321:       PetscCall(SlepcSCCompare(ds->sc,re,0.0,wr[j],0.0,&result));
322: #endif
323:       if (result > 0) {
324:         re = wr[j];
325: #if !PetscDefined(USE_COMPLEX)
326:         im = wi[j];
327: #endif
328:         pos = j;
329:       }
330: #if !PetscDefined(USE_COMPLEX)
331:       if (wi[j] != 0) j++;
332: #endif
333:     }
334:     if (pos) {
335:       /* interchange blocks */
336:       PetscCall(PetscBLASIntCast(pos+1,&ifst));
337:       PetscCall(PetscBLASIntCast(i+1,&ilst));
338: #if !PetscDefined(USE_COMPLEX)
339:       PetscCallLAPACKInfo("LAPACKtgexc",LAPACKtgexc_(&one,&one,&n,S,&ld,T,&ld,Z,&ld,Q,&ld,&ifst,&ilst,work,&lwork,&info));
340: #else
341:       PetscCallLAPACKInfo("LAPACKtgexc",LAPACKtgexc_(&one,&one,&n,S,&ld,T,&ld,Z,&ld,Q,&ld,&ifst,&ilst,&info));
342: #endif
343:       /* recover original eigenvalues from T and S matrices */
344:       for (j=i;j<n;j++) {
345: #if !PetscDefined(USE_COMPLEX)
346:         if (j<n-1 && S[j*ld+j+1] != 0.0) {
347:           /* complex conjugate eigenvalue */
348:           PetscCallBLAS("LAPACKlag2",LAPACKlag2_(S+j*ld+j,&ld,T+j*ld+j,&ld,&safmin,&scale1,&scale2,&re,&a,&im));
349:           wr[j] = re / scale1;
350:           wi[j] = im / scale1;
351:           wr[j+1] = a / scale2;
352:           wi[j+1] = -wi[j];
353:           j++;
354:         } else
355: #endif
356:         {
357:           if (T[j*ld+j] == 0.0) wr[j] = (PetscRealPart(S[j*ld+j])>0.0)? PETSC_MAX_REAL: PETSC_MIN_REAL;
358:           else wr[j] = S[j*ld+j] / T[j*ld+j];
359: #if !PetscDefined(USE_COMPLEX)
360:           wi[j] = 0.0;
361: #endif
362:         }
363:       }
364:     }
365: #if !PetscDefined(USE_COMPLEX)
366:     if (wi[i] != 0.0) i++;
367: #endif
368:   }
369:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&S));
370:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_B],&T));
371:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Q],&Q));
372:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Z],&Z));
373:   PetscFunctionReturn(PETSC_SUCCESS);
374: }

376: static PetscErrorCode DSSort_GNHEP(DS ds,PetscScalar *wr,PetscScalar *wi,PetscScalar *rr,PetscScalar *ri,PetscInt *k)
377: {
378:   PetscFunctionBegin;
379:   if (!rr || wr == rr) PetscCall(DSSort_GNHEP_Total(ds,wr,wi));
380:   else PetscCall(DSSort_GNHEP_Arbitrary(ds,wr,wi,rr,ri,k));
381:   PetscFunctionReturn(PETSC_SUCCESS);
382: }

384: static PetscErrorCode DSUpdateExtraRow_GNHEP(DS ds)
385: {
386:   PetscInt          i;
387:   PetscBLASInt      n,ld,incx=1;
388:   PetscScalar       *A,*B,*x,*y,one=1.0,zero=0.0;
389:   const PetscScalar *Q;

391:   PetscFunctionBegin;
392:   PetscCall(PetscBLASIntCast(ds->n,&n));
393:   PetscCall(PetscBLASIntCast(ds->ld,&ld));
394:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
395:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_B],&B));
396:   PetscCall(MatDenseGetArrayRead(ds->omat[DS_MAT_Q],&Q));
397:   PetscCall(DSAllocateWork_Private(ds,2*ld,0,0));
398:   x = ds->work;
399:   y = ds->work+ld;
400:   for (i=0;i<n;i++) x[i] = PetscConj(A[n+i*ld]);
401:   PetscCallBLAS("BLASgemv",BLASgemv_("C",&n,&n,&one,Q,&ld,x,&incx,&zero,y,&incx));
402:   for (i=0;i<n;i++) A[n+i*ld] = PetscConj(y[i]);
403:   for (i=0;i<n;i++) x[i] = PetscConj(B[n+i*ld]);
404:   PetscCallBLAS("BLASgemv",BLASgemv_("C",&n,&n,&one,Q,&ld,x,&incx,&zero,y,&incx));
405:   for (i=0;i<n;i++) B[n+i*ld] = PetscConj(y[i]);
406:   ds->k = n;
407:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
408:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_B],&B));
409:   PetscCall(MatDenseRestoreArrayRead(ds->omat[DS_MAT_Q],&Q));
410:   PetscFunctionReturn(PETSC_SUCCESS);
411: }

413: /*
414:    Write zeros from the column k to n in the lower triangular part of the
415:    matrices S and T, and inside 2-by-2 diagonal blocks of T in order to
416:    make (S,T) a valid Schur decompositon.
417: */
418: static PetscErrorCode CleanDenseSchur(PetscInt n,PetscInt k,PetscScalar *S,PetscInt ldS,PetscScalar *T,PetscInt ldT,PetscScalar *X,PetscInt ldX,PetscScalar *Y,PetscInt ldY)
419: {
420:   PetscInt       i;
421: #if PetscDefined(USE_COMPLEX)
422:   PetscInt       j;
423:   PetscScalar    s;
424: #else
425:   PetscBLASInt   ldS_,ldT_,n_i,n_i_2,one=1,n_,i_2,i_;
426:   PetscScalar    b11,b22,sr,cr,sl,cl;
427: #endif

429:   PetscFunctionBegin;
430: #if PetscDefined(USE_COMPLEX)
431:   for (i=k; i<n; i++) {
432:     /* Some functions need the diagonal elements in T be real */
433:     if (T && PetscImaginaryPart(T[ldT*i+i]) != 0.0) {
434:       s = PetscConj(T[ldT*i+i])/PetscAbsScalar(T[ldT*i+i]);
435:       for (j=0;j<=i;j++) {
436:         T[ldT*i+j] *= s;
437:         S[ldS*i+j] *= s;
438:       }
439:       T[ldT*i+i] = PetscRealPart(T[ldT*i+i]);
440:       if (X) for (j=0;j<n;j++) X[ldX*i+j] *= s;
441:     }
442:     j = i+1;
443:     if (j<n) {
444:       S[ldS*i+j] = 0.0;
445:       if (T) T[ldT*i+j] = 0.0;
446:     }
447:   }
448: #else
449:   PetscCall(PetscBLASIntCast(ldS,&ldS_));
450:   PetscCall(PetscBLASIntCast(ldT,&ldT_));
451:   PetscCall(PetscBLASIntCast(n,&n_));
452:   for (i=k;i<n-1;i++) {
453:     if (S[ldS*i+i+1] != 0.0) {
454:       /* Check if T(i+1,i) and T(i,i+1) are zero */
455:       if (T[ldT*(i+1)+i] != 0.0 || T[ldT*i+i+1] != 0.0) {
456:         /* Check if T(i+1,i) and T(i,i+1) are negligible */
457:         if (PetscAbs(T[ldT*(i+1)+i])+PetscAbs(T[ldT*i+i+1]) < (PetscAbs(T[ldT*i+i])+PetscAbs(T[ldT*(i+1)+i+1]))*PETSC_MACHINE_EPSILON) {
458:           T[ldT*i+i+1] = 0.0;
459:           T[ldT*(i+1)+i] = 0.0;
460:         } else {
461:           /* If one of T(i+1,i) or T(i,i+1) is negligible, we make zero the other element */
462:           if (PetscAbs(T[ldT*i+i+1]) < (PetscAbs(T[ldT*i+i])+PetscAbs(T[ldT*(i+1)+i+1])+PetscAbs(T[ldT*(i+1)+i]))*PETSC_MACHINE_EPSILON) {
463:             PetscCallBLAS("LAPACKlasv2",LAPACKlasv2_(&T[ldT*i+i],&T[ldT*(i+1)+i],&T[ldT*(i+1)+i+1],&b22,&b11,&sl,&cl,&sr,&cr));
464:           } else if (PetscAbs(T[ldT*(i+1)+i]) < (PetscAbs(T[ldT*i+i])+PetscAbs(T[ldT*(i+1)+i+1])+PetscAbs(T[ldT*i+i+1]))*PETSC_MACHINE_EPSILON) {
465:             PetscCallBLAS("LAPACKlasv2",LAPACKlasv2_(&T[ldT*i+i],&T[ldT*i+i+1],&T[ldT*(i+1)+i+1],&b22,&b11,&sr,&cr,&sl,&cl));
466:           } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported format. Call DSSolve before this function");
467:           PetscCall(PetscBLASIntCast(n-i,&n_i));
468:           n_i_2 = n_i - 2;
469:           PetscCall(PetscBLASIntCast(i+2,&i_2));
470:           PetscCall(PetscBLASIntCast(i,&i_));
471:           if (b11 < 0.0) {
472:             cr = -cr; sr = -sr;
473:             b11 = -b11; b22 = -b22;
474:           }
475:           PetscCallBLAS("BLASrot",BLASrot_(&n_i,&S[ldS*i+i],&ldS_,&S[ldS*i+i+1],&ldS_,&cl,&sl));
476:           PetscCallBLAS("BLASrot",BLASrot_(&i_2,&S[ldS*i],&one,&S[ldS*(i+1)],&one,&cr,&sr));
477:           PetscCallBLAS("BLASrot",BLASrot_(&n_i_2,&T[ldT*(i+2)+i],&ldT_,&T[ldT*(i+2)+i+1],&ldT_,&cl,&sl));
478:           PetscCallBLAS("BLASrot",BLASrot_(&i_,&T[ldT*i],&one,&T[ldT*(i+1)],&one,&cr,&sr));
479:           if (X) PetscCallBLAS("BLASrot",BLASrot_(&n_,&X[ldX*i],&one,&X[ldX*(i+1)],&one,&cr,&sr));
480:           if (Y) PetscCallBLAS("BLASrot",BLASrot_(&n_,&Y[ldY*i],&one,&Y[ldY*(i+1)],&one,&cl,&sl));
481:           T[ldT*i+i] = b11; T[ldT*i+i+1] = 0.0;
482:           T[ldT*(i+1)+i] = 0.0; T[ldT*(i+1)+i+1] = b22;
483:         }
484:       }
485:       i++;
486:     }
487:   }
488: #endif
489:   PetscFunctionReturn(PETSC_SUCCESS);
490: }

492: static PetscErrorCode DSSolve_GNHEP(DS ds,PetscScalar *wr,PetscScalar *wi)
493: {
494:   PetscScalar    *work,*beta,a;
495:   PetscInt       i;
496:   PetscBLASInt   lwork,n,ld,iaux;
497:   PetscScalar    *A,*B,*Z,*Q;
498:   PetscBool      usegges3=(ds->method==1)?PETSC_TRUE:PETSC_FALSE;

500:   PetscFunctionBegin;
501: #if !PetscDefined(USE_COMPLEX)
502:   PetscAssertPointer(wi,3);
503: #endif
504:   PetscCall(PetscBLASIntCast(ds->n,&n));
505:   PetscCall(PetscBLASIntCast(ds->ld,&ld));
506:   lwork = -1;
507:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
508:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_B],&B));
509:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Q],&Q));
510:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Z],&Z));
511: #if !PetscDefined(USE_COMPLEX)
512:   if (usegges3) PetscCallLAPACKInfo("LAPACKgges3",LAPACKgges3_("V","V","N",NULL,&n,A,&ld,B,&ld,&iaux,wr,wi,NULL,Z,&ld,Q,&ld,&a,&lwork,NULL,&info));
513:   else PetscCallLAPACKInfo("LAPACKgges",LAPACKgges_("V","V","N",NULL,&n,A,&ld,B,&ld,&iaux,wr,wi,NULL,Z,&ld,Q,&ld,&a,&lwork,NULL,&info));
514:   PetscCall(PetscBLASIntCast((PetscInt)a,&lwork));
515:   PetscCall(DSAllocateWork_Private(ds,lwork+ld,0,0));
516:   beta = ds->work;
517:   work = beta+ds->n;
518:   PetscCall(PetscBLASIntCast(ds->lwork-ds->n,&lwork));
519:   if (usegges3) PetscCallLAPACKInfo("LAPACKgges3",LAPACKgges3_("V","V","N",NULL,&n,A,&ld,B,&ld,&iaux,wr,wi,beta,Z,&ld,Q,&ld,work,&lwork,NULL,&info));
520:   else PetscCallLAPACKInfo("LAPACKgges",LAPACKgges_("V","V","N",NULL,&n,A,&ld,B,&ld,&iaux,wr,wi,beta,Z,&ld,Q,&ld,work,&lwork,NULL,&info));
521: #else
522:   if (usegges3) PetscCallLAPACKInfo("LAPACKgges3",LAPACKgges3_("V","V","N",NULL,&n,A,&ld,B,&ld,&iaux,wr,NULL,Z,&ld,Q,&ld,&a,&lwork,NULL,NULL,&info));
523:   else PetscCallLAPACKInfo("LAPACKgges",LAPACKgges_("V","V","N",NULL,&n,A,&ld,B,&ld,&iaux,wr,NULL,Z,&ld,Q,&ld,&a,&lwork,NULL,NULL,&info));
524:   PetscCall(PetscBLASIntCast((PetscInt)PetscRealPart(a),&lwork));
525:   PetscCall(DSAllocateWork_Private(ds,lwork+ld,8*ld,0));
526:   beta = ds->work;
527:   work = beta+ds->n;
528:   PetscCall(PetscBLASIntCast(ds->lwork-ds->n,&lwork));
529:   if (usegges3) PetscCallLAPACKInfo("LAPACKgges3",LAPACKgges3_("V","V","N",NULL,&n,A,&ld,B,&ld,&iaux,wr,beta,Z,&ld,Q,&ld,work,&lwork,ds->rwork,NULL,&info));
530:   else PetscCallLAPACKInfo("LAPACKgges",LAPACKgges_("V","V","N",NULL,&n,A,&ld,B,&ld,&iaux,wr,beta,Z,&ld,Q,&ld,work,&lwork,ds->rwork,NULL,&info));
531: #endif
532:   for (i=0;i<n;i++) {
533:     if (beta[i]==0.0) wr[i] = (PetscRealPart(wr[i])>0.0)? PETSC_MAX_REAL: PETSC_MIN_REAL;
534:     else wr[i] /= beta[i];
535: #if !PetscDefined(USE_COMPLEX)
536:     if (beta[i]==0.0) wi[i] = (wi[i]>0.0)? PETSC_MAX_REAL: PETSC_MIN_REAL;
537:     else wi[i] /= beta[i];
538: #else
539:     if (wi) wi[i] = 0.0;
540: #endif
541:   }
542:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
543:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_B],&B));
544:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Q],&Q));
545:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Z],&Z));
546:   PetscFunctionReturn(PETSC_SUCCESS);
547: }

549: #if !PetscDefined(HAVE_MPIUNI)
550: static PetscErrorCode DSSynchronize_GNHEP(DS ds,PetscScalar eigr[],PetscScalar eigi[])
551: {
552:   PetscInt       ld=ds->ld,l=ds->l,k;
553:   PetscMPIInt    n,rank,off=0,size,ldn;
554:   PetscScalar    *A,*B,*Q,*Z;

556:   PetscFunctionBegin;
557:   k = 2*(ds->n-l)*ld;
558:   if (ds->state>DS_STATE_RAW) k += 2*(ds->n-l)*ld;
559:   if (eigr) k += (ds->n-l);
560:   if (eigi) k += (ds->n-l);
561:   PetscCall(DSAllocateWork_Private(ds,k,0,0));
562:   PetscCall(PetscMPIIntCast(k*sizeof(PetscScalar),&size));
563:   PetscCall(PetscMPIIntCast(ds->n-l,&n));
564:   PetscCall(PetscMPIIntCast(ld*(ds->n-l),&ldn));
565:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
566:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_B],&B));
567:   if (ds->state>DS_STATE_RAW) {
568:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Q],&Q));
569:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_Z],&Z));
570:   }
571:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ds),&rank));
572:   if (!rank) {
573:     PetscCallMPI(MPI_Pack(A+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
574:     PetscCallMPI(MPI_Pack(B+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
575:     if (ds->state>DS_STATE_RAW) {
576:       PetscCallMPI(MPI_Pack(Q+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
577:       PetscCallMPI(MPI_Pack(Z+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
578:     }
579:     if (eigr) PetscCallMPI(MPI_Pack(eigr+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
580: #if !PetscDefined(USE_COMPLEX)
581:     if (eigi) PetscCallMPI(MPI_Pack(eigi+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
582: #endif
583:   }
584:   PetscCallMPI(MPI_Bcast(ds->work,size,MPI_BYTE,0,PetscObjectComm((PetscObject)ds)));
585:   if (rank) {
586:     PetscCallMPI(MPI_Unpack(ds->work,size,&off,A+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
587:     PetscCallMPI(MPI_Unpack(ds->work,size,&off,B+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
588:     if (ds->state>DS_STATE_RAW) {
589:       PetscCallMPI(MPI_Unpack(ds->work,size,&off,Q+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
590:       PetscCallMPI(MPI_Unpack(ds->work,size,&off,Z+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
591:     }
592:     if (eigr) PetscCallMPI(MPI_Unpack(ds->work,size,&off,eigr+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
593: #if !PetscDefined(USE_COMPLEX)
594:     if (eigi) PetscCallMPI(MPI_Unpack(ds->work,size,&off,eigi+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
595: #endif
596:   }
597:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
598:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_B],&B));
599:   if (ds->state>DS_STATE_RAW) {
600:     PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Q],&Q));
601:     PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_Z],&Z));
602:   }
603:   PetscFunctionReturn(PETSC_SUCCESS);
604: }
605: #endif

607: static PetscErrorCode DSTruncate_GNHEP(DS ds,PetscInt n,PetscBool trim)
608: {
609:   PetscInt    i,ld=ds->ld,l=ds->l;
610:   PetscScalar *A,*B;

612:   PetscFunctionBegin;
613:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
614:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_B],&B));
615: #if PetscDefined(USE_DEBUG)
616:   /* make sure diagonal 2x2 block is not broken */
617:   PetscCheck(ds->state<DS_STATE_CONDENSED || n==0 || n==ds->n || (A[n+(n-1)*ld]==0.0 && B[n+(n-1)*ld]==0.0),PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"The given size would break a 2x2 block, call DSGetTruncateSize() first");
618: #endif
619:   if (trim) {
620:     if (ds->extrarow) {   /* clean extra row */
621:       for (i=l;i<ds->n;i++) A[ds->n+i*ld] = 0.0;
622:       for (i=l;i<ds->n;i++) B[ds->n+i*ld] = 0.0;
623:     }
624:     ds->l = 0;
625:     ds->k = 0;
626:     ds->n = n;
627:     ds->t = ds->n;   /* truncated length equal to the new dimension */
628:   } else {
629:     if (ds->extrarow && ds->k==ds->n) {
630:       /* copy entries of extra row to the new position, then clean last row */
631:       for (i=l;i<n;i++) A[n+i*ld] = A[ds->n+i*ld];
632:       for (i=l;i<ds->n;i++) A[ds->n+i*ld] = 0.0;
633:       for (i=l;i<n;i++) B[n+i*ld] = B[ds->n+i*ld];
634:       for (i=l;i<ds->n;i++) B[ds->n+i*ld] = 0.0;
635:     }
636:     ds->k = ds->extrarow? n: 0;
637:     ds->t = ds->n;   /* truncated length equal to previous dimension */
638:     ds->n = n;
639:   }
640:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
641:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_B],&B));
642:   PetscFunctionReturn(PETSC_SUCCESS);
643: }

645: /*MC
646:    DSGNHEP - Dense Generalized Non-Hermitian Eigenvalue Problem.

648:    Notes:
649:    The problem is expressed as $AX = BX\Lambda$, where $(A,B)$ is the input
650:    matrix pencil. $\Lambda$ is a diagonal matrix whose diagonal elements are the
651:    arguments of `DSSolve()`. After solve, $(A,B)$ is overwritten with the
652:    generalized (real) Schur form $(S,T) = (Z^*AQ,Z^*BQ)$, with the first
653:    matrix being upper quasi-triangular and the second one triangular.

655:    Used DS matrices:
656: +  `DS_MAT_A` - first problem matrix
657: .  `DS_MAT_B` - second problem matrix
658: .  `DS_MAT_Q` - first orthogonal/unitary transformation that reduces to
659:    generalized (real) Schur form
660: -  `DS_MAT_Z` - second orthogonal/unitary transformation that reduces to
661:    generalized (real) Schur form

663:    Implemented methods:
664: +  0 - QZ iteration (`_gges`)
665: -  1 - blocked QZ iteration (`_gges3`, if available)

667:    Level: beginner

669: .seealso: [](sec:ds), `DSCreate()`, `DSSetType()`, `DSType`
670: M*/
671: SLEPC_EXTERN PetscErrorCode DSCreate_GNHEP(DS ds)
672: {
673:   PetscFunctionBegin;
674:   ds->ops->allocate        = DSAllocate_GNHEP;
675:   ds->ops->view            = DSView_GNHEP;
676:   ds->ops->vectors         = DSVectors_GNHEP;
677:   ds->ops->solve[0]        = DSSolve_GNHEP;
678: #if !defined(SLEPC_MISSING_LAPACK_GGES3)
679:   ds->ops->solve[1]        = DSSolve_GNHEP;
680: #endif
681:   ds->ops->sort            = DSSort_GNHEP;
682: #if !PetscDefined(HAVE_MPIUNI)
683:   ds->ops->synchronize     = DSSynchronize_GNHEP;
684: #endif
685:   ds->ops->gettruncatesize = DSGetTruncateSize_Default;
686:   ds->ops->truncate        = DSTruncate_GNHEP;
687:   ds->ops->update          = DSUpdateExtraRow_GNHEP;
688:   PetscFunctionReturn(PETSC_SUCCESS);
689: }