Actual source code: svdimpl.h

  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: #pragma once

 13: #include <slepcsvd.h>
 14: #include <slepc/private/slepcimpl.h>

 16: /* SUBMANSEC = SVD */

 18: SLEPC_EXTERN PetscBool SVDRegisterAllCalled;
 19: SLEPC_EXTERN PetscBool SVDMonitorRegisterAllCalled;
 20: SLEPC_EXTERN PetscErrorCode SVDRegisterAll(void);
 21: SLEPC_EXTERN PetscErrorCode SVDMonitorRegisterAll(void);
 22: SLEPC_EXTERN PetscLogEvent SVD_SetUp,SVD_Solve;

 24: typedef struct _SVDOps *SVDOps;

 26: struct _SVDOps {
 27:   PetscErrorCode (*solve)(SVD);
 28:   PetscErrorCode (*solveg)(SVD);
 29:   PetscErrorCode (*solveh)(SVD);
 30:   PetscErrorCode (*setup)(SVD);
 31:   PetscErrorCode (*setfromoptions)(SVD,PetscOptionItems);
 32:   PetscErrorCode (*publishoptions)(SVD);
 33:   PetscErrorCode (*destroy)(SVD);
 34:   PetscErrorCode (*reset)(SVD);
 35:   PetscErrorCode (*view)(SVD,PetscViewer);
 36:   PetscErrorCode (*computevectors)(SVD);
 37:   PetscErrorCode (*setdstype)(SVD);
 38: };

 40: /*
 41:      Maximum number of monitors you can run with a single SVD
 42: */
 43: #define MAXSVDMONITORS 5

 45: typedef enum { SVD_STATE_INITIAL,
 46:                SVD_STATE_SETUP,
 47:                SVD_STATE_SOLVED,
 48:                SVD_STATE_VECTORS } SVDStateType;

 50: /*
 51:    To check for unsupported features at SVDSetUp_XXX()
 52: */
 53: typedef enum { SVD_FEATURE_CONVERGENCE=16,  /* convergence test selected by user */
 54:                SVD_FEATURE_STOPPING=32,     /* stopping test */
 55:                SVD_FEATURE_THRESHOLD=64     /* threshold stopping test */
 56:              } SVDFeatureType;

 58: /*
 59:    Defines the SVD data structure.
 60: */
 61: struct _p_SVD {
 62:   PETSCHEADER(struct _SVDOps);
 63:   /*------------------------- User parameters ---------------------------*/
 64:   Mat            OP,OPb;           /* problem matrices */
 65:   Vec            omega;            /* signature for hyperbolic problems */
 66:   PetscInt       max_it;           /* max iterations */
 67:   PetscInt       nsv;              /* number of requested values */
 68:   PetscInt       ncv;              /* basis size */
 69:   PetscInt       mpd;              /* maximum dimension of projected problem */
 70:   PetscInt       nini,ninil;       /* number of initial vecs (negative means not copied yet) */
 71:   PetscReal      tol;              /* tolerance */
 72:   PetscReal      thres;            /* threshold value */
 73:   PetscBool      threlative;       /* threshold is relative */
 74:   SVDConv        conv;             /* convergence test */
 75:   SVDStop        stop;             /* stopping test */
 76:   SVDWhich       which;            /* which singular values are computed */
 77:   SVDProblemType problem_type;     /* which kind of problem to be solved */
 78:   PetscBool      impltrans;        /* implicit transpose mode */
 79:   PetscBool      trackall;         /* whether all the residuals must be computed */

 81:   /*-------------- User-provided functions and contexts -----------------*/
 82:   SVDConvergenceTestFn *converged;
 83:   SVDConvergenceTestFn *convergeduser;
 84:   PetscCtxDestroyFn    *convergeddestroy;
 85:   SVDStoppingTestFn    *stopping;
 86:   SVDStoppingTestFn    *stoppinguser;
 87:   PetscCtxDestroyFn    *stoppingdestroy;
 88:   void                 *convergedctx;
 89:   void                 *stoppingctx;
 90:   SVDMonitorFn         *monitor[MAXSVDMONITORS];
 91:   PetscCtxDestroyFn    *monitordestroy[MAXSVDMONITORS];
 92:   void                 *monitorcontext[MAXSVDMONITORS];
 93:   PetscInt             numbermonitors;

 95:   /*----------------- Child objects and working data -------------------*/
 96:   DS             ds;               /* direct solver object */
 97:   BV             U,V;              /* left and right singular vectors */
 98:   SlepcSC        sc;               /* sorting criterion data */
 99:   Mat            A,B;              /* problem matrices */
100:   Mat            AT,BT;            /* transposed matrices */
101:   Vec            *IS,*ISL;         /* placeholder for references to user initial space */
102:   PetscReal      *sigma;           /* singular values */
103:   PetscReal      *errest;          /* error estimates */
104:   PetscReal      *sign;            /* +-1 for each singular value in hyperbolic problems=U'*Omega*U */
105:   PetscInt       *perm;            /* permutation for singular value ordering */
106:   PetscInt       nworkl,nworkr;    /* number of work vectors */
107:   Vec            *workl,*workr;    /* work vectors */
108:   void           *data;            /* placeholder for solver-specific stuff */

110:   /* ----------------------- Status variables -------------------------- */
111:   SVDStateType   state;            /* initial -> setup -> solved -> vectors */
112:   PetscInt       nconv;            /* number of converged values */
113:   PetscInt       its;              /* iteration counter */
114:   PetscBool      leftbasis;        /* if U is filled by the solver */
115:   PetscBool      swapped;          /* the U and V bases have been swapped (M<N) */
116:   PetscBool      expltrans;        /* explicit transpose created */
117:   PetscReal      nrma,nrmb;        /* computed matrix norms */
118:   PetscBool      isgeneralized;
119:   PetscBool      ishyperbolic;
120:   PetscInt       setfromoptionscalled;
121:   SVDConvergedReason reason;
122: };

124: /*
125:     Macros to test valid SVD arguments
126: */
127: #if !PetscDefined(USE_DEBUG)

129: #define SVDCheckSolved(h,arg) do {(void)(h);} while (0)

131: #else

133: #define SVDCheckSolved(h,arg) \
134:   do { \
135:     PetscCheck((h)->state>=SVD_STATE_SOLVED,PetscObjectComm((PetscObject)(h)),PETSC_ERR_ARG_WRONGSTATE,"Must call SVDSolve() first: Parameter #%d",arg); \
136:   } while (0)

138: #endif

140: /*
141:     Macros to check settings at SVDSetUp()
142: */

144: /* SVDCheckStandard: the problem is not GSVD */
145: #define SVDCheckStandardCondition(svd,condition,msg) \
146:   do { \
147:     if (condition) { \
148:       PetscCheck(!(svd)->isgeneralized,PetscObjectComm((PetscObject)(svd)),PETSC_ERR_SUP,"The solver '%s'%s cannot be used for generalized problems",((PetscObject)(svd))->type_name,(msg)); \
149:     } \
150:   } while (0)
151: #define SVDCheckStandard(svd) SVDCheckStandardCondition(svd,PETSC_TRUE,"")

153: /* SVDCheckDefinite: the problem is not hyperbolic */
154: #define SVDCheckDefiniteCondition(svd,condition,msg) \
155:   do { \
156:     if (condition) { \
157:       PetscCheck(!(svd)->ishyperbolic,PetscObjectComm((PetscObject)(svd)),PETSC_ERR_SUP,"The solver '%s'%s cannot be used for hyperbolic problems",((PetscObject)(svd))->type_name,(msg)); \
158:     } \
159:   } while (0)
160: #define SVDCheckDefinite(svd) SVDCheckDefiniteCondition(svd,PETSC_TRUE,"")

162: /* Check for unsupported features */
163: #define SVDCheckUnsupportedCondition(svd,mask,condition,msg) \
164:   do { \
165:     if (condition) { \
166:       PetscCheck(!((mask) & SVD_FEATURE_CONVERGENCE) || (svd)->converged==SVDConvergedRelative,PetscObjectComm((PetscObject)(svd)),PETSC_ERR_SUP,"The solver '%s'%s only supports the default convergence test",((PetscObject)(svd))->type_name,(msg)); \
167:       PetscCheck(!((mask) & SVD_FEATURE_STOPPING) || (svd)->stopping==SVDStoppingBasic,PetscObjectComm((PetscObject)(svd)),PETSC_ERR_SUP,"The solver '%s'%s only supports the default stopping test",((PetscObject)(svd))->type_name,(msg)); \
168:       PetscCheck(!((mask) & SVD_FEATURE_THRESHOLD) || (svd)->stopping!=SVDStoppingThreshold,PetscObjectComm((PetscObject)(svd)),PETSC_ERR_SUP,"The solver '%s'%s does not support the threshold stopping test",((PetscObject)(svd))->type_name,(msg)); \
169:     } \
170:   } while (0)
171: #define SVDCheckUnsupported(svd,mask) SVDCheckUnsupportedCondition(svd,mask,PETSC_TRUE,"")

173: /* Check for ignored features */
174: #define SVDCheckIgnoredCondition(svd,mask,condition,msg) \
175:   do { \
176:     if (condition) { \
177:       if (((mask) & SVD_FEATURE_CONVERGENCE) && (svd)->converged!=SVDConvergedRelative) PetscCall(PetscInfo((svd),"The solver '%s'%s ignores the convergence test settings\n",((PetscObject)(svd))->type_name,(msg))); \
178:       if (((mask) & SVD_FEATURE_STOPPING) && (svd)->stopping!=SVDStoppingBasic) PetscCall(PetscInfo((svd),"The solver '%s'%s ignores the stopping test settings\n",((PetscObject)(svd))->type_name,(msg))); \
179:     } \
180:   } while (0)
181: #define SVDCheckIgnored(svd,mask) SVDCheckIgnoredCondition(svd,mask,PETSC_TRUE,"")

183: /*
184:     SVDSetCtxThreshold - Fills SVDStoppingCtx with data needed for the threshold stopping test

186:     k = number of converged approximations, n = total number of available approximations
187: */
188: #define SVDSetCtxThreshold(svd,sigma,err_est,k,n) \
189:   do { \
190:     if ((svd)->stop==SVD_STOP_THRESHOLD && (k)) { \
191:       PetscReal __krn=0.0; \
192:       ((SVDStoppingCtx)(svd)->stoppingctx)->firstsv = (sigma)[0]; \
193:       ((SVDStoppingCtx)(svd)->stoppingctx)->lastsv  = (sigma)[(k)-1]; \
194:       if (n>(k)) __krn=(sigma)[k]; \
195:       ((SVDStoppingCtx)(svd)->stoppingctx)->firstnc = __krn; \
196:       ((SVDStoppingCtx)(svd)->stoppingctx)->errest  = (err_est)[k]; \
197:       ((SVDStoppingCtx)(svd)->stoppingctx)->napprox = n; \
198:     } \
199:   } while (0)

201: /*
202:   SVD_KSPSetOperators - Sets the KSP matrices
203: */
204: static inline PetscErrorCode SVD_KSPSetOperators(KSP ksp,Mat A,Mat B)
205: {
206:   const char     *prefix;

208:   PetscFunctionBegin;
209:   PetscCall(KSPSetOperators(ksp,A,B));
210:   PetscCall(MatGetOptionsPrefix(B,&prefix));
211:   if (!prefix) {
212:     /* set Mat prefix to be the same as KSP to enable setting command-line options (e.g. MUMPS)
213:        only applies if the Mat has no user-defined prefix */
214:     PetscCall(KSPGetOptionsPrefix(ksp,&prefix));
215:     PetscCall(MatSetOptionsPrefix(B,prefix));
216:   }
217:   PetscFunctionReturn(PETSC_SUCCESS);
218: }

220: /*
221:    Create the template vector for the left basis in GSVD, as in
222:    MatCreateVecsEmpty(Z,NULL,&t) for Z=[A;B] without forming Z.
223:  */
224: static inline PetscErrorCode SVDCreateLeftTemplate(SVD svd,Vec *t)
225: {
226:   PetscInt       M,P,m,p;
227:   Vec            v1,v2;
228:   VecType        vec_type;

230:   PetscFunctionBegin;
231:   PetscCall(MatCreateVecsEmpty(svd->OP,NULL,&v1));
232:   PetscCall(VecGetSize(v1,&M));
233:   PetscCall(VecGetLocalSize(v1,&m));
234:   PetscCall(VecGetType(v1,&vec_type));
235:   PetscCall(MatCreateVecsEmpty(svd->OPb,NULL,&v2));
236:   PetscCall(VecGetSize(v2,&P));
237:   PetscCall(VecGetLocalSize(v2,&p));
238:   PetscCall(VecCreate(PetscObjectComm((PetscObject)(v1)),t));
239:   PetscCall(VecSetType(*t,vec_type));
240:   PetscCall(VecSetSizes(*t,m+p,M+P));
241:   PetscCall(VecSetUp(*t));
242:   PetscCall(VecDestroy(&v1));
243:   PetscCall(VecDestroy(&v2));
244:   PetscFunctionReturn(PETSC_SUCCESS);
245: }

247: SLEPC_INTERN PetscErrorCode SVDKrylovConvergence(SVD,PetscBool,PetscInt,PetscInt,PetscReal,PetscInt*);
248: SLEPC_INTERN PetscErrorCode SVDTwoSideLanczos(SVD,PetscReal*,PetscReal*,BV,BV,PetscInt,PetscInt*,PetscBool*);
249: SLEPC_INTERN PetscErrorCode SVDSetDimensions_Default(SVD);
250: SLEPC_INTERN PetscErrorCode SVDComputeVectors(SVD);
251: SLEPC_INTERN PetscErrorCode SVDComputeVectors_Left(SVD);