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-12-18 00:51:33 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          11 : int main(int argc,char **argv)
      31             : {
      32          11 :   Mat                  A,B;
      33          11 :   SVD                  svd;
      34          11 :   SVDConv              conv;
      35          11 :   SVDStop              stop;
      36          11 :   SVDWhich             which;
      37          11 :   SVDProblemType       ptype;
      38          11 :   SVDConvergedReason   reason;
      39          11 :   PetscInt             m=20,n,Istart,Iend,i,col[2],its;
      40          11 :   PetscScalar          value[] = { 1, 2 };
      41          11 :   PetscBool            flg,tmode;
      42          11 :   PetscViewerAndFormat *vf;
      43          11 :   const char           *ctest[] = { "absolute", "relative to the singular value", "user-defined" };
      44          11 :   const char           *stest[] = { "basic", "user-defined" };
      45             : 
      46          11 :   PetscFunctionBeginUser;
      47          11 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      48             : 
      49          11 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
      50          11 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,&flg));
      51          11 :   if (!flg) n=m+2;
      52          11 :   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          11 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      59          11 :   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,n));
      60          11 :   PetscCall(MatSetFromOptions(A));
      61             : 
      62          11 :   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
      63         211 :   for (i=Istart;i<Iend;i++) {
      64         200 :     col[0]=i; col[1]=i+1;
      65         200 :     if (i<n-1) PetscCall(MatSetValues(A,1,&i,2,col,value,INSERT_VALUES));
      66         200 :     else if (i==n-1) PetscCall(MatSetValue(A,i,col[0],value[0],INSERT_VALUES));
      67             :   }
      68             : 
      69          11 :   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
      70          11 :   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
      71             : 
      72             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      73             :         Compute singular values
      74             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      75             : 
      76          11 :   PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
      77          11 :   PetscCall(SVDSetOperators(svd,A,NULL));
      78             : 
      79             :   /* test some interface functions */
      80          11 :   PetscCall(SVDGetOperators(svd,&B,NULL));
      81          11 :   PetscCall(MatView(B,PETSC_VIEWER_STDOUT_WORLD));
      82          11 :   PetscCall(SVDSetConvergenceTest(svd,SVD_CONV_ABS));
      83          11 :   PetscCall(SVDSetStoppingTest(svd,SVD_STOP_BASIC));
      84             :   /* test monitors */
      85          11 :   PetscCall(PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf));
      86          11 :   PetscCall(SVDMonitorSet(svd,(PetscErrorCode (*)(SVD,PetscInt,PetscInt,PetscReal*,PetscReal*,PetscInt,void*))SVDMonitorFirst,vf,(PetscCtxDestroyFn*)PetscViewerAndFormatDestroy));
      87             :   /* PetscCall(SVDMonitorCancel(svd)); */
      88          11 :   PetscCall(SVDSetFromOptions(svd));
      89             : 
      90             :   /* query properties and print them */
      91          11 :   PetscCall(SVDGetProblemType(svd,&ptype));
      92          11 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Problem type = %d",(int)ptype));
      93          11 :   PetscCall(SVDIsGeneralized(svd,&flg));
      94          11 :   if (flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD," generalized"));
      95          11 :   PetscCall(SVDGetImplicitTranspose(svd,&tmode));
      96          21 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n Transpose mode is %s\n",tmode?"implicit":"explicit"));
      97          11 :   PetscCall(SVDGetConvergenceTest(svd,&conv));
      98          11 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Convergence test is %s\n",ctest[conv]));
      99          11 :   PetscCall(SVDGetStoppingTest(svd,&stop));
     100          11 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Stopping test is %s\n",stest[stop]));
     101          11 :   PetscCall(SVDGetWhichSingularTriplets(svd,&which));
     102          22 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Which = %s\n",which?"smallest":"largest"));
     103             : 
     104             :   /* call the solver */
     105          11 :   PetscCall(SVDSolve(svd));
     106          11 :   PetscCall(SVDGetConvergedReason(svd,&reason));
     107          11 :   PetscCall(SVDGetIterationNumber(svd,&its));
     108          11 :   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          11 :   PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
     115          11 :   PetscCall(SVDDestroy(&svd));
     116          11 :   PetscCall(MatDestroy(&A));
     117          11 :   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