LCOV - code coverage report
Current view: top level - svd/tutorials - ex14.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 41 41 100.0 %
Date: 2024-11-21 00:34:55 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           7 : int main(int argc,char **argv)
      19             : {
      20           7 :   Mat            A;               /* operator matrix */
      21           7 :   SVD            svd;             /* singular value problem solver context */
      22           7 :   SVDType        type;
      23           7 :   PetscReal      tol;
      24           7 :   PetscInt       nsv,maxit,its;
      25           7 :   char           filename[PETSC_MAX_PATH_LEN];
      26           7 :   PetscViewer    viewer;
      27           7 :   PetscBool      flg,terse;
      28             : 
      29           7 :   PetscFunctionBeginUser;
      30           7 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      31             : 
      32             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      33             :         Load the operator matrix that defines the singular value problem
      34             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      35             : 
      36           7 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nSingular value problem stored in file.\n\n"));
      37           7 :   PetscCall(PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg));
      38           7 :   PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must indicate a file name with the -file option");
      39             : 
      40             : #if defined(PETSC_USE_COMPLEX)
      41             :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n"));
      42             : #else
      43           7 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n"));
      44             : #endif
      45           7 :   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
      46           7 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      47           7 :   PetscCall(MatSetFromOptions(A));
      48           7 :   PetscCall(MatLoad(A,viewer));
      49           7 :   PetscCall(PetscViewerDestroy(&viewer));
      50             : 
      51             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      52             :                 Create the singular value solver and set various options
      53             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      54             : 
      55             :   /*
      56             :      Create singular value solver context
      57             :   */
      58           7 :   PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
      59             : 
      60             :   /*
      61             :      Set operator
      62             :   */
      63           7 :   PetscCall(SVDSetOperators(svd,A,NULL));
      64             : 
      65             :   /*
      66             :      Set solver parameters at runtime
      67             :   */
      68           7 :   PetscCall(SVDSetFromOptions(svd));
      69             : 
      70             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      71             :                       Solve the singular value system
      72             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      73             : 
      74           7 :   PetscCall(SVDSolve(svd));
      75           7 :   PetscCall(SVDGetIterationNumber(svd,&its));
      76           7 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %" PetscInt_FMT "\n",its));
      77             : 
      78             :   /*
      79             :      Optional: Get some information from the solver and display it
      80             :   */
      81           7 :   PetscCall(SVDGetType(svd,&type));
      82           7 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type));
      83           7 :   PetscCall(SVDGetDimensions(svd,&nsv,NULL,NULL));
      84           7 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested singular values: %" PetscInt_FMT "\n",nsv));
      85           7 :   PetscCall(SVDGetTolerances(svd,&tol,&maxit));
      86           7 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%" PetscInt_FMT "\n",(double)tol,maxit));
      87             : 
      88             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      89             :                     Display solution and clean up
      90             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      91             : 
      92             :   /* show detailed info unless -terse option is given by user */
      93           7 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
      94           7 :   if (terse) PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL));
      95             :   else {
      96           1 :     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
      97           1 :     PetscCall(SVDConvergedReasonView(svd,PETSC_VIEWER_STDOUT_WORLD));
      98           1 :     PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
      99           1 :     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
     100             :   }
     101           7 :   PetscCall(SVDDestroy(&svd));
     102           7 :   PetscCall(MatDestroy(&A));
     103           7 :   PetscCall(SlepcFinalize());
     104             :   return 0;
     105             : }
     106             : /*TEST
     107             : 
     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             :          output_file: output/ex14_1_scalapack.out
     125             :          filter: sed -e "s/elemental/scalapack/"
     126             :          requires: elemental
     127             :       test:
     128             :          suffix: 1_ksvd
     129             :          nsize: 6
     130             :          args: -svd_nsv 4 -svd_type ksvd -svd_ksvd_eigen_method {{mrrr elpa}} -svd_ksvd_polar_method {{qdwh zolopd}}
     131             :          output_file: output/ex14_1_scalapack.out
     132             :          filter: sed -e "s/ksvd/scalapack/"
     133             :          requires: elpa polar ksvd
     134             :       test:
     135             :          suffix: 2
     136             :          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
     137             :          filter: grep -v tolerance
     138             :       test:
     139             :          suffix: 2_cross
     140             :          args: -svd_nsv 2 -svd_type cross -svd_cross_explicitmatrix -svd_cross_st_type sinvert -svd_cross_eps_target 100.0
     141             :          filter: grep -v tolerance
     142             : 
     143             :    testset:
     144             :       requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
     145             :       args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -terse
     146             :       test:
     147             :          suffix: 1_complex
     148             :          args: -svd_nsv 4
     149             :       test:
     150             :          suffix: 1_complex_scalapack
     151             :          nsize: {{1 2 3}}
     152             :          args: -svd_nsv 4 -svd_type scalapack
     153             :          requires: scalapack
     154             :       test:
     155             :          suffix: 1_complex_elemental
     156             :          nsize: {{1 2 3}}
     157             :          args: -svd_nsv 4 -svd_type elemental
     158             :          requires: elemental
     159             :       test:
     160             :          suffix: 2_complex
     161             :          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
     162             :          filter: grep -v tolerance
     163             : 
     164             :    testset:
     165             :       args: -svd_nsv 5 -svd_type randomized -svd_max_it 1 -svd_conv_maxit
     166             :       test:
     167             :          suffix: 3
     168             :          args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc
     169             :          requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
     170             :       test:
     171             :          suffix: 3_complex
     172             :          args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc
     173             :          requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
     174             :          filter: sed -e 's/[0-9][0-9]$//'
     175             : 
     176             : TEST*/

Generated by: LCOV version 1.14