Actual source code: dssvd.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: typedef struct {
 15:   PetscInt m;              /* number of columns */
 16:   PetscInt t;              /* number of rows of V after truncating */
 17: } DS_SVD;

 19: static PetscErrorCode DSAllocate_SVD(DS ds,PetscInt ld)
 20: {
 21:   PetscFunctionBegin;
 22:   if (!ds->compact) PetscCall(DSAllocateMat_Private(ds,DS_MAT_A));
 23:   PetscCall(DSAllocateMat_Private(ds,DS_MAT_U));
 24:   PetscCall(DSAllocateMat_Private(ds,DS_MAT_V));
 25:   PetscCall(DSAllocateMat_Private(ds,DS_MAT_T));
 26:   PetscCall(PetscFree(ds->perm));
 27:   PetscCall(PetscMalloc1(ld,&ds->perm));
 28:   PetscFunctionReturn(PETSC_SUCCESS);
 29: }

 31: /*   0       l           k                 m-1
 32:     -----------------------------------------
 33:     |*       .           .                  |
 34:     |  *     .           .                  |
 35:     |    *   .           .                  |
 36:     |      * .           .                  |
 37:     |        o           o                  |
 38:     |          o         o                  |
 39:     |            o       o                  |
 40:     |              o     o                  |
 41:     |                o   o                  |
 42:     |                  o o                  |
 43:     |                    o x                |
 44:     |                      x x              |
 45:     |                        x x            |
 46:     |                          x x          |
 47:     |                            x x        |
 48:     |                              x x      |
 49:     |                                x x    |
 50:     |                                  x x  |
 51:     |                                    x x|
 52: n-1 |                                      x|
 53:     -----------------------------------------
 54: */

 56: static PetscErrorCode DSSwitchFormat_SVD(DS ds)
 57: {
 58:   DS_SVD         *ctx = (DS_SVD*)ds->data;
 59:   PetscReal      *T;
 60:   PetscScalar    *A;
 61:   PetscInt       i,m=ctx->m,k=ds->k,ld=ds->ld;

 63:   PetscFunctionBegin;
 64:   PetscCheck(m,PetscObjectComm((PetscObject)ds),PETSC_ERR_ORDER,"You should set the number of columns with DSSVDSetDimensions()");
 65:   PetscCheck(ds->compact,PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Must have compact storage");
 66:   /* switch from compact (arrow) to dense storage */
 67:   PetscCall(DSAllocateMat_Private(ds,DS_MAT_A));
 68:   PetscCall(MatDenseGetArrayWrite(ds->omat[DS_MAT_A],&A));
 69:   PetscCall(DSGetArrayReal(ds,DS_MAT_T,&T));
 70:   PetscCall(PetscArrayzero(A,ld*ld));
 71:   for (i=0;i<k;i++) {
 72:     A[i+i*ld] = T[i];
 73:     A[i+k*ld] = T[i+ld];
 74:   }
 75:   A[k+k*ld] = T[k];
 76:   for (i=k+1;i<m;i++) {
 77:     A[i+i*ld]   = T[i];
 78:     A[i-1+i*ld] = T[i-1+ld];
 79:   }
 80:   PetscCall(MatDenseRestoreArrayWrite(ds->omat[DS_MAT_A],&A));
 81:   PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&T));
 82:   PetscFunctionReturn(PETSC_SUCCESS);
 83: }

 85: static PetscErrorCode DSView_SVD(DS ds,PetscViewer viewer)
 86: {
 87:   DS_SVD            *ctx = (DS_SVD*)ds->data;
 88:   PetscViewerFormat format;
 89:   PetscInt          i,j,r,c,m=ctx->m,rows,cols;
 90:   PetscReal         *T,value;
 91:   const char        *methodname[] = {
 92:                      "Implicit zero-shift QR for bidiagonals (_bdsqr)",
 93:                      "Divide and Conquer (_bdsdc or _gesdd)"
 94:   };
 95:   const int         nmeth=PETSC_STATIC_ARRAY_LENGTH(methodname);

 97:   PetscFunctionBegin;
 98:   PetscCall(PetscViewerGetFormat(viewer,&format));
 99:   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
100:     PetscCall(PetscViewerASCIIPrintf(viewer,"number of columns: %" PetscInt_FMT "\n",m));
101:     if (ds->method<nmeth) PetscCall(PetscViewerASCIIPrintf(viewer,"solving the problem with: %s\n",methodname[ds->method]));
102:     PetscFunctionReturn(PETSC_SUCCESS);
103:   }
104:   PetscCheck(m,PetscObjectComm((PetscObject)ds),PETSC_ERR_ORDER,"You should set the number of columns with DSSVDSetDimensions()");
105:   if (ds->compact) {
106:     PetscCall(DSGetArrayReal(ds,DS_MAT_T,&T));
107:     PetscCall(PetscViewerASCIIUseTabs(viewer,PETSC_FALSE));
108:     rows = ds->n;
109:     cols = ds->extrarow? m+1: m;
110:     if (format == PETSC_VIEWER_ASCII_MATLAB) {
111:       PetscCall(PetscViewerASCIIPrintf(viewer,"%% Size = %" PetscInt_FMT " %" PetscInt_FMT "\n",rows,cols));
112:       PetscCall(PetscViewerASCIIPrintf(viewer,"zzz = zeros(%" PetscInt_FMT ",3);\n",2*ds->n));
113:       PetscCall(PetscViewerASCIIPrintf(viewer,"zzz = [\n"));
114:       for (i=0;i<PetscMin(ds->n,m);i++) PetscCall(PetscViewerASCIIPrintf(viewer,"%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e\n",i+1,i+1,(double)T[i]));
115:       for (i=0;i<cols-1;i++) {
116:         r = PetscMax(i+2,ds->k+1);
117:         c = i+1;
118:         PetscCall(PetscViewerASCIIPrintf(viewer,"%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e\n",c,r,(double)T[i+ds->ld]));
119:       }
120:       PetscCall(PetscViewerASCIIPrintf(viewer,"];\n%s = spconvert(zzz);\n",DSMatName[DS_MAT_T]));
121:     } else {
122:       for (i=0;i<rows;i++) {
123:         for (j=0;j<cols;j++) {
124:           if (i==j) value = T[i];
125:           else if (i<ds->k && j==ds->k) value = T[PetscMin(i,j)+ds->ld];
126:           else if (i+1==j && i>=ds->k) value = T[i+ds->ld];
127:           else value = 0.0;
128:           PetscCall(PetscViewerASCIIPrintf(viewer," %18.16e ",(double)value));
129:         }
130:         PetscCall(PetscViewerASCIIPrintf(viewer,"\n"));
131:       }
132:     }
133:     PetscCall(PetscViewerASCIIUseTabs(viewer,PETSC_TRUE));
134:     PetscCall(PetscViewerFlush(viewer));
135:     PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&T));
136:   } else PetscCall(DSViewMat(ds,viewer,DS_MAT_A));
137:   if (ds->state>DS_STATE_INTERMEDIATE) {
138:     PetscCall(DSViewMat(ds,viewer,DS_MAT_U));
139:     PetscCall(DSViewMat(ds,viewer,DS_MAT_V));
140:   }
141:   PetscFunctionReturn(PETSC_SUCCESS);
142: }

144: static PetscErrorCode DSVectors_SVD(DS ds,DSMatType mat,PetscInt *j,PetscReal *rnorm)
145: {
146:   PetscFunctionBegin;
147:   switch (mat) {
148:     case DS_MAT_U:
149:     case DS_MAT_V:
150:       if (rnorm) *rnorm = 0.0;
151:       break;
152:     default:
153:       SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_OUTOFRANGE,"Invalid mat parameter");
154:   }
155:   PetscFunctionReturn(PETSC_SUCCESS);
156: }

158: static PetscErrorCode DSSort_SVD(DS ds,PetscScalar *wr,PetscScalar *wi,PetscScalar *rr,PetscScalar *ri,PetscInt *k)
159: {
160:   DS_SVD         *ctx = (DS_SVD*)ds->data;
161:   PetscInt       n,l,i,*perm,ld=ds->ld;
162:   PetscScalar    *A;
163:   PetscReal      *d;

165:   PetscFunctionBegin;
166:   if (!ds->sc) PetscFunctionReturn(PETSC_SUCCESS);
167:   PetscCheck(ctx->m,PetscObjectComm((PetscObject)ds),PETSC_ERR_ORDER,"You should set the number of columns with DSSVDSetDimensions()");
168:   l = ds->l;
169:   n = PetscMin(ds->n,ctx->m);
170:   PetscCall(DSGetArrayReal(ds,DS_MAT_T,&d));
171:   perm = ds->perm;
172:   if (!rr) PetscCall(DSSortEigenvaluesReal_Private(ds,d,perm));
173:   else PetscCall(DSSortEigenvalues_Private(ds,rr,ri,perm,PETSC_FALSE));
174:   for (i=l;i<n;i++) wr[i] = d[perm[i]];
175:   PetscCall(DSPermuteBoth_Private(ds,l,n,ds->n,ctx->m,DS_MAT_U,DS_MAT_V,perm));
176:   for (i=l;i<n;i++) d[i] = PetscRealPart(wr[i]);
177:   if (!ds->compact) {
178:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
179:     for (i=l;i<n;i++) A[i+i*ld] = wr[i];
180:     PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
181:   }
182:   PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&d));
183:   PetscFunctionReturn(PETSC_SUCCESS);
184: }

186: static PetscErrorCode DSUpdateExtraRow_SVD(DS ds)
187: {
188:   DS_SVD            *ctx = (DS_SVD*)ds->data;
189:   PetscInt          i;
190:   PetscBLASInt      n=0,m=0,ld,incx=1;
191:   PetscScalar       *A,*x,*y,one=1.0,zero=0.0;
192:   PetscReal         *T,*e,beta;
193:   const PetscScalar *U;

195:   PetscFunctionBegin;
196:   PetscCheck(ctx->m,PetscObjectComm((PetscObject)ds),PETSC_ERR_ORDER,"You should set the number of columns with DSSVDSetDimensions()");
197:   PetscCall(PetscBLASIntCast(ds->n,&n));
198:   PetscCall(PetscBLASIntCast(ctx->m,&m));
199:   PetscCall(PetscBLASIntCast(ds->ld,&ld));
200:   PetscCall(MatDenseGetArrayRead(ds->omat[DS_MAT_U],&U));
201:   if (ds->compact) {
202:     PetscCall(DSGetArrayReal(ds,DS_MAT_T,&T));
203:     e = T+ld;
204:     beta = e[m-1];   /* in compact, we assume all entries are zero except the last one */
205:     for (i=0;i<n;i++) e[i] = PetscRealPart(beta*U[n-1+i*ld]);
206:     ds->k = m;
207:     PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&T));
208:   } else {
209:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
210:     PetscCall(DSAllocateWork_Private(ds,2*ld,0,0));
211:     x = ds->work;
212:     y = ds->work+ld;
213:     for (i=0;i<n;i++) x[i] = PetscConj(A[i+m*ld]);
214:     PetscCallBLAS("BLASgemv",BLASgemv_("C",&n,&n,&one,U,&ld,x,&incx,&zero,y,&incx));
215:     for (i=0;i<n;i++) A[i+m*ld] = PetscConj(y[i]);
216:     ds->k = m;
217:     PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
218:   }
219:   PetscCall(MatDenseRestoreArrayRead(ds->omat[DS_MAT_U],&U));
220:   PetscFunctionReturn(PETSC_SUCCESS);
221: }

223: static PetscErrorCode DSTruncate_SVD(DS ds,PetscInt n,PetscBool trim)
224: {
225:   PetscInt    i,ld=ds->ld,l=ds->l;
226:   PetscScalar *A;
227:   DS_SVD      *ctx = (DS_SVD*)ds->data;

229:   PetscFunctionBegin;
230:   if (!ds->compact && ds->extrarow) PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
231:   if (trim) {
232:     if (!ds->compact && ds->extrarow) {   /* clean extra column */
233:       for (i=l;i<ds->n;i++) A[i+ctx->m*ld] = 0.0;
234:     }
235:     ds->l  = 0;
236:     ds->k  = 0;
237:     ds->n  = n;
238:     ctx->m = n;
239:     ds->t  = ds->n;   /* truncated length equal to the new dimension */
240:     ctx->t = ctx->m;  /* must also keep the previous dimension of V */
241:   } else {
242:     if (!ds->compact && ds->extrarow && ds->k==ds->n) {
243:       /* copy entries of extra column to the new position, then clean last row */
244:       for (i=l;i<n;i++) A[i+n*ld] = A[i+ctx->m*ld];
245:       for (i=l;i<ds->n;i++) A[i+ctx->m*ld] = 0.0;
246:     }
247:     ds->k  = ds->extrarow? n: 0;
248:     ds->t  = ds->n;   /* truncated length equal to previous dimension */
249:     ctx->t = ctx->m;  /* must also keep the previous dimension of V */
250:     ds->n  = n;
251:     ctx->m = n;
252:   }
253:   if (!ds->compact && ds->extrarow) PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
254:   PetscFunctionReturn(PETSC_SUCCESS);
255: }

257: /*
258:   DSArrowBidiag reduces a real square arrowhead matrix of the form

260:                 [ d 0 0 0 e ]
261:                 [ 0 d 0 0 e ]
262:             A = [ 0 0 d 0 e ]
263:                 [ 0 0 0 d e ]
264:                 [ 0 0 0 0 d ]

266:   to upper bidiagonal form

268:                 [ d e 0 0 0 ]
269:                 [ 0 d e 0 0 ]
270:    B = Q'*A*P = [ 0 0 d e 0 ],
271:                 [ 0 0 0 d e ]
272:                 [ 0 0 0 0 d ]

274:   where P,Q are orthogonal matrices. Uses plane rotations with a bulge chasing scheme.
275:   On input, P and Q must be initialized to the identity matrix.
276: */
277: static PetscErrorCode DSArrowBidiag(PetscBLASInt n,PetscReal *d,PetscReal *e,PetscScalar *Q,PetscBLASInt ldq,PetscScalar *P,PetscBLASInt ldp)
278: {
279:   PetscBLASInt i,j,j2,one=1;
280:   PetscReal    c,s,ct,st,off,temp0,temp1,temp2;

282:   PetscFunctionBegin;
283:   if (n<=2) PetscFunctionReturn(PETSC_SUCCESS);

285:   for (j=0;j<n-2;j++) {

287:     /* Eliminate entry e(j) by a rotation in the planes (j,j+1) */
288:     temp0 = e[j+1];
289:     PetscCallBLAS("LAPACKlartg",LAPACKREALlartg_(&temp0,&e[j],&c,&s,&e[j+1]));
290:     s = -s;

292:     /* Apply rotation to Q */
293:     j2 = j+2;
294:     PetscCallBLAS("BLASrot",BLASMIXEDrot_(&j2,Q+j*ldq,&one,Q+(j+1)*ldq,&one,&c,&s));

296:     /* Apply rotation to diagonal elements, eliminate newly introduced entry A(j+1,j) */
297:     temp0 = d[j+1];
298:     temp1 = c*temp0;
299:     temp2 = -s*d[j];
300:     PetscCallBLAS("LAPACKlartg",LAPACKREALlartg_(&temp1,&temp2,&ct,&st,&d[j+1]));
301:     st = -st;
302:     e[j] = -c*st*d[j] + s*ct*temp0;
303:     d[j] = c*ct*d[j] + s*st*temp0;

305:     /* Apply rotation to P */
306:     PetscCallBLAS("BLASrot",BLASMIXEDrot_(&j2,P+j*ldp,&one,P+(j+1)*ldp,&one,&ct,&st));

308:     /* Chase newly introduced off-diagonal entry to the top left corner */
309:     for (i=j-1;i>=0;i--) {

311:       /* Upper bulge */
312:       off   = -st*e[i];
313:       e[i]  = ct*e[i];
314:       temp0 = e[i+1];
315:       PetscCallBLAS("LAPACKlartg",LAPACKREALlartg_(&temp0,&off,&c,&s,&e[i+1]));
316:       s = -s;
317:       PetscCallBLAS("BLASrot",BLASMIXEDrot_(&j2,Q+i*ldq,&one,Q+(i+1)*ldq,&one,&c,&s));

319:       /* Lower bulge */
320:       temp0 = d[i+1];
321:       temp1 = -s*e[i] + c*temp0;
322:       temp2 = c*e[i] + s*temp0;
323:       off   = -s*d[i];
324:       PetscCallBLAS("LAPACKlartg",LAPACKREALlartg_(&temp1,&off,&ct,&st,&d[i+1]));
325:       st = -st;
326:       e[i] = -c*st*d[i] + ct*temp2;
327:       d[i] = c*ct*d[i] + st*temp2;
328:       PetscCallBLAS("BLASrot",BLASMIXEDrot_(&j2,P+i*ldp,&one,P+(i+1)*ldp,&one,&ct,&st));
329:     }
330:   }
331:   PetscFunctionReturn(PETSC_SUCCESS);
332: }

334: /*
335:    Reduce to bidiagonal form by means of DSArrowBidiag.
336: */
337: static PetscErrorCode DSIntermediate_SVD(DS ds)
338: {
339:   DS_SVD        *ctx = (DS_SVD*)ds->data;
340:   PetscInt      i,j;
341:   PetscBLASInt  n1 = 0,n2,m2,lwork,l = 0,n = 0,m = 0,nm,ld,off;
342:   PetscScalar   *A,*U,*V,*W,*work,*tauq,*taup;
343:   PetscReal     *d,*e;

345:   PetscFunctionBegin;
346:   PetscCall(PetscBLASIntCast(ds->n,&n));
347:   PetscCall(PetscBLASIntCast(ctx->m,&m));
348:   PetscCall(PetscBLASIntCast(ds->l,&l));
349:   PetscCall(PetscBLASIntCast(ds->ld,&ld));
350:   PetscCall(PetscBLASIntCast(PetscMax(0,ds->k-l+1),&n1)); /* size of leading block, excl. locked */
351:   n2 = n-l;     /* n2 = n1 + size of trailing block */
352:   m2 = m-l;
353:   off = l+l*ld;
354:   nm = PetscMin(n,m);
355:   PetscCall(DSGetArrayReal(ds,DS_MAT_T,&d));
356:   e = d+ld;
357:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_U],&U));
358:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_V],&V));
359:   PetscCall(PetscArrayzero(U,ld*ld));
360:   for (i=0;i<n;i++) U[i+i*ld] = 1.0;
361:   PetscCall(PetscArrayzero(V,ld*ld));
362:   for (i=0;i<m;i++) V[i+i*ld] = 1.0;

364:   if (ds->compact) {

366:     if (ds->state<DS_STATE_INTERMEDIATE) PetscCall(DSArrowBidiag(n1,d+l,e+l,U+off,ld,V+off,ld));

368:   } else {

370:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
371:     for (i=0;i<l;i++) { d[i] = PetscRealPart(A[i+i*ld]); e[i] = 0.0; }

373:     if (ds->state<DS_STATE_INTERMEDIATE) {
374:       lwork = (m+n)*16;
375:       PetscCall(DSAllocateWork_Private(ds,2*nm+ld*ld+lwork,0,0));
376:       tauq = ds->work;
377:       taup = ds->work+nm;
378:       W    = ds->work+2*nm;
379:       work = ds->work+2*nm+ld*ld;
380:       for (j=0;j<m;j++) PetscCall(PetscArraycpy(W+j*ld,A+j*ld,n));
381:       PetscCallLAPACKInfo("LAPACKgebrd",LAPACKgebrd_(&n2,&m2,W+off,&ld,d+l,e+l,tauq,taup,work,&lwork,&info));
382:       PetscCallLAPACKInfo("LAPACKormbr",LAPACKormbr_("Q","L","N",&n2,&n2,&m2,W+off,&ld,tauq,U+off,&ld,work,&lwork,&info));
383:       PetscCallLAPACKInfo("LAPACKormbr",LAPACKormbr_("P","R","N",&m2,&m2,&n2,W+off,&ld,taup,V+off,&ld,work,&lwork,&info));
384:     } else {
385:       /* copy bidiagonal to d,e */
386:       for (i=l;i<nm;i++)   d[i] = PetscRealPart(A[i+i*ld]);
387:       for (i=l;i<nm-1;i++) e[i] = PetscRealPart(A[i+(i+1)*ld]);
388:     }
389:     PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
390:   }
391:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_U],&U));
392:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_V],&V));
393:   PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&d));
394:   PetscFunctionReturn(PETSC_SUCCESS);
395: }

397: static PetscErrorCode DSSolve_SVD_QR(DS ds,PetscScalar *wr,PetscScalar *wi)
398: {
399:   DS_SVD         *ctx = (DS_SVD*)ds->data;
400:   PetscInt       i,j,neig=PetscMin(ds->n,ctx->m);
401:   PetscBLASInt   n1,m1,l = 0,n = 0,m = 0,nm,ld,off,zero=0;
402:   PetscScalar    *A,*U,*V,*Vt;
403:   PetscReal      *d,*e;

405:   PetscFunctionBegin;
406:   PetscCheck(ctx->m,PetscObjectComm((PetscObject)ds),PETSC_ERR_ORDER,"You should set the number of columns with DSSVDSetDimensions()");
407:   PetscCall(PetscBLASIntCast(ds->n,&n));
408:   PetscCall(PetscBLASIntCast(ctx->m,&m));
409:   PetscCall(PetscBLASIntCast(ds->l,&l));
410:   PetscCall(PetscBLASIntCast(ds->ld,&ld));
411:   n1 = n-l;     /* n1 = size of leading block, excl. locked + size of trailing block */
412:   m1 = m-l;
413:   nm = PetscMin(n1,m1);
414:   off = l+l*ld;
415:   PetscCall(DSGetArrayReal(ds,DS_MAT_T,&d));
416:   e = d+ld;

418:   /* Reduce to bidiagonal form */
419:   PetscCall(DSIntermediate_SVD(ds));

421:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_U],&U));
422:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_V],&V));

424:   /* solve bidiagonal SVD problem */
425:   for (i=0;i<l;i++) wr[i] = d[i];
426:   PetscCall(DSAllocateWork_Private(ds,ld*ld,4*n1,0));
427:   Vt = ds->work;
428:   for (i=l;i<m;i++) {
429:     for (j=l;j<m;j++) {
430:       Vt[i+j*ld] = PetscConj(V[j+i*ld]);  /* Lapack expects transposed VT */
431:     }
432:   }
433:   PetscCallLAPACKInfo("LAPACKbdsqr",LAPACKbdsqr_(n>=m?"U":"L",&nm,&m1,&n1,&zero,d+l,e+l,Vt+off,&ld,U+off,&ld,NULL,&ld,ds->rwork,&info));
434:   for (i=l;i<m;i++) {
435:     for (j=l;j<m;j++) {
436:       V[i+j*ld] = PetscConj(Vt[j+i*ld]);  /* transpose VT returned by Lapack */
437:     }
438:   }
439:   for (i=l;i<neig;i++) wr[i] = d[i];

441:   /* create diagonal matrix as a result */
442:   if (ds->compact) PetscCall(PetscArrayzero(e,n-1));
443:   else {
444:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
445:     for (i=l;i<m;i++) PetscCall(PetscArrayzero(A+l+i*ld,n-l));
446:     for (i=l;i<neig;i++) A[i+i*ld] = d[i];
447:     PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
448:   }
449:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_U],&U));
450:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_V],&V));
451:   PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&d));

453:   /* Set wi to zero */
454:   if (wi) for (i=l;i<neig;i++) wi[i] = 0.0;
455:   PetscFunctionReturn(PETSC_SUCCESS);
456: }

458: static PetscErrorCode DSSolve_SVD_DC(DS ds,PetscScalar *wr,PetscScalar *wi)
459: {
460:   DS_SVD         *ctx = (DS_SVD*)ds->data;
461:   PetscInt       i,j,neig=PetscMin(ds->n,ctx->m);
462:   PetscBLASInt   n1,m1,l = 0,n = 0,m = 0,ld,off,lwork;
463:   PetscScalar    *A,*U,*V,*W,qwork;
464:   PetscReal      *d,*e,*Ur,*Vr;

466:   PetscFunctionBegin;
467:   PetscCheck(ctx->m,PetscObjectComm((PetscObject)ds),PETSC_ERR_ORDER,"You should set the number of columns with DSSVDSetDimensions()");
468:   PetscCall(PetscBLASIntCast(ds->n,&n));
469:   PetscCall(PetscBLASIntCast(ctx->m,&m));
470:   PetscCall(PetscBLASIntCast(ds->l,&l));
471:   PetscCall(PetscBLASIntCast(ds->ld,&ld));
472:   n1 = n-l;     /* n1 = size of leading block, excl. locked + size of trailing block */
473:   m1 = m-l;
474:   off = l+l*ld;
475:   if (ds->compact) PetscCall(DSAllocateMat_Private(ds,DS_MAT_A));
476:   PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
477:   PetscCall(MatDenseGetArrayWrite(ds->omat[DS_MAT_U],&U));
478:   PetscCall(MatDenseGetArrayWrite(ds->omat[DS_MAT_V],&V));
479:   PetscCall(DSGetArrayReal(ds,DS_MAT_T,&d));
480:   e = d+ld;
481:   PetscCall(PetscArrayzero(U,ld*ld));
482:   for (i=0;i<l;i++) U[i+i*ld] = 1.0;
483:   PetscCall(PetscArrayzero(V,ld*ld));
484:   for (i=0;i<l;i++) V[i+i*ld] = 1.0;

486:   if (ds->state>DS_STATE_RAW) {
487:     /* solve bidiagonal SVD problem */
488:     for (i=0;i<l;i++) wr[i] = d[i];
489: #if PetscDefined(USE_COMPLEX)
490:     PetscCall(DSAllocateWork_Private(ds,0,3*n1*n1+4*n1+2*ld*ld,8*n1));
491:     Ur = ds->rwork+3*n1*n1+4*n1;
492:     Vr = ds->rwork+3*n1*n1+4*n1+ld*ld;
493: #else
494:     PetscCall(DSAllocateWork_Private(ds,0,3*n1*n1+4*n1+ld*ld,8*n1));
495:     Ur = U;
496:     Vr = ds->rwork+3*n1*n1+4*n1;
497: #endif
498:     PetscCallLAPACKInfo("LAPACKbdsdc",LAPACKbdsdc_("U","I",&n1,d+l,e+l,Ur+off,&ld,Vr+off,&ld,NULL,NULL,ds->rwork,ds->iwork,&info));
499:     for (i=l;i<n;i++) {
500:       for (j=l;j<n;j++) {
501: #if PetscDefined(USE_COMPLEX)
502:         U[i+j*ld] = Ur[i+j*ld];
503: #endif
504:         V[i+j*ld] = PetscConj(Vr[j+i*ld]);  /* transpose VT returned by Lapack */
505:       }
506:     }
507:   } else {
508:     /* solve general rectangular SVD problem */
509:     PetscCall(DSAllocateMat_Private(ds,DS_MAT_W));
510:     PetscCall(MatDenseGetArrayWrite(ds->omat[DS_MAT_W],&W));
511:     if (ds->compact) PetscCall(DSSwitchFormat_SVD(ds));
512:     for (i=0;i<l;i++) wr[i] = d[i];
513:     PetscCall(DSAllocateWork_Private(ds,0,0,8*neig));
514:     lwork = -1;
515: #if PetscDefined(USE_COMPLEX)
516:     PetscCall(DSAllocateWork_Private(ds,0,5*neig*neig+7*neig,0));
517:     PetscCallLAPACKInfo("LAPACKgesdd",LAPACKgesdd_("A",&n1,&m1,A+off,&ld,d+l,U+off,&ld,W+off,&ld,&qwork,&lwork,ds->rwork,ds->iwork,&info));
518: #else
519:     PetscCallLAPACKInfo("LAPACKgesdd",LAPACKgesdd_("A",&n1,&m1,A+off,&ld,d+l,U+off,&ld,W+off,&ld,&qwork,&lwork,ds->iwork,&info));
520: #endif
521:     PetscCall(PetscBLASIntCast((PetscInt)PetscRealPart(qwork),&lwork));
522:     PetscCall(DSAllocateWork_Private(ds,lwork,0,0));
523: #if PetscDefined(USE_COMPLEX)
524:     PetscCallLAPACKInfo("LAPACKgesdd",LAPACKgesdd_("A",&n1,&m1,A+off,&ld,d+l,U+off,&ld,W+off,&ld,ds->work,&lwork,ds->rwork,ds->iwork,&info));
525: #else
526:     PetscCallLAPACKInfo("LAPACKgesdd",LAPACKgesdd_("A",&n1,&m1,A+off,&ld,d+l,U+off,&ld,W+off,&ld,ds->work,&lwork,ds->iwork,&info));
527: #endif
528:     for (i=l;i<m;i++) {
529:       for (j=l;j<m;j++) V[i+j*ld] = PetscConj(W[j+i*ld]);  /* transpose VT returned by Lapack */
530:     }
531:     PetscCall(MatDenseRestoreArrayWrite(ds->omat[DS_MAT_W],&W));
532:   }
533:   for (i=l;i<neig;i++) wr[i] = d[i];

535:   /* create diagonal matrix as a result */
536:   if (ds->compact) PetscCall(PetscArrayzero(e,n-1));
537:   else {
538:     for (i=l;i<m;i++) PetscCall(PetscArrayzero(A+l+i*ld,n-l));
539:     for (i=l;i<n;i++) A[i+i*ld] = d[i];
540:   }
541:   PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
542:   PetscCall(MatDenseRestoreArrayWrite(ds->omat[DS_MAT_U],&U));
543:   PetscCall(MatDenseRestoreArrayWrite(ds->omat[DS_MAT_V],&V));
544:   PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&d));

546:   /* Set wi to zero */
547:   if (wi) for (i=l;i<neig;i++) wi[i] = 0.0;
548:   PetscFunctionReturn(PETSC_SUCCESS);
549: }

551: #if !PetscDefined(HAVE_MPIUNI)
552: static PetscErrorCode DSSynchronize_SVD(DS ds,PetscScalar eigr[],PetscScalar eigi[])
553: {
554:   PetscInt       ld=ds->ld,l=ds->l,k=0,kr=0;
555:   PetscMPIInt    n,rank,off=0,size,ldn,ld3;
556:   PetscScalar    *A,*U,*V;
557:   PetscReal      *T;

559:   PetscFunctionBegin;
560:   if (ds->compact) kr = 3*ld;
561:   else k = (ds->n-l)*ld;
562:   if (ds->state>DS_STATE_RAW) k += 2*(ds->n-l)*ld;
563:   if (eigr) k += ds->n-l;
564:   PetscCall(DSAllocateWork_Private(ds,k+kr,0,0));
565:   PetscCall(PetscMPIIntCast(k*sizeof(PetscScalar)+kr*sizeof(PetscReal),&size));
566:   PetscCall(PetscMPIIntCast(ds->n-l,&n));
567:   PetscCall(PetscMPIIntCast(ld*(ds->n-l),&ldn));
568:   PetscCall(PetscMPIIntCast(3*ld,&ld3));
569:   if (ds->compact) PetscCall(DSGetArrayReal(ds,DS_MAT_T,&T));
570:   else PetscCall(MatDenseGetArray(ds->omat[DS_MAT_A],&A));
571:   if (ds->state>DS_STATE_RAW) {
572:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_U],&U));
573:     PetscCall(MatDenseGetArray(ds->omat[DS_MAT_V],&V));
574:   }
575:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ds),&rank));
576:   if (!rank) {
577:     if (ds->compact) PetscCallMPI(MPI_Pack(T,ld3,MPIU_REAL,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
578:     else PetscCallMPI(MPI_Pack(A+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
579:     if (ds->state>DS_STATE_RAW) {
580:       PetscCallMPI(MPI_Pack(U+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
581:       PetscCallMPI(MPI_Pack(V+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
582:     }
583:     if (eigr) PetscCallMPI(MPI_Pack(eigr+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds)));
584:   }
585:   PetscCallMPI(MPI_Bcast(ds->work,size,MPI_BYTE,0,PetscObjectComm((PetscObject)ds)));
586:   if (rank) {
587:     if (ds->compact) PetscCallMPI(MPI_Unpack(ds->work,size,&off,T,ld3,MPIU_REAL,PetscObjectComm((PetscObject)ds)));
588:     else PetscCallMPI(MPI_Unpack(ds->work,size,&off,A+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
589:     if (ds->state>DS_STATE_RAW) {
590:       PetscCallMPI(MPI_Unpack(ds->work,size,&off,U+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
591:       PetscCallMPI(MPI_Unpack(ds->work,size,&off,V+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
592:     }
593:     if (eigr) PetscCallMPI(MPI_Unpack(ds->work,size,&off,eigr+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds)));
594:   }
595:   if (ds->compact) PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&T));
596:   else PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_A],&A));
597:   if (ds->state>DS_STATE_RAW) {
598:     PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_U],&U));
599:     PetscCall(MatDenseRestoreArray(ds->omat[DS_MAT_V],&V));
600:   }
601:   PetscFunctionReturn(PETSC_SUCCESS);
602: }
603: #endif

605: static PetscErrorCode DSMatGetSize_SVD(DS ds,DSMatType t,PetscInt *rows,PetscInt *cols)
606: {
607:   DS_SVD *ctx = (DS_SVD*)ds->data;

609:   PetscFunctionBegin;
610:   PetscCheck(ctx->m,PetscObjectComm((PetscObject)ds),PETSC_ERR_ORDER,"You should set the number of columns with DSSVDSetDimensions()");
611:   switch (t) {
612:     case DS_MAT_A:
613:       *rows = ds->n;
614:       *cols = ds->extrarow? ctx->m+1: ctx->m;
615:       break;
616:     case DS_MAT_T:
617:       *rows = ds->n;
618:       *cols = PetscDefined(USE_COMPLEX)? 2: 3;
619:       break;
620:     case DS_MAT_U:
621:       *rows = ds->state==DS_STATE_TRUNCATED? ds->t: ds->n;
622:       *cols = ds->n;
623:       break;
624:     case DS_MAT_V:
625:       *rows = ds->state==DS_STATE_TRUNCATED? ctx->t: ctx->m;
626:       *cols = ctx->m;
627:       break;
628:     default:
629:       SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_OUTOFRANGE,"Invalid t parameter");
630:   }
631:   PetscFunctionReturn(PETSC_SUCCESS);
632: }

634: static PetscErrorCode DSSVDSetDimensions_SVD(DS ds,PetscInt m)
635: {
636:   DS_SVD *ctx = (DS_SVD*)ds->data;

638:   PetscFunctionBegin;
639:   DSCheckAlloc(ds,1);
640:   if (m==PETSC_DECIDE || m==PETSC_DEFAULT) {
641:     ctx->m = ds->ld;
642:   } else {
643:     PetscCheck(m>0 && m<=ds->ld,PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of m. Must be between 1 and ld");
644:     ctx->m = m;
645:   }
646:   PetscFunctionReturn(PETSC_SUCCESS);
647: }

649: /*@
650:    DSSVDSetDimensions - Sets the number of columns for a `DSSVD`.

652:    Logically Collective

654:    Input Parameters:
655: +  ds - the direct solver context
656: -  m  - the number of columns

658:    Notes:
659:    This call is complementary to `DSSetDimensions()`, to provide a dimension
660:    that is specific to this `DS` type.

662:    Level: intermediate

664: .seealso: [](sec:ds), `DSSVD`, `DSSVDGetDimensions()`, `DSSetDimensions()`
665: @*/
666: PetscErrorCode DSSVDSetDimensions(DS ds,PetscInt m)
667: {
668:   PetscFunctionBegin;
671:   PetscTryMethod(ds,"DSSVDSetDimensions_C",(DS,PetscInt),(ds,m));
672:   PetscFunctionReturn(PETSC_SUCCESS);
673: }

675: static PetscErrorCode DSSVDGetDimensions_SVD(DS ds,PetscInt *m)
676: {
677:   DS_SVD *ctx = (DS_SVD*)ds->data;

679:   PetscFunctionBegin;
680:   *m = ctx->m;
681:   PetscFunctionReturn(PETSC_SUCCESS);
682: }

684: /*@
685:    DSSVDGetDimensions - Returns the number of columns for a `DSSVD`.

687:    Not Collective

689:    Input Parameter:
690: .  ds - the direct solver context

692:    Output Parameter:
693: .  m - the number of columns

695:    Level: intermediate

697: .seealso: [](sec:ds), `DSSVD`, `DSSVDSetDimensions()`
698: @*/
699: PetscErrorCode DSSVDGetDimensions(DS ds,PetscInt *m)
700: {
701:   PetscFunctionBegin;
703:   PetscAssertPointer(m,2);
704:   PetscUseMethod(ds,"DSSVDGetDimensions_C",(DS,PetscInt*),(ds,m));
705:   PetscFunctionReturn(PETSC_SUCCESS);
706: }

708: static PetscErrorCode DSDestroy_SVD(DS ds)
709: {
710:   PetscFunctionBegin;
711:   PetscCall(PetscFree(ds->data));
712:   PetscCall(PetscObjectComposeFunction((PetscObject)ds,"DSSVDSetDimensions_C",NULL));
713:   PetscCall(PetscObjectComposeFunction((PetscObject)ds,"DSSVDGetDimensions_C",NULL));
714:   PetscFunctionReturn(PETSC_SUCCESS);
715: }

717: static PetscErrorCode DSSetCompact_SVD(DS ds,PetscBool comp)
718: {
719:   PetscFunctionBegin;
720:   if (!comp) PetscCall(DSAllocateMat_Private(ds,DS_MAT_A));
721:   PetscFunctionReturn(PETSC_SUCCESS);
722: }

724: static PetscErrorCode DSReallocate_SVD(DS ds,PetscInt ld)
725: {
726:   PetscInt i,*perm=ds->perm;

728:   PetscFunctionBegin;
729:   for (i=0;i<DS_NUM_MAT;i++) {
730:     if (!ds->compact && i==DS_MAT_A) continue;
731:     if (i!=DS_MAT_U && i!=DS_MAT_V && i!=DS_MAT_T) PetscCall(MatDestroy(&ds->omat[i]));
732:   }

734:   if (!ds->compact) PetscCall(DSReallocateMat_Private(ds,DS_MAT_A,ld));
735:   PetscCall(DSReallocateMat_Private(ds,DS_MAT_U,ld));
736:   PetscCall(DSReallocateMat_Private(ds,DS_MAT_V,ld));
737:   PetscCall(DSReallocateMat_Private(ds,DS_MAT_T,ld));

739:   PetscCall(PetscMalloc1(ld,&ds->perm));
740:   PetscCall(PetscArraycpy(ds->perm,perm,ds->ld));
741:   PetscCall(PetscFree(perm));
742:   PetscFunctionReturn(PETSC_SUCCESS);
743: }

745: /*MC
746:    DSSVD - Dense Singular Value Decomposition.

748:    Notes:
749:    The problem is expressed as $A = U\Sigma V^*$, where $A$ is rectangular in
750:    general, with $n$ rows and $m$ columns. $\Sigma$ is a diagonal matrix whose diagonal
751:    elements are the arguments of `DSSolve()`. After solve, $A$ is overwritten
752:    with $\Sigma$.

754:    The orthogonal (or unitary) matrices of left and right singular vectors, $U$
755:    and $V$, have size $n$ and $m$, respectively. The number of columns $m$ must
756:    be specified via `DSSVDSetDimensions()`.

758:    If the `DS` object is in the intermediate state, $A$ is assumed to be in upper
759:    bidiagonal form (possibly with an arrow) and is stored in compact format
760:    on matrix $T$. Otherwise, no particular structure is assumed. The compact
761:    storage is implemented for the square case only, $m=n$. The extra row should
762:    be interpreted in this case as an extra column.

764:    Used DS matrices:
765: +  `DS_MAT_A` - problem matrix (used only if `compact=PETSC_FALSE`)
766: .  `DS_MAT_T` - upper bidiagonal matrix
767: .  `DS_MAT_U` - left singular vectors
768: -  `DS_MAT_V` - right singular vectors

770:    Implemented methods:
771: +  0 - Implicit zero-shift QR for bidiagonals (`_bdsqr`)
772: -  1 - Divide and Conquer (`_bdsdc` or `_gesdd`)

774:    Level: beginner

776: .seealso: [](sec:ds), `DSCreate()`, `DSSetType()`, `DSType`, `DSSVDSetDimensions()`, `DSSetCompact()`
777: M*/
778: SLEPC_EXTERN PetscErrorCode DSCreate_SVD(DS ds)
779: {
780:   DS_SVD         *ctx;

782:   PetscFunctionBegin;
783:   PetscCall(PetscNew(&ctx));
784:   ds->data = (void*)ctx;

786:   ds->ops->allocate      = DSAllocate_SVD;
787:   ds->ops->view          = DSView_SVD;
788:   ds->ops->vectors       = DSVectors_SVD;
789:   ds->ops->solve[0]      = DSSolve_SVD_QR;
790:   ds->ops->solve[1]      = DSSolve_SVD_DC;
791:   ds->ops->sort          = DSSort_SVD;
792:   ds->ops->truncate      = DSTruncate_SVD;
793:   ds->ops->update        = DSUpdateExtraRow_SVD;
794:   ds->ops->destroy       = DSDestroy_SVD;
795:   ds->ops->matgetsize    = DSMatGetSize_SVD;
796: #if !PetscDefined(HAVE_MPIUNI)
797:   ds->ops->synchronize   = DSSynchronize_SVD;
798: #endif
799:   ds->ops->setcompact    = DSSetCompact_SVD;
800:   ds->ops->reallocate    = DSReallocate_SVD;
801:   PetscCall(PetscObjectComposeFunction((PetscObject)ds,"DSSVDSetDimensions_C",DSSVDSetDimensions_SVD));
802:   PetscCall(PetscObjectComposeFunction((PetscObject)ds,"DSSVDGetDimensions_C",DSSVDGetDimensions_SVD));
803:   PetscFunctionReturn(PETSC_SUCCESS);
804: }