Date: 2021-09-20T09:44:17+00:00

Actual source code: svdimpl.h
slepc-3.15.2 2021-09-20
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: #if !defined(SLEPCSVDIMPL_H)
     12: #define SLEPCSVDIMPL_H

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

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

     23: typedef struct _SVDOps *SVDOps;

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

     37: /*
     38:      Maximum number of monitors you can run with a single SVD
     39: */
     40: #define MAXSVDMONITORS 5

     42: typedef enum { SVD_STATE_INITIAL,
     43:                SVD_STATE_SETUP,
     44:                SVD_STATE_SOLVED,
     45:                SVD_STATE_VECTORS } SVDStateType;

     47: /*
     48:    To check for unsupported features at SVDSetUp_XXX()
     49: */
     50: typedef enum { SVD_FEATURE_CONVERGENCE=16,  /* convergence test selected by user */
     51:                SVD_FEATURE_STOPPING=32      /* stopping test */
     52:              } SVDFeatureType;

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

     74:   /*-------------- User-provided functions and contexts -----------------*/
     75:   PetscErrorCode (*converged)(SVD,PetscReal,PetscReal,PetscReal*,void*);
     76:   PetscErrorCode (*convergeduser)(SVD,PetscReal,PetscReal,PetscReal*,void*);
     77:   PetscErrorCode (*convergeddestroy)(void*);
     78:   PetscErrorCode (*stopping)(SVD,PetscInt,PetscInt,PetscInt,PetscInt,SVDConvergedReason*,void*);
     79:   PetscErrorCode (*stoppinguser)(SVD,PetscInt,PetscInt,PetscInt,PetscInt,SVDConvergedReason*,void*);
     80:   PetscErrorCode (*stoppingdestroy)(void*);
     81:   void           *convergedctx;
     82:   void           *stoppingctx;
     83:   PetscErrorCode (*monitor[MAXSVDMONITORS])(SVD,PetscInt,PetscInt,PetscReal*,PetscReal*,PetscInt,void*);
     84:   PetscErrorCode (*monitordestroy[MAXSVDMONITORS])(void**);
     85:   void           *monitorcontext[MAXSVDMONITORS];
     86:   PetscInt       numbermonitors;

     88:   /*----------------- Child objects and working data -------------------*/
     89:   DS             ds;               /* direct solver object */
     90:   BV             U,V;              /* left and right singular vectors */
     91:   SlepcSC        sc;               /* sorting criterion data */
     92:   Mat            A,B;              /* problem matrices */
     93:   Mat            AT,BT;            /* transposed matrices */
     94:   Vec            *IS,*ISL;         /* placeholder for references to user initial space */
     95:   PetscReal      *sigma;           /* singular values */
     96:   PetscReal      *errest;          /* error estimates */
     97:   PetscInt       *perm;            /* permutation for singular value ordering */
     98:   PetscInt       nworkl,nworkr;    /* number of work vectors */
     99:   Vec            *workl,*workr;    /* work vectors */
    100:   void           *data;            /* placeholder for solver-specific stuff */

    102:   /* ----------------------- Status variables -------------------------- */
    103:   SVDStateType   state;            /* initial -> setup -> solved -> vectors */
    104:   PetscInt       nconv;            /* number of converged values */
    105:   PetscInt       its;              /* iteration counter */
    106:   PetscBool      leftbasis;        /* if U is filled by the solver */
    107:   PetscBool      swapped;          /* the U and V bases have been swapped (M<N) */
    108:   PetscBool      expltrans;        /* explicit transpose created */
    109:   PetscBool      isgeneralized;
    110:   SVDConvergedReason reason;
    111: };

    113: /*
    114:     Macros to test valid SVD arguments
    115: */
    116: #if !defined(PETSC_USE_DEBUG)

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

    120: #else

    122: #define SVDCheckSolved(h,arg) \
    123:   do { \
    124:     if ((h)->state<SVD_STATE_SOLVED) SETERRQ1(PetscObjectComm((PetscObject)(h)),PETSC_ERR_ARG_WRONGSTATE,"Must call SVDSolve() first: Parameter #%d",arg); \
    125:   } while (0)

    127: #endif

    129: /*
    130:     Macros to check settings at SVDSetUp()
    131: */

    133: /* SVDCheckStandard: the problem is not GSVD */
    134: #define SVDCheckStandardCondition(svd,condition,msg) \
    135:   do { \
    136:     if (condition) { \
    137:       if ((svd)->isgeneralized) SETERRQ2(PetscObjectComm((PetscObject)(svd)),PETSC_ERR_SUP,"The solver '%s'%s cannot be used for generalized problems",((PetscObject)(svd))->type_name,(msg)); \
    138:     } \
    139:   } while (0)
    140: #define SVDCheckStandard(svd) SVDCheckStandardCondition(svd,PETSC_TRUE,"")

    142: /* Check for unsupported features */
    143: #define SVDCheckUnsupportedCondition(svd,mask,condition,msg) \
    144:   do { \
    145:     if (condition) { \
    146:       if (((mask) & SVD_FEATURE_CONVERGENCE) && (svd)->converged!=SVDConvergedRelative) SETERRQ2(PetscObjectComm((PetscObject)(svd)),PETSC_ERR_SUP,"The solver '%s'%s only supports the default convergence test",((PetscObject)(svd))->type_name,(msg)); \
    147:       if (((mask) & SVD_FEATURE_STOPPING) && (svd)->stopping!=SVDStoppingBasic) SETERRQ2(PetscObjectComm((PetscObject)(svd)),PETSC_ERR_SUP,"The solver '%s'%s only supports the default stopping test",((PetscObject)(svd))->type_name,(msg)); \
    148:     } \
    149:   } while (0)
    150: #define SVDCheckUnsupported(svd,mask) SVDCheckUnsupportedCondition(svd,mask,PETSC_TRUE,"")

    152: /* Check for ignored features */
    153: #define SVDCheckIgnoredCondition(svd,mask,condition,msg) \
    154:   do { \
    155:     PetscErrorCode __ierr; \
    156:     if (condition) { \
    157:       if (((mask) & SVD_FEATURE_CONVERGENCE) && (svd)->converged!=SVDConvergedRelative) { __PetscInfo2((svd),"The solver '%s'%s ignores the convergence test settings\n",((PetscObject)(svd))->type_name,(msg)); } \
    158:       if (((mask) & SVD_FEATURE_STOPPING) && (svd)->stopping!=SVDStoppingBasic) { __PetscInfo2((svd),"The solver '%s'%s ignores the stopping test settings\n",((PetscObject)(svd))->type_name,(msg)); } \
    159:     } \
    160:   } while (0)
    161: #define SVDCheckIgnored(svd,mask) SVDCheckIgnoredCondition(svd,mask,PETSC_TRUE,"")

    163: /*
    164:    Create the template vector for the left basis in GSVD, as in
    165:    MatCreateVecsEmpty(Z,NULL,&t) for Z=[A;B] without forming Z.
    166:  */
    167: PETSC_STATIC_INLINE PetscErrorCode SVDCreateLeftTemplate(SVD svd,Vec *t)
    168: {
    170:   PetscInt       M,P,m,p;
    171:   Vec            v1,v2;
    172:   VecType        vec_type;

    175:   MatCreateVecsEmpty(svd->OP,NULL,&v1);
    176:   VecGetSize(v1,&M);
    177:   VecGetLocalSize(v1,&m);
    178:   VecGetType(v1,&vec_type);
    179:   MatCreateVecsEmpty(svd->OPb,NULL,&v2);
    180:   VecGetSize(v2,&P);
    181:   VecGetLocalSize(v2,&p);
    182:   VecCreate(PetscObjectComm((PetscObject)(v1)),t);
    183:   VecSetType(*t,vec_type);
    184:   VecSetSizes(*t,m+p,M+P);
    185:   VecSetUp(*t);
    186:   VecDestroy(&v1);
    187:   VecDestroy(&v2);
    188:   return(0);
    189: }

    191: SLEPC_INTERN PetscErrorCode SVDTwoSideLanczos(SVD,PetscReal*,PetscReal*,BV,BV,PetscInt,PetscInt);
    192: SLEPC_INTERN PetscErrorCode SVDSetDimensions_Default(SVD);
    193: SLEPC_INTERN PetscErrorCode SVDComputeVectors(SVD);
    194: SLEPC_INTERN PetscErrorCode SVDComputeVectors_Left(SVD);

    196: #endif