LCOV - code coverage report
Current view: top level - svd/tests - test4.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 59 59 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[] = "Test an SVD problem with more columns than rows.\n\n"
      12             :   "The command line options are:\n"
      13             :   "  -m <m>, where <m> = matrix rows.\n"
      14             :   "  -n <n>, where <n> = matrix columns (defaults to m+2).\n\n";
      15             : 
      16             : #include <slepcsvd.h>
      17             : 
      18             : /*
      19             :    This example computes the singular values of a rectangular bidiagonal matrix
      20             : 
      21             :               |  1  2                     |
      22             :               |     1  2                  |
      23             :               |        1  2               |
      24             :           A = |          .  .             |
      25             :               |             .  .          |
      26             :               |                1  2       |
      27             :               |                   1  2    |
      28             :  */
      29             : 
      30          12 : int main(int argc,char **argv)
      31             : {
      32          12 :   Mat                  A,B;
      33          12 :   SVD                  svd;
      34          12 :   SVDConv              conv;
      35          12 :   SVDStop              stop;
      36          12 :   SVDWhich             which;
      37          12 :   SVDProblemType       ptype;
      38          12 :   SVDConvergedReason   reason;
      39          12 :   PetscInt             m=20,n,Istart,Iend,i,col[2],its;
      40          12 :   PetscScalar          value[] = { 1, 2 };
      41          12 :   PetscBool            flg,tmode;
      42          12 :   PetscViewerAndFormat *vf;
      43          12 :   const char           *ctest[] = { "absolute", "relative to the singular value", "user-defined" };
      44          12 :   const char           *stest[] = { "basic", "user-defined" };
      45             : 
      46          12 :   PetscFunctionBeginUser;
      47          12 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      48             : 
      49          12 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
      50          12 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,&flg));
      51          12 :   if (!flg) n=m+2;
      52          12 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nRectangular bidiagonal matrix, m=%" PetscInt_FMT " n=%" PetscInt_FMT "\n\n",m,n));
      53             : 
      54             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      55             :         Generate the matrix
      56             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      57             : 
      58          12 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      59          12 :   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,n));
      60          12 :   PetscCall(MatSetFromOptions(A));
      61             : 
      62          12 :   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
      63         232 :   for (i=Istart;i<Iend;i++) {
      64         220 :     col[0]=i; col[1]=i+1;
      65         220 :     if (i<n-1) PetscCall(MatSetValues(A,1,&i,2,col,value,INSERT_VALUES));
      66         220 :     else if (i==n-1) PetscCall(MatSetValue(A,i,col[0],value[0],INSERT_VALUES));
      67             :   }
      68             : 
      69          12 :   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
      70          12 :   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
      71             : 
      72             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      73             :         Compute singular values
      74             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      75             : 
      76          12 :   PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
      77          12 :   PetscCall(SVDSetOperators(svd,A,NULL));
      78             : 
      79             :   /* test some interface functions */
      80          12 :   PetscCall(SVDGetOperators(svd,&B,NULL));
      81          12 :   PetscCall(MatView(B,PETSC_VIEWER_STDOUT_WORLD));
      82          12 :   PetscCall(SVDSetConvergenceTest(svd,SVD_CONV_ABS));
      83          12 :   PetscCall(SVDSetStoppingTest(svd,SVD_STOP_BASIC));
      84             :   /* test monitors */
      85          12 :   PetscCall(PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf));
      86          12 :   PetscCall(SVDMonitorSet(svd,(PetscErrorCode (*)(SVD,PetscInt,PetscInt,PetscReal*,PetscReal*,PetscInt,void*))SVDMonitorFirst,vf,(PetscCtxDestroyFn*)PetscViewerAndFormatDestroy));
      87             :   /* PetscCall(SVDMonitorCancel(svd)); */
      88          12 :   PetscCall(SVDSetFromOptions(svd));
      89             : 
      90             :   /* query properties and print them */
      91          12 :   PetscCall(SVDGetProblemType(svd,&ptype));
      92          12 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Problem type = %d",(int)ptype));
      93          12 :   PetscCall(SVDIsGeneralized(svd,&flg));
      94          12 :   if (flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD," generalized"));
      95          12 :   PetscCall(SVDGetImplicitTranspose(svd,&tmode));
      96          22 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n Transpose mode is %s\n",tmode?"implicit":"explicit"));
      97          12 :   PetscCall(SVDGetConvergenceTest(svd,&conv));
      98          12 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Convergence test is %s\n",ctest[conv]));
      99          12 :   PetscCall(SVDGetStoppingTest(svd,&stop));
     100          12 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Stopping test is %s\n",stest[stop]));
     101          12 :   PetscCall(SVDGetWhichSingularTriplets(svd,&which));
     102          24 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Which = %s\n",which?"smallest":"largest"));
     103             : 
     104             :   /* call the solver */
     105          12 :   PetscCall(SVDSolve(svd));
     106          12 :   PetscCall(SVDGetConvergedReason(svd,&reason));
     107          12 :   PetscCall(SVDGetIterationNumber(svd,&its));
     108          12 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Finished - converged reason = %d\n",(int)reason));
     109             :   /* PetscCall(PetscPrintf(PETSC_COMM_WORLD," its = %" PetscInt_FMT "\n",its)); */
     110             : 
     111             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     112             :                     Display solution and clean up
     113             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     114          12 :   PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
     115          12 :   PetscCall(SVDDestroy(&svd));
     116          12 :   PetscCall(MatDestroy(&A));
     117          12 :   PetscCall(SlepcFinalize());
     118             :   return 0;
     119             : }
     120             : 
     121             : /*TEST
     122             : 
     123             :    testset:
     124             :       args: -svd_monitor_cancel
     125             :       filter: grep -v "Transpose mode"
     126             :       output_file: output/test4_1.out
     127             :       test:
     128             :          suffix: 1_lanczos
     129             :          args: -svd_type lanczos
     130             :       test:
     131             :          suffix: 1_randomized
     132             :          args: -svd_type randomized
     133             :       test:
     134             :          suffix: 1_trlanczos
     135             :          args: -svd_type trlanczos -svd_ncv 12 -svd_trlanczos_restart 0.6
     136             :       test:
     137             :          suffix: 1_cross
     138             :          args: -svd_type cross
     139             :       test:
     140             :          suffix: 1_cross_exp
     141             :          args: -svd_type cross -svd_cross_explicitmatrix
     142             :       test:
     143             :          suffix: 1_cross_exp_imp
     144             :          args: -svd_type cross -svd_cross_explicitmatrix -svd_implicittranspose
     145             :          requires: !complex
     146             :       test:
     147             :          suffix: 1_cyclic
     148             :          args: -svd_type cyclic
     149             :       test:
     150             :          suffix: 1_cyclic_imp
     151             :          args: -svd_type cyclic -svd_implicittranspose
     152             :       test:
     153             :          suffix: 1_cyclic_exp
     154             :          args: -svd_type cyclic -svd_cyclic_explicitmatrix
     155             :       test:
     156             :          suffix: 1_lapack
     157             :          args: -svd_type lapack
     158             :       test:
     159             :          suffix: 1_scalapack
     160             :          args: -svd_type scalapack
     161             :          requires: scalapack
     162             : 
     163             :    testset:
     164             :       args: -svd_monitor_cancel -mat_type aijcusparse
     165             :       requires: cuda !single
     166             :       filter: grep -v "Transpose mode" | sed -e "s/seqaijcusparse/seqaij/"
     167             :       output_file: output/test4_1.out
     168             :       test:
     169             :          suffix: 2_cuda_lanczos
     170             :          args: -svd_type lanczos
     171             :       test:
     172             :          suffix: 2_cuda_trlanczos
     173             :          args: -svd_type trlanczos -svd_ncv 12
     174             :       test:
     175             :          suffix: 2_cuda_cross
     176             :          args: -svd_type cross
     177             : 
     178             :    test:
     179             :       suffix: 3
     180             :       nsize: 2
     181             :       args: -svd_type trlanczos -svd_ncv 14 -svd_monitor_cancel -ds_parallel synchronized
     182             : 
     183             :    testset:
     184             :       args: -svd_monitor_cancel -mat_type aijhipsparse
     185             :       requires: hip !single
     186             :       filter: grep -v "Transpose mode" | sed -e "s/seqaijhipsparse/seqaij/"
     187             :       output_file: output/test4_1.out
     188             :       test:
     189             :          suffix: 4_hip_lanczos
     190             :          args: -svd_type lanczos
     191             :       test:
     192             :          suffix: 4_hip_trlanczos
     193             :          args: -svd_type trlanczos -svd_ncv 12
     194             :       test:
     195             :          suffix: 4_hip_cross
     196             :          args: -svd_type cross
     197             : 
     198             : TEST*/

Generated by: LCOV version 1.14