Actual source code: dssvd.c

slepc-3.13.3 2020-06-14
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2020, 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_SVD(DS ds,PetscInt ld)
 15: {

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

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

 54: static PetscErrorCode DSSwitchFormat_SVD(DS ds)
 55: {
 57:   PetscReal      *T = ds->rmat[DS_MAT_T];
 58:   PetscScalar    *A = ds->mat[DS_MAT_A];
 59:   PetscInt       i,m=ds->m,k=ds->k,ld=ds->ld;

 62:   if (!m) SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_WRONG,"m was not set");
 63:   /* switch from compact (arrow) to dense storage */
 64:   PetscArrayzero(A,ld*ld);
 65:   for (i=0;i<k;i++) {
 66:     A[i+i*ld] = T[i];
 67:     A[i+k*ld] = T[i+ld];
 68:   }
 69:   A[k+k*ld] = T[k];
 70:   for (i=k+1;i<m;i++) {
 71:     A[i+i*ld]   = T[i];
 72:     A[i-1+i*ld] = T[i-1+ld];
 73:   }
 74:   return(0);
 75: }

 77: PetscErrorCode DSView_SVD(DS ds,PetscViewer viewer)
 78: {
 79:   PetscErrorCode    ierr;
 80:   PetscViewerFormat format;
 81:   PetscInt          i,j,r,c;
 82:   PetscReal         value;

 85:   PetscViewerGetFormat(viewer,&format);
 86:   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) return(0);
 87:   if (ds->compact) {
 88:     if (!ds->m) SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_WRONG,"m was not set");
 89:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
 90:     if (format == PETSC_VIEWER_ASCII_MATLAB) {
 91:       PetscViewerASCIIPrintf(viewer,"%% Size = %D %D\n",ds->n,ds->m);
 92:       PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",2*ds->n);
 93:       PetscViewerASCIIPrintf(viewer,"zzz = [\n");
 94:       for (i=0;i<PetscMin(ds->n,ds->m);i++) {
 95:         PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,i+1,(double)*(ds->rmat[DS_MAT_T]+i));
 96:       }
 97:       for (i=0;i<PetscMin(ds->n,ds->m)-1;i++) {
 98:         r = PetscMax(i+2,ds->k+1);
 99:         c = i+1;
100:         PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",c,r,(double)*(ds->rmat[DS_MAT_T]+ds->ld+i));
101:       }
102:       PetscViewerASCIIPrintf(viewer,"];\n%s = spconvert(zzz);\n",DSMatName[DS_MAT_T]);
103:     } else {
104:       for (i=0;i<ds->n;i++) {
105:         for (j=0;j<ds->m;j++) {
106:           if (i==j) value = *(ds->rmat[DS_MAT_T]+i);
107:           else if (i<ds->k && j==ds->k) value = *(ds->rmat[DS_MAT_T]+ds->ld+PetscMin(i,j));
108:           else if (i==j+1 && i>ds->k) value = *(ds->rmat[DS_MAT_T]+ds->ld+i-1);
109:           else value = 0.0;
110:           PetscViewerASCIIPrintf(viewer," %18.16e ",(double)value);
111:         }
112:         PetscViewerASCIIPrintf(viewer,"\n");
113:       }
114:     }
115:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
116:     PetscViewerFlush(viewer);
117:   } else {
118:     DSViewMat(ds,viewer,DS_MAT_A);
119:   }
120:   if (ds->state>DS_STATE_INTERMEDIATE) {
121:     DSViewMat(ds,viewer,DS_MAT_U);
122:     DSViewMat(ds,viewer,DS_MAT_VT);
123:   }
124:   return(0);
125: }

127: PetscErrorCode DSVectors_SVD(DS ds,DSMatType mat,PetscInt *j,PetscReal *rnorm)
128: {
130:   switch (mat) {
131:     case DS_MAT_U:
132:     case DS_MAT_VT:
133:       if (rnorm) *rnorm = 0.0;
134:       break;
135:     default:
136:       SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_OUTOFRANGE,"Invalid mat parameter");
137:   }
138:   return(0);
139: }

141: PetscErrorCode DSSort_SVD(DS ds,PetscScalar *wr,PetscScalar *wi,PetscScalar *rr,PetscScalar *ri,PetscInt *k)
142: {
144:   PetscInt       n,l,i,*perm,ld=ds->ld;
145:   PetscScalar    *A;
146:   PetscReal      *d;

149:   if (!ds->sc) return(0);
150:   l = ds->l;
151:   n = PetscMin(ds->n,ds->m);
152:   A = ds->mat[DS_MAT_A];
153:   d = ds->rmat[DS_MAT_T];
154:   perm = ds->perm;
155:   if (!rr) {
156:     DSSortEigenvaluesReal_Private(ds,d,perm);
157:   } else {
158:     DSSortEigenvalues_Private(ds,rr,ri,perm,PETSC_FALSE);
159:   }
160:   for (i=l;i<n;i++) wr[i] = d[perm[i]];
161:   DSPermuteBoth_Private(ds,l,n,DS_MAT_U,DS_MAT_VT,perm);
162:   for (i=l;i<n;i++) d[i] = PetscRealPart(wr[i]);
163:   if (!ds->compact) {
164:     for (i=l;i<n;i++) A[i+i*ld] = wr[i];
165:   }
166:   return(0);
167: }

169: PetscErrorCode DSSolve_SVD_DC(DS ds,PetscScalar *wr,PetscScalar *wi)
170: {
172:   PetscInt       i;
173:   PetscBLASInt   n1,n2,n3,m2,m3,info,l,n,m,nm,ld,off,lwork;
174:   PetscScalar    *A,*U,*VT,qwork;
175:   PetscReal      *d,*e,*Ur,*VTr;
176: #if defined(PETSC_USE_COMPLEX)
177:   PetscInt       j;
178: #endif

181:   PetscBLASIntCast(ds->n,&n);
182:   PetscBLASIntCast(ds->m,&m);
183:   PetscBLASIntCast(ds->l,&l);
184:   PetscBLASIntCast(ds->ld,&ld);
185:   PetscBLASIntCast(ds->k-l+1,&n1); /* size of leading block, excl. locked */
186:   PetscBLASIntCast(n-ds->k-1,&n2); /* size of trailing block */
187:   PetscBLASIntCast(m-ds->k-1,&m2);
188:   n3 = n1+n2;
189:   m3 = n1+m2;
190:   off = l+l*ld;
191:   A  = ds->mat[DS_MAT_A];
192:   U  = ds->mat[DS_MAT_U];
193:   VT = ds->mat[DS_MAT_VT];
194:   d  = ds->rmat[DS_MAT_T];
195:   e  = ds->rmat[DS_MAT_T]+ld;
196:   PetscArrayzero(U,ld*ld);
197:   for (i=0;i<l;i++) U[i+i*ld] = 1.0;
198:   PetscArrayzero(VT,ld*ld);
199:   for (i=0;i<l;i++) VT[i+i*ld] = 1.0;

201:   if (ds->state>DS_STATE_RAW) {
202:     /* solve bidiagonal SVD problem */
203:     for (i=0;i<l;i++) wr[i] = d[i];
204:     DSAllocateWork_Private(ds,0,3*ld*ld+4*ld,8*ld);
205: #if defined(PETSC_USE_COMPLEX)
206:     DSAllocateMatReal_Private(ds,DS_MAT_U);
207:     DSAllocateMatReal_Private(ds,DS_MAT_VT);
208:     Ur  = ds->rmat[DS_MAT_U];
209:     VTr = ds->rmat[DS_MAT_VT];
210: #else
211:     Ur  = U;
212:     VTr = VT;
213: #endif
214:     PetscStackCallBLAS("LAPACKbdsdc",LAPACKbdsdc_("U","I",&n3,d+l,e+l,Ur+off,&ld,VTr+off,&ld,NULL,NULL,ds->rwork,ds->iwork,&info));
215:     SlepcCheckLapackInfo("bdsdc",info);
216: #if defined(PETSC_USE_COMPLEX)
217:     for (i=l;i<n;i++) {
218:       for (j=0;j<n;j++) {
219:         U[i+j*ld] = Ur[i+j*ld];
220:         VT[i+j*ld] = VTr[i+j*ld];
221:       }
222:     }
223: #endif
224:   } else {
225:     /* solve general rectangular SVD problem */
226:     if (ds->compact) { DSSwitchFormat_SVD(ds); }
227:     for (i=0;i<l;i++) wr[i] = d[i];
228:     nm = PetscMin(n,m);
229:     DSAllocateWork_Private(ds,0,0,8*nm);
230:     lwork = -1;
231: #if defined(PETSC_USE_COMPLEX)
232:     DSAllocateWork_Private(ds,0,5*nm*nm+7*nm,0);
233:     PetscStackCallBLAS("LAPACKgesdd",LAPACKgesdd_("A",&n3,&m3,A+off,&ld,d+l,U+off,&ld,VT+off,&ld,&qwork,&lwork,ds->rwork,ds->iwork,&info));
234: #else
235:     PetscStackCallBLAS("LAPACKgesdd",LAPACKgesdd_("A",&n3,&m3,A+off,&ld,d+l,U+off,&ld,VT+off,&ld,&qwork,&lwork,ds->iwork,&info));
236: #endif
237:     SlepcCheckLapackInfo("gesdd",info);
238:     PetscBLASIntCast((PetscInt)PetscRealPart(qwork),&lwork);
239:     DSAllocateWork_Private(ds,lwork,0,0);
240: #if defined(PETSC_USE_COMPLEX)
241:     PetscStackCallBLAS("LAPACKgesdd",LAPACKgesdd_("A",&n3,&m3,A+off,&ld,d+l,U+off,&ld,VT+off,&ld,ds->work,&lwork,ds->rwork,ds->iwork,&info));
242: #else
243:     PetscStackCallBLAS("LAPACKgesdd",LAPACKgesdd_("A",&n3,&m3,A+off,&ld,d+l,U+off,&ld,VT+off,&ld,ds->work,&lwork,ds->iwork,&info));
244: #endif
245:     SlepcCheckLapackInfo("gesdd",info);
246:   }
247:   for (i=l;i<PetscMin(ds->n,ds->m);i++) wr[i] = d[i];

249:   /* create diagonal matrix as a result */
250:   if (ds->compact) {
251:     PetscArrayzero(e,n-1);
252:   } else {
253:     for (i=l;i<n;i++) {
254:       PetscArrayzero(A+l+i*ld,n-l);
255:     }
256:     for (i=l;i<n;i++) A[i+i*ld] = d[i];
257:   }
258:   return(0);
259: }

261: PetscErrorCode DSSynchronize_SVD(DS ds,PetscScalar eigr[],PetscScalar eigi[])
262: {
264:   PetscInt       ld=ds->ld,l=ds->l,k=0,kr=0;
265:   PetscMPIInt    n,rank,off=0,size,ldn,ld3;

268:   if (ds->compact) kr = 3*ld;
269:   else k = (ds->n-l)*ld;
270:   if (ds->state>DS_STATE_RAW) k += 2*(ds->n-l)*ld;
271:   if (eigr) k += ds->n-l;
272:   DSAllocateWork_Private(ds,k+kr,0,0);
273:   PetscMPIIntCast(k*sizeof(PetscScalar)+kr*sizeof(PetscReal),&size);
274:   PetscMPIIntCast(ds->n-l,&n);
275:   PetscMPIIntCast(ld*(ds->n-l),&ldn);
276:   PetscMPIIntCast(3*ld,&ld3);
277:   MPI_Comm_rank(PetscObjectComm((PetscObject)ds),&rank);
278:   if (!rank) {
279:     if (ds->compact) {
280:       MPI_Pack(ds->rmat[DS_MAT_T],ld3,MPIU_REAL,ds->work,size,&off,PetscObjectComm((PetscObject)ds));
281:     } else {
282:       MPI_Pack(ds->mat[DS_MAT_A]+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));
283:     }
284:     if (ds->state>DS_STATE_RAW) {
285:       MPI_Pack(ds->mat[DS_MAT_U]+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));
286:       MPI_Pack(ds->mat[DS_MAT_VT]+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));
287:     }
288:     if (eigr) {
289:       MPI_Pack(eigr+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));
290:     }
291:   }
292:   MPI_Bcast(ds->work,size,MPI_BYTE,0,PetscObjectComm((PetscObject)ds));
293:   if (rank) {
294:     if (ds->compact) {
295:       MPI_Unpack(ds->work,size,&off,ds->rmat[DS_MAT_T],ld3,MPIU_REAL,PetscObjectComm((PetscObject)ds));
296:     } else {
297:       MPI_Unpack(ds->work,size,&off,ds->mat[DS_MAT_A]+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));
298:     }
299:     if (ds->state>DS_STATE_RAW) {
300:       MPI_Unpack(ds->work,size,&off,ds->mat[DS_MAT_U]+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));
301:       MPI_Unpack(ds->work,size,&off,ds->mat[DS_MAT_VT]+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));
302:     }
303:     if (eigr) {
304:       MPI_Unpack(ds->work,size,&off,eigr+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));
305:     }
306:   }
307:   return(0);
308: }

310: PetscErrorCode DSMatGetSize_SVD(DS ds,DSMatType t,PetscInt *rows,PetscInt *cols)
311: {
313:   switch (t) {
314:     case DS_MAT_A:
315:     case DS_MAT_T:
316:       *rows = ds->n;
317:       *cols = ds->m;
318:       break;
319:     case DS_MAT_U:
320:       *rows = ds->n;
321:       *cols = ds->n;
322:       break;
323:     case DS_MAT_VT:
324:       *rows = ds->m;
325:       *cols = ds->m;
326:       break;
327:     default:
328:       SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_OUTOFRANGE,"Invalid t parameter");
329:   }
330:   return(0);
331: }

333: SLEPC_EXTERN PetscErrorCode DSCreate_SVD(DS ds)
334: {
336:   ds->ops->allocate      = DSAllocate_SVD;
337:   ds->ops->view          = DSView_SVD;
338:   ds->ops->vectors       = DSVectors_SVD;
339:   ds->ops->solve[0]      = DSSolve_SVD_DC;
340:   ds->ops->sort          = DSSort_SVD;
341:   ds->ops->synchronize   = DSSynchronize_SVD;
342:   ds->ops->matgetsize    = DSMatGetSize_SVD;
343:   return(0);
344: }