LCOV - code coverage report
Current view: top level - svd/tutorials - ex14.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 46 46 100.0 %
Date: 2024-12-04 00:44:48 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
       5             : 
       6             :    This file is part of SLEPc.
       7             :    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
       8             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       9             : */
      10             : 
      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";
      15             : 
      16             : #include <slepcsvd.h>
      17             : 
      18           9 : int main(int argc,char **argv)
      19             : {
      20           9 :   Mat            A;               /* operator matrix */
      21           9 :   SVD            svd;             /* singular value problem solver context */
      22           9 :   SVDType        type;
      23           9 :   SVDStop        stop;
      24           9 :   PetscReal      tol,thres;
      25           9 :   PetscInt       nsv,maxit,its;
      26           9 :   char           filename[PETSC_MAX_PATH_LEN];
      27           9 :   PetscViewer    viewer;
      28           9 :   PetscBool      flg,terse;
      29             : 
      30           9 :   PetscFunctionBeginUser;
      31           9 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      32             : 
      33             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      34             :         Load the operator matrix that defines the singular value problem
      35             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      36             : 
      37           9 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nSingular value problem stored in file.\n\n"));
      38           9 :   PetscCall(PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg));
      39           9 :   PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must indicate a file name with the -file option");
      40             : 
      41             : #if defined(PETSC_USE_COMPLEX)
      42           9 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n"));
      43             : #else
      44             :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n"));
      45             : #endif
      46           9 :   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
      47           9 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      48           9 :   PetscCall(MatSetFromOptions(A));
      49           9 :   PetscCall(MatLoad(A,viewer));
      50           9 :   PetscCall(PetscViewerDestroy(&viewer));
      51             : 
      52             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      53             :                 Create the singular value solver and set various options
      54             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      55             : 
      56             :   /*
      57             :      Create singular value solver context
      58             :   */
      59           9 :   PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
      60             : 
      61             :   /*
      62             :      Set operator
      63             :   */
      64           9 :   PetscCall(SVDSetOperators(svd,A,NULL));
      65             : 
      66             :   /*
      67             :      Set solver parameters at runtime
      68             :   */
      69           9 :   PetscCall(SVDSetFromOptions(svd));
      70             : 
      71             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      72             :                       Solve the singular value system
      73             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      74             : 
      75           9 :   PetscCall(SVDSolve(svd));
      76           9 :   PetscCall(SVDGetIterationNumber(svd,&its));
      77           9 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %" PetscInt_FMT "\n",its));
      78             : 
      79             :   /*
      80             :      Optional: Get some information from the solver and display it
      81             :   */
      82           9 :   PetscCall(SVDGetType(svd,&type));
      83           9 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type));
      84           9 :   PetscCall(SVDGetStoppingTest(svd,&stop));
      85           9 :   if (stop!=SVD_STOP_THRESHOLD) {
      86           3 :     PetscCall(SVDGetDimensions(svd,&nsv,NULL,NULL));
      87           3 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested singular values: %" PetscInt_FMT "\n",nsv));
      88             :   } else {
      89           6 :     PetscCall(SVDGetThreshold(svd,&thres,NULL));
      90           6 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD," Using threshold: %.4g\n",(double)thres));
      91             :   }
      92           9 :   PetscCall(SVDGetTolerances(svd,&tol,&maxit));
      93           9 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%" PetscInt_FMT "\n",(double)tol,maxit));
      94             : 
      95             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      96             :                     Display solution and clean up
      97             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      98             : 
      99             :   /* show detailed info unless -terse option is given by user */
     100           9 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
     101           9 :   if (terse) PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL));
     102             :   else {
     103           1 :     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
     104           1 :     PetscCall(SVDConvergedReasonView(svd,PETSC_VIEWER_STDOUT_WORLD));
     105           1 :     PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
     106           1 :     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
     107             :   }
     108           9 :   PetscCall(SVDDestroy(&svd));
     109           9 :   PetscCall(MatDestroy(&A));
     110           9 :   PetscCall(SlepcFinalize());
     111             :   return 0;
     112             : }
     113             : /*TEST
     114             : 
     115             :    testset:
     116             :       requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
     117             :       args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc -terse
     118             :       test:
     119             :          suffix: 1
     120             :          args: -svd_nsv 4 -svd_standard -svd_ncv 12 -svd_type {{trlanczos lanczos randomized cross}}
     121             :          filter: grep -v method
     122             :       test:
     123             :          suffix: 1_scalapack
     124             :          nsize: {{1 2 3}}
     125             :          args: -svd_nsv 4 -svd_type scalapack
     126             :          requires: scalapack
     127             :       test:
     128             :          suffix: 1_elemental
     129             :          nsize: {{1 2 3}}
     130             :          args: -svd_nsv 4 -svd_type elemental
     131             :          output_file: output/ex14_1_scalapack.out
     132             :          filter: sed -e "s/elemental/scalapack/"
     133             :          requires: elemental
     134             :       test:
     135             :          suffix: 1_ksvd
     136             :          nsize: 6
     137             :          args: -svd_nsv 4 -svd_type ksvd -svd_ksvd_eigen_method {{mrrr elpa}} -svd_ksvd_polar_method {{qdwh zolopd}}
     138             :          output_file: output/ex14_1_scalapack.out
     139             :          filter: sed -e "s/ksvd/scalapack/"
     140             :          requires: elpa polar ksvd
     141             :       test:
     142             :          suffix: 2
     143             :          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
     144             :          filter: grep -v tolerance
     145             :       test:
     146             :          suffix: 2_cross
     147             :          args: -svd_nsv 2 -svd_type cross -svd_cross_explicitmatrix -svd_cross_st_type sinvert -svd_cross_eps_target 100.0
     148             :          filter: grep -v tolerance
     149             : 
     150             :    testset:
     151             :       requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
     152             :       args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -terse
     153             :       test:
     154             :          suffix: 1_complex
     155             :          args: -svd_nsv 4
     156             :       test:
     157             :          suffix: 1_complex_scalapack
     158             :          nsize: {{1 2 3}}
     159             :          args: -svd_nsv 4 -svd_type scalapack
     160             :          requires: scalapack
     161             :       test:
     162             :          suffix: 1_complex_elemental
     163             :          nsize: {{1 2 3}}
     164             :          args: -svd_nsv 4 -svd_type elemental
     165             :          requires: elemental
     166             :       test:
     167             :          suffix: 2_complex
     168             :          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
     169             :          filter: grep -v tolerance
     170             : 
     171             :    testset:
     172             :       args: -svd_nsv 5 -svd_type randomized -svd_max_it 1 -svd_conv_maxit
     173             :       test:
     174             :          suffix: 3
     175             :          args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc
     176             :          requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
     177             :       test:
     178             :          suffix: 3_complex
     179             :          args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc
     180             :          requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
     181             :          filter: sed -e 's/[0-9][0-9]$//'
     182             : 
     183             :    testset:
     184             :       args: -svd_type {{trlanczos lanczos cross}} -terse
     185             :       filter: grep -v method
     186             :       test:
     187             :          suffix: 4
     188             :          args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc -svd_threshold_relative 0.9 -svd_ncv {{26 12}}
     189             :          requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
     190             :       test:
     191             :          suffix: 4_complex
     192             :          args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -svd_threshold_relative 0.6 -svd_ncv {{18 10}}
     193             :          requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
     194             : 
     195             : TEST*/

Generated by: LCOV version 1.14