Actual source code: ex14.c

slepc-3.21.0 2024-03-30
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;

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

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

 36:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nSingular value problem stored in file.\n\n"));
 37:   PetscCall(PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg));
 38:   PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must indicate a file name with the -file option");

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

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

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

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

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

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

 74:   PetscCall(SVDSolve(svd));
 75:   PetscCall(SVDGetIterationNumber(svd,&its));
 76:   PetscCall(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:   PetscCall(SVDGetType(svd,&type));
 82:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type));
 83:   PetscCall(SVDGetDimensions(svd,&nsv,NULL,NULL));
 84:   PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested singular values: %" PetscInt_FMT "\n",nsv));
 85:   PetscCall(SVDGetTolerances(svd,&tol,&maxit));
 86:   PetscCall(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:   PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
 94:   if (terse) PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL));
 95:   else {
 96:     PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
 97:     PetscCall(SVDConvergedReasonView(svd,PETSC_VIEWER_STDOUT_WORLD));
 98:     PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
 99:     PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
100:   }
101:   PetscCall(SVDDestroy(&svd));
102:   PetscCall(MatDestroy(&A));
103:   PetscCall(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:          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

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

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]$//'

176: TEST*/