Date: 2022-10-01T06:40:18+00:00

Actual source code: ex14.c
slepc-3.18.0 2022-10-01
Report Typos and Errors

      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: static char help[] = "Solves a singular value problem with the matrix loaded from a file.\n"
     12:   "This example works for both real and complex numbers.\n\n"
     13:   "The command line options are:\n"
     14:   "  -file <filename>, where <filename> = matrix file in PETSc binary form.\n\n";

     16: #include <slepcsvd.h>

     18: int main(int argc,char **argv)
     19: {
     20:   Mat            A;               /* operator matrix */
     21:   SVD            svd;             /* singular value problem solver context */
     22:   SVDType        type;
     23:   PetscReal      tol;
     24:   PetscInt       nsv,maxit,its;
     25:   char           filename[PETSC_MAX_PATH_LEN];
     26:   PetscViewer    viewer;
     27:   PetscBool      flg,terse;

     30:   SlepcInitialize(&argc,&argv,(char*)0,help);

     32:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     33:         Load the operator matrix that defines the singular value problem
     34:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     36:   PetscPrintf(PETSC_COMM_WORLD,"\nSingular value problem stored in file.\n\n");
     37:   PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg);

     40: #if defined(PETSC_USE_COMPLEX)
     41:   PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n");
     42: #else
     43:   PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n");
     44: #endif
     45:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
     46:   MatCreate(PETSC_COMM_WORLD,&A);
     47:   MatSetFromOptions(A);
     48:   MatLoad(A,viewer);
     49:   PetscViewerDestroy(&viewer);

     51:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     52:                 Create the singular value solver and set various options
     53:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     55:   /*
     56:      Create singular value solver context
     57:   */
     58:   SVDCreate(PETSC_COMM_WORLD,&svd);

     60:   /*
     61:      Set operator
     62:   */
     63:   SVDSetOperators(svd,A,NULL);

     65:   /*
     66:      Set solver parameters at runtime
     67:   */
     68:   SVDSetFromOptions(svd);

     70:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     71:                       Solve the singular value system
     72:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     74:   SVDSolve(svd);
     75:   SVDGetIterationNumber(svd,&its);
     76:   PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %" PetscInt_FMT "\n",its);

     78:   /*
     79:      Optional: Get some information from the solver and display it
     80:   */
     81:   SVDGetType(svd,&type);
     82:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
     83:   SVDGetDimensions(svd,&nsv,NULL,NULL);
     84:   PetscPrintf(PETSC_COMM_WORLD," Number of requested singular values: %" PetscInt_FMT "\n",nsv);
     85:   SVDGetTolerances(svd,&tol,&maxit);
     86:   PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%" PetscInt_FMT "\n",(double)tol,maxit);

     88:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     89:                     Display solution and clean up
     90:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     92:   /* show detailed info unless -terse option is given by user */
     93:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
     94:   if (terse) SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL);
     95:   else {
     96:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
     97:     SVDConvergedReasonView(svd,PETSC_VIEWER_STDOUT_WORLD);
     98:     SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
     99:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
    100:   }
    101:   SVDDestroy(&svd);
    102:   MatDestroy(&A);
    103:   SlepcFinalize();
    104:   return 0;
    105: }
    106: /*TEST

    108:    testset:
    109:       requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
    110:       args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc -terse
    111:       test:
    112:          suffix: 1
    113:          args: -svd_nsv 4 -svd_standard -svd_ncv 12 -svd_type {{trlanczos lanczos randomized cross}}
    114:          filter: grep -v method
    115:       test:
    116:          suffix: 1_scalapack
    117:          nsize: {{1 2 3}}
    118:          args: -svd_nsv 4 -svd_type scalapack
    119:          requires: scalapack
    120:       test:
    121:          suffix: 1_elemental
    122:          nsize: {{1 2 3}}
    123:          args: -svd_nsv 4 -svd_type elemental
    124:          requires: elemental
    125:       test:
    126:          suffix: 2
    127:          args: -svd_nsv 2 -svd_type cyclic -svd_cyclic_explicitmatrix -svd_cyclic_st_type sinvert -svd_cyclic_eps_target 12.5 -svd_cyclic_st_ksp_type preonly -svd_cyclic_st_pc_type lu -svd_view
    128:          filter: grep -v tolerance
    129:       test:
    130:          suffix: 2_cross
    131:          args: -svd_nsv 2 -svd_type cross -svd_cross_explicitmatrix -svd_cross_st_type sinvert -svd_cross_eps_target 100.0
    132:          filter: grep -v tolerance

    134:    testset:
    135:       requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
    136:       args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -terse
    137:       test:
    138:          suffix: 1_complex
    139:          args: -svd_nsv 4
    140:       test:
    141:          suffix: 1_complex_scalapack
    142:          nsize: {{1 2 3}}
    143:          args: -svd_nsv 4 -svd_type scalapack
    144:          requires: scalapack
    145:       test:
    146:          suffix: 1_complex_elemental
    147:          nsize: {{1 2 3}}
    148:          args: -svd_nsv 4 -svd_type elemental
    149:          requires: elemental
    150:       test:
    151:          suffix: 2_complex
    152:          args: -svd_nsv 2 -svd_type cyclic -svd_cyclic_explicitmatrix -svd_cyclic_st_type sinvert -svd_cyclic_eps_target 12.0 -svd_cyclic_st_ksp_type preonly -svd_cyclic_st_pc_type lu -svd_view
    153:          filter: grep -v tolerance

    155:    testset:
    156:       args: -svd_nsv 5 -svd_type randomized -svd_max_it 1 -svd_conv_maxit
    157:       test:
    158:          suffix: 3
    159:          args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc
    160:          requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
    161:       test:
    162:          suffix: 3_complex
    163:          args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc
    164:          requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
    165:          filter: sed -e 's/[0-9][0-9]$//'

    167: TEST*/