Actual source code: dsnhep.c

slepc-3.15.0 2021-03-31
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2021, 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: PetscErrorCode DSAllocate_NHEP(DS ds,PetscInt ld)
 15: {

 19:   DSAllocateMat_Private(ds,DS_MAT_A);
 20:   DSAllocateMat_Private(ds,DS_MAT_Q);
 21:   PetscFree(ds->perm);
 22:   PetscMalloc1(ld,&ds->perm);
 23:   PetscLogObjectMemory((PetscObject)ds,ld*sizeof(PetscInt));
 24:   return(0);
 25: }

 27: PetscErrorCode DSView_NHEP(DS ds,PetscViewer viewer)
 28: {
 29:   PetscErrorCode    ierr;
 30:   PetscViewerFormat format;

 33:   PetscViewerGetFormat(viewer,&format);
 34:   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) return(0);
 35:   DSViewMat(ds,viewer,DS_MAT_A);
 36:   if (ds->state>DS_STATE_INTERMEDIATE) { DSViewMat(ds,viewer,DS_MAT_Q); }
 37:   if (ds->mat[DS_MAT_X]) { DSViewMat(ds,viewer,DS_MAT_X); }
 38:   if (ds->mat[DS_MAT_Y]) { DSViewMat(ds,viewer,DS_MAT_Y); }
 39:   return(0);
 40: }

 42: static PetscErrorCode DSVectors_NHEP_Refined_Some(DS ds,PetscInt *k,PetscReal *rnorm,PetscBool left)
 43: {
 45:   PetscInt       i,j;
 46:   PetscBLASInt   info,ld,n,n1,lwork,inc=1;
 47:   PetscScalar    sdummy,done=1.0,zero=0.0;
 48:   PetscReal      *sigma;
 49:   PetscBool      iscomplex = PETSC_FALSE;
 50:   PetscScalar    *A = ds->mat[DS_MAT_A];
 51:   PetscScalar    *Q = ds->mat[DS_MAT_Q];
 52:   PetscScalar    *X = ds->mat[left?DS_MAT_Y:DS_MAT_X];
 53:   PetscScalar    *W;

 56:   if (left) SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Not implemented for left vectors");
 57:   PetscBLASIntCast(ds->n,&n);
 58:   PetscBLASIntCast(ds->ld,&ld);
 59:   n1 = n+1;
 60:   if ((*k)<n-1 && A[(*k)+1+(*k)*ld]!=0.0) iscomplex = PETSC_TRUE;
 61:   if (iscomplex) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented for complex eigenvalues yet");
 62:   DSAllocateWork_Private(ds,5*ld,6*ld,0);
 63:   DSAllocateMat_Private(ds,DS_MAT_W);
 64:   W = ds->mat[DS_MAT_W];
 65:   lwork = 5*ld;
 66:   sigma = ds->rwork+5*ld;

 68:   /* build A-w*I in W */
 69:   for (j=0;j<n;j++)
 70:     for (i=0;i<=n;i++)
 71:       W[i+j*ld] = A[i+j*ld];
 72:   for (i=0;i<n;i++)
 73:     W[i+i*ld] -= A[(*k)+(*k)*ld];

 75:   /* compute SVD of W */
 76: #if !defined(PETSC_USE_COMPLEX)
 77:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("N","O",&n1,&n,W,&ld,sigma,&sdummy,&ld,&sdummy,&ld,ds->work,&lwork,&info));
 78: #else
 79:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("N","O",&n1,&n,W,&ld,sigma,&sdummy,&ld,&sdummy,&ld,ds->work,&lwork,ds->rwork,&info));
 80: #endif
 81:   SlepcCheckLapackInfo("gesvd",info);

 83:   /* the smallest singular value is the new error estimate */
 84:   if (rnorm) *rnorm = sigma[n-1];

 86:   /* update vector with right singular vector associated to smallest singular value,
 87:      accumulating the transformation matrix Q */
 88:   PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&n,&n,&done,Q,&ld,W+n-1,&ld,&zero,X+(*k)*ld,&inc));
 89:   return(0);
 90: }

 92: static PetscErrorCode DSVectors_NHEP_Refined_All(DS ds,PetscBool left)
 93: {
 95:   PetscInt       i;

 98:   for (i=0;i<ds->n;i++) {
 99:     DSVectors_NHEP_Refined_Some(ds,&i,NULL,left);
100:   }
101:   return(0);
102: }

104: static PetscErrorCode DSVectors_NHEP_Eigen_Some(DS ds,PetscInt *k,PetscReal *rnorm,PetscBool left)
105: {
107:   PetscInt       i;
108:   PetscBLASInt   mm=1,mout,info,ld,n,*select,inc=1,cols=1,zero=0;
109:   PetscScalar    sone=1.0,szero=0.0;
110:   PetscReal      norm,done=1.0;
111:   PetscBool      iscomplex = PETSC_FALSE;
112:   PetscScalar    *A = ds->mat[DS_MAT_A];
113:   PetscScalar    *Q = ds->mat[DS_MAT_Q];
114:   PetscScalar    *X = ds->mat[left?DS_MAT_Y:DS_MAT_X];
115:   PetscScalar    *Y;

118:   PetscBLASIntCast(ds->n,&n);
119:   PetscBLASIntCast(ds->ld,&ld);
120:   DSAllocateWork_Private(ds,0,0,ld);
121:   select = ds->iwork;
122:   for (i=0;i<n;i++) select[i] = (PetscBLASInt)PETSC_FALSE;

124:   /* compute k-th eigenvector Y of A */
125:   Y = X+(*k)*ld;
126:   select[*k] = (PetscBLASInt)PETSC_TRUE;
127: #if !defined(PETSC_USE_COMPLEX)
128:   if ((*k)<n-1 && A[(*k)+1+(*k)*ld]!=0.0) iscomplex = PETSC_TRUE;
129:   mm = iscomplex? 2: 1;
130:   if (iscomplex) select[(*k)+1] = (PetscBLASInt)PETSC_TRUE;
131:   DSAllocateWork_Private(ds,3*ld,0,0);
132:   PetscStackCallBLAS("LAPACKtrevc",LAPACKtrevc_(left?"L":"R","S",select,&n,A,&ld,Y,&ld,Y,&ld,&mm,&mout,ds->work,&info));
133: #else
134:   DSAllocateWork_Private(ds,2*ld,ld,0);
135:   PetscStackCallBLAS("LAPACKtrevc",LAPACKtrevc_(left?"L":"R","S",select,&n,A,&ld,Y,&ld,Y,&ld,&mm,&mout,ds->work,ds->rwork,&info));
136: #endif
137:   SlepcCheckLapackInfo("trevc",info);
138:   if (mout != mm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Inconsistent arguments");

140:   /* accumulate and normalize eigenvectors */
141:   if (ds->state>=DS_STATE_CONDENSED) {
142:     PetscArraycpy(ds->work,Y,mout*ld);
143:     PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&n,&n,&sone,Q,&ld,ds->work,&inc,&szero,Y,&inc));
144: #if !defined(PETSC_USE_COMPLEX)
145:     if (iscomplex) PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&n,&n,&sone,Q,&ld,ds->work+ld,&inc,&szero,Y+ld,&inc));
146: #endif
147:     cols = 1;
148:     norm = BLASnrm2_(&n,Y,&inc);
149: #if !defined(PETSC_USE_COMPLEX)
150:     if (iscomplex) {
151:       norm = SlepcAbsEigenvalue(norm,BLASnrm2_(&n,Y+ld,&inc));
152:       cols = 2;
153:     }
154: #endif
155:     PetscStackCallBLAS("LAPACKlascl",LAPACKlascl_("G",&zero,&zero,&norm,&done,&n,&cols,Y,&ld,&info));
156:     SlepcCheckLapackInfo("lascl",info);
157:   }

159:   /* set output arguments */
160:   if (iscomplex) (*k)++;
161:   if (rnorm) {
162:     if (iscomplex) *rnorm = SlepcAbsEigenvalue(Y[n-1],Y[n-1+ld]);
163:     else *rnorm = PetscAbsScalar(Y[n-1]);
164:   }
165:   return(0);
166: }

168: static PetscErrorCode DSVectors_NHEP_Eigen_All(DS ds,PetscBool left)
169: {
171:   PetscInt       i;
172:   PetscBLASInt   n,ld,mout,info,inc=1,cols,zero=0;
173:   PetscBool      iscomplex;
174:   PetscScalar    *X,*Y,*Z,*A = ds->mat[DS_MAT_A];
175:   PetscReal      norm,done=1.0;
176:   const char     *side,*back;

179:   PetscBLASIntCast(ds->n,&n);
180:   PetscBLASIntCast(ds->ld,&ld);
181:   if (left) {
182:     X = NULL;
183:     Y = ds->mat[DS_MAT_Y];
184:     side = "L";
185:   } else {
186:     X = ds->mat[DS_MAT_X];
187:     Y = NULL;
188:     side = "R";
189:   }
190:   Z = left? Y: X;
191:   if (ds->state>=DS_STATE_CONDENSED) {
192:     /* DSSolve() has been called, backtransform with matrix Q */
193:     back = "B";
194:     PetscArraycpy(Z,ds->mat[DS_MAT_Q],ld*ld);
195:   } else back = "A";
196: #if !defined(PETSC_USE_COMPLEX)
197:   DSAllocateWork_Private(ds,3*ld,0,0);
198:   PetscStackCallBLAS("LAPACKtrevc",LAPACKtrevc_(side,back,NULL,&n,A,&ld,Y,&ld,X,&ld,&n,&mout,ds->work,&info));
199: #else
200:   DSAllocateWork_Private(ds,2*ld,ld,0);
201:   PetscStackCallBLAS("LAPACKtrevc",LAPACKtrevc_(side,back,NULL,&n,A,&ld,Y,&ld,X,&ld,&n,&mout,ds->work,ds->rwork,&info));
202: #endif
203:   SlepcCheckLapackInfo("trevc",info);

205:   /* normalize eigenvectors */
206:   for (i=0;i<n;i++) {
207:     iscomplex = (i<n-1 && A[i+1+i*ld]!=0.0)? PETSC_TRUE: PETSC_FALSE;
208:     cols = 1;
209:     norm = BLASnrm2_(&n,Z+i*ld,&inc);
210: #if !defined(PETSC_USE_COMPLEX)
211:     if (iscomplex) {
212:       norm = SlepcAbsEigenvalue(norm,BLASnrm2_(&n,Z+(i+1)*ld,&inc));
213:       cols = 2;
214:     }
215: #endif
216:     PetscStackCallBLAS("LAPACKlascl",LAPACKlascl_("G",&zero,&zero,&norm,&done,&n,&cols,Z+i*ld,&ld,&info));
217:     SlepcCheckLapackInfo("lascl",info);
218:     if (iscomplex) i++;
219:   }
220:   return(0);
221: }

223: PetscErrorCode DSVectors_NHEP(DS ds,DSMatType mat,PetscInt *j,PetscReal *rnorm)
224: {

228:   switch (mat) {
229:     case DS_MAT_X:
230:       if (ds->refined) {
231:         if (!ds->extrarow) SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Refined vectors require activating the extra row");
232:         if (j) {
233:           DSVectors_NHEP_Refined_Some(ds,j,rnorm,PETSC_FALSE);
234:         } else {
235:           DSVectors_NHEP_Refined_All(ds,PETSC_FALSE);
236:         }
237:       } else {
238:         if (j) {
239:           DSVectors_NHEP_Eigen_Some(ds,j,rnorm,PETSC_FALSE);
240:         } else {
241:           DSVectors_NHEP_Eigen_All(ds,PETSC_FALSE);
242:         }
243:       }
244:       break;
245:     case DS_MAT_Y:
246:       if (ds->refined) SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Not implemented yet");
247:       if (j) {
248:         DSVectors_NHEP_Eigen_Some(ds,j,rnorm,PETSC_TRUE);
249:       } else {
250:         DSVectors_NHEP_Eigen_All(ds,PETSC_TRUE);
251:       }
252:       break;
253:     case DS_MAT_U:
254:     case DS_MAT_VT:
255:       SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Not implemented yet");
256:     default:
257:       SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_OUTOFRANGE,"Invalid mat parameter");
258:   }
259:   return(0);
260: }

262: static PetscErrorCode DSSort_NHEP_Arbitrary(DS ds,PetscScalar *wr,PetscScalar *wi,PetscScalar *rr,PetscScalar *ri,PetscInt *k)
263: {
265:   PetscInt       i;
266:   PetscBLASInt   info,n,ld,mout,lwork,*selection;
267:   PetscScalar    *T = ds->mat[DS_MAT_A],*Q = ds->mat[DS_MAT_Q],*work;
268: #if !defined(PETSC_USE_COMPLEX)
269:   PetscBLASInt   *iwork,liwork;
270: #endif

273:   if (!k) SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_WRONG,"Must supply argument k");
274:   PetscBLASIntCast(ds->n,&n);
275:   PetscBLASIntCast(ds->ld,&ld);
276: #if !defined(PETSC_USE_COMPLEX)
277:   lwork = n;
278:   liwork = 1;
279:   DSAllocateWork_Private(ds,lwork,0,liwork+n);
280:   work = ds->work;
281:   lwork = ds->lwork;
282:   selection = ds->iwork;
283:   iwork = ds->iwork + n;
284:   liwork = ds->liwork - n;
285: #else
286:   lwork = 1;
287:   DSAllocateWork_Private(ds,lwork,0,n);
288:   work = ds->work;
289:   selection = ds->iwork;
290: #endif
291:   /* Compute the selected eigenvalue to be in the leading position */
292:   DSSortEigenvalues_Private(ds,rr,ri,ds->perm,PETSC_FALSE);
293:   PetscArrayzero(selection,n);
294:   for (i=0;i<*k;i++) selection[ds->perm[i]] = 1;
295: #if !defined(PETSC_USE_COMPLEX)
296:   PetscStackCallBLAS("LAPACKtrsen",LAPACKtrsen_("N","V",selection,&n,T,&ld,Q,&ld,wr,wi,&mout,NULL,NULL,work,&lwork,iwork,&liwork,&info));
297: #else
298:   PetscStackCallBLAS("LAPACKtrsen",LAPACKtrsen_("N","V",selection,&n,T,&ld,Q,&ld,wr,&mout,NULL,NULL,work,&lwork,&info));
299: #endif
300:   SlepcCheckLapackInfo("trsen",info);
301:   *k = mout;
302:   return(0);
303: }

305: PetscErrorCode DSSort_NHEP(DS ds,PetscScalar *wr,PetscScalar *wi,PetscScalar *rr,PetscScalar *ri,PetscInt *k)
306: {

310:   if (!rr || wr == rr) {
311:     DSSort_NHEP_Total(ds,ds->mat[DS_MAT_A],ds->mat[DS_MAT_Q],wr,wi);
312:   } else {
313:     DSSort_NHEP_Arbitrary(ds,wr,wi,rr,ri,k);
314:   }
315:   return(0);
316: }

318: static PetscErrorCode DSSortWithPermutation_NHEP(DS ds,PetscInt *perm,PetscScalar *wr,PetscScalar *wi)
319: {

323:   DSSortWithPermutation_NHEP_Private(ds,perm,ds->mat[DS_MAT_A],ds->mat[DS_MAT_Q],wr,wi);
324:   return(0);
325: }

327: PetscErrorCode DSUpdateExtraRow_NHEP(DS ds)
328: {
330:   PetscInt       i;
331:   PetscBLASInt   n,ld,incx=1;
332:   PetscScalar    *A,*Q,*x,*y,one=1.0,zero=0.0;

335:   PetscBLASIntCast(ds->n,&n);
336:   PetscBLASIntCast(ds->ld,&ld);
337:   A  = ds->mat[DS_MAT_A];
338:   Q  = ds->mat[DS_MAT_Q];
339:   DSAllocateWork_Private(ds,2*ld,0,0);
340:   x = ds->work;
341:   y = ds->work+ld;
342:   for (i=0;i<n;i++) x[i] = PetscConj(A[n+i*ld]);
343:   PetscStackCallBLAS("BLASgemv",BLASgemv_("C",&n,&n,&one,Q,&ld,x,&incx,&zero,y,&incx));
344:   for (i=0;i<n;i++) A[n+i*ld] = PetscConj(y[i]);
345:   ds->k = n;
346:   return(0);
347: }

349: PetscErrorCode DSSolve_NHEP(DS ds,PetscScalar *wr,PetscScalar *wi)
350: {

354: #if !defined(PETSC_USE_COMPLEX)
356: #endif
357:   DSSolve_NHEP_Private(ds,ds->mat[DS_MAT_A],ds->mat[DS_MAT_Q],wr,wi);
358:   return(0);
359: }

361: PetscErrorCode DSSynchronize_NHEP(DS ds,PetscScalar eigr[],PetscScalar eigi[])
362: {
364:   PetscInt       ld=ds->ld,l=ds->l,k;
365:   PetscMPIInt    n,rank,off=0,size,ldn;

368:   k = (ds->n-l)*ld;
369:   if (ds->state>DS_STATE_RAW) k += (ds->n-l)*ld;
370:   if (eigr) k += ds->n-l;
371:   if (eigi) k += ds->n-l;
372:   DSAllocateWork_Private(ds,k,0,0);
373:   PetscMPIIntCast(k*sizeof(PetscScalar),&size);
374:   PetscMPIIntCast(ds->n-l,&n);
375:   PetscMPIIntCast(ld*(ds->n-l),&ldn);
376:   MPI_Comm_rank(PetscObjectComm((PetscObject)ds),&rank);CHKERRMPI(ierr);
377:   if (!rank) {
378:     MPI_Pack(ds->mat[DS_MAT_A]+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
379:     if (ds->state>DS_STATE_RAW) {
380:       MPI_Pack(ds->mat[DS_MAT_Q]+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
381:     }
382:     if (eigr) {
383:       MPI_Pack(eigr+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
384:     }
385:     if (eigi) {
386:       MPI_Pack(eigi+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
387:     }
388:   }
389:   MPI_Bcast(ds->work,size,MPI_BYTE,0,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
390:   if (rank) {
391:     MPI_Unpack(ds->work,size,&off,ds->mat[DS_MAT_A]+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
392:     if (ds->state>DS_STATE_RAW) {
393:       MPI_Unpack(ds->work,size,&off,ds->mat[DS_MAT_Q]+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
394:     }
395:     if (eigr) {
396:       MPI_Unpack(ds->work,size,&off,eigr+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
397:     }
398:     if (eigi) {
399:       MPI_Unpack(ds->work,size,&off,eigi+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
400:     }
401:   }
402:   return(0);
403: }

405: PetscErrorCode DSTruncate_NHEP(DS ds,PetscInt n,PetscBool trim)
406: {
407:   PetscInt    i,ld=ds->ld,l=ds->l;
408:   PetscScalar *A = ds->mat[DS_MAT_A];

411: #if defined(PETSC_USE_DEBUG)
412:   /* make sure diagonal 2x2 block is not broken */
413:   if (ds->state>=DS_STATE_CONDENSED && n>0 && n<ds->n && A[n+(n-1)*ld]!=0.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"The given size would break a 2x2 block, call DSGetTruncateSize() first");
414: #endif
415:   if (trim) {
416:     if (ds->extrarow) {   /* clean extra row */
417:       for (i=l;i<ds->n;i++) A[ds->n+i*ld] = 0.0;
418:     }
419:     ds->l = 0;
420:     ds->k = 0;
421:     ds->n = n;
422:     ds->t = ds->n;   /* truncated length equal to the new dimension */
423:   } else {
424:     if (ds->extrarow && ds->k==ds->n) {
425:       /* copy entries of extra row to the new position, then clean last row */
426:       for (i=l;i<n;i++) A[n+i*ld] = A[ds->n+i*ld];
427:       for (i=l;i<ds->n;i++) A[ds->n+i*ld] = 0.0;
428:     }
429:     ds->k = (ds->extrarow)? n: 0;
430:     ds->t = ds->n;   /* truncated length equal to previous dimension */
431:     ds->n = n;
432:   }
433:   return(0);
434: }

436: PetscErrorCode DSCond_NHEP(DS ds,PetscReal *cond)
437: {
439:   PetscScalar    *work;
440:   PetscReal      *rwork;
441:   PetscBLASInt   *ipiv;
442:   PetscBLASInt   lwork,info,n,ld;
443:   PetscReal      hn,hin;
444:   PetscScalar    *A;

447:   PetscBLASIntCast(ds->n,&n);
448:   PetscBLASIntCast(ds->ld,&ld);
449:   lwork = 8*ld;
450:   DSAllocateWork_Private(ds,lwork,ld,ld);
451:   work  = ds->work;
452:   rwork = ds->rwork;
453:   ipiv  = ds->iwork;

455:   /* use workspace matrix W to avoid overwriting A */
456:   DSAllocateMat_Private(ds,DS_MAT_W);
457:   A = ds->mat[DS_MAT_W];
458:   PetscArraycpy(A,ds->mat[DS_MAT_A],ds->ld*ds->ld);

460:   /* norm of A */
461:   if (ds->state<DS_STATE_INTERMEDIATE) hn = LAPACKlange_("I",&n,&n,A,&ld,rwork);
462:   else hn = LAPACKlanhs_("I",&n,A,&ld,rwork);

464:   /* norm of inv(A) */
465:   PetscStackCallBLAS("LAPACKgetrf",LAPACKgetrf_(&n,&n,A,&ld,ipiv,&info));
466:   SlepcCheckLapackInfo("getrf",info);
467:   PetscStackCallBLAS("LAPACKgetri",LAPACKgetri_(&n,A,&ld,ipiv,work,&lwork,&info));
468:   SlepcCheckLapackInfo("getri",info);
469:   hin = LAPACKlange_("I",&n,&n,A,&ld,rwork);

471:   *cond = hn*hin;
472:   return(0);
473: }

475: PetscErrorCode DSTranslateHarmonic_NHEP(DS ds,PetscScalar tau,PetscReal beta,PetscBool recover,PetscScalar *gin,PetscReal *gammaout)
476: {
478:   PetscInt       i,j;
479:   PetscBLASInt   *ipiv,info,n,ld,one=1,ncol;
480:   PetscScalar    *A,*B,*Q,*g=gin,*ghat;
481:   PetscScalar    done=1.0,dmone=-1.0,dzero=0.0;
482:   PetscReal      gamma=1.0;

485:   PetscBLASIntCast(ds->n,&n);
486:   PetscBLASIntCast(ds->ld,&ld);
487:   A  = ds->mat[DS_MAT_A];

489:   if (!recover) {

491:     DSAllocateWork_Private(ds,0,0,ld);
492:     ipiv = ds->iwork;
493:     if (!g) {
494:       DSAllocateWork_Private(ds,ld,0,0);
495:       g = ds->work;
496:     }
497:     /* use workspace matrix W to factor A-tau*eye(n) */
498:     DSAllocateMat_Private(ds,DS_MAT_W);
499:     B = ds->mat[DS_MAT_W];
500:     PetscArraycpy(B,A,ld*ld);

502:     /* Vector g initialy stores b = beta*e_n^T */
503:     PetscArrayzero(g,n);
504:     g[n-1] = beta;

506:     /* g = (A-tau*eye(n))'\b */
507:     for (i=0;i<n;i++) B[i+i*ld] -= tau;
508:     PetscStackCallBLAS("LAPACKgetrf",LAPACKgetrf_(&n,&n,B,&ld,ipiv,&info));
509:     SlepcCheckLapackInfo("getrf",info);
510:     PetscLogFlops(2.0*n*n*n/3.0);
511:     PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("C",&n,&one,B,&ld,ipiv,g,&ld,&info));
512:     SlepcCheckLapackInfo("getrs",info);
513:     PetscLogFlops(2.0*n*n-n);

515:     /* A = A + g*b' */
516:     for (i=0;i<n;i++) A[i+(n-1)*ld] += g[i]*beta;

518:   } else { /* recover */

520:     DSAllocateWork_Private(ds,ld,0,0);
521:     ghat = ds->work;
522:     Q    = ds->mat[DS_MAT_Q];

524:     /* g^ = -Q(:,idx)'*g */
525:     PetscBLASIntCast(ds->l+ds->k,&ncol);
526:     PetscStackCallBLAS("BLASgemv",BLASgemv_("C",&n,&ncol,&dmone,Q,&ld,g,&one,&dzero,ghat,&one));

528:     /* A = A + g^*b' */
529:     for (i=0;i<ds->l+ds->k;i++)
530:       for (j=ds->l;j<ds->l+ds->k;j++)
531:         A[i+j*ld] += ghat[i]*Q[n-1+j*ld]*beta;

533:     /* g~ = (I-Q(:,idx)*Q(:,idx)')*g = g+Q(:,idx)*g^ */
534:     PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&n,&ncol,&done,Q,&ld,ghat,&one,&done,g,&one));
535:   }

537:   /* Compute gamma factor */
538:   if (gammaout || (recover && ds->extrarow)) gamma = SlepcAbs(1.0,BLASnrm2_(&n,g,&one));
539:   if (gammaout) *gammaout = gamma;
540:   if (recover && ds->extrarow) {
541:     for (j=ds->l;j<ds->l+ds->k;j++) A[ds->n+j*ld] *= gamma;
542:   }
543:   return(0);
544: }

546: SLEPC_EXTERN PetscErrorCode DSCreate_NHEP(DS ds)
547: {
549:   ds->ops->allocate        = DSAllocate_NHEP;
550:   ds->ops->view            = DSView_NHEP;
551:   ds->ops->vectors         = DSVectors_NHEP;
552:   ds->ops->solve[0]        = DSSolve_NHEP;
553:   ds->ops->sort            = DSSort_NHEP;
554:   ds->ops->sortperm        = DSSortWithPermutation_NHEP;
555:   ds->ops->synchronize     = DSSynchronize_NHEP;
556:   ds->ops->gettruncatesize = DSGetTruncateSize_Default;
557:   ds->ops->truncate        = DSTruncate_NHEP;
558:   ds->ops->update          = DSUpdateExtraRow_NHEP;
559:   ds->ops->cond            = DSCond_NHEP;
560:   ds->ops->transharm       = DSTranslateHarmonic_NHEP;
561:   return(0);
562: }